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