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