xref: /netbsd/lib/libc/compat-43/sigcompat.c (revision 4e11af46)
1*4e11af46Sperry /*	$NetBSD: sigcompat.c,v 1.13 2005/12/24 21:11:16 perry 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.
15eb7c1594Sagc  * 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*4e11af46Sperry __RCSID("$NetBSD: sigcompat.c,v 1.13 2005/12/24 21:11:16 perry Exp $");
385f34d8e3Schristos #endif
3961f28255Scgd #endif /* LIBC_SCCS and not lint */
4061f28255Scgd 
4161f28255Scgd #include <sys/param.h>
4261f28255Scgd #include <signal.h>
435b84b398Schristos #include <compat/sys/signal.h>
4461f28255Scgd 
45*4e11af46Sperry static inline void sv2sa(struct sigaction *, const struct sigvec *);
46*4e11af46Sperry static inline void sa2sv(struct sigvec *, const struct sigaction *);
47fe2f618cSchristos 
48*4e11af46Sperry static inline void
sv2sa(struct sigaction * sa,const struct sigvec * sv)495b84b398Schristos sv2sa(struct sigaction *sa, const struct sigvec *sv)
50fe2f618cSchristos {
51fe2f618cSchristos 	sigemptyset(&sa->sa_mask);
52fe2f618cSchristos 	sa->sa_mask.__bits[0] = sv->sv_mask;
53fe2f618cSchristos 	sa->sa_handler = sv->sv_handler;
54fe2f618cSchristos 	sa->sa_flags = sv->sv_flags ^ SV_INTERRUPT; /* !SA_INTERRUPT */
55fe2f618cSchristos }
56fe2f618cSchristos 
57*4e11af46Sperry static inline void
sa2sv(struct sigvec * sv,const struct sigaction * sa)585b84b398Schristos sa2sv(struct sigvec *sv, const struct sigaction *sa)
59fe2f618cSchristos {
60fe2f618cSchristos 	sv->sv_mask = sa->sa_mask.__bits[0];
61fe2f618cSchristos 	sv->sv_handler = sa->sa_handler;
62fe2f618cSchristos 	sv->sv_flags = sa->sa_flags ^ SV_INTERRUPT; /* !SA_INTERRUPT */
63fe2f618cSchristos }
64fe2f618cSchristos 
65a054c658Sjtc int
sigvec(int signo,struct sigvec * nsv,struct sigvec * osv)665b84b398Schristos sigvec(int signo, struct sigvec *nsv, struct sigvec *osv)
6761f28255Scgd {
6861f28255Scgd 	int ret;
69fe2f618cSchristos 	struct sigaction osa, nsa;
7061f28255Scgd 
71fe2f618cSchristos 	if (nsv)
72fe2f618cSchristos 		sv2sa(&nsa, nsv);
73fe2f618cSchristos 
74fe2f618cSchristos 	ret = sigaction(signo, nsv ? &nsa : NULL, osv ? &osa : NULL);
75fe2f618cSchristos 
7661f28255Scgd 	if (ret == 0 && osv)
77fe2f618cSchristos 		sa2sv(osv, &osa);
78fe2f618cSchristos 
7961f28255Scgd 	return (ret);
8061f28255Scgd }
8161f28255Scgd 
82a054c658Sjtc int
sigsetmask(int mask)835b84b398Schristos sigsetmask(int mask)
8461f28255Scgd {
8551327000Schristos 	sigset_t nmask, omask;
8651327000Schristos 	int n;
8761f28255Scgd 
8851327000Schristos 	sigemptyset(&nmask);
8951327000Schristos 	nmask.__bits[0] = mask;
9051327000Schristos 
9151327000Schristos 	n = sigprocmask(SIG_SETMASK, &nmask, &omask);
9261f28255Scgd 	if (n)
9361f28255Scgd 		return (n);
9451327000Schristos 	return (omask.__bits[0]);
9561f28255Scgd }
9661f28255Scgd 
97a054c658Sjtc int
sigblock(int mask)985b84b398Schristos sigblock(int mask)
9961f28255Scgd {
10051327000Schristos 	sigset_t nmask, omask;
10151327000Schristos 	int n;
10261f28255Scgd 
10351327000Schristos 	sigemptyset(&nmask);
10451327000Schristos 	nmask.__bits[0] = mask;
10551327000Schristos 
10651327000Schristos 	n = sigprocmask(SIG_BLOCK, &nmask, &omask);
10761f28255Scgd 	if (n)
10861f28255Scgd 		return (n);
10951327000Schristos 	return (omask.__bits[0]);
11061f28255Scgd }
11161f28255Scgd 
112a054c658Sjtc int
sigpause(int mask)1135b84b398Schristos sigpause(int mask)
11461f28255Scgd {
11551327000Schristos 	sigset_t nmask;
11651327000Schristos 
11751327000Schristos 	sigemptyset(&nmask);
11851327000Schristos 	nmask.__bits[0] = mask;
11951327000Schristos 	return (sigsuspend(&nmask));
12061f28255Scgd }
121