1 /* $NetBSD: kern_sig.c,v 1.230 2006/10/12 01:32:17 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.230 2006/10/12 01:32:17 christos Exp $"); 41 42 #include "opt_coredump.h" 43 #include "opt_ktrace.h" 44 #include "opt_ptrace.h" 45 #include "opt_multiprocessor.h" 46 #include "opt_compat_sunos.h" 47 #include "opt_compat_netbsd.h" 48 #include "opt_compat_netbsd32.h" 49 50 #define SIGPROP /* include signal properties table */ 51 #include <sys/param.h> 52 #include <sys/signalvar.h> 53 #include <sys/resourcevar.h> 54 #include <sys/namei.h> 55 #include <sys/vnode.h> 56 #include <sys/proc.h> 57 #include <sys/systm.h> 58 #include <sys/timeb.h> 59 #include <sys/times.h> 60 #include <sys/buf.h> 61 #include <sys/acct.h> 62 #include <sys/file.h> 63 #include <sys/kernel.h> 64 #include <sys/wait.h> 65 #include <sys/ktrace.h> 66 #include <sys/syslog.h> 67 #include <sys/stat.h> 68 #include <sys/core.h> 69 #include <sys/filedesc.h> 70 #include <sys/malloc.h> 71 #include <sys/pool.h> 72 #include <sys/ucontext.h> 73 #include <sys/sa.h> 74 #include <sys/savar.h> 75 #include <sys/exec.h> 76 #include <sys/sysctl.h> 77 #include <sys/kauth.h> 78 79 #include <sys/mount.h> 80 #include <sys/syscallargs.h> 81 82 #include <machine/cpu.h> 83 84 #include <sys/user.h> /* for coredump */ 85 86 #include <uvm/uvm.h> 87 #include <uvm/uvm_extern.h> 88 89 #ifdef COREDUMP 90 static int build_corename(struct proc *, char *, const char *, size_t); 91 #endif 92 static void ksiginfo_exithook(struct proc *, void *); 93 static void ksiginfo_put(struct proc *, const ksiginfo_t *); 94 static ksiginfo_t *ksiginfo_get(struct proc *, int); 95 static void kpsignal2(struct proc *, const ksiginfo_t *, int); 96 97 sigset_t contsigmask, stopsigmask, sigcantmask; 98 99 struct pool sigacts_pool; /* memory pool for sigacts structures */ 100 101 /* 102 * struct sigacts memory pool allocator. 103 */ 104 105 static void * 106 sigacts_poolpage_alloc(struct pool *pp __unused, int flags) 107 { 108 109 return (void *)uvm_km_alloc(kernel_map, 110 (PAGE_SIZE)*2, (PAGE_SIZE)*2, 111 ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK) 112 | UVM_KMF_WIRED); 113 } 114 115 static void 116 sigacts_poolpage_free(struct pool *pp __unused, void *v) 117 { 118 uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED); 119 } 120 121 static struct pool_allocator sigactspool_allocator = { 122 .pa_alloc = sigacts_poolpage_alloc, 123 .pa_free = sigacts_poolpage_free, 124 }; 125 126 POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo", 127 &pool_allocator_nointr); 128 POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL); 129 130 /* 131 * Remove and return the first ksiginfo element that matches our requested 132 * signal, or return NULL if one not found. 133 */ 134 static ksiginfo_t * 135 ksiginfo_get(struct proc *p, int signo) 136 { 137 ksiginfo_t *ksi; 138 int s; 139 140 s = splsoftclock(); 141 simple_lock(&p->p_sigctx.ps_silock); 142 CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) { 143 if (ksi->ksi_signo == signo) { 144 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list); 145 goto out; 146 } 147 } 148 ksi = NULL; 149 out: 150 simple_unlock(&p->p_sigctx.ps_silock); 151 splx(s); 152 return ksi; 153 } 154 155 /* 156 * Append a new ksiginfo element to the list of pending ksiginfo's, if 157 * we need to (SA_SIGINFO was requested). We replace non RT signals if 158 * they already existed in the queue and we add new entries for RT signals, 159 * or for non RT signals with non-existing entries. 160 */ 161 static void 162 ksiginfo_put(struct proc *p, const ksiginfo_t *ksi) 163 { 164 ksiginfo_t *kp; 165 struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo); 166 int s; 167 168 if ((sa->sa_flags & SA_SIGINFO) == 0) 169 return; 170 /* 171 * If there's no info, don't save it. 172 */ 173 if (KSI_EMPTY_P(ksi)) 174 return; 175 176 s = splsoftclock(); 177 simple_lock(&p->p_sigctx.ps_silock); 178 #ifdef notyet /* XXX: QUEUING */ 179 if (ksi->ksi_signo < SIGRTMIN) 180 #endif 181 { 182 CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) { 183 if (kp->ksi_signo == ksi->ksi_signo) { 184 KSI_COPY(ksi, kp); 185 goto out; 186 } 187 } 188 } 189 kp = pool_get(&ksiginfo_pool, PR_NOWAIT); 190 if (kp == NULL) { 191 #ifdef DIAGNOSTIC 192 printf("Out of memory allocating siginfo for pid %d\n", 193 p->p_pid); 194 #endif 195 goto out; 196 } 197 *kp = *ksi; 198 CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list); 199 out: 200 simple_unlock(&p->p_sigctx.ps_silock); 201 splx(s); 202 } 203 204 /* 205 * free all pending ksiginfo on exit 206 */ 207 static void 208 ksiginfo_exithook(struct proc *p, void *v __unused) 209 { 210 int s; 211 212 s = splsoftclock(); 213 simple_lock(&p->p_sigctx.ps_silock); 214 while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) { 215 ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo); 216 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list); 217 pool_put(&ksiginfo_pool, ksi); 218 } 219 simple_unlock(&p->p_sigctx.ps_silock); 220 splx(s); 221 } 222 223 /* 224 * Initialize signal-related data structures. 225 */ 226 void 227 signal_init(void) 228 { 229 230 sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2; 231 232 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl", 233 sizeof(struct sigacts) > PAGE_SIZE ? 234 &sigactspool_allocator : &pool_allocator_nointr); 235 236 exithook_establish(ksiginfo_exithook, NULL); 237 exechook_establish(ksiginfo_exithook, NULL); 238 } 239 240 /* 241 * Create an initial sigctx structure, using the same signal state 242 * as p. If 'share' is set, share the sigctx_proc part, otherwise just 243 * copy it from parent. 244 */ 245 void 246 sigactsinit(struct proc *np, struct proc *pp, int share) 247 { 248 struct sigacts *ps; 249 250 if (share) { 251 np->p_sigacts = pp->p_sigacts; 252 pp->p_sigacts->sa_refcnt++; 253 } else { 254 ps = pool_get(&sigacts_pool, PR_WAITOK); 255 if (pp) 256 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts)); 257 else 258 memset(ps, '\0', sizeof(struct sigacts)); 259 ps->sa_refcnt = 1; 260 np->p_sigacts = ps; 261 } 262 } 263 264 /* 265 * Make this process not share its sigctx, maintaining all 266 * signal state. 267 */ 268 void 269 sigactsunshare(struct proc *p) 270 { 271 struct sigacts *oldps; 272 273 if (p->p_sigacts->sa_refcnt == 1) 274 return; 275 276 oldps = p->p_sigacts; 277 sigactsinit(p, NULL, 0); 278 279 if (--oldps->sa_refcnt == 0) 280 pool_put(&sigacts_pool, oldps); 281 } 282 283 /* 284 * Release a sigctx structure. 285 */ 286 void 287 sigactsfree(struct sigacts *ps) 288 { 289 290 if (--ps->sa_refcnt > 0) 291 return; 292 293 pool_put(&sigacts_pool, ps); 294 } 295 296 int 297 sigaction1(struct proc *p, int signum, const struct sigaction *nsa, 298 struct sigaction *osa, const void *tramp, int vers) 299 { 300 struct sigacts *ps; 301 int prop; 302 303 ps = p->p_sigacts; 304 if (signum <= 0 || signum >= NSIG) 305 return (EINVAL); 306 307 /* 308 * Trampoline ABI version 0 is reserved for the legacy 309 * kernel-provided on-stack trampoline. Conversely, if we are 310 * using a non-0 ABI version, we must have a trampoline. Only 311 * validate the vers if a new sigaction was supplied. Emulations 312 * use legacy kernel trampolines with version 0, alternatively 313 * check for that too. 314 */ 315 if ((vers != 0 && tramp == NULL) || 316 #ifdef SIGTRAMP_VALID 317 (nsa != NULL && 318 ((vers == 0) ? 319 (p->p_emul->e_sigcode == NULL) : 320 !SIGTRAMP_VALID(vers))) || 321 #endif 322 (vers == 0 && tramp != NULL)) 323 return (EINVAL); 324 325 if (osa) 326 *osa = SIGACTION_PS(ps, signum); 327 328 if (nsa) { 329 if (nsa->sa_flags & ~SA_ALLBITS) 330 return (EINVAL); 331 332 prop = sigprop[signum]; 333 if (prop & SA_CANTMASK) 334 return (EINVAL); 335 336 (void) splsched(); /* XXXSMP */ 337 SIGACTION_PS(ps, signum) = *nsa; 338 ps->sa_sigdesc[signum].sd_tramp = tramp; 339 ps->sa_sigdesc[signum].sd_vers = vers; 340 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask); 341 if ((prop & SA_NORESET) != 0) 342 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND; 343 if (signum == SIGCHLD) { 344 if (nsa->sa_flags & SA_NOCLDSTOP) 345 p->p_flag |= P_NOCLDSTOP; 346 else 347 p->p_flag &= ~P_NOCLDSTOP; 348 if (nsa->sa_flags & SA_NOCLDWAIT) { 349 /* 350 * Paranoia: since SA_NOCLDWAIT is implemented 351 * by reparenting the dying child to PID 1 (and 352 * trust it to reap the zombie), PID 1 itself 353 * is forbidden to set SA_NOCLDWAIT. 354 */ 355 if (p->p_pid == 1) 356 p->p_flag &= ~P_NOCLDWAIT; 357 else 358 p->p_flag |= P_NOCLDWAIT; 359 } else 360 p->p_flag &= ~P_NOCLDWAIT; 361 362 if (nsa->sa_handler == SIG_IGN) { 363 /* 364 * Paranoia: same as above. 365 */ 366 if (p->p_pid == 1) 367 p->p_flag &= ~P_CLDSIGIGN; 368 else 369 p->p_flag |= P_CLDSIGIGN; 370 } else 371 p->p_flag &= ~P_CLDSIGIGN; 372 373 } 374 if ((nsa->sa_flags & SA_NODEFER) == 0) 375 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum); 376 else 377 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum); 378 /* 379 * Set bit in p_sigctx.ps_sigignore for signals that are set to 380 * SIG_IGN, and for signals set to SIG_DFL where the default is 381 * to ignore. However, don't put SIGCONT in 382 * p_sigctx.ps_sigignore, as we have to restart the process. 383 */ 384 if (nsa->sa_handler == SIG_IGN || 385 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) { 386 /* never to be seen again */ 387 sigdelset(&p->p_sigctx.ps_siglist, signum); 388 if (signum != SIGCONT) { 389 /* easier in psignal */ 390 sigaddset(&p->p_sigctx.ps_sigignore, signum); 391 } 392 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 393 } else { 394 sigdelset(&p->p_sigctx.ps_sigignore, signum); 395 if (nsa->sa_handler == SIG_DFL) 396 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 397 else 398 sigaddset(&p->p_sigctx.ps_sigcatch, signum); 399 } 400 (void) spl0(); 401 } 402 403 return (0); 404 } 405 406 #ifdef COMPAT_16 407 /* ARGSUSED */ 408 int 409 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval __unused) 410 { 411 struct compat_16_sys___sigaction14_args /* { 412 syscallarg(int) signum; 413 syscallarg(const struct sigaction *) nsa; 414 syscallarg(struct sigaction *) osa; 415 } */ *uap = v; 416 struct proc *p; 417 struct sigaction nsa, osa; 418 int error; 419 420 if (SCARG(uap, nsa)) { 421 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 422 if (error) 423 return (error); 424 } 425 p = l->l_proc; 426 error = sigaction1(p, SCARG(uap, signum), 427 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 428 NULL, 0); 429 if (error) 430 return (error); 431 if (SCARG(uap, osa)) { 432 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 433 if (error) 434 return (error); 435 } 436 return (0); 437 } 438 #endif 439 440 /* ARGSUSED */ 441 int 442 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval __unused) 443 { 444 struct sys___sigaction_sigtramp_args /* { 445 syscallarg(int) signum; 446 syscallarg(const struct sigaction *) nsa; 447 syscallarg(struct sigaction *) osa; 448 syscallarg(void *) tramp; 449 syscallarg(int) vers; 450 } */ *uap = v; 451 struct proc *p = l->l_proc; 452 struct sigaction nsa, osa; 453 int error; 454 455 if (SCARG(uap, nsa)) { 456 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 457 if (error) 458 return (error); 459 } 460 error = sigaction1(p, SCARG(uap, signum), 461 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 462 SCARG(uap, tramp), SCARG(uap, vers)); 463 if (error) 464 return (error); 465 if (SCARG(uap, osa)) { 466 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 467 if (error) 468 return (error); 469 } 470 return (0); 471 } 472 473 /* 474 * Initialize signal state for process 0; 475 * set to ignore signals that are ignored by default and disable the signal 476 * stack. 477 */ 478 void 479 siginit(struct proc *p) 480 { 481 struct sigacts *ps; 482 int signum, prop; 483 484 ps = p->p_sigacts; 485 sigemptyset(&contsigmask); 486 sigemptyset(&stopsigmask); 487 sigemptyset(&sigcantmask); 488 for (signum = 1; signum < NSIG; signum++) { 489 prop = sigprop[signum]; 490 if (prop & SA_CONT) 491 sigaddset(&contsigmask, signum); 492 if (prop & SA_STOP) 493 sigaddset(&stopsigmask, signum); 494 if (prop & SA_CANTMASK) 495 sigaddset(&sigcantmask, signum); 496 if (prop & SA_IGNORE && signum != SIGCONT) 497 sigaddset(&p->p_sigctx.ps_sigignore, signum); 498 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 499 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 500 } 501 sigemptyset(&p->p_sigctx.ps_sigcatch); 502 p->p_sigctx.ps_sigwaited = NULL; 503 p->p_flag &= ~P_NOCLDSTOP; 504 505 /* 506 * Reset stack state to the user stack. 507 */ 508 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 509 p->p_sigctx.ps_sigstk.ss_size = 0; 510 p->p_sigctx.ps_sigstk.ss_sp = 0; 511 512 /* One reference. */ 513 ps->sa_refcnt = 1; 514 } 515 516 /* 517 * Reset signals for an exec of the specified process. 518 */ 519 void 520 execsigs(struct proc *p) 521 { 522 struct sigacts *ps; 523 int signum, prop; 524 525 sigactsunshare(p); 526 527 ps = p->p_sigacts; 528 529 /* 530 * Reset caught signals. Held signals remain held 531 * through p_sigctx.ps_sigmask (unless they were caught, 532 * and are now ignored by default). 533 */ 534 for (signum = 1; signum < NSIG; signum++) { 535 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 536 prop = sigprop[signum]; 537 if (prop & SA_IGNORE) { 538 if ((prop & SA_CONT) == 0) 539 sigaddset(&p->p_sigctx.ps_sigignore, 540 signum); 541 sigdelset(&p->p_sigctx.ps_siglist, signum); 542 } 543 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 544 } 545 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 546 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 547 } 548 sigemptyset(&p->p_sigctx.ps_sigcatch); 549 p->p_sigctx.ps_sigwaited = NULL; 550 551 /* 552 * Reset no zombies if child dies flag as Solaris does. 553 */ 554 p->p_flag &= ~(P_NOCLDWAIT | P_CLDSIGIGN); 555 if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN) 556 SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL; 557 558 /* 559 * Reset stack state to the user stack. 560 */ 561 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 562 p->p_sigctx.ps_sigstk.ss_size = 0; 563 p->p_sigctx.ps_sigstk.ss_sp = 0; 564 } 565 566 int 567 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss) 568 { 569 570 if (oss) 571 *oss = p->p_sigctx.ps_sigmask; 572 573 if (nss) { 574 (void)splsched(); /* XXXSMP */ 575 switch (how) { 576 case SIG_BLOCK: 577 sigplusset(nss, &p->p_sigctx.ps_sigmask); 578 break; 579 case SIG_UNBLOCK: 580 sigminusset(nss, &p->p_sigctx.ps_sigmask); 581 CHECKSIGS(p); 582 break; 583 case SIG_SETMASK: 584 p->p_sigctx.ps_sigmask = *nss; 585 CHECKSIGS(p); 586 break; 587 default: 588 (void)spl0(); /* XXXSMP */ 589 return (EINVAL); 590 } 591 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 592 (void)spl0(); /* XXXSMP */ 593 } 594 595 return (0); 596 } 597 598 /* 599 * Manipulate signal mask. 600 * Note that we receive new mask, not pointer, 601 * and return old mask as return value; 602 * the library stub does the rest. 603 */ 604 int 605 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval __unused) 606 { 607 struct sys___sigprocmask14_args /* { 608 syscallarg(int) how; 609 syscallarg(const sigset_t *) set; 610 syscallarg(sigset_t *) oset; 611 } */ *uap = v; 612 struct proc *p; 613 sigset_t nss, oss; 614 int error; 615 616 if (SCARG(uap, set)) { 617 error = copyin(SCARG(uap, set), &nss, sizeof(nss)); 618 if (error) 619 return (error); 620 } 621 p = l->l_proc; 622 error = sigprocmask1(p, SCARG(uap, how), 623 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0); 624 if (error) 625 return (error); 626 if (SCARG(uap, oset)) { 627 error = copyout(&oss, SCARG(uap, oset), sizeof(oss)); 628 if (error) 629 return (error); 630 } 631 return (0); 632 } 633 634 void 635 sigpending1(struct proc *p, sigset_t *ss) 636 { 637 638 *ss = p->p_sigctx.ps_siglist; 639 sigminusset(&p->p_sigctx.ps_sigmask, ss); 640 } 641 642 /* ARGSUSED */ 643 int 644 sys___sigpending14(struct lwp *l, void *v, register_t *retval __unused) 645 { 646 struct sys___sigpending14_args /* { 647 syscallarg(sigset_t *) set; 648 } */ *uap = v; 649 struct proc *p; 650 sigset_t ss; 651 652 p = l->l_proc; 653 sigpending1(p, &ss); 654 return (copyout(&ss, SCARG(uap, set), sizeof(ss))); 655 } 656 657 int 658 sigsuspend1(struct proc *p, const sigset_t *ss) 659 { 660 struct sigacts *ps; 661 662 ps = p->p_sigacts; 663 if (ss) { 664 /* 665 * When returning from sigpause, we want 666 * the old mask to be restored after the 667 * signal handler has finished. Thus, we 668 * save it here and mark the sigctx structure 669 * to indicate this. 670 */ 671 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask; 672 p->p_sigctx.ps_flags |= SAS_OLDMASK; 673 (void) splsched(); /* XXXSMP */ 674 p->p_sigctx.ps_sigmask = *ss; 675 CHECKSIGS(p); 676 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 677 (void) spl0(); /* XXXSMP */ 678 } 679 680 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 681 /* void */; 682 683 /* always return EINTR rather than ERESTART... */ 684 return (EINTR); 685 } 686 687 /* 688 * Suspend process until signal, providing mask to be set 689 * in the meantime. Note nonstandard calling convention: 690 * libc stub passes mask, not pointer, to save a copyin. 691 */ 692 /* ARGSUSED */ 693 int 694 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval __unused) 695 { 696 struct sys___sigsuspend14_args /* { 697 syscallarg(const sigset_t *) set; 698 } */ *uap = v; 699 struct proc *p; 700 sigset_t ss; 701 int error; 702 703 if (SCARG(uap, set)) { 704 error = copyin(SCARG(uap, set), &ss, sizeof(ss)); 705 if (error) 706 return (error); 707 } 708 709 p = l->l_proc; 710 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0)); 711 } 712 713 int 714 sigaltstack1(struct proc *p, const struct sigaltstack *nss, 715 struct sigaltstack *oss) 716 { 717 718 if (oss) 719 *oss = p->p_sigctx.ps_sigstk; 720 721 if (nss) { 722 if (nss->ss_flags & ~SS_ALLBITS) 723 return (EINVAL); 724 725 if (nss->ss_flags & SS_DISABLE) { 726 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) 727 return (EINVAL); 728 } else { 729 if (nss->ss_size < MINSIGSTKSZ) 730 return (ENOMEM); 731 } 732 p->p_sigctx.ps_sigstk = *nss; 733 } 734 735 return (0); 736 } 737 738 /* ARGSUSED */ 739 int 740 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval __unused) 741 { 742 struct sys___sigaltstack14_args /* { 743 syscallarg(const struct sigaltstack *) nss; 744 syscallarg(struct sigaltstack *) oss; 745 } */ *uap = v; 746 struct proc *p; 747 struct sigaltstack nss, oss; 748 int error; 749 750 if (SCARG(uap, nss)) { 751 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 752 if (error) 753 return (error); 754 } 755 p = l->l_proc; 756 error = sigaltstack1(p, 757 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0); 758 if (error) 759 return (error); 760 if (SCARG(uap, oss)) { 761 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 762 if (error) 763 return (error); 764 } 765 return (0); 766 } 767 768 /* ARGSUSED */ 769 int 770 sys_kill(struct lwp *l, void *v, register_t *retval __unused) 771 { 772 struct sys_kill_args /* { 773 syscallarg(int) pid; 774 syscallarg(int) signum; 775 } */ *uap = v; 776 struct proc *p; 777 ksiginfo_t ksi; 778 int signum = SCARG(uap, signum); 779 int error; 780 781 if ((u_int)signum >= NSIG) 782 return (EINVAL); 783 KSI_INIT(&ksi); 784 ksi.ksi_signo = signum; 785 ksi.ksi_code = SI_USER; 786 ksi.ksi_pid = l->l_proc->p_pid; 787 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 788 if (SCARG(uap, pid) > 0) { 789 /* kill single process */ 790 if ((p = pfind(SCARG(uap, pid))) == NULL) 791 return (ESRCH); 792 error = kauth_authorize_process(l->l_cred, 793 KAUTH_PROCESS_CANSIGNAL, p, (void *)(uintptr_t)signum, 794 NULL, NULL); 795 if (error) 796 return error; 797 if (signum) 798 kpsignal2(p, &ksi, 1); 799 return (0); 800 } 801 switch (SCARG(uap, pid)) { 802 case -1: /* broadcast signal */ 803 return (killpg1(l, &ksi, 0, 1)); 804 case 0: /* signal own process group */ 805 return (killpg1(l, &ksi, 0, 0)); 806 default: /* negative explicit process group */ 807 return (killpg1(l, &ksi, -SCARG(uap, pid), 0)); 808 } 809 /* NOTREACHED */ 810 } 811 812 /* 813 * Common code for kill process group/broadcast kill. 814 * cp is calling process. 815 */ 816 int 817 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all) 818 { 819 struct proc *p, *cp; 820 kauth_cred_t pc; 821 struct pgrp *pgrp; 822 int nfound; 823 int signum = ksi->ksi_signo; 824 825 cp = l->l_proc; 826 pc = l->l_cred; 827 nfound = 0; 828 if (all) { 829 /* 830 * broadcast 831 */ 832 proclist_lock_read(); 833 PROCLIST_FOREACH(p, &allproc) { 834 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp || 835 kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL, 836 p, (void *)(uintptr_t)signum, NULL, NULL) != 0) 837 continue; 838 nfound++; 839 if (signum) 840 kpsignal2(p, ksi, 1); 841 } 842 proclist_unlock_read(); 843 } else { 844 if (pgid == 0) 845 /* 846 * zero pgid means send to my process group. 847 */ 848 pgrp = cp->p_pgrp; 849 else { 850 pgrp = pgfind(pgid); 851 if (pgrp == NULL) 852 return (ESRCH); 853 } 854 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 855 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 856 kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL, 857 p, (void *)(uintptr_t)signum, NULL, NULL) != 0) 858 continue; 859 nfound++; 860 if (signum && P_ZOMBIE(p) == 0) 861 kpsignal2(p, ksi, 1); 862 } 863 } 864 return (nfound ? 0 : ESRCH); 865 } 866 867 /* 868 * Send a signal to a process group. 869 */ 870 void 871 gsignal(int pgid, int signum) 872 { 873 ksiginfo_t ksi; 874 KSI_INIT_EMPTY(&ksi); 875 ksi.ksi_signo = signum; 876 kgsignal(pgid, &ksi, NULL); 877 } 878 879 void 880 kgsignal(int pgid, ksiginfo_t *ksi, void *data) 881 { 882 struct pgrp *pgrp; 883 884 if (pgid && (pgrp = pgfind(pgid))) 885 kpgsignal(pgrp, ksi, data, 0); 886 } 887 888 /* 889 * Send a signal to a process group. If checktty is 1, 890 * limit to members which have a controlling terminal. 891 */ 892 void 893 pgsignal(struct pgrp *pgrp, int sig, int checkctty) 894 { 895 ksiginfo_t ksi; 896 KSI_INIT_EMPTY(&ksi); 897 ksi.ksi_signo = sig; 898 kpgsignal(pgrp, &ksi, NULL, checkctty); 899 } 900 901 void 902 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty) 903 { 904 struct proc *p; 905 906 if (pgrp) 907 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 908 if (checkctty == 0 || p->p_flag & P_CONTROLT) 909 kpsignal(p, ksi, data); 910 } 911 912 /* 913 * Send a signal caused by a trap to the current process. 914 * If it will be caught immediately, deliver it with correct code. 915 * Otherwise, post it normally. 916 */ 917 void 918 trapsignal(struct lwp *l, const ksiginfo_t *ksi) 919 { 920 struct proc *p; 921 struct sigacts *ps; 922 int signum = ksi->ksi_signo; 923 924 KASSERT(KSI_TRAP_P(ksi)); 925 926 p = l->l_proc; 927 ps = p->p_sigacts; 928 if ((p->p_flag & P_TRACED) == 0 && 929 sigismember(&p->p_sigctx.ps_sigcatch, signum) && 930 !sigismember(&p->p_sigctx.ps_sigmask, signum)) { 931 p->p_stats->p_ru.ru_nsignals++; 932 #ifdef KTRACE 933 if (KTRPOINT(p, KTR_PSIG)) 934 ktrpsig(l, signum, SIGACTION_PS(ps, signum).sa_handler, 935 &p->p_sigctx.ps_sigmask, ksi); 936 #endif 937 kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask); 938 (void) splsched(); /* XXXSMP */ 939 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 940 &p->p_sigctx.ps_sigmask); 941 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 942 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 943 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 944 sigaddset(&p->p_sigctx.ps_sigignore, signum); 945 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 946 } 947 (void) spl0(); /* XXXSMP */ 948 } else { 949 p->p_sigctx.ps_lwp = l->l_lid; 950 /* XXX for core dump/debugger */ 951 p->p_sigctx.ps_signo = ksi->ksi_signo; 952 p->p_sigctx.ps_code = ksi->ksi_trap; 953 kpsignal2(p, ksi, 1); 954 } 955 } 956 957 /* 958 * Fill in signal information and signal the parent for a child status change. 959 */ 960 void 961 child_psignal(struct proc *p, int dolock) 962 { 963 ksiginfo_t ksi; 964 965 KSI_INIT(&ksi); 966 ksi.ksi_signo = SIGCHLD; 967 ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED; 968 ksi.ksi_pid = p->p_pid; 969 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred); 970 ksi.ksi_status = p->p_xstat; 971 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 972 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 973 kpsignal2(p->p_pptr, &ksi, dolock); 974 } 975 976 /* 977 * Send the signal to the process. If the signal has an action, the action 978 * is usually performed by the target process rather than the caller; we add 979 * the signal to the set of pending signals for the process. 980 * 981 * Exceptions: 982 * o When a stop signal is sent to a sleeping process that takes the 983 * default action, the process is stopped without awakening it. 984 * o SIGCONT restarts stopped processes (or puts them back to sleep) 985 * regardless of the signal action (eg, blocked or ignored). 986 * 987 * Other ignored signals are discarded immediately. 988 * 989 * XXXSMP: Invoked as psignal() or sched_psignal(). 990 */ 991 void 992 psignal1(struct proc *p, int signum, int dolock) 993 { 994 ksiginfo_t ksi; 995 996 KSI_INIT_EMPTY(&ksi); 997 ksi.ksi_signo = signum; 998 kpsignal2(p, &ksi, dolock); 999 } 1000 1001 void 1002 kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock) 1003 { 1004 1005 if ((p->p_flag & P_WEXIT) == 0 && data) { 1006 size_t fd; 1007 struct filedesc *fdp = p->p_fd; 1008 1009 ksi->ksi_fd = -1; 1010 for (fd = 0; fd < fdp->fd_nfiles; fd++) { 1011 struct file *fp = fdp->fd_ofiles[fd]; 1012 /* XXX: lock? */ 1013 if (fp && fp->f_data == data) { 1014 ksi->ksi_fd = fd; 1015 break; 1016 } 1017 } 1018 } 1019 kpsignal2(p, ksi, dolock); 1020 } 1021 1022 static void 1023 kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock) 1024 { 1025 struct lwp *l, *suspended = NULL; 1026 struct sadata_vp *vp; 1027 int s = 0, prop, allsusp; 1028 sig_t action; 1029 int signum = ksi->ksi_signo; 1030 1031 #ifdef DIAGNOSTIC 1032 if (signum <= 0 || signum >= NSIG) 1033 panic("psignal signal number %d", signum); 1034 1035 /* XXXSMP: works, but icky */ 1036 if (dolock) { 1037 SCHED_ASSERT_UNLOCKED(); 1038 } else { 1039 SCHED_ASSERT_LOCKED(); 1040 } 1041 #endif 1042 1043 /* 1044 * Notify any interested parties in the signal. 1045 */ 1046 KNOTE(&p->p_klist, NOTE_SIGNAL | signum); 1047 1048 prop = sigprop[signum]; 1049 1050 /* 1051 * If proc is traced, always give parent a chance. 1052 */ 1053 if (p->p_flag & P_TRACED) { 1054 action = SIG_DFL; 1055 1056 /* 1057 * If the process is being traced and the signal is being 1058 * caught, make sure to save any ksiginfo. 1059 */ 1060 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) 1061 ksiginfo_put(p, ksi); 1062 } else { 1063 /* 1064 * If the signal was the result of a trap, reset it 1065 * to default action if it's currently masked, so that it would 1066 * coredump immediatelly instead of spinning repeatedly 1067 * taking the signal. 1068 */ 1069 if (KSI_TRAP_P(ksi) 1070 && sigismember(&p->p_sigctx.ps_sigmask, signum) 1071 && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 1072 sigdelset(&p->p_sigctx.ps_sigignore, signum); 1073 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1074 sigdelset(&p->p_sigctx.ps_sigmask, signum); 1075 SIGACTION(p, signum).sa_handler = SIG_DFL; 1076 } 1077 1078 /* 1079 * If the signal is being ignored, 1080 * then we forget about it immediately. 1081 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore, 1082 * and if it is set to SIG_IGN, 1083 * action will be SIG_DFL here.) 1084 */ 1085 if (sigismember(&p->p_sigctx.ps_sigignore, signum)) 1086 return; 1087 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1088 action = SIG_HOLD; 1089 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) 1090 action = SIG_CATCH; 1091 else { 1092 action = SIG_DFL; 1093 1094 if (prop & SA_KILL && p->p_nice > NZERO) 1095 p->p_nice = NZERO; 1096 1097 /* 1098 * If sending a tty stop signal to a member of an 1099 * orphaned process group, discard the signal here if 1100 * the action is default; don't stop the process below 1101 * if sleeping, and don't clear any pending SIGCONT. 1102 */ 1103 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 1104 return; 1105 } 1106 } 1107 1108 if (prop & SA_CONT) 1109 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist); 1110 1111 if (prop & SA_STOP) 1112 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist); 1113 1114 /* 1115 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL, 1116 * please!), check if anything waits on it. If yes, save the 1117 * info into provided ps_sigwaited, and wake-up the waiter. 1118 * The signal won't be processed further here. 1119 */ 1120 if ((prop & SA_CANTMASK) == 0 1121 && p->p_sigctx.ps_sigwaited 1122 && sigismember(p->p_sigctx.ps_sigwait, signum) 1123 && p->p_stat != SSTOP) { 1124 p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info; 1125 p->p_sigctx.ps_sigwaited = NULL; 1126 if (dolock) 1127 wakeup_one(&p->p_sigctx.ps_sigwait); 1128 else 1129 sched_wakeup(&p->p_sigctx.ps_sigwait); 1130 return; 1131 } 1132 1133 sigaddset(&p->p_sigctx.ps_siglist, signum); 1134 1135 /* CHECKSIGS() is "inlined" here. */ 1136 p->p_sigctx.ps_sigcheck = 1; 1137 1138 /* 1139 * Defer further processing for signals which are held, 1140 * except that stopped processes must be continued by SIGCONT. 1141 */ 1142 if (action == SIG_HOLD && 1143 ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) { 1144 ksiginfo_put(p, ksi); 1145 return; 1146 } 1147 /* XXXSMP: works, but icky */ 1148 if (dolock) 1149 SCHED_LOCK(s); 1150 1151 if (p->p_flag & P_SA) { 1152 allsusp = 0; 1153 l = NULL; 1154 if (p->p_stat == SACTIVE) { 1155 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1156 l = vp->savp_lwp; 1157 KDASSERT(l != NULL); 1158 if (l->l_flag & L_SA_IDLE) { 1159 /* wakeup idle LWP */ 1160 goto found; 1161 /*NOTREACHED*/ 1162 } else if (l->l_flag & L_SA_YIELD) { 1163 /* idle LWP is already waking up */ 1164 goto out; 1165 /*NOTREACHED*/ 1166 } 1167 } 1168 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1169 l = vp->savp_lwp; 1170 if (l->l_stat == LSRUN || 1171 l->l_stat == LSONPROC) { 1172 signotify(p); 1173 goto out; 1174 /*NOTREACHED*/ 1175 } 1176 if (l->l_stat == LSSLEEP && 1177 l->l_flag & L_SINTR) { 1178 /* ok to signal vp lwp */ 1179 break; 1180 } else 1181 l = NULL; 1182 } 1183 } else if (p->p_stat == SSTOP) { 1184 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1185 l = vp->savp_lwp; 1186 if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0) 1187 break; 1188 l = NULL; 1189 } 1190 } 1191 } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) { 1192 /* 1193 * At least one LWP is running or on a run queue. 1194 * The signal will be noticed when one of them returns 1195 * to userspace. 1196 */ 1197 signotify(p); 1198 /* 1199 * The signal will be noticed very soon. 1200 */ 1201 goto out; 1202 /*NOTREACHED*/ 1203 } else { 1204 /* 1205 * Find out if any of the sleeps are interruptable, 1206 * and if all the live LWPs remaining are suspended. 1207 */ 1208 allsusp = 1; 1209 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1210 if (l->l_stat == LSSLEEP && 1211 l->l_flag & L_SINTR) 1212 break; 1213 if (l->l_stat == LSSUSPENDED) 1214 suspended = l; 1215 else if ((l->l_stat != LSZOMB) && 1216 (l->l_stat != LSDEAD)) 1217 allsusp = 0; 1218 } 1219 } 1220 1221 found: 1222 switch (p->p_stat) { 1223 case SACTIVE: 1224 1225 if (l != NULL && (p->p_flag & P_TRACED)) 1226 goto run; 1227 1228 /* 1229 * If SIGCONT is default (or ignored) and process is 1230 * asleep, we are finished; the process should not 1231 * be awakened. 1232 */ 1233 if ((prop & SA_CONT) && action == SIG_DFL) { 1234 sigdelset(&p->p_sigctx.ps_siglist, signum); 1235 goto done; 1236 } 1237 1238 /* 1239 * When a sleeping process receives a stop 1240 * signal, process immediately if possible. 1241 */ 1242 if ((prop & SA_STOP) && action == SIG_DFL) { 1243 /* 1244 * If a child holding parent blocked, 1245 * stopping could cause deadlock. 1246 */ 1247 if (p->p_flag & P_PPWAIT) { 1248 goto out; 1249 } 1250 sigdelset(&p->p_sigctx.ps_siglist, signum); 1251 p->p_xstat = signum; 1252 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { 1253 /* 1254 * XXXSMP: recursive call; don't lock 1255 * the second time around. 1256 */ 1257 child_psignal(p, 0); 1258 } 1259 proc_stop(p, 1); /* XXXSMP: recurse? */ 1260 goto done; 1261 } 1262 1263 if (l == NULL) { 1264 /* 1265 * Special case: SIGKILL of a process 1266 * which is entirely composed of 1267 * suspended LWPs should succeed. We 1268 * make this happen by unsuspending one of 1269 * them. 1270 */ 1271 if (allsusp && (signum == SIGKILL)) { 1272 lwp_continue(suspended); 1273 } 1274 goto done; 1275 } 1276 /* 1277 * All other (caught or default) signals 1278 * cause the process to run. 1279 */ 1280 goto runfast; 1281 /*NOTREACHED*/ 1282 case SSTOP: 1283 /* Process is stopped */ 1284 /* 1285 * If traced process is already stopped, 1286 * then no further action is necessary. 1287 */ 1288 if (p->p_flag & P_TRACED) 1289 goto done; 1290 1291 /* 1292 * Kill signal always sets processes running, 1293 * if possible. 1294 */ 1295 if (signum == SIGKILL) { 1296 l = proc_unstop(p); 1297 if (l) 1298 goto runfast; 1299 goto done; 1300 } 1301 1302 if (prop & SA_CONT) { 1303 /* 1304 * If SIGCONT is default (or ignored), 1305 * we continue the process but don't 1306 * leave the signal in ps_siglist, as 1307 * it has no further action. If 1308 * SIGCONT is held, we continue the 1309 * process and leave the signal in 1310 * ps_siglist. If the process catches 1311 * SIGCONT, let it handle the signal 1312 * itself. If it isn't waiting on an 1313 * event, then it goes back to run 1314 * state. Otherwise, process goes 1315 * back to sleep state. 1316 */ 1317 if (action == SIG_DFL) 1318 sigdelset(&p->p_sigctx.ps_siglist, 1319 signum); 1320 l = proc_unstop(p); 1321 if (l && (action == SIG_CATCH)) 1322 goto runfast; 1323 goto out; 1324 } 1325 1326 if (prop & SA_STOP) { 1327 /* 1328 * Already stopped, don't need to stop again. 1329 * (If we did the shell could get confused.) 1330 */ 1331 sigdelset(&p->p_sigctx.ps_siglist, signum); 1332 goto done; 1333 } 1334 1335 /* 1336 * If a lwp is sleeping interruptibly, then 1337 * wake it up; it will run until the kernel 1338 * boundary, where it will stop in issignal(), 1339 * since p->p_stat is still SSTOP. When the 1340 * process is continued, it will be made 1341 * runnable and can look at the signal. 1342 */ 1343 if (l) 1344 goto run; 1345 goto out; 1346 case SIDL: 1347 /* Process is being created by fork */ 1348 /* XXX: We are not ready to receive signals yet */ 1349 goto done; 1350 default: 1351 /* Else what? */ 1352 panic("psignal: Invalid process state %d.", p->p_stat); 1353 } 1354 /*NOTREACHED*/ 1355 1356 runfast: 1357 if (action == SIG_CATCH) { 1358 ksiginfo_put(p, ksi); 1359 action = SIG_HOLD; 1360 } 1361 /* 1362 * Raise priority to at least PUSER. 1363 */ 1364 if (l->l_priority > PUSER) 1365 l->l_priority = PUSER; 1366 run: 1367 if (action == SIG_CATCH) { 1368 ksiginfo_put(p, ksi); 1369 action = SIG_HOLD; 1370 } 1371 1372 setrunnable(l); /* XXXSMP: recurse? */ 1373 out: 1374 if (action == SIG_CATCH) 1375 ksiginfo_put(p, ksi); 1376 done: 1377 /* XXXSMP: works, but icky */ 1378 if (dolock) 1379 SCHED_UNLOCK(s); 1380 } 1381 1382 siginfo_t * 1383 siginfo_alloc(int flags) 1384 { 1385 1386 return pool_get(&siginfo_pool, flags); 1387 } 1388 1389 void 1390 siginfo_free(void *arg) 1391 { 1392 1393 pool_put(&siginfo_pool, arg); 1394 } 1395 1396 void 1397 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask) 1398 { 1399 struct proc *p = l->l_proc; 1400 struct lwp *le, *li; 1401 siginfo_t *si; 1402 int f; 1403 1404 if (p->p_flag & P_SA) { 1405 1406 /* XXXUPSXXX What if not on sa_vp ? */ 1407 1408 f = l->l_flag & L_SA; 1409 l->l_flag &= ~L_SA; 1410 si = siginfo_alloc(PR_WAITOK); 1411 si->_info = ksi->ksi_info; 1412 le = li = NULL; 1413 if (KSI_TRAP_P(ksi)) 1414 le = l; 1415 else 1416 li = l; 1417 if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li, 1418 sizeof(*si), si, siginfo_free) != 0) { 1419 siginfo_free(si); 1420 #if 0 1421 if (KSI_TRAP_P(ksi)) 1422 /* XXX What do we do here?? */; 1423 #endif 1424 } 1425 l->l_flag |= f; 1426 return; 1427 } 1428 1429 (*p->p_emul->e_sendsig)(ksi, mask); 1430 } 1431 1432 static inline int firstsig(const sigset_t *); 1433 1434 static inline int 1435 firstsig(const sigset_t *ss) 1436 { 1437 int sig; 1438 1439 sig = ffs(ss->__bits[0]); 1440 if (sig != 0) 1441 return (sig); 1442 #if NSIG > 33 1443 sig = ffs(ss->__bits[1]); 1444 if (sig != 0) 1445 return (sig + 32); 1446 #endif 1447 #if NSIG > 65 1448 sig = ffs(ss->__bits[2]); 1449 if (sig != 0) 1450 return (sig + 64); 1451 #endif 1452 #if NSIG > 97 1453 sig = ffs(ss->__bits[3]); 1454 if (sig != 0) 1455 return (sig + 96); 1456 #endif 1457 return (0); 1458 } 1459 1460 /* 1461 * If the current process has received a signal (should be caught or cause 1462 * termination, should interrupt current syscall), return the signal number. 1463 * Stop signals with default action are processed immediately, then cleared; 1464 * they aren't returned. This is checked after each entry to the system for 1465 * a syscall or trap (though this can usually be done without calling issignal 1466 * by checking the pending signal masks in the CURSIG macro.) The normal call 1467 * sequence is 1468 * 1469 * while (signum = CURSIG(curlwp)) 1470 * postsig(signum); 1471 */ 1472 int 1473 issignal(struct lwp *l) 1474 { 1475 struct proc *p = l->l_proc; 1476 int s = 0, signum, prop; 1477 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock; 1478 sigset_t ss; 1479 1480 /* Bail out if we do not own the virtual processor */ 1481 if (l->l_flag & L_SA && l->l_savp->savp_lwp != l) 1482 return 0; 1483 1484 if (p->p_stat == SSTOP) { 1485 /* 1486 * The process is stopped/stopping. Stop ourselves now that 1487 * we're on the kernel/userspace boundary. 1488 */ 1489 if (dolock) 1490 SCHED_LOCK(s); 1491 l->l_stat = LSSTOP; 1492 p->p_nrlwps--; 1493 if (p->p_flag & P_TRACED) 1494 goto sigtraceswitch; 1495 else 1496 goto sigswitch; 1497 } 1498 for (;;) { 1499 sigpending1(p, &ss); 1500 if (p->p_flag & P_PPWAIT) 1501 sigminusset(&stopsigmask, &ss); 1502 signum = firstsig(&ss); 1503 if (signum == 0) { /* no signal to send */ 1504 p->p_sigctx.ps_sigcheck = 0; 1505 if (locked && dolock) 1506 SCHED_LOCK(s); 1507 return (0); 1508 } 1509 /* take the signal! */ 1510 sigdelset(&p->p_sigctx.ps_siglist, signum); 1511 1512 /* 1513 * We should see pending but ignored signals 1514 * only if P_TRACED was on when they were posted. 1515 */ 1516 if (sigismember(&p->p_sigctx.ps_sigignore, signum) && 1517 (p->p_flag & P_TRACED) == 0) 1518 continue; 1519 1520 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1521 /* 1522 * If traced, always stop, and stay 1523 * stopped until released by the debugger. 1524 */ 1525 p->p_xstat = signum; 1526 1527 /* Emulation-specific handling of signal trace */ 1528 if ((p->p_emul->e_tracesig != NULL) && 1529 ((*p->p_emul->e_tracesig)(p, signum) != 0)) 1530 goto childresumed; 1531 1532 if ((p->p_flag & P_FSTRACE) == 0) 1533 child_psignal(p, dolock); 1534 if (dolock) 1535 SCHED_LOCK(s); 1536 proc_stop(p, 1); 1537 sigtraceswitch: 1538 mi_switch(l, NULL); 1539 SCHED_ASSERT_UNLOCKED(); 1540 if (dolock) 1541 splx(s); 1542 else 1543 dolock = 1; 1544 1545 childresumed: 1546 /* 1547 * If we are no longer being traced, or the parent 1548 * didn't give us a signal, look for more signals. 1549 */ 1550 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0) 1551 continue; 1552 1553 /* 1554 * If the new signal is being masked, look for other 1555 * signals. 1556 */ 1557 signum = p->p_xstat; 1558 p->p_xstat = 0; 1559 /* 1560 * `p->p_sigctx.ps_siglist |= mask' is done 1561 * in setrunnable(). 1562 */ 1563 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1564 continue; 1565 /* take the signal! */ 1566 sigdelset(&p->p_sigctx.ps_siglist, signum); 1567 } 1568 1569 prop = sigprop[signum]; 1570 1571 /* 1572 * Decide whether the signal should be returned. 1573 * Return the signal's number, or fall through 1574 * to clear it from the pending mask. 1575 */ 1576 switch ((long)SIGACTION(p, signum).sa_handler) { 1577 1578 case (long)SIG_DFL: 1579 /* 1580 * Don't take default actions on system processes. 1581 */ 1582 if (p->p_pid <= 1) { 1583 #ifdef DIAGNOSTIC 1584 /* 1585 * Are you sure you want to ignore SIGSEGV 1586 * in init? XXX 1587 */ 1588 printf("Process (pid %d) got signal %d\n", 1589 p->p_pid, signum); 1590 #endif 1591 break; /* == ignore */ 1592 } 1593 /* 1594 * If there is a pending stop signal to process 1595 * with default action, stop here, 1596 * then clear the signal. However, 1597 * if process is member of an orphaned 1598 * process group, ignore tty stop signals. 1599 */ 1600 if (prop & SA_STOP) { 1601 if (p->p_flag & P_TRACED || 1602 (p->p_pgrp->pg_jobc == 0 && 1603 prop & SA_TTYSTOP)) 1604 break; /* == ignore */ 1605 p->p_xstat = signum; 1606 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 1607 child_psignal(p, dolock); 1608 if (dolock) 1609 SCHED_LOCK(s); 1610 proc_stop(p, 1); 1611 sigswitch: 1612 mi_switch(l, NULL); 1613 SCHED_ASSERT_UNLOCKED(); 1614 if (dolock) 1615 splx(s); 1616 else 1617 dolock = 1; 1618 break; 1619 } else if (prop & SA_IGNORE) { 1620 /* 1621 * Except for SIGCONT, shouldn't get here. 1622 * Default action is to ignore; drop it. 1623 */ 1624 break; /* == ignore */ 1625 } else 1626 goto keep; 1627 /*NOTREACHED*/ 1628 1629 case (long)SIG_IGN: 1630 /* 1631 * Masking above should prevent us ever trying 1632 * to take action on an ignored signal other 1633 * than SIGCONT, unless process is traced. 1634 */ 1635 #ifdef DEBUG_ISSIGNAL 1636 if ((prop & SA_CONT) == 0 && 1637 (p->p_flag & P_TRACED) == 0) 1638 printf("issignal\n"); 1639 #endif 1640 break; /* == ignore */ 1641 1642 default: 1643 /* 1644 * This signal has an action, let 1645 * postsig() process it. 1646 */ 1647 goto keep; 1648 } 1649 } 1650 /* NOTREACHED */ 1651 1652 keep: 1653 /* leave the signal for later */ 1654 sigaddset(&p->p_sigctx.ps_siglist, signum); 1655 CHECKSIGS(p); 1656 if (locked && dolock) 1657 SCHED_LOCK(s); 1658 return (signum); 1659 } 1660 1661 /* 1662 * Put the argument process into the stopped state and notify the parent 1663 * via wakeup. Signals are handled elsewhere. The process must not be 1664 * on the run queue. 1665 */ 1666 void 1667 proc_stop(struct proc *p, int dowakeup) 1668 { 1669 struct lwp *l; 1670 struct proc *parent; 1671 struct sadata_vp *vp; 1672 1673 SCHED_ASSERT_LOCKED(); 1674 1675 /* XXX lock process LWP state */ 1676 p->p_flag &= ~P_WAITED; 1677 p->p_stat = SSTOP; 1678 parent = p->p_pptr; 1679 parent->p_nstopchild++; 1680 1681 if (p->p_flag & P_SA) { 1682 /* 1683 * Only (try to) put the LWP on the VP in stopped 1684 * state. 1685 * All other LWPs will suspend in sa_setwoken() 1686 * because the VP-LWP in stopped state cannot be 1687 * repossessed. 1688 */ 1689 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1690 l = vp->savp_lwp; 1691 if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) { 1692 l->l_stat = LSSTOP; 1693 p->p_nrlwps--; 1694 } else if (l->l_stat == LSRUN) { 1695 /* Remove LWP from the run queue */ 1696 remrunqueue(l); 1697 l->l_stat = LSSTOP; 1698 p->p_nrlwps--; 1699 } else if (l->l_stat == LSSLEEP && 1700 l->l_flag & L_SA_IDLE) { 1701 l->l_flag &= ~L_SA_IDLE; 1702 l->l_stat = LSSTOP; 1703 } 1704 } 1705 goto out; 1706 } 1707 1708 /* 1709 * Put as many LWP's as possible in stopped state. 1710 * Sleeping ones will notice the stopped state as they try to 1711 * return to userspace. 1712 */ 1713 1714 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1715 if (l->l_stat == LSONPROC) { 1716 /* XXX SMP this assumes that a LWP that is LSONPROC 1717 * is curlwp and hence is about to be mi_switched 1718 * away; the only callers of proc_stop() are: 1719 * - psignal 1720 * - issignal() 1721 * For the former, proc_stop() is only called when 1722 * no processes are running, so we don't worry. 1723 * For the latter, proc_stop() is called right 1724 * before mi_switch(). 1725 */ 1726 l->l_stat = LSSTOP; 1727 p->p_nrlwps--; 1728 } else if (l->l_stat == LSRUN) { 1729 /* Remove LWP from the run queue */ 1730 remrunqueue(l); 1731 l->l_stat = LSSTOP; 1732 p->p_nrlwps--; 1733 } else if ((l->l_stat == LSSLEEP) || 1734 (l->l_stat == LSSUSPENDED) || 1735 (l->l_stat == LSZOMB) || 1736 (l->l_stat == LSDEAD)) { 1737 /* 1738 * Don't do anything; let sleeping LWPs 1739 * discover the stopped state of the process 1740 * on their way out of the kernel; otherwise, 1741 * things like NFS threads that sleep with 1742 * locks will block the rest of the system 1743 * from getting any work done. 1744 * 1745 * Suspended/dead/zombie LWPs aren't going 1746 * anywhere, so we don't need to touch them. 1747 */ 1748 } 1749 #ifdef DIAGNOSTIC 1750 else { 1751 panic("proc_stop: process %d lwp %d " 1752 "in unstoppable state %d.\n", 1753 p->p_pid, l->l_lid, l->l_stat); 1754 } 1755 #endif 1756 } 1757 1758 out: 1759 /* XXX unlock process LWP state */ 1760 1761 if (dowakeup) 1762 sched_wakeup((caddr_t)p->p_pptr); 1763 } 1764 1765 /* 1766 * Given a process in state SSTOP, set the state back to SACTIVE and 1767 * move LSSTOP'd LWPs to LSSLEEP or make them runnable. 1768 * 1769 * If no LWPs ended up runnable (and therefore able to take a signal), 1770 * return a LWP that is sleeping interruptably. The caller can wake 1771 * that LWP up to take a signal. 1772 */ 1773 struct lwp * 1774 proc_unstop(struct proc *p) 1775 { 1776 struct lwp *l, *lr = NULL; 1777 struct sadata_vp *vp; 1778 int cantake = 0; 1779 1780 SCHED_ASSERT_LOCKED(); 1781 1782 /* 1783 * Our caller wants to be informed if there are only sleeping 1784 * and interruptable LWPs left after we have run so that it 1785 * can invoke setrunnable() if required - return one of the 1786 * interruptable LWPs if this is the case. 1787 */ 1788 1789 if (!(p->p_flag & P_WAITED)) 1790 p->p_pptr->p_nstopchild--; 1791 p->p_stat = SACTIVE; 1792 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1793 if (l->l_stat == LSRUN) { 1794 lr = NULL; 1795 cantake = 1; 1796 } 1797 if (l->l_stat != LSSTOP) 1798 continue; 1799 1800 if (l->l_wchan != NULL) { 1801 l->l_stat = LSSLEEP; 1802 if ((cantake == 0) && (l->l_flag & L_SINTR)) { 1803 lr = l; 1804 cantake = 1; 1805 } 1806 } else { 1807 setrunnable(l); 1808 lr = NULL; 1809 cantake = 1; 1810 } 1811 } 1812 if (p->p_flag & P_SA) { 1813 /* Only consider returning the LWP on the VP. */ 1814 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1815 lr = vp->savp_lwp; 1816 if (lr->l_stat == LSSLEEP) { 1817 if (lr->l_flag & L_SA_YIELD) { 1818 setrunnable(lr); 1819 break; 1820 } else if (lr->l_flag & L_SINTR) 1821 return lr; 1822 } 1823 } 1824 return NULL; 1825 } 1826 return lr; 1827 } 1828 1829 /* 1830 * Take the action for the specified signal 1831 * from the current set of pending signals. 1832 */ 1833 void 1834 postsig(int signum) 1835 { 1836 struct lwp *l; 1837 struct proc *p; 1838 struct sigacts *ps; 1839 sig_t action; 1840 sigset_t *returnmask; 1841 1842 l = curlwp; 1843 p = l->l_proc; 1844 ps = p->p_sigacts; 1845 #ifdef DIAGNOSTIC 1846 if (signum == 0) 1847 panic("postsig"); 1848 #endif 1849 1850 KERNEL_PROC_LOCK(l); 1851 1852 #ifdef MULTIPROCESSOR 1853 /* 1854 * On MP, issignal() can return the same signal to multiple 1855 * LWPs. The LWPs will block above waiting for the kernel 1856 * lock and the first LWP which gets through will then remove 1857 * the signal from ps_siglist. All other LWPs exit here. 1858 */ 1859 if (!sigismember(&p->p_sigctx.ps_siglist, signum)) { 1860 KERNEL_PROC_UNLOCK(l); 1861 return; 1862 } 1863 #endif 1864 sigdelset(&p->p_sigctx.ps_siglist, signum); 1865 action = SIGACTION_PS(ps, signum).sa_handler; 1866 if (action == SIG_DFL) { 1867 #ifdef KTRACE 1868 if (KTRPOINT(p, KTR_PSIG)) 1869 ktrpsig(l, signum, action, 1870 p->p_sigctx.ps_flags & SAS_OLDMASK ? 1871 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 1872 NULL); 1873 #endif 1874 /* 1875 * Default action, where the default is to kill 1876 * the process. (Other cases were ignored above.) 1877 */ 1878 sigexit(l, signum); 1879 /* NOTREACHED */ 1880 } else { 1881 ksiginfo_t *ksi; 1882 /* 1883 * If we get here, the signal must be caught. 1884 */ 1885 #ifdef DIAGNOSTIC 1886 if (action == SIG_IGN || 1887 sigismember(&p->p_sigctx.ps_sigmask, signum)) 1888 panic("postsig action"); 1889 #endif 1890 /* 1891 * Set the new mask value and also defer further 1892 * occurrences of this signal. 1893 * 1894 * Special case: user has done a sigpause. Here the 1895 * current mask is not of interest, but rather the 1896 * mask from before the sigpause is what we want 1897 * restored after the signal processing is completed. 1898 */ 1899 if (p->p_sigctx.ps_flags & SAS_OLDMASK) { 1900 returnmask = &p->p_sigctx.ps_oldmask; 1901 p->p_sigctx.ps_flags &= ~SAS_OLDMASK; 1902 } else 1903 returnmask = &p->p_sigctx.ps_sigmask; 1904 p->p_stats->p_ru.ru_nsignals++; 1905 ksi = ksiginfo_get(p, signum); 1906 #ifdef KTRACE 1907 if (KTRPOINT(p, KTR_PSIG)) 1908 ktrpsig(l, signum, action, 1909 p->p_sigctx.ps_flags & SAS_OLDMASK ? 1910 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 1911 ksi); 1912 #endif 1913 if (ksi == NULL) { 1914 ksiginfo_t ksi1; 1915 /* 1916 * we did not save any siginfo for this, either 1917 * because the signal was not caught, or because the 1918 * user did not request SA_SIGINFO 1919 */ 1920 KSI_INIT_EMPTY(&ksi1); 1921 ksi1.ksi_signo = signum; 1922 kpsendsig(l, &ksi1, returnmask); 1923 } else { 1924 kpsendsig(l, ksi, returnmask); 1925 pool_put(&ksiginfo_pool, ksi); 1926 } 1927 p->p_sigctx.ps_lwp = 0; 1928 p->p_sigctx.ps_code = 0; 1929 p->p_sigctx.ps_signo = 0; 1930 (void) splsched(); /* XXXSMP */ 1931 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 1932 &p->p_sigctx.ps_sigmask); 1933 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 1934 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1935 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1936 sigaddset(&p->p_sigctx.ps_sigignore, signum); 1937 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 1938 } 1939 (void) spl0(); /* XXXSMP */ 1940 } 1941 1942 KERNEL_PROC_UNLOCK(l); 1943 } 1944 1945 /* 1946 * Kill the current process for stated reason. 1947 */ 1948 void 1949 killproc(struct proc *p, const char *why) 1950 { 1951 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1952 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1953 psignal(p, SIGKILL); 1954 } 1955 1956 /* 1957 * Force the current process to exit with the specified signal, dumping core 1958 * if appropriate. We bypass the normal tests for masked and caught signals, 1959 * allowing unrecoverable failures to terminate the process without changing 1960 * signal state. Mark the accounting record with the signal termination. 1961 * If dumping core, save the signal number for the debugger. Calls exit and 1962 * does not return. 1963 */ 1964 1965 #if defined(DEBUG) 1966 int kern_logsigexit = 1; /* not static to make public for sysctl */ 1967 #else 1968 int kern_logsigexit = 0; /* not static to make public for sysctl */ 1969 #endif 1970 1971 static const char logcoredump[] = 1972 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 1973 static const char lognocoredump[] = 1974 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 1975 1976 /* Wrapper function for use in p_userret */ 1977 static void 1978 lwp_coredump_hook(struct lwp *l, void *arg __unused) 1979 { 1980 int s; 1981 1982 /* 1983 * Suspend ourselves, so that the kernel stack and therefore 1984 * the userland registers saved in the trapframe are around 1985 * for coredump() to write them out. 1986 */ 1987 KERNEL_PROC_LOCK(l); 1988 l->l_flag &= ~L_DETACHED; 1989 SCHED_LOCK(s); 1990 l->l_stat = LSSUSPENDED; 1991 l->l_proc->p_nrlwps--; 1992 /* XXX NJWLWP check if this makes sense here: */ 1993 l->l_proc->p_stats->p_ru.ru_nvcsw++; 1994 mi_switch(l, NULL); 1995 SCHED_ASSERT_UNLOCKED(); 1996 splx(s); 1997 1998 lwp_exit(l); 1999 } 2000 2001 void 2002 sigexit(struct lwp *l, int signum) 2003 { 2004 struct proc *p; 2005 #if 0 2006 struct lwp *l2; 2007 #endif 2008 int exitsig; 2009 #ifdef COREDUMP 2010 int error; 2011 #endif 2012 2013 p = l->l_proc; 2014 2015 /* 2016 * Don't permit coredump() or exit1() multiple times 2017 * in the same process. 2018 */ 2019 if (p->p_flag & P_WEXIT) { 2020 KERNEL_PROC_UNLOCK(l); 2021 (*p->p_userret)(l, p->p_userret_arg); 2022 } 2023 p->p_flag |= P_WEXIT; 2024 /* We don't want to switch away from exiting. */ 2025 /* XXX multiprocessor: stop LWPs on other processors. */ 2026 #if 0 2027 if (p->p_flag & P_SA) { 2028 LIST_FOREACH(l2, &p->p_lwps, l_sibling) 2029 l2->l_flag &= ~L_SA; 2030 p->p_flag &= ~P_SA; 2031 } 2032 #endif 2033 2034 /* Make other LWPs stick around long enough to be dumped */ 2035 p->p_userret = lwp_coredump_hook; 2036 p->p_userret_arg = NULL; 2037 2038 exitsig = signum; 2039 p->p_acflag |= AXSIG; 2040 if (sigprop[signum] & SA_CORE) { 2041 p->p_sigctx.ps_signo = signum; 2042 #ifdef COREDUMP 2043 if ((error = coredump(l, NULL)) == 0) 2044 exitsig |= WCOREFLAG; 2045 #endif 2046 2047 if (kern_logsigexit) { 2048 /* XXX What if we ever have really large UIDs? */ 2049 int uid = l->l_cred ? 2050 (int)kauth_cred_geteuid(l->l_cred) : -1; 2051 2052 #ifdef COREDUMP 2053 if (error) 2054 log(LOG_INFO, lognocoredump, p->p_pid, 2055 p->p_comm, uid, signum, error); 2056 else 2057 #endif 2058 log(LOG_INFO, logcoredump, p->p_pid, 2059 p->p_comm, uid, signum); 2060 } 2061 2062 } 2063 2064 exit1(l, W_EXITCODE(0, exitsig)); 2065 /* NOTREACHED */ 2066 } 2067 2068 #ifdef COREDUMP 2069 struct coredump_iostate { 2070 struct lwp *io_lwp; 2071 struct vnode *io_vp; 2072 kauth_cred_t io_cred; 2073 off_t io_offset; 2074 }; 2075 2076 int 2077 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 2078 { 2079 struct coredump_iostate *io = cookie; 2080 int error; 2081 2082 error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len, 2083 io->io_offset, segflg, 2084 IO_NODELOCKED|IO_UNIT, io->io_cred, NULL, 2085 segflg == UIO_USERSPACE ? io->io_lwp : NULL); 2086 if (error) { 2087 printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n", 2088 io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm, 2089 segflg == UIO_USERSPACE ? "user" : "system", 2090 len, data, (long long) io->io_offset, error); 2091 return (error); 2092 } 2093 2094 io->io_offset += len; 2095 return (0); 2096 } 2097 2098 /* 2099 * Dump core, into a file named "progname.core" or "core" (depending on the 2100 * value of shortcorename), unless the process was setuid/setgid. 2101 */ 2102 int 2103 coredump(struct lwp *l, const char *pattern) 2104 { 2105 struct vnode *vp; 2106 struct proc *p; 2107 struct vmspace *vm; 2108 kauth_cred_t cred; 2109 struct nameidata nd; 2110 struct vattr vattr; 2111 struct mount *mp; 2112 struct coredump_iostate io; 2113 int error, error1; 2114 char *name = NULL; 2115 2116 p = l->l_proc; 2117 vm = p->p_vmspace; 2118 cred = l->l_cred; 2119 2120 /* 2121 * Make sure the process has not set-id, to prevent data leaks, 2122 * unless it was specifically requested to allow set-id coredumps. 2123 */ 2124 if ((p->p_flag & P_SUGID) && !security_setidcore_dump) 2125 return EPERM; 2126 2127 /* 2128 * Refuse to core if the data + stack + user size is larger than 2129 * the core dump limit. XXX THIS IS WRONG, because of mapped 2130 * data. 2131 */ 2132 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >= 2133 p->p_rlimit[RLIMIT_CORE].rlim_cur) 2134 return EFBIG; /* better error code? */ 2135 2136 restart: 2137 /* 2138 * The core dump will go in the current working directory. Make 2139 * sure that the directory is still there and that the mount flags 2140 * allow us to write core dumps there. 2141 */ 2142 vp = p->p_cwdi->cwdi_cdir; 2143 if (vp->v_mount == NULL || 2144 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) { 2145 error = EPERM; 2146 goto done; 2147 } 2148 2149 if ((p->p_flag & P_SUGID) && security_setidcore_dump) 2150 pattern = security_setidcore_path; 2151 2152 if (pattern == NULL) 2153 pattern = p->p_limit->pl_corename; 2154 if (name == NULL) { 2155 name = PNBUF_GET(); 2156 } 2157 if ((error = build_corename(p, name, pattern, MAXPATHLEN)) != 0) 2158 goto done; 2159 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l); 2160 if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, 2161 S_IRUSR | S_IWUSR)) != 0) 2162 goto done; 2163 vp = nd.ni_vp; 2164 2165 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2166 VOP_UNLOCK(vp, 0); 2167 if ((error = vn_close(vp, FWRITE, cred, l)) != 0) 2168 goto done; 2169 if ((error = vn_start_write(NULL, &mp, 2170 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 2171 goto done; 2172 goto restart; 2173 } 2174 2175 /* Don't dump to non-regular files or files with links. */ 2176 if (vp->v_type != VREG || 2177 VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) { 2178 error = EINVAL; 2179 goto out; 2180 } 2181 VATTR_NULL(&vattr); 2182 vattr.va_size = 0; 2183 2184 if ((p->p_flag & P_SUGID) && security_setidcore_dump) { 2185 vattr.va_uid = security_setidcore_owner; 2186 vattr.va_gid = security_setidcore_group; 2187 vattr.va_mode = security_setidcore_mode; 2188 } 2189 2190 VOP_LEASE(vp, l, cred, LEASE_WRITE); 2191 VOP_SETATTR(vp, &vattr, cred, l); 2192 p->p_acflag |= ACORE; 2193 2194 io.io_lwp = l; 2195 io.io_vp = vp; 2196 io.io_cred = cred; 2197 io.io_offset = 0; 2198 2199 /* Now dump the actual core file. */ 2200 error = (*p->p_execsw->es_coredump)(l, &io); 2201 out: 2202 VOP_UNLOCK(vp, 0); 2203 vn_finished_write(mp, 0); 2204 error1 = vn_close(vp, FWRITE, cred, l); 2205 if (error == 0) 2206 error = error1; 2207 done: 2208 if (name != NULL) 2209 PNBUF_PUT(name); 2210 return error; 2211 } 2212 #endif /* COREDUMP */ 2213 2214 /* 2215 * Nonexistent system call-- signal process (may want to handle it). 2216 * Flag error in case process won't see signal immediately (blocked or ignored). 2217 */ 2218 #ifndef PTRACE 2219 __weak_alias(sys_ptrace, sys_nosys); 2220 #endif 2221 2222 /* ARGSUSED */ 2223 int 2224 sys_nosys(struct lwp *l, void *v __unused, register_t *retval __unused) 2225 { 2226 struct proc *p; 2227 2228 p = l->l_proc; 2229 psignal(p, SIGSYS); 2230 return (ENOSYS); 2231 } 2232 2233 #ifdef COREDUMP 2234 static int 2235 build_corename(struct proc *p, char *dst, const char *src, size_t len) 2236 { 2237 const char *s; 2238 char *d, *end; 2239 int i; 2240 2241 for (s = src, d = dst, end = d + len; *s != '\0'; s++) { 2242 if (*s == '%') { 2243 switch (*(s + 1)) { 2244 case 'n': 2245 i = snprintf(d, end - d, "%s", p->p_comm); 2246 break; 2247 case 'p': 2248 i = snprintf(d, end - d, "%d", p->p_pid); 2249 break; 2250 case 'u': 2251 i = snprintf(d, end - d, "%.*s", 2252 (int)sizeof p->p_pgrp->pg_session->s_login, 2253 p->p_pgrp->pg_session->s_login); 2254 break; 2255 case 't': 2256 i = snprintf(d, end - d, "%ld", 2257 p->p_stats->p_start.tv_sec); 2258 break; 2259 default: 2260 goto copy; 2261 } 2262 d += i; 2263 s++; 2264 } else { 2265 copy: *d = *s; 2266 d++; 2267 } 2268 if (d >= end) 2269 return (ENAMETOOLONG); 2270 } 2271 *d = '\0'; 2272 return 0; 2273 } 2274 #endif /* COREDUMP */ 2275 2276 void 2277 getucontext(struct lwp *l, ucontext_t *ucp) 2278 { 2279 struct proc *p; 2280 2281 p = l->l_proc; 2282 2283 ucp->uc_flags = 0; 2284 ucp->uc_link = l->l_ctxlink; 2285 2286 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask); 2287 ucp->uc_flags |= _UC_SIGMASK; 2288 2289 /* 2290 * The (unsupplied) definition of the `current execution stack' 2291 * in the System V Interface Definition appears to allow returning 2292 * the main context stack. 2293 */ 2294 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) { 2295 ucp->uc_stack.ss_sp = (void *)USRSTACK; 2296 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize); 2297 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 2298 } else { 2299 /* Simply copy alternate signal execution stack. */ 2300 ucp->uc_stack = p->p_sigctx.ps_sigstk; 2301 } 2302 ucp->uc_flags |= _UC_STACK; 2303 2304 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 2305 } 2306 2307 /* ARGSUSED */ 2308 int 2309 sys_getcontext(struct lwp *l, void *v, register_t *retval __unused) 2310 { 2311 struct sys_getcontext_args /* { 2312 syscallarg(struct __ucontext *) ucp; 2313 } */ *uap = v; 2314 ucontext_t uc; 2315 2316 getucontext(l, &uc); 2317 2318 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)))); 2319 } 2320 2321 int 2322 setucontext(struct lwp *l, const ucontext_t *ucp) 2323 { 2324 struct proc *p; 2325 int error; 2326 2327 p = l->l_proc; 2328 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0) 2329 return (error); 2330 l->l_ctxlink = ucp->uc_link; 2331 2332 if ((ucp->uc_flags & _UC_SIGMASK) != 0) 2333 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL); 2334 2335 /* 2336 * If there was stack information, update whether or not we are 2337 * still running on an alternate signal stack. 2338 */ 2339 if ((ucp->uc_flags & _UC_STACK) != 0) { 2340 if (ucp->uc_stack.ss_flags & SS_ONSTACK) 2341 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 2342 else 2343 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK; 2344 } 2345 2346 return 0; 2347 } 2348 2349 /* ARGSUSED */ 2350 int 2351 sys_setcontext(struct lwp *l, void *v, register_t *retval __unused) 2352 { 2353 struct sys_setcontext_args /* { 2354 syscallarg(const ucontext_t *) ucp; 2355 } */ *uap = v; 2356 ucontext_t uc; 2357 int error; 2358 2359 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */ 2360 exit1(l, W_EXITCODE(0, 0)); 2361 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 || 2362 (error = setucontext(l, &uc)) != 0) 2363 return (error); 2364 2365 return (EJUSTRETURN); 2366 } 2367 2368 /* 2369 * sigtimedwait(2) system call, used also for implementation 2370 * of sigwaitinfo() and sigwait(). 2371 * 2372 * This only handles single LWP in signal wait. libpthread provides 2373 * it's own sigtimedwait() wrapper to DTRT WRT individual threads. 2374 */ 2375 int 2376 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval) 2377 { 2378 return __sigtimedwait1(l, v, retval, copyout, copyin, copyout); 2379 } 2380 2381 int 2382 __sigtimedwait1(struct lwp *l, void *v, register_t *retval __unused, 2383 copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout) 2384 { 2385 struct sys___sigtimedwait_args /* { 2386 syscallarg(const sigset_t *) set; 2387 syscallarg(siginfo_t *) info; 2388 syscallarg(struct timespec *) timeout; 2389 } */ *uap = v; 2390 sigset_t *waitset, twaitset; 2391 struct proc *p = l->l_proc; 2392 int error, signum; 2393 int timo = 0; 2394 struct timespec ts, tsstart; 2395 ksiginfo_t *ksi; 2396 2397 memset(&tsstart, 0, sizeof tsstart); /* XXX gcc */ 2398 2399 MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK); 2400 2401 if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) { 2402 FREE(waitset, M_TEMP); 2403 return (error); 2404 } 2405 2406 /* 2407 * Silently ignore SA_CANTMASK signals. psignal1() would 2408 * ignore SA_CANTMASK signals in waitset, we do this 2409 * only for the below siglist check. 2410 */ 2411 sigminusset(&sigcantmask, waitset); 2412 2413 /* 2414 * First scan siglist and check if there is signal from 2415 * our waitset already pending. 2416 */ 2417 twaitset = *waitset; 2418 __sigandset(&p->p_sigctx.ps_siglist, &twaitset); 2419 if ((signum = firstsig(&twaitset))) { 2420 /* found pending signal */ 2421 sigdelset(&p->p_sigctx.ps_siglist, signum); 2422 ksi = ksiginfo_get(p, signum); 2423 if (!ksi) { 2424 /* No queued siginfo, manufacture one */ 2425 ksi = pool_get(&ksiginfo_pool, PR_WAITOK); 2426 KSI_INIT(ksi); 2427 ksi->ksi_info._signo = signum; 2428 ksi->ksi_info._code = SI_USER; 2429 } 2430 2431 goto sig; 2432 } 2433 2434 /* 2435 * Calculate timeout, if it was specified. 2436 */ 2437 if (SCARG(uap, timeout)) { 2438 uint64_t ms; 2439 2440 if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts)))) 2441 return (error); 2442 2443 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000); 2444 timo = mstohz(ms); 2445 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0) 2446 timo = 1; 2447 if (timo <= 0) 2448 return (EAGAIN); 2449 2450 /* 2451 * Remember current uptime, it would be used in 2452 * ECANCELED/ERESTART case. 2453 */ 2454 getnanouptime(&tsstart); 2455 } 2456 2457 /* 2458 * Setup ps_sigwait list. Pass pointer to malloced memory 2459 * here; it's not possible to pass pointer to a structure 2460 * on current process's stack, the current process might 2461 * be swapped out at the time the signal would get delivered. 2462 */ 2463 ksi = pool_get(&ksiginfo_pool, PR_WAITOK); 2464 p->p_sigctx.ps_sigwaited = ksi; 2465 p->p_sigctx.ps_sigwait = waitset; 2466 2467 /* 2468 * Wait for signal to arrive. We can either be woken up or 2469 * time out. 2470 */ 2471 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo); 2472 2473 /* 2474 * Need to find out if we woke as a result of lwp_wakeup() 2475 * or a signal outside our wait set. 2476 */ 2477 if (error == EINTR && p->p_sigctx.ps_sigwaited 2478 && !firstsig(&p->p_sigctx.ps_siglist)) { 2479 /* wakeup via _lwp_wakeup() */ 2480 error = ECANCELED; 2481 } else if (!error && p->p_sigctx.ps_sigwaited) { 2482 /* spurious wakeup - arrange for syscall restart */ 2483 error = ERESTART; 2484 goto fail; 2485 } 2486 2487 /* 2488 * On error, clear sigwait indication. psignal1() clears it 2489 * in !error case. 2490 */ 2491 if (error) { 2492 p->p_sigctx.ps_sigwaited = NULL; 2493 2494 /* 2495 * If the sleep was interrupted (either by signal or wakeup), 2496 * update the timeout and copyout new value back. 2497 * It would be used when the syscall would be restarted 2498 * or called again. 2499 */ 2500 if (timo && (error == ERESTART || error == ECANCELED)) { 2501 struct timespec tsnow; 2502 int err; 2503 2504 /* XXX double check the following change */ 2505 getnanouptime(&tsnow); 2506 2507 /* compute how much time has passed since start */ 2508 timespecsub(&tsnow, &tsstart, &tsnow); 2509 /* substract passed time from timeout */ 2510 timespecsub(&ts, &tsnow, &ts); 2511 2512 if (ts.tv_sec < 0) { 2513 error = EAGAIN; 2514 goto fail; 2515 } 2516 /* XXX double check the previous change */ 2517 2518 /* copy updated timeout to userland */ 2519 if ((err = (*put_timeout)(&ts, SCARG(uap, timeout), 2520 sizeof(ts)))) { 2521 error = err; 2522 goto fail; 2523 } 2524 } 2525 2526 goto fail; 2527 } 2528 2529 /* 2530 * If a signal from the wait set arrived, copy it to userland. 2531 * Copy only the used part of siginfo, the padding part is 2532 * left unchanged (userland is not supposed to touch it anyway). 2533 */ 2534 sig: 2535 return (*put_info)(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info)); 2536 2537 fail: 2538 FREE(waitset, M_TEMP); 2539 pool_put(&ksiginfo_pool, ksi); 2540 p->p_sigctx.ps_sigwait = NULL; 2541 2542 return (error); 2543 } 2544 2545 /* 2546 * Returns true if signal is ignored or masked for passed process. 2547 */ 2548 int 2549 sigismasked(struct proc *p, int sig) 2550 { 2551 2552 return (sigismember(&p->p_sigctx.ps_sigignore, sig) || 2553 sigismember(&p->p_sigctx.ps_sigmask, sig)); 2554 } 2555 2556 static int 2557 filt_sigattach(struct knote *kn) 2558 { 2559 struct proc *p = curproc; 2560 2561 kn->kn_ptr.p_proc = p; 2562 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2563 2564 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2565 2566 return (0); 2567 } 2568 2569 static void 2570 filt_sigdetach(struct knote *kn) 2571 { 2572 struct proc *p = kn->kn_ptr.p_proc; 2573 2574 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2575 } 2576 2577 /* 2578 * signal knotes are shared with proc knotes, so we apply a mask to 2579 * the hint in order to differentiate them from process hints. This 2580 * could be avoided by using a signal-specific knote list, but probably 2581 * isn't worth the trouble. 2582 */ 2583 static int 2584 filt_signal(struct knote *kn, long hint) 2585 { 2586 2587 if (hint & NOTE_SIGNAL) { 2588 hint &= ~NOTE_SIGNAL; 2589 2590 if (kn->kn_id == hint) 2591 kn->kn_data++; 2592 } 2593 return (kn->kn_data != 0); 2594 } 2595 2596 const struct filterops sig_filtops = { 2597 0, filt_sigattach, filt_sigdetach, filt_signal 2598 }; 2599