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