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