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