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