1 /* $NetBSD: kern_sig.c,v 1.145 2003/07/21 22:57:46 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.145 2003/07/21 22:57:46 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 534 /* always return EINTR rather than ERESTART... */ 535 return (EINTR); 536 } 537 538 /* 539 * Suspend process until signal, providing mask to be set 540 * in the meantime. Note nonstandard calling convention: 541 * libc stub passes mask, not pointer, to save a copyin. 542 */ 543 /* ARGSUSED */ 544 int 545 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval) 546 { 547 struct sys___sigsuspend14_args /* { 548 syscallarg(const sigset_t *) set; 549 } */ *uap = v; 550 struct proc *p; 551 sigset_t ss; 552 int error; 553 554 if (SCARG(uap, set)) { 555 error = copyin(SCARG(uap, set), &ss, sizeof(ss)); 556 if (error) 557 return (error); 558 } 559 560 p = l->l_proc; 561 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0)); 562 } 563 564 int 565 sigaltstack1(struct proc *p, const struct sigaltstack *nss, 566 struct sigaltstack *oss) 567 { 568 569 if (oss) 570 *oss = p->p_sigctx.ps_sigstk; 571 572 if (nss) { 573 if (nss->ss_flags & ~SS_ALLBITS) 574 return (EINVAL); 575 576 if (nss->ss_flags & SS_DISABLE) { 577 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) 578 return (EINVAL); 579 } else { 580 if (nss->ss_size < MINSIGSTKSZ) 581 return (ENOMEM); 582 } 583 p->p_sigctx.ps_sigstk = *nss; 584 } 585 586 return (0); 587 } 588 589 /* ARGSUSED */ 590 int 591 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval) 592 { 593 struct sys___sigaltstack14_args /* { 594 syscallarg(const struct sigaltstack *) nss; 595 syscallarg(struct sigaltstack *) oss; 596 } */ *uap = v; 597 struct proc *p; 598 struct sigaltstack nss, oss; 599 int error; 600 601 if (SCARG(uap, nss)) { 602 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 603 if (error) 604 return (error); 605 } 606 p = l->l_proc; 607 error = sigaltstack1(p, 608 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0); 609 if (error) 610 return (error); 611 if (SCARG(uap, oss)) { 612 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 613 if (error) 614 return (error); 615 } 616 return (0); 617 } 618 619 /* ARGSUSED */ 620 int 621 sys_kill(struct lwp *l, void *v, register_t *retval) 622 { 623 struct sys_kill_args /* { 624 syscallarg(int) pid; 625 syscallarg(int) signum; 626 } */ *uap = v; 627 struct proc *cp, *p; 628 struct pcred *pc; 629 630 cp = l->l_proc; 631 pc = cp->p_cred; 632 if ((u_int)SCARG(uap, signum) >= NSIG) 633 return (EINVAL); 634 if (SCARG(uap, pid) > 0) { 635 /* kill single process */ 636 if ((p = pfind(SCARG(uap, pid))) == NULL) 637 return (ESRCH); 638 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum))) 639 return (EPERM); 640 if (SCARG(uap, signum)) 641 psignal(p, SCARG(uap, signum)); 642 return (0); 643 } 644 switch (SCARG(uap, pid)) { 645 case -1: /* broadcast signal */ 646 return (killpg1(cp, SCARG(uap, signum), 0, 1)); 647 case 0: /* signal own process group */ 648 return (killpg1(cp, SCARG(uap, signum), 0, 0)); 649 default: /* negative explicit process group */ 650 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0)); 651 } 652 /* NOTREACHED */ 653 } 654 655 /* 656 * Common code for kill process group/broadcast kill. 657 * cp is calling process. 658 */ 659 int 660 killpg1(struct proc *cp, int signum, int pgid, int all) 661 { 662 struct proc *p; 663 struct pcred *pc; 664 struct pgrp *pgrp; 665 int nfound; 666 667 pc = cp->p_cred; 668 nfound = 0; 669 if (all) { 670 /* 671 * broadcast 672 */ 673 proclist_lock_read(); 674 LIST_FOREACH(p, &allproc, p_list) { 675 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 676 p == cp || !CANSIGNAL(cp, pc, p, signum)) 677 continue; 678 nfound++; 679 if (signum) 680 psignal(p, signum); 681 } 682 proclist_unlock_read(); 683 } else { 684 if (pgid == 0) 685 /* 686 * zero pgid means send to my process group. 687 */ 688 pgrp = cp->p_pgrp; 689 else { 690 pgrp = pgfind(pgid); 691 if (pgrp == NULL) 692 return (ESRCH); 693 } 694 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 695 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 696 !CANSIGNAL(cp, pc, p, signum)) 697 continue; 698 nfound++; 699 if (signum && P_ZOMBIE(p) == 0) 700 psignal(p, signum); 701 } 702 } 703 return (nfound ? 0 : ESRCH); 704 } 705 706 /* 707 * Send a signal to a process group. 708 */ 709 void 710 gsignal(int pgid, int signum) 711 { 712 struct pgrp *pgrp; 713 714 if (pgid && (pgrp = pgfind(pgid))) 715 pgsignal(pgrp, signum, 0); 716 } 717 718 /* 719 * Send a signal to a process group. If checktty is 1, 720 * limit to members which have a controlling terminal. 721 */ 722 void 723 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 724 { 725 struct proc *p; 726 727 if (pgrp) 728 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 729 if (checkctty == 0 || p->p_flag & P_CONTROLT) 730 psignal(p, signum); 731 } 732 733 /* 734 * Send a signal caused by a trap to the current process. 735 * If it will be caught immediately, deliver it with correct code. 736 * Otherwise, post it normally. 737 */ 738 void 739 trapsignal(struct lwp *l, int signum, u_long code) 740 { 741 struct proc *p; 742 struct sigacts *ps; 743 744 p = l->l_proc; 745 ps = p->p_sigacts; 746 if ((p->p_flag & P_TRACED) == 0 && 747 sigismember(&p->p_sigctx.ps_sigcatch, signum) && 748 !sigismember(&p->p_sigctx.ps_sigmask, signum)) { 749 p->p_stats->p_ru.ru_nsignals++; 750 #ifdef KTRACE 751 if (KTRPOINT(p, KTR_PSIG)) 752 ktrpsig(p, signum, 753 SIGACTION_PS(ps, signum).sa_handler, 754 &p->p_sigctx.ps_sigmask, code); 755 #endif 756 psendsig(l, signum, &p->p_sigctx.ps_sigmask, code); 757 (void) splsched(); /* XXXSMP */ 758 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 759 &p->p_sigctx.ps_sigmask); 760 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 761 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 762 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 763 sigaddset(&p->p_sigctx.ps_sigignore, signum); 764 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 765 } 766 (void) spl0(); /* XXXSMP */ 767 } else { 768 p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */ 769 p->p_sigctx.ps_sig = signum; /* XXX to verify code */ 770 p->p_sigctx.ps_lwp = l->l_lid; 771 psignal(p, signum); 772 } 773 } 774 775 /* 776 * Send the signal to the process. If the signal has an action, the action 777 * is usually performed by the target process rather than the caller; we add 778 * the signal to the set of pending signals for the process. 779 * 780 * Exceptions: 781 * o When a stop signal is sent to a sleeping process that takes the 782 * default action, the process is stopped without awakening it. 783 * o SIGCONT restarts stopped processes (or puts them back to sleep) 784 * regardless of the signal action (eg, blocked or ignored). 785 * 786 * Other ignored signals are discarded immediately. 787 * 788 * XXXSMP: Invoked as psignal() or sched_psignal(). 789 */ 790 void 791 psignal1(struct proc *p, int signum, 792 int dolock) /* XXXSMP: works, but icky */ 793 { 794 struct lwp *l, *suspended; 795 int s = 0, prop, allsusp; 796 sig_t action; 797 798 #ifdef DIAGNOSTIC 799 if (signum <= 0 || signum >= NSIG) 800 panic("psignal signal number"); 801 802 /* XXXSMP: works, but icky */ 803 if (dolock) 804 SCHED_ASSERT_UNLOCKED(); 805 else 806 SCHED_ASSERT_LOCKED(); 807 #endif 808 /* 809 * Notify any interested parties in the signal. 810 */ 811 KNOTE(&p->p_klist, NOTE_SIGNAL | signum); 812 813 prop = sigprop[signum]; 814 815 /* 816 * If proc is traced, always give parent a chance. 817 */ 818 if (p->p_flag & P_TRACED) 819 action = SIG_DFL; 820 else { 821 /* 822 * If the signal is being ignored, 823 * then we forget about it immediately. 824 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore, 825 * and if it is set to SIG_IGN, 826 * action will be SIG_DFL here.) 827 */ 828 if (sigismember(&p->p_sigctx.ps_sigignore, signum)) 829 return; 830 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 831 action = SIG_HOLD; 832 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) 833 action = SIG_CATCH; 834 else { 835 action = SIG_DFL; 836 837 if (prop & SA_KILL && p->p_nice > NZERO) 838 p->p_nice = NZERO; 839 840 /* 841 * If sending a tty stop signal to a member of an 842 * orphaned process group, discard the signal here if 843 * the action is default; don't stop the process below 844 * if sleeping, and don't clear any pending SIGCONT. 845 */ 846 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 847 return; 848 } 849 } 850 851 if (prop & SA_CONT) 852 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist); 853 854 if (prop & SA_STOP) 855 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist); 856 857 sigaddset(&p->p_sigctx.ps_siglist, signum); 858 859 /* CHECKSIGS() is "inlined" here. */ 860 p->p_sigctx.ps_sigcheck = 1; 861 862 /* 863 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL, 864 * please!), check if anything waits on it. If yes, clear the 865 * pending signal from siglist set, save it to ps_sigwaited, 866 * clear sigwait list, and wakeup any sigwaiters. 867 * The signal won't be processed further here. 868 */ 869 if ((prop & SA_CANTMASK) == 0 870 && p->p_sigctx.ps_sigwaited < 0 871 && sigismember(&p->p_sigctx.ps_sigwait, signum) 872 && p->p_stat != SSTOP) { 873 sigdelset(&p->p_sigctx.ps_siglist, signum); 874 p->p_sigctx.ps_sigwaited = signum; 875 sigemptyset(&p->p_sigctx.ps_sigwait); 876 877 if (dolock) 878 wakeup_one(&p->p_sigctx.ps_sigwait); 879 else 880 sched_wakeup(&p->p_sigctx.ps_sigwait); 881 return; 882 } 883 884 /* 885 * Defer further processing for signals which are held, 886 * except that stopped processes must be continued by SIGCONT. 887 */ 888 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 889 return; 890 /* XXXSMP: works, but icky */ 891 if (dolock) 892 SCHED_LOCK(s); 893 894 /* XXXUPSXXX LWPs might go to sleep without passing signal handling */ 895 if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) { 896 /* 897 * At least one LWP is running or on a run queue. 898 * The signal will be noticed when one of them returns 899 * to userspace. 900 */ 901 signotify(p); 902 /* 903 * The signal will be noticed very soon. 904 */ 905 goto out; 906 } else { 907 /* Process is sleeping or stopped */ 908 if (p->p_flag & P_SA) { 909 struct lwp *l2 = p->p_sa->sa_vp; 910 l = NULL; 911 allsusp = 1; 912 913 if ((l2->l_stat == LSSLEEP) && (l2->l_flag & L_SINTR)) 914 l = l2; 915 else if (l2->l_stat == LSSUSPENDED) 916 suspended = l2; 917 else if ((l2->l_stat != LSZOMB) && 918 (l2->l_stat != LSDEAD)) 919 allsusp = 0; 920 } else { 921 /* 922 * Find out if any of the sleeps are interruptable, 923 * and if all the live LWPs remaining are suspended. 924 */ 925 allsusp = 1; 926 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 927 if (l->l_stat == LSSLEEP && 928 l->l_flag & L_SINTR) 929 break; 930 if (l->l_stat == LSSUSPENDED) 931 suspended = l; 932 else if ((l->l_stat != LSZOMB) && 933 (l->l_stat != LSDEAD)) 934 allsusp = 0; 935 } 936 } 937 if (p->p_stat == SACTIVE) { 938 /* All LWPs must be sleeping */ 939 KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL)); 940 941 if (l != NULL && (p->p_flag & P_TRACED)) 942 goto run; 943 944 /* 945 * If SIGCONT is default (or ignored) and process is 946 * asleep, we are finished; the process should not 947 * be awakened. 948 */ 949 if ((prop & SA_CONT) && action == SIG_DFL) { 950 sigdelset(&p->p_sigctx.ps_siglist, signum); 951 goto out; 952 } 953 954 /* 955 * When a sleeping process receives a stop 956 * signal, process immediately if possible. 957 */ 958 if ((prop & SA_STOP) && action == SIG_DFL) { 959 /* 960 * If a child holding parent blocked, 961 * stopping could cause deadlock. 962 */ 963 if (p->p_flag & P_PPWAIT) 964 goto out; 965 sigdelset(&p->p_sigctx.ps_siglist, signum); 966 p->p_xstat = signum; 967 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { 968 /* 969 * XXXSMP: recursive call; don't lock 970 * the second time around. 971 */ 972 sched_psignal(p->p_pptr, SIGCHLD); 973 } 974 proc_stop(p); /* XXXSMP: recurse? */ 975 goto out; 976 } 977 978 if (l == NULL) { 979 /* 980 * Special case: SIGKILL of a process 981 * which is entirely composed of 982 * suspended LWPs should succeed. We 983 * make this happen by unsuspending one of 984 * them. 985 */ 986 if (allsusp && (signum == SIGKILL)) 987 lwp_continue(suspended); 988 goto out; 989 } 990 /* 991 * All other (caught or default) signals 992 * cause the process to run. 993 */ 994 goto runfast; 995 /*NOTREACHED*/ 996 } else if (p->p_stat == SSTOP) { 997 /* Process is stopped */ 998 /* 999 * If traced process is already stopped, 1000 * then no further action is necessary. 1001 */ 1002 if (p->p_flag & P_TRACED) 1003 goto out; 1004 1005 /* 1006 * Kill signal always sets processes running, 1007 * if possible. 1008 */ 1009 if (signum == SIGKILL) { 1010 l = proc_unstop(p); 1011 if (l) 1012 goto runfast; 1013 goto out; 1014 } 1015 1016 if (prop & SA_CONT) { 1017 /* 1018 * If SIGCONT is default (or ignored), 1019 * we continue the process but don't 1020 * leave the signal in ps_siglist, as 1021 * it has no further action. If 1022 * SIGCONT is held, we continue the 1023 * process and leave the signal in 1024 * ps_siglist. If the process catches 1025 * SIGCONT, let it handle the signal 1026 * itself. If it isn't waiting on an 1027 * event, then it goes back to run 1028 * state. Otherwise, process goes 1029 * back to sleep state. 1030 */ 1031 if (action == SIG_DFL) 1032 sigdelset(&p->p_sigctx.ps_siglist, 1033 signum); 1034 l = proc_unstop(p); 1035 if (l && (action == SIG_CATCH)) 1036 goto runfast; 1037 goto out; 1038 } 1039 1040 if (prop & SA_STOP) { 1041 /* 1042 * Already stopped, don't need to stop again. 1043 * (If we did the shell could get confused.) 1044 */ 1045 sigdelset(&p->p_sigctx.ps_siglist, signum); 1046 goto out; 1047 } 1048 1049 /* 1050 * If a lwp is sleeping interruptibly, then 1051 * wake it up; it will run until the kernel 1052 * boundary, where it will stop in issignal(), 1053 * since p->p_stat is still SSTOP. When the 1054 * process is continued, it will be made 1055 * runnable and can look at the signal. 1056 */ 1057 if (l) 1058 goto run; 1059 goto out; 1060 } else { 1061 /* Else what? */ 1062 panic("psignal: Invalid process state %d.", 1063 p->p_stat); 1064 } 1065 } 1066 /*NOTREACHED*/ 1067 1068 runfast: 1069 /* 1070 * Raise priority to at least PUSER. 1071 */ 1072 if (l->l_priority > PUSER) 1073 l->l_priority = PUSER; 1074 run: 1075 1076 setrunnable(l); /* XXXSMP: recurse? */ 1077 out: 1078 /* XXXSMP: works, but icky */ 1079 if (dolock) 1080 SCHED_UNLOCK(s); 1081 } 1082 1083 void 1084 psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code) 1085 { 1086 struct proc *p = l->l_proc; 1087 struct lwp *le, *li; 1088 siginfo_t *si; 1089 1090 if (p->p_flag & P_SA) { 1091 1092 /* XXXUPSXXX What if not on sa_vp ? */ 1093 1094 int s = l->l_flag & L_SA; 1095 l->l_flag &= ~L_SA; 1096 si = pool_get(&siginfo_pool, PR_WAITOK); 1097 si->si_signo = sig; 1098 si->si_errno = 0; 1099 si->si_code = code; 1100 le = li = NULL; 1101 if (code) 1102 le = l; 1103 else 1104 li = l; 1105 1106 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li, 1107 sizeof(siginfo_t), si); 1108 l->l_flag |= s; 1109 return; 1110 } 1111 1112 (*p->p_emul->e_sendsig)(sig, mask, code); 1113 } 1114 1115 static __inline int firstsig(const sigset_t *); 1116 1117 static __inline int 1118 firstsig(const sigset_t *ss) 1119 { 1120 int sig; 1121 1122 sig = ffs(ss->__bits[0]); 1123 if (sig != 0) 1124 return (sig); 1125 #if NSIG > 33 1126 sig = ffs(ss->__bits[1]); 1127 if (sig != 0) 1128 return (sig + 32); 1129 #endif 1130 #if NSIG > 65 1131 sig = ffs(ss->__bits[2]); 1132 if (sig != 0) 1133 return (sig + 64); 1134 #endif 1135 #if NSIG > 97 1136 sig = ffs(ss->__bits[3]); 1137 if (sig != 0) 1138 return (sig + 96); 1139 #endif 1140 return (0); 1141 } 1142 1143 /* 1144 * If the current process has received a signal (should be caught or cause 1145 * termination, should interrupt current syscall), return the signal number. 1146 * Stop signals with default action are processed immediately, then cleared; 1147 * they aren't returned. This is checked after each entry to the system for 1148 * a syscall or trap (though this can usually be done without calling issignal 1149 * by checking the pending signal masks in the CURSIG macro.) The normal call 1150 * sequence is 1151 * 1152 * while (signum = CURSIG(curlwp)) 1153 * postsig(signum); 1154 */ 1155 int 1156 issignal(struct lwp *l) 1157 { 1158 struct proc *p = l->l_proc; 1159 int s = 0, signum, prop; 1160 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock; 1161 sigset_t ss; 1162 1163 if (l->l_flag & L_SA) { 1164 struct sadata *sa = p->p_sa; 1165 1166 /* Bail out if we do not own the virtual processor */ 1167 if (sa->sa_vp != l) 1168 return 0; 1169 } 1170 1171 if (p->p_stat == SSTOP) { 1172 /* 1173 * The process is stopped/stopping. Stop ourselves now that 1174 * we're on the kernel/userspace boundary. 1175 */ 1176 if (dolock) 1177 SCHED_LOCK(s); 1178 l->l_stat = LSSTOP; 1179 p->p_nrlwps--; 1180 if (p->p_flag & P_TRACED) 1181 goto sigtraceswitch; 1182 else 1183 goto sigswitch; 1184 } 1185 for (;;) { 1186 sigpending1(p, &ss); 1187 if (p->p_flag & P_PPWAIT) 1188 sigminusset(&stopsigmask, &ss); 1189 signum = firstsig(&ss); 1190 if (signum == 0) { /* no signal to send */ 1191 p->p_sigctx.ps_sigcheck = 0; 1192 if (locked && dolock) 1193 SCHED_LOCK(s); 1194 return (0); 1195 } 1196 /* take the signal! */ 1197 sigdelset(&p->p_sigctx.ps_siglist, signum); 1198 1199 /* 1200 * We should see pending but ignored signals 1201 * only if P_TRACED was on when they were posted. 1202 */ 1203 if (sigismember(&p->p_sigctx.ps_sigignore, signum) && 1204 (p->p_flag & P_TRACED) == 0) 1205 continue; 1206 1207 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1208 /* 1209 * If traced, always stop, and stay 1210 * stopped until released by the debugger. 1211 */ 1212 p->p_xstat = signum; 1213 if ((p->p_flag & P_FSTRACE) == 0) 1214 psignal1(p->p_pptr, SIGCHLD, dolock); 1215 if (dolock) 1216 SCHED_LOCK(s); 1217 proc_stop(p); 1218 sigtraceswitch: 1219 mi_switch(l, NULL); 1220 SCHED_ASSERT_UNLOCKED(); 1221 if (dolock) 1222 splx(s); 1223 else 1224 dolock = 1; 1225 1226 /* 1227 * If we are no longer being traced, or the parent 1228 * didn't give us a signal, look for more signals. 1229 */ 1230 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0) 1231 continue; 1232 1233 /* 1234 * If the new signal is being masked, look for other 1235 * signals. 1236 */ 1237 signum = p->p_xstat; 1238 p->p_xstat = 0; 1239 /* 1240 * `p->p_sigctx.ps_siglist |= mask' is done 1241 * in setrunnable(). 1242 */ 1243 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1244 continue; 1245 /* take the signal! */ 1246 sigdelset(&p->p_sigctx.ps_siglist, signum); 1247 } 1248 1249 prop = sigprop[signum]; 1250 1251 /* 1252 * Decide whether the signal should be returned. 1253 * Return the signal's number, or fall through 1254 * to clear it from the pending mask. 1255 */ 1256 switch ((long)SIGACTION(p, signum).sa_handler) { 1257 1258 case (long)SIG_DFL: 1259 /* 1260 * Don't take default actions on system processes. 1261 */ 1262 if (p->p_pid <= 1) { 1263 #ifdef DIAGNOSTIC 1264 /* 1265 * Are you sure you want to ignore SIGSEGV 1266 * in init? XXX 1267 */ 1268 printf("Process (pid %d) got signal %d\n", 1269 p->p_pid, signum); 1270 #endif 1271 break; /* == ignore */ 1272 } 1273 /* 1274 * If there is a pending stop signal to process 1275 * with default action, stop here, 1276 * then clear the signal. However, 1277 * if process is member of an orphaned 1278 * process group, ignore tty stop signals. 1279 */ 1280 if (prop & SA_STOP) { 1281 if (p->p_flag & P_TRACED || 1282 (p->p_pgrp->pg_jobc == 0 && 1283 prop & SA_TTYSTOP)) 1284 break; /* == ignore */ 1285 p->p_xstat = signum; 1286 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 1287 psignal1(p->p_pptr, SIGCHLD, dolock); 1288 if (dolock) 1289 SCHED_LOCK(s); 1290 proc_stop(p); 1291 sigswitch: 1292 mi_switch(l, NULL); 1293 SCHED_ASSERT_UNLOCKED(); 1294 if (dolock) 1295 splx(s); 1296 else 1297 dolock = 1; 1298 break; 1299 } else if (prop & SA_IGNORE) { 1300 /* 1301 * Except for SIGCONT, shouldn't get here. 1302 * Default action is to ignore; drop it. 1303 */ 1304 break; /* == ignore */ 1305 } else 1306 goto keep; 1307 /*NOTREACHED*/ 1308 1309 case (long)SIG_IGN: 1310 /* 1311 * Masking above should prevent us ever trying 1312 * to take action on an ignored signal other 1313 * than SIGCONT, unless process is traced. 1314 */ 1315 #ifdef DEBUG_ISSIGNAL 1316 if ((prop & SA_CONT) == 0 && 1317 (p->p_flag & P_TRACED) == 0) 1318 printf("issignal\n"); 1319 #endif 1320 break; /* == ignore */ 1321 1322 default: 1323 /* 1324 * This signal has an action, let 1325 * postsig() process it. 1326 */ 1327 goto keep; 1328 } 1329 } 1330 /* NOTREACHED */ 1331 1332 keep: 1333 /* leave the signal for later */ 1334 sigaddset(&p->p_sigctx.ps_siglist, signum); 1335 CHECKSIGS(p); 1336 if (locked && dolock) 1337 SCHED_LOCK(s); 1338 return (signum); 1339 } 1340 1341 /* 1342 * Put the argument process into the stopped state and notify the parent 1343 * via wakeup. Signals are handled elsewhere. The process must not be 1344 * on the run queue. 1345 */ 1346 static void 1347 proc_stop(struct proc *p) 1348 { 1349 struct lwp *l; 1350 1351 SCHED_ASSERT_LOCKED(); 1352 1353 /* XXX lock process LWP state */ 1354 p->p_stat = SSTOP; 1355 p->p_flag &= ~P_WAITED; 1356 1357 /* 1358 * Put as many LWP's as possible in stopped state. 1359 * Sleeping ones will notice the stopped state as they try to 1360 * return to userspace. 1361 */ 1362 1363 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1364 if ((l->l_stat == LSONPROC) && (l == curlwp)) { 1365 /* XXX SMP this assumes that a LWP that is LSONPROC 1366 * is curlwp and hence is about to be mi_switched 1367 * away; the only callers of proc_stop() are: 1368 * - psignal 1369 * - issignal() 1370 * For the former, proc_stop() is only called when 1371 * no processes are running, so we don't worry. 1372 * For the latter, proc_stop() is called right 1373 * before mi_switch(). 1374 */ 1375 l->l_stat = LSSTOP; 1376 p->p_nrlwps--; 1377 } 1378 else if ( (l->l_stat == LSSLEEP) && (l->l_flag & L_SINTR)) { 1379 setrunnable(l); 1380 } 1381 1382 /* !!!UPS!!! FIX ME */ 1383 #if 0 1384 else if (l->l_stat == LSRUN) { 1385 /* Remove LWP from the run queue */ 1386 remrunqueue(l); 1387 l->l_stat = LSSTOP; 1388 p->p_nrlwps--; 1389 } else if ((l->l_stat == LSSLEEP) || 1390 (l->l_stat == LSSUSPENDED) || 1391 (l->l_stat == LSZOMB) || 1392 (l->l_stat == LSDEAD)) { 1393 /* 1394 * Don't do anything; let sleeping LWPs 1395 * discover the stopped state of the process 1396 * on their way out of the kernel; otherwise, 1397 * things like NFS threads that sleep with 1398 * locks will block the rest of the system 1399 * from getting any work done. 1400 * 1401 * Suspended/dead/zombie LWPs aren't going 1402 * anywhere, so we don't need to touch them. 1403 */ 1404 } 1405 #ifdef DIAGNOSTIC 1406 else { 1407 panic("proc_stop: process %d lwp %d " 1408 "in unstoppable state %d.\n", 1409 p->p_pid, l->l_lid, l->l_stat); 1410 } 1411 #endif 1412 #endif 1413 } 1414 /* XXX unlock process LWP state */ 1415 1416 sched_wakeup((caddr_t)p->p_pptr); 1417 } 1418 1419 /* 1420 * Given a process in state SSTOP, set the state back to SACTIVE and 1421 * move LSSTOP'd LWPs to LSSLEEP or make them runnable. 1422 * 1423 * If no LWPs ended up runnable (and therefore able to take a signal), 1424 * return a LWP that is sleeping interruptably. The caller can wake 1425 * that LWP up to take a signal. 1426 */ 1427 struct lwp * 1428 proc_unstop(struct proc *p) 1429 { 1430 struct lwp *l, *lr = NULL; 1431 int cantake = 0; 1432 1433 SCHED_ASSERT_LOCKED(); 1434 1435 /* 1436 * Our caller wants to be informed if there are only sleeping 1437 * and interruptable LWPs left after we have run so that it 1438 * can invoke setrunnable() if required - return one of the 1439 * interruptable LWPs if this is the case. 1440 */ 1441 1442 p->p_stat = SACTIVE; 1443 if (p->p_flag & P_SA) { 1444 /* 1445 * Preferentially select the idle LWP as the interruptable 1446 * LWP to return if it exists. 1447 */ 1448 lr = p->p_sa->sa_idle; 1449 if (lr != NULL) 1450 cantake = 1; 1451 } 1452 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1453 if (l->l_stat == LSRUN) { 1454 lr = NULL; 1455 cantake = 1; 1456 } 1457 if (l->l_stat != LSSTOP) 1458 continue; 1459 1460 if (l->l_wchan != NULL) { 1461 l->l_stat = LSSLEEP; 1462 if ((cantake == 0) && (l->l_flag & L_SINTR)) { 1463 lr = l; 1464 cantake = 1; 1465 } 1466 } else { 1467 setrunnable(l); 1468 lr = NULL; 1469 cantake = 1; 1470 } 1471 } 1472 1473 return lr; 1474 } 1475 1476 /* 1477 * Take the action for the specified signal 1478 * from the current set of pending signals. 1479 */ 1480 void 1481 postsig(int signum) 1482 { 1483 struct lwp *l; 1484 struct proc *p; 1485 struct sigacts *ps; 1486 sig_t action; 1487 u_long code; 1488 sigset_t *returnmask; 1489 1490 l = curlwp; 1491 p = l->l_proc; 1492 ps = p->p_sigacts; 1493 #ifdef DIAGNOSTIC 1494 if (signum == 0) 1495 panic("postsig"); 1496 #endif 1497 1498 KERNEL_PROC_LOCK(l); 1499 1500 sigdelset(&p->p_sigctx.ps_siglist, signum); 1501 action = SIGACTION_PS(ps, signum).sa_handler; 1502 #ifdef KTRACE 1503 if (KTRPOINT(p, KTR_PSIG)) 1504 ktrpsig(p, 1505 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ? 1506 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0); 1507 #endif 1508 if (action == SIG_DFL) { 1509 /* 1510 * Default action, where the default is to kill 1511 * the process. (Other cases were ignored above.) 1512 */ 1513 sigexit(l, signum); 1514 /* NOTREACHED */ 1515 } else { 1516 /* 1517 * If we get here, the signal must be caught. 1518 */ 1519 #ifdef DIAGNOSTIC 1520 if (action == SIG_IGN || 1521 sigismember(&p->p_sigctx.ps_sigmask, signum)) 1522 panic("postsig action"); 1523 #endif 1524 /* 1525 * Set the new mask value and also defer further 1526 * occurrences of this signal. 1527 * 1528 * Special case: user has done a sigpause. Here the 1529 * current mask is not of interest, but rather the 1530 * mask from before the sigpause is what we want 1531 * restored after the signal processing is completed. 1532 */ 1533 if (p->p_sigctx.ps_flags & SAS_OLDMASK) { 1534 returnmask = &p->p_sigctx.ps_oldmask; 1535 p->p_sigctx.ps_flags &= ~SAS_OLDMASK; 1536 } else 1537 returnmask = &p->p_sigctx.ps_sigmask; 1538 p->p_stats->p_ru.ru_nsignals++; 1539 if (p->p_sigctx.ps_sig != signum) { 1540 code = 0; 1541 } else { 1542 code = p->p_sigctx.ps_code; 1543 p->p_sigctx.ps_code = 0; 1544 p->p_sigctx.ps_lwp = 0; 1545 p->p_sigctx.ps_sig = 0; 1546 } 1547 psendsig(l, signum, returnmask, code); 1548 (void) splsched(); /* XXXSMP */ 1549 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 1550 &p->p_sigctx.ps_sigmask); 1551 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 1552 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1553 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1554 sigaddset(&p->p_sigctx.ps_sigignore, signum); 1555 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 1556 } 1557 (void) spl0(); /* XXXSMP */ 1558 } 1559 1560 KERNEL_PROC_UNLOCK(l); 1561 } 1562 1563 /* 1564 * Kill the current process for stated reason. 1565 */ 1566 void 1567 killproc(struct proc *p, const char *why) 1568 { 1569 1570 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1571 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1572 psignal(p, SIGKILL); 1573 } 1574 1575 /* 1576 * Force the current process to exit with the specified signal, dumping core 1577 * if appropriate. We bypass the normal tests for masked and caught signals, 1578 * allowing unrecoverable failures to terminate the process without changing 1579 * signal state. Mark the accounting record with the signal termination. 1580 * If dumping core, save the signal number for the debugger. Calls exit and 1581 * does not return. 1582 */ 1583 1584 #if defined(DEBUG) 1585 int kern_logsigexit = 1; /* not static to make public for sysctl */ 1586 #else 1587 int kern_logsigexit = 0; /* not static to make public for sysctl */ 1588 #endif 1589 1590 static const char logcoredump[] = 1591 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 1592 static const char lognocoredump[] = 1593 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 1594 1595 /* Wrapper function for use in p_userret */ 1596 static void 1597 lwp_coredump_hook(struct lwp *l, void *arg) 1598 { 1599 int s; 1600 1601 /* 1602 * Suspend ourselves, so that the kernel stack and therefore 1603 * the userland registers saved in the trapframe are around 1604 * for coredump() to write them out. 1605 */ 1606 KERNEL_PROC_LOCK(l); 1607 l->l_flag &= ~L_DETACHED; 1608 SCHED_LOCK(s); 1609 l->l_stat = LSSUSPENDED; 1610 l->l_proc->p_nrlwps--; 1611 /* XXX NJWLWP check if this makes sense here: */ 1612 l->l_proc->p_stats->p_ru.ru_nvcsw++; 1613 mi_switch(l, NULL); 1614 SCHED_ASSERT_UNLOCKED(); 1615 splx(s); 1616 1617 lwp_exit(l); 1618 } 1619 1620 void 1621 sigexit(struct lwp *l, int signum) 1622 { 1623 struct proc *p; 1624 #if 0 1625 struct lwp *l2; 1626 #endif 1627 int error, exitsig; 1628 1629 p = l->l_proc; 1630 1631 /* 1632 * Don't permit coredump() or exit1() multiple times 1633 * in the same process. 1634 */ 1635 if (p->p_flag & P_WEXIT) { 1636 KERNEL_PROC_UNLOCK(l); 1637 (*p->p_userret)(l, p->p_userret_arg); 1638 } 1639 p->p_flag |= P_WEXIT; 1640 /* We don't want to switch away from exiting. */ 1641 /* XXX multiprocessor: stop LWPs on other processors. */ 1642 #if 0 1643 if (p->p_flag & P_SA) { 1644 LIST_FOREACH(l2, &p->p_lwps, l_sibling) 1645 l2->l_flag &= ~L_SA; 1646 p->p_flag &= ~P_SA; 1647 } 1648 #endif 1649 1650 /* Make other LWPs stick around long enough to be dumped */ 1651 p->p_userret = lwp_coredump_hook; 1652 p->p_userret_arg = NULL; 1653 1654 exitsig = signum; 1655 p->p_acflag |= AXSIG; 1656 if (sigprop[signum] & SA_CORE) { 1657 p->p_sigctx.ps_sig = signum; 1658 if ((error = coredump(l)) == 0) 1659 exitsig |= WCOREFLAG; 1660 1661 if (kern_logsigexit) { 1662 /* XXX What if we ever have really large UIDs? */ 1663 int uid = p->p_cred && p->p_ucred ? 1664 (int) p->p_ucred->cr_uid : -1; 1665 1666 if (error) 1667 log(LOG_INFO, lognocoredump, p->p_pid, 1668 p->p_comm, uid, signum, error); 1669 else 1670 log(LOG_INFO, logcoredump, p->p_pid, 1671 p->p_comm, uid, signum); 1672 } 1673 1674 } 1675 1676 exit1(l, W_EXITCODE(0, exitsig)); 1677 /* NOTREACHED */ 1678 } 1679 1680 /* 1681 * Dump core, into a file named "progname.core" or "core" (depending on the 1682 * value of shortcorename), unless the process was setuid/setgid. 1683 */ 1684 int 1685 coredump(struct lwp *l) 1686 { 1687 struct vnode *vp; 1688 struct proc *p; 1689 struct vmspace *vm; 1690 struct ucred *cred; 1691 struct nameidata nd; 1692 struct vattr vattr; 1693 int error, error1; 1694 char name[MAXPATHLEN]; 1695 1696 p = l->l_proc; 1697 vm = p->p_vmspace; 1698 cred = p->p_cred->pc_ucred; 1699 1700 /* 1701 * Make sure the process has not set-id, to prevent data leaks. 1702 */ 1703 if (p->p_flag & P_SUGID) 1704 return (EPERM); 1705 1706 /* 1707 * Refuse to core if the data + stack + user size is larger than 1708 * the core dump limit. XXX THIS IS WRONG, because of mapped 1709 * data. 1710 */ 1711 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >= 1712 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1713 return (EFBIG); /* better error code? */ 1714 1715 /* 1716 * The core dump will go in the current working directory. Make 1717 * sure that the directory is still there and that the mount flags 1718 * allow us to write core dumps there. 1719 */ 1720 vp = p->p_cwdi->cwdi_cdir; 1721 if (vp->v_mount == NULL || 1722 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) 1723 return (EPERM); 1724 1725 error = build_corename(p, name); 1726 if (error) 1727 return error; 1728 1729 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1730 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR); 1731 if (error) 1732 return (error); 1733 vp = nd.ni_vp; 1734 1735 /* Don't dump to non-regular files or files with links. */ 1736 if (vp->v_type != VREG || 1737 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1738 error = EINVAL; 1739 goto out; 1740 } 1741 VATTR_NULL(&vattr); 1742 vattr.va_size = 0; 1743 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1744 VOP_SETATTR(vp, &vattr, cred, p); 1745 p->p_acflag |= ACORE; 1746 1747 /* Now dump the actual core file. */ 1748 error = (*p->p_execsw->es_coredump)(l, vp, cred); 1749 out: 1750 VOP_UNLOCK(vp, 0); 1751 error1 = vn_close(vp, FWRITE, cred, p); 1752 if (error == 0) 1753 error = error1; 1754 return (error); 1755 } 1756 1757 /* 1758 * Nonexistent system call-- signal process (may want to handle it). 1759 * Flag error in case process won't see signal immediately (blocked or ignored). 1760 */ 1761 /* ARGSUSED */ 1762 int 1763 sys_nosys(struct lwp *l, void *v, register_t *retval) 1764 { 1765 struct proc *p; 1766 1767 p = l->l_proc; 1768 psignal(p, SIGSYS); 1769 return (ENOSYS); 1770 } 1771 1772 static int 1773 build_corename(struct proc *p, char dst[MAXPATHLEN]) 1774 { 1775 const char *s; 1776 char *d, *end; 1777 int i; 1778 1779 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN; 1780 *s != '\0'; s++) { 1781 if (*s == '%') { 1782 switch (*(s + 1)) { 1783 case 'n': 1784 i = snprintf(d, end - d, "%s", p->p_comm); 1785 break; 1786 case 'p': 1787 i = snprintf(d, end - d, "%d", p->p_pid); 1788 break; 1789 case 'u': 1790 i = snprintf(d, end - d, "%.*s", 1791 (int)sizeof p->p_pgrp->pg_session->s_login, 1792 p->p_pgrp->pg_session->s_login); 1793 break; 1794 case 't': 1795 i = snprintf(d, end - d, "%ld", 1796 p->p_stats->p_start.tv_sec); 1797 break; 1798 default: 1799 goto copy; 1800 } 1801 d += i; 1802 s++; 1803 } else { 1804 copy: *d = *s; 1805 d++; 1806 } 1807 if (d >= end) 1808 return (ENAMETOOLONG); 1809 } 1810 *d = '\0'; 1811 return 0; 1812 } 1813 1814 void 1815 getucontext(struct lwp *l, ucontext_t *ucp) 1816 { 1817 struct proc *p; 1818 1819 p = l->l_proc; 1820 1821 ucp->uc_flags = 0; 1822 ucp->uc_link = l->l_ctxlink; 1823 1824 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask); 1825 ucp->uc_flags |= _UC_SIGMASK; 1826 1827 /* 1828 * The (unsupplied) definition of the `current execution stack' 1829 * in the System V Interface Definition appears to allow returning 1830 * the main context stack. 1831 */ 1832 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) { 1833 ucp->uc_stack.ss_sp = (void *)USRSTACK; 1834 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize); 1835 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 1836 } else { 1837 /* Simply copy alternate signal execution stack. */ 1838 ucp->uc_stack = p->p_sigctx.ps_sigstk; 1839 } 1840 ucp->uc_flags |= _UC_STACK; 1841 1842 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 1843 } 1844 1845 /* ARGSUSED */ 1846 int 1847 sys_getcontext(struct lwp *l, void *v, register_t *retval) 1848 { 1849 struct sys_getcontext_args /* { 1850 syscallarg(struct __ucontext *) ucp; 1851 } */ *uap = v; 1852 ucontext_t uc; 1853 1854 getucontext(l, &uc); 1855 1856 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)))); 1857 } 1858 1859 int 1860 setucontext(struct lwp *l, const ucontext_t *ucp) 1861 { 1862 struct proc *p; 1863 int error; 1864 1865 p = l->l_proc; 1866 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0) 1867 return (error); 1868 l->l_ctxlink = ucp->uc_link; 1869 /* 1870 * We might want to take care of the stack portion here but currently 1871 * don't; see the comment in getucontext(). 1872 */ 1873 if ((ucp->uc_flags & _UC_SIGMASK) != 0) 1874 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL); 1875 1876 return 0; 1877 } 1878 1879 /* ARGSUSED */ 1880 int 1881 sys_setcontext(struct lwp *l, void *v, register_t *retval) 1882 { 1883 struct sys_setcontext_args /* { 1884 syscallarg(const ucontext_t *) ucp; 1885 } */ *uap = v; 1886 ucontext_t uc; 1887 int error; 1888 1889 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */ 1890 exit1(l, W_EXITCODE(0, 0)); 1891 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 || 1892 (error = setucontext(l, &uc)) != 0) 1893 return (error); 1894 1895 return (EJUSTRETURN); 1896 } 1897 1898 /* 1899 * sigtimedwait(2) system call, used also for implementation 1900 * of sigwaitinfo() and sigwait(). 1901 * 1902 * This only handles single LWP in signal wait. libpthread provides 1903 * it's own sigtimedwait() wrapper to DTRT WRT individual threads. 1904 * 1905 * XXX no support for queued signals, si_code is always SI_USER. 1906 */ 1907 int 1908 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval) 1909 { 1910 struct sys___sigtimedwait_args /* { 1911 syscallarg(const sigset_t *) set; 1912 syscallarg(siginfo_t *) info; 1913 syscallarg(struct timespec *) timeout; 1914 } */ *uap = v; 1915 sigset_t waitset, twaitset; 1916 struct proc *p = l->l_proc; 1917 int error, signum, s; 1918 int timo = 0; 1919 struct timeval tvstart; 1920 struct timespec ts; 1921 1922 if ((error = copyin(SCARG(uap, set), &waitset, sizeof(waitset)))) 1923 return (error); 1924 1925 /* 1926 * Silently ignore SA_CANTMASK signals. psignal1() would 1927 * ignore SA_CANTMASK signals in waitset, we do this 1928 * only for the below siglist check. 1929 */ 1930 sigminusset(&sigcantmask, &waitset); 1931 1932 /* 1933 * First scan siglist and check if there is signal from 1934 * our waitset already pending. 1935 */ 1936 twaitset = waitset; 1937 __sigandset(&p->p_sigctx.ps_siglist, &twaitset); 1938 if ((signum = firstsig(&twaitset))) { 1939 /* found pending signal */ 1940 sigdelset(&p->p_sigctx.ps_siglist, signum); 1941 goto sig; 1942 } 1943 1944 /* 1945 * Calculate timeout, if it was specified. 1946 */ 1947 if (SCARG(uap, timeout)) { 1948 uint64_t ms; 1949 1950 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts)))) 1951 return (error); 1952 1953 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000); 1954 timo = mstohz(ms); 1955 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0) 1956 timo = 1; 1957 if (timo <= 0) 1958 return (EAGAIN); 1959 1960 /* 1961 * Remember current mono_time, it would be used in 1962 * ECANCELED/ERESTART case. 1963 */ 1964 s = splclock(); 1965 tvstart = mono_time; 1966 splx(s); 1967 } 1968 1969 /* 1970 * Setup ps_sigwait list. 1971 */ 1972 p->p_sigctx.ps_sigwaited = -1; 1973 p->p_sigctx.ps_sigwait = waitset; 1974 1975 /* 1976 * Wait for signal to arrive. We can either be woken up or 1977 * time out. 1978 */ 1979 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo); 1980 1981 /* 1982 * Check if a signal from our wait set has arrived, or if it 1983 * was mere wakeup. 1984 */ 1985 if (!error) { 1986 if ((signum = p->p_sigctx.ps_sigwaited) <= 0) { 1987 /* wakeup via _lwp_wakeup() */ 1988 error = ECANCELED; 1989 } 1990 } 1991 1992 /* 1993 * On error, clear sigwait indication. psignal1() sets it 1994 * in !error case. 1995 */ 1996 if (error) { 1997 p->p_sigctx.ps_sigwaited = 0; 1998 1999 /* 2000 * If the sleep was interrupted (either by signal or wakeup), 2001 * update the timeout and copyout new value back. 2002 * It would be used when the syscall would be restarted 2003 * or called again. 2004 */ 2005 if (timo && (error == ERESTART || error == ECANCELED)) { 2006 struct timeval tvnow, tvtimo; 2007 int err; 2008 2009 s = splclock(); 2010 tvnow = mono_time; 2011 splx(s); 2012 2013 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts); 2014 2015 /* compute how much time has passed since start */ 2016 timersub(&tvnow, &tvstart, &tvnow); 2017 /* substract passed time from timeout */ 2018 timersub(&tvtimo, &tvnow, &tvtimo); 2019 2020 if (tvtimo.tv_sec < 0) 2021 return (EAGAIN); 2022 2023 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts); 2024 2025 /* copy updated timeout to userland */ 2026 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) 2027 return (err); 2028 } 2029 2030 return (error); 2031 } 2032 2033 /* 2034 * If a signal from the wait set arrived, copy it to userland. 2035 * XXX no queued signals for now 2036 */ 2037 if (signum > 0) { 2038 siginfo_t si; 2039 2040 sig: 2041 memset(&si, 0, sizeof(si)); 2042 si.si_signo = signum; 2043 si.si_code = SI_USER; 2044 2045 error = copyout(&si, SCARG(uap, info), sizeof(si)); 2046 if (error) 2047 return (error); 2048 } 2049 2050 return (0); 2051 } 2052 2053 /* 2054 * Returns true if signal is ignored or masked for passed process. 2055 */ 2056 int 2057 sigismasked(struct proc *p, int sig) 2058 { 2059 2060 return (sigismember(&p->p_sigctx.ps_sigignore, sig) || 2061 sigismember(&p->p_sigctx.ps_sigmask, sig)); 2062 } 2063 2064 static int 2065 filt_sigattach(struct knote *kn) 2066 { 2067 struct proc *p = curproc; 2068 2069 kn->kn_ptr.p_proc = p; 2070 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2071 2072 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2073 2074 return (0); 2075 } 2076 2077 static void 2078 filt_sigdetach(struct knote *kn) 2079 { 2080 struct proc *p = kn->kn_ptr.p_proc; 2081 2082 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2083 } 2084 2085 /* 2086 * signal knotes are shared with proc knotes, so we apply a mask to 2087 * the hint in order to differentiate them from process hints. This 2088 * could be avoided by using a signal-specific knote list, but probably 2089 * isn't worth the trouble. 2090 */ 2091 static int 2092 filt_signal(struct knote *kn, long hint) 2093 { 2094 2095 if (hint & NOTE_SIGNAL) { 2096 hint &= ~NOTE_SIGNAL; 2097 2098 if (kn->kn_id == hint) 2099 kn->kn_data++; 2100 } 2101 return (kn->kn_data != 0); 2102 } 2103 2104 const struct filterops sig_filtops = { 2105 0, filt_sigattach, filt_sigdetach, filt_signal 2106 }; 2107