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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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.10 2003/07/12 17:54:35 dillon Exp $
43 #include "opt_compat.h"
44 #include "opt_ktrace.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/sysproto.h>
50 #include <sys/signalvar.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53 #include <sys/event.h>
55 #include <sys/namei.h>
56 #include <sys/pioctl.h>
57 #include <sys/systm.h>
59 #include <sys/fcntl.h>
61 #include <sys/ktrace.h>
62 #include <sys/syslog.h>
64 #include <sys/sysent.h>
65 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
67 #include <sys/unistd.h>
70 #include <machine/ipl.h>
71 #include <machine/cpu.h>
72 #include <machine/smp.h>
74 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
76 static int coredump __P((struct proc *));
77 static int do_sigaction __P((int sig, struct sigaction *act,
78 struct sigaction *oact, int old));
79 static int do_sigprocmask __P((int how, sigset_t *set,
80 sigset_t *oset, int old));
81 static char *expand_name __P((const char *, uid_t, pid_t));
82 static int killpg1 __P((int sig, int pgid, int all));
83 static int sig_ffs __P((sigset_t *set));
84 static int sigprop __P((int sig));
85 static void stop __P((struct proc *));
87 static void signotify_remote(void *arg);
90 static int filt_sigattach(struct knote *kn);
91 static void filt_sigdetach(struct knote *kn);
92 static int filt_signal(struct knote *kn, long hint);
94 struct filterops sig_filtops =
95 { 0, filt_sigattach, filt_sigdetach, filt_signal };
97 static int kern_logsigexit = 1;
98 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
100 "Log processes quitting on abnormal signals to syslog(3)");
103 * Can process p, with pcred pc, send the signal sig to process q?
105 #define CANSIGNAL(q, sig) \
106 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
107 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
110 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
112 #define CANSIGIO(ruid, uc, q) \
113 ((uc)->cr_uid == 0 || \
114 (ruid) == (q)->p_ucred->cr_ruid || \
115 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
116 (ruid) == (q)->p_ucred->cr_uid || \
117 (uc)->cr_uid == (q)->p_ucred->cr_uid)
120 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
121 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
123 static int do_coredump = 1;
124 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
125 &do_coredump, 0, "Enable/Disable coredumps");
128 * Signal properties and actions.
129 * The array below categorizes the signals and their default actions
130 * according to the following properties:
132 #define SA_KILL 0x01 /* terminates process by default */
133 #define SA_CORE 0x02 /* ditto and coredumps */
134 #define SA_STOP 0x04 /* suspend process */
135 #define SA_TTYSTOP 0x08 /* ditto, from tty */
136 #define SA_IGNORE 0x10 /* ignore by default */
137 #define SA_CONT 0x20 /* continue if suspended */
138 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
140 static int sigproptbl[NSIG] = {
141 SA_KILL, /* SIGHUP */
142 SA_KILL, /* SIGINT */
143 SA_KILL|SA_CORE, /* SIGQUIT */
144 SA_KILL|SA_CORE, /* SIGILL */
145 SA_KILL|SA_CORE, /* SIGTRAP */
146 SA_KILL|SA_CORE, /* SIGABRT */
147 SA_KILL|SA_CORE, /* SIGEMT */
148 SA_KILL|SA_CORE, /* SIGFPE */
149 SA_KILL, /* SIGKILL */
150 SA_KILL|SA_CORE, /* SIGBUS */
151 SA_KILL|SA_CORE, /* SIGSEGV */
152 SA_KILL|SA_CORE, /* SIGSYS */
153 SA_KILL, /* SIGPIPE */
154 SA_KILL, /* SIGALRM */
155 SA_KILL, /* SIGTERM */
156 SA_IGNORE, /* SIGURG */
157 SA_STOP, /* SIGSTOP */
158 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
159 SA_IGNORE|SA_CONT, /* SIGCONT */
160 SA_IGNORE, /* SIGCHLD */
161 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
162 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
163 SA_IGNORE, /* SIGIO */
164 SA_KILL, /* SIGXCPU */
165 SA_KILL, /* SIGXFSZ */
166 SA_KILL, /* SIGVTALRM */
167 SA_KILL, /* SIGPROF */
168 SA_IGNORE, /* SIGWINCH */
169 SA_IGNORE, /* SIGINFO */
170 SA_KILL, /* SIGUSR1 */
171 SA_KILL, /* SIGUSR2 */
178 if (sig > 0 && sig < NSIG)
179 return (sigproptbl[_SIG_IDX(sig)]);
184 sig_ffs(sigset_t *set)
188 for (i = 0; i < _SIG_WORDS; i++)
190 return (ffs(set->__bits[i]) + (i * 32));
200 do_sigaction(int sig, struct sigaction *act, struct sigaction *oact, int old)
202 struct proc *p = curproc;
203 struct sigacts *ps = p->p_sigacts;
205 if (sig <= 0 || sig > _SIG_MAXSIG)
209 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
210 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
212 if (SIGISMEMBER(ps->ps_sigonstack, sig))
213 oact->sa_flags |= SA_ONSTACK;
214 if (!SIGISMEMBER(ps->ps_sigintr, sig))
215 oact->sa_flags |= SA_RESTART;
216 if (SIGISMEMBER(ps->ps_sigreset, sig))
217 oact->sa_flags |= SA_RESETHAND;
218 if (SIGISMEMBER(ps->ps_signodefer, sig))
219 oact->sa_flags |= SA_NODEFER;
220 if (SIGISMEMBER(ps->ps_siginfo, sig))
221 oact->sa_flags |= SA_SIGINFO;
222 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
223 oact->sa_flags |= SA_NOCLDSTOP;
224 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
225 oact->sa_flags |= SA_NOCLDWAIT;
228 if ((sig == SIGKILL || sig == SIGSTOP) &&
229 act->sa_handler != SIG_DFL)
233 * Change setting atomically.
237 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
238 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
239 if (act->sa_flags & SA_SIGINFO) {
240 ps->ps_sigact[_SIG_IDX(sig)] =
241 (__sighandler_t *)act->sa_sigaction;
242 SIGADDSET(ps->ps_siginfo, sig);
244 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
245 SIGDELSET(ps->ps_siginfo, sig);
247 if (!(act->sa_flags & SA_RESTART))
248 SIGADDSET(ps->ps_sigintr, sig);
250 SIGDELSET(ps->ps_sigintr, sig);
251 if (act->sa_flags & SA_ONSTACK)
252 SIGADDSET(ps->ps_sigonstack, sig);
254 SIGDELSET(ps->ps_sigonstack, sig);
255 if (act->sa_flags & SA_RESETHAND)
256 SIGADDSET(ps->ps_sigreset, sig);
258 SIGDELSET(ps->ps_sigreset, sig);
259 if (act->sa_flags & SA_NODEFER)
260 SIGADDSET(ps->ps_signodefer, sig);
262 SIGDELSET(ps->ps_signodefer, sig);
264 if (act->sa_flags & SA_USERTRAMP)
265 SIGADDSET(ps->ps_usertramp, sig);
267 SIGDELSET(ps->ps_usertramp, seg);
269 if (sig == SIGCHLD) {
270 if (act->sa_flags & SA_NOCLDSTOP)
271 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
273 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
274 if (act->sa_flags & SA_NOCLDWAIT) {
276 * Paranoia: since SA_NOCLDWAIT is implemented
277 * by reparenting the dying child to PID 1 (and
278 * trust it to reap the zombie), PID 1 itself
279 * is forbidden to set SA_NOCLDWAIT.
282 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
284 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
286 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
289 * Set bit in p_sigignore for signals that are set to SIG_IGN,
290 * and for signals set to SIG_DFL where the default is to
291 * ignore. However, don't put SIGCONT in p_sigignore, as we
292 * have to restart the process.
294 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
295 (sigprop(sig) & SA_IGNORE &&
296 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
297 /* never to be seen again */
298 SIGDELSET(p->p_siglist, sig);
300 /* easier in psignal */
301 SIGADDSET(p->p_sigignore, sig);
302 SIGDELSET(p->p_sigcatch, sig);
304 SIGDELSET(p->p_sigignore, sig);
305 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
306 SIGDELSET(p->p_sigcatch, sig);
308 SIGADDSET(p->p_sigcatch, sig);
310 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
311 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
312 SIGDELSET(ps->ps_osigset, sig);
314 SIGADDSET(ps->ps_osigset, sig);
321 #ifndef _SYS_SYSPROTO_H_
322 struct sigaction_args {
324 struct sigaction *act;
325 struct sigaction *oact;
330 sigaction(struct sigaction_args *uap)
332 struct sigaction act, oact;
333 register struct sigaction *actp, *oactp;
336 actp = (uap->act != NULL) ? &act : NULL;
337 oactp = (uap->oact != NULL) ? &oact : NULL;
339 error = copyin(uap->act, actp, sizeof(act));
343 error = do_sigaction(uap->sig, actp, oactp, 0);
344 if (oactp && !error) {
345 error = copyout(oactp, uap->oact, sizeof(oact));
350 #ifndef _SYS_SYSPROTO_H_
351 struct osigaction_args {
353 struct osigaction *nsa;
354 struct osigaction *osa;
359 osigaction(struct osigaction_args *uap)
361 struct osigaction sa;
362 struct sigaction nsa, osa;
363 register struct sigaction *nsap, *osap;
366 if (uap->signum <= 0 || uap->signum >= ONSIG)
368 nsap = (uap->nsa != NULL) ? &nsa : NULL;
369 osap = (uap->osa != NULL) ? &osa : NULL;
371 error = copyin(uap->nsa, &sa, sizeof(sa));
374 nsap->sa_handler = sa.sa_handler;
375 nsap->sa_flags = sa.sa_flags;
376 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
378 error = do_sigaction(uap->signum, nsap, osap, 1);
379 if (osap && !error) {
380 sa.sa_handler = osap->sa_handler;
381 sa.sa_flags = osap->sa_flags;
382 SIG2OSIG(osap->sa_mask, sa.sa_mask);
383 error = copyout(&sa, uap->osa, sizeof(sa));
389 * Initialize signal state for process 0;
390 * set to ignore signals that are ignored by default.
398 for (i = 1; i <= NSIG; i++)
399 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
400 SIGADDSET(p->p_sigignore, i);
404 * Reset signals for an exec of the specified process.
408 register struct proc *p;
410 register struct sigacts *ps = p->p_sigacts;
414 * Reset caught signals. Held signals remain held
415 * through p_sigmask (unless they were caught,
416 * and are now ignored by default).
418 while (SIGNOTEMPTY(p->p_sigcatch)) {
419 sig = sig_ffs(&p->p_sigcatch);
420 SIGDELSET(p->p_sigcatch, sig);
421 if (sigprop(sig) & SA_IGNORE) {
423 SIGADDSET(p->p_sigignore, sig);
424 SIGDELSET(p->p_siglist, sig);
426 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
429 * Reset stack state to the user stack.
430 * Clear set of signals caught on the signal stack.
432 p->p_sigstk.ss_flags = SS_DISABLE;
433 p->p_sigstk.ss_size = 0;
434 p->p_sigstk.ss_sp = 0;
435 p->p_flag &= ~P_ALTSTACK;
437 * Reset no zombies if child dies flag as Solaris does.
439 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
443 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
445 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
446 * p == curproc. Also remember that in order to remain MP SAFE
447 * no spl*() calls may be made.
450 do_sigprocmask(int how, sigset_t *set, sigset_t *oset, int old)
452 struct proc *p = curproc;
456 *oset = p->p_sigmask;
463 SIGSETOR(p->p_sigmask, *set);
466 SIGSETNAND(p->p_sigmask, *set);
471 SIGSETLO(p->p_sigmask, *set);
484 * sigprocmask() - MP SAFE
487 #ifndef _SYS_SYSPROTO_H_
488 struct sigprocmask_args {
495 sigprocmask(struct sigprocmask_args *uap)
498 sigset_t *setp, *osetp;
501 setp = (uap->set != NULL) ? &set : NULL;
502 osetp = (uap->oset != NULL) ? &oset : NULL;
504 error = copyin(uap->set, setp, sizeof(set));
508 error = do_sigprocmask(uap->how, setp, osetp, 0);
509 if (osetp && !error) {
510 error = copyout(osetp, uap->oset, sizeof(oset));
516 * osigprocmask() - MP SAFE
519 #ifndef _SYS_SYSPROTO_H_
520 struct osigprocmask_args {
526 osigprocmask(struct osigprocmask_args *uap)
531 OSIG2SIG(uap->mask, set);
532 error = do_sigprocmask(uap->how, &set, &oset, 1);
533 SIG2OSIG(oset, curproc->p_retval[0]);
537 #ifndef _SYS_SYSPROTO_H_
538 struct sigpending_args {
544 sigpending(struct sigpending_args *uap)
546 struct proc *p = curproc;
548 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
551 #ifndef _SYS_SYSPROTO_H_
552 struct osigpending_args {
558 osigpending(struct osigpending_args *uap)
560 struct proc *p = curproc;
562 SIG2OSIG(p->p_siglist, p->p_retval[0]);
566 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
568 * Generalized interface signal handler, 4.3-compatible.
570 #ifndef _SYS_SYSPROTO_H_
571 struct osigvec_args {
579 osigvec(struct osigvec_args *uap)
582 struct sigaction nsa, osa;
583 register struct sigaction *nsap, *osap;
586 if (uap->signum <= 0 || uap->signum >= ONSIG)
588 nsap = (uap->nsv != NULL) ? &nsa : NULL;
589 osap = (uap->osv != NULL) ? &osa : NULL;
591 error = copyin(uap->nsv, &vec, sizeof(vec));
594 nsap->sa_handler = vec.sv_handler;
595 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
596 nsap->sa_flags = vec.sv_flags;
597 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
599 nsap->sa_flags |= SA_USERTRAMP;
602 error = do_sigaction(uap->signum, nsap, osap, 1);
603 if (osap && !error) {
604 vec.sv_handler = osap->sa_handler;
605 SIG2OSIG(osap->sa_mask, vec.sv_mask);
606 vec.sv_flags = osap->sa_flags;
607 vec.sv_flags &= ~SA_NOCLDWAIT;
608 vec.sv_flags ^= SA_RESTART;
610 vec.sv_flags &= ~SA_NOCLDSTOP;
612 error = copyout(&vec, uap->osv, sizeof(vec));
617 #ifndef _SYS_SYSPROTO_H_
618 struct osigblock_args {
623 osigblock(struct osigblock_args *uap)
625 struct proc *p = curproc;
628 OSIG2SIG(uap->mask, set);
631 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
632 SIGSETOR(p->p_sigmask, set);
637 #ifndef _SYS_SYSPROTO_H_
638 struct osigsetmask_args {
643 osigsetmask(struct osigsetmask_args *uap)
645 struct proc *p = curproc;
648 OSIG2SIG(uap->mask, set);
651 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
652 SIGSETLO(p->p_sigmask, set);
656 #endif /* COMPAT_43 || COMPAT_SUNOS */
659 * Suspend process until signal, providing mask to be set
660 * in the meantime. Note nonstandard calling convention:
661 * libc stub passes mask, not pointer, to save a copyin.
663 #ifndef _SYS_SYSPROTO_H_
664 struct sigsuspend_args {
665 const sigset_t *sigmask;
670 sigsuspend(struct sigsuspend_args *uap)
672 struct proc *p = curproc;
674 struct sigacts *ps = p->p_sigacts;
677 error = copyin(uap->sigmask, &mask, sizeof(mask));
682 * When returning from sigsuspend, we want
683 * the old mask to be restored after the
684 * signal handler has finished. Thus, we
685 * save it here and mark the sigacts structure
688 p->p_oldsigmask = p->p_sigmask;
689 p->p_flag |= P_OLDMASK;
693 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
695 /* always return EINTR rather than ERESTART... */
699 #ifndef _SYS_SYSPROTO_H_
700 struct osigsuspend_args {
706 osigsuspend(struct osigsuspend_args *uap)
709 struct proc *p = curproc;
710 struct sigacts *ps = p->p_sigacts;
712 p->p_oldsigmask = p->p_sigmask;
713 p->p_flag |= P_OLDMASK;
714 OSIG2SIG(uap->mask, mask);
716 SIGSETLO(p->p_sigmask, mask);
717 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
719 /* always return EINTR rather than ERESTART... */
723 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
724 #ifndef _SYS_SYSPROTO_H_
725 struct osigstack_args {
726 struct sigstack *nss;
727 struct sigstack *oss;
732 osigstack(struct osigstack_args *uap)
734 struct proc *p = curproc;
738 ss.ss_sp = p->p_sigstk.ss_sp;
739 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
740 if (uap->oss && (error = copyout(&ss, uap->oss,
741 sizeof(struct sigstack))))
743 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
744 p->p_sigstk.ss_sp = ss.ss_sp;
745 p->p_sigstk.ss_size = 0;
746 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
747 p->p_flag |= P_ALTSTACK;
751 #endif /* COMPAT_43 || COMPAT_SUNOS */
753 #ifndef _SYS_SYSPROTO_H_
754 struct sigaltstack_args {
761 sigaltstack(struct sigaltstack_args *uap)
763 struct proc *p = curproc;
767 if ((p->p_flag & P_ALTSTACK) == 0)
768 p->p_sigstk.ss_flags |= SS_DISABLE;
769 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
774 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
776 if (ss.ss_flags & SS_DISABLE) {
777 if (p->p_sigstk.ss_flags & SS_ONSTACK)
779 p->p_flag &= ~P_ALTSTACK;
780 p->p_sigstk.ss_flags = ss.ss_flags;
783 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
785 p->p_flag |= P_ALTSTACK;
791 * Common code for kill process group/broadcast kill.
792 * cp is calling process.
795 killpg1(int sig, int pgid, int all)
797 struct proc *cp = curproc;
806 LIST_FOREACH(p, &allproc, p_list) {
807 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
808 p == cp || !CANSIGNAL(p, sig))
817 * zero pgid means send to my process group.
825 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
826 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
827 p->p_stat == SZOMB ||
835 return (nfound ? 0 : ESRCH);
838 #ifndef _SYS_SYSPROTO_H_
846 kill(struct kill_args *uap)
850 if ((u_int)uap->signum > _SIG_MAXSIG)
853 /* kill single process */
854 if ((p = pfind(uap->pid)) == NULL)
856 if (!CANSIGNAL(p, uap->signum))
859 psignal(p, uap->signum);
863 case -1: /* broadcast signal */
864 return (killpg1(uap->signum, 0, 1));
865 case 0: /* signal own process group */
866 return (killpg1(uap->signum, 0, 0));
867 default: /* negative explicit process group */
868 return (killpg1(uap->signum, -uap->pid, 0));
873 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
874 #ifndef _SYS_SYSPROTO_H_
875 struct okillpg_args {
882 okillpg(struct okillpg_args *uap)
884 if ((u_int)uap->signum > _SIG_MAXSIG)
886 return (killpg1(uap->signum, uap->pgid, 0));
888 #endif /* COMPAT_43 || COMPAT_SUNOS */
891 * Send a signal to a process group.
894 gsignal(int pgid, int sig)
898 if (pgid && (pgrp = pgfind(pgid)))
899 pgsignal(pgrp, sig, 0);
903 * Send a signal to a process group. If checktty is 1,
904 * limit to members which have a controlling terminal.
907 pgsignal(pgrp, sig, checkctty)
911 register struct proc *p;
914 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
915 if (checkctty == 0 || p->p_flag & P_CONTROLT)
920 * Send a signal caused by a trap to the current process.
921 * If it will be caught immediately, deliver it with correct code.
922 * Otherwise, post it normally.
925 trapsignal(p, sig, code)
930 struct sigacts *ps = p->p_sigacts;
932 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
933 !SIGISMEMBER(p->p_sigmask, sig)) {
934 p->p_stats->p_ru.ru_nsignals++;
936 if (KTRPOINT(p->p_thread, KTR_PSIG))
937 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
938 &p->p_sigmask, code);
940 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
941 &p->p_sigmask, code);
942 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
943 if (!SIGISMEMBER(ps->ps_signodefer, sig))
944 SIGADDSET(p->p_sigmask, sig);
945 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
947 * See do_sigaction() for origin of this code.
949 SIGDELSET(p->p_sigcatch, sig);
950 if (sig != SIGCONT &&
951 sigprop(sig) & SA_IGNORE)
952 SIGADDSET(p->p_sigignore, sig);
953 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
956 p->p_code = code; /* XXX for core dump/debugger */
957 p->p_sig = sig; /* XXX to verify code */
963 * Send the signal to the process. If the signal has an action, the action
964 * is usually performed by the target process rather than the caller; we add
965 * the signal to the set of pending signals for the process.
968 * o When a stop signal is sent to a sleeping process that takes the
969 * default action, the process is stopped without awakening it.
970 * o SIGCONT restarts stopped processes (or puts them back to sleep)
971 * regardless of the signal action (eg, blocked or ignored).
973 * Other ignored signals are discarded immediately.
978 register struct proc *p;
984 if (sig > _SIG_MAXSIG || sig <= 0) {
985 printf("psignal: signal %d\n", sig);
986 panic("psignal signal number");
990 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
996 * If proc is traced, always give parent a chance;
997 * if signal event is tracked by procfs, give *that*
1000 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1004 * If the signal is being ignored,
1005 * then we forget about it immediately.
1006 * (Note: we don't set SIGCONT in p_sigignore,
1007 * and if it is set to SIG_IGN,
1008 * action will be SIG_DFL here.)
1010 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1012 if (SIGISMEMBER(p->p_sigmask, sig))
1014 else if (SIGISMEMBER(p->p_sigcatch, sig))
1020 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1021 (p->p_flag & P_TRACED) == 0)
1025 SIG_STOPSIGMASK(p->p_siglist);
1027 if (prop & SA_STOP) {
1029 * If sending a tty stop signal to a member of an orphaned
1030 * process group, discard the signal here if the action
1031 * is default; don't stop the process below if sleeping,
1032 * and don't clear any pending SIGCONT.
1034 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1037 SIG_CONTSIGMASK(p->p_siglist);
1039 SIGADDSET(p->p_siglist, sig);
1042 * Defer further processing for signals which are held,
1043 * except that stopped processes must be continued by SIGCONT.
1045 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1048 switch (p->p_stat) {
1052 * If process is sleeping uninterruptibly
1053 * we can't interrupt the sleep... the signal will
1054 * be noticed when the process returns through
1055 * trap() or syscall().
1057 if ((p->p_flag & P_SINTR) == 0)
1060 * Process is sleeping and traced... make it runnable
1061 * so it can discover the signal in issignal() and stop
1064 if (p->p_flag & P_TRACED)
1067 * If SIGCONT is default (or ignored) and process is
1068 * asleep, we are finished; the process should not
1071 if ((prop & SA_CONT) && action == SIG_DFL) {
1072 SIGDELSET(p->p_siglist, sig);
1076 * When a sleeping process receives a stop
1077 * signal, process immediately if possible.
1078 * All other (caught or default) signals
1079 * cause the process to run.
1081 if (prop & SA_STOP) {
1082 if (action != SIG_DFL)
1085 * If a child holding parent blocked,
1086 * stopping could cause deadlock.
1088 if (p->p_flag & P_PPWAIT)
1090 SIGDELSET(p->p_siglist, sig);
1092 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1093 psignal(p->p_pptr, SIGCHLD);
1102 * If traced process is already stopped,
1103 * then no further action is necessary.
1105 if (p->p_flag & P_TRACED)
1109 * Kill signal always sets processes running.
1114 if (prop & SA_CONT) {
1116 * If SIGCONT is default (or ignored), we continue the
1117 * process but don't leave the signal in p_siglist, as
1118 * it has no further action. If SIGCONT is held, we
1119 * continue the process and leave the signal in
1120 * p_siglist. If the process catches SIGCONT, let it
1121 * handle the signal itself. If it isn't waiting on
1122 * an event, then it goes back to run state.
1123 * Otherwise, process goes back to sleep state.
1125 if (action == SIG_DFL)
1126 SIGDELSET(p->p_siglist, sig);
1127 if (action == SIG_CATCH)
1129 if (p->p_wchan == 0)
1131 clrrunnable(p, SSLEEP);
1135 if (prop & SA_STOP) {
1137 * Already stopped, don't need to stop again.
1138 * (If we did the shell could get confused.)
1140 SIGDELSET(p->p_siglist, sig);
1145 * If process is sleeping interruptibly, then simulate a
1146 * wakeup so that when it is continued, it will be made
1147 * runnable and can look at the signal. But don't make
1148 * the process runnable, leave it stopped.
1150 if (p->p_wchan && (p->p_flag & P_SINTR))
1151 unsleep(p->p_thread);
1156 * SRUN, SIDL, SZOMB do nothing with the signal,
1157 * other than kicking ourselves if we are running.
1158 * It will either never be noticed, or noticed very soon.
1160 * For SMP we may have to forward the request to another cpu.
1161 * YYY the MP lock prevents the target process from moving
1162 * to another cpu, see kern/kern_switch.c
1165 if (p == lwkt_preempted_proc()) {
1168 int cpuid = p->p_thread->td_cpu;
1169 if (cpuid != mycpu->gd_cpuid)
1170 lwkt_send_ipiq(cpuid, signotify_remote, p);
1173 if (p == lwkt_preempted_proc())
1188 * This function is called via an IPI. We will be in a critical section but
1189 * the MP lock will NOT be held. Also note that by the time the ipi message
1190 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1193 signotify_remote(void *arg)
1195 struct proc *p = arg;
1196 if (p == lwkt_preempted_proc())
1203 * If the current process has received a signal (should be caught or cause
1204 * termination, should interrupt current syscall), return the signal number.
1205 * Stop signals with default action are processed immediately, then cleared;
1206 * they aren't returned. This is checked after each entry to the system for
1207 * a syscall or trap (though this can usually be done without calling issignal
1208 * by checking the pending signal masks in the CURSIG macro.) The normal call
1211 * while (sig = CURSIG(curproc))
1216 register struct proc *p;
1219 register int sig, prop;
1222 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1224 mask = p->p_siglist;
1225 SIGSETNAND(mask, p->p_sigmask);
1226 if (p->p_flag & P_PPWAIT)
1227 SIG_STOPSIGMASK(mask);
1228 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1230 sig = sig_ffs(&mask);
1232 STOPEVENT(p, S_SIG, sig);
1235 * We should see pending but ignored signals
1236 * only if P_TRACED was on when they were posted.
1238 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1239 SIGDELSET(p->p_siglist, sig);
1242 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1244 * If traced, always stop, and stay
1245 * stopped until released by the parent.
1248 psignal(p->p_pptr, SIGCHLD);
1252 } while (!trace_req(p) && p->p_flag & P_TRACED);
1255 * If parent wants us to take the signal,
1256 * then it will leave it in p->p_xstat;
1257 * otherwise we just look for signals again.
1259 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1265 * Put the new signal into p_siglist. If the
1266 * signal is being masked, look for other signals.
1268 SIGADDSET(p->p_siglist, sig);
1269 if (SIGISMEMBER(p->p_sigmask, sig))
1273 * If the traced bit got turned off, go back up
1274 * to the top to rescan signals. This ensures
1275 * that p_sig* and ps_sigact are consistent.
1277 if ((p->p_flag & P_TRACED) == 0)
1281 prop = sigprop(sig);
1284 * Decide whether the signal should be returned.
1285 * Return the signal's number, or fall through
1286 * to clear it from the pending mask.
1288 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1292 * Don't take default actions on system processes.
1294 if (p->p_pid <= 1) {
1297 * Are you sure you want to ignore SIGSEGV
1300 printf("Process (pid %lu) got signal %d\n",
1301 (u_long)p->p_pid, sig);
1303 break; /* == ignore */
1306 * If there is a pending stop signal to process
1307 * with default action, stop here,
1308 * then clear the signal. However,
1309 * if process is member of an orphaned
1310 * process group, ignore tty stop signals.
1312 if (prop & SA_STOP) {
1313 if (p->p_flag & P_TRACED ||
1314 (p->p_pgrp->pg_jobc == 0 &&
1316 break; /* == ignore */
1319 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1320 psignal(p->p_pptr, SIGCHLD);
1323 } else if (prop & SA_IGNORE) {
1325 * Except for SIGCONT, shouldn't get here.
1326 * Default action is to ignore; drop it.
1328 break; /* == ignore */
1335 * Masking above should prevent us ever trying
1336 * to take action on an ignored signal other
1337 * than SIGCONT, unless process is traced.
1339 if ((prop & SA_CONT) == 0 &&
1340 (p->p_flag & P_TRACED) == 0)
1341 printf("issignal\n");
1342 break; /* == ignore */
1346 * This signal has an action, let
1347 * postsig() process it.
1351 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1357 * Put the argument process into the stopped state and notify the parent
1358 * via wakeup. Signals are handled elsewhere. The process must not be
1363 register struct proc *p;
1367 p->p_flag &= ~P_WAITED;
1368 wakeup((caddr_t)p->p_pptr);
1372 * Take the action for the specified signal
1373 * from the current set of pending signals.
1379 struct proc *p = curproc;
1380 struct sigacts *ps = p->p_sigacts;
1382 sigset_t returnmask;
1385 KASSERT(sig != 0, ("postsig"));
1387 SIGDELSET(p->p_siglist, sig);
1388 action = ps->ps_sigact[_SIG_IDX(sig)];
1390 if (KTRPOINT(p->p_thread, KTR_PSIG))
1391 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1392 &p->p_oldsigmask : &p->p_sigmask, 0);
1394 STOPEVENT(p, S_SIG, sig);
1396 if (action == SIG_DFL) {
1398 * Default action, where the default is to kill
1399 * the process. (Other cases were ignored above.)
1405 * If we get here, the signal must be caught.
1407 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1408 ("postsig action"));
1410 * Set the new mask value and also defer further
1411 * occurrences of this signal.
1413 * Special case: user has done a sigsuspend. Here the
1414 * current mask is not of interest, but rather the
1415 * mask from before the sigsuspend is what we want
1416 * restored after the signal processing is completed.
1419 if (p->p_flag & P_OLDMASK) {
1420 returnmask = p->p_oldsigmask;
1421 p->p_flag &= ~P_OLDMASK;
1423 returnmask = p->p_sigmask;
1425 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1426 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1427 SIGADDSET(p->p_sigmask, sig);
1429 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1431 * See do_sigaction() for origin of this code.
1433 SIGDELSET(p->p_sigcatch, sig);
1434 if (sig != SIGCONT &&
1435 sigprop(sig) & SA_IGNORE)
1436 SIGADDSET(p->p_sigignore, sig);
1437 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1440 p->p_stats->p_ru.ru_nsignals++;
1441 if (p->p_sig != sig) {
1448 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1453 * Kill the current process for stated reason.
1460 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1461 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1462 psignal(p, SIGKILL);
1466 * Force the current process to exit with the specified signal, dumping core
1467 * if appropriate. We bypass the normal tests for masked and caught signals,
1468 * allowing unrecoverable failures to terminate the process without changing
1469 * signal state. Mark the accounting record with the signal termination.
1470 * If dumping core, save the signal number for the debugger. Calls exit and
1474 sigexit(struct proc *p, int sig)
1476 p->p_acflag |= AXSIG;
1477 if (sigprop(sig) & SA_CORE) {
1480 * Log signals which would cause core dumps
1481 * (Log as LOG_INFO to appease those who don't want
1483 * XXX : Todo, as well as euid, write out ruid too
1485 if (coredump(p) == 0)
1487 if (kern_logsigexit)
1489 "pid %d (%s), uid %d: exited on signal %d%s\n",
1490 p->p_pid, p->p_comm,
1491 p->p_ucred ? p->p_ucred->cr_uid : -1,
1493 sig & WCOREFLAG ? " (core dumped)" : "");
1495 exit1(W_EXITCODE(0, sig));
1499 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1500 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1501 sizeof(corefilename), "process corefile name format string");
1504 * expand_name(name, uid, pid)
1505 * Expand the name described in corefilename, using name, uid, and pid.
1506 * corefilename is a printf-like string, with three format specifiers:
1507 * %N name of process ("name")
1508 * %P process id (pid)
1510 * For example, "%N.core" is the default; they can be disabled completely
1511 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1512 * This is controlled by the sysctl variable kern.corefile (see above).
1516 expand_name(name, uid, pid)
1517 const char *name; uid_t uid; pid_t pid; {
1519 char buf[11]; /* Buffer for pid/uid -- max 4B */
1521 char *format = corefilename;
1524 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1527 namelen = strlen(name);
1528 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1530 switch (format[i]) {
1531 case '%': /* Format character */
1533 switch (format[i]) {
1537 case 'N': /* process name */
1538 if ((n + namelen) > MAXPATHLEN) {
1539 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1540 pid, name, uid, temp, name);
1544 memcpy(temp+n, name, namelen);
1547 case 'P': /* process id */
1548 l = sprintf(buf, "%u", pid);
1549 if ((n + l) > MAXPATHLEN) {
1550 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1551 pid, name, uid, temp, name);
1555 memcpy(temp+n, buf, l);
1558 case 'U': /* user id */
1559 l = sprintf(buf, "%u", uid);
1560 if ((n + l) > MAXPATHLEN) {
1561 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1562 pid, name, uid, temp, name);
1566 memcpy(temp+n, buf, l);
1570 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1574 temp[n++] = format[i];
1582 * Dump a process' core. The main routine does some
1583 * policy checking, and creates the name of the coredump;
1584 * then it passes on a vnode and a size limit to the process-specific
1585 * coredump routine if there is one; if there _is not_ one, it returns
1586 * ENOSYS; otherwise it returns the error from the process-specific routine.
1590 coredump(struct proc *p)
1593 struct ucred *cred = p->p_ucred;
1594 struct thread *td = p->p_thread;
1596 struct nameidata nd;
1599 char *name; /* name of corefile */
1602 STOPEVENT(p, S_CORE, 0);
1604 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1608 * Note that the bulk of limit checking is done after
1609 * the corefile is created. The exception is if the limit
1610 * for corefiles is 0, in which case we don't bother
1611 * creating the corefile at all. This layout means that
1612 * a corefile is truncated instead of not being created,
1613 * if it is larger than the limit.
1615 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1619 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1622 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
1623 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1627 NDFREE(&nd, NDF_ONLY_PNBUF);
1630 VOP_UNLOCK(vp, 0, td);
1631 lf.l_whence = SEEK_SET;
1634 lf.l_type = F_WRLCK;
1635 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1639 /* Don't dump to non-regular files or files with links. */
1640 if (vp->v_type != VREG ||
1641 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1647 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1649 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1650 VOP_SETATTR(vp, &vattr, cred, td);
1651 p->p_acflag |= ACORE;
1652 VOP_UNLOCK(vp, 0, td);
1654 error = p->p_sysent->sv_coredump ?
1655 p->p_sysent->sv_coredump(p, vp, limit) :
1659 lf.l_type = F_UNLCK;
1660 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1662 error1 = vn_close(vp, FWRITE, td);
1669 * Nonexistent system call-- signal process (may want to handle it).
1670 * Flag error in case process won't see signal immediately (blocked or ignored).
1672 #ifndef _SYS_SYSPROTO_H_
1679 nosys(struct nosys_args *args)
1681 psignal(curproc, SIGSYS);
1686 * Send a SIGIO or SIGURG signal to a process or process group using
1687 * stored credentials rather than those of the current process.
1690 pgsigio(struct sigio *sigio, int sig, int checkctty)
1695 if (sigio->sio_pgid > 0) {
1696 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1698 psignal(sigio->sio_proc, sig);
1699 } else if (sigio->sio_pgid < 0) {
1702 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1703 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1704 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1710 filt_sigattach(struct knote *kn)
1712 struct proc *p = curproc;
1714 kn->kn_ptr.p_proc = p;
1715 kn->kn_flags |= EV_CLEAR; /* automatically set */
1717 /* XXX lock the proc here while adding to the list? */
1718 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1724 filt_sigdetach(struct knote *kn)
1726 struct proc *p = kn->kn_ptr.p_proc;
1728 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1732 * signal knotes are shared with proc knotes, so we apply a mask to
1733 * the hint in order to differentiate them from process hints. This
1734 * could be avoided by using a signal-specific knote list, but probably
1735 * isn't worth the trouble.
1738 filt_signal(struct knote *kn, long hint)
1741 if (hint & NOTE_SIGNAL) {
1742 hint &= ~NOTE_SIGNAL;
1744 if (kn->kn_id == hint)
1747 return (kn->kn_data != 0);