1 /* $NetBSD: kern_sig.c,v 1.140 2003/04/23 21:32:10 nathanw 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. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.140 2003/04/23 21:32:10 nathanw Exp $"); 45 46 #include "opt_ktrace.h" 47 #include "opt_compat_sunos.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 77 #include <sys/mount.h> 78 #include <sys/syscallargs.h> 79 80 #include <machine/cpu.h> 81 82 #include <sys/user.h> /* for coredump */ 83 84 #include <uvm/uvm_extern.h> 85 86 static void proc_stop(struct proc *p); 87 static int build_corename(struct proc *, char [MAXPATHLEN]); 88 sigset_t contsigmask, stopsigmask, sigcantmask; 89 90 struct pool sigacts_pool; /* memory pool for sigacts structures */ 91 struct pool siginfo_pool; /* memory pool for siginfo structures */ 92 93 /* 94 * Can process p, with pcred pc, send the signal signum to process q? 95 */ 96 #define CANSIGNAL(p, pc, q, signum) \ 97 ((pc)->pc_ucred->cr_uid == 0 || \ 98 (pc)->p_ruid == (q)->p_cred->p_ruid || \ 99 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \ 100 (pc)->p_ruid == (q)->p_ucred->cr_uid || \ 101 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \ 102 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)) 103 104 /* 105 * Initialize signal-related data structures. 106 */ 107 void 108 signal_init(void) 109 { 110 111 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl", 112 &pool_allocator_nointr); 113 pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo", 114 &pool_allocator_nointr); 115 } 116 117 /* 118 * Create an initial sigctx structure, using the same signal state 119 * as p. If 'share' is set, share the sigctx_proc part, otherwise just 120 * copy it from parent. 121 */ 122 void 123 sigactsinit(struct proc *np, struct proc *pp, int share) 124 { 125 struct sigacts *ps; 126 127 if (share) { 128 np->p_sigacts = pp->p_sigacts; 129 pp->p_sigacts->sa_refcnt++; 130 } else { 131 ps = pool_get(&sigacts_pool, PR_WAITOK); 132 if (pp) 133 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts)); 134 else 135 memset(ps, '\0', sizeof(struct sigacts)); 136 ps->sa_refcnt = 1; 137 np->p_sigacts = ps; 138 } 139 } 140 141 /* 142 * Make this process not share its sigctx, maintaining all 143 * signal state. 144 */ 145 void 146 sigactsunshare(struct proc *p) 147 { 148 struct sigacts *oldps; 149 150 if (p->p_sigacts->sa_refcnt == 1) 151 return; 152 153 oldps = p->p_sigacts; 154 sigactsinit(p, NULL, 0); 155 156 if (--oldps->sa_refcnt == 0) 157 pool_put(&sigacts_pool, oldps); 158 } 159 160 /* 161 * Release a sigctx structure. 162 */ 163 void 164 sigactsfree(struct proc *p) 165 { 166 struct sigacts *ps; 167 168 ps = p->p_sigacts; 169 if (--ps->sa_refcnt > 0) 170 return; 171 172 pool_put(&sigacts_pool, ps); 173 } 174 175 int 176 sigaction1(struct proc *p, int signum, const struct sigaction *nsa, 177 struct sigaction *osa, void *tramp, int vers) 178 { 179 struct sigacts *ps; 180 int prop; 181 182 ps = p->p_sigacts; 183 if (signum <= 0 || signum >= NSIG) 184 return (EINVAL); 185 186 /* 187 * Trampoline ABI version 0 is reserved for the legacy 188 * kernel-provided on-stack trampoline. Conversely, if 189 * we are using a non-0 ABI version, we must have a 190 * trampoline. 191 */ 192 if ((vers != 0 && tramp == NULL) || 193 (vers == 0 && tramp != NULL)) 194 return (EINVAL); 195 196 if (osa) 197 *osa = SIGACTION_PS(ps, signum); 198 199 if (nsa) { 200 if (nsa->sa_flags & ~SA_ALLBITS) 201 return (EINVAL); 202 203 prop = sigprop[signum]; 204 if (prop & SA_CANTMASK) 205 return (EINVAL); 206 207 (void) splsched(); /* XXXSMP */ 208 SIGACTION_PS(ps, signum) = *nsa; 209 ps->sa_sigdesc[signum].sd_tramp = tramp; 210 ps->sa_sigdesc[signum].sd_vers = vers; 211 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask); 212 if ((prop & SA_NORESET) != 0) 213 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND; 214 if (signum == SIGCHLD) { 215 if (nsa->sa_flags & SA_NOCLDSTOP) 216 p->p_flag |= P_NOCLDSTOP; 217 else 218 p->p_flag &= ~P_NOCLDSTOP; 219 if (nsa->sa_flags & SA_NOCLDWAIT) { 220 /* 221 * Paranoia: since SA_NOCLDWAIT is implemented 222 * by reparenting the dying child to PID 1 (and 223 * trust it to reap the zombie), PID 1 itself 224 * is forbidden to set SA_NOCLDWAIT. 225 */ 226 if (p->p_pid == 1) 227 p->p_flag &= ~P_NOCLDWAIT; 228 else 229 p->p_flag |= P_NOCLDWAIT; 230 } else 231 p->p_flag &= ~P_NOCLDWAIT; 232 } 233 if ((nsa->sa_flags & SA_NODEFER) == 0) 234 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum); 235 else 236 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum); 237 /* 238 * Set bit in p_sigctx.ps_sigignore for signals that are set to 239 * SIG_IGN, and for signals set to SIG_DFL where the default is 240 * to ignore. However, don't put SIGCONT in 241 * p_sigctx.ps_sigignore, as we have to restart the process. 242 */ 243 if (nsa->sa_handler == SIG_IGN || 244 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) { 245 /* never to be seen again */ 246 sigdelset(&p->p_sigctx.ps_siglist, signum); 247 if (signum != SIGCONT) { 248 /* easier in psignal */ 249 sigaddset(&p->p_sigctx.ps_sigignore, signum); 250 } 251 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 252 } else { 253 sigdelset(&p->p_sigctx.ps_sigignore, signum); 254 if (nsa->sa_handler == SIG_DFL) 255 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 256 else 257 sigaddset(&p->p_sigctx.ps_sigcatch, signum); 258 } 259 (void) spl0(); 260 } 261 262 return (0); 263 } 264 265 /* ARGSUSED */ 266 int 267 sys___sigaction14(struct lwp *l, void *v, register_t *retval) 268 { 269 struct sys___sigaction14_args /* { 270 syscallarg(int) signum; 271 syscallarg(const struct sigaction *) nsa; 272 syscallarg(struct sigaction *) osa; 273 } */ *uap = v; 274 struct proc *p; 275 struct sigaction nsa, osa; 276 int error; 277 278 if (SCARG(uap, nsa)) { 279 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 280 if (error) 281 return (error); 282 } 283 p = l->l_proc; 284 error = sigaction1(p, SCARG(uap, signum), 285 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 286 NULL, 0); 287 if (error) 288 return (error); 289 if (SCARG(uap, osa)) { 290 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 291 if (error) 292 return (error); 293 } 294 return (0); 295 } 296 297 /* ARGSUSED */ 298 int 299 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval) 300 { 301 struct sys___sigaction_sigtramp_args /* { 302 syscallarg(int) signum; 303 syscallarg(const struct sigaction *) nsa; 304 syscallarg(struct sigaction *) osa; 305 syscallarg(void *) tramp; 306 syscallarg(int) vers; 307 } */ *uap = v; 308 struct proc *p = l->l_proc; 309 struct sigaction nsa, osa; 310 int error; 311 312 if (SCARG(uap, nsa)) { 313 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 314 if (error) 315 return (error); 316 } 317 error = sigaction1(p, SCARG(uap, signum), 318 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 319 SCARG(uap, tramp), SCARG(uap, vers)); 320 if (error) 321 return (error); 322 if (SCARG(uap, osa)) { 323 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 324 if (error) 325 return (error); 326 } 327 return (0); 328 } 329 330 /* 331 * Initialize signal state for process 0; 332 * set to ignore signals that are ignored by default and disable the signal 333 * stack. 334 */ 335 void 336 siginit(struct proc *p) 337 { 338 struct sigacts *ps; 339 int signum, prop; 340 341 ps = p->p_sigacts; 342 sigemptyset(&contsigmask); 343 sigemptyset(&stopsigmask); 344 sigemptyset(&sigcantmask); 345 for (signum = 1; signum < NSIG; signum++) { 346 prop = sigprop[signum]; 347 if (prop & SA_CONT) 348 sigaddset(&contsigmask, signum); 349 if (prop & SA_STOP) 350 sigaddset(&stopsigmask, signum); 351 if (prop & SA_CANTMASK) 352 sigaddset(&sigcantmask, signum); 353 if (prop & SA_IGNORE && signum != SIGCONT) 354 sigaddset(&p->p_sigctx.ps_sigignore, signum); 355 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 356 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 357 } 358 sigemptyset(&p->p_sigctx.ps_sigcatch); 359 p->p_sigctx.ps_sigwaited = 0; 360 p->p_flag &= ~P_NOCLDSTOP; 361 362 /* 363 * Reset stack state to the user stack. 364 */ 365 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 366 p->p_sigctx.ps_sigstk.ss_size = 0; 367 p->p_sigctx.ps_sigstk.ss_sp = 0; 368 369 /* One reference. */ 370 ps->sa_refcnt = 1; 371 } 372 373 /* 374 * Reset signals for an exec of the specified process. 375 */ 376 void 377 execsigs(struct proc *p) 378 { 379 struct sigacts *ps; 380 int signum, prop; 381 382 sigactsunshare(p); 383 384 ps = p->p_sigacts; 385 386 /* 387 * Reset caught signals. Held signals remain held 388 * through p_sigctx.ps_sigmask (unless they were caught, 389 * and are now ignored by default). 390 */ 391 for (signum = 1; signum < NSIG; signum++) { 392 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 393 prop = sigprop[signum]; 394 if (prop & SA_IGNORE) { 395 if ((prop & SA_CONT) == 0) 396 sigaddset(&p->p_sigctx.ps_sigignore, 397 signum); 398 sigdelset(&p->p_sigctx.ps_siglist, signum); 399 } 400 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 401 } 402 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 403 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 404 } 405 sigemptyset(&p->p_sigctx.ps_sigcatch); 406 p->p_sigctx.ps_sigwaited = 0; 407 p->p_flag &= ~P_NOCLDSTOP; 408 409 /* 410 * Reset stack state to the user stack. 411 */ 412 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 413 p->p_sigctx.ps_sigstk.ss_size = 0; 414 p->p_sigctx.ps_sigstk.ss_sp = 0; 415 } 416 417 int 418 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss) 419 { 420 421 if (oss) 422 *oss = p->p_sigctx.ps_sigmask; 423 424 if (nss) { 425 (void)splsched(); /* XXXSMP */ 426 switch (how) { 427 case SIG_BLOCK: 428 sigplusset(nss, &p->p_sigctx.ps_sigmask); 429 break; 430 case SIG_UNBLOCK: 431 sigminusset(nss, &p->p_sigctx.ps_sigmask); 432 CHECKSIGS(p); 433 break; 434 case SIG_SETMASK: 435 p->p_sigctx.ps_sigmask = *nss; 436 CHECKSIGS(p); 437 break; 438 default: 439 (void)spl0(); /* XXXSMP */ 440 return (EINVAL); 441 } 442 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 443 (void)spl0(); /* XXXSMP */ 444 } 445 446 return (0); 447 } 448 449 /* 450 * Manipulate signal mask. 451 * Note that we receive new mask, not pointer, 452 * and return old mask as return value; 453 * the library stub does the rest. 454 */ 455 int 456 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval) 457 { 458 struct sys___sigprocmask14_args /* { 459 syscallarg(int) how; 460 syscallarg(const sigset_t *) set; 461 syscallarg(sigset_t *) oset; 462 } */ *uap = v; 463 struct proc *p; 464 sigset_t nss, oss; 465 int error; 466 467 if (SCARG(uap, set)) { 468 error = copyin(SCARG(uap, set), &nss, sizeof(nss)); 469 if (error) 470 return (error); 471 } 472 p = l->l_proc; 473 error = sigprocmask1(p, SCARG(uap, how), 474 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0); 475 if (error) 476 return (error); 477 if (SCARG(uap, oset)) { 478 error = copyout(&oss, SCARG(uap, oset), sizeof(oss)); 479 if (error) 480 return (error); 481 } 482 return (0); 483 } 484 485 void 486 sigpending1(struct proc *p, sigset_t *ss) 487 { 488 489 *ss = p->p_sigctx.ps_siglist; 490 sigminusset(&p->p_sigctx.ps_sigmask, ss); 491 } 492 493 /* ARGSUSED */ 494 int 495 sys___sigpending14(struct lwp *l, void *v, register_t *retval) 496 { 497 struct sys___sigpending14_args /* { 498 syscallarg(sigset_t *) set; 499 } */ *uap = v; 500 struct proc *p; 501 sigset_t ss; 502 503 p = l->l_proc; 504 sigpending1(p, &ss); 505 return (copyout(&ss, SCARG(uap, set), sizeof(ss))); 506 } 507 508 int 509 sigsuspend1(struct proc *p, const sigset_t *ss) 510 { 511 struct sigacts *ps; 512 513 ps = p->p_sigacts; 514 if (ss) { 515 /* 516 * When returning from sigpause, we want 517 * the old mask to be restored after the 518 * signal handler has finished. Thus, we 519 * save it here and mark the sigctx structure 520 * to indicate this. 521 */ 522 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask; 523 p->p_sigctx.ps_flags |= SAS_OLDMASK; 524 (void) splsched(); /* XXXSMP */ 525 p->p_sigctx.ps_sigmask = *ss; 526 CHECKSIGS(p); 527 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 528 (void) spl0(); /* XXXSMP */ 529 } 530 531 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 532 /* void */; 533 /* always return EINTR rather than ERESTART... */ 534 return (EINTR); 535 } 536 537 /* 538 * Suspend process until signal, providing mask to be set 539 * in the meantime. Note nonstandard calling convention: 540 * libc stub passes mask, not pointer, to save a copyin. 541 */ 542 /* ARGSUSED */ 543 int 544 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval) 545 { 546 struct sys___sigsuspend14_args /* { 547 syscallarg(const sigset_t *) set; 548 } */ *uap = v; 549 struct proc *p; 550 sigset_t ss; 551 int error; 552 553 if (SCARG(uap, set)) { 554 error = copyin(SCARG(uap, set), &ss, sizeof(ss)); 555 if (error) 556 return (error); 557 } 558 559 p = l->l_proc; 560 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0)); 561 } 562 563 int 564 sigaltstack1(struct proc *p, const struct sigaltstack *nss, 565 struct sigaltstack *oss) 566 { 567 568 if (oss) 569 *oss = p->p_sigctx.ps_sigstk; 570 571 if (nss) { 572 if (nss->ss_flags & ~SS_ALLBITS) 573 return (EINVAL); 574 575 if (nss->ss_flags & SS_DISABLE) { 576 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) 577 return (EINVAL); 578 } else { 579 if (nss->ss_size < MINSIGSTKSZ) 580 return (ENOMEM); 581 } 582 p->p_sigctx.ps_sigstk = *nss; 583 } 584 585 return (0); 586 } 587 588 /* ARGSUSED */ 589 int 590 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval) 591 { 592 struct sys___sigaltstack14_args /* { 593 syscallarg(const struct sigaltstack *) nss; 594 syscallarg(struct sigaltstack *) oss; 595 } */ *uap = v; 596 struct proc *p; 597 struct sigaltstack nss, oss; 598 int error; 599 600 if (SCARG(uap, nss)) { 601 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 602 if (error) 603 return (error); 604 } 605 p = l->l_proc; 606 error = sigaltstack1(p, 607 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0); 608 if (error) 609 return (error); 610 if (SCARG(uap, oss)) { 611 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 612 if (error) 613 return (error); 614 } 615 return (0); 616 } 617 618 /* ARGSUSED */ 619 int 620 sys_kill(struct lwp *l, void *v, register_t *retval) 621 { 622 struct sys_kill_args /* { 623 syscallarg(int) pid; 624 syscallarg(int) signum; 625 } */ *uap = v; 626 struct proc *cp, *p; 627 struct pcred *pc; 628 629 cp = l->l_proc; 630 pc = cp->p_cred; 631 if ((u_int)SCARG(uap, signum) >= NSIG) 632 return (EINVAL); 633 if (SCARG(uap, pid) > 0) { 634 /* kill single process */ 635 if ((p = pfind(SCARG(uap, pid))) == NULL) 636 return (ESRCH); 637 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum))) 638 return (EPERM); 639 if (SCARG(uap, signum)) 640 psignal(p, SCARG(uap, signum)); 641 return (0); 642 } 643 switch (SCARG(uap, pid)) { 644 case -1: /* broadcast signal */ 645 return (killpg1(cp, SCARG(uap, signum), 0, 1)); 646 case 0: /* signal own process group */ 647 return (killpg1(cp, SCARG(uap, signum), 0, 0)); 648 default: /* negative explicit process group */ 649 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0)); 650 } 651 /* NOTREACHED */ 652 } 653 654 /* 655 * Common code for kill process group/broadcast kill. 656 * cp is calling process. 657 */ 658 int 659 killpg1(struct proc *cp, int signum, int pgid, int all) 660 { 661 struct proc *p; 662 struct pcred *pc; 663 struct pgrp *pgrp; 664 int nfound; 665 666 pc = cp->p_cred; 667 nfound = 0; 668 if (all) { 669 /* 670 * broadcast 671 */ 672 proclist_lock_read(); 673 LIST_FOREACH(p, &allproc, p_list) { 674 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 675 p == cp || !CANSIGNAL(cp, pc, p, signum)) 676 continue; 677 nfound++; 678 if (signum) 679 psignal(p, signum); 680 } 681 proclist_unlock_read(); 682 } else { 683 if (pgid == 0) 684 /* 685 * zero pgid means send to my process group. 686 */ 687 pgrp = cp->p_pgrp; 688 else { 689 pgrp = pgfind(pgid); 690 if (pgrp == NULL) 691 return (ESRCH); 692 } 693 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 694 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 695 !CANSIGNAL(cp, pc, p, signum)) 696 continue; 697 nfound++; 698 if (signum && P_ZOMBIE(p) == 0) 699 psignal(p, signum); 700 } 701 } 702 return (nfound ? 0 : ESRCH); 703 } 704 705 /* 706 * Send a signal to a process group. 707 */ 708 void 709 gsignal(int pgid, int signum) 710 { 711 struct pgrp *pgrp; 712 713 if (pgid && (pgrp = pgfind(pgid))) 714 pgsignal(pgrp, signum, 0); 715 } 716 717 /* 718 * Send a signal to a process group. If checktty is 1, 719 * limit to members which have a controlling terminal. 720 */ 721 void 722 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 723 { 724 struct proc *p; 725 726 if (pgrp) 727 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 728 if (checkctty == 0 || p->p_flag & P_CONTROLT) 729 psignal(p, signum); 730 } 731 732 /* 733 * Send a signal caused by a trap to the current process. 734 * If it will be caught immediately, deliver it with correct code. 735 * Otherwise, post it normally. 736 */ 737 void 738 trapsignal(struct lwp *l, int signum, u_long code) 739 { 740 struct proc *p; 741 struct sigacts *ps; 742 743 p = l->l_proc; 744 ps = p->p_sigacts; 745 if ((p->p_flag & P_TRACED) == 0 && 746 sigismember(&p->p_sigctx.ps_sigcatch, signum) && 747 !sigismember(&p->p_sigctx.ps_sigmask, signum)) { 748 p->p_stats->p_ru.ru_nsignals++; 749 #ifdef KTRACE 750 if (KTRPOINT(p, KTR_PSIG)) 751 ktrpsig(p, signum, 752 SIGACTION_PS(ps, signum).sa_handler, 753 &p->p_sigctx.ps_sigmask, code); 754 #endif 755 psendsig(l, signum, &p->p_sigctx.ps_sigmask, code); 756 (void) splsched(); /* XXXSMP */ 757 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 758 &p->p_sigctx.ps_sigmask); 759 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 760 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 761 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 762 sigaddset(&p->p_sigctx.ps_sigignore, signum); 763 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 764 } 765 (void) spl0(); /* XXXSMP */ 766 } else { 767 p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */ 768 p->p_sigctx.ps_sig = signum; /* XXX to verify code */ 769 psignal(p, signum); 770 } 771 } 772 773 /* 774 * Send the signal to the process. If the signal has an action, the action 775 * is usually performed by the target process rather than the caller; we add 776 * the signal to the set of pending signals for the process. 777 * 778 * Exceptions: 779 * o When a stop signal is sent to a sleeping process that takes the 780 * default action, the process is stopped without awakening it. 781 * o SIGCONT restarts stopped processes (or puts them back to sleep) 782 * regardless of the signal action (eg, blocked or ignored). 783 * 784 * Other ignored signals are discarded immediately. 785 * 786 * XXXSMP: Invoked as psignal() or sched_psignal(). 787 */ 788 void 789 psignal1(struct proc *p, int signum, 790 int dolock) /* XXXSMP: works, but icky */ 791 { 792 struct lwp *l, *suspended; 793 int s = 0, prop, allsusp; 794 sig_t action; 795 796 #ifdef DIAGNOSTIC 797 if (signum <= 0 || signum >= NSIG) 798 panic("psignal signal number"); 799 800 /* XXXSMP: works, but icky */ 801 if (dolock) 802 SCHED_ASSERT_UNLOCKED(); 803 else 804 SCHED_ASSERT_LOCKED(); 805 #endif 806 /* 807 * Notify any interested parties in the signal. 808 */ 809 KNOTE(&p->p_klist, NOTE_SIGNAL | signum); 810 811 prop = sigprop[signum]; 812 813 /* 814 * If proc is traced, always give parent a chance. 815 */ 816 if (p->p_flag & P_TRACED) 817 action = SIG_DFL; 818 else { 819 /* 820 * If the signal is being ignored, 821 * then we forget about it immediately. 822 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore, 823 * and if it is set to SIG_IGN, 824 * action will be SIG_DFL here.) 825 */ 826 if (sigismember(&p->p_sigctx.ps_sigignore, signum)) 827 return; 828 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 829 action = SIG_HOLD; 830 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) 831 action = SIG_CATCH; 832 else { 833 action = SIG_DFL; 834 835 if (prop & SA_KILL && p->p_nice > NZERO) 836 p->p_nice = NZERO; 837 838 /* 839 * If sending a tty stop signal to a member of an 840 * orphaned process group, discard the signal here if 841 * the action is default; don't stop the process below 842 * if sleeping, and don't clear any pending SIGCONT. 843 */ 844 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 845 return; 846 } 847 } 848 849 if (prop & SA_CONT) 850 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist); 851 852 if (prop & SA_STOP) 853 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist); 854 855 sigaddset(&p->p_sigctx.ps_siglist, signum); 856 857 /* CHECKSIGS() is "inlined" here. */ 858 p->p_sigctx.ps_sigcheck = 1; 859 860 /* 861 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL, 862 * please!), check if anything waits on it. If yes, clear the 863 * pending signal from siglist set, save it to ps_sigwaited, 864 * clear sigwait list, and wakeup any sigwaiters. 865 * The signal won't be processed further here. 866 */ 867 if ((prop & SA_CANTMASK) == 0 868 && p->p_sigctx.ps_sigwaited < 0 869 && sigismember(&p->p_sigctx.ps_sigwait, signum)) { 870 sigdelset(&p->p_sigctx.ps_siglist, signum); 871 p->p_sigctx.ps_sigwaited = signum; 872 sigemptyset(&p->p_sigctx.ps_sigwait); 873 874 if (dolock) 875 wakeup_one(&p->p_sigctx.ps_sigwait); 876 else 877 sched_wakeup(&p->p_sigctx.ps_sigwait); 878 return; 879 } 880 881 /* 882 * Defer further processing for signals which are held, 883 * except that stopped processes must be continued by SIGCONT. 884 */ 885 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 886 return; 887 /* XXXSMP: works, but icky */ 888 if (dolock) 889 SCHED_LOCK(s); 890 891 if (p->p_nrlwps > 0) { 892 /* 893 * At least one LWP is running or on a run queue. 894 * The signal will be noticed when one of them returns 895 * to userspace. 896 */ 897 signotify(p); 898 /* 899 * The signal will be noticed very soon. 900 */ 901 goto out; 902 } else { 903 /* Process is sleeping or stopped */ 904 if (p->p_flag & P_SA) { 905 l = p->p_sa->sa_idle; 906 } else { 907 /* 908 * Find out if any of the sleeps are interruptable, 909 * and if all the live LWPs remaining are suspended. 910 */ 911 allsusp = 1; 912 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 913 if (l->l_stat == LSSLEEP && 914 l->l_flag & L_SINTR) 915 break; 916 if (l->l_stat == LSSUSPENDED) 917 suspended = l; 918 else if ((l->l_stat != LSZOMB) && 919 (l->l_stat != LSDEAD)) 920 allsusp = 0; 921 } 922 } 923 if (p->p_stat == SACTIVE) { 924 /* All LWPs must be sleeping */ 925 KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL)); 926 927 if (l != NULL && (p->p_flag & P_TRACED)) 928 goto run; 929 930 /* 931 * If SIGCONT is default (or ignored) and process is 932 * asleep, we are finished; the process should not 933 * be awakened. 934 */ 935 if ((prop & SA_CONT) && action == SIG_DFL) { 936 sigdelset(&p->p_sigctx.ps_siglist, signum); 937 goto out; 938 } 939 940 /* 941 * When a sleeping process receives a stop 942 * signal, process immediately if possible. 943 */ 944 if ((prop & SA_STOP) && action == SIG_DFL) { 945 /* 946 * If a child holding parent blocked, 947 * stopping could cause deadlock. 948 */ 949 if (p->p_flag & P_PPWAIT) 950 goto out; 951 sigdelset(&p->p_sigctx.ps_siglist, signum); 952 p->p_xstat = signum; 953 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { 954 /* 955 * XXXSMP: recursive call; don't lock 956 * the second time around. 957 */ 958 sched_psignal(p->p_pptr, SIGCHLD); 959 } 960 proc_stop(p); /* XXXSMP: recurse? */ 961 goto out; 962 } 963 964 if (l == NULL) { 965 /* 966 * Special case: SIGKILL of a process 967 * which is entirely composed of 968 * suspended LWPs should succeed. We 969 * make this happen by unsuspending one of 970 * them. 971 */ 972 if (allsusp && (signum == SIGKILL)) 973 lwp_continue(suspended); 974 goto out; 975 } 976 /* 977 * All other (caught or default) signals 978 * cause the process to run. 979 */ 980 goto runfast; 981 /*NOTREACHED*/ 982 } else if (p->p_stat == SSTOP) { 983 /* Process is stopped */ 984 /* 985 * If traced process is already stopped, 986 * then no further action is necessary. 987 */ 988 if (p->p_flag & P_TRACED) 989 goto out; 990 991 /* 992 * Kill signal always sets processes running, 993 * if possible. 994 */ 995 if (signum == SIGKILL) { 996 l = proc_unstop(p); 997 if (l) 998 goto runfast; 999 goto out; 1000 } 1001 1002 if (prop & SA_CONT) { 1003 /* 1004 * If SIGCONT is default (or ignored), 1005 * we continue the process but don't 1006 * leave the signal in ps_siglist, as 1007 * it has no further action. If 1008 * SIGCONT is held, we continue the 1009 * process and leave the signal in 1010 * ps_siglist. If the process catches 1011 * SIGCONT, let it handle the signal 1012 * itself. If it isn't waiting on an 1013 * event, then it goes back to run 1014 * state. Otherwise, process goes 1015 * back to sleep state. 1016 */ 1017 if (action == SIG_DFL) 1018 sigdelset(&p->p_sigctx.ps_siglist, 1019 signum); 1020 l = proc_unstop(p); 1021 if (l && (action == SIG_CATCH)) 1022 goto runfast; 1023 goto out; 1024 } 1025 1026 if (prop & SA_STOP) { 1027 /* 1028 * Already stopped, don't need to stop again. 1029 * (If we did the shell could get confused.) 1030 */ 1031 sigdelset(&p->p_sigctx.ps_siglist, signum); 1032 goto out; 1033 } 1034 1035 /* 1036 * If a lwp is sleeping interruptibly, then 1037 * wake it up; it will run until the kernel 1038 * boundary, where it will stop in issignal(), 1039 * since p->p_stat is still SSTOP. When the 1040 * process is continued, it will be made 1041 * runnable and can look at the signal. 1042 */ 1043 if (l) 1044 goto run; 1045 goto out; 1046 } else { 1047 /* Else what? */ 1048 panic("psignal: Invalid process state %d.", 1049 p->p_stat); 1050 } 1051 } 1052 /*NOTREACHED*/ 1053 1054 runfast: 1055 /* 1056 * Raise priority to at least PUSER. 1057 */ 1058 if (l->l_priority > PUSER) 1059 l->l_priority = PUSER; 1060 run: 1061 setrunnable(l); /* XXXSMP: recurse? */ 1062 out: 1063 /* XXXSMP: works, but icky */ 1064 if (dolock) 1065 SCHED_UNLOCK(s); 1066 } 1067 1068 void 1069 psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code) 1070 { 1071 struct proc *p = l->l_proc; 1072 struct lwp *le, *li; 1073 siginfo_t *si; 1074 1075 if (p->p_flag & P_SA) { 1076 si = pool_get(&siginfo_pool, PR_WAITOK); 1077 si->si_signo = sig; 1078 si->si_errno = 0; 1079 si->si_code = code; 1080 le = li = NULL; 1081 if (code) 1082 le = l; 1083 else 1084 li = l; 1085 1086 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li, 1087 sizeof(siginfo_t), si); 1088 return; 1089 } 1090 1091 (*p->p_emul->e_sendsig)(sig, mask, code); 1092 } 1093 1094 static __inline int firstsig(const sigset_t *); 1095 1096 static __inline int 1097 firstsig(const sigset_t *ss) 1098 { 1099 int sig; 1100 1101 sig = ffs(ss->__bits[0]); 1102 if (sig != 0) 1103 return (sig); 1104 #if NSIG > 33 1105 sig = ffs(ss->__bits[1]); 1106 if (sig != 0) 1107 return (sig + 32); 1108 #endif 1109 #if NSIG > 65 1110 sig = ffs(ss->__bits[2]); 1111 if (sig != 0) 1112 return (sig + 64); 1113 #endif 1114 #if NSIG > 97 1115 sig = ffs(ss->__bits[3]); 1116 if (sig != 0) 1117 return (sig + 96); 1118 #endif 1119 return (0); 1120 } 1121 1122 /* 1123 * If the current process has received a signal (should be caught or cause 1124 * termination, should interrupt current syscall), return the signal number. 1125 * Stop signals with default action are processed immediately, then cleared; 1126 * they aren't returned. This is checked after each entry to the system for 1127 * a syscall or trap (though this can usually be done without calling issignal 1128 * by checking the pending signal masks in the CURSIG macro.) The normal call 1129 * sequence is 1130 * 1131 * while (signum = CURSIG(curlwp)) 1132 * postsig(signum); 1133 */ 1134 int 1135 issignal(struct lwp *l) 1136 { 1137 struct proc *p = l->l_proc; 1138 int s = 0, signum, prop; 1139 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock; 1140 sigset_t ss; 1141 1142 if (p->p_stat == SSTOP) { 1143 /* 1144 * The process is stopped/stopping. Stop ourselves now that 1145 * we're on the kernel/userspace boundary. 1146 */ 1147 if (dolock) 1148 SCHED_LOCK(s); 1149 l->l_stat = LSSTOP; 1150 p->p_nrlwps--; 1151 if (p->p_flag & P_TRACED) 1152 goto sigtraceswitch; 1153 else 1154 goto sigswitch; 1155 } 1156 for (;;) { 1157 sigpending1(p, &ss); 1158 if (p->p_flag & P_PPWAIT) 1159 sigminusset(&stopsigmask, &ss); 1160 signum = firstsig(&ss); 1161 if (signum == 0) { /* no signal to send */ 1162 p->p_sigctx.ps_sigcheck = 0; 1163 if (locked && dolock) 1164 SCHED_LOCK(s); 1165 return (0); 1166 } 1167 /* take the signal! */ 1168 sigdelset(&p->p_sigctx.ps_siglist, signum); 1169 1170 /* 1171 * We should see pending but ignored signals 1172 * only if P_TRACED was on when they were posted. 1173 */ 1174 if (sigismember(&p->p_sigctx.ps_sigignore, signum) && 1175 (p->p_flag & P_TRACED) == 0) 1176 continue; 1177 1178 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1179 /* 1180 * If traced, always stop, and stay 1181 * stopped until released by the debugger. 1182 */ 1183 p->p_xstat = signum; 1184 if ((p->p_flag & P_FSTRACE) == 0) 1185 psignal1(p->p_pptr, SIGCHLD, dolock); 1186 if (dolock) 1187 SCHED_LOCK(s); 1188 proc_stop(p); 1189 sigtraceswitch: 1190 mi_switch(l, NULL); 1191 SCHED_ASSERT_UNLOCKED(); 1192 if (dolock) 1193 splx(s); 1194 else 1195 dolock = 1; 1196 1197 /* 1198 * If we are no longer being traced, or the parent 1199 * didn't give us a signal, look for more signals. 1200 */ 1201 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0) 1202 continue; 1203 1204 /* 1205 * If the new signal is being masked, look for other 1206 * signals. 1207 */ 1208 signum = p->p_xstat; 1209 p->p_xstat = 0; 1210 /* 1211 * `p->p_sigctx.ps_siglist |= mask' is done 1212 * in setrunnable(). 1213 */ 1214 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1215 continue; 1216 /* take the signal! */ 1217 sigdelset(&p->p_sigctx.ps_siglist, signum); 1218 } 1219 1220 prop = sigprop[signum]; 1221 1222 /* 1223 * Decide whether the signal should be returned. 1224 * Return the signal's number, or fall through 1225 * to clear it from the pending mask. 1226 */ 1227 switch ((long)SIGACTION(p, signum).sa_handler) { 1228 1229 case (long)SIG_DFL: 1230 /* 1231 * Don't take default actions on system processes. 1232 */ 1233 if (p->p_pid <= 1) { 1234 #ifdef DIAGNOSTIC 1235 /* 1236 * Are you sure you want to ignore SIGSEGV 1237 * in init? XXX 1238 */ 1239 printf("Process (pid %d) got signal %d\n", 1240 p->p_pid, signum); 1241 #endif 1242 break; /* == ignore */ 1243 } 1244 /* 1245 * If there is a pending stop signal to process 1246 * with default action, stop here, 1247 * then clear the signal. However, 1248 * if process is member of an orphaned 1249 * process group, ignore tty stop signals. 1250 */ 1251 if (prop & SA_STOP) { 1252 if (p->p_flag & P_TRACED || 1253 (p->p_pgrp->pg_jobc == 0 && 1254 prop & SA_TTYSTOP)) 1255 break; /* == ignore */ 1256 p->p_xstat = signum; 1257 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 1258 psignal1(p->p_pptr, SIGCHLD, dolock); 1259 if (dolock) 1260 SCHED_LOCK(s); 1261 proc_stop(p); 1262 sigswitch: 1263 mi_switch(l, NULL); 1264 SCHED_ASSERT_UNLOCKED(); 1265 if (dolock) 1266 splx(s); 1267 else 1268 dolock = 1; 1269 break; 1270 } else if (prop & SA_IGNORE) { 1271 /* 1272 * Except for SIGCONT, shouldn't get here. 1273 * Default action is to ignore; drop it. 1274 */ 1275 break; /* == ignore */ 1276 } else 1277 goto keep; 1278 /*NOTREACHED*/ 1279 1280 case (long)SIG_IGN: 1281 /* 1282 * Masking above should prevent us ever trying 1283 * to take action on an ignored signal other 1284 * than SIGCONT, unless process is traced. 1285 */ 1286 #ifdef DEBUG_ISSIGNAL 1287 if ((prop & SA_CONT) == 0 && 1288 (p->p_flag & P_TRACED) == 0) 1289 printf("issignal\n"); 1290 #endif 1291 break; /* == ignore */ 1292 1293 default: 1294 /* 1295 * This signal has an action, let 1296 * postsig() process it. 1297 */ 1298 goto keep; 1299 } 1300 } 1301 /* NOTREACHED */ 1302 1303 keep: 1304 /* leave the signal for later */ 1305 sigaddset(&p->p_sigctx.ps_siglist, signum); 1306 CHECKSIGS(p); 1307 if (locked && dolock) 1308 SCHED_LOCK(s); 1309 return (signum); 1310 } 1311 1312 /* 1313 * Put the argument process into the stopped state and notify the parent 1314 * via wakeup. Signals are handled elsewhere. The process must not be 1315 * on the run queue. 1316 */ 1317 static void 1318 proc_stop(struct proc *p) 1319 { 1320 struct lwp *l; 1321 1322 SCHED_ASSERT_LOCKED(); 1323 1324 /* XXX lock process LWP state */ 1325 p->p_stat = SSTOP; 1326 p->p_flag &= ~P_WAITED; 1327 1328 /* 1329 * Put as many LWP's as possible in stopped state. 1330 * Sleeping ones will notice the stopped state as they try to 1331 * return to userspace. 1332 */ 1333 1334 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1335 if (l->l_stat == LSONPROC) { 1336 /* XXX SMP this assumes that a LWP that is LSONPROC 1337 * is curlwp and hence is about to be mi_switched 1338 * away; the only callers of proc_stop() are: 1339 * - psignal 1340 * - issignal() 1341 * For the former, proc_stop() is only called when 1342 * no processes are running, so we don't worry. 1343 * For the latter, proc_stop() is called right 1344 * before mi_switch(). 1345 */ 1346 l->l_stat = LSSTOP; 1347 p->p_nrlwps--; 1348 } else if (l->l_stat == LSRUN) { 1349 /* Remove LWP from the run queue */ 1350 remrunqueue(l); 1351 l->l_stat = LSSTOP; 1352 p->p_nrlwps--; 1353 } else if ((l->l_stat == LSSLEEP) || 1354 (l->l_stat == LSSUSPENDED) || 1355 (l->l_stat == LSZOMB) || 1356 (l->l_stat == LSDEAD)) { 1357 /* 1358 * Don't do anything; let sleeping LWPs 1359 * discover the stopped state of the process 1360 * on their way out of the kernel; otherwise, 1361 * things like NFS threads that sleep with 1362 * locks will block the rest of the system 1363 * from getting any work done. 1364 * 1365 * Suspended/dead/zombie LWPs aren't going 1366 * anywhere, so we don't need to touch them. 1367 */ 1368 } 1369 #ifdef DIAGNOSTIC 1370 else { 1371 panic("proc_stop: process %d lwp %d " 1372 "in unstoppable state %d.\n", 1373 p->p_pid, l->l_lid, l->l_stat); 1374 } 1375 #endif 1376 } 1377 /* XXX unlock process LWP state */ 1378 1379 sched_wakeup((caddr_t)p->p_pptr); 1380 } 1381 1382 /* 1383 * Given a process in state SSTOP, set the state back to SACTIVE and 1384 * move LSSTOP'd LWPs to LSSLEEP or make them runnable. 1385 * 1386 * If no LWPs ended up runnable (and therefore able to take a signal), 1387 * return a LWP that is sleeping interruptably. The caller can wake 1388 * that LWP up to take a signal. 1389 */ 1390 struct lwp * 1391 proc_unstop(struct proc *p) 1392 { 1393 struct lwp *l, *lr = NULL; 1394 int cantake = 0; 1395 1396 SCHED_ASSERT_LOCKED(); 1397 1398 /* 1399 * Our caller wants to be informed if there are only sleeping 1400 * and interruptable LWPs left after we have run so that it 1401 * can invoke setrunnable() if required - return one of the 1402 * interruptable LWPs if this is the case. 1403 */ 1404 1405 p->p_stat = SACTIVE; 1406 if (p->p_flag & P_SA) { 1407 /* 1408 * Preferentially select the idle LWP as the interruptable 1409 * LWP to return if it exists. 1410 */ 1411 lr = p->p_sa->sa_idle; 1412 if (lr != NULL) 1413 cantake = 1; 1414 } 1415 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1416 if (l->l_stat == LSRUN) { 1417 lr = NULL; 1418 cantake = 1; 1419 } 1420 if (l->l_stat != LSSTOP) 1421 continue; 1422 1423 if (l->l_wchan != NULL) { 1424 l->l_stat = LSSLEEP; 1425 if ((cantake == 0) && (l->l_flag & L_SINTR)) { 1426 lr = l; 1427 cantake = 1; 1428 } 1429 } else { 1430 setrunnable(l); 1431 lr = NULL; 1432 cantake = 1; 1433 } 1434 } 1435 1436 return lr; 1437 } 1438 1439 /* 1440 * Take the action for the specified signal 1441 * from the current set of pending signals. 1442 */ 1443 void 1444 postsig(int signum) 1445 { 1446 struct lwp *l; 1447 struct proc *p; 1448 struct sigacts *ps; 1449 sig_t action; 1450 u_long code; 1451 sigset_t *returnmask; 1452 1453 l = curlwp; 1454 p = l->l_proc; 1455 ps = p->p_sigacts; 1456 #ifdef DIAGNOSTIC 1457 if (signum == 0) 1458 panic("postsig"); 1459 #endif 1460 1461 KERNEL_PROC_LOCK(l); 1462 1463 sigdelset(&p->p_sigctx.ps_siglist, signum); 1464 action = SIGACTION_PS(ps, signum).sa_handler; 1465 #ifdef KTRACE 1466 if (KTRPOINT(p, KTR_PSIG)) 1467 ktrpsig(p, 1468 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ? 1469 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0); 1470 #endif 1471 if (action == SIG_DFL) { 1472 /* 1473 * Default action, where the default is to kill 1474 * the process. (Other cases were ignored above.) 1475 */ 1476 sigexit(l, signum); 1477 /* NOTREACHED */ 1478 } else { 1479 /* 1480 * If we get here, the signal must be caught. 1481 */ 1482 #ifdef DIAGNOSTIC 1483 if (action == SIG_IGN || 1484 sigismember(&p->p_sigctx.ps_sigmask, signum)) 1485 panic("postsig action"); 1486 #endif 1487 /* 1488 * Set the new mask value and also defer further 1489 * occurrences of this signal. 1490 * 1491 * Special case: user has done a sigpause. Here the 1492 * current mask is not of interest, but rather the 1493 * mask from before the sigpause is what we want 1494 * restored after the signal processing is completed. 1495 */ 1496 if (p->p_sigctx.ps_flags & SAS_OLDMASK) { 1497 returnmask = &p->p_sigctx.ps_oldmask; 1498 p->p_sigctx.ps_flags &= ~SAS_OLDMASK; 1499 } else 1500 returnmask = &p->p_sigctx.ps_sigmask; 1501 p->p_stats->p_ru.ru_nsignals++; 1502 if (p->p_sigctx.ps_sig != signum) { 1503 code = 0; 1504 } else { 1505 code = p->p_sigctx.ps_code; 1506 p->p_sigctx.ps_code = 0; 1507 p->p_sigctx.ps_sig = 0; 1508 } 1509 psendsig(l, signum, returnmask, code); 1510 (void) splsched(); /* XXXSMP */ 1511 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 1512 &p->p_sigctx.ps_sigmask); 1513 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 1514 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1515 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1516 sigaddset(&p->p_sigctx.ps_sigignore, signum); 1517 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 1518 } 1519 (void) spl0(); /* XXXSMP */ 1520 } 1521 1522 KERNEL_PROC_UNLOCK(l); 1523 } 1524 1525 /* 1526 * Kill the current process for stated reason. 1527 */ 1528 void 1529 killproc(struct proc *p, const char *why) 1530 { 1531 1532 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1533 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1534 psignal(p, SIGKILL); 1535 } 1536 1537 /* 1538 * Force the current process to exit with the specified signal, dumping core 1539 * if appropriate. We bypass the normal tests for masked and caught signals, 1540 * allowing unrecoverable failures to terminate the process without changing 1541 * signal state. Mark the accounting record with the signal termination. 1542 * If dumping core, save the signal number for the debugger. Calls exit and 1543 * does not return. 1544 */ 1545 1546 #if defined(DEBUG) 1547 int kern_logsigexit = 1; /* not static to make public for sysctl */ 1548 #else 1549 int kern_logsigexit = 0; /* not static to make public for sysctl */ 1550 #endif 1551 1552 static const char logcoredump[] = 1553 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 1554 static const char lognocoredump[] = 1555 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 1556 1557 /* Wrapper function for use in p_userret */ 1558 static void 1559 lwp_coredump_hook(struct lwp *l, void *arg) 1560 { 1561 int s; 1562 1563 /* 1564 * Suspend ourselves, so that the kernel stack and therefore 1565 * the userland registers saved in the trapframe are around 1566 * for coredump() to write them out. 1567 */ 1568 KERNEL_PROC_LOCK(l); 1569 l->l_flag &= ~L_DETACHED; 1570 SCHED_LOCK(s); 1571 l->l_stat = LSSUSPENDED; 1572 l->l_proc->p_nrlwps--; 1573 /* XXX NJWLWP check if this makes sense here: */ 1574 l->l_proc->p_stats->p_ru.ru_nvcsw++; 1575 mi_switch(l, NULL); 1576 SCHED_ASSERT_UNLOCKED(); 1577 splx(s); 1578 1579 lwp_exit(l); 1580 } 1581 1582 void 1583 sigexit(struct lwp *l, int signum) 1584 { 1585 struct proc *p; 1586 struct lwp *l2; 1587 int error, exitsig; 1588 1589 p = l->l_proc; 1590 1591 /* 1592 * Don't permit coredump() or exit1() multiple times 1593 * in the same process. 1594 */ 1595 if (p->p_flag & P_WEXIT) { 1596 KERNEL_PROC_UNLOCK(l); 1597 (*p->p_userret)(l, p->p_userret_arg); 1598 } 1599 p->p_flag |= P_WEXIT; 1600 /* We don't want to switch away from exiting. */ 1601 /* XXX multiprocessor: stop LWPs on other processors. */ 1602 if (p->p_flag & P_SA) { 1603 LIST_FOREACH(l2, &p->p_lwps, l_sibling) 1604 l2->l_flag &= ~L_SA; 1605 p->p_flag &= ~P_SA; 1606 } 1607 1608 /* Make other LWPs stick around long enough to be dumped */ 1609 p->p_userret = lwp_coredump_hook; 1610 p->p_userret_arg = NULL; 1611 1612 exitsig = signum; 1613 p->p_acflag |= AXSIG; 1614 if (sigprop[signum] & SA_CORE) { 1615 p->p_sigctx.ps_sig = signum; 1616 if ((error = coredump(l)) == 0) 1617 exitsig |= WCOREFLAG; 1618 1619 if (kern_logsigexit) { 1620 /* XXX What if we ever have really large UIDs? */ 1621 int uid = p->p_cred && p->p_ucred ? 1622 (int) p->p_ucred->cr_uid : -1; 1623 1624 if (error) 1625 log(LOG_INFO, lognocoredump, p->p_pid, 1626 p->p_comm, uid, signum, error); 1627 else 1628 log(LOG_INFO, logcoredump, p->p_pid, 1629 p->p_comm, uid, signum); 1630 } 1631 1632 } 1633 1634 exit1(l, W_EXITCODE(0, exitsig)); 1635 /* NOTREACHED */ 1636 } 1637 1638 /* 1639 * Dump core, into a file named "progname.core" or "core" (depending on the 1640 * value of shortcorename), unless the process was setuid/setgid. 1641 */ 1642 int 1643 coredump(struct lwp *l) 1644 { 1645 struct vnode *vp; 1646 struct proc *p; 1647 struct vmspace *vm; 1648 struct ucred *cred; 1649 struct nameidata nd; 1650 struct vattr vattr; 1651 int error, error1; 1652 char name[MAXPATHLEN]; 1653 1654 p = l->l_proc; 1655 vm = p->p_vmspace; 1656 cred = p->p_cred->pc_ucred; 1657 1658 /* 1659 * Make sure the process has not set-id, to prevent data leaks. 1660 */ 1661 if (p->p_flag & P_SUGID) 1662 return (EPERM); 1663 1664 /* 1665 * Refuse to core if the data + stack + user size is larger than 1666 * the core dump limit. XXX THIS IS WRONG, because of mapped 1667 * data. 1668 */ 1669 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >= 1670 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1671 return (EFBIG); /* better error code? */ 1672 1673 /* 1674 * The core dump will go in the current working directory. Make 1675 * sure that the directory is still there and that the mount flags 1676 * allow us to write core dumps there. 1677 */ 1678 vp = p->p_cwdi->cwdi_cdir; 1679 if (vp->v_mount == NULL || 1680 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) 1681 return (EPERM); 1682 1683 error = build_corename(p, name); 1684 if (error) 1685 return error; 1686 1687 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1688 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR); 1689 if (error) 1690 return (error); 1691 vp = nd.ni_vp; 1692 1693 /* Don't dump to non-regular files or files with links. */ 1694 if (vp->v_type != VREG || 1695 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1696 error = EINVAL; 1697 goto out; 1698 } 1699 VATTR_NULL(&vattr); 1700 vattr.va_size = 0; 1701 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1702 VOP_SETATTR(vp, &vattr, cred, p); 1703 p->p_acflag |= ACORE; 1704 1705 /* Now dump the actual core file. */ 1706 error = (*p->p_execsw->es_coredump)(l, vp, cred); 1707 out: 1708 VOP_UNLOCK(vp, 0); 1709 error1 = vn_close(vp, FWRITE, cred, p); 1710 if (error == 0) 1711 error = error1; 1712 return (error); 1713 } 1714 1715 /* 1716 * Nonexistent system call-- signal process (may want to handle it). 1717 * Flag error in case process won't see signal immediately (blocked or ignored). 1718 */ 1719 /* ARGSUSED */ 1720 int 1721 sys_nosys(struct lwp *l, void *v, register_t *retval) 1722 { 1723 struct proc *p; 1724 1725 p = l->l_proc; 1726 psignal(p, SIGSYS); 1727 return (ENOSYS); 1728 } 1729 1730 static int 1731 build_corename(struct proc *p, char dst[MAXPATHLEN]) 1732 { 1733 const char *s; 1734 char *d, *end; 1735 int i; 1736 1737 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN; 1738 *s != '\0'; s++) { 1739 if (*s == '%') { 1740 switch (*(s + 1)) { 1741 case 'n': 1742 i = snprintf(d, end - d, "%s", p->p_comm); 1743 break; 1744 case 'p': 1745 i = snprintf(d, end - d, "%d", p->p_pid); 1746 break; 1747 case 'u': 1748 i = snprintf(d, end - d, "%.*s", 1749 (int)sizeof p->p_pgrp->pg_session->s_login, 1750 p->p_pgrp->pg_session->s_login); 1751 break; 1752 case 't': 1753 i = snprintf(d, end - d, "%ld", 1754 p->p_stats->p_start.tv_sec); 1755 break; 1756 default: 1757 goto copy; 1758 } 1759 d += i; 1760 s++; 1761 } else { 1762 copy: *d = *s; 1763 d++; 1764 } 1765 if (d >= end) 1766 return (ENAMETOOLONG); 1767 } 1768 *d = '\0'; 1769 return 0; 1770 } 1771 1772 void 1773 getucontext(struct lwp *l, ucontext_t *ucp) 1774 { 1775 struct proc *p; 1776 1777 p = l->l_proc; 1778 1779 ucp->uc_flags = 0; 1780 ucp->uc_link = l->l_ctxlink; 1781 1782 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask); 1783 ucp->uc_flags |= _UC_SIGMASK; 1784 1785 /* 1786 * The (unsupplied) definition of the `current execution stack' 1787 * in the System V Interface Definition appears to allow returning 1788 * the main context stack. 1789 */ 1790 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) { 1791 ucp->uc_stack.ss_sp = (void *)USRSTACK; 1792 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize); 1793 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 1794 } else { 1795 /* Simply copy alternate signal execution stack. */ 1796 ucp->uc_stack = p->p_sigctx.ps_sigstk; 1797 } 1798 ucp->uc_flags |= _UC_STACK; 1799 1800 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 1801 } 1802 1803 /* ARGSUSED */ 1804 int 1805 sys_getcontext(struct lwp *l, void *v, register_t *retval) 1806 { 1807 struct sys_getcontext_args /* { 1808 syscallarg(struct __ucontext *) ucp; 1809 } */ *uap = v; 1810 ucontext_t uc; 1811 1812 getucontext(l, &uc); 1813 1814 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)))); 1815 } 1816 1817 int 1818 setucontext(struct lwp *l, const ucontext_t *ucp) 1819 { 1820 struct proc *p; 1821 int error; 1822 1823 p = l->l_proc; 1824 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0) 1825 return (error); 1826 l->l_ctxlink = ucp->uc_link; 1827 /* 1828 * We might want to take care of the stack portion here but currently 1829 * don't; see the comment in getucontext(). 1830 */ 1831 if ((ucp->uc_flags & _UC_SIGMASK) != 0) 1832 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL); 1833 1834 return 0; 1835 } 1836 1837 /* ARGSUSED */ 1838 int 1839 sys_setcontext(struct lwp *l, void *v, register_t *retval) 1840 { 1841 struct sys_setcontext_args /* { 1842 syscallarg(const ucontext_t *) ucp; 1843 } */ *uap = v; 1844 ucontext_t uc; 1845 int error; 1846 1847 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */ 1848 exit1(l, W_EXITCODE(0, 0)); 1849 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 || 1850 (error = setucontext(l, &uc)) != 0) 1851 return (error); 1852 1853 return (EJUSTRETURN); 1854 } 1855 1856 /* 1857 * sigtimedwait(2) system call, used also for implementation 1858 * of sigwaitinfo() and sigwait(). 1859 * 1860 * This only handles single LWP in signal wait. libpthread provides 1861 * it's own sigtimedwait() wrapper to DTRT WRT individual threads. 1862 * 1863 * XXX no support for queued signals, si_code is always SI_USER. 1864 */ 1865 int 1866 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval) 1867 { 1868 struct sys___sigtimedwait_args /* { 1869 syscallarg(const sigset_t *) set; 1870 syscallarg(siginfo_t *) info; 1871 syscallarg(struct timespec *) timeout; 1872 } */ *uap = v; 1873 sigset_t waitset, twaitset; 1874 struct proc *p = l->l_proc; 1875 int error, signum, s; 1876 int timo = 0; 1877 struct timeval tvstart; 1878 struct timespec ts; 1879 1880 if ((error = copyin(SCARG(uap, set), &waitset, sizeof(waitset)))) 1881 return (error); 1882 1883 /* 1884 * Silently ignore SA_CANTMASK signals. psignal1() would 1885 * ignore SA_CANTMASK signals in waitset, we do this 1886 * only for the below siglist check. 1887 */ 1888 sigminusset(&sigcantmask, &waitset); 1889 1890 /* 1891 * First scan siglist and check if there is signal from 1892 * our waitset already pending. 1893 */ 1894 twaitset = waitset; 1895 __sigandset(&p->p_sigctx.ps_siglist, &twaitset); 1896 if ((signum = firstsig(&twaitset))) { 1897 /* found pending signal */ 1898 sigdelset(&p->p_sigctx.ps_siglist, signum); 1899 goto sig; 1900 } 1901 1902 /* 1903 * Calculate timeout, if it was specified. 1904 */ 1905 if (SCARG(uap, timeout)) { 1906 uint64_t ms; 1907 1908 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts)))) 1909 return (error); 1910 1911 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000); 1912 timo = mstohz(ms); 1913 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0) 1914 timo = 1; 1915 if (timo <= 0) 1916 return (EAGAIN); 1917 1918 /* 1919 * Remember current mono_time, it would be used in 1920 * ECANCELED/ERESTART case. 1921 */ 1922 s = splclock(); 1923 tvstart = mono_time; 1924 splx(s); 1925 } 1926 1927 /* 1928 * Setup ps_sigwait list. 1929 */ 1930 p->p_sigctx.ps_sigwaited = -1; 1931 p->p_sigctx.ps_sigwait = waitset; 1932 1933 /* 1934 * Wait for signal to arrive. We can either be woken up or 1935 * time out. 1936 */ 1937 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo); 1938 1939 /* 1940 * Check if a signal from our wait set has arrived, or if it 1941 * was mere wakeup. 1942 */ 1943 if (!error) { 1944 if ((signum = p->p_sigctx.ps_sigwaited) <= 0) { 1945 /* wakeup via _lwp_wakeup() */ 1946 error = ECANCELED; 1947 } 1948 } 1949 1950 /* 1951 * On error, clear sigwait indication. psignal1() sets it 1952 * in !error case. 1953 */ 1954 if (error) { 1955 p->p_sigctx.ps_sigwaited = 0; 1956 1957 /* 1958 * If the sleep was interrupted (either by signal or wakeup), 1959 * update the timeout and copyout new value back. 1960 * It would be used when the syscall would be restarted 1961 * or called again. 1962 */ 1963 if (timo && (error == ERESTART || error == ECANCELED)) { 1964 struct timeval tvnow, tvtimo; 1965 int err; 1966 1967 s = splclock(); 1968 tvnow = mono_time; 1969 splx(s); 1970 1971 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts); 1972 1973 /* compute how much time has passed since start */ 1974 timersub(&tvnow, &tvstart, &tvnow); 1975 /* substract passed time from timeout */ 1976 timersub(&tvtimo, &tvnow, &tvtimo); 1977 1978 if (tvtimo.tv_sec < 0) 1979 return (EAGAIN); 1980 1981 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts); 1982 1983 /* copy updated timeout to userland */ 1984 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) 1985 return (err); 1986 } 1987 1988 return (error); 1989 } 1990 1991 /* 1992 * If a signal from the wait set arrived, copy it to userland. 1993 * XXX no queued signals for now 1994 */ 1995 if (signum > 0) { 1996 siginfo_t si; 1997 1998 sig: 1999 memset(&si, 0, sizeof(si)); 2000 si.si_signo = signum; 2001 si.si_code = SI_USER; 2002 2003 error = copyout(&si, SCARG(uap, info), sizeof(si)); 2004 if (error) 2005 return (error); 2006 } 2007 2008 return (0); 2009 } 2010 2011 /* 2012 * Returns true if signal is ignored or masked for passed process. 2013 */ 2014 int 2015 sigismasked(struct proc *p, int sig) 2016 { 2017 2018 return (sigismember(&p->p_sigctx.ps_sigignore, sig) || 2019 sigismember(&p->p_sigctx.ps_sigmask, sig)); 2020 } 2021 2022 static int 2023 filt_sigattach(struct knote *kn) 2024 { 2025 struct proc *p = curproc; 2026 2027 kn->kn_ptr.p_proc = p; 2028 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2029 2030 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2031 2032 return (0); 2033 } 2034 2035 static void 2036 filt_sigdetach(struct knote *kn) 2037 { 2038 struct proc *p = kn->kn_ptr.p_proc; 2039 2040 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2041 } 2042 2043 /* 2044 * signal knotes are shared with proc knotes, so we apply a mask to 2045 * the hint in order to differentiate them from process hints. This 2046 * could be avoided by using a signal-specific knote list, but probably 2047 * isn't worth the trouble. 2048 */ 2049 static int 2050 filt_signal(struct knote *kn, long hint) 2051 { 2052 2053 if (hint & NOTE_SIGNAL) { 2054 hint &= ~NOTE_SIGNAL; 2055 2056 if (kn->kn_id == hint) 2057 kn->kn_data++; 2058 } 2059 return (kn->kn_data != 0); 2060 } 2061 2062 const struct filterops sig_filtops = { 2063 0, filt_sigattach, filt_sigdetach, filt_signal 2064 }; 2065