2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include "opt_procdesc.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
51 #include <sys/capability.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/imgact.h>
56 #include <sys/kernel.h>
58 #include <sys/ktrace.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
64 #include <sys/procdesc.h>
65 #include <sys/posix4.h>
66 #include <sys/pioctl.h>
67 #include <sys/racct.h>
68 #include <sys/resourcevar.h>
71 #include <sys/sleepqueue.h>
75 #include <sys/syscallsubr.h>
76 #include <sys/sysctl.h>
77 #include <sys/sysent.h>
78 #include <sys/syslog.h>
79 #include <sys/sysproto.h>
80 #include <sys/timers.h>
81 #include <sys/unistd.h>
84 #include <vm/vm_extern.h>
89 #include <machine/cpu.h>
91 #include <security/audit/audit.h>
93 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
95 SDT_PROVIDER_DECLARE(proc);
96 SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send);
97 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *");
98 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *");
99 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int");
100 SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear);
101 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int");
102 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
103 SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard);
104 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
105 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
106 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
108 static int coredump(struct thread *);
109 static char *expand_name(const char *, uid_t, pid_t, struct thread *, int);
110 static int killpg1(struct thread *td, int sig, int pgid, int all,
112 static int issignal(struct thread *td, int stop_allowed);
113 static int sigprop(int sig);
114 static void tdsigwakeup(struct thread *, int, sig_t, int);
115 static void sig_suspend_threads(struct thread *, struct proc *, int);
116 static int filt_sigattach(struct knote *kn);
117 static void filt_sigdetach(struct knote *kn);
118 static int filt_signal(struct knote *kn, long hint);
119 static struct thread *sigtd(struct proc *p, int sig, int prop);
120 static void sigqueue_start(void);
122 static uma_zone_t ksiginfo_zone = NULL;
123 struct filterops sig_filtops = {
125 .f_attach = filt_sigattach,
126 .f_detach = filt_sigdetach,
127 .f_event = filt_signal,
130 static int kern_logsigexit = 1;
131 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
133 "Log processes quitting on abnormal signals to syslog(3)");
135 static int kern_forcesigexit = 1;
136 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
137 &kern_forcesigexit, 0, "Force trap signal to be handled");
139 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
140 "POSIX real time signal");
142 static int max_pending_per_proc = 128;
143 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
144 &max_pending_per_proc, 0, "Max pending signals per proc");
146 static int preallocate_siginfo = 1024;
147 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
148 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
149 &preallocate_siginfo, 0, "Preallocated signal memory size");
151 static int signal_overflow = 0;
152 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
153 &signal_overflow, 0, "Number of signals overflew");
155 static int signal_alloc_fail = 0;
156 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
157 &signal_alloc_fail, 0, "signals failed to be allocated");
159 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
162 * Policy -- Can ucred cr1 send SIGIO to process cr2?
163 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
164 * in the right situations.
166 #define CANSIGIO(cr1, cr2) \
167 ((cr1)->cr_uid == 0 || \
168 (cr1)->cr_ruid == (cr2)->cr_ruid || \
169 (cr1)->cr_uid == (cr2)->cr_ruid || \
170 (cr1)->cr_ruid == (cr2)->cr_uid || \
171 (cr1)->cr_uid == (cr2)->cr_uid)
173 static int sugid_coredump;
174 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
175 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
177 static int do_coredump = 1;
178 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
179 &do_coredump, 0, "Enable/Disable coredumps");
181 static int set_core_nodump_flag = 0;
182 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
183 0, "Enable setting the NODUMP flag on coredump files");
186 * Signal properties and actions.
187 * The array below categorizes the signals and their default actions
188 * according to the following properties:
190 #define SA_KILL 0x01 /* terminates process by default */
191 #define SA_CORE 0x02 /* ditto and coredumps */
192 #define SA_STOP 0x04 /* suspend process */
193 #define SA_TTYSTOP 0x08 /* ditto, from tty */
194 #define SA_IGNORE 0x10 /* ignore by default */
195 #define SA_CONT 0x20 /* continue if suspended */
196 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
197 #define SA_PROC 0x80 /* deliverable to any thread */
199 static int sigproptbl[NSIG] = {
200 SA_KILL|SA_PROC, /* SIGHUP */
201 SA_KILL|SA_PROC, /* SIGINT */
202 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
203 SA_KILL|SA_CORE, /* SIGILL */
204 SA_KILL|SA_CORE, /* SIGTRAP */
205 SA_KILL|SA_CORE, /* SIGABRT */
206 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
207 SA_KILL|SA_CORE, /* SIGFPE */
208 SA_KILL|SA_PROC, /* SIGKILL */
209 SA_KILL|SA_CORE, /* SIGBUS */
210 SA_KILL|SA_CORE, /* SIGSEGV */
211 SA_KILL|SA_CORE, /* SIGSYS */
212 SA_KILL|SA_PROC, /* SIGPIPE */
213 SA_KILL|SA_PROC, /* SIGALRM */
214 SA_KILL|SA_PROC, /* SIGTERM */
215 SA_IGNORE|SA_PROC, /* SIGURG */
216 SA_STOP|SA_PROC, /* SIGSTOP */
217 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
218 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
219 SA_IGNORE|SA_PROC, /* SIGCHLD */
220 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
221 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
222 SA_IGNORE|SA_PROC, /* SIGIO */
223 SA_KILL, /* SIGXCPU */
224 SA_KILL, /* SIGXFSZ */
225 SA_KILL|SA_PROC, /* SIGVTALRM */
226 SA_KILL|SA_PROC, /* SIGPROF */
227 SA_IGNORE|SA_PROC, /* SIGWINCH */
228 SA_IGNORE|SA_PROC, /* SIGINFO */
229 SA_KILL|SA_PROC, /* SIGUSR1 */
230 SA_KILL|SA_PROC, /* SIGUSR2 */
233 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
238 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
239 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
240 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
241 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
242 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
243 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
247 ksiginfo_alloc(int wait)
254 if (ksiginfo_zone != NULL)
255 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
260 ksiginfo_free(ksiginfo_t *ksi)
262 uma_zfree(ksiginfo_zone, ksi);
266 ksiginfo_tryfree(ksiginfo_t *ksi)
268 if (!(ksi->ksi_flags & KSI_EXT)) {
269 uma_zfree(ksiginfo_zone, ksi);
276 sigqueue_init(sigqueue_t *list, struct proc *p)
278 SIGEMPTYSET(list->sq_signals);
279 SIGEMPTYSET(list->sq_kill);
280 TAILQ_INIT(&list->sq_list);
282 list->sq_flags = SQ_INIT;
286 * Get a signal's ksiginfo.
288 * 0 - signal not found
289 * others - signal number
292 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
294 struct proc *p = sq->sq_proc;
295 struct ksiginfo *ksi, *next;
298 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
300 if (!SIGISMEMBER(sq->sq_signals, signo))
303 if (SIGISMEMBER(sq->sq_kill, signo)) {
305 SIGDELSET(sq->sq_kill, signo);
308 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
309 if (ksi->ksi_signo == signo) {
311 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
312 ksi->ksi_sigq = NULL;
313 ksiginfo_copy(ksi, si);
314 if (ksiginfo_tryfree(ksi) && p != NULL)
323 SIGDELSET(sq->sq_signals, signo);
324 si->ksi_signo = signo;
329 sigqueue_take(ksiginfo_t *ksi)
335 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
339 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
340 ksi->ksi_sigq = NULL;
341 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
344 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
345 kp = TAILQ_NEXT(kp, ksi_link)) {
346 if (kp->ksi_signo == ksi->ksi_signo)
349 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
350 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
354 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
356 struct proc *p = sq->sq_proc;
357 struct ksiginfo *ksi;
360 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
362 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
363 SIGADDSET(sq->sq_kill, signo);
367 /* directly insert the ksi, don't copy it */
368 if (si->ksi_flags & KSI_INS) {
369 if (si->ksi_flags & KSI_HEAD)
370 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
372 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
377 if (__predict_false(ksiginfo_zone == NULL)) {
378 SIGADDSET(sq->sq_kill, signo);
382 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
385 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
391 ksiginfo_copy(si, ksi);
392 ksi->ksi_signo = signo;
393 if (si->ksi_flags & KSI_HEAD)
394 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
396 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
400 if ((si->ksi_flags & KSI_TRAP) != 0 ||
401 (si->ksi_flags & KSI_SIGQ) == 0) {
403 SIGADDSET(sq->sq_kill, signo);
412 SIGADDSET(sq->sq_signals, signo);
417 sigqueue_flush(sigqueue_t *sq)
419 struct proc *p = sq->sq_proc;
422 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
425 PROC_LOCK_ASSERT(p, MA_OWNED);
427 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
428 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
429 ksi->ksi_sigq = NULL;
430 if (ksiginfo_tryfree(ksi) && p != NULL)
434 SIGEMPTYSET(sq->sq_signals);
435 SIGEMPTYSET(sq->sq_kill);
439 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
442 struct proc *p1, *p2;
443 ksiginfo_t *ksi, *next;
445 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
446 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
449 /* Move siginfo to target list */
450 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
451 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
452 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
455 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
462 /* Move pending bits to target list */
464 SIGSETAND(tmp, *set);
465 SIGSETOR(dst->sq_kill, tmp);
466 SIGSETNAND(src->sq_kill, tmp);
468 tmp = src->sq_signals;
469 SIGSETAND(tmp, *set);
470 SIGSETOR(dst->sq_signals, tmp);
471 SIGSETNAND(src->sq_signals, tmp);
476 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
481 SIGADDSET(set, signo);
482 sigqueue_move_set(src, dst, &set);
487 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
489 struct proc *p = sq->sq_proc;
490 ksiginfo_t *ksi, *next;
492 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
494 /* Remove siginfo queue */
495 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
496 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
497 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
498 ksi->ksi_sigq = NULL;
499 if (ksiginfo_tryfree(ksi) && p != NULL)
503 SIGSETNAND(sq->sq_kill, *set);
504 SIGSETNAND(sq->sq_signals, *set);
508 sigqueue_delete(sigqueue_t *sq, int signo)
513 SIGADDSET(set, signo);
514 sigqueue_delete_set(sq, &set);
517 /* Remove a set of signals for a process */
519 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
524 PROC_LOCK_ASSERT(p, MA_OWNED);
526 sigqueue_init(&worklist, NULL);
527 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
529 FOREACH_THREAD_IN_PROC(p, td0)
530 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
532 sigqueue_flush(&worklist);
536 sigqueue_delete_proc(struct proc *p, int signo)
541 SIGADDSET(set, signo);
542 sigqueue_delete_set_proc(p, &set);
546 sigqueue_delete_stopmask_proc(struct proc *p)
551 SIGADDSET(set, SIGSTOP);
552 SIGADDSET(set, SIGTSTP);
553 SIGADDSET(set, SIGTTIN);
554 SIGADDSET(set, SIGTTOU);
555 sigqueue_delete_set_proc(p, &set);
559 * Determine signal that should be delivered to process p, the current
560 * process, 0 if none. If there is a pending stop signal with default
561 * action, the process stops in issignal().
564 cursig(struct thread *td, int stop_allowed)
566 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
567 KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
568 stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
569 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
570 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
571 return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
575 * Arrange for ast() to handle unmasked pending signals on return to user
576 * mode. This must be called whenever a signal is added to td_sigqueue or
577 * unmasked in td_sigmask.
580 signotify(struct thread *td)
586 PROC_LOCK_ASSERT(p, MA_OWNED);
588 if (SIGPENDING(td)) {
590 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
596 sigonstack(size_t sp)
598 struct thread *td = curthread;
600 return ((td->td_pflags & TDP_ALTSTACK) ?
601 #if defined(COMPAT_43)
602 ((td->td_sigstk.ss_size == 0) ?
603 (td->td_sigstk.ss_flags & SS_ONSTACK) :
604 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
606 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
615 if (sig > 0 && sig < NSIG)
616 return (sigproptbl[_SIG_IDX(sig)]);
621 sig_ffs(sigset_t *set)
625 for (i = 0; i < _SIG_WORDS; i++)
627 return (ffs(set->__bits[i]) + (i * 32));
638 kern_sigaction(td, sig, act, oact, flags)
641 struct sigaction *act, *oact;
645 struct proc *p = td->td_proc;
647 if (!_SIG_VALID(sig))
652 mtx_lock(&ps->ps_mtx);
654 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
656 if (SIGISMEMBER(ps->ps_sigonstack, sig))
657 oact->sa_flags |= SA_ONSTACK;
658 if (!SIGISMEMBER(ps->ps_sigintr, sig))
659 oact->sa_flags |= SA_RESTART;
660 if (SIGISMEMBER(ps->ps_sigreset, sig))
661 oact->sa_flags |= SA_RESETHAND;
662 if (SIGISMEMBER(ps->ps_signodefer, sig))
663 oact->sa_flags |= SA_NODEFER;
664 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
665 oact->sa_flags |= SA_SIGINFO;
667 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
669 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
670 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
671 oact->sa_flags |= SA_NOCLDSTOP;
672 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
673 oact->sa_flags |= SA_NOCLDWAIT;
676 if ((sig == SIGKILL || sig == SIGSTOP) &&
677 act->sa_handler != SIG_DFL) {
678 mtx_unlock(&ps->ps_mtx);
684 * Change setting atomically.
687 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
688 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
689 if (act->sa_flags & SA_SIGINFO) {
690 ps->ps_sigact[_SIG_IDX(sig)] =
691 (__sighandler_t *)act->sa_sigaction;
692 SIGADDSET(ps->ps_siginfo, sig);
694 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
695 SIGDELSET(ps->ps_siginfo, sig);
697 if (!(act->sa_flags & SA_RESTART))
698 SIGADDSET(ps->ps_sigintr, sig);
700 SIGDELSET(ps->ps_sigintr, sig);
701 if (act->sa_flags & SA_ONSTACK)
702 SIGADDSET(ps->ps_sigonstack, sig);
704 SIGDELSET(ps->ps_sigonstack, sig);
705 if (act->sa_flags & SA_RESETHAND)
706 SIGADDSET(ps->ps_sigreset, sig);
708 SIGDELSET(ps->ps_sigreset, sig);
709 if (act->sa_flags & SA_NODEFER)
710 SIGADDSET(ps->ps_signodefer, sig);
712 SIGDELSET(ps->ps_signodefer, sig);
713 if (sig == SIGCHLD) {
714 if (act->sa_flags & SA_NOCLDSTOP)
715 ps->ps_flag |= PS_NOCLDSTOP;
717 ps->ps_flag &= ~PS_NOCLDSTOP;
718 if (act->sa_flags & SA_NOCLDWAIT) {
720 * Paranoia: since SA_NOCLDWAIT is implemented
721 * by reparenting the dying child to PID 1 (and
722 * trust it to reap the zombie), PID 1 itself
723 * is forbidden to set SA_NOCLDWAIT.
726 ps->ps_flag &= ~PS_NOCLDWAIT;
728 ps->ps_flag |= PS_NOCLDWAIT;
730 ps->ps_flag &= ~PS_NOCLDWAIT;
731 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
732 ps->ps_flag |= PS_CLDSIGIGN;
734 ps->ps_flag &= ~PS_CLDSIGIGN;
737 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
738 * and for signals set to SIG_DFL where the default is to
739 * ignore. However, don't put SIGCONT in ps_sigignore, as we
740 * have to restart the process.
742 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
743 (sigprop(sig) & SA_IGNORE &&
744 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
745 /* never to be seen again */
746 sigqueue_delete_proc(p, sig);
748 /* easier in psignal */
749 SIGADDSET(ps->ps_sigignore, sig);
750 SIGDELSET(ps->ps_sigcatch, sig);
752 SIGDELSET(ps->ps_sigignore, sig);
753 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
754 SIGDELSET(ps->ps_sigcatch, sig);
756 SIGADDSET(ps->ps_sigcatch, sig);
758 #ifdef COMPAT_FREEBSD4
759 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
760 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
761 (flags & KSA_FREEBSD4) == 0)
762 SIGDELSET(ps->ps_freebsd4, sig);
764 SIGADDSET(ps->ps_freebsd4, sig);
767 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
768 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
769 (flags & KSA_OSIGSET) == 0)
770 SIGDELSET(ps->ps_osigset, sig);
772 SIGADDSET(ps->ps_osigset, sig);
775 mtx_unlock(&ps->ps_mtx);
780 #ifndef _SYS_SYSPROTO_H_
781 struct sigaction_args {
783 struct sigaction *act;
784 struct sigaction *oact;
788 sys_sigaction(td, uap)
790 register struct sigaction_args *uap;
792 struct sigaction act, oact;
793 register struct sigaction *actp, *oactp;
796 actp = (uap->act != NULL) ? &act : NULL;
797 oactp = (uap->oact != NULL) ? &oact : NULL;
799 error = copyin(uap->act, actp, sizeof(act));
803 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
805 error = copyout(oactp, uap->oact, sizeof(oact));
809 #ifdef COMPAT_FREEBSD4
810 #ifndef _SYS_SYSPROTO_H_
811 struct freebsd4_sigaction_args {
813 struct sigaction *act;
814 struct sigaction *oact;
818 freebsd4_sigaction(td, uap)
820 register struct freebsd4_sigaction_args *uap;
822 struct sigaction act, oact;
823 register struct sigaction *actp, *oactp;
827 actp = (uap->act != NULL) ? &act : NULL;
828 oactp = (uap->oact != NULL) ? &oact : NULL;
830 error = copyin(uap->act, actp, sizeof(act));
834 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
836 error = copyout(oactp, uap->oact, sizeof(oact));
839 #endif /* COMAPT_FREEBSD4 */
841 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
842 #ifndef _SYS_SYSPROTO_H_
843 struct osigaction_args {
845 struct osigaction *nsa;
846 struct osigaction *osa;
852 register struct osigaction_args *uap;
854 struct osigaction sa;
855 struct sigaction nsa, osa;
856 register struct sigaction *nsap, *osap;
859 if (uap->signum <= 0 || uap->signum >= ONSIG)
862 nsap = (uap->nsa != NULL) ? &nsa : NULL;
863 osap = (uap->osa != NULL) ? &osa : NULL;
866 error = copyin(uap->nsa, &sa, sizeof(sa));
869 nsap->sa_handler = sa.sa_handler;
870 nsap->sa_flags = sa.sa_flags;
871 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
873 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
874 if (osap && !error) {
875 sa.sa_handler = osap->sa_handler;
876 sa.sa_flags = osap->sa_flags;
877 SIG2OSIG(osap->sa_mask, sa.sa_mask);
878 error = copyout(&sa, uap->osa, sizeof(sa));
883 #if !defined(__i386__)
884 /* Avoid replicating the same stub everywhere */
888 struct osigreturn_args *uap;
891 return (nosys(td, (struct nosys_args *)uap));
894 #endif /* COMPAT_43 */
897 * Initialize signal state for process 0;
898 * set to ignore signals that are ignored by default.
909 mtx_lock(&ps->ps_mtx);
910 for (i = 1; i <= NSIG; i++)
911 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
912 SIGADDSET(ps->ps_sigignore, i);
913 mtx_unlock(&ps->ps_mtx);
918 * Reset signals for an exec of the specified process.
921 execsigs(struct proc *p)
928 * Reset caught signals. Held signals remain held
929 * through td_sigmask (unless they were caught,
930 * and are now ignored by default).
932 PROC_LOCK_ASSERT(p, MA_OWNED);
933 td = FIRST_THREAD_IN_PROC(p);
935 mtx_lock(&ps->ps_mtx);
936 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
937 sig = sig_ffs(&ps->ps_sigcatch);
938 SIGDELSET(ps->ps_sigcatch, sig);
939 if (sigprop(sig) & SA_IGNORE) {
941 SIGADDSET(ps->ps_sigignore, sig);
942 sigqueue_delete_proc(p, sig);
944 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
947 * Reset stack state to the user stack.
948 * Clear set of signals caught on the signal stack.
950 td->td_sigstk.ss_flags = SS_DISABLE;
951 td->td_sigstk.ss_size = 0;
952 td->td_sigstk.ss_sp = 0;
953 td->td_pflags &= ~TDP_ALTSTACK;
955 * Reset no zombies if child dies flag as Solaris does.
957 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
958 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
959 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
960 mtx_unlock(&ps->ps_mtx);
966 * Manipulate signal mask.
969 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
972 sigset_t new_block, oset1;
977 if (!(flags & SIGPROCMASK_PROC_LOCKED))
980 *oset = td->td_sigmask;
987 oset1 = td->td_sigmask;
988 SIGSETOR(td->td_sigmask, *set);
989 new_block = td->td_sigmask;
990 SIGSETNAND(new_block, oset1);
993 SIGSETNAND(td->td_sigmask, *set);
998 oset1 = td->td_sigmask;
999 if (flags & SIGPROCMASK_OLD)
1000 SIGSETLO(td->td_sigmask, *set);
1002 td->td_sigmask = *set;
1003 new_block = td->td_sigmask;
1004 SIGSETNAND(new_block, oset1);
1013 * The new_block set contains signals that were not previously
1014 * blocked, but are blocked now.
1016 * In case we block any signal that was not previously blocked
1017 * for td, and process has the signal pending, try to schedule
1018 * signal delivery to some thread that does not block the
1019 * signal, possibly waking it up.
1021 if (p->p_numthreads != 1)
1022 reschedule_signals(p, new_block, flags);
1026 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1031 #ifndef _SYS_SYSPROTO_H_
1032 struct sigprocmask_args {
1034 const sigset_t *set;
1039 sys_sigprocmask(td, uap)
1040 register struct thread *td;
1041 struct sigprocmask_args *uap;
1044 sigset_t *setp, *osetp;
1047 setp = (uap->set != NULL) ? &set : NULL;
1048 osetp = (uap->oset != NULL) ? &oset : NULL;
1050 error = copyin(uap->set, setp, sizeof(set));
1054 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1055 if (osetp && !error) {
1056 error = copyout(osetp, uap->oset, sizeof(oset));
1061 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1062 #ifndef _SYS_SYSPROTO_H_
1063 struct osigprocmask_args {
1069 osigprocmask(td, uap)
1070 register struct thread *td;
1071 struct osigprocmask_args *uap;
1076 OSIG2SIG(uap->mask, set);
1077 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1078 SIG2OSIG(oset, td->td_retval[0]);
1081 #endif /* COMPAT_43 */
1084 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1090 error = copyin(uap->set, &set, sizeof(set));
1092 td->td_retval[0] = error;
1096 error = kern_sigtimedwait(td, set, &ksi, NULL);
1098 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1100 if (error == ERESTART)
1102 td->td_retval[0] = error;
1106 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1107 td->td_retval[0] = error;
1112 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1115 struct timespec *timeout;
1121 error = copyin(uap->timeout, &ts, sizeof(ts));
1129 error = copyin(uap->set, &set, sizeof(set));
1133 error = kern_sigtimedwait(td, set, &ksi, timeout);
1138 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1141 td->td_retval[0] = ksi.ksi_signo;
1146 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1152 error = copyin(uap->set, &set, sizeof(set));
1156 error = kern_sigtimedwait(td, set, &ksi, NULL);
1161 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1164 td->td_retval[0] = ksi.ksi_signo;
1169 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1170 struct timespec *timeout)
1173 sigset_t saved_mask, new_block;
1175 int error, sig, timo, timevalid = 0;
1176 struct timespec rts, ets, ts;
1184 if (timeout != NULL) {
1185 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1187 getnanouptime(&rts);
1189 timespecadd(&ets, timeout);
1193 /* Some signals can not be waited for. */
1194 SIG_CANTMASK(waitset);
1197 saved_mask = td->td_sigmask;
1198 SIGSETNAND(td->td_sigmask, waitset);
1200 mtx_lock(&ps->ps_mtx);
1201 sig = cursig(td, SIG_STOP_ALLOWED);
1202 mtx_unlock(&ps->ps_mtx);
1203 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1204 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1205 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1215 * POSIX says this must be checked after looking for pending
1218 if (timeout != NULL) {
1223 getnanouptime(&rts);
1224 if (timespeccmp(&rts, &ets, >=)) {
1229 timespecsub(&ts, &rts);
1230 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1236 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1238 if (timeout != NULL) {
1239 if (error == ERESTART) {
1240 /* Timeout can not be restarted. */
1242 } else if (error == EAGAIN) {
1243 /* We will calculate timeout by ourself. */
1249 new_block = saved_mask;
1250 SIGSETNAND(new_block, td->td_sigmask);
1251 td->td_sigmask = saved_mask;
1253 * Fewer signals can be delivered to us, reschedule signal
1256 if (p->p_numthreads != 1)
1257 reschedule_signals(p, new_block, 0);
1260 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1262 if (ksi->ksi_code == SI_TIMER)
1263 itimer_accept(p, ksi->ksi_timerid, ksi);
1266 if (KTRPOINT(td, KTR_PSIG)) {
1269 mtx_lock(&ps->ps_mtx);
1270 action = ps->ps_sigact[_SIG_IDX(sig)];
1271 mtx_unlock(&ps->ps_mtx);
1272 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1282 #ifndef _SYS_SYSPROTO_H_
1283 struct sigpending_args {
1288 sys_sigpending(td, uap)
1290 struct sigpending_args *uap;
1292 struct proc *p = td->td_proc;
1296 pending = p->p_sigqueue.sq_signals;
1297 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1299 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1302 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1303 #ifndef _SYS_SYSPROTO_H_
1304 struct osigpending_args {
1309 osigpending(td, uap)
1311 struct osigpending_args *uap;
1313 struct proc *p = td->td_proc;
1317 pending = p->p_sigqueue.sq_signals;
1318 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1320 SIG2OSIG(pending, td->td_retval[0]);
1323 #endif /* COMPAT_43 */
1325 #if defined(COMPAT_43)
1327 * Generalized interface signal handler, 4.3-compatible.
1329 #ifndef _SYS_SYSPROTO_H_
1330 struct osigvec_args {
1340 register struct osigvec_args *uap;
1343 struct sigaction nsa, osa;
1344 register struct sigaction *nsap, *osap;
1347 if (uap->signum <= 0 || uap->signum >= ONSIG)
1349 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1350 osap = (uap->osv != NULL) ? &osa : NULL;
1352 error = copyin(uap->nsv, &vec, sizeof(vec));
1355 nsap->sa_handler = vec.sv_handler;
1356 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1357 nsap->sa_flags = vec.sv_flags;
1358 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1360 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1361 if (osap && !error) {
1362 vec.sv_handler = osap->sa_handler;
1363 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1364 vec.sv_flags = osap->sa_flags;
1365 vec.sv_flags &= ~SA_NOCLDWAIT;
1366 vec.sv_flags ^= SA_RESTART;
1367 error = copyout(&vec, uap->osv, sizeof(vec));
1372 #ifndef _SYS_SYSPROTO_H_
1373 struct osigblock_args {
1379 register struct thread *td;
1380 struct osigblock_args *uap;
1384 OSIG2SIG(uap->mask, set);
1385 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1386 SIG2OSIG(oset, td->td_retval[0]);
1390 #ifndef _SYS_SYSPROTO_H_
1391 struct osigsetmask_args {
1396 osigsetmask(td, uap)
1398 struct osigsetmask_args *uap;
1402 OSIG2SIG(uap->mask, set);
1403 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1404 SIG2OSIG(oset, td->td_retval[0]);
1407 #endif /* COMPAT_43 */
1410 * Suspend calling thread until signal, providing mask to be set in the
1413 #ifndef _SYS_SYSPROTO_H_
1414 struct sigsuspend_args {
1415 const sigset_t *sigmask;
1420 sys_sigsuspend(td, uap)
1422 struct sigsuspend_args *uap;
1427 error = copyin(uap->sigmask, &mask, sizeof(mask));
1430 return (kern_sigsuspend(td, mask));
1434 kern_sigsuspend(struct thread *td, sigset_t mask)
1436 struct proc *p = td->td_proc;
1440 * When returning from sigsuspend, we want
1441 * the old mask to be restored after the
1442 * signal handler has finished. Thus, we
1443 * save it here and mark the sigacts structure
1447 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1448 SIGPROCMASK_PROC_LOCKED);
1449 td->td_pflags |= TDP_OLDMASK;
1452 * Process signals now. Otherwise, we can get spurious wakeup
1453 * due to signal entered process queue, but delivered to other
1454 * thread. But sigsuspend should return only on signal
1457 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1458 for (has_sig = 0; !has_sig;) {
1459 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1462 thread_suspend_check(0);
1463 mtx_lock(&p->p_sigacts->ps_mtx);
1464 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
1465 has_sig += postsig(sig);
1466 mtx_unlock(&p->p_sigacts->ps_mtx);
1469 td->td_errno = EINTR;
1470 td->td_pflags |= TDP_NERRNO;
1471 return (EJUSTRETURN);
1474 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1476 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1477 * convention: libc stub passes mask, not pointer, to save a copyin.
1479 #ifndef _SYS_SYSPROTO_H_
1480 struct osigsuspend_args {
1486 osigsuspend(td, uap)
1488 struct osigsuspend_args *uap;
1492 OSIG2SIG(uap->mask, mask);
1493 return (kern_sigsuspend(td, mask));
1495 #endif /* COMPAT_43 */
1497 #if defined(COMPAT_43)
1498 #ifndef _SYS_SYSPROTO_H_
1499 struct osigstack_args {
1500 struct sigstack *nss;
1501 struct sigstack *oss;
1508 register struct osigstack_args *uap;
1510 struct sigstack nss, oss;
1513 if (uap->nss != NULL) {
1514 error = copyin(uap->nss, &nss, sizeof(nss));
1518 oss.ss_sp = td->td_sigstk.ss_sp;
1519 oss.ss_onstack = sigonstack(cpu_getstack(td));
1520 if (uap->nss != NULL) {
1521 td->td_sigstk.ss_sp = nss.ss_sp;
1522 td->td_sigstk.ss_size = 0;
1523 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1524 td->td_pflags |= TDP_ALTSTACK;
1526 if (uap->oss != NULL)
1527 error = copyout(&oss, uap->oss, sizeof(oss));
1531 #endif /* COMPAT_43 */
1533 #ifndef _SYS_SYSPROTO_H_
1534 struct sigaltstack_args {
1541 sys_sigaltstack(td, uap)
1543 register struct sigaltstack_args *uap;
1548 if (uap->ss != NULL) {
1549 error = copyin(uap->ss, &ss, sizeof(ss));
1553 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1554 (uap->oss != NULL) ? &oss : NULL);
1557 if (uap->oss != NULL)
1558 error = copyout(&oss, uap->oss, sizeof(stack_t));
1563 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1565 struct proc *p = td->td_proc;
1568 oonstack = sigonstack(cpu_getstack(td));
1571 *oss = td->td_sigstk;
1572 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1573 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1579 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1581 if (!(ss->ss_flags & SS_DISABLE)) {
1582 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1585 td->td_sigstk = *ss;
1586 td->td_pflags |= TDP_ALTSTACK;
1588 td->td_pflags &= ~TDP_ALTSTACK;
1595 * Common code for kill process group/broadcast kill.
1596 * cp is calling process.
1599 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1611 sx_slock(&allproc_lock);
1612 FOREACH_PROC_IN_SYSTEM(p) {
1614 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1615 p == td->td_proc || p->p_state == PRS_NEW) {
1619 err = p_cansignal(td, p, sig);
1622 pksignal(p, sig, ksi);
1625 else if (ret == ESRCH)
1629 sx_sunlock(&allproc_lock);
1631 sx_slock(&proctree_lock);
1634 * zero pgid means send to my process group.
1636 pgrp = td->td_proc->p_pgrp;
1639 pgrp = pgfind(pgid);
1641 sx_sunlock(&proctree_lock);
1645 sx_sunlock(&proctree_lock);
1646 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1648 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1649 p->p_state == PRS_NEW) {
1653 err = p_cansignal(td, p, sig);
1656 pksignal(p, sig, ksi);
1659 else if (ret == ESRCH)
1668 #ifndef _SYS_SYSPROTO_H_
1676 sys_kill(struct thread *td, struct kill_args *uap)
1682 AUDIT_ARG_SIGNUM(uap->signum);
1683 AUDIT_ARG_PID(uap->pid);
1684 if ((u_int)uap->signum > _SIG_MAXSIG)
1687 ksiginfo_init(&ksi);
1688 ksi.ksi_signo = uap->signum;
1689 ksi.ksi_code = SI_USER;
1690 ksi.ksi_pid = td->td_proc->p_pid;
1691 ksi.ksi_uid = td->td_ucred->cr_ruid;
1694 /* kill single process */
1695 if ((p = pfind(uap->pid)) == NULL) {
1696 if ((p = zpfind(uap->pid)) == NULL)
1699 AUDIT_ARG_PROCESS(p);
1700 error = p_cansignal(td, p, uap->signum);
1701 if (error == 0 && uap->signum)
1702 pksignal(p, uap->signum, &ksi);
1707 case -1: /* broadcast signal */
1708 return (killpg1(td, uap->signum, 0, 1, &ksi));
1709 case 0: /* signal own process group */
1710 return (killpg1(td, uap->signum, 0, 0, &ksi));
1711 default: /* negative explicit process group */
1712 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1720 struct pdkill_args *uap;
1726 AUDIT_ARG_SIGNUM(uap->signum);
1727 AUDIT_ARG_FD(uap->fd);
1728 if ((u_int)uap->signum > _SIG_MAXSIG)
1731 error = procdesc_find(td, uap->fd, CAP_PDKILL, &p);
1734 AUDIT_ARG_PROCESS(p);
1735 error = p_cansignal(td, p, uap->signum);
1736 if (error == 0 && uap->signum)
1737 kern_psignal(p, uap->signum);
1745 #if defined(COMPAT_43)
1746 #ifndef _SYS_SYSPROTO_H_
1747 struct okillpg_args {
1754 okillpg(struct thread *td, struct okillpg_args *uap)
1758 AUDIT_ARG_SIGNUM(uap->signum);
1759 AUDIT_ARG_PID(uap->pgid);
1760 if ((u_int)uap->signum > _SIG_MAXSIG)
1763 ksiginfo_init(&ksi);
1764 ksi.ksi_signo = uap->signum;
1765 ksi.ksi_code = SI_USER;
1766 ksi.ksi_pid = td->td_proc->p_pid;
1767 ksi.ksi_uid = td->td_ucred->cr_ruid;
1768 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1770 #endif /* COMPAT_43 */
1772 #ifndef _SYS_SYSPROTO_H_
1773 struct sigqueue_args {
1776 /* union sigval */ void *value;
1780 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1786 if ((u_int)uap->signum > _SIG_MAXSIG)
1790 * Specification says sigqueue can only send signal to
1796 if ((p = pfind(uap->pid)) == NULL) {
1797 if ((p = zpfind(uap->pid)) == NULL)
1800 error = p_cansignal(td, p, uap->signum);
1801 if (error == 0 && uap->signum != 0) {
1802 ksiginfo_init(&ksi);
1803 ksi.ksi_flags = KSI_SIGQ;
1804 ksi.ksi_signo = uap->signum;
1805 ksi.ksi_code = SI_QUEUE;
1806 ksi.ksi_pid = td->td_proc->p_pid;
1807 ksi.ksi_uid = td->td_ucred->cr_ruid;
1808 ksi.ksi_value.sival_ptr = uap->value;
1809 error = pksignal(p, ksi.ksi_signo, &ksi);
1816 * Send a signal to a process group.
1819 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1824 sx_slock(&proctree_lock);
1825 pgrp = pgfind(pgid);
1826 sx_sunlock(&proctree_lock);
1828 pgsignal(pgrp, sig, 0, ksi);
1835 * Send a signal to a process group. If checktty is 1,
1836 * limit to members which have a controlling terminal.
1839 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1844 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1845 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1847 if (p->p_state == PRS_NORMAL &&
1848 (checkctty == 0 || p->p_flag & P_CONTROLT))
1849 pksignal(p, sig, ksi);
1856 * Send a signal caused by a trap to the current thread. If it will be
1857 * caught immediately, deliver it with correct code. Otherwise, post it
1861 trapsignal(struct thread *td, ksiginfo_t *ksi)
1870 sig = ksi->ksi_signo;
1871 code = ksi->ksi_code;
1872 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1876 mtx_lock(&ps->ps_mtx);
1877 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1878 !SIGISMEMBER(td->td_sigmask, sig)) {
1879 td->td_ru.ru_nsignals++;
1881 if (KTRPOINT(curthread, KTR_PSIG))
1882 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1883 &td->td_sigmask, code);
1885 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1886 ksi, &td->td_sigmask);
1887 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1888 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1889 SIGADDSET(mask, sig);
1890 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1891 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1892 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1894 * See kern_sigaction() for origin of this code.
1896 SIGDELSET(ps->ps_sigcatch, sig);
1897 if (sig != SIGCONT &&
1898 sigprop(sig) & SA_IGNORE)
1899 SIGADDSET(ps->ps_sigignore, sig);
1900 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1902 mtx_unlock(&ps->ps_mtx);
1905 * Avoid a possible infinite loop if the thread
1906 * masking the signal or process is ignoring the
1909 if (kern_forcesigexit &&
1910 (SIGISMEMBER(td->td_sigmask, sig) ||
1911 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1912 SIGDELSET(td->td_sigmask, sig);
1913 SIGDELSET(ps->ps_sigcatch, sig);
1914 SIGDELSET(ps->ps_sigignore, sig);
1915 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1917 mtx_unlock(&ps->ps_mtx);
1918 p->p_code = code; /* XXX for core dump/debugger */
1919 p->p_sig = sig; /* XXX to verify code */
1920 tdsendsignal(p, td, sig, ksi);
1925 static struct thread *
1926 sigtd(struct proc *p, int sig, int prop)
1928 struct thread *td, *signal_td;
1930 PROC_LOCK_ASSERT(p, MA_OWNED);
1933 * Check if current thread can handle the signal without
1934 * switching context to another thread.
1936 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1939 FOREACH_THREAD_IN_PROC(p, td) {
1940 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1945 if (signal_td == NULL)
1946 signal_td = FIRST_THREAD_IN_PROC(p);
1951 * Send the signal to the process. If the signal has an action, the action
1952 * is usually performed by the target process rather than the caller; we add
1953 * the signal to the set of pending signals for the process.
1956 * o When a stop signal is sent to a sleeping process that takes the
1957 * default action, the process is stopped without awakening it.
1958 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1959 * regardless of the signal action (eg, blocked or ignored).
1961 * Other ignored signals are discarded immediately.
1963 * NB: This function may be entered from the debugger via the "kill" DDB
1964 * command. There is little that can be done to mitigate the possibly messy
1965 * side effects of this unwise possibility.
1968 kern_psignal(struct proc *p, int sig)
1972 ksiginfo_init(&ksi);
1973 ksi.ksi_signo = sig;
1974 ksi.ksi_code = SI_KERNEL;
1975 (void) tdsendsignal(p, NULL, sig, &ksi);
1979 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1982 return (tdsendsignal(p, NULL, sig, ksi));
1985 /* Utility function for finding a thread to send signal event to. */
1987 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
1991 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1992 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2004 tdsignal(struct thread *td, int sig)
2008 ksiginfo_init(&ksi);
2009 ksi.ksi_signo = sig;
2010 ksi.ksi_code = SI_KERNEL;
2011 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2015 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2018 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2022 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2025 sigqueue_t *sigqueue;
2032 MPASS(td == NULL || p == td->td_proc);
2033 PROC_LOCK_ASSERT(p, MA_OWNED);
2035 if (!_SIG_VALID(sig))
2036 panic("%s(): invalid signal %d", __func__, sig);
2038 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2041 * IEEE Std 1003.1-2001: return success when killing a zombie.
2043 if (p->p_state == PRS_ZOMBIE) {
2044 if (ksi && (ksi->ksi_flags & KSI_INS))
2045 ksiginfo_tryfree(ksi);
2050 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2051 prop = sigprop(sig);
2054 td = sigtd(p, sig, prop);
2055 sigqueue = &p->p_sigqueue;
2057 KASSERT(td->td_proc == p, ("invalid thread"));
2058 sigqueue = &td->td_sigqueue;
2061 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2064 * If the signal is being ignored,
2065 * then we forget about it immediately.
2066 * (Note: we don't set SIGCONT in ps_sigignore,
2067 * and if it is set to SIG_IGN,
2068 * action will be SIG_DFL here.)
2070 mtx_lock(&ps->ps_mtx);
2071 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2072 SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 );
2074 mtx_unlock(&ps->ps_mtx);
2075 if (ksi && (ksi->ksi_flags & KSI_INS))
2076 ksiginfo_tryfree(ksi);
2079 if (SIGISMEMBER(td->td_sigmask, sig))
2081 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2085 if (SIGISMEMBER(ps->ps_sigintr, sig))
2089 mtx_unlock(&ps->ps_mtx);
2092 sigqueue_delete_stopmask_proc(p);
2093 else if (prop & SA_STOP) {
2095 * If sending a tty stop signal to a member of an orphaned
2096 * process group, discard the signal here if the action
2097 * is default; don't stop the process below if sleeping,
2098 * and don't clear any pending SIGCONT.
2100 if ((prop & SA_TTYSTOP) &&
2101 (p->p_pgrp->pg_jobc == 0) &&
2102 (action == SIG_DFL)) {
2103 if (ksi && (ksi->ksi_flags & KSI_INS))
2104 ksiginfo_tryfree(ksi);
2107 sigqueue_delete_proc(p, SIGCONT);
2108 if (p->p_flag & P_CONTINUED) {
2109 p->p_flag &= ~P_CONTINUED;
2110 PROC_LOCK(p->p_pptr);
2111 sigqueue_take(p->p_ksi);
2112 PROC_UNLOCK(p->p_pptr);
2116 ret = sigqueue_add(sigqueue, sig, ksi);
2121 * Defer further processing for signals which are held,
2122 * except that stopped processes must be continued by SIGCONT.
2124 if (action == SIG_HOLD &&
2125 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2128 * SIGKILL: Remove procfs STOPEVENTs.
2130 if (sig == SIGKILL) {
2131 /* from procfs_ioctl.c: PIOCBIC */
2133 /* from procfs_ioctl.c: PIOCCONT */
2138 * Some signals have a process-wide effect and a per-thread
2139 * component. Most processing occurs when the process next
2140 * tries to cross the user boundary, however there are some
2141 * times when processing needs to be done immediatly, such as
2142 * waking up threads so that they can cross the user boundary.
2143 * We try do the per-process part here.
2145 if (P_SHOULDSTOP(p)) {
2146 KASSERT(!(p->p_flag & P_WEXIT),
2147 ("signal to stopped but exiting process"));
2148 if (sig == SIGKILL) {
2150 * If traced process is already stopped,
2151 * then no further action is necessary.
2153 if (p->p_flag & P_TRACED)
2156 * SIGKILL sets process running.
2157 * It will die elsewhere.
2158 * All threads must be restarted.
2160 p->p_flag &= ~P_STOPPED_SIG;
2164 if (prop & SA_CONT) {
2166 * If traced process is already stopped,
2167 * then no further action is necessary.
2169 if (p->p_flag & P_TRACED)
2172 * If SIGCONT is default (or ignored), we continue the
2173 * process but don't leave the signal in sigqueue as
2174 * it has no further action. If SIGCONT is held, we
2175 * continue the process and leave the signal in
2176 * sigqueue. If the process catches SIGCONT, let it
2177 * handle the signal itself. If it isn't waiting on
2178 * an event, it goes back to run state.
2179 * Otherwise, process goes back to sleep state.
2181 p->p_flag &= ~P_STOPPED_SIG;
2183 if (p->p_numthreads == p->p_suspcount) {
2185 p->p_flag |= P_CONTINUED;
2186 p->p_xstat = SIGCONT;
2187 PROC_LOCK(p->p_pptr);
2188 childproc_continued(p);
2189 PROC_UNLOCK(p->p_pptr);
2192 if (action == SIG_DFL) {
2193 thread_unsuspend(p);
2195 sigqueue_delete(sigqueue, sig);
2198 if (action == SIG_CATCH) {
2200 * The process wants to catch it so it needs
2201 * to run at least one thread, but which one?
2207 * The signal is not ignored or caught.
2209 thread_unsuspend(p);
2214 if (prop & SA_STOP) {
2216 * If traced process is already stopped,
2217 * then no further action is necessary.
2219 if (p->p_flag & P_TRACED)
2222 * Already stopped, don't need to stop again
2223 * (If we did the shell could get confused).
2224 * Just make sure the signal STOP bit set.
2226 p->p_flag |= P_STOPPED_SIG;
2227 sigqueue_delete(sigqueue, sig);
2232 * All other kinds of signals:
2233 * If a thread is sleeping interruptibly, simulate a
2234 * wakeup so that when it is continued it will be made
2235 * runnable and can look at the signal. However, don't make
2236 * the PROCESS runnable, leave it stopped.
2237 * It may run a bit until it hits a thread_suspend_check().
2242 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2243 wakeup_swapper = sleepq_abort(td, intrval);
2250 * Mutexes are short lived. Threads waiting on them will
2251 * hit thread_suspend_check() soon.
2253 } else if (p->p_state == PRS_NORMAL) {
2254 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2255 tdsigwakeup(td, sig, action, intrval);
2259 MPASS(action == SIG_DFL);
2261 if (prop & SA_STOP) {
2262 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2264 p->p_flag |= P_STOPPED_SIG;
2267 sig_suspend_threads(td, p, 1);
2268 if (p->p_numthreads == p->p_suspcount) {
2270 * only thread sending signal to another
2271 * process can reach here, if thread is sending
2272 * signal to its process, because thread does
2273 * not suspend itself here, p_numthreads
2274 * should never be equal to p_suspcount.
2278 sigqueue_delete_proc(p, p->p_xstat);
2284 /* Not in "NORMAL" state. discard the signal. */
2285 sigqueue_delete(sigqueue, sig);
2290 * The process is not stopped so we need to apply the signal to all the
2294 tdsigwakeup(td, sig, action, intrval);
2296 thread_unsuspend(p);
2299 /* If we jump here, proc slock should not be owned. */
2300 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2305 * The force of a signal has been directed against a single
2306 * thread. We need to see what we can do about knocking it
2307 * out of any sleep it may be in etc.
2310 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2312 struct proc *p = td->td_proc;
2317 PROC_LOCK_ASSERT(p, MA_OWNED);
2318 prop = sigprop(sig);
2323 * Bring the priority of a thread up if we want it to get
2324 * killed in this lifetime.
2326 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2327 sched_prio(td, PUSER);
2328 if (TD_ON_SLEEPQ(td)) {
2330 * If thread is sleeping uninterruptibly
2331 * we can't interrupt the sleep... the signal will
2332 * be noticed when the process returns through
2333 * trap() or syscall().
2335 if ((td->td_flags & TDF_SINTR) == 0)
2338 * If SIGCONT is default (or ignored) and process is
2339 * asleep, we are finished; the process should not
2342 if ((prop & SA_CONT) && action == SIG_DFL) {
2345 sigqueue_delete(&p->p_sigqueue, sig);
2347 * It may be on either list in this state.
2348 * Remove from both for now.
2350 sigqueue_delete(&td->td_sigqueue, sig);
2355 * Don't awaken a sleeping thread for SIGSTOP if the
2356 * STOP signal is deferred.
2358 if ((prop & SA_STOP) && (td->td_flags & TDF_SBDRY))
2362 * Give low priority threads a better chance to run.
2364 if (td->td_priority > PUSER)
2365 sched_prio(td, PUSER);
2367 wakeup_swapper = sleepq_abort(td, intrval);
2370 * Other states do nothing with the signal immediately,
2371 * other than kicking ourselves if we are running.
2372 * It will either never be noticed, or noticed very soon.
2375 if (TD_IS_RUNNING(td) && td != curthread)
2387 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2392 PROC_LOCK_ASSERT(p, MA_OWNED);
2393 PROC_SLOCK_ASSERT(p, MA_OWNED);
2396 FOREACH_THREAD_IN_PROC(p, td2) {
2398 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2399 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2400 (td2->td_flags & TDF_SINTR)) {
2401 if (td2->td_flags & TDF_SBDRY) {
2403 * Once a thread is asleep with
2404 * TDF_SBDRY set, it should never
2405 * become suspended due to this check.
2407 KASSERT(!TD_IS_SUSPENDED(td2),
2408 ("thread with deferred stops suspended"));
2409 } else if (!TD_IS_SUSPENDED(td2)) {
2410 thread_suspend_one(td2);
2412 } else if (!TD_IS_SUSPENDED(td2)) {
2413 if (sending || td != td2)
2414 td2->td_flags |= TDF_ASTPENDING;
2416 if (TD_IS_RUNNING(td2) && td2 != td)
2417 forward_signal(td2);
2427 ptracestop(struct thread *td, int sig)
2429 struct proc *p = td->td_proc;
2431 PROC_LOCK_ASSERT(p, MA_OWNED);
2432 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2433 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2434 &p->p_mtx.lock_object, "Stopping for traced signal");
2436 td->td_dbgflags |= TDB_XSIG;
2439 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2440 if (p->p_flag & P_SINGLE_EXIT) {
2441 td->td_dbgflags &= ~TDB_XSIG;
2446 * Just make wait() to work, the last stopped thread
2451 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2452 sig_suspend_threads(td, p, 0);
2453 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2454 td->td_dbgflags &= ~TDB_STOPATFORK;
2455 cv_broadcast(&p->p_dbgwait);
2458 thread_suspend_switch(td);
2459 if (!(p->p_flag & P_TRACED)) {
2462 if (td->td_dbgflags & TDB_SUSPEND) {
2463 if (p->p_flag & P_SINGLE_EXIT)
2469 return (td->td_xsig);
2473 reschedule_signals(struct proc *p, sigset_t block, int flags)
2479 PROC_LOCK_ASSERT(p, MA_OWNED);
2480 if (SIGISEMPTY(p->p_siglist))
2483 SIGSETAND(block, p->p_siglist);
2484 while ((sig = sig_ffs(&block)) != 0) {
2485 SIGDELSET(block, sig);
2486 td = sigtd(p, sig, 0);
2488 if (!(flags & SIGPROCMASK_PS_LOCKED))
2489 mtx_lock(&ps->ps_mtx);
2490 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2491 tdsigwakeup(td, sig, SIG_CATCH,
2492 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2494 if (!(flags & SIGPROCMASK_PS_LOCKED))
2495 mtx_unlock(&ps->ps_mtx);
2500 tdsigcleanup(struct thread *td)
2506 PROC_LOCK_ASSERT(p, MA_OWNED);
2508 sigqueue_flush(&td->td_sigqueue);
2509 if (p->p_numthreads == 1)
2513 * Since we cannot handle signals, notify signal post code
2514 * about this by filling the sigmask.
2516 * Also, if needed, wake up thread(s) that do not block the
2517 * same signals as the exiting thread, since the thread might
2518 * have been selected for delivery and woken up.
2520 SIGFILLSET(unblocked);
2521 SIGSETNAND(unblocked, td->td_sigmask);
2522 SIGFILLSET(td->td_sigmask);
2523 reschedule_signals(p, unblocked, 0);
2528 * Defer the delivery of SIGSTOP for the current thread. Returns true
2529 * if stops were deferred and false if they were already deferred.
2537 if (td->td_flags & TDF_SBDRY)
2540 td->td_flags |= TDF_SBDRY;
2546 * Permit the delivery of SIGSTOP for the current thread. This does
2547 * not immediately suspend if a stop was posted. Instead, the thread
2548 * will suspend either via ast() or a subsequent interruptible sleep.
2557 td->td_flags &= ~TDF_SBDRY;
2562 * If the current process has received a signal (should be caught or cause
2563 * termination, should interrupt current syscall), return the signal number.
2564 * Stop signals with default action are processed immediately, then cleared;
2565 * they aren't returned. This is checked after each entry to the system for
2566 * a syscall or trap (though this can usually be done without calling issignal
2567 * by checking the pending signal masks in cursig.) The normal call
2570 * while (sig = cursig(curthread))
2574 issignal(struct thread *td, int stop_allowed)
2578 struct sigqueue *queue;
2579 sigset_t sigpending;
2580 int sig, prop, newsig;
2584 mtx_assert(&ps->ps_mtx, MA_OWNED);
2585 PROC_LOCK_ASSERT(p, MA_OWNED);
2587 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2589 sigpending = td->td_sigqueue.sq_signals;
2590 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2591 SIGSETNAND(sigpending, td->td_sigmask);
2593 if (p->p_flag & P_PPWAIT || td->td_flags & TDF_SBDRY)
2594 SIG_STOPSIGMASK(sigpending);
2595 if (SIGISEMPTY(sigpending)) /* no signal to send */
2597 sig = sig_ffs(&sigpending);
2599 if (p->p_stops & S_SIG) {
2600 mtx_unlock(&ps->ps_mtx);
2601 stopevent(p, S_SIG, sig);
2602 mtx_lock(&ps->ps_mtx);
2606 * We should see pending but ignored signals
2607 * only if P_TRACED was on when they were posted.
2609 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2610 sigqueue_delete(&td->td_sigqueue, sig);
2611 sigqueue_delete(&p->p_sigqueue, sig);
2614 if (p->p_flag & P_TRACED && (p->p_flag & P_PPTRACE) == 0) {
2616 * If traced, always stop.
2617 * Remove old signal from queue before the stop.
2618 * XXX shrug off debugger, it causes siginfo to
2621 queue = &td->td_sigqueue;
2622 td->td_dbgksi.ksi_signo = 0;
2623 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2624 queue = &p->p_sigqueue;
2625 sigqueue_get(queue, sig, &td->td_dbgksi);
2628 mtx_unlock(&ps->ps_mtx);
2629 newsig = ptracestop(td, sig);
2630 mtx_lock(&ps->ps_mtx);
2632 if (sig != newsig) {
2635 * If parent wants us to take the signal,
2636 * then it will leave it in p->p_xstat;
2637 * otherwise we just look for signals again.
2644 * Put the new signal into td_sigqueue. If the
2645 * signal is being masked, look for other signals.
2647 sigqueue_add(queue, sig, NULL);
2648 if (SIGISMEMBER(td->td_sigmask, sig))
2652 if (td->td_dbgksi.ksi_signo != 0) {
2653 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2654 if (sigqueue_add(&td->td_sigqueue, sig,
2655 &td->td_dbgksi) != 0)
2656 td->td_dbgksi.ksi_signo = 0;
2658 if (td->td_dbgksi.ksi_signo == 0)
2659 sigqueue_add(&td->td_sigqueue, sig,
2664 * If the traced bit got turned off, go back up
2665 * to the top to rescan signals. This ensures
2666 * that p_sig* and p_sigact are consistent.
2668 if ((p->p_flag & P_TRACED) == 0)
2672 prop = sigprop(sig);
2675 * Decide whether the signal should be returned.
2676 * Return the signal's number, or fall through
2677 * to clear it from the pending mask.
2679 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2681 case (intptr_t)SIG_DFL:
2683 * Don't take default actions on system processes.
2685 if (p->p_pid <= 1) {
2688 * Are you sure you want to ignore SIGSEGV
2691 printf("Process (pid %lu) got signal %d\n",
2692 (u_long)p->p_pid, sig);
2694 break; /* == ignore */
2697 * If there is a pending stop signal to process
2698 * with default action, stop here,
2699 * then clear the signal. However,
2700 * if process is member of an orphaned
2701 * process group, ignore tty stop signals.
2703 if (prop & SA_STOP) {
2704 if (p->p_flag & (P_TRACED|P_WEXIT) ||
2705 (p->p_pgrp->pg_jobc == 0 &&
2707 break; /* == ignore */
2708 mtx_unlock(&ps->ps_mtx);
2709 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2710 &p->p_mtx.lock_object, "Catching SIGSTOP");
2711 p->p_flag |= P_STOPPED_SIG;
2714 sig_suspend_threads(td, p, 0);
2715 thread_suspend_switch(td);
2717 mtx_lock(&ps->ps_mtx);
2719 } else if (prop & SA_IGNORE) {
2721 * Except for SIGCONT, shouldn't get here.
2722 * Default action is to ignore; drop it.
2724 break; /* == ignore */
2729 case (intptr_t)SIG_IGN:
2731 * Masking above should prevent us ever trying
2732 * to take action on an ignored signal other
2733 * than SIGCONT, unless process is traced.
2735 if ((prop & SA_CONT) == 0 &&
2736 (p->p_flag & P_TRACED) == 0)
2737 printf("issignal\n");
2738 break; /* == ignore */
2742 * This signal has an action, let
2743 * postsig() process it.
2747 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2748 sigqueue_delete(&p->p_sigqueue, sig);
2754 thread_stopped(struct proc *p)
2758 PROC_LOCK_ASSERT(p, MA_OWNED);
2759 PROC_SLOCK_ASSERT(p, MA_OWNED);
2763 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2765 p->p_flag &= ~P_WAITED;
2766 PROC_LOCK(p->p_pptr);
2767 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2768 CLD_TRAPPED : CLD_STOPPED);
2769 PROC_UNLOCK(p->p_pptr);
2775 * Take the action for the specified signal
2776 * from the current set of pending signals.
2782 struct thread *td = curthread;
2783 register struct proc *p = td->td_proc;
2787 sigset_t returnmask, mask;
2789 KASSERT(sig != 0, ("postsig"));
2791 PROC_LOCK_ASSERT(p, MA_OWNED);
2793 mtx_assert(&ps->ps_mtx, MA_OWNED);
2794 ksiginfo_init(&ksi);
2795 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2796 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2798 ksi.ksi_signo = sig;
2799 if (ksi.ksi_code == SI_TIMER)
2800 itimer_accept(p, ksi.ksi_timerid, &ksi);
2801 action = ps->ps_sigact[_SIG_IDX(sig)];
2803 if (KTRPOINT(td, KTR_PSIG))
2804 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2805 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2807 if (p->p_stops & S_SIG) {
2808 mtx_unlock(&ps->ps_mtx);
2809 stopevent(p, S_SIG, sig);
2810 mtx_lock(&ps->ps_mtx);
2813 if (action == SIG_DFL) {
2815 * Default action, where the default is to kill
2816 * the process. (Other cases were ignored above.)
2818 mtx_unlock(&ps->ps_mtx);
2823 * If we get here, the signal must be caught.
2825 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2826 ("postsig action"));
2828 * Set the new mask value and also defer further
2829 * occurrences of this signal.
2831 * Special case: user has done a sigsuspend. Here the
2832 * current mask is not of interest, but rather the
2833 * mask from before the sigsuspend is what we want
2834 * restored after the signal processing is completed.
2836 if (td->td_pflags & TDP_OLDMASK) {
2837 returnmask = td->td_oldsigmask;
2838 td->td_pflags &= ~TDP_OLDMASK;
2840 returnmask = td->td_sigmask;
2842 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2843 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2844 SIGADDSET(mask, sig);
2845 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2846 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2848 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2850 * See kern_sigaction() for origin of this code.
2852 SIGDELSET(ps->ps_sigcatch, sig);
2853 if (sig != SIGCONT &&
2854 sigprop(sig) & SA_IGNORE)
2855 SIGADDSET(ps->ps_sigignore, sig);
2856 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2858 td->td_ru.ru_nsignals++;
2859 if (p->p_sig == sig) {
2863 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2869 * Kill the current process for stated reason.
2877 PROC_LOCK_ASSERT(p, MA_OWNED);
2878 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2879 p, p->p_pid, p->p_comm);
2880 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2881 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2882 p->p_flag |= P_WKILLED;
2883 kern_psignal(p, SIGKILL);
2887 * Force the current process to exit with the specified signal, dumping core
2888 * if appropriate. We bypass the normal tests for masked and caught signals,
2889 * allowing unrecoverable failures to terminate the process without changing
2890 * signal state. Mark the accounting record with the signal termination.
2891 * If dumping core, save the signal number for the debugger. Calls exit and
2899 struct proc *p = td->td_proc;
2901 PROC_LOCK_ASSERT(p, MA_OWNED);
2902 p->p_acflag |= AXSIG;
2904 * We must be single-threading to generate a core dump. This
2905 * ensures that the registers in the core file are up-to-date.
2906 * Also, the ELF dump handler assumes that the thread list doesn't
2907 * change out from under it.
2909 * XXX If another thread attempts to single-thread before us
2910 * (e.g. via fork()), we won't get a dump at all.
2912 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2915 * Log signals which would cause core dumps
2916 * (Log as LOG_INFO to appease those who don't want
2918 * XXX : Todo, as well as euid, write out ruid too
2919 * Note that coredump() drops proc lock.
2921 if (coredump(td) == 0)
2923 if (kern_logsigexit)
2925 "pid %d (%s), uid %d: exited on signal %d%s\n",
2926 p->p_pid, p->p_comm,
2927 td->td_ucred ? td->td_ucred->cr_uid : -1,
2929 sig & WCOREFLAG ? " (core dumped)" : "");
2932 exit1(td, W_EXITCODE(0, sig));
2937 * Send queued SIGCHLD to parent when child process's state
2941 sigparent(struct proc *p, int reason, int status)
2943 PROC_LOCK_ASSERT(p, MA_OWNED);
2944 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2946 if (p->p_ksi != NULL) {
2947 p->p_ksi->ksi_signo = SIGCHLD;
2948 p->p_ksi->ksi_code = reason;
2949 p->p_ksi->ksi_status = status;
2950 p->p_ksi->ksi_pid = p->p_pid;
2951 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2952 if (KSI_ONQ(p->p_ksi))
2955 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2959 childproc_jobstate(struct proc *p, int reason, int status)
2963 PROC_LOCK_ASSERT(p, MA_OWNED);
2964 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2967 * Wake up parent sleeping in kern_wait(), also send
2968 * SIGCHLD to parent, but SIGCHLD does not guarantee
2969 * that parent will awake, because parent may masked
2972 p->p_pptr->p_flag |= P_STATCHILD;
2975 ps = p->p_pptr->p_sigacts;
2976 mtx_lock(&ps->ps_mtx);
2977 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2978 mtx_unlock(&ps->ps_mtx);
2979 sigparent(p, reason, status);
2981 mtx_unlock(&ps->ps_mtx);
2985 childproc_stopped(struct proc *p, int reason)
2987 childproc_jobstate(p, reason, p->p_xstat);
2991 childproc_continued(struct proc *p)
2993 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2997 childproc_exited(struct proc *p)
3000 int status = p->p_xstat; /* convert to int */
3002 reason = CLD_EXITED;
3003 if (WCOREDUMP(status))
3004 reason = CLD_DUMPED;
3005 else if (WIFSIGNALED(status))
3006 reason = CLD_KILLED;
3008 * XXX avoid calling wakeup(p->p_pptr), the work is
3011 sigparent(p, reason, status);
3015 * We only have 1 character for the core count in the format
3016 * string, so the range will be 0-9
3018 #define MAX_NUM_CORES 10
3019 static int num_cores = 5;
3022 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3027 new_val = num_cores;
3028 error = sysctl_handle_int(oidp, &new_val, 0, req);
3029 if (error != 0 || req->newptr == NULL)
3031 if (new_val > MAX_NUM_CORES)
3032 new_val = MAX_NUM_CORES;
3035 num_cores = new_val;
3038 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3039 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
3041 #if defined(COMPRESS_USER_CORES)
3042 int compress_user_cores = 1;
3043 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
3044 &compress_user_cores, 0, "");
3046 int compress_user_cores_gzlevel = -1; /* default level */
3047 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
3048 &compress_user_cores_gzlevel, -1, "user core gz compression level");
3050 #define GZ_SUFFIX ".gz"
3051 #define GZ_SUFFIX_LEN 3
3054 static char corefilename[MAXPATHLEN] = {"%N.core"};
3055 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
3056 sizeof(corefilename), "process corefile name format string");
3059 * expand_name(name, uid, pid, td, compress)
3060 * Expand the name described in corefilename, using name, uid, and pid.
3061 * corefilename is a printf-like string, with three format specifiers:
3062 * %N name of process ("name")
3063 * %P process id (pid)
3065 * For example, "%N.core" is the default; they can be disabled completely
3066 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3067 * This is controlled by the sysctl variable kern.corefile (see above).
3070 expand_name(const char *name, uid_t uid, pid_t pid, struct thread *td,
3081 format = corefilename;
3082 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
3086 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
3087 for (i = 0; format[i]; i++) {
3088 switch (format[i]) {
3089 case '%': /* Format character */
3091 switch (format[i]) {
3093 sbuf_putc(&sb, '%');
3095 case 'H': /* hostname */
3096 if (hostname == NULL) {
3097 hostname = malloc(MAXHOSTNAMELEN,
3099 if (hostname == NULL) {
3101 "pid %ld (%s), uid (%lu): "
3102 "unable to alloc memory "
3103 "for corefile hostname\n",
3109 getcredhostname(td->td_ucred, hostname,
3111 sbuf_printf(&sb, "%s", hostname);
3113 case 'I': /* autoincrementing index */
3114 sbuf_printf(&sb, "0");
3115 indexpos = sbuf_len(&sb) - 1;
3117 case 'N': /* process name */
3118 sbuf_printf(&sb, "%s", name);
3120 case 'P': /* process id */
3121 sbuf_printf(&sb, "%u", pid);
3123 case 'U': /* user id */
3124 sbuf_printf(&sb, "%u", uid);
3128 "Unknown format character %c in "
3129 "corename `%s'\n", format[i], format);
3133 sbuf_putc(&sb, format[i]);
3136 free(hostname, M_TEMP);
3137 #ifdef COMPRESS_USER_CORES
3139 sbuf_printf(&sb, GZ_SUFFIX);
3142 if (sbuf_error(&sb) != 0) {
3143 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3144 "long\n", (long)pid, name, (u_long)uid);
3154 * If the core format has a %I in it, then we need to check
3155 * for existing corefiles before returning a name.
3156 * To do this we iterate over 0..num_cores to find a
3157 * non-existing core file name to use.
3159 if (indexpos != -1) {
3160 struct nameidata nd;
3162 int flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3163 int cmode = S_IRUSR | S_IWUSR;
3166 for (n = 0; n < num_cores; n++) {
3167 temp[indexpos] = '0' + n;
3168 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE,
3170 error = vn_open(&nd, &flags, cmode, NULL);
3172 if (error == EEXIST) {
3176 "pid %d (%s), uid (%u): Path `%s' failed "
3177 "on initial open test, error = %d\n",
3178 pid, name, uid, temp, error);
3182 vfslocked = NDHASGIANT(&nd);
3183 NDFREE(&nd, NDF_ONLY_PNBUF);
3184 VOP_UNLOCK(nd.ni_vp, 0);
3185 error = vn_close(nd.ni_vp, FWRITE, td->td_ucred, td);
3186 VFS_UNLOCK_GIANT(vfslocked);
3189 "pid %d (%s), uid (%u): Path `%s' failed "
3190 "on close after initial open test, "
3192 pid, name, uid, temp, error);
3203 * Dump a process' core. The main routine does some
3204 * policy checking, and creates the name of the coredump;
3205 * then it passes on a vnode and a size limit to the process-specific
3206 * coredump routine if there is one; if there _is not_ one, it returns
3207 * ENOSYS; otherwise it returns the error from the process-specific routine.
3211 coredump(struct thread *td)
3213 struct proc *p = td->td_proc;
3214 register struct vnode *vp;
3215 register struct ucred *cred = td->td_ucred;
3217 struct nameidata nd;
3219 int error, error1, flags, locked;
3221 char *name; /* name of corefile */
3226 #ifdef COMPRESS_USER_CORES
3227 compress = compress_user_cores;
3231 PROC_LOCK_ASSERT(p, MA_OWNED);
3232 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3233 _STOPEVENT(p, S_CORE, 0);
3235 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid, td,
3240 audit_proc_coredump(td, NULL, EINVAL);
3244 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3247 audit_proc_coredump(td, name, EFAULT);
3254 * Note that the bulk of limit checking is done after
3255 * the corefile is created. The exception is if the limit
3256 * for corefiles is 0, in which case we don't bother
3257 * creating the corefile at all. This layout means that
3258 * a corefile is truncated instead of not being created,
3259 * if it is larger than the limit.
3261 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3262 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3265 audit_proc_coredump(td, name, EFBIG);
3273 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3274 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3275 error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
3279 audit_proc_coredump(td, name, error);
3284 vfslocked = NDHASGIANT(&nd);
3285 NDFREE(&nd, NDF_ONLY_PNBUF);
3288 /* Don't dump to non-regular files or files with links. */
3289 if (vp->v_type != VREG ||
3290 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
3297 lf.l_whence = SEEK_SET;
3300 lf.l_type = F_WRLCK;
3301 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3303 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3304 lf.l_type = F_UNLCK;
3306 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3307 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3309 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3311 VFS_UNLOCK_GIANT(vfslocked);
3317 if (set_core_nodump_flag)
3318 vattr.va_flags = UF_NODUMP;
3319 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3320 VOP_SETATTR(vp, &vattr, cred);
3322 vn_finished_write(mp);
3324 p->p_acflag |= ACORE;
3327 error = p->p_sysent->sv_coredump ?
3328 p->p_sysent->sv_coredump(td, vp, limit, compress ? IMGACT_CORE_COMPRESS : 0) :
3332 lf.l_type = F_UNLCK;
3333 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3336 error1 = vn_close(vp, FWRITE, cred, td);
3341 audit_proc_coredump(td, name, error);
3344 VFS_UNLOCK_GIANT(vfslocked);
3349 * Nonexistent system call-- signal process (may want to handle it). Flag
3350 * error in case process won't see signal immediately (blocked or ignored).
3352 #ifndef _SYS_SYSPROTO_H_
3361 struct nosys_args *args;
3363 struct proc *p = td->td_proc;
3366 tdsignal(td, SIGSYS);
3372 * Send a SIGIO or SIGURG signal to a process or process group using stored
3373 * credentials rather than those of the current process.
3376 pgsigio(sigiop, sig, checkctty)
3377 struct sigio **sigiop;
3381 struct sigio *sigio;
3383 ksiginfo_init(&ksi);
3384 ksi.ksi_signo = sig;
3385 ksi.ksi_code = SI_KERNEL;
3389 if (sigio == NULL) {
3393 if (sigio->sio_pgid > 0) {
3394 PROC_LOCK(sigio->sio_proc);
3395 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3396 kern_psignal(sigio->sio_proc, sig);
3397 PROC_UNLOCK(sigio->sio_proc);
3398 } else if (sigio->sio_pgid < 0) {
3401 PGRP_LOCK(sigio->sio_pgrp);
3402 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3404 if (p->p_state == PRS_NORMAL &&
3405 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3406 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3407 kern_psignal(p, sig);
3410 PGRP_UNLOCK(sigio->sio_pgrp);
3416 filt_sigattach(struct knote *kn)
3418 struct proc *p = curproc;
3420 kn->kn_ptr.p_proc = p;
3421 kn->kn_flags |= EV_CLEAR; /* automatically set */
3423 knlist_add(&p->p_klist, kn, 0);
3429 filt_sigdetach(struct knote *kn)
3431 struct proc *p = kn->kn_ptr.p_proc;
3433 knlist_remove(&p->p_klist, kn, 0);
3437 * signal knotes are shared with proc knotes, so we apply a mask to
3438 * the hint in order to differentiate them from process hints. This
3439 * could be avoided by using a signal-specific knote list, but probably
3440 * isn't worth the trouble.
3443 filt_signal(struct knote *kn, long hint)
3446 if (hint & NOTE_SIGNAL) {
3447 hint &= ~NOTE_SIGNAL;
3449 if (kn->kn_id == hint)
3452 return (kn->kn_data != 0);
3460 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3462 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3467 sigacts_free(struct sigacts *ps)
3470 mtx_lock(&ps->ps_mtx);
3472 if (ps->ps_refcnt == 0) {
3473 mtx_destroy(&ps->ps_mtx);
3474 free(ps, M_SUBPROC);
3476 mtx_unlock(&ps->ps_mtx);
3480 sigacts_hold(struct sigacts *ps)
3482 mtx_lock(&ps->ps_mtx);
3484 mtx_unlock(&ps->ps_mtx);
3489 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3492 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3493 mtx_lock(&src->ps_mtx);
3494 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3495 mtx_unlock(&src->ps_mtx);
3499 sigacts_shared(struct sigacts *ps)
3503 mtx_lock(&ps->ps_mtx);
3504 shared = ps->ps_refcnt > 1;
3505 mtx_unlock(&ps->ps_mtx);