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: src/sys/kern/kern_sig.c,v 1.72.2.17 2003/05/16 16:34:34 obrien Exp $ 40 * $DragonFly: src/sys/kern/kern_sig.c,v 1.50 2006/06/05 07:26:10 dillon Exp $ 41 */ 42 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/sysproto.h> 49 #include <sys/signalvar.h> 50 #include <sys/resourcevar.h> 51 #include <sys/vnode.h> 52 #include <sys/event.h> 53 #include <sys/proc.h> 54 #include <sys/nlookup.h> 55 #include <sys/pioctl.h> 56 #include <sys/systm.h> 57 #include <sys/acct.h> 58 #include <sys/fcntl.h> 59 #include <sys/wait.h> 60 #include <sys/ktrace.h> 61 #include <sys/syslog.h> 62 #include <sys/stat.h> 63 #include <sys/sysent.h> 64 #include <sys/sysctl.h> 65 #include <sys/malloc.h> 66 #include <sys/unistd.h> 67 #include <sys/kern_syscall.h> 68 #include <sys/thread2.h> 69 70 71 #include <machine/ipl.h> 72 #include <machine/cpu.h> 73 #include <machine/smp.h> 74 75 static int coredump(struct proc *); 76 static char *expand_name(const char *, uid_t, pid_t); 77 static int killpg(int sig, int pgid, int all); 78 static int sig_ffs(sigset_t *set); 79 static int sigprop(int sig); 80 #ifdef SMP 81 static void signotify_remote(void *arg); 82 #endif 83 static int kern_sigtimedwait(sigset_t set, siginfo_t *info, 84 struct timespec *timeout); 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(q, sig) \ 102 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \ 103 ((sig) == SIGCONT && (q)->p_session == curproc->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_ucred->cr_ruid || \ 111 (uc)->cr_uid == (q)->p_ucred->cr_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 #define SA_CKPT 0x80 /* checkpoint process */ 136 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 SA_IGNORE, /* SIGTHR */ 171 SA_CKPT, /* SIGCKPT */ 172 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */ 173 SA_IGNORE, 174 SA_IGNORE, 175 SA_IGNORE, 176 SA_IGNORE, 177 SA_IGNORE, 178 SA_IGNORE, 179 SA_IGNORE, 180 SA_IGNORE, 181 SA_IGNORE, 182 SA_IGNORE, 183 SA_IGNORE, 184 SA_IGNORE, 185 SA_IGNORE, 186 SA_IGNORE, 187 SA_IGNORE, 188 SA_IGNORE, 189 SA_IGNORE, 190 SA_IGNORE, 191 SA_IGNORE, 192 SA_IGNORE, 193 SA_IGNORE, 194 SA_IGNORE, 195 SA_IGNORE, 196 SA_IGNORE, 197 SA_IGNORE, 198 SA_IGNORE, 199 SA_IGNORE, 200 SA_IGNORE, 201 SA_IGNORE, 202 SA_IGNORE, 203 204 }; 205 206 static __inline int 207 sigprop(int sig) 208 { 209 210 if (sig > 0 && sig < NSIG) 211 return (sigproptbl[_SIG_IDX(sig)]); 212 return (0); 213 } 214 215 static __inline int 216 sig_ffs(sigset_t *set) 217 { 218 int i; 219 220 for (i = 0; i < _SIG_WORDS; i++) 221 if (set->__bits[i]) 222 return (ffs(set->__bits[i]) + (i * 32)); 223 return (0); 224 } 225 226 int 227 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact) 228 { 229 struct thread *td = curthread; 230 struct proc *p = td->td_proc; 231 struct sigacts *ps = p->p_sigacts; 232 233 if (sig <= 0 || sig > _SIG_MAXSIG) 234 return (EINVAL); 235 236 if (oact) { 237 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 238 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 239 oact->sa_flags = 0; 240 if (SIGISMEMBER(ps->ps_sigonstack, sig)) 241 oact->sa_flags |= SA_ONSTACK; 242 if (!SIGISMEMBER(ps->ps_sigintr, sig)) 243 oact->sa_flags |= SA_RESTART; 244 if (SIGISMEMBER(ps->ps_sigreset, sig)) 245 oact->sa_flags |= SA_RESETHAND; 246 if (SIGISMEMBER(ps->ps_signodefer, sig)) 247 oact->sa_flags |= SA_NODEFER; 248 if (SIGISMEMBER(ps->ps_siginfo, sig)) 249 oact->sa_flags |= SA_SIGINFO; 250 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 251 oact->sa_flags |= SA_NOCLDSTOP; 252 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 253 oact->sa_flags |= SA_NOCLDWAIT; 254 } 255 if (act) { 256 if ((sig == SIGKILL || sig == SIGSTOP) && 257 act->sa_handler != SIG_DFL) 258 return (EINVAL); 259 260 /* 261 * Change setting atomically. 262 */ 263 crit_enter(); 264 265 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 266 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 267 if (act->sa_flags & SA_SIGINFO) { 268 ps->ps_sigact[_SIG_IDX(sig)] = 269 (__sighandler_t *)act->sa_sigaction; 270 SIGADDSET(ps->ps_siginfo, sig); 271 } else { 272 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 273 SIGDELSET(ps->ps_siginfo, sig); 274 } 275 if (!(act->sa_flags & SA_RESTART)) 276 SIGADDSET(ps->ps_sigintr, sig); 277 else 278 SIGDELSET(ps->ps_sigintr, sig); 279 if (act->sa_flags & SA_ONSTACK) 280 SIGADDSET(ps->ps_sigonstack, sig); 281 else 282 SIGDELSET(ps->ps_sigonstack, sig); 283 if (act->sa_flags & SA_RESETHAND) 284 SIGADDSET(ps->ps_sigreset, sig); 285 else 286 SIGDELSET(ps->ps_sigreset, sig); 287 if (act->sa_flags & SA_NODEFER) 288 SIGADDSET(ps->ps_signodefer, sig); 289 else 290 SIGDELSET(ps->ps_signodefer, sig); 291 if (sig == SIGCHLD) { 292 if (act->sa_flags & SA_NOCLDSTOP) 293 p->p_procsig->ps_flag |= PS_NOCLDSTOP; 294 else 295 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 296 if (act->sa_flags & SA_NOCLDWAIT) { 297 /* 298 * Paranoia: since SA_NOCLDWAIT is implemented 299 * by reparenting the dying child to PID 1 (and 300 * trust it to reap the zombie), PID 1 itself 301 * is forbidden to set SA_NOCLDWAIT. 302 */ 303 if (p->p_pid == 1) 304 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 305 else 306 p->p_procsig->ps_flag |= PS_NOCLDWAIT; 307 } else { 308 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 309 } 310 } 311 /* 312 * Set bit in p_sigignore for signals that are set to SIG_IGN, 313 * and for signals set to SIG_DFL where the default is to 314 * ignore. However, don't put SIGCONT in p_sigignore, as we 315 * have to restart the process. 316 */ 317 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 318 (sigprop(sig) & SA_IGNORE && 319 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 320 /* never to be seen again */ 321 SIGDELSET(p->p_siglist, sig); 322 if (sig != SIGCONT) 323 /* easier in psignal */ 324 SIGADDSET(p->p_sigignore, sig); 325 SIGDELSET(p->p_sigcatch, sig); 326 } else { 327 SIGDELSET(p->p_sigignore, sig); 328 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 329 SIGDELSET(p->p_sigcatch, sig); 330 else 331 SIGADDSET(p->p_sigcatch, sig); 332 } 333 334 crit_exit(); 335 } 336 return (0); 337 } 338 339 int 340 sys_sigaction(struct sigaction_args *uap) 341 { 342 struct sigaction act, oact; 343 struct sigaction *actp, *oactp; 344 int error; 345 346 actp = (uap->act != NULL) ? &act : NULL; 347 oactp = (uap->oact != NULL) ? &oact : NULL; 348 if (actp) { 349 error = copyin(uap->act, actp, sizeof(act)); 350 if (error) 351 return (error); 352 } 353 error = kern_sigaction(uap->sig, actp, oactp); 354 if (oactp && !error) { 355 error = copyout(oactp, uap->oact, sizeof(oact)); 356 } 357 return (error); 358 } 359 360 /* 361 * Initialize signal state for process 0; 362 * set to ignore signals that are ignored by default. 363 */ 364 void 365 siginit(struct proc *p) 366 { 367 int i; 368 369 for (i = 1; i <= NSIG; i++) 370 if (sigprop(i) & SA_IGNORE && i != SIGCONT) 371 SIGADDSET(p->p_sigignore, i); 372 } 373 374 /* 375 * Reset signals for an exec of the specified process. 376 */ 377 void 378 execsigs(struct proc *p) 379 { 380 struct sigacts *ps = p->p_sigacts; 381 int sig; 382 383 /* 384 * Reset caught signals. Held signals remain held 385 * through p_sigmask (unless they were caught, 386 * and are now ignored by default). 387 */ 388 while (SIGNOTEMPTY(p->p_sigcatch)) { 389 sig = sig_ffs(&p->p_sigcatch); 390 SIGDELSET(p->p_sigcatch, sig); 391 if (sigprop(sig) & SA_IGNORE) { 392 if (sig != SIGCONT) 393 SIGADDSET(p->p_sigignore, sig); 394 SIGDELSET(p->p_siglist, sig); 395 } 396 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 397 } 398 /* 399 * Reset stack state to the user stack. 400 * Clear set of signals caught on the signal stack. 401 */ 402 p->p_sigstk.ss_flags = SS_DISABLE; 403 p->p_sigstk.ss_size = 0; 404 p->p_sigstk.ss_sp = 0; 405 p->p_flag &= ~P_ALTSTACK; 406 /* 407 * Reset no zombies if child dies flag as Solaris does. 408 */ 409 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 410 } 411 412 /* 413 * kern_sigprocmask() - MP SAFE ONLY IF p == curproc 414 * 415 * Manipulate signal mask. This routine is MP SAFE *ONLY* if 416 * p == curproc. 417 */ 418 int 419 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset) 420 { 421 struct thread *td = curthread; 422 struct proc *p = td->td_proc; 423 int error; 424 425 if (oset != NULL) 426 *oset = p->p_sigmask; 427 428 error = 0; 429 if (set != NULL) { 430 switch (how) { 431 case SIG_BLOCK: 432 SIG_CANTMASK(*set); 433 SIGSETOR(p->p_sigmask, *set); 434 break; 435 case SIG_UNBLOCK: 436 SIGSETNAND(p->p_sigmask, *set); 437 break; 438 case SIG_SETMASK: 439 SIG_CANTMASK(*set); 440 p->p_sigmask = *set; 441 break; 442 default: 443 error = EINVAL; 444 break; 445 } 446 } 447 return (error); 448 } 449 450 /* 451 * sigprocmask() - MP SAFE 452 */ 453 int 454 sys_sigprocmask(struct sigprocmask_args *uap) 455 { 456 sigset_t set, oset; 457 sigset_t *setp, *osetp; 458 int error; 459 460 setp = (uap->set != NULL) ? &set : NULL; 461 osetp = (uap->oset != NULL) ? &oset : NULL; 462 if (setp) { 463 error = copyin(uap->set, setp, sizeof(set)); 464 if (error) 465 return (error); 466 } 467 error = kern_sigprocmask(uap->how, setp, osetp); 468 if (osetp && !error) { 469 error = copyout(osetp, uap->oset, sizeof(oset)); 470 } 471 return (error); 472 } 473 474 int 475 kern_sigpending(struct __sigset *set) 476 { 477 struct thread *td = curthread; 478 struct proc *p = td->td_proc; 479 480 *set = p->p_siglist; 481 482 return (0); 483 } 484 485 int 486 sys_sigpending(struct sigpending_args *uap) 487 { 488 sigset_t set; 489 int error; 490 491 error = kern_sigpending(&set); 492 493 if (error == 0) 494 error = copyout(&set, uap->set, sizeof(set)); 495 return (error); 496 } 497 498 /* 499 * Suspend process until signal, providing mask to be set 500 * in the meantime. 501 */ 502 int 503 kern_sigsuspend(struct __sigset *set) 504 { 505 struct thread *td = curthread; 506 struct proc *p = td->td_proc; 507 struct sigacts *ps = p->p_sigacts; 508 509 /* 510 * When returning from sigsuspend, we want 511 * the old mask to be restored after the 512 * signal handler has finished. Thus, we 513 * save it here and mark the sigacts structure 514 * to indicate this. 515 */ 516 p->p_oldsigmask = p->p_sigmask; 517 p->p_flag |= P_OLDMASK; 518 519 SIG_CANTMASK(*set); 520 p->p_sigmask = *set; 521 while (tsleep(ps, PCATCH, "pause", 0) == 0) 522 /* void */; 523 /* always return EINTR rather than ERESTART... */ 524 return (EINTR); 525 } 526 527 /* 528 * Note nonstandard calling convention: libc stub passes mask, not 529 * pointer, to save a copyin. 530 */ 531 int 532 sys_sigsuspend(struct sigsuspend_args *uap) 533 { 534 sigset_t mask; 535 int error; 536 537 error = copyin(uap->sigmask, &mask, sizeof(mask)); 538 if (error) 539 return (error); 540 541 error = kern_sigsuspend(&mask); 542 543 return (error); 544 } 545 546 int 547 kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss) 548 { 549 struct thread *td = curthread; 550 struct proc *p = td->td_proc; 551 552 if ((p->p_flag & P_ALTSTACK) == 0) 553 p->p_sigstk.ss_flags |= SS_DISABLE; 554 555 if (oss) 556 *oss = p->p_sigstk; 557 558 if (ss) { 559 if (ss->ss_flags & SS_DISABLE) { 560 if (p->p_sigstk.ss_flags & SS_ONSTACK) 561 return (EINVAL); 562 p->p_flag &= ~P_ALTSTACK; 563 p->p_sigstk.ss_flags = ss->ss_flags; 564 } else { 565 if (ss->ss_size < p->p_sysent->sv_minsigstksz) 566 return (ENOMEM); 567 p->p_flag |= P_ALTSTACK; 568 p->p_sigstk = *ss; 569 } 570 } 571 572 return (0); 573 } 574 575 int 576 sys_sigaltstack(struct sigaltstack_args *uap) 577 { 578 stack_t ss, oss; 579 int error; 580 581 if (uap->ss) { 582 error = copyin(uap->ss, &ss, sizeof(ss)); 583 if (error) 584 return (error); 585 } 586 587 error = kern_sigaltstack(uap->ss ? &ss : NULL, 588 uap->oss ? &oss : NULL); 589 590 if (error == 0 && uap->oss) 591 error = copyout(&oss, uap->oss, sizeof(*uap->oss)); 592 return (error); 593 } 594 595 /* 596 * Common code for kill process group/broadcast kill. 597 * cp is calling process. 598 */ 599 struct killpg_info { 600 int nfound; 601 int sig; 602 }; 603 604 static int killpg_all_callback(struct proc *p, void *data); 605 606 static int 607 killpg(int sig, int pgid, int all) 608 { 609 struct killpg_info info; 610 struct proc *cp = curproc; 611 struct proc *p; 612 struct pgrp *pgrp; 613 614 info.nfound = 0; 615 info.sig = sig; 616 617 if (all) { 618 /* 619 * broadcast 620 */ 621 allproc_scan(killpg_all_callback, &info); 622 } else { 623 if (pgid == 0) { 624 /* 625 * zero pgid means send to my process group. 626 */ 627 pgrp = cp->p_pgrp; 628 } else { 629 pgrp = pgfind(pgid); 630 if (pgrp == NULL) 631 return (ESRCH); 632 } 633 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 634 if (p->p_pid <= 1 || 635 (p->p_flag & (P_SYSTEM | P_ZOMBIE)) || 636 !CANSIGNAL(p, sig)) { 637 continue; 638 } 639 ++info.nfound; 640 if (sig) 641 psignal(p, sig); 642 } 643 } 644 return (info.nfound ? 0 : ESRCH); 645 } 646 647 static int 648 killpg_all_callback(struct proc *p, void *data) 649 { 650 struct killpg_info *info = data; 651 652 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) || 653 p == curproc || !CANSIGNAL(p, info->sig)) { 654 return (0); 655 } 656 ++info->nfound; 657 if (info->sig) 658 psignal(p, info->sig); 659 return(0); 660 } 661 662 int 663 kern_kill(int sig, int pid) 664 { 665 struct thread *td = curthread; 666 struct proc *p = td->td_proc; 667 668 if ((u_int)sig > _SIG_MAXSIG) 669 return (EINVAL); 670 if (pid > 0) { 671 /* kill single process */ 672 if ((p = pfind(pid)) == NULL) 673 return (ESRCH); 674 if (!CANSIGNAL(p, sig)) 675 return (EPERM); 676 if (sig) 677 psignal(p, sig); 678 return (0); 679 } 680 switch (pid) { 681 case -1: /* broadcast signal */ 682 return (killpg(sig, 0, 1)); 683 case 0: /* signal own process group */ 684 return (killpg(sig, 0, 0)); 685 default: /* negative explicit process group */ 686 return (killpg(sig, -pid, 0)); 687 } 688 /* NOTREACHED */ 689 } 690 691 int 692 sys_kill(struct kill_args *uap) 693 { 694 int error; 695 696 error = kern_kill(uap->signum, uap->pid); 697 698 return (error); 699 } 700 701 /* 702 * Send a signal to a process group. 703 */ 704 void 705 gsignal(int pgid, int sig) 706 { 707 struct pgrp *pgrp; 708 709 if (pgid && (pgrp = pgfind(pgid))) 710 pgsignal(pgrp, sig, 0); 711 } 712 713 /* 714 * Send a signal to a process group. If checktty is 1, 715 * limit to members which have a controlling terminal. 716 */ 717 void 718 pgsignal(struct pgrp *pgrp, int sig, int checkctty) 719 { 720 struct proc *p; 721 722 if (pgrp) 723 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 724 if (checkctty == 0 || p->p_flag & P_CONTROLT) 725 psignal(p, sig); 726 } 727 728 /* 729 * Send a signal caused by a trap to the current process. 730 * If it will be caught immediately, deliver it with correct code. 731 * Otherwise, post it normally. 732 */ 733 void 734 trapsignal(struct proc *p, int sig, u_long code) 735 { 736 struct sigacts *ps = p->p_sigacts; 737 738 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 739 !SIGISMEMBER(p->p_sigmask, sig)) { 740 p->p_stats->p_ru.ru_nsignals++; 741 #ifdef KTRACE 742 if (KTRPOINT(p->p_thread, KTR_PSIG)) 743 ktrpsig(p, sig, ps->ps_sigact[_SIG_IDX(sig)], 744 &p->p_sigmask, code); 745 #endif 746 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 747 &p->p_sigmask, code); 748 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 749 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 750 SIGADDSET(p->p_sigmask, sig); 751 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 752 /* 753 * See kern_sigaction() for origin of this code. 754 */ 755 SIGDELSET(p->p_sigcatch, sig); 756 if (sig != SIGCONT && 757 sigprop(sig) & SA_IGNORE) 758 SIGADDSET(p->p_sigignore, sig); 759 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 760 } 761 } else { 762 p->p_code = code; /* XXX for core dump/debugger */ 763 p->p_sig = sig; /* XXX to verify code */ 764 psignal(p, sig); 765 } 766 } 767 768 /* 769 * Send the signal to the process. If the signal has an action, the action 770 * is usually performed by the target process rather than the caller; we add 771 * the signal to the set of pending signals for the process. 772 * 773 * Exceptions: 774 * o When a stop signal is sent to a sleeping process that takes the 775 * default action, the process is stopped without awakening it. 776 * o SIGCONT restarts stopped processes (or puts them back to sleep) 777 * regardless of the signal action (eg, blocked or ignored). 778 * 779 * Other ignored signals are discarded immediately. 780 */ 781 void 782 psignal(struct proc *p, int sig) 783 { 784 struct lwp *lp = &p->p_lwp; 785 int prop; 786 sig_t action; 787 788 if (sig > _SIG_MAXSIG || sig <= 0) { 789 printf("psignal: signal %d\n", sig); 790 panic("psignal signal number"); 791 } 792 793 crit_enter(); 794 KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 795 crit_exit(); 796 797 prop = sigprop(sig); 798 799 /* 800 * If proc is traced, always give parent a chance; 801 * if signal event is tracked by procfs, give *that* 802 * a chance, as well. 803 */ 804 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 805 action = SIG_DFL; 806 } else { 807 /* 808 * If the signal is being ignored, 809 * then we forget about it immediately. 810 * (Note: we don't set SIGCONT in p_sigignore, 811 * and if it is set to SIG_IGN, 812 * action will be SIG_DFL here.) 813 */ 814 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 815 return; 816 if (SIGISMEMBER(p->p_sigmask, sig)) 817 action = SIG_HOLD; 818 else if (SIGISMEMBER(p->p_sigcatch, sig)) 819 action = SIG_CATCH; 820 else 821 action = SIG_DFL; 822 } 823 824 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 825 (p->p_flag & P_TRACED) == 0) { 826 p->p_nice = NZERO; 827 } 828 829 /* 830 * If continuing, clear any pending STOP signals. 831 */ 832 if (prop & SA_CONT) 833 SIG_STOPSIGMASK(p->p_siglist); 834 835 if (prop & SA_STOP) { 836 /* 837 * If sending a tty stop signal to a member of an orphaned 838 * process group, discard the signal here if the action 839 * is default; don't stop the process below if sleeping, 840 * and don't clear any pending SIGCONT. 841 */ 842 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 843 action == SIG_DFL) { 844 return; 845 } 846 SIG_CONTSIGMASK(p->p_siglist); 847 } 848 SIGADDSET(p->p_siglist, sig); 849 850 /* 851 * Defer further processing for signals which are held, 852 * except that stopped processes must be continued by SIGCONT. 853 */ 854 if (action == SIG_HOLD) { 855 if ((prop & SA_CONT) == 0 || (p->p_flag & P_STOPPED) == 0) 856 return; 857 } 858 859 crit_enter(); 860 861 /* 862 * Process is in tsleep and not stopped 863 */ 864 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED) == 0) { 865 /* 866 * If the process is sleeping uninterruptibly 867 * we can't interrupt the sleep... the signal will 868 * be noticed when the process returns through 869 * trap() or syscall(). 870 */ 871 if ((p->p_flag & P_SINTR) == 0) 872 goto out; 873 874 /* 875 * If the process is sleeping and traced, make it runnable 876 * so it can discover the signal in issignal() and stop 877 * for the parent. 878 * 879 * If the process is stopped and traced, no further action 880 * is necessary. 881 */ 882 if (p->p_flag & P_TRACED) 883 goto run; 884 885 /* 886 * If the process is sleeping and SA_CONT, and the signal 887 * mode is SIG_DFL, then make the process runnable. 888 * 889 * However, do *NOT* set P_BREAKTSLEEP. We do not want 890 * a SIGCONT to terminate an interruptable tsleep early 891 * and generate a spurious EINTR. 892 */ 893 if ((prop & SA_CONT) && action == SIG_DFL) { 894 SIGDELSET(p->p_siglist, sig); 895 goto run_no_break; 896 } 897 898 /* 899 * If the process is sleeping and receives a STOP signal, 900 * process immediately if possible. All other (caught or 901 * default) signals cause the process to run. 902 */ 903 if (prop & SA_STOP) { 904 if (action != SIG_DFL) 905 goto run; 906 907 /* 908 * If a child holding parent blocked, stopping 909 * could cause deadlock. Take no action at this 910 * time. 911 */ 912 if (p->p_flag & P_PPWAIT) 913 goto out; 914 915 /* 916 * Do not actually try to manipulate the process 917 * while it is sleeping, simply set P_STOPPED to 918 * indicate that it should stop as soon as it safely 919 * can. 920 */ 921 SIGDELSET(p->p_siglist, sig); 922 p->p_flag |= P_STOPPED; 923 p->p_flag &= ~P_WAITED; 924 p->p_xstat = sig; 925 wakeup(p->p_pptr); 926 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 927 psignal(p->p_pptr, SIGCHLD); 928 goto out; 929 } 930 931 /* 932 * Otherwise the signal can interrupt the sleep. 933 */ 934 goto run; 935 } 936 937 /* 938 * Process is in tsleep and is stopped 939 */ 940 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED)) { 941 /* 942 * If the process is stopped and is being traced, then no 943 * further action is necessary. 944 */ 945 if (p->p_flag & P_TRACED) 946 goto out; 947 948 /* 949 * If the process is stopped and receives a KILL signal, 950 * make the process runnable. 951 */ 952 if (sig == SIGKILL) 953 goto run; 954 955 /* 956 * If the process is stopped and receives a CONT signal, 957 * then try to make the process runnable again. 958 */ 959 if (prop & SA_CONT) { 960 /* 961 * If SIGCONT is default (or ignored), we continue the 962 * process but don't leave the signal in p_siglist, as 963 * it has no further action. If SIGCONT is held, we 964 * continue the process and leave the signal in 965 * p_siglist. If the process catches SIGCONT, let it 966 * handle the signal itself. 967 */ 968 if (action == SIG_DFL) 969 SIGDELSET(p->p_siglist, sig); 970 if (action == SIG_CATCH) 971 goto run; 972 973 /* 974 * Make runnable but do not break a tsleep unless 975 * some other signal was pending. 976 */ 977 goto run_no_break; 978 } 979 980 /* 981 * If the process is stopped and receives another STOP 982 * signal, we do not need to stop it again. If we did 983 * the shell could get confused. 984 */ 985 if (prop & SA_STOP) { 986 SIGDELSET(p->p_siglist, sig); 987 goto out; 988 } 989 990 /* 991 * Otherwise the process is sleeping interruptably but 992 * is stopped, just set the P_BREAKTSLEEP flag and take 993 * no further action. The next runnable action will wake 994 * the process up. 995 */ 996 p->p_flag |= P_BREAKTSLEEP; 997 goto out; 998 } 999 1000 /* 1001 * Otherwise the process is running 1002 * 1003 * SRUN, SIDL, SZOMB do nothing with the signal, 1004 * other than kicking ourselves if we are running. 1005 * It will either never be noticed, or noticed very soon. 1006 * 1007 * Note that p_thread may be NULL or may not be completely 1008 * initialized if the process is in the SIDL or SZOMB state. 1009 * 1010 * For SMP we may have to forward the request to another cpu. 1011 * YYY the MP lock prevents the target process from moving 1012 * to another cpu, see kern/kern_switch.c 1013 * 1014 * If the target thread is waiting on its message port, 1015 * wakeup the target thread so it can check (or ignore) 1016 * the new signal. YYY needs cleanup. 1017 */ 1018 if (lp == lwkt_preempted_proc()) { 1019 signotify(); 1020 } else if (p->p_stat == SRUN) { 1021 struct thread *td = p->p_thread; 1022 1023 KASSERT(td != NULL, 1024 ("pid %d NULL p_thread stat %d flags %08x", 1025 p->p_pid, p->p_stat, p->p_flag)); 1026 1027 #ifdef SMP 1028 if (td->td_gd != mycpu) 1029 lwkt_send_ipiq(td->td_gd, signotify_remote, lp); 1030 else 1031 #endif 1032 if (td->td_msgport.mp_flags & MSGPORTF_WAITING) 1033 lwkt_schedule(td); 1034 } 1035 goto out; 1036 /*NOTREACHED*/ 1037 run: 1038 /* 1039 * Make runnable and break out of any tsleep as well. 1040 */ 1041 p->p_flag |= P_BREAKTSLEEP; 1042 run_no_break: 1043 setrunnable(p); 1044 out: 1045 crit_exit(); 1046 } 1047 1048 #ifdef SMP 1049 1050 /* 1051 * This function is called via an IPI. We will be in a critical section but 1052 * the MP lock will NOT be held. Also note that by the time the ipi message 1053 * gets to us the process 'p' (arg) may no longer be scheduled or even valid. 1054 */ 1055 static void 1056 signotify_remote(void *arg) 1057 { 1058 struct lwp *lp = arg; 1059 1060 if (lp == lwkt_preempted_proc()) { 1061 signotify(); 1062 } else { 1063 struct thread *td = lp->lwp_thread; 1064 if (td->td_msgport.mp_flags & MSGPORTF_WAITING) 1065 lwkt_schedule(td); 1066 } 1067 } 1068 1069 #endif 1070 1071 static int 1072 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout) 1073 { 1074 sigset_t savedmask, set; 1075 struct proc *p = curproc; 1076 int error, sig, hz, timevalid = 0; 1077 struct timespec rts, ets, ts; 1078 struct timeval tv; 1079 1080 error = 0; 1081 sig = 0; 1082 SIG_CANTMASK(waitset); 1083 savedmask = p->p_sigmask; 1084 1085 if (timeout) { 1086 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 && 1087 timeout->tv_nsec < 1000000000) { 1088 timevalid = 1; 1089 getnanouptime(&rts); 1090 ets = rts; 1091 timespecadd(&ets, timeout); 1092 } 1093 } 1094 1095 for (;;) { 1096 set = p->p_siglist; 1097 SIGSETAND(set, waitset); 1098 if ((sig = sig_ffs(&set)) != 0) { 1099 SIGFILLSET(p->p_sigmask); 1100 SIGDELSET(p->p_sigmask, sig); 1101 SIG_CANTMASK(p->p_sigmask); 1102 sig = issignal(p); 1103 /* 1104 * It may be a STOP signal, in the case, issignal 1105 * returns 0, because we may stop there, and new 1106 * signal can come in, we should restart if we got 1107 * nothing. 1108 */ 1109 if (sig == 0) 1110 continue; 1111 else 1112 break; 1113 } 1114 1115 /* 1116 * Previous checking got nothing, and we retried but still 1117 * got nothing, we should return the error status. 1118 */ 1119 if (error) 1120 break; 1121 1122 /* 1123 * POSIX says this must be checked after looking for pending 1124 * signals. 1125 */ 1126 if (timeout) { 1127 if (!timevalid) { 1128 error = EINVAL; 1129 break; 1130 } 1131 getnanouptime(&rts); 1132 if (timespeccmp(&rts, &ets, >=)) { 1133 error = EAGAIN; 1134 break; 1135 } 1136 ts = ets; 1137 timespecsub(&ts, &rts); 1138 TIMESPEC_TO_TIMEVAL(&tv, &ts); 1139 hz = tvtohz_high(&tv); 1140 } else 1141 hz = 0; 1142 1143 p->p_sigmask = savedmask; 1144 SIGSETNAND(p->p_sigmask, waitset); 1145 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz); 1146 if (timeout) { 1147 if (error == ERESTART) { 1148 /* can not restart a timeout wait. */ 1149 error = EINTR; 1150 } else if (error == EAGAIN) { 1151 /* will calculate timeout by ourself. */ 1152 error = 0; 1153 } 1154 } 1155 /* Retry ... */ 1156 } 1157 1158 p->p_sigmask = savedmask; 1159 if (sig) { 1160 error = 0; 1161 bzero(info, sizeof(*info)); 1162 info->si_signo = sig; 1163 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1164 1165 if (sig == SIGKILL) 1166 sigexit(p, sig); 1167 } 1168 return (error); 1169 } 1170 1171 int 1172 sys_sigtimedwait(struct sigtimedwait_args *uap) 1173 { 1174 struct timespec ts; 1175 struct timespec *timeout; 1176 sigset_t set; 1177 siginfo_t info; 1178 int error; 1179 1180 if (uap->timeout) { 1181 error = copyin(uap->timeout, &ts, sizeof(ts)); 1182 if (error) 1183 return (error); 1184 timeout = &ts; 1185 } else { 1186 timeout = NULL; 1187 } 1188 error = copyin(uap->set, &set, sizeof(set)); 1189 if (error) 1190 return (error); 1191 error = kern_sigtimedwait(set, &info, timeout); 1192 if (error) 1193 return (error); 1194 if (uap->info) 1195 error = copyout(&info, uap->info, sizeof(info)); 1196 /* Repost if we got an error. */ 1197 if (error) 1198 psignal(curproc, info.si_signo); 1199 else 1200 uap->sysmsg_result = info.si_signo; 1201 return (error); 1202 } 1203 1204 int 1205 sys_sigwaitinfo(struct sigwaitinfo_args *uap) 1206 { 1207 siginfo_t info; 1208 sigset_t set; 1209 int error; 1210 1211 error = copyin(uap->set, &set, sizeof(set)); 1212 if (error) 1213 return (error); 1214 error = kern_sigtimedwait(set, &info, NULL); 1215 if (error) 1216 return (error); 1217 if (uap->info) 1218 error = copyout(&info, uap->info, sizeof(info)); 1219 /* Repost if we got an error. */ 1220 if (error) 1221 psignal(curproc, info.si_signo); 1222 else 1223 uap->sysmsg_result = info.si_signo; 1224 return (error); 1225 } 1226 1227 /* 1228 * If the current process has received a signal that would interrupt a 1229 * system call, return EINTR or ERESTART as appropriate. 1230 */ 1231 int 1232 iscaught(struct proc *p) 1233 { 1234 int sig; 1235 1236 if (p) { 1237 if ((sig = CURSIG(p)) != 0) { 1238 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig)) 1239 return (EINTR); 1240 return (ERESTART); 1241 } 1242 } 1243 return(EWOULDBLOCK); 1244 } 1245 1246 /* 1247 * If the current process has received a signal (should be caught or cause 1248 * termination, should interrupt current syscall), return the signal number. 1249 * Stop signals with default action are processed immediately, then cleared; 1250 * they aren't returned. This is checked after each entry to the system for 1251 * a syscall or trap (though this can usually be done without calling issignal 1252 * by checking the pending signal masks in the CURSIG macro.) The normal call 1253 * sequence is 1254 * 1255 * This routine is called via CURSIG/__cursig and the MP lock might not be 1256 * held. Obtain the MP lock for the duration of the operation. 1257 * 1258 * while (sig = CURSIG(curproc)) 1259 * postsig(sig); 1260 */ 1261 int 1262 issignal(struct proc *p) 1263 { 1264 sigset_t mask; 1265 int sig, prop; 1266 1267 get_mplock(); 1268 for (;;) { 1269 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 1270 1271 mask = p->p_siglist; 1272 SIGSETNAND(mask, p->p_sigmask); 1273 if (p->p_flag & P_PPWAIT) 1274 SIG_STOPSIGMASK(mask); 1275 if (!SIGNOTEMPTY(mask)) { /* no signal to send */ 1276 rel_mplock(); 1277 return (0); 1278 } 1279 sig = sig_ffs(&mask); 1280 1281 STOPEVENT(p, S_SIG, sig); 1282 1283 /* 1284 * We should see pending but ignored signals 1285 * only if P_TRACED was on when they were posted. 1286 */ 1287 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 1288 SIGDELSET(p->p_siglist, sig); 1289 continue; 1290 } 1291 if ((p->p_flag & P_TRACED) && (p->p_flag & P_PPWAIT) == 0) { 1292 /* 1293 * If traced, always stop, and stay stopped until 1294 * released by the parent. 1295 * 1296 * NOTE: P_STOPPED may get cleared during the loop, 1297 * but we do not re-notify the parent if we have 1298 * to loop several times waiting for the parent 1299 * to let us continue. 1300 */ 1301 p->p_xstat = sig; 1302 p->p_flag |= P_STOPPED; 1303 p->p_flag &= ~P_WAITED; 1304 psignal(p->p_pptr, SIGCHLD); 1305 do { 1306 tstop(p); 1307 } while (!trace_req(p) && (p->p_flag & P_TRACED)); 1308 p->p_flag &= ~P_STOPPED; 1309 1310 /* 1311 * If parent wants us to take the signal, 1312 * then it will leave it in p->p_xstat; 1313 * otherwise we just look for signals again. 1314 */ 1315 SIGDELSET(p->p_siglist, sig); /* clear old signal */ 1316 sig = p->p_xstat; 1317 if (sig == 0) 1318 continue; 1319 1320 /* 1321 * Put the new signal into p_siglist. If the 1322 * signal is being masked, look for other signals. 1323 */ 1324 SIGADDSET(p->p_siglist, sig); 1325 if (SIGISMEMBER(p->p_sigmask, sig)) 1326 continue; 1327 1328 /* 1329 * If the traced bit got turned off, go back up 1330 * to the top to rescan signals. This ensures 1331 * that p_sig* and ps_sigact are consistent. 1332 */ 1333 if ((p->p_flag & P_TRACED) == 0) 1334 continue; 1335 } 1336 1337 prop = sigprop(sig); 1338 1339 /* 1340 * Decide whether the signal should be returned. 1341 * Return the signal's number, or fall through 1342 * to clear it from the pending mask. 1343 */ 1344 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1345 case (int)SIG_DFL: 1346 /* 1347 * Don't take default actions on system processes. 1348 */ 1349 if (p->p_pid <= 1) { 1350 #ifdef DIAGNOSTIC 1351 /* 1352 * Are you sure you want to ignore SIGSEGV 1353 * in init? XXX 1354 */ 1355 printf("Process (pid %lu) got signal %d\n", 1356 (u_long)p->p_pid, sig); 1357 #endif 1358 break; /* == ignore */ 1359 } 1360 1361 /* 1362 * Handle the in-kernel checkpoint action 1363 */ 1364 if (prop & SA_CKPT) { 1365 checkpoint_signal_handler(p); 1366 break; 1367 } 1368 1369 /* 1370 * If there is a pending stop signal to process 1371 * with default action, stop here, 1372 * then clear the signal. However, 1373 * if process is member of an orphaned 1374 * process group, ignore tty stop signals. 1375 */ 1376 if (prop & SA_STOP) { 1377 if (p->p_flag & P_TRACED || 1378 (p->p_pgrp->pg_jobc == 0 && 1379 prop & SA_TTYSTOP)) 1380 break; /* == ignore */ 1381 p->p_xstat = sig; 1382 p->p_flag |= P_STOPPED; 1383 p->p_flag &= ~P_WAITED; 1384 1385 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1386 psignal(p->p_pptr, SIGCHLD); 1387 while (p->p_flag & P_STOPPED) { 1388 tstop(p); 1389 } 1390 break; 1391 } else if (prop & SA_IGNORE) { 1392 /* 1393 * Except for SIGCONT, shouldn't get here. 1394 * Default action is to ignore; drop it. 1395 */ 1396 break; /* == ignore */ 1397 } else { 1398 rel_mplock(); 1399 return (sig); 1400 } 1401 1402 /*NOTREACHED*/ 1403 1404 case (int)SIG_IGN: 1405 /* 1406 * Masking above should prevent us ever trying 1407 * to take action on an ignored signal other 1408 * than SIGCONT, unless process is traced. 1409 */ 1410 if ((prop & SA_CONT) == 0 && 1411 (p->p_flag & P_TRACED) == 0) 1412 printf("issignal\n"); 1413 break; /* == ignore */ 1414 1415 default: 1416 /* 1417 * This signal has an action, let 1418 * postsig() process it. 1419 */ 1420 rel_mplock(); 1421 return (sig); 1422 } 1423 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1424 } 1425 /* NOTREACHED */ 1426 } 1427 1428 /* 1429 * Take the action for the specified signal 1430 * from the current set of pending signals. 1431 */ 1432 void 1433 postsig(int sig) 1434 { 1435 struct proc *p = curproc; 1436 struct sigacts *ps = p->p_sigacts; 1437 sig_t action; 1438 sigset_t returnmask; 1439 int code; 1440 1441 KASSERT(sig != 0, ("postsig")); 1442 1443 SIGDELSET(p->p_siglist, sig); 1444 action = ps->ps_sigact[_SIG_IDX(sig)]; 1445 #ifdef KTRACE 1446 if (KTRPOINT(p->p_thread, KTR_PSIG)) 1447 ktrpsig(p, sig, action, p->p_flag & P_OLDMASK ? 1448 &p->p_oldsigmask : &p->p_sigmask, 0); 1449 #endif 1450 STOPEVENT(p, S_SIG, sig); 1451 1452 if (action == SIG_DFL) { 1453 /* 1454 * Default action, where the default is to kill 1455 * the process. (Other cases were ignored above.) 1456 */ 1457 sigexit(p, sig); 1458 /* NOTREACHED */ 1459 } else { 1460 /* 1461 * If we get here, the signal must be caught. 1462 */ 1463 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 1464 ("postsig action")); 1465 /* 1466 * Set the new mask value and also defer further 1467 * occurrences of this signal. 1468 * 1469 * Special case: user has done a sigsuspend. Here the 1470 * current mask is not of interest, but rather the 1471 * mask from before the sigsuspend is what we want 1472 * restored after the signal processing is completed. 1473 */ 1474 crit_enter(); 1475 if (p->p_flag & P_OLDMASK) { 1476 returnmask = p->p_oldsigmask; 1477 p->p_flag &= ~P_OLDMASK; 1478 } else { 1479 returnmask = p->p_sigmask; 1480 } 1481 1482 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1483 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1484 SIGADDSET(p->p_sigmask, sig); 1485 1486 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1487 /* 1488 * See kern_sigaction() for origin of this code. 1489 */ 1490 SIGDELSET(p->p_sigcatch, sig); 1491 if (sig != SIGCONT && 1492 sigprop(sig) & SA_IGNORE) 1493 SIGADDSET(p->p_sigignore, sig); 1494 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1495 } 1496 crit_exit(); 1497 p->p_stats->p_ru.ru_nsignals++; 1498 if (p->p_sig != sig) { 1499 code = 0; 1500 } else { 1501 code = p->p_code; 1502 p->p_code = 0; 1503 p->p_sig = 0; 1504 } 1505 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1506 } 1507 } 1508 1509 /* 1510 * Kill the current process for stated reason. 1511 */ 1512 void 1513 killproc(struct proc *p, char *why) 1514 { 1515 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1516 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1517 psignal(p, SIGKILL); 1518 } 1519 1520 /* 1521 * Force the current process to exit with the specified signal, dumping core 1522 * if appropriate. We bypass the normal tests for masked and caught signals, 1523 * allowing unrecoverable failures to terminate the process without changing 1524 * signal state. Mark the accounting record with the signal termination. 1525 * If dumping core, save the signal number for the debugger. Calls exit and 1526 * does not return. 1527 */ 1528 void 1529 sigexit(struct proc *p, int sig) 1530 { 1531 p->p_acflag |= AXSIG; 1532 if (sigprop(sig) & SA_CORE) { 1533 p->p_sig = sig; 1534 /* 1535 * Log signals which would cause core dumps 1536 * (Log as LOG_INFO to appease those who don't want 1537 * these messages.) 1538 * XXX : Todo, as well as euid, write out ruid too 1539 */ 1540 if (coredump(p) == 0) 1541 sig |= WCOREFLAG; 1542 if (kern_logsigexit) 1543 log(LOG_INFO, 1544 "pid %d (%s), uid %d: exited on signal %d%s\n", 1545 p->p_pid, p->p_comm, 1546 p->p_ucred ? p->p_ucred->cr_uid : -1, 1547 sig &~ WCOREFLAG, 1548 sig & WCOREFLAG ? " (core dumped)" : ""); 1549 } 1550 exit1(W_EXITCODE(0, sig)); 1551 /* NOTREACHED */ 1552 } 1553 1554 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1555 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1556 sizeof(corefilename), "process corefile name format string"); 1557 1558 /* 1559 * expand_name(name, uid, pid) 1560 * Expand the name described in corefilename, using name, uid, and pid. 1561 * corefilename is a printf-like string, with three format specifiers: 1562 * %N name of process ("name") 1563 * %P process id (pid) 1564 * %U user id (uid) 1565 * For example, "%N.core" is the default; they can be disabled completely 1566 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1567 * This is controlled by the sysctl variable kern.corefile (see above). 1568 */ 1569 1570 static char * 1571 expand_name(const char *name, uid_t uid, pid_t pid) 1572 { 1573 char *temp; 1574 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1575 int i, n; 1576 char *format = corefilename; 1577 size_t namelen; 1578 1579 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 1580 if (temp == NULL) 1581 return NULL; 1582 namelen = strlen(name); 1583 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1584 int l; 1585 switch (format[i]) { 1586 case '%': /* Format character */ 1587 i++; 1588 switch (format[i]) { 1589 case '%': 1590 temp[n++] = '%'; 1591 break; 1592 case 'N': /* process name */ 1593 if ((n + namelen) > MAXPATHLEN) { 1594 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1595 pid, name, uid, temp, name); 1596 free(temp, M_TEMP); 1597 return NULL; 1598 } 1599 memcpy(temp+n, name, namelen); 1600 n += namelen; 1601 break; 1602 case 'P': /* process id */ 1603 l = sprintf(buf, "%u", pid); 1604 if ((n + l) > MAXPATHLEN) { 1605 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1606 pid, name, uid, temp, name); 1607 free(temp, M_TEMP); 1608 return NULL; 1609 } 1610 memcpy(temp+n, buf, l); 1611 n += l; 1612 break; 1613 case 'U': /* user id */ 1614 l = sprintf(buf, "%u", uid); 1615 if ((n + l) > MAXPATHLEN) { 1616 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1617 pid, name, uid, temp, name); 1618 free(temp, M_TEMP); 1619 return NULL; 1620 } 1621 memcpy(temp+n, buf, l); 1622 n += l; 1623 break; 1624 default: 1625 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1626 } 1627 break; 1628 default: 1629 temp[n++] = format[i]; 1630 } 1631 } 1632 temp[n] = '\0'; 1633 return temp; 1634 } 1635 1636 /* 1637 * Dump a process' core. The main routine does some 1638 * policy checking, and creates the name of the coredump; 1639 * then it passes on a vnode and a size limit to the process-specific 1640 * coredump routine if there is one; if there _is not_ one, it returns 1641 * ENOSYS; otherwise it returns the error from the process-specific routine. 1642 */ 1643 1644 static int 1645 coredump(struct proc *p) 1646 { 1647 struct vnode *vp; 1648 struct ucred *cred = p->p_ucred; 1649 struct flock lf; 1650 struct nlookupdata nd; 1651 struct vattr vattr; 1652 int error, error1; 1653 char *name; /* name of corefile */ 1654 off_t limit; 1655 1656 STOPEVENT(p, S_CORE, 0); 1657 1658 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) 1659 return (EFAULT); 1660 1661 /* 1662 * Note that the bulk of limit checking is done after 1663 * the corefile is created. The exception is if the limit 1664 * for corefiles is 0, in which case we don't bother 1665 * creating the corefile at all. This layout means that 1666 * a corefile is truncated instead of not being created, 1667 * if it is larger than the limit. 1668 */ 1669 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1670 if (limit == 0) 1671 return EFBIG; 1672 1673 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1674 if (name == NULL) 1675 return (EINVAL); 1676 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP); 1677 if (error == 0) 1678 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR); 1679 free(name, M_TEMP); 1680 if (error) { 1681 nlookup_done(&nd); 1682 return (error); 1683 } 1684 vp = nd.nl_open_vp; 1685 nd.nl_open_vp = NULL; 1686 nlookup_done(&nd); 1687 1688 VOP_UNLOCK(vp, 0); 1689 lf.l_whence = SEEK_SET; 1690 lf.l_start = 0; 1691 lf.l_len = 0; 1692 lf.l_type = F_WRLCK; 1693 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, 0); 1694 if (error) 1695 goto out2; 1696 1697 /* Don't dump to non-regular files or files with links. */ 1698 if (vp->v_type != VREG || 1699 VOP_GETATTR(vp, &vattr) || vattr.va_nlink != 1) { 1700 error = EFAULT; 1701 goto out1; 1702 } 1703 1704 VATTR_NULL(&vattr); 1705 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1706 vattr.va_size = 0; 1707 VOP_SETATTR(vp, &vattr, cred); 1708 p->p_acflag |= ACORE; 1709 VOP_UNLOCK(vp, 0); 1710 1711 error = p->p_sysent->sv_coredump ? 1712 p->p_sysent->sv_coredump(p, vp, limit) : ENOSYS; 1713 1714 out1: 1715 lf.l_type = F_UNLCK; 1716 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, 0); 1717 out2: 1718 error1 = vn_close(vp, FWRITE); 1719 if (error == 0) 1720 error = error1; 1721 return (error); 1722 } 1723 1724 /* 1725 * Nonexistent system call-- signal process (may want to handle it). 1726 * Flag error in case process won't see signal immediately (blocked or ignored). 1727 */ 1728 /* ARGSUSED */ 1729 int 1730 sys_nosys(struct nosys_args *args) 1731 { 1732 psignal(curproc, SIGSYS); 1733 return (EINVAL); 1734 } 1735 1736 /* 1737 * Send a SIGIO or SIGURG signal to a process or process group using 1738 * stored credentials rather than those of the current process. 1739 */ 1740 void 1741 pgsigio(struct sigio *sigio, int sig, int checkctty) 1742 { 1743 if (sigio == NULL) 1744 return; 1745 1746 if (sigio->sio_pgid > 0) { 1747 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1748 sigio->sio_proc)) 1749 psignal(sigio->sio_proc, sig); 1750 } else if (sigio->sio_pgid < 0) { 1751 struct proc *p; 1752 1753 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) 1754 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1755 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1756 psignal(p, sig); 1757 } 1758 } 1759 1760 static int 1761 filt_sigattach(struct knote *kn) 1762 { 1763 struct proc *p = curproc; 1764 1765 kn->kn_ptr.p_proc = p; 1766 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1767 1768 /* XXX lock the proc here while adding to the list? */ 1769 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 1770 1771 return (0); 1772 } 1773 1774 static void 1775 filt_sigdetach(struct knote *kn) 1776 { 1777 struct proc *p = kn->kn_ptr.p_proc; 1778 1779 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 1780 } 1781 1782 /* 1783 * signal knotes are shared with proc knotes, so we apply a mask to 1784 * the hint in order to differentiate them from process hints. This 1785 * could be avoided by using a signal-specific knote list, but probably 1786 * isn't worth the trouble. 1787 */ 1788 static int 1789 filt_signal(struct knote *kn, long hint) 1790 { 1791 if (hint & NOTE_SIGNAL) { 1792 hint &= ~NOTE_SIGNAL; 1793 1794 if (kn->kn_id == hint) 1795 kn->kn_data++; 1796 } 1797 return (kn->kn_data != 0); 1798 } 1799