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