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