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