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"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
50 #include <sys/condvar.h>
51 #include <sys/event.h>
52 #include <sys/fcntl.h>
53 #include <sys/imgact.h>
54 #include <sys/kernel.h>
56 #include <sys/ktrace.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/namei.h>
62 #include <sys/posix4.h>
63 #include <sys/pioctl.h>
64 #include <sys/racct.h>
65 #include <sys/resourcevar.h>
68 #include <sys/sleepqueue.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #include <sys/sysent.h>
75 #include <sys/syslog.h>
76 #include <sys/sysproto.h>
77 #include <sys/timers.h>
78 #include <sys/unistd.h>
81 #include <vm/vm_extern.h>
86 #include <machine/cpu.h>
88 #include <security/audit/audit.h>
90 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
92 SDT_PROVIDER_DECLARE(proc);
93 SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send);
94 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *");
95 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *");
96 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int");
97 SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear);
98 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int");
99 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
100 SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard);
101 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
102 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
103 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
105 static int coredump(struct thread *);
106 static char *expand_name(const char *, uid_t, pid_t, struct thread *, int);
107 static int killpg1(struct thread *td, int sig, int pgid, int all,
109 static int issignal(struct thread *td, int stop_allowed);
110 static int sigprop(int sig);
111 static void tdsigwakeup(struct thread *, int, sig_t, int);
112 static void sig_suspend_threads(struct thread *, struct proc *, int);
113 static int filt_sigattach(struct knote *kn);
114 static void filt_sigdetach(struct knote *kn);
115 static int filt_signal(struct knote *kn, long hint);
116 static struct thread *sigtd(struct proc *p, int sig, int prop);
117 static void sigqueue_start(void);
119 static uma_zone_t ksiginfo_zone = NULL;
120 struct filterops sig_filtops = {
122 .f_attach = filt_sigattach,
123 .f_detach = filt_sigdetach,
124 .f_event = filt_signal,
127 static int kern_logsigexit = 1;
128 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
130 "Log processes quitting on abnormal signals to syslog(3)");
132 static int kern_forcesigexit = 1;
133 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
134 &kern_forcesigexit, 0, "Force trap signal to be handled");
136 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
138 static int max_pending_per_proc = 128;
139 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
140 &max_pending_per_proc, 0, "Max pending signals per proc");
142 static int preallocate_siginfo = 1024;
143 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
144 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
145 &preallocate_siginfo, 0, "Preallocated signal memory size");
147 static int signal_overflow = 0;
148 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
149 &signal_overflow, 0, "Number of signals overflew");
151 static int signal_alloc_fail = 0;
152 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
153 &signal_alloc_fail, 0, "signals failed to be allocated");
155 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
158 * Policy -- Can ucred cr1 send SIGIO to process cr2?
159 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
160 * in the right situations.
162 #define CANSIGIO(cr1, cr2) \
163 ((cr1)->cr_uid == 0 || \
164 (cr1)->cr_ruid == (cr2)->cr_ruid || \
165 (cr1)->cr_uid == (cr2)->cr_ruid || \
166 (cr1)->cr_ruid == (cr2)->cr_uid || \
167 (cr1)->cr_uid == (cr2)->cr_uid)
169 static int sugid_coredump;
170 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
171 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
173 static int do_coredump = 1;
174 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
175 &do_coredump, 0, "Enable/Disable coredumps");
177 static int set_core_nodump_flag = 0;
178 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
179 0, "Enable setting the NODUMP flag on coredump files");
182 * Signal properties and actions.
183 * The array below categorizes the signals and their default actions
184 * according to the following properties:
186 #define SA_KILL 0x01 /* terminates process by default */
187 #define SA_CORE 0x02 /* ditto and coredumps */
188 #define SA_STOP 0x04 /* suspend process */
189 #define SA_TTYSTOP 0x08 /* ditto, from tty */
190 #define SA_IGNORE 0x10 /* ignore by default */
191 #define SA_CONT 0x20 /* continue if suspended */
192 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
193 #define SA_PROC 0x80 /* deliverable to any thread */
195 static int sigproptbl[NSIG] = {
196 SA_KILL|SA_PROC, /* SIGHUP */
197 SA_KILL|SA_PROC, /* SIGINT */
198 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
199 SA_KILL|SA_CORE, /* SIGILL */
200 SA_KILL|SA_CORE, /* SIGTRAP */
201 SA_KILL|SA_CORE, /* SIGABRT */
202 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
203 SA_KILL|SA_CORE, /* SIGFPE */
204 SA_KILL|SA_PROC, /* SIGKILL */
205 SA_KILL|SA_CORE, /* SIGBUS */
206 SA_KILL|SA_CORE, /* SIGSEGV */
207 SA_KILL|SA_CORE, /* SIGSYS */
208 SA_KILL|SA_PROC, /* SIGPIPE */
209 SA_KILL|SA_PROC, /* SIGALRM */
210 SA_KILL|SA_PROC, /* SIGTERM */
211 SA_IGNORE|SA_PROC, /* SIGURG */
212 SA_STOP|SA_PROC, /* SIGSTOP */
213 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
214 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
215 SA_IGNORE|SA_PROC, /* SIGCHLD */
216 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
217 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
218 SA_IGNORE|SA_PROC, /* SIGIO */
219 SA_KILL, /* SIGXCPU */
220 SA_KILL, /* SIGXFSZ */
221 SA_KILL|SA_PROC, /* SIGVTALRM */
222 SA_KILL|SA_PROC, /* SIGPROF */
223 SA_IGNORE|SA_PROC, /* SIGWINCH */
224 SA_IGNORE|SA_PROC, /* SIGINFO */
225 SA_KILL|SA_PROC, /* SIGUSR1 */
226 SA_KILL|SA_PROC, /* SIGUSR2 */
229 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
234 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
235 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
236 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
237 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
238 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
239 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
243 ksiginfo_alloc(int wait)
250 if (ksiginfo_zone != NULL)
251 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
256 ksiginfo_free(ksiginfo_t *ksi)
258 uma_zfree(ksiginfo_zone, ksi);
262 ksiginfo_tryfree(ksiginfo_t *ksi)
264 if (!(ksi->ksi_flags & KSI_EXT)) {
265 uma_zfree(ksiginfo_zone, ksi);
272 sigqueue_init(sigqueue_t *list, struct proc *p)
274 SIGEMPTYSET(list->sq_signals);
275 SIGEMPTYSET(list->sq_kill);
276 TAILQ_INIT(&list->sq_list);
278 list->sq_flags = SQ_INIT;
282 * Get a signal's ksiginfo.
284 * 0 - signal not found
285 * others - signal number
288 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
290 struct proc *p = sq->sq_proc;
291 struct ksiginfo *ksi, *next;
294 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
296 if (!SIGISMEMBER(sq->sq_signals, signo))
299 if (SIGISMEMBER(sq->sq_kill, signo)) {
301 SIGDELSET(sq->sq_kill, signo);
304 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
305 if (ksi->ksi_signo == signo) {
307 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
308 ksi->ksi_sigq = NULL;
309 ksiginfo_copy(ksi, si);
310 if (ksiginfo_tryfree(ksi) && p != NULL)
319 SIGDELSET(sq->sq_signals, signo);
320 si->ksi_signo = signo;
325 sigqueue_take(ksiginfo_t *ksi)
331 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
335 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
336 ksi->ksi_sigq = NULL;
337 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
340 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
341 kp = TAILQ_NEXT(kp, ksi_link)) {
342 if (kp->ksi_signo == ksi->ksi_signo)
345 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
346 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
350 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
352 struct proc *p = sq->sq_proc;
353 struct ksiginfo *ksi;
356 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
358 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
359 SIGADDSET(sq->sq_kill, signo);
363 /* directly insert the ksi, don't copy it */
364 if (si->ksi_flags & KSI_INS) {
365 if (si->ksi_flags & KSI_HEAD)
366 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
368 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
373 if (__predict_false(ksiginfo_zone == NULL)) {
374 SIGADDSET(sq->sq_kill, signo);
378 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
381 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
387 ksiginfo_copy(si, ksi);
388 ksi->ksi_signo = signo;
389 if (si->ksi_flags & KSI_HEAD)
390 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
392 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
396 if ((si->ksi_flags & KSI_TRAP) != 0 ||
397 (si->ksi_flags & KSI_SIGQ) == 0) {
399 SIGADDSET(sq->sq_kill, signo);
408 SIGADDSET(sq->sq_signals, signo);
413 sigqueue_flush(sigqueue_t *sq)
415 struct proc *p = sq->sq_proc;
418 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
421 PROC_LOCK_ASSERT(p, MA_OWNED);
423 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
424 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
425 ksi->ksi_sigq = NULL;
426 if (ksiginfo_tryfree(ksi) && p != NULL)
430 SIGEMPTYSET(sq->sq_signals);
431 SIGEMPTYSET(sq->sq_kill);
435 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
438 struct proc *p1, *p2;
439 ksiginfo_t *ksi, *next;
441 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
442 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
445 /* Move siginfo to target list */
446 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
447 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
448 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
451 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
458 /* Move pending bits to target list */
460 SIGSETAND(tmp, *set);
461 SIGSETOR(dst->sq_kill, tmp);
462 SIGSETNAND(src->sq_kill, tmp);
464 tmp = src->sq_signals;
465 SIGSETAND(tmp, *set);
466 SIGSETOR(dst->sq_signals, tmp);
467 SIGSETNAND(src->sq_signals, tmp);
472 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
477 SIGADDSET(set, signo);
478 sigqueue_move_set(src, dst, &set);
483 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
485 struct proc *p = sq->sq_proc;
486 ksiginfo_t *ksi, *next;
488 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
490 /* Remove siginfo queue */
491 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
492 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
493 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
494 ksi->ksi_sigq = NULL;
495 if (ksiginfo_tryfree(ksi) && p != NULL)
499 SIGSETNAND(sq->sq_kill, *set);
500 SIGSETNAND(sq->sq_signals, *set);
504 sigqueue_delete(sigqueue_t *sq, int signo)
509 SIGADDSET(set, signo);
510 sigqueue_delete_set(sq, &set);
513 /* Remove a set of signals for a process */
515 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
520 PROC_LOCK_ASSERT(p, MA_OWNED);
522 sigqueue_init(&worklist, NULL);
523 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
525 FOREACH_THREAD_IN_PROC(p, td0)
526 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
528 sigqueue_flush(&worklist);
532 sigqueue_delete_proc(struct proc *p, int signo)
537 SIGADDSET(set, signo);
538 sigqueue_delete_set_proc(p, &set);
542 sigqueue_delete_stopmask_proc(struct proc *p)
547 SIGADDSET(set, SIGSTOP);
548 SIGADDSET(set, SIGTSTP);
549 SIGADDSET(set, SIGTTIN);
550 SIGADDSET(set, SIGTTOU);
551 sigqueue_delete_set_proc(p, &set);
555 * Determine signal that should be delivered to process p, the current
556 * process, 0 if none. If there is a pending stop signal with default
557 * action, the process stops in issignal().
560 cursig(struct thread *td, int stop_allowed)
562 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
563 KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
564 stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
565 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
566 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
567 return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
571 * Arrange for ast() to handle unmasked pending signals on return to user
572 * mode. This must be called whenever a signal is added to td_sigqueue or
573 * unmasked in td_sigmask.
576 signotify(struct thread *td)
582 PROC_LOCK_ASSERT(p, MA_OWNED);
584 if (SIGPENDING(td)) {
586 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
592 sigonstack(size_t sp)
594 struct thread *td = curthread;
596 return ((td->td_pflags & TDP_ALTSTACK) ?
597 #if defined(COMPAT_43)
598 ((td->td_sigstk.ss_size == 0) ?
599 (td->td_sigstk.ss_flags & SS_ONSTACK) :
600 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
602 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
611 if (sig > 0 && sig < NSIG)
612 return (sigproptbl[_SIG_IDX(sig)]);
617 sig_ffs(sigset_t *set)
621 for (i = 0; i < _SIG_WORDS; i++)
623 return (ffs(set->__bits[i]) + (i * 32));
634 kern_sigaction(td, sig, act, oact, flags)
637 struct sigaction *act, *oact;
641 struct proc *p = td->td_proc;
643 if (!_SIG_VALID(sig))
648 mtx_lock(&ps->ps_mtx);
650 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
652 if (SIGISMEMBER(ps->ps_sigonstack, sig))
653 oact->sa_flags |= SA_ONSTACK;
654 if (!SIGISMEMBER(ps->ps_sigintr, sig))
655 oact->sa_flags |= SA_RESTART;
656 if (SIGISMEMBER(ps->ps_sigreset, sig))
657 oact->sa_flags |= SA_RESETHAND;
658 if (SIGISMEMBER(ps->ps_signodefer, sig))
659 oact->sa_flags |= SA_NODEFER;
660 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
661 oact->sa_flags |= SA_SIGINFO;
663 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
665 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
666 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
667 oact->sa_flags |= SA_NOCLDSTOP;
668 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
669 oact->sa_flags |= SA_NOCLDWAIT;
672 if ((sig == SIGKILL || sig == SIGSTOP) &&
673 act->sa_handler != SIG_DFL) {
674 mtx_unlock(&ps->ps_mtx);
680 * Change setting atomically.
683 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
684 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
685 if (act->sa_flags & SA_SIGINFO) {
686 ps->ps_sigact[_SIG_IDX(sig)] =
687 (__sighandler_t *)act->sa_sigaction;
688 SIGADDSET(ps->ps_siginfo, sig);
690 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
691 SIGDELSET(ps->ps_siginfo, sig);
693 if (!(act->sa_flags & SA_RESTART))
694 SIGADDSET(ps->ps_sigintr, sig);
696 SIGDELSET(ps->ps_sigintr, sig);
697 if (act->sa_flags & SA_ONSTACK)
698 SIGADDSET(ps->ps_sigonstack, sig);
700 SIGDELSET(ps->ps_sigonstack, sig);
701 if (act->sa_flags & SA_RESETHAND)
702 SIGADDSET(ps->ps_sigreset, sig);
704 SIGDELSET(ps->ps_sigreset, sig);
705 if (act->sa_flags & SA_NODEFER)
706 SIGADDSET(ps->ps_signodefer, sig);
708 SIGDELSET(ps->ps_signodefer, sig);
709 if (sig == SIGCHLD) {
710 if (act->sa_flags & SA_NOCLDSTOP)
711 ps->ps_flag |= PS_NOCLDSTOP;
713 ps->ps_flag &= ~PS_NOCLDSTOP;
714 if (act->sa_flags & SA_NOCLDWAIT) {
716 * Paranoia: since SA_NOCLDWAIT is implemented
717 * by reparenting the dying child to PID 1 (and
718 * trust it to reap the zombie), PID 1 itself
719 * is forbidden to set SA_NOCLDWAIT.
722 ps->ps_flag &= ~PS_NOCLDWAIT;
724 ps->ps_flag |= PS_NOCLDWAIT;
726 ps->ps_flag &= ~PS_NOCLDWAIT;
727 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
728 ps->ps_flag |= PS_CLDSIGIGN;
730 ps->ps_flag &= ~PS_CLDSIGIGN;
733 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
734 * and for signals set to SIG_DFL where the default is to
735 * ignore. However, don't put SIGCONT in ps_sigignore, as we
736 * have to restart the process.
738 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
739 (sigprop(sig) & SA_IGNORE &&
740 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
741 /* never to be seen again */
742 sigqueue_delete_proc(p, sig);
744 /* easier in psignal */
745 SIGADDSET(ps->ps_sigignore, sig);
746 SIGDELSET(ps->ps_sigcatch, sig);
748 SIGDELSET(ps->ps_sigignore, sig);
749 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
750 SIGDELSET(ps->ps_sigcatch, sig);
752 SIGADDSET(ps->ps_sigcatch, sig);
754 #ifdef COMPAT_FREEBSD4
755 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
756 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
757 (flags & KSA_FREEBSD4) == 0)
758 SIGDELSET(ps->ps_freebsd4, sig);
760 SIGADDSET(ps->ps_freebsd4, sig);
763 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
764 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
765 (flags & KSA_OSIGSET) == 0)
766 SIGDELSET(ps->ps_osigset, sig);
768 SIGADDSET(ps->ps_osigset, sig);
771 mtx_unlock(&ps->ps_mtx);
776 #ifndef _SYS_SYSPROTO_H_
777 struct sigaction_args {
779 struct sigaction *act;
780 struct sigaction *oact;
786 register struct sigaction_args *uap;
788 struct sigaction act, oact;
789 register struct sigaction *actp, *oactp;
792 actp = (uap->act != NULL) ? &act : NULL;
793 oactp = (uap->oact != NULL) ? &oact : NULL;
795 error = copyin(uap->act, actp, sizeof(act));
799 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
801 error = copyout(oactp, uap->oact, sizeof(oact));
805 #ifdef COMPAT_FREEBSD4
806 #ifndef _SYS_SYSPROTO_H_
807 struct freebsd4_sigaction_args {
809 struct sigaction *act;
810 struct sigaction *oact;
814 freebsd4_sigaction(td, uap)
816 register struct freebsd4_sigaction_args *uap;
818 struct sigaction act, oact;
819 register struct sigaction *actp, *oactp;
823 actp = (uap->act != NULL) ? &act : NULL;
824 oactp = (uap->oact != NULL) ? &oact : NULL;
826 error = copyin(uap->act, actp, sizeof(act));
830 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
832 error = copyout(oactp, uap->oact, sizeof(oact));
835 #endif /* COMAPT_FREEBSD4 */
837 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
838 #ifndef _SYS_SYSPROTO_H_
839 struct osigaction_args {
841 struct osigaction *nsa;
842 struct osigaction *osa;
848 register struct osigaction_args *uap;
850 struct osigaction sa;
851 struct sigaction nsa, osa;
852 register struct sigaction *nsap, *osap;
855 if (uap->signum <= 0 || uap->signum >= ONSIG)
858 nsap = (uap->nsa != NULL) ? &nsa : NULL;
859 osap = (uap->osa != NULL) ? &osa : NULL;
862 error = copyin(uap->nsa, &sa, sizeof(sa));
865 nsap->sa_handler = sa.sa_handler;
866 nsap->sa_flags = sa.sa_flags;
867 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
869 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
870 if (osap && !error) {
871 sa.sa_handler = osap->sa_handler;
872 sa.sa_flags = osap->sa_flags;
873 SIG2OSIG(osap->sa_mask, sa.sa_mask);
874 error = copyout(&sa, uap->osa, sizeof(sa));
879 #if !defined(__i386__)
880 /* Avoid replicating the same stub everywhere */
884 struct osigreturn_args *uap;
887 return (nosys(td, (struct nosys_args *)uap));
890 #endif /* COMPAT_43 */
893 * Initialize signal state for process 0;
894 * set to ignore signals that are ignored by default.
905 mtx_lock(&ps->ps_mtx);
906 for (i = 1; i <= NSIG; i++)
907 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
908 SIGADDSET(ps->ps_sigignore, i);
909 mtx_unlock(&ps->ps_mtx);
914 * Reset signals for an exec of the specified process.
917 execsigs(struct proc *p)
924 * Reset caught signals. Held signals remain held
925 * through td_sigmask (unless they were caught,
926 * and are now ignored by default).
928 PROC_LOCK_ASSERT(p, MA_OWNED);
929 td = FIRST_THREAD_IN_PROC(p);
931 mtx_lock(&ps->ps_mtx);
932 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
933 sig = sig_ffs(&ps->ps_sigcatch);
934 SIGDELSET(ps->ps_sigcatch, sig);
935 if (sigprop(sig) & SA_IGNORE) {
937 SIGADDSET(ps->ps_sigignore, sig);
938 sigqueue_delete_proc(p, sig);
940 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
943 * Reset stack state to the user stack.
944 * Clear set of signals caught on the signal stack.
946 td->td_sigstk.ss_flags = SS_DISABLE;
947 td->td_sigstk.ss_size = 0;
948 td->td_sigstk.ss_sp = 0;
949 td->td_pflags &= ~TDP_ALTSTACK;
951 * Reset no zombies if child dies flag as Solaris does.
953 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
954 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
955 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
956 mtx_unlock(&ps->ps_mtx);
962 * Manipulate signal mask.
965 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
968 sigset_t new_block, oset1;
973 if (!(flags & SIGPROCMASK_PROC_LOCKED))
976 *oset = td->td_sigmask;
983 oset1 = td->td_sigmask;
984 SIGSETOR(td->td_sigmask, *set);
985 new_block = td->td_sigmask;
986 SIGSETNAND(new_block, oset1);
989 SIGSETNAND(td->td_sigmask, *set);
994 oset1 = td->td_sigmask;
995 if (flags & SIGPROCMASK_OLD)
996 SIGSETLO(td->td_sigmask, *set);
998 td->td_sigmask = *set;
999 new_block = td->td_sigmask;
1000 SIGSETNAND(new_block, oset1);
1009 * The new_block set contains signals that were not previously
1010 * blocked, but are blocked now.
1012 * In case we block any signal that was not previously blocked
1013 * for td, and process has the signal pending, try to schedule
1014 * signal delivery to some thread that does not block the
1015 * signal, possibly waking it up.
1017 if (p->p_numthreads != 1)
1018 reschedule_signals(p, new_block, flags);
1022 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1027 #ifndef _SYS_SYSPROTO_H_
1028 struct sigprocmask_args {
1030 const sigset_t *set;
1035 sigprocmask(td, uap)
1036 register struct thread *td;
1037 struct sigprocmask_args *uap;
1040 sigset_t *setp, *osetp;
1043 setp = (uap->set != NULL) ? &set : NULL;
1044 osetp = (uap->oset != NULL) ? &oset : NULL;
1046 error = copyin(uap->set, setp, sizeof(set));
1050 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1051 if (osetp && !error) {
1052 error = copyout(osetp, uap->oset, sizeof(oset));
1057 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1058 #ifndef _SYS_SYSPROTO_H_
1059 struct osigprocmask_args {
1065 osigprocmask(td, uap)
1066 register struct thread *td;
1067 struct osigprocmask_args *uap;
1072 OSIG2SIG(uap->mask, set);
1073 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1074 SIG2OSIG(oset, td->td_retval[0]);
1077 #endif /* COMPAT_43 */
1080 sigwait(struct thread *td, struct sigwait_args *uap)
1086 error = copyin(uap->set, &set, sizeof(set));
1088 td->td_retval[0] = error;
1092 error = kern_sigtimedwait(td, set, &ksi, NULL);
1094 if (error == ERESTART)
1096 td->td_retval[0] = error;
1100 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1101 td->td_retval[0] = error;
1106 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1109 struct timespec *timeout;
1115 error = copyin(uap->timeout, &ts, sizeof(ts));
1123 error = copyin(uap->set, &set, sizeof(set));
1127 error = kern_sigtimedwait(td, set, &ksi, timeout);
1132 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1135 td->td_retval[0] = ksi.ksi_signo;
1140 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1146 error = copyin(uap->set, &set, sizeof(set));
1150 error = kern_sigtimedwait(td, set, &ksi, NULL);
1155 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1158 td->td_retval[0] = ksi.ksi_signo;
1163 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1164 struct timespec *timeout)
1167 sigset_t saved_mask, new_block;
1169 int error, sig, timo, timevalid = 0;
1170 struct timespec rts, ets, ts;
1178 if (timeout != NULL) {
1179 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1181 getnanouptime(&rts);
1183 timespecadd(&ets, timeout);
1187 /* Some signals can not be waited for. */
1188 SIG_CANTMASK(waitset);
1191 saved_mask = td->td_sigmask;
1192 SIGSETNAND(td->td_sigmask, waitset);
1194 mtx_lock(&ps->ps_mtx);
1195 sig = cursig(td, SIG_STOP_ALLOWED);
1196 mtx_unlock(&ps->ps_mtx);
1197 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1198 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1199 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1209 * POSIX says this must be checked after looking for pending
1212 if (timeout != NULL) {
1217 getnanouptime(&rts);
1218 if (timespeccmp(&rts, &ets, >=)) {
1223 timespecsub(&ts, &rts);
1224 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1230 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1232 if (timeout != NULL) {
1233 if (error == ERESTART) {
1234 /* Timeout can not be restarted. */
1236 } else if (error == EAGAIN) {
1237 /* We will calculate timeout by ourself. */
1243 new_block = saved_mask;
1244 SIGSETNAND(new_block, td->td_sigmask);
1245 td->td_sigmask = saved_mask;
1247 * Fewer signals can be delivered to us, reschedule signal
1250 if (p->p_numthreads != 1)
1251 reschedule_signals(p, new_block, 0);
1254 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1256 if (ksi->ksi_code == SI_TIMER)
1257 itimer_accept(p, ksi->ksi_timerid, ksi);
1260 if (KTRPOINT(td, KTR_PSIG)) {
1263 mtx_lock(&ps->ps_mtx);
1264 action = ps->ps_sigact[_SIG_IDX(sig)];
1265 mtx_unlock(&ps->ps_mtx);
1266 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1276 #ifndef _SYS_SYSPROTO_H_
1277 struct sigpending_args {
1284 struct sigpending_args *uap;
1286 struct proc *p = td->td_proc;
1290 pending = p->p_sigqueue.sq_signals;
1291 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1293 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1296 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1297 #ifndef _SYS_SYSPROTO_H_
1298 struct osigpending_args {
1303 osigpending(td, uap)
1305 struct osigpending_args *uap;
1307 struct proc *p = td->td_proc;
1311 pending = p->p_sigqueue.sq_signals;
1312 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1314 SIG2OSIG(pending, td->td_retval[0]);
1317 #endif /* COMPAT_43 */
1319 #if defined(COMPAT_43)
1321 * Generalized interface signal handler, 4.3-compatible.
1323 #ifndef _SYS_SYSPROTO_H_
1324 struct osigvec_args {
1334 register struct osigvec_args *uap;
1337 struct sigaction nsa, osa;
1338 register struct sigaction *nsap, *osap;
1341 if (uap->signum <= 0 || uap->signum >= ONSIG)
1343 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1344 osap = (uap->osv != NULL) ? &osa : NULL;
1346 error = copyin(uap->nsv, &vec, sizeof(vec));
1349 nsap->sa_handler = vec.sv_handler;
1350 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1351 nsap->sa_flags = vec.sv_flags;
1352 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1354 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1355 if (osap && !error) {
1356 vec.sv_handler = osap->sa_handler;
1357 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1358 vec.sv_flags = osap->sa_flags;
1359 vec.sv_flags &= ~SA_NOCLDWAIT;
1360 vec.sv_flags ^= SA_RESTART;
1361 error = copyout(&vec, uap->osv, sizeof(vec));
1366 #ifndef _SYS_SYSPROTO_H_
1367 struct osigblock_args {
1373 register struct thread *td;
1374 struct osigblock_args *uap;
1378 OSIG2SIG(uap->mask, set);
1379 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1380 SIG2OSIG(oset, td->td_retval[0]);
1384 #ifndef _SYS_SYSPROTO_H_
1385 struct osigsetmask_args {
1390 osigsetmask(td, uap)
1392 struct osigsetmask_args *uap;
1396 OSIG2SIG(uap->mask, set);
1397 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1398 SIG2OSIG(oset, td->td_retval[0]);
1401 #endif /* COMPAT_43 */
1404 * Suspend calling thread until signal, providing mask to be set in the
1407 #ifndef _SYS_SYSPROTO_H_
1408 struct sigsuspend_args {
1409 const sigset_t *sigmask;
1416 struct sigsuspend_args *uap;
1421 error = copyin(uap->sigmask, &mask, sizeof(mask));
1424 return (kern_sigsuspend(td, mask));
1428 kern_sigsuspend(struct thread *td, sigset_t mask)
1430 struct proc *p = td->td_proc;
1434 * When returning from sigsuspend, we want
1435 * the old mask to be restored after the
1436 * signal handler has finished. Thus, we
1437 * save it here and mark the sigacts structure
1441 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1442 SIGPROCMASK_PROC_LOCKED);
1443 td->td_pflags |= TDP_OLDMASK;
1446 * Process signals now. Otherwise, we can get spurious wakeup
1447 * due to signal entered process queue, but delivered to other
1448 * thread. But sigsuspend should return only on signal
1451 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1452 for (has_sig = 0; !has_sig;) {
1453 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1456 thread_suspend_check(0);
1457 mtx_lock(&p->p_sigacts->ps_mtx);
1458 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
1459 has_sig += postsig(sig);
1460 mtx_unlock(&p->p_sigacts->ps_mtx);
1463 return (EJUSTRETURN);
1466 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1468 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1469 * convention: libc stub passes mask, not pointer, to save a copyin.
1471 #ifndef _SYS_SYSPROTO_H_
1472 struct osigsuspend_args {
1478 osigsuspend(td, uap)
1480 struct osigsuspend_args *uap;
1484 OSIG2SIG(uap->mask, mask);
1485 return (kern_sigsuspend(td, mask));
1487 #endif /* COMPAT_43 */
1489 #if defined(COMPAT_43)
1490 #ifndef _SYS_SYSPROTO_H_
1491 struct osigstack_args {
1492 struct sigstack *nss;
1493 struct sigstack *oss;
1500 register struct osigstack_args *uap;
1502 struct sigstack nss, oss;
1505 if (uap->nss != NULL) {
1506 error = copyin(uap->nss, &nss, sizeof(nss));
1510 oss.ss_sp = td->td_sigstk.ss_sp;
1511 oss.ss_onstack = sigonstack(cpu_getstack(td));
1512 if (uap->nss != NULL) {
1513 td->td_sigstk.ss_sp = nss.ss_sp;
1514 td->td_sigstk.ss_size = 0;
1515 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1516 td->td_pflags |= TDP_ALTSTACK;
1518 if (uap->oss != NULL)
1519 error = copyout(&oss, uap->oss, sizeof(oss));
1523 #endif /* COMPAT_43 */
1525 #ifndef _SYS_SYSPROTO_H_
1526 struct sigaltstack_args {
1533 sigaltstack(td, uap)
1535 register struct sigaltstack_args *uap;
1540 if (uap->ss != NULL) {
1541 error = copyin(uap->ss, &ss, sizeof(ss));
1545 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1546 (uap->oss != NULL) ? &oss : NULL);
1549 if (uap->oss != NULL)
1550 error = copyout(&oss, uap->oss, sizeof(stack_t));
1555 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1557 struct proc *p = td->td_proc;
1560 oonstack = sigonstack(cpu_getstack(td));
1563 *oss = td->td_sigstk;
1564 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1565 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1571 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1573 if (!(ss->ss_flags & SS_DISABLE)) {
1574 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1577 td->td_sigstk = *ss;
1578 td->td_pflags |= TDP_ALTSTACK;
1580 td->td_pflags &= ~TDP_ALTSTACK;
1587 * Common code for kill process group/broadcast kill.
1588 * cp is calling process.
1591 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1601 sx_slock(&allproc_lock);
1602 FOREACH_PROC_IN_SYSTEM(p) {
1604 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1605 p == td->td_proc || p->p_state == PRS_NEW) {
1609 if (p_cansignal(td, p, sig) == 0) {
1612 pksignal(p, sig, ksi);
1616 sx_sunlock(&allproc_lock);
1618 sx_slock(&proctree_lock);
1621 * zero pgid means send to my process group.
1623 pgrp = td->td_proc->p_pgrp;
1626 pgrp = pgfind(pgid);
1628 sx_sunlock(&proctree_lock);
1632 sx_sunlock(&proctree_lock);
1633 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1635 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1636 p->p_state == PRS_NEW) {
1640 if (p_cansignal(td, p, sig) == 0) {
1643 pksignal(p, sig, ksi);
1649 return (nfound ? 0 : ESRCH);
1652 #ifndef _SYS_SYSPROTO_H_
1660 kill(struct thread *td, struct kill_args *uap)
1666 AUDIT_ARG_SIGNUM(uap->signum);
1667 AUDIT_ARG_PID(uap->pid);
1668 if ((u_int)uap->signum > _SIG_MAXSIG)
1671 ksiginfo_init(&ksi);
1672 ksi.ksi_signo = uap->signum;
1673 ksi.ksi_code = SI_USER;
1674 ksi.ksi_pid = td->td_proc->p_pid;
1675 ksi.ksi_uid = td->td_ucred->cr_ruid;
1678 /* kill single process */
1679 if ((p = pfind(uap->pid)) == NULL) {
1680 if ((p = zpfind(uap->pid)) == NULL)
1683 AUDIT_ARG_PROCESS(p);
1684 error = p_cansignal(td, p, uap->signum);
1685 if (error == 0 && uap->signum)
1686 pksignal(p, uap->signum, &ksi);
1691 case -1: /* broadcast signal */
1692 return (killpg1(td, uap->signum, 0, 1, &ksi));
1693 case 0: /* signal own process group */
1694 return (killpg1(td, uap->signum, 0, 0, &ksi));
1695 default: /* negative explicit process group */
1696 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1701 #if defined(COMPAT_43)
1702 #ifndef _SYS_SYSPROTO_H_
1703 struct okillpg_args {
1710 okillpg(struct thread *td, struct okillpg_args *uap)
1714 AUDIT_ARG_SIGNUM(uap->signum);
1715 AUDIT_ARG_PID(uap->pgid);
1716 if ((u_int)uap->signum > _SIG_MAXSIG)
1719 ksiginfo_init(&ksi);
1720 ksi.ksi_signo = uap->signum;
1721 ksi.ksi_code = SI_USER;
1722 ksi.ksi_pid = td->td_proc->p_pid;
1723 ksi.ksi_uid = td->td_ucred->cr_ruid;
1724 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1726 #endif /* COMPAT_43 */
1728 #ifndef _SYS_SYSPROTO_H_
1729 struct sigqueue_args {
1732 /* union sigval */ void *value;
1736 sigqueue(struct thread *td, struct sigqueue_args *uap)
1742 if ((u_int)uap->signum > _SIG_MAXSIG)
1746 * Specification says sigqueue can only send signal to
1752 if ((p = pfind(uap->pid)) == NULL) {
1753 if ((p = zpfind(uap->pid)) == NULL)
1756 error = p_cansignal(td, p, uap->signum);
1757 if (error == 0 && uap->signum != 0) {
1758 ksiginfo_init(&ksi);
1759 ksi.ksi_flags = KSI_SIGQ;
1760 ksi.ksi_signo = uap->signum;
1761 ksi.ksi_code = SI_QUEUE;
1762 ksi.ksi_pid = td->td_proc->p_pid;
1763 ksi.ksi_uid = td->td_ucred->cr_ruid;
1764 ksi.ksi_value.sival_ptr = uap->value;
1765 error = pksignal(p, ksi.ksi_signo, &ksi);
1772 * Send a signal to a process group.
1775 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1780 sx_slock(&proctree_lock);
1781 pgrp = pgfind(pgid);
1782 sx_sunlock(&proctree_lock);
1784 pgsignal(pgrp, sig, 0, ksi);
1791 * Send a signal to a process group. If checktty is 1,
1792 * limit to members which have a controlling terminal.
1795 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1800 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1801 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1803 if (p->p_state == PRS_NORMAL &&
1804 (checkctty == 0 || p->p_flag & P_CONTROLT))
1805 pksignal(p, sig, ksi);
1812 * Send a signal caused by a trap to the current thread. If it will be
1813 * caught immediately, deliver it with correct code. Otherwise, post it
1817 trapsignal(struct thread *td, ksiginfo_t *ksi)
1826 sig = ksi->ksi_signo;
1827 code = ksi->ksi_code;
1828 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1832 mtx_lock(&ps->ps_mtx);
1833 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1834 !SIGISMEMBER(td->td_sigmask, sig)) {
1835 td->td_ru.ru_nsignals++;
1837 if (KTRPOINT(curthread, KTR_PSIG))
1838 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1839 &td->td_sigmask, code);
1841 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1842 ksi, &td->td_sigmask);
1843 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1844 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1845 SIGADDSET(mask, sig);
1846 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1847 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1848 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1850 * See kern_sigaction() for origin of this code.
1852 SIGDELSET(ps->ps_sigcatch, sig);
1853 if (sig != SIGCONT &&
1854 sigprop(sig) & SA_IGNORE)
1855 SIGADDSET(ps->ps_sigignore, sig);
1856 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1858 mtx_unlock(&ps->ps_mtx);
1861 * Avoid a possible infinite loop if the thread
1862 * masking the signal or process is ignoring the
1865 if (kern_forcesigexit &&
1866 (SIGISMEMBER(td->td_sigmask, sig) ||
1867 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1868 SIGDELSET(td->td_sigmask, sig);
1869 SIGDELSET(ps->ps_sigcatch, sig);
1870 SIGDELSET(ps->ps_sigignore, sig);
1871 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1873 mtx_unlock(&ps->ps_mtx);
1874 p->p_code = code; /* XXX for core dump/debugger */
1875 p->p_sig = sig; /* XXX to verify code */
1876 tdsendsignal(p, td, sig, ksi);
1881 static struct thread *
1882 sigtd(struct proc *p, int sig, int prop)
1884 struct thread *td, *signal_td;
1886 PROC_LOCK_ASSERT(p, MA_OWNED);
1889 * Check if current thread can handle the signal without
1890 * switching context to another thread.
1892 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1895 FOREACH_THREAD_IN_PROC(p, td) {
1896 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1901 if (signal_td == NULL)
1902 signal_td = FIRST_THREAD_IN_PROC(p);
1907 * Send the signal to the process. If the signal has an action, the action
1908 * is usually performed by the target process rather than the caller; we add
1909 * the signal to the set of pending signals for the process.
1912 * o When a stop signal is sent to a sleeping process that takes the
1913 * default action, the process is stopped without awakening it.
1914 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1915 * regardless of the signal action (eg, blocked or ignored).
1917 * Other ignored signals are discarded immediately.
1919 * NB: This function may be entered from the debugger via the "kill" DDB
1920 * command. There is little that can be done to mitigate the possibly messy
1921 * side effects of this unwise possibility.
1924 psignal(struct proc *p, int sig)
1928 ksiginfo_init(&ksi);
1929 ksi.ksi_signo = sig;
1930 ksi.ksi_code = SI_KERNEL;
1931 (void) tdsendsignal(p, NULL, sig, &ksi);
1935 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1938 return (tdsendsignal(p, NULL, sig, ksi));
1941 /* Utility function for finding a thread to send signal event to. */
1943 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
1947 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1948 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
1960 tdsignal(struct thread *td, int sig)
1964 ksiginfo_init(&ksi);
1965 ksi.ksi_signo = sig;
1966 ksi.ksi_code = SI_KERNEL;
1967 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
1971 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
1974 (void) tdsendsignal(td->td_proc, td, sig, ksi);
1978 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
1981 sigqueue_t *sigqueue;
1988 MPASS(td == NULL || p == td->td_proc);
1989 PROC_LOCK_ASSERT(p, MA_OWNED);
1991 if (!_SIG_VALID(sig))
1992 panic("%s(): invalid signal %d", __func__, sig);
1994 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
1997 * IEEE Std 1003.1-2001: return success when killing a zombie.
1999 if (p->p_state == PRS_ZOMBIE) {
2000 if (ksi && (ksi->ksi_flags & KSI_INS))
2001 ksiginfo_tryfree(ksi);
2006 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2007 prop = sigprop(sig);
2010 td = sigtd(p, sig, prop);
2011 sigqueue = &p->p_sigqueue;
2013 KASSERT(td->td_proc == p, ("invalid thread"));
2014 sigqueue = &td->td_sigqueue;
2017 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2020 * If the signal is being ignored,
2021 * then we forget about it immediately.
2022 * (Note: we don't set SIGCONT in ps_sigignore,
2023 * and if it is set to SIG_IGN,
2024 * action will be SIG_DFL here.)
2026 mtx_lock(&ps->ps_mtx);
2027 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2028 SDT_PROBE(proc, kernel, , signal_discard, ps, td, sig, 0, 0 );
2030 mtx_unlock(&ps->ps_mtx);
2031 if (ksi && (ksi->ksi_flags & KSI_INS))
2032 ksiginfo_tryfree(ksi);
2035 if (SIGISMEMBER(td->td_sigmask, sig))
2037 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2041 if (SIGISMEMBER(ps->ps_sigintr, sig))
2045 mtx_unlock(&ps->ps_mtx);
2048 sigqueue_delete_stopmask_proc(p);
2049 else if (prop & SA_STOP) {
2051 * If sending a tty stop signal to a member of an orphaned
2052 * process group, discard the signal here if the action
2053 * is default; don't stop the process below if sleeping,
2054 * and don't clear any pending SIGCONT.
2056 if ((prop & SA_TTYSTOP) &&
2057 (p->p_pgrp->pg_jobc == 0) &&
2058 (action == SIG_DFL)) {
2059 if (ksi && (ksi->ksi_flags & KSI_INS))
2060 ksiginfo_tryfree(ksi);
2063 sigqueue_delete_proc(p, SIGCONT);
2064 if (p->p_flag & P_CONTINUED) {
2065 p->p_flag &= ~P_CONTINUED;
2066 PROC_LOCK(p->p_pptr);
2067 sigqueue_take(p->p_ksi);
2068 PROC_UNLOCK(p->p_pptr);
2072 ret = sigqueue_add(sigqueue, sig, ksi);
2077 * Defer further processing for signals which are held,
2078 * except that stopped processes must be continued by SIGCONT.
2080 if (action == SIG_HOLD &&
2081 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2084 * SIGKILL: Remove procfs STOPEVENTs.
2086 if (sig == SIGKILL) {
2087 /* from procfs_ioctl.c: PIOCBIC */
2089 /* from procfs_ioctl.c: PIOCCONT */
2094 * Some signals have a process-wide effect and a per-thread
2095 * component. Most processing occurs when the process next
2096 * tries to cross the user boundary, however there are some
2097 * times when processing needs to be done immediatly, such as
2098 * waking up threads so that they can cross the user boundary.
2099 * We try do the per-process part here.
2101 if (P_SHOULDSTOP(p)) {
2102 if (sig == SIGKILL) {
2104 * If traced process is already stopped,
2105 * then no further action is necessary.
2107 if (p->p_flag & P_TRACED)
2110 * SIGKILL sets process running.
2111 * It will die elsewhere.
2112 * All threads must be restarted.
2114 p->p_flag &= ~P_STOPPED_SIG;
2118 if (prop & SA_CONT) {
2120 * If traced process is already stopped,
2121 * then no further action is necessary.
2123 if (p->p_flag & P_TRACED)
2126 * If SIGCONT is default (or ignored), we continue the
2127 * process but don't leave the signal in sigqueue as
2128 * it has no further action. If SIGCONT is held, we
2129 * continue the process and leave the signal in
2130 * sigqueue. If the process catches SIGCONT, let it
2131 * handle the signal itself. If it isn't waiting on
2132 * an event, it goes back to run state.
2133 * Otherwise, process goes back to sleep state.
2135 p->p_flag &= ~P_STOPPED_SIG;
2137 if (p->p_numthreads == p->p_suspcount) {
2139 p->p_flag |= P_CONTINUED;
2140 p->p_xstat = SIGCONT;
2141 PROC_LOCK(p->p_pptr);
2142 childproc_continued(p);
2143 PROC_UNLOCK(p->p_pptr);
2146 if (action == SIG_DFL) {
2147 thread_unsuspend(p);
2149 sigqueue_delete(sigqueue, sig);
2152 if (action == SIG_CATCH) {
2154 * The process wants to catch it so it needs
2155 * to run at least one thread, but which one?
2161 * The signal is not ignored or caught.
2163 thread_unsuspend(p);
2168 if (prop & SA_STOP) {
2170 * If traced process is already stopped,
2171 * then no further action is necessary.
2173 if (p->p_flag & P_TRACED)
2176 * Already stopped, don't need to stop again
2177 * (If we did the shell could get confused).
2178 * Just make sure the signal STOP bit set.
2180 p->p_flag |= P_STOPPED_SIG;
2181 sigqueue_delete(sigqueue, sig);
2186 * All other kinds of signals:
2187 * If a thread is sleeping interruptibly, simulate a
2188 * wakeup so that when it is continued it will be made
2189 * runnable and can look at the signal. However, don't make
2190 * the PROCESS runnable, leave it stopped.
2191 * It may run a bit until it hits a thread_suspend_check().
2196 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2197 wakeup_swapper = sleepq_abort(td, intrval);
2204 * Mutexes are short lived. Threads waiting on them will
2205 * hit thread_suspend_check() soon.
2207 } else if (p->p_state == PRS_NORMAL) {
2208 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2209 tdsigwakeup(td, sig, action, intrval);
2213 MPASS(action == SIG_DFL);
2215 if (prop & SA_STOP) {
2216 if (p->p_flag & P_PPWAIT)
2218 p->p_flag |= P_STOPPED_SIG;
2221 sig_suspend_threads(td, p, 1);
2222 if (p->p_numthreads == p->p_suspcount) {
2224 * only thread sending signal to another
2225 * process can reach here, if thread is sending
2226 * signal to its process, because thread does
2227 * not suspend itself here, p_numthreads
2228 * should never be equal to p_suspcount.
2232 sigqueue_delete_proc(p, p->p_xstat);
2238 /* Not in "NORMAL" state. discard the signal. */
2239 sigqueue_delete(sigqueue, sig);
2244 * The process is not stopped so we need to apply the signal to all the
2248 tdsigwakeup(td, sig, action, intrval);
2250 thread_unsuspend(p);
2253 /* If we jump here, proc slock should not be owned. */
2254 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2259 * The force of a signal has been directed against a single
2260 * thread. We need to see what we can do about knocking it
2261 * out of any sleep it may be in etc.
2264 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2266 struct proc *p = td->td_proc;
2271 PROC_LOCK_ASSERT(p, MA_OWNED);
2272 prop = sigprop(sig);
2277 * Bring the priority of a thread up if we want it to get
2278 * killed in this lifetime.
2280 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2281 sched_prio(td, PUSER);
2282 if (TD_ON_SLEEPQ(td)) {
2284 * If thread is sleeping uninterruptibly
2285 * we can't interrupt the sleep... the signal will
2286 * be noticed when the process returns through
2287 * trap() or syscall().
2289 if ((td->td_flags & TDF_SINTR) == 0)
2292 * If SIGCONT is default (or ignored) and process is
2293 * asleep, we are finished; the process should not
2296 if ((prop & SA_CONT) && action == SIG_DFL) {
2299 sigqueue_delete(&p->p_sigqueue, sig);
2301 * It may be on either list in this state.
2302 * Remove from both for now.
2304 sigqueue_delete(&td->td_sigqueue, sig);
2309 * Give low priority threads a better chance to run.
2311 if (td->td_priority > PUSER)
2312 sched_prio(td, PUSER);
2314 wakeup_swapper = sleepq_abort(td, intrval);
2317 * Other states do nothing with the signal immediately,
2318 * other than kicking ourselves if we are running.
2319 * It will either never be noticed, or noticed very soon.
2322 if (TD_IS_RUNNING(td) && td != curthread)
2334 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2339 PROC_LOCK_ASSERT(p, MA_OWNED);
2340 PROC_SLOCK_ASSERT(p, MA_OWNED);
2343 FOREACH_THREAD_IN_PROC(p, td2) {
2345 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2346 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2347 (td2->td_flags & TDF_SINTR)) {
2348 if (td2->td_flags & TDF_SBDRY) {
2349 if (TD_IS_SUSPENDED(td2))
2351 thread_unsuspend_one(td2);
2352 if (TD_ON_SLEEPQ(td2))
2354 sleepq_abort(td2, ERESTART);
2355 } else if (!TD_IS_SUSPENDED(td2)) {
2356 thread_suspend_one(td2);
2358 } else if (!TD_IS_SUSPENDED(td2)) {
2359 if (sending || td != td2)
2360 td2->td_flags |= TDF_ASTPENDING;
2362 if (TD_IS_RUNNING(td2) && td2 != td)
2363 forward_signal(td2);
2373 ptracestop(struct thread *td, int sig)
2375 struct proc *p = td->td_proc;
2377 PROC_LOCK_ASSERT(p, MA_OWNED);
2378 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2379 &p->p_mtx.lock_object, "Stopping for traced signal");
2381 td->td_dbgflags |= TDB_XSIG;
2384 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2385 if (p->p_flag & P_SINGLE_EXIT) {
2386 td->td_dbgflags &= ~TDB_XSIG;
2391 * Just make wait() to work, the last stopped thread
2396 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2397 sig_suspend_threads(td, p, 0);
2398 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2399 td->td_dbgflags &= ~TDB_STOPATFORK;
2400 cv_broadcast(&p->p_dbgwait);
2403 thread_suspend_switch(td);
2404 if (!(p->p_flag & P_TRACED)) {
2407 if (td->td_dbgflags & TDB_SUSPEND) {
2408 if (p->p_flag & P_SINGLE_EXIT)
2414 return (td->td_xsig);
2418 reschedule_signals(struct proc *p, sigset_t block, int flags)
2424 PROC_LOCK_ASSERT(p, MA_OWNED);
2425 if (SIGISEMPTY(p->p_siglist))
2428 SIGSETAND(block, p->p_siglist);
2429 while ((sig = sig_ffs(&block)) != 0) {
2430 SIGDELSET(block, sig);
2431 td = sigtd(p, sig, 0);
2433 if (!(flags & SIGPROCMASK_PS_LOCKED))
2434 mtx_lock(&ps->ps_mtx);
2435 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2436 tdsigwakeup(td, sig, SIG_CATCH,
2437 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2439 if (!(flags & SIGPROCMASK_PS_LOCKED))
2440 mtx_unlock(&ps->ps_mtx);
2445 tdsigcleanup(struct thread *td)
2451 PROC_LOCK_ASSERT(p, MA_OWNED);
2453 sigqueue_flush(&td->td_sigqueue);
2454 if (p->p_numthreads == 1)
2458 * Since we cannot handle signals, notify signal post code
2459 * about this by filling the sigmask.
2461 * Also, if needed, wake up thread(s) that do not block the
2462 * same signals as the exiting thread, since the thread might
2463 * have been selected for delivery and woken up.
2465 SIGFILLSET(unblocked);
2466 SIGSETNAND(unblocked, td->td_sigmask);
2467 SIGFILLSET(td->td_sigmask);
2468 reschedule_signals(p, unblocked, 0);
2473 * If the current process has received a signal (should be caught or cause
2474 * termination, should interrupt current syscall), return the signal number.
2475 * Stop signals with default action are processed immediately, then cleared;
2476 * they aren't returned. This is checked after each entry to the system for
2477 * a syscall or trap (though this can usually be done without calling issignal
2478 * by checking the pending signal masks in cursig.) The normal call
2481 * while (sig = cursig(curthread))
2485 issignal(struct thread *td, int stop_allowed)
2489 struct sigqueue *queue;
2490 sigset_t sigpending;
2491 int sig, prop, newsig;
2495 mtx_assert(&ps->ps_mtx, MA_OWNED);
2496 PROC_LOCK_ASSERT(p, MA_OWNED);
2498 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2500 sigpending = td->td_sigqueue.sq_signals;
2501 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2502 SIGSETNAND(sigpending, td->td_sigmask);
2504 if (p->p_flag & P_PPWAIT)
2505 SIG_STOPSIGMASK(sigpending);
2506 if (SIGISEMPTY(sigpending)) /* no signal to send */
2508 sig = sig_ffs(&sigpending);
2510 if (p->p_stops & S_SIG) {
2511 mtx_unlock(&ps->ps_mtx);
2512 stopevent(p, S_SIG, sig);
2513 mtx_lock(&ps->ps_mtx);
2517 * We should see pending but ignored signals
2518 * only if P_TRACED was on when they were posted.
2520 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2521 sigqueue_delete(&td->td_sigqueue, sig);
2522 sigqueue_delete(&p->p_sigqueue, sig);
2525 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2527 * If traced, always stop.
2528 * Remove old signal from queue before the stop.
2529 * XXX shrug off debugger, it causes siginfo to
2532 queue = &td->td_sigqueue;
2533 td->td_dbgksi.ksi_signo = 0;
2534 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2535 queue = &p->p_sigqueue;
2536 sigqueue_get(queue, sig, &td->td_dbgksi);
2539 mtx_unlock(&ps->ps_mtx);
2540 newsig = ptracestop(td, sig);
2541 mtx_lock(&ps->ps_mtx);
2543 if (sig != newsig) {
2546 * If parent wants us to take the signal,
2547 * then it will leave it in p->p_xstat;
2548 * otherwise we just look for signals again.
2555 * Put the new signal into td_sigqueue. If the
2556 * signal is being masked, look for other signals.
2558 sigqueue_add(queue, sig, NULL);
2559 if (SIGISMEMBER(td->td_sigmask, sig))
2563 if (td->td_dbgksi.ksi_signo != 0) {
2564 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2565 if (sigqueue_add(&td->td_sigqueue, sig,
2566 &td->td_dbgksi) != 0)
2567 td->td_dbgksi.ksi_signo = 0;
2569 if (td->td_dbgksi.ksi_signo == 0)
2570 sigqueue_add(&td->td_sigqueue, sig,
2575 * If the traced bit got turned off, go back up
2576 * to the top to rescan signals. This ensures
2577 * that p_sig* and p_sigact are consistent.
2579 if ((p->p_flag & P_TRACED) == 0)
2583 prop = sigprop(sig);
2586 * Decide whether the signal should be returned.
2587 * Return the signal's number, or fall through
2588 * to clear it from the pending mask.
2590 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2592 case (intptr_t)SIG_DFL:
2594 * Don't take default actions on system processes.
2596 if (p->p_pid <= 1) {
2599 * Are you sure you want to ignore SIGSEGV
2602 printf("Process (pid %lu) got signal %d\n",
2603 (u_long)p->p_pid, sig);
2605 break; /* == ignore */
2608 * If there is a pending stop signal to process
2609 * with default action, stop here,
2610 * then clear the signal. However,
2611 * if process is member of an orphaned
2612 * process group, ignore tty stop signals.
2614 if (prop & SA_STOP) {
2615 if (p->p_flag & P_TRACED ||
2616 (p->p_pgrp->pg_jobc == 0 &&
2618 break; /* == ignore */
2620 /* Ignore, but do not drop the stop signal. */
2621 if (stop_allowed != SIG_STOP_ALLOWED)
2623 mtx_unlock(&ps->ps_mtx);
2624 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2625 &p->p_mtx.lock_object, "Catching SIGSTOP");
2626 p->p_flag |= P_STOPPED_SIG;
2629 sig_suspend_threads(td, p, 0);
2630 thread_suspend_switch(td);
2632 mtx_lock(&ps->ps_mtx);
2634 } else if (prop & SA_IGNORE) {
2636 * Except for SIGCONT, shouldn't get here.
2637 * Default action is to ignore; drop it.
2639 break; /* == ignore */
2644 case (intptr_t)SIG_IGN:
2646 * Masking above should prevent us ever trying
2647 * to take action on an ignored signal other
2648 * than SIGCONT, unless process is traced.
2650 if ((prop & SA_CONT) == 0 &&
2651 (p->p_flag & P_TRACED) == 0)
2652 printf("issignal\n");
2653 break; /* == ignore */
2657 * This signal has an action, let
2658 * postsig() process it.
2662 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2663 sigqueue_delete(&p->p_sigqueue, sig);
2669 thread_stopped(struct proc *p)
2673 PROC_LOCK_ASSERT(p, MA_OWNED);
2674 PROC_SLOCK_ASSERT(p, MA_OWNED);
2678 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2680 p->p_flag &= ~P_WAITED;
2681 PROC_LOCK(p->p_pptr);
2682 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2683 CLD_TRAPPED : CLD_STOPPED);
2684 PROC_UNLOCK(p->p_pptr);
2690 * Take the action for the specified signal
2691 * from the current set of pending signals.
2697 struct thread *td = curthread;
2698 register struct proc *p = td->td_proc;
2702 sigset_t returnmask, mask;
2704 KASSERT(sig != 0, ("postsig"));
2706 PROC_LOCK_ASSERT(p, MA_OWNED);
2708 mtx_assert(&ps->ps_mtx, MA_OWNED);
2709 ksiginfo_init(&ksi);
2710 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2711 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2713 ksi.ksi_signo = sig;
2714 if (ksi.ksi_code == SI_TIMER)
2715 itimer_accept(p, ksi.ksi_timerid, &ksi);
2716 action = ps->ps_sigact[_SIG_IDX(sig)];
2718 if (KTRPOINT(td, KTR_PSIG))
2719 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2720 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2722 if (p->p_stops & S_SIG) {
2723 mtx_unlock(&ps->ps_mtx);
2724 stopevent(p, S_SIG, sig);
2725 mtx_lock(&ps->ps_mtx);
2728 if (action == SIG_DFL) {
2730 * Default action, where the default is to kill
2731 * the process. (Other cases were ignored above.)
2733 mtx_unlock(&ps->ps_mtx);
2738 * If we get here, the signal must be caught.
2740 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2741 ("postsig action"));
2743 * Set the new mask value and also defer further
2744 * occurrences of this signal.
2746 * Special case: user has done a sigsuspend. Here the
2747 * current mask is not of interest, but rather the
2748 * mask from before the sigsuspend is what we want
2749 * restored after the signal processing is completed.
2751 if (td->td_pflags & TDP_OLDMASK) {
2752 returnmask = td->td_oldsigmask;
2753 td->td_pflags &= ~TDP_OLDMASK;
2755 returnmask = td->td_sigmask;
2757 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2758 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2759 SIGADDSET(mask, sig);
2760 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2761 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2763 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2765 * See kern_sigaction() for origin of this code.
2767 SIGDELSET(ps->ps_sigcatch, sig);
2768 if (sig != SIGCONT &&
2769 sigprop(sig) & SA_IGNORE)
2770 SIGADDSET(ps->ps_sigignore, sig);
2771 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2773 td->td_ru.ru_nsignals++;
2774 if (p->p_sig == sig) {
2778 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2784 * Kill the current process for stated reason.
2792 PROC_LOCK_ASSERT(p, MA_OWNED);
2793 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2794 p, p->p_pid, p->p_comm);
2795 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2796 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2797 p->p_flag |= P_WKILLED;
2798 psignal(p, SIGKILL);
2802 * Force the current process to exit with the specified signal, dumping core
2803 * if appropriate. We bypass the normal tests for masked and caught signals,
2804 * allowing unrecoverable failures to terminate the process without changing
2805 * signal state. Mark the accounting record with the signal termination.
2806 * If dumping core, save the signal number for the debugger. Calls exit and
2814 struct proc *p = td->td_proc;
2816 PROC_LOCK_ASSERT(p, MA_OWNED);
2817 p->p_acflag |= AXSIG;
2819 * We must be single-threading to generate a core dump. This
2820 * ensures that the registers in the core file are up-to-date.
2821 * Also, the ELF dump handler assumes that the thread list doesn't
2822 * change out from under it.
2824 * XXX If another thread attempts to single-thread before us
2825 * (e.g. via fork()), we won't get a dump at all.
2827 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2830 * Log signals which would cause core dumps
2831 * (Log as LOG_INFO to appease those who don't want
2833 * XXX : Todo, as well as euid, write out ruid too
2834 * Note that coredump() drops proc lock.
2836 if (coredump(td) == 0)
2838 if (kern_logsigexit)
2840 "pid %d (%s), uid %d: exited on signal %d%s\n",
2841 p->p_pid, p->p_comm,
2842 td->td_ucred ? td->td_ucred->cr_uid : -1,
2844 sig & WCOREFLAG ? " (core dumped)" : "");
2847 exit1(td, W_EXITCODE(0, sig));
2852 * Send queued SIGCHLD to parent when child process's state
2856 sigparent(struct proc *p, int reason, int status)
2858 PROC_LOCK_ASSERT(p, MA_OWNED);
2859 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2861 if (p->p_ksi != NULL) {
2862 p->p_ksi->ksi_signo = SIGCHLD;
2863 p->p_ksi->ksi_code = reason;
2864 p->p_ksi->ksi_status = status;
2865 p->p_ksi->ksi_pid = p->p_pid;
2866 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2867 if (KSI_ONQ(p->p_ksi))
2870 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2874 childproc_jobstate(struct proc *p, int reason, int status)
2878 PROC_LOCK_ASSERT(p, MA_OWNED);
2879 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2882 * Wake up parent sleeping in kern_wait(), also send
2883 * SIGCHLD to parent, but SIGCHLD does not guarantee
2884 * that parent will awake, because parent may masked
2887 p->p_pptr->p_flag |= P_STATCHILD;
2890 ps = p->p_pptr->p_sigacts;
2891 mtx_lock(&ps->ps_mtx);
2892 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2893 mtx_unlock(&ps->ps_mtx);
2894 sigparent(p, reason, status);
2896 mtx_unlock(&ps->ps_mtx);
2900 childproc_stopped(struct proc *p, int reason)
2902 childproc_jobstate(p, reason, p->p_xstat);
2906 childproc_continued(struct proc *p)
2908 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2912 childproc_exited(struct proc *p)
2915 int status = p->p_xstat; /* convert to int */
2917 reason = CLD_EXITED;
2918 if (WCOREDUMP(status))
2919 reason = CLD_DUMPED;
2920 else if (WIFSIGNALED(status))
2921 reason = CLD_KILLED;
2923 * XXX avoid calling wakeup(p->p_pptr), the work is
2926 sigparent(p, reason, status);
2930 * We only have 1 character for the core count in the format
2931 * string, so the range will be 0-9
2933 #define MAX_NUM_CORES 10
2934 static int num_cores = 5;
2937 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
2942 new_val = num_cores;
2943 error = sysctl_handle_int(oidp, &new_val, 0, req);
2944 if (error != 0 || req->newptr == NULL)
2946 if (new_val > MAX_NUM_CORES)
2947 new_val = MAX_NUM_CORES;
2950 num_cores = new_val;
2953 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
2954 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
2956 #if defined(COMPRESS_USER_CORES)
2957 int compress_user_cores = 1;
2958 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
2959 &compress_user_cores, 0, "");
2961 int compress_user_cores_gzlevel = -1; /* default level */
2962 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
2963 &compress_user_cores_gzlevel, -1, "user core gz compression level");
2965 #define GZ_SUFFIX ".gz"
2966 #define GZ_SUFFIX_LEN 3
2969 static char corefilename[MAXPATHLEN] = {"%N.core"};
2970 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2971 sizeof(corefilename), "process corefile name format string");
2974 * expand_name(name, uid, pid, td, compress)
2975 * Expand the name described in corefilename, using name, uid, and pid.
2976 * corefilename is a printf-like string, with three format specifiers:
2977 * %N name of process ("name")
2978 * %P process id (pid)
2980 * For example, "%N.core" is the default; they can be disabled completely
2981 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2982 * This is controlled by the sysctl variable kern.corefile (see above).
2985 expand_name(const char *name, uid_t uid, pid_t pid, struct thread *td,
2996 format = corefilename;
2997 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
3001 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
3002 for (i = 0; format[i]; i++) {
3003 switch (format[i]) {
3004 case '%': /* Format character */
3006 switch (format[i]) {
3008 sbuf_putc(&sb, '%');
3010 case 'H': /* hostname */
3011 if (hostname == NULL) {
3012 hostname = malloc(MAXHOSTNAMELEN,
3014 if (hostname == NULL) {
3016 "pid %ld (%s), uid (%lu): "
3017 "unable to alloc memory "
3018 "for corefile hostname\n",
3024 getcredhostname(td->td_ucred, hostname,
3026 sbuf_printf(&sb, "%s", hostname);
3028 case 'I': /* autoincrementing index */
3029 sbuf_printf(&sb, "0");
3030 indexpos = sbuf_len(&sb) - 1;
3032 case 'N': /* process name */
3033 sbuf_printf(&sb, "%s", name);
3035 case 'P': /* process id */
3036 sbuf_printf(&sb, "%u", pid);
3038 case 'U': /* user id */
3039 sbuf_printf(&sb, "%u", uid);
3043 "Unknown format character %c in "
3044 "corename `%s'\n", format[i], format);
3048 sbuf_putc(&sb, format[i]);
3051 free(hostname, M_TEMP);
3052 #ifdef COMPRESS_USER_CORES
3054 sbuf_printf(&sb, GZ_SUFFIX);
3057 if (sbuf_error(&sb) != 0) {
3058 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3059 "long\n", (long)pid, name, (u_long)uid);
3069 * If the core format has a %I in it, then we need to check
3070 * for existing corefiles before returning a name.
3071 * To do this we iterate over 0..num_cores to find a
3072 * non-existing core file name to use.
3074 if (indexpos != -1) {
3075 struct nameidata nd;
3077 int flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3078 int cmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
3081 for (n = 0; n < num_cores; n++) {
3082 temp[indexpos] = '0' + n;
3083 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE,
3085 error = vn_open(&nd, &flags, cmode, NULL);
3087 if (error == EEXIST) {
3091 "pid %d (%s), uid (%u): Path `%s' failed "
3092 "on initial open test, error = %d\n",
3093 pid, name, uid, temp, error);
3097 vfslocked = NDHASGIANT(&nd);
3098 NDFREE(&nd, NDF_ONLY_PNBUF);
3099 VOP_UNLOCK(nd.ni_vp, 0);
3100 error = vn_close(nd.ni_vp, FWRITE, td->td_ucred, td);
3101 VFS_UNLOCK_GIANT(vfslocked);
3104 "pid %d (%s), uid (%u): Path `%s' failed "
3105 "on close after initial open test, "
3107 pid, name, uid, temp, error);
3118 * Dump a process' core. The main routine does some
3119 * policy checking, and creates the name of the coredump;
3120 * then it passes on a vnode and a size limit to the process-specific
3121 * coredump routine if there is one; if there _is not_ one, it returns
3122 * ENOSYS; otherwise it returns the error from the process-specific routine.
3126 coredump(struct thread *td)
3128 struct proc *p = td->td_proc;
3129 register struct vnode *vp;
3130 register struct ucred *cred = td->td_ucred;
3132 struct nameidata nd;
3134 int error, error1, flags, locked;
3136 char *name; /* name of corefile */
3141 #ifdef COMPRESS_USER_CORES
3142 compress = compress_user_cores;
3146 PROC_LOCK_ASSERT(p, MA_OWNED);
3147 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3148 _STOPEVENT(p, S_CORE, 0);
3150 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid, td,
3155 audit_proc_coredump(td, NULL, EINVAL);
3159 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3162 audit_proc_coredump(td, name, EFAULT);
3169 * Note that the bulk of limit checking is done after
3170 * the corefile is created. The exception is if the limit
3171 * for corefiles is 0, in which case we don't bother
3172 * creating the corefile at all. This layout means that
3173 * a corefile is truncated instead of not being created,
3174 * if it is larger than the limit.
3176 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3177 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3180 audit_proc_coredump(td, name, EFBIG);
3188 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3189 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3190 error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
3194 audit_proc_coredump(td, name, error);
3199 vfslocked = NDHASGIANT(&nd);
3200 NDFREE(&nd, NDF_ONLY_PNBUF);
3203 /* Don't dump to non-regular files or files with links. */
3204 if (vp->v_type != VREG ||
3205 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
3212 lf.l_whence = SEEK_SET;
3215 lf.l_type = F_WRLCK;
3216 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3218 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3219 lf.l_type = F_UNLCK;
3221 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3222 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3224 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3226 VFS_UNLOCK_GIANT(vfslocked);
3232 if (set_core_nodump_flag)
3233 vattr.va_flags = UF_NODUMP;
3234 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3235 VOP_SETATTR(vp, &vattr, cred);
3237 vn_finished_write(mp);
3239 p->p_acflag |= ACORE;
3242 error = p->p_sysent->sv_coredump ?
3243 p->p_sysent->sv_coredump(td, vp, limit, compress ? IMGACT_CORE_COMPRESS : 0) :
3247 lf.l_type = F_UNLCK;
3248 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3251 error1 = vn_close(vp, FWRITE, cred, td);
3256 audit_proc_coredump(td, name, error);
3259 VFS_UNLOCK_GIANT(vfslocked);
3264 * Nonexistent system call-- signal process (may want to handle it). Flag
3265 * error in case process won't see signal immediately (blocked or ignored).
3267 #ifndef _SYS_SYSPROTO_H_
3276 struct nosys_args *args;
3278 struct proc *p = td->td_proc;
3287 * Send a SIGIO or SIGURG signal to a process or process group using stored
3288 * credentials rather than those of the current process.
3291 pgsigio(sigiop, sig, checkctty)
3292 struct sigio **sigiop;
3296 struct sigio *sigio;
3298 ksiginfo_init(&ksi);
3299 ksi.ksi_signo = sig;
3300 ksi.ksi_code = SI_KERNEL;
3304 if (sigio == NULL) {
3308 if (sigio->sio_pgid > 0) {
3309 PROC_LOCK(sigio->sio_proc);
3310 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3311 psignal(sigio->sio_proc, sig);
3312 PROC_UNLOCK(sigio->sio_proc);
3313 } else if (sigio->sio_pgid < 0) {
3316 PGRP_LOCK(sigio->sio_pgrp);
3317 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3319 if (p->p_state == PRS_NORMAL &&
3320 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3321 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3325 PGRP_UNLOCK(sigio->sio_pgrp);
3331 filt_sigattach(struct knote *kn)
3333 struct proc *p = curproc;
3335 kn->kn_ptr.p_proc = p;
3336 kn->kn_flags |= EV_CLEAR; /* automatically set */
3338 knlist_add(&p->p_klist, kn, 0);
3344 filt_sigdetach(struct knote *kn)
3346 struct proc *p = kn->kn_ptr.p_proc;
3348 knlist_remove(&p->p_klist, kn, 0);
3352 * signal knotes are shared with proc knotes, so we apply a mask to
3353 * the hint in order to differentiate them from process hints. This
3354 * could be avoided by using a signal-specific knote list, but probably
3355 * isn't worth the trouble.
3358 filt_signal(struct knote *kn, long hint)
3361 if (hint & NOTE_SIGNAL) {
3362 hint &= ~NOTE_SIGNAL;
3364 if (kn->kn_id == hint)
3367 return (kn->kn_data != 0);
3375 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3377 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3382 sigacts_free(struct sigacts *ps)
3385 mtx_lock(&ps->ps_mtx);
3387 if (ps->ps_refcnt == 0) {
3388 mtx_destroy(&ps->ps_mtx);
3389 free(ps, M_SUBPROC);
3391 mtx_unlock(&ps->ps_mtx);
3395 sigacts_hold(struct sigacts *ps)
3397 mtx_lock(&ps->ps_mtx);
3399 mtx_unlock(&ps->ps_mtx);
3404 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3407 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3408 mtx_lock(&src->ps_mtx);
3409 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3410 mtx_unlock(&src->ps_mtx);
3414 sigacts_shared(struct sigacts *ps)
3418 mtx_lock(&ps->ps_mtx);
3419 shared = ps->ps_refcnt > 1;
3420 mtx_unlock(&ps->ps_mtx);