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