xref: /freebsd/lib/libthr/thread/thr_sig.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/signalvar.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <pthread.h>
44 
45 #include "thr_private.h"
46 
47 /* #define DEBUG_SIGNAL */
48 #ifdef DEBUG_SIGNAL
49 #define DBG_MSG		stdout_debug
50 #else
51 #define DBG_MSG(x...)
52 #endif
53 
54 static void
55 sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
56 {
57 	struct pthread *curthread = _get_curthread();
58 
59 	if (curthread->cancelflags & THR_CANCEL_AT_POINT)
60 		pthread_testcancel();
61 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
62 		__sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
63 		_thr_suspend_check(curthread);
64 	}
65 }
66 
67 void
68 _thr_suspend_check(struct pthread *curthread)
69 {
70 	long cycle;
71 
72 	/* Async suspend. */
73 	_thr_signal_block(curthread);
74 	THR_LOCK(curthread);
75 	if ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
76 		== THR_FLAGS_NEED_SUSPEND) {
77 		curthread->flags |= THR_FLAGS_SUSPENDED;
78 		while (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
79 			cycle = curthread->cycle;
80 			THR_UNLOCK(curthread);
81 			_thr_signal_unblock(curthread);
82 			_thr_umtx_wait(&curthread->cycle, cycle, NULL);
83 			_thr_signal_block(curthread);
84 			THR_LOCK(curthread);
85 		}
86 		curthread->flags &= ~THR_FLAGS_SUSPENDED;
87 	}
88 	THR_UNLOCK(curthread);
89 	_thr_signal_unblock(curthread);
90 }
91 
92 void
93 _thr_signal_init(void)
94 {
95 	struct sigaction act;
96 
97 	/* Install cancel handler. */
98 	SIGEMPTYSET(act.sa_mask);
99 	act.sa_flags = SA_SIGINFO | SA_RESTART;
100 	act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
101 	__sys_sigaction(SIGCANCEL, &act, NULL);
102 }
103 
104 void
105 _thr_signal_deinit(void)
106 {
107 }
108 
109 __weak_reference(_sigaction, sigaction);
110 
111 int
112 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
113 {
114 	/* Check if the signal number is out of range: */
115 	if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
116 		/* Return an invalid argument: */
117 		errno = EINVAL;
118 		return (-1);
119 	}
120 
121 	return __sys_sigaction(sig, act, oact);
122 }
123 
124 __weak_reference(_sigprocmask, sigprocmask);
125 
126 int
127 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
128 {
129 	const sigset_t *p = set;
130 	sigset_t newset;
131 
132 	if (how != SIG_UNBLOCK) {
133 		if (set != NULL) {
134 			newset = *set;
135 			SIGDELSET(newset, SIGCANCEL);
136 			p = &newset;
137 		}
138 	}
139 	return (__sys_sigprocmask(how, p, oset));
140 }
141 
142 __weak_reference(_pthread_sigmask, pthread_sigmask);
143 
144 int
145 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
146 {
147 	if (_sigprocmask(how, set, oset))
148 		return (errno);
149 	return (0);
150 }
151 
152 __weak_reference(_sigsuspend, sigsuspend);
153 
154 int
155 _sigsuspend(const sigset_t * set)
156 {
157 	struct pthread *curthread = _get_curthread();
158 	sigset_t newset;
159 	const sigset_t *pset;
160 	int oldcancel;
161 	int ret;
162 
163 	if (SIGISMEMBER(*set, SIGCANCEL)) {
164 		newset = *set;
165 		SIGDELSET(newset, SIGCANCEL);
166 		pset = &newset;
167 	} else
168 		pset = set;
169 
170 	oldcancel = _thr_cancel_enter(curthread);
171 	ret = __sys_sigsuspend(pset);
172 	_thr_cancel_leave(curthread, oldcancel);
173 
174 	return (ret);
175 }
176 
177 __weak_reference(__sigwait, sigwait);
178 __weak_reference(__sigtimedwait, sigtimedwait);
179 __weak_reference(__sigwaitinfo, sigwaitinfo);
180 
181 int
182 __sigtimedwait(const sigset_t *set, siginfo_t *info,
183 	const struct timespec * timeout)
184 {
185 	struct pthread	*curthread = _get_curthread();
186 	sigset_t newset;
187 	const sigset_t *pset;
188 	int oldcancel;
189 	int ret;
190 
191 	if (SIGISMEMBER(*set, SIGCANCEL)) {
192 		newset = *set;
193 		SIGDELSET(newset, SIGCANCEL);
194 		pset = &newset;
195 	} else
196 		pset = set;
197 	oldcancel = _thr_cancel_enter(curthread);
198 	ret = __sys_sigtimedwait(pset, info, timeout);
199 	_thr_cancel_leave(curthread, oldcancel);
200 	return (ret);
201 }
202 
203 int
204 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
205 {
206 	struct pthread	*curthread = _get_curthread();
207 	sigset_t newset;
208 	const sigset_t *pset;
209 	int oldcancel;
210 	int ret;
211 
212 	if (SIGISMEMBER(*set, SIGCANCEL)) {
213 		newset = *set;
214 		SIGDELSET(newset, SIGCANCEL);
215 		pset = &newset;
216 	} else
217 		pset = set;
218 
219 	oldcancel = _thr_cancel_enter(curthread);
220 	ret = __sys_sigwaitinfo(pset, info);
221 	_thr_cancel_leave(curthread, oldcancel);
222 	return (ret);
223 }
224 
225 int
226 __sigwait(const sigset_t *set, int *sig)
227 {
228 	struct pthread	*curthread = _get_curthread();
229 	sigset_t newset;
230 	const sigset_t *pset;
231 	int oldcancel;
232 	int ret;
233 
234 	if (SIGISMEMBER(*set, SIGCANCEL)) {
235 		newset = *set;
236 		SIGDELSET(newset, SIGCANCEL);
237 		pset = &newset;
238 	} else
239 		pset = set;
240 
241 	oldcancel = _thr_cancel_enter(curthread);
242 	ret = __sys_sigwait(pset, sig);
243 	_thr_cancel_leave(curthread, oldcancel);
244 	return (ret);
245 }
246