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