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