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