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