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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include "opt_procdesc.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
51 #include <sys/capability.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/imgact.h>
56 #include <sys/kernel.h>
58 #include <sys/ktrace.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
64 #include <sys/procdesc.h>
65 #include <sys/posix4.h>
66 #include <sys/pioctl.h>
67 #include <sys/racct.h>
68 #include <sys/resourcevar.h>
71 #include <sys/sleepqueue.h>
75 #include <sys/syscallsubr.h>
76 #include <sys/sysctl.h>
77 #include <sys/sysent.h>
78 #include <sys/syslog.h>
79 #include <sys/sysproto.h>
80 #include <sys/timers.h>
81 #include <sys/unistd.h>
84 #include <vm/vm_extern.h>
89 #include <machine/cpu.h>
91 #include <security/audit/audit.h>
93 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
95 SDT_PROVIDER_DECLARE(proc);
96 SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send);
97 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *");
98 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *");
99 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int");
100 SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear);
101 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int");
102 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
103 SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard);
104 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
105 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
106 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
108 static int coredump(struct thread *);
109 static char *expand_name(const char *, uid_t, pid_t, struct thread *, int);
110 static int killpg1(struct thread *td, int sig, int pgid, int all,
112 static int issignal(struct thread *td, int stop_allowed);
113 static int sigprop(int sig);
114 static void tdsigwakeup(struct thread *, int, sig_t, int);
115 static void sig_suspend_threads(struct thread *, struct proc *, int);
116 static int filt_sigattach(struct knote *kn);
117 static void filt_sigdetach(struct knote *kn);
118 static int filt_signal(struct knote *kn, long hint);
119 static struct thread *sigtd(struct proc *p, int sig, int prop);
120 static void sigqueue_start(void);
122 static uma_zone_t ksiginfo_zone = NULL;
123 struct filterops sig_filtops = {
125 .f_attach = filt_sigattach,
126 .f_detach = filt_sigdetach,
127 .f_event = filt_signal,
130 static int kern_logsigexit = 1;
131 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
133 "Log processes quitting on abnormal signals to syslog(3)");
135 static int kern_forcesigexit = 1;
136 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
137 &kern_forcesigexit, 0, "Force trap signal to be handled");
139 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
140 "POSIX real time signal");
142 static int max_pending_per_proc = 128;
143 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
144 &max_pending_per_proc, 0, "Max pending signals per proc");
146 static int preallocate_siginfo = 1024;
147 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
148 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
149 &preallocate_siginfo, 0, "Preallocated signal memory size");
151 static int signal_overflow = 0;
152 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
153 &signal_overflow, 0, "Number of signals overflew");
155 static int signal_alloc_fail = 0;
156 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
157 &signal_alloc_fail, 0, "signals failed to be allocated");
159 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
162 * Policy -- Can ucred cr1 send SIGIO to process cr2?
163 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
164 * in the right situations.
166 #define CANSIGIO(cr1, cr2) \
167 ((cr1)->cr_uid == 0 || \
168 (cr1)->cr_ruid == (cr2)->cr_ruid || \
169 (cr1)->cr_uid == (cr2)->cr_ruid || \
170 (cr1)->cr_ruid == (cr2)->cr_uid || \
171 (cr1)->cr_uid == (cr2)->cr_uid)
173 static int sugid_coredump;
174 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
175 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
177 static int do_coredump = 1;
178 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
179 &do_coredump, 0, "Enable/Disable coredumps");
181 static int set_core_nodump_flag = 0;
182 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
183 0, "Enable setting the NODUMP flag on coredump files");
186 * Signal properties and actions.
187 * The array below categorizes the signals and their default actions
188 * according to the following properties:
190 #define SA_KILL 0x01 /* terminates process by default */
191 #define SA_CORE 0x02 /* ditto and coredumps */
192 #define SA_STOP 0x04 /* suspend process */
193 #define SA_TTYSTOP 0x08 /* ditto, from tty */
194 #define SA_IGNORE 0x10 /* ignore by default */
195 #define SA_CONT 0x20 /* continue if suspended */
196 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
197 #define SA_PROC 0x80 /* deliverable to any thread */
199 static int sigproptbl[NSIG] = {
200 SA_KILL|SA_PROC, /* SIGHUP */
201 SA_KILL|SA_PROC, /* SIGINT */
202 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
203 SA_KILL|SA_CORE, /* SIGILL */
204 SA_KILL|SA_CORE, /* SIGTRAP */
205 SA_KILL|SA_CORE, /* SIGABRT */
206 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
207 SA_KILL|SA_CORE, /* SIGFPE */
208 SA_KILL|SA_PROC, /* SIGKILL */
209 SA_KILL|SA_CORE, /* SIGBUS */
210 SA_KILL|SA_CORE, /* SIGSEGV */
211 SA_KILL|SA_CORE, /* SIGSYS */
212 SA_KILL|SA_PROC, /* SIGPIPE */
213 SA_KILL|SA_PROC, /* SIGALRM */
214 SA_KILL|SA_PROC, /* SIGTERM */
215 SA_IGNORE|SA_PROC, /* SIGURG */
216 SA_STOP|SA_PROC, /* SIGSTOP */
217 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
218 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
219 SA_IGNORE|SA_PROC, /* SIGCHLD */
220 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
221 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
222 SA_IGNORE|SA_PROC, /* SIGIO */
223 SA_KILL, /* SIGXCPU */
224 SA_KILL, /* SIGXFSZ */
225 SA_KILL|SA_PROC, /* SIGVTALRM */
226 SA_KILL|SA_PROC, /* SIGPROF */
227 SA_IGNORE|SA_PROC, /* SIGWINCH */
228 SA_IGNORE|SA_PROC, /* SIGINFO */
229 SA_KILL|SA_PROC, /* SIGUSR1 */
230 SA_KILL|SA_PROC, /* SIGUSR2 */
233 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
238 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
239 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
240 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
241 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
242 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
243 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
247 ksiginfo_alloc(int wait)
254 if (ksiginfo_zone != NULL)
255 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
260 ksiginfo_free(ksiginfo_t *ksi)
262 uma_zfree(ksiginfo_zone, ksi);
266 ksiginfo_tryfree(ksiginfo_t *ksi)
268 if (!(ksi->ksi_flags & KSI_EXT)) {
269 uma_zfree(ksiginfo_zone, ksi);
276 sigqueue_init(sigqueue_t *list, struct proc *p)
278 SIGEMPTYSET(list->sq_signals);
279 SIGEMPTYSET(list->sq_kill);
280 TAILQ_INIT(&list->sq_list);
282 list->sq_flags = SQ_INIT;
286 * Get a signal's ksiginfo.
288 * 0 - signal not found
289 * others - signal number
292 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
294 struct proc *p = sq->sq_proc;
295 struct ksiginfo *ksi, *next;
298 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
300 if (!SIGISMEMBER(sq->sq_signals, signo))
303 if (SIGISMEMBER(sq->sq_kill, signo)) {
305 SIGDELSET(sq->sq_kill, signo);
308 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
309 if (ksi->ksi_signo == signo) {
311 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
312 ksi->ksi_sigq = NULL;
313 ksiginfo_copy(ksi, si);
314 if (ksiginfo_tryfree(ksi) && p != NULL)
323 SIGDELSET(sq->sq_signals, signo);
324 si->ksi_signo = signo;
329 sigqueue_take(ksiginfo_t *ksi)
335 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
339 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
340 ksi->ksi_sigq = NULL;
341 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
344 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
345 kp = TAILQ_NEXT(kp, ksi_link)) {
346 if (kp->ksi_signo == ksi->ksi_signo)
349 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
350 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
354 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
356 struct proc *p = sq->sq_proc;
357 struct ksiginfo *ksi;
360 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
362 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
363 SIGADDSET(sq->sq_kill, signo);
367 /* directly insert the ksi, don't copy it */
368 if (si->ksi_flags & KSI_INS) {
369 if (si->ksi_flags & KSI_HEAD)
370 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
372 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
377 if (__predict_false(ksiginfo_zone == NULL)) {
378 SIGADDSET(sq->sq_kill, signo);
382 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
385 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
391 ksiginfo_copy(si, ksi);
392 ksi->ksi_signo = signo;
393 if (si->ksi_flags & KSI_HEAD)
394 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
396 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
400 if ((si->ksi_flags & KSI_TRAP) != 0 ||
401 (si->ksi_flags & KSI_SIGQ) == 0) {
403 SIGADDSET(sq->sq_kill, signo);
412 SIGADDSET(sq->sq_signals, signo);
417 sigqueue_flush(sigqueue_t *sq)
419 struct proc *p = sq->sq_proc;
422 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
425 PROC_LOCK_ASSERT(p, MA_OWNED);
427 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
428 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
429 ksi->ksi_sigq = NULL;
430 if (ksiginfo_tryfree(ksi) && p != NULL)
434 SIGEMPTYSET(sq->sq_signals);
435 SIGEMPTYSET(sq->sq_kill);
439 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
442 struct proc *p1, *p2;
443 ksiginfo_t *ksi, *next;
445 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
446 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
449 /* Move siginfo to target list */
450 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
451 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
452 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
455 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
462 /* Move pending bits to target list */
464 SIGSETAND(tmp, *set);
465 SIGSETOR(dst->sq_kill, tmp);
466 SIGSETNAND(src->sq_kill, tmp);
468 tmp = src->sq_signals;
469 SIGSETAND(tmp, *set);
470 SIGSETOR(dst->sq_signals, tmp);
471 SIGSETNAND(src->sq_signals, tmp);
476 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
481 SIGADDSET(set, signo);
482 sigqueue_move_set(src, dst, &set);
487 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
489 struct proc *p = sq->sq_proc;
490 ksiginfo_t *ksi, *next;
492 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
494 /* Remove siginfo queue */
495 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
496 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
497 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
498 ksi->ksi_sigq = NULL;
499 if (ksiginfo_tryfree(ksi) && p != NULL)
503 SIGSETNAND(sq->sq_kill, *set);
504 SIGSETNAND(sq->sq_signals, *set);
508 sigqueue_delete(sigqueue_t *sq, int signo)
513 SIGADDSET(set, signo);
514 sigqueue_delete_set(sq, &set);
517 /* Remove a set of signals for a process */
519 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
524 PROC_LOCK_ASSERT(p, MA_OWNED);
526 sigqueue_init(&worklist, NULL);
527 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
529 FOREACH_THREAD_IN_PROC(p, td0)
530 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
532 sigqueue_flush(&worklist);
536 sigqueue_delete_proc(struct proc *p, int signo)
541 SIGADDSET(set, signo);
542 sigqueue_delete_set_proc(p, &set);
546 sigqueue_delete_stopmask_proc(struct proc *p)
551 SIGADDSET(set, SIGSTOP);
552 SIGADDSET(set, SIGTSTP);
553 SIGADDSET(set, SIGTTIN);
554 SIGADDSET(set, SIGTTOU);
555 sigqueue_delete_set_proc(p, &set);
559 * Determine signal that should be delivered to process p, the current
560 * process, 0 if none. If there is a pending stop signal with default
561 * action, the process stops in issignal().
564 cursig(struct thread *td, int stop_allowed)
566 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
567 KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
568 stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
569 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
570 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
571 return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
575 * Arrange for ast() to handle unmasked pending signals on return to user
576 * mode. This must be called whenever a signal is added to td_sigqueue or
577 * unmasked in td_sigmask.
580 signotify(struct thread *td)
586 PROC_LOCK_ASSERT(p, MA_OWNED);
588 if (SIGPENDING(td)) {
590 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
596 sigonstack(size_t sp)
598 struct thread *td = curthread;
600 return ((td->td_pflags & TDP_ALTSTACK) ?
601 #if defined(COMPAT_43)
602 ((td->td_sigstk.ss_size == 0) ?
603 (td->td_sigstk.ss_flags & SS_ONSTACK) :
604 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
606 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
615 if (sig > 0 && sig < NSIG)
616 return (sigproptbl[_SIG_IDX(sig)]);
621 sig_ffs(sigset_t *set)
625 for (i = 0; i < _SIG_WORDS; i++)
627 return (ffs(set->__bits[i]) + (i * 32));
638 kern_sigaction(td, sig, act, oact, flags)
641 struct sigaction *act, *oact;
645 struct proc *p = td->td_proc;
647 if (!_SIG_VALID(sig))
652 mtx_lock(&ps->ps_mtx);
654 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
656 if (SIGISMEMBER(ps->ps_sigonstack, sig))
657 oact->sa_flags |= SA_ONSTACK;
658 if (!SIGISMEMBER(ps->ps_sigintr, sig))
659 oact->sa_flags |= SA_RESTART;
660 if (SIGISMEMBER(ps->ps_sigreset, sig))
661 oact->sa_flags |= SA_RESETHAND;
662 if (SIGISMEMBER(ps->ps_signodefer, sig))
663 oact->sa_flags |= SA_NODEFER;
664 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
665 oact->sa_flags |= SA_SIGINFO;
667 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
669 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
670 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
671 oact->sa_flags |= SA_NOCLDSTOP;
672 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
673 oact->sa_flags |= SA_NOCLDWAIT;
676 if ((sig == SIGKILL || sig == SIGSTOP) &&
677 act->sa_handler != SIG_DFL) {
678 mtx_unlock(&ps->ps_mtx);
684 * Change setting atomically.
687 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
688 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
689 if (act->sa_flags & SA_SIGINFO) {
690 ps->ps_sigact[_SIG_IDX(sig)] =
691 (__sighandler_t *)act->sa_sigaction;
692 SIGADDSET(ps->ps_siginfo, sig);
694 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
695 SIGDELSET(ps->ps_siginfo, sig);
697 if (!(act->sa_flags & SA_RESTART))
698 SIGADDSET(ps->ps_sigintr, sig);
700 SIGDELSET(ps->ps_sigintr, sig);
701 if (act->sa_flags & SA_ONSTACK)
702 SIGADDSET(ps->ps_sigonstack, sig);
704 SIGDELSET(ps->ps_sigonstack, sig);
705 if (act->sa_flags & SA_RESETHAND)
706 SIGADDSET(ps->ps_sigreset, sig);
708 SIGDELSET(ps->ps_sigreset, sig);
709 if (act->sa_flags & SA_NODEFER)
710 SIGADDSET(ps->ps_signodefer, sig);
712 SIGDELSET(ps->ps_signodefer, sig);
713 if (sig == SIGCHLD) {
714 if (act->sa_flags & SA_NOCLDSTOP)
715 ps->ps_flag |= PS_NOCLDSTOP;
717 ps->ps_flag &= ~PS_NOCLDSTOP;
718 if (act->sa_flags & SA_NOCLDWAIT) {
720 * Paranoia: since SA_NOCLDWAIT is implemented
721 * by reparenting the dying child to PID 1 (and
722 * trust it to reap the zombie), PID 1 itself
723 * is forbidden to set SA_NOCLDWAIT.
726 ps->ps_flag &= ~PS_NOCLDWAIT;
728 ps->ps_flag |= PS_NOCLDWAIT;
730 ps->ps_flag &= ~PS_NOCLDWAIT;
731 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
732 ps->ps_flag |= PS_CLDSIGIGN;
734 ps->ps_flag &= ~PS_CLDSIGIGN;
737 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
738 * and for signals set to SIG_DFL where the default is to
739 * ignore. However, don't put SIGCONT in ps_sigignore, as we
740 * have to restart the process.
742 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
743 (sigprop(sig) & SA_IGNORE &&
744 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
745 /* never to be seen again */
746 sigqueue_delete_proc(p, sig);
748 /* easier in psignal */
749 SIGADDSET(ps->ps_sigignore, sig);
750 SIGDELSET(ps->ps_sigcatch, sig);
752 SIGDELSET(ps->ps_sigignore, sig);
753 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
754 SIGDELSET(ps->ps_sigcatch, sig);
756 SIGADDSET(ps->ps_sigcatch, sig);
758 #ifdef COMPAT_FREEBSD4
759 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
760 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
761 (flags & KSA_FREEBSD4) == 0)
762 SIGDELSET(ps->ps_freebsd4, sig);
764 SIGADDSET(ps->ps_freebsd4, sig);
767 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
768 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
769 (flags & KSA_OSIGSET) == 0)
770 SIGDELSET(ps->ps_osigset, sig);
772 SIGADDSET(ps->ps_osigset, sig);
775 mtx_unlock(&ps->ps_mtx);
780 #ifndef _SYS_SYSPROTO_H_
781 struct sigaction_args {
783 struct sigaction *act;
784 struct sigaction *oact;
788 sys_sigaction(td, uap)
790 register struct sigaction_args *uap;
792 struct sigaction act, oact;
793 register struct sigaction *actp, *oactp;
796 actp = (uap->act != NULL) ? &act : NULL;
797 oactp = (uap->oact != NULL) ? &oact : NULL;
799 error = copyin(uap->act, actp, sizeof(act));
803 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
805 error = copyout(oactp, uap->oact, sizeof(oact));
809 #ifdef COMPAT_FREEBSD4
810 #ifndef _SYS_SYSPROTO_H_
811 struct freebsd4_sigaction_args {
813 struct sigaction *act;
814 struct sigaction *oact;
818 freebsd4_sigaction(td, uap)
820 register struct freebsd4_sigaction_args *uap;
822 struct sigaction act, oact;
823 register struct sigaction *actp, *oactp;
827 actp = (uap->act != NULL) ? &act : NULL;
828 oactp = (uap->oact != NULL) ? &oact : NULL;
830 error = copyin(uap->act, actp, sizeof(act));
834 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
836 error = copyout(oactp, uap->oact, sizeof(oact));
839 #endif /* COMAPT_FREEBSD4 */
841 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
842 #ifndef _SYS_SYSPROTO_H_
843 struct osigaction_args {
845 struct osigaction *nsa;
846 struct osigaction *osa;
852 register struct osigaction_args *uap;
854 struct osigaction sa;
855 struct sigaction nsa, osa;
856 register struct sigaction *nsap, *osap;
859 if (uap->signum <= 0 || uap->signum >= ONSIG)
862 nsap = (uap->nsa != NULL) ? &nsa : NULL;
863 osap = (uap->osa != NULL) ? &osa : NULL;
866 error = copyin(uap->nsa, &sa, sizeof(sa));
869 nsap->sa_handler = sa.sa_handler;
870 nsap->sa_flags = sa.sa_flags;
871 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
873 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
874 if (osap && !error) {
875 sa.sa_handler = osap->sa_handler;
876 sa.sa_flags = osap->sa_flags;
877 SIG2OSIG(osap->sa_mask, sa.sa_mask);
878 error = copyout(&sa, uap->osa, sizeof(sa));
883 #if !defined(__i386__)
884 /* Avoid replicating the same stub everywhere */
888 struct osigreturn_args *uap;
891 return (nosys(td, (struct nosys_args *)uap));
894 #endif /* COMPAT_43 */
897 * Initialize signal state for process 0;
898 * set to ignore signals that are ignored by default.
909 mtx_lock(&ps->ps_mtx);
910 for (i = 1; i <= NSIG; i++)
911 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
912 SIGADDSET(ps->ps_sigignore, i);
913 mtx_unlock(&ps->ps_mtx);
918 * Reset signals for an exec of the specified process.
921 execsigs(struct proc *p)
928 * Reset caught signals. Held signals remain held
929 * through td_sigmask (unless they were caught,
930 * and are now ignored by default).
932 PROC_LOCK_ASSERT(p, MA_OWNED);
933 td = FIRST_THREAD_IN_PROC(p);
935 mtx_lock(&ps->ps_mtx);
936 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
937 sig = sig_ffs(&ps->ps_sigcatch);
938 SIGDELSET(ps->ps_sigcatch, sig);
939 if (sigprop(sig) & SA_IGNORE) {
941 SIGADDSET(ps->ps_sigignore, sig);
942 sigqueue_delete_proc(p, sig);
944 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
947 * Reset stack state to the user stack.
948 * Clear set of signals caught on the signal stack.
950 td->td_sigstk.ss_flags = SS_DISABLE;
951 td->td_sigstk.ss_size = 0;
952 td->td_sigstk.ss_sp = 0;
953 td->td_pflags &= ~TDP_ALTSTACK;
955 * Reset no zombies if child dies flag as Solaris does.
957 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
958 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
959 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
960 mtx_unlock(&ps->ps_mtx);
966 * Manipulate signal mask.
969 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
972 sigset_t new_block, oset1;
977 if (!(flags & SIGPROCMASK_PROC_LOCKED))
980 *oset = td->td_sigmask;
987 oset1 = td->td_sigmask;
988 SIGSETOR(td->td_sigmask, *set);
989 new_block = td->td_sigmask;
990 SIGSETNAND(new_block, oset1);
993 SIGSETNAND(td->td_sigmask, *set);
998 oset1 = td->td_sigmask;
999 if (flags & SIGPROCMASK_OLD)
1000 SIGSETLO(td->td_sigmask, *set);
1002 td->td_sigmask = *set;
1003 new_block = td->td_sigmask;
1004 SIGSETNAND(new_block, oset1);
1013 * The new_block set contains signals that were not previously
1014 * blocked, but are blocked now.
1016 * In case we block any signal that was not previously blocked
1017 * for td, and process has the signal pending, try to schedule
1018 * signal delivery to some thread that does not block the
1019 * signal, possibly waking it up.
1021 if (p->p_numthreads != 1)
1022 reschedule_signals(p, new_block, flags);
1026 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1031 #ifndef _SYS_SYSPROTO_H_
1032 struct sigprocmask_args {
1034 const sigset_t *set;
1039 sys_sigprocmask(td, uap)
1040 register struct thread *td;
1041 struct sigprocmask_args *uap;
1044 sigset_t *setp, *osetp;
1047 setp = (uap->set != NULL) ? &set : NULL;
1048 osetp = (uap->oset != NULL) ? &oset : NULL;
1050 error = copyin(uap->set, setp, sizeof(set));
1054 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1055 if (osetp && !error) {
1056 error = copyout(osetp, uap->oset, sizeof(oset));
1061 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1062 #ifndef _SYS_SYSPROTO_H_
1063 struct osigprocmask_args {
1069 osigprocmask(td, uap)
1070 register struct thread *td;
1071 struct osigprocmask_args *uap;
1076 OSIG2SIG(uap->mask, set);
1077 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1078 SIG2OSIG(oset, td->td_retval[0]);
1081 #endif /* COMPAT_43 */
1084 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1090 error = copyin(uap->set, &set, sizeof(set));
1092 td->td_retval[0] = error;
1096 error = kern_sigtimedwait(td, set, &ksi, NULL);
1098 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1100 if (error == ERESTART)
1102 td->td_retval[0] = error;
1106 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1107 td->td_retval[0] = error;
1112 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1115 struct timespec *timeout;
1121 error = copyin(uap->timeout, &ts, sizeof(ts));
1129 error = copyin(uap->set, &set, sizeof(set));
1133 error = kern_sigtimedwait(td, set, &ksi, timeout);
1138 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1141 td->td_retval[0] = ksi.ksi_signo;
1146 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1152 error = copyin(uap->set, &set, sizeof(set));
1156 error = kern_sigtimedwait(td, set, &ksi, NULL);
1161 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1164 td->td_retval[0] = ksi.ksi_signo;
1169 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1170 struct timespec *timeout)
1173 sigset_t saved_mask, new_block;
1175 int error, sig, timo, timevalid = 0;
1176 struct timespec rts, ets, ts;
1184 if (timeout != NULL) {
1185 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1187 getnanouptime(&rts);
1189 timespecadd(&ets, timeout);
1193 /* Some signals can not be waited for. */
1194 SIG_CANTMASK(waitset);
1197 saved_mask = td->td_sigmask;
1198 SIGSETNAND(td->td_sigmask, waitset);
1200 mtx_lock(&ps->ps_mtx);
1201 sig = cursig(td, SIG_STOP_ALLOWED);
1202 mtx_unlock(&ps->ps_mtx);
1203 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1204 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1205 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1215 * POSIX says this must be checked after looking for pending
1218 if (timeout != NULL) {
1223 getnanouptime(&rts);
1224 if (timespeccmp(&rts, &ets, >=)) {
1229 timespecsub(&ts, &rts);
1230 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1236 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1238 if (timeout != NULL) {
1239 if (error == ERESTART) {
1240 /* Timeout can not be restarted. */
1242 } else if (error == EAGAIN) {
1243 /* We will calculate timeout by ourself. */
1249 new_block = saved_mask;
1250 SIGSETNAND(new_block, td->td_sigmask);
1251 td->td_sigmask = saved_mask;
1253 * Fewer signals can be delivered to us, reschedule signal
1256 if (p->p_numthreads != 1)
1257 reschedule_signals(p, new_block, 0);
1260 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1262 if (ksi->ksi_code == SI_TIMER)
1263 itimer_accept(p, ksi->ksi_timerid, ksi);
1266 if (KTRPOINT(td, KTR_PSIG)) {
1269 mtx_lock(&ps->ps_mtx);
1270 action = ps->ps_sigact[_SIG_IDX(sig)];
1271 mtx_unlock(&ps->ps_mtx);
1272 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1282 #ifndef _SYS_SYSPROTO_H_
1283 struct sigpending_args {
1288 sys_sigpending(td, uap)
1290 struct sigpending_args *uap;
1292 struct proc *p = td->td_proc;
1296 pending = p->p_sigqueue.sq_signals;
1297 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1299 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1302 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1303 #ifndef _SYS_SYSPROTO_H_
1304 struct osigpending_args {
1309 osigpending(td, uap)
1311 struct osigpending_args *uap;
1313 struct proc *p = td->td_proc;
1317 pending = p->p_sigqueue.sq_signals;
1318 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1320 SIG2OSIG(pending, td->td_retval[0]);
1323 #endif /* COMPAT_43 */
1325 #if defined(COMPAT_43)
1327 * Generalized interface signal handler, 4.3-compatible.
1329 #ifndef _SYS_SYSPROTO_H_
1330 struct osigvec_args {
1340 register struct osigvec_args *uap;
1343 struct sigaction nsa, osa;
1344 register struct sigaction *nsap, *osap;
1347 if (uap->signum <= 0 || uap->signum >= ONSIG)
1349 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1350 osap = (uap->osv != NULL) ? &osa : NULL;
1352 error = copyin(uap->nsv, &vec, sizeof(vec));
1355 nsap->sa_handler = vec.sv_handler;
1356 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1357 nsap->sa_flags = vec.sv_flags;
1358 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1360 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1361 if (osap && !error) {
1362 vec.sv_handler = osap->sa_handler;
1363 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1364 vec.sv_flags = osap->sa_flags;
1365 vec.sv_flags &= ~SA_NOCLDWAIT;
1366 vec.sv_flags ^= SA_RESTART;
1367 error = copyout(&vec, uap->osv, sizeof(vec));
1372 #ifndef _SYS_SYSPROTO_H_
1373 struct osigblock_args {
1379 register struct thread *td;
1380 struct osigblock_args *uap;
1384 OSIG2SIG(uap->mask, set);
1385 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1386 SIG2OSIG(oset, td->td_retval[0]);
1390 #ifndef _SYS_SYSPROTO_H_
1391 struct osigsetmask_args {
1396 osigsetmask(td, uap)
1398 struct osigsetmask_args *uap;
1402 OSIG2SIG(uap->mask, set);
1403 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1404 SIG2OSIG(oset, td->td_retval[0]);
1407 #endif /* COMPAT_43 */
1410 * Suspend calling thread until signal, providing mask to be set in the
1413 #ifndef _SYS_SYSPROTO_H_
1414 struct sigsuspend_args {
1415 const sigset_t *sigmask;
1420 sys_sigsuspend(td, uap)
1422 struct sigsuspend_args *uap;
1427 error = copyin(uap->sigmask, &mask, sizeof(mask));
1430 return (kern_sigsuspend(td, mask));
1434 kern_sigsuspend(struct thread *td, sigset_t mask)
1436 struct proc *p = td->td_proc;
1440 * When returning from sigsuspend, we want
1441 * the old mask to be restored after the
1442 * signal handler has finished. Thus, we
1443 * save it here and mark the sigacts structure
1447 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1448 SIGPROCMASK_PROC_LOCKED);
1449 td->td_pflags |= TDP_OLDMASK;
1452 * Process signals now. Otherwise, we can get spurious wakeup
1453 * due to signal entered process queue, but delivered to other
1454 * thread. But sigsuspend should return only on signal
1457 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1458 for (has_sig = 0; !has_sig;) {
1459 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1462 thread_suspend_check(0);
1463 mtx_lock(&p->p_sigacts->ps_mtx);
1464 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
1465 has_sig += postsig(sig);
1466 mtx_unlock(&p->p_sigacts->ps_mtx);
1469 return (EJUSTRETURN);
1472 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1474 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1475 * convention: libc stub passes mask, not pointer, to save a copyin.
1477 #ifndef _SYS_SYSPROTO_H_
1478 struct osigsuspend_args {
1484 osigsuspend(td, uap)
1486 struct osigsuspend_args *uap;
1490 OSIG2SIG(uap->mask, mask);
1491 return (kern_sigsuspend(td, mask));
1493 #endif /* COMPAT_43 */
1495 #if defined(COMPAT_43)
1496 #ifndef _SYS_SYSPROTO_H_
1497 struct osigstack_args {
1498 struct sigstack *nss;
1499 struct sigstack *oss;
1506 register struct osigstack_args *uap;
1508 struct sigstack nss, oss;
1511 if (uap->nss != NULL) {
1512 error = copyin(uap->nss, &nss, sizeof(nss));
1516 oss.ss_sp = td->td_sigstk.ss_sp;
1517 oss.ss_onstack = sigonstack(cpu_getstack(td));
1518 if (uap->nss != NULL) {
1519 td->td_sigstk.ss_sp = nss.ss_sp;
1520 td->td_sigstk.ss_size = 0;
1521 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1522 td->td_pflags |= TDP_ALTSTACK;
1524 if (uap->oss != NULL)
1525 error = copyout(&oss, uap->oss, sizeof(oss));
1529 #endif /* COMPAT_43 */
1531 #ifndef _SYS_SYSPROTO_H_
1532 struct sigaltstack_args {
1539 sys_sigaltstack(td, uap)
1541 register struct sigaltstack_args *uap;
1546 if (uap->ss != NULL) {
1547 error = copyin(uap->ss, &ss, sizeof(ss));
1551 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1552 (uap->oss != NULL) ? &oss : NULL);
1555 if (uap->oss != NULL)
1556 error = copyout(&oss, uap->oss, sizeof(stack_t));
1561 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1563 struct proc *p = td->td_proc;
1566 oonstack = sigonstack(cpu_getstack(td));
1569 *oss = td->td_sigstk;
1570 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1571 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1577 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1579 if (!(ss->ss_flags & SS_DISABLE)) {
1580 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1583 td->td_sigstk = *ss;
1584 td->td_pflags |= TDP_ALTSTACK;
1586 td->td_pflags &= ~TDP_ALTSTACK;
1593 * Common code for kill process group/broadcast kill.
1594 * cp is calling process.
1597 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1607 sx_slock(&allproc_lock);
1608 FOREACH_PROC_IN_SYSTEM(p) {
1610 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1611 p == td->td_proc || p->p_state == PRS_NEW) {
1615 if (p_cansignal(td, p, sig) == 0) {
1618 pksignal(p, sig, ksi);
1622 sx_sunlock(&allproc_lock);
1624 sx_slock(&proctree_lock);
1627 * zero pgid means send to my process group.
1629 pgrp = td->td_proc->p_pgrp;
1632 pgrp = pgfind(pgid);
1634 sx_sunlock(&proctree_lock);
1638 sx_sunlock(&proctree_lock);
1639 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1641 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1642 p->p_state == PRS_NEW) {
1646 if (p_cansignal(td, p, sig) == 0) {
1649 pksignal(p, sig, ksi);
1655 return (nfound ? 0 : ESRCH);
1658 #ifndef _SYS_SYSPROTO_H_
1666 sys_kill(struct thread *td, struct kill_args *uap)
1672 AUDIT_ARG_SIGNUM(uap->signum);
1673 AUDIT_ARG_PID(uap->pid);
1674 if ((u_int)uap->signum > _SIG_MAXSIG)
1677 ksiginfo_init(&ksi);
1678 ksi.ksi_signo = uap->signum;
1679 ksi.ksi_code = SI_USER;
1680 ksi.ksi_pid = td->td_proc->p_pid;
1681 ksi.ksi_uid = td->td_ucred->cr_ruid;
1684 /* kill single process */
1685 if ((p = pfind(uap->pid)) == NULL) {
1686 if ((p = zpfind(uap->pid)) == NULL)
1689 AUDIT_ARG_PROCESS(p);
1690 error = p_cansignal(td, p, uap->signum);
1691 if (error == 0 && uap->signum)
1692 pksignal(p, uap->signum, &ksi);
1697 case -1: /* broadcast signal */
1698 return (killpg1(td, uap->signum, 0, 1, &ksi));
1699 case 0: /* signal own process group */
1700 return (killpg1(td, uap->signum, 0, 0, &ksi));
1701 default: /* negative explicit process group */
1702 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1710 struct pdkill_args *uap;
1716 AUDIT_ARG_SIGNUM(uap->signum);
1717 AUDIT_ARG_FD(uap->fd);
1718 if ((u_int)uap->signum > _SIG_MAXSIG)
1721 error = procdesc_find(td, uap->fd, CAP_PDKILL, &p);
1724 AUDIT_ARG_PROCESS(p);
1725 error = p_cansignal(td, p, uap->signum);
1726 if (error == 0 && uap->signum)
1727 kern_psignal(p, uap->signum);
1735 #if defined(COMPAT_43)
1736 #ifndef _SYS_SYSPROTO_H_
1737 struct okillpg_args {
1744 okillpg(struct thread *td, struct okillpg_args *uap)
1748 AUDIT_ARG_SIGNUM(uap->signum);
1749 AUDIT_ARG_PID(uap->pgid);
1750 if ((u_int)uap->signum > _SIG_MAXSIG)
1753 ksiginfo_init(&ksi);
1754 ksi.ksi_signo = uap->signum;
1755 ksi.ksi_code = SI_USER;
1756 ksi.ksi_pid = td->td_proc->p_pid;
1757 ksi.ksi_uid = td->td_ucred->cr_ruid;
1758 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1760 #endif /* COMPAT_43 */
1762 #ifndef _SYS_SYSPROTO_H_
1763 struct sigqueue_args {
1766 /* union sigval */ void *value;
1770 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1776 if ((u_int)uap->signum > _SIG_MAXSIG)
1780 * Specification says sigqueue can only send signal to
1786 if ((p = pfind(uap->pid)) == NULL) {
1787 if ((p = zpfind(uap->pid)) == NULL)
1790 error = p_cansignal(td, p, uap->signum);
1791 if (error == 0 && uap->signum != 0) {
1792 ksiginfo_init(&ksi);
1793 ksi.ksi_flags = KSI_SIGQ;
1794 ksi.ksi_signo = uap->signum;
1795 ksi.ksi_code = SI_QUEUE;
1796 ksi.ksi_pid = td->td_proc->p_pid;
1797 ksi.ksi_uid = td->td_ucred->cr_ruid;
1798 ksi.ksi_value.sival_ptr = uap->value;
1799 error = pksignal(p, ksi.ksi_signo, &ksi);
1806 * Send a signal to a process group.
1809 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1814 sx_slock(&proctree_lock);
1815 pgrp = pgfind(pgid);
1816 sx_sunlock(&proctree_lock);
1818 pgsignal(pgrp, sig, 0, ksi);
1825 * Send a signal to a process group. If checktty is 1,
1826 * limit to members which have a controlling terminal.
1829 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1834 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1835 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1837 if (p->p_state == PRS_NORMAL &&
1838 (checkctty == 0 || p->p_flag & P_CONTROLT))
1839 pksignal(p, sig, ksi);
1846 * Send a signal caused by a trap to the current thread. If it will be
1847 * caught immediately, deliver it with correct code. Otherwise, post it
1851 trapsignal(struct thread *td, ksiginfo_t *ksi)
1860 sig = ksi->ksi_signo;
1861 code = ksi->ksi_code;
1862 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1866 mtx_lock(&ps->ps_mtx);
1867 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1868 !SIGISMEMBER(td->td_sigmask, sig)) {
1869 td->td_ru.ru_nsignals++;
1871 if (KTRPOINT(curthread, KTR_PSIG))
1872 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1873 &td->td_sigmask, code);
1875 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1876 ksi, &td->td_sigmask);
1877 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1878 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1879 SIGADDSET(mask, sig);
1880 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1881 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1882 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1884 * See kern_sigaction() for origin of this code.
1886 SIGDELSET(ps->ps_sigcatch, sig);
1887 if (sig != SIGCONT &&
1888 sigprop(sig) & SA_IGNORE)
1889 SIGADDSET(ps->ps_sigignore, sig);
1890 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1892 mtx_unlock(&ps->ps_mtx);
1895 * Avoid a possible infinite loop if the thread
1896 * masking the signal or process is ignoring the
1899 if (kern_forcesigexit &&
1900 (SIGISMEMBER(td->td_sigmask, sig) ||
1901 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1902 SIGDELSET(td->td_sigmask, sig);
1903 SIGDELSET(ps->ps_sigcatch, sig);
1904 SIGDELSET(ps->ps_sigignore, sig);
1905 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1907 mtx_unlock(&ps->ps_mtx);
1908 p->p_code = code; /* XXX for core dump/debugger */
1909 p->p_sig = sig; /* XXX to verify code */
1910 tdsendsignal(p, td, sig, ksi);
1915 static struct thread *
1916 sigtd(struct proc *p, int sig, int prop)
1918 struct thread *td, *signal_td;
1920 PROC_LOCK_ASSERT(p, MA_OWNED);
1923 * Check if current thread can handle the signal without
1924 * switching context to another thread.
1926 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1929 FOREACH_THREAD_IN_PROC(p, td) {
1930 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1935 if (signal_td == NULL)
1936 signal_td = FIRST_THREAD_IN_PROC(p);
1941 * Send the signal to the process. If the signal has an action, the action
1942 * is usually performed by the target process rather than the caller; we add
1943 * the signal to the set of pending signals for the process.
1946 * o When a stop signal is sent to a sleeping process that takes the
1947 * default action, the process is stopped without awakening it.
1948 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1949 * regardless of the signal action (eg, blocked or ignored).
1951 * Other ignored signals are discarded immediately.
1953 * NB: This function may be entered from the debugger via the "kill" DDB
1954 * command. There is little that can be done to mitigate the possibly messy
1955 * side effects of this unwise possibility.
1958 kern_psignal(struct proc *p, int sig)
1962 ksiginfo_init(&ksi);
1963 ksi.ksi_signo = sig;
1964 ksi.ksi_code = SI_KERNEL;
1965 (void) tdsendsignal(p, NULL, sig, &ksi);
1969 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1972 return (tdsendsignal(p, NULL, sig, ksi));
1975 /* Utility function for finding a thread to send signal event to. */
1977 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
1981 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1982 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
1994 tdsignal(struct thread *td, int sig)
1998 ksiginfo_init(&ksi);
1999 ksi.ksi_signo = sig;
2000 ksi.ksi_code = SI_KERNEL;
2001 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2005 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2008 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2012 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2015 sigqueue_t *sigqueue;
2022 MPASS(td == NULL || p == td->td_proc);
2023 PROC_LOCK_ASSERT(p, MA_OWNED);
2025 if (!_SIG_VALID(sig))
2026 panic("%s(): invalid signal %d", __func__, sig);
2028 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2031 * IEEE Std 1003.1-2001: return success when killing a zombie.
2033 if (p->p_state == PRS_ZOMBIE) {
2034 if (ksi && (ksi->ksi_flags & KSI_INS))
2035 ksiginfo_tryfree(ksi);
2040 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2041 prop = sigprop(sig);
2044 td = sigtd(p, sig, prop);
2045 sigqueue = &p->p_sigqueue;
2047 KASSERT(td->td_proc == p, ("invalid thread"));
2048 sigqueue = &td->td_sigqueue;
2051 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2054 * If the signal is being ignored,
2055 * then we forget about it immediately.
2056 * (Note: we don't set SIGCONT in ps_sigignore,
2057 * and if it is set to SIG_IGN,
2058 * action will be SIG_DFL here.)
2060 mtx_lock(&ps->ps_mtx);
2061 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2062 SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 );
2064 mtx_unlock(&ps->ps_mtx);
2065 if (ksi && (ksi->ksi_flags & KSI_INS))
2066 ksiginfo_tryfree(ksi);
2069 if (SIGISMEMBER(td->td_sigmask, sig))
2071 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2075 if (SIGISMEMBER(ps->ps_sigintr, sig))
2079 mtx_unlock(&ps->ps_mtx);
2082 sigqueue_delete_stopmask_proc(p);
2083 else if (prop & SA_STOP) {
2085 * If sending a tty stop signal to a member of an orphaned
2086 * process group, discard the signal here if the action
2087 * is default; don't stop the process below if sleeping,
2088 * and don't clear any pending SIGCONT.
2090 if ((prop & SA_TTYSTOP) &&
2091 (p->p_pgrp->pg_jobc == 0) &&
2092 (action == SIG_DFL)) {
2093 if (ksi && (ksi->ksi_flags & KSI_INS))
2094 ksiginfo_tryfree(ksi);
2097 sigqueue_delete_proc(p, SIGCONT);
2098 if (p->p_flag & P_CONTINUED) {
2099 p->p_flag &= ~P_CONTINUED;
2100 PROC_LOCK(p->p_pptr);
2101 sigqueue_take(p->p_ksi);
2102 PROC_UNLOCK(p->p_pptr);
2106 ret = sigqueue_add(sigqueue, sig, ksi);
2111 * Defer further processing for signals which are held,
2112 * except that stopped processes must be continued by SIGCONT.
2114 if (action == SIG_HOLD &&
2115 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2118 * SIGKILL: Remove procfs STOPEVENTs.
2120 if (sig == SIGKILL) {
2121 /* from procfs_ioctl.c: PIOCBIC */
2123 /* from procfs_ioctl.c: PIOCCONT */
2128 * Some signals have a process-wide effect and a per-thread
2129 * component. Most processing occurs when the process next
2130 * tries to cross the user boundary, however there are some
2131 * times when processing needs to be done immediatly, such as
2132 * waking up threads so that they can cross the user boundary.
2133 * We try do the per-process part here.
2135 if (P_SHOULDSTOP(p)) {
2136 if (sig == SIGKILL) {
2138 * If traced process is already stopped,
2139 * then no further action is necessary.
2141 if (p->p_flag & P_TRACED)
2144 * SIGKILL sets process running.
2145 * It will die elsewhere.
2146 * All threads must be restarted.
2148 p->p_flag &= ~P_STOPPED_SIG;
2152 if (prop & SA_CONT) {
2154 * If traced process is already stopped,
2155 * then no further action is necessary.
2157 if (p->p_flag & P_TRACED)
2160 * If SIGCONT is default (or ignored), we continue the
2161 * process but don't leave the signal in sigqueue as
2162 * it has no further action. If SIGCONT is held, we
2163 * continue the process and leave the signal in
2164 * sigqueue. If the process catches SIGCONT, let it
2165 * handle the signal itself. If it isn't waiting on
2166 * an event, it goes back to run state.
2167 * Otherwise, process goes back to sleep state.
2169 p->p_flag &= ~P_STOPPED_SIG;
2171 if (p->p_numthreads == p->p_suspcount) {
2173 p->p_flag |= P_CONTINUED;
2174 p->p_xstat = SIGCONT;
2175 PROC_LOCK(p->p_pptr);
2176 childproc_continued(p);
2177 PROC_UNLOCK(p->p_pptr);
2180 if (action == SIG_DFL) {
2181 thread_unsuspend(p);
2183 sigqueue_delete(sigqueue, sig);
2186 if (action == SIG_CATCH) {
2188 * The process wants to catch it so it needs
2189 * to run at least one thread, but which one?
2195 * The signal is not ignored or caught.
2197 thread_unsuspend(p);
2202 if (prop & SA_STOP) {
2204 * If traced process is already stopped,
2205 * then no further action is necessary.
2207 if (p->p_flag & P_TRACED)
2210 * Already stopped, don't need to stop again
2211 * (If we did the shell could get confused).
2212 * Just make sure the signal STOP bit set.
2214 p->p_flag |= P_STOPPED_SIG;
2215 sigqueue_delete(sigqueue, sig);
2220 * All other kinds of signals:
2221 * If a thread is sleeping interruptibly, simulate a
2222 * wakeup so that when it is continued it will be made
2223 * runnable and can look at the signal. However, don't make
2224 * the PROCESS runnable, leave it stopped.
2225 * It may run a bit until it hits a thread_suspend_check().
2230 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2231 wakeup_swapper = sleepq_abort(td, intrval);
2238 * Mutexes are short lived. Threads waiting on them will
2239 * hit thread_suspend_check() soon.
2241 } else if (p->p_state == PRS_NORMAL) {
2242 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2243 tdsigwakeup(td, sig, action, intrval);
2247 MPASS(action == SIG_DFL);
2249 if (prop & SA_STOP) {
2250 if (p->p_flag & P_PPWAIT)
2252 p->p_flag |= P_STOPPED_SIG;
2255 sig_suspend_threads(td, p, 1);
2256 if (p->p_numthreads == p->p_suspcount) {
2258 * only thread sending signal to another
2259 * process can reach here, if thread is sending
2260 * signal to its process, because thread does
2261 * not suspend itself here, p_numthreads
2262 * should never be equal to p_suspcount.
2266 sigqueue_delete_proc(p, p->p_xstat);
2272 /* Not in "NORMAL" state. discard the signal. */
2273 sigqueue_delete(sigqueue, sig);
2278 * The process is not stopped so we need to apply the signal to all the
2282 tdsigwakeup(td, sig, action, intrval);
2284 thread_unsuspend(p);
2287 /* If we jump here, proc slock should not be owned. */
2288 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2293 * The force of a signal has been directed against a single
2294 * thread. We need to see what we can do about knocking it
2295 * out of any sleep it may be in etc.
2298 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2300 struct proc *p = td->td_proc;
2305 PROC_LOCK_ASSERT(p, MA_OWNED);
2306 prop = sigprop(sig);
2311 * Bring the priority of a thread up if we want it to get
2312 * killed in this lifetime.
2314 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2315 sched_prio(td, PUSER);
2316 if (TD_ON_SLEEPQ(td)) {
2318 * If thread is sleeping uninterruptibly
2319 * we can't interrupt the sleep... the signal will
2320 * be noticed when the process returns through
2321 * trap() or syscall().
2323 if ((td->td_flags & TDF_SINTR) == 0)
2326 * If SIGCONT is default (or ignored) and process is
2327 * asleep, we are finished; the process should not
2330 if ((prop & SA_CONT) && action == SIG_DFL) {
2333 sigqueue_delete(&p->p_sigqueue, sig);
2335 * It may be on either list in this state.
2336 * Remove from both for now.
2338 sigqueue_delete(&td->td_sigqueue, sig);
2343 * Give low priority threads a better chance to run.
2345 if (td->td_priority > PUSER)
2346 sched_prio(td, PUSER);
2348 wakeup_swapper = sleepq_abort(td, intrval);
2351 * Other states do nothing with the signal immediately,
2352 * other than kicking ourselves if we are running.
2353 * It will either never be noticed, or noticed very soon.
2356 if (TD_IS_RUNNING(td) && td != curthread)
2368 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2373 PROC_LOCK_ASSERT(p, MA_OWNED);
2374 PROC_SLOCK_ASSERT(p, MA_OWNED);
2377 FOREACH_THREAD_IN_PROC(p, td2) {
2379 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2380 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2381 (td2->td_flags & TDF_SINTR)) {
2382 if (td2->td_flags & TDF_SBDRY) {
2383 if (TD_IS_SUSPENDED(td2))
2385 thread_unsuspend_one(td2);
2386 if (TD_ON_SLEEPQ(td2))
2388 sleepq_abort(td2, ERESTART);
2389 } else if (!TD_IS_SUSPENDED(td2)) {
2390 thread_suspend_one(td2);
2392 } else if (!TD_IS_SUSPENDED(td2)) {
2393 if (sending || td != td2)
2394 td2->td_flags |= TDF_ASTPENDING;
2396 if (TD_IS_RUNNING(td2) && td2 != td)
2397 forward_signal(td2);
2407 ptracestop(struct thread *td, int sig)
2409 struct proc *p = td->td_proc;
2411 PROC_LOCK_ASSERT(p, MA_OWNED);
2412 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2413 &p->p_mtx.lock_object, "Stopping for traced signal");
2415 td->td_dbgflags |= TDB_XSIG;
2418 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2419 if (p->p_flag & P_SINGLE_EXIT) {
2420 td->td_dbgflags &= ~TDB_XSIG;
2425 * Just make wait() to work, the last stopped thread
2430 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2431 sig_suspend_threads(td, p, 0);
2432 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2433 td->td_dbgflags &= ~TDB_STOPATFORK;
2434 cv_broadcast(&p->p_dbgwait);
2437 thread_suspend_switch(td);
2438 if (!(p->p_flag & P_TRACED)) {
2441 if (td->td_dbgflags & TDB_SUSPEND) {
2442 if (p->p_flag & P_SINGLE_EXIT)
2448 return (td->td_xsig);
2452 reschedule_signals(struct proc *p, sigset_t block, int flags)
2458 PROC_LOCK_ASSERT(p, MA_OWNED);
2459 if (SIGISEMPTY(p->p_siglist))
2462 SIGSETAND(block, p->p_siglist);
2463 while ((sig = sig_ffs(&block)) != 0) {
2464 SIGDELSET(block, sig);
2465 td = sigtd(p, sig, 0);
2467 if (!(flags & SIGPROCMASK_PS_LOCKED))
2468 mtx_lock(&ps->ps_mtx);
2469 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2470 tdsigwakeup(td, sig, SIG_CATCH,
2471 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2473 if (!(flags & SIGPROCMASK_PS_LOCKED))
2474 mtx_unlock(&ps->ps_mtx);
2479 tdsigcleanup(struct thread *td)
2485 PROC_LOCK_ASSERT(p, MA_OWNED);
2487 sigqueue_flush(&td->td_sigqueue);
2488 if (p->p_numthreads == 1)
2492 * Since we cannot handle signals, notify signal post code
2493 * about this by filling the sigmask.
2495 * Also, if needed, wake up thread(s) that do not block the
2496 * same signals as the exiting thread, since the thread might
2497 * have been selected for delivery and woken up.
2499 SIGFILLSET(unblocked);
2500 SIGSETNAND(unblocked, td->td_sigmask);
2501 SIGFILLSET(td->td_sigmask);
2502 reschedule_signals(p, unblocked, 0);
2507 * If the current process has received a signal (should be caught or cause
2508 * termination, should interrupt current syscall), return the signal number.
2509 * Stop signals with default action are processed immediately, then cleared;
2510 * they aren't returned. This is checked after each entry to the system for
2511 * a syscall or trap (though this can usually be done without calling issignal
2512 * by checking the pending signal masks in cursig.) The normal call
2515 * while (sig = cursig(curthread))
2519 issignal(struct thread *td, int stop_allowed)
2523 struct sigqueue *queue;
2524 sigset_t sigpending;
2525 int sig, prop, newsig;
2529 mtx_assert(&ps->ps_mtx, MA_OWNED);
2530 PROC_LOCK_ASSERT(p, MA_OWNED);
2532 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2534 sigpending = td->td_sigqueue.sq_signals;
2535 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2536 SIGSETNAND(sigpending, td->td_sigmask);
2538 if (p->p_flag & P_PPWAIT)
2539 SIG_STOPSIGMASK(sigpending);
2540 if (SIGISEMPTY(sigpending)) /* no signal to send */
2542 sig = sig_ffs(&sigpending);
2544 if (p->p_stops & S_SIG) {
2545 mtx_unlock(&ps->ps_mtx);
2546 stopevent(p, S_SIG, sig);
2547 mtx_lock(&ps->ps_mtx);
2551 * We should see pending but ignored signals
2552 * only if P_TRACED was on when they were posted.
2554 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2555 sigqueue_delete(&td->td_sigqueue, sig);
2556 sigqueue_delete(&p->p_sigqueue, sig);
2559 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2561 * If traced, always stop.
2562 * Remove old signal from queue before the stop.
2563 * XXX shrug off debugger, it causes siginfo to
2566 queue = &td->td_sigqueue;
2567 td->td_dbgksi.ksi_signo = 0;
2568 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2569 queue = &p->p_sigqueue;
2570 sigqueue_get(queue, sig, &td->td_dbgksi);
2573 mtx_unlock(&ps->ps_mtx);
2574 newsig = ptracestop(td, sig);
2575 mtx_lock(&ps->ps_mtx);
2577 if (sig != newsig) {
2580 * If parent wants us to take the signal,
2581 * then it will leave it in p->p_xstat;
2582 * otherwise we just look for signals again.
2589 * Put the new signal into td_sigqueue. If the
2590 * signal is being masked, look for other signals.
2592 sigqueue_add(queue, sig, NULL);
2593 if (SIGISMEMBER(td->td_sigmask, sig))
2597 if (td->td_dbgksi.ksi_signo != 0) {
2598 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2599 if (sigqueue_add(&td->td_sigqueue, sig,
2600 &td->td_dbgksi) != 0)
2601 td->td_dbgksi.ksi_signo = 0;
2603 if (td->td_dbgksi.ksi_signo == 0)
2604 sigqueue_add(&td->td_sigqueue, sig,
2609 * If the traced bit got turned off, go back up
2610 * to the top to rescan signals. This ensures
2611 * that p_sig* and p_sigact are consistent.
2613 if ((p->p_flag & P_TRACED) == 0)
2617 prop = sigprop(sig);
2620 * Decide whether the signal should be returned.
2621 * Return the signal's number, or fall through
2622 * to clear it from the pending mask.
2624 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2626 case (intptr_t)SIG_DFL:
2628 * Don't take default actions on system processes.
2630 if (p->p_pid <= 1) {
2633 * Are you sure you want to ignore SIGSEGV
2636 printf("Process (pid %lu) got signal %d\n",
2637 (u_long)p->p_pid, sig);
2639 break; /* == ignore */
2642 * If there is a pending stop signal to process
2643 * with default action, stop here,
2644 * then clear the signal. However,
2645 * if process is member of an orphaned
2646 * process group, ignore tty stop signals.
2648 if (prop & SA_STOP) {
2649 if (p->p_flag & P_TRACED ||
2650 (p->p_pgrp->pg_jobc == 0 &&
2652 break; /* == ignore */
2654 /* Ignore, but do not drop the stop signal. */
2655 if (stop_allowed != SIG_STOP_ALLOWED)
2657 mtx_unlock(&ps->ps_mtx);
2658 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2659 &p->p_mtx.lock_object, "Catching SIGSTOP");
2660 p->p_flag |= P_STOPPED_SIG;
2663 sig_suspend_threads(td, p, 0);
2664 thread_suspend_switch(td);
2666 mtx_lock(&ps->ps_mtx);
2668 } else if (prop & SA_IGNORE) {
2670 * Except for SIGCONT, shouldn't get here.
2671 * Default action is to ignore; drop it.
2673 break; /* == ignore */
2678 case (intptr_t)SIG_IGN:
2680 * Masking above should prevent us ever trying
2681 * to take action on an ignored signal other
2682 * than SIGCONT, unless process is traced.
2684 if ((prop & SA_CONT) == 0 &&
2685 (p->p_flag & P_TRACED) == 0)
2686 printf("issignal\n");
2687 break; /* == ignore */
2691 * This signal has an action, let
2692 * postsig() process it.
2696 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2697 sigqueue_delete(&p->p_sigqueue, sig);
2703 thread_stopped(struct proc *p)
2707 PROC_LOCK_ASSERT(p, MA_OWNED);
2708 PROC_SLOCK_ASSERT(p, MA_OWNED);
2712 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2714 p->p_flag &= ~P_WAITED;
2715 PROC_LOCK(p->p_pptr);
2716 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2717 CLD_TRAPPED : CLD_STOPPED);
2718 PROC_UNLOCK(p->p_pptr);
2724 * Take the action for the specified signal
2725 * from the current set of pending signals.
2731 struct thread *td = curthread;
2732 register struct proc *p = td->td_proc;
2736 sigset_t returnmask, mask;
2738 KASSERT(sig != 0, ("postsig"));
2740 PROC_LOCK_ASSERT(p, MA_OWNED);
2742 mtx_assert(&ps->ps_mtx, MA_OWNED);
2743 ksiginfo_init(&ksi);
2744 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2745 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2747 ksi.ksi_signo = sig;
2748 if (ksi.ksi_code == SI_TIMER)
2749 itimer_accept(p, ksi.ksi_timerid, &ksi);
2750 action = ps->ps_sigact[_SIG_IDX(sig)];
2752 if (KTRPOINT(td, KTR_PSIG))
2753 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2754 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2756 if (p->p_stops & S_SIG) {
2757 mtx_unlock(&ps->ps_mtx);
2758 stopevent(p, S_SIG, sig);
2759 mtx_lock(&ps->ps_mtx);
2762 if (action == SIG_DFL) {
2764 * Default action, where the default is to kill
2765 * the process. (Other cases were ignored above.)
2767 mtx_unlock(&ps->ps_mtx);
2772 * If we get here, the signal must be caught.
2774 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2775 ("postsig action"));
2777 * Set the new mask value and also defer further
2778 * occurrences of this signal.
2780 * Special case: user has done a sigsuspend. Here the
2781 * current mask is not of interest, but rather the
2782 * mask from before the sigsuspend is what we want
2783 * restored after the signal processing is completed.
2785 if (td->td_pflags & TDP_OLDMASK) {
2786 returnmask = td->td_oldsigmask;
2787 td->td_pflags &= ~TDP_OLDMASK;
2789 returnmask = td->td_sigmask;
2791 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2792 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2793 SIGADDSET(mask, sig);
2794 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2795 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2797 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2799 * See kern_sigaction() for origin of this code.
2801 SIGDELSET(ps->ps_sigcatch, sig);
2802 if (sig != SIGCONT &&
2803 sigprop(sig) & SA_IGNORE)
2804 SIGADDSET(ps->ps_sigignore, sig);
2805 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2807 td->td_ru.ru_nsignals++;
2808 if (p->p_sig == sig) {
2812 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2818 * Kill the current process for stated reason.
2826 PROC_LOCK_ASSERT(p, MA_OWNED);
2827 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2828 p, p->p_pid, p->p_comm);
2829 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2830 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2831 p->p_flag |= P_WKILLED;
2832 kern_psignal(p, SIGKILL);
2836 * Force the current process to exit with the specified signal, dumping core
2837 * if appropriate. We bypass the normal tests for masked and caught signals,
2838 * allowing unrecoverable failures to terminate the process without changing
2839 * signal state. Mark the accounting record with the signal termination.
2840 * If dumping core, save the signal number for the debugger. Calls exit and
2848 struct proc *p = td->td_proc;
2850 PROC_LOCK_ASSERT(p, MA_OWNED);
2851 p->p_acflag |= AXSIG;
2853 * We must be single-threading to generate a core dump. This
2854 * ensures that the registers in the core file are up-to-date.
2855 * Also, the ELF dump handler assumes that the thread list doesn't
2856 * change out from under it.
2858 * XXX If another thread attempts to single-thread before us
2859 * (e.g. via fork()), we won't get a dump at all.
2861 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2864 * Log signals which would cause core dumps
2865 * (Log as LOG_INFO to appease those who don't want
2867 * XXX : Todo, as well as euid, write out ruid too
2868 * Note that coredump() drops proc lock.
2870 if (coredump(td) == 0)
2872 if (kern_logsigexit)
2874 "pid %d (%s), uid %d: exited on signal %d%s\n",
2875 p->p_pid, p->p_comm,
2876 td->td_ucred ? td->td_ucred->cr_uid : -1,
2878 sig & WCOREFLAG ? " (core dumped)" : "");
2881 exit1(td, W_EXITCODE(0, sig));
2886 * Send queued SIGCHLD to parent when child process's state
2890 sigparent(struct proc *p, int reason, int status)
2892 PROC_LOCK_ASSERT(p, MA_OWNED);
2893 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2895 if (p->p_ksi != NULL) {
2896 p->p_ksi->ksi_signo = SIGCHLD;
2897 p->p_ksi->ksi_code = reason;
2898 p->p_ksi->ksi_status = status;
2899 p->p_ksi->ksi_pid = p->p_pid;
2900 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2901 if (KSI_ONQ(p->p_ksi))
2904 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2908 childproc_jobstate(struct proc *p, int reason, int status)
2912 PROC_LOCK_ASSERT(p, MA_OWNED);
2913 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2916 * Wake up parent sleeping in kern_wait(), also send
2917 * SIGCHLD to parent, but SIGCHLD does not guarantee
2918 * that parent will awake, because parent may masked
2921 p->p_pptr->p_flag |= P_STATCHILD;
2924 ps = p->p_pptr->p_sigacts;
2925 mtx_lock(&ps->ps_mtx);
2926 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2927 mtx_unlock(&ps->ps_mtx);
2928 sigparent(p, reason, status);
2930 mtx_unlock(&ps->ps_mtx);
2934 childproc_stopped(struct proc *p, int reason)
2936 childproc_jobstate(p, reason, p->p_xstat);
2940 childproc_continued(struct proc *p)
2942 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2946 childproc_exited(struct proc *p)
2949 int status = p->p_xstat; /* convert to int */
2951 reason = CLD_EXITED;
2952 if (WCOREDUMP(status))
2953 reason = CLD_DUMPED;
2954 else if (WIFSIGNALED(status))
2955 reason = CLD_KILLED;
2957 * XXX avoid calling wakeup(p->p_pptr), the work is
2960 sigparent(p, reason, status);
2964 * We only have 1 character for the core count in the format
2965 * string, so the range will be 0-9
2967 #define MAX_NUM_CORES 10
2968 static int num_cores = 5;
2971 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
2976 new_val = num_cores;
2977 error = sysctl_handle_int(oidp, &new_val, 0, req);
2978 if (error != 0 || req->newptr == NULL)
2980 if (new_val > MAX_NUM_CORES)
2981 new_val = MAX_NUM_CORES;
2984 num_cores = new_val;
2987 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
2988 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
2990 #if defined(COMPRESS_USER_CORES)
2991 int compress_user_cores = 1;
2992 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
2993 &compress_user_cores, 0, "");
2995 int compress_user_cores_gzlevel = -1; /* default level */
2996 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
2997 &compress_user_cores_gzlevel, -1, "user core gz compression level");
2999 #define GZ_SUFFIX ".gz"
3000 #define GZ_SUFFIX_LEN 3
3003 static char corefilename[MAXPATHLEN] = {"%N.core"};
3004 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
3005 sizeof(corefilename), "process corefile name format string");
3008 * expand_name(name, uid, pid, td, compress)
3009 * Expand the name described in corefilename, using name, uid, and pid.
3010 * corefilename is a printf-like string, with three format specifiers:
3011 * %N name of process ("name")
3012 * %P process id (pid)
3014 * For example, "%N.core" is the default; they can be disabled completely
3015 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3016 * This is controlled by the sysctl variable kern.corefile (see above).
3019 expand_name(const char *name, uid_t uid, pid_t pid, struct thread *td,
3030 format = corefilename;
3031 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
3035 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
3036 for (i = 0; format[i]; i++) {
3037 switch (format[i]) {
3038 case '%': /* Format character */
3040 switch (format[i]) {
3042 sbuf_putc(&sb, '%');
3044 case 'H': /* hostname */
3045 if (hostname == NULL) {
3046 hostname = malloc(MAXHOSTNAMELEN,
3048 if (hostname == NULL) {
3050 "pid %ld (%s), uid (%lu): "
3051 "unable to alloc memory "
3052 "for corefile hostname\n",
3058 getcredhostname(td->td_ucred, hostname,
3060 sbuf_printf(&sb, "%s", hostname);
3062 case 'I': /* autoincrementing index */
3063 sbuf_printf(&sb, "0");
3064 indexpos = sbuf_len(&sb) - 1;
3066 case 'N': /* process name */
3067 sbuf_printf(&sb, "%s", name);
3069 case 'P': /* process id */
3070 sbuf_printf(&sb, "%u", pid);
3072 case 'U': /* user id */
3073 sbuf_printf(&sb, "%u", uid);
3077 "Unknown format character %c in "
3078 "corename `%s'\n", format[i], format);
3082 sbuf_putc(&sb, format[i]);
3085 free(hostname, M_TEMP);
3086 #ifdef COMPRESS_USER_CORES
3088 sbuf_printf(&sb, GZ_SUFFIX);
3091 if (sbuf_error(&sb) != 0) {
3092 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3093 "long\n", (long)pid, name, (u_long)uid);
3103 * If the core format has a %I in it, then we need to check
3104 * for existing corefiles before returning a name.
3105 * To do this we iterate over 0..num_cores to find a
3106 * non-existing core file name to use.
3108 if (indexpos != -1) {
3109 struct nameidata nd;
3111 int flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3112 int cmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
3115 for (n = 0; n < num_cores; n++) {
3116 temp[indexpos] = '0' + n;
3117 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE,
3119 error = vn_open(&nd, &flags, cmode, NULL);
3121 if (error == EEXIST) {
3125 "pid %d (%s), uid (%u): Path `%s' failed "
3126 "on initial open test, error = %d\n",
3127 pid, name, uid, temp, error);
3131 vfslocked = NDHASGIANT(&nd);
3132 NDFREE(&nd, NDF_ONLY_PNBUF);
3133 VOP_UNLOCK(nd.ni_vp, 0);
3134 error = vn_close(nd.ni_vp, FWRITE, td->td_ucred, td);
3135 VFS_UNLOCK_GIANT(vfslocked);
3138 "pid %d (%s), uid (%u): Path `%s' failed "
3139 "on close after initial open test, "
3141 pid, name, uid, temp, error);
3152 * Dump a process' core. The main routine does some
3153 * policy checking, and creates the name of the coredump;
3154 * then it passes on a vnode and a size limit to the process-specific
3155 * coredump routine if there is one; if there _is not_ one, it returns
3156 * ENOSYS; otherwise it returns the error from the process-specific routine.
3160 coredump(struct thread *td)
3162 struct proc *p = td->td_proc;
3163 register struct vnode *vp;
3164 register struct ucred *cred = td->td_ucred;
3166 struct nameidata nd;
3168 int error, error1, flags, locked;
3170 char *name; /* name of corefile */
3175 #ifdef COMPRESS_USER_CORES
3176 compress = compress_user_cores;
3180 PROC_LOCK_ASSERT(p, MA_OWNED);
3181 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3182 _STOPEVENT(p, S_CORE, 0);
3184 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid, td,
3189 audit_proc_coredump(td, NULL, EINVAL);
3193 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3196 audit_proc_coredump(td, name, EFAULT);
3203 * Note that the bulk of limit checking is done after
3204 * the corefile is created. The exception is if the limit
3205 * for corefiles is 0, in which case we don't bother
3206 * creating the corefile at all. This layout means that
3207 * a corefile is truncated instead of not being created,
3208 * if it is larger than the limit.
3210 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3211 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3214 audit_proc_coredump(td, name, EFBIG);
3222 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3223 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3224 error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
3228 audit_proc_coredump(td, name, error);
3233 vfslocked = NDHASGIANT(&nd);
3234 NDFREE(&nd, NDF_ONLY_PNBUF);
3237 /* Don't dump to non-regular files or files with links. */
3238 if (vp->v_type != VREG ||
3239 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
3246 lf.l_whence = SEEK_SET;
3249 lf.l_type = F_WRLCK;
3250 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3252 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3253 lf.l_type = F_UNLCK;
3255 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3256 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3258 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3260 VFS_UNLOCK_GIANT(vfslocked);
3266 if (set_core_nodump_flag)
3267 vattr.va_flags = UF_NODUMP;
3268 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3269 VOP_SETATTR(vp, &vattr, cred);
3271 vn_finished_write(mp);
3273 p->p_acflag |= ACORE;
3276 error = p->p_sysent->sv_coredump ?
3277 p->p_sysent->sv_coredump(td, vp, limit, compress ? IMGACT_CORE_COMPRESS : 0) :
3281 lf.l_type = F_UNLCK;
3282 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3285 error1 = vn_close(vp, FWRITE, cred, td);
3290 audit_proc_coredump(td, name, error);
3293 VFS_UNLOCK_GIANT(vfslocked);
3298 * Nonexistent system call-- signal process (may want to handle it). Flag
3299 * error in case process won't see signal immediately (blocked or ignored).
3301 #ifndef _SYS_SYSPROTO_H_
3310 struct nosys_args *args;
3312 struct proc *p = td->td_proc;
3315 kern_psignal(p, SIGSYS);
3321 * Send a SIGIO or SIGURG signal to a process or process group using stored
3322 * credentials rather than those of the current process.
3325 pgsigio(sigiop, sig, checkctty)
3326 struct sigio **sigiop;
3330 struct sigio *sigio;
3332 ksiginfo_init(&ksi);
3333 ksi.ksi_signo = sig;
3334 ksi.ksi_code = SI_KERNEL;
3338 if (sigio == NULL) {
3342 if (sigio->sio_pgid > 0) {
3343 PROC_LOCK(sigio->sio_proc);
3344 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3345 kern_psignal(sigio->sio_proc, sig);
3346 PROC_UNLOCK(sigio->sio_proc);
3347 } else if (sigio->sio_pgid < 0) {
3350 PGRP_LOCK(sigio->sio_pgrp);
3351 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3353 if (p->p_state == PRS_NORMAL &&
3354 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3355 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3356 kern_psignal(p, sig);
3359 PGRP_UNLOCK(sigio->sio_pgrp);
3365 filt_sigattach(struct knote *kn)
3367 struct proc *p = curproc;
3369 kn->kn_ptr.p_proc = p;
3370 kn->kn_flags |= EV_CLEAR; /* automatically set */
3372 knlist_add(&p->p_klist, kn, 0);
3378 filt_sigdetach(struct knote *kn)
3380 struct proc *p = kn->kn_ptr.p_proc;
3382 knlist_remove(&p->p_klist, kn, 0);
3386 * signal knotes are shared with proc knotes, so we apply a mask to
3387 * the hint in order to differentiate them from process hints. This
3388 * could be avoided by using a signal-specific knote list, but probably
3389 * isn't worth the trouble.
3392 filt_signal(struct knote *kn, long hint)
3395 if (hint & NOTE_SIGNAL) {
3396 hint &= ~NOTE_SIGNAL;
3398 if (kn->kn_id == hint)
3401 return (kn->kn_data != 0);
3409 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3411 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3416 sigacts_free(struct sigacts *ps)
3419 mtx_lock(&ps->ps_mtx);
3421 if (ps->ps_refcnt == 0) {
3422 mtx_destroy(&ps->ps_mtx);
3423 free(ps, M_SUBPROC);
3425 mtx_unlock(&ps->ps_mtx);
3429 sigacts_hold(struct sigacts *ps)
3431 mtx_lock(&ps->ps_mtx);
3433 mtx_unlock(&ps->ps_mtx);
3438 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3441 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3442 mtx_lock(&src->ps_mtx);
3443 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3444 mtx_unlock(&src->ps_mtx);
3448 sigacts_shared(struct sigacts *ps)
3452 mtx_lock(&ps->ps_mtx);
3453 shared = ps->ps_refcnt > 1;
3454 mtx_unlock(&ps->ps_mtx);