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.4 2003/06/23 17:55: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((int sig, struct sigaction *act,
77 struct sigaction *oact, int old));
78 static int do_sigprocmask __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((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(q, sig) \
102 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
103 ((sig) == SIGCONT && (q)->p_session == curproc->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_ucred->cr_ruid || \
111 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
112 (ruid) == (q)->p_ucred->cr_uid || \
113 (uc)->cr_uid == (q)->p_ucred->cr_uid)
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(int sig, struct sigaction *act, struct sigaction *oact, int old)
198 struct proc *p = curproc;
199 struct sigacts *ps = p->p_sigacts;
201 if (sig <= 0 || sig > _SIG_MAXSIG)
205 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
206 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
208 if (SIGISMEMBER(ps->ps_sigonstack, sig))
209 oact->sa_flags |= SA_ONSTACK;
210 if (!SIGISMEMBER(ps->ps_sigintr, sig))
211 oact->sa_flags |= SA_RESTART;
212 if (SIGISMEMBER(ps->ps_sigreset, sig))
213 oact->sa_flags |= SA_RESETHAND;
214 if (SIGISMEMBER(ps->ps_signodefer, sig))
215 oact->sa_flags |= SA_NODEFER;
216 if (SIGISMEMBER(ps->ps_siginfo, sig))
217 oact->sa_flags |= SA_SIGINFO;
218 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
219 oact->sa_flags |= SA_NOCLDSTOP;
220 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
221 oact->sa_flags |= SA_NOCLDWAIT;
224 if ((sig == SIGKILL || sig == SIGSTOP) &&
225 act->sa_handler != SIG_DFL)
229 * Change setting atomically.
233 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
234 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
235 if (act->sa_flags & SA_SIGINFO) {
236 ps->ps_sigact[_SIG_IDX(sig)] =
237 (__sighandler_t *)act->sa_sigaction;
238 SIGADDSET(ps->ps_siginfo, sig);
240 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
241 SIGDELSET(ps->ps_siginfo, sig);
243 if (!(act->sa_flags & SA_RESTART))
244 SIGADDSET(ps->ps_sigintr, sig);
246 SIGDELSET(ps->ps_sigintr, sig);
247 if (act->sa_flags & SA_ONSTACK)
248 SIGADDSET(ps->ps_sigonstack, sig);
250 SIGDELSET(ps->ps_sigonstack, sig);
251 if (act->sa_flags & SA_RESETHAND)
252 SIGADDSET(ps->ps_sigreset, sig);
254 SIGDELSET(ps->ps_sigreset, sig);
255 if (act->sa_flags & SA_NODEFER)
256 SIGADDSET(ps->ps_signodefer, sig);
258 SIGDELSET(ps->ps_signodefer, sig);
260 if (act->sa_flags & SA_USERTRAMP)
261 SIGADDSET(ps->ps_usertramp, sig);
263 SIGDELSET(ps->ps_usertramp, seg);
265 if (sig == SIGCHLD) {
266 if (act->sa_flags & SA_NOCLDSTOP)
267 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
269 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
270 if (act->sa_flags & SA_NOCLDWAIT) {
272 * Paranoia: since SA_NOCLDWAIT is implemented
273 * by reparenting the dying child to PID 1 (and
274 * trust it to reap the zombie), PID 1 itself
275 * is forbidden to set SA_NOCLDWAIT.
278 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
280 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
282 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
285 * Set bit in p_sigignore for signals that are set to SIG_IGN,
286 * and for signals set to SIG_DFL where the default is to
287 * ignore. However, don't put SIGCONT in p_sigignore, as we
288 * have to restart the process.
290 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
291 (sigprop(sig) & SA_IGNORE &&
292 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
293 /* never to be seen again */
294 SIGDELSET(p->p_siglist, sig);
296 /* easier in psignal */
297 SIGADDSET(p->p_sigignore, sig);
298 SIGDELSET(p->p_sigcatch, sig);
300 SIGDELSET(p->p_sigignore, sig);
301 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
302 SIGDELSET(p->p_sigcatch, sig);
304 SIGADDSET(p->p_sigcatch, sig);
306 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
307 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
308 SIGDELSET(ps->ps_osigset, sig);
310 SIGADDSET(ps->ps_osigset, sig);
317 #ifndef _SYS_SYSPROTO_H_
318 struct sigaction_args {
320 struct sigaction *act;
321 struct sigaction *oact;
326 sigaction(struct sigaction_args *uap)
328 struct sigaction act, oact;
329 register struct sigaction *actp, *oactp;
332 actp = (uap->act != NULL) ? &act : NULL;
333 oactp = (uap->oact != NULL) ? &oact : NULL;
335 error = copyin(uap->act, actp, sizeof(act));
339 error = do_sigaction(uap->sig, actp, oactp, 0);
340 if (oactp && !error) {
341 error = copyout(oactp, uap->oact, sizeof(oact));
346 #ifndef _SYS_SYSPROTO_H_
347 struct osigaction_args {
349 struct osigaction *nsa;
350 struct osigaction *osa;
355 osigaction(struct osigaction_args *uap)
357 struct osigaction sa;
358 struct sigaction nsa, osa;
359 register struct sigaction *nsap, *osap;
362 if (uap->signum <= 0 || uap->signum >= ONSIG)
364 nsap = (uap->nsa != NULL) ? &nsa : NULL;
365 osap = (uap->osa != NULL) ? &osa : NULL;
367 error = copyin(uap->nsa, &sa, sizeof(sa));
370 nsap->sa_handler = sa.sa_handler;
371 nsap->sa_flags = sa.sa_flags;
372 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
374 error = do_sigaction(uap->signum, nsap, osap, 1);
375 if (osap && !error) {
376 sa.sa_handler = osap->sa_handler;
377 sa.sa_flags = osap->sa_flags;
378 SIG2OSIG(osap->sa_mask, sa.sa_mask);
379 error = copyout(&sa, uap->osa, sizeof(sa));
385 * Initialize signal state for process 0;
386 * set to ignore signals that are ignored by default.
394 for (i = 1; i <= NSIG; i++)
395 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
396 SIGADDSET(p->p_sigignore, i);
400 * Reset signals for an exec of the specified process.
404 register struct proc *p;
406 register struct sigacts *ps = p->p_sigacts;
410 * Reset caught signals. Held signals remain held
411 * through p_sigmask (unless they were caught,
412 * and are now ignored by default).
414 while (SIGNOTEMPTY(p->p_sigcatch)) {
415 sig = sig_ffs(&p->p_sigcatch);
416 SIGDELSET(p->p_sigcatch, sig);
417 if (sigprop(sig) & SA_IGNORE) {
419 SIGADDSET(p->p_sigignore, sig);
420 SIGDELSET(p->p_siglist, sig);
422 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
425 * Reset stack state to the user stack.
426 * Clear set of signals caught on the signal stack.
428 p->p_sigstk.ss_flags = SS_DISABLE;
429 p->p_sigstk.ss_size = 0;
430 p->p_sigstk.ss_sp = 0;
431 p->p_flag &= ~P_ALTSTACK;
433 * Reset no zombies if child dies flag as Solaris does.
435 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
439 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
441 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
442 * p == curproc. Also remember that in order to remain MP SAFE
443 * no spl*() calls may be made.
446 do_sigprocmask(int how, sigset_t *set, sigset_t *oset, int old)
448 struct proc *p = curproc;
452 *oset = p->p_sigmask;
459 SIGSETOR(p->p_sigmask, *set);
462 SIGSETNAND(p->p_sigmask, *set);
467 SIGSETLO(p->p_sigmask, *set);
480 * sigprocmask() - MP SAFE
483 #ifndef _SYS_SYSPROTO_H_
484 struct sigprocmask_args {
491 sigprocmask(struct sigprocmask_args *uap)
494 sigset_t *setp, *osetp;
497 setp = (uap->set != NULL) ? &set : NULL;
498 osetp = (uap->oset != NULL) ? &oset : NULL;
500 error = copyin(uap->set, setp, sizeof(set));
504 error = do_sigprocmask(uap->how, setp, osetp, 0);
505 if (osetp && !error) {
506 error = copyout(osetp, uap->oset, sizeof(oset));
512 * osigprocmask() - MP SAFE
515 #ifndef _SYS_SYSPROTO_H_
516 struct osigprocmask_args {
522 osigprocmask(struct osigprocmask_args *uap)
527 OSIG2SIG(uap->mask, set);
528 error = do_sigprocmask(uap->how, &set, &oset, 1);
529 SIG2OSIG(oset, curproc->p_retval[0]);
533 #ifndef _SYS_SYSPROTO_H_
534 struct sigpending_args {
540 sigpending(struct sigpending_args *uap)
542 struct proc *p = curproc;
544 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
547 #ifndef _SYS_SYSPROTO_H_
548 struct osigpending_args {
554 osigpending(struct osigpending_args *uap)
556 struct proc *p = curproc;
558 SIG2OSIG(p->p_siglist, p->p_retval[0]);
562 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
564 * Generalized interface signal handler, 4.3-compatible.
566 #ifndef _SYS_SYSPROTO_H_
567 struct osigvec_args {
575 osigvec(struct osigvec_args *uap)
578 struct sigaction nsa, osa;
579 register struct sigaction *nsap, *osap;
582 if (uap->signum <= 0 || uap->signum >= ONSIG)
584 nsap = (uap->nsv != NULL) ? &nsa : NULL;
585 osap = (uap->osv != NULL) ? &osa : NULL;
587 error = copyin(uap->nsv, &vec, sizeof(vec));
590 nsap->sa_handler = vec.sv_handler;
591 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
592 nsap->sa_flags = vec.sv_flags;
593 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
595 nsap->sa_flags |= SA_USERTRAMP;
598 error = do_sigaction(uap->signum, nsap, osap, 1);
599 if (osap && !error) {
600 vec.sv_handler = osap->sa_handler;
601 SIG2OSIG(osap->sa_mask, vec.sv_mask);
602 vec.sv_flags = osap->sa_flags;
603 vec.sv_flags &= ~SA_NOCLDWAIT;
604 vec.sv_flags ^= SA_RESTART;
606 vec.sv_flags &= ~SA_NOCLDSTOP;
608 error = copyout(&vec, uap->osv, sizeof(vec));
613 #ifndef _SYS_SYSPROTO_H_
614 struct osigblock_args {
619 osigblock(struct osigblock_args *uap)
621 struct proc *p = curproc;
624 OSIG2SIG(uap->mask, set);
627 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
628 SIGSETOR(p->p_sigmask, set);
633 #ifndef _SYS_SYSPROTO_H_
634 struct osigsetmask_args {
639 osigsetmask(struct osigsetmask_args *uap)
641 struct proc *p = curproc;
644 OSIG2SIG(uap->mask, set);
647 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
648 SIGSETLO(p->p_sigmask, set);
652 #endif /* COMPAT_43 || COMPAT_SUNOS */
655 * Suspend process until signal, providing mask to be set
656 * in the meantime. Note nonstandard calling convention:
657 * libc stub passes mask, not pointer, to save a copyin.
659 #ifndef _SYS_SYSPROTO_H_
660 struct sigsuspend_args {
661 const sigset_t *sigmask;
666 sigsuspend(struct sigsuspend_args *uap)
668 struct proc *p = curproc;
670 struct sigacts *ps = p->p_sigacts;
673 error = copyin(uap->sigmask, &mask, sizeof(mask));
678 * When returning from sigsuspend, we want
679 * the old mask to be restored after the
680 * signal handler has finished. Thus, we
681 * save it here and mark the sigacts structure
684 p->p_oldsigmask = p->p_sigmask;
685 p->p_flag |= P_OLDMASK;
689 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
691 /* always return EINTR rather than ERESTART... */
695 #ifndef _SYS_SYSPROTO_H_
696 struct osigsuspend_args {
702 osigsuspend(struct osigsuspend_args *uap)
705 struct proc *p = curproc;
706 struct sigacts *ps = p->p_sigacts;
708 p->p_oldsigmask = p->p_sigmask;
709 p->p_flag |= P_OLDMASK;
710 OSIG2SIG(uap->mask, mask);
712 SIGSETLO(p->p_sigmask, mask);
713 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
715 /* always return EINTR rather than ERESTART... */
719 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
720 #ifndef _SYS_SYSPROTO_H_
721 struct osigstack_args {
722 struct sigstack *nss;
723 struct sigstack *oss;
728 osigstack(struct osigstack_args *uap)
730 struct proc *p = curproc;
734 ss.ss_sp = p->p_sigstk.ss_sp;
735 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
736 if (uap->oss && (error = copyout(&ss, uap->oss,
737 sizeof(struct sigstack))))
739 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
740 p->p_sigstk.ss_sp = ss.ss_sp;
741 p->p_sigstk.ss_size = 0;
742 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
743 p->p_flag |= P_ALTSTACK;
747 #endif /* COMPAT_43 || COMPAT_SUNOS */
749 #ifndef _SYS_SYSPROTO_H_
750 struct sigaltstack_args {
757 sigaltstack(struct sigaltstack_args *uap)
759 struct proc *p = curproc;
763 if ((p->p_flag & P_ALTSTACK) == 0)
764 p->p_sigstk.ss_flags |= SS_DISABLE;
765 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
770 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
772 if (ss.ss_flags & SS_DISABLE) {
773 if (p->p_sigstk.ss_flags & SS_ONSTACK)
775 p->p_flag &= ~P_ALTSTACK;
776 p->p_sigstk.ss_flags = ss.ss_flags;
779 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
781 p->p_flag |= P_ALTSTACK;
787 * Common code for kill process group/broadcast kill.
788 * cp is calling process.
791 killpg1(int sig, int pgid, int all)
793 struct proc *cp = curproc;
802 LIST_FOREACH(p, &allproc, p_list) {
803 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
804 p == cp || !CANSIGNAL(p, sig))
813 * zero pgid means send to my process group.
821 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
822 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
823 p->p_stat == SZOMB ||
831 return (nfound ? 0 : ESRCH);
834 #ifndef _SYS_SYSPROTO_H_
842 kill(struct kill_args *uap)
846 if ((u_int)uap->signum > _SIG_MAXSIG)
849 /* kill single process */
850 if ((p = pfind(uap->pid)) == NULL)
852 if (!CANSIGNAL(p, uap->signum))
855 psignal(p, uap->signum);
859 case -1: /* broadcast signal */
860 return (killpg1(uap->signum, 0, 1));
861 case 0: /* signal own process group */
862 return (killpg1(uap->signum, 0, 0));
863 default: /* negative explicit process group */
864 return (killpg1(uap->signum, -uap->pid, 0));
869 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
870 #ifndef _SYS_SYSPROTO_H_
871 struct okillpg_args {
878 okillpg(struct okillpg_args *uap)
880 if ((u_int)uap->signum > _SIG_MAXSIG)
882 return (killpg1(uap->signum, uap->pgid, 0));
884 #endif /* COMPAT_43 || COMPAT_SUNOS */
887 * Send a signal to a process group.
890 gsignal(int pgid, int sig)
894 if (pgid && (pgrp = pgfind(pgid)))
895 pgsignal(pgrp, sig, 0);
899 * Send a signal to a process group. If checktty is 1,
900 * limit to members which have a controlling terminal.
903 pgsignal(pgrp, sig, checkctty)
907 register struct proc *p;
910 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
911 if (checkctty == 0 || p->p_flag & P_CONTROLT)
916 * Send a signal caused by a trap to the current process.
917 * If it will be caught immediately, deliver it with correct code.
918 * Otherwise, post it normally.
921 trapsignal(p, sig, code)
926 register struct sigacts *ps = p->p_sigacts;
928 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
929 !SIGISMEMBER(p->p_sigmask, sig)) {
930 p->p_stats->p_ru.ru_nsignals++;
932 if (KTRPOINT(p, KTR_PSIG))
933 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
934 &p->p_sigmask, code);
936 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
937 &p->p_sigmask, code);
938 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
939 if (!SIGISMEMBER(ps->ps_signodefer, sig))
940 SIGADDSET(p->p_sigmask, sig);
941 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
943 * See do_sigaction() for origin of this code.
945 SIGDELSET(p->p_sigcatch, sig);
946 if (sig != SIGCONT &&
947 sigprop(sig) & SA_IGNORE)
948 SIGADDSET(p->p_sigignore, sig);
949 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
952 p->p_code = code; /* XXX for core dump/debugger */
953 p->p_sig = sig; /* XXX to verify code */
959 * Send the signal to the process. If the signal has an action, the action
960 * is usually performed by the target process rather than the caller; we add
961 * the signal to the set of pending signals for the process.
964 * o When a stop signal is sent to a sleeping process that takes the
965 * default action, the process is stopped without awakening it.
966 * o SIGCONT restarts stopped processes (or puts them back to sleep)
967 * regardless of the signal action (eg, blocked or ignored).
969 * Other ignored signals are discarded immediately.
973 register struct proc *p;
976 register int s, prop;
977 register sig_t action;
979 if (sig > _SIG_MAXSIG || sig <= 0) {
980 printf("psignal: signal %d\n", sig);
981 panic("psignal signal number");
985 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
991 * If proc is traced, always give parent a chance;
992 * if signal event is tracked by procfs, give *that*
995 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
999 * If the signal is being ignored,
1000 * then we forget about it immediately.
1001 * (Note: we don't set SIGCONT in p_sigignore,
1002 * and if it is set to SIG_IGN,
1003 * action will be SIG_DFL here.)
1005 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1007 if (SIGISMEMBER(p->p_sigmask, sig))
1009 else if (SIGISMEMBER(p->p_sigcatch, sig))
1015 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1016 (p->p_flag & P_TRACED) == 0)
1020 SIG_STOPSIGMASK(p->p_siglist);
1022 if (prop & SA_STOP) {
1024 * If sending a tty stop signal to a member of an orphaned
1025 * process group, discard the signal here if the action
1026 * is default; don't stop the process below if sleeping,
1027 * and don't clear any pending SIGCONT.
1029 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1032 SIG_CONTSIGMASK(p->p_siglist);
1034 SIGADDSET(p->p_siglist, sig);
1037 * Defer further processing for signals which are held,
1038 * except that stopped processes must be continued by SIGCONT.
1040 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1043 switch (p->p_stat) {
1047 * If process is sleeping uninterruptibly
1048 * we can't interrupt the sleep... the signal will
1049 * be noticed when the process returns through
1050 * trap() or syscall().
1052 if ((p->p_flag & P_SINTR) == 0)
1055 * Process is sleeping and traced... make it runnable
1056 * so it can discover the signal in issignal() and stop
1059 if (p->p_flag & P_TRACED)
1062 * If SIGCONT is default (or ignored) and process is
1063 * asleep, we are finished; the process should not
1066 if ((prop & SA_CONT) && action == SIG_DFL) {
1067 SIGDELSET(p->p_siglist, sig);
1071 * When a sleeping process receives a stop
1072 * signal, process immediately if possible.
1073 * All other (caught or default) signals
1074 * cause the process to run.
1076 if (prop & SA_STOP) {
1077 if (action != SIG_DFL)
1080 * If a child holding parent blocked,
1081 * stopping could cause deadlock.
1083 if (p->p_flag & P_PPWAIT)
1085 SIGDELSET(p->p_siglist, sig);
1087 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1088 psignal(p->p_pptr, SIGCHLD);
1097 * If traced process is already stopped,
1098 * then no further action is necessary.
1100 if (p->p_flag & P_TRACED)
1104 * Kill signal always sets processes running.
1109 if (prop & SA_CONT) {
1111 * If SIGCONT is default (or ignored), we continue the
1112 * process but don't leave the signal in p_siglist, as
1113 * it has no further action. If SIGCONT is held, we
1114 * continue the process and leave the signal in
1115 * p_siglist. If the process catches SIGCONT, let it
1116 * handle the signal itself. If it isn't waiting on
1117 * an event, then it goes back to run state.
1118 * Otherwise, process goes back to sleep state.
1120 if (action == SIG_DFL)
1121 SIGDELSET(p->p_siglist, sig);
1122 if (action == SIG_CATCH)
1124 if (p->p_wchan == 0)
1130 if (prop & SA_STOP) {
1132 * Already stopped, don't need to stop again.
1133 * (If we did the shell could get confused.)
1135 SIGDELSET(p->p_siglist, sig);
1140 * If process is sleeping interruptibly, then simulate a
1141 * wakeup so that when it is continued, it will be made
1142 * runnable and can look at the signal. But don't make
1143 * the process runnable, leave it stopped.
1145 if (p->p_wchan && (p->p_flag & P_SINTR))
1151 * SRUN, SIDL, SZOMB do nothing with the signal,
1152 * other than kicking ourselves if we are running.
1153 * It will either never be noticed, or noticed very soon.
1158 else if (p->p_stat == SRUN)
1167 * Raise priority to at least PUSER.
1169 if (p->p_priority > PUSER)
1170 p->p_priority = PUSER;
1178 * If the current process has received a signal (should be caught or cause
1179 * termination, should interrupt current syscall), return the signal number.
1180 * Stop signals with default action are processed immediately, then cleared;
1181 * they aren't returned. This is checked after each entry to the system for
1182 * a syscall or trap (though this can usually be done without calling issignal
1183 * by checking the pending signal masks in the CURSIG macro.) The normal call
1186 * while (sig = CURSIG(curproc))
1191 register struct proc *p;
1194 register int sig, prop;
1197 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1199 mask = p->p_siglist;
1200 SIGSETNAND(mask, p->p_sigmask);
1201 if (p->p_flag & P_PPWAIT)
1202 SIG_STOPSIGMASK(mask);
1203 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1205 sig = sig_ffs(&mask);
1207 STOPEVENT(p, S_SIG, sig);
1210 * We should see pending but ignored signals
1211 * only if P_TRACED was on when they were posted.
1213 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1214 SIGDELSET(p->p_siglist, sig);
1217 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1219 * If traced, always stop, and stay
1220 * stopped until released by the parent.
1223 psignal(p->p_pptr, SIGCHLD);
1227 } while (!trace_req(p)
1228 && p->p_flag & P_TRACED);
1231 * If parent wants us to take the signal,
1232 * then it will leave it in p->p_xstat;
1233 * otherwise we just look for signals again.
1235 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1241 * Put the new signal into p_siglist. If the
1242 * signal is being masked, look for other signals.
1244 SIGADDSET(p->p_siglist, sig);
1245 if (SIGISMEMBER(p->p_sigmask, sig))
1249 * If the traced bit got turned off, go back up
1250 * to the top to rescan signals. This ensures
1251 * that p_sig* and ps_sigact are consistent.
1253 if ((p->p_flag & P_TRACED) == 0)
1257 prop = sigprop(sig);
1260 * Decide whether the signal should be returned.
1261 * Return the signal's number, or fall through
1262 * to clear it from the pending mask.
1264 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1268 * Don't take default actions on system processes.
1270 if (p->p_pid <= 1) {
1273 * Are you sure you want to ignore SIGSEGV
1276 printf("Process (pid %lu) got signal %d\n",
1277 (u_long)p->p_pid, sig);
1279 break; /* == ignore */
1282 * If there is a pending stop signal to process
1283 * with default action, stop here,
1284 * then clear the signal. However,
1285 * if process is member of an orphaned
1286 * process group, ignore tty stop signals.
1288 if (prop & SA_STOP) {
1289 if (p->p_flag & P_TRACED ||
1290 (p->p_pgrp->pg_jobc == 0 &&
1292 break; /* == ignore */
1295 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1296 psignal(p->p_pptr, SIGCHLD);
1299 } else if (prop & SA_IGNORE) {
1301 * Except for SIGCONT, shouldn't get here.
1302 * Default action is to ignore; drop it.
1304 break; /* == ignore */
1311 * Masking above should prevent us ever trying
1312 * to take action on an ignored signal other
1313 * than SIGCONT, unless process is traced.
1315 if ((prop & SA_CONT) == 0 &&
1316 (p->p_flag & P_TRACED) == 0)
1317 printf("issignal\n");
1318 break; /* == ignore */
1322 * This signal has an action, let
1323 * postsig() process it.
1327 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1333 * Put the argument process into the stopped state and notify the parent
1334 * via wakeup. Signals are handled elsewhere. The process must not be
1339 register struct proc *p;
1343 p->p_flag &= ~P_WAITED;
1344 wakeup((caddr_t)p->p_pptr);
1348 * Take the action for the specified signal
1349 * from the current set of pending signals.
1355 struct proc *p = curproc;
1356 struct sigacts *ps = p->p_sigacts;
1358 sigset_t returnmask;
1361 KASSERT(sig != 0, ("postsig"));
1363 SIGDELSET(p->p_siglist, sig);
1364 action = ps->ps_sigact[_SIG_IDX(sig)];
1366 if (KTRPOINT(p, KTR_PSIG))
1367 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1368 &p->p_oldsigmask : &p->p_sigmask, 0);
1370 STOPEVENT(p, S_SIG, sig);
1372 if (action == SIG_DFL) {
1374 * Default action, where the default is to kill
1375 * the process. (Other cases were ignored above.)
1381 * If we get here, the signal must be caught.
1383 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1384 ("postsig action"));
1386 * Set the new mask value and also defer further
1387 * occurrences of this signal.
1389 * Special case: user has done a sigsuspend. Here the
1390 * current mask is not of interest, but rather the
1391 * mask from before the sigsuspend is what we want
1392 * restored after the signal processing is completed.
1395 if (p->p_flag & P_OLDMASK) {
1396 returnmask = p->p_oldsigmask;
1397 p->p_flag &= ~P_OLDMASK;
1399 returnmask = p->p_sigmask;
1401 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1402 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1403 SIGADDSET(p->p_sigmask, sig);
1405 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1407 * See do_sigaction() for origin of this code.
1409 SIGDELSET(p->p_sigcatch, sig);
1410 if (sig != SIGCONT &&
1411 sigprop(sig) & SA_IGNORE)
1412 SIGADDSET(p->p_sigignore, sig);
1413 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1416 p->p_stats->p_ru.ru_nsignals++;
1417 if (p->p_sig != sig) {
1424 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1429 * Kill the current process for stated reason.
1436 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1437 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1438 psignal(p, SIGKILL);
1442 * Force the current process to exit with the specified signal, dumping core
1443 * if appropriate. We bypass the normal tests for masked and caught signals,
1444 * allowing unrecoverable failures to terminate the process without changing
1445 * signal state. Mark the accounting record with the signal termination.
1446 * If dumping core, save the signal number for the debugger. Calls exit and
1450 sigexit(struct proc *p, int sig)
1452 p->p_acflag |= AXSIG;
1453 if (sigprop(sig) & SA_CORE) {
1456 * Log signals which would cause core dumps
1457 * (Log as LOG_INFO to appease those who don't want
1459 * XXX : Todo, as well as euid, write out ruid too
1461 if (coredump(p) == 0)
1463 if (kern_logsigexit)
1465 "pid %d (%s), uid %d: exited on signal %d%s\n",
1466 p->p_pid, p->p_comm,
1467 p->p_ucred ? p->p_ucred->cr_uid : -1,
1469 sig & WCOREFLAG ? " (core dumped)" : "");
1471 exit1(W_EXITCODE(0, sig));
1475 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1476 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1477 sizeof(corefilename), "process corefile name format string");
1480 * expand_name(name, uid, pid)
1481 * Expand the name described in corefilename, using name, uid, and pid.
1482 * corefilename is a printf-like string, with three format specifiers:
1483 * %N name of process ("name")
1484 * %P process id (pid)
1486 * For example, "%N.core" is the default; they can be disabled completely
1487 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1488 * This is controlled by the sysctl variable kern.corefile (see above).
1492 expand_name(name, uid, pid)
1493 const char *name; uid_t uid; pid_t pid; {
1495 char buf[11]; /* Buffer for pid/uid -- max 4B */
1497 char *format = corefilename;
1500 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1503 namelen = strlen(name);
1504 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1506 switch (format[i]) {
1507 case '%': /* Format character */
1509 switch (format[i]) {
1513 case 'N': /* process name */
1514 if ((n + namelen) > MAXPATHLEN) {
1515 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1516 pid, name, uid, temp, name);
1520 memcpy(temp+n, name, namelen);
1523 case 'P': /* process id */
1524 l = sprintf(buf, "%u", pid);
1525 if ((n + l) > MAXPATHLEN) {
1526 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1527 pid, name, uid, temp, name);
1531 memcpy(temp+n, buf, l);
1534 case 'U': /* user id */
1535 l = sprintf(buf, "%u", uid);
1536 if ((n + l) > MAXPATHLEN) {
1537 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1538 pid, name, uid, temp, name);
1542 memcpy(temp+n, buf, l);
1546 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1550 temp[n++] = format[i];
1558 * Dump a process' core. The main routine does some
1559 * policy checking, and creates the name of the coredump;
1560 * then it passes on a vnode and a size limit to the process-specific
1561 * coredump routine if there is one; if there _is not_ one, it returns
1562 * ENOSYS; otherwise it returns the error from the process-specific routine.
1567 register struct proc *p;
1569 register struct vnode *vp;
1570 register struct ucred *cred = p->p_ucred;
1572 struct nameidata nd;
1575 char *name; /* name of corefile */
1578 STOPEVENT(p, S_CORE, 0);
1580 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1584 * Note that the bulk of limit checking is done after
1585 * the corefile is created. The exception is if the limit
1586 * for corefiles is 0, in which case we don't bother
1587 * creating the corefile at all. This layout means that
1588 * a corefile is truncated instead of not being created,
1589 * if it is larger than the limit.
1591 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1595 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1598 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1599 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1603 NDFREE(&nd, NDF_ONLY_PNBUF);
1606 VOP_UNLOCK(vp, 0, p);
1607 lf.l_whence = SEEK_SET;
1610 lf.l_type = F_WRLCK;
1611 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1615 /* Don't dump to non-regular files or files with links. */
1616 if (vp->v_type != VREG ||
1617 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1623 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1625 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1626 VOP_SETATTR(vp, &vattr, cred, p);
1627 p->p_acflag |= ACORE;
1628 VOP_UNLOCK(vp, 0, p);
1630 error = p->p_sysent->sv_coredump ?
1631 p->p_sysent->sv_coredump(p, vp, limit) :
1635 lf.l_type = F_UNLCK;
1636 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1638 error1 = vn_close(vp, FWRITE, cred, p);
1645 * Nonexistent system call-- signal process (may want to handle it).
1646 * Flag error in case process won't see signal immediately (blocked or ignored).
1648 #ifndef _SYS_SYSPROTO_H_
1655 nosys(struct nosys_args *args)
1657 psignal(curproc, SIGSYS);
1662 * Send a SIGIO or SIGURG signal to a process or process group using
1663 * stored credentials rather than those of the current process.
1666 pgsigio(sigio, sig, checkctty)
1667 struct sigio *sigio;
1673 if (sigio->sio_pgid > 0) {
1674 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1676 psignal(sigio->sio_proc, sig);
1677 } else if (sigio->sio_pgid < 0) {
1680 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1681 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1682 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1688 filt_sigattach(struct knote *kn)
1690 struct proc *p = curproc;
1692 kn->kn_ptr.p_proc = p;
1693 kn->kn_flags |= EV_CLEAR; /* automatically set */
1695 /* XXX lock the proc here while adding to the list? */
1696 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1702 filt_sigdetach(struct knote *kn)
1704 struct proc *p = kn->kn_ptr.p_proc;
1706 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1710 * signal knotes are shared with proc knotes, so we apply a mask to
1711 * the hint in order to differentiate them from process hints. This
1712 * could be avoided by using a signal-specific knote list, but probably
1713 * isn't worth the trouble.
1716 filt_signal(struct knote *kn, long hint)
1719 if (hint & NOTE_SIGNAL) {
1720 hint &= ~NOTE_SIGNAL;
1722 if (kn->kn_id == hint)
1725 return (kn->kn_data != 0);