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