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.22 2003/10/13 21:16:42 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 (struct proc *);
77 static int do_sigaction (int sig, struct sigaction *act,
78 struct sigaction *oact, int old);
79 static int do_sigprocmask (int how, sigset_t *set,
80 sigset_t *oset, int old);
81 static char *expand_name (const char *, uid_t, pid_t);
82 static int killpg1 (int sig, int pgid, int all);
83 static int sig_ffs (sigset_t *set);
84 static int sigprop (int sig);
85 static void stop (struct proc *);
87 static void signotify_remote(void *arg);
90 static int filt_sigattach(struct knote *kn);
91 static void filt_sigdetach(struct knote *kn);
92 static int filt_signal(struct knote *kn, long hint);
94 struct filterops sig_filtops =
95 { 0, filt_sigattach, filt_sigdetach, filt_signal };
97 static int kern_logsigexit = 1;
98 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
100 "Log processes quitting on abnormal signals to syslog(3)");
103 * Can process p, with pcred pc, send the signal sig to process q?
105 #define CANSIGNAL(q, sig) \
106 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
107 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
110 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
112 #define CANSIGIO(ruid, uc, q) \
113 ((uc)->cr_uid == 0 || \
114 (ruid) == (q)->p_ucred->cr_ruid || \
115 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
116 (ruid) == (q)->p_ucred->cr_uid || \
117 (uc)->cr_uid == (q)->p_ucred->cr_uid)
120 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
121 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
123 static int do_coredump = 1;
124 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
125 &do_coredump, 0, "Enable/Disable coredumps");
128 * Signal properties and actions.
129 * The array below categorizes the signals and their default actions
130 * according to the following properties:
132 #define SA_KILL 0x01 /* terminates process by default */
133 #define SA_CORE 0x02 /* ditto and coredumps */
134 #define SA_STOP 0x04 /* suspend process */
135 #define SA_TTYSTOP 0x08 /* ditto, from tty */
136 #define SA_IGNORE 0x10 /* ignore by default */
137 #define SA_CONT 0x20 /* continue if suspended */
138 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
139 #define SA_CKPT 0x80 /* checkpoint process */
142 static int sigproptbl[NSIG] = {
143 SA_KILL, /* SIGHUP */
144 SA_KILL, /* SIGINT */
145 SA_KILL|SA_CORE, /* SIGQUIT */
146 SA_KILL|SA_CORE, /* SIGILL */
147 SA_KILL|SA_CORE, /* SIGTRAP */
148 SA_KILL|SA_CORE, /* SIGABRT */
149 SA_KILL|SA_CORE, /* SIGEMT */
150 SA_KILL|SA_CORE, /* SIGFPE */
151 SA_KILL, /* SIGKILL */
152 SA_KILL|SA_CORE, /* SIGBUS */
153 SA_KILL|SA_CORE, /* SIGSEGV */
154 SA_KILL|SA_CORE, /* SIGSYS */
155 SA_KILL, /* SIGPIPE */
156 SA_KILL, /* SIGALRM */
157 SA_KILL, /* SIGTERM */
158 SA_IGNORE, /* SIGURG */
159 SA_STOP, /* SIGSTOP */
160 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
161 SA_IGNORE|SA_CONT, /* SIGCONT */
162 SA_IGNORE, /* SIGCHLD */
163 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
164 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
165 SA_IGNORE, /* SIGIO */
166 SA_KILL, /* SIGXCPU */
167 SA_KILL, /* SIGXFSZ */
168 SA_KILL, /* SIGVTALRM */
169 SA_KILL, /* SIGPROF */
170 SA_IGNORE, /* SIGWINCH */
171 SA_IGNORE, /* SIGINFO */
172 SA_KILL, /* SIGUSR1 */
173 SA_KILL, /* SIGUSR2 */
174 SA_IGNORE, /* SIGTHR */
175 SA_CKPT, /* SIGCKPT */
176 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */
214 if (sig > 0 && sig < NSIG)
215 return (sigproptbl[_SIG_IDX(sig)]);
220 sig_ffs(sigset_t *set)
224 for (i = 0; i < _SIG_WORDS; i++)
226 return (ffs(set->__bits[i]) + (i * 32));
236 do_sigaction(int sig, struct sigaction *act, struct sigaction *oact, int old)
238 struct proc *p = curproc;
239 struct sigacts *ps = p->p_sigacts;
241 if (sig <= 0 || sig > _SIG_MAXSIG)
245 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
246 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
248 if (SIGISMEMBER(ps->ps_sigonstack, sig))
249 oact->sa_flags |= SA_ONSTACK;
250 if (!SIGISMEMBER(ps->ps_sigintr, sig))
251 oact->sa_flags |= SA_RESTART;
252 if (SIGISMEMBER(ps->ps_sigreset, sig))
253 oact->sa_flags |= SA_RESETHAND;
254 if (SIGISMEMBER(ps->ps_signodefer, sig))
255 oact->sa_flags |= SA_NODEFER;
256 if (SIGISMEMBER(ps->ps_siginfo, sig))
257 oact->sa_flags |= SA_SIGINFO;
258 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
259 oact->sa_flags |= SA_NOCLDSTOP;
260 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
261 oact->sa_flags |= SA_NOCLDWAIT;
264 if ((sig == SIGKILL || sig == SIGSTOP) &&
265 act->sa_handler != SIG_DFL)
269 * Change setting atomically.
273 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
274 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
275 if (act->sa_flags & SA_SIGINFO) {
276 ps->ps_sigact[_SIG_IDX(sig)] =
277 (__sighandler_t *)act->sa_sigaction;
278 SIGADDSET(ps->ps_siginfo, sig);
280 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
281 SIGDELSET(ps->ps_siginfo, sig);
283 if (!(act->sa_flags & SA_RESTART))
284 SIGADDSET(ps->ps_sigintr, sig);
286 SIGDELSET(ps->ps_sigintr, sig);
287 if (act->sa_flags & SA_ONSTACK)
288 SIGADDSET(ps->ps_sigonstack, sig);
290 SIGDELSET(ps->ps_sigonstack, sig);
291 if (act->sa_flags & SA_RESETHAND)
292 SIGADDSET(ps->ps_sigreset, sig);
294 SIGDELSET(ps->ps_sigreset, sig);
295 if (act->sa_flags & SA_NODEFER)
296 SIGADDSET(ps->ps_signodefer, sig);
298 SIGDELSET(ps->ps_signodefer, sig);
300 if (act->sa_flags & SA_USERTRAMP)
301 SIGADDSET(ps->ps_usertramp, sig);
303 SIGDELSET(ps->ps_usertramp, seg);
305 if (sig == SIGCHLD) {
306 if (act->sa_flags & SA_NOCLDSTOP)
307 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
309 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
310 if (act->sa_flags & SA_NOCLDWAIT) {
312 * Paranoia: since SA_NOCLDWAIT is implemented
313 * by reparenting the dying child to PID 1 (and
314 * trust it to reap the zombie), PID 1 itself
315 * is forbidden to set SA_NOCLDWAIT.
318 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
320 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
322 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
325 * Set bit in p_sigignore for signals that are set to SIG_IGN,
326 * and for signals set to SIG_DFL where the default is to
327 * ignore. However, don't put SIGCONT in p_sigignore, as we
328 * have to restart the process.
330 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
331 (sigprop(sig) & SA_IGNORE &&
332 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
333 /* never to be seen again */
334 SIGDELSET(p->p_siglist, sig);
336 /* easier in psignal */
337 SIGADDSET(p->p_sigignore, sig);
338 SIGDELSET(p->p_sigcatch, sig);
340 SIGDELSET(p->p_sigignore, sig);
341 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
342 SIGDELSET(p->p_sigcatch, sig);
344 SIGADDSET(p->p_sigcatch, sig);
346 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
347 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
348 SIGDELSET(ps->ps_osigset, sig);
350 SIGADDSET(ps->ps_osigset, sig);
359 sigaction(struct sigaction_args *uap)
361 struct sigaction act, oact;
362 struct sigaction *actp, *oactp;
365 actp = (uap->act != NULL) ? &act : NULL;
366 oactp = (uap->oact != NULL) ? &oact : NULL;
368 error = copyin(uap->act, actp, sizeof(act));
372 error = do_sigaction(uap->sig, actp, oactp, 0);
373 if (oactp && !error) {
374 error = copyout(oactp, uap->oact, sizeof(oact));
381 osigaction(struct osigaction_args *uap)
383 struct osigaction sa;
384 struct sigaction nsa, osa;
385 struct sigaction *nsap, *osap;
388 if (uap->signum <= 0 || uap->signum >= ONSIG)
390 nsap = (uap->nsa != NULL) ? &nsa : NULL;
391 osap = (uap->osa != NULL) ? &osa : NULL;
393 error = copyin(uap->nsa, &sa, sizeof(sa));
396 nsap->sa_handler = sa.sa_handler;
397 nsap->sa_flags = sa.sa_flags;
398 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
400 error = do_sigaction(uap->signum, nsap, osap, 1);
401 if (osap && !error) {
402 sa.sa_handler = osap->sa_handler;
403 sa.sa_flags = osap->sa_flags;
404 SIG2OSIG(osap->sa_mask, sa.sa_mask);
405 error = copyout(&sa, uap->osa, sizeof(sa));
411 * Initialize signal state for process 0;
412 * set to ignore signals that are ignored by default.
420 for (i = 1; i <= NSIG; i++)
421 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
422 SIGADDSET(p->p_sigignore, i);
426 * Reset signals for an exec of the specified process.
432 struct sigacts *ps = p->p_sigacts;
436 * Reset caught signals. Held signals remain held
437 * through p_sigmask (unless they were caught,
438 * and are now ignored by default).
440 while (SIGNOTEMPTY(p->p_sigcatch)) {
441 sig = sig_ffs(&p->p_sigcatch);
442 SIGDELSET(p->p_sigcatch, sig);
443 if (sigprop(sig) & SA_IGNORE) {
445 SIGADDSET(p->p_sigignore, sig);
446 SIGDELSET(p->p_siglist, sig);
448 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
451 * Reset stack state to the user stack.
452 * Clear set of signals caught on the signal stack.
454 p->p_sigstk.ss_flags = SS_DISABLE;
455 p->p_sigstk.ss_size = 0;
456 p->p_sigstk.ss_sp = 0;
457 p->p_flag &= ~P_ALTSTACK;
459 * Reset no zombies if child dies flag as Solaris does.
461 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
465 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
467 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
468 * p == curproc. Also remember that in order to remain MP SAFE
469 * no spl*() calls may be made.
472 do_sigprocmask(int how, sigset_t *set, sigset_t *oset, int old)
474 struct proc *p = curproc;
478 *oset = p->p_sigmask;
485 SIGSETOR(p->p_sigmask, *set);
488 SIGSETNAND(p->p_sigmask, *set);
493 SIGSETLO(p->p_sigmask, *set);
506 * sigprocmask() - MP SAFE
509 sigprocmask(struct sigprocmask_args *uap)
512 sigset_t *setp, *osetp;
515 setp = (uap->set != NULL) ? &set : NULL;
516 osetp = (uap->oset != NULL) ? &oset : NULL;
518 error = copyin(uap->set, setp, sizeof(set));
522 error = do_sigprocmask(uap->how, setp, osetp, 0);
523 if (osetp && !error) {
524 error = copyout(osetp, uap->oset, sizeof(oset));
530 * osigprocmask() - MP SAFE
533 osigprocmask(struct osigprocmask_args *uap)
538 OSIG2SIG(uap->mask, set);
539 error = do_sigprocmask(uap->how, &set, &oset, 1);
540 SIG2OSIG(oset, uap->sysmsg_result);
546 sigpending(struct sigpending_args *uap)
548 struct proc *p = curproc;
550 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
555 osigpending(struct osigpending_args *uap)
557 struct proc *p = curproc;
559 SIG2OSIG(p->p_siglist, uap->sysmsg_result);
563 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
565 * Generalized interface signal handler, 4.3-compatible.
569 osigvec(struct osigvec_args *uap)
572 struct sigaction nsa, osa;
573 struct sigaction *nsap, *osap;
576 if (uap->signum <= 0 || uap->signum >= ONSIG)
578 nsap = (uap->nsv != NULL) ? &nsa : NULL;
579 osap = (uap->osv != NULL) ? &osa : NULL;
581 error = copyin(uap->nsv, &vec, sizeof(vec));
584 nsap->sa_handler = vec.sv_handler;
585 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
586 nsap->sa_flags = vec.sv_flags;
587 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
589 nsap->sa_flags |= SA_USERTRAMP;
592 error = do_sigaction(uap->signum, nsap, osap, 1);
593 if (osap && !error) {
594 vec.sv_handler = osap->sa_handler;
595 SIG2OSIG(osap->sa_mask, vec.sv_mask);
596 vec.sv_flags = osap->sa_flags;
597 vec.sv_flags &= ~SA_NOCLDWAIT;
598 vec.sv_flags ^= SA_RESTART;
600 vec.sv_flags &= ~SA_NOCLDSTOP;
602 error = copyout(&vec, uap->osv, sizeof(vec));
608 osigblock(struct osigblock_args *uap)
610 struct proc *p = curproc;
613 OSIG2SIG(uap->mask, set);
616 SIG2OSIG(p->p_sigmask, uap->sysmsg_result);
617 SIGSETOR(p->p_sigmask, set);
623 osigsetmask(struct osigsetmask_args *uap)
625 struct proc *p = curproc;
628 OSIG2SIG(uap->mask, set);
631 SIG2OSIG(p->p_sigmask, uap->sysmsg_result);
632 SIGSETLO(p->p_sigmask, set);
636 #endif /* COMPAT_43 || COMPAT_SUNOS */
639 * Suspend process until signal, providing mask to be set
640 * in the meantime. Note nonstandard calling convention:
641 * libc stub passes mask, not pointer, to save a copyin.
645 sigsuspend(struct sigsuspend_args *uap)
647 struct proc *p = curproc;
649 struct sigacts *ps = p->p_sigacts;
652 error = copyin(uap->sigmask, &mask, sizeof(mask));
657 * When returning from sigsuspend, we want
658 * the old mask to be restored after the
659 * signal handler has finished. Thus, we
660 * save it here and mark the sigacts structure
663 p->p_oldsigmask = p->p_sigmask;
664 p->p_flag |= P_OLDMASK;
668 while (tsleep((caddr_t) ps, PCATCH, "pause", 0) == 0)
670 /* always return EINTR rather than ERESTART... */
676 osigsuspend(struct osigsuspend_args *uap)
679 struct proc *p = curproc;
680 struct sigacts *ps = p->p_sigacts;
682 p->p_oldsigmask = p->p_sigmask;
683 p->p_flag |= P_OLDMASK;
684 OSIG2SIG(uap->mask, mask);
686 SIGSETLO(p->p_sigmask, mask);
687 while (tsleep((caddr_t) ps, PCATCH, "opause", 0) == 0)
689 /* always return EINTR rather than ERESTART... */
693 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
696 osigstack(struct osigstack_args *uap)
698 struct proc *p = curproc;
702 ss.ss_sp = p->p_sigstk.ss_sp;
703 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
704 if (uap->oss && (error = copyout(&ss, uap->oss,
705 sizeof(struct sigstack))))
707 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
708 p->p_sigstk.ss_sp = ss.ss_sp;
709 p->p_sigstk.ss_size = 0;
710 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
711 p->p_flag |= P_ALTSTACK;
715 #endif /* COMPAT_43 || COMPAT_SUNOS */
719 sigaltstack(struct sigaltstack_args *uap)
721 struct proc *p = curproc;
725 if ((p->p_flag & P_ALTSTACK) == 0)
726 p->p_sigstk.ss_flags |= SS_DISABLE;
727 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
732 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
734 if (ss.ss_flags & SS_DISABLE) {
735 if (p->p_sigstk.ss_flags & SS_ONSTACK)
737 p->p_flag &= ~P_ALTSTACK;
738 p->p_sigstk.ss_flags = ss.ss_flags;
741 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
743 p->p_flag |= P_ALTSTACK;
749 * Common code for kill process group/broadcast kill.
750 * cp is calling process.
753 killpg1(int sig, int pgid, int all)
755 struct proc *cp = curproc;
764 FOREACH_PROC_IN_SYSTEM(p) {
765 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
766 p == cp || !CANSIGNAL(p, sig))
775 * zero pgid means send to my process group.
783 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
784 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
785 p->p_stat == SZOMB ||
793 return (nfound ? 0 : ESRCH);
798 kill(struct kill_args *uap)
802 if ((u_int)uap->signum > _SIG_MAXSIG)
805 /* kill single process */
806 if ((p = pfind(uap->pid)) == NULL)
808 if (!CANSIGNAL(p, uap->signum))
811 psignal(p, uap->signum);
815 case -1: /* broadcast signal */
816 return (killpg1(uap->signum, 0, 1));
817 case 0: /* signal own process group */
818 return (killpg1(uap->signum, 0, 0));
819 default: /* negative explicit process group */
820 return (killpg1(uap->signum, -uap->pid, 0));
825 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
828 okillpg(struct okillpg_args *uap)
830 if ((u_int)uap->signum > _SIG_MAXSIG)
832 return (killpg1(uap->signum, uap->pgid, 0));
834 #endif /* COMPAT_43 || COMPAT_SUNOS */
837 * Send a signal to a process group.
840 gsignal(int pgid, int sig)
844 if (pgid && (pgrp = pgfind(pgid)))
845 pgsignal(pgrp, sig, 0);
849 * Send a signal to a process group. If checktty is 1,
850 * limit to members which have a controlling terminal.
853 pgsignal(pgrp, sig, checkctty)
860 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
861 if (checkctty == 0 || p->p_flag & P_CONTROLT)
866 * Send a signal caused by a trap to the current process.
867 * If it will be caught immediately, deliver it with correct code.
868 * Otherwise, post it normally.
871 trapsignal(p, sig, code)
876 struct sigacts *ps = p->p_sigacts;
878 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
879 !SIGISMEMBER(p->p_sigmask, sig)) {
880 p->p_stats->p_ru.ru_nsignals++;
882 if (KTRPOINT(p->p_thread, KTR_PSIG))
883 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
884 &p->p_sigmask, code);
886 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
887 &p->p_sigmask, code);
888 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
889 if (!SIGISMEMBER(ps->ps_signodefer, sig))
890 SIGADDSET(p->p_sigmask, sig);
891 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
893 * See do_sigaction() for origin of this code.
895 SIGDELSET(p->p_sigcatch, sig);
896 if (sig != SIGCONT &&
897 sigprop(sig) & SA_IGNORE)
898 SIGADDSET(p->p_sigignore, sig);
899 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
902 p->p_code = code; /* XXX for core dump/debugger */
903 p->p_sig = sig; /* XXX to verify code */
909 * Send the signal to the process. If the signal has an action, the action
910 * is usually performed by the target process rather than the caller; we add
911 * the signal to the set of pending signals for the process.
914 * o When a stop signal is sent to a sleeping process that takes the
915 * default action, the process is stopped without awakening it.
916 * o SIGCONT restarts stopped processes (or puts them back to sleep)
917 * regardless of the signal action (eg, blocked or ignored).
919 * Other ignored signals are discarded immediately.
923 * temporary hack to allow checkpoint code to continue to
924 * be in a module for the moment
927 static proc_func_t ckpt_func;
930 register_ckpt_func(proc_func_t func)
932 proc_func_t old_func;
934 old_func = ckpt_func;
947 if (sig > _SIG_MAXSIG || sig <= 0) {
948 printf("psignal: signal %d\n", sig);
949 panic("psignal signal number");
953 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
959 * If proc is traced, always give parent a chance;
960 * if signal event is tracked by procfs, give *that*
963 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
967 * If the signal is being ignored,
968 * then we forget about it immediately.
969 * (Note: we don't set SIGCONT in p_sigignore,
970 * and if it is set to SIG_IGN,
971 * action will be SIG_DFL here.)
973 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
975 if (SIGISMEMBER(p->p_sigmask, sig))
977 else if (SIGISMEMBER(p->p_sigcatch, sig))
983 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
984 (p->p_flag & P_TRACED) == 0)
988 SIG_STOPSIGMASK(p->p_siglist);
991 if (prop & SA_STOP) {
993 * If sending a tty stop signal to a member of an orphaned
994 * process group, discard the signal here if the action
995 * is default; don't stop the process below if sleeping,
996 * and don't clear any pending SIGCONT.
998 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1001 SIG_CONTSIGMASK(p->p_siglist);
1003 SIGADDSET(p->p_siglist, sig);
1006 * Defer further processing for signals which are held,
1007 * except that stopped processes must be continued by SIGCONT.
1009 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1012 switch (p->p_stat) {
1016 * If process is sleeping uninterruptibly
1017 * we can't interrupt the sleep... the signal will
1018 * be noticed when the process returns through
1019 * trap() or syscall().
1021 if ((p->p_flag & P_SINTR) == 0)
1024 * Process is sleeping and traced... make it runnable
1025 * so it can discover the signal in issignal() and stop
1028 if (p->p_flag & P_TRACED)
1031 * If SIGCONT is default (or ignored) and process is
1032 * asleep, we are finished; the process should not
1035 if ((prop & SA_CONT) && action == SIG_DFL) {
1036 SIGDELSET(p->p_siglist, sig);
1040 * When a sleeping process receives a stop
1041 * signal, process immediately if possible.
1042 * All other (caught or default) signals
1043 * cause the process to run.
1045 if (prop & SA_STOP) {
1046 if (action != SIG_DFL)
1049 * If a child holding parent blocked,
1050 * stopping could cause deadlock.
1052 if (p->p_flag & P_PPWAIT)
1054 SIGDELSET(p->p_siglist, sig);
1056 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1057 psignal(p->p_pptr, SIGCHLD);
1066 * If traced process is already stopped,
1067 * then no further action is necessary.
1069 if (p->p_flag & P_TRACED)
1073 * Kill signal always sets processes running.
1078 if (prop & SA_CONT) {
1080 * If SIGCONT is default (or ignored), we continue the
1081 * process but don't leave the signal in p_siglist, as
1082 * it has no further action. If SIGCONT is held, we
1083 * continue the process and leave the signal in
1084 * p_siglist. If the process catches SIGCONT, let it
1085 * handle the signal itself. If it isn't waiting on
1086 * an event, then it goes back to run state.
1087 * Otherwise, process goes back to sleep state.
1089 if (action == SIG_DFL)
1090 SIGDELSET(p->p_siglist, sig);
1091 if (action == SIG_CATCH)
1093 if (p->p_wchan == 0)
1095 clrrunnable(p, SSLEEP);
1099 if (prop & SA_STOP) {
1101 * Already stopped, don't need to stop again.
1102 * (If we did the shell could get confused.)
1104 SIGDELSET(p->p_siglist, sig);
1109 * If process is sleeping interruptibly, then simulate a
1110 * wakeup so that when it is continued, it will be made
1111 * runnable and can look at the signal. But don't make
1112 * the process runnable, leave it stopped.
1114 if (p->p_wchan && (p->p_flag & P_SINTR))
1115 unsleep(p->p_thread);
1120 * SRUN, SIDL, SZOMB do nothing with the signal,
1121 * other than kicking ourselves if we are running.
1122 * It will either never be noticed, or noticed very soon.
1124 * For SMP we may have to forward the request to another cpu.
1125 * YYY the MP lock prevents the target process from moving
1126 * to another cpu, see kern/kern_switch.c
1129 if (p == lwkt_preempted_proc()) {
1132 struct thread *td = p->p_thread;
1134 if (td->td_gd != mycpu)
1135 lwkt_send_ipiq(td->td_gd->gd_cpuid, signotify_remote, p);
1138 if (p == lwkt_preempted_proc())
1153 * This function is called via an IPI. We will be in a critical section but
1154 * the MP lock will NOT be held. Also note that by the time the ipi message
1155 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1158 signotify_remote(void *arg)
1160 struct proc *p = arg;
1161 if (p == lwkt_preempted_proc())
1168 * If the current process has received a signal (should be caught or cause
1169 * termination, should interrupt current syscall), return the signal number.
1170 * Stop signals with default action are processed immediately, then cleared;
1171 * they aren't returned. This is checked after each entry to the system for
1172 * a syscall or trap (though this can usually be done without calling issignal
1173 * by checking the pending signal masks in the CURSIG macro.) The normal call
1176 * while (sig = CURSIG(curproc))
1187 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1189 mask = p->p_siglist;
1190 SIGSETNAND(mask, p->p_sigmask);
1191 if (p->p_flag & P_PPWAIT)
1192 SIG_STOPSIGMASK(mask);
1193 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1195 sig = sig_ffs(&mask);
1197 STOPEVENT(p, S_SIG, sig);
1200 * We should see pending but ignored signals
1201 * only if P_TRACED was on when they were posted.
1203 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1204 SIGDELSET(p->p_siglist, sig);
1207 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1209 * If traced, always stop, and stay
1210 * stopped until released by the parent.
1213 psignal(p->p_pptr, SIGCHLD);
1217 } while (!trace_req(p) && p->p_flag & P_TRACED);
1220 * If parent wants us to take the signal,
1221 * then it will leave it in p->p_xstat;
1222 * otherwise we just look for signals again.
1224 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1230 * Put the new signal into p_siglist. If the
1231 * signal is being masked, look for other signals.
1233 SIGADDSET(p->p_siglist, sig);
1234 if (SIGISMEMBER(p->p_sigmask, sig))
1238 * If the traced bit got turned off, go back up
1239 * to the top to rescan signals. This ensures
1240 * that p_sig* and ps_sigact are consistent.
1242 if ((p->p_flag & P_TRACED) == 0)
1246 prop = sigprop(sig);
1249 * Decide whether the signal should be returned.
1250 * Return the signal's number, or fall through
1251 * to clear it from the pending mask.
1253 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1257 * Don't take default actions on system processes.
1259 if (p->p_pid <= 1) {
1262 * Are you sure you want to ignore SIGSEGV
1265 printf("Process (pid %lu) got signal %d\n",
1266 (u_long)p->p_pid, sig);
1268 break; /* == ignore */
1272 * Handle the in-kernel checkpoint action
1274 if (prop & SA_CKPT) {
1281 * If there is a pending stop signal to process
1282 * with default action, stop here,
1283 * then clear the signal. However,
1284 * if process is member of an orphaned
1285 * process group, ignore tty stop signals.
1287 if (prop & SA_STOP) {
1288 if (p->p_flag & P_TRACED ||
1289 (p->p_pgrp->pg_jobc == 0 &&
1291 break; /* == ignore */
1294 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1295 psignal(p->p_pptr, SIGCHLD);
1298 } else if (prop & SA_IGNORE) {
1300 * Except for SIGCONT, shouldn't get here.
1301 * Default action is to ignore; drop it.
1303 break; /* == ignore */
1310 * Masking above should prevent us ever trying
1311 * to take action on an ignored signal other
1312 * than SIGCONT, unless process is traced.
1314 if ((prop & SA_CONT) == 0 &&
1315 (p->p_flag & P_TRACED) == 0)
1316 printf("issignal\n");
1317 break; /* == ignore */
1321 * This signal has an action, let
1322 * postsig() process it.
1326 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1332 * Put the argument process into the stopped state and notify the parent
1333 * via wakeup. Signals are handled elsewhere. The process must not be
1342 p->p_flag &= ~P_WAITED;
1343 wakeup((caddr_t)p->p_pptr);
1347 * Take the action for the specified signal
1348 * from the current set of pending signals.
1354 struct proc *p = curproc;
1355 struct sigacts *ps = p->p_sigacts;
1357 sigset_t returnmask;
1360 KASSERT(sig != 0, ("postsig"));
1362 SIGDELSET(p->p_siglist, sig);
1363 action = ps->ps_sigact[_SIG_IDX(sig)];
1365 if (KTRPOINT(p->p_thread, KTR_PSIG))
1366 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1367 &p->p_oldsigmask : &p->p_sigmask, 0);
1369 STOPEVENT(p, S_SIG, sig);
1371 if (action == SIG_DFL) {
1373 * Default action, where the default is to kill
1374 * the process. (Other cases were ignored above.)
1380 * If we get here, the signal must be caught.
1382 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1383 ("postsig action"));
1385 * Set the new mask value and also defer further
1386 * occurrences of this signal.
1388 * Special case: user has done a sigsuspend. Here the
1389 * current mask is not of interest, but rather the
1390 * mask from before the sigsuspend is what we want
1391 * restored after the signal processing is completed.
1394 if (p->p_flag & P_OLDMASK) {
1395 returnmask = p->p_oldsigmask;
1396 p->p_flag &= ~P_OLDMASK;
1398 returnmask = p->p_sigmask;
1400 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1401 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1402 SIGADDSET(p->p_sigmask, sig);
1404 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1406 * See do_sigaction() for origin of this code.
1408 SIGDELSET(p->p_sigcatch, sig);
1409 if (sig != SIGCONT &&
1410 sigprop(sig) & SA_IGNORE)
1411 SIGADDSET(p->p_sigignore, sig);
1412 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1415 p->p_stats->p_ru.ru_nsignals++;
1416 if (p->p_sig != sig) {
1423 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1428 * Kill the current process for stated reason.
1435 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1436 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1437 psignal(p, SIGKILL);
1441 * Force the current process to exit with the specified signal, dumping core
1442 * if appropriate. We bypass the normal tests for masked and caught signals,
1443 * allowing unrecoverable failures to terminate the process without changing
1444 * signal state. Mark the accounting record with the signal termination.
1445 * If dumping core, save the signal number for the debugger. Calls exit and
1449 sigexit(struct proc *p, int sig)
1451 p->p_acflag |= AXSIG;
1452 if (sigprop(sig) & SA_CORE) {
1455 * Log signals which would cause core dumps
1456 * (Log as LOG_INFO to appease those who don't want
1458 * XXX : Todo, as well as euid, write out ruid too
1460 if (coredump(p) == 0)
1462 if (kern_logsigexit)
1464 "pid %d (%s), uid %d: exited on signal %d%s\n",
1465 p->p_pid, p->p_comm,
1466 p->p_ucred ? p->p_ucred->cr_uid : -1,
1468 sig & WCOREFLAG ? " (core dumped)" : "");
1470 exit1(W_EXITCODE(0, sig));
1474 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1475 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1476 sizeof(corefilename), "process corefile name format string");
1479 * expand_name(name, uid, pid)
1480 * Expand the name described in corefilename, using name, uid, and pid.
1481 * corefilename is a printf-like string, with three format specifiers:
1482 * %N name of process ("name")
1483 * %P process id (pid)
1485 * For example, "%N.core" is the default; they can be disabled completely
1486 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1487 * This is controlled by the sysctl variable kern.corefile (see above).
1491 expand_name(name, uid, pid)
1492 const char *name; uid_t uid; pid_t pid; {
1494 char buf[11]; /* Buffer for pid/uid -- max 4B */
1496 char *format = corefilename;
1499 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1502 namelen = strlen(name);
1503 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1505 switch (format[i]) {
1506 case '%': /* Format character */
1508 switch (format[i]) {
1512 case 'N': /* process name */
1513 if ((n + namelen) > MAXPATHLEN) {
1514 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1515 pid, name, uid, temp, name);
1519 memcpy(temp+n, name, namelen);
1522 case 'P': /* process id */
1523 l = sprintf(buf, "%u", pid);
1524 if ((n + l) > MAXPATHLEN) {
1525 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1526 pid, name, uid, temp, name);
1530 memcpy(temp+n, buf, l);
1533 case 'U': /* user id */
1534 l = sprintf(buf, "%u", uid);
1535 if ((n + l) > MAXPATHLEN) {
1536 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1537 pid, name, uid, temp, name);
1541 memcpy(temp+n, buf, l);
1545 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1549 temp[n++] = format[i];
1557 * Dump a process' core. The main routine does some
1558 * policy checking, and creates the name of the coredump;
1559 * then it passes on a vnode and a size limit to the process-specific
1560 * coredump routine if there is one; if there _is not_ one, it returns
1561 * ENOSYS; otherwise it returns the error from the process-specific routine.
1565 coredump(struct proc *p)
1568 struct ucred *cred = p->p_ucred;
1569 struct thread *td = p->p_thread;
1571 struct nameidata nd;
1574 char *name; /* name of corefile */
1577 STOPEVENT(p, S_CORE, 0);
1579 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1583 * Note that the bulk of limit checking is done after
1584 * the corefile is created. The exception is if the limit
1585 * for corefiles is 0, in which case we don't bother
1586 * creating the corefile at all. This layout means that
1587 * a corefile is truncated instead of not being created,
1588 * if it is larger than the limit.
1590 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1594 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1597 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_SYSSPACE, name, td);
1598 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1602 NDFREE(&nd, NDF_ONLY_PNBUF);
1605 VOP_UNLOCK(vp, 0, td);
1606 lf.l_whence = SEEK_SET;
1609 lf.l_type = F_WRLCK;
1610 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1614 /* Don't dump to non-regular files or files with links. */
1615 if (vp->v_type != VREG ||
1616 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1622 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1624 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1625 VOP_SETATTR(vp, &vattr, cred, td);
1626 p->p_acflag |= ACORE;
1627 VOP_UNLOCK(vp, 0, td);
1629 error = p->p_sysent->sv_coredump ?
1630 p->p_sysent->sv_coredump(p, vp, limit) :
1634 lf.l_type = F_UNLCK;
1635 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1637 error1 = vn_close(vp, FWRITE, td);
1644 * Nonexistent system call-- signal process (may want to handle it).
1645 * Flag error in case process won't see signal immediately (blocked or ignored).
1649 nosys(struct nosys_args *args)
1651 psignal(curproc, SIGSYS);
1656 * Send a SIGIO or SIGURG signal to a process or process group using
1657 * stored credentials rather than those of the current process.
1660 pgsigio(struct sigio *sigio, int sig, int checkctty)
1665 if (sigio->sio_pgid > 0) {
1666 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1668 psignal(sigio->sio_proc, sig);
1669 } else if (sigio->sio_pgid < 0) {
1672 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1673 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1674 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1680 filt_sigattach(struct knote *kn)
1682 struct proc *p = curproc;
1684 kn->kn_ptr.p_proc = p;
1685 kn->kn_flags |= EV_CLEAR; /* automatically set */
1687 /* XXX lock the proc here while adding to the list? */
1688 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1694 filt_sigdetach(struct knote *kn)
1696 struct proc *p = kn->kn_ptr.p_proc;
1698 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1702 * signal knotes are shared with proc knotes, so we apply a mask to
1703 * the hint in order to differentiate them from process hints. This
1704 * could be avoided by using a signal-specific knote list, but probably
1705 * isn't worth the trouble.
1708 filt_signal(struct knote *kn, long hint)
1711 if (hint & NOTE_SIGNAL) {
1712 hint &= ~NOTE_SIGNAL;
1714 if (kn->kn_id == hint)
1717 return (kn->kn_data != 0);