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