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