1 /* $NetBSD: kern_sig.c,v 1.55 1996/09/01 01:56:10 mycroft 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.7 (Berkeley) 4/18/94 41 */ 42 43 #define SIGPROP /* include signal properties table */ 44 #include <sys/param.h> 45 #include <sys/signalvar.h> 46 #include <sys/resourcevar.h> 47 #include <sys/namei.h> 48 #include <sys/vnode.h> 49 #include <sys/proc.h> 50 #include <sys/systm.h> 51 #include <sys/timeb.h> 52 #include <sys/times.h> 53 #include <sys/buf.h> 54 #include <sys/acct.h> 55 #include <sys/file.h> 56 #include <sys/kernel.h> 57 #include <sys/wait.h> 58 #include <sys/ktrace.h> 59 #include <sys/syslog.h> 60 #include <sys/stat.h> 61 #include <sys/core.h> 62 #include <sys/ptrace.h> 63 64 #include <sys/mount.h> 65 #include <sys/syscallargs.h> 66 67 #include <machine/cpu.h> 68 69 #include <vm/vm.h> 70 #include <sys/user.h> /* for coredump */ 71 72 void stop __P((struct proc *p)); 73 void killproc __P((struct proc *, char *)); 74 75 /* 76 * Can process p, with pcred pc, send the signal signum to process q? 77 */ 78 #define CANSIGNAL(p, pc, q, signum) \ 79 ((pc)->pc_ucred->cr_uid == 0 || \ 80 (pc)->p_ruid == (q)->p_cred->p_ruid || \ 81 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \ 82 (pc)->p_ruid == (q)->p_ucred->cr_uid || \ 83 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \ 84 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)) 85 86 /* ARGSUSED */ 87 int 88 sys_sigaction(p, v, retval) 89 struct proc *p; 90 void *v; 91 register_t *retval; 92 { 93 register struct sys_sigaction_args /* { 94 syscallarg(int) signum; 95 syscallarg(struct sigaction *) nsa; 96 syscallarg(struct sigaction *) osa; 97 } */ *uap = v; 98 struct sigaction vec; 99 register struct sigaction *sa; 100 register struct sigacts *ps = p->p_sigacts; 101 register int signum; 102 int bit, error; 103 104 signum = SCARG(uap, signum); 105 if (signum <= 0 || signum >= NSIG || 106 signum == SIGKILL || signum == SIGSTOP) 107 return (EINVAL); 108 sa = &vec; 109 if (SCARG(uap, osa)) { 110 sa->sa_handler = ps->ps_sigact[signum]; 111 sa->sa_mask = ps->ps_catchmask[signum]; 112 bit = sigmask(signum); 113 sa->sa_flags = 0; 114 if ((ps->ps_sigonstack & bit) != 0) 115 sa->sa_flags |= SA_ONSTACK; 116 if ((ps->ps_sigintr & bit) == 0) 117 sa->sa_flags |= SA_RESTART; 118 if ((ps->ps_sigreset & bit) != 0) 119 sa->sa_flags |= SA_RESETHAND; 120 if (signum == SIGCHLD) { 121 if ((p->p_flag & P_NOCLDSTOP) != 0) 122 sa->sa_flags |= SA_NOCLDSTOP; 123 } 124 if ((sa->sa_mask & bit) == 0) 125 sa->sa_flags |= SA_NODEFER; 126 sa->sa_mask &= ~bit; 127 error = copyout((caddr_t)sa, (caddr_t)SCARG(uap, osa), 128 sizeof (vec)); 129 if (error) 130 return (error); 131 } 132 if (SCARG(uap, nsa)) { 133 error = copyin((caddr_t)SCARG(uap, nsa), (caddr_t)sa, 134 sizeof (vec)); 135 if (error) 136 return (error); 137 setsigvec(p, signum, sa); 138 } 139 return (0); 140 } 141 142 void 143 setsigvec(p, signum, sa) 144 register struct proc *p; 145 int signum; 146 register struct sigaction *sa; 147 { 148 register struct sigacts *ps = p->p_sigacts; 149 register int bit; 150 151 bit = sigmask(signum); 152 /* 153 * Change setting atomically. 154 */ 155 (void) splhigh(); 156 ps->ps_sigact[signum] = sa->sa_handler; 157 if ((sa->sa_flags & SA_NODEFER) == 0) 158 sa->sa_mask |= sigmask(signum); 159 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask; 160 if (signum == SIGCHLD) { 161 if (sa->sa_flags & SA_NOCLDSTOP) 162 p->p_flag |= P_NOCLDSTOP; 163 else 164 p->p_flag &= ~P_NOCLDSTOP; 165 } 166 if ((sa->sa_flags & SA_RESETHAND) != 0) 167 ps->ps_sigreset |= bit; 168 else 169 ps->ps_sigreset &= ~bit; 170 if ((sa->sa_flags & SA_RESTART) == 0) 171 ps->ps_sigintr |= bit; 172 else 173 ps->ps_sigintr &= ~bit; 174 if ((sa->sa_flags & SA_ONSTACK) != 0) 175 ps->ps_sigonstack |= bit; 176 else 177 ps->ps_sigonstack &= ~bit; 178 #ifdef COMPAT_SUNOS 179 { 180 extern struct emul emul_sunos; 181 if (p->p_emul == &emul_sunos && sa->sa_flags & SA_USERTRAMP) 182 ps->ps_usertramp |= bit; 183 else 184 ps->ps_usertramp &= ~bit; 185 } 186 #endif 187 /* 188 * Set bit in p_sigignore for signals that are set to SIG_IGN, 189 * and for signals set to SIG_DFL where the default is to ignore. 190 * However, don't put SIGCONT in p_sigignore, 191 * as we have to restart the process. 192 */ 193 if (sa->sa_handler == SIG_IGN || 194 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) { 195 p->p_siglist &= ~bit; /* never to be seen again */ 196 if (signum != SIGCONT) 197 p->p_sigignore |= bit; /* easier in psignal */ 198 p->p_sigcatch &= ~bit; 199 } else { 200 p->p_sigignore &= ~bit; 201 if (sa->sa_handler == SIG_DFL) 202 p->p_sigcatch &= ~bit; 203 else 204 p->p_sigcatch |= bit; 205 } 206 (void) spl0(); 207 } 208 209 /* 210 * Initialize signal state for process 0; 211 * set to ignore signals that are ignored by default. 212 */ 213 void 214 siginit(p) 215 struct proc *p; 216 { 217 register int i; 218 219 for (i = 0; i < NSIG; i++) 220 if (sigprop[i] & SA_IGNORE && i != SIGCONT) 221 p->p_sigignore |= sigmask(i); 222 } 223 224 /* 225 * Reset signals for an exec of the specified process. 226 */ 227 void 228 execsigs(p) 229 register struct proc *p; 230 { 231 register struct sigacts *ps = p->p_sigacts; 232 register int nc, mask; 233 234 /* 235 * Reset caught signals. Held signals remain held 236 * through p_sigmask (unless they were caught, 237 * and are now ignored by default). 238 */ 239 while (p->p_sigcatch) { 240 nc = ffs((long)p->p_sigcatch); 241 mask = sigmask(nc); 242 p->p_sigcatch &= ~mask; 243 if (sigprop[nc] & SA_IGNORE) { 244 if (nc != SIGCONT) 245 p->p_sigignore |= mask; 246 p->p_siglist &= ~mask; 247 } 248 ps->ps_sigact[nc] = SIG_DFL; 249 } 250 /* 251 * Reset stack state to the user stack. 252 * Clear set of signals caught on the signal stack. 253 */ 254 ps->ps_sigstk.ss_flags = SS_DISABLE; 255 ps->ps_sigstk.ss_size = 0; 256 ps->ps_sigstk.ss_sp = 0; 257 ps->ps_flags = 0; 258 } 259 260 /* 261 * Manipulate signal mask. 262 * Note that we receive new mask, not pointer, 263 * and return old mask as return value; 264 * the library stub does the rest. 265 */ 266 int 267 sys_sigprocmask(p, v, retval) 268 register struct proc *p; 269 void *v; 270 register_t *retval; 271 { 272 struct sys_sigprocmask_args /* { 273 syscallarg(int) how; 274 syscallarg(sigset_t) mask; 275 } */ *uap = v; 276 int error = 0; 277 278 *retval = p->p_sigmask; 279 (void) splhigh(); 280 281 switch (SCARG(uap, how)) { 282 case SIG_BLOCK: 283 p->p_sigmask |= SCARG(uap, mask) &~ sigcantmask; 284 break; 285 286 case SIG_UNBLOCK: 287 p->p_sigmask &= ~SCARG(uap, mask); 288 break; 289 290 case SIG_SETMASK: 291 p->p_sigmask = SCARG(uap, mask) &~ sigcantmask; 292 break; 293 294 default: 295 error = EINVAL; 296 break; 297 } 298 (void) spl0(); 299 return (error); 300 } 301 302 /* ARGSUSED */ 303 int 304 sys_sigpending(p, v, retval) 305 struct proc *p; 306 void *v; 307 register_t *retval; 308 { 309 310 *retval = p->p_siglist; 311 return (0); 312 } 313 314 /* 315 * Suspend process until signal, providing mask to be set 316 * in the meantime. Note nonstandard calling convention: 317 * libc stub passes mask, not pointer, to save a copyin. 318 */ 319 /* ARGSUSED */ 320 int 321 sys_sigsuspend(p, v, retval) 322 register struct proc *p; 323 void *v; 324 register_t *retval; 325 { 326 struct sys_sigsuspend_args /* { 327 syscallarg(int) mask; 328 } */ *uap = v; 329 register struct sigacts *ps = p->p_sigacts; 330 331 /* 332 * When returning from sigpause, we want 333 * the old mask to be restored after the 334 * signal handler has finished. Thus, we 335 * save it here and mark the sigacts structure 336 * to indicate this. 337 */ 338 ps->ps_oldmask = p->p_sigmask; 339 ps->ps_flags |= SAS_OLDMASK; 340 p->p_sigmask = SCARG(uap, mask) &~ sigcantmask; 341 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 342 /* void */; 343 /* always return EINTR rather than ERESTART... */ 344 return (EINTR); 345 } 346 347 /* ARGSUSED */ 348 int 349 sys_sigaltstack(p, v, retval) 350 struct proc *p; 351 void *v; 352 register_t *retval; 353 { 354 register struct sys_sigaltstack_args /* { 355 syscallarg(struct sigaltstack *) nss; 356 syscallarg(struct sigaltstack *) oss; 357 } */ *uap = v; 358 struct sigacts *psp; 359 struct sigaltstack ss; 360 int error; 361 362 psp = p->p_sigacts; 363 if ((psp->ps_flags & SAS_ALTSTACK) == 0) 364 psp->ps_sigstk.ss_flags |= SS_DISABLE; 365 if (SCARG(uap, oss) && (error = copyout((caddr_t)&psp->ps_sigstk, 366 (caddr_t)SCARG(uap, oss), sizeof (struct sigaltstack)))) 367 return (error); 368 if (SCARG(uap, nss) == 0) 369 return (0); 370 error = copyin((caddr_t)SCARG(uap, nss), (caddr_t)&ss, sizeof (ss)); 371 if (error) 372 return (error); 373 if (ss.ss_flags & SS_DISABLE) { 374 if (psp->ps_sigstk.ss_flags & SS_ONSTACK) 375 return (EINVAL); 376 psp->ps_flags &= ~SAS_ALTSTACK; 377 psp->ps_sigstk.ss_flags = ss.ss_flags; 378 return (0); 379 } 380 if (ss.ss_size < MINSIGSTKSZ) 381 return (ENOMEM); 382 psp->ps_flags |= SAS_ALTSTACK; 383 psp->ps_sigstk= ss; 384 return (0); 385 } 386 387 /* ARGSUSED */ 388 int 389 sys_kill(cp, v, retval) 390 register struct proc *cp; 391 void *v; 392 register_t *retval; 393 { 394 register struct sys_kill_args /* { 395 syscallarg(int) pid; 396 syscallarg(int) signum; 397 } */ *uap = v; 398 register struct proc *p; 399 register struct pcred *pc = cp->p_cred; 400 401 #ifdef COMPAT_09 402 SCARG(uap, pid) = (short) SCARG(uap, pid); 403 #endif 404 405 if ((u_int)SCARG(uap, signum) >= NSIG) 406 return (EINVAL); 407 if (SCARG(uap, pid) > 0) { 408 /* kill single process */ 409 if ((p = pfind(SCARG(uap, pid))) == NULL) 410 return (ESRCH); 411 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum))) 412 return (EPERM); 413 if (SCARG(uap, signum)) 414 psignal(p, SCARG(uap, signum)); 415 return (0); 416 } 417 switch (SCARG(uap, pid)) { 418 case -1: /* broadcast signal */ 419 return (killpg1(cp, SCARG(uap, signum), 0, 1)); 420 case 0: /* signal own process group */ 421 return (killpg1(cp, SCARG(uap, signum), 0, 0)); 422 default: /* negative explicit process group */ 423 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0)); 424 } 425 /* NOTREACHED */ 426 } 427 428 /* 429 * Common code for kill process group/broadcast kill. 430 * cp is calling process. 431 */ 432 int 433 killpg1(cp, signum, pgid, all) 434 register struct proc *cp; 435 int signum, pgid, all; 436 { 437 register struct proc *p; 438 register struct pcred *pc = cp->p_cred; 439 struct pgrp *pgrp; 440 int nfound = 0; 441 442 if (all) 443 /* 444 * broadcast 445 */ 446 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 447 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 448 p == cp || !CANSIGNAL(cp, pc, p, signum)) 449 continue; 450 nfound++; 451 if (signum) 452 psignal(p, signum); 453 } 454 else { 455 if (pgid == 0) 456 /* 457 * zero pgid means send to my process group. 458 */ 459 pgrp = cp->p_pgrp; 460 else { 461 pgrp = pgfind(pgid); 462 if (pgrp == NULL) 463 return (ESRCH); 464 } 465 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 466 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 467 p->p_stat == SZOMB || 468 !CANSIGNAL(cp, pc, p, signum)) 469 continue; 470 nfound++; 471 if (signum) 472 psignal(p, signum); 473 } 474 } 475 return (nfound ? 0 : ESRCH); 476 } 477 478 /* 479 * Send a signal to a process group. 480 */ 481 void 482 gsignal(pgid, signum) 483 int pgid, signum; 484 { 485 struct pgrp *pgrp; 486 487 if (pgid && (pgrp = pgfind(pgid))) 488 pgsignal(pgrp, signum, 0); 489 } 490 491 /* 492 * Send a signal to a process group. If checktty is 1, 493 * limit to members which have a controlling terminal. 494 */ 495 void 496 pgsignal(pgrp, signum, checkctty) 497 struct pgrp *pgrp; 498 int signum, checkctty; 499 { 500 register struct proc *p; 501 502 if (pgrp) 503 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) 504 if (checkctty == 0 || p->p_flag & P_CONTROLT) 505 psignal(p, signum); 506 } 507 508 /* 509 * Send a signal caused by a trap to the current process. 510 * If it will be caught immediately, deliver it with correct code. 511 * Otherwise, post it normally. 512 */ 513 void 514 trapsignal(p, signum, code) 515 struct proc *p; 516 register int signum; 517 u_long code; 518 { 519 register struct sigacts *ps = p->p_sigacts; 520 int mask; 521 522 mask = sigmask(signum); 523 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 && 524 (p->p_sigmask & mask) == 0) { 525 p->p_stats->p_ru.ru_nsignals++; 526 #ifdef KTRACE 527 if (KTRPOINT(p, KTR_PSIG)) 528 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum], 529 p->p_sigmask, code); 530 #endif 531 (*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum, 532 p->p_sigmask, code); 533 p->p_sigmask |= ps->ps_catchmask[signum]; 534 if ((ps->ps_sigreset & mask) != 0) { 535 p->p_sigcatch &= ~mask; 536 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 537 p->p_sigignore |= mask; 538 ps->ps_sigact[signum] = SIG_DFL; 539 } 540 } else { 541 ps->ps_code = code; /* XXX for core dump/debugger */ 542 psignal(p, signum); 543 } 544 } 545 546 /* 547 * Send the signal to the process. If the signal has an action, the action 548 * is usually performed by the target process rather than the caller; we add 549 * the signal to the set of pending signals for the process. 550 * 551 * Exceptions: 552 * o When a stop signal is sent to a sleeping process that takes the 553 * default action, the process is stopped without awakening it. 554 * o SIGCONT restarts stopped processes (or puts them back to sleep) 555 * regardless of the signal action (eg, blocked or ignored). 556 * 557 * Other ignored signals are discarded immediately. 558 */ 559 void 560 psignal(p, signum) 561 register struct proc *p; 562 register int signum; 563 { 564 register int s, prop; 565 register sig_t action; 566 int mask; 567 568 if ((u_int)signum >= NSIG || signum == 0) 569 panic("psignal signal number"); 570 mask = sigmask(signum); 571 prop = sigprop[signum]; 572 573 /* 574 * If proc is traced, always give parent a chance. 575 */ 576 if (p->p_flag & P_TRACED) 577 action = SIG_DFL; 578 else { 579 /* 580 * If the signal is being ignored, 581 * then we forget about it immediately. 582 * (Note: we don't set SIGCONT in p_sigignore, 583 * and if it is set to SIG_IGN, 584 * action will be SIG_DFL here.) 585 */ 586 if (p->p_sigignore & mask) 587 return; 588 if (p->p_sigmask & mask) 589 action = SIG_HOLD; 590 else if (p->p_sigcatch & mask) 591 action = SIG_CATCH; 592 else { 593 action = SIG_DFL; 594 595 if (prop & SA_KILL && p->p_nice > NZERO) 596 p->p_nice = NZERO; 597 598 /* 599 * If sending a tty stop signal to a member of an 600 * orphaned process group, discard the signal here if 601 * the action is default; don't stop the process below 602 * if sleeping, and don't clear any pending SIGCONT. 603 */ 604 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 605 return; 606 } 607 } 608 609 if (prop & SA_CONT) 610 p->p_siglist &= ~stopsigmask; 611 612 if (prop & SA_STOP) 613 p->p_siglist &= ~contsigmask; 614 615 p->p_siglist |= mask; 616 617 /* 618 * Defer further processing for signals which are held, 619 * except that stopped processes must be continued by SIGCONT. 620 */ 621 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 622 return; 623 s = splhigh(); 624 switch (p->p_stat) { 625 626 case SSLEEP: 627 /* 628 * If process is sleeping uninterruptibly 629 * we can't interrupt the sleep... the signal will 630 * be noticed when the process returns through 631 * trap() or syscall(). 632 */ 633 if ((p->p_flag & P_SINTR) == 0) 634 goto out; 635 /* 636 * Process is sleeping and traced... make it runnable 637 * so it can discover the signal in issignal() and stop 638 * for the parent. 639 */ 640 if (p->p_flag & P_TRACED) 641 goto run; 642 /* 643 * If SIGCONT is default (or ignored) and process is 644 * asleep, we are finished; the process should not 645 * be awakened. 646 */ 647 if ((prop & SA_CONT) && action == SIG_DFL) { 648 p->p_siglist &= ~mask; 649 goto out; 650 } 651 /* 652 * When a sleeping process receives a stop 653 * signal, process immediately if possible. 654 */ 655 if ((prop & SA_STOP) && action == SIG_DFL) { 656 /* 657 * If a child holding parent blocked, 658 * stopping could cause deadlock. 659 */ 660 if (p->p_flag & P_PPWAIT) 661 goto out; 662 p->p_siglist &= ~mask; 663 p->p_xstat = signum; 664 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 665 psignal(p->p_pptr, SIGCHLD); 666 stop(p); 667 goto out; 668 } 669 /* 670 * All other (caught or default) signals 671 * cause the process to run. 672 */ 673 goto runfast; 674 /*NOTREACHED*/ 675 676 case SSTOP: 677 /* 678 * If traced process is already stopped, 679 * then no further action is necessary. 680 */ 681 if (p->p_flag & P_TRACED) 682 goto out; 683 684 /* 685 * Kill signal always sets processes running. 686 */ 687 if (signum == SIGKILL) 688 goto runfast; 689 690 if (prop & SA_CONT) { 691 /* 692 * If SIGCONT is default (or ignored), we continue the 693 * process but don't leave the signal in p_siglist, as 694 * it has no further action. If SIGCONT is held, we 695 * continue the process and leave the signal in 696 * p_siglist. If the process catches SIGCONT, let it 697 * handle the signal itself. If it isn't waiting on 698 * an event, then it goes back to run state. 699 * Otherwise, process goes back to sleep state. 700 */ 701 if (action == SIG_DFL) 702 p->p_siglist &= ~mask; 703 if (action == SIG_CATCH) 704 goto runfast; 705 if (p->p_wchan == 0) 706 goto run; 707 p->p_stat = SSLEEP; 708 goto out; 709 } 710 711 if (prop & SA_STOP) { 712 /* 713 * Already stopped, don't need to stop again. 714 * (If we did the shell could get confused.) 715 */ 716 p->p_siglist &= ~mask; /* take it away */ 717 goto out; 718 } 719 720 /* 721 * If process is sleeping interruptibly, then simulate a 722 * wakeup so that when it is continued, it will be made 723 * runnable and can look at the signal. But don't make 724 * the process runnable, leave it stopped. 725 */ 726 if (p->p_wchan && p->p_flag & P_SINTR) 727 unsleep(p); 728 goto out; 729 730 default: 731 /* 732 * SRUN, SIDL, SZOMB do nothing with the signal, 733 * other than kicking ourselves if we are running. 734 * It will either never be noticed, or noticed very soon. 735 */ 736 if (p == curproc) 737 signotify(p); 738 goto out; 739 } 740 /*NOTREACHED*/ 741 742 runfast: 743 /* 744 * Raise priority to at least PUSER. 745 */ 746 if (p->p_priority > PUSER) 747 p->p_priority = PUSER; 748 run: 749 setrunnable(p); 750 out: 751 splx(s); 752 } 753 754 /* 755 * If the current process has received a signal (should be caught or cause 756 * termination, should interrupt current syscall), return the signal number. 757 * Stop signals with default action are processed immediately, then cleared; 758 * they aren't returned. This is checked after each entry to the system for 759 * a syscall or trap (though this can usually be done without calling issignal 760 * by checking the pending signal masks in the CURSIG macro.) The normal call 761 * sequence is 762 * 763 * while (signum = CURSIG(curproc)) 764 * postsig(signum); 765 */ 766 int 767 issignal(p) 768 register struct proc *p; 769 { 770 register int signum, mask, prop; 771 772 for (;;) { 773 mask = p->p_siglist & ~p->p_sigmask; 774 if (p->p_flag & P_PPWAIT) 775 mask &= ~stopsigmask; 776 if (mask == 0) /* no signal to send */ 777 return (0); 778 signum = ffs((long)mask); 779 mask = sigmask(signum); 780 p->p_siglist &= ~mask; /* take the signal! */ 781 782 /* 783 * We should see pending but ignored signals 784 * only if P_TRACED was on when they were posted. 785 */ 786 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) 787 continue; 788 789 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 790 /* 791 * If traced, always stop, and stay 792 * stopped until released by the debugger. 793 */ 794 p->p_xstat = signum; 795 796 if (p->p_flag & P_FSTRACE) { 797 #ifdef PROCFS 798 /* procfs debugging */ 799 p->p_stat = SSTOP; 800 wakeup((caddr_t)p); 801 mi_switch(); 802 #else 803 panic("procfs debugging"); 804 #endif 805 } else { 806 /* ptrace debugging */ 807 psignal(p->p_pptr, SIGCHLD); 808 do { 809 stop(p); 810 mi_switch(); 811 } while (!trace_req(p) && p->p_flag & P_TRACED); 812 } 813 814 /* 815 * If we are no longer being traced, or the parent 816 * didn't give us a signal, look for more signals. 817 */ 818 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0) 819 continue; 820 821 /* 822 * If the new signal is being masked, look for other 823 * signals. 824 */ 825 signum = p->p_xstat; 826 mask = sigmask(signum); 827 if ((p->p_sigmask & mask) != 0) 828 continue; 829 p->p_siglist &= ~mask; /* take the signal! */ 830 } 831 832 prop = sigprop[signum]; 833 834 /* 835 * Decide whether the signal should be returned. 836 * Return the signal's number, or fall through 837 * to clear it from the pending mask. 838 */ 839 switch ((long)p->p_sigacts->ps_sigact[signum]) { 840 841 case (long)SIG_DFL: 842 /* 843 * Don't take default actions on system processes. 844 */ 845 if (p->p_pid <= 1) { 846 #ifdef DIAGNOSTIC 847 /* 848 * Are you sure you want to ignore SIGSEGV 849 * in init? XXX 850 */ 851 printf("Process (pid %d) got signal %d\n", 852 p->p_pid, signum); 853 #endif 854 break; /* == ignore */ 855 } 856 /* 857 * If there is a pending stop signal to process 858 * with default action, stop here, 859 * then clear the signal. However, 860 * if process is member of an orphaned 861 * process group, ignore tty stop signals. 862 */ 863 if (prop & SA_STOP) { 864 if (p->p_flag & P_TRACED || 865 (p->p_pgrp->pg_jobc == 0 && 866 prop & SA_TTYSTOP)) 867 break; /* == ignore */ 868 p->p_xstat = signum; 869 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 870 psignal(p->p_pptr, SIGCHLD); 871 stop(p); 872 mi_switch(); 873 break; 874 } else if (prop & SA_IGNORE) { 875 /* 876 * Except for SIGCONT, shouldn't get here. 877 * Default action is to ignore; drop it. 878 */ 879 break; /* == ignore */ 880 } else 881 goto keep; 882 /*NOTREACHED*/ 883 884 case (long)SIG_IGN: 885 /* 886 * Masking above should prevent us ever trying 887 * to take action on an ignored signal other 888 * than SIGCONT, unless process is traced. 889 */ 890 if ((prop & SA_CONT) == 0 && 891 (p->p_flag & P_TRACED) == 0) 892 printf("issignal\n"); 893 break; /* == ignore */ 894 895 default: 896 /* 897 * This signal has an action, let 898 * postsig() process it. 899 */ 900 goto keep; 901 } 902 } 903 /* NOTREACHED */ 904 905 keep: 906 p->p_siglist |= mask; /* leave the signal for later */ 907 return (signum); 908 } 909 910 /* 911 * Put the argument process into the stopped state and notify the parent 912 * via wakeup. Signals are handled elsewhere. The process must not be 913 * on the run queue. 914 */ 915 void 916 stop(p) 917 register struct proc *p; 918 { 919 920 p->p_stat = SSTOP; 921 p->p_flag &= ~P_WAITED; 922 wakeup((caddr_t)p->p_pptr); 923 } 924 925 /* 926 * Take the action for the specified signal 927 * from the current set of pending signals. 928 */ 929 void 930 postsig(signum) 931 register int signum; 932 { 933 register struct proc *p = curproc; 934 register struct sigacts *ps = p->p_sigacts; 935 register sig_t action; 936 u_long code; 937 int mask, returnmask; 938 939 #ifdef DIAGNOSTIC 940 if (signum == 0) 941 panic("postsig"); 942 #endif 943 mask = sigmask(signum); 944 p->p_siglist &= ~mask; 945 action = ps->ps_sigact[signum]; 946 #ifdef KTRACE 947 if (KTRPOINT(p, KTR_PSIG)) 948 ktrpsig(p->p_tracep, 949 signum, action, ps->ps_flags & SAS_OLDMASK ? 950 ps->ps_oldmask : p->p_sigmask, 0); 951 #endif 952 if (action == SIG_DFL) { 953 /* 954 * Default action, where the default is to kill 955 * the process. (Other cases were ignored above.) 956 */ 957 sigexit(p, signum); 958 /* NOTREACHED */ 959 } else { 960 /* 961 * If we get here, the signal must be caught. 962 */ 963 #ifdef DIAGNOSTIC 964 if (action == SIG_IGN || (p->p_sigmask & mask)) 965 panic("postsig action"); 966 #endif 967 /* 968 * Set the new mask value and also defer further 969 * occurences of this signal. 970 * 971 * Special case: user has done a sigpause. Here the 972 * current mask is not of interest, but rather the 973 * mask from before the sigpause is what we want 974 * restored after the signal processing is completed. 975 */ 976 (void) splhigh(); 977 if (ps->ps_flags & SAS_OLDMASK) { 978 returnmask = ps->ps_oldmask; 979 ps->ps_flags &= ~SAS_OLDMASK; 980 } else 981 returnmask = p->p_sigmask; 982 p->p_sigmask |= ps->ps_catchmask[signum]; 983 if ((ps->ps_sigreset & mask) != 0) { 984 p->p_sigcatch &= ~mask; 985 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 986 p->p_sigignore |= mask; 987 ps->ps_sigact[signum] = SIG_DFL; 988 } 989 (void) spl0(); 990 p->p_stats->p_ru.ru_nsignals++; 991 if (ps->ps_sig != signum) { 992 code = 0; 993 } else { 994 code = ps->ps_code; 995 ps->ps_code = 0; 996 } 997 (*p->p_emul->e_sendsig)(action, signum, returnmask, code); 998 } 999 } 1000 1001 /* 1002 * Kill the current process for stated reason. 1003 */ 1004 void 1005 killproc(p, why) 1006 struct proc *p; 1007 char *why; 1008 { 1009 1010 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1011 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1012 psignal(p, SIGKILL); 1013 } 1014 1015 /* 1016 * Force the current process to exit with the specified signal, dumping core 1017 * if appropriate. We bypass the normal tests for masked and caught signals, 1018 * allowing unrecoverable failures to terminate the process without changing 1019 * signal state. Mark the accounting record with the signal termination. 1020 * If dumping core, save the signal number for the debugger. Calls exit and 1021 * does not return. 1022 */ 1023 void 1024 sigexit(p, signum) 1025 register struct proc *p; 1026 int signum; 1027 { 1028 1029 p->p_acflag |= AXSIG; 1030 if (sigprop[signum] & SA_CORE) { 1031 p->p_sigacts->ps_sig = signum; 1032 if (coredump(p) == 0) 1033 signum |= WCOREFLAG; 1034 } 1035 exit1(p, W_EXITCODE(0, signum)); 1036 /* NOTREACHED */ 1037 } 1038 1039 /* 1040 * Dump core, into a file named "progname.core", unless the process was 1041 * setuid/setgid. 1042 */ 1043 int 1044 coredump(p) 1045 register struct proc *p; 1046 { 1047 register struct vnode *vp; 1048 register struct pcred *pcred = p->p_cred; 1049 register struct ucred *cred = pcred->pc_ucred; 1050 register struct vmspace *vm = p->p_vmspace; 1051 struct nameidata nd; 1052 struct vattr vattr; 1053 int error, error1; 1054 char name[MAXCOMLEN+6]; /* progname.core */ 1055 struct core core; 1056 1057 if (pcred->p_svuid != pcred->p_ruid || 1058 cred->cr_uid != pcred->p_ruid || 1059 pcred->p_svgid != pcred->p_rgid || 1060 cred->cr_gid != pcred->p_rgid) 1061 return (EFAULT); 1062 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >= 1063 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1064 return (EFAULT); 1065 sprintf(name, "%s.core", p->p_comm); 1066 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p); 1067 error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR); 1068 if (error) 1069 return (error); 1070 vp = nd.ni_vp; 1071 1072 /* Don't dump to non-regular files or files with links. */ 1073 if (vp->v_type != VREG || 1074 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1075 error = EFAULT; 1076 goto out; 1077 } 1078 VATTR_NULL(&vattr); 1079 vattr.va_size = 0; 1080 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1081 VOP_SETATTR(vp, &vattr, cred, p); 1082 p->p_acflag |= ACORE; 1083 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc)); 1084 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); 1085 1086 core.c_midmag = 0; 1087 strncpy(core.c_name, p->p_comm, MAXCOMLEN); 1088 core.c_nseg = 0; 1089 core.c_signo = p->p_sigacts->ps_sig; 1090 core.c_ucode = p->p_sigacts->ps_code; 1091 core.c_cpusize = 0; 1092 core.c_tsize = (u_long)ctob(vm->vm_tsize); 1093 core.c_dsize = (u_long)ctob(vm->vm_dsize); 1094 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize)); 1095 error = cpu_coredump(p, vp, cred, &core); 1096 if (error) 1097 goto out; 1098 if (core.c_midmag == 0) { 1099 /* XXX 1100 * cpu_coredump() didn't bother to set the magic; assume 1101 * this is a request to do a traditional dump. cpu_coredump() 1102 * is still responsible for setting sensible values in 1103 * the core header. 1104 */ 1105 if (core.c_cpusize == 0) 1106 core.c_cpusize = USPACE; /* Just in case */ 1107 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr, 1108 (int)core.c_dsize, 1109 (off_t)core.c_cpusize, UIO_USERSPACE, 1110 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1111 if (error) 1112 goto out; 1113 error = vn_rdwr(UIO_WRITE, vp, 1114 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)), 1115 core.c_ssize, 1116 (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE, 1117 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1118 } else { 1119 /* 1120 * vm_coredump() spits out all appropriate segments. 1121 * All that's left to do is to write the core header. 1122 */ 1123 error = vm_coredump(p, vp, cred, &core); 1124 if (error) 1125 goto out; 1126 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core, 1127 (int)core.c_hdrsize, (off_t)0, 1128 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1129 } 1130 out: 1131 VOP_UNLOCK(vp); 1132 error1 = vn_close(vp, FWRITE, cred, p); 1133 if (error == 0) 1134 error = error1; 1135 return (error); 1136 } 1137 1138 /* 1139 * Nonexistent system call-- signal process (may want to handle it). 1140 * Flag error in case process won't see signal immediately (blocked or ignored). 1141 */ 1142 /* ARGSUSED */ 1143 int 1144 sys_nosys(p, v, retval) 1145 struct proc *p; 1146 void *v; 1147 register_t *retval; 1148 { 1149 1150 psignal(p, SIGSYS); 1151 return (ENOSYS); 1152 } 1153