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.42 2005/12/02 19:31:49 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 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 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 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 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 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 static int 600 killpg(int sig, int pgid, int all) 601 { 602 struct proc *cp = curproc; 603 struct proc *p; 604 struct pgrp *pgrp; 605 int nfound = 0; 606 607 if (all) { 608 /* 609 * broadcast 610 */ 611 FOREACH_PROC_IN_SYSTEM(p) { 612 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 613 p == cp || !CANSIGNAL(p, sig)) 614 continue; 615 nfound++; 616 if (sig) 617 psignal(p, sig); 618 } 619 } else { 620 if (pgid == 0) { 621 /* 622 * zero pgid means send to my process group. 623 */ 624 pgrp = cp->p_pgrp; 625 } else { 626 pgrp = pgfind(pgid); 627 if (pgrp == NULL) 628 return (ESRCH); 629 } 630 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 631 if (p->p_pid <= 1 || 632 (p->p_flag & (P_SYSTEM | P_ZOMBIE)) || 633 !CANSIGNAL(p, sig)) { 634 continue; 635 } 636 nfound++; 637 if (sig) 638 psignal(p, sig); 639 } 640 } 641 return (nfound ? 0 : ESRCH); 642 } 643 644 int 645 kern_kill(int sig, int pid) 646 { 647 struct thread *td = curthread; 648 struct proc *p = td->td_proc; 649 650 if ((u_int)sig > _SIG_MAXSIG) 651 return (EINVAL); 652 if (pid > 0) { 653 /* kill single process */ 654 if ((p = pfind(pid)) == NULL) 655 return (ESRCH); 656 if (!CANSIGNAL(p, sig)) 657 return (EPERM); 658 if (sig) 659 psignal(p, sig); 660 return (0); 661 } 662 switch (pid) { 663 case -1: /* broadcast signal */ 664 return (killpg(sig, 0, 1)); 665 case 0: /* signal own process group */ 666 return (killpg(sig, 0, 0)); 667 default: /* negative explicit process group */ 668 return (killpg(sig, -pid, 0)); 669 } 670 /* NOTREACHED */ 671 } 672 673 int 674 kill(struct kill_args *uap) 675 { 676 int error; 677 678 error = kern_kill(uap->signum, uap->pid); 679 680 return (error); 681 } 682 683 /* 684 * Send a signal to a process group. 685 */ 686 void 687 gsignal(int pgid, int sig) 688 { 689 struct pgrp *pgrp; 690 691 if (pgid && (pgrp = pgfind(pgid))) 692 pgsignal(pgrp, sig, 0); 693 } 694 695 /* 696 * Send a signal to a process group. If checktty is 1, 697 * limit to members which have a controlling terminal. 698 */ 699 void 700 pgsignal(struct pgrp *pgrp, int sig, int checkctty) 701 { 702 struct proc *p; 703 704 if (pgrp) 705 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 706 if (checkctty == 0 || p->p_flag & P_CONTROLT) 707 psignal(p, sig); 708 } 709 710 /* 711 * Send a signal caused by a trap to the current process. 712 * If it will be caught immediately, deliver it with correct code. 713 * Otherwise, post it normally. 714 */ 715 void 716 trapsignal(struct proc *p, int sig, u_long code) 717 { 718 struct sigacts *ps = p->p_sigacts; 719 720 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 721 !SIGISMEMBER(p->p_sigmask, sig)) { 722 p->p_stats->p_ru.ru_nsignals++; 723 #ifdef KTRACE 724 if (KTRPOINT(p->p_thread, KTR_PSIG)) 725 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 726 &p->p_sigmask, code); 727 #endif 728 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 729 &p->p_sigmask, code); 730 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 731 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 732 SIGADDSET(p->p_sigmask, sig); 733 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 734 /* 735 * See kern_sigaction() for origin of this code. 736 */ 737 SIGDELSET(p->p_sigcatch, sig); 738 if (sig != SIGCONT && 739 sigprop(sig) & SA_IGNORE) 740 SIGADDSET(p->p_sigignore, sig); 741 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 742 } 743 } else { 744 p->p_code = code; /* XXX for core dump/debugger */ 745 p->p_sig = sig; /* XXX to verify code */ 746 psignal(p, sig); 747 } 748 } 749 750 /* 751 * Send the signal to the process. If the signal has an action, the action 752 * is usually performed by the target process rather than the caller; we add 753 * the signal to the set of pending signals for the process. 754 * 755 * Exceptions: 756 * o When a stop signal is sent to a sleeping process that takes the 757 * default action, the process is stopped without awakening it. 758 * o SIGCONT restarts stopped processes (or puts them back to sleep) 759 * regardless of the signal action (eg, blocked or ignored). 760 * 761 * Other ignored signals are discarded immediately. 762 */ 763 void 764 psignal(struct proc *p, int sig) 765 { 766 struct lwp *lp = &p->p_lwp; 767 int prop; 768 sig_t action; 769 770 if (sig > _SIG_MAXSIG || sig <= 0) { 771 printf("psignal: signal %d\n", sig); 772 panic("psignal signal number"); 773 } 774 775 crit_enter(); 776 KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 777 crit_exit(); 778 779 prop = sigprop(sig); 780 781 /* 782 * If proc is traced, always give parent a chance; 783 * if signal event is tracked by procfs, give *that* 784 * a chance, as well. 785 */ 786 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 787 action = SIG_DFL; 788 } else { 789 /* 790 * If the signal is being ignored, 791 * then we forget about it immediately. 792 * (Note: we don't set SIGCONT in p_sigignore, 793 * and if it is set to SIG_IGN, 794 * action will be SIG_DFL here.) 795 */ 796 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 797 return; 798 if (SIGISMEMBER(p->p_sigmask, sig)) 799 action = SIG_HOLD; 800 else if (SIGISMEMBER(p->p_sigcatch, sig)) 801 action = SIG_CATCH; 802 else 803 action = SIG_DFL; 804 } 805 806 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 807 (p->p_flag & P_TRACED) == 0) { 808 p->p_nice = NZERO; 809 } 810 811 /* 812 * If continuing, clear any pending STOP signals. 813 */ 814 if (prop & SA_CONT) 815 SIG_STOPSIGMASK(p->p_siglist); 816 817 if (prop & SA_STOP) { 818 /* 819 * If sending a tty stop signal to a member of an orphaned 820 * process group, discard the signal here if the action 821 * is default; don't stop the process below if sleeping, 822 * and don't clear any pending SIGCONT. 823 */ 824 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 825 action == SIG_DFL) { 826 return; 827 } 828 SIG_CONTSIGMASK(p->p_siglist); 829 } 830 SIGADDSET(p->p_siglist, sig); 831 832 /* 833 * Defer further processing for signals which are held, 834 * except that stopped processes must be continued by SIGCONT. 835 */ 836 if (action == SIG_HOLD) { 837 if ((prop & SA_CONT) == 0 || (p->p_flag & P_STOPPED) == 0) 838 return; 839 } 840 841 crit_enter(); 842 843 /* 844 * Process is in tsleep and not stopped 845 */ 846 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED) == 0) { 847 /* 848 * If the process is sleeping uninterruptibly 849 * we can't interrupt the sleep... the signal will 850 * be noticed when the process returns through 851 * trap() or syscall(). 852 */ 853 if ((p->p_flag & P_SINTR) == 0) 854 goto out; 855 856 /* 857 * If the process is sleeping and traced, make it runnable 858 * so it can discover the signal in issignal() and stop 859 * for the parent. 860 * 861 * If the process is stopped and traced, no further action 862 * is necessary. 863 */ 864 if (p->p_flag & P_TRACED) 865 goto run; 866 867 /* 868 * If the process is sleeping and SA_CONT, and the signal 869 * mode is SIG_DFL, then make the process runnable. 870 * 871 * However, do *NOT* set P_BREAKTSLEEP. We do not want 872 * a SIGCONT to terminate an interruptable tsleep early 873 * and generate a spurious EINTR. 874 */ 875 if ((prop & SA_CONT) && action == SIG_DFL) { 876 SIGDELSET(p->p_siglist, sig); 877 goto run_no_break; 878 } 879 880 /* 881 * If the process is sleeping and receives a STOP signal, 882 * process immediately if possible. All other (caught or 883 * default) signals cause the process to run. 884 */ 885 if (prop & SA_STOP) { 886 if (action != SIG_DFL) 887 goto run; 888 889 /* 890 * If a child holding parent blocked, stopping 891 * could cause deadlock. Take no action at this 892 * time. 893 */ 894 if (p->p_flag & P_PPWAIT) 895 goto out; 896 897 /* 898 * Do not actually try to manipulate the process 899 * while it is sleeping, simply set P_STOPPED to 900 * indicate that it should stop as soon as it safely 901 * can. 902 */ 903 SIGDELSET(p->p_siglist, sig); 904 p->p_flag |= P_STOPPED; 905 p->p_flag &= ~P_WAITED; 906 p->p_xstat = sig; 907 wakeup(p->p_pptr); 908 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 909 psignal(p->p_pptr, SIGCHLD); 910 goto out; 911 } 912 913 /* 914 * Otherwise the signal can interrupt the sleep. 915 */ 916 goto run; 917 } 918 919 /* 920 * Process is in tsleep and is stopped 921 */ 922 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED)) { 923 /* 924 * If the process is stopped and is being traced, then no 925 * further action is necessary. 926 */ 927 if (p->p_flag & P_TRACED) 928 goto out; 929 930 /* 931 * If the process is stopped and receives a KILL signal, 932 * make the process runnable. 933 */ 934 if (sig == SIGKILL) 935 goto run; 936 937 /* 938 * If the process is stopped and receives a CONT signal, 939 * then try to make the process runnable again. 940 */ 941 if (prop & SA_CONT) { 942 /* 943 * If SIGCONT is default (or ignored), we continue the 944 * process but don't leave the signal in p_siglist, as 945 * it has no further action. If SIGCONT is held, we 946 * continue the process and leave the signal in 947 * p_siglist. If the process catches SIGCONT, let it 948 * handle the signal itself. 949 */ 950 if (action == SIG_DFL) 951 SIGDELSET(p->p_siglist, sig); 952 if (action == SIG_CATCH) 953 goto run; 954 955 /* 956 * Make runnable but do not break a tsleep unless 957 * some other signal was pending. 958 */ 959 goto run_no_break; 960 } 961 962 /* 963 * If the process is stopped and receives another STOP 964 * signal, we do not need to stop it again. If we did 965 * the shell could get confused. 966 */ 967 if (prop & SA_STOP) { 968 SIGDELSET(p->p_siglist, sig); 969 goto out; 970 } 971 972 /* 973 * Otherwise the process is sleeping interruptably but 974 * is stopped, just set the P_BREAKTSLEEP flag and take 975 * no further action. The next runnable action will wake 976 * the process up. 977 */ 978 p->p_flag |= P_BREAKTSLEEP; 979 goto out; 980 } 981 982 /* 983 * Otherwise the process is running 984 * 985 * SRUN, SIDL, SZOMB do nothing with the signal, 986 * other than kicking ourselves if we are running. 987 * It will either never be noticed, or noticed very soon. 988 * 989 * Note that p_thread may be NULL or may not be completely 990 * initialized if the process is in the SIDL or SZOMB state. 991 * 992 * For SMP we may have to forward the request to another cpu. 993 * YYY the MP lock prevents the target process from moving 994 * to another cpu, see kern/kern_switch.c 995 * 996 * If the target thread is waiting on its message port, 997 * wakeup the target thread so it can check (or ignore) 998 * the new signal. YYY needs cleanup. 999 */ 1000 if (lp == lwkt_preempted_proc()) { 1001 signotify(); 1002 } else if (p->p_stat == SRUN) { 1003 struct thread *td = p->p_thread; 1004 1005 KASSERT(td != NULL, 1006 ("pid %d NULL p_thread stat %d flags %08x", 1007 p->p_pid, p->p_stat, p->p_flag)); 1008 1009 #ifdef SMP 1010 if (td->td_gd != mycpu) 1011 lwkt_send_ipiq(td->td_gd, signotify_remote, lp); 1012 else 1013 #endif 1014 if (td->td_msgport.mp_flags & MSGPORTF_WAITING) 1015 lwkt_schedule(td); 1016 } 1017 goto out; 1018 /*NOTREACHED*/ 1019 run: 1020 /* 1021 * Make runnable and break out of any tsleep as well. 1022 */ 1023 p->p_flag |= P_BREAKTSLEEP; 1024 run_no_break: 1025 setrunnable(p); 1026 out: 1027 crit_exit(); 1028 } 1029 1030 #ifdef SMP 1031 1032 /* 1033 * This function is called via an IPI. We will be in a critical section but 1034 * the MP lock will NOT be held. Also note that by the time the ipi message 1035 * gets to us the process 'p' (arg) may no longer be scheduled or even valid. 1036 */ 1037 static void 1038 signotify_remote(void *arg) 1039 { 1040 struct lwp *lp = arg; 1041 1042 if (lp == lwkt_preempted_proc()) { 1043 signotify(); 1044 } else { 1045 struct thread *td = lp->lwp_thread; 1046 if (td->td_msgport.mp_flags & MSGPORTF_WAITING) 1047 lwkt_schedule(td); 1048 } 1049 } 1050 1051 #endif 1052 1053 static int 1054 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout) 1055 { 1056 sigset_t savedmask, set; 1057 struct proc *p = curproc; 1058 int error, sig, hz, timevalid = 0; 1059 struct timespec rts, ets, ts; 1060 struct timeval tv; 1061 1062 error = 0; 1063 sig = 0; 1064 SIG_CANTMASK(waitset); 1065 savedmask = p->p_sigmask; 1066 1067 if (timeout) { 1068 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 && 1069 timeout->tv_nsec < 1000000000) { 1070 timevalid = 1; 1071 getnanouptime(&rts); 1072 ets = rts; 1073 timespecadd(&ets, timeout); 1074 } 1075 } 1076 1077 for (;;) { 1078 set = p->p_siglist; 1079 SIGSETAND(set, waitset); 1080 if ((sig = sig_ffs(&set)) != 0) { 1081 SIGFILLSET(p->p_sigmask); 1082 SIGDELSET(p->p_sigmask, sig); 1083 SIG_CANTMASK(p->p_sigmask); 1084 sig = issignal(p); 1085 /* 1086 * It may be a STOP signal, in the case, issignal 1087 * returns 0, because we may stop there, and new 1088 * signal can come in, we should restart if we got 1089 * nothing. 1090 */ 1091 if (sig == 0) 1092 continue; 1093 else 1094 break; 1095 } 1096 1097 /* 1098 * Previous checking got nothing, and we retried but still 1099 * got nothing, we should return the error status. 1100 */ 1101 if (error) 1102 break; 1103 1104 /* 1105 * POSIX says this must be checked after looking for pending 1106 * signals. 1107 */ 1108 if (timeout) { 1109 if (!timevalid) { 1110 error = EINVAL; 1111 break; 1112 } 1113 getnanouptime(&rts); 1114 if (timespeccmp(&rts, &ets, >=)) { 1115 error = EAGAIN; 1116 break; 1117 } 1118 ts = ets; 1119 timespecsub(&ts, &rts); 1120 TIMESPEC_TO_TIMEVAL(&tv, &ts); 1121 hz = tvtohz_high(&tv); 1122 } else 1123 hz = 0; 1124 1125 p->p_sigmask = savedmask; 1126 SIGSETNAND(p->p_sigmask, waitset); 1127 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz); 1128 if (timeout) { 1129 if (error == ERESTART) { 1130 /* can not restart a timeout wait. */ 1131 error = EINTR; 1132 } else if (error == EAGAIN) { 1133 /* will calculate timeout by ourself. */ 1134 error = 0; 1135 } 1136 } 1137 /* Retry ... */ 1138 } 1139 1140 p->p_sigmask = savedmask; 1141 if (sig) { 1142 error = 0; 1143 bzero(info, sizeof(*info)); 1144 info->si_signo = sig; 1145 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1146 } 1147 return (error); 1148 } 1149 1150 int 1151 sigtimedwait(struct sigtimedwait_args *uap) 1152 { 1153 struct timespec ts; 1154 struct timespec *timeout; 1155 sigset_t set; 1156 siginfo_t info; 1157 int error; 1158 1159 if (uap->timeout) { 1160 error = copyin(uap->timeout, &ts, sizeof(ts)); 1161 if (error) 1162 return (error); 1163 timeout = &ts; 1164 } else { 1165 timeout = NULL; 1166 } 1167 error = copyin(uap->set, &set, sizeof(set)); 1168 if (error) 1169 return (error); 1170 error = kern_sigtimedwait(set, &info, timeout); 1171 if (error) 1172 return (error); 1173 if (uap->info) 1174 error = copyout(&info, uap->info, sizeof(info)); 1175 /* Repost if we got an error. */ 1176 if (error) 1177 psignal(curproc, info.si_signo); 1178 else 1179 uap->sysmsg_result = info.si_signo; 1180 return (error); 1181 } 1182 1183 int 1184 sigwaitinfo(struct sigwaitinfo_args *uap) 1185 { 1186 siginfo_t info; 1187 sigset_t set; 1188 int error; 1189 1190 error = copyin(uap->set, &set, sizeof(set)); 1191 if (error) 1192 return (error); 1193 error = kern_sigtimedwait(set, &info, NULL); 1194 if (error) 1195 return (error); 1196 if (uap->info) 1197 error = copyout(&info, uap->info, sizeof(info)); 1198 /* Repost if we got an error. */ 1199 if (error) 1200 psignal(curproc, info.si_signo); 1201 else 1202 uap->sysmsg_result = info.si_signo; 1203 return (error); 1204 } 1205 1206 /* 1207 * If the current process has received a signal that would interrupt a 1208 * system call, return EINTR or ERESTART as appropriate. 1209 */ 1210 int 1211 iscaught(struct proc *p) 1212 { 1213 int sig; 1214 1215 if (p) { 1216 if ((sig = CURSIG(p)) != 0) { 1217 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig)) 1218 return (EINTR); 1219 return (ERESTART); 1220 } 1221 } 1222 return(EWOULDBLOCK); 1223 } 1224 1225 /* 1226 * If the current process has received a signal (should be caught or cause 1227 * termination, should interrupt current syscall), return the signal number. 1228 * Stop signals with default action are processed immediately, then cleared; 1229 * they aren't returned. This is checked after each entry to the system for 1230 * a syscall or trap (though this can usually be done without calling issignal 1231 * by checking the pending signal masks in the CURSIG macro.) The normal call 1232 * sequence is 1233 * 1234 * This routine is called via CURSIG/__cursig and the MP lock might not be 1235 * held. Obtain the MP lock for the duration of the operation. 1236 * 1237 * while (sig = CURSIG(curproc)) 1238 * postsig(sig); 1239 */ 1240 int 1241 issignal(struct proc *p) 1242 { 1243 sigset_t mask; 1244 int sig, prop; 1245 1246 get_mplock(); 1247 for (;;) { 1248 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 1249 1250 mask = p->p_siglist; 1251 SIGSETNAND(mask, p->p_sigmask); 1252 if (p->p_flag & P_PPWAIT) 1253 SIG_STOPSIGMASK(mask); 1254 if (!SIGNOTEMPTY(mask)) { /* no signal to send */ 1255 rel_mplock(); 1256 return (0); 1257 } 1258 sig = sig_ffs(&mask); 1259 1260 STOPEVENT(p, S_SIG, sig); 1261 1262 /* 1263 * We should see pending but ignored signals 1264 * only if P_TRACED was on when they were posted. 1265 */ 1266 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 1267 SIGDELSET(p->p_siglist, sig); 1268 continue; 1269 } 1270 if ((p->p_flag & P_TRACED) && (p->p_flag & P_PPWAIT) == 0) { 1271 /* 1272 * If traced, always stop, and stay stopped until 1273 * released by the parent. 1274 * 1275 * NOTE: P_STOPPED may get cleared during the loop, 1276 * but we do not re-notify the parent if we have 1277 * to loop several times waiting for the parent 1278 * to let us continue. 1279 */ 1280 p->p_xstat = sig; 1281 p->p_flag |= P_STOPPED; 1282 p->p_flag &= ~P_WAITED; 1283 psignal(p->p_pptr, SIGCHLD); 1284 do { 1285 tstop(p); 1286 } while (!trace_req(p) && (p->p_flag & P_TRACED)); 1287 p->p_flag &= ~P_STOPPED; 1288 1289 /* 1290 * If parent wants us to take the signal, 1291 * then it will leave it in p->p_xstat; 1292 * otherwise we just look for signals again. 1293 */ 1294 SIGDELSET(p->p_siglist, sig); /* clear old signal */ 1295 sig = p->p_xstat; 1296 if (sig == 0) 1297 continue; 1298 1299 /* 1300 * Put the new signal into p_siglist. If the 1301 * signal is being masked, look for other signals. 1302 */ 1303 SIGADDSET(p->p_siglist, sig); 1304 if (SIGISMEMBER(p->p_sigmask, sig)) 1305 continue; 1306 1307 /* 1308 * If the traced bit got turned off, go back up 1309 * to the top to rescan signals. This ensures 1310 * that p_sig* and ps_sigact are consistent. 1311 */ 1312 if ((p->p_flag & P_TRACED) == 0) 1313 continue; 1314 } 1315 1316 prop = sigprop(sig); 1317 1318 /* 1319 * Decide whether the signal should be returned. 1320 * Return the signal's number, or fall through 1321 * to clear it from the pending mask. 1322 */ 1323 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1324 case (int)SIG_DFL: 1325 /* 1326 * Don't take default actions on system processes. 1327 */ 1328 if (p->p_pid <= 1) { 1329 #ifdef DIAGNOSTIC 1330 /* 1331 * Are you sure you want to ignore SIGSEGV 1332 * in init? XXX 1333 */ 1334 printf("Process (pid %lu) got signal %d\n", 1335 (u_long)p->p_pid, sig); 1336 #endif 1337 break; /* == ignore */ 1338 } 1339 1340 /* 1341 * Handle the in-kernel checkpoint action 1342 */ 1343 if (prop & SA_CKPT) { 1344 checkpoint_signal_handler(p); 1345 break; 1346 } 1347 1348 /* 1349 * If there is a pending stop signal to process 1350 * with default action, stop here, 1351 * then clear the signal. However, 1352 * if process is member of an orphaned 1353 * process group, ignore tty stop signals. 1354 */ 1355 if (prop & SA_STOP) { 1356 if (p->p_flag & P_TRACED || 1357 (p->p_pgrp->pg_jobc == 0 && 1358 prop & SA_TTYSTOP)) 1359 break; /* == ignore */ 1360 p->p_xstat = sig; 1361 p->p_flag |= P_STOPPED; 1362 p->p_flag &= ~P_WAITED; 1363 1364 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1365 psignal(p->p_pptr, SIGCHLD); 1366 while (p->p_flag & P_STOPPED) { 1367 tstop(p); 1368 } 1369 break; 1370 } else if (prop & SA_IGNORE) { 1371 /* 1372 * Except for SIGCONT, shouldn't get here. 1373 * Default action is to ignore; drop it. 1374 */ 1375 break; /* == ignore */ 1376 } else { 1377 rel_mplock(); 1378 return (sig); 1379 } 1380 1381 /*NOTREACHED*/ 1382 1383 case (int)SIG_IGN: 1384 /* 1385 * Masking above should prevent us ever trying 1386 * to take action on an ignored signal other 1387 * than SIGCONT, unless process is traced. 1388 */ 1389 if ((prop & SA_CONT) == 0 && 1390 (p->p_flag & P_TRACED) == 0) 1391 printf("issignal\n"); 1392 break; /* == ignore */ 1393 1394 default: 1395 /* 1396 * This signal has an action, let 1397 * postsig() process it. 1398 */ 1399 rel_mplock(); 1400 return (sig); 1401 } 1402 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1403 } 1404 /* NOTREACHED */ 1405 } 1406 1407 /* 1408 * Take the action for the specified signal 1409 * from the current set of pending signals. 1410 */ 1411 void 1412 postsig(int sig) 1413 { 1414 struct proc *p = curproc; 1415 struct sigacts *ps = p->p_sigacts; 1416 sig_t action; 1417 sigset_t returnmask; 1418 int code; 1419 1420 KASSERT(sig != 0, ("postsig")); 1421 1422 SIGDELSET(p->p_siglist, sig); 1423 action = ps->ps_sigact[_SIG_IDX(sig)]; 1424 #ifdef KTRACE 1425 if (KTRPOINT(p->p_thread, KTR_PSIG)) 1426 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 1427 &p->p_oldsigmask : &p->p_sigmask, 0); 1428 #endif 1429 STOPEVENT(p, S_SIG, sig); 1430 1431 if (action == SIG_DFL) { 1432 /* 1433 * Default action, where the default is to kill 1434 * the process. (Other cases were ignored above.) 1435 */ 1436 sigexit(p, sig); 1437 /* NOTREACHED */ 1438 } else { 1439 /* 1440 * If we get here, the signal must be caught. 1441 */ 1442 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 1443 ("postsig action")); 1444 /* 1445 * Set the new mask value and also defer further 1446 * occurrences of this signal. 1447 * 1448 * Special case: user has done a sigsuspend. Here the 1449 * current mask is not of interest, but rather the 1450 * mask from before the sigsuspend is what we want 1451 * restored after the signal processing is completed. 1452 */ 1453 crit_enter(); 1454 if (p->p_flag & P_OLDMASK) { 1455 returnmask = p->p_oldsigmask; 1456 p->p_flag &= ~P_OLDMASK; 1457 } else { 1458 returnmask = p->p_sigmask; 1459 } 1460 1461 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1462 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1463 SIGADDSET(p->p_sigmask, sig); 1464 1465 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1466 /* 1467 * See kern_sigaction() for origin of this code. 1468 */ 1469 SIGDELSET(p->p_sigcatch, sig); 1470 if (sig != SIGCONT && 1471 sigprop(sig) & SA_IGNORE) 1472 SIGADDSET(p->p_sigignore, sig); 1473 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1474 } 1475 crit_exit(); 1476 p->p_stats->p_ru.ru_nsignals++; 1477 if (p->p_sig != sig) { 1478 code = 0; 1479 } else { 1480 code = p->p_code; 1481 p->p_code = 0; 1482 p->p_sig = 0; 1483 } 1484 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1485 } 1486 } 1487 1488 /* 1489 * Kill the current process for stated reason. 1490 */ 1491 void 1492 killproc(struct proc *p, char *why) 1493 { 1494 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1495 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1496 psignal(p, SIGKILL); 1497 } 1498 1499 /* 1500 * Force the current process to exit with the specified signal, dumping core 1501 * if appropriate. We bypass the normal tests for masked and caught signals, 1502 * allowing unrecoverable failures to terminate the process without changing 1503 * signal state. Mark the accounting record with the signal termination. 1504 * If dumping core, save the signal number for the debugger. Calls exit and 1505 * does not return. 1506 */ 1507 void 1508 sigexit(struct proc *p, int sig) 1509 { 1510 p->p_acflag |= AXSIG; 1511 if (sigprop(sig) & SA_CORE) { 1512 p->p_sig = sig; 1513 /* 1514 * Log signals which would cause core dumps 1515 * (Log as LOG_INFO to appease those who don't want 1516 * these messages.) 1517 * XXX : Todo, as well as euid, write out ruid too 1518 */ 1519 if (coredump(p) == 0) 1520 sig |= WCOREFLAG; 1521 if (kern_logsigexit) 1522 log(LOG_INFO, 1523 "pid %d (%s), uid %d: exited on signal %d%s\n", 1524 p->p_pid, p->p_comm, 1525 p->p_ucred ? p->p_ucred->cr_uid : -1, 1526 sig &~ WCOREFLAG, 1527 sig & WCOREFLAG ? " (core dumped)" : ""); 1528 } 1529 exit1(W_EXITCODE(0, sig)); 1530 /* NOTREACHED */ 1531 } 1532 1533 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1534 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1535 sizeof(corefilename), "process corefile name format string"); 1536 1537 /* 1538 * expand_name(name, uid, pid) 1539 * Expand the name described in corefilename, using name, uid, and pid. 1540 * corefilename is a printf-like string, with three format specifiers: 1541 * %N name of process ("name") 1542 * %P process id (pid) 1543 * %U user id (uid) 1544 * For example, "%N.core" is the default; they can be disabled completely 1545 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1546 * This is controlled by the sysctl variable kern.corefile (see above). 1547 */ 1548 1549 static char * 1550 expand_name(const char *name, uid_t uid, pid_t pid) 1551 { 1552 char *temp; 1553 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1554 int i, n; 1555 char *format = corefilename; 1556 size_t namelen; 1557 1558 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 1559 if (temp == NULL) 1560 return NULL; 1561 namelen = strlen(name); 1562 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1563 int l; 1564 switch (format[i]) { 1565 case '%': /* Format character */ 1566 i++; 1567 switch (format[i]) { 1568 case '%': 1569 temp[n++] = '%'; 1570 break; 1571 case 'N': /* process name */ 1572 if ((n + namelen) > MAXPATHLEN) { 1573 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1574 pid, name, uid, temp, name); 1575 free(temp, M_TEMP); 1576 return NULL; 1577 } 1578 memcpy(temp+n, name, namelen); 1579 n += namelen; 1580 break; 1581 case 'P': /* process id */ 1582 l = sprintf(buf, "%u", pid); 1583 if ((n + l) > MAXPATHLEN) { 1584 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1585 pid, name, uid, temp, name); 1586 free(temp, M_TEMP); 1587 return NULL; 1588 } 1589 memcpy(temp+n, buf, l); 1590 n += l; 1591 break; 1592 case 'U': /* user id */ 1593 l = sprintf(buf, "%u", uid); 1594 if ((n + l) > MAXPATHLEN) { 1595 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1596 pid, name, uid, temp, name); 1597 free(temp, M_TEMP); 1598 return NULL; 1599 } 1600 memcpy(temp+n, buf, l); 1601 n += l; 1602 break; 1603 default: 1604 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1605 } 1606 break; 1607 default: 1608 temp[n++] = format[i]; 1609 } 1610 } 1611 temp[n] = '\0'; 1612 return temp; 1613 } 1614 1615 /* 1616 * Dump a process' core. The main routine does some 1617 * policy checking, and creates the name of the coredump; 1618 * then it passes on a vnode and a size limit to the process-specific 1619 * coredump routine if there is one; if there _is not_ one, it returns 1620 * ENOSYS; otherwise it returns the error from the process-specific routine. 1621 */ 1622 1623 static int 1624 coredump(struct proc *p) 1625 { 1626 struct vnode *vp; 1627 struct ucred *cred = p->p_ucred; 1628 struct thread *td = p->p_thread; 1629 struct flock lf; 1630 struct nlookupdata nd; 1631 struct vattr vattr; 1632 int error, error1; 1633 char *name; /* name of corefile */ 1634 off_t limit; 1635 1636 STOPEVENT(p, S_CORE, 0); 1637 1638 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) 1639 return (EFAULT); 1640 1641 /* 1642 * Note that the bulk of limit checking is done after 1643 * the corefile is created. The exception is if the limit 1644 * for corefiles is 0, in which case we don't bother 1645 * creating the corefile at all. This layout means that 1646 * a corefile is truncated instead of not being created, 1647 * if it is larger than the limit. 1648 */ 1649 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1650 if (limit == 0) 1651 return EFBIG; 1652 1653 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1654 if (name == NULL) 1655 return (EINVAL); 1656 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP); 1657 if (error == 0) 1658 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR); 1659 free(name, M_TEMP); 1660 if (error) { 1661 nlookup_done(&nd); 1662 return (error); 1663 } 1664 vp = nd.nl_open_vp; 1665 nd.nl_open_vp = NULL; 1666 nlookup_done(&nd); 1667 1668 VOP_UNLOCK(vp, 0, td); 1669 lf.l_whence = SEEK_SET; 1670 lf.l_start = 0; 1671 lf.l_len = 0; 1672 lf.l_type = F_WRLCK; 1673 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 1674 if (error) 1675 goto out2; 1676 1677 /* Don't dump to non-regular files or files with links. */ 1678 if (vp->v_type != VREG || 1679 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) { 1680 error = EFAULT; 1681 goto out1; 1682 } 1683 1684 VATTR_NULL(&vattr); 1685 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1686 vattr.va_size = 0; 1687 VOP_LEASE(vp, td, cred, LEASE_WRITE); 1688 VOP_SETATTR(vp, &vattr, cred, td); 1689 p->p_acflag |= ACORE; 1690 VOP_UNLOCK(vp, 0, td); 1691 1692 error = p->p_sysent->sv_coredump ? 1693 p->p_sysent->sv_coredump(p, vp, limit) : ENOSYS; 1694 1695 out1: 1696 lf.l_type = F_UNLCK; 1697 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 1698 out2: 1699 error1 = vn_close(vp, FWRITE, td); 1700 if (error == 0) 1701 error = error1; 1702 return (error); 1703 } 1704 1705 /* 1706 * Nonexistent system call-- signal process (may want to handle it). 1707 * Flag error in case process won't see signal immediately (blocked or ignored). 1708 */ 1709 /* ARGSUSED */ 1710 int 1711 nosys(struct nosys_args *args) 1712 { 1713 psignal(curproc, SIGSYS); 1714 return (EINVAL); 1715 } 1716 1717 /* 1718 * Send a SIGIO or SIGURG signal to a process or process group using 1719 * stored credentials rather than those of the current process. 1720 */ 1721 void 1722 pgsigio(struct sigio *sigio, int sig, int checkctty) 1723 { 1724 if (sigio == NULL) 1725 return; 1726 1727 if (sigio->sio_pgid > 0) { 1728 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1729 sigio->sio_proc)) 1730 psignal(sigio->sio_proc, sig); 1731 } else if (sigio->sio_pgid < 0) { 1732 struct proc *p; 1733 1734 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) 1735 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1736 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1737 psignal(p, sig); 1738 } 1739 } 1740 1741 static int 1742 filt_sigattach(struct knote *kn) 1743 { 1744 struct proc *p = curproc; 1745 1746 kn->kn_ptr.p_proc = p; 1747 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1748 1749 /* XXX lock the proc here while adding to the list? */ 1750 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 1751 1752 return (0); 1753 } 1754 1755 static void 1756 filt_sigdetach(struct knote *kn) 1757 { 1758 struct proc *p = kn->kn_ptr.p_proc; 1759 1760 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 1761 } 1762 1763 /* 1764 * signal knotes are shared with proc knotes, so we apply a mask to 1765 * the hint in order to differentiate them from process hints. This 1766 * could be avoided by using a signal-specific knote list, but probably 1767 * isn't worth the trouble. 1768 */ 1769 static int 1770 filt_signal(struct knote *kn, long hint) 1771 { 1772 if (hint & NOTE_SIGNAL) { 1773 hint &= ~NOTE_SIGNAL; 1774 1775 if (kn->kn_id == hint) 1776 kn->kn_data++; 1777 } 1778 return (kn->kn_data != 0); 1779 } 1780