2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include "opt_procdesc.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
51 #include <sys/capability.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/imgact.h>
56 #include <sys/kernel.h>
58 #include <sys/ktrace.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
64 #include <sys/procdesc.h>
65 #include <sys/posix4.h>
66 #include <sys/pioctl.h>
67 #include <sys/racct.h>
68 #include <sys/resourcevar.h>
71 #include <sys/sleepqueue.h>
75 #include <sys/syscallsubr.h>
76 #include <sys/sysctl.h>
77 #include <sys/sysent.h>
78 #include <sys/syslog.h>
79 #include <sys/sysproto.h>
80 #include <sys/timers.h>
81 #include <sys/unistd.h>
84 #include <vm/vm_extern.h>
89 #include <machine/cpu.h>
91 #include <security/audit/audit.h>
93 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
95 SDT_PROVIDER_DECLARE(proc);
96 SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send);
97 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *");
98 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *");
99 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int");
100 SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear);
101 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int");
102 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
103 SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard);
104 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
105 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
106 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
108 static int coredump(struct thread *);
109 static char *expand_name(const char *, uid_t, pid_t, struct thread *, int);
110 static int killpg1(struct thread *td, int sig, int pgid, int all,
112 static int issignal(struct thread *td, int stop_allowed);
113 static int sigprop(int sig);
114 static void tdsigwakeup(struct thread *, int, sig_t, int);
115 static void sig_suspend_threads(struct thread *, struct proc *, int);
116 static int filt_sigattach(struct knote *kn);
117 static void filt_sigdetach(struct knote *kn);
118 static int filt_signal(struct knote *kn, long hint);
119 static struct thread *sigtd(struct proc *p, int sig, int prop);
120 static void sigqueue_start(void);
122 static uma_zone_t ksiginfo_zone = NULL;
123 struct filterops sig_filtops = {
125 .f_attach = filt_sigattach,
126 .f_detach = filt_sigdetach,
127 .f_event = filt_signal,
130 static int kern_logsigexit = 1;
131 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
133 "Log processes quitting on abnormal signals to syslog(3)");
135 static int kern_forcesigexit = 1;
136 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
137 &kern_forcesigexit, 0, "Force trap signal to be handled");
139 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
141 static int max_pending_per_proc = 128;
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
143 &max_pending_per_proc, 0, "Max pending signals per proc");
145 static int preallocate_siginfo = 1024;
146 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
147 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
148 &preallocate_siginfo, 0, "Preallocated signal memory size");
150 static int signal_overflow = 0;
151 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
152 &signal_overflow, 0, "Number of signals overflew");
154 static int signal_alloc_fail = 0;
155 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
156 &signal_alloc_fail, 0, "signals failed to be allocated");
158 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
161 * Policy -- Can ucred cr1 send SIGIO to process cr2?
162 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
163 * in the right situations.
165 #define CANSIGIO(cr1, cr2) \
166 ((cr1)->cr_uid == 0 || \
167 (cr1)->cr_ruid == (cr2)->cr_ruid || \
168 (cr1)->cr_uid == (cr2)->cr_ruid || \
169 (cr1)->cr_ruid == (cr2)->cr_uid || \
170 (cr1)->cr_uid == (cr2)->cr_uid)
172 static int sugid_coredump;
173 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
174 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
176 static int do_coredump = 1;
177 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
178 &do_coredump, 0, "Enable/Disable coredumps");
180 static int set_core_nodump_flag = 0;
181 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
182 0, "Enable setting the NODUMP flag on coredump files");
185 * Signal properties and actions.
186 * The array below categorizes the signals and their default actions
187 * according to the following properties:
189 #define SA_KILL 0x01 /* terminates process by default */
190 #define SA_CORE 0x02 /* ditto and coredumps */
191 #define SA_STOP 0x04 /* suspend process */
192 #define SA_TTYSTOP 0x08 /* ditto, from tty */
193 #define SA_IGNORE 0x10 /* ignore by default */
194 #define SA_CONT 0x20 /* continue if suspended */
195 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
196 #define SA_PROC 0x80 /* deliverable to any thread */
198 static int sigproptbl[NSIG] = {
199 SA_KILL|SA_PROC, /* SIGHUP */
200 SA_KILL|SA_PROC, /* SIGINT */
201 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
202 SA_KILL|SA_CORE, /* SIGILL */
203 SA_KILL|SA_CORE, /* SIGTRAP */
204 SA_KILL|SA_CORE, /* SIGABRT */
205 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
206 SA_KILL|SA_CORE, /* SIGFPE */
207 SA_KILL|SA_PROC, /* SIGKILL */
208 SA_KILL|SA_CORE, /* SIGBUS */
209 SA_KILL|SA_CORE, /* SIGSEGV */
210 SA_KILL|SA_CORE, /* SIGSYS */
211 SA_KILL|SA_PROC, /* SIGPIPE */
212 SA_KILL|SA_PROC, /* SIGALRM */
213 SA_KILL|SA_PROC, /* SIGTERM */
214 SA_IGNORE|SA_PROC, /* SIGURG */
215 SA_STOP|SA_PROC, /* SIGSTOP */
216 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
217 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
218 SA_IGNORE|SA_PROC, /* SIGCHLD */
219 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
220 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
221 SA_IGNORE|SA_PROC, /* SIGIO */
222 SA_KILL, /* SIGXCPU */
223 SA_KILL, /* SIGXFSZ */
224 SA_KILL|SA_PROC, /* SIGVTALRM */
225 SA_KILL|SA_PROC, /* SIGPROF */
226 SA_IGNORE|SA_PROC, /* SIGWINCH */
227 SA_IGNORE|SA_PROC, /* SIGINFO */
228 SA_KILL|SA_PROC, /* SIGUSR1 */
229 SA_KILL|SA_PROC, /* 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 process p, the current
559 * process, 0 if none. If there is a pending stop signal with default
560 * action, the process stops in issignal().
563 cursig(struct thread *td, int stop_allowed)
565 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
566 KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
567 stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
568 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
569 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
570 return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
574 * Arrange for ast() to handle unmasked pending signals on return to user
575 * mode. This must be called whenever a signal is added to td_sigqueue or
576 * unmasked in td_sigmask.
579 signotify(struct thread *td)
585 PROC_LOCK_ASSERT(p, MA_OWNED);
587 if (SIGPENDING(td)) {
589 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
595 sigonstack(size_t sp)
597 struct thread *td = curthread;
599 return ((td->td_pflags & TDP_ALTSTACK) ?
600 #if defined(COMPAT_43)
601 ((td->td_sigstk.ss_size == 0) ?
602 (td->td_sigstk.ss_flags & SS_ONSTACK) :
603 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
605 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
614 if (sig > 0 && sig < NSIG)
615 return (sigproptbl[_SIG_IDX(sig)]);
620 sig_ffs(sigset_t *set)
624 for (i = 0; i < _SIG_WORDS; i++)
626 return (ffs(set->__bits[i]) + (i * 32));
637 kern_sigaction(td, sig, act, oact, flags)
640 struct sigaction *act, *oact;
644 struct proc *p = td->td_proc;
646 if (!_SIG_VALID(sig))
651 mtx_lock(&ps->ps_mtx);
653 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
655 if (SIGISMEMBER(ps->ps_sigonstack, sig))
656 oact->sa_flags |= SA_ONSTACK;
657 if (!SIGISMEMBER(ps->ps_sigintr, sig))
658 oact->sa_flags |= SA_RESTART;
659 if (SIGISMEMBER(ps->ps_sigreset, sig))
660 oact->sa_flags |= SA_RESETHAND;
661 if (SIGISMEMBER(ps->ps_signodefer, sig))
662 oact->sa_flags |= SA_NODEFER;
663 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
664 oact->sa_flags |= SA_SIGINFO;
666 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
668 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
669 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
670 oact->sa_flags |= SA_NOCLDSTOP;
671 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
672 oact->sa_flags |= SA_NOCLDWAIT;
675 if ((sig == SIGKILL || sig == SIGSTOP) &&
676 act->sa_handler != SIG_DFL) {
677 mtx_unlock(&ps->ps_mtx);
683 * Change setting atomically.
686 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
687 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
688 if (act->sa_flags & SA_SIGINFO) {
689 ps->ps_sigact[_SIG_IDX(sig)] =
690 (__sighandler_t *)act->sa_sigaction;
691 SIGADDSET(ps->ps_siginfo, sig);
693 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
694 SIGDELSET(ps->ps_siginfo, sig);
696 if (!(act->sa_flags & SA_RESTART))
697 SIGADDSET(ps->ps_sigintr, sig);
699 SIGDELSET(ps->ps_sigintr, sig);
700 if (act->sa_flags & SA_ONSTACK)
701 SIGADDSET(ps->ps_sigonstack, sig);
703 SIGDELSET(ps->ps_sigonstack, sig);
704 if (act->sa_flags & SA_RESETHAND)
705 SIGADDSET(ps->ps_sigreset, sig);
707 SIGDELSET(ps->ps_sigreset, sig);
708 if (act->sa_flags & SA_NODEFER)
709 SIGADDSET(ps->ps_signodefer, sig);
711 SIGDELSET(ps->ps_signodefer, sig);
712 if (sig == SIGCHLD) {
713 if (act->sa_flags & SA_NOCLDSTOP)
714 ps->ps_flag |= PS_NOCLDSTOP;
716 ps->ps_flag &= ~PS_NOCLDSTOP;
717 if (act->sa_flags & SA_NOCLDWAIT) {
719 * Paranoia: since SA_NOCLDWAIT is implemented
720 * by reparenting the dying child to PID 1 (and
721 * trust it to reap the zombie), PID 1 itself
722 * is forbidden to set SA_NOCLDWAIT.
725 ps->ps_flag &= ~PS_NOCLDWAIT;
727 ps->ps_flag |= PS_NOCLDWAIT;
729 ps->ps_flag &= ~PS_NOCLDWAIT;
730 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
731 ps->ps_flag |= PS_CLDSIGIGN;
733 ps->ps_flag &= ~PS_CLDSIGIGN;
736 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
737 * and for signals set to SIG_DFL where the default is to
738 * ignore. However, don't put SIGCONT in ps_sigignore, as we
739 * have to restart the process.
741 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
742 (sigprop(sig) & SA_IGNORE &&
743 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
744 /* never to be seen again */
745 sigqueue_delete_proc(p, sig);
747 /* easier in psignal */
748 SIGADDSET(ps->ps_sigignore, sig);
749 SIGDELSET(ps->ps_sigcatch, sig);
751 SIGDELSET(ps->ps_sigignore, sig);
752 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
753 SIGDELSET(ps->ps_sigcatch, sig);
755 SIGADDSET(ps->ps_sigcatch, sig);
757 #ifdef COMPAT_FREEBSD4
758 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
759 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
760 (flags & KSA_FREEBSD4) == 0)
761 SIGDELSET(ps->ps_freebsd4, sig);
763 SIGADDSET(ps->ps_freebsd4, sig);
766 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
767 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
768 (flags & KSA_OSIGSET) == 0)
769 SIGDELSET(ps->ps_osigset, sig);
771 SIGADDSET(ps->ps_osigset, sig);
774 mtx_unlock(&ps->ps_mtx);
779 #ifndef _SYS_SYSPROTO_H_
780 struct sigaction_args {
782 struct sigaction *act;
783 struct sigaction *oact;
787 sys_sigaction(td, uap)
789 register struct sigaction_args *uap;
791 struct sigaction act, oact;
792 register struct sigaction *actp, *oactp;
795 actp = (uap->act != NULL) ? &act : NULL;
796 oactp = (uap->oact != NULL) ? &oact : NULL;
798 error = copyin(uap->act, actp, sizeof(act));
802 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
804 error = copyout(oactp, uap->oact, sizeof(oact));
808 #ifdef COMPAT_FREEBSD4
809 #ifndef _SYS_SYSPROTO_H_
810 struct freebsd4_sigaction_args {
812 struct sigaction *act;
813 struct sigaction *oact;
817 freebsd4_sigaction(td, uap)
819 register struct freebsd4_sigaction_args *uap;
821 struct sigaction act, oact;
822 register struct sigaction *actp, *oactp;
826 actp = (uap->act != NULL) ? &act : NULL;
827 oactp = (uap->oact != NULL) ? &oact : NULL;
829 error = copyin(uap->act, actp, sizeof(act));
833 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
835 error = copyout(oactp, uap->oact, sizeof(oact));
838 #endif /* COMAPT_FREEBSD4 */
840 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
841 #ifndef _SYS_SYSPROTO_H_
842 struct osigaction_args {
844 struct osigaction *nsa;
845 struct osigaction *osa;
851 register struct osigaction_args *uap;
853 struct osigaction sa;
854 struct sigaction nsa, osa;
855 register struct sigaction *nsap, *osap;
858 if (uap->signum <= 0 || uap->signum >= ONSIG)
861 nsap = (uap->nsa != NULL) ? &nsa : NULL;
862 osap = (uap->osa != NULL) ? &osa : NULL;
865 error = copyin(uap->nsa, &sa, sizeof(sa));
868 nsap->sa_handler = sa.sa_handler;
869 nsap->sa_flags = sa.sa_flags;
870 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
872 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
873 if (osap && !error) {
874 sa.sa_handler = osap->sa_handler;
875 sa.sa_flags = osap->sa_flags;
876 SIG2OSIG(osap->sa_mask, sa.sa_mask);
877 error = copyout(&sa, uap->osa, sizeof(sa));
882 #if !defined(__i386__)
883 /* Avoid replicating the same stub everywhere */
887 struct osigreturn_args *uap;
890 return (nosys(td, (struct nosys_args *)uap));
893 #endif /* COMPAT_43 */
896 * Initialize signal state for process 0;
897 * set to ignore signals that are ignored by default.
908 mtx_lock(&ps->ps_mtx);
909 for (i = 1; i <= NSIG; i++)
910 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
911 SIGADDSET(ps->ps_sigignore, i);
912 mtx_unlock(&ps->ps_mtx);
917 * Reset signals for an exec of the specified process.
920 execsigs(struct proc *p)
927 * Reset caught signals. Held signals remain held
928 * through td_sigmask (unless they were caught,
929 * and are now ignored by default).
931 PROC_LOCK_ASSERT(p, MA_OWNED);
932 td = FIRST_THREAD_IN_PROC(p);
934 mtx_lock(&ps->ps_mtx);
935 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
936 sig = sig_ffs(&ps->ps_sigcatch);
937 SIGDELSET(ps->ps_sigcatch, sig);
938 if (sigprop(sig) & SA_IGNORE) {
940 SIGADDSET(ps->ps_sigignore, sig);
941 sigqueue_delete_proc(p, sig);
943 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
946 * Reset stack state to the user stack.
947 * Clear set of signals caught on the signal stack.
949 td->td_sigstk.ss_flags = SS_DISABLE;
950 td->td_sigstk.ss_size = 0;
951 td->td_sigstk.ss_sp = 0;
952 td->td_pflags &= ~TDP_ALTSTACK;
954 * Reset no zombies if child dies flag as Solaris does.
956 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
957 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
958 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
959 mtx_unlock(&ps->ps_mtx);
965 * Manipulate signal mask.
968 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
971 sigset_t new_block, oset1;
976 if (!(flags & SIGPROCMASK_PROC_LOCKED))
979 *oset = td->td_sigmask;
986 oset1 = td->td_sigmask;
987 SIGSETOR(td->td_sigmask, *set);
988 new_block = td->td_sigmask;
989 SIGSETNAND(new_block, oset1);
992 SIGSETNAND(td->td_sigmask, *set);
997 oset1 = td->td_sigmask;
998 if (flags & SIGPROCMASK_OLD)
999 SIGSETLO(td->td_sigmask, *set);
1001 td->td_sigmask = *set;
1002 new_block = td->td_sigmask;
1003 SIGSETNAND(new_block, oset1);
1012 * The new_block set contains signals that were not previously
1013 * blocked, but are blocked now.
1015 * In case we block any signal that was not previously blocked
1016 * for td, and process has the signal pending, try to schedule
1017 * signal delivery to some thread that does not block the
1018 * signal, possibly waking it up.
1020 if (p->p_numthreads != 1)
1021 reschedule_signals(p, new_block, flags);
1025 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1030 #ifndef _SYS_SYSPROTO_H_
1031 struct sigprocmask_args {
1033 const sigset_t *set;
1038 sys_sigprocmask(td, uap)
1039 register struct thread *td;
1040 struct sigprocmask_args *uap;
1043 sigset_t *setp, *osetp;
1046 setp = (uap->set != NULL) ? &set : NULL;
1047 osetp = (uap->oset != NULL) ? &oset : NULL;
1049 error = copyin(uap->set, setp, sizeof(set));
1053 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1054 if (osetp && !error) {
1055 error = copyout(osetp, uap->oset, sizeof(oset));
1060 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1061 #ifndef _SYS_SYSPROTO_H_
1062 struct osigprocmask_args {
1068 osigprocmask(td, uap)
1069 register struct thread *td;
1070 struct osigprocmask_args *uap;
1075 OSIG2SIG(uap->mask, set);
1076 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1077 SIG2OSIG(oset, td->td_retval[0]);
1080 #endif /* COMPAT_43 */
1083 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1089 error = copyin(uap->set, &set, sizeof(set));
1091 td->td_retval[0] = error;
1095 error = kern_sigtimedwait(td, set, &ksi, NULL);
1097 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1099 if (error == ERESTART)
1101 td->td_retval[0] = error;
1105 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1106 td->td_retval[0] = error;
1111 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1114 struct timespec *timeout;
1120 error = copyin(uap->timeout, &ts, sizeof(ts));
1128 error = copyin(uap->set, &set, sizeof(set));
1132 error = kern_sigtimedwait(td, set, &ksi, timeout);
1137 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1140 td->td_retval[0] = ksi.ksi_signo;
1145 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1151 error = copyin(uap->set, &set, sizeof(set));
1155 error = kern_sigtimedwait(td, set, &ksi, NULL);
1160 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1163 td->td_retval[0] = ksi.ksi_signo;
1168 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1169 struct timespec *timeout)
1172 sigset_t saved_mask, new_block;
1174 int error, sig, timo, timevalid = 0;
1175 struct timespec rts, ets, ts;
1183 if (timeout != NULL) {
1184 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1186 getnanouptime(&rts);
1188 timespecadd(&ets, timeout);
1192 /* Some signals can not be waited for. */
1193 SIG_CANTMASK(waitset);
1196 saved_mask = td->td_sigmask;
1197 SIGSETNAND(td->td_sigmask, waitset);
1199 mtx_lock(&ps->ps_mtx);
1200 sig = cursig(td, SIG_STOP_ALLOWED);
1201 mtx_unlock(&ps->ps_mtx);
1202 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1203 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1204 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1214 * POSIX says this must be checked after looking for pending
1217 if (timeout != NULL) {
1222 getnanouptime(&rts);
1223 if (timespeccmp(&rts, &ets, >=)) {
1228 timespecsub(&ts, &rts);
1229 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1235 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1237 if (timeout != NULL) {
1238 if (error == ERESTART) {
1239 /* Timeout can not be restarted. */
1241 } else if (error == EAGAIN) {
1242 /* We will calculate timeout by ourself. */
1248 new_block = saved_mask;
1249 SIGSETNAND(new_block, td->td_sigmask);
1250 td->td_sigmask = saved_mask;
1252 * Fewer signals can be delivered to us, reschedule signal
1255 if (p->p_numthreads != 1)
1256 reschedule_signals(p, new_block, 0);
1259 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1261 if (ksi->ksi_code == SI_TIMER)
1262 itimer_accept(p, ksi->ksi_timerid, ksi);
1265 if (KTRPOINT(td, KTR_PSIG)) {
1268 mtx_lock(&ps->ps_mtx);
1269 action = ps->ps_sigact[_SIG_IDX(sig)];
1270 mtx_unlock(&ps->ps_mtx);
1271 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1281 #ifndef _SYS_SYSPROTO_H_
1282 struct sigpending_args {
1287 sys_sigpending(td, uap)
1289 struct sigpending_args *uap;
1291 struct proc *p = td->td_proc;
1295 pending = p->p_sigqueue.sq_signals;
1296 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1298 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1301 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1302 #ifndef _SYS_SYSPROTO_H_
1303 struct osigpending_args {
1308 osigpending(td, uap)
1310 struct osigpending_args *uap;
1312 struct proc *p = td->td_proc;
1316 pending = p->p_sigqueue.sq_signals;
1317 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1319 SIG2OSIG(pending, td->td_retval[0]);
1322 #endif /* COMPAT_43 */
1324 #if defined(COMPAT_43)
1326 * Generalized interface signal handler, 4.3-compatible.
1328 #ifndef _SYS_SYSPROTO_H_
1329 struct osigvec_args {
1339 register struct osigvec_args *uap;
1342 struct sigaction nsa, osa;
1343 register struct sigaction *nsap, *osap;
1346 if (uap->signum <= 0 || uap->signum >= ONSIG)
1348 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1349 osap = (uap->osv != NULL) ? &osa : NULL;
1351 error = copyin(uap->nsv, &vec, sizeof(vec));
1354 nsap->sa_handler = vec.sv_handler;
1355 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1356 nsap->sa_flags = vec.sv_flags;
1357 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1359 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1360 if (osap && !error) {
1361 vec.sv_handler = osap->sa_handler;
1362 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1363 vec.sv_flags = osap->sa_flags;
1364 vec.sv_flags &= ~SA_NOCLDWAIT;
1365 vec.sv_flags ^= SA_RESTART;
1366 error = copyout(&vec, uap->osv, sizeof(vec));
1371 #ifndef _SYS_SYSPROTO_H_
1372 struct osigblock_args {
1378 register struct thread *td;
1379 struct osigblock_args *uap;
1383 OSIG2SIG(uap->mask, set);
1384 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1385 SIG2OSIG(oset, td->td_retval[0]);
1389 #ifndef _SYS_SYSPROTO_H_
1390 struct osigsetmask_args {
1395 osigsetmask(td, uap)
1397 struct osigsetmask_args *uap;
1401 OSIG2SIG(uap->mask, set);
1402 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1403 SIG2OSIG(oset, td->td_retval[0]);
1406 #endif /* COMPAT_43 */
1409 * Suspend calling thread until signal, providing mask to be set in the
1412 #ifndef _SYS_SYSPROTO_H_
1413 struct sigsuspend_args {
1414 const sigset_t *sigmask;
1419 sys_sigsuspend(td, uap)
1421 struct sigsuspend_args *uap;
1426 error = copyin(uap->sigmask, &mask, sizeof(mask));
1429 return (kern_sigsuspend(td, mask));
1433 kern_sigsuspend(struct thread *td, sigset_t mask)
1435 struct proc *p = td->td_proc;
1439 * When returning from sigsuspend, we want
1440 * the old mask to be restored after the
1441 * signal handler has finished. Thus, we
1442 * save it here and mark the sigacts structure
1446 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1447 SIGPROCMASK_PROC_LOCKED);
1448 td->td_pflags |= TDP_OLDMASK;
1451 * Process signals now. Otherwise, we can get spurious wakeup
1452 * due to signal entered process queue, but delivered to other
1453 * thread. But sigsuspend should return only on signal
1456 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1457 for (has_sig = 0; !has_sig;) {
1458 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1461 thread_suspend_check(0);
1462 mtx_lock(&p->p_sigacts->ps_mtx);
1463 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
1464 has_sig += postsig(sig);
1465 mtx_unlock(&p->p_sigacts->ps_mtx);
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)
1606 sx_slock(&allproc_lock);
1607 FOREACH_PROC_IN_SYSTEM(p) {
1609 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1610 p == td->td_proc || p->p_state == PRS_NEW) {
1614 if (p_cansignal(td, p, sig) == 0) {
1617 pksignal(p, sig, ksi);
1621 sx_sunlock(&allproc_lock);
1623 sx_slock(&proctree_lock);
1626 * zero pgid means send to my process group.
1628 pgrp = td->td_proc->p_pgrp;
1631 pgrp = pgfind(pgid);
1633 sx_sunlock(&proctree_lock);
1637 sx_sunlock(&proctree_lock);
1638 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1640 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1641 p->p_state == PRS_NEW) {
1645 if (p_cansignal(td, p, sig) == 0) {
1648 pksignal(p, sig, ksi);
1654 return (nfound ? 0 : ESRCH);
1657 #ifndef _SYS_SYSPROTO_H_
1665 sys_kill(struct thread *td, struct kill_args *uap)
1671 AUDIT_ARG_SIGNUM(uap->signum);
1672 AUDIT_ARG_PID(uap->pid);
1673 if ((u_int)uap->signum > _SIG_MAXSIG)
1676 ksiginfo_init(&ksi);
1677 ksi.ksi_signo = uap->signum;
1678 ksi.ksi_code = SI_USER;
1679 ksi.ksi_pid = td->td_proc->p_pid;
1680 ksi.ksi_uid = td->td_ucred->cr_ruid;
1683 /* kill single process */
1684 if ((p = pfind(uap->pid)) == NULL) {
1685 if ((p = zpfind(uap->pid)) == NULL)
1688 AUDIT_ARG_PROCESS(p);
1689 error = p_cansignal(td, p, uap->signum);
1690 if (error == 0 && uap->signum)
1691 pksignal(p, uap->signum, &ksi);
1696 case -1: /* broadcast signal */
1697 return (killpg1(td, uap->signum, 0, 1, &ksi));
1698 case 0: /* signal own process group */
1699 return (killpg1(td, uap->signum, 0, 0, &ksi));
1700 default: /* negative explicit process group */
1701 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1709 struct pdkill_args *uap;
1715 AUDIT_ARG_SIGNUM(uap->signum);
1716 AUDIT_ARG_FD(uap->fd);
1717 if ((u_int)uap->signum > _SIG_MAXSIG)
1720 error = procdesc_find(td, uap->fd, CAP_PDKILL, &p);
1723 AUDIT_ARG_PROCESS(p);
1724 error = p_cansignal(td, p, uap->signum);
1725 if (error == 0 && uap->signum)
1726 kern_psignal(p, uap->signum);
1734 #if defined(COMPAT_43)
1735 #ifndef _SYS_SYSPROTO_H_
1736 struct okillpg_args {
1743 okillpg(struct thread *td, struct okillpg_args *uap)
1747 AUDIT_ARG_SIGNUM(uap->signum);
1748 AUDIT_ARG_PID(uap->pgid);
1749 if ((u_int)uap->signum > _SIG_MAXSIG)
1752 ksiginfo_init(&ksi);
1753 ksi.ksi_signo = uap->signum;
1754 ksi.ksi_code = SI_USER;
1755 ksi.ksi_pid = td->td_proc->p_pid;
1756 ksi.ksi_uid = td->td_ucred->cr_ruid;
1757 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1759 #endif /* COMPAT_43 */
1761 #ifndef _SYS_SYSPROTO_H_
1762 struct sigqueue_args {
1765 /* union sigval */ void *value;
1769 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1775 if ((u_int)uap->signum > _SIG_MAXSIG)
1779 * Specification says sigqueue can only send signal to
1785 if ((p = pfind(uap->pid)) == NULL) {
1786 if ((p = zpfind(uap->pid)) == NULL)
1789 error = p_cansignal(td, p, uap->signum);
1790 if (error == 0 && uap->signum != 0) {
1791 ksiginfo_init(&ksi);
1792 ksi.ksi_flags = KSI_SIGQ;
1793 ksi.ksi_signo = uap->signum;
1794 ksi.ksi_code = SI_QUEUE;
1795 ksi.ksi_pid = td->td_proc->p_pid;
1796 ksi.ksi_uid = td->td_ucred->cr_ruid;
1797 ksi.ksi_value.sival_ptr = uap->value;
1798 error = pksignal(p, ksi.ksi_signo, &ksi);
1805 * Send a signal to a process group.
1808 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1813 sx_slock(&proctree_lock);
1814 pgrp = pgfind(pgid);
1815 sx_sunlock(&proctree_lock);
1817 pgsignal(pgrp, sig, 0, ksi);
1824 * Send a signal to a process group. If checktty is 1,
1825 * limit to members which have a controlling terminal.
1828 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1833 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1834 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1836 if (p->p_state == PRS_NORMAL &&
1837 (checkctty == 0 || p->p_flag & P_CONTROLT))
1838 pksignal(p, sig, ksi);
1845 * Send a signal caused by a trap to the current thread. If it will be
1846 * caught immediately, deliver it with correct code. Otherwise, post it
1850 trapsignal(struct thread *td, ksiginfo_t *ksi)
1859 sig = ksi->ksi_signo;
1860 code = ksi->ksi_code;
1861 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1865 mtx_lock(&ps->ps_mtx);
1866 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1867 !SIGISMEMBER(td->td_sigmask, sig)) {
1868 td->td_ru.ru_nsignals++;
1870 if (KTRPOINT(curthread, KTR_PSIG))
1871 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1872 &td->td_sigmask, code);
1874 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1875 ksi, &td->td_sigmask);
1876 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1877 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1878 SIGADDSET(mask, sig);
1879 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1880 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1881 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1883 * See kern_sigaction() for origin of this code.
1885 SIGDELSET(ps->ps_sigcatch, sig);
1886 if (sig != SIGCONT &&
1887 sigprop(sig) & SA_IGNORE)
1888 SIGADDSET(ps->ps_sigignore, sig);
1889 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1891 mtx_unlock(&ps->ps_mtx);
1894 * Avoid a possible infinite loop if the thread
1895 * masking the signal or process is ignoring the
1898 if (kern_forcesigexit &&
1899 (SIGISMEMBER(td->td_sigmask, sig) ||
1900 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1901 SIGDELSET(td->td_sigmask, sig);
1902 SIGDELSET(ps->ps_sigcatch, sig);
1903 SIGDELSET(ps->ps_sigignore, sig);
1904 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1906 mtx_unlock(&ps->ps_mtx);
1907 p->p_code = code; /* XXX for core dump/debugger */
1908 p->p_sig = sig; /* XXX to verify code */
1909 tdsendsignal(p, td, sig, ksi);
1914 static struct thread *
1915 sigtd(struct proc *p, int sig, int prop)
1917 struct thread *td, *signal_td;
1919 PROC_LOCK_ASSERT(p, MA_OWNED);
1922 * Check if current thread can handle the signal without
1923 * switching context to another thread.
1925 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1928 FOREACH_THREAD_IN_PROC(p, td) {
1929 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1934 if (signal_td == NULL)
1935 signal_td = FIRST_THREAD_IN_PROC(p);
1940 * Send the signal to the process. If the signal has an action, the action
1941 * is usually performed by the target process rather than the caller; we add
1942 * the signal to the set of pending signals for the process.
1945 * o When a stop signal is sent to a sleeping process that takes the
1946 * default action, the process is stopped without awakening it.
1947 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1948 * regardless of the signal action (eg, blocked or ignored).
1950 * Other ignored signals are discarded immediately.
1952 * NB: This function may be entered from the debugger via the "kill" DDB
1953 * command. There is little that can be done to mitigate the possibly messy
1954 * side effects of this unwise possibility.
1957 kern_psignal(struct proc *p, int sig)
1961 ksiginfo_init(&ksi);
1962 ksi.ksi_signo = sig;
1963 ksi.ksi_code = SI_KERNEL;
1964 (void) tdsendsignal(p, NULL, sig, &ksi);
1968 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1971 return (tdsendsignal(p, NULL, sig, ksi));
1974 /* Utility function for finding a thread to send signal event to. */
1976 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
1980 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1981 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
1993 tdsignal(struct thread *td, int sig)
1997 ksiginfo_init(&ksi);
1998 ksi.ksi_signo = sig;
1999 ksi.ksi_code = SI_KERNEL;
2000 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2004 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2007 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2011 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2014 sigqueue_t *sigqueue;
2021 MPASS(td == NULL || p == td->td_proc);
2022 PROC_LOCK_ASSERT(p, MA_OWNED);
2024 if (!_SIG_VALID(sig))
2025 panic("%s(): invalid signal %d", __func__, sig);
2027 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2030 * IEEE Std 1003.1-2001: return success when killing a zombie.
2032 if (p->p_state == PRS_ZOMBIE) {
2033 if (ksi && (ksi->ksi_flags & KSI_INS))
2034 ksiginfo_tryfree(ksi);
2039 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2040 prop = sigprop(sig);
2043 td = sigtd(p, sig, prop);
2044 sigqueue = &p->p_sigqueue;
2046 KASSERT(td->td_proc == p, ("invalid thread"));
2047 sigqueue = &td->td_sigqueue;
2050 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2053 * If the signal is being ignored,
2054 * then we forget about it immediately.
2055 * (Note: we don't set SIGCONT in ps_sigignore,
2056 * and if it is set to SIG_IGN,
2057 * action will be SIG_DFL here.)
2059 mtx_lock(&ps->ps_mtx);
2060 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2061 SDT_PROBE(proc, kernel, , signal_discard, ps, td, sig, 0, 0 );
2063 mtx_unlock(&ps->ps_mtx);
2064 if (ksi && (ksi->ksi_flags & KSI_INS))
2065 ksiginfo_tryfree(ksi);
2068 if (SIGISMEMBER(td->td_sigmask, sig))
2070 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2074 if (SIGISMEMBER(ps->ps_sigintr, sig))
2078 mtx_unlock(&ps->ps_mtx);
2081 sigqueue_delete_stopmask_proc(p);
2082 else if (prop & SA_STOP) {
2084 * If sending a tty stop signal to a member of an orphaned
2085 * process group, discard the signal here if the action
2086 * is default; don't stop the process below if sleeping,
2087 * and don't clear any pending SIGCONT.
2089 if ((prop & SA_TTYSTOP) &&
2090 (p->p_pgrp->pg_jobc == 0) &&
2091 (action == SIG_DFL)) {
2092 if (ksi && (ksi->ksi_flags & KSI_INS))
2093 ksiginfo_tryfree(ksi);
2096 sigqueue_delete_proc(p, SIGCONT);
2097 if (p->p_flag & P_CONTINUED) {
2098 p->p_flag &= ~P_CONTINUED;
2099 PROC_LOCK(p->p_pptr);
2100 sigqueue_take(p->p_ksi);
2101 PROC_UNLOCK(p->p_pptr);
2105 ret = sigqueue_add(sigqueue, sig, ksi);
2110 * Defer further processing for signals which are held,
2111 * except that stopped processes must be continued by SIGCONT.
2113 if (action == SIG_HOLD &&
2114 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2117 * SIGKILL: Remove procfs STOPEVENTs.
2119 if (sig == SIGKILL) {
2120 /* from procfs_ioctl.c: PIOCBIC */
2122 /* from procfs_ioctl.c: PIOCCONT */
2127 * Some signals have a process-wide effect and a per-thread
2128 * component. Most processing occurs when the process next
2129 * tries to cross the user boundary, however there are some
2130 * times when processing needs to be done immediatly, such as
2131 * waking up threads so that they can cross the user boundary.
2132 * We try do the per-process part here.
2134 if (P_SHOULDSTOP(p)) {
2135 if (sig == SIGKILL) {
2137 * If traced process is already stopped,
2138 * then no further action is necessary.
2140 if (p->p_flag & P_TRACED)
2143 * SIGKILL sets process running.
2144 * It will die elsewhere.
2145 * All threads must be restarted.
2147 p->p_flag &= ~P_STOPPED_SIG;
2151 if (prop & SA_CONT) {
2153 * If traced process is already stopped,
2154 * then no further action is necessary.
2156 if (p->p_flag & P_TRACED)
2159 * If SIGCONT is default (or ignored), we continue the
2160 * process but don't leave the signal in sigqueue as
2161 * it has no further action. If SIGCONT is held, we
2162 * continue the process and leave the signal in
2163 * sigqueue. If the process catches SIGCONT, let it
2164 * handle the signal itself. If it isn't waiting on
2165 * an event, it goes back to run state.
2166 * Otherwise, process goes back to sleep state.
2168 p->p_flag &= ~P_STOPPED_SIG;
2170 if (p->p_numthreads == p->p_suspcount) {
2172 p->p_flag |= P_CONTINUED;
2173 p->p_xstat = SIGCONT;
2174 PROC_LOCK(p->p_pptr);
2175 childproc_continued(p);
2176 PROC_UNLOCK(p->p_pptr);
2179 if (action == SIG_DFL) {
2180 thread_unsuspend(p);
2182 sigqueue_delete(sigqueue, sig);
2185 if (action == SIG_CATCH) {
2187 * The process wants to catch it so it needs
2188 * to run at least one thread, but which one?
2194 * The signal is not ignored or caught.
2196 thread_unsuspend(p);
2201 if (prop & SA_STOP) {
2203 * If traced process is already stopped,
2204 * then no further action is necessary.
2206 if (p->p_flag & P_TRACED)
2209 * Already stopped, don't need to stop again
2210 * (If we did the shell could get confused).
2211 * Just make sure the signal STOP bit set.
2213 p->p_flag |= P_STOPPED_SIG;
2214 sigqueue_delete(sigqueue, sig);
2219 * All other kinds of signals:
2220 * If a thread is sleeping interruptibly, simulate a
2221 * wakeup so that when it is continued it will be made
2222 * runnable and can look at the signal. However, don't make
2223 * the PROCESS runnable, leave it stopped.
2224 * It may run a bit until it hits a thread_suspend_check().
2229 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2230 wakeup_swapper = sleepq_abort(td, intrval);
2237 * Mutexes are short lived. Threads waiting on them will
2238 * hit thread_suspend_check() soon.
2240 } else if (p->p_state == PRS_NORMAL) {
2241 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2242 tdsigwakeup(td, sig, action, intrval);
2246 MPASS(action == SIG_DFL);
2248 if (prop & SA_STOP) {
2249 if (p->p_flag & P_PPWAIT)
2251 p->p_flag |= P_STOPPED_SIG;
2254 sig_suspend_threads(td, p, 1);
2255 if (p->p_numthreads == p->p_suspcount) {
2257 * only thread sending signal to another
2258 * process can reach here, if thread is sending
2259 * signal to its process, because thread does
2260 * not suspend itself here, p_numthreads
2261 * should never be equal to p_suspcount.
2265 sigqueue_delete_proc(p, p->p_xstat);
2271 /* Not in "NORMAL" state. discard the signal. */
2272 sigqueue_delete(sigqueue, sig);
2277 * The process is not stopped so we need to apply the signal to all the
2281 tdsigwakeup(td, sig, action, intrval);
2283 thread_unsuspend(p);
2286 /* If we jump here, proc slock should not be owned. */
2287 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2292 * The force of a signal has been directed against a single
2293 * thread. We need to see what we can do about knocking it
2294 * out of any sleep it may be in etc.
2297 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2299 struct proc *p = td->td_proc;
2304 PROC_LOCK_ASSERT(p, MA_OWNED);
2305 prop = sigprop(sig);
2310 * Bring the priority of a thread up if we want it to get
2311 * killed in this lifetime.
2313 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2314 sched_prio(td, PUSER);
2315 if (TD_ON_SLEEPQ(td)) {
2317 * If thread is sleeping uninterruptibly
2318 * we can't interrupt the sleep... the signal will
2319 * be noticed when the process returns through
2320 * trap() or syscall().
2322 if ((td->td_flags & TDF_SINTR) == 0)
2325 * If SIGCONT is default (or ignored) and process is
2326 * asleep, we are finished; the process should not
2329 if ((prop & SA_CONT) && action == SIG_DFL) {
2332 sigqueue_delete(&p->p_sigqueue, sig);
2334 * It may be on either list in this state.
2335 * Remove from both for now.
2337 sigqueue_delete(&td->td_sigqueue, sig);
2342 * Give low priority threads a better chance to run.
2344 if (td->td_priority > PUSER)
2345 sched_prio(td, PUSER);
2347 wakeup_swapper = sleepq_abort(td, intrval);
2350 * Other states do nothing with the signal immediately,
2351 * other than kicking ourselves if we are running.
2352 * It will either never be noticed, or noticed very soon.
2355 if (TD_IS_RUNNING(td) && td != curthread)
2367 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2372 PROC_LOCK_ASSERT(p, MA_OWNED);
2373 PROC_SLOCK_ASSERT(p, MA_OWNED);
2376 FOREACH_THREAD_IN_PROC(p, td2) {
2378 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2379 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2380 (td2->td_flags & TDF_SINTR)) {
2381 if (td2->td_flags & TDF_SBDRY) {
2382 if (TD_IS_SUSPENDED(td2))
2384 thread_unsuspend_one(td2);
2385 if (TD_ON_SLEEPQ(td2))
2387 sleepq_abort(td2, ERESTART);
2388 } else if (!TD_IS_SUSPENDED(td2)) {
2389 thread_suspend_one(td2);
2391 } else if (!TD_IS_SUSPENDED(td2)) {
2392 if (sending || td != td2)
2393 td2->td_flags |= TDF_ASTPENDING;
2395 if (TD_IS_RUNNING(td2) && td2 != td)
2396 forward_signal(td2);
2406 ptracestop(struct thread *td, int sig)
2408 struct proc *p = td->td_proc;
2410 PROC_LOCK_ASSERT(p, MA_OWNED);
2411 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2412 &p->p_mtx.lock_object, "Stopping for traced signal");
2414 td->td_dbgflags |= TDB_XSIG;
2417 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2418 if (p->p_flag & P_SINGLE_EXIT) {
2419 td->td_dbgflags &= ~TDB_XSIG;
2424 * Just make wait() to work, the last stopped thread
2429 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2430 sig_suspend_threads(td, p, 0);
2431 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2432 td->td_dbgflags &= ~TDB_STOPATFORK;
2433 cv_broadcast(&p->p_dbgwait);
2436 thread_suspend_switch(td);
2437 if (!(p->p_flag & P_TRACED)) {
2440 if (td->td_dbgflags & TDB_SUSPEND) {
2441 if (p->p_flag & P_SINGLE_EXIT)
2447 return (td->td_xsig);
2451 reschedule_signals(struct proc *p, sigset_t block, int flags)
2457 PROC_LOCK_ASSERT(p, MA_OWNED);
2458 if (SIGISEMPTY(p->p_siglist))
2461 SIGSETAND(block, p->p_siglist);
2462 while ((sig = sig_ffs(&block)) != 0) {
2463 SIGDELSET(block, sig);
2464 td = sigtd(p, sig, 0);
2466 if (!(flags & SIGPROCMASK_PS_LOCKED))
2467 mtx_lock(&ps->ps_mtx);
2468 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2469 tdsigwakeup(td, sig, SIG_CATCH,
2470 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2472 if (!(flags & SIGPROCMASK_PS_LOCKED))
2473 mtx_unlock(&ps->ps_mtx);
2478 tdsigcleanup(struct thread *td)
2484 PROC_LOCK_ASSERT(p, MA_OWNED);
2486 sigqueue_flush(&td->td_sigqueue);
2487 if (p->p_numthreads == 1)
2491 * Since we cannot handle signals, notify signal post code
2492 * about this by filling the sigmask.
2494 * Also, if needed, wake up thread(s) that do not block the
2495 * same signals as the exiting thread, since the thread might
2496 * have been selected for delivery and woken up.
2498 SIGFILLSET(unblocked);
2499 SIGSETNAND(unblocked, td->td_sigmask);
2500 SIGFILLSET(td->td_sigmask);
2501 reschedule_signals(p, unblocked, 0);
2506 * If the current process has received a signal (should be caught or cause
2507 * termination, should interrupt current syscall), return the signal number.
2508 * Stop signals with default action are processed immediately, then cleared;
2509 * they aren't returned. This is checked after each entry to the system for
2510 * a syscall or trap (though this can usually be done without calling issignal
2511 * by checking the pending signal masks in cursig.) The normal call
2514 * while (sig = cursig(curthread))
2518 issignal(struct thread *td, int stop_allowed)
2522 struct sigqueue *queue;
2523 sigset_t sigpending;
2524 int sig, prop, newsig;
2528 mtx_assert(&ps->ps_mtx, MA_OWNED);
2529 PROC_LOCK_ASSERT(p, MA_OWNED);
2531 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2533 sigpending = td->td_sigqueue.sq_signals;
2534 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2535 SIGSETNAND(sigpending, td->td_sigmask);
2537 if (p->p_flag & P_PPWAIT)
2538 SIG_STOPSIGMASK(sigpending);
2539 if (SIGISEMPTY(sigpending)) /* no signal to send */
2541 sig = sig_ffs(&sigpending);
2543 if (p->p_stops & S_SIG) {
2544 mtx_unlock(&ps->ps_mtx);
2545 stopevent(p, S_SIG, sig);
2546 mtx_lock(&ps->ps_mtx);
2550 * We should see pending but ignored signals
2551 * only if P_TRACED was on when they were posted.
2553 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2554 sigqueue_delete(&td->td_sigqueue, sig);
2555 sigqueue_delete(&p->p_sigqueue, sig);
2558 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2560 * If traced, always stop.
2561 * Remove old signal from queue before the stop.
2562 * XXX shrug off debugger, it causes siginfo to
2565 queue = &td->td_sigqueue;
2566 td->td_dbgksi.ksi_signo = 0;
2567 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2568 queue = &p->p_sigqueue;
2569 sigqueue_get(queue, sig, &td->td_dbgksi);
2572 mtx_unlock(&ps->ps_mtx);
2573 newsig = ptracestop(td, sig);
2574 mtx_lock(&ps->ps_mtx);
2576 if (sig != newsig) {
2579 * If parent wants us to take the signal,
2580 * then it will leave it in p->p_xstat;
2581 * otherwise we just look for signals again.
2588 * Put the new signal into td_sigqueue. If the
2589 * signal is being masked, look for other signals.
2591 sigqueue_add(queue, sig, NULL);
2592 if (SIGISMEMBER(td->td_sigmask, sig))
2596 if (td->td_dbgksi.ksi_signo != 0) {
2597 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2598 if (sigqueue_add(&td->td_sigqueue, sig,
2599 &td->td_dbgksi) != 0)
2600 td->td_dbgksi.ksi_signo = 0;
2602 if (td->td_dbgksi.ksi_signo == 0)
2603 sigqueue_add(&td->td_sigqueue, sig,
2608 * If the traced bit got turned off, go back up
2609 * to the top to rescan signals. This ensures
2610 * that p_sig* and p_sigact are consistent.
2612 if ((p->p_flag & P_TRACED) == 0)
2616 prop = sigprop(sig);
2619 * Decide whether the signal should be returned.
2620 * Return the signal's number, or fall through
2621 * to clear it from the pending mask.
2623 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2625 case (intptr_t)SIG_DFL:
2627 * Don't take default actions on system processes.
2629 if (p->p_pid <= 1) {
2632 * Are you sure you want to ignore SIGSEGV
2635 printf("Process (pid %lu) got signal %d\n",
2636 (u_long)p->p_pid, sig);
2638 break; /* == ignore */
2641 * If there is a pending stop signal to process
2642 * with default action, stop here,
2643 * then clear the signal. However,
2644 * if process is member of an orphaned
2645 * process group, ignore tty stop signals.
2647 if (prop & SA_STOP) {
2648 if (p->p_flag & P_TRACED ||
2649 (p->p_pgrp->pg_jobc == 0 &&
2651 break; /* == ignore */
2653 /* Ignore, but do not drop the stop signal. */
2654 if (stop_allowed != SIG_STOP_ALLOWED)
2656 mtx_unlock(&ps->ps_mtx);
2657 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2658 &p->p_mtx.lock_object, "Catching SIGSTOP");
2659 p->p_flag |= P_STOPPED_SIG;
2662 sig_suspend_threads(td, p, 0);
2663 thread_suspend_switch(td);
2665 mtx_lock(&ps->ps_mtx);
2667 } else if (prop & SA_IGNORE) {
2669 * Except for SIGCONT, shouldn't get here.
2670 * Default action is to ignore; drop it.
2672 break; /* == ignore */
2677 case (intptr_t)SIG_IGN:
2679 * Masking above should prevent us ever trying
2680 * to take action on an ignored signal other
2681 * than SIGCONT, unless process is traced.
2683 if ((prop & SA_CONT) == 0 &&
2684 (p->p_flag & P_TRACED) == 0)
2685 printf("issignal\n");
2686 break; /* == ignore */
2690 * This signal has an action, let
2691 * postsig() process it.
2695 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2696 sigqueue_delete(&p->p_sigqueue, sig);
2702 thread_stopped(struct proc *p)
2706 PROC_LOCK_ASSERT(p, MA_OWNED);
2707 PROC_SLOCK_ASSERT(p, MA_OWNED);
2711 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2713 p->p_flag &= ~P_WAITED;
2714 PROC_LOCK(p->p_pptr);
2715 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2716 CLD_TRAPPED : CLD_STOPPED);
2717 PROC_UNLOCK(p->p_pptr);
2723 * Take the action for the specified signal
2724 * from the current set of pending signals.
2730 struct thread *td = curthread;
2731 register struct proc *p = td->td_proc;
2735 sigset_t returnmask, mask;
2737 KASSERT(sig != 0, ("postsig"));
2739 PROC_LOCK_ASSERT(p, MA_OWNED);
2741 mtx_assert(&ps->ps_mtx, MA_OWNED);
2742 ksiginfo_init(&ksi);
2743 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2744 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2746 ksi.ksi_signo = sig;
2747 if (ksi.ksi_code == SI_TIMER)
2748 itimer_accept(p, ksi.ksi_timerid, &ksi);
2749 action = ps->ps_sigact[_SIG_IDX(sig)];
2751 if (KTRPOINT(td, KTR_PSIG))
2752 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2753 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2755 if (p->p_stops & S_SIG) {
2756 mtx_unlock(&ps->ps_mtx);
2757 stopevent(p, S_SIG, sig);
2758 mtx_lock(&ps->ps_mtx);
2761 if (action == SIG_DFL) {
2763 * Default action, where the default is to kill
2764 * the process. (Other cases were ignored above.)
2766 mtx_unlock(&ps->ps_mtx);
2771 * If we get here, the signal must be caught.
2773 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2774 ("postsig action"));
2776 * Set the new mask value and also defer further
2777 * occurrences of this signal.
2779 * Special case: user has done a sigsuspend. Here the
2780 * current mask is not of interest, but rather the
2781 * mask from before the sigsuspend is what we want
2782 * restored after the signal processing is completed.
2784 if (td->td_pflags & TDP_OLDMASK) {
2785 returnmask = td->td_oldsigmask;
2786 td->td_pflags &= ~TDP_OLDMASK;
2788 returnmask = td->td_sigmask;
2790 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2791 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2792 SIGADDSET(mask, sig);
2793 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2794 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2796 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2798 * See kern_sigaction() for origin of this code.
2800 SIGDELSET(ps->ps_sigcatch, sig);
2801 if (sig != SIGCONT &&
2802 sigprop(sig) & SA_IGNORE)
2803 SIGADDSET(ps->ps_sigignore, sig);
2804 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2806 td->td_ru.ru_nsignals++;
2807 if (p->p_sig == sig) {
2811 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2817 * Kill the current process for stated reason.
2825 PROC_LOCK_ASSERT(p, MA_OWNED);
2826 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2827 p, p->p_pid, p->p_comm);
2828 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2829 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2830 p->p_flag |= P_WKILLED;
2831 kern_psignal(p, SIGKILL);
2835 * Force the current process to exit with the specified signal, dumping core
2836 * if appropriate. We bypass the normal tests for masked and caught signals,
2837 * allowing unrecoverable failures to terminate the process without changing
2838 * signal state. Mark the accounting record with the signal termination.
2839 * If dumping core, save the signal number for the debugger. Calls exit and
2847 struct proc *p = td->td_proc;
2849 PROC_LOCK_ASSERT(p, MA_OWNED);
2850 p->p_acflag |= AXSIG;
2852 * We must be single-threading to generate a core dump. This
2853 * ensures that the registers in the core file are up-to-date.
2854 * Also, the ELF dump handler assumes that the thread list doesn't
2855 * change out from under it.
2857 * XXX If another thread attempts to single-thread before us
2858 * (e.g. via fork()), we won't get a dump at all.
2860 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2863 * Log signals which would cause core dumps
2864 * (Log as LOG_INFO to appease those who don't want
2866 * XXX : Todo, as well as euid, write out ruid too
2867 * Note that coredump() drops proc lock.
2869 if (coredump(td) == 0)
2871 if (kern_logsigexit)
2873 "pid %d (%s), uid %d: exited on signal %d%s\n",
2874 p->p_pid, p->p_comm,
2875 td->td_ucred ? td->td_ucred->cr_uid : -1,
2877 sig & WCOREFLAG ? " (core dumped)" : "");
2880 exit1(td, W_EXITCODE(0, sig));
2885 * Send queued SIGCHLD to parent when child process's state
2889 sigparent(struct proc *p, int reason, int status)
2891 PROC_LOCK_ASSERT(p, MA_OWNED);
2892 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2894 if (p->p_ksi != NULL) {
2895 p->p_ksi->ksi_signo = SIGCHLD;
2896 p->p_ksi->ksi_code = reason;
2897 p->p_ksi->ksi_status = status;
2898 p->p_ksi->ksi_pid = p->p_pid;
2899 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2900 if (KSI_ONQ(p->p_ksi))
2903 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2907 childproc_jobstate(struct proc *p, int reason, int status)
2911 PROC_LOCK_ASSERT(p, MA_OWNED);
2912 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2915 * Wake up parent sleeping in kern_wait(), also send
2916 * SIGCHLD to parent, but SIGCHLD does not guarantee
2917 * that parent will awake, because parent may masked
2920 p->p_pptr->p_flag |= P_STATCHILD;
2923 ps = p->p_pptr->p_sigacts;
2924 mtx_lock(&ps->ps_mtx);
2925 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2926 mtx_unlock(&ps->ps_mtx);
2927 sigparent(p, reason, status);
2929 mtx_unlock(&ps->ps_mtx);
2933 childproc_stopped(struct proc *p, int reason)
2935 childproc_jobstate(p, reason, p->p_xstat);
2939 childproc_continued(struct proc *p)
2941 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2945 childproc_exited(struct proc *p)
2948 int status = p->p_xstat; /* convert to int */
2950 reason = CLD_EXITED;
2951 if (WCOREDUMP(status))
2952 reason = CLD_DUMPED;
2953 else if (WIFSIGNALED(status))
2954 reason = CLD_KILLED;
2956 * XXX avoid calling wakeup(p->p_pptr), the work is
2959 sigparent(p, reason, status);
2963 * We only have 1 character for the core count in the format
2964 * string, so the range will be 0-9
2966 #define MAX_NUM_CORES 10
2967 static int num_cores = 5;
2970 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
2975 new_val = num_cores;
2976 error = sysctl_handle_int(oidp, &new_val, 0, req);
2977 if (error != 0 || req->newptr == NULL)
2979 if (new_val > MAX_NUM_CORES)
2980 new_val = MAX_NUM_CORES;
2983 num_cores = new_val;
2986 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
2987 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
2989 #if defined(COMPRESS_USER_CORES)
2990 int compress_user_cores = 1;
2991 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
2992 &compress_user_cores, 0, "");
2994 int compress_user_cores_gzlevel = -1; /* default level */
2995 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
2996 &compress_user_cores_gzlevel, -1, "user core gz compression level");
2998 #define GZ_SUFFIX ".gz"
2999 #define GZ_SUFFIX_LEN 3
3002 static char corefilename[MAXPATHLEN] = {"%N.core"};
3003 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
3004 sizeof(corefilename), "process corefile name format string");
3007 * expand_name(name, uid, pid, td, compress)
3008 * Expand the name described in corefilename, using name, uid, and pid.
3009 * corefilename is a printf-like string, with three format specifiers:
3010 * %N name of process ("name")
3011 * %P process id (pid)
3013 * For example, "%N.core" is the default; they can be disabled completely
3014 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3015 * This is controlled by the sysctl variable kern.corefile (see above).
3018 expand_name(const char *name, uid_t uid, pid_t pid, struct thread *td,
3029 format = corefilename;
3030 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
3034 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
3035 for (i = 0; format[i]; i++) {
3036 switch (format[i]) {
3037 case '%': /* Format character */
3039 switch (format[i]) {
3041 sbuf_putc(&sb, '%');
3043 case 'H': /* hostname */
3044 if (hostname == NULL) {
3045 hostname = malloc(MAXHOSTNAMELEN,
3047 if (hostname == NULL) {
3049 "pid %ld (%s), uid (%lu): "
3050 "unable to alloc memory "
3051 "for corefile hostname\n",
3057 getcredhostname(td->td_ucred, hostname,
3059 sbuf_printf(&sb, "%s", hostname);
3061 case 'I': /* autoincrementing index */
3062 sbuf_printf(&sb, "0");
3063 indexpos = sbuf_len(&sb) - 1;
3065 case 'N': /* process name */
3066 sbuf_printf(&sb, "%s", name);
3068 case 'P': /* process id */
3069 sbuf_printf(&sb, "%u", pid);
3071 case 'U': /* user id */
3072 sbuf_printf(&sb, "%u", uid);
3076 "Unknown format character %c in "
3077 "corename `%s'\n", format[i], format);
3081 sbuf_putc(&sb, format[i]);
3084 free(hostname, M_TEMP);
3085 #ifdef COMPRESS_USER_CORES
3087 sbuf_printf(&sb, GZ_SUFFIX);
3090 if (sbuf_error(&sb) != 0) {
3091 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3092 "long\n", (long)pid, name, (u_long)uid);
3102 * If the core format has a %I in it, then we need to check
3103 * for existing corefiles before returning a name.
3104 * To do this we iterate over 0..num_cores to find a
3105 * non-existing core file name to use.
3107 if (indexpos != -1) {
3108 struct nameidata nd;
3110 int flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3111 int cmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
3114 for (n = 0; n < num_cores; n++) {
3115 temp[indexpos] = '0' + n;
3116 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE,
3118 error = vn_open(&nd, &flags, cmode, NULL);
3120 if (error == EEXIST) {
3124 "pid %d (%s), uid (%u): Path `%s' failed "
3125 "on initial open test, error = %d\n",
3126 pid, name, uid, temp, error);
3130 vfslocked = NDHASGIANT(&nd);
3131 NDFREE(&nd, NDF_ONLY_PNBUF);
3132 VOP_UNLOCK(nd.ni_vp, 0);
3133 error = vn_close(nd.ni_vp, FWRITE, td->td_ucred, td);
3134 VFS_UNLOCK_GIANT(vfslocked);
3137 "pid %d (%s), uid (%u): Path `%s' failed "
3138 "on close after initial open test, "
3140 pid, name, uid, temp, error);
3151 * Dump a process' core. The main routine does some
3152 * policy checking, and creates the name of the coredump;
3153 * then it passes on a vnode and a size limit to the process-specific
3154 * coredump routine if there is one; if there _is not_ one, it returns
3155 * ENOSYS; otherwise it returns the error from the process-specific routine.
3159 coredump(struct thread *td)
3161 struct proc *p = td->td_proc;
3162 register struct vnode *vp;
3163 register struct ucred *cred = td->td_ucred;
3165 struct nameidata nd;
3167 int error, error1, flags, locked;
3169 char *name; /* name of corefile */
3174 #ifdef COMPRESS_USER_CORES
3175 compress = compress_user_cores;
3179 PROC_LOCK_ASSERT(p, MA_OWNED);
3180 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3181 _STOPEVENT(p, S_CORE, 0);
3183 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid, td,
3188 audit_proc_coredump(td, NULL, EINVAL);
3192 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3195 audit_proc_coredump(td, name, EFAULT);
3202 * Note that the bulk of limit checking is done after
3203 * the corefile is created. The exception is if the limit
3204 * for corefiles is 0, in which case we don't bother
3205 * creating the corefile at all. This layout means that
3206 * a corefile is truncated instead of not being created,
3207 * if it is larger than the limit.
3209 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3210 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3213 audit_proc_coredump(td, name, EFBIG);
3221 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3222 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3223 error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
3227 audit_proc_coredump(td, name, error);
3232 vfslocked = NDHASGIANT(&nd);
3233 NDFREE(&nd, NDF_ONLY_PNBUF);
3236 /* Don't dump to non-regular files or files with links. */
3237 if (vp->v_type != VREG ||
3238 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
3245 lf.l_whence = SEEK_SET;
3248 lf.l_type = F_WRLCK;
3249 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3251 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3252 lf.l_type = F_UNLCK;
3254 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3255 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3257 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3259 VFS_UNLOCK_GIANT(vfslocked);
3265 if (set_core_nodump_flag)
3266 vattr.va_flags = UF_NODUMP;
3267 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3268 VOP_SETATTR(vp, &vattr, cred);
3270 vn_finished_write(mp);
3272 p->p_acflag |= ACORE;
3275 error = p->p_sysent->sv_coredump ?
3276 p->p_sysent->sv_coredump(td, vp, limit, compress ? IMGACT_CORE_COMPRESS : 0) :
3280 lf.l_type = F_UNLCK;
3281 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3284 error1 = vn_close(vp, FWRITE, cred, td);
3289 audit_proc_coredump(td, name, error);
3292 VFS_UNLOCK_GIANT(vfslocked);
3297 * Nonexistent system call-- signal process (may want to handle it). Flag
3298 * error in case process won't see signal immediately (blocked or ignored).
3300 #ifndef _SYS_SYSPROTO_H_
3309 struct nosys_args *args;
3311 struct proc *p = td->td_proc;
3314 kern_psignal(p, SIGSYS);
3320 * Send a SIGIO or SIGURG signal to a process or process group using stored
3321 * credentials rather than those of the current process.
3324 pgsigio(sigiop, sig, checkctty)
3325 struct sigio **sigiop;
3329 struct sigio *sigio;
3331 ksiginfo_init(&ksi);
3332 ksi.ksi_signo = sig;
3333 ksi.ksi_code = SI_KERNEL;
3337 if (sigio == NULL) {
3341 if (sigio->sio_pgid > 0) {
3342 PROC_LOCK(sigio->sio_proc);
3343 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3344 kern_psignal(sigio->sio_proc, sig);
3345 PROC_UNLOCK(sigio->sio_proc);
3346 } else if (sigio->sio_pgid < 0) {
3349 PGRP_LOCK(sigio->sio_pgrp);
3350 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3352 if (p->p_state == PRS_NORMAL &&
3353 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3354 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3355 kern_psignal(p, sig);
3358 PGRP_UNLOCK(sigio->sio_pgrp);
3364 filt_sigattach(struct knote *kn)
3366 struct proc *p = curproc;
3368 kn->kn_ptr.p_proc = p;
3369 kn->kn_flags |= EV_CLEAR; /* automatically set */
3371 knlist_add(&p->p_klist, kn, 0);
3377 filt_sigdetach(struct knote *kn)
3379 struct proc *p = kn->kn_ptr.p_proc;
3381 knlist_remove(&p->p_klist, kn, 0);
3385 * signal knotes are shared with proc knotes, so we apply a mask to
3386 * the hint in order to differentiate them from process hints. This
3387 * could be avoided by using a signal-specific knote list, but probably
3388 * isn't worth the trouble.
3391 filt_signal(struct knote *kn, long hint)
3394 if (hint & NOTE_SIGNAL) {
3395 hint &= ~NOTE_SIGNAL;
3397 if (kn->kn_id == hint)
3400 return (kn->kn_data != 0);
3408 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3410 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3415 sigacts_free(struct sigacts *ps)
3418 mtx_lock(&ps->ps_mtx);
3420 if (ps->ps_refcnt == 0) {
3421 mtx_destroy(&ps->ps_mtx);
3422 free(ps, M_SUBPROC);
3424 mtx_unlock(&ps->ps_mtx);
3428 sigacts_hold(struct sigacts *ps)
3430 mtx_lock(&ps->ps_mtx);
3432 mtx_unlock(&ps->ps_mtx);
3437 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3440 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3441 mtx_lock(&src->ps_mtx);
3442 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3443 mtx_unlock(&src->ps_mtx);
3447 sigacts_shared(struct sigacts *ps)
3451 mtx_lock(&ps->ps_mtx);
3452 shared = ps->ps_refcnt > 1;
3453 mtx_unlock(&ps->ps_mtx);