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.8 2003/06/30 19:50:31 dillon Exp $
43 #include "opt_compat.h"
44 #include "opt_ktrace.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/sysproto.h>
50 #include <sys/signalvar.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53 #include <sys/event.h>
55 #include <sys/namei.h>
56 #include <sys/pioctl.h>
57 #include <sys/systm.h>
59 #include <sys/fcntl.h>
61 #include <sys/ktrace.h>
62 #include <sys/syslog.h>
64 #include <sys/sysent.h>
65 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
67 #include <sys/unistd.h>
70 #include <machine/ipl.h>
71 #include <machine/cpu.h>
72 #include <machine/smp.h>
74 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
76 static int coredump __P((struct proc *));
77 static int do_sigaction __P((int sig, struct sigaction *act,
78 struct sigaction *oact, int old));
79 static int do_sigprocmask __P((int how, sigset_t *set,
80 sigset_t *oset, int old));
81 static char *expand_name __P((const char *, uid_t, pid_t));
82 static int killpg1 __P((int sig, int pgid, int all));
83 static int sig_ffs __P((sigset_t *set));
84 static int sigprop __P((int sig));
85 static void stop __P((struct proc *));
87 static int filt_sigattach(struct knote *kn);
88 static void filt_sigdetach(struct knote *kn);
89 static int filt_signal(struct knote *kn, long hint);
91 struct filterops sig_filtops =
92 { 0, filt_sigattach, filt_sigdetach, filt_signal };
94 static int kern_logsigexit = 1;
95 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
97 "Log processes quitting on abnormal signals to syslog(3)");
100 * Can process p, with pcred pc, send the signal sig to process q?
102 #define CANSIGNAL(q, sig) \
103 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
104 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
107 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
109 #define CANSIGIO(ruid, uc, q) \
110 ((uc)->cr_uid == 0 || \
111 (ruid) == (q)->p_ucred->cr_ruid || \
112 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
113 (ruid) == (q)->p_ucred->cr_uid || \
114 (uc)->cr_uid == (q)->p_ucred->cr_uid)
117 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
118 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120 static int do_coredump = 1;
121 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
122 &do_coredump, 0, "Enable/Disable coredumps");
125 * Signal properties and actions.
126 * The array below categorizes the signals and their default actions
127 * according to the following properties:
129 #define SA_KILL 0x01 /* terminates process by default */
130 #define SA_CORE 0x02 /* ditto and coredumps */
131 #define SA_STOP 0x04 /* suspend process */
132 #define SA_TTYSTOP 0x08 /* ditto, from tty */
133 #define SA_IGNORE 0x10 /* ignore by default */
134 #define SA_CONT 0x20 /* continue if suspended */
135 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
137 static int sigproptbl[NSIG] = {
138 SA_KILL, /* SIGHUP */
139 SA_KILL, /* SIGINT */
140 SA_KILL|SA_CORE, /* SIGQUIT */
141 SA_KILL|SA_CORE, /* SIGILL */
142 SA_KILL|SA_CORE, /* SIGTRAP */
143 SA_KILL|SA_CORE, /* SIGABRT */
144 SA_KILL|SA_CORE, /* SIGEMT */
145 SA_KILL|SA_CORE, /* SIGFPE */
146 SA_KILL, /* SIGKILL */
147 SA_KILL|SA_CORE, /* SIGBUS */
148 SA_KILL|SA_CORE, /* SIGSEGV */
149 SA_KILL|SA_CORE, /* SIGSYS */
150 SA_KILL, /* SIGPIPE */
151 SA_KILL, /* SIGALRM */
152 SA_KILL, /* SIGTERM */
153 SA_IGNORE, /* SIGURG */
154 SA_STOP, /* SIGSTOP */
155 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
156 SA_IGNORE|SA_CONT, /* SIGCONT */
157 SA_IGNORE, /* SIGCHLD */
158 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
159 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
160 SA_IGNORE, /* SIGIO */
161 SA_KILL, /* SIGXCPU */
162 SA_KILL, /* SIGXFSZ */
163 SA_KILL, /* SIGVTALRM */
164 SA_KILL, /* SIGPROF */
165 SA_IGNORE, /* SIGWINCH */
166 SA_IGNORE, /* SIGINFO */
167 SA_KILL, /* SIGUSR1 */
168 SA_KILL, /* SIGUSR2 */
175 if (sig > 0 && sig < NSIG)
176 return (sigproptbl[_SIG_IDX(sig)]);
181 sig_ffs(sigset_t *set)
185 for (i = 0; i < _SIG_WORDS; i++)
187 return (ffs(set->__bits[i]) + (i * 32));
197 do_sigaction(int sig, struct sigaction *act, struct sigaction *oact, int old)
199 struct proc *p = curproc;
200 struct sigacts *ps = p->p_sigacts;
202 if (sig <= 0 || sig > _SIG_MAXSIG)
206 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
207 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
209 if (SIGISMEMBER(ps->ps_sigonstack, sig))
210 oact->sa_flags |= SA_ONSTACK;
211 if (!SIGISMEMBER(ps->ps_sigintr, sig))
212 oact->sa_flags |= SA_RESTART;
213 if (SIGISMEMBER(ps->ps_sigreset, sig))
214 oact->sa_flags |= SA_RESETHAND;
215 if (SIGISMEMBER(ps->ps_signodefer, sig))
216 oact->sa_flags |= SA_NODEFER;
217 if (SIGISMEMBER(ps->ps_siginfo, sig))
218 oact->sa_flags |= SA_SIGINFO;
219 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
220 oact->sa_flags |= SA_NOCLDSTOP;
221 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
222 oact->sa_flags |= SA_NOCLDWAIT;
225 if ((sig == SIGKILL || sig == SIGSTOP) &&
226 act->sa_handler != SIG_DFL)
230 * Change setting atomically.
234 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
235 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
236 if (act->sa_flags & SA_SIGINFO) {
237 ps->ps_sigact[_SIG_IDX(sig)] =
238 (__sighandler_t *)act->sa_sigaction;
239 SIGADDSET(ps->ps_siginfo, sig);
241 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
242 SIGDELSET(ps->ps_siginfo, sig);
244 if (!(act->sa_flags & SA_RESTART))
245 SIGADDSET(ps->ps_sigintr, sig);
247 SIGDELSET(ps->ps_sigintr, sig);
248 if (act->sa_flags & SA_ONSTACK)
249 SIGADDSET(ps->ps_sigonstack, sig);
251 SIGDELSET(ps->ps_sigonstack, sig);
252 if (act->sa_flags & SA_RESETHAND)
253 SIGADDSET(ps->ps_sigreset, sig);
255 SIGDELSET(ps->ps_sigreset, sig);
256 if (act->sa_flags & SA_NODEFER)
257 SIGADDSET(ps->ps_signodefer, sig);
259 SIGDELSET(ps->ps_signodefer, sig);
261 if (act->sa_flags & SA_USERTRAMP)
262 SIGADDSET(ps->ps_usertramp, sig);
264 SIGDELSET(ps->ps_usertramp, seg);
266 if (sig == SIGCHLD) {
267 if (act->sa_flags & SA_NOCLDSTOP)
268 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
270 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
271 if (act->sa_flags & SA_NOCLDWAIT) {
273 * Paranoia: since SA_NOCLDWAIT is implemented
274 * by reparenting the dying child to PID 1 (and
275 * trust it to reap the zombie), PID 1 itself
276 * is forbidden to set SA_NOCLDWAIT.
279 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
281 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
283 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
286 * Set bit in p_sigignore for signals that are set to SIG_IGN,
287 * and for signals set to SIG_DFL where the default is to
288 * ignore. However, don't put SIGCONT in p_sigignore, as we
289 * have to restart the process.
291 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
292 (sigprop(sig) & SA_IGNORE &&
293 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
294 /* never to be seen again */
295 SIGDELSET(p->p_siglist, sig);
297 /* easier in psignal */
298 SIGADDSET(p->p_sigignore, sig);
299 SIGDELSET(p->p_sigcatch, sig);
301 SIGDELSET(p->p_sigignore, sig);
302 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
303 SIGDELSET(p->p_sigcatch, sig);
305 SIGADDSET(p->p_sigcatch, sig);
307 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
308 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
309 SIGDELSET(ps->ps_osigset, sig);
311 SIGADDSET(ps->ps_osigset, sig);
318 #ifndef _SYS_SYSPROTO_H_
319 struct sigaction_args {
321 struct sigaction *act;
322 struct sigaction *oact;
327 sigaction(struct sigaction_args *uap)
329 struct sigaction act, oact;
330 register struct sigaction *actp, *oactp;
333 actp = (uap->act != NULL) ? &act : NULL;
334 oactp = (uap->oact != NULL) ? &oact : NULL;
336 error = copyin(uap->act, actp, sizeof(act));
340 error = do_sigaction(uap->sig, actp, oactp, 0);
341 if (oactp && !error) {
342 error = copyout(oactp, uap->oact, sizeof(oact));
347 #ifndef _SYS_SYSPROTO_H_
348 struct osigaction_args {
350 struct osigaction *nsa;
351 struct osigaction *osa;
356 osigaction(struct osigaction_args *uap)
358 struct osigaction sa;
359 struct sigaction nsa, osa;
360 register struct sigaction *nsap, *osap;
363 if (uap->signum <= 0 || uap->signum >= ONSIG)
365 nsap = (uap->nsa != NULL) ? &nsa : NULL;
366 osap = (uap->osa != NULL) ? &osa : NULL;
368 error = copyin(uap->nsa, &sa, sizeof(sa));
371 nsap->sa_handler = sa.sa_handler;
372 nsap->sa_flags = sa.sa_flags;
373 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
375 error = do_sigaction(uap->signum, nsap, osap, 1);
376 if (osap && !error) {
377 sa.sa_handler = osap->sa_handler;
378 sa.sa_flags = osap->sa_flags;
379 SIG2OSIG(osap->sa_mask, sa.sa_mask);
380 error = copyout(&sa, uap->osa, sizeof(sa));
386 * Initialize signal state for process 0;
387 * set to ignore signals that are ignored by default.
395 for (i = 1; i <= NSIG; i++)
396 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
397 SIGADDSET(p->p_sigignore, i);
401 * Reset signals for an exec of the specified process.
405 register struct proc *p;
407 register struct sigacts *ps = p->p_sigacts;
411 * Reset caught signals. Held signals remain held
412 * through p_sigmask (unless they were caught,
413 * and are now ignored by default).
415 while (SIGNOTEMPTY(p->p_sigcatch)) {
416 sig = sig_ffs(&p->p_sigcatch);
417 SIGDELSET(p->p_sigcatch, sig);
418 if (sigprop(sig) & SA_IGNORE) {
420 SIGADDSET(p->p_sigignore, sig);
421 SIGDELSET(p->p_siglist, sig);
423 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
426 * Reset stack state to the user stack.
427 * Clear set of signals caught on the signal stack.
429 p->p_sigstk.ss_flags = SS_DISABLE;
430 p->p_sigstk.ss_size = 0;
431 p->p_sigstk.ss_sp = 0;
432 p->p_flag &= ~P_ALTSTACK;
434 * Reset no zombies if child dies flag as Solaris does.
436 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
440 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
442 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
443 * p == curproc. Also remember that in order to remain MP SAFE
444 * no spl*() calls may be made.
447 do_sigprocmask(int how, sigset_t *set, sigset_t *oset, int old)
449 struct proc *p = curproc;
453 *oset = p->p_sigmask;
460 SIGSETOR(p->p_sigmask, *set);
463 SIGSETNAND(p->p_sigmask, *set);
468 SIGSETLO(p->p_sigmask, *set);
481 * sigprocmask() - MP SAFE
484 #ifndef _SYS_SYSPROTO_H_
485 struct sigprocmask_args {
492 sigprocmask(struct sigprocmask_args *uap)
495 sigset_t *setp, *osetp;
498 setp = (uap->set != NULL) ? &set : NULL;
499 osetp = (uap->oset != NULL) ? &oset : NULL;
501 error = copyin(uap->set, setp, sizeof(set));
505 error = do_sigprocmask(uap->how, setp, osetp, 0);
506 if (osetp && !error) {
507 error = copyout(osetp, uap->oset, sizeof(oset));
513 * osigprocmask() - MP SAFE
516 #ifndef _SYS_SYSPROTO_H_
517 struct osigprocmask_args {
523 osigprocmask(struct osigprocmask_args *uap)
528 OSIG2SIG(uap->mask, set);
529 error = do_sigprocmask(uap->how, &set, &oset, 1);
530 SIG2OSIG(oset, curproc->p_retval[0]);
534 #ifndef _SYS_SYSPROTO_H_
535 struct sigpending_args {
541 sigpending(struct sigpending_args *uap)
543 struct proc *p = curproc;
545 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
548 #ifndef _SYS_SYSPROTO_H_
549 struct osigpending_args {
555 osigpending(struct osigpending_args *uap)
557 struct proc *p = curproc;
559 SIG2OSIG(p->p_siglist, p->p_retval[0]);
563 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
565 * Generalized interface signal handler, 4.3-compatible.
567 #ifndef _SYS_SYSPROTO_H_
568 struct osigvec_args {
576 osigvec(struct osigvec_args *uap)
579 struct sigaction nsa, osa;
580 register struct sigaction *nsap, *osap;
583 if (uap->signum <= 0 || uap->signum >= ONSIG)
585 nsap = (uap->nsv != NULL) ? &nsa : NULL;
586 osap = (uap->osv != NULL) ? &osa : NULL;
588 error = copyin(uap->nsv, &vec, sizeof(vec));
591 nsap->sa_handler = vec.sv_handler;
592 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
593 nsap->sa_flags = vec.sv_flags;
594 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
596 nsap->sa_flags |= SA_USERTRAMP;
599 error = do_sigaction(uap->signum, nsap, osap, 1);
600 if (osap && !error) {
601 vec.sv_handler = osap->sa_handler;
602 SIG2OSIG(osap->sa_mask, vec.sv_mask);
603 vec.sv_flags = osap->sa_flags;
604 vec.sv_flags &= ~SA_NOCLDWAIT;
605 vec.sv_flags ^= SA_RESTART;
607 vec.sv_flags &= ~SA_NOCLDSTOP;
609 error = copyout(&vec, uap->osv, sizeof(vec));
614 #ifndef _SYS_SYSPROTO_H_
615 struct osigblock_args {
620 osigblock(struct osigblock_args *uap)
622 struct proc *p = curproc;
625 OSIG2SIG(uap->mask, set);
628 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
629 SIGSETOR(p->p_sigmask, set);
634 #ifndef _SYS_SYSPROTO_H_
635 struct osigsetmask_args {
640 osigsetmask(struct osigsetmask_args *uap)
642 struct proc *p = curproc;
645 OSIG2SIG(uap->mask, set);
648 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
649 SIGSETLO(p->p_sigmask, set);
653 #endif /* COMPAT_43 || COMPAT_SUNOS */
656 * Suspend process until signal, providing mask to be set
657 * in the meantime. Note nonstandard calling convention:
658 * libc stub passes mask, not pointer, to save a copyin.
660 #ifndef _SYS_SYSPROTO_H_
661 struct sigsuspend_args {
662 const sigset_t *sigmask;
667 sigsuspend(struct sigsuspend_args *uap)
669 struct proc *p = curproc;
671 struct sigacts *ps = p->p_sigacts;
674 error = copyin(uap->sigmask, &mask, sizeof(mask));
679 * When returning from sigsuspend, we want
680 * the old mask to be restored after the
681 * signal handler has finished. Thus, we
682 * save it here and mark the sigacts structure
685 p->p_oldsigmask = p->p_sigmask;
686 p->p_flag |= P_OLDMASK;
690 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
692 /* always return EINTR rather than ERESTART... */
696 #ifndef _SYS_SYSPROTO_H_
697 struct osigsuspend_args {
703 osigsuspend(struct osigsuspend_args *uap)
706 struct proc *p = curproc;
707 struct sigacts *ps = p->p_sigacts;
709 p->p_oldsigmask = p->p_sigmask;
710 p->p_flag |= P_OLDMASK;
711 OSIG2SIG(uap->mask, mask);
713 SIGSETLO(p->p_sigmask, mask);
714 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
716 /* always return EINTR rather than ERESTART... */
720 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
721 #ifndef _SYS_SYSPROTO_H_
722 struct osigstack_args {
723 struct sigstack *nss;
724 struct sigstack *oss;
729 osigstack(struct osigstack_args *uap)
731 struct proc *p = curproc;
735 ss.ss_sp = p->p_sigstk.ss_sp;
736 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
737 if (uap->oss && (error = copyout(&ss, uap->oss,
738 sizeof(struct sigstack))))
740 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
741 p->p_sigstk.ss_sp = ss.ss_sp;
742 p->p_sigstk.ss_size = 0;
743 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
744 p->p_flag |= P_ALTSTACK;
748 #endif /* COMPAT_43 || COMPAT_SUNOS */
750 #ifndef _SYS_SYSPROTO_H_
751 struct sigaltstack_args {
758 sigaltstack(struct sigaltstack_args *uap)
760 struct proc *p = curproc;
764 if ((p->p_flag & P_ALTSTACK) == 0)
765 p->p_sigstk.ss_flags |= SS_DISABLE;
766 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
771 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
773 if (ss.ss_flags & SS_DISABLE) {
774 if (p->p_sigstk.ss_flags & SS_ONSTACK)
776 p->p_flag &= ~P_ALTSTACK;
777 p->p_sigstk.ss_flags = ss.ss_flags;
780 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
782 p->p_flag |= P_ALTSTACK;
788 * Common code for kill process group/broadcast kill.
789 * cp is calling process.
792 killpg1(int sig, int pgid, int all)
794 struct proc *cp = curproc;
803 LIST_FOREACH(p, &allproc, p_list) {
804 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
805 p == cp || !CANSIGNAL(p, sig))
814 * zero pgid means send to my process group.
822 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
823 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
824 p->p_stat == SZOMB ||
832 return (nfound ? 0 : ESRCH);
835 #ifndef _SYS_SYSPROTO_H_
843 kill(struct kill_args *uap)
847 if ((u_int)uap->signum > _SIG_MAXSIG)
850 /* kill single process */
851 if ((p = pfind(uap->pid)) == NULL)
853 if (!CANSIGNAL(p, uap->signum))
856 psignal(p, uap->signum);
860 case -1: /* broadcast signal */
861 return (killpg1(uap->signum, 0, 1));
862 case 0: /* signal own process group */
863 return (killpg1(uap->signum, 0, 0));
864 default: /* negative explicit process group */
865 return (killpg1(uap->signum, -uap->pid, 0));
870 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
871 #ifndef _SYS_SYSPROTO_H_
872 struct okillpg_args {
879 okillpg(struct okillpg_args *uap)
881 if ((u_int)uap->signum > _SIG_MAXSIG)
883 return (killpg1(uap->signum, uap->pgid, 0));
885 #endif /* COMPAT_43 || COMPAT_SUNOS */
888 * Send a signal to a process group.
891 gsignal(int pgid, int sig)
895 if (pgid && (pgrp = pgfind(pgid)))
896 pgsignal(pgrp, sig, 0);
900 * Send a signal to a process group. If checktty is 1,
901 * limit to members which have a controlling terminal.
904 pgsignal(pgrp, sig, checkctty)
908 register struct proc *p;
911 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
912 if (checkctty == 0 || p->p_flag & P_CONTROLT)
917 * Send a signal caused by a trap to the current process.
918 * If it will be caught immediately, deliver it with correct code.
919 * Otherwise, post it normally.
922 trapsignal(p, sig, code)
927 struct sigacts *ps = p->p_sigacts;
929 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
930 !SIGISMEMBER(p->p_sigmask, sig)) {
931 p->p_stats->p_ru.ru_nsignals++;
933 if (KTRPOINT(p->p_thread, KTR_PSIG))
934 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
935 &p->p_sigmask, code);
937 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
938 &p->p_sigmask, code);
939 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
940 if (!SIGISMEMBER(ps->ps_signodefer, sig))
941 SIGADDSET(p->p_sigmask, sig);
942 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
944 * See do_sigaction() for origin of this code.
946 SIGDELSET(p->p_sigcatch, sig);
947 if (sig != SIGCONT &&
948 sigprop(sig) & SA_IGNORE)
949 SIGADDSET(p->p_sigignore, sig);
950 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
953 p->p_code = code; /* XXX for core dump/debugger */
954 p->p_sig = sig; /* XXX to verify code */
960 * Send the signal to the process. If the signal has an action, the action
961 * is usually performed by the target process rather than the caller; we add
962 * the signal to the set of pending signals for the process.
965 * o When a stop signal is sent to a sleeping process that takes the
966 * default action, the process is stopped without awakening it.
967 * o SIGCONT restarts stopped processes (or puts them back to sleep)
968 * regardless of the signal action (eg, blocked or ignored).
970 * Other ignored signals are discarded immediately.
974 register struct proc *p;
977 register int s, prop;
978 register sig_t action;
980 if (sig > _SIG_MAXSIG || sig <= 0) {
981 printf("psignal: signal %d\n", sig);
982 panic("psignal signal number");
986 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
992 * If proc is traced, always give parent a chance;
993 * if signal event is tracked by procfs, give *that*
996 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1000 * If the signal is being ignored,
1001 * then we forget about it immediately.
1002 * (Note: we don't set SIGCONT in p_sigignore,
1003 * and if it is set to SIG_IGN,
1004 * action will be SIG_DFL here.)
1006 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1008 if (SIGISMEMBER(p->p_sigmask, sig))
1010 else if (SIGISMEMBER(p->p_sigcatch, sig))
1016 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1017 (p->p_flag & P_TRACED) == 0)
1021 SIG_STOPSIGMASK(p->p_siglist);
1023 if (prop & SA_STOP) {
1025 * If sending a tty stop signal to a member of an orphaned
1026 * process group, discard the signal here if the action
1027 * is default; don't stop the process below if sleeping,
1028 * and don't clear any pending SIGCONT.
1030 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1033 SIG_CONTSIGMASK(p->p_siglist);
1035 SIGADDSET(p->p_siglist, sig);
1038 * Defer further processing for signals which are held,
1039 * except that stopped processes must be continued by SIGCONT.
1041 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1044 switch (p->p_stat) {
1048 * If process is sleeping uninterruptibly
1049 * we can't interrupt the sleep... the signal will
1050 * be noticed when the process returns through
1051 * trap() or syscall().
1053 if ((p->p_flag & P_SINTR) == 0)
1056 * Process is sleeping and traced... make it runnable
1057 * so it can discover the signal in issignal() and stop
1060 if (p->p_flag & P_TRACED)
1063 * If SIGCONT is default (or ignored) and process is
1064 * asleep, we are finished; the process should not
1067 if ((prop & SA_CONT) && action == SIG_DFL) {
1068 SIGDELSET(p->p_siglist, sig);
1072 * When a sleeping process receives a stop
1073 * signal, process immediately if possible.
1074 * All other (caught or default) signals
1075 * cause the process to run.
1077 if (prop & SA_STOP) {
1078 if (action != SIG_DFL)
1081 * If a child holding parent blocked,
1082 * stopping could cause deadlock.
1084 if (p->p_flag & P_PPWAIT)
1086 SIGDELSET(p->p_siglist, sig);
1088 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1089 psignal(p->p_pptr, SIGCHLD);
1098 * If traced process is already stopped,
1099 * then no further action is necessary.
1101 if (p->p_flag & P_TRACED)
1105 * Kill signal always sets processes running.
1110 if (prop & SA_CONT) {
1112 * If SIGCONT is default (or ignored), we continue the
1113 * process but don't leave the signal in p_siglist, as
1114 * it has no further action. If SIGCONT is held, we
1115 * continue the process and leave the signal in
1116 * p_siglist. If the process catches SIGCONT, let it
1117 * handle the signal itself. If it isn't waiting on
1118 * an event, then it goes back to run state.
1119 * Otherwise, process goes back to sleep state.
1121 if (action == SIG_DFL)
1122 SIGDELSET(p->p_siglist, sig);
1123 if (action == SIG_CATCH)
1125 if (p->p_wchan == 0)
1127 clrrunnable(p, SSLEEP);
1131 if (prop & SA_STOP) {
1133 * Already stopped, don't need to stop again.
1134 * (If we did the shell could get confused.)
1136 SIGDELSET(p->p_siglist, sig);
1141 * If process is sleeping interruptibly, then simulate a
1142 * wakeup so that when it is continued, it will be made
1143 * runnable and can look at the signal. But don't make
1144 * the process runnable, leave it stopped.
1146 if (p->p_wchan && (p->p_flag & P_SINTR))
1147 unsleep(p->p_thread);
1152 * SRUN, SIDL, SZOMB do nothing with the signal,
1153 * other than kicking ourselves if we are running.
1154 * It will either never be noticed, or noticed very soon.
1159 else if (p->p_stat == SRUN)
1172 * If the current process has received a signal (should be caught or cause
1173 * termination, should interrupt current syscall), return the signal number.
1174 * Stop signals with default action are processed immediately, then cleared;
1175 * they aren't returned. This is checked after each entry to the system for
1176 * a syscall or trap (though this can usually be done without calling issignal
1177 * by checking the pending signal masks in the CURSIG macro.) The normal call
1180 * while (sig = CURSIG(curproc))
1185 register struct proc *p;
1188 register int sig, prop;
1191 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1193 mask = p->p_siglist;
1194 SIGSETNAND(mask, p->p_sigmask);
1195 if (p->p_flag & P_PPWAIT)
1196 SIG_STOPSIGMASK(mask);
1197 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1199 sig = sig_ffs(&mask);
1201 STOPEVENT(p, S_SIG, sig);
1204 * We should see pending but ignored signals
1205 * only if P_TRACED was on when they were posted.
1207 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1208 SIGDELSET(p->p_siglist, sig);
1211 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1213 * If traced, always stop, and stay
1214 * stopped until released by the parent.
1217 psignal(p->p_pptr, SIGCHLD);
1221 } while (!trace_req(p) && p->p_flag & P_TRACED);
1224 * If parent wants us to take the signal,
1225 * then it will leave it in p->p_xstat;
1226 * otherwise we just look for signals again.
1228 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1234 * Put the new signal into p_siglist. If the
1235 * signal is being masked, look for other signals.
1237 SIGADDSET(p->p_siglist, sig);
1238 if (SIGISMEMBER(p->p_sigmask, sig))
1242 * If the traced bit got turned off, go back up
1243 * to the top to rescan signals. This ensures
1244 * that p_sig* and ps_sigact are consistent.
1246 if ((p->p_flag & P_TRACED) == 0)
1250 prop = sigprop(sig);
1253 * Decide whether the signal should be returned.
1254 * Return the signal's number, or fall through
1255 * to clear it from the pending mask.
1257 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1261 * Don't take default actions on system processes.
1263 if (p->p_pid <= 1) {
1266 * Are you sure you want to ignore SIGSEGV
1269 printf("Process (pid %lu) got signal %d\n",
1270 (u_long)p->p_pid, sig);
1272 break; /* == ignore */
1275 * If there is a pending stop signal to process
1276 * with default action, stop here,
1277 * then clear the signal. However,
1278 * if process is member of an orphaned
1279 * process group, ignore tty stop signals.
1281 if (prop & SA_STOP) {
1282 if (p->p_flag & P_TRACED ||
1283 (p->p_pgrp->pg_jobc == 0 &&
1285 break; /* == ignore */
1288 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1289 psignal(p->p_pptr, SIGCHLD);
1292 } else if (prop & SA_IGNORE) {
1294 * Except for SIGCONT, shouldn't get here.
1295 * Default action is to ignore; drop it.
1297 break; /* == ignore */
1304 * Masking above should prevent us ever trying
1305 * to take action on an ignored signal other
1306 * than SIGCONT, unless process is traced.
1308 if ((prop & SA_CONT) == 0 &&
1309 (p->p_flag & P_TRACED) == 0)
1310 printf("issignal\n");
1311 break; /* == ignore */
1315 * This signal has an action, let
1316 * postsig() process it.
1320 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1326 * Put the argument process into the stopped state and notify the parent
1327 * via wakeup. Signals are handled elsewhere. The process must not be
1332 register struct proc *p;
1336 p->p_flag &= ~P_WAITED;
1337 wakeup((caddr_t)p->p_pptr);
1341 * Take the action for the specified signal
1342 * from the current set of pending signals.
1348 struct proc *p = curproc;
1349 struct sigacts *ps = p->p_sigacts;
1351 sigset_t returnmask;
1354 KASSERT(sig != 0, ("postsig"));
1356 SIGDELSET(p->p_siglist, sig);
1357 action = ps->ps_sigact[_SIG_IDX(sig)];
1359 if (KTRPOINT(p->p_thread, KTR_PSIG))
1360 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1361 &p->p_oldsigmask : &p->p_sigmask, 0);
1363 STOPEVENT(p, S_SIG, sig);
1365 if (action == SIG_DFL) {
1367 * Default action, where the default is to kill
1368 * the process. (Other cases were ignored above.)
1374 * If we get here, the signal must be caught.
1376 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1377 ("postsig action"));
1379 * Set the new mask value and also defer further
1380 * occurrences of this signal.
1382 * Special case: user has done a sigsuspend. Here the
1383 * current mask is not of interest, but rather the
1384 * mask from before the sigsuspend is what we want
1385 * restored after the signal processing is completed.
1388 if (p->p_flag & P_OLDMASK) {
1389 returnmask = p->p_oldsigmask;
1390 p->p_flag &= ~P_OLDMASK;
1392 returnmask = p->p_sigmask;
1394 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1395 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1396 SIGADDSET(p->p_sigmask, sig);
1398 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1400 * See do_sigaction() for origin of this code.
1402 SIGDELSET(p->p_sigcatch, sig);
1403 if (sig != SIGCONT &&
1404 sigprop(sig) & SA_IGNORE)
1405 SIGADDSET(p->p_sigignore, sig);
1406 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1409 p->p_stats->p_ru.ru_nsignals++;
1410 if (p->p_sig != sig) {
1417 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1422 * Kill the current process for stated reason.
1429 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1430 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1431 psignal(p, SIGKILL);
1435 * Force the current process to exit with the specified signal, dumping core
1436 * if appropriate. We bypass the normal tests for masked and caught signals,
1437 * allowing unrecoverable failures to terminate the process without changing
1438 * signal state. Mark the accounting record with the signal termination.
1439 * If dumping core, save the signal number for the debugger. Calls exit and
1443 sigexit(struct proc *p, int sig)
1445 p->p_acflag |= AXSIG;
1446 if (sigprop(sig) & SA_CORE) {
1449 * Log signals which would cause core dumps
1450 * (Log as LOG_INFO to appease those who don't want
1452 * XXX : Todo, as well as euid, write out ruid too
1454 if (coredump(p) == 0)
1456 if (kern_logsigexit)
1458 "pid %d (%s), uid %d: exited on signal %d%s\n",
1459 p->p_pid, p->p_comm,
1460 p->p_ucred ? p->p_ucred->cr_uid : -1,
1462 sig & WCOREFLAG ? " (core dumped)" : "");
1464 exit1(W_EXITCODE(0, sig));
1468 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1469 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1470 sizeof(corefilename), "process corefile name format string");
1473 * expand_name(name, uid, pid)
1474 * Expand the name described in corefilename, using name, uid, and pid.
1475 * corefilename is a printf-like string, with three format specifiers:
1476 * %N name of process ("name")
1477 * %P process id (pid)
1479 * For example, "%N.core" is the default; they can be disabled completely
1480 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1481 * This is controlled by the sysctl variable kern.corefile (see above).
1485 expand_name(name, uid, pid)
1486 const char *name; uid_t uid; pid_t pid; {
1488 char buf[11]; /* Buffer for pid/uid -- max 4B */
1490 char *format = corefilename;
1493 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1496 namelen = strlen(name);
1497 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1499 switch (format[i]) {
1500 case '%': /* Format character */
1502 switch (format[i]) {
1506 case 'N': /* process name */
1507 if ((n + namelen) > MAXPATHLEN) {
1508 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1509 pid, name, uid, temp, name);
1513 memcpy(temp+n, name, namelen);
1516 case 'P': /* process id */
1517 l = sprintf(buf, "%u", pid);
1518 if ((n + l) > MAXPATHLEN) {
1519 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1520 pid, name, uid, temp, name);
1524 memcpy(temp+n, buf, l);
1527 case 'U': /* user id */
1528 l = sprintf(buf, "%u", uid);
1529 if ((n + l) > MAXPATHLEN) {
1530 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1531 pid, name, uid, temp, name);
1535 memcpy(temp+n, buf, l);
1539 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1543 temp[n++] = format[i];
1551 * Dump a process' core. The main routine does some
1552 * policy checking, and creates the name of the coredump;
1553 * then it passes on a vnode and a size limit to the process-specific
1554 * coredump routine if there is one; if there _is not_ one, it returns
1555 * ENOSYS; otherwise it returns the error from the process-specific routine.
1559 coredump(struct proc *p)
1562 struct ucred *cred = p->p_ucred;
1563 struct thread *td = p->p_thread;
1565 struct nameidata nd;
1568 char *name; /* name of corefile */
1571 STOPEVENT(p, S_CORE, 0);
1573 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1577 * Note that the bulk of limit checking is done after
1578 * the corefile is created. The exception is if the limit
1579 * for corefiles is 0, in which case we don't bother
1580 * creating the corefile at all. This layout means that
1581 * a corefile is truncated instead of not being created,
1582 * if it is larger than the limit.
1584 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1588 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1591 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
1592 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1596 NDFREE(&nd, NDF_ONLY_PNBUF);
1599 VOP_UNLOCK(vp, 0, td);
1600 lf.l_whence = SEEK_SET;
1603 lf.l_type = F_WRLCK;
1604 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1608 /* Don't dump to non-regular files or files with links. */
1609 if (vp->v_type != VREG ||
1610 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1616 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1618 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1619 VOP_SETATTR(vp, &vattr, cred, td);
1620 p->p_acflag |= ACORE;
1621 VOP_UNLOCK(vp, 0, td);
1623 error = p->p_sysent->sv_coredump ?
1624 p->p_sysent->sv_coredump(p, vp, limit) :
1628 lf.l_type = F_UNLCK;
1629 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1631 error1 = vn_close(vp, FWRITE, td);
1638 * Nonexistent system call-- signal process (may want to handle it).
1639 * Flag error in case process won't see signal immediately (blocked or ignored).
1641 #ifndef _SYS_SYSPROTO_H_
1648 nosys(struct nosys_args *args)
1650 psignal(curproc, SIGSYS);
1655 * Send a SIGIO or SIGURG signal to a process or process group using
1656 * stored credentials rather than those of the current process.
1659 pgsigio(struct sigio *sigio, int sig, int checkctty)
1664 if (sigio->sio_pgid > 0) {
1665 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1667 psignal(sigio->sio_proc, sig);
1668 } else if (sigio->sio_pgid < 0) {
1671 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1672 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1673 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1679 filt_sigattach(struct knote *kn)
1681 struct proc *p = curproc;
1683 kn->kn_ptr.p_proc = p;
1684 kn->kn_flags |= EV_CLEAR; /* automatically set */
1686 /* XXX lock the proc here while adding to the list? */
1687 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1693 filt_sigdetach(struct knote *kn)
1695 struct proc *p = kn->kn_ptr.p_proc;
1697 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1701 * signal knotes are shared with proc knotes, so we apply a mask to
1702 * the hint in order to differentiate them from process hints. This
1703 * could be avoided by using a signal-specific knote list, but probably
1704 * isn't worth the trouble.
1707 filt_signal(struct knote *kn, long hint)
1710 if (hint & NOTE_SIGNAL) {
1711 hint &= ~NOTE_SIGNAL;
1713 if (kn->kn_id == hint)
1716 return (kn->kn_data != 0);