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