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 * 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 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 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 113 _thr_send_sig(struct pthread *thread, int sig) 114 { 115 return thr_kill(thread->tid, sig); 116 } 117 118 static inline void 119 remove_thr_signals(sigset_t *set) 120 { 121 if (SIGISMEMBER(*set, SIGCANCEL)) 122 SIGDELSET(*set, SIGCANCEL); 123 } 124 125 static const sigset_t * 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 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 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 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 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 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 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 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 | 378 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) { 379 curthread->cycle++; 380 cycle = curthread->cycle; 381 382 /* Wake the thread suspending us. */ 383 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0); 384 385 /* 386 * if we are from pthread_exit, we don't want to 387 * suspend, just go and die. 388 */ 389 if (curthread->state == PS_DEAD) 390 break; 391 curthread->flags |= THR_FLAGS_SUSPENDED; 392 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); 393 _thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0); 394 THR_UMUTEX_LOCK(curthread, &(curthread)->lock); 395 curthread->flags &= ~THR_FLAGS_SUSPENDED; 396 } 397 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); 398 curthread->critical_count--; 399 400 _thr_signal_unblock(curthread); 401 } 402 403 void 404 _thr_signal_init(int dlopened) 405 { 406 struct sigaction act, nact, oact; 407 struct usigaction *usa; 408 sigset_t oldset; 409 int sig, error; 410 411 if (dlopened) { 412 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset); 413 for (sig = 1; sig <= _SIG_MAXSIG; sig++) { 414 if (sig == SIGCANCEL) 415 continue; 416 error = __sys_sigaction(sig, NULL, &oact); 417 if (error == -1 || oact.sa_handler == SIG_DFL || 418 oact.sa_handler == SIG_IGN) 419 continue; 420 usa = __libc_sigaction_slot(sig); 421 usa->sigact = oact; 422 nact = oact; 423 remove_thr_signals(&usa->sigact.sa_mask); 424 nact.sa_flags &= ~SA_NODEFER; 425 nact.sa_flags |= SA_SIGINFO; 426 nact.sa_sigaction = thr_sighandler; 427 nact.sa_mask = _thr_maskset; 428 (void)__sys_sigaction(sig, &nact, NULL); 429 } 430 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); 431 } 432 433 /* Install SIGCANCEL handler. */ 434 SIGFILLSET(act.sa_mask); 435 act.sa_flags = SA_SIGINFO; 436 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler; 437 __sys_sigaction(SIGCANCEL, &act, NULL); 438 439 /* Unblock SIGCANCEL */ 440 SIGEMPTYSET(act.sa_mask); 441 SIGADDSET(act.sa_mask, SIGCANCEL); 442 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL); 443 } 444 445 void 446 _thr_sigact_unload(struct dl_phdr_info *phdr_info) 447 { 448 #if 0 449 struct pthread *curthread = _get_curthread(); 450 struct urwlock *rwlp; 451 struct sigaction *actp; 452 struct usigaction *usa; 453 struct sigaction kact; 454 void (*handler)(int); 455 int sig; 456 457 _thr_signal_block(curthread); 458 for (sig = 1; sig <= _SIG_MAXSIG; sig++) { 459 usa = __libc_sigaction_slot(sig); 460 actp = &usa->sigact; 461 retry: 462 handler = actp->sa_handler; 463 if (handler != SIG_DFL && handler != SIG_IGN && 464 __elf_phdr_match_addr(phdr_info, handler)) { 465 rwlp = &usa->lock; 466 _thr_rwl_wrlock(rwlp); 467 if (handler != actp->sa_handler) { 468 _thr_rwl_unlock(rwlp); 469 goto retry; 470 } 471 actp->sa_handler = SIG_DFL; 472 actp->sa_flags = SA_SIGINFO; 473 SIGEMPTYSET(actp->sa_mask); 474 if (__sys_sigaction(sig, NULL, &kact) == 0 && 475 kact.sa_handler != SIG_DFL && 476 kact.sa_handler != SIG_IGN) 477 __sys_sigaction(sig, actp, NULL); 478 _thr_rwl_unlock(rwlp); 479 } 480 } 481 _thr_signal_unblock(curthread); 482 #endif 483 } 484 485 void 486 _thr_signal_prefork(void) 487 { 488 int i; 489 490 for (i = 1; i <= _SIG_MAXSIG; ++i) 491 _thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock); 492 } 493 494 void 495 _thr_signal_postfork(void) 496 { 497 int i; 498 499 for (i = 1; i <= _SIG_MAXSIG; ++i) 500 _thr_rwl_unlock(&__libc_sigaction_slot(i)->lock); 501 } 502 503 void 504 _thr_signal_postfork_child(void) 505 { 506 int i; 507 508 for (i = 1; i <= _SIG_MAXSIG; ++i) { 509 bzero(&__libc_sigaction_slot(i) -> lock, 510 sizeof(struct urwlock)); 511 } 512 } 513 514 void 515 _thr_signal_deinit(void) 516 { 517 } 518 519 int 520 __thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) 521 { 522 struct sigaction newact, oldact, oldact2; 523 sigset_t oldset; 524 struct usigaction *usa; 525 int ret, err; 526 527 if (!_SIG_VALID(sig) || sig == SIGCANCEL) { 528 errno = EINVAL; 529 return (-1); 530 } 531 532 ret = 0; 533 err = 0; 534 usa = __libc_sigaction_slot(sig); 535 536 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset); 537 _thr_rwl_wrlock(&usa->lock); 538 539 if (act != NULL) { 540 oldact2 = usa->sigact; 541 newact = *act; 542 543 /* 544 * if a new sig handler is SIG_DFL or SIG_IGN, 545 * don't remove old handler from __libc_sigact[], 546 * so deferred signals still can use the handlers, 547 * multiple threads invoking sigaction itself is 548 * a race condition, so it is not a problem. 549 */ 550 if (newact.sa_handler != SIG_DFL && 551 newact.sa_handler != SIG_IGN) { 552 usa->sigact = newact; 553 remove_thr_signals(&usa->sigact.sa_mask); 554 newact.sa_flags &= ~SA_NODEFER; 555 newact.sa_flags |= SA_SIGINFO; 556 newact.sa_sigaction = thr_sighandler; 557 newact.sa_mask = _thr_maskset; /* mask all signals */ 558 } 559 ret = __sys_sigaction(sig, &newact, &oldact); 560 if (ret == -1) { 561 err = errno; 562 usa->sigact = oldact2; 563 } 564 } else if (oact != NULL) { 565 ret = __sys_sigaction(sig, NULL, &oldact); 566 err = errno; 567 } 568 569 if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) { 570 if (act != NULL) 571 oldact = oldact2; 572 else if (oact != NULL) 573 oldact = usa->sigact; 574 } 575 576 _thr_rwl_unlock(&usa->lock); 577 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); 578 579 if (ret == 0) { 580 if (oact != NULL) 581 *oact = oldact; 582 } else { 583 errno = err; 584 } 585 return (ret); 586 } 587 588 int 589 __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset) 590 { 591 const sigset_t *p = set; 592 sigset_t newset; 593 594 if (how != SIG_UNBLOCK) { 595 if (set != NULL) { 596 newset = *set; 597 SIGDELSET(newset, SIGCANCEL); 598 p = &newset; 599 } 600 } 601 return (__sys_sigprocmask(how, p, oset)); 602 } 603 604 __weak_reference(_pthread_sigmask, pthread_sigmask); 605 606 int 607 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 608 { 609 610 if (__thr_sigprocmask(how, set, oset)) 611 return (errno); 612 return (0); 613 } 614 615 int 616 _sigsuspend(const sigset_t * set) 617 { 618 sigset_t newset; 619 620 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset))); 621 } 622 623 int 624 __thr_sigsuspend(const sigset_t * set) 625 { 626 struct pthread *curthread; 627 sigset_t newset; 628 int ret, old; 629 630 curthread = _get_curthread(); 631 632 old = curthread->in_sigsuspend; 633 curthread->in_sigsuspend = 1; 634 _thr_cancel_enter(curthread); 635 ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset)); 636 _thr_cancel_leave(curthread, 1); 637 curthread->in_sigsuspend = old; 638 if (curthread->unblock_sigcancel) { 639 curthread->unblock_sigcancel = 0; 640 SIGEMPTYSET(newset); 641 SIGADDSET(newset, SIGCANCEL); 642 __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL); 643 } 644 645 return (ret); 646 } 647 648 int 649 _sigtimedwait(const sigset_t *set, siginfo_t *info, 650 const struct timespec * timeout) 651 { 652 sigset_t newset; 653 654 return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info, 655 timeout)); 656 } 657 658 /* 659 * Cancellation behavior: 660 * Thread may be canceled at start, if thread got signal, 661 * it is not canceled. 662 */ 663 int 664 __thr_sigtimedwait(const sigset_t *set, siginfo_t *info, 665 const struct timespec * timeout) 666 { 667 struct pthread *curthread = _get_curthread(); 668 sigset_t newset; 669 int ret; 670 671 _thr_cancel_enter(curthread); 672 ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info, 673 timeout); 674 _thr_cancel_leave(curthread, (ret == -1)); 675 return (ret); 676 } 677 678 int 679 _sigwaitinfo(const sigset_t *set, siginfo_t *info) 680 { 681 sigset_t newset; 682 683 return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info)); 684 } 685 686 /* 687 * Cancellation behavior: 688 * Thread may be canceled at start, if thread got signal, 689 * it is not canceled. 690 */ 691 int 692 __thr_sigwaitinfo(const sigset_t *set, siginfo_t *info) 693 { 694 struct pthread *curthread = _get_curthread(); 695 sigset_t newset; 696 int ret; 697 698 _thr_cancel_enter(curthread); 699 ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info); 700 _thr_cancel_leave(curthread, ret == -1); 701 return (ret); 702 } 703 704 int 705 _sigwait(const sigset_t *set, int *sig) 706 { 707 sigset_t newset; 708 709 return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig)); 710 } 711 712 /* 713 * Cancellation behavior: 714 * Thread may be canceled at start, if thread got signal, 715 * it is not canceled. 716 */ 717 int 718 __thr_sigwait(const sigset_t *set, int *sig) 719 { 720 struct pthread *curthread = _get_curthread(); 721 sigset_t newset; 722 int ret; 723 724 do { 725 _thr_cancel_enter(curthread); 726 ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig); 727 _thr_cancel_leave(curthread, (ret != 0)); 728 } while (ret == EINTR); 729 return (ret); 730 } 731 732 int 733 __thr_setcontext(const ucontext_t *ucp) 734 { 735 ucontext_t uc; 736 737 if (ucp == NULL) { 738 errno = EINVAL; 739 return (-1); 740 } 741 if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL)) 742 return __sys_setcontext(ucp); 743 (void) memcpy(&uc, ucp, sizeof(uc)); 744 SIGDELSET(uc.uc_sigmask, SIGCANCEL); 745 return (__sys_setcontext(&uc)); 746 } 747 748 int 749 __thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp) 750 { 751 ucontext_t uc; 752 753 if (oucp == NULL || ucp == NULL) { 754 errno = EINVAL; 755 return (-1); 756 } 757 if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) { 758 (void) memcpy(&uc, ucp, sizeof(uc)); 759 SIGDELSET(uc.uc_sigmask, SIGCANCEL); 760 ucp = &uc; 761 } 762 return (__sys_swapcontext(oucp, ucp)); 763 } 764