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.26 2004/02/12 06:57:48 dillon Exp $
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysproto.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/vnode.h>
52 #include <sys/event.h>
54 #include <sys/namei.h>
55 #include <sys/pioctl.h>
56 #include <sys/systm.h>
58 #include <sys/fcntl.h>
60 #include <sys/ktrace.h>
61 #include <sys/syslog.h>
63 #include <sys/sysent.h>
64 #include <sys/sysctl.h>
65 #include <sys/malloc.h>
66 #include <sys/unistd.h>
67 #include <sys/kern_syscall.h>
70 #include <machine/ipl.h>
71 #include <machine/cpu.h>
72 #include <machine/smp.h>
74 static int coredump (struct proc *);
75 static char *expand_name (const char *, uid_t, pid_t);
76 static int killpg (int sig, int pgid, int all);
77 static int sig_ffs (sigset_t *set);
78 static int sigprop (int sig);
79 static void stop (struct proc *);
81 static void signotify_remote(void *arg);
84 static int filt_sigattach(struct knote *kn);
85 static void filt_sigdetach(struct knote *kn);
86 static int filt_signal(struct knote *kn, long hint);
88 struct filterops sig_filtops =
89 { 0, filt_sigattach, filt_sigdetach, filt_signal };
91 static int kern_logsigexit = 1;
92 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
94 "Log processes quitting on abnormal signals to syslog(3)");
97 * Can process p, with pcred pc, send the signal sig to process q?
99 #define CANSIGNAL(q, sig) \
100 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
101 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
104 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
106 #define CANSIGIO(ruid, uc, q) \
107 ((uc)->cr_uid == 0 || \
108 (ruid) == (q)->p_ucred->cr_ruid || \
109 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
110 (ruid) == (q)->p_ucred->cr_uid || \
111 (uc)->cr_uid == (q)->p_ucred->cr_uid)
114 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
115 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
117 static int do_coredump = 1;
118 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
119 &do_coredump, 0, "Enable/Disable coredumps");
122 * Signal properties and actions.
123 * The array below categorizes the signals and their default actions
124 * according to the following properties:
126 #define SA_KILL 0x01 /* terminates process by default */
127 #define SA_CORE 0x02 /* ditto and coredumps */
128 #define SA_STOP 0x04 /* suspend process */
129 #define SA_TTYSTOP 0x08 /* ditto, from tty */
130 #define SA_IGNORE 0x10 /* ignore by default */
131 #define SA_CONT 0x20 /* continue if suspended */
132 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
133 #define SA_CKPT 0x80 /* checkpoint process */
136 static int sigproptbl[NSIG] = {
137 SA_KILL, /* SIGHUP */
138 SA_KILL, /* SIGINT */
139 SA_KILL|SA_CORE, /* SIGQUIT */
140 SA_KILL|SA_CORE, /* SIGILL */
141 SA_KILL|SA_CORE, /* SIGTRAP */
142 SA_KILL|SA_CORE, /* SIGABRT */
143 SA_KILL|SA_CORE, /* SIGEMT */
144 SA_KILL|SA_CORE, /* SIGFPE */
145 SA_KILL, /* SIGKILL */
146 SA_KILL|SA_CORE, /* SIGBUS */
147 SA_KILL|SA_CORE, /* SIGSEGV */
148 SA_KILL|SA_CORE, /* SIGSYS */
149 SA_KILL, /* SIGPIPE */
150 SA_KILL, /* SIGALRM */
151 SA_KILL, /* SIGTERM */
152 SA_IGNORE, /* SIGURG */
153 SA_STOP, /* SIGSTOP */
154 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
155 SA_IGNORE|SA_CONT, /* SIGCONT */
156 SA_IGNORE, /* SIGCHLD */
157 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
158 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
159 SA_IGNORE, /* SIGIO */
160 SA_KILL, /* SIGXCPU */
161 SA_KILL, /* SIGXFSZ */
162 SA_KILL, /* SIGVTALRM */
163 SA_KILL, /* SIGPROF */
164 SA_IGNORE, /* SIGWINCH */
165 SA_IGNORE, /* SIGINFO */
166 SA_KILL, /* SIGUSR1 */
167 SA_KILL, /* SIGUSR2 */
168 SA_IGNORE, /* SIGTHR */
169 SA_CKPT, /* SIGCKPT */
170 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */
208 if (sig > 0 && sig < NSIG)
209 return (sigproptbl[_SIG_IDX(sig)]);
214 sig_ffs(sigset_t *set)
218 for (i = 0; i < _SIG_WORDS; i++)
220 return (ffs(set->__bits[i]) + (i * 32));
225 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
227 struct thread *td = curthread;
228 struct proc *p = td->td_proc;
229 struct sigacts *ps = p->p_sigacts;
231 if (sig <= 0 || sig > _SIG_MAXSIG)
235 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
236 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
238 if (SIGISMEMBER(ps->ps_sigonstack, sig))
239 oact->sa_flags |= SA_ONSTACK;
240 if (!SIGISMEMBER(ps->ps_sigintr, sig))
241 oact->sa_flags |= SA_RESTART;
242 if (SIGISMEMBER(ps->ps_sigreset, sig))
243 oact->sa_flags |= SA_RESETHAND;
244 if (SIGISMEMBER(ps->ps_signodefer, sig))
245 oact->sa_flags |= SA_NODEFER;
246 if (SIGISMEMBER(ps->ps_siginfo, sig))
247 oact->sa_flags |= SA_SIGINFO;
248 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
249 oact->sa_flags |= SA_NOCLDSTOP;
250 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
251 oact->sa_flags |= SA_NOCLDWAIT;
254 if ((sig == SIGKILL || sig == SIGSTOP) &&
255 act->sa_handler != SIG_DFL)
259 * Change setting atomically.
263 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
264 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
265 if (act->sa_flags & SA_SIGINFO) {
266 ps->ps_sigact[_SIG_IDX(sig)] =
267 (__sighandler_t *)act->sa_sigaction;
268 SIGADDSET(ps->ps_siginfo, sig);
270 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
271 SIGDELSET(ps->ps_siginfo, sig);
273 if (!(act->sa_flags & SA_RESTART))
274 SIGADDSET(ps->ps_sigintr, sig);
276 SIGDELSET(ps->ps_sigintr, sig);
277 if (act->sa_flags & SA_ONSTACK)
278 SIGADDSET(ps->ps_sigonstack, sig);
280 SIGDELSET(ps->ps_sigonstack, sig);
281 if (act->sa_flags & SA_RESETHAND)
282 SIGADDSET(ps->ps_sigreset, sig);
284 SIGDELSET(ps->ps_sigreset, sig);
285 if (act->sa_flags & SA_NODEFER)
286 SIGADDSET(ps->ps_signodefer, sig);
288 SIGDELSET(ps->ps_signodefer, sig);
289 if (sig == SIGCHLD) {
290 if (act->sa_flags & SA_NOCLDSTOP)
291 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
293 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
294 if (act->sa_flags & SA_NOCLDWAIT) {
296 * Paranoia: since SA_NOCLDWAIT is implemented
297 * by reparenting the dying child to PID 1 (and
298 * trust it to reap the zombie), PID 1 itself
299 * is forbidden to set SA_NOCLDWAIT.
302 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
304 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
306 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
309 * Set bit in p_sigignore for signals that are set to SIG_IGN,
310 * and for signals set to SIG_DFL where the default is to
311 * ignore. However, don't put SIGCONT in p_sigignore, as we
312 * have to restart the process.
314 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
315 (sigprop(sig) & SA_IGNORE &&
316 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
317 /* never to be seen again */
318 SIGDELSET(p->p_siglist, sig);
320 /* easier in psignal */
321 SIGADDSET(p->p_sigignore, sig);
322 SIGDELSET(p->p_sigcatch, sig);
324 SIGDELSET(p->p_sigignore, sig);
325 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
326 SIGDELSET(p->p_sigcatch, sig);
328 SIGADDSET(p->p_sigcatch, sig);
337 sigaction(struct sigaction_args *uap)
339 struct sigaction act, oact;
340 struct sigaction *actp, *oactp;
343 actp = (uap->act != NULL) ? &act : NULL;
344 oactp = (uap->oact != NULL) ? &oact : NULL;
346 error = copyin(uap->act, actp, sizeof(act));
350 error = kern_sigaction(uap->sig, actp, oactp);
351 if (oactp && !error) {
352 error = copyout(oactp, uap->oact, sizeof(oact));
358 * Initialize signal state for process 0;
359 * set to ignore signals that are ignored by default.
367 for (i = 1; i <= NSIG; i++)
368 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
369 SIGADDSET(p->p_sigignore, i);
373 * Reset signals for an exec of the specified process.
379 struct sigacts *ps = p->p_sigacts;
383 * Reset caught signals. Held signals remain held
384 * through p_sigmask (unless they were caught,
385 * and are now ignored by default).
387 while (SIGNOTEMPTY(p->p_sigcatch)) {
388 sig = sig_ffs(&p->p_sigcatch);
389 SIGDELSET(p->p_sigcatch, sig);
390 if (sigprop(sig) & SA_IGNORE) {
392 SIGADDSET(p->p_sigignore, sig);
393 SIGDELSET(p->p_siglist, sig);
395 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
398 * Reset stack state to the user stack.
399 * Clear set of signals caught on the signal stack.
401 p->p_sigstk.ss_flags = SS_DISABLE;
402 p->p_sigstk.ss_size = 0;
403 p->p_sigstk.ss_sp = 0;
404 p->p_flag &= ~P_ALTSTACK;
406 * Reset no zombies if child dies flag as Solaris does.
408 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
412 * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
414 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
415 * p == curproc. Also remember that in order to remain MP SAFE
416 * no spl*() calls may be made.
419 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
421 struct thread *td = curthread;
422 struct proc *p = td->td_proc;
426 *oset = p->p_sigmask;
433 SIGSETOR(p->p_sigmask, *set);
436 SIGSETNAND(p->p_sigmask, *set);
451 * sigprocmask() - MP SAFE
454 sigprocmask(struct sigprocmask_args *uap)
457 sigset_t *setp, *osetp;
460 setp = (uap->set != NULL) ? &set : NULL;
461 osetp = (uap->oset != NULL) ? &oset : NULL;
463 error = copyin(uap->set, setp, sizeof(set));
467 error = kern_sigprocmask(uap->how, setp, osetp);
468 if (osetp && !error) {
469 error = copyout(osetp, uap->oset, sizeof(oset));
475 kern_sigpending(struct __sigset *set)
477 struct thread *td = curthread;
478 struct proc *p = td->td_proc;
486 sigpending(struct sigpending_args *uap)
491 error = kern_sigpending(&set);
494 error = copyout(&set, uap->set, sizeof(set));
499 * Suspend process until signal, providing mask to be set
503 kern_sigsuspend(struct __sigset *set)
505 struct thread *td = curthread;
506 struct proc *p = td->td_proc;
507 struct sigacts *ps = p->p_sigacts;
510 * When returning from sigsuspend, we want
511 * the old mask to be restored after the
512 * signal handler has finished. Thus, we
513 * save it here and mark the sigacts structure
516 p->p_oldsigmask = p->p_sigmask;
517 p->p_flag |= P_OLDMASK;
521 while (tsleep(ps, PCATCH, "pause", 0) == 0)
523 /* always return EINTR rather than ERESTART... */
528 * Note nonstandard calling convention: libc stub passes mask, not
529 * pointer, to save a copyin.
532 sigsuspend(struct sigsuspend_args *uap)
537 error = copyin(uap->sigmask, &mask, sizeof(mask));
541 error = kern_sigsuspend(&mask);
547 kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss)
549 struct thread *td = curthread;
550 struct proc *p = td->td_proc;
552 if ((p->p_flag & P_ALTSTACK) == 0)
553 p->p_sigstk.ss_flags |= SS_DISABLE;
559 if (ss->ss_flags & SS_DISABLE) {
560 if (p->p_sigstk.ss_flags & SS_ONSTACK)
562 p->p_flag &= ~P_ALTSTACK;
563 p->p_sigstk.ss_flags = ss->ss_flags;
565 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
567 p->p_flag |= P_ALTSTACK;
576 sigaltstack(struct sigaltstack_args *uap)
582 error = copyin(uap->ss, &ss, sizeof(ss));
587 error = kern_sigaltstack(uap->ss ? &ss : NULL,
588 uap->oss ? &oss : NULL);
590 if (error == 0 && uap->oss)
591 error = copyout(&oss, uap->oss, sizeof(*uap->oss));
596 * Common code for kill process group/broadcast kill.
597 * cp is calling process.
600 killpg(int sig, int pgid, int all)
602 struct proc *cp = curproc;
611 FOREACH_PROC_IN_SYSTEM(p) {
612 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
613 p == cp || !CANSIGNAL(p, sig))
622 * zero pgid means send to my process group.
630 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
631 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
632 p->p_stat == SZOMB ||
640 return (nfound ? 0 : ESRCH);
644 kern_kill(int sig, int pid)
646 struct thread *td = curthread;
647 struct proc *p = td->td_proc;
649 if ((u_int)sig > _SIG_MAXSIG)
652 /* kill single process */
653 if ((p = pfind(pid)) == NULL)
655 if (!CANSIGNAL(p, sig))
662 case -1: /* broadcast signal */
663 return (killpg(sig, 0, 1));
664 case 0: /* signal own process group */
665 return (killpg(sig, 0, 0));
666 default: /* negative explicit process group */
667 return (killpg(sig, -pid, 0));
673 kill(struct kill_args *uap)
677 error = kern_kill(uap->signum, uap->pid);
683 * Send a signal to a process group.
686 gsignal(int pgid, int sig)
690 if (pgid && (pgrp = pgfind(pgid)))
691 pgsignal(pgrp, sig, 0);
695 * Send a signal to a process group. If checktty is 1,
696 * limit to members which have a controlling terminal.
699 pgsignal(pgrp, sig, checkctty)
706 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
707 if (checkctty == 0 || p->p_flag & P_CONTROLT)
712 * Send a signal caused by a trap to the current process.
713 * If it will be caught immediately, deliver it with correct code.
714 * Otherwise, post it normally.
717 trapsignal(p, sig, code)
722 struct sigacts *ps = p->p_sigacts;
724 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
725 !SIGISMEMBER(p->p_sigmask, sig)) {
726 p->p_stats->p_ru.ru_nsignals++;
728 if (KTRPOINT(p->p_thread, KTR_PSIG))
729 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
730 &p->p_sigmask, code);
732 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
733 &p->p_sigmask, code);
734 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
735 if (!SIGISMEMBER(ps->ps_signodefer, sig))
736 SIGADDSET(p->p_sigmask, sig);
737 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
739 * See kern_sigaction() for origin of this code.
741 SIGDELSET(p->p_sigcatch, sig);
742 if (sig != SIGCONT &&
743 sigprop(sig) & SA_IGNORE)
744 SIGADDSET(p->p_sigignore, sig);
745 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
748 p->p_code = code; /* XXX for core dump/debugger */
749 p->p_sig = sig; /* XXX to verify code */
755 * Send the signal to the process. If the signal has an action, the action
756 * is usually performed by the target process rather than the caller; we add
757 * the signal to the set of pending signals for the process.
760 * o When a stop signal is sent to a sleeping process that takes the
761 * default action, the process is stopped without awakening it.
762 * o SIGCONT restarts stopped processes (or puts them back to sleep)
763 * regardless of the signal action (eg, blocked or ignored).
765 * Other ignored signals are discarded immediately.
769 * temporary hack to allow checkpoint code to continue to
770 * be in a module for the moment
773 static proc_func_t ckpt_func;
776 register_ckpt_func(proc_func_t func)
778 proc_func_t old_func;
780 old_func = ckpt_func;
793 if (sig > _SIG_MAXSIG || sig <= 0) {
794 printf("psignal: signal %d\n", sig);
795 panic("psignal signal number");
799 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
805 * If proc is traced, always give parent a chance;
806 * if signal event is tracked by procfs, give *that*
809 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
813 * If the signal is being ignored,
814 * then we forget about it immediately.
815 * (Note: we don't set SIGCONT in p_sigignore,
816 * and if it is set to SIG_IGN,
817 * action will be SIG_DFL here.)
819 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
821 if (SIGISMEMBER(p->p_sigmask, sig))
823 else if (SIGISMEMBER(p->p_sigcatch, sig))
829 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
830 (p->p_flag & P_TRACED) == 0)
834 SIG_STOPSIGMASK(p->p_siglist);
837 if (prop & SA_STOP) {
839 * If sending a tty stop signal to a member of an orphaned
840 * process group, discard the signal here if the action
841 * is default; don't stop the process below if sleeping,
842 * and don't clear any pending SIGCONT.
844 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
847 SIG_CONTSIGMASK(p->p_siglist);
849 SIGADDSET(p->p_siglist, sig);
852 * Defer further processing for signals which are held,
853 * except that stopped processes must be continued by SIGCONT.
855 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
862 * If process is sleeping uninterruptibly
863 * we can't interrupt the sleep... the signal will
864 * be noticed when the process returns through
865 * trap() or syscall().
867 if ((p->p_flag & P_SINTR) == 0)
870 * Process is sleeping and traced... make it runnable
871 * so it can discover the signal in issignal() and stop
874 if (p->p_flag & P_TRACED)
877 * If SIGCONT is default (or ignored) and process is
878 * asleep, we are finished; the process should not
881 if ((prop & SA_CONT) && action == SIG_DFL) {
882 SIGDELSET(p->p_siglist, sig);
886 * When a sleeping process receives a stop
887 * signal, process immediately if possible.
888 * All other (caught or default) signals
889 * cause the process to run.
891 if (prop & SA_STOP) {
892 if (action != SIG_DFL)
895 * If a child holding parent blocked,
896 * stopping could cause deadlock.
898 if (p->p_flag & P_PPWAIT)
900 SIGDELSET(p->p_siglist, sig);
902 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
903 psignal(p->p_pptr, SIGCHLD);
912 * If traced process is already stopped,
913 * then no further action is necessary.
915 if (p->p_flag & P_TRACED)
919 * Kill signal always sets processes running.
924 if (prop & SA_CONT) {
926 * If SIGCONT is default (or ignored), we continue the
927 * process but don't leave the signal in p_siglist, as
928 * it has no further action. If SIGCONT is held, we
929 * continue the process and leave the signal in
930 * p_siglist. If the process catches SIGCONT, let it
931 * handle the signal itself. If it isn't waiting on
932 * an event, then it goes back to run state.
933 * Otherwise, process goes back to sleep state.
935 if (action == SIG_DFL)
936 SIGDELSET(p->p_siglist, sig);
937 if (action == SIG_CATCH)
941 clrrunnable(p, SSLEEP);
945 if (prop & SA_STOP) {
947 * Already stopped, don't need to stop again.
948 * (If we did the shell could get confused.)
950 SIGDELSET(p->p_siglist, sig);
955 * If process is sleeping interruptibly, then simulate a
956 * wakeup so that when it is continued, it will be made
957 * runnable and can look at the signal. But don't make
958 * the process runnable, leave it stopped.
960 if (p->p_wchan && (p->p_flag & P_SINTR))
961 unsleep(p->p_thread);
966 * SRUN, SIDL, SZOMB do nothing with the signal,
967 * other than kicking ourselves if we are running.
968 * It will either never be noticed, or noticed very soon.
970 * For SMP we may have to forward the request to another cpu.
971 * YYY the MP lock prevents the target process from moving
972 * to another cpu, see kern/kern_switch.c
975 if (p == lwkt_preempted_proc()) {
978 struct thread *td = p->p_thread;
980 if (td->td_gd != mycpu)
981 lwkt_send_ipiq(td->td_gd, signotify_remote, p);
984 if (p == lwkt_preempted_proc())
999 * This function is called via an IPI. We will be in a critical section but
1000 * the MP lock will NOT be held. Also note that by the time the ipi message
1001 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1004 signotify_remote(void *arg)
1006 struct proc *p = arg;
1007 if (p == lwkt_preempted_proc())
1014 * If the current process has received a signal that would interrupt a
1015 * system call, return EINTR or ERESTART as appropriate.
1018 iscaught(struct proc *p)
1023 if ((sig = CURSIG(p)) != 0) {
1024 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1029 return(EWOULDBLOCK);
1033 * If the current process has received a signal (should be caught or cause
1034 * termination, should interrupt current syscall), return the signal number.
1035 * Stop signals with default action are processed immediately, then cleared;
1036 * they aren't returned. This is checked after each entry to the system for
1037 * a syscall or trap (though this can usually be done without calling issignal
1038 * by checking the pending signal masks in the CURSIG macro.) The normal call
1041 * while (sig = CURSIG(curproc))
1052 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1054 mask = p->p_siglist;
1055 SIGSETNAND(mask, p->p_sigmask);
1056 if (p->p_flag & P_PPWAIT)
1057 SIG_STOPSIGMASK(mask);
1058 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1060 sig = sig_ffs(&mask);
1062 STOPEVENT(p, S_SIG, sig);
1065 * We should see pending but ignored signals
1066 * only if P_TRACED was on when they were posted.
1068 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1069 SIGDELSET(p->p_siglist, sig);
1072 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1074 * If traced, always stop, and stay
1075 * stopped until released by the parent.
1078 psignal(p->p_pptr, SIGCHLD);
1082 } while (!trace_req(p) && p->p_flag & P_TRACED);
1085 * If parent wants us to take the signal,
1086 * then it will leave it in p->p_xstat;
1087 * otherwise we just look for signals again.
1089 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1095 * Put the new signal into p_siglist. If the
1096 * signal is being masked, look for other signals.
1098 SIGADDSET(p->p_siglist, sig);
1099 if (SIGISMEMBER(p->p_sigmask, sig))
1103 * If the traced bit got turned off, go back up
1104 * to the top to rescan signals. This ensures
1105 * that p_sig* and ps_sigact are consistent.
1107 if ((p->p_flag & P_TRACED) == 0)
1111 prop = sigprop(sig);
1114 * Decide whether the signal should be returned.
1115 * Return the signal's number, or fall through
1116 * to clear it from the pending mask.
1118 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1122 * Don't take default actions on system processes.
1124 if (p->p_pid <= 1) {
1127 * Are you sure you want to ignore SIGSEGV
1130 printf("Process (pid %lu) got signal %d\n",
1131 (u_long)p->p_pid, sig);
1133 break; /* == ignore */
1137 * Handle the in-kernel checkpoint action
1139 if (prop & SA_CKPT) {
1146 * If there is a pending stop signal to process
1147 * with default action, stop here,
1148 * then clear the signal. However,
1149 * if process is member of an orphaned
1150 * process group, ignore tty stop signals.
1152 if (prop & SA_STOP) {
1153 if (p->p_flag & P_TRACED ||
1154 (p->p_pgrp->pg_jobc == 0 &&
1156 break; /* == ignore */
1159 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1160 psignal(p->p_pptr, SIGCHLD);
1163 } else if (prop & SA_IGNORE) {
1165 * Except for SIGCONT, shouldn't get here.
1166 * Default action is to ignore; drop it.
1168 break; /* == ignore */
1175 * Masking above should prevent us ever trying
1176 * to take action on an ignored signal other
1177 * than SIGCONT, unless process is traced.
1179 if ((prop & SA_CONT) == 0 &&
1180 (p->p_flag & P_TRACED) == 0)
1181 printf("issignal\n");
1182 break; /* == ignore */
1186 * This signal has an action, let
1187 * postsig() process it.
1191 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1197 * Put the argument process into the stopped state and notify the parent
1198 * via wakeup. Signals are handled elsewhere. The process must not be
1207 p->p_flag &= ~P_WAITED;
1208 wakeup((caddr_t)p->p_pptr);
1212 * Take the action for the specified signal
1213 * from the current set of pending signals.
1219 struct proc *p = curproc;
1220 struct sigacts *ps = p->p_sigacts;
1222 sigset_t returnmask;
1225 KASSERT(sig != 0, ("postsig"));
1227 SIGDELSET(p->p_siglist, sig);
1228 action = ps->ps_sigact[_SIG_IDX(sig)];
1230 if (KTRPOINT(p->p_thread, KTR_PSIG))
1231 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1232 &p->p_oldsigmask : &p->p_sigmask, 0);
1234 STOPEVENT(p, S_SIG, sig);
1236 if (action == SIG_DFL) {
1238 * Default action, where the default is to kill
1239 * the process. (Other cases were ignored above.)
1245 * If we get here, the signal must be caught.
1247 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1248 ("postsig action"));
1250 * Set the new mask value and also defer further
1251 * occurrences of this signal.
1253 * Special case: user has done a sigsuspend. Here the
1254 * current mask is not of interest, but rather the
1255 * mask from before the sigsuspend is what we want
1256 * restored after the signal processing is completed.
1259 if (p->p_flag & P_OLDMASK) {
1260 returnmask = p->p_oldsigmask;
1261 p->p_flag &= ~P_OLDMASK;
1263 returnmask = p->p_sigmask;
1265 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1266 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1267 SIGADDSET(p->p_sigmask, sig);
1269 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1271 * See kern_sigaction() for origin of this code.
1273 SIGDELSET(p->p_sigcatch, sig);
1274 if (sig != SIGCONT &&
1275 sigprop(sig) & SA_IGNORE)
1276 SIGADDSET(p->p_sigignore, sig);
1277 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1280 p->p_stats->p_ru.ru_nsignals++;
1281 if (p->p_sig != sig) {
1288 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1293 * Kill the current process for stated reason.
1300 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1301 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1302 psignal(p, SIGKILL);
1306 * Force the current process to exit with the specified signal, dumping core
1307 * if appropriate. We bypass the normal tests for masked and caught signals,
1308 * allowing unrecoverable failures to terminate the process without changing
1309 * signal state. Mark the accounting record with the signal termination.
1310 * If dumping core, save the signal number for the debugger. Calls exit and
1314 sigexit(struct proc *p, int sig)
1316 p->p_acflag |= AXSIG;
1317 if (sigprop(sig) & SA_CORE) {
1320 * Log signals which would cause core dumps
1321 * (Log as LOG_INFO to appease those who don't want
1323 * XXX : Todo, as well as euid, write out ruid too
1325 if (coredump(p) == 0)
1327 if (kern_logsigexit)
1329 "pid %d (%s), uid %d: exited on signal %d%s\n",
1330 p->p_pid, p->p_comm,
1331 p->p_ucred ? p->p_ucred->cr_uid : -1,
1333 sig & WCOREFLAG ? " (core dumped)" : "");
1335 exit1(W_EXITCODE(0, sig));
1339 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1340 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1341 sizeof(corefilename), "process corefile name format string");
1344 * expand_name(name, uid, pid)
1345 * Expand the name described in corefilename, using name, uid, and pid.
1346 * corefilename is a printf-like string, with three format specifiers:
1347 * %N name of process ("name")
1348 * %P process id (pid)
1350 * For example, "%N.core" is the default; they can be disabled completely
1351 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1352 * This is controlled by the sysctl variable kern.corefile (see above).
1356 expand_name(name, uid, pid)
1357 const char *name; uid_t uid; pid_t pid; {
1359 char buf[11]; /* Buffer for pid/uid -- max 4B */
1361 char *format = corefilename;
1364 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1367 namelen = strlen(name);
1368 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1370 switch (format[i]) {
1371 case '%': /* Format character */
1373 switch (format[i]) {
1377 case 'N': /* process name */
1378 if ((n + namelen) > MAXPATHLEN) {
1379 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1380 pid, name, uid, temp, name);
1384 memcpy(temp+n, name, namelen);
1387 case 'P': /* process id */
1388 l = sprintf(buf, "%u", pid);
1389 if ((n + l) > MAXPATHLEN) {
1390 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1391 pid, name, uid, temp, name);
1395 memcpy(temp+n, buf, l);
1398 case 'U': /* user id */
1399 l = sprintf(buf, "%u", uid);
1400 if ((n + l) > MAXPATHLEN) {
1401 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1402 pid, name, uid, temp, name);
1406 memcpy(temp+n, buf, l);
1410 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1414 temp[n++] = format[i];
1422 * Dump a process' core. The main routine does some
1423 * policy checking, and creates the name of the coredump;
1424 * then it passes on a vnode and a size limit to the process-specific
1425 * coredump routine if there is one; if there _is not_ one, it returns
1426 * ENOSYS; otherwise it returns the error from the process-specific routine.
1430 coredump(struct proc *p)
1433 struct ucred *cred = p->p_ucred;
1434 struct thread *td = p->p_thread;
1436 struct nameidata nd;
1439 char *name; /* name of corefile */
1442 STOPEVENT(p, S_CORE, 0);
1444 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1448 * Note that the bulk of limit checking is done after
1449 * the corefile is created. The exception is if the limit
1450 * for corefiles is 0, in which case we don't bother
1451 * creating the corefile at all. This layout means that
1452 * a corefile is truncated instead of not being created,
1453 * if it is larger than the limit.
1455 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1459 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1462 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_SYSSPACE, name, td);
1463 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1467 NDFREE(&nd, NDF_ONLY_PNBUF);
1470 VOP_UNLOCK(vp, 0, td);
1471 lf.l_whence = SEEK_SET;
1474 lf.l_type = F_WRLCK;
1475 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1479 /* Don't dump to non-regular files or files with links. */
1480 if (vp->v_type != VREG ||
1481 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1487 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1489 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1490 VOP_SETATTR(vp, &vattr, cred, td);
1491 p->p_acflag |= ACORE;
1492 VOP_UNLOCK(vp, 0, td);
1494 error = p->p_sysent->sv_coredump ?
1495 p->p_sysent->sv_coredump(p, vp, limit) :
1499 lf.l_type = F_UNLCK;
1500 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1502 error1 = vn_close(vp, FWRITE, td);
1509 * Nonexistent system call-- signal process (may want to handle it).
1510 * Flag error in case process won't see signal immediately (blocked or ignored).
1514 nosys(struct nosys_args *args)
1516 psignal(curproc, SIGSYS);
1521 * Send a SIGIO or SIGURG signal to a process or process group using
1522 * stored credentials rather than those of the current process.
1525 pgsigio(struct sigio *sigio, int sig, int checkctty)
1530 if (sigio->sio_pgid > 0) {
1531 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1533 psignal(sigio->sio_proc, sig);
1534 } else if (sigio->sio_pgid < 0) {
1537 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1538 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1539 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1545 filt_sigattach(struct knote *kn)
1547 struct proc *p = curproc;
1549 kn->kn_ptr.p_proc = p;
1550 kn->kn_flags |= EV_CLEAR; /* automatically set */
1552 /* XXX lock the proc here while adding to the list? */
1553 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1559 filt_sigdetach(struct knote *kn)
1561 struct proc *p = kn->kn_ptr.p_proc;
1563 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1567 * signal knotes are shared with proc knotes, so we apply a mask to
1568 * the hint in order to differentiate them from process hints. This
1569 * could be avoided by using a signal-specific knote list, but probably
1570 * isn't worth the trouble.
1573 filt_signal(struct knote *kn, long hint)
1576 if (hint & NOTE_SIGNAL) {
1577 hint &= ~NOTE_SIGNAL;
1579 if (kn->kn_id == hint)
1582 return (kn->kn_data != 0);