xref: /netbsd/lib/libc/compat-43/sigcompat.c (revision eb7c1594)
1*eb7c1594Sagc /*	$NetBSD: sigcompat.c,v 1.11 2003/08/07 16:42:40 agc Exp $	*/
25f34d8e3Schristos 
361f28255Scgd /*
49f6c3e59Sperry  * Copyright (c) 1989, 1993
59f6c3e59Sperry  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15*eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
325f34d8e3Schristos #include <sys/cdefs.h>
3361f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
345f34d8e3Schristos #if 0
359f6c3e59Sperry static char sccsid[] = "@(#)sigcompat.c	8.1 (Berkeley) 6/2/93";
365f34d8e3Schristos #else
37*eb7c1594Sagc __RCSID("$NetBSD: sigcompat.c,v 1.11 2003/08/07 16:42:40 agc Exp $");
385f34d8e3Schristos #endif
3961f28255Scgd #endif /* LIBC_SCCS and not lint */
4061f28255Scgd 
4161f28255Scgd #include <sys/param.h>
4261f28255Scgd #include <signal.h>
4361f28255Scgd 
44fe2f618cSchristos static __inline void sv2sa __P((struct sigaction *, const struct sigvec *));
45fe2f618cSchristos static __inline void sa2sv __P((struct sigvec *, const struct sigaction *));
46fe2f618cSchristos 
47fe2f618cSchristos static __inline void
48fe2f618cSchristos sv2sa(sa, sv)
49fe2f618cSchristos 	struct sigaction *sa;
50fe2f618cSchristos 	const struct sigvec *sv;
51fe2f618cSchristos {
52fe2f618cSchristos 	sigemptyset(&sa->sa_mask);
53fe2f618cSchristos 	sa->sa_mask.__bits[0] = sv->sv_mask;
54fe2f618cSchristos 	sa->sa_handler = sv->sv_handler;
55fe2f618cSchristos 	sa->sa_flags = sv->sv_flags ^ SV_INTERRUPT; /* !SA_INTERRUPT */
56fe2f618cSchristos }
57fe2f618cSchristos 
58fe2f618cSchristos static __inline void
59fe2f618cSchristos sa2sv(sv, sa)
60fe2f618cSchristos 	struct sigvec *sv;
61fe2f618cSchristos 	const struct sigaction *sa;
62fe2f618cSchristos {
63fe2f618cSchristos 	sv->sv_mask = sa->sa_mask.__bits[0];
64fe2f618cSchristos 	sv->sv_handler = sa->sa_handler;
65fe2f618cSchristos 	sv->sv_flags = sa->sa_flags ^ SV_INTERRUPT; /* !SA_INTERRUPT */
66fe2f618cSchristos }
67fe2f618cSchristos 
68a054c658Sjtc int
69fe2f618cSchristos sigvec(signo, nsv, osv)
7061f28255Scgd 	int signo;
71fe2f618cSchristos 	struct sigvec *nsv, *osv;
7261f28255Scgd {
7361f28255Scgd 	int ret;
74fe2f618cSchristos 	struct sigaction osa, nsa;
7561f28255Scgd 
76fe2f618cSchristos 	if (nsv)
77fe2f618cSchristos 		sv2sa(&nsa, nsv);
78fe2f618cSchristos 
79fe2f618cSchristos 	ret = sigaction(signo, nsv ? &nsa : NULL, osv ? &osa : NULL);
80fe2f618cSchristos 
8161f28255Scgd 	if (ret == 0 && osv)
82fe2f618cSchristos 		sa2sv(osv, &osa);
83fe2f618cSchristos 
8461f28255Scgd 	return (ret);
8561f28255Scgd }
8661f28255Scgd 
87a054c658Sjtc int
8861f28255Scgd sigsetmask(mask)
8961f28255Scgd 	int mask;
9061f28255Scgd {
9151327000Schristos 	sigset_t nmask, omask;
9251327000Schristos 	int n;
9361f28255Scgd 
9451327000Schristos 	sigemptyset(&nmask);
9551327000Schristos 	nmask.__bits[0] = mask;
9651327000Schristos 
9751327000Schristos 	n = sigprocmask(SIG_SETMASK, &nmask, &omask);
9861f28255Scgd 	if (n)
9961f28255Scgd 		return (n);
10051327000Schristos 	return (omask.__bits[0]);
10161f28255Scgd }
10261f28255Scgd 
103a054c658Sjtc int
10461f28255Scgd sigblock(mask)
10561f28255Scgd 	int mask;
10661f28255Scgd {
10751327000Schristos 	sigset_t nmask, omask;
10851327000Schristos 	int n;
10961f28255Scgd 
11051327000Schristos 	sigemptyset(&nmask);
11151327000Schristos 	nmask.__bits[0] = mask;
11251327000Schristos 
11351327000Schristos 	n = sigprocmask(SIG_BLOCK, &nmask, &omask);
11461f28255Scgd 	if (n)
11561f28255Scgd 		return (n);
11651327000Schristos 	return (omask.__bits[0]);
11761f28255Scgd }
11861f28255Scgd 
119a054c658Sjtc int
12061f28255Scgd sigpause(mask)
12161f28255Scgd 	int mask;
12261f28255Scgd {
12351327000Schristos 	sigset_t nmask;
12451327000Schristos 
12551327000Schristos 	sigemptyset(&nmask);
12651327000Schristos 	nmask.__bits[0] = mask;
12751327000Schristos 	return (sigsuspend(&nmask));
12861f28255Scgd }
129