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