thr_sig.c revision 277640
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: stable/10/lib/libthr/thread/thr_sig.c 277640 2015-01-24 08:35:49Z kib $
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
51struct usigaction {
52	struct sigaction sigact;
53	struct urwlock   lock;
54};
55
56static struct usigaction _thr_sigact[_SIG_MAXSIG];
57
58static inline struct usigaction *
59__libc_sigaction_slot(int signo)
60{
61
62	return (&_thr_sigact[signo - 1]);
63}
64
65static void thr_sighandler(int, siginfo_t *, void *);
66static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
67static void check_deferred_signal(struct pthread *);
68static void check_suspend(struct pthread *);
69static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
70
71int	_sigtimedwait(const sigset_t *set, siginfo_t *info,
72	const struct timespec * timeout);
73int	_sigwaitinfo(const sigset_t *set, siginfo_t *info);
74int	_sigwait(const sigset_t *set, int *sig);
75int	_setcontext(const ucontext_t *);
76int	_swapcontext(ucontext_t *, const ucontext_t *);
77
78static 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
85static const sigset_t _thr_maskset={{
86	0xffffffff,
87	0xffffffff,
88	0xffffffff,
89	0xffffffff}};
90
91void
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
103void
104_thr_signal_unblock(struct pthread *curthread)
105{
106	if (--curthread->sigblock == 0)
107		__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
108}
109
110int
111_thr_send_sig(struct pthread *thread, int sig)
112{
113	return thr_kill(thread->tid, sig);
114}
115
116static inline void
117remove_thr_signals(sigset_t *set)
118{
119	if (SIGISMEMBER(*set, SIGCANCEL))
120		SIGDELSET(*set, SIGCANCEL);
121}
122
123static const sigset_t *
124thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
125{
126	*newset = *set;
127	remove_thr_signals(newset);
128	return (newset);
129}
130
131static void
132sigcancel_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
146typedef 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 */
152static void
153thr_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
190static void
191handle_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
263void
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 */
275static void
276check_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
320static void
321check_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
349static void
350check_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
401void
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
443void
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;
459retry:
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
483void
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
492void
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
501void
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
512void
513_thr_signal_deinit(void)
514{
515}
516
517int
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
586int
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
604int
605_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
606{
607
608	if (__thr_sigprocmask(how, set, oset))
609		return (errno);
610	return (0);
611}
612
613int
614_sigsuspend(const sigset_t * set)
615{
616	sigset_t newset;
617
618	return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
619}
620
621int
622__thr_sigsuspend(const sigset_t * set)
623{
624	struct pthread *curthread;
625	sigset_t newset;
626	int ret, old;
627
628	curthread = _get_curthread();
629
630	old = curthread->in_sigsuspend;
631	curthread->in_sigsuspend = 1;
632	_thr_cancel_enter(curthread);
633	ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
634	_thr_cancel_leave(curthread, 1);
635	curthread->in_sigsuspend = old;
636	if (curthread->unblock_sigcancel) {
637		curthread->unblock_sigcancel = 0;
638		SIGEMPTYSET(newset);
639		SIGADDSET(newset, SIGCANCEL);
640		__sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
641	}
642
643	return (ret);
644}
645
646int
647_sigtimedwait(const sigset_t *set, siginfo_t *info,
648	const struct timespec * timeout)
649{
650	sigset_t newset;
651
652	return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
653	    timeout));
654}
655
656/*
657 * Cancellation behavior:
658 *   Thread may be canceled at start, if thread got signal,
659 *   it is not canceled.
660 */
661int
662__thr_sigtimedwait(const sigset_t *set, siginfo_t *info,
663    const struct timespec * timeout)
664{
665	struct pthread	*curthread = _get_curthread();
666	sigset_t newset;
667	int ret;
668
669	_thr_cancel_enter(curthread);
670	ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
671	    timeout);
672	_thr_cancel_leave(curthread, (ret == -1));
673	return (ret);
674}
675
676int
677_sigwaitinfo(const sigset_t *set, siginfo_t *info)
678{
679	sigset_t newset;
680
681	return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
682}
683
684/*
685 * Cancellation behavior:
686 *   Thread may be canceled at start, if thread got signal,
687 *   it is not canceled.
688 */
689int
690__thr_sigwaitinfo(const sigset_t *set, siginfo_t *info)
691{
692	struct pthread	*curthread = _get_curthread();
693	sigset_t newset;
694	int ret;
695
696	_thr_cancel_enter(curthread);
697	ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
698	_thr_cancel_leave(curthread, ret == -1);
699	return (ret);
700}
701
702int
703_sigwait(const sigset_t *set, int *sig)
704{
705	sigset_t newset;
706
707	return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
708}
709
710/*
711 * Cancellation behavior:
712 *   Thread may be canceled at start, if thread got signal,
713 *   it is not canceled.
714 */
715int
716__thr_sigwait(const sigset_t *set, int *sig)
717{
718	struct pthread	*curthread = _get_curthread();
719	sigset_t newset;
720	int ret;
721
722	do {
723		_thr_cancel_enter(curthread);
724		ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
725		_thr_cancel_leave(curthread, (ret != 0));
726	} while (ret == EINTR);
727	return (ret);
728}
729
730int
731__thr_setcontext(const ucontext_t *ucp)
732{
733	ucontext_t uc;
734
735	if (ucp == NULL) {
736		errno = EINVAL;
737		return (-1);
738	}
739	if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL))
740		return __sys_setcontext(ucp);
741	(void) memcpy(&uc, ucp, sizeof(uc));
742	SIGDELSET(uc.uc_sigmask, SIGCANCEL);
743	return (__sys_setcontext(&uc));
744}
745
746int
747__thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
748{
749	ucontext_t uc;
750
751	if (oucp == NULL || ucp == NULL) {
752		errno = EINVAL;
753		return (-1);
754	}
755	if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) {
756		(void) memcpy(&uc, ucp, sizeof(uc));
757		SIGDELSET(uc.uc_sigmask, SIGCANCEL);
758		ucp = &uc;
759	}
760	return (__sys_swapcontext(oucp, ucp));
761}
762