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 $
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/sysproto.h>
48 #include <sys/signalvar.h>
49 #include <sys/resourcevar.h>
50 #include <sys/vnode.h>
51 #include <sys/event.h>
53 #include <sys/nlookup.h>
54 #include <sys/pioctl.h>
56 #include <sys/fcntl.h>
59 #include <sys/ktrace.h>
60 #include <sys/syslog.h>
62 #include <sys/sysent.h>
63 #include <sys/sysctl.h>
64 #include <sys/malloc.h>
65 #include <sys/interrupt.h>
66 #include <sys/unistd.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/vkernel.h>
70 #include <sys/signal2.h>
71 #include <sys/thread2.h>
73 #include <machine/cpu.h>
74 #include <machine/smp.h>
76 static int coredump(struct lwp *, int);
77 static char *expand_name(const char *, uid_t, pid_t);
78 static int dokillpg(int sig, int pgid, int all);
79 static int sig_ffs(sigset_t *set);
80 static int sigprop(int sig);
81 static void lwp_signotify(struct lwp *lp);
83 static void signotify_remote(void *arg);
85 static int kern_sigtimedwait(sigset_t set, siginfo_t *info,
86 struct timespec *timeout);
88 static int filt_sigattach(struct knote *kn);
89 static void filt_sigdetach(struct knote *kn);
90 static int filt_signal(struct knote *kn, long hint);
92 struct filterops sig_filtops =
93 { 0, filt_sigattach, filt_sigdetach, filt_signal };
95 static int kern_logsigexit = 1;
96 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
98 "Log processes quitting on abnormal signals to syslog(3)");
101 * Can process p, with pcred pc, send the signal sig to process q?
103 #define CANSIGNAL(q, sig) \
104 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
105 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
108 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
110 #define CANSIGIO(ruid, uc, q) \
111 ((uc)->cr_uid == 0 || \
112 (ruid) == (q)->p_ucred->cr_ruid || \
113 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
114 (ruid) == (q)->p_ucred->cr_uid || \
115 (uc)->cr_uid == (q)->p_ucred->cr_uid)
118 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
119 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
121 static int do_coredump = 1;
122 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123 &do_coredump, 0, "Enable/Disable coredumps");
126 * Signal properties and actions.
127 * The array below categorizes the signals and their default actions
128 * according to the following properties:
130 #define SA_KILL 0x01 /* terminates process by default */
131 #define SA_CORE 0x02 /* ditto and coredumps */
132 #define SA_STOP 0x04 /* suspend process */
133 #define SA_TTYSTOP 0x08 /* ditto, from tty */
134 #define SA_IGNORE 0x10 /* ignore by default */
135 #define SA_CONT 0x20 /* continue if suspended */
136 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
137 #define SA_CKPT 0x80 /* checkpoint process */
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 */
172 SA_IGNORE, /* SIGTHR */
173 SA_CKPT, /* SIGCKPT */
174 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */
212 if (sig > 0 && sig < NSIG)
213 return (sigproptbl[_SIG_IDX(sig)]);
218 sig_ffs(sigset_t *set)
222 for (i = 0; i < _SIG_WORDS; i++)
224 return (ffs(set->__bits[i]) + (i * 32));
232 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
234 struct thread *td = curthread;
235 struct proc *p = td->td_proc;
237 struct sigacts *ps = p->p_sigacts;
239 if (sig <= 0 || sig > _SIG_MAXSIG)
242 lwkt_gettoken(&p->p_token);
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 (SIGISMEMBER(ps->ps_sigmailbox, sig))
259 oact->sa_flags |= SA_MAILBOX;
260 if (sig == SIGCHLD && p->p_sigacts->ps_flag & PS_NOCLDSTOP)
261 oact->sa_flags |= SA_NOCLDSTOP;
262 if (sig == SIGCHLD && p->p_sigacts->ps_flag & PS_NOCLDWAIT)
263 oact->sa_flags |= SA_NOCLDWAIT;
267 * Check for invalid requests. KILL and STOP cannot be
270 if (sig == SIGKILL || sig == SIGSTOP) {
271 if (act->sa_handler != SIG_DFL) {
272 lwkt_reltoken(&p->p_token);
276 /* (not needed, SIG_DFL forces action to occur) */
277 if (act->sa_flags & SA_MAILBOX) {
278 lwkt_reltoken(&p->p_token);
285 * Change setting atomically.
289 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
290 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
291 if (act->sa_flags & SA_SIGINFO) {
292 ps->ps_sigact[_SIG_IDX(sig)] =
293 (__sighandler_t *)act->sa_sigaction;
294 SIGADDSET(ps->ps_siginfo, sig);
296 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
297 SIGDELSET(ps->ps_siginfo, sig);
299 if (!(act->sa_flags & SA_RESTART))
300 SIGADDSET(ps->ps_sigintr, sig);
302 SIGDELSET(ps->ps_sigintr, sig);
303 if (act->sa_flags & SA_ONSTACK)
304 SIGADDSET(ps->ps_sigonstack, sig);
306 SIGDELSET(ps->ps_sigonstack, sig);
307 if (act->sa_flags & SA_RESETHAND)
308 SIGADDSET(ps->ps_sigreset, sig);
310 SIGDELSET(ps->ps_sigreset, sig);
311 if (act->sa_flags & SA_NODEFER)
312 SIGADDSET(ps->ps_signodefer, sig);
314 SIGDELSET(ps->ps_signodefer, sig);
315 if (act->sa_flags & SA_MAILBOX)
316 SIGADDSET(ps->ps_sigmailbox, sig);
318 SIGDELSET(ps->ps_sigmailbox, sig);
319 if (sig == SIGCHLD) {
320 if (act->sa_flags & SA_NOCLDSTOP)
321 p->p_sigacts->ps_flag |= PS_NOCLDSTOP;
323 p->p_sigacts->ps_flag &= ~PS_NOCLDSTOP;
324 if (act->sa_flags & SA_NOCLDWAIT) {
326 * Paranoia: since SA_NOCLDWAIT is implemented
327 * by reparenting the dying child to PID 1 (and
328 * trust it to reap the zombie), PID 1 itself
329 * is forbidden to set SA_NOCLDWAIT.
332 p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
334 p->p_sigacts->ps_flag |= PS_NOCLDWAIT;
336 p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
340 * Set bit in p_sigignore for signals that are set to SIG_IGN,
341 * and for signals set to SIG_DFL where the default is to
342 * ignore. However, don't put SIGCONT in p_sigignore, as we
343 * have to restart the process.
345 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
346 (sigprop(sig) & SA_IGNORE &&
347 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
348 /* never to be seen again */
349 SIGDELSET(p->p_siglist, sig);
351 * Remove the signal also from the thread lists.
353 FOREACH_LWP_IN_PROC(lp, p) {
355 lwkt_gettoken(&lp->lwp_token);
356 SIGDELSET(lp->lwp_siglist, sig);
357 lwkt_reltoken(&lp->lwp_token);
360 if (sig != SIGCONT) {
361 /* easier in ksignal */
362 SIGADDSET(p->p_sigignore, sig);
364 SIGDELSET(p->p_sigcatch, sig);
366 SIGDELSET(p->p_sigignore, sig);
367 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
368 SIGDELSET(p->p_sigcatch, sig);
370 SIGADDSET(p->p_sigcatch, sig);
375 lwkt_reltoken(&p->p_token);
380 sys_sigaction(struct sigaction_args *uap)
382 struct sigaction act, oact;
383 struct sigaction *actp, *oactp;
386 actp = (uap->act != NULL) ? &act : NULL;
387 oactp = (uap->oact != NULL) ? &oact : NULL;
389 error = copyin(uap->act, actp, sizeof(act));
393 error = kern_sigaction(uap->sig, actp, oactp);
394 if (oactp && !error) {
395 error = copyout(oactp, uap->oact, sizeof(oact));
401 * Initialize signal state for process 0;
402 * set to ignore signals that are ignored by default.
405 siginit(struct proc *p)
409 for (i = 1; i <= NSIG; i++)
410 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
411 SIGADDSET(p->p_sigignore, i);
415 * Reset signals for an exec of the specified process.
418 execsigs(struct proc *p)
420 struct sigacts *ps = p->p_sigacts;
424 lp = ONLY_LWP_IN_PROC(p);
427 * Reset caught signals. Held signals remain held
428 * through p_sigmask (unless they were caught,
429 * and are now ignored by default).
431 while (SIGNOTEMPTY(p->p_sigcatch)) {
432 sig = sig_ffs(&p->p_sigcatch);
433 SIGDELSET(p->p_sigcatch, sig);
434 if (sigprop(sig) & SA_IGNORE) {
436 SIGADDSET(p->p_sigignore, sig);
437 SIGDELSET(p->p_siglist, sig);
438 SIGDELSET(lp->lwp_siglist, sig);
440 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
444 * Reset stack state to the user stack.
445 * Clear set of signals caught on the signal stack.
447 lp->lwp_sigstk.ss_flags = SS_DISABLE;
448 lp->lwp_sigstk.ss_size = 0;
449 lp->lwp_sigstk.ss_sp = 0;
450 lp->lwp_flag &= ~LWP_ALTSTACK;
452 * Reset no zombies if child dies flag as Solaris does.
454 p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
458 * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
460 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
464 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
466 struct thread *td = curthread;
467 struct lwp *lp = td->td_lwp;
468 struct proc *p = td->td_proc;
471 lwkt_gettoken(&p->p_token);
474 *oset = lp->lwp_sigmask;
481 SIGSETOR(lp->lwp_sigmask, *set);
484 SIGSETNAND(lp->lwp_sigmask, *set);
488 lp->lwp_sigmask = *set;
496 lwkt_reltoken(&p->p_token);
507 sys_sigprocmask(struct sigprocmask_args *uap)
510 sigset_t *setp, *osetp;
513 setp = (uap->set != NULL) ? &set : NULL;
514 osetp = (uap->oset != NULL) ? &oset : NULL;
516 error = copyin(uap->set, setp, sizeof(set));
520 error = kern_sigprocmask(uap->how, setp, osetp);
521 if (osetp && !error) {
522 error = copyout(osetp, uap->oset, sizeof(oset));
531 kern_sigpending(struct __sigset *set)
533 struct lwp *lp = curthread->td_lwp;
535 *set = lwp_sigpend(lp);
544 sys_sigpending(struct sigpending_args *uap)
549 error = kern_sigpending(&set);
552 error = copyout(&set, uap->set, sizeof(set));
557 * Suspend process until signal, providing mask to be set
563 kern_sigsuspend(struct __sigset *set)
565 struct thread *td = curthread;
566 struct lwp *lp = td->td_lwp;
567 struct proc *p = td->td_proc;
568 struct sigacts *ps = p->p_sigacts;
571 * When returning from sigsuspend, we want
572 * the old mask to be restored after the
573 * signal handler has finished. Thus, we
574 * save it here and mark the sigacts structure
577 lp->lwp_oldsigmask = lp->lwp_sigmask;
578 lp->lwp_flag |= LWP_OLDMASK;
581 lp->lwp_sigmask = *set;
582 while (tsleep(ps, PCATCH, "pause", 0) == 0)
584 /* always return EINTR rather than ERESTART... */
589 * Note nonstandard calling convention: libc stub passes mask, not
590 * pointer, to save a copyin.
595 sys_sigsuspend(struct sigsuspend_args *uap)
600 error = copyin(uap->sigmask, &mask, sizeof(mask));
604 error = kern_sigsuspend(&mask);
613 kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss)
615 struct thread *td = curthread;
616 struct lwp *lp = td->td_lwp;
617 struct proc *p = td->td_proc;
619 if ((lp->lwp_flag & LWP_ALTSTACK) == 0)
620 lp->lwp_sigstk.ss_flags |= SS_DISABLE;
623 *oss = lp->lwp_sigstk;
626 if (ss->ss_flags & SS_DISABLE) {
627 if (lp->lwp_sigstk.ss_flags & SS_ONSTACK)
629 lp->lwp_flag &= ~LWP_ALTSTACK;
630 lp->lwp_sigstk.ss_flags = ss->ss_flags;
632 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
634 lp->lwp_flag |= LWP_ALTSTACK;
635 lp->lwp_sigstk = *ss;
646 sys_sigaltstack(struct sigaltstack_args *uap)
652 error = copyin(uap->ss, &ss, sizeof(ss));
657 error = kern_sigaltstack(uap->ss ? &ss : NULL,
658 uap->oss ? &oss : NULL);
660 if (error == 0 && uap->oss)
661 error = copyout(&oss, uap->oss, sizeof(*uap->oss));
666 * Common code for kill process group/broadcast kill.
667 * cp is calling process.
674 static int killpg_all_callback(struct proc *p, void *data);
677 dokillpg(int sig, int pgid, int all)
679 struct killpg_info info;
680 struct proc *cp = curproc;
691 allproc_scan(killpg_all_callback, &info);
695 * zero pgid means send to my process group.
706 * Must interlock all signals against fork
708 lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
709 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
711 p->p_stat == SZOMB ||
712 (p->p_flag & P_SYSTEM) ||
713 !CANSIGNAL(p, sig)) {
720 lockmgr(&pgrp->pg_lock, LK_RELEASE);
723 return (info.nfound ? 0 : ESRCH);
727 killpg_all_callback(struct proc *p, void *data)
729 struct killpg_info *info = data;
731 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) ||
732 p == curproc || !CANSIGNAL(p, info->sig)) {
737 ksignal(p, info->sig);
742 * Send a general signal to a process or LWPs within that process. Note
743 * that new signals cannot be sent if a process is exiting.
748 kern_kill(int sig, pid_t pid, lwpid_t tid)
752 if ((u_int)sig > _SIG_MAXSIG)
755 lwkt_gettoken(&proc_token);
759 struct lwp *lp = NULL;
761 /* kill single process */
762 if ((p = pfind(pid)) == NULL) {
763 lwkt_reltoken(&proc_token);
766 lwkt_gettoken(&p->p_token);
767 if (!CANSIGNAL(p, sig)) {
768 lwkt_reltoken(&p->p_token);
770 lwkt_reltoken(&proc_token);
775 * NOP if the process is exiting. Note that lwpsignal() is
776 * called directly with P_WEXIT set to kill individual LWPs
777 * during exit, which is allowed.
779 if (p->p_flag & P_WEXIT) {
780 lwkt_reltoken(&p->p_token);
782 lwkt_reltoken(&proc_token);
786 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, tid);
788 lwkt_reltoken(&p->p_token);
790 lwkt_reltoken(&proc_token);
795 lwpsignal(p, lp, sig);
796 lwkt_reltoken(&p->p_token);
798 lwkt_reltoken(&proc_token);
803 * If we come here, pid is a special broadcast pid.
804 * This doesn't mix with a tid.
807 lwkt_reltoken(&proc_token);
811 case -1: /* broadcast signal */
812 t = (dokillpg(sig, 0, 1));
814 case 0: /* signal own process group */
815 t = (dokillpg(sig, 0, 0));
817 default: /* negative explicit process group */
818 t = (dokillpg(sig, -pid, 0));
821 lwkt_reltoken(&proc_token);
826 sys_kill(struct kill_args *uap)
830 error = kern_kill(uap->signum, uap->pid, -1);
835 sys_lwp_kill(struct lwp_kill_args *uap)
838 pid_t pid = uap->pid;
841 * A tid is mandatory for lwp_kill(), otherwise
842 * you could simply use kill().
848 * To save on a getpid() function call for intra-process
849 * signals, pid == -1 means current process.
852 pid = curproc->p_pid;
854 error = kern_kill(uap->signum, pid, uap->tid);
859 * Send a signal to a process group.
862 gsignal(int pgid, int sig)
866 if (pgid && (pgrp = pgfind(pgid)))
867 pgsignal(pgrp, sig, 0);
871 * Send a signal to a process group. If checktty is 1,
872 * limit to members which have a controlling terminal.
874 * pg_lock interlocks against a fork that might be in progress, to
875 * ensure that the new child process picks up the signal.
878 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
883 * Must interlock all signals against fork
887 lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
888 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
889 if (checkctty == 0 || p->p_flag & P_CONTROLT)
892 lockmgr(&pgrp->pg_lock, LK_RELEASE);
898 * Send a signal caused by a trap to the current lwp. If it will be caught
899 * immediately, deliver it with correct code. Otherwise, post it normally.
901 * These signals may ONLY be delivered to the specified lwp and may never
902 * be delivered to the process generically.
905 trapsignal(struct lwp *lp, int sig, u_long code)
907 struct proc *p = lp->lwp_proc;
908 struct sigacts *ps = p->p_sigacts;
911 * If we are a virtual kernel running an emulated user process
912 * context, switch back to the virtual kernel context before
913 * trying to post the signal.
915 if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
916 struct trapframe *tf = lp->lwp_md.md_regs;
918 vkernel_trap(lp, tf);
922 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
923 !SIGISMEMBER(lp->lwp_sigmask, sig)) {
924 lp->lwp_ru.ru_nsignals++;
926 if (KTRPOINT(lp->lwp_thread, KTR_PSIG))
927 ktrpsig(lp, sig, ps->ps_sigact[_SIG_IDX(sig)],
928 &lp->lwp_sigmask, code);
930 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
931 &lp->lwp_sigmask, code);
932 SIGSETOR(lp->lwp_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
933 if (!SIGISMEMBER(ps->ps_signodefer, sig))
934 SIGADDSET(lp->lwp_sigmask, sig);
935 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
937 * See kern_sigaction() for origin of this code.
939 SIGDELSET(p->p_sigcatch, sig);
940 if (sig != SIGCONT &&
941 sigprop(sig) & SA_IGNORE)
942 SIGADDSET(p->p_sigignore, sig);
943 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
946 lp->lwp_code = code; /* XXX for core dump/debugger */
947 lp->lwp_sig = sig; /* XXX to verify code */
948 lwpsignal(p, lp, sig);
953 * Find a suitable lwp to deliver the signal to. Returns NULL if all
954 * lwps hold the signal blocked.
956 * Caller must hold p->p_token.
958 * Returns a lp or NULL. If non-NULL the lp is held and its token is
962 find_lwp_for_signal(struct proc *p, int sig)
965 struct lwp *run, *sleep, *stop;
968 * If the running/preempted thread belongs to the proc to which
969 * the signal is being delivered and this thread does not block
970 * the signal, then we can avoid a context switch by delivering
971 * the signal to this thread, because it will return to userland
974 lp = lwkt_preempted_proc();
975 if (lp != NULL && lp->lwp_proc == p) {
977 lwkt_gettoken(&lp->lwp_token);
978 if (!SIGISMEMBER(lp->lwp_sigmask, sig)) {
979 /* return w/ token held */
982 lwkt_reltoken(&lp->lwp_token);
986 run = sleep = stop = NULL;
987 FOREACH_LWP_IN_PROC(lp, p) {
989 * If the signal is being blocked by the lwp, then this
990 * lwp is not eligible for receiving the signal.
993 lwkt_gettoken(&lp->lwp_token);
995 if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
996 lwkt_reltoken(&lp->lwp_token);
1001 switch (lp->lwp_stat) {
1005 lwkt_reltoken(&sleep->lwp_token);
1011 lwkt_reltoken(&stop->lwp_token);
1020 if (lp->lwp_flag & LWP_SINTR) {
1022 lwkt_reltoken(&lp->lwp_token);
1026 lwkt_reltoken(&stop->lwp_token);
1034 lwkt_reltoken(&lp->lwp_token);
1040 lwkt_reltoken(&lp->lwp_token);
1043 lwkt_reltoken(&lp->lwp_token);
1056 else if (sleep != NULL)
1063 * Send the signal to the process. If the signal has an action, the action
1064 * is usually performed by the target process rather than the caller; we add
1065 * the signal to the set of pending signals for the process.
1068 * o When a stop signal is sent to a sleeping process that takes the
1069 * default action, the process is stopped without awakening it.
1070 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1071 * regardless of the signal action (eg, blocked or ignored).
1073 * Other ignored signals are discarded immediately.
1075 * If the caller wishes to call this function from a hard code section the
1076 * caller must already hold p->p_token (see kern_clock.c).
1081 ksignal(struct proc *p, int sig)
1083 lwpsignal(p, NULL, sig);
1087 * The core for ksignal. lp may be NULL, then a suitable thread
1088 * will be chosen. If not, lp MUST be a member of p.
1090 * If the caller wishes to call this function from a hard code section the
1091 * caller must already hold p->p_token.
1096 lwpsignal(struct proc *p, struct lwp *lp, int sig)
1102 if (sig > _SIG_MAXSIG || sig <= 0) {
1103 kprintf("lwpsignal: signal %d\n", sig);
1104 panic("lwpsignal signal number");
1107 KKASSERT(lp == NULL || lp->lwp_proc == p);
1110 lwkt_gettoken(&p->p_token);
1113 lwkt_gettoken(&lp->lwp_token);
1116 prop = sigprop(sig);
1119 * If proc is traced, always give parent a chance;
1120 * if signal event is tracked by procfs, give *that*
1121 * a chance, as well.
1123 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1127 * Do not try to deliver signals to an exiting lwp. Note
1128 * that we must still deliver the signal if P_WEXIT is set
1129 * in the process flags.
1131 if (lp && (lp->lwp_flag & LWP_WEXIT)) {
1133 lwkt_reltoken(&lp->lwp_token);
1136 lwkt_reltoken(&p->p_token);
1142 * If the signal is being ignored, then we forget about
1143 * it immediately. NOTE: We don't set SIGCONT in p_sigignore,
1144 * and if it is set to SIG_IGN, action will be SIG_DFL here.
1146 if (SIGISMEMBER(p->p_sigignore, sig)) {
1148 * Even if a signal is set SIG_IGN, it may still be
1149 * lurking in a kqueue.
1151 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1153 lwkt_reltoken(&lp->lwp_token);
1156 lwkt_reltoken(&p->p_token);
1160 if (SIGISMEMBER(p->p_sigcatch, sig))
1167 * If continuing, clear any pending STOP signals.
1170 SIG_STOPSIGMASK(p->p_siglist);
1172 if (prop & SA_STOP) {
1174 * If sending a tty stop signal to a member of an orphaned
1175 * process group, discard the signal here if the action
1176 * is default; don't stop the process below if sleeping,
1177 * and don't clear any pending SIGCONT.
1179 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1180 action == SIG_DFL) {
1181 lwkt_reltoken(&p->p_token);
1185 SIG_CONTSIGMASK(p->p_siglist);
1186 p->p_flag &= ~P_CONTINUED;
1191 if (p->p_stat == SSTOP) {
1193 * Nobody can handle this signal, add it to the lwp or
1194 * process pending list
1197 SIGADDSET(lp->lwp_siglist, sig);
1199 SIGADDSET(p->p_siglist, sig);
1202 * If the process is stopped and is being traced, then no
1203 * further action is necessary.
1205 if (p->p_flag & P_TRACED)
1209 * If the process is stopped and receives a KILL signal,
1210 * make the process runnable.
1212 if (sig == SIGKILL) {
1214 goto active_process;
1218 * If the process is stopped and receives a CONT signal,
1219 * then try to make the process runnable again.
1221 if (prop & SA_CONT) {
1223 * If SIGCONT is default (or ignored), we continue the
1224 * process but don't leave the signal in p_siglist, as
1225 * it has no further action. If SIGCONT is held, we
1226 * continue the process and leave the signal in
1227 * p_siglist. If the process catches SIGCONT, let it
1228 * handle the signal itself.
1230 * XXX what if the signal is being held blocked?
1232 * Token required to interlock kern_wait().
1233 * Reparenting can also cause a race so we have to
1238 lwkt_gettoken(&q->p_token);
1239 p->p_flag |= P_CONTINUED;
1241 if (action == SIG_DFL)
1242 SIGDELSET(p->p_siglist, sig);
1244 lwkt_reltoken(&q->p_token);
1246 if (action == SIG_CATCH)
1247 goto active_process;
1252 * If the process is stopped and receives another STOP
1253 * signal, we do not need to stop it again. If we did
1254 * the shell could get confused.
1256 * However, if the current/preempted lwp is part of the
1257 * process receiving the signal, we need to keep it,
1258 * so that this lwp can stop in issignal() later, as
1259 * we don't want to wait until it reaches userret!
1261 if (prop & SA_STOP) {
1262 if (lwkt_preempted_proc() == NULL ||
1263 lwkt_preempted_proc()->lwp_proc != p)
1264 SIGDELSET(p->p_siglist, sig);
1268 * Otherwise the process is stopped and it received some
1269 * signal, which does not change its stopped state.
1271 * We have to select one thread to set LWP_BREAKTSLEEP,
1272 * so that the current signal will break the sleep
1273 * as soon as a SA_CONT signal will unstop the process.
1276 /* NOTE: returns lp w/ token held */
1277 lp = find_lwp_for_signal(p, sig);
1280 (lp->lwp_stat == LSSLEEP || lp->lwp_stat == LSSTOP))
1281 lp->lwp_flag |= LWP_BREAKTSLEEP;
1286 /* else not stopped */
1290 * Never deliver a lwp-specific signal to a random lwp.
1293 /* NOTE: returns lp w/ token held */
1294 lp = find_lwp_for_signal(p, sig);
1296 if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
1297 lwkt_reltoken(&lp->lwp_token);
1305 * Deliver to the process generically if (1) the signal is being
1306 * sent to any thread or (2) we could not find a thread to deliver
1310 SIGADDSET(p->p_siglist, sig);
1315 * Deliver to a specific LWP whether it masks it or not. It will
1316 * not be dispatched if masked but we must still deliver it.
1318 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1319 (p->p_flag & P_TRACED) == 0) {
1324 * If the process receives a STOP signal which indeed needs to
1325 * stop the process, do so. If the process chose to catch the
1326 * signal, it will be treated like any other signal.
1328 if ((prop & SA_STOP) && action == SIG_DFL) {
1330 * If a child holding parent blocked, stopping
1331 * could cause deadlock. Take no action at this
1334 if (p->p_flag & P_PPWAIT) {
1335 SIGADDSET(p->p_siglist, sig);
1340 * Do not actually try to manipulate the process, but simply
1341 * stop it. Lwps will stop as soon as they safely can.
1349 * If it is a CONT signal with default action, just ignore it.
1351 if ((prop & SA_CONT) && action == SIG_DFL)
1355 * Mark signal pending at this specific thread.
1357 SIGADDSET(lp->lwp_siglist, sig);
1363 lwkt_reltoken(&lp->lwp_token);
1366 lwkt_reltoken(&p->p_token);
1372 * p->p_token must be held
1375 lwp_signotify(struct lwp *lp)
1377 ASSERT_LWKT_TOKEN_HELD(&lp->lwp_proc->p_token);
1380 if (lp->lwp_stat == LSSLEEP || lp->lwp_stat == LSSTOP) {
1382 * Thread is in tsleep.
1386 * If the thread is sleeping uninterruptibly
1387 * we can't interrupt the sleep... the signal will
1388 * be noticed when the lwp returns through
1389 * trap() or syscall().
1391 * Otherwise the signal can interrupt the sleep.
1393 * If the process is traced, the lwp will handle the
1394 * tracing in issignal() when it returns to userland.
1396 if (lp->lwp_flag & LWP_SINTR) {
1398 * Make runnable and break out of any tsleep as well.
1400 lp->lwp_flag |= LWP_BREAKTSLEEP;
1405 * Otherwise the thread is running
1407 * LSRUN does nothing with the signal, other than kicking
1408 * ourselves if we are running.
1409 * SZOMB and SIDL mean that it will either never be noticed,
1410 * or noticed very soon.
1412 * Note that lwp_thread may be NULL or may not be completely
1413 * initialized if the process is in the SIDL or SZOMB state.
1415 * For SMP we may have to forward the request to another cpu.
1416 * YYY the MP lock prevents the target process from moving
1417 * to another cpu, see kern/kern_switch.c
1419 * If the target thread is waiting on its message port,
1420 * wakeup the target thread so it can check (or ignore)
1421 * the new signal. YYY needs cleanup.
1423 if (lp == lwkt_preempted_proc()) {
1425 } else if (lp->lwp_stat == LSRUN) {
1426 struct thread *td = lp->lwp_thread;
1427 struct proc *p __debugvar = lp->lwp_proc;
1430 ("pid %d/%d NULL lwp_thread stat %d flags %08x/%08x",
1431 p->p_pid, lp->lwp_tid, lp->lwp_stat,
1432 p->p_flag, lp->lwp_flag));
1435 * To prevent a MP race with TDF_SINTR we must
1436 * schedule the thread on the correct cpu.
1439 if (td->td_gd != mycpu) {
1441 lwkt_send_ipiq(td->td_gd, signotify_remote, lp);
1444 if (td->td_flags & TDF_SINTR)
1454 * This function is called via an IPI. We will be in a critical section but
1455 * the MP lock will NOT be held. The passed lp will be held.
1457 * We must essentially repeat the code at the end of lwp_signotify(),
1458 * in particular rechecking all races. If we are still not on the
1459 * correct cpu we leave the lwp ref intact and continue the chase.
1461 * XXX this may still not be entirely correct, since we are checking
1462 * lwp_stat asynchronously.
1465 signotify_remote(void *arg)
1467 struct lwp *lp = arg;
1470 if (lp == lwkt_preempted_proc()) {
1472 } else if (lp->lwp_stat == LSRUN) {
1474 * To prevent a MP race with TDF_SINTR we must
1475 * schedule the thread on the correct cpu.
1477 td = lp->lwp_thread;
1478 if (td->td_gd != mycpu) {
1479 lwkt_send_ipiq(td->td_gd, signotify_remote, lp);
1483 if (td->td_flags & TDF_SINTR)
1492 * Caller must hold p->p_token
1495 proc_stop(struct proc *p)
1500 ASSERT_LWKT_TOKEN_HELD(&p->p_token);
1503 /* If somebody raced us, be happy with it */
1504 if (p->p_stat == SSTOP || p->p_stat == SZOMB) {
1510 FOREACH_LWP_IN_PROC(lp, p) {
1512 lwkt_gettoken(&lp->lwp_token);
1514 switch (lp->lwp_stat) {
1517 * Do nothing, we are already counted in
1524 * We're sleeping, but we will stop before
1525 * returning to userspace, so count us
1526 * as stopped as well. We set LWP_WSTOP
1527 * to signal the lwp that it should not
1528 * increase p_nstopped when reaching tstop().
1530 if ((lp->lwp_flag & LWP_WSTOP) == 0) {
1531 lp->lwp_flag |= LWP_WSTOP;
1538 * We might notify ourself, but that's not
1544 lwkt_reltoken(&lp->lwp_token);
1548 if (p->p_nstopped == p->p_nthreads) {
1550 * Token required to interlock kern_wait(). Reparenting can
1551 * also cause a race so we have to hold (q).
1555 lwkt_gettoken(&q->p_token);
1556 p->p_flag &= ~P_WAITED;
1558 if ((q->p_sigacts->ps_flag & PS_NOCLDSTOP) == 0)
1559 ksignal(p->p_pptr, SIGCHLD);
1560 lwkt_reltoken(&q->p_token);
1567 * Caller must hold proc_token
1570 proc_unstop(struct proc *p)
1574 ASSERT_LWKT_TOKEN_HELD(&p->p_token);
1577 if (p->p_stat != SSTOP) {
1582 p->p_stat = SACTIVE;
1584 FOREACH_LWP_IN_PROC(lp, p) {
1586 lwkt_gettoken(&lp->lwp_token);
1588 switch (lp->lwp_stat) {
1591 * Uh? Not stopped? Well, I guess that's okay.
1594 kprintf("proc_unstop: lwp %d/%d not sleeping\n",
1595 p->p_pid, lp->lwp_tid);
1600 * Still sleeping. Don't bother waking it up.
1601 * However, if this thread was counted as
1602 * stopped, undo this.
1604 * Nevertheless we call setrunnable() so that it
1605 * will wake up in case a signal or timeout arrived
1608 if (lp->lwp_flag & LWP_WSTOP) {
1609 lp->lwp_flag &= ~LWP_WSTOP;
1613 kprintf("proc_unstop: lwp %d/%d sleeping, not stopped\n",
1614 p->p_pid, lp->lwp_tid);
1623 lwkt_reltoken(&lp->lwp_token);
1633 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1635 sigset_t savedmask, set;
1636 struct proc *p = curproc;
1637 struct lwp *lp = curthread->td_lwp;
1638 int error, sig, hz, timevalid = 0;
1639 struct timespec rts, ets, ts;
1644 ets.tv_sec = 0; /* silence compiler warning */
1645 ets.tv_nsec = 0; /* silence compiler warning */
1646 SIG_CANTMASK(waitset);
1647 savedmask = lp->lwp_sigmask;
1650 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1651 timeout->tv_nsec < 1000000000) {
1653 getnanouptime(&rts);
1655 timespecadd(&ets, timeout);
1660 set = lwp_sigpend(lp);
1661 SIGSETAND(set, waitset);
1662 if ((sig = sig_ffs(&set)) != 0) {
1663 SIGFILLSET(lp->lwp_sigmask);
1664 SIGDELSET(lp->lwp_sigmask, sig);
1665 SIG_CANTMASK(lp->lwp_sigmask);
1666 sig = issignal(lp, 1);
1668 * It may be a STOP signal, in the case, issignal
1669 * returns 0, because we may stop there, and new
1670 * signal can come in, we should restart if we got
1680 * Previous checking got nothing, and we retried but still
1681 * got nothing, we should return the error status.
1687 * POSIX says this must be checked after looking for pending
1691 if (timevalid == 0) {
1695 getnanouptime(&rts);
1696 if (timespeccmp(&rts, &ets, >=)) {
1701 timespecsub(&ts, &rts);
1702 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1703 hz = tvtohz_high(&tv);
1708 lp->lwp_sigmask = savedmask;
1709 SIGSETNAND(lp->lwp_sigmask, waitset);
1711 * We won't ever be woken up. Instead, our sleep will
1712 * be broken in lwpsignal().
1714 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1716 if (error == ERESTART) {
1717 /* can not restart a timeout wait. */
1719 } else if (error == EAGAIN) {
1720 /* will calculate timeout by ourself. */
1727 lp->lwp_sigmask = savedmask;
1730 bzero(info, sizeof(*info));
1731 info->si_signo = sig;
1732 lwp_delsig(lp, sig); /* take the signal! */
1734 if (sig == SIGKILL) {
1747 sys_sigtimedwait(struct sigtimedwait_args *uap)
1750 struct timespec *timeout;
1756 error = copyin(uap->timeout, &ts, sizeof(ts));
1763 error = copyin(uap->set, &set, sizeof(set));
1766 error = kern_sigtimedwait(set, &info, timeout);
1770 error = copyout(&info, uap->info, sizeof(info));
1771 /* Repost if we got an error. */
1775 * This could transform a thread-specific signal to another
1776 * thread / process pending signal.
1779 ksignal(curproc, info.si_signo);
1781 uap->sysmsg_result = info.si_signo;
1790 sys_sigwaitinfo(struct sigwaitinfo_args *uap)
1796 error = copyin(uap->set, &set, sizeof(set));
1799 error = kern_sigtimedwait(set, &info, NULL);
1803 error = copyout(&info, uap->info, sizeof(info));
1804 /* Repost if we got an error. */
1808 * This could transform a thread-specific signal to another
1809 * thread / process pending signal.
1812 ksignal(curproc, info.si_signo);
1814 uap->sysmsg_result = info.si_signo;
1820 * If the current process has received a signal that would interrupt a
1821 * system call, return EINTR or ERESTART as appropriate.
1824 iscaught(struct lwp *lp)
1826 struct proc *p = lp->lwp_proc;
1830 if ((sig = CURSIG(lp)) != 0) {
1831 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1836 return(EWOULDBLOCK);
1840 * If the current process has received a signal (should be caught or cause
1841 * termination, should interrupt current syscall), return the signal number.
1842 * Stop signals with default action are processed immediately, then cleared;
1843 * they aren't returned. This is checked after each entry to the system for
1844 * a syscall or trap (though this can usually be done without calling issignal
1845 * by checking the pending signal masks in the CURSIG macro).
1847 * This routine is called via CURSIG/__cursig. We will acquire and release
1848 * p->p_token but if the caller needs to interlock the test the caller must
1849 * also hold p->p_token.
1851 * while (sig = CURSIG(curproc))
1857 issignal(struct lwp *lp, int maytrace)
1859 struct proc *p = lp->lwp_proc;
1863 lwkt_gettoken(&p->p_token);
1866 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1869 * If this process is supposed to stop, stop this thread.
1871 if (p->p_stat == SSTOP)
1874 mask = lwp_sigpend(lp);
1875 SIGSETNAND(mask, lp->lwp_sigmask);
1876 if (p->p_flag & P_PPWAIT)
1877 SIG_STOPSIGMASK(mask);
1878 if (SIGISEMPTY(mask)) { /* no signal to send */
1879 lwkt_reltoken(&p->p_token);
1882 sig = sig_ffs(&mask);
1884 STOPEVENT(p, S_SIG, sig);
1887 * We should see pending but ignored signals
1888 * only if P_TRACED was on when they were posted.
1890 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1891 lwp_delsig(lp, sig);
1894 if (maytrace && (p->p_flag & P_TRACED) && (p->p_flag & P_PPWAIT) == 0) {
1896 * If traced, always stop, and stay stopped until
1897 * released by the parent.
1899 * NOTE: SSTOP may get cleared during the loop,
1900 * but we do not re-notify the parent if we have
1901 * to loop several times waiting for the parent
1902 * to let us continue.
1904 * XXX not sure if this is still true
1910 } while (!trace_req(p) && (p->p_flag & P_TRACED));
1913 * If parent wants us to take the signal,
1914 * then it will leave it in p->p_xstat;
1915 * otherwise we just look for signals again.
1917 lwp_delsig(lp, sig); /* clear old signal */
1923 * Put the new signal into p_siglist. If the
1924 * signal is being masked, look for other signals.
1926 * XXX lwp might need a call to ksignal()
1928 SIGADDSET(p->p_siglist, sig);
1929 if (SIGISMEMBER(lp->lwp_sigmask, sig))
1933 * If the traced bit got turned off, go back up
1934 * to the top to rescan signals. This ensures
1935 * that p_sig* and ps_sigact are consistent.
1937 if ((p->p_flag & P_TRACED) == 0)
1941 prop = sigprop(sig);
1944 * Decide whether the signal should be returned.
1945 * Return the signal's number, or fall through
1946 * to clear it from the pending mask.
1948 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1949 case (intptr_t)SIG_DFL:
1951 * Don't take default actions on system processes.
1953 if (p->p_pid <= 1) {
1956 * Are you sure you want to ignore SIGSEGV
1959 kprintf("Process (pid %lu) got signal %d\n",
1960 (u_long)p->p_pid, sig);
1962 break; /* == ignore */
1966 * Handle the in-kernel checkpoint action
1968 if (prop & SA_CKPT) {
1969 checkpoint_signal_handler(lp);
1974 * If there is a pending stop signal to process
1975 * with default action, stop here,
1976 * then clear the signal. However,
1977 * if process is member of an orphaned
1978 * process group, ignore tty stop signals.
1980 if (prop & SA_STOP) {
1981 if (p->p_flag & P_TRACED ||
1982 (p->p_pgrp->pg_jobc == 0 &&
1984 break; /* == ignore */
1989 } else if (prop & SA_IGNORE) {
1991 * Except for SIGCONT, shouldn't get here.
1992 * Default action is to ignore; drop it.
1994 break; /* == ignore */
1996 lwkt_reltoken(&p->p_token);
2002 case (intptr_t)SIG_IGN:
2004 * Masking above should prevent us ever trying
2005 * to take action on an ignored signal other
2006 * than SIGCONT, unless process is traced.
2008 if ((prop & SA_CONT) == 0 &&
2009 (p->p_flag & P_TRACED) == 0)
2010 kprintf("issignal\n");
2011 break; /* == ignore */
2015 * This signal has an action, let
2016 * postsig() process it.
2018 lwkt_reltoken(&p->p_token);
2021 lwp_delsig(lp, sig); /* take the signal! */
2027 * Take the action for the specified signal
2028 * from the current set of pending signals.
2030 * Caller must hold p->p_token
2035 struct lwp *lp = curthread->td_lwp;
2036 struct proc *p = lp->lwp_proc;
2037 struct sigacts *ps = p->p_sigacts;
2039 sigset_t returnmask;
2042 KASSERT(sig != 0, ("postsig"));
2044 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
2047 * If we are a virtual kernel running an emulated user process
2048 * context, switch back to the virtual kernel context before
2049 * trying to post the signal.
2051 if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
2052 struct trapframe *tf = lp->lwp_md.md_regs;
2054 vkernel_trap(lp, tf);
2057 lwp_delsig(lp, sig);
2058 action = ps->ps_sigact[_SIG_IDX(sig)];
2060 if (KTRPOINT(lp->lwp_thread, KTR_PSIG))
2061 ktrpsig(lp, sig, action, lp->lwp_flag & LWP_OLDMASK ?
2062 &lp->lwp_oldsigmask : &lp->lwp_sigmask, 0);
2064 STOPEVENT(p, S_SIG, sig);
2066 if (action == SIG_DFL) {
2068 * Default action, where the default is to kill
2069 * the process. (Other cases were ignored above.)
2075 * If we get here, the signal must be caught.
2077 KASSERT(action != SIG_IGN && !SIGISMEMBER(lp->lwp_sigmask, sig),
2078 ("postsig action"));
2083 * Reset the signal handler if asked to
2085 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2087 * See kern_sigaction() for origin of this code.
2089 SIGDELSET(p->p_sigcatch, sig);
2090 if (sig != SIGCONT &&
2091 sigprop(sig) & SA_IGNORE)
2092 SIGADDSET(p->p_sigignore, sig);
2093 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2097 * Handle the mailbox case. Copyout to the appropriate
2098 * location but do not generate a signal frame. The system
2099 * call simply returns EINTR and the user is responsible for
2100 * polling the mailbox.
2102 if (SIGISMEMBER(ps->ps_sigmailbox, sig)) {
2104 copyout(&sig_copy, (void *)action, sizeof(int));
2105 lwkt_gettoken(&curproc->p_token);
2106 curproc->p_flag |= P_MAILBOX;
2107 lwkt_reltoken(&curproc->p_token);
2113 * Set the signal mask and calculate the mask to restore
2114 * when the signal function returns.
2116 * Special case: user has done a sigsuspend. Here the
2117 * current mask is not of interest, but rather the
2118 * mask from before the sigsuspend is what we want
2119 * restored after the signal processing is completed.
2121 if (lp->lwp_flag & LWP_OLDMASK) {
2122 returnmask = lp->lwp_oldsigmask;
2123 lp->lwp_flag &= ~LWP_OLDMASK;
2125 returnmask = lp->lwp_sigmask;
2128 SIGSETOR(lp->lwp_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2129 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2130 SIGADDSET(lp->lwp_sigmask, sig);
2133 lp->lwp_ru.ru_nsignals++;
2134 if (lp->lwp_sig != sig) {
2137 code = lp->lwp_code;
2141 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
2148 * Kill the current process for stated reason.
2151 killproc(struct proc *p, char *why)
2153 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n",
2154 p->p_pid, p->p_comm,
2155 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2156 ksignal(p, SIGKILL);
2160 * Force the current process to exit with the specified signal, dumping core
2161 * if appropriate. We bypass the normal tests for masked and caught signals,
2162 * allowing unrecoverable failures to terminate the process without changing
2163 * signal state. Mark the accounting record with the signal termination.
2164 * If dumping core, save the signal number for the debugger. Calls exit and
2167 * This routine does not return.
2170 sigexit(struct lwp *lp, int sig)
2172 struct proc *p = lp->lwp_proc;
2174 lwkt_gettoken(&p->p_token);
2175 p->p_acflag |= AXSIG;
2176 if (sigprop(sig) & SA_CORE) {
2179 * Log signals which would cause core dumps
2180 * (Log as LOG_INFO to appease those who don't want
2182 * XXX : Todo, as well as euid, write out ruid too
2184 if (coredump(lp, sig) == 0)
2186 if (kern_logsigexit)
2188 "pid %d (%s), uid %d: exited on signal %d%s\n",
2189 p->p_pid, p->p_comm,
2190 p->p_ucred ? p->p_ucred->cr_uid : -1,
2192 sig & WCOREFLAG ? " (core dumped)" : "");
2194 lwkt_reltoken(&p->p_token);
2195 exit1(W_EXITCODE(0, sig));
2199 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2200 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2201 sizeof(corefilename), "process corefile name format string");
2204 * expand_name(name, uid, pid)
2205 * Expand the name described in corefilename, using name, uid, and pid.
2206 * corefilename is a kprintf-like string, with three format specifiers:
2207 * %N name of process ("name")
2208 * %P process id (pid)
2210 * For example, "%N.core" is the default; they can be disabled completely
2211 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2212 * This is controlled by the sysctl variable kern.corefile (see above).
2216 expand_name(const char *name, uid_t uid, pid_t pid)
2219 char buf[11]; /* Buffer for pid/uid -- max 4B */
2221 char *format = corefilename;
2224 temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
2227 namelen = strlen(name);
2228 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2230 switch (format[i]) {
2231 case '%': /* Format character */
2233 switch (format[i]) {
2237 case 'N': /* process name */
2238 if ((n + namelen) > MAXPATHLEN) {
2239 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
2240 pid, name, uid, temp, name);
2241 kfree(temp, M_TEMP);
2244 memcpy(temp+n, name, namelen);
2247 case 'P': /* process id */
2248 l = ksprintf(buf, "%u", pid);
2249 if ((n + l) > MAXPATHLEN) {
2250 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
2251 pid, name, uid, temp, name);
2252 kfree(temp, M_TEMP);
2255 memcpy(temp+n, buf, l);
2258 case 'U': /* user id */
2259 l = ksprintf(buf, "%u", uid);
2260 if ((n + l) > MAXPATHLEN) {
2261 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
2262 pid, name, uid, temp, name);
2263 kfree(temp, M_TEMP);
2266 memcpy(temp+n, buf, l);
2270 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
2274 temp[n++] = format[i];
2282 * Dump a process' core. The main routine does some
2283 * policy checking, and creates the name of the coredump;
2284 * then it passes on a vnode and a size limit to the process-specific
2285 * coredump routine if there is one; if there _is not_ one, it returns
2286 * ENOSYS; otherwise it returns the error from the process-specific routine.
2288 * The parameter `lp' is the lwp which triggered the coredump.
2292 coredump(struct lwp *lp, int sig)
2294 struct proc *p = lp->lwp_proc;
2296 struct ucred *cred = p->p_ucred;
2298 struct nlookupdata nd;
2301 char *name; /* name of corefile */
2304 STOPEVENT(p, S_CORE, 0);
2306 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
2310 * Note that the bulk of limit checking is done after
2311 * the corefile is created. The exception is if the limit
2312 * for corefiles is 0, in which case we don't bother
2313 * creating the corefile at all. This layout means that
2314 * a corefile is truncated instead of not being created,
2315 * if it is larger than the limit.
2317 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2321 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
2324 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
2326 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2327 kfree(name, M_TEMP);
2333 nd.nl_open_vp = NULL;
2337 lf.l_whence = SEEK_SET;
2340 lf.l_type = F_WRLCK;
2341 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, 0);
2345 /* Don't dump to non-regular files or files with links. */
2346 if (vp->v_type != VREG ||
2347 VOP_GETATTR(vp, &vattr) || vattr.va_nlink != 1) {
2352 /* Don't dump to files current user does not own */
2353 if (vattr.va_uid != p->p_ucred->cr_uid) {
2359 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2361 VOP_SETATTR(vp, &vattr, cred);
2362 p->p_acflag |= ACORE;
2365 error = p->p_sysent->sv_coredump ?
2366 p->p_sysent->sv_coredump(lp, sig, vp, limit) : ENOSYS;
2369 lf.l_type = F_UNLCK;
2370 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, 0);
2372 error1 = vn_close(vp, FWRITE);
2379 * Nonexistent system call-- signal process (may want to handle it).
2380 * Flag error in case process won't see signal immediately (blocked or ignored).
2386 sys_nosys(struct nosys_args *args)
2388 lwpsignal(curproc, curthread->td_lwp, SIGSYS);
2393 * Send a SIGIO or SIGURG signal to a process or process group using
2394 * stored credentials rather than those of the current process.
2397 pgsigio(struct sigio *sigio, int sig, int checkctty)
2402 if (sigio->sio_pgid > 0) {
2403 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
2405 ksignal(sigio->sio_proc, sig);
2406 } else if (sigio->sio_pgid < 0) {
2408 struct pgrp *pg = sigio->sio_pgrp;
2411 * Must interlock all signals against fork
2414 lockmgr(&pg->pg_lock, LK_EXCLUSIVE);
2415 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
2416 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
2417 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2420 lockmgr(&pg->pg_lock, LK_RELEASE);
2426 filt_sigattach(struct knote *kn)
2428 struct proc *p = curproc;
2430 kn->kn_ptr.p_proc = p;
2431 kn->kn_flags |= EV_CLEAR; /* automatically set */
2433 /* XXX lock the proc here while adding to the list? */
2434 knote_insert(&p->p_klist, kn);
2440 filt_sigdetach(struct knote *kn)
2442 struct proc *p = kn->kn_ptr.p_proc;
2444 knote_remove(&p->p_klist, kn);
2448 * signal knotes are shared with proc knotes, so we apply a mask to
2449 * the hint in order to differentiate them from process hints. This
2450 * could be avoided by using a signal-specific knote list, but probably
2451 * isn't worth the trouble.
2454 filt_signal(struct knote *kn, long hint)
2456 if (hint & NOTE_SIGNAL) {
2457 hint &= ~NOTE_SIGNAL;
2459 if (kn->kn_id == hint)
2462 return (kn->kn_data != 0);