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