xref: /freebsd/lib/libc/compat-43/sigcompat.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)sigcompat.c	8.1 (Berkeley) 6/2/93";
34 #endif /* LIBC_SCCS and not lint */
35 #include "namespace.h"
36 #include <sys/param.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <string.h>
40 #include "un-namespace.h"
41 #include "libc_private.h"
42 
43 int
44 sigvec(int signo, struct sigvec *sv, struct sigvec *osv)
45 {
46 	struct sigaction sa, osa;
47 	struct sigaction *sap, *osap;
48 	int ret;
49 
50 	if (sv != NULL) {
51 		sa.sa_handler = sv->sv_handler;
52 		sa.sa_flags = sv->sv_flags ^ SV_INTERRUPT;
53 		sigemptyset(&sa.sa_mask);
54 		sa.sa_mask.__bits[0] = sv->sv_mask;
55 		sap = &sa;
56 	} else
57 		sap = NULL;
58 	osap = osv != NULL ? &osa : NULL;
59 	ret = __libc_sigaction(signo, sap, osap);
60 	if (ret == 0 && osv != NULL) {
61 		osv->sv_handler = osa.sa_handler;
62 		osv->sv_flags = osa.sa_flags ^ SV_INTERRUPT;
63 		osv->sv_mask = osa.sa_mask.__bits[0];
64 	}
65 	return (ret);
66 }
67 
68 int
69 sigsetmask(int mask)
70 {
71 	sigset_t set, oset;
72 	int n;
73 
74 	sigemptyset(&set);
75 	set.__bits[0] = mask;
76 	n = __libc_sigprocmask(SIG_SETMASK, &set, &oset);
77 	if (n)
78 		return (n);
79 	return (oset.__bits[0]);
80 }
81 
82 int
83 sigblock(int mask)
84 {
85 	sigset_t set, oset;
86 	int n;
87 
88 	sigemptyset(&set);
89 	set.__bits[0] = mask;
90 	n = __libc_sigprocmask(SIG_BLOCK, &set, &oset);
91 	if (n)
92 		return (n);
93 	return (oset.__bits[0]);
94 }
95 
96 int
97 sigpause(int mask)
98 {
99 	sigset_t set;
100 
101 	sigemptyset(&set);
102 	set.__bits[0] = mask;
103 	return (__libc_sigsuspend(&set));
104 }
105 
106 int
107 xsi_sigpause(int sig)
108 {
109 	sigset_t set;
110 
111 	if (__libc_sigprocmask(SIG_BLOCK, NULL, &set) == -1)
112 		return (-1);
113 	if (sigdelset(&set, sig) == -1)
114 		return (-1);
115 	return (__libc_sigsuspend(&set));
116 }
117 
118 int
119 sighold(int sig)
120 {
121 	sigset_t set;
122 
123 	sigemptyset(&set);
124 	if (sigaddset(&set, sig) == -1)
125 		return (-1);
126 	return (__libc_sigprocmask(SIG_BLOCK, &set, NULL));
127 }
128 
129 int
130 sigignore(int sig)
131 {
132 	struct sigaction sa;
133 
134 	bzero(&sa, sizeof(sa));
135 	sa.sa_handler = SIG_IGN;
136 	return (__libc_sigaction(sig, &sa, NULL));
137 }
138 
139 int
140 sigrelse(int sig)
141 {
142 	sigset_t set;
143 
144 	sigemptyset(&set);
145 	if (sigaddset(&set, sig) == -1)
146 		return (-1);
147 	return (__libc_sigprocmask(SIG_UNBLOCK, &set, NULL));
148 }
149 
150 void
151 (*sigset(int sig, void (*disp)(int)))(int)
152 {
153 	sigset_t set, pset;
154 	struct sigaction sa, psa;
155 
156 	sigemptyset(&set);
157 	if (sigaddset(&set, sig) == -1)
158 		return (SIG_ERR);
159 	if (__libc_sigprocmask(SIG_BLOCK, NULL, &pset) == -1)
160 		return (SIG_ERR);
161 	if ((__sighandler_t *)disp == SIG_HOLD) {
162 		if (__libc_sigprocmask(SIG_BLOCK, &set, &pset) == -1)
163 			return (SIG_ERR);
164 		if (sigismember(&pset, sig))
165 			return (SIG_HOLD);
166 		else {
167 			if (__libc_sigaction(sig, NULL, &psa) == -1)
168 				return (SIG_ERR);
169 			return (psa.sa_handler);
170 		}
171 	} else {
172 		if (__libc_sigprocmask(SIG_UNBLOCK, &set, &pset) == -1)
173 			return (SIG_ERR);
174 	}
175 
176 	bzero(&sa, sizeof(sa));
177 	sa.sa_handler = disp;
178 	if (__libc_sigaction(sig, &sa, &psa) == -1)
179 		return (SIG_ERR);
180 	if (sigismember(&pset, sig))
181 		return (SIG_HOLD);
182 	else
183 		return (psa.sa_handler);
184 }
185