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 $
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/sysproto.h>
48 #include <sys/signalvar.h>
49 #include <sys/resourcevar.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/event.h>
54 #include <sys/pioctl.h>
55 #include <sys/systm.h>
57 #include <sys/fcntl.h>
59 #include <sys/ktrace.h>
60 #include <sys/syslog.h>
62 #include <sys/sysent.h>
63 #include <sys/sysctl.h>
64 #include <sys/malloc.h>
65 #include <sys/unistd.h>
68 #include <machine/ipl.h>
69 #include <machine/cpu.h>
70 #include <machine/smp.h>
72 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
74 static int coredump __P((struct proc *));
75 static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act,
76 struct sigaction *oact, int old));
77 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
78 sigset_t *oset, int old));
79 static char *expand_name __P((const char *, uid_t, pid_t));
80 static int killpg1 __P((struct proc *cp, int sig, int pgid, int all));
81 static int sig_ffs __P((sigset_t *set));
82 static int sigprop __P((int sig));
83 static void stop __P((struct proc *));
85 static int filt_sigattach(struct knote *kn);
86 static void filt_sigdetach(struct knote *kn);
87 static int filt_signal(struct knote *kn, long hint);
89 struct filterops sig_filtops =
90 { 0, filt_sigattach, filt_sigdetach, filt_signal };
92 static int kern_logsigexit = 1;
93 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
95 "Log processes quitting on abnormal signals to syslog(3)");
98 * Can process p, with pcred pc, send the signal sig to process q?
100 #define CANSIGNAL(p, q, sig) \
101 (!p_trespass(p, q) || \
102 ((sig) == SIGCONT && (q)->p_session == (p)->p_session))
105 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
107 #define CANSIGIO(ruid, uc, q) \
108 ((uc)->cr_uid == 0 || \
109 (ruid) == (q)->p_cred->p_ruid || \
110 (uc)->cr_uid == (q)->p_cred->p_ruid || \
111 (ruid) == (q)->p_ucred->cr_uid || \
112 (uc)->cr_uid == (q)->p_ucred->cr_uid)
115 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
116 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
118 static int do_coredump = 1;
119 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
120 &do_coredump, 0, "Enable/Disable coredumps");
123 * Signal properties and actions.
124 * The array below categorizes the signals and their default actions
125 * according to the following properties:
127 #define SA_KILL 0x01 /* terminates process by default */
128 #define SA_CORE 0x02 /* ditto and coredumps */
129 #define SA_STOP 0x04 /* suspend process */
130 #define SA_TTYSTOP 0x08 /* ditto, from tty */
131 #define SA_IGNORE 0x10 /* ignore by default */
132 #define SA_CONT 0x20 /* continue if suspended */
133 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
135 static int sigproptbl[NSIG] = {
136 SA_KILL, /* SIGHUP */
137 SA_KILL, /* SIGINT */
138 SA_KILL|SA_CORE, /* SIGQUIT */
139 SA_KILL|SA_CORE, /* SIGILL */
140 SA_KILL|SA_CORE, /* SIGTRAP */
141 SA_KILL|SA_CORE, /* SIGABRT */
142 SA_KILL|SA_CORE, /* SIGEMT */
143 SA_KILL|SA_CORE, /* SIGFPE */
144 SA_KILL, /* SIGKILL */
145 SA_KILL|SA_CORE, /* SIGBUS */
146 SA_KILL|SA_CORE, /* SIGSEGV */
147 SA_KILL|SA_CORE, /* SIGSYS */
148 SA_KILL, /* SIGPIPE */
149 SA_KILL, /* SIGALRM */
150 SA_KILL, /* SIGTERM */
151 SA_IGNORE, /* SIGURG */
152 SA_STOP, /* SIGSTOP */
153 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
154 SA_IGNORE|SA_CONT, /* SIGCONT */
155 SA_IGNORE, /* SIGCHLD */
156 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
157 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
158 SA_IGNORE, /* SIGIO */
159 SA_KILL, /* SIGXCPU */
160 SA_KILL, /* SIGXFSZ */
161 SA_KILL, /* SIGVTALRM */
162 SA_KILL, /* SIGPROF */
163 SA_IGNORE, /* SIGWINCH */
164 SA_IGNORE, /* SIGINFO */
165 SA_KILL, /* SIGUSR1 */
166 SA_KILL, /* SIGUSR2 */
173 if (sig > 0 && sig < NSIG)
174 return (sigproptbl[_SIG_IDX(sig)]);
179 sig_ffs(sigset_t *set)
183 for (i = 0; i < _SIG_WORDS; i++)
185 return (ffs(set->__bits[i]) + (i * 32));
195 do_sigaction(p, sig, act, oact, old)
198 struct sigaction *act, *oact;
201 register struct sigacts *ps = p->p_sigacts;
203 if (sig <= 0 || sig > _SIG_MAXSIG)
207 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
208 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
210 if (SIGISMEMBER(ps->ps_sigonstack, sig))
211 oact->sa_flags |= SA_ONSTACK;
212 if (!SIGISMEMBER(ps->ps_sigintr, sig))
213 oact->sa_flags |= SA_RESTART;
214 if (SIGISMEMBER(ps->ps_sigreset, sig))
215 oact->sa_flags |= SA_RESETHAND;
216 if (SIGISMEMBER(ps->ps_signodefer, sig))
217 oact->sa_flags |= SA_NODEFER;
218 if (SIGISMEMBER(ps->ps_siginfo, sig))
219 oact->sa_flags |= SA_SIGINFO;
220 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
221 oact->sa_flags |= SA_NOCLDSTOP;
222 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
223 oact->sa_flags |= SA_NOCLDWAIT;
226 if ((sig == SIGKILL || sig == SIGSTOP) &&
227 act->sa_handler != SIG_DFL)
231 * Change setting atomically.
235 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
236 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
237 if (act->sa_flags & SA_SIGINFO) {
238 ps->ps_sigact[_SIG_IDX(sig)] =
239 (__sighandler_t *)act->sa_sigaction;
240 SIGADDSET(ps->ps_siginfo, sig);
242 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
243 SIGDELSET(ps->ps_siginfo, sig);
245 if (!(act->sa_flags & SA_RESTART))
246 SIGADDSET(ps->ps_sigintr, sig);
248 SIGDELSET(ps->ps_sigintr, sig);
249 if (act->sa_flags & SA_ONSTACK)
250 SIGADDSET(ps->ps_sigonstack, sig);
252 SIGDELSET(ps->ps_sigonstack, sig);
253 if (act->sa_flags & SA_RESETHAND)
254 SIGADDSET(ps->ps_sigreset, sig);
256 SIGDELSET(ps->ps_sigreset, sig);
257 if (act->sa_flags & SA_NODEFER)
258 SIGADDSET(ps->ps_signodefer, sig);
260 SIGDELSET(ps->ps_signodefer, sig);
262 if (act->sa_flags & SA_USERTRAMP)
263 SIGADDSET(ps->ps_usertramp, sig);
265 SIGDELSET(ps->ps_usertramp, seg);
267 if (sig == SIGCHLD) {
268 if (act->sa_flags & SA_NOCLDSTOP)
269 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
271 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
272 if (act->sa_flags & SA_NOCLDWAIT) {
274 * Paranoia: since SA_NOCLDWAIT is implemented
275 * by reparenting the dying child to PID 1 (and
276 * trust it to reap the zombie), PID 1 itself
277 * is forbidden to set SA_NOCLDWAIT.
280 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
282 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
284 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
287 * Set bit in p_sigignore for signals that are set to SIG_IGN,
288 * and for signals set to SIG_DFL where the default is to
289 * ignore. However, don't put SIGCONT in p_sigignore, as we
290 * have to restart the process.
292 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
293 (sigprop(sig) & SA_IGNORE &&
294 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
295 /* never to be seen again */
296 SIGDELSET(p->p_siglist, sig);
298 /* easier in psignal */
299 SIGADDSET(p->p_sigignore, sig);
300 SIGDELSET(p->p_sigcatch, sig);
302 SIGDELSET(p->p_sigignore, sig);
303 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
304 SIGDELSET(p->p_sigcatch, sig);
306 SIGADDSET(p->p_sigcatch, sig);
308 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
309 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
310 SIGDELSET(ps->ps_osigset, sig);
312 SIGADDSET(ps->ps_osigset, sig);
319 #ifndef _SYS_SYSPROTO_H_
320 struct sigaction_args {
322 struct sigaction *act;
323 struct sigaction *oact;
330 register 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(p, 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;
361 register struct osigaction_args *uap;
363 struct osigaction sa;
364 struct sigaction nsa, osa;
365 register struct sigaction *nsap, *osap;
368 if (uap->signum <= 0 || uap->signum >= ONSIG)
370 nsap = (uap->nsa != NULL) ? &nsa : NULL;
371 osap = (uap->osa != NULL) ? &osa : NULL;
373 error = copyin(uap->nsa, &sa, sizeof(sa));
376 nsap->sa_handler = sa.sa_handler;
377 nsap->sa_flags = sa.sa_flags;
378 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
380 error = do_sigaction(p, uap->signum, nsap, osap, 1);
381 if (osap && !error) {
382 sa.sa_handler = osap->sa_handler;
383 sa.sa_flags = osap->sa_flags;
384 SIG2OSIG(osap->sa_mask, sa.sa_mask);
385 error = copyout(&sa, uap->osa, sizeof(sa));
391 * Initialize signal state for process 0;
392 * set to ignore signals that are ignored by default.
400 for (i = 1; i <= NSIG; i++)
401 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
402 SIGADDSET(p->p_sigignore, i);
406 * Reset signals for an exec of the specified process.
410 register struct proc *p;
412 register struct sigacts *ps = p->p_sigacts;
416 * Reset caught signals. Held signals remain held
417 * through p_sigmask (unless they were caught,
418 * and are now ignored by default).
420 while (SIGNOTEMPTY(p->p_sigcatch)) {
421 sig = sig_ffs(&p->p_sigcatch);
422 SIGDELSET(p->p_sigcatch, sig);
423 if (sigprop(sig) & SA_IGNORE) {
425 SIGADDSET(p->p_sigignore, sig);
426 SIGDELSET(p->p_siglist, sig);
428 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
431 * Reset stack state to the user stack.
432 * Clear set of signals caught on the signal stack.
434 p->p_sigstk.ss_flags = SS_DISABLE;
435 p->p_sigstk.ss_size = 0;
436 p->p_sigstk.ss_sp = 0;
437 p->p_flag &= ~P_ALTSTACK;
439 * Reset no zombies if child dies flag as Solaris does.
441 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
445 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
447 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
448 * p == curproc. Also remember that in order to remain MP SAFE
449 * no spl*() calls may be made.
452 do_sigprocmask(p, how, set, oset, old)
455 sigset_t *set, *oset;
461 *oset = p->p_sigmask;
468 SIGSETOR(p->p_sigmask, *set);
471 SIGSETNAND(p->p_sigmask, *set);
476 SIGSETLO(p->p_sigmask, *set);
489 * sigprocmask() - MP SAFE
492 #ifndef _SYS_SYSPROTO_H_
493 struct sigprocmask_args {
501 register struct proc *p;
502 struct sigprocmask_args *uap;
505 sigset_t *setp, *osetp;
508 setp = (uap->set != NULL) ? &set : NULL;
509 osetp = (uap->oset != NULL) ? &oset : NULL;
511 error = copyin(uap->set, setp, sizeof(set));
515 error = do_sigprocmask(p, uap->how, setp, osetp, 0);
516 if (osetp && !error) {
517 error = copyout(osetp, uap->oset, sizeof(oset));
523 * osigprocmask() - MP SAFE
526 #ifndef _SYS_SYSPROTO_H_
527 struct osigprocmask_args {
534 register struct proc *p;
535 struct osigprocmask_args *uap;
540 OSIG2SIG(uap->mask, set);
541 error = do_sigprocmask(p, uap->how, &set, &oset, 1);
542 SIG2OSIG(oset, p->p_retval[0]);
546 #ifndef _SYS_SYSPROTO_H_
547 struct sigpending_args {
555 struct sigpending_args *uap;
558 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
561 #ifndef _SYS_SYSPROTO_H_
562 struct osigpending_args {
570 struct osigpending_args *uap;
573 SIG2OSIG(p->p_siglist, p->p_retval[0]);
577 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
579 * Generalized interface signal handler, 4.3-compatible.
581 #ifndef _SYS_SYSPROTO_H_
582 struct osigvec_args {
592 register struct osigvec_args *uap;
595 struct sigaction nsa, osa;
596 register struct sigaction *nsap, *osap;
599 if (uap->signum <= 0 || uap->signum >= ONSIG)
601 nsap = (uap->nsv != NULL) ? &nsa : NULL;
602 osap = (uap->osv != NULL) ? &osa : NULL;
604 error = copyin(uap->nsv, &vec, sizeof(vec));
607 nsap->sa_handler = vec.sv_handler;
608 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
609 nsap->sa_flags = vec.sv_flags;
610 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
612 nsap->sa_flags |= SA_USERTRAMP;
615 error = do_sigaction(p, uap->signum, nsap, osap, 1);
616 if (osap && !error) {
617 vec.sv_handler = osap->sa_handler;
618 SIG2OSIG(osap->sa_mask, vec.sv_mask);
619 vec.sv_flags = osap->sa_flags;
620 vec.sv_flags &= ~SA_NOCLDWAIT;
621 vec.sv_flags ^= SA_RESTART;
623 vec.sv_flags &= ~SA_NOCLDSTOP;
625 error = copyout(&vec, uap->osv, sizeof(vec));
630 #ifndef _SYS_SYSPROTO_H_
631 struct osigblock_args {
637 register struct proc *p;
638 struct osigblock_args *uap;
642 OSIG2SIG(uap->mask, set);
645 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
646 SIGSETOR(p->p_sigmask, set);
651 #ifndef _SYS_SYSPROTO_H_
652 struct osigsetmask_args {
659 struct osigsetmask_args *uap;
663 OSIG2SIG(uap->mask, set);
666 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
667 SIGSETLO(p->p_sigmask, set);
671 #endif /* COMPAT_43 || COMPAT_SUNOS */
674 * Suspend process until signal, providing mask to be set
675 * in the meantime. Note nonstandard calling convention:
676 * libc stub passes mask, not pointer, to save a copyin.
678 #ifndef _SYS_SYSPROTO_H_
679 struct sigsuspend_args {
680 const sigset_t *sigmask;
686 register struct proc *p;
687 struct sigsuspend_args *uap;
690 register struct sigacts *ps = p->p_sigacts;
693 error = copyin(uap->sigmask, &mask, sizeof(mask));
698 * When returning from sigsuspend, we want
699 * the old mask to be restored after the
700 * signal handler has finished. Thus, we
701 * save it here and mark the sigacts structure
704 p->p_oldsigmask = p->p_sigmask;
705 p->p_flag |= P_OLDMASK;
709 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
711 /* always return EINTR rather than ERESTART... */
715 #ifndef _SYS_SYSPROTO_H_
716 struct osigsuspend_args {
723 register struct proc *p;
724 struct osigsuspend_args *uap;
727 register struct sigacts *ps = p->p_sigacts;
729 p->p_oldsigmask = p->p_sigmask;
730 p->p_flag |= P_OLDMASK;
731 OSIG2SIG(uap->mask, mask);
733 SIGSETLO(p->p_sigmask, mask);
734 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
736 /* always return EINTR rather than ERESTART... */
740 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
741 #ifndef _SYS_SYSPROTO_H_
742 struct osigstack_args {
743 struct sigstack *nss;
744 struct sigstack *oss;
751 register struct osigstack_args *uap;
756 ss.ss_sp = p->p_sigstk.ss_sp;
757 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
758 if (uap->oss && (error = copyout(&ss, uap->oss,
759 sizeof(struct sigstack))))
761 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
762 p->p_sigstk.ss_sp = ss.ss_sp;
763 p->p_sigstk.ss_size = 0;
764 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
765 p->p_flag |= P_ALTSTACK;
769 #endif /* COMPAT_43 || COMPAT_SUNOS */
771 #ifndef _SYS_SYSPROTO_H_
772 struct sigaltstack_args {
781 register struct sigaltstack_args *uap;
786 if ((p->p_flag & P_ALTSTACK) == 0)
787 p->p_sigstk.ss_flags |= SS_DISABLE;
788 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
793 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
795 if (ss.ss_flags & SS_DISABLE) {
796 if (p->p_sigstk.ss_flags & SS_ONSTACK)
798 p->p_flag &= ~P_ALTSTACK;
799 p->p_sigstk.ss_flags = ss.ss_flags;
802 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
804 p->p_flag |= P_ALTSTACK;
810 * Common code for kill process group/broadcast kill.
811 * cp is calling process.
814 killpg1(cp, sig, pgid, all)
815 register struct proc *cp;
818 register struct proc *p;
826 LIST_FOREACH(p, &allproc, p_list) {
827 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
828 p == cp || !CANSIGNAL(cp, p, sig))
837 * zero pgid means send to my process group.
845 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
846 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
847 p->p_stat == SZOMB ||
848 !CANSIGNAL(cp, p, sig))
855 return (nfound ? 0 : ESRCH);
858 #ifndef _SYS_SYSPROTO_H_
867 register struct proc *cp;
868 register struct kill_args *uap;
870 register struct proc *p;
872 if ((u_int)uap->signum > _SIG_MAXSIG)
875 /* kill single process */
876 if ((p = pfind(uap->pid)) == NULL)
878 if (!CANSIGNAL(cp, p, uap->signum))
881 psignal(p, uap->signum);
885 case -1: /* broadcast signal */
886 return (killpg1(cp, uap->signum, 0, 1));
887 case 0: /* signal own process group */
888 return (killpg1(cp, uap->signum, 0, 0));
889 default: /* negative explicit process group */
890 return (killpg1(cp, uap->signum, -uap->pid, 0));
895 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
896 #ifndef _SYS_SYSPROTO_H_
897 struct okillpg_args {
906 register struct okillpg_args *uap;
909 if ((u_int)uap->signum > _SIG_MAXSIG)
911 return (killpg1(p, uap->signum, uap->pgid, 0));
913 #endif /* COMPAT_43 || COMPAT_SUNOS */
916 * Send a signal to a process group.
924 if (pgid && (pgrp = pgfind(pgid)))
925 pgsignal(pgrp, sig, 0);
929 * Send a signal to a process group. If checktty is 1,
930 * limit to members which have a controlling terminal.
933 pgsignal(pgrp, sig, checkctty)
937 register struct proc *p;
940 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
941 if (checkctty == 0 || p->p_flag & P_CONTROLT)
946 * Send a signal caused by a trap to the current process.
947 * If it will be caught immediately, deliver it with correct code.
948 * Otherwise, post it normally.
951 trapsignal(p, sig, code)
956 register struct sigacts *ps = p->p_sigacts;
958 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
959 !SIGISMEMBER(p->p_sigmask, sig)) {
960 p->p_stats->p_ru.ru_nsignals++;
962 if (KTRPOINT(p, KTR_PSIG))
963 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
964 &p->p_sigmask, code);
966 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
967 &p->p_sigmask, code);
968 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
969 if (!SIGISMEMBER(ps->ps_signodefer, sig))
970 SIGADDSET(p->p_sigmask, sig);
971 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
973 * See do_sigaction() for origin of this code.
975 SIGDELSET(p->p_sigcatch, sig);
976 if (sig != SIGCONT &&
977 sigprop(sig) & SA_IGNORE)
978 SIGADDSET(p->p_sigignore, sig);
979 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
982 p->p_code = code; /* XXX for core dump/debugger */
983 p->p_sig = sig; /* XXX to verify code */
989 * Send the signal to the process. If the signal has an action, the action
990 * is usually performed by the target process rather than the caller; we add
991 * the signal to the set of pending signals for the process.
994 * o When a stop signal is sent to a sleeping process that takes the
995 * default action, the process is stopped without awakening it.
996 * o SIGCONT restarts stopped processes (or puts them back to sleep)
997 * regardless of the signal action (eg, blocked or ignored).
999 * Other ignored signals are discarded immediately.
1003 register struct proc *p;
1006 register int s, prop;
1007 register sig_t action;
1009 if (sig > _SIG_MAXSIG || sig <= 0) {
1010 printf("psignal: signal %d\n", sig);
1011 panic("psignal signal number");
1015 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1018 prop = sigprop(sig);
1021 * If proc is traced, always give parent a chance;
1022 * if signal event is tracked by procfs, give *that*
1023 * a chance, as well.
1025 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1029 * If the signal is being ignored,
1030 * then we forget about it immediately.
1031 * (Note: we don't set SIGCONT in p_sigignore,
1032 * and if it is set to SIG_IGN,
1033 * action will be SIG_DFL here.)
1035 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1037 if (SIGISMEMBER(p->p_sigmask, sig))
1039 else if (SIGISMEMBER(p->p_sigcatch, sig))
1045 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1046 (p->p_flag & P_TRACED) == 0)
1050 SIG_STOPSIGMASK(p->p_siglist);
1052 if (prop & SA_STOP) {
1054 * If sending a tty stop signal to a member of an orphaned
1055 * process group, discard the signal here if the action
1056 * is default; don't stop the process below if sleeping,
1057 * and don't clear any pending SIGCONT.
1059 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1062 SIG_CONTSIGMASK(p->p_siglist);
1064 SIGADDSET(p->p_siglist, sig);
1067 * Defer further processing for signals which are held,
1068 * except that stopped processes must be continued by SIGCONT.
1070 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1073 switch (p->p_stat) {
1077 * If process is sleeping uninterruptibly
1078 * we can't interrupt the sleep... the signal will
1079 * be noticed when the process returns through
1080 * trap() or syscall().
1082 if ((p->p_flag & P_SINTR) == 0)
1085 * Process is sleeping and traced... make it runnable
1086 * so it can discover the signal in issignal() and stop
1089 if (p->p_flag & P_TRACED)
1092 * If SIGCONT is default (or ignored) and process is
1093 * asleep, we are finished; the process should not
1096 if ((prop & SA_CONT) && action == SIG_DFL) {
1097 SIGDELSET(p->p_siglist, sig);
1101 * When a sleeping process receives a stop
1102 * signal, process immediately if possible.
1103 * All other (caught or default) signals
1104 * cause the process to run.
1106 if (prop & SA_STOP) {
1107 if (action != SIG_DFL)
1110 * If a child holding parent blocked,
1111 * stopping could cause deadlock.
1113 if (p->p_flag & P_PPWAIT)
1115 SIGDELSET(p->p_siglist, sig);
1117 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1118 psignal(p->p_pptr, SIGCHLD);
1127 * If traced process is already stopped,
1128 * then no further action is necessary.
1130 if (p->p_flag & P_TRACED)
1134 * Kill signal always sets processes running.
1139 if (prop & SA_CONT) {
1141 * If SIGCONT is default (or ignored), we continue the
1142 * process but don't leave the signal in p_siglist, as
1143 * it has no further action. If SIGCONT is held, we
1144 * continue the process and leave the signal in
1145 * p_siglist. If the process catches SIGCONT, let it
1146 * handle the signal itself. If it isn't waiting on
1147 * an event, then it goes back to run state.
1148 * Otherwise, process goes back to sleep state.
1150 if (action == SIG_DFL)
1151 SIGDELSET(p->p_siglist, sig);
1152 if (action == SIG_CATCH)
1154 if (p->p_wchan == 0)
1160 if (prop & SA_STOP) {
1162 * Already stopped, don't need to stop again.
1163 * (If we did the shell could get confused.)
1165 SIGDELSET(p->p_siglist, sig);
1170 * If process is sleeping interruptibly, then simulate a
1171 * wakeup so that when it is continued, it will be made
1172 * runnable and can look at the signal. But don't make
1173 * the process runnable, leave it stopped.
1175 if (p->p_wchan && p->p_flag & P_SINTR)
1181 * SRUN, SIDL, SZOMB do nothing with the signal,
1182 * other than kicking ourselves if we are running.
1183 * It will either never be noticed, or noticed very soon.
1188 else if (p->p_stat == SRUN)
1197 * Raise priority to at least PUSER.
1199 if (p->p_priority > PUSER)
1200 p->p_priority = PUSER;
1208 * If the current process has received a signal (should be caught or cause
1209 * termination, should interrupt current syscall), return the signal number.
1210 * Stop signals with default action are processed immediately, then cleared;
1211 * they aren't returned. This is checked after each entry to the system for
1212 * a syscall or trap (though this can usually be done without calling issignal
1213 * by checking the pending signal masks in the CURSIG macro.) The normal call
1216 * while (sig = CURSIG(curproc))
1221 register struct proc *p;
1224 register int sig, prop;
1227 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1229 mask = p->p_siglist;
1230 SIGSETNAND(mask, p->p_sigmask);
1231 if (p->p_flag & P_PPWAIT)
1232 SIG_STOPSIGMASK(mask);
1233 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1235 sig = sig_ffs(&mask);
1237 STOPEVENT(p, S_SIG, sig);
1240 * We should see pending but ignored signals
1241 * only if P_TRACED was on when they were posted.
1243 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1244 SIGDELSET(p->p_siglist, sig);
1247 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1249 * If traced, always stop, and stay
1250 * stopped until released by the parent.
1253 psignal(p->p_pptr, SIGCHLD);
1257 } while (!trace_req(p)
1258 && p->p_flag & P_TRACED);
1261 * If parent wants us to take the signal,
1262 * then it will leave it in p->p_xstat;
1263 * otherwise we just look for signals again.
1265 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1271 * Put the new signal into p_siglist. If the
1272 * signal is being masked, look for other signals.
1274 SIGADDSET(p->p_siglist, sig);
1275 if (SIGISMEMBER(p->p_sigmask, sig))
1279 * If the traced bit got turned off, go back up
1280 * to the top to rescan signals. This ensures
1281 * that p_sig* and ps_sigact are consistent.
1283 if ((p->p_flag & P_TRACED) == 0)
1287 prop = sigprop(sig);
1290 * Decide whether the signal should be returned.
1291 * Return the signal's number, or fall through
1292 * to clear it from the pending mask.
1294 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1298 * Don't take default actions on system processes.
1300 if (p->p_pid <= 1) {
1303 * Are you sure you want to ignore SIGSEGV
1306 printf("Process (pid %lu) got signal %d\n",
1307 (u_long)p->p_pid, sig);
1309 break; /* == ignore */
1312 * If there is a pending stop signal to process
1313 * with default action, stop here,
1314 * then clear the signal. However,
1315 * if process is member of an orphaned
1316 * process group, ignore tty stop signals.
1318 if (prop & SA_STOP) {
1319 if (p->p_flag & P_TRACED ||
1320 (p->p_pgrp->pg_jobc == 0 &&
1322 break; /* == ignore */
1325 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1326 psignal(p->p_pptr, SIGCHLD);
1329 } else if (prop & SA_IGNORE) {
1331 * Except for SIGCONT, shouldn't get here.
1332 * Default action is to ignore; drop it.
1334 break; /* == ignore */
1341 * Masking above should prevent us ever trying
1342 * to take action on an ignored signal other
1343 * than SIGCONT, unless process is traced.
1345 if ((prop & SA_CONT) == 0 &&
1346 (p->p_flag & P_TRACED) == 0)
1347 printf("issignal\n");
1348 break; /* == ignore */
1352 * This signal has an action, let
1353 * postsig() process it.
1357 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1363 * Put the argument process into the stopped state and notify the parent
1364 * via wakeup. Signals are handled elsewhere. The process must not be
1369 register struct proc *p;
1373 p->p_flag &= ~P_WAITED;
1374 wakeup((caddr_t)p->p_pptr);
1378 * Take the action for the specified signal
1379 * from the current set of pending signals.
1385 register struct proc *p = curproc;
1386 struct sigacts *ps = p->p_sigacts;
1388 sigset_t returnmask;
1391 KASSERT(sig != 0, ("postsig"));
1393 SIGDELSET(p->p_siglist, sig);
1394 action = ps->ps_sigact[_SIG_IDX(sig)];
1396 if (KTRPOINT(p, KTR_PSIG))
1397 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1398 &p->p_oldsigmask : &p->p_sigmask, 0);
1400 STOPEVENT(p, S_SIG, sig);
1402 if (action == SIG_DFL) {
1404 * Default action, where the default is to kill
1405 * the process. (Other cases were ignored above.)
1411 * If we get here, the signal must be caught.
1413 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1414 ("postsig action"));
1416 * Set the new mask value and also defer further
1417 * occurrences of this signal.
1419 * Special case: user has done a sigsuspend. Here the
1420 * current mask is not of interest, but rather the
1421 * mask from before the sigsuspend is what we want
1422 * restored after the signal processing is completed.
1425 if (p->p_flag & P_OLDMASK) {
1426 returnmask = p->p_oldsigmask;
1427 p->p_flag &= ~P_OLDMASK;
1429 returnmask = p->p_sigmask;
1431 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1432 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1433 SIGADDSET(p->p_sigmask, sig);
1435 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1437 * See do_sigaction() for origin of this code.
1439 SIGDELSET(p->p_sigcatch, sig);
1440 if (sig != SIGCONT &&
1441 sigprop(sig) & SA_IGNORE)
1442 SIGADDSET(p->p_sigignore, sig);
1443 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1446 p->p_stats->p_ru.ru_nsignals++;
1447 if (p->p_sig != sig) {
1454 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1459 * Kill the current process for stated reason.
1466 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1467 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1468 psignal(p, SIGKILL);
1472 * Force the current process to exit with the specified signal, dumping core
1473 * if appropriate. We bypass the normal tests for masked and caught signals,
1474 * allowing unrecoverable failures to terminate the process without changing
1475 * signal state. Mark the accounting record with the signal termination.
1476 * If dumping core, save the signal number for the debugger. Calls exit and
1481 register struct proc *p;
1485 p->p_acflag |= AXSIG;
1486 if (sigprop(sig) & SA_CORE) {
1489 * Log signals which would cause core dumps
1490 * (Log as LOG_INFO to appease those who don't want
1492 * XXX : Todo, as well as euid, write out ruid too
1494 if (coredump(p) == 0)
1496 if (kern_logsigexit)
1498 "pid %d (%s), uid %d: exited on signal %d%s\n",
1499 p->p_pid, p->p_comm,
1500 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1502 sig & WCOREFLAG ? " (core dumped)" : "");
1504 exit1(p, W_EXITCODE(0, sig));
1508 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1509 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1510 sizeof(corefilename), "process corefile name format string");
1513 * expand_name(name, uid, pid)
1514 * Expand the name described in corefilename, using name, uid, and pid.
1515 * corefilename is a printf-like string, with three format specifiers:
1516 * %N name of process ("name")
1517 * %P process id (pid)
1519 * For example, "%N.core" is the default; they can be disabled completely
1520 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1521 * This is controlled by the sysctl variable kern.corefile (see above).
1525 expand_name(name, uid, pid)
1526 const char *name; uid_t uid; pid_t pid; {
1528 char buf[11]; /* Buffer for pid/uid -- max 4B */
1530 char *format = corefilename;
1533 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1536 namelen = strlen(name);
1537 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1539 switch (format[i]) {
1540 case '%': /* Format character */
1542 switch (format[i]) {
1546 case 'N': /* process name */
1547 if ((n + namelen) > MAXPATHLEN) {
1548 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1549 pid, name, uid, temp, name);
1553 memcpy(temp+n, name, namelen);
1556 case 'P': /* process id */
1557 l = sprintf(buf, "%u", pid);
1558 if ((n + l) > MAXPATHLEN) {
1559 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1560 pid, name, uid, temp, name);
1564 memcpy(temp+n, buf, l);
1567 case 'U': /* user id */
1568 l = sprintf(buf, "%u", uid);
1569 if ((n + l) > MAXPATHLEN) {
1570 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1571 pid, name, uid, temp, name);
1575 memcpy(temp+n, buf, l);
1579 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1583 temp[n++] = format[i];
1591 * Dump a process' core. The main routine does some
1592 * policy checking, and creates the name of the coredump;
1593 * then it passes on a vnode and a size limit to the process-specific
1594 * coredump routine if there is one; if there _is not_ one, it returns
1595 * ENOSYS; otherwise it returns the error from the process-specific routine.
1600 register struct proc *p;
1602 register struct vnode *vp;
1603 register struct ucred *cred = p->p_ucred;
1605 struct nameidata nd;
1608 char *name; /* name of corefile */
1611 STOPEVENT(p, S_CORE, 0);
1613 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1617 * Note that the bulk of limit checking is done after
1618 * the corefile is created. The exception is if the limit
1619 * for corefiles is 0, in which case we don't bother
1620 * creating the corefile at all. This layout means that
1621 * a corefile is truncated instead of not being created,
1622 * if it is larger than the limit.
1624 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1628 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1631 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1632 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1636 NDFREE(&nd, NDF_ONLY_PNBUF);
1639 VOP_UNLOCK(vp, 0, p);
1640 lf.l_whence = SEEK_SET;
1643 lf.l_type = F_WRLCK;
1644 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1648 /* Don't dump to non-regular files or files with links. */
1649 if (vp->v_type != VREG ||
1650 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1656 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1658 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1659 VOP_SETATTR(vp, &vattr, cred, p);
1660 p->p_acflag |= ACORE;
1661 VOP_UNLOCK(vp, 0, p);
1663 error = p->p_sysent->sv_coredump ?
1664 p->p_sysent->sv_coredump(p, vp, limit) :
1668 lf.l_type = F_UNLCK;
1669 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1671 error1 = vn_close(vp, FWRITE, cred, p);
1678 * Nonexistent system call-- signal process (may want to handle it).
1679 * Flag error in case process won't see signal immediately (blocked or ignored).
1681 #ifndef _SYS_SYSPROTO_H_
1690 struct nosys_args *args;
1698 * Send a SIGIO or SIGURG signal to a process or process group using
1699 * stored credentials rather than those of the current process.
1702 pgsigio(sigio, sig, checkctty)
1703 struct sigio *sigio;
1709 if (sigio->sio_pgid > 0) {
1710 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1712 psignal(sigio->sio_proc, sig);
1713 } else if (sigio->sio_pgid < 0) {
1716 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1717 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1718 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1724 filt_sigattach(struct knote *kn)
1726 struct proc *p = curproc;
1728 kn->kn_ptr.p_proc = p;
1729 kn->kn_flags |= EV_CLEAR; /* automatically set */
1731 /* XXX lock the proc here while adding to the list? */
1732 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1738 filt_sigdetach(struct knote *kn)
1740 struct proc *p = kn->kn_ptr.p_proc;
1742 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1746 * signal knotes are shared with proc knotes, so we apply a mask to
1747 * the hint in order to differentiate them from process hints. This
1748 * could be avoided by using a signal-specific knote list, but probably
1749 * isn't worth the trouble.
1752 filt_signal(struct knote *kn, long hint)
1755 if (hint & NOTE_SIGNAL) {
1756 hint &= ~NOTE_SIGNAL;
1758 if (kn->kn_id == hint)
1761 return (kn->kn_data != 0);