xref: /freebsd/lib/libc/compat-43/sigcompat.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 #include "namespace.h"
33 #include <sys/param.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <string.h>
37 #include "un-namespace.h"
38 #include "libc_private.h"
39 
40 int
41 sigvec(int signo, struct sigvec *sv, struct sigvec *osv)
42 {
43 	struct sigaction sa, osa;
44 	struct sigaction *sap, *osap;
45 	int ret;
46 
47 	if (sv != NULL) {
48 		sa.sa_handler = sv->sv_handler;
49 		sa.sa_flags = sv->sv_flags ^ SV_INTERRUPT;
50 		sigemptyset(&sa.sa_mask);
51 		sa.sa_mask.__bits[0] = sv->sv_mask;
52 		sap = &sa;
53 	} else
54 		sap = NULL;
55 	osap = osv != NULL ? &osa : NULL;
56 	ret = __libc_sigaction(signo, sap, osap);
57 	if (ret == 0 && osv != NULL) {
58 		osv->sv_handler = osa.sa_handler;
59 		osv->sv_flags = osa.sa_flags ^ SV_INTERRUPT;
60 		osv->sv_mask = osa.sa_mask.__bits[0];
61 	}
62 	return (ret);
63 }
64 
65 int
66 sigsetmask(int mask)
67 {
68 	sigset_t set, oset;
69 	int n;
70 
71 	sigemptyset(&set);
72 	set.__bits[0] = mask;
73 	n = __libc_sigprocmask(SIG_SETMASK, &set, &oset);
74 	if (n)
75 		return (n);
76 	return (oset.__bits[0]);
77 }
78 
79 int
80 sigblock(int mask)
81 {
82 	sigset_t set, oset;
83 	int n;
84 
85 	sigemptyset(&set);
86 	set.__bits[0] = mask;
87 	n = __libc_sigprocmask(SIG_BLOCK, &set, &oset);
88 	if (n)
89 		return (n);
90 	return (oset.__bits[0]);
91 }
92 
93 int
94 sigpause(int mask)
95 {
96 	sigset_t set;
97 
98 	sigemptyset(&set);
99 	set.__bits[0] = mask;
100 	return (__libc_sigsuspend(&set));
101 }
102 
103 int
104 xsi_sigpause(int sig)
105 {
106 	sigset_t set;
107 
108 	if (__libc_sigprocmask(SIG_BLOCK, NULL, &set) == -1)
109 		return (-1);
110 	if (sigdelset(&set, sig) == -1)
111 		return (-1);
112 	return (__libc_sigsuspend(&set));
113 }
114 
115 int
116 sighold(int sig)
117 {
118 	sigset_t set;
119 
120 	sigemptyset(&set);
121 	if (sigaddset(&set, sig) == -1)
122 		return (-1);
123 	return (__libc_sigprocmask(SIG_BLOCK, &set, NULL));
124 }
125 
126 int
127 sigignore(int sig)
128 {
129 	struct sigaction sa;
130 
131 	bzero(&sa, sizeof(sa));
132 	sa.sa_handler = SIG_IGN;
133 	return (__libc_sigaction(sig, &sa, NULL));
134 }
135 
136 int
137 sigrelse(int sig)
138 {
139 	sigset_t set;
140 
141 	sigemptyset(&set);
142 	if (sigaddset(&set, sig) == -1)
143 		return (-1);
144 	return (__libc_sigprocmask(SIG_UNBLOCK, &set, NULL));
145 }
146 
147 void
148 (*sigset(int sig, void (*disp)(int)))(int)
149 {
150 	sigset_t set, pset;
151 	struct sigaction sa, psa;
152 
153 	sigemptyset(&set);
154 	if (sigaddset(&set, sig) == -1)
155 		return (SIG_ERR);
156 	if (__libc_sigprocmask(SIG_BLOCK, NULL, &pset) == -1)
157 		return (SIG_ERR);
158 	if ((__sighandler_t *)disp == SIG_HOLD) {
159 		if (__libc_sigprocmask(SIG_BLOCK, &set, &pset) == -1)
160 			return (SIG_ERR);
161 		if (sigismember(&pset, sig))
162 			return (SIG_HOLD);
163 		else {
164 			if (__libc_sigaction(sig, NULL, &psa) == -1)
165 				return (SIG_ERR);
166 			return (psa.sa_handler);
167 		}
168 	} else {
169 		if (__libc_sigprocmask(SIG_UNBLOCK, &set, &pset) == -1)
170 			return (SIG_ERR);
171 	}
172 
173 	bzero(&sa, sizeof(sa));
174 	sa.sa_handler = disp;
175 	if (__libc_sigaction(sig, &sa, &psa) == -1)
176 		return (SIG_ERR);
177 	if (sigismember(&pset, sig))
178 		return (SIG_HOLD);
179 	else
180 		return (psa.sa_handler);
181 }
182