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