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