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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "namespace.h"
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/signalvar.h>
34 #include <sys/syscall.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include "un-namespace.h"
41 #include "libc_private.h"
42
43 #include "libc_private.h"
44 #include "thr_private.h"
45
46 /* #define DEBUG_SIGNAL */
47 #ifdef DEBUG_SIGNAL
48 #define DBG_MSG stdout_debug
49 #else
50 #define DBG_MSG(x...)
51 #endif
52
53 struct usigaction {
54 struct sigaction sigact;
55 struct urwlock lock;
56 };
57
58 static struct usigaction _thr_sigact[_SIG_MAXSIG];
59
60 static inline struct usigaction *
__libc_sigaction_slot(int signo)61 __libc_sigaction_slot(int signo)
62 {
63
64 return (&_thr_sigact[signo - 1]);
65 }
66
67 static void thr_sighandler(int, siginfo_t *, void *);
68 static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
69 static void check_deferred_signal(struct pthread *);
70 static void check_suspend(struct pthread *);
71 static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
72
73 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
74 const struct timespec * timeout);
75 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
76 int _sigwait(const sigset_t *set, int *sig);
77 int _setcontext(const ucontext_t *);
78 int _swapcontext(ucontext_t *, const ucontext_t *);
79
80 static const sigset_t _thr_deferset={{
81 0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)|
82 _SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)),
83 0xffffffff,
84 0xffffffff,
85 0xffffffff}};
86
87 static const sigset_t _thr_maskset={{
88 0xffffffff,
89 0xffffffff,
90 0xffffffff,
91 0xffffffff}};
92
93 void
_thr_signal_block(struct pthread * curthread)94 _thr_signal_block(struct pthread *curthread)
95 {
96
97 if (curthread->sigblock > 0) {
98 curthread->sigblock++;
99 return;
100 }
101 __sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask);
102 curthread->sigblock++;
103 }
104
105 void
_thr_signal_unblock(struct pthread * curthread)106 _thr_signal_unblock(struct pthread *curthread)
107 {
108 if (--curthread->sigblock == 0)
109 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
110 }
111
112 int
_thr_send_sig(struct pthread * thread,int sig)113 _thr_send_sig(struct pthread *thread, int sig)
114 {
115 return thr_kill(thread->tid, sig);
116 }
117
118 static inline void
remove_thr_signals(sigset_t * set)119 remove_thr_signals(sigset_t *set)
120 {
121 if (SIGISMEMBER(*set, SIGCANCEL))
122 SIGDELSET(*set, SIGCANCEL);
123 }
124
125 static const sigset_t *
thr_remove_thr_signals(const sigset_t * set,sigset_t * newset)126 thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
127 {
128 *newset = *set;
129 remove_thr_signals(newset);
130 return (newset);
131 }
132
133 static void
sigcancel_handler(int sig __unused,siginfo_t * info __unused,ucontext_t * ucp)134 sigcancel_handler(int sig __unused,
135 siginfo_t *info __unused, ucontext_t *ucp)
136 {
137 struct pthread *curthread = _get_curthread();
138 int err;
139
140 if (THR_IN_CRITICAL(curthread))
141 return;
142 err = errno;
143 check_suspend(curthread);
144 check_cancel(curthread, ucp);
145 errno = err;
146 }
147
148 typedef void (*ohandler)(int sig, int code, struct sigcontext *scp,
149 char *addr, __sighandler_t *catcher);
150
151 /*
152 * The signal handler wrapper is entered with all signal masked.
153 */
154 static void
thr_sighandler(int sig,siginfo_t * info,void * _ucp)155 thr_sighandler(int sig, siginfo_t *info, void *_ucp)
156 {
157 struct pthread *curthread;
158 ucontext_t *ucp;
159 struct sigaction act;
160 struct usigaction *usa;
161 int err;
162
163 err = errno;
164 curthread = _get_curthread();
165 ucp = _ucp;
166 usa = __libc_sigaction_slot(sig);
167 _thr_rwl_rdlock(&usa->lock);
168 act = usa->sigact;
169 _thr_rwl_unlock(&usa->lock);
170 errno = err;
171 curthread->deferred_run = 0;
172
173 /*
174 * if a thread is in critical region, for example it holds low level locks,
175 * try to defer the signal processing, however if the signal is synchronous
176 * signal, it means a bad thing has happened, this is a programming error,
177 * resuming fault point can not help anything (normally causes deadloop),
178 * so here we let user code handle it immediately.
179 */
180 if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
181 memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
182 memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
183 curthread->deferred_sigmask = ucp->uc_sigmask;
184 /* mask all signals, we will restore it later. */
185 ucp->uc_sigmask = _thr_deferset;
186 return;
187 }
188
189 handle_signal(&act, sig, info, ucp);
190 }
191
192 static void
handle_signal(struct sigaction * actp,int sig,siginfo_t * info,ucontext_t * ucp)193 handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
194 {
195 struct pthread *curthread = _get_curthread();
196 ucontext_t uc2;
197 __siginfohandler_t *sigfunc;
198 int cancel_point;
199 int cancel_async;
200 int cancel_enable;
201 int in_sigsuspend;
202 int err;
203
204 /* add previous level mask */
205 SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
206
207 /* add this signal's mask */
208 if (!(actp->sa_flags & SA_NODEFER))
209 SIGADDSET(actp->sa_mask, sig);
210
211 in_sigsuspend = curthread->in_sigsuspend;
212 curthread->in_sigsuspend = 0;
213
214 /*
215 * If thread is in deferred cancellation mode, disable cancellation
216 * in signal handler.
217 * If user signal handler calls a cancellation point function, e.g,
218 * it calls write() to write data to file, because write() is a
219 * cancellation point, the thread is immediately cancelled if
220 * cancellation is pending, to avoid this problem while thread is in
221 * deferring mode, cancellation is temporarily disabled.
222 */
223 cancel_point = curthread->cancel_point;
224 cancel_async = curthread->cancel_async;
225 cancel_enable = curthread->cancel_enable;
226 curthread->cancel_point = 0;
227 if (!cancel_async)
228 curthread->cancel_enable = 0;
229
230 /* restore correct mask before calling user handler */
231 __sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
232
233 sigfunc = actp->sa_sigaction;
234
235 /*
236 * We have already reset cancellation point flags, so if user's code
237 * longjmp()s out of its signal handler, wish its jmpbuf was set
238 * outside of a cancellation point, in most cases, this would be
239 * true. However, there is no way to save cancel_enable in jmpbuf,
240 * so after setjmps() returns once more, the user code may need to
241 * re-set cancel_enable flag by calling pthread_setcancelstate().
242 */
243 if ((actp->sa_flags & SA_SIGINFO) != 0) {
244 sigfunc(sig, info, ucp);
245 } else {
246 ((ohandler)sigfunc)(sig, info->si_code,
247 (struct sigcontext *)ucp, info->si_addr,
248 (__sighandler_t *)sigfunc);
249 }
250 err = errno;
251
252 curthread->in_sigsuspend = in_sigsuspend;
253 curthread->cancel_point = cancel_point;
254 curthread->cancel_enable = cancel_enable;
255
256 memcpy(&uc2, ucp, sizeof(uc2));
257 SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
258
259 /* reschedule cancellation */
260 check_cancel(curthread, &uc2);
261 errno = err;
262 syscall(SYS_sigreturn, &uc2);
263 }
264
265 void
_thr_ast(struct pthread * curthread)266 _thr_ast(struct pthread *curthread)
267 {
268
269 if (!THR_IN_CRITICAL(curthread)) {
270 check_deferred_signal(curthread);
271 check_suspend(curthread);
272 check_cancel(curthread, NULL);
273 }
274 }
275
276 /* reschedule cancellation */
277 static void
check_cancel(struct pthread * curthread,ucontext_t * ucp)278 check_cancel(struct pthread *curthread, ucontext_t *ucp)
279 {
280
281 if (__predict_true(!curthread->cancel_pending ||
282 !curthread->cancel_enable || curthread->no_cancel))
283 return;
284
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 by 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 } else if (curthread->cancel_async) {
313 /*
314 * asynchronous cancellation mode, act upon
315 * immediately.
316 */
317 _pthread_exit_mask(PTHREAD_CANCELED,
318 ucp? &ucp->uc_sigmask : NULL);
319 }
320 }
321
322 static void
check_deferred_signal(struct pthread * curthread)323 check_deferred_signal(struct pthread *curthread)
324 {
325 ucontext_t *uc;
326 struct sigaction act;
327 siginfo_t info;
328 int uc_len;
329
330 if (__predict_true(curthread->deferred_siginfo.si_signo == 0 ||
331 curthread->deferred_run))
332 return;
333
334 curthread->deferred_run = 1;
335 uc_len = __getcontextx_size();
336 uc = alloca(uc_len);
337 getcontext(uc);
338 if (curthread->deferred_siginfo.si_signo == 0) {
339 curthread->deferred_run = 0;
340 return;
341 }
342 __fillcontextx2((char *)uc);
343 act = curthread->deferred_sigact;
344 uc->uc_sigmask = curthread->deferred_sigmask;
345 memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
346 /* remove signal */
347 curthread->deferred_siginfo.si_signo = 0;
348 handle_signal(&act, info.si_signo, &info, uc);
349 }
350
351 static void
check_suspend(struct pthread * curthread)352 check_suspend(struct pthread *curthread)
353 {
354 uint32_t cycle;
355
356 if (__predict_true((curthread->flags &
357 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
358 != THR_FLAGS_NEED_SUSPEND))
359 return;
360 if (curthread == _single_thread)
361 return;
362 if (curthread->force_exit)
363 return;
364
365 /*
366 * Blocks SIGCANCEL which other threads must send.
367 */
368 _thr_signal_block(curthread);
369
370 /*
371 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
372 * because we are leaf code, we don't want to recursively call
373 * ourself.
374 */
375 curthread->critical_count++;
376 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
377 while ((curthread->flags & THR_FLAGS_NEED_SUSPEND) != 0) {
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 }
395 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
396 curthread->critical_count--;
397
398 _thr_signal_unblock(curthread);
399 }
400
401 void
_thr_signal_init(int dlopened)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
_thr_sigact_unload(struct dl_phdr_info * phdr_info __unused)444 _thr_sigact_unload(struct dl_phdr_info *phdr_info __unused)
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
_thr_signal_prefork(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
_thr_signal_postfork(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
_thr_signal_postfork_child(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
_thr_signal_deinit(void)513 _thr_signal_deinit(void)
514 {
515 }
516
517 int
__thr_sigaction(int sig,const struct sigaction * act,struct sigaction * oact)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
__thr_sigprocmask(int how,const sigset_t * set,sigset_t * oset)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
_pthread_sigmask(int how,const sigset_t * set,sigset_t * oset)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
613 int
_sigsuspend(const sigset_t * set)614 _sigsuspend(const sigset_t * set)
615 {
616 sigset_t newset;
617
618 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
619 }
620
621 int
__thr_sigsuspend(const sigset_t * set)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
646 int
_sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)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 */
661 int
__thr_sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)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
676 int
_sigwaitinfo(const sigset_t * set,siginfo_t * info)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 */
689 int
__thr_sigwaitinfo(const sigset_t * set,siginfo_t * info)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
702 int
_sigwait(const sigset_t * set,int * sig)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 */
715 int
__thr_sigwait(const sigset_t * set,int * sig)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
730 int
__thr_setcontext(const ucontext_t * ucp)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(ucp->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
746 int
__thr_swapcontext(ucontext_t * oucp,const ucontext_t * ucp)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