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