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