xref: /freebsd/lib/libthr/thread/thr_sig.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "namespace.h"
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/signalvar.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <pthread.h>
39 #include "un-namespace.h"
40 
41 #include "thr_private.h"
42 
43 /* #define DEBUG_SIGNAL */
44 #ifdef DEBUG_SIGNAL
45 #define DBG_MSG		stdout_debug
46 #else
47 #define DBG_MSG(x...)
48 #endif
49 
50 extern int	__pause(void);
51 int	___pause(void);
52 int	_raise(int);
53 int	__sigtimedwait(const sigset_t *set, siginfo_t *info,
54 	const struct timespec * timeout);
55 int	_sigtimedwait(const sigset_t *set, siginfo_t *info,
56 	const struct timespec * timeout);
57 int	__sigwaitinfo(const sigset_t *set, siginfo_t *info);
58 int	_sigwaitinfo(const sigset_t *set, siginfo_t *info);
59 int	__sigwait(const sigset_t *set, int *sig);
60 int	_sigwait(const sigset_t *set, int *sig);
61 int	__sigsuspend(const sigset_t *sigmask);
62 
63 
64 static void
65 sigcancel_handler(int sig __unused,
66 	siginfo_t *info __unused, ucontext_t *ucp __unused)
67 {
68 	struct pthread *curthread = _get_curthread();
69 
70 	if (curthread->cancel_defer && curthread->cancel_pending)
71 		thr_wake(curthread->tid);
72 	_thr_ast(curthread);
73 }
74 
75 void
76 _thr_ast(struct pthread *curthread)
77 {
78 	if (!THR_IN_CRITICAL(curthread)) {
79 		_thr_testcancel(curthread);
80 		if (__predict_false((curthread->flags &
81 		    (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
82 			== THR_FLAGS_NEED_SUSPEND))
83 			_thr_suspend_check(curthread);
84 	}
85 }
86 
87 void
88 _thr_suspend_check(struct pthread *curthread)
89 {
90 	long cycle;
91 	int err;
92 
93 	err = errno;
94 	/*
95 	 * Blocks SIGCANCEL which other threads must send.
96 	 */
97 	_thr_signal_block(curthread);
98 
99 	/*
100 	 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
101 	 * because we are leaf code, we don't want to recursively call
102 	 * ourself.
103 	 */
104 	curthread->critical_count++;
105 	THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
106 	while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
107 		THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
108 		curthread->cycle++;
109 		cycle = curthread->cycle;
110 
111 		/* Wake the thread suspending us. */
112 		_thr_umtx_wake(&curthread->cycle, INT_MAX);
113 
114 		/*
115 		 * if we are from pthread_exit, we don't want to
116 		 * suspend, just go and die.
117 		 */
118 		if (curthread->state == PS_DEAD)
119 			break;
120 		curthread->flags |= THR_FLAGS_SUSPENDED;
121 		THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
122 		_thr_umtx_wait(&curthread->cycle, cycle, NULL);
123 		THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
124 		curthread->flags &= ~THR_FLAGS_SUSPENDED;
125 	}
126 	THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
127 	curthread->critical_count--;
128 
129 	/*
130 	 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and
131 	 * a new signal frame will nest us, this seems a problem because
132 	 * stack will grow and overflow, but because kernel will automatically
133 	 * mask the SIGCANCEL when delivering the signal, so we at most only
134 	 * have one nesting signal frame, this should be fine.
135 	 */
136 	_thr_signal_unblock(curthread);
137 	errno = err;
138 }
139 
140 void
141 _thr_signal_init(void)
142 {
143 	struct sigaction act;
144 
145 	/* Install cancel handler. */
146 	SIGEMPTYSET(act.sa_mask);
147 	act.sa_flags = SA_SIGINFO | SA_RESTART;
148 	act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
149 	__sys_sigaction(SIGCANCEL, &act, NULL);
150 }
151 
152 void
153 _thr_signal_deinit(void)
154 {
155 }
156 
157 __weak_reference(___pause, pause);
158 
159 int
160 ___pause(void)
161 {
162 	struct pthread *curthread = _get_curthread();
163 	int	ret;
164 
165 	_thr_cancel_enter(curthread);
166 	ret = __pause();
167 	_thr_cancel_leave(curthread);
168 
169 	return ret;
170 }
171 
172 __weak_reference(_raise, raise);
173 
174 int
175 _raise(int sig)
176 {
177 	int ret;
178 
179 	if (!_thr_isthreaded())
180 		ret = kill(getpid(), sig);
181 	else
182 		ret = _thr_send_sig(_get_curthread(), sig);
183 	return (ret);
184 }
185 
186 __weak_reference(_sigaction, sigaction);
187 
188 int
189 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
190 {
191 	/* Check if the signal number is out of range: */
192 	if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
193 		/* Return an invalid argument: */
194 		errno = EINVAL;
195 		return (-1);
196 	}
197 
198 	return __sys_sigaction(sig, act, oact);
199 }
200 
201 __weak_reference(_sigprocmask, sigprocmask);
202 
203 int
204 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
205 {
206 	const sigset_t *p = set;
207 	sigset_t newset;
208 
209 	if (how != SIG_UNBLOCK) {
210 		if (set != NULL) {
211 			newset = *set;
212 			SIGDELSET(newset, SIGCANCEL);
213 			p = &newset;
214 		}
215 	}
216 	return (__sys_sigprocmask(how, p, oset));
217 }
218 
219 __weak_reference(_pthread_sigmask, pthread_sigmask);
220 
221 int
222 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
223 {
224 	if (_sigprocmask(how, set, oset))
225 		return (errno);
226 	return (0);
227 }
228 
229 __weak_reference(__sigsuspend, sigsuspend);
230 
231 int
232 _sigsuspend(const sigset_t * set)
233 {
234 	sigset_t newset;
235 	const sigset_t *pset;
236 	int ret;
237 
238 	if (SIGISMEMBER(*set, SIGCANCEL)) {
239 		newset = *set;
240 		SIGDELSET(newset, SIGCANCEL);
241 		pset = &newset;
242 	} else
243 		pset = set;
244 
245 	ret = __sys_sigsuspend(pset);
246 
247 	return (ret);
248 }
249 
250 int
251 __sigsuspend(const sigset_t * set)
252 {
253 	struct pthread *curthread = _get_curthread();
254 	sigset_t newset;
255 	const sigset_t *pset;
256 	int ret;
257 
258 	if (SIGISMEMBER(*set, SIGCANCEL)) {
259 		newset = *set;
260 		SIGDELSET(newset, SIGCANCEL);
261 		pset = &newset;
262 	} else
263 		pset = set;
264 
265 	_thr_cancel_enter(curthread);
266 	ret = __sys_sigsuspend(pset);
267 	_thr_cancel_leave(curthread);
268 
269 	return (ret);
270 }
271 
272 __weak_reference(__sigwait, sigwait);
273 __weak_reference(__sigtimedwait, sigtimedwait);
274 __weak_reference(__sigwaitinfo, sigwaitinfo);
275 
276 int
277 _sigtimedwait(const sigset_t *set, siginfo_t *info,
278 	const struct timespec * timeout)
279 {
280 	sigset_t newset;
281 	const sigset_t *pset;
282 	int ret;
283 
284 	if (SIGISMEMBER(*set, SIGCANCEL)) {
285 		newset = *set;
286 		SIGDELSET(newset, SIGCANCEL);
287 		pset = &newset;
288 	} else
289 		pset = set;
290 	ret = __sys_sigtimedwait(pset, info, timeout);
291 	return (ret);
292 }
293 
294 int
295 __sigtimedwait(const sigset_t *set, siginfo_t *info,
296 	const struct timespec * timeout)
297 {
298 	struct pthread	*curthread = _get_curthread();
299 	sigset_t newset;
300 	const sigset_t *pset;
301 	int ret;
302 
303 	if (SIGISMEMBER(*set, SIGCANCEL)) {
304 		newset = *set;
305 		SIGDELSET(newset, SIGCANCEL);
306 		pset = &newset;
307 	} else
308 		pset = set;
309 	_thr_cancel_enter(curthread);
310 	ret = __sys_sigtimedwait(pset, info, timeout);
311 	_thr_cancel_leave(curthread);
312 	return (ret);
313 }
314 
315 int
316 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
317 {
318 	sigset_t newset;
319 	const sigset_t *pset;
320 	int ret;
321 
322 	if (SIGISMEMBER(*set, SIGCANCEL)) {
323 		newset = *set;
324 		SIGDELSET(newset, SIGCANCEL);
325 		pset = &newset;
326 	} else
327 		pset = set;
328 
329 	ret = __sys_sigwaitinfo(pset, info);
330 	return (ret);
331 }
332 
333 int
334 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
335 {
336 	struct pthread	*curthread = _get_curthread();
337 	sigset_t newset;
338 	const sigset_t *pset;
339 	int ret;
340 
341 	if (SIGISMEMBER(*set, SIGCANCEL)) {
342 		newset = *set;
343 		SIGDELSET(newset, SIGCANCEL);
344 		pset = &newset;
345 	} else
346 		pset = set;
347 
348 	_thr_cancel_enter(curthread);
349 	ret = __sys_sigwaitinfo(pset, info);
350 	_thr_cancel_leave(curthread);
351 	return (ret);
352 }
353 
354 int
355 _sigwait(const sigset_t *set, int *sig)
356 {
357 	sigset_t newset;
358 	const sigset_t *pset;
359 	int ret;
360 
361 	if (SIGISMEMBER(*set, SIGCANCEL)) {
362 		newset = *set;
363 		SIGDELSET(newset, SIGCANCEL);
364 		pset = &newset;
365 	} else
366 		pset = set;
367 
368 	ret = __sys_sigwait(pset, sig);
369 	return (ret);
370 }
371 
372 int
373 __sigwait(const sigset_t *set, int *sig)
374 {
375 	struct pthread	*curthread = _get_curthread();
376 	sigset_t newset;
377 	const sigset_t *pset;
378 	int ret;
379 
380 	if (SIGISMEMBER(*set, SIGCANCEL)) {
381 		newset = *set;
382 		SIGDELSET(newset, SIGCANCEL);
383 		pset = &newset;
384 	} else
385 		pset = set;
386 
387 	_thr_cancel_enter(curthread);
388 	ret = __sys_sigwait(pset, sig);
389 	_thr_cancel_leave(curthread);
390 	return (ret);
391 }
392