1 /* $NetBSD: kern_sig.c,v 1.295 2009/01/22 14:38:35 yamt 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.295 2009/01/22 14:38:35 yamt 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 110 int sigunwait(struct proc *, const ksiginfo_t *); 111 void sigput(sigpend_t *, struct proc *, ksiginfo_t *); 112 int sigpost(struct lwp *, sig_t, int, int, int); 113 int sigchecktrace(void); 114 void sigswitch(bool, int, int); 115 void sigrealloc(ksiginfo_t *); 116 117 sigset_t contsigmask, stopsigmask, sigcantmask; 118 static pool_cache_t sigacts_cache; /* memory pool for sigacts structures */ 119 static void sigacts_poolpage_free(struct pool *, void *); 120 static void *sigacts_poolpage_alloc(struct pool *, int); 121 static callout_t proc_stop_ch; 122 static pool_cache_t siginfo_cache; 123 static pool_cache_t ksiginfo_cache; 124 125 void (*sendsig_sigcontext_vec)(const struct ksiginfo *, const sigset_t *); 126 int (*coredump_vec)(struct lwp *, const char *) = 127 (int (*)(struct lwp *, const char *))enosys; 128 129 static struct pool_allocator sigactspool_allocator = { 130 .pa_alloc = sigacts_poolpage_alloc, 131 .pa_free = sigacts_poolpage_free, 132 }; 133 134 #ifdef DEBUG 135 int kern_logsigexit = 1; 136 #else 137 int kern_logsigexit = 0; 138 #endif 139 140 static const char logcoredump[] = 141 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 142 static const char lognocoredump[] = 143 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 144 145 /* 146 * signal_init: 147 * 148 * Initialize global signal-related data structures. 149 */ 150 void 151 signal_init(void) 152 { 153 154 sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2; 155 156 sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0, 157 "sigacts", sizeof(struct sigacts) > PAGE_SIZE ? 158 &sigactspool_allocator : NULL, IPL_NONE, NULL, NULL, NULL); 159 160 siginfo_cache = pool_cache_init(sizeof(siginfo_t), 0, 0, 0, 161 "siginfo", NULL, IPL_NONE, NULL, NULL, NULL); 162 163 ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0, 164 "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL); 165 166 exechook_establish(ksiginfo_exechook, NULL); 167 168 callout_init(&proc_stop_ch, CALLOUT_MPSAFE); 169 callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL); 170 } 171 172 /* 173 * sigacts_poolpage_alloc: 174 * 175 * Allocate a page for the sigacts memory pool. 176 */ 177 static void * 178 sigacts_poolpage_alloc(struct pool *pp, int flags) 179 { 180 181 return (void *)uvm_km_alloc(kernel_map, 182 (PAGE_SIZE)*2, (PAGE_SIZE)*2, 183 ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK) 184 | UVM_KMF_WIRED); 185 } 186 187 /* 188 * sigacts_poolpage_free: 189 * 190 * Free a page on behalf of the sigacts memory pool. 191 */ 192 static void 193 sigacts_poolpage_free(struct pool *pp, void *v) 194 { 195 196 uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED); 197 } 198 199 /* 200 * sigactsinit: 201 * 202 * Create an initial sigctx structure, using the same signal state as 203 * p. If 'share' is set, share the sigctx_proc part, otherwise just 204 * copy it from parent. 205 */ 206 struct sigacts * 207 sigactsinit(struct proc *pp, int share) 208 { 209 struct sigacts *ps, *ps2; 210 211 ps = pp->p_sigacts; 212 213 if (share) { 214 atomic_inc_uint(&ps->sa_refcnt); 215 ps2 = ps; 216 } else { 217 ps2 = pool_cache_get(sigacts_cache, PR_WAITOK); 218 /* XXXAD get rid of this */ 219 mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED); 220 mutex_enter(&ps->sa_mutex); 221 memcpy(&ps2->sa_sigdesc, ps->sa_sigdesc, 222 sizeof(ps2->sa_sigdesc)); 223 mutex_exit(&ps->sa_mutex); 224 ps2->sa_refcnt = 1; 225 } 226 227 return ps2; 228 } 229 230 /* 231 * sigactsunshare: 232 * 233 * Make this process not share its sigctx, maintaining all 234 * signal state. 235 */ 236 void 237 sigactsunshare(struct proc *p) 238 { 239 struct sigacts *ps, *oldps; 240 241 oldps = p->p_sigacts; 242 if (oldps->sa_refcnt == 1) 243 return; 244 ps = pool_cache_get(sigacts_cache, PR_WAITOK); 245 /* XXXAD get rid of this */ 246 mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED); 247 memset(&ps->sa_sigdesc, 0, sizeof(ps->sa_sigdesc)); 248 p->p_sigacts = ps; 249 sigactsfree(oldps); 250 } 251 252 /* 253 * sigactsfree; 254 * 255 * Release a sigctx structure. 256 */ 257 void 258 sigactsfree(struct sigacts *ps) 259 { 260 261 if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) { 262 mutex_destroy(&ps->sa_mutex); 263 pool_cache_put(sigacts_cache, ps); 264 } 265 } 266 267 /* 268 * siginit: 269 * 270 * Initialize signal state for process 0; set to ignore signals that 271 * are ignored by default and disable the signal stack. Locking not 272 * required as the system is still cold. 273 */ 274 void 275 siginit(struct proc *p) 276 { 277 struct lwp *l; 278 struct sigacts *ps; 279 int signo, prop; 280 281 ps = p->p_sigacts; 282 sigemptyset(&contsigmask); 283 sigemptyset(&stopsigmask); 284 sigemptyset(&sigcantmask); 285 for (signo = 1; signo < NSIG; signo++) { 286 prop = sigprop[signo]; 287 if (prop & SA_CONT) 288 sigaddset(&contsigmask, signo); 289 if (prop & SA_STOP) 290 sigaddset(&stopsigmask, signo); 291 if (prop & SA_CANTMASK) 292 sigaddset(&sigcantmask, signo); 293 if (prop & SA_IGNORE && signo != SIGCONT) 294 sigaddset(&p->p_sigctx.ps_sigignore, signo); 295 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask); 296 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART; 297 } 298 sigemptyset(&p->p_sigctx.ps_sigcatch); 299 p->p_sflag &= ~PS_NOCLDSTOP; 300 301 ksiginfo_queue_init(&p->p_sigpend.sp_info); 302 sigemptyset(&p->p_sigpend.sp_set); 303 304 /* 305 * Reset per LWP state. 306 */ 307 l = LIST_FIRST(&p->p_lwps); 308 l->l_sigwaited = NULL; 309 l->l_sigstk.ss_flags = SS_DISABLE; 310 l->l_sigstk.ss_size = 0; 311 l->l_sigstk.ss_sp = 0; 312 ksiginfo_queue_init(&l->l_sigpend.sp_info); 313 sigemptyset(&l->l_sigpend.sp_set); 314 315 /* One reference. */ 316 ps->sa_refcnt = 1; 317 } 318 319 /* 320 * execsigs: 321 * 322 * Reset signals for an exec of the specified process. 323 */ 324 void 325 execsigs(struct proc *p) 326 { 327 struct sigacts *ps; 328 struct lwp *l; 329 int signo, prop; 330 sigset_t tset; 331 ksiginfoq_t kq; 332 333 KASSERT(p->p_nlwps == 1); 334 335 sigactsunshare(p); 336 ps = p->p_sigacts; 337 338 /* 339 * Reset caught signals. Held signals remain held through 340 * l->l_sigmask (unless they were caught, and are now ignored 341 * by default). 342 * 343 * No need to lock yet, the process has only one LWP and 344 * at this point the sigacts are private to the process. 345 */ 346 sigemptyset(&tset); 347 for (signo = 1; signo < NSIG; signo++) { 348 if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) { 349 prop = sigprop[signo]; 350 if (prop & SA_IGNORE) { 351 if ((prop & SA_CONT) == 0) 352 sigaddset(&p->p_sigctx.ps_sigignore, 353 signo); 354 sigaddset(&tset, signo); 355 } 356 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL; 357 } 358 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask); 359 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART; 360 } 361 ksiginfo_queue_init(&kq); 362 363 mutex_enter(p->p_lock); 364 sigclearall(p, &tset, &kq); 365 sigemptyset(&p->p_sigctx.ps_sigcatch); 366 367 /* 368 * Reset no zombies if child dies flag as Solaris does. 369 */ 370 p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN); 371 if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN) 372 SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL; 373 374 /* 375 * Reset per-LWP state. 376 */ 377 l = LIST_FIRST(&p->p_lwps); 378 l->l_sigwaited = NULL; 379 l->l_sigstk.ss_flags = SS_DISABLE; 380 l->l_sigstk.ss_size = 0; 381 l->l_sigstk.ss_sp = 0; 382 ksiginfo_queue_init(&l->l_sigpend.sp_info); 383 sigemptyset(&l->l_sigpend.sp_set); 384 mutex_exit(p->p_lock); 385 386 ksiginfo_queue_drain(&kq); 387 } 388 389 /* 390 * ksiginfo_exechook: 391 * 392 * Free all pending ksiginfo entries from a process on exec. 393 * Additionally, drain any unused ksiginfo structures in the 394 * system back to the pool. 395 * 396 * XXX This should not be a hook, every process has signals. 397 */ 398 static void 399 ksiginfo_exechook(struct proc *p, void *v) 400 { 401 ksiginfoq_t kq; 402 403 ksiginfo_queue_init(&kq); 404 405 mutex_enter(p->p_lock); 406 sigclearall(p, NULL, &kq); 407 mutex_exit(p->p_lock); 408 409 ksiginfo_queue_drain(&kq); 410 } 411 412 /* 413 * ksiginfo_alloc: 414 * 415 * Allocate a new ksiginfo structure from the pool, and optionally copy 416 * an existing one. If the existing ksiginfo_t is from the pool, and 417 * has not been queued somewhere, then just return it. Additionally, 418 * if the existing ksiginfo_t does not contain any information beyond 419 * the signal number, then just return it. 420 */ 421 ksiginfo_t * 422 ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags) 423 { 424 ksiginfo_t *kp; 425 426 if (ok != NULL) { 427 if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) == 428 KSI_FROMPOOL) 429 return ok; 430 if (KSI_EMPTY_P(ok)) 431 return ok; 432 } 433 434 kp = pool_cache_get(ksiginfo_cache, flags); 435 if (kp == NULL) { 436 #ifdef DIAGNOSTIC 437 printf("Out of memory allocating ksiginfo for pid %d\n", 438 p->p_pid); 439 #endif 440 return NULL; 441 } 442 443 if (ok != NULL) { 444 memcpy(kp, ok, sizeof(*kp)); 445 kp->ksi_flags &= ~KSI_QUEUED; 446 } else 447 KSI_INIT_EMPTY(kp); 448 449 kp->ksi_flags |= KSI_FROMPOOL; 450 451 return kp; 452 } 453 454 /* 455 * ksiginfo_free: 456 * 457 * If the given ksiginfo_t is from the pool and has not been queued, 458 * then free it. 459 */ 460 void 461 ksiginfo_free(ksiginfo_t *kp) 462 { 463 464 if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL) 465 return; 466 pool_cache_put(ksiginfo_cache, kp); 467 } 468 469 /* 470 * ksiginfo_queue_drain: 471 * 472 * Drain a non-empty ksiginfo_t queue. 473 */ 474 void 475 ksiginfo_queue_drain0(ksiginfoq_t *kq) 476 { 477 ksiginfo_t *ksi; 478 479 KASSERT(!CIRCLEQ_EMPTY(kq)); 480 481 while (!CIRCLEQ_EMPTY(kq)) { 482 ksi = CIRCLEQ_FIRST(kq); 483 CIRCLEQ_REMOVE(kq, ksi, ksi_list); 484 pool_cache_put(ksiginfo_cache, ksi); 485 } 486 } 487 488 /* 489 * sigget: 490 * 491 * Fetch the first pending signal from a set. Optionally, also fetch 492 * or manufacture a ksiginfo element. Returns the number of the first 493 * pending signal, or zero. 494 */ 495 int 496 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask) 497 { 498 ksiginfo_t *ksi; 499 sigset_t tset; 500 501 /* If there's no pending set, the signal is from the debugger. */ 502 if (sp == NULL) 503 goto out; 504 505 /* Construct mask from signo, and 'mask'. */ 506 if (signo == 0) { 507 if (mask != NULL) { 508 tset = *mask; 509 __sigandset(&sp->sp_set, &tset); 510 } else 511 tset = sp->sp_set; 512 513 /* If there are no signals pending, that's it. */ 514 if ((signo = firstsig(&tset)) == 0) 515 goto out; 516 } else { 517 KASSERT(sigismember(&sp->sp_set, signo)); 518 } 519 520 sigdelset(&sp->sp_set, signo); 521 522 /* Find siginfo and copy it out. */ 523 CIRCLEQ_FOREACH(ksi, &sp->sp_info, ksi_list) { 524 if (ksi->ksi_signo == signo) { 525 CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list); 526 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0); 527 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0); 528 ksi->ksi_flags &= ~KSI_QUEUED; 529 if (out != NULL) { 530 memcpy(out, ksi, sizeof(*out)); 531 out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED); 532 } 533 ksiginfo_free(ksi); 534 return signo; 535 } 536 } 537 538 out: 539 /* If there's no siginfo, then manufacture it. */ 540 if (out != NULL) { 541 KSI_INIT(out); 542 out->ksi_info._signo = signo; 543 out->ksi_info._code = SI_NOINFO; 544 } 545 546 return signo; 547 } 548 549 /* 550 * sigput: 551 * 552 * Append a new ksiginfo element to the list of pending ksiginfo's, if 553 * we need to (e.g. SA_SIGINFO was requested). 554 */ 555 void 556 sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi) 557 { 558 ksiginfo_t *kp; 559 struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo); 560 561 KASSERT(mutex_owned(p->p_lock)); 562 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0); 563 564 sigaddset(&sp->sp_set, ksi->ksi_signo); 565 566 /* 567 * If there is no siginfo, or is not required (and we don't add 568 * it for the benefit of ktrace, we are done). 569 */ 570 if (KSI_EMPTY_P(ksi) || 571 (!KTRPOINT(p, KTR_PSIG) && (sa->sa_flags & SA_SIGINFO) == 0)) 572 return; 573 574 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0); 575 576 #ifdef notyet /* XXX: QUEUING */ 577 if (ksi->ksi_signo < SIGRTMIN) 578 #endif 579 { 580 CIRCLEQ_FOREACH(kp, &sp->sp_info, ksi_list) { 581 if (kp->ksi_signo == ksi->ksi_signo) { 582 KSI_COPY(ksi, kp); 583 kp->ksi_flags |= KSI_QUEUED; 584 return; 585 } 586 } 587 } 588 589 ksi->ksi_flags |= KSI_QUEUED; 590 CIRCLEQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list); 591 } 592 593 /* 594 * sigclear: 595 * 596 * Clear all pending signals in the specified set. 597 */ 598 void 599 sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq) 600 { 601 ksiginfo_t *ksi, *next; 602 603 if (mask == NULL) 604 sigemptyset(&sp->sp_set); 605 else 606 sigminusset(mask, &sp->sp_set); 607 608 ksi = CIRCLEQ_FIRST(&sp->sp_info); 609 for (; ksi != (void *)&sp->sp_info; ksi = next) { 610 next = CIRCLEQ_NEXT(ksi, ksi_list); 611 if (mask == NULL || sigismember(mask, ksi->ksi_signo)) { 612 CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list); 613 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0); 614 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0); 615 CIRCLEQ_INSERT_TAIL(kq, ksi, ksi_list); 616 } 617 } 618 } 619 620 /* 621 * sigclearall: 622 * 623 * Clear all pending signals in the specified set from a process and 624 * its LWPs. 625 */ 626 void 627 sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq) 628 { 629 struct lwp *l; 630 631 KASSERT(mutex_owned(p->p_lock)); 632 633 sigclear(&p->p_sigpend, mask, kq); 634 635 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 636 sigclear(&l->l_sigpend, mask, kq); 637 } 638 } 639 640 /* 641 * sigispending: 642 * 643 * Return true if there are pending signals for the current LWP. May 644 * be called unlocked provided that LW_PENDSIG is set, and that the 645 * signal has been posted to the appopriate queue before LW_PENDSIG is 646 * set. 647 */ 648 int 649 sigispending(struct lwp *l, int signo) 650 { 651 struct proc *p = l->l_proc; 652 sigset_t tset; 653 654 membar_consumer(); 655 656 tset = l->l_sigpend.sp_set; 657 sigplusset(&p->p_sigpend.sp_set, &tset); 658 sigminusset(&p->p_sigctx.ps_sigignore, &tset); 659 sigminusset(&l->l_sigmask, &tset); 660 661 if (signo == 0) { 662 if (firstsig(&tset) != 0) 663 return EINTR; 664 } else if (sigismember(&tset, signo)) 665 return EINTR; 666 667 return 0; 668 } 669 670 /* 671 * siginfo_alloc: 672 * 673 * Allocate a new siginfo_t structure from the pool. 674 */ 675 siginfo_t * 676 siginfo_alloc(int flags) 677 { 678 679 return pool_cache_get(siginfo_cache, flags); 680 } 681 682 /* 683 * siginfo_free: 684 * 685 * Return a siginfo_t structure to the pool. 686 */ 687 void 688 siginfo_free(void *arg) 689 { 690 691 pool_cache_put(siginfo_cache, arg); 692 } 693 694 void 695 getucontext(struct lwp *l, ucontext_t *ucp) 696 { 697 struct proc *p = l->l_proc; 698 699 KASSERT(mutex_owned(p->p_lock)); 700 701 ucp->uc_flags = 0; 702 ucp->uc_link = l->l_ctxlink; 703 704 #if KERN_SA 705 if (p->p_sa != NULL) 706 ucp->uc_sigmask = p->p_sa->sa_sigmask; 707 else 708 #endif /* KERN_SA */ 709 ucp->uc_sigmask = l->l_sigmask; 710 ucp->uc_flags |= _UC_SIGMASK; 711 712 /* 713 * The (unsupplied) definition of the `current execution stack' 714 * in the System V Interface Definition appears to allow returning 715 * the main context stack. 716 */ 717 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) { 718 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase; 719 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize); 720 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 721 } else { 722 /* Simply copy alternate signal execution stack. */ 723 ucp->uc_stack = l->l_sigstk; 724 } 725 ucp->uc_flags |= _UC_STACK; 726 mutex_exit(p->p_lock); 727 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 728 mutex_enter(p->p_lock); 729 } 730 731 /* 732 * getucontext_sa: 733 * Get a ucontext_t for use in SA upcall generation. 734 * Teweaked version of getucontext(). We 1) do not take p_lock, 2) 735 * fudge things with uc_link (which is usually NULL for libpthread 736 * code), and 3) we report an empty signal mask. 737 */ 738 void 739 getucontext_sa(struct lwp *l, ucontext_t *ucp) 740 { 741 ucp->uc_flags = 0; 742 ucp->uc_link = l->l_ctxlink; 743 744 sigemptyset(&ucp->uc_sigmask); 745 ucp->uc_flags |= _UC_SIGMASK; 746 747 /* 748 * The (unsupplied) definition of the `current execution stack' 749 * in the System V Interface Definition appears to allow returning 750 * the main context stack. 751 */ 752 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) { 753 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase; 754 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize); 755 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 756 } else { 757 /* Simply copy alternate signal execution stack. */ 758 ucp->uc_stack = l->l_sigstk; 759 } 760 ucp->uc_flags |= _UC_STACK; 761 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 762 } 763 764 int 765 setucontext(struct lwp *l, const ucontext_t *ucp) 766 { 767 struct proc *p = l->l_proc; 768 int error; 769 770 KASSERT(mutex_owned(p->p_lock)); 771 772 if ((ucp->uc_flags & _UC_SIGMASK) != 0) { 773 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL); 774 if (error != 0) 775 return error; 776 } 777 778 mutex_exit(p->p_lock); 779 error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags); 780 mutex_enter(p->p_lock); 781 if (error != 0) 782 return (error); 783 784 l->l_ctxlink = ucp->uc_link; 785 786 /* 787 * If there was stack information, update whether or not we are 788 * still running on an alternate signal stack. 789 */ 790 if ((ucp->uc_flags & _UC_STACK) != 0) { 791 if (ucp->uc_stack.ss_flags & SS_ONSTACK) 792 l->l_sigstk.ss_flags |= SS_ONSTACK; 793 else 794 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 795 } 796 797 return 0; 798 } 799 800 /* 801 * Common code for kill process group/broadcast kill. cp is calling 802 * process. 803 */ 804 int 805 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all) 806 { 807 struct proc *p, *cp; 808 kauth_cred_t pc; 809 struct pgrp *pgrp; 810 int nfound; 811 int signo = ksi->ksi_signo; 812 813 cp = l->l_proc; 814 pc = l->l_cred; 815 nfound = 0; 816 817 mutex_enter(proc_lock); 818 if (all) { 819 /* 820 * broadcast 821 */ 822 PROCLIST_FOREACH(p, &allproc) { 823 if (p->p_pid <= 1 || p == cp || 824 p->p_flag & (PK_SYSTEM|PK_MARKER)) 825 continue; 826 mutex_enter(p->p_lock); 827 if (kauth_authorize_process(pc, 828 KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL, 829 NULL) == 0) { 830 nfound++; 831 if (signo) 832 kpsignal2(p, ksi); 833 } 834 mutex_exit(p->p_lock); 835 } 836 } else { 837 if (pgid == 0) 838 /* 839 * zero pgid means send to my process group. 840 */ 841 pgrp = cp->p_pgrp; 842 else { 843 pgrp = pg_find(pgid, PFIND_LOCKED); 844 if (pgrp == NULL) 845 goto out; 846 } 847 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 848 if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM) 849 continue; 850 mutex_enter(p->p_lock); 851 if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL, 852 p, KAUTH_ARG(signo), NULL, NULL) == 0) { 853 nfound++; 854 if (signo && P_ZOMBIE(p) == 0) 855 kpsignal2(p, ksi); 856 } 857 mutex_exit(p->p_lock); 858 } 859 } 860 out: 861 mutex_exit(proc_lock); 862 return (nfound ? 0 : ESRCH); 863 } 864 865 /* 866 * Send a signal to a process group. If checktty is 1, limit to members 867 * which have a controlling terminal. 868 */ 869 void 870 pgsignal(struct pgrp *pgrp, int sig, int checkctty) 871 { 872 ksiginfo_t ksi; 873 874 KASSERT(!cpu_intr_p()); 875 KASSERT(mutex_owned(proc_lock)); 876 877 KSI_INIT_EMPTY(&ksi); 878 ksi.ksi_signo = sig; 879 kpgsignal(pgrp, &ksi, NULL, checkctty); 880 } 881 882 void 883 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty) 884 { 885 struct proc *p; 886 887 KASSERT(!cpu_intr_p()); 888 KASSERT(mutex_owned(proc_lock)); 889 890 if (pgrp) 891 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 892 if (checkctty == 0 || p->p_lflag & PL_CONTROLT) 893 kpsignal(p, ksi, data); 894 } 895 896 /* 897 * Send a signal caused by a trap to the current LWP. If it will be caught 898 * immediately, deliver it with correct code. Otherwise, post it normally. 899 */ 900 void 901 trapsignal(struct lwp *l, ksiginfo_t *ksi) 902 { 903 struct proc *p; 904 struct sigacts *ps; 905 int signo = ksi->ksi_signo; 906 sigset_t *mask; 907 908 KASSERT(KSI_TRAP_P(ksi)); 909 910 ksi->ksi_lid = l->l_lid; 911 p = l->l_proc; 912 913 KASSERT(!cpu_intr_p()); 914 mutex_enter(proc_lock); 915 mutex_enter(p->p_lock); 916 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask; 917 ps = p->p_sigacts; 918 if ((p->p_slflag & PSL_TRACED) == 0 && 919 sigismember(&p->p_sigctx.ps_sigcatch, signo) && 920 !sigismember(mask, signo)) { 921 mutex_exit(proc_lock); 922 l->l_ru.ru_nsignals++; 923 kpsendsig(l, ksi, mask); 924 mutex_exit(p->p_lock); 925 ktrpsig(signo, SIGACTION_PS(ps, signo).sa_handler, 926 mask, ksi); 927 } else { 928 /* XXX for core dump/debugger */ 929 p->p_sigctx.ps_lwp = l->l_lid; 930 p->p_sigctx.ps_signo = ksi->ksi_signo; 931 p->p_sigctx.ps_code = ksi->ksi_trap; 932 kpsignal2(p, ksi); 933 mutex_exit(p->p_lock); 934 mutex_exit(proc_lock); 935 } 936 } 937 938 /* 939 * Fill in signal information and signal the parent for a child status change. 940 */ 941 void 942 child_psignal(struct proc *p, int mask) 943 { 944 ksiginfo_t ksi; 945 struct proc *q; 946 int xstat; 947 948 KASSERT(mutex_owned(proc_lock)); 949 KASSERT(mutex_owned(p->p_lock)); 950 951 xstat = p->p_xstat; 952 953 KSI_INIT(&ksi); 954 ksi.ksi_signo = SIGCHLD; 955 ksi.ksi_code = (xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED); 956 ksi.ksi_pid = p->p_pid; 957 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred); 958 ksi.ksi_status = xstat; 959 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 960 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 961 962 q = p->p_pptr; 963 964 mutex_exit(p->p_lock); 965 mutex_enter(q->p_lock); 966 967 if ((q->p_sflag & mask) == 0) 968 kpsignal2(q, &ksi); 969 970 mutex_exit(q->p_lock); 971 mutex_enter(p->p_lock); 972 } 973 974 void 975 psignal(struct proc *p, int signo) 976 { 977 ksiginfo_t ksi; 978 979 KASSERT(!cpu_intr_p()); 980 KASSERT(mutex_owned(proc_lock)); 981 982 KSI_INIT_EMPTY(&ksi); 983 ksi.ksi_signo = signo; 984 mutex_enter(p->p_lock); 985 kpsignal2(p, &ksi); 986 mutex_exit(p->p_lock); 987 } 988 989 void 990 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data) 991 { 992 fdfile_t *ff; 993 file_t *fp; 994 995 KASSERT(!cpu_intr_p()); 996 KASSERT(mutex_owned(proc_lock)); 997 998 if ((p->p_sflag & PS_WEXIT) == 0 && data) { 999 size_t fd; 1000 filedesc_t *fdp = p->p_fd; 1001 1002 /* XXXSMP locking */ 1003 ksi->ksi_fd = -1; 1004 for (fd = 0; fd < fdp->fd_nfiles; fd++) { 1005 if ((ff = fdp->fd_ofiles[fd]) == NULL) 1006 continue; 1007 if ((fp = ff->ff_file) == NULL) 1008 continue; 1009 if (fp->f_data == data) { 1010 ksi->ksi_fd = fd; 1011 break; 1012 } 1013 } 1014 } 1015 mutex_enter(p->p_lock); 1016 kpsignal2(p, ksi); 1017 mutex_exit(p->p_lock); 1018 } 1019 1020 /* 1021 * sigismasked: 1022 * 1023 * Returns true if signal is ignored or masked for the specified LWP. 1024 */ 1025 int 1026 sigismasked(struct lwp *l, int sig) 1027 { 1028 struct proc *p = l->l_proc; 1029 1030 return (sigismember(&p->p_sigctx.ps_sigignore, sig) || 1031 sigismember(&l->l_sigmask, sig) 1032 #if KERN_SA 1033 || ((p->p_sa != NULL) && sigismember(&p->p_sa->sa_sigmask, sig)) 1034 #endif /* KERN_SA */ 1035 ); 1036 } 1037 1038 /* 1039 * sigpost: 1040 * 1041 * Post a pending signal to an LWP. Returns non-zero if the LWP may 1042 * be able to take the signal. 1043 */ 1044 int 1045 sigpost(struct lwp *l, sig_t action, int prop, int sig, int idlecheck) 1046 { 1047 int rv, masked; 1048 struct proc *p = l->l_proc; 1049 1050 KASSERT(mutex_owned(p->p_lock)); 1051 1052 /* 1053 * If the LWP is on the way out, sigclear() will be busy draining all 1054 * pending signals. Don't give it more. 1055 */ 1056 if (l->l_refcnt == 0) 1057 return 0; 1058 1059 /* 1060 * Have the LWP check for signals. This ensures that even if no LWP 1061 * is found to take the signal immediately, it should be taken soon. 1062 */ 1063 lwp_lock(l); 1064 l->l_flag |= LW_PENDSIG; 1065 1066 /* 1067 * When sending signals to SA processes, we first try to find an 1068 * idle VP to take it. 1069 */ 1070 if (idlecheck && (l->l_flag & (LW_SA_IDLE | LW_SA_YIELD)) == 0) { 1071 lwp_unlock(l); 1072 return 0; 1073 } 1074 1075 /* 1076 * SIGCONT can be masked, but must always restart stopped LWPs. 1077 */ 1078 #if KERN_SA 1079 if (p->p_sa != NULL) 1080 masked = sigismember(&p->p_sa->sa_sigmask, sig); 1081 else 1082 #endif /* KERN_SA */ 1083 masked = sigismember(&l->l_sigmask, sig); 1084 if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) { 1085 lwp_unlock(l); 1086 return 0; 1087 } 1088 1089 /* 1090 * If killing the process, make it run fast. 1091 */ 1092 if (__predict_false((prop & SA_KILL) != 0) && 1093 action == SIG_DFL && l->l_priority < MAXPRI_USER) { 1094 KASSERT(l->l_class == SCHED_OTHER); 1095 lwp_changepri(l, MAXPRI_USER); 1096 } 1097 1098 /* 1099 * If the LWP is running or on a run queue, then we win. If it's 1100 * sleeping interruptably, wake it and make it take the signal. If 1101 * the sleep isn't interruptable, then the chances are it will get 1102 * to see the signal soon anyhow. If suspended, it can't take the 1103 * signal right now. If it's LWP private or for all LWPs, save it 1104 * for later; otherwise punt. 1105 */ 1106 rv = 0; 1107 1108 switch (l->l_stat) { 1109 case LSRUN: 1110 case LSONPROC: 1111 lwp_need_userret(l); 1112 rv = 1; 1113 break; 1114 1115 case LSSLEEP: 1116 if ((l->l_flag & LW_SINTR) != 0) { 1117 /* setrunnable() will release the lock. */ 1118 setrunnable(l); 1119 return 1; 1120 } 1121 break; 1122 1123 case LSSUSPENDED: 1124 if ((prop & SA_KILL) != 0) { 1125 /* lwp_continue() will release the lock. */ 1126 lwp_continue(l); 1127 return 1; 1128 } 1129 break; 1130 1131 case LSSTOP: 1132 if ((prop & SA_STOP) != 0) 1133 break; 1134 1135 /* 1136 * If the LWP is stopped and we are sending a continue 1137 * signal, then start it again. 1138 */ 1139 if ((prop & SA_CONT) != 0) { 1140 if (l->l_wchan != NULL) { 1141 l->l_stat = LSSLEEP; 1142 p->p_nrlwps++; 1143 rv = 1; 1144 break; 1145 } 1146 /* setrunnable() will release the lock. */ 1147 setrunnable(l); 1148 return 1; 1149 } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) { 1150 /* setrunnable() will release the lock. */ 1151 setrunnable(l); 1152 return 1; 1153 } 1154 break; 1155 1156 default: 1157 break; 1158 } 1159 1160 lwp_unlock(l); 1161 return rv; 1162 } 1163 1164 /* 1165 * Notify an LWP that it has a pending signal. 1166 */ 1167 void 1168 signotify(struct lwp *l) 1169 { 1170 KASSERT(lwp_locked(l, NULL)); 1171 1172 l->l_flag |= LW_PENDSIG; 1173 lwp_need_userret(l); 1174 } 1175 1176 /* 1177 * Find an LWP within process p that is waiting on signal ksi, and hand 1178 * it on. 1179 */ 1180 int 1181 sigunwait(struct proc *p, const ksiginfo_t *ksi) 1182 { 1183 struct lwp *l; 1184 int signo; 1185 1186 KASSERT(mutex_owned(p->p_lock)); 1187 1188 signo = ksi->ksi_signo; 1189 1190 if (ksi->ksi_lid != 0) { 1191 /* 1192 * Signal came via _lwp_kill(). Find the LWP and see if 1193 * it's interested. 1194 */ 1195 if ((l = lwp_find(p, ksi->ksi_lid)) == NULL) 1196 return 0; 1197 if (l->l_sigwaited == NULL || 1198 !sigismember(&l->l_sigwaitset, signo)) 1199 return 0; 1200 } else { 1201 /* 1202 * Look for any LWP that may be interested. 1203 */ 1204 LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) { 1205 KASSERT(l->l_sigwaited != NULL); 1206 if (sigismember(&l->l_sigwaitset, signo)) 1207 break; 1208 } 1209 } 1210 1211 if (l != NULL) { 1212 l->l_sigwaited->ksi_info = ksi->ksi_info; 1213 l->l_sigwaited = NULL; 1214 LIST_REMOVE(l, l_sigwaiter); 1215 cv_signal(&l->l_sigcv); 1216 return 1; 1217 } 1218 1219 return 0; 1220 } 1221 1222 /* 1223 * Send the signal to the process. If the signal has an action, the action 1224 * is usually performed by the target process rather than the caller; we add 1225 * the signal to the set of pending signals for the process. 1226 * 1227 * Exceptions: 1228 * o When a stop signal is sent to a sleeping process that takes the 1229 * default action, the process is stopped without awakening it. 1230 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1231 * regardless of the signal action (eg, blocked or ignored). 1232 * 1233 * Other ignored signals are discarded immediately. 1234 */ 1235 void 1236 kpsignal2(struct proc *p, ksiginfo_t *ksi) 1237 { 1238 int prop, lid, toall, signo = ksi->ksi_signo; 1239 struct sigacts *sa; 1240 struct lwp *l; 1241 ksiginfo_t *kp; 1242 ksiginfoq_t kq; 1243 sig_t action; 1244 #ifdef KERN_SA 1245 struct sadata_vp *vp; 1246 #endif 1247 1248 KASSERT(!cpu_intr_p()); 1249 KASSERT(mutex_owned(proc_lock)); 1250 KASSERT(mutex_owned(p->p_lock)); 1251 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0); 1252 KASSERT(signo > 0 && signo < NSIG); 1253 1254 /* 1255 * If the process is being created by fork, is a zombie or is 1256 * exiting, then just drop the signal here and bail out. 1257 */ 1258 if (p->p_stat != SACTIVE && p->p_stat != SSTOP) 1259 return; 1260 1261 /* 1262 * Notify any interested parties of the signal. 1263 */ 1264 KNOTE(&p->p_klist, NOTE_SIGNAL | signo); 1265 1266 /* 1267 * Some signals including SIGKILL must act on the entire process. 1268 */ 1269 kp = NULL; 1270 prop = sigprop[signo]; 1271 toall = ((prop & SA_TOALL) != 0); 1272 1273 if (toall) 1274 lid = 0; 1275 else 1276 lid = ksi->ksi_lid; 1277 1278 /* 1279 * If proc is traced, always give parent a chance. 1280 */ 1281 if (p->p_slflag & PSL_TRACED) { 1282 action = SIG_DFL; 1283 1284 if (lid == 0) { 1285 /* 1286 * If the process is being traced and the signal 1287 * is being caught, make sure to save any ksiginfo. 1288 */ 1289 if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL) 1290 return; 1291 sigput(&p->p_sigpend, p, kp); 1292 } 1293 } else { 1294 /* 1295 * If the signal was the result of a trap and is not being 1296 * caught, then reset it to default action so that the 1297 * process dumps core immediately. 1298 */ 1299 if (KSI_TRAP_P(ksi)) { 1300 sa = p->p_sigacts; 1301 mutex_enter(&sa->sa_mutex); 1302 if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) { 1303 sigdelset(&p->p_sigctx.ps_sigignore, signo); 1304 SIGACTION(p, signo).sa_handler = SIG_DFL; 1305 } 1306 mutex_exit(&sa->sa_mutex); 1307 } 1308 1309 /* 1310 * If the signal is being ignored, then drop it. Note: we 1311 * don't set SIGCONT in ps_sigignore, and if it is set to 1312 * SIG_IGN, action will be SIG_DFL here. 1313 */ 1314 if (sigismember(&p->p_sigctx.ps_sigignore, signo)) 1315 return; 1316 1317 else if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) 1318 action = SIG_CATCH; 1319 else { 1320 action = SIG_DFL; 1321 1322 /* 1323 * If sending a tty stop signal to a member of an 1324 * orphaned process group, discard the signal here if 1325 * the action is default; don't stop the process below 1326 * if sleeping, and don't clear any pending SIGCONT. 1327 */ 1328 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 1329 return; 1330 1331 if (prop & SA_KILL && p->p_nice > NZERO) 1332 p->p_nice = NZERO; 1333 } 1334 } 1335 1336 /* 1337 * If stopping or continuing a process, discard any pending 1338 * signals that would do the inverse. 1339 */ 1340 if ((prop & (SA_CONT | SA_STOP)) != 0) { 1341 ksiginfo_queue_init(&kq); 1342 if ((prop & SA_CONT) != 0) 1343 sigclear(&p->p_sigpend, &stopsigmask, &kq); 1344 if ((prop & SA_STOP) != 0) 1345 sigclear(&p->p_sigpend, &contsigmask, &kq); 1346 ksiginfo_queue_drain(&kq); /* XXXSMP */ 1347 } 1348 1349 /* 1350 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL, 1351 * please!), check if any LWPs are waiting on it. If yes, pass on 1352 * the signal info. The signal won't be processed further here. 1353 */ 1354 if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) && 1355 p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 && 1356 sigunwait(p, ksi)) 1357 return; 1358 1359 /* 1360 * XXXSMP Should be allocated by the caller, we're holding locks 1361 * here. 1362 */ 1363 if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL) 1364 return; 1365 1366 /* 1367 * LWP private signals are easy - just find the LWP and post 1368 * the signal to it. 1369 */ 1370 if (lid != 0) { 1371 l = lwp_find(p, lid); 1372 if (l != NULL) { 1373 sigput(&l->l_sigpend, p, kp); 1374 membar_producer(); 1375 (void)sigpost(l, action, prop, kp->ksi_signo, 0); 1376 } 1377 goto out; 1378 } 1379 1380 /* 1381 * Some signals go to all LWPs, even if posted with _lwp_kill() 1382 * or for an SA process. 1383 */ 1384 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) { 1385 if ((p->p_slflag & PSL_TRACED) != 0) 1386 goto deliver; 1387 1388 /* 1389 * If SIGCONT is default (or ignored) and process is 1390 * asleep, we are finished; the process should not 1391 * be awakened. 1392 */ 1393 if ((prop & SA_CONT) != 0 && action == SIG_DFL) 1394 goto out; 1395 1396 sigput(&p->p_sigpend, p, kp); 1397 } else { 1398 /* 1399 * Process is stopped or stopping. If traced, then no 1400 * further action is necessary. 1401 */ 1402 if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) 1403 goto out; 1404 1405 if ((prop & (SA_CONT | SA_KILL)) != 0) { 1406 /* 1407 * Re-adjust p_nstopchild if the process wasn't 1408 * collected by its parent. 1409 */ 1410 p->p_stat = SACTIVE; 1411 p->p_sflag &= ~PS_STOPPING; 1412 if (!p->p_waited) 1413 p->p_pptr->p_nstopchild--; 1414 1415 /* 1416 * If SIGCONT is default (or ignored), we continue 1417 * the process but don't leave the signal in 1418 * ps_siglist, as it has no further action. If 1419 * SIGCONT is held, we continue the process and 1420 * leave the signal in ps_siglist. If the process 1421 * catches SIGCONT, let it handle the signal itself. 1422 * If it isn't waiting on an event, then it goes 1423 * back to run state. Otherwise, process goes back 1424 * to sleep state. 1425 */ 1426 if ((prop & SA_CONT) == 0 || action != SIG_DFL) 1427 sigput(&p->p_sigpend, p, kp); 1428 } else if ((prop & SA_STOP) != 0) { 1429 /* 1430 * Already stopped, don't need to stop again. 1431 * (If we did the shell could get confused.) 1432 */ 1433 goto out; 1434 } else 1435 sigput(&p->p_sigpend, p, kp); 1436 } 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 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 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