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