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