1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * Copyright 2015 Joyent, Inc. 26 */ 27 28 #include "lint.h" 29 #include <sys/feature_tests.h> 30 /* 31 * setcontext() really can return, if UC_CPU is not specified. 32 * Make the compiler shut up about it. 33 */ 34 #if defined(__NORETURN) 35 #undef __NORETURN 36 #endif 37 #define __NORETURN 38 #include "thr_uberdata.h" 39 #include "asyncio.h" 40 #include <signal.h> 41 #include <siginfo.h> 42 #include <sys/systm.h> 43 44 /* maskable signals */ 45 const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3}; 46 47 /* 48 * Return true if the valid signal bits in both sets are the same. 49 */ 50 int 51 sigequalset(const sigset_t *s1, const sigset_t *s2) 52 { 53 /* 54 * We only test valid signal bits, not rubbish following MAXSIG 55 * (for speed). Algorithm: 56 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0 57 */ 58 /* see lib/libc/inc/thr_uberdata.h for why this must be true */ 59 #if (MAXSIG > (2 * 32) && MAXSIG <= (3 * 32)) 60 return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) | 61 (s1->__sigbits[1] ^ s2->__sigbits[1]) | 62 ((s1->__sigbits[2] ^ s2->__sigbits[2]) & FILLSET2))); 63 #else 64 #error "fix me: MAXSIG out of bounds" 65 #endif 66 } 67 68 /* 69 * Common code for calling the user-specified signal handler. 70 */ 71 void 72 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp) 73 { 74 ulwp_t *self = curthread; 75 uberdata_t *udp = self->ul_uberdata; 76 struct sigaction uact; 77 volatile struct sigaction *sap; 78 79 /* 80 * If we are taking a signal while parked or about to be parked 81 * on __lwp_park() then remove ourself from the sleep queue so 82 * that we can grab locks. The code in mutex_lock_queue() and 83 * cond_wait_common() will detect this and deal with it when 84 * __lwp_park() returns. 85 */ 86 unsleep_self(); 87 set_parking_flag(self, 0); 88 89 if (__td_event_report(self, TD_CATCHSIG, udp)) { 90 self->ul_td_evbuf.eventnum = TD_CATCHSIG; 91 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig; 92 tdb_event(TD_CATCHSIG, udp); 93 } 94 95 /* 96 * Get a self-consistent set of flags, handler, and mask 97 * while holding the sig's sig_lock for the least possible time. 98 * We must acquire the sig's sig_lock because some thread running 99 * in sigaction() might be establishing a new signal handler. 100 * The code in sigaction() acquires the writer lock; here 101 * we acquire the readers lock to ehance concurrency in the 102 * face of heavy signal traffic, such as generated by java. 103 * 104 * Locking exceptions: 105 * No locking for a child of vfork(). 106 * If the signal is SIGPROF with an si_code of PROF_SIG, 107 * then we assume that this signal was generated by 108 * setitimer(ITIMER_REALPROF) set up by the dbx collector. 109 * If the signal is SIGEMT with an si_code of EMT_CPCOVF, 110 * then we assume that the signal was generated by 111 * a hardware performance counter overflow. 112 * In these cases, assume that we need no locking. It is the 113 * monitoring program's responsibility to ensure correctness. 114 */ 115 sap = &udp->siguaction[sig].sig_uaction; 116 if (self->ul_vfork || 117 (sip != NULL && 118 ((sig == SIGPROF && sip->si_code == PROF_SIG) || 119 (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) { 120 /* we wish this assignment could be atomic */ 121 (void) memcpy(&uact, (void *)sap, sizeof (uact)); 122 } else { 123 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock; 124 lrw_rdlock(rwlp); 125 (void) memcpy(&uact, (void *)sap, sizeof (uact)); 126 if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) && 127 (sap->sa_flags & SA_RESETHAND)) 128 sap->sa_sigaction = SIG_DFL; 129 lrw_unlock(rwlp); 130 } 131 132 /* 133 * Set the proper signal mask and call the user's signal handler. 134 * (We overrode the user-requested signal mask with maskset 135 * so we currently have all blockable signals blocked.) 136 * 137 * We would like to ASSERT() that the signal is not a member of the 138 * signal mask at the previous level (ucp->uc_sigmask) or the specified 139 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but 140 * /proc can override this via PCSSIG, so we don't bother. 141 * 142 * We would also like to ASSERT() that the signal mask at the previous 143 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()), 144 * but /proc can change the thread's signal mask via PCSHOLD, so we 145 * don't bother with that either. 146 */ 147 ASSERT(ucp->uc_flags & UC_SIGMASK); 148 if (self->ul_sigsuspend) { 149 ucp->uc_sigmask = self->ul_sigmask; 150 self->ul_sigsuspend = 0; 151 /* the sigsuspend() or pollsys() signal mask */ 152 sigorset(&uact.sa_mask, &self->ul_tmpmask); 153 } else { 154 /* the signal mask at the previous level */ 155 sigorset(&uact.sa_mask, &ucp->uc_sigmask); 156 } 157 if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */ 158 (void) sigaddset(&uact.sa_mask, sig); 159 self->ul_sigmask = uact.sa_mask; 160 self->ul_siglink = ucp; 161 (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask); 162 163 /* 164 * If this thread has been sent SIGCANCEL from the kernel 165 * or from pthread_cancel(), it is being asked to exit. 166 * The kernel may send SIGCANCEL without a siginfo struct. 167 * If the SIGCANCEL is process-directed (from kill() or 168 * sigqueue()), treat it as an ordinary signal. 169 */ 170 if (sig == SIGCANCEL) { 171 if (sip == NULL || SI_FROMKERNEL(sip) || 172 sip->si_code == SI_LWP) { 173 do_sigcancel(); 174 goto out; 175 } 176 /* SIGCANCEL is ignored by default */ 177 if (uact.sa_sigaction == SIG_DFL || 178 uact.sa_sigaction == SIG_IGN) 179 goto out; 180 } 181 182 /* 183 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and 184 * we are an aio worker thread, cancel the aio request. 185 */ 186 if (sig == SIGAIOCANCEL) { 187 aio_worker_t *aiowp = pthread_getspecific(_aio_key); 188 189 if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL) 190 siglongjmp(aiowp->work_jmp_buf, 1); 191 /* SIGLWP is ignored by default */ 192 if (uact.sa_sigaction == SIG_DFL || 193 uact.sa_sigaction == SIG_IGN) 194 goto out; 195 } 196 197 if (!(uact.sa_flags & SA_SIGINFO)) 198 sip = NULL; 199 __sighndlr(sig, sip, ucp, uact.sa_sigaction); 200 201 #if defined(sparc) || defined(__sparc) 202 /* 203 * If this is a floating point exception and the queue 204 * is non-empty, pop the top entry from the queue. This 205 * is to maintain expected behavior. 206 */ 207 if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) { 208 fpregset_t *fp = &ucp->uc_mcontext.fpregs; 209 210 if (--fp->fpu_qcnt > 0) { 211 unsigned char i; 212 struct fq *fqp; 213 214 fqp = fp->fpu_q; 215 for (i = 0; i < fp->fpu_qcnt; i++) 216 fqp[i] = fqp[i+1]; 217 } 218 } 219 #endif /* sparc */ 220 221 out: 222 (void) setcontext(ucp); 223 thr_panic("call_user_handler(): setcontext() returned"); 224 } 225 226 /* 227 * take_deferred_signal() is called when ul_critical and ul_sigdefer become 228 * zero and a deferred signal has been recorded on the current thread. 229 * We are out of the critical region and are ready to take a signal. 230 * The kernel has all signals blocked on this lwp, but our value of 231 * ul_sigmask is the correct signal mask for the previous context. 232 * 233 * We call __sigresend() to atomically restore the signal mask and 234 * cause the signal to be sent again with the remembered siginfo. 235 * We will not return successfully from __sigresend() until the 236 * application's signal handler has been run via sigacthandler(). 237 */ 238 void 239 take_deferred_signal(int sig) 240 { 241 extern int __sigresend(int, siginfo_t *, sigset_t *); 242 ulwp_t *self = curthread; 243 siguaction_t *suap = &self->ul_uberdata->siguaction[sig]; 244 siginfo_t *sip; 245 int error; 246 247 ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0); 248 249 /* 250 * If the signal handler was established with SA_RESETHAND, 251 * the kernel has reset the handler to SIG_DFL, so we have 252 * to reestablish the handler now so that it will be entered 253 * again when we call __sigresend(), below. 254 * 255 * Logically, we should acquire and release the signal's 256 * sig_lock around this operation to protect the integrity 257 * of the signal action while we copy it, as is done below 258 * in _libc_sigaction(). However, we may be on a user-level 259 * sleep queue at this point and lrw_wrlock(&suap->sig_lock) 260 * might attempt to sleep on a different sleep queue and 261 * that would corrupt the entire sleep queue mechanism. 262 * 263 * If we are on a sleep queue we will remove ourself from 264 * it in call_user_handler(), called from sigacthandler(), 265 * before entering the application's signal handler. 266 * In the meantime, we must not acquire any locks. 267 */ 268 if (suap->sig_uaction.sa_flags & SA_RESETHAND) { 269 struct sigaction tact = suap->sig_uaction; 270 tact.sa_flags &= ~SA_NODEFER; 271 tact.sa_sigaction = self->ul_uberdata->sigacthandler; 272 tact.sa_mask = maskset; 273 (void) __sigaction(sig, &tact, NULL); 274 } 275 276 if (self->ul_siginfo.si_signo == 0) 277 sip = NULL; 278 else 279 sip = &self->ul_siginfo; 280 281 /* EAGAIN can happen only for a pending SIGSTOP signal */ 282 while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN) 283 continue; 284 if (error) 285 thr_panic("take_deferred_signal(): __sigresend() failed"); 286 } 287 288 /* 289 * sigacthandler() attempts to clean up dangling uc_link pointers in 290 * signal handling contexts when libc believes us to have escaped 291 * a signal handler incorrectly in the past. 292 * 293 * Branded processes have a legitimate use for a chain including contexts 294 * other than those used for signal handling when tracking emulation 295 * requests from the kernel. We allow them to disable this cleanup 296 * behaviour. 297 */ 298 static int escaped_context_cleanup = 1; 299 300 void 301 set_escaped_context_cleanup(int on) 302 { 303 escaped_context_cleanup = on; 304 } 305 306 void 307 sigacthandler(int sig, siginfo_t *sip, void *uvp) 308 { 309 ucontext_t *ucp = uvp; 310 ulwp_t *self = curthread; 311 312 /* 313 * Do this in case we took a signal while in a cancelable system call. 314 * It does no harm if we were not in such a system call. 315 */ 316 self->ul_sp = 0; 317 if (sig != SIGCANCEL) 318 self->ul_cancel_async = self->ul_save_async; 319 320 /* 321 * If this thread has performed a longjmp() from a signal handler 322 * back to main level some time in the past, it has left the kernel 323 * thinking that it is still in the signal context. We repair this 324 * possible damage by setting ucp->uc_link to NULL if we know that 325 * we are actually executing at main level (self->ul_siglink == NULL). 326 * See the code for setjmp()/longjmp() for more details. 327 */ 328 if (escaped_context_cleanup && self->ul_siglink == NULL) 329 ucp->uc_link = NULL; 330 331 /* 332 * If we are not in a critical region and are 333 * not deferring signals, take the signal now. 334 */ 335 if ((self->ul_critical + self->ul_sigdefer) == 0) { 336 call_user_handler(sig, sip, ucp); 337 /* 338 * On the surface, the following call seems redundant 339 * because call_user_handler() cannot return. However, 340 * we don't want to return from here because the compiler 341 * might recycle our frame. We want to keep it on the 342 * stack to assist debuggers such as pstack in identifying 343 * signal frames. The call to thr_panic() serves to prevent 344 * tail-call optimisation here. 345 */ 346 thr_panic("sigacthandler(): call_user_handler() returned"); 347 } 348 349 /* 350 * We are in a critical region or we are deferring signals. When 351 * we emerge from the region we will call take_deferred_signal(). 352 */ 353 ASSERT(self->ul_cursig == 0); 354 self->ul_cursig = (char)sig; 355 if (sip != NULL) 356 (void) memcpy(&self->ul_siginfo, 357 sip, sizeof (siginfo_t)); 358 else 359 self->ul_siginfo.si_signo = 0; 360 361 /* 362 * Make sure that if we return to a call to __lwp_park() 363 * or ___lwp_cond_wait() that it returns right away 364 * (giving us a spurious wakeup but not a deadlock). 365 */ 366 set_parking_flag(self, 0); 367 368 /* 369 * Return to the previous context with all signals blocked. 370 * We will restore the signal mask in take_deferred_signal(). 371 * Note that we are calling the system call trap here, not 372 * the setcontext() wrapper. We don't want to change the 373 * thread's ul_sigmask by this operation. 374 */ 375 ucp->uc_sigmask = maskset; 376 (void) __setcontext(ucp); 377 thr_panic("sigacthandler(): __setcontext() returned"); 378 } 379 380 #pragma weak _sigaction = sigaction 381 int 382 sigaction(int sig, const struct sigaction *nact, struct sigaction *oact) 383 { 384 ulwp_t *self = curthread; 385 uberdata_t *udp = self->ul_uberdata; 386 struct sigaction oaction; 387 struct sigaction tact; 388 struct sigaction *tactp = NULL; 389 int rv; 390 391 if (sig <= 0 || sig >= NSIG) { 392 errno = EINVAL; 393 return (-1); 394 } 395 396 if (!self->ul_vfork) 397 lrw_wrlock(&udp->siguaction[sig].sig_lock); 398 399 oaction = udp->siguaction[sig].sig_uaction; 400 401 if (nact != NULL) { 402 tact = *nact; /* make a copy so we can modify it */ 403 tactp = &tact; 404 delete_reserved_signals(&tact.sa_mask); 405 406 #if !defined(_LP64) 407 tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */ 408 #endif 409 /* 410 * To be compatible with the behavior of SunOS 4.x: 411 * If the new signal handler is SIG_IGN or SIG_DFL, do 412 * not change the signal's entry in the siguaction array. 413 * This allows a child of vfork(2) to set signal handlers 414 * to SIG_IGN or SIG_DFL without affecting the parent. 415 * 416 * This also covers a race condition with some thread 417 * setting the signal action to SIG_DFL or SIG_IGN 418 * when the thread has also received and deferred 419 * that signal. When the thread takes the deferred 420 * signal, even though it has set the action to SIG_DFL 421 * or SIG_IGN, it will execute the old signal handler 422 * anyway. This is an inherent signaling race condition 423 * and is not a bug. 424 * 425 * A child of vfork() is not allowed to change signal 426 * handlers to anything other than SIG_DFL or SIG_IGN. 427 */ 428 if (self->ul_vfork) { 429 if (tact.sa_sigaction != SIG_IGN) 430 tact.sa_sigaction = SIG_DFL; 431 } else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) { 432 /* 433 * Always catch these signals. 434 * We need SIGCANCEL for pthread_cancel() to work. 435 * We need SIGAIOCANCEL for aio_cancel() to work. 436 */ 437 udp->siguaction[sig].sig_uaction = tact; 438 if (tact.sa_sigaction == SIG_DFL || 439 tact.sa_sigaction == SIG_IGN) 440 tact.sa_flags = SA_SIGINFO; 441 else { 442 tact.sa_flags |= SA_SIGINFO; 443 tact.sa_flags &= 444 ~(SA_NODEFER | SA_RESETHAND | SA_RESTART); 445 } 446 tact.sa_sigaction = udp->sigacthandler; 447 tact.sa_mask = maskset; 448 } else if (tact.sa_sigaction != SIG_DFL && 449 tact.sa_sigaction != SIG_IGN) { 450 udp->siguaction[sig].sig_uaction = tact; 451 tact.sa_flags &= ~SA_NODEFER; 452 tact.sa_sigaction = udp->sigacthandler; 453 tact.sa_mask = maskset; 454 } 455 } 456 457 if ((rv = __sigaction(sig, tactp, oact)) != 0) 458 udp->siguaction[sig].sig_uaction = oaction; 459 else if (oact != NULL && 460 oact->sa_sigaction != SIG_DFL && 461 oact->sa_sigaction != SIG_IGN) 462 *oact = oaction; 463 464 /* 465 * We detect setting the disposition of SIGIO just to set the 466 * _sigio_enabled flag for the asynchronous i/o (aio) code. 467 */ 468 if (sig == SIGIO && rv == 0 && tactp != NULL) { 469 _sigio_enabled = 470 (tactp->sa_handler != SIG_DFL && 471 tactp->sa_handler != SIG_IGN); 472 } 473 474 if (!self->ul_vfork) 475 lrw_unlock(&udp->siguaction[sig].sig_lock); 476 return (rv); 477 } 478 479 /* 480 * This is a private interface for the lx brand. 481 */ 482 void 483 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *), 484 void (**osigacthandler)(int, siginfo_t *, void *), 485 int (*brsetctxt)(const ucontext_t *)) 486 { 487 ulwp_t *self = curthread; 488 uberdata_t *udp = self->ul_uberdata; 489 490 if (osigacthandler != NULL) 491 *osigacthandler = udp->sigacthandler; 492 493 udp->sigacthandler = nsigacthandler; 494 495 if (brsetctxt != NULL) 496 udp->setctxt = brsetctxt; 497 } 498 499 /* 500 * Tell the kernel to block all signals. 501 * Use the schedctl interface, or failing that, use __lwp_sigmask(). 502 * This action can be rescinded only by making a system call that 503 * sets the signal mask: 504 * __lwp_sigmask(), __sigprocmask(), __setcontext(), 505 * __sigsuspend() or __pollsys(). 506 * In particular, this action cannot be reversed by assigning 507 * scp->sc_sigblock = 0. That would be a way to lose signals. 508 * See the definition of restore_signals(self). 509 */ 510 void 511 block_all_signals(ulwp_t *self) 512 { 513 volatile sc_shared_t *scp; 514 515 enter_critical(self); 516 if ((scp = self->ul_schedctl) != NULL || 517 (scp = setup_schedctl()) != NULL) 518 scp->sc_sigblock = 1; 519 else 520 (void) __lwp_sigmask(SIG_SETMASK, &maskset); 521 exit_critical(self); 522 } 523 524 /* 525 * setcontext() has code that forcibly restores the curthread 526 * pointer in a context passed to the setcontext(2) syscall. 527 * 528 * Certain processes may need to disable this feature, so these routines 529 * provide the mechanism to do so. 530 * 531 * (As an example, branded 32-bit x86 processes may use %gs for their own 532 * purposes, so they need to be able to specify a %gs value to be restored 533 * on return from a signal handler via the passed ucontext_t.) 534 */ 535 static int setcontext_enforcement = 1; 536 537 void 538 set_setcontext_enforcement(int on) 539 { 540 setcontext_enforcement = on; 541 } 542 543 #pragma weak _setcontext = setcontext 544 int 545 setcontext(const ucontext_t *ucp) 546 { 547 ulwp_t *self = curthread; 548 uberdata_t *udp = self->ul_uberdata; 549 int ret; 550 ucontext_t uc; 551 552 /* 553 * Returning from the main context (uc_link == NULL) causes 554 * the thread to exit. See setcontext(2) and makecontext(3C). 555 */ 556 if (ucp == NULL) 557 thr_exit(NULL); 558 (void) memcpy(&uc, ucp, sizeof (uc)); 559 560 /* 561 * Restore previous signal mask and context link. 562 */ 563 if (uc.uc_flags & UC_SIGMASK) { 564 block_all_signals(self); 565 delete_reserved_signals(&uc.uc_sigmask); 566 self->ul_sigmask = uc.uc_sigmask; 567 if (self->ul_cursig) { 568 /* 569 * We have a deferred signal present. 570 * The signal mask will be set when the 571 * signal is taken in take_deferred_signal(). 572 */ 573 ASSERT(self->ul_critical + self->ul_sigdefer != 0); 574 uc.uc_flags &= ~UC_SIGMASK; 575 } 576 } 577 self->ul_siglink = uc.uc_link; 578 579 /* 580 * We don't know where this context structure has been. 581 * Preserve the curthread pointer, at least. 582 * 583 * Allow this feature to be disabled if a particular process 584 * requests it. 585 */ 586 if (setcontext_enforcement) { 587 #if defined(__sparc) 588 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self; 589 #elif defined(__amd64) 590 uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */ 591 #elif defined(__i386) 592 uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL; 593 #else 594 #error "none of __sparc, __amd64, __i386 defined" 595 #endif 596 } 597 598 /* 599 * Make sure that if we return to a call to __lwp_park() 600 * or ___lwp_cond_wait() that it returns right away 601 * (giving us a spurious wakeup but not a deadlock). 602 */ 603 set_parking_flag(self, 0); 604 self->ul_sp = 0; 605 ret = udp->setctxt(&uc); 606 607 /* 608 * It is OK for setcontext() to return if the user has not specified 609 * UC_CPU. 610 */ 611 if (uc.uc_flags & UC_CPU) 612 thr_panic("setcontext(): __setcontext() returned"); 613 return (ret); 614 } 615 616 #pragma weak _thr_sigsetmask = thr_sigsetmask 617 int 618 thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset) 619 { 620 ulwp_t *self = curthread; 621 sigset_t saveset; 622 623 if (set == NULL) { 624 enter_critical(self); 625 if (oset != NULL) 626 *oset = self->ul_sigmask; 627 exit_critical(self); 628 } else { 629 switch (how) { 630 case SIG_BLOCK: 631 case SIG_UNBLOCK: 632 case SIG_SETMASK: 633 break; 634 default: 635 return (EINVAL); 636 } 637 638 /* 639 * The assignments to self->ul_sigmask must be protected from 640 * signals. The nuances of this code are subtle. Be careful. 641 */ 642 block_all_signals(self); 643 if (oset != NULL) 644 saveset = self->ul_sigmask; 645 switch (how) { 646 case SIG_BLOCK: 647 self->ul_sigmask.__sigbits[0] |= set->__sigbits[0]; 648 self->ul_sigmask.__sigbits[1] |= set->__sigbits[1]; 649 self->ul_sigmask.__sigbits[2] |= set->__sigbits[2]; 650 self->ul_sigmask.__sigbits[3] |= set->__sigbits[3]; 651 break; 652 case SIG_UNBLOCK: 653 self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0]; 654 self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1]; 655 self->ul_sigmask.__sigbits[2] &= ~set->__sigbits[2]; 656 self->ul_sigmask.__sigbits[3] &= ~set->__sigbits[3]; 657 break; 658 case SIG_SETMASK: 659 self->ul_sigmask.__sigbits[0] = set->__sigbits[0]; 660 self->ul_sigmask.__sigbits[1] = set->__sigbits[1]; 661 self->ul_sigmask.__sigbits[2] = set->__sigbits[2]; 662 self->ul_sigmask.__sigbits[3] = set->__sigbits[3]; 663 break; 664 } 665 delete_reserved_signals(&self->ul_sigmask); 666 if (oset != NULL) 667 *oset = saveset; 668 restore_signals(self); 669 } 670 671 return (0); 672 } 673 674 #pragma weak _pthread_sigmask = pthread_sigmask 675 int 676 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 677 { 678 return (thr_sigsetmask(how, set, oset)); 679 } 680 681 #pragma weak _sigprocmask = sigprocmask 682 int 683 sigprocmask(int how, const sigset_t *set, sigset_t *oset) 684 { 685 int error; 686 687 /* 688 * Guard against children of vfork(). 689 */ 690 if (curthread->ul_vfork) 691 return (__sigprocmask(how, set, oset)); 692 693 if ((error = thr_sigsetmask(how, set, oset)) != 0) { 694 errno = error; 695 return (-1); 696 } 697 698 return (0); 699 } 700 701 /* 702 * Called at library initialization to set up signal handling. 703 * All we really do is initialize the sig_lock rwlocks. 704 * All signal handlers are either SIG_DFL or SIG_IGN on exec(). 705 * However, if any signal handlers were established on alternate 706 * link maps before the primary link map has been initialized, 707 * then inform the kernel of the new sigacthandler. 708 */ 709 void 710 signal_init() 711 { 712 uberdata_t *udp = curthread->ul_uberdata; 713 struct sigaction *sap; 714 struct sigaction act; 715 rwlock_t *rwlp; 716 int sig; 717 718 for (sig = 0; sig < NSIG; sig++) { 719 rwlp = &udp->siguaction[sig].sig_lock; 720 rwlp->rwlock_magic = RWL_MAGIC; 721 rwlp->mutex.mutex_flag = LOCK_INITED; 722 rwlp->mutex.mutex_magic = MUTEX_MAGIC; 723 sap = &udp->siguaction[sig].sig_uaction; 724 if (sap->sa_sigaction != SIG_DFL && 725 sap->sa_sigaction != SIG_IGN && 726 __sigaction(sig, NULL, &act) == 0 && 727 act.sa_sigaction != SIG_DFL && 728 act.sa_sigaction != SIG_IGN) { 729 act = *sap; 730 act.sa_flags &= ~SA_NODEFER; 731 act.sa_sigaction = udp->sigacthandler; 732 act.sa_mask = maskset; 733 (void) __sigaction(sig, &act, NULL); 734 } 735 } 736 } 737 738 /* 739 * Common code for cancelling self in _sigcancel() and pthread_cancel(). 740 * First record the fact that a cancellation is pending. 741 * Then, if cancellation is disabled or if we are holding unprotected 742 * libc locks, just return to defer the cancellation. 743 * Then, if we are at a cancellation point (ul_cancelable) just 744 * return and let _canceloff() do the exit. 745 * Else exit immediately if async mode is in effect. 746 */ 747 void 748 do_sigcancel(void) 749 { 750 ulwp_t *self = curthread; 751 752 ASSERT(self->ul_critical == 0); 753 ASSERT(self->ul_sigdefer == 0); 754 self->ul_cancel_pending = 1; 755 if (self->ul_cancel_async && 756 !self->ul_cancel_disabled && 757 self->ul_libc_locks == 0 && 758 !self->ul_cancelable) 759 pthread_exit(PTHREAD_CANCELED); 760 set_cancel_pending_flag(self, 0); 761 } 762 763 /* 764 * Set up the SIGCANCEL handler for threads cancellation, 765 * needed only when we have more than one thread, 766 * or the SIGAIOCANCEL handler for aio cancellation, 767 * called when aio is initialized, in __uaio_init(). 768 */ 769 void 770 setup_cancelsig(int sig) 771 { 772 uberdata_t *udp = curthread->ul_uberdata; 773 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock; 774 struct sigaction act; 775 776 ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL); 777 lrw_rdlock(rwlp); 778 act = udp->siguaction[sig].sig_uaction; 779 lrw_unlock(rwlp); 780 if (act.sa_sigaction == SIG_DFL || 781 act.sa_sigaction == SIG_IGN) 782 act.sa_flags = SA_SIGINFO; 783 else { 784 act.sa_flags |= SA_SIGINFO; 785 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART); 786 } 787 act.sa_sigaction = udp->sigacthandler; 788 act.sa_mask = maskset; 789 (void) __sigaction(sig, &act, NULL); 790 }