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; 786 787 if (uap->oss != NULL) { 788 ss.ss_sp = p->p_sigstk.ss_sp; 789 ss.ss_onstack = sigonstack(cpu_getstack(p)); 790 error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 791 if (error) 792 return (error); 793 } 794 795 if (uap->nss != NULL) { 796 if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 797 return (error); 798 p->p_sigstk.ss_sp = ss.ss_sp; 799 p->p_sigstk.ss_size = 0; 800 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 801 p->p_flag |= P_ALTSTACK; 802 } 803 return (0); 804 } 805 #endif /* COMPAT_43 || COMPAT_SUNOS */ 806 807 #ifndef _SYS_SYSPROTO_H_ 808 struct sigaltstack_args { 809 stack_t *ss; 810 stack_t *oss; 811 }; 812 #endif 813 /* ARGSUSED */ 814 int 815 sigaltstack(p, uap) 816 struct proc *p; 817 register struct sigaltstack_args *uap; 818 { 819 stack_t ss; 820 int error, oonstack; 821 822 oonstack = sigonstack(cpu_getstack(p)); 823 824 if (uap->oss != NULL) { 825 ss = p->p_sigstk; 826 ss.ss_flags = (p->p_flag & P_ALTSTACK) 827 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 828 if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0) 829 return (error); 830 } 831 832 if (uap->ss != NULL) { 833 if (oonstack) 834 return (EPERM); 835 if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0) 836 return (error); 837 if ((ss.ss_flags & ~SS_DISABLE) != 0) 838 return (EINVAL); 839 if (!(ss.ss_flags & SS_DISABLE)) { 840 if (ss.ss_size < p->p_sysent->sv_minsigstksz) 841 return (ENOMEM); 842 p->p_sigstk = ss; 843 p->p_flag |= P_ALTSTACK; 844 } else 845 p->p_flag &= ~P_ALTSTACK; 846 } 847 return (0); 848 } 849 850 /* 851 * Common code for kill process group/broadcast kill. 852 * cp is calling process. 853 */ 854 int 855 killpg1(cp, sig, pgid, all) 856 register struct proc *cp; 857 int sig, pgid, all; 858 { 859 register struct proc *p; 860 struct pgrp *pgrp; 861 int nfound = 0; 862 863 if (all) { 864 /* 865 * broadcast 866 */ 867 ALLPROC_LOCK(AP_SHARED); 868 LIST_FOREACH(p, &allproc, p_list) { 869 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 870 p == cp || !CANSIGNAL(cp, p, sig)) 871 continue; 872 nfound++; 873 if (sig) 874 psignal(p, sig); 875 } 876 ALLPROC_LOCK(AP_RELEASE); 877 } else { 878 if (pgid == 0) 879 /* 880 * zero pgid means send to my process group. 881 */ 882 pgrp = cp->p_pgrp; 883 else { 884 pgrp = pgfind(pgid); 885 if (pgrp == NULL) 886 return (ESRCH); 887 } 888 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 889 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 890 p->p_stat == SZOMB || 891 !CANSIGNAL(cp, p, sig)) 892 continue; 893 nfound++; 894 if (sig) 895 psignal(p, sig); 896 } 897 } 898 return (nfound ? 0 : ESRCH); 899 } 900 901 #ifndef _SYS_SYSPROTO_H_ 902 struct kill_args { 903 int pid; 904 int signum; 905 }; 906 #endif 907 /* ARGSUSED */ 908 int 909 kill(cp, uap) 910 register struct proc *cp; 911 register struct kill_args *uap; 912 { 913 register struct proc *p; 914 915 if ((u_int)uap->signum > _SIG_MAXSIG) 916 return (EINVAL); 917 if (uap->pid > 0) { 918 /* kill single process */ 919 if ((p = pfind(uap->pid)) == NULL) 920 return (ESRCH); 921 if (!CANSIGNAL(cp, p, uap->signum)) 922 return (EPERM); 923 if (uap->signum) 924 psignal(p, uap->signum); 925 return (0); 926 } 927 switch (uap->pid) { 928 case -1: /* broadcast signal */ 929 return (killpg1(cp, uap->signum, 0, 1)); 930 case 0: /* signal own process group */ 931 return (killpg1(cp, uap->signum, 0, 0)); 932 default: /* negative explicit process group */ 933 return (killpg1(cp, uap->signum, -uap->pid, 0)); 934 } 935 /* NOTREACHED */ 936 } 937 938 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 939 #ifndef _SYS_SYSPROTO_H_ 940 struct okillpg_args { 941 int pgid; 942 int signum; 943 }; 944 #endif 945 /* ARGSUSED */ 946 int 947 okillpg(p, uap) 948 struct proc *p; 949 register struct okillpg_args *uap; 950 { 951 952 if ((u_int)uap->signum > _SIG_MAXSIG) 953 return (EINVAL); 954 return (killpg1(p, uap->signum, uap->pgid, 0)); 955 } 956 #endif /* COMPAT_43 || COMPAT_SUNOS */ 957 958 /* 959 * Send a signal to a process group. 960 */ 961 void 962 gsignal(pgid, sig) 963 int pgid, sig; 964 { 965 struct pgrp *pgrp; 966 967 if (pgid && (pgrp = pgfind(pgid))) 968 pgsignal(pgrp, sig, 0); 969 } 970 971 /* 972 * Send a signal to a process group. If checktty is 1, 973 * limit to members which have a controlling terminal. 974 */ 975 void 976 pgsignal(pgrp, sig, checkctty) 977 struct pgrp *pgrp; 978 int sig, checkctty; 979 { 980 register struct proc *p; 981 982 if (pgrp) 983 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 984 if (checkctty == 0 || p->p_flag & P_CONTROLT) 985 psignal(p, sig); 986 } 987 988 /* 989 * Send a signal caused by a trap to the current process. 990 * If it will be caught immediately, deliver it with correct code. 991 * Otherwise, post it normally. 992 */ 993 void 994 trapsignal(p, sig, code) 995 struct proc *p; 996 register int sig; 997 u_long code; 998 { 999 register struct sigacts *ps = p->p_sigacts; 1000 1001 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1002 SIGISMEMBER(p->p_sigmask, sig)) { 1003 p->p_stats->p_ru.ru_nsignals++; 1004 #ifdef KTRACE 1005 if (KTRPOINT(p, KTR_PSIG)) 1006 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 1007 &p->p_sigmask, code); 1008 #endif 1009 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 1010 &p->p_sigmask, code); 1011 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1012 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1013 SIGADDSET(p->p_sigmask, sig); 1014 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1015 /* 1016 * See do_sigaction() for origin of this code. 1017 */ 1018 SIGDELSET(p->p_sigcatch, sig); 1019 if (sig != SIGCONT && 1020 sigprop(sig) & SA_IGNORE) 1021 SIGADDSET(p->p_sigignore, sig); 1022 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1023 } 1024 } else { 1025 p->p_code = code; /* XXX for core dump/debugger */ 1026 p->p_sig = sig; /* XXX to verify code */ 1027 psignal(p, sig); 1028 } 1029 } 1030 1031 /* 1032 * Send the signal to the process. If the signal has an action, the action 1033 * is usually performed by the target process rather than the caller; we add 1034 * the signal to the set of pending signals for the process. 1035 * 1036 * Exceptions: 1037 * o When a stop signal is sent to a sleeping process that takes the 1038 * default action, the process is stopped without awakening it. 1039 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1040 * regardless of the signal action (eg, blocked or ignored). 1041 * 1042 * Other ignored signals are discarded immediately. 1043 */ 1044 void 1045 psignal(p, sig) 1046 register struct proc *p; 1047 register int sig; 1048 { 1049 register int s, prop; 1050 register sig_t action; 1051 1052 if (sig > _SIG_MAXSIG || sig <= 0) { 1053 printf("psignal: signal %d\n", sig); 1054 panic("psignal signal number"); 1055 } 1056 1057 KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1058 1059 prop = sigprop(sig); 1060 1061 /* 1062 * If proc is traced, always give parent a chance; 1063 * if signal event is tracked by procfs, give *that* 1064 * a chance, as well. 1065 */ 1066 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 1067 action = SIG_DFL; 1068 else { 1069 /* 1070 * If the signal is being ignored, 1071 * then we forget about it immediately. 1072 * (Note: we don't set SIGCONT in p_sigignore, 1073 * and if it is set to SIG_IGN, 1074 * action will be SIG_DFL here.) 1075 */ 1076 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1077 return; 1078 if (SIGISMEMBER(p->p_sigmask, sig)) 1079 action = SIG_HOLD; 1080 else if (SIGISMEMBER(p->p_sigcatch, sig)) 1081 action = SIG_CATCH; 1082 else 1083 action = SIG_DFL; 1084 } 1085 1086 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 1087 (p->p_flag & P_TRACED) == 0) 1088 p->p_nice = NZERO; 1089 1090 if (prop & SA_CONT) 1091 SIG_STOPSIGMASK(p->p_siglist); 1092 1093 if (prop & SA_STOP) { 1094 /* 1095 * If sending a tty stop signal to a member of an orphaned 1096 * process group, discard the signal here if the action 1097 * is default; don't stop the process below if sleeping, 1098 * and don't clear any pending SIGCONT. 1099 */ 1100 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1101 action == SIG_DFL) 1102 return; 1103 SIG_CONTSIGMASK(p->p_siglist); 1104 } 1105 SIGADDSET(p->p_siglist, sig); 1106 1107 /* 1108 * Defer further processing for signals which are held, 1109 * except that stopped processes must be continued by SIGCONT. 1110 */ 1111 mtx_enter(&sched_lock, MTX_SPIN); 1112 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) { 1113 mtx_exit(&sched_lock, MTX_SPIN); 1114 return; 1115 } 1116 s = splhigh(); 1117 switch (p->p_stat) { 1118 1119 case SSLEEP: 1120 /* 1121 * If process is sleeping uninterruptibly 1122 * we can't interrupt the sleep... the signal will 1123 * be noticed when the process returns through 1124 * trap() or syscall(). 1125 */ 1126 if ((p->p_flag & P_SINTR) == 0) 1127 goto out; 1128 /* 1129 * Process is sleeping and traced... make it runnable 1130 * so it can discover the signal in issignal() and stop 1131 * for the parent. 1132 */ 1133 if (p->p_flag & P_TRACED) 1134 goto run; 1135 /* 1136 * If SIGCONT is default (or ignored) and process is 1137 * asleep, we are finished; the process should not 1138 * be awakened. 1139 */ 1140 if ((prop & SA_CONT) && action == SIG_DFL) { 1141 SIGDELSET(p->p_siglist, sig); 1142 goto out; 1143 } 1144 /* 1145 * When a sleeping process receives a stop 1146 * signal, process immediately if possible. 1147 * All other (caught or default) signals 1148 * cause the process to run. 1149 */ 1150 if (prop & SA_STOP) { 1151 if (action != SIG_DFL) 1152 goto runfast; 1153 /* 1154 * If a child holding parent blocked, 1155 * stopping could cause deadlock. 1156 */ 1157 if (p->p_flag & P_PPWAIT) 1158 goto out; 1159 SIGDELSET(p->p_siglist, sig); 1160 p->p_xstat = sig; 1161 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1162 psignal(p->p_pptr, SIGCHLD); 1163 stop(p); 1164 goto out; 1165 } else 1166 goto runfast; 1167 /*NOTREACHED*/ 1168 1169 case SSTOP: 1170 /* 1171 * If traced process is already stopped, 1172 * then no further action is necessary. 1173 */ 1174 if (p->p_flag & P_TRACED) 1175 goto out; 1176 1177 /* 1178 * Kill signal always sets processes running. 1179 */ 1180 if (sig == SIGKILL) 1181 goto runfast; 1182 1183 if (prop & SA_CONT) { 1184 /* 1185 * If SIGCONT is default (or ignored), we continue the 1186 * process but don't leave the signal in p_siglist, as 1187 * it has no further action. If SIGCONT is held, we 1188 * continue the process and leave the signal in 1189 * p_siglist. If the process catches SIGCONT, let it 1190 * handle the signal itself. If it isn't waiting on 1191 * an event, then it goes back to run state. 1192 * Otherwise, process goes back to sleep state. 1193 */ 1194 if (action == SIG_DFL) 1195 SIGDELSET(p->p_siglist, sig); 1196 if (action == SIG_CATCH) 1197 goto runfast; 1198 if (p->p_wchan == 0) 1199 goto run; 1200 p->p_stat = SSLEEP; 1201 goto out; 1202 } 1203 1204 if (prop & SA_STOP) { 1205 /* 1206 * Already stopped, don't need to stop again. 1207 * (If we did the shell could get confused.) 1208 */ 1209 SIGDELSET(p->p_siglist, sig); 1210 goto out; 1211 } 1212 1213 /* 1214 * If process is sleeping interruptibly, then simulate a 1215 * wakeup so that when it is continued, it will be made 1216 * runnable and can look at the signal. But don't make 1217 * the process runnable, leave it stopped. 1218 */ 1219 if (p->p_wchan && p->p_flag & P_SINTR) 1220 unsleep(p); 1221 goto out; 1222 1223 default: 1224 /* 1225 * SRUN, SIDL, SZOMB do nothing with the signal, 1226 * other than kicking ourselves if we are running. 1227 * It will either never be noticed, or noticed very soon. 1228 */ 1229 if (p == curproc) 1230 signotify(p); 1231 #ifdef SMP 1232 else if (p->p_stat == SRUN) 1233 forward_signal(p); 1234 #endif 1235 goto out; 1236 } 1237 /*NOTREACHED*/ 1238 1239 runfast: 1240 /* 1241 * Raise priority to at least PUSER. 1242 */ 1243 if (p->p_priority > PUSER) 1244 p->p_priority = PUSER; 1245 run: 1246 setrunnable(p); 1247 out: 1248 mtx_exit(&sched_lock, MTX_SPIN); 1249 splx(s); 1250 } 1251 1252 /* 1253 * If the current process has received a signal (should be caught or cause 1254 * termination, should interrupt current syscall), return the signal number. 1255 * Stop signals with default action are processed immediately, then cleared; 1256 * they aren't returned. This is checked after each entry to the system for 1257 * a syscall or trap (though this can usually be done without calling issignal 1258 * by checking the pending signal masks in the CURSIG macro.) The normal call 1259 * sequence is 1260 * 1261 * while (sig = CURSIG(curproc)) 1262 * postsig(sig); 1263 */ 1264 int 1265 issignal(p) 1266 register struct proc *p; 1267 { 1268 sigset_t mask; 1269 register int sig, prop; 1270 1271 for (;;) { 1272 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 1273 1274 mask = p->p_siglist; 1275 SIGSETNAND(mask, p->p_sigmask); 1276 if (p->p_flag & P_PPWAIT) 1277 SIG_STOPSIGMASK(mask); 1278 if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1279 return (0); 1280 sig = sig_ffs(&mask); 1281 prop = sigprop(sig); 1282 1283 STOPEVENT(p, S_SIG, sig); 1284 1285 /* 1286 * We should see pending but ignored signals 1287 * only if P_TRACED was on when they were posted. 1288 */ 1289 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 1290 SIGDELSET(p->p_siglist, sig); 1291 continue; 1292 } 1293 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1294 /* 1295 * If traced, always stop, and stay 1296 * stopped until released by the parent. 1297 */ 1298 p->p_xstat = sig; 1299 psignal(p->p_pptr, SIGCHLD); 1300 do { 1301 stop(p); 1302 mtx_enter(&sched_lock, MTX_SPIN); 1303 DROP_GIANT_NOSWITCH(); 1304 mi_switch(); 1305 mtx_exit(&sched_lock, MTX_SPIN); 1306 PICKUP_GIANT(); 1307 } while (!trace_req(p) 1308 && p->p_flag & P_TRACED); 1309 1310 /* 1311 * If the traced bit got turned off, go back up 1312 * to the top to rescan signals. This ensures 1313 * that p_sig* and ps_sigact are consistent. 1314 */ 1315 if ((p->p_flag & P_TRACED) == 0) 1316 continue; 1317 1318 /* 1319 * If parent wants us to take the signal, 1320 * then it will leave it in p->p_xstat; 1321 * otherwise we just look for signals again. 1322 */ 1323 SIGDELSET(p->p_siglist, sig); /* clear old signal */ 1324 sig = p->p_xstat; 1325 if (sig == 0) 1326 continue; 1327 1328 /* 1329 * Put the new signal into p_siglist. If the 1330 * signal is being masked, look for other signals. 1331 */ 1332 SIGADDSET(p->p_siglist, sig); 1333 if (SIGISMEMBER(p->p_sigmask, sig)) 1334 continue; 1335 } 1336 1337 /* 1338 * Decide whether the signal should be returned. 1339 * Return the signal's number, or fall through 1340 * to clear it from the pending mask. 1341 */ 1342 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1343 1344 case (int)SIG_DFL: 1345 /* 1346 * Don't take default actions on system processes. 1347 */ 1348 if (p->p_pid <= 1) { 1349 #ifdef DIAGNOSTIC 1350 /* 1351 * Are you sure you want to ignore SIGSEGV 1352 * in init? XXX 1353 */ 1354 printf("Process (pid %lu) got signal %d\n", 1355 (u_long)p->p_pid, sig); 1356 #endif 1357 break; /* == ignore */ 1358 } 1359 /* 1360 * If there is a pending stop signal to process 1361 * with default action, stop here, 1362 * then clear the signal. However, 1363 * if process is member of an orphaned 1364 * process group, ignore tty stop signals. 1365 */ 1366 if (prop & SA_STOP) { 1367 if (p->p_flag & P_TRACED || 1368 (p->p_pgrp->pg_jobc == 0 && 1369 prop & SA_TTYSTOP)) 1370 break; /* == ignore */ 1371 p->p_xstat = sig; 1372 stop(p); 1373 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1374 psignal(p->p_pptr, SIGCHLD); 1375 mtx_enter(&sched_lock, MTX_SPIN); 1376 DROP_GIANT_NOSWITCH(); 1377 mi_switch(); 1378 mtx_exit(&sched_lock, MTX_SPIN); 1379 PICKUP_GIANT(); 1380 break; 1381 } else if (prop & SA_IGNORE) { 1382 /* 1383 * Except for SIGCONT, shouldn't get here. 1384 * Default action is to ignore; drop it. 1385 */ 1386 break; /* == ignore */ 1387 } else 1388 return (sig); 1389 /*NOTREACHED*/ 1390 1391 case (int)SIG_IGN: 1392 /* 1393 * Masking above should prevent us ever trying 1394 * to take action on an ignored signal other 1395 * than SIGCONT, unless process is traced. 1396 */ 1397 if ((prop & SA_CONT) == 0 && 1398 (p->p_flag & P_TRACED) == 0) 1399 printf("issignal\n"); 1400 break; /* == ignore */ 1401 1402 default: 1403 /* 1404 * This signal has an action, let 1405 * postsig() process it. 1406 */ 1407 return (sig); 1408 } 1409 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1410 } 1411 /* NOTREACHED */ 1412 } 1413 1414 /* 1415 * Put the argument process into the stopped state and notify the parent 1416 * via wakeup. Signals are handled elsewhere. The process must not be 1417 * on the run queue. 1418 */ 1419 void 1420 stop(p) 1421 register struct proc *p; 1422 { 1423 1424 mtx_enter(&sched_lock, MTX_SPIN); 1425 p->p_stat = SSTOP; 1426 p->p_flag &= ~P_WAITED; 1427 wakeup((caddr_t)p->p_pptr); 1428 mtx_exit(&sched_lock, MTX_SPIN); 1429 } 1430 1431 /* 1432 * Take the action for the specified signal 1433 * from the current set of pending signals. 1434 */ 1435 void 1436 postsig(sig) 1437 register int sig; 1438 { 1439 register struct proc *p = curproc; 1440 struct sigacts *ps = p->p_sigacts; 1441 sig_t action; 1442 sigset_t returnmask; 1443 int code; 1444 1445 KASSERT(sig != 0, ("postsig")); 1446 1447 SIGDELSET(p->p_siglist, sig); 1448 action = ps->ps_sigact[_SIG_IDX(sig)]; 1449 #ifdef KTRACE 1450 if (KTRPOINT(p, KTR_PSIG)) 1451 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 1452 &p->p_oldsigmask : &p->p_sigmask, 0); 1453 #endif 1454 STOPEVENT(p, S_SIG, sig); 1455 1456 if (action == SIG_DFL) { 1457 /* 1458 * Default action, where the default is to kill 1459 * the process. (Other cases were ignored above.) 1460 */ 1461 sigexit(p, sig); 1462 /* NOTREACHED */ 1463 } else { 1464 /* 1465 * If we get here, the signal must be caught. 1466 */ 1467 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 1468 ("postsig action")); 1469 /* 1470 * Set the new mask value and also defer further 1471 * occurrences of this signal. 1472 * 1473 * Special case: user has done a sigsuspend. Here the 1474 * current mask is not of interest, but rather the 1475 * mask from before the sigsuspend is what we want 1476 * restored after the signal processing is completed. 1477 */ 1478 (void) splhigh(); 1479 if (p->p_flag & P_OLDMASK) { 1480 returnmask = p->p_oldsigmask; 1481 p->p_flag &= ~P_OLDMASK; 1482 } else 1483 returnmask = p->p_sigmask; 1484 1485 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1486 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1487 SIGADDSET(p->p_sigmask, sig); 1488 1489 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1490 /* 1491 * See do_sigaction() for origin of this code. 1492 */ 1493 SIGDELSET(p->p_sigcatch, sig); 1494 if (sig != SIGCONT && 1495 sigprop(sig) & SA_IGNORE) 1496 SIGADDSET(p->p_sigignore, sig); 1497 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1498 } 1499 (void) spl0(); 1500 p->p_stats->p_ru.ru_nsignals++; 1501 if (p->p_sig != sig) { 1502 code = 0; 1503 } else { 1504 code = p->p_code; 1505 p->p_code = 0; 1506 p->p_sig = 0; 1507 } 1508 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1509 } 1510 } 1511 1512 /* 1513 * Kill the current process for stated reason. 1514 */ 1515 void 1516 killproc(p, why) 1517 struct proc *p; 1518 char *why; 1519 { 1520 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 1521 p, p->p_pid, p->p_comm); 1522 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1523 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1524 psignal(p, SIGKILL); 1525 } 1526 1527 /* 1528 * Force the current process to exit with the specified signal, dumping core 1529 * if appropriate. We bypass the normal tests for masked and caught signals, 1530 * allowing unrecoverable failures to terminate the process without changing 1531 * signal state. Mark the accounting record with the signal termination. 1532 * If dumping core, save the signal number for the debugger. Calls exit and 1533 * does not return. 1534 */ 1535 void 1536 sigexit(p, sig) 1537 register struct proc *p; 1538 int sig; 1539 { 1540 1541 p->p_acflag |= AXSIG; 1542 if (sigprop(sig) & SA_CORE) { 1543 p->p_sig = sig; 1544 /* 1545 * Log signals which would cause core dumps 1546 * (Log as LOG_INFO to appease those who don't want 1547 * these messages.) 1548 * XXX : Todo, as well as euid, write out ruid too 1549 */ 1550 if (coredump(p) == 0) 1551 sig |= WCOREFLAG; 1552 if (kern_logsigexit) 1553 log(LOG_INFO, 1554 "pid %d (%s), uid %d: exited on signal %d%s\n", 1555 p->p_pid, p->p_comm, 1556 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 1557 sig &~ WCOREFLAG, 1558 sig & WCOREFLAG ? " (core dumped)" : ""); 1559 } 1560 exit1(p, W_EXITCODE(0, sig)); 1561 /* NOTREACHED */ 1562 } 1563 1564 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1565 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1566 sizeof(corefilename), "process corefile name format string"); 1567 1568 /* 1569 * expand_name(name, uid, pid) 1570 * Expand the name described in corefilename, using name, uid, and pid. 1571 * corefilename is a printf-like string, with three format specifiers: 1572 * %N name of process ("name") 1573 * %P process id (pid) 1574 * %U user id (uid) 1575 * For example, "%N.core" is the default; they can be disabled completely 1576 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1577 * This is controlled by the sysctl variable kern.corefile (see above). 1578 */ 1579 1580 static char * 1581 expand_name(name, uid, pid) 1582 const char *name; uid_t uid; pid_t pid; { 1583 char *temp; 1584 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1585 int i, n; 1586 char *format = corefilename; 1587 size_t namelen; 1588 1589 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 1590 if (temp == NULL) 1591 return NULL; 1592 namelen = strlen(name); 1593 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1594 int l; 1595 switch (format[i]) { 1596 case '%': /* Format character */ 1597 i++; 1598 switch (format[i]) { 1599 case '%': 1600 temp[n++] = '%'; 1601 break; 1602 case 'N': /* process name */ 1603 if ((n + namelen) > MAXPATHLEN) { 1604 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1605 pid, name, uid, temp, name); 1606 free(temp, M_TEMP); 1607 return NULL; 1608 } 1609 memcpy(temp+n, name, namelen); 1610 n += namelen; 1611 break; 1612 case 'P': /* process id */ 1613 l = sprintf(buf, "%u", pid); 1614 if ((n + l) > MAXPATHLEN) { 1615 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1616 pid, name, uid, temp, name); 1617 free(temp, M_TEMP); 1618 return NULL; 1619 } 1620 memcpy(temp+n, buf, l); 1621 n += l; 1622 break; 1623 case 'U': /* user id */ 1624 l = sprintf(buf, "%u", uid); 1625 if ((n + l) > MAXPATHLEN) { 1626 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1627 pid, name, uid, temp, name); 1628 free(temp, M_TEMP); 1629 return NULL; 1630 } 1631 memcpy(temp+n, buf, l); 1632 n += l; 1633 break; 1634 default: 1635 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1636 } 1637 break; 1638 default: 1639 temp[n++] = format[i]; 1640 } 1641 } 1642 temp[n] = '\0'; 1643 return temp; 1644 } 1645 1646 /* 1647 * Dump a process' core. The main routine does some 1648 * policy checking, and creates the name of the coredump; 1649 * then it passes on a vnode and a size limit to the process-specific 1650 * coredump routine if there is one; if there _is not_ one, it returns 1651 * ENOSYS; otherwise it returns the error from the process-specific routine. 1652 */ 1653 1654 static int 1655 coredump(p) 1656 register struct proc *p; 1657 { 1658 register struct vnode *vp; 1659 register struct ucred *cred = p->p_ucred; 1660 struct nameidata nd; 1661 struct vattr vattr; 1662 int error, error1, flags; 1663 struct mount *mp; 1664 char *name; /* name of corefile */ 1665 off_t limit; 1666 1667 STOPEVENT(p, S_CORE, 0); 1668 1669 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) 1670 return (EFAULT); 1671 1672 /* 1673 * Note that the bulk of limit checking is done after 1674 * the corefile is created. The exception is if the limit 1675 * for corefiles is 0, in which case we don't bother 1676 * creating the corefile at all. This layout means that 1677 * a corefile is truncated instead of not being created, 1678 * if it is larger than the limit. 1679 */ 1680 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1681 if (limit == 0) 1682 return 0; 1683 1684 restart: 1685 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1686 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1687 flags = O_CREAT | FWRITE | O_NOFOLLOW; 1688 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 1689 free(name, M_TEMP); 1690 if (error) 1691 return (error); 1692 NDFREE(&nd, NDF_ONLY_PNBUF); 1693 vp = nd.ni_vp; 1694 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1695 VOP_UNLOCK(vp, 0, p); 1696 if ((error = vn_close(vp, FWRITE, cred, p)) != 0) 1697 return (error); 1698 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1699 return (error); 1700 goto restart; 1701 } 1702 1703 /* Don't dump to non-regular files or files with links. */ 1704 if (vp->v_type != VREG || 1705 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1706 error = EFAULT; 1707 goto out; 1708 } 1709 VATTR_NULL(&vattr); 1710 vattr.va_size = 0; 1711 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1712 VOP_SETATTR(vp, &vattr, cred, p); 1713 p->p_acflag |= ACORE; 1714 1715 error = p->p_sysent->sv_coredump ? 1716 p->p_sysent->sv_coredump(p, vp, limit) : 1717 ENOSYS; 1718 1719 out: 1720 VOP_UNLOCK(vp, 0, p); 1721 vn_finished_write(mp); 1722 error1 = vn_close(vp, FWRITE, cred, p); 1723 if (error == 0) 1724 error = error1; 1725 return (error); 1726 } 1727 1728 /* 1729 * Nonexistent system call-- signal process (may want to handle it). 1730 * Flag error in case process won't see signal immediately (blocked or ignored). 1731 */ 1732 #ifndef _SYS_SYSPROTO_H_ 1733 struct nosys_args { 1734 int dummy; 1735 }; 1736 #endif 1737 /* ARGSUSED */ 1738 int 1739 nosys(p, args) 1740 struct proc *p; 1741 struct nosys_args *args; 1742 { 1743 1744 psignal(p, SIGSYS); 1745 return (EINVAL); 1746 } 1747 1748 /* 1749 * Send a signal to a SIGIO or SIGURG to a process or process group using 1750 * stored credentials rather than those of the current process. 1751 */ 1752 void 1753 pgsigio(sigio, sig, checkctty) 1754 struct sigio *sigio; 1755 int sig, checkctty; 1756 { 1757 if (sigio == NULL) 1758 return; 1759 1760 if (sigio->sio_pgid > 0) { 1761 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1762 sigio->sio_proc)) 1763 psignal(sigio->sio_proc, sig); 1764 } else if (sigio->sio_pgid < 0) { 1765 struct proc *p; 1766 1767 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) 1768 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1769 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1770 psignal(p, sig); 1771 } 1772 } 1773 1774 static int 1775 filt_sigattach(struct knote *kn) 1776 { 1777 struct proc *p = curproc; 1778 1779 kn->kn_ptr.p_proc = p; 1780 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1781 1782 /* XXX lock the proc here while adding to the list? */ 1783 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 1784 1785 return (0); 1786 } 1787 1788 static void 1789 filt_sigdetach(struct knote *kn) 1790 { 1791 struct proc *p = kn->kn_ptr.p_proc; 1792 1793 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 1794 } 1795 1796 /* 1797 * signal knotes are shared with proc knotes, so we apply a mask to 1798 * the hint in order to differentiate them from process hints. This 1799 * could be avoided by using a signal-specific knote list, but probably 1800 * isn't worth the trouble. 1801 */ 1802 static int 1803 filt_signal(struct knote *kn, long hint) 1804 { 1805 1806 if (hint & NOTE_SIGNAL) { 1807 hint &= ~NOTE_SIGNAL; 1808 1809 if (kn->kn_id == hint) 1810 kn->kn_data++; 1811 } 1812 return (kn->kn_data != 0); 1813 } 1814