1 /* $NetBSD: kern_sig.c,v 1.298 2009/05/24 21:41:26 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1989, 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * (c) UNIX System Laboratories, Inc. 36 * All or some portions of this file are derived from material licensed 37 * to the University of California by American Telephone and Telegraph 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 39 * the permission of UNIX System Laboratories, Inc. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.298 2009/05/24 21:41:26 ad Exp $"); 70 71 #include "opt_ptrace.h" 72 #include "opt_compat_sunos.h" 73 #include "opt_compat_netbsd.h" 74 #include "opt_compat_netbsd32.h" 75 #include "opt_pax.h" 76 #include "opt_sa.h" 77 78 #define SIGPROP /* include signal properties table */ 79 #include <sys/param.h> 80 #include <sys/signalvar.h> 81 #include <sys/proc.h> 82 #include <sys/systm.h> 83 #include <sys/wait.h> 84 #include <sys/ktrace.h> 85 #include <sys/syslog.h> 86 #include <sys/filedesc.h> 87 #include <sys/file.h> 88 #include <sys/pool.h> 89 #include <sys/ucontext.h> 90 #include <sys/sa.h> 91 #include <sys/savar.h> 92 #include <sys/exec.h> 93 #include <sys/kauth.h> 94 #include <sys/acct.h> 95 #include <sys/callout.h> 96 #include <sys/atomic.h> 97 #include <sys/cpu.h> 98 #include <sys/module.h> 99 100 #ifdef PAX_SEGVGUARD 101 #include <sys/pax.h> 102 #endif /* PAX_SEGVGUARD */ 103 104 #include <uvm/uvm.h> 105 #include <uvm/uvm_extern.h> 106 107 static void ksiginfo_exechook(struct proc *, void *); 108 static void proc_stop_callout(void *); 109 static int sigchecktrace(void); 110 static int sigpost(struct lwp *, sig_t, int, int, int); 111 static void sigput(sigpend_t *, struct proc *, ksiginfo_t *); 112 static int sigunwait(struct proc *, const ksiginfo_t *); 113 static void sigswitch(bool, int, int); 114 115 sigset_t contsigmask, stopsigmask, sigcantmask; 116 static pool_cache_t sigacts_cache; /* memory pool for sigacts structures */ 117 static void sigacts_poolpage_free(struct pool *, void *); 118 static void *sigacts_poolpage_alloc(struct pool *, int); 119 static callout_t proc_stop_ch; 120 static pool_cache_t siginfo_cache; 121 static pool_cache_t ksiginfo_cache; 122 123 void (*sendsig_sigcontext_vec)(const struct ksiginfo *, const sigset_t *); 124 int (*coredump_vec)(struct lwp *, const char *) = 125 (int (*)(struct lwp *, const char *))enosys; 126 127 static struct pool_allocator sigactspool_allocator = { 128 .pa_alloc = sigacts_poolpage_alloc, 129 .pa_free = sigacts_poolpage_free, 130 }; 131 132 #ifdef DEBUG 133 int kern_logsigexit = 1; 134 #else 135 int kern_logsigexit = 0; 136 #endif 137 138 static const char logcoredump[] = 139 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 140 static const char lognocoredump[] = 141 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 142 143 /* 144 * signal_init: 145 * 146 * Initialize global signal-related data structures. 147 */ 148 void 149 signal_init(void) 150 { 151 152 sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2; 153 154 sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0, 155 "sigacts", sizeof(struct sigacts) > PAGE_SIZE ? 156 &sigactspool_allocator : NULL, IPL_NONE, NULL, NULL, NULL); 157 158 siginfo_cache = pool_cache_init(sizeof(siginfo_t), 0, 0, 0, 159 "siginfo", NULL, IPL_NONE, NULL, NULL, NULL); 160 161 ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0, 162 "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL); 163 164 exechook_establish(ksiginfo_exechook, NULL); 165 166 callout_init(&proc_stop_ch, CALLOUT_MPSAFE); 167 callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL); 168 } 169 170 /* 171 * sigacts_poolpage_alloc: 172 * 173 * Allocate a page for the sigacts memory pool. 174 */ 175 static void * 176 sigacts_poolpage_alloc(struct pool *pp, int flags) 177 { 178 179 return (void *)uvm_km_alloc(kernel_map, 180 (PAGE_SIZE)*2, (PAGE_SIZE)*2, 181 ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK) 182 | UVM_KMF_WIRED); 183 } 184 185 /* 186 * sigacts_poolpage_free: 187 * 188 * Free a page on behalf of the sigacts memory pool. 189 */ 190 static void 191 sigacts_poolpage_free(struct pool *pp, void *v) 192 { 193 194 uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED); 195 } 196 197 /* 198 * sigactsinit: 199 * 200 * Create an initial sigctx structure, using the same signal state as 201 * p. If 'share' is set, share the sigctx_proc part, otherwise just 202 * copy it from parent. 203 */ 204 struct sigacts * 205 sigactsinit(struct proc *pp, int share) 206 { 207 struct sigacts *ps, *ps2; 208 209 ps = pp->p_sigacts; 210 211 if (share) { 212 atomic_inc_uint(&ps->sa_refcnt); 213 ps2 = ps; 214 } else { 215 ps2 = pool_cache_get(sigacts_cache, PR_WAITOK); 216 /* XXXAD get rid of this */ 217 mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED); 218 mutex_enter(&ps->sa_mutex); 219 memcpy(&ps2->sa_sigdesc, ps->sa_sigdesc, 220 sizeof(ps2->sa_sigdesc)); 221 mutex_exit(&ps->sa_mutex); 222 ps2->sa_refcnt = 1; 223 } 224 225 return ps2; 226 } 227 228 /* 229 * sigactsunshare: 230 * 231 * Make this process not share its sigctx, maintaining all 232 * signal state. 233 */ 234 void 235 sigactsunshare(struct proc *p) 236 { 237 struct sigacts *ps, *oldps; 238 239 oldps = p->p_sigacts; 240 if (oldps->sa_refcnt == 1) 241 return; 242 ps = pool_cache_get(sigacts_cache, PR_WAITOK); 243 /* XXXAD get rid of this */ 244 mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED); 245 memset(&ps->sa_sigdesc, 0, sizeof(ps->sa_sigdesc)); 246 p->p_sigacts = ps; 247 sigactsfree(oldps); 248 } 249 250 /* 251 * sigactsfree; 252 * 253 * Release a sigctx structure. 254 */ 255 void 256 sigactsfree(struct sigacts *ps) 257 { 258 259 if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) { 260 mutex_destroy(&ps->sa_mutex); 261 pool_cache_put(sigacts_cache, ps); 262 } 263 } 264 265 /* 266 * siginit: 267 * 268 * Initialize signal state for process 0; set to ignore signals that 269 * are ignored by default and disable the signal stack. Locking not 270 * required as the system is still cold. 271 */ 272 void 273 siginit(struct proc *p) 274 { 275 struct lwp *l; 276 struct sigacts *ps; 277 int signo, prop; 278 279 ps = p->p_sigacts; 280 sigemptyset(&contsigmask); 281 sigemptyset(&stopsigmask); 282 sigemptyset(&sigcantmask); 283 for (signo = 1; signo < NSIG; signo++) { 284 prop = sigprop[signo]; 285 if (prop & SA_CONT) 286 sigaddset(&contsigmask, signo); 287 if (prop & SA_STOP) 288 sigaddset(&stopsigmask, signo); 289 if (prop & SA_CANTMASK) 290 sigaddset(&sigcantmask, signo); 291 if (prop & SA_IGNORE && signo != SIGCONT) 292 sigaddset(&p->p_sigctx.ps_sigignore, signo); 293 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask); 294 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART; 295 } 296 sigemptyset(&p->p_sigctx.ps_sigcatch); 297 p->p_sflag &= ~PS_NOCLDSTOP; 298 299 ksiginfo_queue_init(&p->p_sigpend.sp_info); 300 sigemptyset(&p->p_sigpend.sp_set); 301 302 /* 303 * Reset per LWP state. 304 */ 305 l = LIST_FIRST(&p->p_lwps); 306 l->l_sigwaited = NULL; 307 l->l_sigstk.ss_flags = SS_DISABLE; 308 l->l_sigstk.ss_size = 0; 309 l->l_sigstk.ss_sp = 0; 310 ksiginfo_queue_init(&l->l_sigpend.sp_info); 311 sigemptyset(&l->l_sigpend.sp_set); 312 313 /* One reference. */ 314 ps->sa_refcnt = 1; 315 } 316 317 /* 318 * execsigs: 319 * 320 * Reset signals for an exec of the specified process. 321 */ 322 void 323 execsigs(struct proc *p) 324 { 325 struct sigacts *ps; 326 struct lwp *l; 327 int signo, prop; 328 sigset_t tset; 329 ksiginfoq_t kq; 330 331 KASSERT(p->p_nlwps == 1); 332 333 sigactsunshare(p); 334 ps = p->p_sigacts; 335 336 /* 337 * Reset caught signals. Held signals remain held through 338 * l->l_sigmask (unless they were caught, and are now ignored 339 * by default). 340 * 341 * No need to lock yet, the process has only one LWP and 342 * at this point the sigacts are private to the process. 343 */ 344 sigemptyset(&tset); 345 for (signo = 1; signo < NSIG; signo++) { 346 if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) { 347 prop = sigprop[signo]; 348 if (prop & SA_IGNORE) { 349 if ((prop & SA_CONT) == 0) 350 sigaddset(&p->p_sigctx.ps_sigignore, 351 signo); 352 sigaddset(&tset, signo); 353 } 354 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL; 355 } 356 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask); 357 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART; 358 } 359 ksiginfo_queue_init(&kq); 360 361 mutex_enter(p->p_lock); 362 sigclearall(p, &tset, &kq); 363 sigemptyset(&p->p_sigctx.ps_sigcatch); 364 365 /* 366 * Reset no zombies if child dies flag as Solaris does. 367 */ 368 p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN); 369 if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN) 370 SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL; 371 372 /* 373 * Reset per-LWP state. 374 */ 375 l = LIST_FIRST(&p->p_lwps); 376 l->l_sigwaited = NULL; 377 l->l_sigstk.ss_flags = SS_DISABLE; 378 l->l_sigstk.ss_size = 0; 379 l->l_sigstk.ss_sp = 0; 380 ksiginfo_queue_init(&l->l_sigpend.sp_info); 381 sigemptyset(&l->l_sigpend.sp_set); 382 mutex_exit(p->p_lock); 383 384 ksiginfo_queue_drain(&kq); 385 } 386 387 /* 388 * ksiginfo_exechook: 389 * 390 * Free all pending ksiginfo entries from a process on exec. 391 * Additionally, drain any unused ksiginfo structures in the 392 * system back to the pool. 393 * 394 * XXX This should not be a hook, every process has signals. 395 */ 396 static void 397 ksiginfo_exechook(struct proc *p, void *v) 398 { 399 ksiginfoq_t kq; 400 401 ksiginfo_queue_init(&kq); 402 403 mutex_enter(p->p_lock); 404 sigclearall(p, NULL, &kq); 405 mutex_exit(p->p_lock); 406 407 ksiginfo_queue_drain(&kq); 408 } 409 410 /* 411 * ksiginfo_alloc: 412 * 413 * Allocate a new ksiginfo structure from the pool, and optionally copy 414 * an existing one. If the existing ksiginfo_t is from the pool, and 415 * has not been queued somewhere, then just return it. Additionally, 416 * if the existing ksiginfo_t does not contain any information beyond 417 * the signal number, then just return it. 418 */ 419 ksiginfo_t * 420 ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags) 421 { 422 ksiginfo_t *kp; 423 424 if (ok != NULL) { 425 if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) == 426 KSI_FROMPOOL) 427 return ok; 428 if (KSI_EMPTY_P(ok)) 429 return ok; 430 } 431 432 kp = pool_cache_get(ksiginfo_cache, flags); 433 if (kp == NULL) { 434 #ifdef DIAGNOSTIC 435 printf("Out of memory allocating ksiginfo for pid %d\n", 436 p->p_pid); 437 #endif 438 return NULL; 439 } 440 441 if (ok != NULL) { 442 memcpy(kp, ok, sizeof(*kp)); 443 kp->ksi_flags &= ~KSI_QUEUED; 444 } else 445 KSI_INIT_EMPTY(kp); 446 447 kp->ksi_flags |= KSI_FROMPOOL; 448 449 return kp; 450 } 451 452 /* 453 * ksiginfo_free: 454 * 455 * If the given ksiginfo_t is from the pool and has not been queued, 456 * then free it. 457 */ 458 void 459 ksiginfo_free(ksiginfo_t *kp) 460 { 461 462 if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL) 463 return; 464 pool_cache_put(ksiginfo_cache, kp); 465 } 466 467 /* 468 * ksiginfo_queue_drain: 469 * 470 * Drain a non-empty ksiginfo_t queue. 471 */ 472 void 473 ksiginfo_queue_drain0(ksiginfoq_t *kq) 474 { 475 ksiginfo_t *ksi; 476 477 KASSERT(!CIRCLEQ_EMPTY(kq)); 478 479 while (!CIRCLEQ_EMPTY(kq)) { 480 ksi = CIRCLEQ_FIRST(kq); 481 CIRCLEQ_REMOVE(kq, ksi, ksi_list); 482 pool_cache_put(ksiginfo_cache, ksi); 483 } 484 } 485 486 /* 487 * sigget: 488 * 489 * Fetch the first pending signal from a set. Optionally, also fetch 490 * or manufacture a ksiginfo element. Returns the number of the first 491 * pending signal, or zero. 492 */ 493 int 494 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask) 495 { 496 ksiginfo_t *ksi; 497 sigset_t tset; 498 499 /* If there's no pending set, the signal is from the debugger. */ 500 if (sp == NULL) 501 goto out; 502 503 /* Construct mask from signo, and 'mask'. */ 504 if (signo == 0) { 505 if (mask != NULL) { 506 tset = *mask; 507 __sigandset(&sp->sp_set, &tset); 508 } else 509 tset = sp->sp_set; 510 511 /* If there are no signals pending, that's it. */ 512 if ((signo = firstsig(&tset)) == 0) 513 goto out; 514 } else { 515 KASSERT(sigismember(&sp->sp_set, signo)); 516 } 517 518 sigdelset(&sp->sp_set, signo); 519 520 /* Find siginfo and copy it out. */ 521 CIRCLEQ_FOREACH(ksi, &sp->sp_info, ksi_list) { 522 if (ksi->ksi_signo == signo) { 523 CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list); 524 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0); 525 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0); 526 ksi->ksi_flags &= ~KSI_QUEUED; 527 if (out != NULL) { 528 memcpy(out, ksi, sizeof(*out)); 529 out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED); 530 } 531 ksiginfo_free(ksi); 532 return signo; 533 } 534 } 535 536 out: 537 /* If there's no siginfo, then manufacture it. */ 538 if (out != NULL) { 539 KSI_INIT(out); 540 out->ksi_info._signo = signo; 541 out->ksi_info._code = SI_NOINFO; 542 } 543 544 return signo; 545 } 546 547 /* 548 * sigput: 549 * 550 * Append a new ksiginfo element to the list of pending ksiginfo's. 551 */ 552 static void 553 sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi) 554 { 555 ksiginfo_t *kp; 556 557 KASSERT(mutex_owned(p->p_lock)); 558 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0); 559 560 sigaddset(&sp->sp_set, ksi->ksi_signo); 561 562 /* 563 * If there is no siginfo, we are done. 564 */ 565 if (KSI_EMPTY_P(ksi)) 566 return; 567 568 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0); 569 570 #ifdef notyet /* XXX: QUEUING */ 571 if (ksi->ksi_signo < SIGRTMIN) 572 #endif 573 { 574 CIRCLEQ_FOREACH(kp, &sp->sp_info, ksi_list) { 575 if (kp->ksi_signo == ksi->ksi_signo) { 576 KSI_COPY(ksi, kp); 577 kp->ksi_flags |= KSI_QUEUED; 578 return; 579 } 580 } 581 } 582 583 ksi->ksi_flags |= KSI_QUEUED; 584 CIRCLEQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list); 585 } 586 587 /* 588 * sigclear: 589 * 590 * Clear all pending signals in the specified set. 591 */ 592 void 593 sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq) 594 { 595 ksiginfo_t *ksi, *next; 596 597 if (mask == NULL) 598 sigemptyset(&sp->sp_set); 599 else 600 sigminusset(mask, &sp->sp_set); 601 602 ksi = CIRCLEQ_FIRST(&sp->sp_info); 603 for (; ksi != (void *)&sp->sp_info; ksi = next) { 604 next = CIRCLEQ_NEXT(ksi, ksi_list); 605 if (mask == NULL || sigismember(mask, ksi->ksi_signo)) { 606 CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list); 607 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0); 608 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0); 609 CIRCLEQ_INSERT_TAIL(kq, ksi, ksi_list); 610 } 611 } 612 } 613 614 /* 615 * sigclearall: 616 * 617 * Clear all pending signals in the specified set from a process and 618 * its LWPs. 619 */ 620 void 621 sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq) 622 { 623 struct lwp *l; 624 625 KASSERT(mutex_owned(p->p_lock)); 626 627 sigclear(&p->p_sigpend, mask, kq); 628 629 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 630 sigclear(&l->l_sigpend, mask, kq); 631 } 632 } 633 634 /* 635 * sigispending: 636 * 637 * Return true if there are pending signals for the current LWP. May 638 * be called unlocked provided that LW_PENDSIG is set, and that the 639 * signal has been posted to the appopriate queue before LW_PENDSIG is 640 * set. 641 */ 642 int 643 sigispending(struct lwp *l, int signo) 644 { 645 struct proc *p = l->l_proc; 646 sigset_t tset; 647 648 membar_consumer(); 649 650 tset = l->l_sigpend.sp_set; 651 sigplusset(&p->p_sigpend.sp_set, &tset); 652 sigminusset(&p->p_sigctx.ps_sigignore, &tset); 653 sigminusset(&l->l_sigmask, &tset); 654 655 if (signo == 0) { 656 if (firstsig(&tset) != 0) 657 return EINTR; 658 } else if (sigismember(&tset, signo)) 659 return EINTR; 660 661 return 0; 662 } 663 664 /* 665 * siginfo_alloc: 666 * 667 * Allocate a new siginfo_t structure from the pool. 668 */ 669 siginfo_t * 670 siginfo_alloc(int flags) 671 { 672 673 return pool_cache_get(siginfo_cache, flags); 674 } 675 676 /* 677 * siginfo_free: 678 * 679 * Return a siginfo_t structure to the pool. 680 */ 681 void 682 siginfo_free(void *arg) 683 { 684 685 pool_cache_put(siginfo_cache, arg); 686 } 687 688 void 689 getucontext(struct lwp *l, ucontext_t *ucp) 690 { 691 struct proc *p = l->l_proc; 692 693 KASSERT(mutex_owned(p->p_lock)); 694 695 ucp->uc_flags = 0; 696 ucp->uc_link = l->l_ctxlink; 697 698 #if KERN_SA 699 if (p->p_sa != NULL) 700 ucp->uc_sigmask = p->p_sa->sa_sigmask; 701 else 702 #endif /* KERN_SA */ 703 ucp->uc_sigmask = l->l_sigmask; 704 ucp->uc_flags |= _UC_SIGMASK; 705 706 /* 707 * The (unsupplied) definition of the `current execution stack' 708 * in the System V Interface Definition appears to allow returning 709 * the main context stack. 710 */ 711 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) { 712 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase; 713 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize); 714 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 715 } else { 716 /* Simply copy alternate signal execution stack. */ 717 ucp->uc_stack = l->l_sigstk; 718 } 719 ucp->uc_flags |= _UC_STACK; 720 mutex_exit(p->p_lock); 721 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 722 mutex_enter(p->p_lock); 723 } 724 725 /* 726 * getucontext_sa: 727 * Get a ucontext_t for use in SA upcall generation. 728 * Teweaked version of getucontext(). We 1) do not take p_lock, 2) 729 * fudge things with uc_link (which is usually NULL for libpthread 730 * code), and 3) we report an empty signal mask. 731 */ 732 void 733 getucontext_sa(struct lwp *l, ucontext_t *ucp) 734 { 735 ucp->uc_flags = 0; 736 ucp->uc_link = l->l_ctxlink; 737 738 sigemptyset(&ucp->uc_sigmask); 739 ucp->uc_flags |= _UC_SIGMASK; 740 741 /* 742 * The (unsupplied) definition of the `current execution stack' 743 * in the System V Interface Definition appears to allow returning 744 * the main context stack. 745 */ 746 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) { 747 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase; 748 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize); 749 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 750 } else { 751 /* Simply copy alternate signal execution stack. */ 752 ucp->uc_stack = l->l_sigstk; 753 } 754 ucp->uc_flags |= _UC_STACK; 755 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 756 } 757 758 int 759 setucontext(struct lwp *l, const ucontext_t *ucp) 760 { 761 struct proc *p = l->l_proc; 762 int error; 763 764 KASSERT(mutex_owned(p->p_lock)); 765 766 if ((ucp->uc_flags & _UC_SIGMASK) != 0) { 767 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL); 768 if (error != 0) 769 return error; 770 } 771 772 mutex_exit(p->p_lock); 773 error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags); 774 mutex_enter(p->p_lock); 775 if (error != 0) 776 return (error); 777 778 l->l_ctxlink = ucp->uc_link; 779 780 /* 781 * If there was stack information, update whether or not we are 782 * still running on an alternate signal stack. 783 */ 784 if ((ucp->uc_flags & _UC_STACK) != 0) { 785 if (ucp->uc_stack.ss_flags & SS_ONSTACK) 786 l->l_sigstk.ss_flags |= SS_ONSTACK; 787 else 788 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 789 } 790 791 return 0; 792 } 793 794 /* 795 * Common code for kill process group/broadcast kill. cp is calling 796 * process. 797 */ 798 int 799 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all) 800 { 801 struct proc *p, *cp; 802 kauth_cred_t pc; 803 struct pgrp *pgrp; 804 int nfound; 805 int signo = ksi->ksi_signo; 806 807 cp = l->l_proc; 808 pc = l->l_cred; 809 nfound = 0; 810 811 mutex_enter(proc_lock); 812 if (all) { 813 /* 814 * broadcast 815 */ 816 PROCLIST_FOREACH(p, &allproc) { 817 if (p->p_pid <= 1 || p == cp || 818 p->p_flag & (PK_SYSTEM|PK_MARKER)) 819 continue; 820 mutex_enter(p->p_lock); 821 if (kauth_authorize_process(pc, 822 KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL, 823 NULL) == 0) { 824 nfound++; 825 if (signo) 826 kpsignal2(p, ksi); 827 } 828 mutex_exit(p->p_lock); 829 } 830 } else { 831 if (pgid == 0) 832 /* 833 * zero pgid means send to my process group. 834 */ 835 pgrp = cp->p_pgrp; 836 else { 837 pgrp = pg_find(pgid, PFIND_LOCKED); 838 if (pgrp == NULL) 839 goto out; 840 } 841 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 842 if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM) 843 continue; 844 mutex_enter(p->p_lock); 845 if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL, 846 p, KAUTH_ARG(signo), NULL, NULL) == 0) { 847 nfound++; 848 if (signo && P_ZOMBIE(p) == 0) 849 kpsignal2(p, ksi); 850 } 851 mutex_exit(p->p_lock); 852 } 853 } 854 out: 855 mutex_exit(proc_lock); 856 return (nfound ? 0 : ESRCH); 857 } 858 859 /* 860 * Send a signal to a process group. If checktty is 1, limit to members 861 * which have a controlling terminal. 862 */ 863 void 864 pgsignal(struct pgrp *pgrp, int sig, int checkctty) 865 { 866 ksiginfo_t ksi; 867 868 KASSERT(!cpu_intr_p()); 869 KASSERT(mutex_owned(proc_lock)); 870 871 KSI_INIT_EMPTY(&ksi); 872 ksi.ksi_signo = sig; 873 kpgsignal(pgrp, &ksi, NULL, checkctty); 874 } 875 876 void 877 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty) 878 { 879 struct proc *p; 880 881 KASSERT(!cpu_intr_p()); 882 KASSERT(mutex_owned(proc_lock)); 883 884 if (__predict_false(pgrp == 0)) 885 return; 886 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 887 if (checkctty == 0 || p->p_lflag & PL_CONTROLT) 888 kpsignal(p, ksi, data); 889 } 890 891 /* 892 * Send a signal caused by a trap to the current LWP. If it will be caught 893 * immediately, deliver it with correct code. Otherwise, post it normally. 894 */ 895 void 896 trapsignal(struct lwp *l, ksiginfo_t *ksi) 897 { 898 struct proc *p; 899 struct sigacts *ps; 900 int signo = ksi->ksi_signo; 901 sigset_t *mask; 902 903 KASSERT(KSI_TRAP_P(ksi)); 904 905 ksi->ksi_lid = l->l_lid; 906 p = l->l_proc; 907 908 KASSERT(!cpu_intr_p()); 909 mutex_enter(proc_lock); 910 mutex_enter(p->p_lock); 911 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask; 912 ps = p->p_sigacts; 913 if ((p->p_slflag & PSL_TRACED) == 0 && 914 sigismember(&p->p_sigctx.ps_sigcatch, signo) && 915 !sigismember(mask, signo)) { 916 mutex_exit(proc_lock); 917 l->l_ru.ru_nsignals++; 918 kpsendsig(l, ksi, mask); 919 mutex_exit(p->p_lock); 920 ktrpsig(signo, SIGACTION_PS(ps, signo).sa_handler, 921 mask, ksi); 922 } else { 923 /* XXX for core dump/debugger */ 924 p->p_sigctx.ps_lwp = l->l_lid; 925 p->p_sigctx.ps_signo = ksi->ksi_signo; 926 p->p_sigctx.ps_code = ksi->ksi_trap; 927 kpsignal2(p, ksi); 928 mutex_exit(p->p_lock); 929 mutex_exit(proc_lock); 930 } 931 } 932 933 /* 934 * Fill in signal information and signal the parent for a child status change. 935 */ 936 void 937 child_psignal(struct proc *p, int mask) 938 { 939 ksiginfo_t ksi; 940 struct proc *q; 941 int xstat; 942 943 KASSERT(mutex_owned(proc_lock)); 944 KASSERT(mutex_owned(p->p_lock)); 945 946 xstat = p->p_xstat; 947 948 KSI_INIT(&ksi); 949 ksi.ksi_signo = SIGCHLD; 950 ksi.ksi_code = (xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED); 951 ksi.ksi_pid = p->p_pid; 952 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred); 953 ksi.ksi_status = xstat; 954 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 955 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 956 957 q = p->p_pptr; 958 959 mutex_exit(p->p_lock); 960 mutex_enter(q->p_lock); 961 962 if ((q->p_sflag & mask) == 0) 963 kpsignal2(q, &ksi); 964 965 mutex_exit(q->p_lock); 966 mutex_enter(p->p_lock); 967 } 968 969 void 970 psignal(struct proc *p, int signo) 971 { 972 ksiginfo_t ksi; 973 974 KASSERT(!cpu_intr_p()); 975 KASSERT(mutex_owned(proc_lock)); 976 977 KSI_INIT_EMPTY(&ksi); 978 ksi.ksi_signo = signo; 979 mutex_enter(p->p_lock); 980 kpsignal2(p, &ksi); 981 mutex_exit(p->p_lock); 982 } 983 984 void 985 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data) 986 { 987 fdfile_t *ff; 988 file_t *fp; 989 fdtab_t *dt; 990 991 KASSERT(!cpu_intr_p()); 992 KASSERT(mutex_owned(proc_lock)); 993 994 if ((p->p_sflag & PS_WEXIT) == 0 && data) { 995 size_t fd; 996 filedesc_t *fdp = p->p_fd; 997 998 /* XXXSMP locking */ 999 ksi->ksi_fd = -1; 1000 dt = fdp->fd_dt; 1001 for (fd = 0; fd < dt->dt_nfiles; fd++) { 1002 if ((ff = dt->dt_ff[fd]) == NULL) 1003 continue; 1004 if ((fp = ff->ff_file) == NULL) 1005 continue; 1006 if (fp->f_data == data) { 1007 ksi->ksi_fd = fd; 1008 break; 1009 } 1010 } 1011 } 1012 mutex_enter(p->p_lock); 1013 kpsignal2(p, ksi); 1014 mutex_exit(p->p_lock); 1015 } 1016 1017 /* 1018 * sigismasked: 1019 * 1020 * Returns true if signal is ignored or masked for the specified LWP. 1021 */ 1022 int 1023 sigismasked(struct lwp *l, int sig) 1024 { 1025 struct proc *p = l->l_proc; 1026 1027 return (sigismember(&p->p_sigctx.ps_sigignore, sig) || 1028 sigismember(&l->l_sigmask, sig) 1029 #if KERN_SA 1030 || ((p->p_sa != NULL) && sigismember(&p->p_sa->sa_sigmask, sig)) 1031 #endif /* KERN_SA */ 1032 ); 1033 } 1034 1035 /* 1036 * sigpost: 1037 * 1038 * Post a pending signal to an LWP. Returns non-zero if the LWP may 1039 * be able to take the signal. 1040 */ 1041 static int 1042 sigpost(struct lwp *l, sig_t action, int prop, int sig, int idlecheck) 1043 { 1044 int rv, masked; 1045 struct proc *p = l->l_proc; 1046 1047 KASSERT(mutex_owned(p->p_lock)); 1048 1049 /* 1050 * If the LWP is on the way out, sigclear() will be busy draining all 1051 * pending signals. Don't give it more. 1052 */ 1053 if (l->l_refcnt == 0) 1054 return 0; 1055 1056 /* 1057 * Have the LWP check for signals. This ensures that even if no LWP 1058 * is found to take the signal immediately, it should be taken soon. 1059 */ 1060 lwp_lock(l); 1061 l->l_flag |= LW_PENDSIG; 1062 1063 /* 1064 * When sending signals to SA processes, we first try to find an 1065 * idle VP to take it. 1066 */ 1067 if (idlecheck && (l->l_flag & (LW_SA_IDLE | LW_SA_YIELD)) == 0) { 1068 lwp_unlock(l); 1069 return 0; 1070 } 1071 1072 /* 1073 * SIGCONT can be masked, but if LWP is stopped, it needs restart. 1074 * Note: SIGKILL and SIGSTOP cannot be masked. 1075 */ 1076 #if KERN_SA 1077 if (p->p_sa != NULL) 1078 masked = sigismember(&p->p_sa->sa_sigmask, sig); 1079 else 1080 #endif 1081 masked = sigismember(&l->l_sigmask, sig); 1082 if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) { 1083 lwp_unlock(l); 1084 return 0; 1085 } 1086 1087 /* 1088 * If killing the process, make it run fast. 1089 */ 1090 if (__predict_false((prop & SA_KILL) != 0) && 1091 action == SIG_DFL && l->l_priority < MAXPRI_USER) { 1092 KASSERT(l->l_class == SCHED_OTHER); 1093 lwp_changepri(l, MAXPRI_USER); 1094 } 1095 1096 /* 1097 * If the LWP is running or on a run queue, then we win. If it's 1098 * sleeping interruptably, wake it and make it take the signal. If 1099 * the sleep isn't interruptable, then the chances are it will get 1100 * to see the signal soon anyhow. If suspended, it can't take the 1101 * signal right now. If it's LWP private or for all LWPs, save it 1102 * for later; otherwise punt. 1103 */ 1104 rv = 0; 1105 1106 switch (l->l_stat) { 1107 case LSRUN: 1108 case LSONPROC: 1109 lwp_need_userret(l); 1110 rv = 1; 1111 break; 1112 1113 case LSSLEEP: 1114 if ((l->l_flag & LW_SINTR) != 0) { 1115 /* setrunnable() will release the lock. */ 1116 setrunnable(l); 1117 return 1; 1118 } 1119 break; 1120 1121 case LSSUSPENDED: 1122 if ((prop & SA_KILL) != 0) { 1123 /* lwp_continue() will release the lock. */ 1124 lwp_continue(l); 1125 return 1; 1126 } 1127 break; 1128 1129 case LSSTOP: 1130 if ((prop & SA_STOP) != 0) 1131 break; 1132 1133 /* 1134 * If the LWP is stopped and we are sending a continue 1135 * signal, then start it again. 1136 */ 1137 if ((prop & SA_CONT) != 0) { 1138 if (l->l_wchan != NULL) { 1139 l->l_stat = LSSLEEP; 1140 p->p_nrlwps++; 1141 rv = 1; 1142 break; 1143 } 1144 /* setrunnable() will release the lock. */ 1145 setrunnable(l); 1146 return 1; 1147 } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) { 1148 /* setrunnable() will release the lock. */ 1149 setrunnable(l); 1150 return 1; 1151 } 1152 break; 1153 1154 default: 1155 break; 1156 } 1157 1158 lwp_unlock(l); 1159 return rv; 1160 } 1161 1162 /* 1163 * Notify an LWP that it has a pending signal. 1164 */ 1165 void 1166 signotify(struct lwp *l) 1167 { 1168 KASSERT(lwp_locked(l, NULL)); 1169 1170 l->l_flag |= LW_PENDSIG; 1171 lwp_need_userret(l); 1172 } 1173 1174 /* 1175 * Find an LWP within process p that is waiting on signal ksi, and hand 1176 * it on. 1177 */ 1178 static int 1179 sigunwait(struct proc *p, const ksiginfo_t *ksi) 1180 { 1181 struct lwp *l; 1182 int signo; 1183 1184 KASSERT(mutex_owned(p->p_lock)); 1185 1186 signo = ksi->ksi_signo; 1187 1188 if (ksi->ksi_lid != 0) { 1189 /* 1190 * Signal came via _lwp_kill(). Find the LWP and see if 1191 * it's interested. 1192 */ 1193 if ((l = lwp_find(p, ksi->ksi_lid)) == NULL) 1194 return 0; 1195 if (l->l_sigwaited == NULL || 1196 !sigismember(&l->l_sigwaitset, signo)) 1197 return 0; 1198 } else { 1199 /* 1200 * Look for any LWP that may be interested. 1201 */ 1202 LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) { 1203 KASSERT(l->l_sigwaited != NULL); 1204 if (sigismember(&l->l_sigwaitset, signo)) 1205 break; 1206 } 1207 } 1208 1209 if (l != NULL) { 1210 l->l_sigwaited->ksi_info = ksi->ksi_info; 1211 l->l_sigwaited = NULL; 1212 LIST_REMOVE(l, l_sigwaiter); 1213 cv_signal(&l->l_sigcv); 1214 return 1; 1215 } 1216 1217 return 0; 1218 } 1219 1220 /* 1221 * Send the signal to the process. If the signal has an action, the action 1222 * is usually performed by the target process rather than the caller; we add 1223 * the signal to the set of pending signals for the process. 1224 * 1225 * Exceptions: 1226 * o When a stop signal is sent to a sleeping process that takes the 1227 * default action, the process is stopped without awakening it. 1228 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1229 * regardless of the signal action (eg, blocked or ignored). 1230 * 1231 * Other ignored signals are discarded immediately. 1232 */ 1233 void 1234 kpsignal2(struct proc *p, ksiginfo_t *ksi) 1235 { 1236 int prop, lid, toall, signo = ksi->ksi_signo; 1237 struct sigacts *sa; 1238 struct lwp *l; 1239 ksiginfo_t *kp; 1240 ksiginfoq_t kq; 1241 sig_t action; 1242 #ifdef KERN_SA 1243 struct sadata_vp *vp; 1244 #endif 1245 1246 KASSERT(!cpu_intr_p()); 1247 KASSERT(mutex_owned(proc_lock)); 1248 KASSERT(mutex_owned(p->p_lock)); 1249 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0); 1250 KASSERT(signo > 0 && signo < NSIG); 1251 1252 /* 1253 * If the process is being created by fork, is a zombie or is 1254 * exiting, then just drop the signal here and bail out. 1255 */ 1256 if (p->p_stat != SACTIVE && p->p_stat != SSTOP) 1257 return; 1258 1259 /* 1260 * Notify any interested parties of the signal. 1261 */ 1262 KNOTE(&p->p_klist, NOTE_SIGNAL | signo); 1263 1264 /* 1265 * Some signals including SIGKILL must act on the entire process. 1266 */ 1267 kp = NULL; 1268 prop = sigprop[signo]; 1269 toall = ((prop & SA_TOALL) != 0); 1270 1271 if (toall) 1272 lid = 0; 1273 else 1274 lid = ksi->ksi_lid; 1275 1276 /* 1277 * If proc is traced, always give parent a chance. 1278 */ 1279 if (p->p_slflag & PSL_TRACED) { 1280 action = SIG_DFL; 1281 1282 if (lid == 0) { 1283 /* 1284 * If the process is being traced and the signal 1285 * is being caught, make sure to save any ksiginfo. 1286 */ 1287 if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL) 1288 return; 1289 sigput(&p->p_sigpend, p, kp); 1290 } 1291 } else { 1292 /* 1293 * If the signal was the result of a trap and is not being 1294 * caught, then reset it to default action so that the 1295 * process dumps core immediately. 1296 */ 1297 if (KSI_TRAP_P(ksi)) { 1298 sa = p->p_sigacts; 1299 mutex_enter(&sa->sa_mutex); 1300 if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) { 1301 sigdelset(&p->p_sigctx.ps_sigignore, signo); 1302 SIGACTION(p, signo).sa_handler = SIG_DFL; 1303 } 1304 mutex_exit(&sa->sa_mutex); 1305 } 1306 1307 /* 1308 * If the signal is being ignored, then drop it. Note: we 1309 * don't set SIGCONT in ps_sigignore, and if it is set to 1310 * SIG_IGN, action will be SIG_DFL here. 1311 */ 1312 if (sigismember(&p->p_sigctx.ps_sigignore, signo)) 1313 return; 1314 1315 else if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) 1316 action = SIG_CATCH; 1317 else { 1318 action = SIG_DFL; 1319 1320 /* 1321 * If sending a tty stop signal to a member of an 1322 * orphaned process group, discard the signal here if 1323 * the action is default; don't stop the process below 1324 * if sleeping, and don't clear any pending SIGCONT. 1325 */ 1326 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 1327 return; 1328 1329 if (prop & SA_KILL && p->p_nice > NZERO) 1330 p->p_nice = NZERO; 1331 } 1332 } 1333 1334 /* 1335 * If stopping or continuing a process, discard any pending 1336 * signals that would do the inverse. 1337 */ 1338 if ((prop & (SA_CONT | SA_STOP)) != 0) { 1339 ksiginfo_queue_init(&kq); 1340 if ((prop & SA_CONT) != 0) 1341 sigclear(&p->p_sigpend, &stopsigmask, &kq); 1342 if ((prop & SA_STOP) != 0) 1343 sigclear(&p->p_sigpend, &contsigmask, &kq); 1344 ksiginfo_queue_drain(&kq); /* XXXSMP */ 1345 } 1346 1347 /* 1348 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL, 1349 * please!), check if any LWPs are waiting on it. If yes, pass on 1350 * the signal info. The signal won't be processed further here. 1351 */ 1352 if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) && 1353 p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 && 1354 sigunwait(p, ksi)) 1355 return; 1356 1357 /* 1358 * XXXSMP Should be allocated by the caller, we're holding locks 1359 * here. 1360 */ 1361 if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL) 1362 return; 1363 1364 /* 1365 * LWP private signals are easy - just find the LWP and post 1366 * the signal to it. 1367 */ 1368 if (lid != 0) { 1369 l = lwp_find(p, lid); 1370 if (l != NULL) { 1371 sigput(&l->l_sigpend, p, kp); 1372 membar_producer(); 1373 (void)sigpost(l, action, prop, kp->ksi_signo, 0); 1374 } 1375 goto out; 1376 } 1377 1378 /* 1379 * Some signals go to all LWPs, even if posted with _lwp_kill() 1380 * or for an SA process. 1381 */ 1382 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) { 1383 if ((p->p_slflag & PSL_TRACED) != 0) 1384 goto deliver; 1385 1386 /* 1387 * If SIGCONT is default (or ignored) and process is 1388 * asleep, we are finished; the process should not 1389 * be awakened. 1390 */ 1391 if ((prop & SA_CONT) != 0 && action == SIG_DFL) 1392 goto out; 1393 } else { 1394 /* 1395 * Process is stopped or stopping. If traced, then no 1396 * further action is necessary. 1397 */ 1398 if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) 1399 goto out; 1400 1401 /* 1402 * Run the process only if sending SIGCONT or SIGKILL. 1403 */ 1404 if ((prop & SA_CONT) != 0 || signo == SIGKILL) { 1405 /* 1406 * Re-adjust p_nstopchild if the process wasn't 1407 * collected by its parent. 1408 */ 1409 p->p_stat = SACTIVE; 1410 p->p_sflag &= ~PS_STOPPING; 1411 if (!p->p_waited) 1412 p->p_pptr->p_nstopchild--; 1413 1414 /* 1415 * Do not make signal pending if SIGCONT is default. 1416 * 1417 * If the process catches SIGCONT, let it handle the 1418 * signal itself (if waiting on event - process runs, 1419 * otherwise continues sleeping). 1420 */ 1421 if ((prop & SA_CONT) != 0 && action == SIG_DFL) { 1422 KASSERT(signo != SIGKILL); 1423 goto deliver; 1424 } 1425 } else if ((prop & SA_STOP) != 0) { 1426 /* 1427 * Already stopped, don't need to stop again. 1428 * (If we did the shell could get confused.) 1429 */ 1430 goto out; 1431 } 1432 } 1433 /* 1434 * Make signal pending. 1435 */ 1436 sigput(&p->p_sigpend, p, kp); 1437 1438 deliver: 1439 /* 1440 * Before we set LW_PENDSIG on any LWP, ensure that the signal is 1441 * visible on the per process list (for sigispending()). This 1442 * is unlikely to be needed in practice, but... 1443 */ 1444 membar_producer(); 1445 1446 /* 1447 * Try to find an LWP that can take the signal. 1448 */ 1449 #if KERN_SA 1450 if ((p->p_sa != NULL) && !toall) { 1451 /* 1452 * If we're in this delivery path, we are delivering a 1453 * signal that needs to go to one thread in the process. 1454 * 1455 * In the SA case, we try to find an idle LWP that can take 1456 * the signal. If that fails, only then do we consider 1457 * interrupting active LWPs. Since the signal's going to 1458 * just one thread, we need only look at "blessed" lwps, 1459 * so scan the vps for them. 1460 */ 1461 l = NULL; 1462 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1463 l = vp->savp_lwp; 1464 if (sigpost(l, action, prop, kp->ksi_signo, 1)) 1465 break; 1466 } 1467 1468 if (l == NULL) { 1469 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1470 l = vp->savp_lwp; 1471 if (sigpost(l, action, prop, kp->ksi_signo, 0)) 1472 break; 1473 } 1474 } 1475 } else /* Catch the brace below if we're defined */ 1476 #endif /* KERN_SA */ 1477 { 1478 LIST_FOREACH(l, &p->p_lwps, l_sibling) 1479 if (sigpost(l, action, prop, kp->ksi_signo, 0) && !toall) 1480 break; 1481 } 1482 1483 out: 1484 /* 1485 * If the ksiginfo wasn't used, then bin it. XXXSMP freeing memory 1486 * with locks held. The caller should take care of this. 1487 */ 1488 ksiginfo_free(kp); 1489 } 1490 1491 void 1492 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask) 1493 { 1494 struct proc *p = l->l_proc; 1495 #ifdef KERN_SA 1496 struct lwp *le, *li; 1497 siginfo_t *si; 1498 int f; 1499 #endif /* KERN_SA */ 1500 1501 KASSERT(mutex_owned(p->p_lock)); 1502 1503 #ifdef KERN_SA 1504 if (p->p_sflag & PS_SA) { 1505 /* f indicates if we should clear LP_SA_NOBLOCK */ 1506 f = ~l->l_pflag & LP_SA_NOBLOCK; 1507 l->l_pflag |= LP_SA_NOBLOCK; 1508 1509 mutex_exit(p->p_lock); 1510 /* XXXUPSXXX What if not on sa_vp? */ 1511 /* 1512 * WRS: I think it won't matter, beyond the 1513 * question of what exactly we do with a signal 1514 * to a blocked user thread. Also, we try hard to always 1515 * send signals to blessed lwps, so we would only send 1516 * to a non-blessed lwp under special circumstances. 1517 */ 1518 si = siginfo_alloc(PR_WAITOK); 1519 1520 si->_info = ksi->ksi_info; 1521 1522 /* 1523 * Figure out if we're the innocent victim or the main 1524 * perpitrator. 1525 */ 1526 le = li = NULL; 1527 if (KSI_TRAP_P(ksi)) 1528 le = l; 1529 else 1530 li = l; 1531 if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li, 1532 sizeof(*si), si, siginfo_free) != 0) { 1533 siginfo_free(si); 1534 #if 0 1535 if (KSI_TRAP_P(ksi)) 1536 /* XXX What dowe do here? The signal 1537 * didn't make it 1538 */; 1539 #endif 1540 } 1541 l->l_pflag ^= f; 1542 mutex_enter(p->p_lock); 1543 return; 1544 } 1545 #endif /* KERN_SA */ 1546 1547 (*p->p_emul->e_sendsig)(ksi, mask); 1548 } 1549 1550 /* 1551 * Stop any LWPs sleeping interruptably. 1552 */ 1553 static void 1554 proc_stop_lwps(struct proc *p) 1555 { 1556 struct lwp *l; 1557 1558 KASSERT(mutex_owned(p->p_lock)); 1559 KASSERT((p->p_sflag & PS_STOPPING) != 0); 1560 1561 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1562 lwp_lock(l); 1563 if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) { 1564 l->l_stat = LSSTOP; 1565 p->p_nrlwps--; 1566 } 1567 lwp_unlock(l); 1568 } 1569 } 1570 1571 /* 1572 * Finish stopping of a process. Mark it stopped and notify the parent. 1573 * 1574 * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true. 1575 */ 1576 static void 1577 proc_stop_done(struct proc *p, bool ppsig, int ppmask) 1578 { 1579 1580 KASSERT(mutex_owned(proc_lock)); 1581 KASSERT(mutex_owned(p->p_lock)); 1582 KASSERT((p->p_sflag & PS_STOPPING) != 0); 1583 KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc)); 1584 1585 p->p_sflag &= ~PS_STOPPING; 1586 p->p_stat = SSTOP; 1587 p->p_waited = 0; 1588 p->p_pptr->p_nstopchild++; 1589 if ((p->p_sflag & PS_NOTIFYSTOP) != 0) { 1590 if (ppsig) { 1591 /* child_psignal drops p_lock briefly. */ 1592 child_psignal(p, ppmask); 1593 } 1594 cv_broadcast(&p->p_pptr->p_waitcv); 1595 } 1596 } 1597 1598 /* 1599 * Stop the current process and switch away when being stopped or traced. 1600 */ 1601 static void 1602 sigswitch(bool ppsig, int ppmask, int signo) 1603 { 1604 struct lwp *l = curlwp; 1605 struct proc *p = l->l_proc; 1606 int biglocks; 1607 1608 KASSERT(mutex_owned(p->p_lock)); 1609 KASSERT(l->l_stat == LSONPROC); 1610 KASSERT(p->p_nrlwps > 0); 1611 1612 /* 1613 * On entry we know that the process needs to stop. If it's 1614 * the result of a 'sideways' stop signal that has been sourced 1615 * through issignal(), then stop other LWPs in the process too. 1616 */ 1617 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) { 1618 KASSERT(signo != 0); 1619 proc_stop(p, 1, signo); 1620 KASSERT(p->p_nrlwps > 0); 1621 } 1622 1623 /* 1624 * If we are the last live LWP, and the stop was a result of 1625 * a new signal, then signal the parent. 1626 */ 1627 if ((p->p_sflag & PS_STOPPING) != 0) { 1628 if (!mutex_tryenter(proc_lock)) { 1629 mutex_exit(p->p_lock); 1630 mutex_enter(proc_lock); 1631 mutex_enter(p->p_lock); 1632 } 1633 1634 if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) { 1635 /* 1636 * Note that proc_stop_done() can drop 1637 * p->p_lock briefly. 1638 */ 1639 proc_stop_done(p, ppsig, ppmask); 1640 } 1641 1642 mutex_exit(proc_lock); 1643 } 1644 1645 /* 1646 * Unlock and switch away. 1647 */ 1648 KERNEL_UNLOCK_ALL(l, &biglocks); 1649 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) { 1650 p->p_nrlwps--; 1651 lwp_lock(l); 1652 KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP); 1653 l->l_stat = LSSTOP; 1654 lwp_unlock(l); 1655 } 1656 1657 mutex_exit(p->p_lock); 1658 lwp_lock(l); 1659 mi_switch(l); 1660 KERNEL_LOCK(biglocks, l); 1661 mutex_enter(p->p_lock); 1662 } 1663 1664 /* 1665 * Check for a signal from the debugger. 1666 */ 1667 static int 1668 sigchecktrace(void) 1669 { 1670 struct lwp *l = curlwp; 1671 struct proc *p = l->l_proc; 1672 sigset_t *mask; 1673 int signo; 1674 1675 KASSERT(mutex_owned(p->p_lock)); 1676 1677 /* If there's a pending SIGKILL, process it immediately. */ 1678 if (sigismember(&p->p_sigpend.sp_set, SIGKILL)) 1679 return 0; 1680 1681 /* 1682 * If we are no longer being traced, or the parent didn't 1683 * give us a signal, look for more signals. 1684 */ 1685 if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xstat == 0) 1686 return 0; 1687 1688 /* 1689 * If the new signal is being masked, look for other signals. 1690 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable(). 1691 */ 1692 signo = p->p_xstat; 1693 p->p_xstat = 0; 1694 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask; 1695 if (sigismember(mask, signo)) 1696 signo = 0; 1697 1698 return signo; 1699 } 1700 1701 /* 1702 * If the current process has received a signal (should be caught or cause 1703 * termination, should interrupt current syscall), return the signal number. 1704 * 1705 * Stop signals with default action are processed immediately, then cleared; 1706 * they aren't returned. This is checked after each entry to the system for 1707 * a syscall or trap. 1708 * 1709 * We will also return -1 if the process is exiting and the current LWP must 1710 * follow suit. 1711 */ 1712 int 1713 issignal(struct lwp *l) 1714 { 1715 struct proc *p; 1716 int signo, prop; 1717 sigpend_t *sp; 1718 sigset_t ss; 1719 1720 p = l->l_proc; 1721 sp = NULL; 1722 signo = 0; 1723 1724 KASSERT(p == curproc); 1725 KASSERT(mutex_owned(p->p_lock)); 1726 1727 for (;;) { 1728 /* Discard any signals that we have decided not to take. */ 1729 if (signo != 0) 1730 (void)sigget(sp, NULL, signo, NULL); 1731 1732 /* Bail out if we do not own the virtual processor */ 1733 if (l->l_flag & LW_SA && l->l_savp->savp_lwp != l) 1734 break; 1735 1736 /* 1737 * If the process is stopped/stopping, then stop ourselves 1738 * now that we're on the kernel/userspace boundary. When 1739 * we awaken, check for a signal from the debugger. 1740 */ 1741 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) { 1742 sigswitch(true, PS_NOCLDSTOP, 0); 1743 signo = sigchecktrace(); 1744 } else 1745 signo = 0; 1746 1747 /* Signals from the debugger are "out of band". */ 1748 sp = NULL; 1749 1750 /* 1751 * If the debugger didn't provide a signal, find a pending 1752 * signal from our set. Check per-LWP signals first, and 1753 * then per-process. 1754 */ 1755 if (signo == 0) { 1756 sp = &l->l_sigpend; 1757 ss = sp->sp_set; 1758 if ((p->p_lflag & PL_PPWAIT) != 0) 1759 sigminusset(&stopsigmask, &ss); 1760 sigminusset(&l->l_sigmask, &ss); 1761 1762 if ((signo = firstsig(&ss)) == 0) { 1763 sp = &p->p_sigpend; 1764 ss = sp->sp_set; 1765 if ((p->p_lflag & PL_PPWAIT) != 0) 1766 sigminusset(&stopsigmask, &ss); 1767 sigminusset(&l->l_sigmask, &ss); 1768 1769 if ((signo = firstsig(&ss)) == 0) { 1770 /* 1771 * No signal pending - clear the 1772 * indicator and bail out. 1773 */ 1774 lwp_lock(l); 1775 l->l_flag &= ~LW_PENDSIG; 1776 lwp_unlock(l); 1777 sp = NULL; 1778 break; 1779 } 1780 } 1781 } 1782 1783 /* 1784 * We should see pending but ignored signals only if 1785 * we are being traced. 1786 */ 1787 if (sigismember(&p->p_sigctx.ps_sigignore, signo) && 1788 (p->p_slflag & PSL_TRACED) == 0) { 1789 /* Discard the signal. */ 1790 continue; 1791 } 1792 1793 /* 1794 * If traced, always stop, and stay stopped until released 1795 * by the debugger. If the our parent process is waiting 1796 * for us, don't hang as we could deadlock. 1797 */ 1798 if ((p->p_slflag & PSL_TRACED) != 0 && 1799 (p->p_lflag & PL_PPWAIT) == 0 && signo != SIGKILL) { 1800 /* Take the signal. */ 1801 (void)sigget(sp, NULL, signo, NULL); 1802 p->p_xstat = signo; 1803 1804 /* Emulation-specific handling of signal trace */ 1805 if (p->p_emul->e_tracesig == NULL || 1806 (*p->p_emul->e_tracesig)(p, signo) == 0) 1807 sigswitch(!(p->p_slflag & PSL_FSTRACE), 0, 1808 signo); 1809 1810 /* Check for a signal from the debugger. */ 1811 if ((signo = sigchecktrace()) == 0) 1812 continue; 1813 1814 /* Signals from the debugger are "out of band". */ 1815 sp = NULL; 1816 } 1817 1818 prop = sigprop[signo]; 1819 1820 /* 1821 * Decide whether the signal should be returned. 1822 */ 1823 switch ((long)SIGACTION(p, signo).sa_handler) { 1824 case (long)SIG_DFL: 1825 /* 1826 * Don't take default actions on system processes. 1827 */ 1828 if (p->p_pid <= 1) { 1829 #ifdef DIAGNOSTIC 1830 /* 1831 * Are you sure you want to ignore SIGSEGV 1832 * in init? XXX 1833 */ 1834 printf_nolog("Process (pid %d) got sig %d\n", 1835 p->p_pid, signo); 1836 #endif 1837 continue; 1838 } 1839 1840 /* 1841 * If there is a pending stop signal to process with 1842 * default action, stop here, then clear the signal. 1843 * However, if process is member of an orphaned 1844 * process group, ignore tty stop signals. 1845 */ 1846 if (prop & SA_STOP) { 1847 /* 1848 * XXX Don't hold proc_lock for p_lflag, 1849 * but it's not a big deal. 1850 */ 1851 if (p->p_slflag & PSL_TRACED || 1852 ((p->p_lflag & PL_ORPHANPG) != 0 && 1853 prop & SA_TTYSTOP)) { 1854 /* Ignore the signal. */ 1855 continue; 1856 } 1857 /* Take the signal. */ 1858 (void)sigget(sp, NULL, signo, NULL); 1859 p->p_xstat = signo; 1860 signo = 0; 1861 sigswitch(true, PS_NOCLDSTOP, p->p_xstat); 1862 } else if (prop & SA_IGNORE) { 1863 /* 1864 * Except for SIGCONT, shouldn't get here. 1865 * Default action is to ignore; drop it. 1866 */ 1867 continue; 1868 } 1869 break; 1870 1871 case (long)SIG_IGN: 1872 #ifdef DEBUG_ISSIGNAL 1873 /* 1874 * Masking above should prevent us ever trying 1875 * to take action on an ignored signal other 1876 * than SIGCONT, unless process is traced. 1877 */ 1878 if ((prop & SA_CONT) == 0 && 1879 (p->p_slflag & PSL_TRACED) == 0) 1880 printf_nolog("issignal\n"); 1881 #endif 1882 continue; 1883 1884 default: 1885 /* 1886 * This signal has an action, let postsig() process 1887 * it. 1888 */ 1889 break; 1890 } 1891 1892 break; 1893 } 1894 1895 l->l_sigpendset = sp; 1896 return signo; 1897 } 1898 1899 /* 1900 * Take the action for the specified signal 1901 * from the current set of pending signals. 1902 */ 1903 void 1904 postsig(int signo) 1905 { 1906 struct lwp *l; 1907 struct proc *p; 1908 struct sigacts *ps; 1909 sig_t action; 1910 sigset_t *returnmask; 1911 ksiginfo_t ksi; 1912 1913 l = curlwp; 1914 p = l->l_proc; 1915 ps = p->p_sigacts; 1916 1917 KASSERT(mutex_owned(p->p_lock)); 1918 KASSERT(signo > 0); 1919 1920 /* 1921 * Set the new mask value and also defer further occurrences of this 1922 * signal. 1923 * 1924 * Special case: user has done a sigsuspend. Here the current mask is 1925 * not of interest, but rather the mask from before the sigsuspend is 1926 * what we want restored after the signal processing is completed. 1927 */ 1928 if (l->l_sigrestore) { 1929 returnmask = &l->l_sigoldmask; 1930 l->l_sigrestore = 0; 1931 } else 1932 returnmask = &l->l_sigmask; 1933 1934 /* 1935 * Commit to taking the signal before releasing the mutex. 1936 */ 1937 action = SIGACTION_PS(ps, signo).sa_handler; 1938 l->l_ru.ru_nsignals++; 1939 sigget(l->l_sigpendset, &ksi, signo, NULL); 1940 1941 if (ktrpoint(KTR_PSIG)) { 1942 mutex_exit(p->p_lock); 1943 ktrpsig(signo, action, returnmask, &ksi); 1944 mutex_enter(p->p_lock); 1945 } 1946 1947 if (action == SIG_DFL) { 1948 /* 1949 * Default action, where the default is to kill 1950 * the process. (Other cases were ignored above.) 1951 */ 1952 sigexit(l, signo); 1953 return; 1954 } 1955 1956 /* 1957 * If we get here, the signal must be caught. 1958 */ 1959 #ifdef DIAGNOSTIC 1960 if (action == SIG_IGN || sigismember(&l->l_sigmask, signo)) 1961 panic("postsig action"); 1962 #endif 1963 1964 kpsendsig(l, &ksi, returnmask); 1965 } 1966 1967 /* 1968 * sendsig: 1969 * 1970 * Default signal delivery method for NetBSD. 1971 */ 1972 void 1973 sendsig(const struct ksiginfo *ksi, const sigset_t *mask) 1974 { 1975 struct sigacts *sa; 1976 int sig; 1977 1978 sig = ksi->ksi_signo; 1979 sa = curproc->p_sigacts; 1980 1981 switch (sa->sa_sigdesc[sig].sd_vers) { 1982 case 0: 1983 case 1: 1984 /* Compat for 1.6 and earlier. */ 1985 if (sendsig_sigcontext_vec == NULL) { 1986 break; 1987 } 1988 (*sendsig_sigcontext_vec)(ksi, mask); 1989 return; 1990 case 2: 1991 case 3: 1992 sendsig_siginfo(ksi, mask); 1993 return; 1994 default: 1995 break; 1996 } 1997 1998 printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers); 1999 sigexit(curlwp, SIGILL); 2000 } 2001 2002 /* 2003 * sendsig_reset: 2004 * 2005 * Reset the signal action. Called from emulation specific sendsig() 2006 * before unlocking to deliver the signal. 2007 */ 2008 void 2009 sendsig_reset(struct lwp *l, int signo) 2010 { 2011 struct proc *p = l->l_proc; 2012 struct sigacts *ps = p->p_sigacts; 2013 sigset_t *mask; 2014 2015 KASSERT(mutex_owned(p->p_lock)); 2016 2017 p->p_sigctx.ps_lwp = 0; 2018 p->p_sigctx.ps_code = 0; 2019 p->p_sigctx.ps_signo = 0; 2020 2021 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask; 2022 2023 mutex_enter(&ps->sa_mutex); 2024 sigplusset(&SIGACTION_PS(ps, signo).sa_mask, mask); 2025 if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) { 2026 sigdelset(&p->p_sigctx.ps_sigcatch, signo); 2027 if (signo != SIGCONT && sigprop[signo] & SA_IGNORE) 2028 sigaddset(&p->p_sigctx.ps_sigignore, signo); 2029 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL; 2030 } 2031 mutex_exit(&ps->sa_mutex); 2032 } 2033 2034 /* 2035 * Kill the current process for stated reason. 2036 */ 2037 void 2038 killproc(struct proc *p, const char *why) 2039 { 2040 2041 KASSERT(mutex_owned(proc_lock)); 2042 2043 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 2044 uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why); 2045 psignal(p, SIGKILL); 2046 } 2047 2048 /* 2049 * Force the current process to exit with the specified signal, dumping core 2050 * if appropriate. We bypass the normal tests for masked and caught 2051 * signals, allowing unrecoverable failures to terminate the process without 2052 * changing signal state. Mark the accounting record with the signal 2053 * termination. If dumping core, save the signal number for the debugger. 2054 * Calls exit and does not return. 2055 */ 2056 void 2057 sigexit(struct lwp *l, int signo) 2058 { 2059 int exitsig, error, docore; 2060 struct proc *p; 2061 struct lwp *t; 2062 2063 p = l->l_proc; 2064 2065 KASSERT(mutex_owned(p->p_lock)); 2066 KERNEL_UNLOCK_ALL(l, NULL); 2067 2068 /* 2069 * Don't permit coredump() multiple times in the same process. 2070 * Call back into sigexit, where we will be suspended until 2071 * the deed is done. Note that this is a recursive call, but 2072 * LW_WCORE will prevent us from coming back this way. 2073 */ 2074 if ((p->p_sflag & PS_WCORE) != 0) { 2075 lwp_lock(l); 2076 l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND); 2077 lwp_unlock(l); 2078 mutex_exit(p->p_lock); 2079 lwp_userret(l); 2080 panic("sigexit 1"); 2081 /* NOTREACHED */ 2082 } 2083 2084 /* If process is already on the way out, then bail now. */ 2085 if ((p->p_sflag & PS_WEXIT) != 0) { 2086 mutex_exit(p->p_lock); 2087 lwp_exit(l); 2088 panic("sigexit 2"); 2089 /* NOTREACHED */ 2090 } 2091 2092 /* 2093 * Prepare all other LWPs for exit. If dumping core, suspend them 2094 * so that their registers are available long enough to be dumped. 2095 */ 2096 if ((docore = (sigprop[signo] & SA_CORE)) != 0) { 2097 p->p_sflag |= PS_WCORE; 2098 for (;;) { 2099 LIST_FOREACH(t, &p->p_lwps, l_sibling) { 2100 lwp_lock(t); 2101 if (t == l) { 2102 t->l_flag &= ~LW_WSUSPEND; 2103 lwp_unlock(t); 2104 continue; 2105 } 2106 t->l_flag |= (LW_WCORE | LW_WEXIT); 2107 lwp_suspend(l, t); 2108 } 2109 2110 if (p->p_nrlwps == 1) 2111 break; 2112 2113 /* 2114 * Kick any LWPs sitting in lwp_wait1(), and wait 2115 * for everyone else to stop before proceeding. 2116 */ 2117 p->p_nlwpwait++; 2118 cv_broadcast(&p->p_lwpcv); 2119 cv_wait(&p->p_lwpcv, p->p_lock); 2120 p->p_nlwpwait--; 2121 } 2122 } 2123 2124 exitsig = signo; 2125 p->p_acflag |= AXSIG; 2126 p->p_sigctx.ps_signo = signo; 2127 2128 if (docore) { 2129 mutex_exit(p->p_lock); 2130 if ((error = (*coredump_vec)(l, NULL)) == 0) 2131 exitsig |= WCOREFLAG; 2132 2133 if (kern_logsigexit) { 2134 int uid = l->l_cred ? 2135 (int)kauth_cred_geteuid(l->l_cred) : -1; 2136 2137 if (error) 2138 log(LOG_INFO, lognocoredump, p->p_pid, 2139 p->p_comm, uid, signo, error); 2140 else 2141 log(LOG_INFO, logcoredump, p->p_pid, 2142 p->p_comm, uid, signo); 2143 } 2144 2145 #ifdef PAX_SEGVGUARD 2146 pax_segvguard(l, p->p_textvp, p->p_comm, true); 2147 #endif /* PAX_SEGVGUARD */ 2148 /* Acquire the sched state mutex. exit1() will release it. */ 2149 mutex_enter(p->p_lock); 2150 } 2151 2152 /* No longer dumping core. */ 2153 p->p_sflag &= ~PS_WCORE; 2154 2155 exit1(l, W_EXITCODE(0, exitsig)); 2156 /* NOTREACHED */ 2157 } 2158 2159 /* 2160 * Put process 'p' into the stopped state and optionally, notify the parent. 2161 */ 2162 void 2163 proc_stop(struct proc *p, int notify, int signo) 2164 { 2165 struct lwp *l; 2166 2167 KASSERT(mutex_owned(p->p_lock)); 2168 2169 /* 2170 * First off, set the stopping indicator and bring all sleeping 2171 * LWPs to a halt so they are included in p->p_nrlwps. We musn't 2172 * unlock between here and the p->p_nrlwps check below. 2173 */ 2174 p->p_sflag |= PS_STOPPING; 2175 if (notify) 2176 p->p_sflag |= PS_NOTIFYSTOP; 2177 else 2178 p->p_sflag &= ~PS_NOTIFYSTOP; 2179 membar_producer(); 2180 2181 proc_stop_lwps(p); 2182 2183 /* 2184 * If there are no LWPs available to take the signal, then we 2185 * signal the parent process immediately. Otherwise, the last 2186 * LWP to stop will take care of it. 2187 */ 2188 2189 if (p->p_nrlwps == 0) { 2190 proc_stop_done(p, true, PS_NOCLDSTOP); 2191 } else { 2192 /* 2193 * Have the remaining LWPs come to a halt, and trigger 2194 * proc_stop_callout() to ensure that they do. 2195 */ 2196 LIST_FOREACH(l, &p->p_lwps, l_sibling) 2197 sigpost(l, SIG_DFL, SA_STOP, signo, 0); 2198 callout_schedule(&proc_stop_ch, 1); 2199 } 2200 } 2201 2202 /* 2203 * When stopping a process, we do not immediatly set sleeping LWPs stopped, 2204 * but wait for them to come to a halt at the kernel-user boundary. This is 2205 * to allow LWPs to release any locks that they may hold before stopping. 2206 * 2207 * Non-interruptable sleeps can be long, and there is the potential for an 2208 * LWP to begin sleeping interruptably soon after the process has been set 2209 * stopping (PS_STOPPING). These LWPs will not notice that the process is 2210 * stopping, and so complete halt of the process and the return of status 2211 * information to the parent could be delayed indefinitely. 2212 * 2213 * To handle this race, proc_stop_callout() runs once per tick while there 2214 * are stopping processes in the system. It sets LWPs that are sleeping 2215 * interruptably into the LSSTOP state. 2216 * 2217 * Note that we are not concerned about keeping all LWPs stopped while the 2218 * process is stopped: stopped LWPs can awaken briefly to handle signals. 2219 * What we do need to ensure is that all LWPs in a stopping process have 2220 * stopped at least once, so that notification can be sent to the parent 2221 * process. 2222 */ 2223 static void 2224 proc_stop_callout(void *cookie) 2225 { 2226 bool more, restart; 2227 struct proc *p; 2228 2229 (void)cookie; 2230 2231 do { 2232 restart = false; 2233 more = false; 2234 2235 mutex_enter(proc_lock); 2236 PROCLIST_FOREACH(p, &allproc) { 2237 if ((p->p_flag & PK_MARKER) != 0) 2238 continue; 2239 mutex_enter(p->p_lock); 2240 2241 if ((p->p_sflag & PS_STOPPING) == 0) { 2242 mutex_exit(p->p_lock); 2243 continue; 2244 } 2245 2246 /* Stop any LWPs sleeping interruptably. */ 2247 proc_stop_lwps(p); 2248 if (p->p_nrlwps == 0) { 2249 /* 2250 * We brought the process to a halt. 2251 * Mark it as stopped and notify the 2252 * parent. 2253 */ 2254 if ((p->p_sflag & PS_NOTIFYSTOP) != 0) { 2255 /* 2256 * Note that proc_stop_done() will 2257 * drop p->p_lock briefly. 2258 * Arrange to restart and check 2259 * all processes again. 2260 */ 2261 restart = true; 2262 } 2263 proc_stop_done(p, true, PS_NOCLDSTOP); 2264 } else 2265 more = true; 2266 2267 mutex_exit(p->p_lock); 2268 if (restart) 2269 break; 2270 } 2271 mutex_exit(proc_lock); 2272 } while (restart); 2273 2274 /* 2275 * If we noted processes that are stopping but still have 2276 * running LWPs, then arrange to check again in 1 tick. 2277 */ 2278 if (more) 2279 callout_schedule(&proc_stop_ch, 1); 2280 } 2281 2282 /* 2283 * Given a process in state SSTOP, set the state back to SACTIVE and 2284 * move LSSTOP'd LWPs to LSSLEEP or make them runnable. 2285 */ 2286 void 2287 proc_unstop(struct proc *p) 2288 { 2289 struct lwp *l; 2290 int sig; 2291 2292 KASSERT(mutex_owned(proc_lock)); 2293 KASSERT(mutex_owned(p->p_lock)); 2294 2295 p->p_stat = SACTIVE; 2296 p->p_sflag &= ~PS_STOPPING; 2297 sig = p->p_xstat; 2298 2299 if (!p->p_waited) 2300 p->p_pptr->p_nstopchild--; 2301 2302 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 2303 lwp_lock(l); 2304 if (l->l_stat != LSSTOP) { 2305 lwp_unlock(l); 2306 continue; 2307 } 2308 if (l->l_wchan == NULL) { 2309 setrunnable(l); 2310 continue; 2311 } 2312 if (sig && (l->l_flag & LW_SINTR) != 0) { 2313 setrunnable(l); 2314 sig = 0; 2315 } else { 2316 l->l_stat = LSSLEEP; 2317 p->p_nrlwps++; 2318 lwp_unlock(l); 2319 } 2320 } 2321 } 2322 2323 static int 2324 filt_sigattach(struct knote *kn) 2325 { 2326 struct proc *p = curproc; 2327 2328 kn->kn_obj = p; 2329 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2330 2331 mutex_enter(p->p_lock); 2332 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2333 mutex_exit(p->p_lock); 2334 2335 return (0); 2336 } 2337 2338 static void 2339 filt_sigdetach(struct knote *kn) 2340 { 2341 struct proc *p = kn->kn_obj; 2342 2343 mutex_enter(p->p_lock); 2344 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2345 mutex_exit(p->p_lock); 2346 } 2347 2348 /* 2349 * signal knotes are shared with proc knotes, so we apply a mask to 2350 * the hint in order to differentiate them from process hints. This 2351 * could be avoided by using a signal-specific knote list, but probably 2352 * isn't worth the trouble. 2353 */ 2354 static int 2355 filt_signal(struct knote *kn, long hint) 2356 { 2357 2358 if (hint & NOTE_SIGNAL) { 2359 hint &= ~NOTE_SIGNAL; 2360 2361 if (kn->kn_id == hint) 2362 kn->kn_data++; 2363 } 2364 return (kn->kn_data != 0); 2365 } 2366 2367 const struct filterops sig_filtops = { 2368 0, filt_sigattach, filt_sigdetach, filt_signal 2369 }; 2370