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 299521 2016-05-12 06:53:22Z kib $
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 *
__libc_sigaction_slot(int signo)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
_thr_signal_block(struct pthread * curthread)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
_thr_signal_unblock(struct pthread * curthread)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
_thr_send_sig(struct pthread * thread,int sig)112 _thr_send_sig(struct pthread *thread, int sig)
113 {
114 return thr_kill(thread->tid, sig);
115 }
116
117 static inline void
remove_thr_signals(sigset_t * set)118 remove_thr_signals(sigset_t *set)
119 {
120 if (SIGISMEMBER(*set, SIGCANCEL))
121 SIGDELSET(*set, SIGCANCEL);
122 }
123
124 static const sigset_t *
thr_remove_thr_signals(const sigset_t * set,sigset_t * newset)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
sigcancel_handler(int sig __unused,siginfo_t * info __unused,ucontext_t * ucp)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
thr_sighandler(int sig,siginfo_t * info,void * _ucp)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
handle_signal(struct sigaction * actp,int sig,siginfo_t * info,ucontext_t * ucp)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
_thr_ast(struct pthread * curthread)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
check_cancel(struct pthread * curthread,ucontext_t * ucp)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
check_deferred_signal(struct pthread * curthread)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
check_suspend(struct pthread * curthread)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) != 0) {
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 }
394 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
395 curthread->critical_count--;
396
397 _thr_signal_unblock(curthread);
398 }
399
400 void
_thr_signal_init(int dlopened)401 _thr_signal_init(int dlopened)
402 {
403 struct sigaction act, nact, oact;
404 struct usigaction *usa;
405 sigset_t oldset;
406 int sig, error;
407
408 if (dlopened) {
409 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
410 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
411 if (sig == SIGCANCEL)
412 continue;
413 error = __sys_sigaction(sig, NULL, &oact);
414 if (error == -1 || oact.sa_handler == SIG_DFL ||
415 oact.sa_handler == SIG_IGN)
416 continue;
417 usa = __libc_sigaction_slot(sig);
418 usa->sigact = oact;
419 nact = oact;
420 remove_thr_signals(&usa->sigact.sa_mask);
421 nact.sa_flags &= ~SA_NODEFER;
422 nact.sa_flags |= SA_SIGINFO;
423 nact.sa_sigaction = thr_sighandler;
424 nact.sa_mask = _thr_maskset;
425 (void)__sys_sigaction(sig, &nact, NULL);
426 }
427 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
428 }
429
430 /* Install SIGCANCEL handler. */
431 SIGFILLSET(act.sa_mask);
432 act.sa_flags = SA_SIGINFO;
433 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
434 __sys_sigaction(SIGCANCEL, &act, NULL);
435
436 /* Unblock SIGCANCEL */
437 SIGEMPTYSET(act.sa_mask);
438 SIGADDSET(act.sa_mask, SIGCANCEL);
439 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
440 }
441
442 void
_thr_sigact_unload(struct dl_phdr_info * phdr_info)443 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
444 {
445 #if 0
446 struct pthread *curthread = _get_curthread();
447 struct urwlock *rwlp;
448 struct sigaction *actp;
449 struct usigaction *usa;
450 struct sigaction kact;
451 void (*handler)(int);
452 int sig;
453
454 _thr_signal_block(curthread);
455 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
456 usa = __libc_sigaction_slot(sig);
457 actp = &usa->sigact;
458 retry:
459 handler = actp->sa_handler;
460 if (handler != SIG_DFL && handler != SIG_IGN &&
461 __elf_phdr_match_addr(phdr_info, handler)) {
462 rwlp = &usa->lock;
463 _thr_rwl_wrlock(rwlp);
464 if (handler != actp->sa_handler) {
465 _thr_rwl_unlock(rwlp);
466 goto retry;
467 }
468 actp->sa_handler = SIG_DFL;
469 actp->sa_flags = SA_SIGINFO;
470 SIGEMPTYSET(actp->sa_mask);
471 if (__sys_sigaction(sig, NULL, &kact) == 0 &&
472 kact.sa_handler != SIG_DFL &&
473 kact.sa_handler != SIG_IGN)
474 __sys_sigaction(sig, actp, NULL);
475 _thr_rwl_unlock(rwlp);
476 }
477 }
478 _thr_signal_unblock(curthread);
479 #endif
480 }
481
482 void
_thr_signal_prefork(void)483 _thr_signal_prefork(void)
484 {
485 int i;
486
487 for (i = 1; i <= _SIG_MAXSIG; ++i)
488 _thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock);
489 }
490
491 void
_thr_signal_postfork(void)492 _thr_signal_postfork(void)
493 {
494 int i;
495
496 for (i = 1; i <= _SIG_MAXSIG; ++i)
497 _thr_rwl_unlock(&__libc_sigaction_slot(i)->lock);
498 }
499
500 void
_thr_signal_postfork_child(void)501 _thr_signal_postfork_child(void)
502 {
503 int i;
504
505 for (i = 1; i <= _SIG_MAXSIG; ++i) {
506 bzero(&__libc_sigaction_slot(i) -> lock,
507 sizeof(struct urwlock));
508 }
509 }
510
511 void
_thr_signal_deinit(void)512 _thr_signal_deinit(void)
513 {
514 }
515
516 int
__thr_sigaction(int sig,const struct sigaction * act,struct sigaction * oact)517 __thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
518 {
519 struct sigaction newact, oldact, oldact2;
520 sigset_t oldset;
521 struct usigaction *usa;
522 int ret, err;
523
524 if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
525 errno = EINVAL;
526 return (-1);
527 }
528
529 ret = 0;
530 err = 0;
531 usa = __libc_sigaction_slot(sig);
532
533 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
534 _thr_rwl_wrlock(&usa->lock);
535
536 if (act != NULL) {
537 oldact2 = usa->sigact;
538 newact = *act;
539
540 /*
541 * if a new sig handler is SIG_DFL or SIG_IGN,
542 * don't remove old handler from __libc_sigact[],
543 * so deferred signals still can use the handlers,
544 * multiple threads invoking sigaction itself is
545 * a race condition, so it is not a problem.
546 */
547 if (newact.sa_handler != SIG_DFL &&
548 newact.sa_handler != SIG_IGN) {
549 usa->sigact = newact;
550 remove_thr_signals(&usa->sigact.sa_mask);
551 newact.sa_flags &= ~SA_NODEFER;
552 newact.sa_flags |= SA_SIGINFO;
553 newact.sa_sigaction = thr_sighandler;
554 newact.sa_mask = _thr_maskset; /* mask all signals */
555 }
556 ret = __sys_sigaction(sig, &newact, &oldact);
557 if (ret == -1) {
558 err = errno;
559 usa->sigact = oldact2;
560 }
561 } else if (oact != NULL) {
562 ret = __sys_sigaction(sig, NULL, &oldact);
563 err = errno;
564 }
565
566 if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) {
567 if (act != NULL)
568 oldact = oldact2;
569 else if (oact != NULL)
570 oldact = usa->sigact;
571 }
572
573 _thr_rwl_unlock(&usa->lock);
574 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
575
576 if (ret == 0) {
577 if (oact != NULL)
578 *oact = oldact;
579 } else {
580 errno = err;
581 }
582 return (ret);
583 }
584
585 int
__thr_sigprocmask(int how,const sigset_t * set,sigset_t * oset)586 __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
587 {
588 const sigset_t *p = set;
589 sigset_t newset;
590
591 if (how != SIG_UNBLOCK) {
592 if (set != NULL) {
593 newset = *set;
594 SIGDELSET(newset, SIGCANCEL);
595 p = &newset;
596 }
597 }
598 return (__sys_sigprocmask(how, p, oset));
599 }
600
601 __weak_reference(_pthread_sigmask, pthread_sigmask);
602
603 int
_pthread_sigmask(int how,const sigset_t * set,sigset_t * oset)604 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
605 {
606
607 if (__thr_sigprocmask(how, set, oset))
608 return (errno);
609 return (0);
610 }
611
612 int
_sigsuspend(const sigset_t * set)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
__thr_sigsuspend(const sigset_t * set)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
_sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)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
__thr_sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)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
_sigwaitinfo(const sigset_t * set,siginfo_t * info)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
__thr_sigwaitinfo(const sigset_t * set,siginfo_t * info)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
_sigwait(const sigset_t * set,int * sig)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
__thr_sigwait(const sigset_t * set,int * sig)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
__thr_setcontext(const ucontext_t * ucp)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
__thr_swapcontext(ucontext_t * oucp,const ucontext_t * ucp)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