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