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