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.18 2003/08/26 21:09:02 rob 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 */
140 static int sigproptbl[NSIG] = {
141 SA_KILL, /* SIGHUP */
142 SA_KILL, /* SIGINT */
143 SA_KILL|SA_CORE, /* SIGQUIT */
144 SA_KILL|SA_CORE, /* SIGILL */
145 SA_KILL|SA_CORE, /* SIGTRAP */
146 SA_KILL|SA_CORE, /* SIGABRT */
147 SA_KILL|SA_CORE, /* SIGEMT */
148 SA_KILL|SA_CORE, /* SIGFPE */
149 SA_KILL, /* SIGKILL */
150 SA_KILL|SA_CORE, /* SIGBUS */
151 SA_KILL|SA_CORE, /* SIGSEGV */
152 SA_KILL|SA_CORE, /* SIGSYS */
153 SA_KILL, /* SIGPIPE */
154 SA_KILL, /* SIGALRM */
155 SA_KILL, /* SIGTERM */
156 SA_IGNORE, /* SIGURG */
157 SA_STOP, /* SIGSTOP */
158 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
159 SA_IGNORE|SA_CONT, /* SIGCONT */
160 SA_IGNORE, /* SIGCHLD */
161 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
162 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
163 SA_IGNORE, /* SIGIO */
164 SA_KILL, /* SIGXCPU */
165 SA_KILL, /* SIGXFSZ */
166 SA_KILL, /* SIGVTALRM */
167 SA_KILL, /* SIGPROF */
168 SA_IGNORE, /* SIGWINCH */
169 SA_IGNORE, /* SIGINFO */
170 SA_KILL, /* SIGUSR1 */
171 SA_KILL, /* SIGUSR2 */
178 if (sig > 0 && sig < NSIG)
179 return (sigproptbl[_SIG_IDX(sig)]);
184 sig_ffs(sigset_t *set)
188 for (i = 0; i < _SIG_WORDS; i++)
190 return (ffs(set->__bits[i]) + (i * 32));
200 do_sigaction(int sig, struct sigaction *act, struct sigaction *oact, int old)
202 struct proc *p = curproc;
203 struct sigacts *ps = p->p_sigacts;
205 if (sig <= 0 || sig > _SIG_MAXSIG)
209 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
210 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
212 if (SIGISMEMBER(ps->ps_sigonstack, sig))
213 oact->sa_flags |= SA_ONSTACK;
214 if (!SIGISMEMBER(ps->ps_sigintr, sig))
215 oact->sa_flags |= SA_RESTART;
216 if (SIGISMEMBER(ps->ps_sigreset, sig))
217 oact->sa_flags |= SA_RESETHAND;
218 if (SIGISMEMBER(ps->ps_signodefer, sig))
219 oact->sa_flags |= SA_NODEFER;
220 if (SIGISMEMBER(ps->ps_siginfo, sig))
221 oact->sa_flags |= SA_SIGINFO;
222 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
223 oact->sa_flags |= SA_NOCLDSTOP;
224 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
225 oact->sa_flags |= SA_NOCLDWAIT;
228 if ((sig == SIGKILL || sig == SIGSTOP) &&
229 act->sa_handler != SIG_DFL)
233 * Change setting atomically.
237 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
238 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
239 if (act->sa_flags & SA_SIGINFO) {
240 ps->ps_sigact[_SIG_IDX(sig)] =
241 (__sighandler_t *)act->sa_sigaction;
242 SIGADDSET(ps->ps_siginfo, sig);
244 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
245 SIGDELSET(ps->ps_siginfo, sig);
247 if (!(act->sa_flags & SA_RESTART))
248 SIGADDSET(ps->ps_sigintr, sig);
250 SIGDELSET(ps->ps_sigintr, sig);
251 if (act->sa_flags & SA_ONSTACK)
252 SIGADDSET(ps->ps_sigonstack, sig);
254 SIGDELSET(ps->ps_sigonstack, sig);
255 if (act->sa_flags & SA_RESETHAND)
256 SIGADDSET(ps->ps_sigreset, sig);
258 SIGDELSET(ps->ps_sigreset, sig);
259 if (act->sa_flags & SA_NODEFER)
260 SIGADDSET(ps->ps_signodefer, sig);
262 SIGDELSET(ps->ps_signodefer, sig);
264 if (act->sa_flags & SA_USERTRAMP)
265 SIGADDSET(ps->ps_usertramp, sig);
267 SIGDELSET(ps->ps_usertramp, seg);
269 if (sig == SIGCHLD) {
270 if (act->sa_flags & SA_NOCLDSTOP)
271 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
273 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
274 if (act->sa_flags & SA_NOCLDWAIT) {
276 * Paranoia: since SA_NOCLDWAIT is implemented
277 * by reparenting the dying child to PID 1 (and
278 * trust it to reap the zombie), PID 1 itself
279 * is forbidden to set SA_NOCLDWAIT.
282 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
284 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
286 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
289 * Set bit in p_sigignore for signals that are set to SIG_IGN,
290 * and for signals set to SIG_DFL where the default is to
291 * ignore. However, don't put SIGCONT in p_sigignore, as we
292 * have to restart the process.
294 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
295 (sigprop(sig) & SA_IGNORE &&
296 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
297 /* never to be seen again */
298 SIGDELSET(p->p_siglist, sig);
300 /* easier in psignal */
301 SIGADDSET(p->p_sigignore, sig);
302 SIGDELSET(p->p_sigcatch, sig);
304 SIGDELSET(p->p_sigignore, sig);
305 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
306 SIGDELSET(p->p_sigcatch, sig);
308 SIGADDSET(p->p_sigcatch, sig);
310 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
311 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
312 SIGDELSET(ps->ps_osigset, sig);
314 SIGADDSET(ps->ps_osigset, sig);
323 sigaction(struct sigaction_args *uap)
325 struct sigaction act, oact;
326 struct sigaction *actp, *oactp;
329 actp = (uap->act != NULL) ? &act : NULL;
330 oactp = (uap->oact != NULL) ? &oact : NULL;
332 error = copyin(uap->act, actp, sizeof(act));
336 error = do_sigaction(uap->sig, actp, oactp, 0);
337 if (oactp && !error) {
338 error = copyout(oactp, uap->oact, sizeof(oact));
345 osigaction(struct osigaction_args *uap)
347 struct osigaction sa;
348 struct sigaction nsa, osa;
349 struct sigaction *nsap, *osap;
352 if (uap->signum <= 0 || uap->signum >= ONSIG)
354 nsap = (uap->nsa != NULL) ? &nsa : NULL;
355 osap = (uap->osa != NULL) ? &osa : NULL;
357 error = copyin(uap->nsa, &sa, sizeof(sa));
360 nsap->sa_handler = sa.sa_handler;
361 nsap->sa_flags = sa.sa_flags;
362 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
364 error = do_sigaction(uap->signum, nsap, osap, 1);
365 if (osap && !error) {
366 sa.sa_handler = osap->sa_handler;
367 sa.sa_flags = osap->sa_flags;
368 SIG2OSIG(osap->sa_mask, sa.sa_mask);
369 error = copyout(&sa, uap->osa, sizeof(sa));
375 * Initialize signal state for process 0;
376 * set to ignore signals that are ignored by default.
384 for (i = 1; i <= NSIG; i++)
385 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
386 SIGADDSET(p->p_sigignore, i);
390 * Reset signals for an exec of the specified process.
396 struct sigacts *ps = p->p_sigacts;
400 * Reset caught signals. Held signals remain held
401 * through p_sigmask (unless they were caught,
402 * and are now ignored by default).
404 while (SIGNOTEMPTY(p->p_sigcatch)) {
405 sig = sig_ffs(&p->p_sigcatch);
406 SIGDELSET(p->p_sigcatch, sig);
407 if (sigprop(sig) & SA_IGNORE) {
409 SIGADDSET(p->p_sigignore, sig);
410 SIGDELSET(p->p_siglist, sig);
412 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
415 * Reset stack state to the user stack.
416 * Clear set of signals caught on the signal stack.
418 p->p_sigstk.ss_flags = SS_DISABLE;
419 p->p_sigstk.ss_size = 0;
420 p->p_sigstk.ss_sp = 0;
421 p->p_flag &= ~P_ALTSTACK;
423 * Reset no zombies if child dies flag as Solaris does.
425 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
429 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
431 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
432 * p == curproc. Also remember that in order to remain MP SAFE
433 * no spl*() calls may be made.
436 do_sigprocmask(int how, sigset_t *set, sigset_t *oset, int old)
438 struct proc *p = curproc;
442 *oset = p->p_sigmask;
449 SIGSETOR(p->p_sigmask, *set);
452 SIGSETNAND(p->p_sigmask, *set);
457 SIGSETLO(p->p_sigmask, *set);
470 * sigprocmask() - MP SAFE
473 sigprocmask(struct sigprocmask_args *uap)
476 sigset_t *setp, *osetp;
479 setp = (uap->set != NULL) ? &set : NULL;
480 osetp = (uap->oset != NULL) ? &oset : NULL;
482 error = copyin(uap->set, setp, sizeof(set));
486 error = do_sigprocmask(uap->how, setp, osetp, 0);
487 if (osetp && !error) {
488 error = copyout(osetp, uap->oset, sizeof(oset));
494 * osigprocmask() - MP SAFE
497 osigprocmask(struct osigprocmask_args *uap)
502 OSIG2SIG(uap->mask, set);
503 error = do_sigprocmask(uap->how, &set, &oset, 1);
504 SIG2OSIG(oset, uap->sysmsg_result);
510 sigpending(struct sigpending_args *uap)
512 struct proc *p = curproc;
514 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
519 osigpending(struct osigpending_args *uap)
521 struct proc *p = curproc;
523 SIG2OSIG(p->p_siglist, uap->sysmsg_result);
527 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
529 * Generalized interface signal handler, 4.3-compatible.
533 osigvec(struct osigvec_args *uap)
536 struct sigaction nsa, osa;
537 struct sigaction *nsap, *osap;
540 if (uap->signum <= 0 || uap->signum >= ONSIG)
542 nsap = (uap->nsv != NULL) ? &nsa : NULL;
543 osap = (uap->osv != NULL) ? &osa : NULL;
545 error = copyin(uap->nsv, &vec, sizeof(vec));
548 nsap->sa_handler = vec.sv_handler;
549 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
550 nsap->sa_flags = vec.sv_flags;
551 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
553 nsap->sa_flags |= SA_USERTRAMP;
556 error = do_sigaction(uap->signum, nsap, osap, 1);
557 if (osap && !error) {
558 vec.sv_handler = osap->sa_handler;
559 SIG2OSIG(osap->sa_mask, vec.sv_mask);
560 vec.sv_flags = osap->sa_flags;
561 vec.sv_flags &= ~SA_NOCLDWAIT;
562 vec.sv_flags ^= SA_RESTART;
564 vec.sv_flags &= ~SA_NOCLDSTOP;
566 error = copyout(&vec, uap->osv, sizeof(vec));
572 osigblock(struct osigblock_args *uap)
574 struct proc *p = curproc;
577 OSIG2SIG(uap->mask, set);
580 SIG2OSIG(p->p_sigmask, uap->sysmsg_result);
581 SIGSETOR(p->p_sigmask, set);
587 osigsetmask(struct osigsetmask_args *uap)
589 struct proc *p = curproc;
592 OSIG2SIG(uap->mask, set);
595 SIG2OSIG(p->p_sigmask, uap->sysmsg_result);
596 SIGSETLO(p->p_sigmask, set);
600 #endif /* COMPAT_43 || COMPAT_SUNOS */
603 * Suspend process until signal, providing mask to be set
604 * in the meantime. Note nonstandard calling convention:
605 * libc stub passes mask, not pointer, to save a copyin.
609 sigsuspend(struct sigsuspend_args *uap)
611 struct proc *p = curproc;
613 struct sigacts *ps = p->p_sigacts;
616 error = copyin(uap->sigmask, &mask, sizeof(mask));
621 * When returning from sigsuspend, we want
622 * the old mask to be restored after the
623 * signal handler has finished. Thus, we
624 * save it here and mark the sigacts structure
627 p->p_oldsigmask = p->p_sigmask;
628 p->p_flag |= P_OLDMASK;
632 while (tsleep((caddr_t) ps, PCATCH, "pause", 0) == 0)
634 /* always return EINTR rather than ERESTART... */
640 osigsuspend(struct osigsuspend_args *uap)
643 struct proc *p = curproc;
644 struct sigacts *ps = p->p_sigacts;
646 p->p_oldsigmask = p->p_sigmask;
647 p->p_flag |= P_OLDMASK;
648 OSIG2SIG(uap->mask, mask);
650 SIGSETLO(p->p_sigmask, mask);
651 while (tsleep((caddr_t) ps, PCATCH, "opause", 0) == 0)
653 /* always return EINTR rather than ERESTART... */
657 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
660 osigstack(struct osigstack_args *uap)
662 struct proc *p = curproc;
666 ss.ss_sp = p->p_sigstk.ss_sp;
667 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
668 if (uap->oss && (error = copyout(&ss, uap->oss,
669 sizeof(struct sigstack))))
671 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
672 p->p_sigstk.ss_sp = ss.ss_sp;
673 p->p_sigstk.ss_size = 0;
674 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
675 p->p_flag |= P_ALTSTACK;
679 #endif /* COMPAT_43 || COMPAT_SUNOS */
683 sigaltstack(struct sigaltstack_args *uap)
685 struct proc *p = curproc;
689 if ((p->p_flag & P_ALTSTACK) == 0)
690 p->p_sigstk.ss_flags |= SS_DISABLE;
691 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
696 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
698 if (ss.ss_flags & SS_DISABLE) {
699 if (p->p_sigstk.ss_flags & SS_ONSTACK)
701 p->p_flag &= ~P_ALTSTACK;
702 p->p_sigstk.ss_flags = ss.ss_flags;
705 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
707 p->p_flag |= P_ALTSTACK;
713 * Common code for kill process group/broadcast kill.
714 * cp is calling process.
717 killpg1(int sig, int pgid, int all)
719 struct proc *cp = curproc;
728 FOREACH_PROC_IN_SYSTEM(p) {
729 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
730 p == cp || !CANSIGNAL(p, sig))
739 * zero pgid means send to my process group.
747 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
748 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
749 p->p_stat == SZOMB ||
757 return (nfound ? 0 : ESRCH);
762 kill(struct kill_args *uap)
766 if ((u_int)uap->signum > _SIG_MAXSIG)
769 /* kill single process */
770 if ((p = pfind(uap->pid)) == NULL)
772 if (!CANSIGNAL(p, uap->signum))
775 psignal(p, uap->signum);
779 case -1: /* broadcast signal */
780 return (killpg1(uap->signum, 0, 1));
781 case 0: /* signal own process group */
782 return (killpg1(uap->signum, 0, 0));
783 default: /* negative explicit process group */
784 return (killpg1(uap->signum, -uap->pid, 0));
789 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
792 okillpg(struct okillpg_args *uap)
794 if ((u_int)uap->signum > _SIG_MAXSIG)
796 return (killpg1(uap->signum, uap->pgid, 0));
798 #endif /* COMPAT_43 || COMPAT_SUNOS */
801 * Send a signal to a process group.
804 gsignal(int pgid, int sig)
808 if (pgid && (pgrp = pgfind(pgid)))
809 pgsignal(pgrp, sig, 0);
813 * Send a signal to a process group. If checktty is 1,
814 * limit to members which have a controlling terminal.
817 pgsignal(pgrp, sig, checkctty)
824 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
825 if (checkctty == 0 || p->p_flag & P_CONTROLT)
830 * Send a signal caused by a trap to the current process.
831 * If it will be caught immediately, deliver it with correct code.
832 * Otherwise, post it normally.
835 trapsignal(p, sig, code)
840 struct sigacts *ps = p->p_sigacts;
842 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
843 !SIGISMEMBER(p->p_sigmask, sig)) {
844 p->p_stats->p_ru.ru_nsignals++;
846 if (KTRPOINT(p->p_thread, KTR_PSIG))
847 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
848 &p->p_sigmask, code);
850 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
851 &p->p_sigmask, code);
852 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
853 if (!SIGISMEMBER(ps->ps_signodefer, sig))
854 SIGADDSET(p->p_sigmask, sig);
855 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
857 * See do_sigaction() for origin of this code.
859 SIGDELSET(p->p_sigcatch, sig);
860 if (sig != SIGCONT &&
861 sigprop(sig) & SA_IGNORE)
862 SIGADDSET(p->p_sigignore, sig);
863 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
866 p->p_code = code; /* XXX for core dump/debugger */
867 p->p_sig = sig; /* XXX to verify code */
873 * Send the signal to the process. If the signal has an action, the action
874 * is usually performed by the target process rather than the caller; we add
875 * the signal to the set of pending signals for the process.
878 * o When a stop signal is sent to a sleeping process that takes the
879 * default action, the process is stopped without awakening it.
880 * o SIGCONT restarts stopped processes (or puts them back to sleep)
881 * regardless of the signal action (eg, blocked or ignored).
883 * Other ignored signals are discarded immediately.
894 if (sig > _SIG_MAXSIG || sig <= 0) {
895 printf("psignal: signal %d\n", sig);
896 panic("psignal signal number");
900 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
906 * If proc is traced, always give parent a chance;
907 * if signal event is tracked by procfs, give *that*
910 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
914 * If the signal is being ignored,
915 * then we forget about it immediately.
916 * (Note: we don't set SIGCONT in p_sigignore,
917 * and if it is set to SIG_IGN,
918 * action will be SIG_DFL here.)
920 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
922 if (SIGISMEMBER(p->p_sigmask, sig))
924 else if (SIGISMEMBER(p->p_sigcatch, sig))
930 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
931 (p->p_flag & P_TRACED) == 0)
935 SIG_STOPSIGMASK(p->p_siglist);
937 if (prop & SA_STOP) {
939 * If sending a tty stop signal to a member of an orphaned
940 * process group, discard the signal here if the action
941 * is default; don't stop the process below if sleeping,
942 * and don't clear any pending SIGCONT.
944 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
947 SIG_CONTSIGMASK(p->p_siglist);
949 SIGADDSET(p->p_siglist, sig);
952 * Defer further processing for signals which are held,
953 * except that stopped processes must be continued by SIGCONT.
955 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
962 * If process is sleeping uninterruptibly
963 * we can't interrupt the sleep... the signal will
964 * be noticed when the process returns through
965 * trap() or syscall().
967 if ((p->p_flag & P_SINTR) == 0)
970 * Process is sleeping and traced... make it runnable
971 * so it can discover the signal in issignal() and stop
974 if (p->p_flag & P_TRACED)
977 * If SIGCONT is default (or ignored) and process is
978 * asleep, we are finished; the process should not
981 if ((prop & SA_CONT) && action == SIG_DFL) {
982 SIGDELSET(p->p_siglist, sig);
986 * When a sleeping process receives a stop
987 * signal, process immediately if possible.
988 * All other (caught or default) signals
989 * cause the process to run.
991 if (prop & SA_STOP) {
992 if (action != SIG_DFL)
995 * If a child holding parent blocked,
996 * stopping could cause deadlock.
998 if (p->p_flag & P_PPWAIT)
1000 SIGDELSET(p->p_siglist, sig);
1002 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1003 psignal(p->p_pptr, SIGCHLD);
1012 * If traced process is already stopped,
1013 * then no further action is necessary.
1015 if (p->p_flag & P_TRACED)
1019 * Kill signal always sets processes running.
1024 if (prop & SA_CONT) {
1026 * If SIGCONT is default (or ignored), we continue the
1027 * process but don't leave the signal in p_siglist, as
1028 * it has no further action. If SIGCONT is held, we
1029 * continue the process and leave the signal in
1030 * p_siglist. If the process catches SIGCONT, let it
1031 * handle the signal itself. If it isn't waiting on
1032 * an event, then it goes back to run state.
1033 * Otherwise, process goes back to sleep state.
1035 if (action == SIG_DFL)
1036 SIGDELSET(p->p_siglist, sig);
1037 if (action == SIG_CATCH)
1039 if (p->p_wchan == 0)
1041 clrrunnable(p, SSLEEP);
1045 if (prop & SA_STOP) {
1047 * Already stopped, don't need to stop again.
1048 * (If we did the shell could get confused.)
1050 SIGDELSET(p->p_siglist, sig);
1055 * If process is sleeping interruptibly, then simulate a
1056 * wakeup so that when it is continued, it will be made
1057 * runnable and can look at the signal. But don't make
1058 * the process runnable, leave it stopped.
1060 if (p->p_wchan && (p->p_flag & P_SINTR))
1061 unsleep(p->p_thread);
1066 * SRUN, SIDL, SZOMB do nothing with the signal,
1067 * other than kicking ourselves if we are running.
1068 * It will either never be noticed, or noticed very soon.
1070 * For SMP we may have to forward the request to another cpu.
1071 * YYY the MP lock prevents the target process from moving
1072 * to another cpu, see kern/kern_switch.c
1075 if (p == lwkt_preempted_proc()) {
1078 struct thread *td = p->p_thread;
1080 if (td->td_gd != mycpu)
1081 lwkt_send_ipiq(td->td_gd->gd_cpuid, signotify_remote, p);
1084 if (p == lwkt_preempted_proc())
1099 * This function is called via an IPI. We will be in a critical section but
1100 * the MP lock will NOT be held. Also note that by the time the ipi message
1101 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1104 signotify_remote(void *arg)
1106 struct proc *p = arg;
1107 if (p == lwkt_preempted_proc())
1114 * If the current process has received a signal (should be caught or cause
1115 * termination, should interrupt current syscall), return the signal number.
1116 * Stop signals with default action are processed immediately, then cleared;
1117 * they aren't returned. This is checked after each entry to the system for
1118 * a syscall or trap (though this can usually be done without calling issignal
1119 * by checking the pending signal masks in the CURSIG macro.) The normal call
1122 * while (sig = CURSIG(curproc))
1133 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1135 mask = p->p_siglist;
1136 SIGSETNAND(mask, p->p_sigmask);
1137 if (p->p_flag & P_PPWAIT)
1138 SIG_STOPSIGMASK(mask);
1139 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1141 sig = sig_ffs(&mask);
1143 STOPEVENT(p, S_SIG, sig);
1146 * We should see pending but ignored signals
1147 * only if P_TRACED was on when they were posted.
1149 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1150 SIGDELSET(p->p_siglist, sig);
1153 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1155 * If traced, always stop, and stay
1156 * stopped until released by the parent.
1159 psignal(p->p_pptr, SIGCHLD);
1163 } while (!trace_req(p) && p->p_flag & P_TRACED);
1166 * If parent wants us to take the signal,
1167 * then it will leave it in p->p_xstat;
1168 * otherwise we just look for signals again.
1170 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1176 * Put the new signal into p_siglist. If the
1177 * signal is being masked, look for other signals.
1179 SIGADDSET(p->p_siglist, sig);
1180 if (SIGISMEMBER(p->p_sigmask, sig))
1184 * If the traced bit got turned off, go back up
1185 * to the top to rescan signals. This ensures
1186 * that p_sig* and ps_sigact are consistent.
1188 if ((p->p_flag & P_TRACED) == 0)
1192 prop = sigprop(sig);
1195 * Decide whether the signal should be returned.
1196 * Return the signal's number, or fall through
1197 * to clear it from the pending mask.
1199 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1203 * Don't take default actions on system processes.
1205 if (p->p_pid <= 1) {
1208 * Are you sure you want to ignore SIGSEGV
1211 printf("Process (pid %lu) got signal %d\n",
1212 (u_long)p->p_pid, sig);
1214 break; /* == ignore */
1217 * If there is a pending stop signal to process
1218 * with default action, stop here,
1219 * then clear the signal. However,
1220 * if process is member of an orphaned
1221 * process group, ignore tty stop signals.
1223 if (prop & SA_STOP) {
1224 if (p->p_flag & P_TRACED ||
1225 (p->p_pgrp->pg_jobc == 0 &&
1227 break; /* == ignore */
1230 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1231 psignal(p->p_pptr, SIGCHLD);
1234 } else if (prop & SA_IGNORE) {
1236 * Except for SIGCONT, shouldn't get here.
1237 * Default action is to ignore; drop it.
1239 break; /* == ignore */
1246 * Masking above should prevent us ever trying
1247 * to take action on an ignored signal other
1248 * than SIGCONT, unless process is traced.
1250 if ((prop & SA_CONT) == 0 &&
1251 (p->p_flag & P_TRACED) == 0)
1252 printf("issignal\n");
1253 break; /* == ignore */
1257 * This signal has an action, let
1258 * postsig() process it.
1262 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1268 * Put the argument process into the stopped state and notify the parent
1269 * via wakeup. Signals are handled elsewhere. The process must not be
1278 p->p_flag &= ~P_WAITED;
1279 wakeup((caddr_t)p->p_pptr);
1283 * Take the action for the specified signal
1284 * from the current set of pending signals.
1290 struct proc *p = curproc;
1291 struct sigacts *ps = p->p_sigacts;
1293 sigset_t returnmask;
1296 KASSERT(sig != 0, ("postsig"));
1298 SIGDELSET(p->p_siglist, sig);
1299 action = ps->ps_sigact[_SIG_IDX(sig)];
1301 if (KTRPOINT(p->p_thread, KTR_PSIG))
1302 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1303 &p->p_oldsigmask : &p->p_sigmask, 0);
1305 STOPEVENT(p, S_SIG, sig);
1307 if (action == SIG_DFL) {
1309 * Default action, where the default is to kill
1310 * the process. (Other cases were ignored above.)
1316 * If we get here, the signal must be caught.
1318 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1319 ("postsig action"));
1321 * Set the new mask value and also defer further
1322 * occurrences of this signal.
1324 * Special case: user has done a sigsuspend. Here the
1325 * current mask is not of interest, but rather the
1326 * mask from before the sigsuspend is what we want
1327 * restored after the signal processing is completed.
1330 if (p->p_flag & P_OLDMASK) {
1331 returnmask = p->p_oldsigmask;
1332 p->p_flag &= ~P_OLDMASK;
1334 returnmask = p->p_sigmask;
1336 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1337 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1338 SIGADDSET(p->p_sigmask, sig);
1340 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1342 * See do_sigaction() for origin of this code.
1344 SIGDELSET(p->p_sigcatch, sig);
1345 if (sig != SIGCONT &&
1346 sigprop(sig) & SA_IGNORE)
1347 SIGADDSET(p->p_sigignore, sig);
1348 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1351 p->p_stats->p_ru.ru_nsignals++;
1352 if (p->p_sig != sig) {
1359 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1364 * Kill the current process for stated reason.
1371 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1372 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1373 psignal(p, SIGKILL);
1377 * Force the current process to exit with the specified signal, dumping core
1378 * if appropriate. We bypass the normal tests for masked and caught signals,
1379 * allowing unrecoverable failures to terminate the process without changing
1380 * signal state. Mark the accounting record with the signal termination.
1381 * If dumping core, save the signal number for the debugger. Calls exit and
1385 sigexit(struct proc *p, int sig)
1387 p->p_acflag |= AXSIG;
1388 if (sigprop(sig) & SA_CORE) {
1391 * Log signals which would cause core dumps
1392 * (Log as LOG_INFO to appease those who don't want
1394 * XXX : Todo, as well as euid, write out ruid too
1396 if (coredump(p) == 0)
1398 if (kern_logsigexit)
1400 "pid %d (%s), uid %d: exited on signal %d%s\n",
1401 p->p_pid, p->p_comm,
1402 p->p_ucred ? p->p_ucred->cr_uid : -1,
1404 sig & WCOREFLAG ? " (core dumped)" : "");
1406 exit1(W_EXITCODE(0, sig));
1410 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1411 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1412 sizeof(corefilename), "process corefile name format string");
1415 * expand_name(name, uid, pid)
1416 * Expand the name described in corefilename, using name, uid, and pid.
1417 * corefilename is a printf-like string, with three format specifiers:
1418 * %N name of process ("name")
1419 * %P process id (pid)
1421 * For example, "%N.core" is the default; they can be disabled completely
1422 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1423 * This is controlled by the sysctl variable kern.corefile (see above).
1427 expand_name(name, uid, pid)
1428 const char *name; uid_t uid; pid_t pid; {
1430 char buf[11]; /* Buffer for pid/uid -- max 4B */
1432 char *format = corefilename;
1435 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1438 namelen = strlen(name);
1439 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1441 switch (format[i]) {
1442 case '%': /* Format character */
1444 switch (format[i]) {
1448 case 'N': /* process name */
1449 if ((n + namelen) > MAXPATHLEN) {
1450 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1451 pid, name, uid, temp, name);
1455 memcpy(temp+n, name, namelen);
1458 case 'P': /* process id */
1459 l = sprintf(buf, "%u", pid);
1460 if ((n + l) > MAXPATHLEN) {
1461 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1462 pid, name, uid, temp, name);
1466 memcpy(temp+n, buf, l);
1469 case 'U': /* user id */
1470 l = sprintf(buf, "%u", uid);
1471 if ((n + l) > MAXPATHLEN) {
1472 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1473 pid, name, uid, temp, name);
1477 memcpy(temp+n, buf, l);
1481 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1485 temp[n++] = format[i];
1493 * Dump a process' core. The main routine does some
1494 * policy checking, and creates the name of the coredump;
1495 * then it passes on a vnode and a size limit to the process-specific
1496 * coredump routine if there is one; if there _is not_ one, it returns
1497 * ENOSYS; otherwise it returns the error from the process-specific routine.
1501 coredump(struct proc *p)
1504 struct ucred *cred = p->p_ucred;
1505 struct thread *td = p->p_thread;
1507 struct nameidata nd;
1510 char *name; /* name of corefile */
1513 STOPEVENT(p, S_CORE, 0);
1515 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1519 * Note that the bulk of limit checking is done after
1520 * the corefile is created. The exception is if the limit
1521 * for corefiles is 0, in which case we don't bother
1522 * creating the corefile at all. This layout means that
1523 * a corefile is truncated instead of not being created,
1524 * if it is larger than the limit.
1526 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1530 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1533 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
1534 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1538 NDFREE(&nd, NDF_ONLY_PNBUF);
1541 VOP_UNLOCK(vp, 0, td);
1542 lf.l_whence = SEEK_SET;
1545 lf.l_type = F_WRLCK;
1546 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1550 /* Don't dump to non-regular files or files with links. */
1551 if (vp->v_type != VREG ||
1552 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1558 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1560 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1561 VOP_SETATTR(vp, &vattr, cred, td);
1562 p->p_acflag |= ACORE;
1563 VOP_UNLOCK(vp, 0, td);
1565 error = p->p_sysent->sv_coredump ?
1566 p->p_sysent->sv_coredump(p, vp, limit) :
1570 lf.l_type = F_UNLCK;
1571 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1573 error1 = vn_close(vp, FWRITE, td);
1580 * Nonexistent system call-- signal process (may want to handle it).
1581 * Flag error in case process won't see signal immediately (blocked or ignored).
1585 nosys(struct nosys_args *args)
1587 psignal(curproc, SIGSYS);
1592 * Send a SIGIO or SIGURG signal to a process or process group using
1593 * stored credentials rather than those of the current process.
1596 pgsigio(struct sigio *sigio, int sig, int checkctty)
1601 if (sigio->sio_pgid > 0) {
1602 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1604 psignal(sigio->sio_proc, sig);
1605 } else if (sigio->sio_pgid < 0) {
1608 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1609 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1610 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1616 filt_sigattach(struct knote *kn)
1618 struct proc *p = curproc;
1620 kn->kn_ptr.p_proc = p;
1621 kn->kn_flags |= EV_CLEAR; /* automatically set */
1623 /* XXX lock the proc here while adding to the list? */
1624 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1630 filt_sigdetach(struct knote *kn)
1632 struct proc *p = kn->kn_ptr.p_proc;
1634 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1638 * signal knotes are shared with proc knotes, so we apply a mask to
1639 * the hint in order to differentiate them from process hints. This
1640 * could be avoided by using a signal-specific knote list, but probably
1641 * isn't worth the trouble.
1644 filt_signal(struct knote *kn, long hint)
1647 if (hint & NOTE_SIGNAL) {
1648 hint &= ~NOTE_SIGNAL;
1650 if (kn->kn_id == hint)
1653 return (kn->kn_data != 0);