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