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.8 (Berkeley) 05/19/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 ps->ps_sig = signum; /* XXX to verify code */ 618 psignal(p, signum); 619 } 620 } 621 622 /* 623 * Send the signal to the process. If the signal has an action, the action 624 * is usually performed by the target process rather than the caller; we add 625 * the signal to the set of pending signals for the process. 626 * 627 * Exceptions: 628 * o When a stop signal is sent to a sleeping process that takes the 629 * default action, the process is stopped without awakening it. 630 * o SIGCONT restarts stopped processes (or puts them back to sleep) 631 * regardless of the signal action (eg, blocked or ignored). 632 * 633 * Other ignored signals are discarded immediately. 634 */ 635 void 636 psignal(p, signum) 637 register struct proc *p; 638 register int signum; 639 { 640 register int s, prop; 641 register sig_t action; 642 int mask; 643 644 if ((u_int)signum >= NSIG || signum == 0) 645 panic("psignal signal number"); 646 mask = sigmask(signum); 647 prop = sigprop[signum]; 648 649 /* 650 * If proc is traced, always give parent a chance. 651 */ 652 if (p->p_flag & P_TRACED) 653 action = SIG_DFL; 654 else { 655 /* 656 * If the signal is being ignored, 657 * then we forget about it immediately. 658 * (Note: we don't set SIGCONT in p_sigignore, 659 * and if it is set to SIG_IGN, 660 * action will be SIG_DFL here.) 661 */ 662 if (p->p_sigignore & mask) 663 return; 664 if (p->p_sigmask & mask) 665 action = SIG_HOLD; 666 else if (p->p_sigcatch & mask) 667 action = SIG_CATCH; 668 else 669 action = SIG_DFL; 670 } 671 672 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 673 (p->p_flag & P_TRACED) == 0) 674 p->p_nice = NZERO; 675 676 if (prop & SA_CONT) 677 p->p_siglist &= ~stopsigmask; 678 679 if (prop & SA_STOP) { 680 /* 681 * If sending a tty stop signal to a member of an orphaned 682 * process group, discard the signal here if the action 683 * is default; don't stop the process below if sleeping, 684 * and don't clear any pending SIGCONT. 685 */ 686 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 687 action == SIG_DFL) 688 return; 689 p->p_siglist &= ~contsigmask; 690 } 691 p->p_siglist |= mask; 692 693 /* 694 * Defer further processing for signals which are held, 695 * except that stopped processes must be continued by SIGCONT. 696 */ 697 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 698 return; 699 s = splhigh(); 700 switch (p->p_stat) { 701 702 case SSLEEP: 703 /* 704 * If process is sleeping uninterruptibly 705 * we can't interrupt the sleep... the signal will 706 * be noticed when the process returns through 707 * trap() or syscall(). 708 */ 709 if ((p->p_flag & P_SINTR) == 0) 710 goto out; 711 /* 712 * Process is sleeping and traced... make it runnable 713 * so it can discover the signal in issignal() and stop 714 * for the parent. 715 */ 716 if (p->p_flag & P_TRACED) 717 goto run; 718 /* 719 * If SIGCONT is default (or ignored) and process is 720 * asleep, we are finished; the process should not 721 * be awakened. 722 */ 723 if ((prop & SA_CONT) && action == SIG_DFL) { 724 p->p_siglist &= ~mask; 725 goto out; 726 } 727 /* 728 * When a sleeping process receives a stop 729 * signal, process immediately if possible. 730 * All other (caught or default) signals 731 * cause the process to run. 732 */ 733 if (prop & SA_STOP) { 734 if (action != SIG_DFL) 735 goto runfast; 736 /* 737 * If a child holding parent blocked, 738 * stopping could cause deadlock. 739 */ 740 if (p->p_flag & P_PPWAIT) 741 goto out; 742 p->p_siglist &= ~mask; 743 p->p_xstat = signum; 744 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 745 psignal(p->p_pptr, SIGCHLD); 746 stop(p); 747 goto out; 748 } else 749 goto runfast; 750 /*NOTREACHED*/ 751 752 case SSTOP: 753 /* 754 * If traced process is already stopped, 755 * then no further action is necessary. 756 */ 757 if (p->p_flag & P_TRACED) 758 goto out; 759 760 /* 761 * Kill signal always sets processes running. 762 */ 763 if (signum == SIGKILL) 764 goto runfast; 765 766 if (prop & SA_CONT) { 767 /* 768 * If SIGCONT is default (or ignored), we continue the 769 * process but don't leave the signal in p_siglist, as 770 * it has no further action. If SIGCONT is held, we 771 * continue the process and leave the signal in 772 * p_siglist. If the process catches SIGCONT, let it 773 * handle the signal itself. If it isn't waiting on 774 * an event, then it goes back to run state. 775 * Otherwise, process goes back to sleep state. 776 */ 777 if (action == SIG_DFL) 778 p->p_siglist &= ~mask; 779 if (action == SIG_CATCH) 780 goto runfast; 781 if (p->p_wchan == 0) 782 goto run; 783 p->p_stat = SSLEEP; 784 goto out; 785 } 786 787 if (prop & SA_STOP) { 788 /* 789 * Already stopped, don't need to stop again. 790 * (If we did the shell could get confused.) 791 */ 792 p->p_siglist &= ~mask; /* take it away */ 793 goto out; 794 } 795 796 /* 797 * If process is sleeping interruptibly, then simulate a 798 * wakeup so that when it is continued, it will be made 799 * runnable and can look at the signal. But don't make 800 * the process runnable, leave it stopped. 801 */ 802 if (p->p_wchan && p->p_flag & P_SINTR) 803 unsleep(p); 804 goto out; 805 806 default: 807 /* 808 * SRUN, SIDL, SZOMB do nothing with the signal, 809 * other than kicking ourselves if we are running. 810 * It will either never be noticed, or noticed very soon. 811 */ 812 if (p == curproc) 813 signotify(p); 814 goto out; 815 } 816 /*NOTREACHED*/ 817 818 runfast: 819 /* 820 * Raise priority to at least PUSER. 821 */ 822 if (p->p_priority > PUSER) 823 p->p_priority = PUSER; 824 run: 825 setrunnable(p); 826 out: 827 splx(s); 828 } 829 830 /* 831 * If the current process has received a signal (should be caught or cause 832 * termination, should interrupt current syscall), return the signal number. 833 * Stop signals with default action are processed immediately, then cleared; 834 * they aren't returned. This is checked after each entry to the system for 835 * a syscall or trap (though this can usually be done without calling issignal 836 * by checking the pending signal masks in the CURSIG macro.) The normal call 837 * sequence is 838 * 839 * while (signum = CURSIG(curproc)) 840 * postsig(signum); 841 */ 842 issignal(p) 843 register struct proc *p; 844 { 845 register int signum, mask, prop; 846 847 for (;;) { 848 mask = p->p_siglist & ~p->p_sigmask; 849 if (p->p_flag & P_PPWAIT) 850 mask &= ~stopsigmask; 851 if (mask == 0) /* no signal to send */ 852 return (0); 853 signum = ffs((long)mask); 854 mask = sigmask(signum); 855 prop = sigprop[signum]; 856 /* 857 * We should see pending but ignored signals 858 * only if P_TRACED was on when they were posted. 859 */ 860 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) { 861 p->p_siglist &= ~mask; 862 continue; 863 } 864 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 865 /* 866 * If traced, always stop, and stay 867 * stopped until released by the parent. 868 * 869 * Note that we must clear the pending signal 870 * before we call trace_req since that routine 871 * might cause a fault, calling tsleep and 872 * leading us back here again with the same signal. 873 * Then we would be deadlocked because the tracer 874 * would still be blocked on the ipc struct from 875 * the initial request. 876 */ 877 p->p_xstat = signum; 878 p->p_siglist &= ~mask; 879 psignal(p->p_pptr, SIGCHLD); 880 do { 881 stop(p); 882 mi_switch(); 883 } while (!trace_req(p) && p->p_flag & P_TRACED); 884 885 /* 886 * If parent wants us to take the signal, 887 * then it will leave it in p->p_xstat; 888 * otherwise we just look for signals again. 889 */ 890 signum = p->p_xstat; 891 if (signum == 0) 892 continue; 893 894 /* 895 * Put the new signal into p_siglist. If the 896 * signal is being masked, look for other signals. 897 */ 898 mask = sigmask(signum); 899 p->p_siglist |= mask; 900 if (p->p_sigmask & mask) 901 continue; 902 903 /* 904 * If the traced bit got turned off, go back up 905 * to the top to rescan signals. This ensures 906 * that p_sig* and ps_sigact are consistent. 907 */ 908 if ((p->p_flag & P_TRACED) == 0) 909 continue; 910 } 911 912 /* 913 * Decide whether the signal should be returned. 914 * Return the signal's number, or fall through 915 * to clear it from the pending mask. 916 */ 917 switch ((int)p->p_sigacts->ps_sigact[signum]) { 918 919 case SIG_DFL: 920 /* 921 * Don't take default actions on system processes. 922 */ 923 if (p->p_pid <= 1) { 924 #ifdef DIAGNOSTIC 925 /* 926 * Are you sure you want to ignore SIGSEGV 927 * in init? XXX 928 */ 929 printf("Process (pid %d) got signal %d\n", 930 p->p_pid, signum); 931 #endif 932 break; /* == ignore */ 933 } 934 /* 935 * If there is a pending stop signal to process 936 * with default action, stop here, 937 * then clear the signal. However, 938 * if process is member of an orphaned 939 * process group, ignore tty stop signals. 940 */ 941 if (prop & SA_STOP) { 942 if (p->p_flag & P_TRACED || 943 (p->p_pgrp->pg_jobc == 0 && 944 prop & SA_TTYSTOP)) 945 break; /* == ignore */ 946 p->p_xstat = signum; 947 stop(p); 948 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 949 psignal(p->p_pptr, SIGCHLD); 950 mi_switch(); 951 break; 952 } else if (prop & SA_IGNORE) { 953 /* 954 * Except for SIGCONT, shouldn't get here. 955 * Default action is to ignore; drop it. 956 */ 957 break; /* == ignore */ 958 } else 959 return (signum); 960 /*NOTREACHED*/ 961 962 case SIG_IGN: 963 /* 964 * Masking above should prevent us ever trying 965 * to take action on an ignored signal other 966 * than SIGCONT, unless process is traced. 967 */ 968 if ((prop & SA_CONT) == 0 && 969 (p->p_flag & P_TRACED) == 0) 970 printf("issignal\n"); 971 break; /* == ignore */ 972 973 default: 974 /* 975 * This signal has an action, let 976 * postsig() process it. 977 */ 978 return (signum); 979 } 980 p->p_siglist &= ~mask; /* take the signal! */ 981 } 982 /* NOTREACHED */ 983 } 984 985 /* 986 * Put the argument process into the stopped state and notify the parent 987 * via wakeup. Signals are handled elsewhere. The process must not be 988 * on the run queue. 989 */ 990 stop(p) 991 register struct proc *p; 992 { 993 994 p->p_stat = SSTOP; 995 p->p_flag &= ~P_WAITED; 996 wakeup((caddr_t)p->p_pptr); 997 } 998 999 /* 1000 * Take the action for the specified signal 1001 * from the current set of pending signals. 1002 */ 1003 void 1004 postsig(signum) 1005 register int signum; 1006 { 1007 register struct proc *p = curproc; 1008 register struct sigacts *ps = p->p_sigacts; 1009 register sig_t action; 1010 int code, mask, returnmask; 1011 1012 #ifdef DIAGNOSTIC 1013 if (signum == 0) 1014 panic("postsig"); 1015 #endif 1016 mask = sigmask(signum); 1017 p->p_siglist &= ~mask; 1018 action = ps->ps_sigact[signum]; 1019 #ifdef KTRACE 1020 if (KTRPOINT(p, KTR_PSIG)) 1021 ktrpsig(p->p_tracep, 1022 signum, action, ps->ps_flags & SAS_OLDMASK ? 1023 ps->ps_oldmask : p->p_sigmask, 0); 1024 #endif 1025 if (action == SIG_DFL) { 1026 /* 1027 * Default action, where the default is to kill 1028 * the process. (Other cases were ignored above.) 1029 */ 1030 sigexit(p, signum); 1031 /* NOTREACHED */ 1032 } else { 1033 /* 1034 * If we get here, the signal must be caught. 1035 */ 1036 #ifdef DIAGNOSTIC 1037 if (action == SIG_IGN || (p->p_sigmask & mask)) 1038 panic("postsig action"); 1039 #endif 1040 /* 1041 * Set the new mask value and also defer further 1042 * occurences of this signal. 1043 * 1044 * Special case: user has done a sigpause. Here the 1045 * current mask is not of interest, but rather the 1046 * mask from before the sigpause is what we want 1047 * restored after the signal processing is completed. 1048 */ 1049 (void) splhigh(); 1050 if (ps->ps_flags & SAS_OLDMASK) { 1051 returnmask = ps->ps_oldmask; 1052 ps->ps_flags &= ~SAS_OLDMASK; 1053 } else 1054 returnmask = p->p_sigmask; 1055 p->p_sigmask |= ps->ps_catchmask[signum] | mask; 1056 (void) spl0(); 1057 p->p_stats->p_ru.ru_nsignals++; 1058 if (ps->ps_sig != signum) { 1059 code = 0; 1060 } else { 1061 code = ps->ps_code; 1062 ps->ps_code = 0; 1063 ps->ps_sig = 0; 1064 } 1065 sendsig(action, signum, returnmask, code); 1066 } 1067 } 1068 1069 /* 1070 * Kill the current process for stated reason. 1071 */ 1072 killproc(p, why) 1073 struct proc *p; 1074 char *why; 1075 { 1076 1077 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1078 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1079 psignal(p, SIGKILL); 1080 } 1081 1082 /* 1083 * Force the current process to exit with the specified signal, dumping core 1084 * if appropriate. We bypass the normal tests for masked and caught signals, 1085 * allowing unrecoverable failures to terminate the process without changing 1086 * signal state. Mark the accounting record with the signal termination. 1087 * If dumping core, save the signal number for the debugger. Calls exit and 1088 * does not return. 1089 */ 1090 sigexit(p, signum) 1091 register struct proc *p; 1092 int signum; 1093 { 1094 1095 p->p_acflag |= AXSIG; 1096 if (sigprop[signum] & SA_CORE) { 1097 p->p_sigacts->ps_sig = signum; 1098 if (coredump(p) == 0) 1099 signum |= WCOREFLAG; 1100 } 1101 exit1(p, W_EXITCODE(0, signum)); 1102 /* NOTREACHED */ 1103 } 1104 1105 /* 1106 * Dump core, into a file named "progname.core", unless the process was 1107 * setuid/setgid. 1108 */ 1109 coredump(p) 1110 register struct proc *p; 1111 { 1112 register struct vnode *vp; 1113 register struct pcred *pcred = p->p_cred; 1114 register struct ucred *cred = pcred->pc_ucred; 1115 register struct vmspace *vm = p->p_vmspace; 1116 struct nameidata nd; 1117 struct vattr vattr; 1118 int error, error1; 1119 char name[MAXCOMLEN+6]; /* progname.core */ 1120 1121 if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid) 1122 return (EFAULT); 1123 if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >= 1124 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1125 return (EFAULT); 1126 sprintf(name, "%s.core", p->p_comm); 1127 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p); 1128 if (error = vn_open(&nd, 1129 O_CREAT | FWRITE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) 1130 return (error); 1131 vp = nd.ni_vp; 1132 1133 /* Don't dump to non-regular files or files with links. */ 1134 if (vp->v_type != VREG || 1135 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1136 error = EFAULT; 1137 goto out; 1138 } 1139 VATTR_NULL(&vattr); 1140 vattr.va_size = 0; 1141 LEASE_CHECK(vp, p, cred, LEASE_WRITE); 1142 VOP_SETATTR(vp, &vattr, cred, p); 1143 p->p_acflag |= ACORE; 1144 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc)); 1145 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); 1146 error = cpu_coredump(p, vp, cred); 1147 if (error == 0) 1148 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr, 1149 (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 1150 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1151 if (error == 0) 1152 error = vn_rdwr(UIO_WRITE, vp, 1153 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)), 1154 round_page(ctob(vm->vm_ssize)), 1155 (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE, 1156 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1157 out: 1158 VOP_UNLOCK(vp); 1159 error1 = vn_close(vp, FWRITE, cred, p); 1160 if (error == 0) 1161 error = error1; 1162 return (error); 1163 } 1164 1165 /* 1166 * Nonexistent system call-- signal process (may want to handle it). 1167 * Flag error in case process won't see signal immediately (blocked or ignored). 1168 */ 1169 struct nosys_args { 1170 int dummy; 1171 }; 1172 /* ARGSUSED */ 1173 nosys(p, args, retval) 1174 struct proc *p; 1175 struct nosys_args *args; 1176 int *retval; 1177 { 1178 1179 psignal(p, SIGSYS); 1180 return (EINVAL); 1181 } 1182