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