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