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_DEFINE3(proc, kernel, , signal_send, signal-send, "struct thread *",
97 "struct proc *", "int");
98 SDT_PROBE_DEFINE2(proc, kernel, , signal_clear, signal-clear, "int",
100 SDT_PROBE_DEFINE3(proc, kernel, , signal_discard, signal-discard,
101 "struct thread *", "struct proc *", "int");
103 static int coredump(struct thread *);
104 static int killpg1(struct thread *td, int sig, int pgid, int all,
106 static int issignal(struct thread *td);
107 static int sigprop(int sig);
108 static void tdsigwakeup(struct thread *, int, sig_t, int);
109 static void sig_suspend_threads(struct thread *, struct proc *, int);
110 static int filt_sigattach(struct knote *kn);
111 static void filt_sigdetach(struct knote *kn);
112 static int filt_signal(struct knote *kn, long hint);
113 static struct thread *sigtd(struct proc *p, int sig, int prop);
114 static void sigqueue_start(void);
116 static uma_zone_t ksiginfo_zone = NULL;
117 struct filterops sig_filtops = {
119 .f_attach = filt_sigattach,
120 .f_detach = filt_sigdetach,
121 .f_event = filt_signal,
124 static int kern_logsigexit = 1;
125 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
127 "Log processes quitting on abnormal signals to syslog(3)");
129 static int kern_forcesigexit = 1;
130 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
131 &kern_forcesigexit, 0, "Force trap signal to be handled");
133 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
134 "POSIX real time signal");
136 static int max_pending_per_proc = 128;
137 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
138 &max_pending_per_proc, 0, "Max pending signals per proc");
140 static int preallocate_siginfo = 1024;
141 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
143 &preallocate_siginfo, 0, "Preallocated signal memory size");
145 static int signal_overflow = 0;
146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
147 &signal_overflow, 0, "Number of signals overflew");
149 static int signal_alloc_fail = 0;
150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
151 &signal_alloc_fail, 0, "signals failed to be allocated");
153 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
156 * Policy -- Can ucred cr1 send SIGIO to process cr2?
157 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
158 * in the right situations.
160 #define CANSIGIO(cr1, cr2) \
161 ((cr1)->cr_uid == 0 || \
162 (cr1)->cr_ruid == (cr2)->cr_ruid || \
163 (cr1)->cr_uid == (cr2)->cr_ruid || \
164 (cr1)->cr_ruid == (cr2)->cr_uid || \
165 (cr1)->cr_uid == (cr2)->cr_uid)
167 static int sugid_coredump;
168 TUNABLE_INT("kern.sugid_coredump", &sugid_coredump);
169 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
170 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
172 static int capmode_coredump;
173 TUNABLE_INT("kern.capmode_coredump", &capmode_coredump);
174 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RW,
175 &capmode_coredump, 0, "Allow processes in capability mode 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 */
198 static int sigproptbl[NSIG] = {
199 SA_KILL, /* SIGHUP */
200 SA_KILL, /* SIGINT */
201 SA_KILL|SA_CORE, /* SIGQUIT */
202 SA_KILL|SA_CORE, /* SIGILL */
203 SA_KILL|SA_CORE, /* SIGTRAP */
204 SA_KILL|SA_CORE, /* SIGABRT */
205 SA_KILL|SA_CORE, /* SIGEMT */
206 SA_KILL|SA_CORE, /* SIGFPE */
207 SA_KILL, /* SIGKILL */
208 SA_KILL|SA_CORE, /* SIGBUS */
209 SA_KILL|SA_CORE, /* SIGSEGV */
210 SA_KILL|SA_CORE, /* SIGSYS */
211 SA_KILL, /* SIGPIPE */
212 SA_KILL, /* SIGALRM */
213 SA_KILL, /* SIGTERM */
214 SA_IGNORE, /* SIGURG */
215 SA_STOP, /* SIGSTOP */
216 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
217 SA_IGNORE|SA_CONT, /* SIGCONT */
218 SA_IGNORE, /* SIGCHLD */
219 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
220 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
221 SA_IGNORE, /* SIGIO */
222 SA_KILL, /* SIGXCPU */
223 SA_KILL, /* SIGXFSZ */
224 SA_KILL, /* SIGVTALRM */
225 SA_KILL, /* SIGPROF */
226 SA_IGNORE, /* SIGWINCH */
227 SA_IGNORE, /* SIGINFO */
228 SA_KILL, /* SIGUSR1 */
229 SA_KILL, /* SIGUSR2 */
232 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
237 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
238 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
239 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
240 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
241 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
242 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
246 ksiginfo_alloc(int wait)
253 if (ksiginfo_zone != NULL)
254 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
259 ksiginfo_free(ksiginfo_t *ksi)
261 uma_zfree(ksiginfo_zone, ksi);
265 ksiginfo_tryfree(ksiginfo_t *ksi)
267 if (!(ksi->ksi_flags & KSI_EXT)) {
268 uma_zfree(ksiginfo_zone, ksi);
275 sigqueue_init(sigqueue_t *list, struct proc *p)
277 SIGEMPTYSET(list->sq_signals);
278 SIGEMPTYSET(list->sq_kill);
279 TAILQ_INIT(&list->sq_list);
281 list->sq_flags = SQ_INIT;
285 * Get a signal's ksiginfo.
287 * 0 - signal not found
288 * others - signal number
291 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
293 struct proc *p = sq->sq_proc;
294 struct ksiginfo *ksi, *next;
297 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
299 if (!SIGISMEMBER(sq->sq_signals, signo))
302 if (SIGISMEMBER(sq->sq_kill, signo)) {
304 SIGDELSET(sq->sq_kill, signo);
307 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
308 if (ksi->ksi_signo == signo) {
310 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
311 ksi->ksi_sigq = NULL;
312 ksiginfo_copy(ksi, si);
313 if (ksiginfo_tryfree(ksi) && p != NULL)
322 SIGDELSET(sq->sq_signals, signo);
323 si->ksi_signo = signo;
328 sigqueue_take(ksiginfo_t *ksi)
334 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
338 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
339 ksi->ksi_sigq = NULL;
340 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
343 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
344 kp = TAILQ_NEXT(kp, ksi_link)) {
345 if (kp->ksi_signo == ksi->ksi_signo)
348 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
349 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
353 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
355 struct proc *p = sq->sq_proc;
356 struct ksiginfo *ksi;
359 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
361 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
362 SIGADDSET(sq->sq_kill, signo);
366 /* directly insert the ksi, don't copy it */
367 if (si->ksi_flags & KSI_INS) {
368 if (si->ksi_flags & KSI_HEAD)
369 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
371 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
376 if (__predict_false(ksiginfo_zone == NULL)) {
377 SIGADDSET(sq->sq_kill, signo);
381 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
384 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
390 ksiginfo_copy(si, ksi);
391 ksi->ksi_signo = signo;
392 if (si->ksi_flags & KSI_HEAD)
393 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
395 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
399 if ((si->ksi_flags & KSI_TRAP) != 0 ||
400 (si->ksi_flags & KSI_SIGQ) == 0) {
402 SIGADDSET(sq->sq_kill, signo);
411 SIGADDSET(sq->sq_signals, signo);
416 sigqueue_flush(sigqueue_t *sq)
418 struct proc *p = sq->sq_proc;
421 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
424 PROC_LOCK_ASSERT(p, MA_OWNED);
426 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
427 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
428 ksi->ksi_sigq = NULL;
429 if (ksiginfo_tryfree(ksi) && p != NULL)
433 SIGEMPTYSET(sq->sq_signals);
434 SIGEMPTYSET(sq->sq_kill);
438 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
441 struct proc *p1, *p2;
442 ksiginfo_t *ksi, *next;
444 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
445 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
448 /* Move siginfo to target list */
449 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
450 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
451 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
454 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
461 /* Move pending bits to target list */
463 SIGSETAND(tmp, *set);
464 SIGSETOR(dst->sq_kill, tmp);
465 SIGSETNAND(src->sq_kill, tmp);
467 tmp = src->sq_signals;
468 SIGSETAND(tmp, *set);
469 SIGSETOR(dst->sq_signals, tmp);
470 SIGSETNAND(src->sq_signals, tmp);
475 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
480 SIGADDSET(set, signo);
481 sigqueue_move_set(src, dst, &set);
486 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
488 struct proc *p = sq->sq_proc;
489 ksiginfo_t *ksi, *next;
491 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
493 /* Remove siginfo queue */
494 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
495 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
496 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
497 ksi->ksi_sigq = NULL;
498 if (ksiginfo_tryfree(ksi) && p != NULL)
502 SIGSETNAND(sq->sq_kill, *set);
503 SIGSETNAND(sq->sq_signals, *set);
507 sigqueue_delete(sigqueue_t *sq, int signo)
512 SIGADDSET(set, signo);
513 sigqueue_delete_set(sq, &set);
516 /* Remove a set of signals for a process */
518 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
523 PROC_LOCK_ASSERT(p, MA_OWNED);
525 sigqueue_init(&worklist, NULL);
526 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
528 FOREACH_THREAD_IN_PROC(p, td0)
529 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
531 sigqueue_flush(&worklist);
535 sigqueue_delete_proc(struct proc *p, int signo)
540 SIGADDSET(set, signo);
541 sigqueue_delete_set_proc(p, &set);
545 sigqueue_delete_stopmask_proc(struct proc *p)
550 SIGADDSET(set, SIGSTOP);
551 SIGADDSET(set, SIGTSTP);
552 SIGADDSET(set, SIGTTIN);
553 SIGADDSET(set, SIGTTOU);
554 sigqueue_delete_set_proc(p, &set);
558 * Determine signal that should be delivered to thread td, the current
559 * thread, 0 if none. If there is a pending stop signal with default
560 * action, the process stops in issignal().
563 cursig(struct thread *td)
565 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
566 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
567 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
568 return (SIGPENDING(td) ? issignal(td) : 0);
572 * Arrange for ast() to handle unmasked pending signals on return to user
573 * mode. This must be called whenever a signal is added to td_sigqueue or
574 * unmasked in td_sigmask.
577 signotify(struct thread *td)
583 PROC_LOCK_ASSERT(p, MA_OWNED);
585 if (SIGPENDING(td)) {
587 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
593 sigonstack(size_t sp)
595 struct thread *td = curthread;
597 return ((td->td_pflags & TDP_ALTSTACK) ?
598 #if defined(COMPAT_43)
599 ((td->td_sigstk.ss_size == 0) ?
600 (td->td_sigstk.ss_flags & SS_ONSTACK) :
601 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
603 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
612 if (sig > 0 && sig < NSIG)
613 return (sigproptbl[_SIG_IDX(sig)]);
618 sig_ffs(sigset_t *set)
622 for (i = 0; i < _SIG_WORDS; i++)
624 return (ffs(set->__bits[i]) + (i * 32));
635 kern_sigaction(td, sig, act, oact, flags)
638 struct sigaction *act, *oact;
642 struct proc *p = td->td_proc;
644 if (!_SIG_VALID(sig))
649 mtx_lock(&ps->ps_mtx);
651 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
653 if (SIGISMEMBER(ps->ps_sigonstack, sig))
654 oact->sa_flags |= SA_ONSTACK;
655 if (!SIGISMEMBER(ps->ps_sigintr, sig))
656 oact->sa_flags |= SA_RESTART;
657 if (SIGISMEMBER(ps->ps_sigreset, sig))
658 oact->sa_flags |= SA_RESETHAND;
659 if (SIGISMEMBER(ps->ps_signodefer, sig))
660 oact->sa_flags |= SA_NODEFER;
661 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
662 oact->sa_flags |= SA_SIGINFO;
664 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
666 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
667 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
668 oact->sa_flags |= SA_NOCLDSTOP;
669 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
670 oact->sa_flags |= SA_NOCLDWAIT;
673 if ((sig == SIGKILL || sig == SIGSTOP) &&
674 act->sa_handler != SIG_DFL) {
675 mtx_unlock(&ps->ps_mtx);
681 * Change setting atomically.
684 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
685 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
686 if (act->sa_flags & SA_SIGINFO) {
687 ps->ps_sigact[_SIG_IDX(sig)] =
688 (__sighandler_t *)act->sa_sigaction;
689 SIGADDSET(ps->ps_siginfo, sig);
691 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
692 SIGDELSET(ps->ps_siginfo, sig);
694 if (!(act->sa_flags & SA_RESTART))
695 SIGADDSET(ps->ps_sigintr, sig);
697 SIGDELSET(ps->ps_sigintr, sig);
698 if (act->sa_flags & SA_ONSTACK)
699 SIGADDSET(ps->ps_sigonstack, sig);
701 SIGDELSET(ps->ps_sigonstack, sig);
702 if (act->sa_flags & SA_RESETHAND)
703 SIGADDSET(ps->ps_sigreset, sig);
705 SIGDELSET(ps->ps_sigreset, sig);
706 if (act->sa_flags & SA_NODEFER)
707 SIGADDSET(ps->ps_signodefer, sig);
709 SIGDELSET(ps->ps_signodefer, sig);
710 if (sig == SIGCHLD) {
711 if (act->sa_flags & SA_NOCLDSTOP)
712 ps->ps_flag |= PS_NOCLDSTOP;
714 ps->ps_flag &= ~PS_NOCLDSTOP;
715 if (act->sa_flags & SA_NOCLDWAIT) {
717 * Paranoia: since SA_NOCLDWAIT is implemented
718 * by reparenting the dying child to PID 1 (and
719 * trust it to reap the zombie), PID 1 itself
720 * is forbidden to set SA_NOCLDWAIT.
723 ps->ps_flag &= ~PS_NOCLDWAIT;
725 ps->ps_flag |= PS_NOCLDWAIT;
727 ps->ps_flag &= ~PS_NOCLDWAIT;
728 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
729 ps->ps_flag |= PS_CLDSIGIGN;
731 ps->ps_flag &= ~PS_CLDSIGIGN;
734 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
735 * and for signals set to SIG_DFL where the default is to
736 * ignore. However, don't put SIGCONT in ps_sigignore, as we
737 * have to restart the process.
739 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
740 (sigprop(sig) & SA_IGNORE &&
741 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
742 /* never to be seen again */
743 sigqueue_delete_proc(p, sig);
745 /* easier in psignal */
746 SIGADDSET(ps->ps_sigignore, sig);
747 SIGDELSET(ps->ps_sigcatch, sig);
749 SIGDELSET(ps->ps_sigignore, sig);
750 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
751 SIGDELSET(ps->ps_sigcatch, sig);
753 SIGADDSET(ps->ps_sigcatch, sig);
755 #ifdef COMPAT_FREEBSD4
756 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
757 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
758 (flags & KSA_FREEBSD4) == 0)
759 SIGDELSET(ps->ps_freebsd4, sig);
761 SIGADDSET(ps->ps_freebsd4, sig);
764 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
765 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
766 (flags & KSA_OSIGSET) == 0)
767 SIGDELSET(ps->ps_osigset, sig);
769 SIGADDSET(ps->ps_osigset, sig);
772 mtx_unlock(&ps->ps_mtx);
777 #ifndef _SYS_SYSPROTO_H_
778 struct sigaction_args {
780 struct sigaction *act;
781 struct sigaction *oact;
785 sys_sigaction(td, uap)
787 register struct sigaction_args *uap;
789 struct sigaction act, oact;
790 register struct sigaction *actp, *oactp;
793 actp = (uap->act != NULL) ? &act : NULL;
794 oactp = (uap->oact != NULL) ? &oact : NULL;
796 error = copyin(uap->act, actp, sizeof(act));
800 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
802 error = copyout(oactp, uap->oact, sizeof(oact));
806 #ifdef COMPAT_FREEBSD4
807 #ifndef _SYS_SYSPROTO_H_
808 struct freebsd4_sigaction_args {
810 struct sigaction *act;
811 struct sigaction *oact;
815 freebsd4_sigaction(td, uap)
817 register struct freebsd4_sigaction_args *uap;
819 struct sigaction act, oact;
820 register struct sigaction *actp, *oactp;
824 actp = (uap->act != NULL) ? &act : NULL;
825 oactp = (uap->oact != NULL) ? &oact : NULL;
827 error = copyin(uap->act, actp, sizeof(act));
831 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
833 error = copyout(oactp, uap->oact, sizeof(oact));
836 #endif /* COMAPT_FREEBSD4 */
838 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
839 #ifndef _SYS_SYSPROTO_H_
840 struct osigaction_args {
842 struct osigaction *nsa;
843 struct osigaction *osa;
849 register struct osigaction_args *uap;
851 struct osigaction sa;
852 struct sigaction nsa, osa;
853 register struct sigaction *nsap, *osap;
856 if (uap->signum <= 0 || uap->signum >= ONSIG)
859 nsap = (uap->nsa != NULL) ? &nsa : NULL;
860 osap = (uap->osa != NULL) ? &osa : NULL;
863 error = copyin(uap->nsa, &sa, sizeof(sa));
866 nsap->sa_handler = sa.sa_handler;
867 nsap->sa_flags = sa.sa_flags;
868 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
870 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
871 if (osap && !error) {
872 sa.sa_handler = osap->sa_handler;
873 sa.sa_flags = osap->sa_flags;
874 SIG2OSIG(osap->sa_mask, sa.sa_mask);
875 error = copyout(&sa, uap->osa, sizeof(sa));
880 #if !defined(__i386__)
881 /* Avoid replicating the same stub everywhere */
885 struct osigreturn_args *uap;
888 return (nosys(td, (struct nosys_args *)uap));
891 #endif /* COMPAT_43 */
894 * Initialize signal state for process 0;
895 * set to ignore signals that are ignored by default.
906 mtx_lock(&ps->ps_mtx);
907 for (i = 1; i <= NSIG; i++)
908 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
909 SIGADDSET(ps->ps_sigignore, i);
910 mtx_unlock(&ps->ps_mtx);
915 * Reset signals for an exec of the specified process.
918 execsigs(struct proc *p)
925 * Reset caught signals. Held signals remain held
926 * through td_sigmask (unless they were caught,
927 * and are now ignored by default).
929 PROC_LOCK_ASSERT(p, MA_OWNED);
930 td = FIRST_THREAD_IN_PROC(p);
932 mtx_lock(&ps->ps_mtx);
933 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
934 sig = sig_ffs(&ps->ps_sigcatch);
935 SIGDELSET(ps->ps_sigcatch, sig);
936 if (sigprop(sig) & SA_IGNORE) {
938 SIGADDSET(ps->ps_sigignore, sig);
939 sigqueue_delete_proc(p, sig);
941 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
944 * Reset stack state to the user stack.
945 * Clear set of signals caught on the signal stack.
947 td->td_sigstk.ss_flags = SS_DISABLE;
948 td->td_sigstk.ss_size = 0;
949 td->td_sigstk.ss_sp = 0;
950 td->td_pflags &= ~TDP_ALTSTACK;
952 * Reset no zombies if child dies flag as Solaris does.
954 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
955 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
956 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
957 mtx_unlock(&ps->ps_mtx);
963 * Manipulate signal mask.
966 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
969 sigset_t new_block, oset1;
974 if (!(flags & SIGPROCMASK_PROC_LOCKED))
977 *oset = td->td_sigmask;
984 oset1 = td->td_sigmask;
985 SIGSETOR(td->td_sigmask, *set);
986 new_block = td->td_sigmask;
987 SIGSETNAND(new_block, oset1);
990 SIGSETNAND(td->td_sigmask, *set);
995 oset1 = td->td_sigmask;
996 if (flags & SIGPROCMASK_OLD)
997 SIGSETLO(td->td_sigmask, *set);
999 td->td_sigmask = *set;
1000 new_block = td->td_sigmask;
1001 SIGSETNAND(new_block, oset1);
1010 * The new_block set contains signals that were not previously
1011 * blocked, but are blocked now.
1013 * In case we block any signal that was not previously blocked
1014 * for td, and process has the signal pending, try to schedule
1015 * signal delivery to some thread that does not block the
1016 * signal, possibly waking it up.
1018 if (p->p_numthreads != 1)
1019 reschedule_signals(p, new_block, flags);
1023 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1028 #ifndef _SYS_SYSPROTO_H_
1029 struct sigprocmask_args {
1031 const sigset_t *set;
1036 sys_sigprocmask(td, uap)
1037 register struct thread *td;
1038 struct sigprocmask_args *uap;
1041 sigset_t *setp, *osetp;
1044 setp = (uap->set != NULL) ? &set : NULL;
1045 osetp = (uap->oset != NULL) ? &oset : NULL;
1047 error = copyin(uap->set, setp, sizeof(set));
1051 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1052 if (osetp && !error) {
1053 error = copyout(osetp, uap->oset, sizeof(oset));
1058 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1059 #ifndef _SYS_SYSPROTO_H_
1060 struct osigprocmask_args {
1066 osigprocmask(td, uap)
1067 register struct thread *td;
1068 struct osigprocmask_args *uap;
1073 OSIG2SIG(uap->mask, set);
1074 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1075 SIG2OSIG(oset, td->td_retval[0]);
1078 #endif /* COMPAT_43 */
1081 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1087 error = copyin(uap->set, &set, sizeof(set));
1089 td->td_retval[0] = error;
1093 error = kern_sigtimedwait(td, set, &ksi, NULL);
1095 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1097 if (error == ERESTART)
1099 td->td_retval[0] = error;
1103 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1104 td->td_retval[0] = error;
1109 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1112 struct timespec *timeout;
1118 error = copyin(uap->timeout, &ts, sizeof(ts));
1126 error = copyin(uap->set, &set, sizeof(set));
1130 error = kern_sigtimedwait(td, set, &ksi, timeout);
1135 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1138 td->td_retval[0] = ksi.ksi_signo;
1143 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1149 error = copyin(uap->set, &set, sizeof(set));
1153 error = kern_sigtimedwait(td, set, &ksi, NULL);
1158 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1161 td->td_retval[0] = ksi.ksi_signo;
1166 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1167 struct timespec *timeout)
1170 sigset_t saved_mask, new_block;
1172 int error, sig, timo, timevalid = 0;
1173 struct timespec rts, ets, ts;
1181 if (timeout != NULL) {
1182 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1184 getnanouptime(&rts);
1186 timespecadd(&ets, timeout);
1190 /* Some signals can not be waited for. */
1191 SIG_CANTMASK(waitset);
1194 saved_mask = td->td_sigmask;
1195 SIGSETNAND(td->td_sigmask, waitset);
1197 mtx_lock(&ps->ps_mtx);
1199 mtx_unlock(&ps->ps_mtx);
1200 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1201 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1202 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1212 * POSIX says this must be checked after looking for pending
1215 if (timeout != NULL) {
1220 getnanouptime(&rts);
1221 if (timespeccmp(&rts, &ets, >=)) {
1226 timespecsub(&ts, &rts);
1227 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1233 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1235 if (timeout != NULL) {
1236 if (error == ERESTART) {
1237 /* Timeout can not be restarted. */
1239 } else if (error == EAGAIN) {
1240 /* We will calculate timeout by ourself. */
1246 new_block = saved_mask;
1247 SIGSETNAND(new_block, td->td_sigmask);
1248 td->td_sigmask = saved_mask;
1250 * Fewer signals can be delivered to us, reschedule signal
1253 if (p->p_numthreads != 1)
1254 reschedule_signals(p, new_block, 0);
1257 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1259 if (ksi->ksi_code == SI_TIMER)
1260 itimer_accept(p, ksi->ksi_timerid, ksi);
1263 if (KTRPOINT(td, KTR_PSIG)) {
1266 mtx_lock(&ps->ps_mtx);
1267 action = ps->ps_sigact[_SIG_IDX(sig)];
1268 mtx_unlock(&ps->ps_mtx);
1269 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1279 #ifndef _SYS_SYSPROTO_H_
1280 struct sigpending_args {
1285 sys_sigpending(td, uap)
1287 struct sigpending_args *uap;
1289 struct proc *p = td->td_proc;
1293 pending = p->p_sigqueue.sq_signals;
1294 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1296 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1299 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1300 #ifndef _SYS_SYSPROTO_H_
1301 struct osigpending_args {
1306 osigpending(td, uap)
1308 struct osigpending_args *uap;
1310 struct proc *p = td->td_proc;
1314 pending = p->p_sigqueue.sq_signals;
1315 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1317 SIG2OSIG(pending, td->td_retval[0]);
1320 #endif /* COMPAT_43 */
1322 #if defined(COMPAT_43)
1324 * Generalized interface signal handler, 4.3-compatible.
1326 #ifndef _SYS_SYSPROTO_H_
1327 struct osigvec_args {
1337 register struct osigvec_args *uap;
1340 struct sigaction nsa, osa;
1341 register struct sigaction *nsap, *osap;
1344 if (uap->signum <= 0 || uap->signum >= ONSIG)
1346 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1347 osap = (uap->osv != NULL) ? &osa : NULL;
1349 error = copyin(uap->nsv, &vec, sizeof(vec));
1352 nsap->sa_handler = vec.sv_handler;
1353 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1354 nsap->sa_flags = vec.sv_flags;
1355 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1357 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1358 if (osap && !error) {
1359 vec.sv_handler = osap->sa_handler;
1360 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1361 vec.sv_flags = osap->sa_flags;
1362 vec.sv_flags &= ~SA_NOCLDWAIT;
1363 vec.sv_flags ^= SA_RESTART;
1364 error = copyout(&vec, uap->osv, sizeof(vec));
1369 #ifndef _SYS_SYSPROTO_H_
1370 struct osigblock_args {
1376 register struct thread *td;
1377 struct osigblock_args *uap;
1381 OSIG2SIG(uap->mask, set);
1382 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1383 SIG2OSIG(oset, td->td_retval[0]);
1387 #ifndef _SYS_SYSPROTO_H_
1388 struct osigsetmask_args {
1393 osigsetmask(td, uap)
1395 struct osigsetmask_args *uap;
1399 OSIG2SIG(uap->mask, set);
1400 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1401 SIG2OSIG(oset, td->td_retval[0]);
1404 #endif /* COMPAT_43 */
1407 * Suspend calling thread until signal, providing mask to be set in the
1410 #ifndef _SYS_SYSPROTO_H_
1411 struct sigsuspend_args {
1412 const sigset_t *sigmask;
1417 sys_sigsuspend(td, uap)
1419 struct sigsuspend_args *uap;
1424 error = copyin(uap->sigmask, &mask, sizeof(mask));
1427 return (kern_sigsuspend(td, mask));
1431 kern_sigsuspend(struct thread *td, sigset_t mask)
1433 struct proc *p = td->td_proc;
1437 * When returning from sigsuspend, we want
1438 * the old mask to be restored after the
1439 * signal handler has finished. Thus, we
1440 * save it here and mark the sigacts structure
1444 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1445 SIGPROCMASK_PROC_LOCKED);
1446 td->td_pflags |= TDP_OLDMASK;
1449 * Process signals now. Otherwise, we can get spurious wakeup
1450 * due to signal entered process queue, but delivered to other
1451 * thread. But sigsuspend should return only on signal
1454 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1455 for (has_sig = 0; !has_sig;) {
1456 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1459 thread_suspend_check(0);
1460 mtx_lock(&p->p_sigacts->ps_mtx);
1461 while ((sig = cursig(td)) != 0)
1462 has_sig += postsig(sig);
1463 mtx_unlock(&p->p_sigacts->ps_mtx);
1466 td->td_errno = EINTR;
1467 td->td_pflags |= TDP_NERRNO;
1468 return (EJUSTRETURN);
1471 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1473 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1474 * convention: libc stub passes mask, not pointer, to save a copyin.
1476 #ifndef _SYS_SYSPROTO_H_
1477 struct osigsuspend_args {
1483 osigsuspend(td, uap)
1485 struct osigsuspend_args *uap;
1489 OSIG2SIG(uap->mask, mask);
1490 return (kern_sigsuspend(td, mask));
1492 #endif /* COMPAT_43 */
1494 #if defined(COMPAT_43)
1495 #ifndef _SYS_SYSPROTO_H_
1496 struct osigstack_args {
1497 struct sigstack *nss;
1498 struct sigstack *oss;
1505 register struct osigstack_args *uap;
1507 struct sigstack nss, oss;
1510 if (uap->nss != NULL) {
1511 error = copyin(uap->nss, &nss, sizeof(nss));
1515 oss.ss_sp = td->td_sigstk.ss_sp;
1516 oss.ss_onstack = sigonstack(cpu_getstack(td));
1517 if (uap->nss != NULL) {
1518 td->td_sigstk.ss_sp = nss.ss_sp;
1519 td->td_sigstk.ss_size = 0;
1520 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1521 td->td_pflags |= TDP_ALTSTACK;
1523 if (uap->oss != NULL)
1524 error = copyout(&oss, uap->oss, sizeof(oss));
1528 #endif /* COMPAT_43 */
1530 #ifndef _SYS_SYSPROTO_H_
1531 struct sigaltstack_args {
1538 sys_sigaltstack(td, uap)
1540 register struct sigaltstack_args *uap;
1545 if (uap->ss != NULL) {
1546 error = copyin(uap->ss, &ss, sizeof(ss));
1550 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1551 (uap->oss != NULL) ? &oss : NULL);
1554 if (uap->oss != NULL)
1555 error = copyout(&oss, uap->oss, sizeof(stack_t));
1560 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1562 struct proc *p = td->td_proc;
1565 oonstack = sigonstack(cpu_getstack(td));
1568 *oss = td->td_sigstk;
1569 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1570 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1576 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1578 if (!(ss->ss_flags & SS_DISABLE)) {
1579 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1582 td->td_sigstk = *ss;
1583 td->td_pflags |= TDP_ALTSTACK;
1585 td->td_pflags &= ~TDP_ALTSTACK;
1592 * Common code for kill process group/broadcast kill.
1593 * cp is calling process.
1596 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1608 sx_slock(&allproc_lock);
1609 FOREACH_PROC_IN_SYSTEM(p) {
1611 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1612 p == td->td_proc || p->p_state == PRS_NEW) {
1616 err = p_cansignal(td, p, sig);
1619 pksignal(p, sig, ksi);
1622 else if (ret == ESRCH)
1626 sx_sunlock(&allproc_lock);
1628 sx_slock(&proctree_lock);
1631 * zero pgid means send to my process group.
1633 pgrp = td->td_proc->p_pgrp;
1636 pgrp = pgfind(pgid);
1638 sx_sunlock(&proctree_lock);
1642 sx_sunlock(&proctree_lock);
1643 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1645 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1646 p->p_state == PRS_NEW) {
1650 err = p_cansignal(td, p, sig);
1653 pksignal(p, sig, ksi);
1656 else if (ret == ESRCH)
1665 #ifndef _SYS_SYSPROTO_H_
1673 sys_kill(struct thread *td, struct kill_args *uap)
1680 * A process in capability mode can send signals only to himself.
1681 * The main rationale behind this is that abort(3) is implemented as
1682 * kill(getpid(), SIGABRT).
1684 if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid)
1687 AUDIT_ARG_SIGNUM(uap->signum);
1688 AUDIT_ARG_PID(uap->pid);
1689 if ((u_int)uap->signum > _SIG_MAXSIG)
1692 ksiginfo_init(&ksi);
1693 ksi.ksi_signo = uap->signum;
1694 ksi.ksi_code = SI_USER;
1695 ksi.ksi_pid = td->td_proc->p_pid;
1696 ksi.ksi_uid = td->td_ucred->cr_ruid;
1699 /* kill single process */
1700 if ((p = pfind(uap->pid)) == NULL) {
1701 if ((p = zpfind(uap->pid)) == NULL)
1704 AUDIT_ARG_PROCESS(p);
1705 error = p_cansignal(td, p, uap->signum);
1706 if (error == 0 && uap->signum)
1707 pksignal(p, uap->signum, &ksi);
1712 case -1: /* broadcast signal */
1713 return (killpg1(td, uap->signum, 0, 1, &ksi));
1714 case 0: /* signal own process group */
1715 return (killpg1(td, uap->signum, 0, 0, &ksi));
1716 default: /* negative explicit process group */
1717 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1725 struct pdkill_args *uap;
1729 cap_rights_t rights;
1732 AUDIT_ARG_SIGNUM(uap->signum);
1733 AUDIT_ARG_FD(uap->fd);
1734 if ((u_int)uap->signum > _SIG_MAXSIG)
1737 error = procdesc_find(td, uap->fd,
1738 cap_rights_init(&rights, CAP_PDKILL), &p);
1741 AUDIT_ARG_PROCESS(p);
1742 error = p_cansignal(td, p, uap->signum);
1743 if (error == 0 && uap->signum)
1744 kern_psignal(p, uap->signum);
1752 #if defined(COMPAT_43)
1753 #ifndef _SYS_SYSPROTO_H_
1754 struct okillpg_args {
1761 okillpg(struct thread *td, struct okillpg_args *uap)
1765 AUDIT_ARG_SIGNUM(uap->signum);
1766 AUDIT_ARG_PID(uap->pgid);
1767 if ((u_int)uap->signum > _SIG_MAXSIG)
1770 ksiginfo_init(&ksi);
1771 ksi.ksi_signo = uap->signum;
1772 ksi.ksi_code = SI_USER;
1773 ksi.ksi_pid = td->td_proc->p_pid;
1774 ksi.ksi_uid = td->td_ucred->cr_ruid;
1775 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1777 #endif /* COMPAT_43 */
1779 #ifndef _SYS_SYSPROTO_H_
1780 struct sigqueue_args {
1783 /* union sigval */ void *value;
1787 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1793 if ((u_int)uap->signum > _SIG_MAXSIG)
1797 * Specification says sigqueue can only send signal to
1803 if ((p = pfind(uap->pid)) == NULL) {
1804 if ((p = zpfind(uap->pid)) == NULL)
1807 error = p_cansignal(td, p, uap->signum);
1808 if (error == 0 && uap->signum != 0) {
1809 ksiginfo_init(&ksi);
1810 ksi.ksi_flags = KSI_SIGQ;
1811 ksi.ksi_signo = uap->signum;
1812 ksi.ksi_code = SI_QUEUE;
1813 ksi.ksi_pid = td->td_proc->p_pid;
1814 ksi.ksi_uid = td->td_ucred->cr_ruid;
1815 ksi.ksi_value.sival_ptr = uap->value;
1816 error = pksignal(p, ksi.ksi_signo, &ksi);
1823 * Send a signal to a process group.
1826 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1831 sx_slock(&proctree_lock);
1832 pgrp = pgfind(pgid);
1833 sx_sunlock(&proctree_lock);
1835 pgsignal(pgrp, sig, 0, ksi);
1842 * Send a signal to a process group. If checktty is 1,
1843 * limit to members which have a controlling terminal.
1846 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1851 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1852 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1854 if (p->p_state == PRS_NORMAL &&
1855 (checkctty == 0 || p->p_flag & P_CONTROLT))
1856 pksignal(p, sig, ksi);
1863 * Send a signal caused by a trap to the current thread. If it will be
1864 * caught immediately, deliver it with correct code. Otherwise, post it
1868 trapsignal(struct thread *td, ksiginfo_t *ksi)
1877 sig = ksi->ksi_signo;
1878 code = ksi->ksi_code;
1879 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1883 mtx_lock(&ps->ps_mtx);
1884 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1885 !SIGISMEMBER(td->td_sigmask, sig)) {
1886 td->td_ru.ru_nsignals++;
1888 if (KTRPOINT(curthread, KTR_PSIG))
1889 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1890 &td->td_sigmask, code);
1892 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1893 ksi, &td->td_sigmask);
1894 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1895 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1896 SIGADDSET(mask, sig);
1897 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1898 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1899 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1901 * See kern_sigaction() for origin of this code.
1903 SIGDELSET(ps->ps_sigcatch, sig);
1904 if (sig != SIGCONT &&
1905 sigprop(sig) & SA_IGNORE)
1906 SIGADDSET(ps->ps_sigignore, sig);
1907 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1909 mtx_unlock(&ps->ps_mtx);
1912 * Avoid a possible infinite loop if the thread
1913 * masking the signal or process is ignoring the
1916 if (kern_forcesigexit &&
1917 (SIGISMEMBER(td->td_sigmask, sig) ||
1918 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1919 SIGDELSET(td->td_sigmask, sig);
1920 SIGDELSET(ps->ps_sigcatch, sig);
1921 SIGDELSET(ps->ps_sigignore, sig);
1922 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1924 mtx_unlock(&ps->ps_mtx);
1925 p->p_code = code; /* XXX for core dump/debugger */
1926 p->p_sig = sig; /* XXX to verify code */
1927 tdsendsignal(p, td, sig, ksi);
1932 static struct thread *
1933 sigtd(struct proc *p, int sig, int prop)
1935 struct thread *td, *signal_td;
1937 PROC_LOCK_ASSERT(p, MA_OWNED);
1940 * Check if current thread can handle the signal without
1941 * switching context to another thread.
1943 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1946 FOREACH_THREAD_IN_PROC(p, td) {
1947 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1952 if (signal_td == NULL)
1953 signal_td = FIRST_THREAD_IN_PROC(p);
1958 * Send the signal to the process. If the signal has an action, the action
1959 * is usually performed by the target process rather than the caller; we add
1960 * the signal to the set of pending signals for the process.
1963 * o When a stop signal is sent to a sleeping process that takes the
1964 * default action, the process is stopped without awakening it.
1965 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1966 * regardless of the signal action (eg, blocked or ignored).
1968 * Other ignored signals are discarded immediately.
1970 * NB: This function may be entered from the debugger via the "kill" DDB
1971 * command. There is little that can be done to mitigate the possibly messy
1972 * side effects of this unwise possibility.
1975 kern_psignal(struct proc *p, int sig)
1979 ksiginfo_init(&ksi);
1980 ksi.ksi_signo = sig;
1981 ksi.ksi_code = SI_KERNEL;
1982 (void) tdsendsignal(p, NULL, sig, &ksi);
1986 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1989 return (tdsendsignal(p, NULL, sig, ksi));
1992 /* Utility function for finding a thread to send signal event to. */
1994 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
1998 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1999 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2011 tdsignal(struct thread *td, int sig)
2015 ksiginfo_init(&ksi);
2016 ksi.ksi_signo = sig;
2017 ksi.ksi_code = SI_KERNEL;
2018 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2022 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2025 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2029 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2032 sigqueue_t *sigqueue;
2039 MPASS(td == NULL || p == td->td_proc);
2040 PROC_LOCK_ASSERT(p, MA_OWNED);
2042 if (!_SIG_VALID(sig))
2043 panic("%s(): invalid signal %d", __func__, sig);
2045 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2048 * IEEE Std 1003.1-2001: return success when killing a zombie.
2050 if (p->p_state == PRS_ZOMBIE) {
2051 if (ksi && (ksi->ksi_flags & KSI_INS))
2052 ksiginfo_tryfree(ksi);
2057 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2058 prop = sigprop(sig);
2061 td = sigtd(p, sig, prop);
2062 sigqueue = &p->p_sigqueue;
2064 sigqueue = &td->td_sigqueue;
2066 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2069 * If the signal is being ignored,
2070 * then we forget about it immediately.
2071 * (Note: we don't set SIGCONT in ps_sigignore,
2072 * and if it is set to SIG_IGN,
2073 * action will be SIG_DFL here.)
2075 mtx_lock(&ps->ps_mtx);
2076 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2077 SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 );
2079 mtx_unlock(&ps->ps_mtx);
2080 if (ksi && (ksi->ksi_flags & KSI_INS))
2081 ksiginfo_tryfree(ksi);
2084 if (SIGISMEMBER(td->td_sigmask, sig))
2086 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2090 if (SIGISMEMBER(ps->ps_sigintr, sig))
2094 mtx_unlock(&ps->ps_mtx);
2097 sigqueue_delete_stopmask_proc(p);
2098 else if (prop & SA_STOP) {
2100 * If sending a tty stop signal to a member of an orphaned
2101 * process group, discard the signal here if the action
2102 * is default; don't stop the process below if sleeping,
2103 * and don't clear any pending SIGCONT.
2105 if ((prop & SA_TTYSTOP) &&
2106 (p->p_pgrp->pg_jobc == 0) &&
2107 (action == SIG_DFL)) {
2108 if (ksi && (ksi->ksi_flags & KSI_INS))
2109 ksiginfo_tryfree(ksi);
2112 sigqueue_delete_proc(p, SIGCONT);
2113 if (p->p_flag & P_CONTINUED) {
2114 p->p_flag &= ~P_CONTINUED;
2115 PROC_LOCK(p->p_pptr);
2116 sigqueue_take(p->p_ksi);
2117 PROC_UNLOCK(p->p_pptr);
2121 ret = sigqueue_add(sigqueue, sig, ksi);
2126 * Defer further processing for signals which are held,
2127 * except that stopped processes must be continued by SIGCONT.
2129 if (action == SIG_HOLD &&
2130 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2133 * SIGKILL: Remove procfs STOPEVENTs.
2135 if (sig == SIGKILL) {
2136 /* from procfs_ioctl.c: PIOCBIC */
2138 /* from procfs_ioctl.c: PIOCCONT */
2143 * Some signals have a process-wide effect and a per-thread
2144 * component. Most processing occurs when the process next
2145 * tries to cross the user boundary, however there are some
2146 * times when processing needs to be done immediately, such as
2147 * waking up threads so that they can cross the user boundary.
2148 * We try to do the per-process part here.
2150 if (P_SHOULDSTOP(p)) {
2151 KASSERT(!(p->p_flag & P_WEXIT),
2152 ("signal to stopped but exiting process"));
2153 if (sig == SIGKILL) {
2155 * If traced process is already stopped,
2156 * then no further action is necessary.
2158 if (p->p_flag & P_TRACED)
2161 * SIGKILL sets process running.
2162 * It will die elsewhere.
2163 * All threads must be restarted.
2165 p->p_flag &= ~P_STOPPED_SIG;
2169 if (prop & SA_CONT) {
2171 * If traced process is already stopped,
2172 * then no further action is necessary.
2174 if (p->p_flag & P_TRACED)
2177 * If SIGCONT is default (or ignored), we continue the
2178 * process but don't leave the signal in sigqueue as
2179 * it has no further action. If SIGCONT is held, we
2180 * continue the process and leave the signal in
2181 * sigqueue. If the process catches SIGCONT, let it
2182 * handle the signal itself. If it isn't waiting on
2183 * an event, it goes back to run state.
2184 * Otherwise, process goes back to sleep state.
2186 p->p_flag &= ~P_STOPPED_SIG;
2188 if (p->p_numthreads == p->p_suspcount) {
2190 p->p_flag |= P_CONTINUED;
2191 p->p_xstat = SIGCONT;
2192 PROC_LOCK(p->p_pptr);
2193 childproc_continued(p);
2194 PROC_UNLOCK(p->p_pptr);
2197 if (action == SIG_DFL) {
2198 thread_unsuspend(p);
2200 sigqueue_delete(sigqueue, sig);
2203 if (action == SIG_CATCH) {
2205 * The process wants to catch it so it needs
2206 * to run at least one thread, but which one?
2212 * The signal is not ignored or caught.
2214 thread_unsuspend(p);
2219 if (prop & SA_STOP) {
2221 * If traced process is already stopped,
2222 * then no further action is necessary.
2224 if (p->p_flag & P_TRACED)
2227 * Already stopped, don't need to stop again
2228 * (If we did the shell could get confused).
2229 * Just make sure the signal STOP bit set.
2231 p->p_flag |= P_STOPPED_SIG;
2232 sigqueue_delete(sigqueue, sig);
2237 * All other kinds of signals:
2238 * If a thread is sleeping interruptibly, simulate a
2239 * wakeup so that when it is continued it will be made
2240 * runnable and can look at the signal. However, don't make
2241 * the PROCESS runnable, leave it stopped.
2242 * It may run a bit until it hits a thread_suspend_check().
2247 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2248 wakeup_swapper = sleepq_abort(td, intrval);
2255 * Mutexes are short lived. Threads waiting on them will
2256 * hit thread_suspend_check() soon.
2258 } else if (p->p_state == PRS_NORMAL) {
2259 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2260 tdsigwakeup(td, sig, action, intrval);
2264 MPASS(action == SIG_DFL);
2266 if (prop & SA_STOP) {
2267 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2269 p->p_flag |= P_STOPPED_SIG;
2272 sig_suspend_threads(td, p, 1);
2273 if (p->p_numthreads == p->p_suspcount) {
2275 * only thread sending signal to another
2276 * process can reach here, if thread is sending
2277 * signal to its process, because thread does
2278 * not suspend itself here, p_numthreads
2279 * should never be equal to p_suspcount.
2283 sigqueue_delete_proc(p, p->p_xstat);
2289 /* Not in "NORMAL" state. discard the signal. */
2290 sigqueue_delete(sigqueue, sig);
2295 * The process is not stopped so we need to apply the signal to all the
2299 tdsigwakeup(td, sig, action, intrval);
2301 thread_unsuspend(p);
2304 /* If we jump here, proc slock should not be owned. */
2305 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2310 * The force of a signal has been directed against a single
2311 * thread. We need to see what we can do about knocking it
2312 * out of any sleep it may be in etc.
2315 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2317 struct proc *p = td->td_proc;
2322 PROC_LOCK_ASSERT(p, MA_OWNED);
2323 prop = sigprop(sig);
2328 * Bring the priority of a thread up if we want it to get
2329 * killed in this lifetime.
2331 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2332 sched_prio(td, PUSER);
2333 if (TD_ON_SLEEPQ(td)) {
2335 * If thread is sleeping uninterruptibly
2336 * we can't interrupt the sleep... the signal will
2337 * be noticed when the process returns through
2338 * trap() or syscall().
2340 if ((td->td_flags & TDF_SINTR) == 0)
2343 * If SIGCONT is default (or ignored) and process is
2344 * asleep, we are finished; the process should not
2347 if ((prop & SA_CONT) && action == SIG_DFL) {
2350 sigqueue_delete(&p->p_sigqueue, sig);
2352 * It may be on either list in this state.
2353 * Remove from both for now.
2355 sigqueue_delete(&td->td_sigqueue, sig);
2360 * Don't awaken a sleeping thread for SIGSTOP if the
2361 * STOP signal is deferred.
2363 if ((prop & SA_STOP) && (td->td_flags & TDF_SBDRY))
2367 * Give low priority threads a better chance to run.
2369 if (td->td_priority > PUSER)
2370 sched_prio(td, PUSER);
2372 wakeup_swapper = sleepq_abort(td, intrval);
2375 * Other states do nothing with the signal immediately,
2376 * other than kicking ourselves if we are running.
2377 * It will either never be noticed, or noticed very soon.
2380 if (TD_IS_RUNNING(td) && td != curthread)
2392 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2396 PROC_LOCK_ASSERT(p, MA_OWNED);
2397 PROC_SLOCK_ASSERT(p, MA_OWNED);
2399 FOREACH_THREAD_IN_PROC(p, td2) {
2401 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2402 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2403 (td2->td_flags & TDF_SINTR)) {
2404 if (td2->td_flags & TDF_SBDRY) {
2406 * Once a thread is asleep with
2407 * TDF_SBDRY set, it should never
2408 * become suspended due to this check.
2410 KASSERT(!TD_IS_SUSPENDED(td2),
2411 ("thread with deferred stops suspended"));
2412 } else if (!TD_IS_SUSPENDED(td2)) {
2413 thread_suspend_one(td2);
2415 } else if (!TD_IS_SUSPENDED(td2)) {
2416 if (sending || td != td2)
2417 td2->td_flags |= TDF_ASTPENDING;
2419 if (TD_IS_RUNNING(td2) && td2 != td)
2420 forward_signal(td2);
2428 ptracestop(struct thread *td, int sig)
2430 struct proc *p = td->td_proc;
2432 PROC_LOCK_ASSERT(p, MA_OWNED);
2433 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2434 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2435 &p->p_mtx.lock_object, "Stopping for traced signal");
2437 td->td_dbgflags |= TDB_XSIG;
2440 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2441 if (p->p_flag & P_SINGLE_EXIT) {
2442 td->td_dbgflags &= ~TDB_XSIG;
2447 * Just make wait() to work, the last stopped thread
2452 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2453 sig_suspend_threads(td, p, 0);
2454 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2455 td->td_dbgflags &= ~TDB_STOPATFORK;
2456 cv_broadcast(&p->p_dbgwait);
2459 thread_suspend_switch(td);
2460 if (p->p_xthread == td)
2461 p->p_xthread = NULL;
2462 if (!(p->p_flag & P_TRACED))
2464 if (td->td_dbgflags & TDB_SUSPEND) {
2465 if (p->p_flag & P_SINGLE_EXIT)
2471 return (td->td_xsig);
2475 reschedule_signals(struct proc *p, sigset_t block, int flags)
2481 PROC_LOCK_ASSERT(p, MA_OWNED);
2482 if (SIGISEMPTY(p->p_siglist))
2485 SIGSETAND(block, p->p_siglist);
2486 while ((sig = sig_ffs(&block)) != 0) {
2487 SIGDELSET(block, sig);
2488 td = sigtd(p, sig, 0);
2490 if (!(flags & SIGPROCMASK_PS_LOCKED))
2491 mtx_lock(&ps->ps_mtx);
2492 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2493 tdsigwakeup(td, sig, SIG_CATCH,
2494 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2496 if (!(flags & SIGPROCMASK_PS_LOCKED))
2497 mtx_unlock(&ps->ps_mtx);
2502 tdsigcleanup(struct thread *td)
2508 PROC_LOCK_ASSERT(p, MA_OWNED);
2510 sigqueue_flush(&td->td_sigqueue);
2511 if (p->p_numthreads == 1)
2515 * Since we cannot handle signals, notify signal post code
2516 * about this by filling the sigmask.
2518 * Also, if needed, wake up thread(s) that do not block the
2519 * same signals as the exiting thread, since the thread might
2520 * have been selected for delivery and woken up.
2522 SIGFILLSET(unblocked);
2523 SIGSETNAND(unblocked, td->td_sigmask);
2524 SIGFILLSET(td->td_sigmask);
2525 reschedule_signals(p, unblocked, 0);
2530 * Defer the delivery of SIGSTOP for the current thread. Returns true
2531 * if stops were deferred and false if they were already deferred.
2539 if (td->td_flags & TDF_SBDRY)
2542 td->td_flags |= TDF_SBDRY;
2548 * Permit the delivery of SIGSTOP for the current thread. This does
2549 * not immediately suspend if a stop was posted. Instead, the thread
2550 * will suspend either via ast() or a subsequent interruptible sleep.
2559 td->td_flags &= ~TDF_SBDRY;
2564 * If the current process has received a signal (should be caught or cause
2565 * termination, should interrupt current syscall), return the signal number.
2566 * Stop signals with default action are processed immediately, then cleared;
2567 * they aren't returned. This is checked after each entry to the system for
2568 * a syscall or trap (though this can usually be done without calling issignal
2569 * by checking the pending signal masks in cursig.) The normal call
2572 * while (sig = cursig(curthread))
2576 issignal(struct thread *td)
2580 struct sigqueue *queue;
2581 sigset_t sigpending;
2582 int sig, prop, newsig;
2586 mtx_assert(&ps->ps_mtx, MA_OWNED);
2587 PROC_LOCK_ASSERT(p, MA_OWNED);
2589 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2591 sigpending = td->td_sigqueue.sq_signals;
2592 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2593 SIGSETNAND(sigpending, td->td_sigmask);
2595 if (p->p_flag & P_PPWAIT || td->td_flags & TDF_SBDRY)
2596 SIG_STOPSIGMASK(sigpending);
2597 if (SIGISEMPTY(sigpending)) /* no signal to send */
2599 sig = sig_ffs(&sigpending);
2601 if (p->p_stops & S_SIG) {
2602 mtx_unlock(&ps->ps_mtx);
2603 stopevent(p, S_SIG, sig);
2604 mtx_lock(&ps->ps_mtx);
2608 * We should see pending but ignored signals
2609 * only if P_TRACED was on when they were posted.
2611 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2612 sigqueue_delete(&td->td_sigqueue, sig);
2613 sigqueue_delete(&p->p_sigqueue, sig);
2616 if (p->p_flag & P_TRACED && (p->p_flag & P_PPTRACE) == 0) {
2618 * If traced, always stop.
2619 * Remove old signal from queue before the stop.
2620 * XXX shrug off debugger, it causes siginfo to
2623 queue = &td->td_sigqueue;
2624 td->td_dbgksi.ksi_signo = 0;
2625 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2626 queue = &p->p_sigqueue;
2627 sigqueue_get(queue, sig, &td->td_dbgksi);
2630 mtx_unlock(&ps->ps_mtx);
2631 newsig = ptracestop(td, sig);
2632 mtx_lock(&ps->ps_mtx);
2634 if (sig != newsig) {
2637 * If parent wants us to take the signal,
2638 * then it will leave it in p->p_xstat;
2639 * otherwise we just look for signals again.
2646 * Put the new signal into td_sigqueue. If the
2647 * signal is being masked, look for other
2650 sigqueue_add(queue, sig, NULL);
2651 if (SIGISMEMBER(td->td_sigmask, sig))
2655 if (td->td_dbgksi.ksi_signo != 0) {
2656 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2657 if (sigqueue_add(&td->td_sigqueue, sig,
2658 &td->td_dbgksi) != 0)
2659 td->td_dbgksi.ksi_signo = 0;
2661 if (td->td_dbgksi.ksi_signo == 0)
2662 sigqueue_add(&td->td_sigqueue, sig,
2667 * If the traced bit got turned off, go back up
2668 * to the top to rescan signals. This ensures
2669 * that p_sig* and p_sigact are consistent.
2671 if ((p->p_flag & P_TRACED) == 0)
2675 prop = sigprop(sig);
2678 * Decide whether the signal should be returned.
2679 * Return the signal's number, or fall through
2680 * to clear it from the pending mask.
2682 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2684 case (intptr_t)SIG_DFL:
2686 * Don't take default actions on system processes.
2688 if (p->p_pid <= 1) {
2691 * Are you sure you want to ignore SIGSEGV
2694 printf("Process (pid %lu) got signal %d\n",
2695 (u_long)p->p_pid, sig);
2697 break; /* == ignore */
2700 * If there is a pending stop signal to process
2701 * with default action, stop here,
2702 * then clear the signal. However,
2703 * if process is member of an orphaned
2704 * process group, ignore tty stop signals.
2706 if (prop & SA_STOP) {
2707 if (p->p_flag & (P_TRACED|P_WEXIT) ||
2708 (p->p_pgrp->pg_jobc == 0 &&
2710 break; /* == ignore */
2711 mtx_unlock(&ps->ps_mtx);
2712 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2713 &p->p_mtx.lock_object, "Catching SIGSTOP");
2714 p->p_flag |= P_STOPPED_SIG;
2717 sig_suspend_threads(td, p, 0);
2718 thread_suspend_switch(td);
2720 mtx_lock(&ps->ps_mtx);
2722 } else if (prop & SA_IGNORE) {
2724 * Except for SIGCONT, shouldn't get here.
2725 * Default action is to ignore; drop it.
2727 break; /* == ignore */
2732 case (intptr_t)SIG_IGN:
2734 * Masking above should prevent us ever trying
2735 * to take action on an ignored signal other
2736 * than SIGCONT, unless process is traced.
2738 if ((prop & SA_CONT) == 0 &&
2739 (p->p_flag & P_TRACED) == 0)
2740 printf("issignal\n");
2741 break; /* == ignore */
2745 * This signal has an action, let
2746 * postsig() process it.
2750 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2751 sigqueue_delete(&p->p_sigqueue, sig);
2757 thread_stopped(struct proc *p)
2761 PROC_LOCK_ASSERT(p, MA_OWNED);
2762 PROC_SLOCK_ASSERT(p, MA_OWNED);
2766 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2768 p->p_flag &= ~P_WAITED;
2769 PROC_LOCK(p->p_pptr);
2770 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2771 CLD_TRAPPED : CLD_STOPPED);
2772 PROC_UNLOCK(p->p_pptr);
2778 * Take the action for the specified signal
2779 * from the current set of pending signals.
2785 struct thread *td = curthread;
2786 register struct proc *p = td->td_proc;
2790 sigset_t returnmask, mask;
2792 KASSERT(sig != 0, ("postsig"));
2794 PROC_LOCK_ASSERT(p, MA_OWNED);
2796 mtx_assert(&ps->ps_mtx, MA_OWNED);
2797 ksiginfo_init(&ksi);
2798 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2799 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2801 ksi.ksi_signo = sig;
2802 if (ksi.ksi_code == SI_TIMER)
2803 itimer_accept(p, ksi.ksi_timerid, &ksi);
2804 action = ps->ps_sigact[_SIG_IDX(sig)];
2806 if (KTRPOINT(td, KTR_PSIG))
2807 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2808 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2810 if (p->p_stops & S_SIG) {
2811 mtx_unlock(&ps->ps_mtx);
2812 stopevent(p, S_SIG, sig);
2813 mtx_lock(&ps->ps_mtx);
2816 if (action == SIG_DFL) {
2818 * Default action, where the default is to kill
2819 * the process. (Other cases were ignored above.)
2821 mtx_unlock(&ps->ps_mtx);
2826 * If we get here, the signal must be caught.
2828 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2829 ("postsig action"));
2831 * Set the new mask value and also defer further
2832 * occurrences of this signal.
2834 * Special case: user has done a sigsuspend. Here the
2835 * current mask is not of interest, but rather the
2836 * mask from before the sigsuspend is what we want
2837 * restored after the signal processing is completed.
2839 if (td->td_pflags & TDP_OLDMASK) {
2840 returnmask = td->td_oldsigmask;
2841 td->td_pflags &= ~TDP_OLDMASK;
2843 returnmask = td->td_sigmask;
2845 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2846 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2847 SIGADDSET(mask, sig);
2848 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2849 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2851 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2853 * See kern_sigaction() for origin of this code.
2855 SIGDELSET(ps->ps_sigcatch, sig);
2856 if (sig != SIGCONT &&
2857 sigprop(sig) & SA_IGNORE)
2858 SIGADDSET(ps->ps_sigignore, sig);
2859 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2861 td->td_ru.ru_nsignals++;
2862 if (p->p_sig == sig) {
2866 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2872 * Kill the current process for stated reason.
2880 PROC_LOCK_ASSERT(p, MA_OWNED);
2881 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
2883 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid,
2884 p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2885 p->p_flag |= P_WKILLED;
2886 kern_psignal(p, SIGKILL);
2890 * Force the current process to exit with the specified signal, dumping core
2891 * if appropriate. We bypass the normal tests for masked and caught signals,
2892 * allowing unrecoverable failures to terminate the process without changing
2893 * signal state. Mark the accounting record with the signal termination.
2894 * If dumping core, save the signal number for the debugger. Calls exit and
2902 struct proc *p = td->td_proc;
2904 PROC_LOCK_ASSERT(p, MA_OWNED);
2905 p->p_acflag |= AXSIG;
2907 * We must be single-threading to generate a core dump. This
2908 * ensures that the registers in the core file are up-to-date.
2909 * Also, the ELF dump handler assumes that the thread list doesn't
2910 * change out from under it.
2912 * XXX If another thread attempts to single-thread before us
2913 * (e.g. via fork()), we won't get a dump at all.
2915 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2918 * Log signals which would cause core dumps
2919 * (Log as LOG_INFO to appease those who don't want
2921 * XXX : Todo, as well as euid, write out ruid too
2922 * Note that coredump() drops proc lock.
2924 if (coredump(td) == 0)
2926 if (kern_logsigexit)
2928 "pid %d (%s), uid %d: exited on signal %d%s\n",
2929 p->p_pid, p->p_comm,
2930 td->td_ucred ? td->td_ucred->cr_uid : -1,
2932 sig & WCOREFLAG ? " (core dumped)" : "");
2935 exit1(td, W_EXITCODE(0, sig));
2940 * Send queued SIGCHLD to parent when child process's state
2944 sigparent(struct proc *p, int reason, int status)
2946 PROC_LOCK_ASSERT(p, MA_OWNED);
2947 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2949 if (p->p_ksi != NULL) {
2950 p->p_ksi->ksi_signo = SIGCHLD;
2951 p->p_ksi->ksi_code = reason;
2952 p->p_ksi->ksi_status = status;
2953 p->p_ksi->ksi_pid = p->p_pid;
2954 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2955 if (KSI_ONQ(p->p_ksi))
2958 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2962 childproc_jobstate(struct proc *p, int reason, int status)
2966 PROC_LOCK_ASSERT(p, MA_OWNED);
2967 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2970 * Wake up parent sleeping in kern_wait(), also send
2971 * SIGCHLD to parent, but SIGCHLD does not guarantee
2972 * that parent will awake, because parent may masked
2975 p->p_pptr->p_flag |= P_STATCHILD;
2978 ps = p->p_pptr->p_sigacts;
2979 mtx_lock(&ps->ps_mtx);
2980 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2981 mtx_unlock(&ps->ps_mtx);
2982 sigparent(p, reason, status);
2984 mtx_unlock(&ps->ps_mtx);
2988 childproc_stopped(struct proc *p, int reason)
2990 childproc_jobstate(p, reason, p->p_xstat);
2994 childproc_continued(struct proc *p)
2996 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3000 childproc_exited(struct proc *p)
3003 int status = p->p_xstat; /* convert to int */
3005 reason = CLD_EXITED;
3006 if (WCOREDUMP(status))
3007 reason = CLD_DUMPED;
3008 else if (WIFSIGNALED(status))
3009 reason = CLD_KILLED;
3011 * XXX avoid calling wakeup(p->p_pptr), the work is
3014 sigparent(p, reason, status);
3018 * We only have 1 character for the core count in the format
3019 * string, so the range will be 0-9
3021 #define MAX_NUM_CORES 10
3022 static int num_cores = 5;
3025 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3030 new_val = num_cores;
3031 error = sysctl_handle_int(oidp, &new_val, 0, req);
3032 if (error != 0 || req->newptr == NULL)
3034 if (new_val > MAX_NUM_CORES)
3035 new_val = MAX_NUM_CORES;
3038 num_cores = new_val;
3041 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3042 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
3044 #if defined(COMPRESS_USER_CORES)
3045 int compress_user_cores = 1;
3046 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
3047 &compress_user_cores, 0, "Compression of user corefiles");
3049 int compress_user_cores_gzlevel = -1; /* default level */
3050 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
3051 &compress_user_cores_gzlevel, -1, "Corefile gzip compression level");
3053 #define GZ_SUFFIX ".gz"
3054 #define GZ_SUFFIX_LEN 3
3057 static char corefilename[MAXPATHLEN] = {"%N.core"};
3058 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3059 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
3060 sizeof(corefilename), "Process corefile name format string");
3063 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3064 * Expand the name described in corefilename, using name, uid, and pid
3065 * and open/create core file.
3066 * corefilename is a printf-like string, with three format specifiers:
3067 * %N name of process ("name")
3068 * %P process id (pid)
3070 * For example, "%N.core" is the default; they can be disabled completely
3071 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3072 * This is controlled by the sysctl variable kern.corefile (see above).
3075 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3076 int compress, struct vnode **vpp, char **namep)
3078 struct nameidata nd;
3081 char *hostname, *name;
3082 int indexpos, i, error, cmode, flags, oflags;
3085 format = corefilename;
3086 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3088 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3089 for (i = 0; format[i] != '\0'; i++) {
3090 switch (format[i]) {
3091 case '%': /* Format character */
3093 switch (format[i]) {
3095 sbuf_putc(&sb, '%');
3097 case 'H': /* hostname */
3098 if (hostname == NULL) {
3099 hostname = malloc(MAXHOSTNAMELEN,
3102 getcredhostname(td->td_ucred, hostname,
3104 sbuf_printf(&sb, "%s", hostname);
3106 case 'I': /* autoincrementing index */
3107 sbuf_printf(&sb, "0");
3108 indexpos = sbuf_len(&sb) - 1;
3110 case 'N': /* process name */
3111 sbuf_printf(&sb, "%s", comm);
3113 case 'P': /* process id */
3114 sbuf_printf(&sb, "%u", pid);
3116 case 'U': /* user id */
3117 sbuf_printf(&sb, "%u", uid);
3121 "Unknown format character %c in "
3122 "corename `%s'\n", format[i], format);
3127 sbuf_putc(&sb, format[i]);
3131 free(hostname, M_TEMP);
3132 #ifdef COMPRESS_USER_CORES
3134 sbuf_printf(&sb, GZ_SUFFIX);
3136 if (sbuf_error(&sb) != 0) {
3137 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3138 "long\n", (long)pid, comm, (u_long)uid);
3146 cmode = S_IRUSR | S_IWUSR;
3147 oflags = VN_OPEN_NOAUDIT | (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3150 * If the core format has a %I in it, then we need to check
3151 * for existing corefiles before returning a name.
3152 * To do this we iterate over 0..num_cores to find a
3153 * non-existing core file name to use.
3155 if (indexpos != -1) {
3156 for (i = 0; i < num_cores; i++) {
3157 flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3158 name[indexpos] = '0' + i;
3159 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3160 error = vn_open_cred(&nd, &flags, cmode, oflags,
3161 td->td_ucred, NULL);
3163 if (error == EEXIST)
3166 "pid %d (%s), uid (%u): Path `%s' failed "
3167 "on initial open test, error = %d\n",
3168 pid, comm, uid, name, error);
3174 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3175 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3176 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL);
3180 audit_proc_coredump(td, name, error);
3185 NDFREE(&nd, NDF_ONLY_PNBUF);
3192 * Dump a process' core. The main routine does some
3193 * policy checking, and creates the name of the coredump;
3194 * then it passes on a vnode and a size limit to the process-specific
3195 * coredump routine if there is one; if there _is not_ one, it returns
3196 * ENOSYS; otherwise it returns the error from the process-specific routine.
3200 coredump(struct thread *td)
3202 struct proc *p = td->td_proc;
3203 struct ucred *cred = td->td_ucred;
3207 int error, error1, locked;
3209 char *name; /* name of corefile */
3213 #ifdef COMPRESS_USER_CORES
3214 compress = compress_user_cores;
3218 PROC_LOCK_ASSERT(p, MA_OWNED);
3219 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3220 _STOPEVENT(p, S_CORE, 0);
3222 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0)) {
3228 * Note that the bulk of limit checking is done after
3229 * the corefile is created. The exception is if the limit
3230 * for corefiles is 0, in which case we don't bother
3231 * creating the corefile at all. This layout means that
3232 * a corefile is truncated instead of not being created,
3233 * if it is larger than the limit.
3235 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3236 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3243 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, compress,
3248 /* Don't dump to non-regular files or files with links. */
3249 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3250 vattr.va_nlink != 1) {
3257 lf.l_whence = SEEK_SET;
3260 lf.l_type = F_WRLCK;
3261 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3263 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3264 lf.l_type = F_UNLCK;
3266 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3267 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3269 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3277 if (set_core_nodump_flag)
3278 vattr.va_flags = UF_NODUMP;
3279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3280 VOP_SETATTR(vp, &vattr, cred);
3282 vn_finished_write(mp);
3284 p->p_acflag |= ACORE;
3287 if (p->p_sysent->sv_coredump != NULL) {
3288 error = p->p_sysent->sv_coredump(td, vp, limit,
3289 compress ? IMGACT_CORE_COMPRESS : 0);
3295 lf.l_type = F_UNLCK;
3296 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3299 error1 = vn_close(vp, FWRITE, cred, td);
3304 audit_proc_coredump(td, name, error);
3311 * Nonexistent system call-- signal process (may want to handle it). Flag
3312 * error in case process won't see signal immediately (blocked or ignored).
3314 #ifndef _SYS_SYSPROTO_H_
3323 struct nosys_args *args;
3325 struct proc *p = td->td_proc;
3328 tdsignal(td, SIGSYS);
3334 * Send a SIGIO or SIGURG signal to a process or process group using stored
3335 * credentials rather than those of the current process.
3338 pgsigio(sigiop, sig, checkctty)
3339 struct sigio **sigiop;
3343 struct sigio *sigio;
3345 ksiginfo_init(&ksi);
3346 ksi.ksi_signo = sig;
3347 ksi.ksi_code = SI_KERNEL;
3351 if (sigio == NULL) {
3355 if (sigio->sio_pgid > 0) {
3356 PROC_LOCK(sigio->sio_proc);
3357 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3358 kern_psignal(sigio->sio_proc, sig);
3359 PROC_UNLOCK(sigio->sio_proc);
3360 } else if (sigio->sio_pgid < 0) {
3363 PGRP_LOCK(sigio->sio_pgrp);
3364 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3366 if (p->p_state == PRS_NORMAL &&
3367 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3368 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3369 kern_psignal(p, sig);
3372 PGRP_UNLOCK(sigio->sio_pgrp);
3378 filt_sigattach(struct knote *kn)
3380 struct proc *p = curproc;
3382 kn->kn_ptr.p_proc = p;
3383 kn->kn_flags |= EV_CLEAR; /* automatically set */
3385 knlist_add(&p->p_klist, kn, 0);
3391 filt_sigdetach(struct knote *kn)
3393 struct proc *p = kn->kn_ptr.p_proc;
3395 knlist_remove(&p->p_klist, kn, 0);
3399 * signal knotes are shared with proc knotes, so we apply a mask to
3400 * the hint in order to differentiate them from process hints. This
3401 * could be avoided by using a signal-specific knote list, but probably
3402 * isn't worth the trouble.
3405 filt_signal(struct knote *kn, long hint)
3408 if (hint & NOTE_SIGNAL) {
3409 hint &= ~NOTE_SIGNAL;
3411 if (kn->kn_id == hint)
3414 return (kn->kn_data != 0);
3422 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3424 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3429 sigacts_free(struct sigacts *ps)
3432 mtx_lock(&ps->ps_mtx);
3434 if (ps->ps_refcnt == 0) {
3435 mtx_destroy(&ps->ps_mtx);
3436 free(ps, M_SUBPROC);
3438 mtx_unlock(&ps->ps_mtx);
3442 sigacts_hold(struct sigacts *ps)
3444 mtx_lock(&ps->ps_mtx);
3446 mtx_unlock(&ps->ps_mtx);
3451 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3454 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3455 mtx_lock(&src->ps_mtx);
3456 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3457 mtx_unlock(&src->ps_mtx);
3461 sigacts_shared(struct sigacts *ps)
3465 mtx_lock(&ps->ps_mtx);
3466 shared = ps->ps_refcnt > 1;
3467 mtx_unlock(&ps->ps_mtx);