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