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 * $FreeBSD$ 40 */ 41 42 #include "opt_compat.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/kernel.h> 47 #include <sys/sysproto.h> 48 #include <sys/signalvar.h> 49 #include <sys/resourcevar.h> 50 #include <sys/namei.h> 51 #include <sys/vnode.h> 52 #include <sys/event.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 67 #include <machine/ipl.h> 68 #include <machine/cpu.h> 69 #include <machine/smp.h> 70 71 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 72 73 static int coredump __P((struct proc *)); 74 static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act, 75 struct sigaction *oact, int old)); 76 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set, 77 sigset_t *oset, int old)); 78 static char *expand_name __P((const char *, uid_t, pid_t)); 79 static int killpg1 __P((struct proc *cp, int sig, int pgid, int all)); 80 static int sig_ffs __P((sigset_t *set)); 81 static int sigprop __P((int sig)); 82 static void stop __P((struct proc *)); 83 84 static int filt_sigattach(struct knote *kn); 85 static void filt_sigdetach(struct knote *kn); 86 static int filt_signal(struct knote *kn, long hint); 87 88 struct filterops sig_filtops = 89 { 0, filt_sigattach, filt_sigdetach, filt_signal }; 90 91 static int kern_logsigexit = 1; 92 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 93 &kern_logsigexit, 0, 94 "Log processes quitting on abnormal signals to syslog(3)"); 95 96 /* 97 * Can process p, with pcred pc, send the signal sig to process q? 98 */ 99 #define CANSIGNAL(p, q, sig) \ 100 (!p_trespass(p, q) || \ 101 ((sig) == SIGCONT && (q)->p_session == (p)->p_session)) 102 103 /* 104 * Policy -- Can real uid ruid with ucred uc send a signal to process q? 105 */ 106 #define CANSIGIO(ruid, uc, q) \ 107 ((uc)->cr_uid == 0 || \ 108 (ruid) == (q)->p_cred->p_ruid || \ 109 (uc)->cr_uid == (q)->p_cred->p_ruid || \ 110 (ruid) == (q)->p_ucred->cr_uid || \ 111 (uc)->cr_uid == (q)->p_ucred->cr_uid) 112 113 int sugid_coredump; 114 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 115 &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 116 117 static int do_coredump = 1; 118 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 119 &do_coredump, 0, "Enable/Disable coredumps"); 120 121 /* 122 * Signal properties and actions. 123 * The array below categorizes the signals and their default actions 124 * according to the following properties: 125 */ 126 #define SA_KILL 0x01 /* terminates process by default */ 127 #define SA_CORE 0x02 /* ditto and coredumps */ 128 #define SA_STOP 0x04 /* suspend process */ 129 #define SA_TTYSTOP 0x08 /* ditto, from tty */ 130 #define SA_IGNORE 0x10 /* ignore by default */ 131 #define SA_CONT 0x20 /* continue if suspended */ 132 #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 133 134 static int sigproptbl[NSIG] = { 135 SA_KILL, /* SIGHUP */ 136 SA_KILL, /* SIGINT */ 137 SA_KILL|SA_CORE, /* SIGQUIT */ 138 SA_KILL|SA_CORE, /* SIGILL */ 139 SA_KILL|SA_CORE, /* SIGTRAP */ 140 SA_KILL|SA_CORE, /* SIGABRT */ 141 SA_KILL|SA_CORE, /* SIGEMT */ 142 SA_KILL|SA_CORE, /* SIGFPE */ 143 SA_KILL, /* SIGKILL */ 144 SA_KILL|SA_CORE, /* SIGBUS */ 145 SA_KILL|SA_CORE, /* SIGSEGV */ 146 SA_KILL|SA_CORE, /* SIGSYS */ 147 SA_KILL, /* SIGPIPE */ 148 SA_KILL, /* SIGALRM */ 149 SA_KILL, /* SIGTERM */ 150 SA_IGNORE, /* SIGURG */ 151 SA_STOP, /* SIGSTOP */ 152 SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 153 SA_IGNORE|SA_CONT, /* SIGCONT */ 154 SA_IGNORE, /* SIGCHLD */ 155 SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 156 SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 157 SA_IGNORE, /* SIGIO */ 158 SA_KILL, /* SIGXCPU */ 159 SA_KILL, /* SIGXFSZ */ 160 SA_KILL, /* SIGVTALRM */ 161 SA_KILL, /* SIGPROF */ 162 SA_IGNORE, /* SIGWINCH */ 163 SA_IGNORE, /* SIGINFO */ 164 SA_KILL, /* SIGUSR1 */ 165 SA_KILL, /* SIGUSR2 */ 166 }; 167 168 static __inline int 169 sigprop(int sig) 170 { 171 172 if (sig > 0 && sig < NSIG) 173 return (sigproptbl[_SIG_IDX(sig)]); 174 return (0); 175 } 176 177 static __inline int 178 sig_ffs(sigset_t *set) 179 { 180 int i; 181 182 for (i = 0; i < _SIG_WORDS; i++) 183 if (set->__bits[i]) 184 return (ffs(set->__bits[i]) + (i * 32)); 185 return (0); 186 } 187 188 /* 189 * do_sigaction 190 * sigaction 191 * osigaction 192 */ 193 static int 194 do_sigaction(p, sig, act, oact, old) 195 struct proc *p; 196 register int sig; 197 struct sigaction *act, *oact; 198 int old; 199 { 200 register struct sigacts *ps = p->p_sigacts; 201 202 if (sig <= 0 || sig > _SIG_MAXSIG) 203 return (EINVAL); 204 205 if (oact) { 206 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 207 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 208 oact->sa_flags = 0; 209 if (SIGISMEMBER(ps->ps_sigonstack, sig)) 210 oact->sa_flags |= SA_ONSTACK; 211 if (!SIGISMEMBER(ps->ps_sigintr, sig)) 212 oact->sa_flags |= SA_RESTART; 213 if (SIGISMEMBER(ps->ps_sigreset, sig)) 214 oact->sa_flags |= SA_RESETHAND; 215 if (SIGISMEMBER(ps->ps_signodefer, sig)) 216 oact->sa_flags |= SA_NODEFER; 217 if (SIGISMEMBER(ps->ps_siginfo, sig)) 218 oact->sa_flags |= SA_SIGINFO; 219 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 220 oact->sa_flags |= SA_NOCLDSTOP; 221 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 222 oact->sa_flags |= SA_NOCLDWAIT; 223 } 224 if (act) { 225 if ((sig == SIGKILL || sig == SIGSTOP) && 226 act->sa_handler != SIG_DFL) 227 return (EINVAL); 228 229 /* 230 * Change setting atomically. 231 */ 232 (void) splhigh(); 233 234 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 235 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 236 if (act->sa_flags & SA_SIGINFO) { 237 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 238 SIGADDSET(ps->ps_siginfo, sig); 239 } else { 240 ps->ps_sigact[_SIG_IDX(sig)] = 241 (__sighandler_t *)act->sa_sigaction; 242 SIGDELSET(ps->ps_siginfo, sig); 243 } 244 if (!(act->sa_flags & SA_RESTART)) 245 SIGADDSET(ps->ps_sigintr, sig); 246 else 247 SIGDELSET(ps->ps_sigintr, sig); 248 if (act->sa_flags & SA_ONSTACK) 249 SIGADDSET(ps->ps_sigonstack, sig); 250 else 251 SIGDELSET(ps->ps_sigonstack, sig); 252 if (act->sa_flags & SA_RESETHAND) 253 SIGADDSET(ps->ps_sigreset, sig); 254 else 255 SIGDELSET(ps->ps_sigreset, sig); 256 if (act->sa_flags & SA_NODEFER) 257 SIGADDSET(ps->ps_signodefer, sig); 258 else 259 SIGDELSET(ps->ps_signodefer, sig); 260 #ifdef COMPAT_SUNOS 261 if (act->sa_flags & SA_USERTRAMP) 262 SIGADDSET(ps->ps_usertramp, sig); 263 else 264 SIGDELSET(ps->ps_usertramp, seg); 265 #endif 266 if (sig == SIGCHLD) { 267 if (act->sa_flags & SA_NOCLDSTOP) 268 p->p_procsig->ps_flag |= PS_NOCLDSTOP; 269 else 270 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 271 if (act->sa_flags & SA_NOCLDWAIT) { 272 /* 273 * Paranoia: since SA_NOCLDWAIT is implemented 274 * by reparenting the dying child to PID 1 (and 275 * trust it to reap the zombie), PID 1 itself 276 * is forbidden to set SA_NOCLDWAIT. 277 */ 278 if (p->p_pid == 1) 279 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 280 else 281 p->p_procsig->ps_flag |= PS_NOCLDWAIT; 282 } else 283 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 284 } 285 /* 286 * Set bit in p_sigignore for signals that are set to SIG_IGN, 287 * and for signals set to SIG_DFL where the default is to 288 * ignore. However, don't put SIGCONT in p_sigignore, as we 289 * have to restart the process. 290 */ 291 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 292 (sigprop(sig) & SA_IGNORE && 293 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 294 /* never to be seen again */ 295 SIGDELSET(p->p_siglist, sig); 296 if (sig != SIGCONT) 297 /* easier in psignal */ 298 SIGADDSET(p->p_sigignore, sig); 299 SIGDELSET(p->p_sigcatch, sig); 300 } else { 301 SIGDELSET(p->p_sigignore, sig); 302 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 303 SIGDELSET(p->p_sigcatch, sig); 304 else 305 SIGADDSET(p->p_sigcatch, sig); 306 } 307 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 308 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 309 SIGDELSET(ps->ps_osigset, sig); 310 else 311 SIGADDSET(ps->ps_osigset, sig); 312 313 (void) spl0(); 314 } 315 return (0); 316 } 317 318 #ifndef _SYS_SYSPROTO_H_ 319 struct sigaction_args { 320 int sig; 321 struct sigaction *act; 322 struct sigaction *oact; 323 }; 324 #endif 325 /* ARGSUSED */ 326 int 327 sigaction(p, uap) 328 struct proc *p; 329 register struct sigaction_args *uap; 330 { 331 struct sigaction act, oact; 332 register struct sigaction *actp, *oactp; 333 int error; 334 335 actp = (uap->act != NULL) ? &act : NULL; 336 oactp = (uap->oact != NULL) ? &oact : NULL; 337 if (actp) { 338 error = copyin(uap->act, actp, sizeof(act)); 339 if (error) 340 return (error); 341 } 342 error = do_sigaction(p, uap->sig, actp, oactp, 0); 343 if (oactp && !error) { 344 error = copyout(oactp, uap->oact, sizeof(oact)); 345 } 346 return (error); 347 } 348 349 #ifndef _SYS_SYSPROTO_H_ 350 struct osigaction_args { 351 int signum; 352 struct osigaction *nsa; 353 struct osigaction *osa; 354 }; 355 #endif 356 /* ARGSUSED */ 357 int 358 osigaction(p, uap) 359 struct proc *p; 360 register struct osigaction_args *uap; 361 { 362 struct osigaction sa; 363 struct sigaction nsa, osa; 364 register struct sigaction *nsap, *osap; 365 int error; 366 367 if (uap->signum <= 0 || uap->signum >= ONSIG) 368 return (EINVAL); 369 nsap = (uap->nsa != NULL) ? &nsa : NULL; 370 osap = (uap->osa != NULL) ? &osa : NULL; 371 if (nsap) { 372 error = copyin(uap->nsa, &sa, sizeof(sa)); 373 if (error) 374 return (error); 375 nsap->sa_handler = sa.sa_handler; 376 nsap->sa_flags = sa.sa_flags; 377 OSIG2SIG(sa.sa_mask, nsap->sa_mask); 378 } 379 error = do_sigaction(p, uap->signum, nsap, osap, 1); 380 if (osap && !error) { 381 sa.sa_handler = osap->sa_handler; 382 sa.sa_flags = osap->sa_flags; 383 SIG2OSIG(osap->sa_mask, sa.sa_mask); 384 error = copyout(&sa, uap->osa, sizeof(sa)); 385 } 386 return (error); 387 } 388 389 /* 390 * Initialize signal state for process 0; 391 * set to ignore signals that are ignored by default. 392 */ 393 void 394 siginit(p) 395 struct proc *p; 396 { 397 register int i; 398 399 for (i = 1; i <= NSIG; i++) 400 if (sigprop(i) & SA_IGNORE && i != SIGCONT) 401 SIGADDSET(p->p_sigignore, i); 402 } 403 404 /* 405 * Reset signals for an exec of the specified process. 406 */ 407 void 408 execsigs(p) 409 register struct proc *p; 410 { 411 register struct sigacts *ps = p->p_sigacts; 412 register int sig; 413 414 /* 415 * Reset caught signals. Held signals remain held 416 * through p_sigmask (unless they were caught, 417 * and are now ignored by default). 418 */ 419 while (SIGNOTEMPTY(p->p_sigcatch)) { 420 sig = sig_ffs(&p->p_sigcatch); 421 SIGDELSET(p->p_sigcatch, sig); 422 if (sigprop(sig) & SA_IGNORE) { 423 if (sig != SIGCONT) 424 SIGADDSET(p->p_sigignore, sig); 425 SIGDELSET(p->p_siglist, sig); 426 } 427 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 428 } 429 /* 430 * Reset stack state to the user stack. 431 * Clear set of signals caught on the signal stack. 432 */ 433 p->p_sigstk.ss_flags = SS_DISABLE; 434 p->p_sigstk.ss_size = 0; 435 p->p_sigstk.ss_sp = 0; 436 /* 437 * Reset no zombies if child dies flag as Solaris does. 438 */ 439 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 440 } 441 442 /* 443 * do_sigprocmask() - MP SAFE ONLY IF p == curproc 444 * 445 * Manipulate signal mask. This routine is MP SAFE *ONLY* if 446 * p == curproc. Also remember that in order to remain MP SAFE 447 * no spl*() calls may be made. 448 */ 449 static int 450 do_sigprocmask(p, how, set, oset, old) 451 struct proc *p; 452 int how; 453 sigset_t *set, *oset; 454 int old; 455 { 456 int error; 457 458 if (oset != NULL) 459 *oset = p->p_sigmask; 460 461 error = 0; 462 if (set != NULL) { 463 switch (how) { 464 case SIG_BLOCK: 465 SIG_CANTMASK(*set); 466 SIGSETOR(p->p_sigmask, *set); 467 break; 468 case SIG_UNBLOCK: 469 SIGSETNAND(p->p_sigmask, *set); 470 break; 471 case SIG_SETMASK: 472 SIG_CANTMASK(*set); 473 if (old) 474 SIGSETLO(p->p_sigmask, *set); 475 else 476 p->p_sigmask = *set; 477 break; 478 default: 479 error = EINVAL; 480 break; 481 } 482 } 483 return (error); 484 } 485 486 /* 487 * sigprocmask() - MP SAFE 488 */ 489 490 #ifndef _SYS_SYSPROTO_H_ 491 struct sigprocmask_args { 492 int how; 493 const sigset_t *set; 494 sigset_t *oset; 495 }; 496 #endif 497 int 498 sigprocmask(p, uap) 499 register struct proc *p; 500 struct sigprocmask_args *uap; 501 { 502 sigset_t set, oset; 503 sigset_t *setp, *osetp; 504 int error; 505 506 setp = (uap->set != NULL) ? &set : NULL; 507 osetp = (uap->oset != NULL) ? &oset : NULL; 508 if (setp) { 509 error = copyin(uap->set, setp, sizeof(set)); 510 if (error) 511 return (error); 512 } 513 error = do_sigprocmask(p, uap->how, setp, osetp, 0); 514 if (osetp && !error) { 515 error = copyout(osetp, uap->oset, sizeof(oset)); 516 } 517 return (error); 518 } 519 520 /* 521 * osigprocmask() - MP SAFE 522 */ 523 524 #ifndef _SYS_SYSPROTO_H_ 525 struct osigprocmask_args { 526 int how; 527 osigset_t mask; 528 }; 529 #endif 530 int 531 osigprocmask(p, uap) 532 register struct proc *p; 533 struct osigprocmask_args *uap; 534 { 535 sigset_t set, oset; 536 int error; 537 538 OSIG2SIG(uap->mask, set); 539 error = do_sigprocmask(p, uap->how, &set, &oset, 1); 540 SIG2OSIG(oset, p->p_retval[0]); 541 return (error); 542 } 543 544 #ifndef _SYS_SYSPROTO_H_ 545 struct sigpending_args { 546 sigset_t *set; 547 }; 548 #endif 549 /* ARGSUSED */ 550 int 551 sigpending(p, uap) 552 struct proc *p; 553 struct sigpending_args *uap; 554 { 555 556 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t))); 557 } 558 559 #ifndef _SYS_SYSPROTO_H_ 560 struct osigpending_args { 561 int dummy; 562 }; 563 #endif 564 /* ARGSUSED */ 565 int 566 osigpending(p, uap) 567 struct proc *p; 568 struct osigpending_args *uap; 569 { 570 571 SIG2OSIG(p->p_siglist, p->p_retval[0]); 572 return (0); 573 } 574 575 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 576 /* 577 * Generalized interface signal handler, 4.3-compatible. 578 */ 579 #ifndef _SYS_SYSPROTO_H_ 580 struct osigvec_args { 581 int signum; 582 struct sigvec *nsv; 583 struct sigvec *osv; 584 }; 585 #endif 586 /* ARGSUSED */ 587 int 588 osigvec(p, uap) 589 struct proc *p; 590 register struct osigvec_args *uap; 591 { 592 struct sigvec vec; 593 struct sigaction nsa, osa; 594 register struct sigaction *nsap, *osap; 595 int error; 596 597 if (uap->signum <= 0 || uap->signum >= ONSIG) 598 return (EINVAL); 599 nsap = (uap->nsv != NULL) ? &nsa : NULL; 600 osap = (uap->osv != NULL) ? &osa : NULL; 601 if (nsap) { 602 error = copyin(uap->nsv, &vec, sizeof(vec)); 603 if (error) 604 return (error); 605 nsap->sa_handler = vec.sv_handler; 606 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 607 nsap->sa_flags = vec.sv_flags; 608 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 609 #ifdef COMPAT_SUNOS 610 nsap->sa_flags |= SA_USERTRAMP; 611 #endif 612 } 613 error = do_sigaction(p, uap->signum, nsap, osap, 1); 614 if (osap && !error) { 615 vec.sv_handler = osap->sa_handler; 616 SIG2OSIG(osap->sa_mask, vec.sv_mask); 617 vec.sv_flags = osap->sa_flags; 618 vec.sv_flags &= ~SA_NOCLDWAIT; 619 vec.sv_flags ^= SA_RESTART; 620 #ifdef COMPAT_SUNOS 621 vec.sv_flags &= ~SA_NOCLDSTOP; 622 #endif 623 error = copyout(&vec, uap->osv, sizeof(vec)); 624 } 625 return (error); 626 } 627 628 #ifndef _SYS_SYSPROTO_H_ 629 struct osigblock_args { 630 int mask; 631 }; 632 #endif 633 int 634 osigblock(p, uap) 635 register struct proc *p; 636 struct osigblock_args *uap; 637 { 638 sigset_t set; 639 640 OSIG2SIG(uap->mask, set); 641 SIG_CANTMASK(set); 642 (void) splhigh(); 643 SIG2OSIG(p->p_sigmask, p->p_retval[0]); 644 SIGSETOR(p->p_sigmask, set); 645 (void) spl0(); 646 return (0); 647 } 648 649 #ifndef _SYS_SYSPROTO_H_ 650 struct osigsetmask_args { 651 int mask; 652 }; 653 #endif 654 int 655 osigsetmask(p, uap) 656 struct proc *p; 657 struct osigsetmask_args *uap; 658 { 659 sigset_t set; 660 661 OSIG2SIG(uap->mask, set); 662 SIG_CANTMASK(set); 663 (void) splhigh(); 664 SIG2OSIG(p->p_sigmask, p->p_retval[0]); 665 SIGSETLO(p->p_sigmask, set); 666 (void) spl0(); 667 return (0); 668 } 669 #endif /* COMPAT_43 || COMPAT_SUNOS */ 670 671 /* 672 * Suspend process until signal, providing mask to be set 673 * in the meantime. Note nonstandard calling convention: 674 * libc stub passes mask, not pointer, to save a copyin. 675 */ 676 #ifndef _SYS_SYSPROTO_H_ 677 struct sigsuspend_args { 678 const sigset_t *sigmask; 679 }; 680 #endif 681 /* ARGSUSED */ 682 int 683 sigsuspend(p, uap) 684 register struct proc *p; 685 struct sigsuspend_args *uap; 686 { 687 sigset_t mask; 688 register struct sigacts *ps = p->p_sigacts; 689 int error; 690 691 error = copyin(uap->sigmask, &mask, sizeof(mask)); 692 if (error) 693 return (error); 694 695 /* 696 * When returning from sigsuspend, we want 697 * the old mask to be restored after the 698 * signal handler has finished. Thus, we 699 * save it here and mark the sigacts structure 700 * to indicate this. 701 */ 702 p->p_oldsigmask = p->p_sigmask; 703 p->p_flag |= P_OLDMASK; 704 705 SIG_CANTMASK(mask); 706 p->p_sigmask = mask; 707 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 708 /* void */; 709 /* always return EINTR rather than ERESTART... */ 710 return (EINTR); 711 } 712 713 #ifndef _SYS_SYSPROTO_H_ 714 struct osigsuspend_args { 715 osigset_t mask; 716 }; 717 #endif 718 /* ARGSUSED */ 719 int 720 osigsuspend(p, uap) 721 register struct proc *p; 722 struct osigsuspend_args *uap; 723 { 724 sigset_t mask; 725 register struct sigacts *ps = p->p_sigacts; 726 727 p->p_oldsigmask = p->p_sigmask; 728 p->p_flag |= P_OLDMASK; 729 OSIG2SIG(uap->mask, mask); 730 SIG_CANTMASK(mask); 731 SIGSETLO(p->p_sigmask, mask); 732 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0) 733 /* void */; 734 /* always return EINTR rather than ERESTART... */ 735 return (EINTR); 736 } 737 738 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 739 #ifndef _SYS_SYSPROTO_H_ 740 struct osigstack_args { 741 struct sigstack *nss; 742 struct sigstack *oss; 743 }; 744 #endif 745 /* ARGSUSED */ 746 int 747 osigstack(p, uap) 748 struct proc *p; 749 register struct osigstack_args *uap; 750 { 751 struct sigstack ss; 752 int error = 0; 753 754 ss.ss_sp = p->p_sigstk.ss_sp; 755 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK; 756 if (uap->oss && (error = copyout(&ss, uap->oss, 757 sizeof(struct sigstack)))) 758 return (error); 759 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) { 760 p->p_sigstk.ss_sp = ss.ss_sp; 761 p->p_sigstk.ss_size = 0; 762 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 763 p->p_flag |= P_ALTSTACK; 764 } 765 return (error); 766 } 767 #endif /* COMPAT_43 || COMPAT_SUNOS */ 768 769 #ifndef _SYS_SYSPROTO_H_ 770 struct sigaltstack_args { 771 stack_t *ss; 772 stack_t *oss; 773 }; 774 #endif 775 /* ARGSUSED */ 776 int 777 sigaltstack(p, uap) 778 struct proc *p; 779 register struct sigaltstack_args *uap; 780 { 781 stack_t ss; 782 int error; 783 784 if ((p->p_flag & P_ALTSTACK) == 0) 785 p->p_sigstk.ss_flags |= SS_DISABLE; 786 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss, 787 sizeof(stack_t)))) 788 return (error); 789 if (uap->ss == 0) 790 return (0); 791 if ((error = copyin(uap->ss, &ss, sizeof(ss)))) 792 return (error); 793 if (ss.ss_flags & SS_DISABLE) { 794 if (p->p_sigstk.ss_flags & SS_ONSTACK) 795 return (EINVAL); 796 p->p_flag &= ~P_ALTSTACK; 797 p->p_sigstk.ss_flags = ss.ss_flags; 798 return (0); 799 } 800 if (ss.ss_size < MINSIGSTKSZ) 801 return (ENOMEM); 802 p->p_flag |= P_ALTSTACK; 803 p->p_sigstk = ss; 804 return (0); 805 } 806 807 /* 808 * Common code for kill process group/broadcast kill. 809 * cp is calling process. 810 */ 811 int 812 killpg1(cp, sig, pgid, all) 813 register struct proc *cp; 814 int sig, pgid, all; 815 { 816 register struct proc *p; 817 struct pgrp *pgrp; 818 int nfound = 0; 819 820 if (all) 821 /* 822 * broadcast 823 */ 824 LIST_FOREACH(p, &allproc, p_list) { 825 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 826 p == cp || !CANSIGNAL(cp, p, sig)) 827 continue; 828 nfound++; 829 if (sig) 830 psignal(p, sig); 831 } 832 else { 833 if (pgid == 0) 834 /* 835 * zero pgid means send to my process group. 836 */ 837 pgrp = cp->p_pgrp; 838 else { 839 pgrp = pgfind(pgid); 840 if (pgrp == NULL) 841 return (ESRCH); 842 } 843 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 844 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 845 p->p_stat == SZOMB || 846 !CANSIGNAL(cp, p, sig)) 847 continue; 848 nfound++; 849 if (sig) 850 psignal(p, sig); 851 } 852 } 853 return (nfound ? 0 : ESRCH); 854 } 855 856 #ifndef _SYS_SYSPROTO_H_ 857 struct kill_args { 858 int pid; 859 int signum; 860 }; 861 #endif 862 /* ARGSUSED */ 863 int 864 kill(cp, uap) 865 register struct proc *cp; 866 register struct kill_args *uap; 867 { 868 register struct proc *p; 869 870 if ((u_int)uap->signum > _SIG_MAXSIG) 871 return (EINVAL); 872 if (uap->pid > 0) { 873 /* kill single process */ 874 if ((p = pfind(uap->pid)) == NULL) 875 return (ESRCH); 876 if (!CANSIGNAL(cp, p, uap->signum)) 877 return (EPERM); 878 if (uap->signum) 879 psignal(p, uap->signum); 880 return (0); 881 } 882 switch (uap->pid) { 883 case -1: /* broadcast signal */ 884 return (killpg1(cp, uap->signum, 0, 1)); 885 case 0: /* signal own process group */ 886 return (killpg1(cp, uap->signum, 0, 0)); 887 default: /* negative explicit process group */ 888 return (killpg1(cp, uap->signum, -uap->pid, 0)); 889 } 890 /* NOTREACHED */ 891 } 892 893 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 894 #ifndef _SYS_SYSPROTO_H_ 895 struct okillpg_args { 896 int pgid; 897 int signum; 898 }; 899 #endif 900 /* ARGSUSED */ 901 int 902 okillpg(p, uap) 903 struct proc *p; 904 register struct okillpg_args *uap; 905 { 906 907 if ((u_int)uap->signum > _SIG_MAXSIG) 908 return (EINVAL); 909 return (killpg1(p, uap->signum, uap->pgid, 0)); 910 } 911 #endif /* COMPAT_43 || COMPAT_SUNOS */ 912 913 /* 914 * Send a signal to a process group. 915 */ 916 void 917 gsignal(pgid, sig) 918 int pgid, sig; 919 { 920 struct pgrp *pgrp; 921 922 if (pgid && (pgrp = pgfind(pgid))) 923 pgsignal(pgrp, sig, 0); 924 } 925 926 /* 927 * Send a signal to a process group. If checktty is 1, 928 * limit to members which have a controlling terminal. 929 */ 930 void 931 pgsignal(pgrp, sig, checkctty) 932 struct pgrp *pgrp; 933 int sig, checkctty; 934 { 935 register struct proc *p; 936 937 if (pgrp) 938 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 939 if (checkctty == 0 || p->p_flag & P_CONTROLT) 940 psignal(p, sig); 941 } 942 943 /* 944 * Send a signal caused by a trap to the current process. 945 * If it will be caught immediately, deliver it with correct code. 946 * Otherwise, post it normally. 947 */ 948 void 949 trapsignal(p, sig, code) 950 struct proc *p; 951 register int sig; 952 u_long code; 953 { 954 register struct sigacts *ps = p->p_sigacts; 955 956 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 957 SIGISMEMBER(p->p_sigmask, sig)) { 958 p->p_stats->p_ru.ru_nsignals++; 959 #ifdef KTRACE 960 if (KTRPOINT(p, KTR_PSIG)) 961 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 962 &p->p_sigmask, code); 963 #endif 964 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 965 &p->p_sigmask, code); 966 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 967 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 968 SIGADDSET(p->p_sigmask, sig); 969 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 970 /* 971 * See do_sigaction() for origin of this code. 972 */ 973 SIGDELSET(p->p_sigcatch, sig); 974 if (sig != SIGCONT && 975 sigprop(sig) & SA_IGNORE) 976 SIGADDSET(p->p_sigignore, sig); 977 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 978 } 979 } else { 980 p->p_code = code; /* XXX for core dump/debugger */ 981 p->p_sig = sig; /* XXX to verify code */ 982 psignal(p, sig); 983 } 984 } 985 986 /* 987 * Send the signal to the process. If the signal has an action, the action 988 * is usually performed by the target process rather than the caller; we add 989 * the signal to the set of pending signals for the process. 990 * 991 * Exceptions: 992 * o When a stop signal is sent to a sleeping process that takes the 993 * default action, the process is stopped without awakening it. 994 * o SIGCONT restarts stopped processes (or puts them back to sleep) 995 * regardless of the signal action (eg, blocked or ignored). 996 * 997 * Other ignored signals are discarded immediately. 998 */ 999 void 1000 psignal(p, sig) 1001 register struct proc *p; 1002 register int sig; 1003 { 1004 register int s, prop; 1005 register sig_t action; 1006 1007 if (sig > _SIG_MAXSIG || sig <= 0) { 1008 printf("psignal: signal %d\n", sig); 1009 panic("psignal signal number"); 1010 } 1011 1012 KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1013 1014 prop = sigprop(sig); 1015 1016 /* 1017 * If proc is traced, always give parent a chance; 1018 * if signal event is tracked by procfs, give *that* 1019 * a chance, as well. 1020 */ 1021 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 1022 action = SIG_DFL; 1023 else { 1024 /* 1025 * If the signal is being ignored, 1026 * then we forget about it immediately. 1027 * (Note: we don't set SIGCONT in p_sigignore, 1028 * and if it is set to SIG_IGN, 1029 * action will be SIG_DFL here.) 1030 */ 1031 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1032 return; 1033 if (SIGISMEMBER(p->p_sigmask, sig)) 1034 action = SIG_HOLD; 1035 else if (SIGISMEMBER(p->p_sigcatch, sig)) 1036 action = SIG_CATCH; 1037 else 1038 action = SIG_DFL; 1039 } 1040 1041 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 1042 (p->p_flag & P_TRACED) == 0) 1043 p->p_nice = NZERO; 1044 1045 if (prop & SA_CONT) 1046 SIG_STOPSIGMASK(p->p_siglist); 1047 1048 if (prop & SA_STOP) { 1049 /* 1050 * If sending a tty stop signal to a member of an orphaned 1051 * process group, discard the signal here if the action 1052 * is default; don't stop the process below if sleeping, 1053 * and don't clear any pending SIGCONT. 1054 */ 1055 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1056 action == SIG_DFL) 1057 return; 1058 SIG_CONTSIGMASK(p->p_siglist); 1059 } 1060 SIGADDSET(p->p_siglist, sig); 1061 1062 /* 1063 * Defer further processing for signals which are held, 1064 * except that stopped processes must be continued by SIGCONT. 1065 */ 1066 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) 1067 return; 1068 s = splhigh(); 1069 switch (p->p_stat) { 1070 1071 case SSLEEP: 1072 /* 1073 * If process is sleeping uninterruptibly 1074 * we can't interrupt the sleep... the signal will 1075 * be noticed when the process returns through 1076 * trap() or syscall(). 1077 */ 1078 if ((p->p_flag & P_SINTR) == 0) 1079 goto out; 1080 /* 1081 * Process is sleeping and traced... make it runnable 1082 * so it can discover the signal in issignal() and stop 1083 * for the parent. 1084 */ 1085 if (p->p_flag & P_TRACED) 1086 goto run; 1087 /* 1088 * If SIGCONT is default (or ignored) and process is 1089 * asleep, we are finished; the process should not 1090 * be awakened. 1091 */ 1092 if ((prop & SA_CONT) && action == SIG_DFL) { 1093 SIGDELSET(p->p_siglist, sig); 1094 goto out; 1095 } 1096 /* 1097 * When a sleeping process receives a stop 1098 * signal, process immediately if possible. 1099 * All other (caught or default) signals 1100 * cause the process to run. 1101 */ 1102 if (prop & SA_STOP) { 1103 if (action != SIG_DFL) 1104 goto runfast; 1105 /* 1106 * If a child holding parent blocked, 1107 * stopping could cause deadlock. 1108 */ 1109 if (p->p_flag & P_PPWAIT) 1110 goto out; 1111 SIGDELSET(p->p_siglist, sig); 1112 p->p_xstat = sig; 1113 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1114 psignal(p->p_pptr, SIGCHLD); 1115 stop(p); 1116 goto out; 1117 } else 1118 goto runfast; 1119 /*NOTREACHED*/ 1120 1121 case SSTOP: 1122 /* 1123 * If traced process is already stopped, 1124 * then no further action is necessary. 1125 */ 1126 if (p->p_flag & P_TRACED) 1127 goto out; 1128 1129 /* 1130 * Kill signal always sets processes running. 1131 */ 1132 if (sig == SIGKILL) 1133 goto runfast; 1134 1135 if (prop & SA_CONT) { 1136 /* 1137 * If SIGCONT is default (or ignored), we continue the 1138 * process but don't leave the signal in p_siglist, as 1139 * it has no further action. If SIGCONT is held, we 1140 * continue the process and leave the signal in 1141 * p_siglist. If the process catches SIGCONT, let it 1142 * handle the signal itself. If it isn't waiting on 1143 * an event, then it goes back to run state. 1144 * Otherwise, process goes back to sleep state. 1145 */ 1146 if (action == SIG_DFL) 1147 SIGDELSET(p->p_siglist, sig); 1148 if (action == SIG_CATCH) 1149 goto runfast; 1150 if (p->p_wchan == 0) 1151 goto run; 1152 p->p_stat = SSLEEP; 1153 goto out; 1154 } 1155 1156 if (prop & SA_STOP) { 1157 /* 1158 * Already stopped, don't need to stop again. 1159 * (If we did the shell could get confused.) 1160 */ 1161 SIGDELSET(p->p_siglist, sig); 1162 goto out; 1163 } 1164 1165 /* 1166 * If process is sleeping interruptibly, then simulate a 1167 * wakeup so that when it is continued, it will be made 1168 * runnable and can look at the signal. But don't make 1169 * the process runnable, leave it stopped. 1170 */ 1171 if (p->p_wchan && p->p_flag & P_SINTR) 1172 unsleep(p); 1173 goto out; 1174 1175 default: 1176 /* 1177 * SRUN, SIDL, SZOMB do nothing with the signal, 1178 * other than kicking ourselves if we are running. 1179 * It will either never be noticed, or noticed very soon. 1180 */ 1181 if (p == curproc) 1182 signotify(p); 1183 #ifdef SMP 1184 else if (p->p_stat == SRUN) 1185 forward_signal(p); 1186 #endif 1187 goto out; 1188 } 1189 /*NOTREACHED*/ 1190 1191 runfast: 1192 /* 1193 * Raise priority to at least PUSER. 1194 */ 1195 if (p->p_priority > PUSER) 1196 p->p_priority = PUSER; 1197 run: 1198 setrunnable(p); 1199 out: 1200 splx(s); 1201 } 1202 1203 /* 1204 * If the current process has received a signal (should be caught or cause 1205 * termination, should interrupt current syscall), return the signal number. 1206 * Stop signals with default action are processed immediately, then cleared; 1207 * they aren't returned. This is checked after each entry to the system for 1208 * a syscall or trap (though this can usually be done without calling issignal 1209 * by checking the pending signal masks in the CURSIG macro.) The normal call 1210 * sequence is 1211 * 1212 * while (sig = CURSIG(curproc)) 1213 * postsig(sig); 1214 */ 1215 int 1216 issignal(p) 1217 register struct proc *p; 1218 { 1219 sigset_t mask; 1220 register int sig, prop; 1221 1222 for (;;) { 1223 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 1224 1225 mask = p->p_siglist; 1226 SIGSETNAND(mask, p->p_sigmask); 1227 if (p->p_flag & P_PPWAIT) 1228 SIG_STOPSIGMASK(mask); 1229 if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1230 return (0); 1231 sig = sig_ffs(&mask); 1232 prop = sigprop(sig); 1233 1234 STOPEVENT(p, S_SIG, sig); 1235 1236 /* 1237 * We should see pending but ignored signals 1238 * only if P_TRACED was on when they were posted. 1239 */ 1240 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 1241 SIGDELSET(p->p_siglist, sig); 1242 continue; 1243 } 1244 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1245 /* 1246 * If traced, always stop, and stay 1247 * stopped until released by the parent. 1248 */ 1249 p->p_xstat = sig; 1250 psignal(p->p_pptr, SIGCHLD); 1251 do { 1252 stop(p); 1253 mi_switch(); 1254 } while (!trace_req(p) 1255 && p->p_flag & P_TRACED); 1256 1257 /* 1258 * If the traced bit got turned off, go back up 1259 * to the top to rescan signals. This ensures 1260 * that p_sig* and ps_sigact are consistent. 1261 */ 1262 if ((p->p_flag & P_TRACED) == 0) 1263 continue; 1264 1265 /* 1266 * If parent wants us to take the signal, 1267 * then it will leave it in p->p_xstat; 1268 * otherwise we just look for signals again. 1269 */ 1270 SIGDELSET(p->p_siglist, sig); /* clear old signal */ 1271 sig = p->p_xstat; 1272 if (sig == 0) 1273 continue; 1274 1275 /* 1276 * Put the new signal into p_siglist. If the 1277 * signal is being masked, look for other signals. 1278 */ 1279 SIGADDSET(p->p_siglist, sig); 1280 if (SIGISMEMBER(p->p_sigmask, sig)) 1281 continue; 1282 } 1283 1284 /* 1285 * Decide whether the signal should be returned. 1286 * Return the signal's number, or fall through 1287 * to clear it from the pending mask. 1288 */ 1289 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1290 1291 case (int)SIG_DFL: 1292 /* 1293 * Don't take default actions on system processes. 1294 */ 1295 if (p->p_pid <= 1) { 1296 #ifdef DIAGNOSTIC 1297 /* 1298 * Are you sure you want to ignore SIGSEGV 1299 * in init? XXX 1300 */ 1301 printf("Process (pid %lu) got signal %d\n", 1302 (u_long)p->p_pid, sig); 1303 #endif 1304 break; /* == ignore */ 1305 } 1306 /* 1307 * If there is a pending stop signal to process 1308 * with default action, stop here, 1309 * then clear the signal. However, 1310 * if process is member of an orphaned 1311 * process group, ignore tty stop signals. 1312 */ 1313 if (prop & SA_STOP) { 1314 if (p->p_flag & P_TRACED || 1315 (p->p_pgrp->pg_jobc == 0 && 1316 prop & SA_TTYSTOP)) 1317 break; /* == ignore */ 1318 p->p_xstat = sig; 1319 stop(p); 1320 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1321 psignal(p->p_pptr, SIGCHLD); 1322 mi_switch(); 1323 break; 1324 } else if (prop & SA_IGNORE) { 1325 /* 1326 * Except for SIGCONT, shouldn't get here. 1327 * Default action is to ignore; drop it. 1328 */ 1329 break; /* == ignore */ 1330 } else 1331 return (sig); 1332 /*NOTREACHED*/ 1333 1334 case (int)SIG_IGN: 1335 /* 1336 * Masking above should prevent us ever trying 1337 * to take action on an ignored signal other 1338 * than SIGCONT, unless process is traced. 1339 */ 1340 if ((prop & SA_CONT) == 0 && 1341 (p->p_flag & P_TRACED) == 0) 1342 printf("issignal\n"); 1343 break; /* == ignore */ 1344 1345 default: 1346 /* 1347 * This signal has an action, let 1348 * postsig() process it. 1349 */ 1350 return (sig); 1351 } 1352 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1353 } 1354 /* NOTREACHED */ 1355 } 1356 1357 /* 1358 * Put the argument process into the stopped state and notify the parent 1359 * via wakeup. Signals are handled elsewhere. The process must not be 1360 * on the run queue. 1361 */ 1362 void 1363 stop(p) 1364 register struct proc *p; 1365 { 1366 1367 p->p_stat = SSTOP; 1368 p->p_flag &= ~P_WAITED; 1369 wakeup((caddr_t)p->p_pptr); 1370 } 1371 1372 /* 1373 * Take the action for the specified signal 1374 * from the current set of pending signals. 1375 */ 1376 void 1377 postsig(sig) 1378 register int sig; 1379 { 1380 register struct proc *p = curproc; 1381 struct sigacts *ps = p->p_sigacts; 1382 sig_t action; 1383 sigset_t returnmask; 1384 int code; 1385 1386 KASSERT(sig != 0, ("postsig")); 1387 1388 SIGDELSET(p->p_siglist, sig); 1389 action = ps->ps_sigact[_SIG_IDX(sig)]; 1390 #ifdef KTRACE 1391 if (KTRPOINT(p, KTR_PSIG)) 1392 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 1393 &p->p_oldsigmask : &p->p_sigmask, 0); 1394 #endif 1395 STOPEVENT(p, S_SIG, sig); 1396 1397 if (action == SIG_DFL) { 1398 /* 1399 * Default action, where the default is to kill 1400 * the process. (Other cases were ignored above.) 1401 */ 1402 sigexit(p, sig); 1403 /* NOTREACHED */ 1404 } else { 1405 /* 1406 * If we get here, the signal must be caught. 1407 */ 1408 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 1409 ("postsig action")); 1410 /* 1411 * Set the new mask value and also defer further 1412 * occurrences of this signal. 1413 * 1414 * Special case: user has done a sigsuspend. Here the 1415 * current mask is not of interest, but rather the 1416 * mask from before the sigsuspend is what we want 1417 * restored after the signal processing is completed. 1418 */ 1419 (void) splhigh(); 1420 if (p->p_flag & P_OLDMASK) { 1421 returnmask = p->p_oldsigmask; 1422 p->p_flag &= ~P_OLDMASK; 1423 } else 1424 returnmask = p->p_sigmask; 1425 1426 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1427 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1428 SIGADDSET(p->p_sigmask, sig); 1429 1430 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1431 /* 1432 * See do_sigaction() for origin of this code. 1433 */ 1434 SIGDELSET(p->p_sigcatch, sig); 1435 if (sig != SIGCONT && 1436 sigprop(sig) & SA_IGNORE) 1437 SIGADDSET(p->p_sigignore, sig); 1438 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1439 } 1440 (void) spl0(); 1441 p->p_stats->p_ru.ru_nsignals++; 1442 if (p->p_sig != sig) { 1443 code = 0; 1444 } else { 1445 code = p->p_code; 1446 p->p_code = 0; 1447 p->p_sig = 0; 1448 } 1449 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1450 } 1451 } 1452 1453 /* 1454 * Kill the current process for stated reason. 1455 */ 1456 void 1457 killproc(p, why) 1458 struct proc *p; 1459 char *why; 1460 { 1461 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1462 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1463 psignal(p, SIGKILL); 1464 } 1465 1466 /* 1467 * Force the current process to exit with the specified signal, dumping core 1468 * if appropriate. We bypass the normal tests for masked and caught signals, 1469 * allowing unrecoverable failures to terminate the process without changing 1470 * signal state. Mark the accounting record with the signal termination. 1471 * If dumping core, save the signal number for the debugger. Calls exit and 1472 * does not return. 1473 */ 1474 void 1475 sigexit(p, sig) 1476 register struct proc *p; 1477 int sig; 1478 { 1479 1480 p->p_acflag |= AXSIG; 1481 if (sigprop(sig) & SA_CORE) { 1482 p->p_sig = sig; 1483 /* 1484 * Log signals which would cause core dumps 1485 * (Log as LOG_INFO to appease those who don't want 1486 * these messages.) 1487 * XXX : Todo, as well as euid, write out ruid too 1488 */ 1489 if (coredump(p) == 0) 1490 sig |= WCOREFLAG; 1491 if (kern_logsigexit) 1492 log(LOG_INFO, 1493 "pid %d (%s), uid %d: exited on signal %d%s\n", 1494 p->p_pid, p->p_comm, 1495 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 1496 sig &~ WCOREFLAG, 1497 sig & WCOREFLAG ? " (core dumped)" : ""); 1498 } 1499 exit1(p, W_EXITCODE(0, sig)); 1500 /* NOTREACHED */ 1501 } 1502 1503 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1504 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1505 sizeof(corefilename), "process corefile name format string"); 1506 1507 /* 1508 * expand_name(name, uid, pid) 1509 * Expand the name described in corefilename, using name, uid, and pid. 1510 * corefilename is a printf-like string, with three format specifiers: 1511 * %N name of process ("name") 1512 * %P process id (pid) 1513 * %U user id (uid) 1514 * For example, "%N.core" is the default; they can be disabled completely 1515 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1516 * This is controlled by the sysctl variable kern.corefile (see above). 1517 */ 1518 1519 static char * 1520 expand_name(name, uid, pid) 1521 const char *name; uid_t uid; pid_t pid; { 1522 char *temp; 1523 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1524 int i, n; 1525 char *format = corefilename; 1526 size_t namelen; 1527 1528 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 1529 if (temp == NULL) 1530 return NULL; 1531 namelen = strlen(name); 1532 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1533 int l; 1534 switch (format[i]) { 1535 case '%': /* Format character */ 1536 i++; 1537 switch (format[i]) { 1538 case '%': 1539 temp[n++] = '%'; 1540 break; 1541 case 'N': /* process name */ 1542 if ((n + namelen) > MAXPATHLEN) { 1543 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1544 pid, name, uid, temp, name); 1545 free(temp, M_TEMP); 1546 return NULL; 1547 } 1548 memcpy(temp+n, name, namelen); 1549 n += namelen; 1550 break; 1551 case 'P': /* process id */ 1552 l = sprintf(buf, "%u", pid); 1553 if ((n + l) > MAXPATHLEN) { 1554 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1555 pid, name, uid, temp, name); 1556 free(temp, M_TEMP); 1557 return NULL; 1558 } 1559 memcpy(temp+n, buf, l); 1560 n += l; 1561 break; 1562 case 'U': /* user id */ 1563 l = sprintf(buf, "%u", uid); 1564 if ((n + l) > MAXPATHLEN) { 1565 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1566 pid, name, uid, temp, name); 1567 free(temp, M_TEMP); 1568 return NULL; 1569 } 1570 memcpy(temp+n, buf, l); 1571 n += l; 1572 break; 1573 default: 1574 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1575 } 1576 break; 1577 default: 1578 temp[n++] = format[i]; 1579 } 1580 } 1581 temp[n] = '\0'; 1582 return temp; 1583 } 1584 1585 /* 1586 * Dump a process' core. The main routine does some 1587 * policy checking, and creates the name of the coredump; 1588 * then it passes on a vnode and a size limit to the process-specific 1589 * coredump routine if there is one; if there _is not_ one, it returns 1590 * ENOSYS; otherwise it returns the error from the process-specific routine. 1591 */ 1592 1593 static int 1594 coredump(p) 1595 register struct proc *p; 1596 { 1597 register struct vnode *vp; 1598 register struct ucred *cred = p->p_ucred; 1599 struct nameidata nd; 1600 struct vattr vattr; 1601 int error, error1, flags; 1602 struct mount *mp; 1603 char *name; /* name of corefile */ 1604 off_t limit; 1605 1606 STOPEVENT(p, S_CORE, 0); 1607 1608 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) 1609 return (EFAULT); 1610 1611 /* 1612 * Note that the bulk of limit checking is done after 1613 * the corefile is created. The exception is if the limit 1614 * for corefiles is 0, in which case we don't bother 1615 * creating the corefile at all. This layout means that 1616 * a corefile is truncated instead of not being created, 1617 * if it is larger than the limit. 1618 */ 1619 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1620 if (limit == 0) 1621 return 0; 1622 1623 restart: 1624 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1625 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1626 flags = O_CREAT | FWRITE | O_NOFOLLOW; 1627 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 1628 free(name, M_TEMP); 1629 if (error) 1630 return (error); 1631 NDFREE(&nd, NDF_ONLY_PNBUF); 1632 vp = nd.ni_vp; 1633 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1634 VOP_UNLOCK(vp, 0, p); 1635 if ((error = vn_close(vp, FWRITE, cred, p)) != 0) 1636 return (error); 1637 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1638 return (error); 1639 goto restart; 1640 } 1641 1642 /* Don't dump to non-regular files or files with links. */ 1643 if (vp->v_type != VREG || 1644 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1645 error = EFAULT; 1646 goto out; 1647 } 1648 VATTR_NULL(&vattr); 1649 vattr.va_size = 0; 1650 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1651 VOP_SETATTR(vp, &vattr, cred, p); 1652 p->p_acflag |= ACORE; 1653 1654 error = p->p_sysent->sv_coredump ? 1655 p->p_sysent->sv_coredump(p, vp, limit) : 1656 ENOSYS; 1657 1658 out: 1659 VOP_UNLOCK(vp, 0, p); 1660 vn_finished_write(mp); 1661 error1 = vn_close(vp, FWRITE, cred, p); 1662 if (error == 0) 1663 error = error1; 1664 return (error); 1665 } 1666 1667 /* 1668 * Nonexistent system call-- signal process (may want to handle it). 1669 * Flag error in case process won't see signal immediately (blocked or ignored). 1670 */ 1671 #ifndef _SYS_SYSPROTO_H_ 1672 struct nosys_args { 1673 int dummy; 1674 }; 1675 #endif 1676 /* ARGSUSED */ 1677 int 1678 nosys(p, args) 1679 struct proc *p; 1680 struct nosys_args *args; 1681 { 1682 1683 psignal(p, SIGSYS); 1684 return (EINVAL); 1685 } 1686 1687 /* 1688 * Send a signal to a SIGIO or SIGURG to a process or process group using 1689 * stored credentials rather than those of the current process. 1690 */ 1691 void 1692 pgsigio(sigio, sig, checkctty) 1693 struct sigio *sigio; 1694 int sig, checkctty; 1695 { 1696 if (sigio == NULL) 1697 return; 1698 1699 if (sigio->sio_pgid > 0) { 1700 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1701 sigio->sio_proc)) 1702 psignal(sigio->sio_proc, sig); 1703 } else if (sigio->sio_pgid < 0) { 1704 struct proc *p; 1705 1706 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) 1707 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1708 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1709 psignal(p, sig); 1710 } 1711 } 1712 1713 static int 1714 filt_sigattach(struct knote *kn) 1715 { 1716 struct proc *p = curproc; 1717 1718 kn->kn_ptr.p_proc = p; 1719 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1720 1721 /* XXX lock the proc here while adding to the list? */ 1722 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 1723 1724 return (0); 1725 } 1726 1727 static void 1728 filt_sigdetach(struct knote *kn) 1729 { 1730 struct proc *p = kn->kn_ptr.p_proc; 1731 1732 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 1733 } 1734 1735 /* 1736 * signal knotes are shared with proc knotes, so we apply a mask to 1737 * the hint in order to differentiate them from process hints. This 1738 * could be avoided by using a signal-specific knote list, but probably 1739 * isn't worth the trouble. 1740 */ 1741 static int 1742 filt_signal(struct knote *kn, long hint) 1743 { 1744 1745 if (hint & NOTE_SIGNAL) { 1746 hint &= ~NOTE_SIGNAL; 1747 1748 if (kn->kn_id == hint) 1749 kn->kn_data++; 1750 } 1751 return (kn->kn_data != 0); 1752 } 1753