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