xref: /freebsd/lib/libthr/thread/thr_sig.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 <stdlib.h>
36 #include <string.h>
37 #include <pthread.h>
38 #include "un-namespace.h"
39 #include "libc_private.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 struct usigaction {
51 	struct sigaction sigact;
52 	struct urwlock   lock;
53 };
54 
55 static struct usigaction _thr_sigact[_SIG_MAXSIG];
56 
57 static void thr_sighandler(int, siginfo_t *, void *);
58 static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
59 static void check_deferred_signal(struct pthread *);
60 static void check_suspend(struct pthread *);
61 static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
62 
63 int	___pause(void);
64 int	_raise(int);
65 int	__sigtimedwait(const sigset_t *set, siginfo_t *info,
66 	const struct timespec * timeout);
67 int	_sigtimedwait(const sigset_t *set, siginfo_t *info,
68 	const struct timespec * timeout);
69 int	__sigwaitinfo(const sigset_t *set, siginfo_t *info);
70 int	_sigwaitinfo(const sigset_t *set, siginfo_t *info);
71 int	___sigwait(const sigset_t *set, int *sig);
72 int	_sigwait(const sigset_t *set, int *sig);
73 int	__sigsuspend(const sigset_t *sigmask);
74 int	_sigaction(int, const struct sigaction *, struct sigaction *);
75 int	_setcontext(const ucontext_t *);
76 int	_swapcontext(ucontext_t *, const ucontext_t *);
77 
78 static const sigset_t _thr_deferset={{
79 	0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)|
80 	_SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)),
81 	0xffffffff,
82 	0xffffffff,
83 	0xffffffff}};
84 
85 static const sigset_t _thr_maskset={{
86 	0xffffffff,
87 	0xffffffff,
88 	0xffffffff,
89 	0xffffffff}};
90 
91 void
92 _thr_signal_block(struct pthread *curthread)
93 {
94 
95 	if (curthread->sigblock > 0) {
96 		curthread->sigblock++;
97 		return;
98 	}
99 	__sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask);
100 	curthread->sigblock++;
101 }
102 
103 void
104 _thr_signal_unblock(struct pthread *curthread)
105 {
106 	if (--curthread->sigblock == 0)
107 		__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
108 }
109 
110 int
111 _thr_send_sig(struct pthread *thread, int sig)
112 {
113 	return thr_kill(thread->tid, sig);
114 }
115 
116 static inline void
117 remove_thr_signals(sigset_t *set)
118 {
119 	if (SIGISMEMBER(*set, SIGCANCEL))
120 		SIGDELSET(*set, SIGCANCEL);
121 }
122 
123 static const sigset_t *
124 thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
125 {
126 	*newset = *set;
127 	remove_thr_signals(newset);
128 	return (newset);
129 }
130 
131 static void
132 sigcancel_handler(int sig __unused,
133 	siginfo_t *info __unused, ucontext_t *ucp)
134 {
135 	struct pthread *curthread = _get_curthread();
136 	int err;
137 
138 	if (THR_IN_CRITICAL(curthread))
139 		return;
140 	err = errno;
141 	check_suspend(curthread);
142 	check_cancel(curthread, ucp);
143 	errno = err;
144 }
145 
146 typedef void (*ohandler)(int sig, int code,
147 	struct sigcontext *scp, char *addr, __sighandler_t *catcher);
148 
149 /*
150  * The signal handler wrapper is entered with all signal masked.
151  */
152 static void
153 thr_sighandler(int sig, siginfo_t *info, void *_ucp)
154 {
155 	struct pthread *curthread = _get_curthread();
156 	ucontext_t *ucp = _ucp;
157 	struct sigaction act;
158 	int err;
159 
160 	err = errno;
161 	_thr_rwl_rdlock(&_thr_sigact[sig-1].lock);
162 	act = _thr_sigact[sig-1].sigact;
163 	_thr_rwl_unlock(&_thr_sigact[sig-1].lock);
164 	errno = err;
165 
166 	/*
167 	 * if a thread is in critical region, for example it holds low level locks,
168 	 * try to defer the signal processing, however if the signal is synchronous
169 	 * signal, it means a bad thing has happened, this is a programming error,
170 	 * resuming fault point can not help anything (normally causes deadloop),
171 	 * so here we let user code handle it immediately.
172 	 */
173 	if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
174 		memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
175 		memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
176 		curthread->deferred_sigmask = ucp->uc_sigmask;
177 		/* mask all signals, we will restore it later. */
178 		ucp->uc_sigmask = _thr_deferset;
179 		return;
180 	}
181 
182 	handle_signal(&act, sig, info, ucp);
183 }
184 
185 static void
186 handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
187 {
188 	struct pthread *curthread = _get_curthread();
189 	ucontext_t uc2;
190 	__siginfohandler_t *sigfunc;
191 	int cancel_point;
192 	int cancel_async;
193 	int cancel_enable;
194 	int in_sigsuspend;
195 	int err;
196 
197 	/* add previous level mask */
198 	SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
199 
200 	/* add this signal's mask */
201 	if (!(actp->sa_flags & SA_NODEFER))
202 		SIGADDSET(actp->sa_mask, sig);
203 
204 	in_sigsuspend = curthread->in_sigsuspend;
205 	curthread->in_sigsuspend = 0;
206 
207 	/*
208 	 * If thread is in deferred cancellation mode, disable cancellation
209 	 * in signal handler.
210 	 * If user signal handler calls a cancellation point function, e.g,
211 	 * it calls write() to write data to file, because write() is a
212 	 * cancellation point, the thread is immediately cancelled if
213 	 * cancellation is pending, to avoid this problem while thread is in
214 	 * deferring mode, cancellation is temporarily disabled.
215 	 */
216 	cancel_point = curthread->cancel_point;
217 	cancel_async = curthread->cancel_async;
218 	cancel_enable = curthread->cancel_enable;
219 	curthread->cancel_point = 0;
220 	if (!cancel_async)
221 		curthread->cancel_enable = 0;
222 
223 	/* restore correct mask before calling user handler */
224 	__sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
225 
226 	sigfunc = actp->sa_sigaction;
227 
228 	/*
229 	 * We have already reset cancellation point flags, so if user's code
230 	 * longjmp()s out of its signal handler, wish its jmpbuf was set
231 	 * outside of a cancellation point, in most cases, this would be
232 	 * true.  However, there is no way to save cancel_enable in jmpbuf,
233 	 * so after setjmps() returns once more, the user code may need to
234 	 * re-set cancel_enable flag by calling pthread_setcancelstate().
235 	 */
236 	if ((actp->sa_flags & SA_SIGINFO) != 0)
237 		(*(sigfunc))(sig, info, ucp);
238 	else {
239 		((ohandler)(*sigfunc))(
240 			sig, info->si_code, (struct sigcontext *)ucp,
241 			info->si_addr, (__sighandler_t *)sigfunc);
242 	}
243 	err = errno;
244 
245 	curthread->in_sigsuspend = in_sigsuspend;
246 	curthread->cancel_point = cancel_point;
247 	curthread->cancel_enable = cancel_enable;
248 
249 	memcpy(&uc2, ucp, sizeof(uc2));
250 	SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
251 
252 	/* reschedule cancellation */
253 	check_cancel(curthread, &uc2);
254 	errno = err;
255 	__sys_sigreturn(&uc2);
256 }
257 
258 void
259 _thr_ast(struct pthread *curthread)
260 {
261 
262 	if (!THR_IN_CRITICAL(curthread)) {
263 		check_deferred_signal(curthread);
264 		check_suspend(curthread);
265 		check_cancel(curthread, NULL);
266 	}
267 }
268 
269 /* reschedule cancellation */
270 static void
271 check_cancel(struct pthread *curthread, ucontext_t *ucp)
272 {
273 
274 	if (__predict_true(!curthread->cancel_pending ||
275 	    !curthread->cancel_enable || curthread->no_cancel))
276 		return;
277 
278 	/*
279  	 * Otherwise, we are in defer mode, and we are at
280 	 * cancel point, tell kernel to not block the current
281 	 * thread on next cancelable system call.
282 	 *
283 	 * There are three cases we should call thr_wake() to
284 	 * turn on TDP_WAKEUP or send SIGCANCEL in kernel:
285 	 * 1) we are going to call a cancelable system call,
286 	 *    non-zero cancel_point means we are already in
287 	 *    cancelable state, next system call is cancelable.
288 	 * 2) because _thr_ast() may be called by
289 	 *    THR_CRITICAL_LEAVE() which is used by rtld rwlock
290 	 *    and any libthr internal locks, when rtld rwlock
291 	 *    is used, it is mostly caused my an unresolved PLT.
292 	 *    those routines may clear the TDP_WAKEUP flag by
293 	 *    invoking some system calls, in those cases, we
294 	 *    also should reenable the flag.
295 	 * 3) thread is in sigsuspend(), and the syscall insists
296 	 *    on getting a signal before it agrees to return.
297  	 */
298 	if (curthread->cancel_point) {
299 		if (curthread->in_sigsuspend && ucp) {
300 			SIGADDSET(ucp->uc_sigmask, SIGCANCEL);
301 			curthread->unblock_sigcancel = 1;
302 			_thr_send_sig(curthread, SIGCANCEL);
303 		} else
304 			thr_wake(curthread->tid);
305 	} else if (curthread->cancel_async) {
306 		/*
307 		 * asynchronous cancellation mode, act upon
308 		 * immediately.
309 		 */
310 		_pthread_exit_mask(PTHREAD_CANCELED,
311 		    ucp? &ucp->uc_sigmask : NULL);
312 	}
313 }
314 
315 static void
316 check_deferred_signal(struct pthread *curthread)
317 {
318 	ucontext_t *uc;
319 	struct sigaction act;
320 	siginfo_t info;
321 	int uc_len;
322 
323 	if (__predict_true(curthread->deferred_siginfo.si_signo == 0))
324 		return;
325 
326 	uc_len = __getcontextx_size();
327 	uc = alloca(uc_len);
328 	getcontext(uc);
329 	if (curthread->deferred_siginfo.si_signo == 0)
330 		return;
331 	__fillcontextx2((char *)uc);
332 	act = curthread->deferred_sigact;
333 	uc->uc_sigmask = curthread->deferred_sigmask;
334 	memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
335 	/* remove signal */
336 	curthread->deferred_siginfo.si_signo = 0;
337 	handle_signal(&act, info.si_signo, &info, uc);
338 }
339 
340 static void
341 check_suspend(struct pthread *curthread)
342 {
343 	uint32_t cycle;
344 
345 	if (__predict_true((curthread->flags &
346 		(THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
347 		!= THR_FLAGS_NEED_SUSPEND))
348 		return;
349 	if (curthread == _single_thread)
350 		return;
351 	if (curthread->force_exit)
352 		return;
353 
354 	/*
355 	 * Blocks SIGCANCEL which other threads must send.
356 	 */
357 	_thr_signal_block(curthread);
358 
359 	/*
360 	 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
361 	 * because we are leaf code, we don't want to recursively call
362 	 * ourself.
363 	 */
364 	curthread->critical_count++;
365 	THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
366 	while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
367 		THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
368 		curthread->cycle++;
369 		cycle = curthread->cycle;
370 
371 		/* Wake the thread suspending us. */
372 		_thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
373 
374 		/*
375 		 * if we are from pthread_exit, we don't want to
376 		 * suspend, just go and die.
377 		 */
378 		if (curthread->state == PS_DEAD)
379 			break;
380 		curthread->flags |= THR_FLAGS_SUSPENDED;
381 		THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
382 		_thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0);
383 		THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
384 		curthread->flags &= ~THR_FLAGS_SUSPENDED;
385 	}
386 	THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
387 	curthread->critical_count--;
388 
389 	_thr_signal_unblock(curthread);
390 }
391 
392 void
393 _thr_signal_init(void)
394 {
395 	struct sigaction act;
396 
397 	/* Install SIGCANCEL handler. */
398 	SIGFILLSET(act.sa_mask);
399 	act.sa_flags = SA_SIGINFO;
400 	act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
401 	__sys_sigaction(SIGCANCEL, &act, NULL);
402 
403 	/* Unblock SIGCANCEL */
404 	SIGEMPTYSET(act.sa_mask);
405 	SIGADDSET(act.sa_mask, SIGCANCEL);
406 	__sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
407 }
408 
409 void
410 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
411 {
412 #if 0
413 	struct pthread *curthread = _get_curthread();
414 	struct urwlock *rwlp;
415 	struct sigaction *actp;
416 	struct sigaction kact;
417 	void (*handler)(int);
418 	int sig;
419 
420 	_thr_signal_block(curthread);
421 	for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
422 		actp = &_thr_sigact[sig-1].sigact;
423 retry:
424 		handler = actp->sa_handler;
425 		if (handler != SIG_DFL && handler != SIG_IGN &&
426 		    __elf_phdr_match_addr(phdr_info, handler)) {
427 			rwlp = &_thr_sigact[sig-1].lock;
428 			_thr_rwl_wrlock(rwlp);
429 			if (handler != actp->sa_handler) {
430 				_thr_rwl_unlock(rwlp);
431 				goto retry;
432 			}
433 			actp->sa_handler = SIG_DFL;
434 			actp->sa_flags = SA_SIGINFO;
435 			SIGEMPTYSET(actp->sa_mask);
436 			if (__sys_sigaction(sig, NULL, &kact) == 0 &&
437 				kact.sa_handler != SIG_DFL &&
438 				kact.sa_handler != SIG_IGN)
439 				__sys_sigaction(sig, actp, NULL);
440 			_thr_rwl_unlock(rwlp);
441 		}
442 	}
443 	_thr_signal_unblock(curthread);
444 #endif
445 }
446 
447 void
448 _thr_signal_prefork(void)
449 {
450 	int i;
451 
452 	for (i = 1; i <= _SIG_MAXSIG; ++i)
453 		_thr_rwl_rdlock(&_thr_sigact[i-1].lock);
454 }
455 
456 void
457 _thr_signal_postfork(void)
458 {
459 	int i;
460 
461 	for (i = 1; i <= _SIG_MAXSIG; ++i)
462 		_thr_rwl_unlock(&_thr_sigact[i-1].lock);
463 }
464 
465 void
466 _thr_signal_postfork_child(void)
467 {
468 	int i;
469 
470 	for (i = 1; i <= _SIG_MAXSIG; ++i)
471 		bzero(&_thr_sigact[i-1].lock, sizeof(struct urwlock));
472 }
473 
474 void
475 _thr_signal_deinit(void)
476 {
477 }
478 
479 __weak_reference(___pause, pause);
480 
481 int
482 ___pause(void)
483 {
484 	sigset_t oset;
485 
486 	if (_sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
487 		return (-1);
488 	return (__sigsuspend(&oset));
489 }
490 
491 __weak_reference(_raise, raise);
492 
493 int
494 _raise(int sig)
495 {
496 	return _thr_send_sig(_get_curthread(), sig);
497 }
498 
499 __weak_reference(_sigaction, sigaction);
500 
501 int
502 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
503 {
504 	struct sigaction newact, oldact, oldact2;
505 	sigset_t oldset;
506 	int ret = 0, err = 0;
507 
508 	if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
509 		errno = EINVAL;
510 		return (-1);
511 	}
512 
513 	if (act)
514 		newact = *act;
515 
516 	__sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
517 	_thr_rwl_wrlock(&_thr_sigact[sig-1].lock);
518 
519 	if (act != NULL) {
520 		oldact2 = _thr_sigact[sig-1].sigact;
521 
522  		/*
523 		 * if a new sig handler is SIG_DFL or SIG_IGN,
524 		 * don't remove old handler from _thr_sigact[],
525 		 * so deferred signals still can use the handlers,
526 		 * multiple threads invoking sigaction itself is
527 		 * a race condition, so it is not a problem.
528 		 */
529 		if (newact.sa_handler != SIG_DFL &&
530 		    newact.sa_handler != SIG_IGN) {
531 			_thr_sigact[sig-1].sigact = newact;
532 			remove_thr_signals(
533 				&_thr_sigact[sig-1].sigact.sa_mask);
534 			newact.sa_flags &= ~SA_NODEFER;
535 			newact.sa_flags |= SA_SIGINFO;
536 			newact.sa_sigaction = thr_sighandler;
537 			newact.sa_mask = _thr_maskset; /* mask all signals */
538 		}
539 		if ((ret = __sys_sigaction(sig, &newact, &oldact))) {
540 			err = errno;
541 			_thr_sigact[sig-1].sigact = oldact2;
542 		}
543 	} else if (oact != NULL) {
544 		ret = __sys_sigaction(sig, NULL, &oldact);
545 		err = errno;
546 	}
547 
548 	if (oldact.sa_handler != SIG_DFL &&
549 	    oldact.sa_handler != SIG_IGN) {
550 		if (act != NULL)
551 			oldact = oldact2;
552 		else if (oact != NULL)
553 			oldact = _thr_sigact[sig-1].sigact;
554 	}
555 
556 	_thr_rwl_unlock(&_thr_sigact[sig-1].lock);
557 	__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
558 
559 	if (ret == 0) {
560 		if (oact != NULL)
561 			*oact = oldact;
562 	} else {
563 		errno = err;
564 	}
565 	return (ret);
566 }
567 
568 __weak_reference(_sigprocmask, sigprocmask);
569 
570 int
571 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
572 {
573 	const sigset_t *p = set;
574 	sigset_t newset;
575 
576 	if (how != SIG_UNBLOCK) {
577 		if (set != NULL) {
578 			newset = *set;
579 			SIGDELSET(newset, SIGCANCEL);
580 			p = &newset;
581 		}
582 	}
583 	return (__sys_sigprocmask(how, p, oset));
584 }
585 
586 __weak_reference(_pthread_sigmask, pthread_sigmask);
587 
588 int
589 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
590 {
591 	if (_sigprocmask(how, set, oset))
592 		return (errno);
593 	return (0);
594 }
595 
596 __weak_reference(__sigsuspend, sigsuspend);
597 
598 int
599 _sigsuspend(const sigset_t * set)
600 {
601 	sigset_t newset;
602 
603 	return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
604 }
605 
606 int
607 __sigsuspend(const sigset_t * set)
608 {
609 	struct pthread *curthread;
610 	sigset_t newset;
611 	int ret, old;
612 
613 	curthread = _get_curthread();
614 
615 	old = curthread->in_sigsuspend;
616 	curthread->in_sigsuspend = 1;
617 	_thr_cancel_enter(curthread);
618 	ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
619 	_thr_cancel_leave(curthread, 1);
620 	curthread->in_sigsuspend = old;
621 	if (curthread->unblock_sigcancel) {
622 		curthread->unblock_sigcancel = 0;
623 		SIGEMPTYSET(newset);
624 		SIGADDSET(newset, SIGCANCEL);
625 		__sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
626 	}
627 
628 	return (ret);
629 }
630 
631 __weak_reference(___sigwait, sigwait);
632 __weak_reference(__sigtimedwait, sigtimedwait);
633 __weak_reference(__sigwaitinfo, sigwaitinfo);
634 
635 int
636 _sigtimedwait(const sigset_t *set, siginfo_t *info,
637 	const struct timespec * timeout)
638 {
639 	sigset_t newset;
640 
641 	return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
642 	    timeout));
643 }
644 
645 /*
646  * Cancellation behavior:
647  *   Thread may be canceled at start, if thread got signal,
648  *   it is not canceled.
649  */
650 int
651 __sigtimedwait(const sigset_t *set, siginfo_t *info,
652 	const struct timespec * timeout)
653 {
654 	struct pthread	*curthread = _get_curthread();
655 	sigset_t newset;
656 	int ret;
657 
658 	_thr_cancel_enter(curthread);
659 	ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
660 	    timeout);
661 	_thr_cancel_leave(curthread, (ret == -1));
662 	return (ret);
663 }
664 
665 int
666 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
667 {
668 	sigset_t newset;
669 
670 	return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
671 }
672 
673 /*
674  * Cancellation behavior:
675  *   Thread may be canceled at start, if thread got signal,
676  *   it is not canceled.
677  */
678 int
679 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
680 {
681 	struct pthread	*curthread = _get_curthread();
682 	sigset_t newset;
683 	int ret;
684 
685 	_thr_cancel_enter(curthread);
686 	ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
687 	_thr_cancel_leave(curthread, ret == -1);
688 	return (ret);
689 }
690 
691 int
692 _sigwait(const sigset_t *set, int *sig)
693 {
694 	sigset_t newset;
695 
696 	return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
697 }
698 
699 /*
700  * Cancellation behavior:
701  *   Thread may be canceled at start, if thread got signal,
702  *   it is not canceled.
703  */
704 int
705 ___sigwait(const sigset_t *set, int *sig)
706 {
707 	struct pthread	*curthread = _get_curthread();
708 	sigset_t newset;
709 	int ret;
710 
711 	do {
712 		_thr_cancel_enter(curthread);
713 		ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
714 		_thr_cancel_leave(curthread, (ret != 0));
715 	} while (ret == EINTR);
716 	return (ret);
717 }
718 
719 __weak_reference(_setcontext, setcontext);
720 int
721 _setcontext(const ucontext_t *ucp)
722 {
723 	ucontext_t uc;
724 
725 	if (ucp == NULL) {
726 		errno = EINVAL;
727 		return (-1);
728 	}
729 	if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL))
730 		return __sys_setcontext(ucp);
731 	(void) memcpy(&uc, ucp, sizeof(uc));
732 	SIGDELSET(uc.uc_sigmask, SIGCANCEL);
733 	return __sys_setcontext(&uc);
734 }
735 
736 __weak_reference(_swapcontext, swapcontext);
737 int
738 _swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
739 {
740 	ucontext_t uc;
741 
742 	if (oucp == NULL || ucp == NULL) {
743 		errno = EINVAL;
744 		return (-1);
745 	}
746 	if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) {
747 		(void) memcpy(&uc, ucp, sizeof(uc));
748 		SIGDELSET(uc.uc_sigmask, SIGCANCEL);
749 		ucp = &uc;
750 	}
751 	return __sys_swapcontext(oucp, ucp);
752 }
753