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.2 2003/06/17 04:28:41 dillon Exp $
43 #include "opt_compat.h"
44 #include "opt_ktrace.h"
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/sysproto.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/event.h>
55 #include <sys/pioctl.h>
56 #include <sys/systm.h>
58 #include <sys/fcntl.h>
60 #include <sys/ktrace.h>
61 #include <sys/syslog.h>
63 #include <sys/sysent.h>
64 #include <sys/sysctl.h>
65 #include <sys/malloc.h>
66 #include <sys/unistd.h>
69 #include <machine/ipl.h>
70 #include <machine/cpu.h>
71 #include <machine/smp.h>
73 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
75 static int coredump __P((struct proc *));
76 static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act,
77 struct sigaction *oact, int old));
78 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
79 sigset_t *oset, int old));
80 static char *expand_name __P((const char *, uid_t, pid_t));
81 static int killpg1 __P((struct proc *cp, int sig, int pgid, int all));
82 static int sig_ffs __P((sigset_t *set));
83 static int sigprop __P((int sig));
84 static void stop __P((struct proc *));
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);
90 struct filterops sig_filtops =
91 { 0, filt_sigattach, filt_sigdetach, filt_signal };
93 static int kern_logsigexit = 1;
94 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
96 "Log processes quitting on abnormal signals to syslog(3)");
99 * Can process p, with pcred pc, send the signal sig to process q?
101 #define CANSIGNAL(p, q, sig) \
102 (!p_trespass(p, q) || \
103 ((sig) == SIGCONT && (q)->p_session == (p)->p_session))
106 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
108 #define CANSIGIO(ruid, uc, q) \
109 ((uc)->cr_uid == 0 || \
110 (ruid) == (q)->p_cred->p_ruid || \
111 (uc)->cr_uid == (q)->p_cred->p_ruid || \
112 (ruid) == (q)->p_ucred->cr_uid || \
113 (uc)->cr_uid == (q)->p_ucred->cr_uid)
116 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
117 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
119 static int do_coredump = 1;
120 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121 &do_coredump, 0, "Enable/Disable coredumps");
124 * Signal properties and actions.
125 * The array below categorizes the signals and their default actions
126 * according to the following properties:
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 */
136 static int sigproptbl[NSIG] = {
137 SA_KILL, /* SIGHUP */
138 SA_KILL, /* SIGINT */
139 SA_KILL|SA_CORE, /* SIGQUIT */
140 SA_KILL|SA_CORE, /* SIGILL */
141 SA_KILL|SA_CORE, /* SIGTRAP */
142 SA_KILL|SA_CORE, /* SIGABRT */
143 SA_KILL|SA_CORE, /* SIGEMT */
144 SA_KILL|SA_CORE, /* SIGFPE */
145 SA_KILL, /* SIGKILL */
146 SA_KILL|SA_CORE, /* SIGBUS */
147 SA_KILL|SA_CORE, /* SIGSEGV */
148 SA_KILL|SA_CORE, /* SIGSYS */
149 SA_KILL, /* SIGPIPE */
150 SA_KILL, /* SIGALRM */
151 SA_KILL, /* SIGTERM */
152 SA_IGNORE, /* SIGURG */
153 SA_STOP, /* SIGSTOP */
154 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
155 SA_IGNORE|SA_CONT, /* SIGCONT */
156 SA_IGNORE, /* SIGCHLD */
157 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
158 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
159 SA_IGNORE, /* SIGIO */
160 SA_KILL, /* SIGXCPU */
161 SA_KILL, /* SIGXFSZ */
162 SA_KILL, /* SIGVTALRM */
163 SA_KILL, /* SIGPROF */
164 SA_IGNORE, /* SIGWINCH */
165 SA_IGNORE, /* SIGINFO */
166 SA_KILL, /* SIGUSR1 */
167 SA_KILL, /* SIGUSR2 */
174 if (sig > 0 && sig < NSIG)
175 return (sigproptbl[_SIG_IDX(sig)]);
180 sig_ffs(sigset_t *set)
184 for (i = 0; i < _SIG_WORDS; i++)
186 return (ffs(set->__bits[i]) + (i * 32));
196 do_sigaction(p, sig, act, oact, old)
199 struct sigaction *act, *oact;
202 register struct sigacts *ps = p->p_sigacts;
204 if (sig <= 0 || sig > _SIG_MAXSIG)
208 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
209 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
211 if (SIGISMEMBER(ps->ps_sigonstack, sig))
212 oact->sa_flags |= SA_ONSTACK;
213 if (!SIGISMEMBER(ps->ps_sigintr, sig))
214 oact->sa_flags |= SA_RESTART;
215 if (SIGISMEMBER(ps->ps_sigreset, sig))
216 oact->sa_flags |= SA_RESETHAND;
217 if (SIGISMEMBER(ps->ps_signodefer, sig))
218 oact->sa_flags |= SA_NODEFER;
219 if (SIGISMEMBER(ps->ps_siginfo, sig))
220 oact->sa_flags |= SA_SIGINFO;
221 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
222 oact->sa_flags |= SA_NOCLDSTOP;
223 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
224 oact->sa_flags |= SA_NOCLDWAIT;
227 if ((sig == SIGKILL || sig == SIGSTOP) &&
228 act->sa_handler != SIG_DFL)
232 * Change setting atomically.
236 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
237 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
238 if (act->sa_flags & SA_SIGINFO) {
239 ps->ps_sigact[_SIG_IDX(sig)] =
240 (__sighandler_t *)act->sa_sigaction;
241 SIGADDSET(ps->ps_siginfo, sig);
243 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
244 SIGDELSET(ps->ps_siginfo, sig);
246 if (!(act->sa_flags & SA_RESTART))
247 SIGADDSET(ps->ps_sigintr, sig);
249 SIGDELSET(ps->ps_sigintr, sig);
250 if (act->sa_flags & SA_ONSTACK)
251 SIGADDSET(ps->ps_sigonstack, sig);
253 SIGDELSET(ps->ps_sigonstack, sig);
254 if (act->sa_flags & SA_RESETHAND)
255 SIGADDSET(ps->ps_sigreset, sig);
257 SIGDELSET(ps->ps_sigreset, sig);
258 if (act->sa_flags & SA_NODEFER)
259 SIGADDSET(ps->ps_signodefer, sig);
261 SIGDELSET(ps->ps_signodefer, sig);
263 if (act->sa_flags & SA_USERTRAMP)
264 SIGADDSET(ps->ps_usertramp, sig);
266 SIGDELSET(ps->ps_usertramp, seg);
268 if (sig == SIGCHLD) {
269 if (act->sa_flags & SA_NOCLDSTOP)
270 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
272 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
273 if (act->sa_flags & SA_NOCLDWAIT) {
275 * Paranoia: since SA_NOCLDWAIT is implemented
276 * by reparenting the dying child to PID 1 (and
277 * trust it to reap the zombie), PID 1 itself
278 * is forbidden to set SA_NOCLDWAIT.
281 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
283 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
285 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
288 * Set bit in p_sigignore for signals that are set to SIG_IGN,
289 * and for signals set to SIG_DFL where the default is to
290 * ignore. However, don't put SIGCONT in p_sigignore, as we
291 * have to restart the process.
293 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
294 (sigprop(sig) & SA_IGNORE &&
295 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
296 /* never to be seen again */
297 SIGDELSET(p->p_siglist, sig);
299 /* easier in psignal */
300 SIGADDSET(p->p_sigignore, sig);
301 SIGDELSET(p->p_sigcatch, sig);
303 SIGDELSET(p->p_sigignore, sig);
304 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
305 SIGDELSET(p->p_sigcatch, sig);
307 SIGADDSET(p->p_sigcatch, sig);
309 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
310 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
311 SIGDELSET(ps->ps_osigset, sig);
313 SIGADDSET(ps->ps_osigset, sig);
320 #ifndef _SYS_SYSPROTO_H_
321 struct sigaction_args {
323 struct sigaction *act;
324 struct sigaction *oact;
331 register struct sigaction_args *uap;
333 struct sigaction act, oact;
334 register struct sigaction *actp, *oactp;
337 actp = (uap->act != NULL) ? &act : NULL;
338 oactp = (uap->oact != NULL) ? &oact : NULL;
340 error = copyin(uap->act, actp, sizeof(act));
344 error = do_sigaction(p, uap->sig, actp, oactp, 0);
345 if (oactp && !error) {
346 error = copyout(oactp, uap->oact, sizeof(oact));
351 #ifndef _SYS_SYSPROTO_H_
352 struct osigaction_args {
354 struct osigaction *nsa;
355 struct osigaction *osa;
362 register struct osigaction_args *uap;
364 struct osigaction sa;
365 struct sigaction nsa, osa;
366 register struct sigaction *nsap, *osap;
369 if (uap->signum <= 0 || uap->signum >= ONSIG)
371 nsap = (uap->nsa != NULL) ? &nsa : NULL;
372 osap = (uap->osa != NULL) ? &osa : NULL;
374 error = copyin(uap->nsa, &sa, sizeof(sa));
377 nsap->sa_handler = sa.sa_handler;
378 nsap->sa_flags = sa.sa_flags;
379 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
381 error = do_sigaction(p, uap->signum, nsap, osap, 1);
382 if (osap && !error) {
383 sa.sa_handler = osap->sa_handler;
384 sa.sa_flags = osap->sa_flags;
385 SIG2OSIG(osap->sa_mask, sa.sa_mask);
386 error = copyout(&sa, uap->osa, sizeof(sa));
392 * Initialize signal state for process 0;
393 * set to ignore signals that are ignored by default.
401 for (i = 1; i <= NSIG; i++)
402 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
403 SIGADDSET(p->p_sigignore, i);
407 * Reset signals for an exec of the specified process.
411 register struct proc *p;
413 register struct sigacts *ps = p->p_sigacts;
417 * Reset caught signals. Held signals remain held
418 * through p_sigmask (unless they were caught,
419 * and are now ignored by default).
421 while (SIGNOTEMPTY(p->p_sigcatch)) {
422 sig = sig_ffs(&p->p_sigcatch);
423 SIGDELSET(p->p_sigcatch, sig);
424 if (sigprop(sig) & SA_IGNORE) {
426 SIGADDSET(p->p_sigignore, sig);
427 SIGDELSET(p->p_siglist, sig);
429 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
432 * Reset stack state to the user stack.
433 * Clear set of signals caught on the signal stack.
435 p->p_sigstk.ss_flags = SS_DISABLE;
436 p->p_sigstk.ss_size = 0;
437 p->p_sigstk.ss_sp = 0;
438 p->p_flag &= ~P_ALTSTACK;
440 * Reset no zombies if child dies flag as Solaris does.
442 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
446 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
448 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
449 * p == curproc. Also remember that in order to remain MP SAFE
450 * no spl*() calls may be made.
453 do_sigprocmask(p, how, set, oset, old)
456 sigset_t *set, *oset;
462 *oset = p->p_sigmask;
469 SIGSETOR(p->p_sigmask, *set);
472 SIGSETNAND(p->p_sigmask, *set);
477 SIGSETLO(p->p_sigmask, *set);
490 * sigprocmask() - MP SAFE
493 #ifndef _SYS_SYSPROTO_H_
494 struct sigprocmask_args {
502 register struct proc *p;
503 struct sigprocmask_args *uap;
506 sigset_t *setp, *osetp;
509 setp = (uap->set != NULL) ? &set : NULL;
510 osetp = (uap->oset != NULL) ? &oset : NULL;
512 error = copyin(uap->set, setp, sizeof(set));
516 error = do_sigprocmask(p, uap->how, setp, osetp, 0);
517 if (osetp && !error) {
518 error = copyout(osetp, uap->oset, sizeof(oset));
524 * osigprocmask() - MP SAFE
527 #ifndef _SYS_SYSPROTO_H_
528 struct osigprocmask_args {
535 register struct proc *p;
536 struct osigprocmask_args *uap;
541 OSIG2SIG(uap->mask, set);
542 error = do_sigprocmask(p, uap->how, &set, &oset, 1);
543 SIG2OSIG(oset, p->p_retval[0]);
547 #ifndef _SYS_SYSPROTO_H_
548 struct sigpending_args {
556 struct sigpending_args *uap;
559 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
562 #ifndef _SYS_SYSPROTO_H_
563 struct osigpending_args {
571 struct osigpending_args *uap;
574 SIG2OSIG(p->p_siglist, p->p_retval[0]);
578 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
580 * Generalized interface signal handler, 4.3-compatible.
582 #ifndef _SYS_SYSPROTO_H_
583 struct osigvec_args {
593 register struct osigvec_args *uap;
596 struct sigaction nsa, osa;
597 register struct sigaction *nsap, *osap;
600 if (uap->signum <= 0 || uap->signum >= ONSIG)
602 nsap = (uap->nsv != NULL) ? &nsa : NULL;
603 osap = (uap->osv != NULL) ? &osa : NULL;
605 error = copyin(uap->nsv, &vec, sizeof(vec));
608 nsap->sa_handler = vec.sv_handler;
609 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
610 nsap->sa_flags = vec.sv_flags;
611 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
613 nsap->sa_flags |= SA_USERTRAMP;
616 error = do_sigaction(p, uap->signum, nsap, osap, 1);
617 if (osap && !error) {
618 vec.sv_handler = osap->sa_handler;
619 SIG2OSIG(osap->sa_mask, vec.sv_mask);
620 vec.sv_flags = osap->sa_flags;
621 vec.sv_flags &= ~SA_NOCLDWAIT;
622 vec.sv_flags ^= SA_RESTART;
624 vec.sv_flags &= ~SA_NOCLDSTOP;
626 error = copyout(&vec, uap->osv, sizeof(vec));
631 #ifndef _SYS_SYSPROTO_H_
632 struct osigblock_args {
638 register struct proc *p;
639 struct osigblock_args *uap;
643 OSIG2SIG(uap->mask, set);
646 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
647 SIGSETOR(p->p_sigmask, set);
652 #ifndef _SYS_SYSPROTO_H_
653 struct osigsetmask_args {
660 struct osigsetmask_args *uap;
664 OSIG2SIG(uap->mask, set);
667 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
668 SIGSETLO(p->p_sigmask, set);
672 #endif /* COMPAT_43 || COMPAT_SUNOS */
675 * Suspend process until signal, providing mask to be set
676 * in the meantime. Note nonstandard calling convention:
677 * libc stub passes mask, not pointer, to save a copyin.
679 #ifndef _SYS_SYSPROTO_H_
680 struct sigsuspend_args {
681 const sigset_t *sigmask;
687 register struct proc *p;
688 struct sigsuspend_args *uap;
691 register struct sigacts *ps = p->p_sigacts;
694 error = copyin(uap->sigmask, &mask, sizeof(mask));
699 * When returning from sigsuspend, we want
700 * the old mask to be restored after the
701 * signal handler has finished. Thus, we
702 * save it here and mark the sigacts structure
705 p->p_oldsigmask = p->p_sigmask;
706 p->p_flag |= P_OLDMASK;
710 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
712 /* always return EINTR rather than ERESTART... */
716 #ifndef _SYS_SYSPROTO_H_
717 struct osigsuspend_args {
724 register struct proc *p;
725 struct osigsuspend_args *uap;
728 register struct sigacts *ps = p->p_sigacts;
730 p->p_oldsigmask = p->p_sigmask;
731 p->p_flag |= P_OLDMASK;
732 OSIG2SIG(uap->mask, mask);
734 SIGSETLO(p->p_sigmask, mask);
735 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
737 /* always return EINTR rather than ERESTART... */
741 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
742 #ifndef _SYS_SYSPROTO_H_
743 struct osigstack_args {
744 struct sigstack *nss;
745 struct sigstack *oss;
752 register struct osigstack_args *uap;
757 ss.ss_sp = p->p_sigstk.ss_sp;
758 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
759 if (uap->oss && (error = copyout(&ss, uap->oss,
760 sizeof(struct sigstack))))
762 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
763 p->p_sigstk.ss_sp = ss.ss_sp;
764 p->p_sigstk.ss_size = 0;
765 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
766 p->p_flag |= P_ALTSTACK;
770 #endif /* COMPAT_43 || COMPAT_SUNOS */
772 #ifndef _SYS_SYSPROTO_H_
773 struct sigaltstack_args {
782 register struct sigaltstack_args *uap;
787 if ((p->p_flag & P_ALTSTACK) == 0)
788 p->p_sigstk.ss_flags |= SS_DISABLE;
789 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
794 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
796 if (ss.ss_flags & SS_DISABLE) {
797 if (p->p_sigstk.ss_flags & SS_ONSTACK)
799 p->p_flag &= ~P_ALTSTACK;
800 p->p_sigstk.ss_flags = ss.ss_flags;
803 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
805 p->p_flag |= P_ALTSTACK;
811 * Common code for kill process group/broadcast kill.
812 * cp is calling process.
815 killpg1(cp, sig, pgid, all)
816 register struct proc *cp;
819 register struct proc *p;
827 LIST_FOREACH(p, &allproc, p_list) {
828 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
829 p == cp || !CANSIGNAL(cp, p, sig))
838 * zero pgid means send to my process group.
846 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
847 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
848 p->p_stat == SZOMB ||
849 !CANSIGNAL(cp, p, sig))
856 return (nfound ? 0 : ESRCH);
859 #ifndef _SYS_SYSPROTO_H_
868 register struct proc *cp;
869 register struct kill_args *uap;
871 register struct proc *p;
873 if ((u_int)uap->signum > _SIG_MAXSIG)
876 /* kill single process */
877 if ((p = pfind(uap->pid)) == NULL)
879 if (!CANSIGNAL(cp, p, uap->signum))
882 psignal(p, uap->signum);
886 case -1: /* broadcast signal */
887 return (killpg1(cp, uap->signum, 0, 1));
888 case 0: /* signal own process group */
889 return (killpg1(cp, uap->signum, 0, 0));
890 default: /* negative explicit process group */
891 return (killpg1(cp, uap->signum, -uap->pid, 0));
896 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
897 #ifndef _SYS_SYSPROTO_H_
898 struct okillpg_args {
907 register struct okillpg_args *uap;
910 if ((u_int)uap->signum > _SIG_MAXSIG)
912 return (killpg1(p, uap->signum, uap->pgid, 0));
914 #endif /* COMPAT_43 || COMPAT_SUNOS */
917 * Send a signal to a process group.
925 if (pgid && (pgrp = pgfind(pgid)))
926 pgsignal(pgrp, sig, 0);
930 * Send a signal to a process group. If checktty is 1,
931 * limit to members which have a controlling terminal.
934 pgsignal(pgrp, sig, checkctty)
938 register struct proc *p;
941 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
942 if (checkctty == 0 || p->p_flag & P_CONTROLT)
947 * Send a signal caused by a trap to the current process.
948 * If it will be caught immediately, deliver it with correct code.
949 * Otherwise, post it normally.
952 trapsignal(p, sig, code)
957 register struct sigacts *ps = p->p_sigacts;
959 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
960 !SIGISMEMBER(p->p_sigmask, sig)) {
961 p->p_stats->p_ru.ru_nsignals++;
963 if (KTRPOINT(p, KTR_PSIG))
964 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
965 &p->p_sigmask, code);
967 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
968 &p->p_sigmask, code);
969 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
970 if (!SIGISMEMBER(ps->ps_signodefer, sig))
971 SIGADDSET(p->p_sigmask, sig);
972 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
974 * See do_sigaction() for origin of this code.
976 SIGDELSET(p->p_sigcatch, sig);
977 if (sig != SIGCONT &&
978 sigprop(sig) & SA_IGNORE)
979 SIGADDSET(p->p_sigignore, sig);
980 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
983 p->p_code = code; /* XXX for core dump/debugger */
984 p->p_sig = sig; /* XXX to verify code */
990 * Send the signal to the process. If the signal has an action, the action
991 * is usually performed by the target process rather than the caller; we add
992 * the signal to the set of pending signals for the process.
995 * o When a stop signal is sent to a sleeping process that takes the
996 * default action, the process is stopped without awakening it.
997 * o SIGCONT restarts stopped processes (or puts them back to sleep)
998 * regardless of the signal action (eg, blocked or ignored).
1000 * Other ignored signals are discarded immediately.
1004 register struct proc *p;
1007 register int s, prop;
1008 register sig_t action;
1010 if (sig > _SIG_MAXSIG || sig <= 0) {
1011 printf("psignal: signal %d\n", sig);
1012 panic("psignal signal number");
1016 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1019 prop = sigprop(sig);
1022 * If proc is traced, always give parent a chance;
1023 * if signal event is tracked by procfs, give *that*
1024 * a chance, as well.
1026 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1030 * If the signal is being ignored,
1031 * then we forget about it immediately.
1032 * (Note: we don't set SIGCONT in p_sigignore,
1033 * and if it is set to SIG_IGN,
1034 * action will be SIG_DFL here.)
1036 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1038 if (SIGISMEMBER(p->p_sigmask, sig))
1040 else if (SIGISMEMBER(p->p_sigcatch, sig))
1046 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1047 (p->p_flag & P_TRACED) == 0)
1051 SIG_STOPSIGMASK(p->p_siglist);
1053 if (prop & SA_STOP) {
1055 * If sending a tty stop signal to a member of an orphaned
1056 * process group, discard the signal here if the action
1057 * is default; don't stop the process below if sleeping,
1058 * and don't clear any pending SIGCONT.
1060 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1063 SIG_CONTSIGMASK(p->p_siglist);
1065 SIGADDSET(p->p_siglist, sig);
1068 * Defer further processing for signals which are held,
1069 * except that stopped processes must be continued by SIGCONT.
1071 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1074 switch (p->p_stat) {
1078 * If process is sleeping uninterruptibly
1079 * we can't interrupt the sleep... the signal will
1080 * be noticed when the process returns through
1081 * trap() or syscall().
1083 if ((p->p_flag & P_SINTR) == 0)
1086 * Process is sleeping and traced... make it runnable
1087 * so it can discover the signal in issignal() and stop
1090 if (p->p_flag & P_TRACED)
1093 * If SIGCONT is default (or ignored) and process is
1094 * asleep, we are finished; the process should not
1097 if ((prop & SA_CONT) && action == SIG_DFL) {
1098 SIGDELSET(p->p_siglist, sig);
1102 * When a sleeping process receives a stop
1103 * signal, process immediately if possible.
1104 * All other (caught or default) signals
1105 * cause the process to run.
1107 if (prop & SA_STOP) {
1108 if (action != SIG_DFL)
1111 * If a child holding parent blocked,
1112 * stopping could cause deadlock.
1114 if (p->p_flag & P_PPWAIT)
1116 SIGDELSET(p->p_siglist, sig);
1118 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1119 psignal(p->p_pptr, SIGCHLD);
1128 * If traced process is already stopped,
1129 * then no further action is necessary.
1131 if (p->p_flag & P_TRACED)
1135 * Kill signal always sets processes running.
1140 if (prop & SA_CONT) {
1142 * If SIGCONT is default (or ignored), we continue the
1143 * process but don't leave the signal in p_siglist, as
1144 * it has no further action. If SIGCONT is held, we
1145 * continue the process and leave the signal in
1146 * p_siglist. If the process catches SIGCONT, let it
1147 * handle the signal itself. If it isn't waiting on
1148 * an event, then it goes back to run state.
1149 * Otherwise, process goes back to sleep state.
1151 if (action == SIG_DFL)
1152 SIGDELSET(p->p_siglist, sig);
1153 if (action == SIG_CATCH)
1155 if (p->p_wchan == 0)
1161 if (prop & SA_STOP) {
1163 * Already stopped, don't need to stop again.
1164 * (If we did the shell could get confused.)
1166 SIGDELSET(p->p_siglist, sig);
1171 * If process is sleeping interruptibly, then simulate a
1172 * wakeup so that when it is continued, it will be made
1173 * runnable and can look at the signal. But don't make
1174 * the process runnable, leave it stopped.
1176 if (p->p_wchan && p->p_flag & P_SINTR)
1182 * SRUN, SIDL, SZOMB do nothing with the signal,
1183 * other than kicking ourselves if we are running.
1184 * It will either never be noticed, or noticed very soon.
1189 else if (p->p_stat == SRUN)
1198 * Raise priority to at least PUSER.
1200 if (p->p_priority > PUSER)
1201 p->p_priority = PUSER;
1209 * If the current process has received a signal (should be caught or cause
1210 * termination, should interrupt current syscall), return the signal number.
1211 * Stop signals with default action are processed immediately, then cleared;
1212 * they aren't returned. This is checked after each entry to the system for
1213 * a syscall or trap (though this can usually be done without calling issignal
1214 * by checking the pending signal masks in the CURSIG macro.) The normal call
1217 * while (sig = CURSIG(curproc))
1222 register struct proc *p;
1225 register int sig, prop;
1228 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1230 mask = p->p_siglist;
1231 SIGSETNAND(mask, p->p_sigmask);
1232 if (p->p_flag & P_PPWAIT)
1233 SIG_STOPSIGMASK(mask);
1234 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1236 sig = sig_ffs(&mask);
1238 STOPEVENT(p, S_SIG, sig);
1241 * We should see pending but ignored signals
1242 * only if P_TRACED was on when they were posted.
1244 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1245 SIGDELSET(p->p_siglist, sig);
1248 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1250 * If traced, always stop, and stay
1251 * stopped until released by the parent.
1254 psignal(p->p_pptr, SIGCHLD);
1258 } while (!trace_req(p)
1259 && p->p_flag & P_TRACED);
1262 * If parent wants us to take the signal,
1263 * then it will leave it in p->p_xstat;
1264 * otherwise we just look for signals again.
1266 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1272 * Put the new signal into p_siglist. If the
1273 * signal is being masked, look for other signals.
1275 SIGADDSET(p->p_siglist, sig);
1276 if (SIGISMEMBER(p->p_sigmask, sig))
1280 * If the traced bit got turned off, go back up
1281 * to the top to rescan signals. This ensures
1282 * that p_sig* and ps_sigact are consistent.
1284 if ((p->p_flag & P_TRACED) == 0)
1288 prop = sigprop(sig);
1291 * Decide whether the signal should be returned.
1292 * Return the signal's number, or fall through
1293 * to clear it from the pending mask.
1295 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1299 * Don't take default actions on system processes.
1301 if (p->p_pid <= 1) {
1304 * Are you sure you want to ignore SIGSEGV
1307 printf("Process (pid %lu) got signal %d\n",
1308 (u_long)p->p_pid, sig);
1310 break; /* == ignore */
1313 * If there is a pending stop signal to process
1314 * with default action, stop here,
1315 * then clear the signal. However,
1316 * if process is member of an orphaned
1317 * process group, ignore tty stop signals.
1319 if (prop & SA_STOP) {
1320 if (p->p_flag & P_TRACED ||
1321 (p->p_pgrp->pg_jobc == 0 &&
1323 break; /* == ignore */
1326 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1327 psignal(p->p_pptr, SIGCHLD);
1330 } else if (prop & SA_IGNORE) {
1332 * Except for SIGCONT, shouldn't get here.
1333 * Default action is to ignore; drop it.
1335 break; /* == ignore */
1342 * Masking above should prevent us ever trying
1343 * to take action on an ignored signal other
1344 * than SIGCONT, unless process is traced.
1346 if ((prop & SA_CONT) == 0 &&
1347 (p->p_flag & P_TRACED) == 0)
1348 printf("issignal\n");
1349 break; /* == ignore */
1353 * This signal has an action, let
1354 * postsig() process it.
1358 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1364 * Put the argument process into the stopped state and notify the parent
1365 * via wakeup. Signals are handled elsewhere. The process must not be
1370 register struct proc *p;
1374 p->p_flag &= ~P_WAITED;
1375 wakeup((caddr_t)p->p_pptr);
1379 * Take the action for the specified signal
1380 * from the current set of pending signals.
1386 register struct proc *p = curproc;
1387 struct sigacts *ps = p->p_sigacts;
1389 sigset_t returnmask;
1392 KASSERT(sig != 0, ("postsig"));
1394 SIGDELSET(p->p_siglist, sig);
1395 action = ps->ps_sigact[_SIG_IDX(sig)];
1397 if (KTRPOINT(p, KTR_PSIG))
1398 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1399 &p->p_oldsigmask : &p->p_sigmask, 0);
1401 STOPEVENT(p, S_SIG, sig);
1403 if (action == SIG_DFL) {
1405 * Default action, where the default is to kill
1406 * the process. (Other cases were ignored above.)
1412 * If we get here, the signal must be caught.
1414 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1415 ("postsig action"));
1417 * Set the new mask value and also defer further
1418 * occurrences of this signal.
1420 * Special case: user has done a sigsuspend. Here the
1421 * current mask is not of interest, but rather the
1422 * mask from before the sigsuspend is what we want
1423 * restored after the signal processing is completed.
1426 if (p->p_flag & P_OLDMASK) {
1427 returnmask = p->p_oldsigmask;
1428 p->p_flag &= ~P_OLDMASK;
1430 returnmask = p->p_sigmask;
1432 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1433 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1434 SIGADDSET(p->p_sigmask, sig);
1436 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1438 * See do_sigaction() for origin of this code.
1440 SIGDELSET(p->p_sigcatch, sig);
1441 if (sig != SIGCONT &&
1442 sigprop(sig) & SA_IGNORE)
1443 SIGADDSET(p->p_sigignore, sig);
1444 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1447 p->p_stats->p_ru.ru_nsignals++;
1448 if (p->p_sig != sig) {
1455 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1460 * Kill the current process for stated reason.
1467 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1468 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1469 psignal(p, SIGKILL);
1473 * Force the current process to exit with the specified signal, dumping core
1474 * if appropriate. We bypass the normal tests for masked and caught signals,
1475 * allowing unrecoverable failures to terminate the process without changing
1476 * signal state. Mark the accounting record with the signal termination.
1477 * If dumping core, save the signal number for the debugger. Calls exit and
1482 register struct proc *p;
1486 p->p_acflag |= AXSIG;
1487 if (sigprop(sig) & SA_CORE) {
1490 * Log signals which would cause core dumps
1491 * (Log as LOG_INFO to appease those who don't want
1493 * XXX : Todo, as well as euid, write out ruid too
1495 if (coredump(p) == 0)
1497 if (kern_logsigexit)
1499 "pid %d (%s), uid %d: exited on signal %d%s\n",
1500 p->p_pid, p->p_comm,
1501 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1503 sig & WCOREFLAG ? " (core dumped)" : "");
1505 exit1(p, W_EXITCODE(0, sig));
1509 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1510 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1511 sizeof(corefilename), "process corefile name format string");
1514 * expand_name(name, uid, pid)
1515 * Expand the name described in corefilename, using name, uid, and pid.
1516 * corefilename is a printf-like string, with three format specifiers:
1517 * %N name of process ("name")
1518 * %P process id (pid)
1520 * For example, "%N.core" is the default; they can be disabled completely
1521 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1522 * This is controlled by the sysctl variable kern.corefile (see above).
1526 expand_name(name, uid, pid)
1527 const char *name; uid_t uid; pid_t pid; {
1529 char buf[11]; /* Buffer for pid/uid -- max 4B */
1531 char *format = corefilename;
1534 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1537 namelen = strlen(name);
1538 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1540 switch (format[i]) {
1541 case '%': /* Format character */
1543 switch (format[i]) {
1547 case 'N': /* process name */
1548 if ((n + namelen) > MAXPATHLEN) {
1549 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1550 pid, name, uid, temp, name);
1554 memcpy(temp+n, name, namelen);
1557 case 'P': /* process id */
1558 l = sprintf(buf, "%u", pid);
1559 if ((n + l) > MAXPATHLEN) {
1560 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1561 pid, name, uid, temp, name);
1565 memcpy(temp+n, buf, l);
1568 case 'U': /* user id */
1569 l = sprintf(buf, "%u", uid);
1570 if ((n + l) > MAXPATHLEN) {
1571 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1572 pid, name, uid, temp, name);
1576 memcpy(temp+n, buf, l);
1580 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1584 temp[n++] = format[i];
1592 * Dump a process' core. The main routine does some
1593 * policy checking, and creates the name of the coredump;
1594 * then it passes on a vnode and a size limit to the process-specific
1595 * coredump routine if there is one; if there _is not_ one, it returns
1596 * ENOSYS; otherwise it returns the error from the process-specific routine.
1601 register struct proc *p;
1603 register struct vnode *vp;
1604 register struct ucred *cred = p->p_ucred;
1606 struct nameidata nd;
1609 char *name; /* name of corefile */
1612 STOPEVENT(p, S_CORE, 0);
1614 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1618 * Note that the bulk of limit checking is done after
1619 * the corefile is created. The exception is if the limit
1620 * for corefiles is 0, in which case we don't bother
1621 * creating the corefile at all. This layout means that
1622 * a corefile is truncated instead of not being created,
1623 * if it is larger than the limit.
1625 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1629 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1632 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1633 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1637 NDFREE(&nd, NDF_ONLY_PNBUF);
1640 VOP_UNLOCK(vp, 0, p);
1641 lf.l_whence = SEEK_SET;
1644 lf.l_type = F_WRLCK;
1645 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1649 /* Don't dump to non-regular files or files with links. */
1650 if (vp->v_type != VREG ||
1651 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1657 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1659 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1660 VOP_SETATTR(vp, &vattr, cred, p);
1661 p->p_acflag |= ACORE;
1662 VOP_UNLOCK(vp, 0, p);
1664 error = p->p_sysent->sv_coredump ?
1665 p->p_sysent->sv_coredump(p, vp, limit) :
1669 lf.l_type = F_UNLCK;
1670 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1672 error1 = vn_close(vp, FWRITE, cred, p);
1679 * Nonexistent system call-- signal process (may want to handle it).
1680 * Flag error in case process won't see signal immediately (blocked or ignored).
1682 #ifndef _SYS_SYSPROTO_H_
1691 struct nosys_args *args;
1699 * Send a SIGIO or SIGURG signal to a process or process group using
1700 * stored credentials rather than those of the current process.
1703 pgsigio(sigio, sig, checkctty)
1704 struct sigio *sigio;
1710 if (sigio->sio_pgid > 0) {
1711 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1713 psignal(sigio->sio_proc, sig);
1714 } else if (sigio->sio_pgid < 0) {
1717 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1718 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1719 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1725 filt_sigattach(struct knote *kn)
1727 struct proc *p = curproc;
1729 kn->kn_ptr.p_proc = p;
1730 kn->kn_flags |= EV_CLEAR; /* automatically set */
1732 /* XXX lock the proc here while adding to the list? */
1733 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1739 filt_sigdetach(struct knote *kn)
1741 struct proc *p = kn->kn_ptr.p_proc;
1743 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1747 * signal knotes are shared with proc knotes, so we apply a mask to
1748 * the hint in order to differentiate them from process hints. This
1749 * could be avoided by using a signal-specific knote list, but probably
1750 * isn't worth the trouble.
1753 filt_signal(struct knote *kn, long hint)
1756 if (hint & NOTE_SIGNAL) {
1757 hint &= ~NOTE_SIGNAL;
1759 if (kn->kn_id == hint)
1762 return (kn->kn_data != 0);