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