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_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
49 #include <sys/capability.h>
50 #include <sys/condvar.h>
51 #include <sys/event.h>
52 #include <sys/fcntl.h>
53 #include <sys/imgact.h>
54 #include <sys/kernel.h>
56 #include <sys/ktrace.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/namei.h>
62 #include <sys/procdesc.h>
63 #include <sys/posix4.h>
64 #include <sys/pioctl.h>
65 #include <sys/racct.h>
66 #include <sys/resourcevar.h>
69 #include <sys/sleepqueue.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
75 #include <sys/sysent.h>
76 #include <sys/syslog.h>
77 #include <sys/sysproto.h>
78 #include <sys/timers.h>
79 #include <sys/unistd.h>
82 #include <vm/vm_extern.h>
87 #include <machine/cpu.h>
89 #include <security/audit/audit.h>
91 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
93 SDT_PROVIDER_DECLARE(proc);
94 SDT_PROBE_DEFINE3(proc, kernel, , signal__send, "struct thread *",
95 "struct proc *", "int");
96 SDT_PROBE_DEFINE2(proc, kernel, , signal__clear, "int",
98 SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
99 "struct thread *", "struct proc *", "int");
101 static int coredump(struct thread *);
102 static int killpg1(struct thread *td, int sig, int pgid, int all,
104 static int issignal(struct thread *td);
105 static int sigprop(int sig);
106 static void tdsigwakeup(struct thread *, int, sig_t, int);
107 static void sig_suspend_threads(struct thread *, struct proc *, int);
108 static int filt_sigattach(struct knote *kn);
109 static void filt_sigdetach(struct knote *kn);
110 static int filt_signal(struct knote *kn, long hint);
111 static struct thread *sigtd(struct proc *p, int sig, int prop);
112 static void sigqueue_start(void);
114 static uma_zone_t ksiginfo_zone = NULL;
115 struct filterops sig_filtops = {
117 .f_attach = filt_sigattach,
118 .f_detach = filt_sigdetach,
119 .f_event = filt_signal,
122 static int kern_logsigexit = 1;
123 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
125 "Log processes quitting on abnormal signals to syslog(3)");
127 static int kern_forcesigexit = 1;
128 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
129 &kern_forcesigexit, 0, "Force trap signal to be handled");
131 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
132 "POSIX real time signal");
134 static int max_pending_per_proc = 128;
135 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
136 &max_pending_per_proc, 0, "Max pending signals per proc");
138 static int preallocate_siginfo = 1024;
139 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
140 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
141 &preallocate_siginfo, 0, "Preallocated signal memory size");
143 static int signal_overflow = 0;
144 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
145 &signal_overflow, 0, "Number of signals overflew");
147 static int signal_alloc_fail = 0;
148 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
149 &signal_alloc_fail, 0, "signals failed to be allocated");
151 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
154 * Policy -- Can ucred cr1 send SIGIO to process cr2?
155 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
156 * in the right situations.
158 #define CANSIGIO(cr1, cr2) \
159 ((cr1)->cr_uid == 0 || \
160 (cr1)->cr_ruid == (cr2)->cr_ruid || \
161 (cr1)->cr_uid == (cr2)->cr_ruid || \
162 (cr1)->cr_ruid == (cr2)->cr_uid || \
163 (cr1)->cr_uid == (cr2)->cr_uid)
165 static int sugid_coredump;
166 TUNABLE_INT("kern.sugid_coredump", &sugid_coredump);
167 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
168 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
170 static int capmode_coredump;
171 TUNABLE_INT("kern.capmode_coredump", &capmode_coredump);
172 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RW,
173 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
175 static int do_coredump = 1;
176 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
177 &do_coredump, 0, "Enable/Disable coredumps");
179 static int set_core_nodump_flag = 0;
180 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
181 0, "Enable setting the NODUMP flag on coredump files");
184 * Signal properties and actions.
185 * The array below categorizes the signals and their default actions
186 * according to the following properties:
188 #define SA_KILL 0x01 /* terminates process by default */
189 #define SA_CORE 0x02 /* ditto and coredumps */
190 #define SA_STOP 0x04 /* suspend process */
191 #define SA_TTYSTOP 0x08 /* ditto, from tty */
192 #define SA_IGNORE 0x10 /* ignore by default */
193 #define SA_CONT 0x20 /* continue if suspended */
194 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
196 static int sigproptbl[NSIG] = {
197 SA_KILL, /* SIGHUP */
198 SA_KILL, /* SIGINT */
199 SA_KILL|SA_CORE, /* SIGQUIT */
200 SA_KILL|SA_CORE, /* SIGILL */
201 SA_KILL|SA_CORE, /* SIGTRAP */
202 SA_KILL|SA_CORE, /* SIGABRT */
203 SA_KILL|SA_CORE, /* SIGEMT */
204 SA_KILL|SA_CORE, /* SIGFPE */
205 SA_KILL, /* SIGKILL */
206 SA_KILL|SA_CORE, /* SIGBUS */
207 SA_KILL|SA_CORE, /* SIGSEGV */
208 SA_KILL|SA_CORE, /* SIGSYS */
209 SA_KILL, /* SIGPIPE */
210 SA_KILL, /* SIGALRM */
211 SA_KILL, /* SIGTERM */
212 SA_IGNORE, /* SIGURG */
213 SA_STOP, /* SIGSTOP */
214 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
215 SA_IGNORE|SA_CONT, /* SIGCONT */
216 SA_IGNORE, /* SIGCHLD */
217 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
218 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
219 SA_IGNORE, /* SIGIO */
220 SA_KILL, /* SIGXCPU */
221 SA_KILL, /* SIGXFSZ */
222 SA_KILL, /* SIGVTALRM */
223 SA_KILL, /* SIGPROF */
224 SA_IGNORE, /* SIGWINCH */
225 SA_IGNORE, /* SIGINFO */
226 SA_KILL, /* SIGUSR1 */
227 SA_KILL, /* SIGUSR2 */
230 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
235 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
236 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
237 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
238 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
239 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
240 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
244 ksiginfo_alloc(int wait)
251 if (ksiginfo_zone != NULL)
252 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
257 ksiginfo_free(ksiginfo_t *ksi)
259 uma_zfree(ksiginfo_zone, ksi);
263 ksiginfo_tryfree(ksiginfo_t *ksi)
265 if (!(ksi->ksi_flags & KSI_EXT)) {
266 uma_zfree(ksiginfo_zone, ksi);
273 sigqueue_init(sigqueue_t *list, struct proc *p)
275 SIGEMPTYSET(list->sq_signals);
276 SIGEMPTYSET(list->sq_kill);
277 TAILQ_INIT(&list->sq_list);
279 list->sq_flags = SQ_INIT;
283 * Get a signal's ksiginfo.
285 * 0 - signal not found
286 * others - signal number
289 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
291 struct proc *p = sq->sq_proc;
292 struct ksiginfo *ksi, *next;
295 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
297 if (!SIGISMEMBER(sq->sq_signals, signo))
300 if (SIGISMEMBER(sq->sq_kill, signo)) {
302 SIGDELSET(sq->sq_kill, signo);
305 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
306 if (ksi->ksi_signo == signo) {
308 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
309 ksi->ksi_sigq = NULL;
310 ksiginfo_copy(ksi, si);
311 if (ksiginfo_tryfree(ksi) && p != NULL)
320 SIGDELSET(sq->sq_signals, signo);
321 si->ksi_signo = signo;
326 sigqueue_take(ksiginfo_t *ksi)
332 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
336 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
337 ksi->ksi_sigq = NULL;
338 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
341 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
342 kp = TAILQ_NEXT(kp, ksi_link)) {
343 if (kp->ksi_signo == ksi->ksi_signo)
346 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
347 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
351 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
353 struct proc *p = sq->sq_proc;
354 struct ksiginfo *ksi;
357 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
359 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
360 SIGADDSET(sq->sq_kill, signo);
364 /* directly insert the ksi, don't copy it */
365 if (si->ksi_flags & KSI_INS) {
366 if (si->ksi_flags & KSI_HEAD)
367 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
369 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
374 if (__predict_false(ksiginfo_zone == NULL)) {
375 SIGADDSET(sq->sq_kill, signo);
379 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
382 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
388 ksiginfo_copy(si, ksi);
389 ksi->ksi_signo = signo;
390 if (si->ksi_flags & KSI_HEAD)
391 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
393 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
397 if ((si->ksi_flags & KSI_TRAP) != 0 ||
398 (si->ksi_flags & KSI_SIGQ) == 0) {
400 SIGADDSET(sq->sq_kill, signo);
409 SIGADDSET(sq->sq_signals, signo);
414 sigqueue_flush(sigqueue_t *sq)
416 struct proc *p = sq->sq_proc;
419 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
422 PROC_LOCK_ASSERT(p, MA_OWNED);
424 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
425 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
426 ksi->ksi_sigq = NULL;
427 if (ksiginfo_tryfree(ksi) && p != NULL)
431 SIGEMPTYSET(sq->sq_signals);
432 SIGEMPTYSET(sq->sq_kill);
436 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
439 struct proc *p1, *p2;
440 ksiginfo_t *ksi, *next;
442 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
443 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
446 /* Move siginfo to target list */
447 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
448 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
449 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
452 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
459 /* Move pending bits to target list */
461 SIGSETAND(tmp, *set);
462 SIGSETOR(dst->sq_kill, tmp);
463 SIGSETNAND(src->sq_kill, tmp);
465 tmp = src->sq_signals;
466 SIGSETAND(tmp, *set);
467 SIGSETOR(dst->sq_signals, tmp);
468 SIGSETNAND(src->sq_signals, tmp);
473 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
478 SIGADDSET(set, signo);
479 sigqueue_move_set(src, dst, &set);
484 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
486 struct proc *p = sq->sq_proc;
487 ksiginfo_t *ksi, *next;
489 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
491 /* Remove siginfo queue */
492 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
493 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
494 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
495 ksi->ksi_sigq = NULL;
496 if (ksiginfo_tryfree(ksi) && p != NULL)
500 SIGSETNAND(sq->sq_kill, *set);
501 SIGSETNAND(sq->sq_signals, *set);
505 sigqueue_delete(sigqueue_t *sq, int signo)
510 SIGADDSET(set, signo);
511 sigqueue_delete_set(sq, &set);
514 /* Remove a set of signals for a process */
516 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
521 PROC_LOCK_ASSERT(p, MA_OWNED);
523 sigqueue_init(&worklist, NULL);
524 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
526 FOREACH_THREAD_IN_PROC(p, td0)
527 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
529 sigqueue_flush(&worklist);
533 sigqueue_delete_proc(struct proc *p, int signo)
538 SIGADDSET(set, signo);
539 sigqueue_delete_set_proc(p, &set);
543 sigqueue_delete_stopmask_proc(struct proc *p)
548 SIGADDSET(set, SIGSTOP);
549 SIGADDSET(set, SIGTSTP);
550 SIGADDSET(set, SIGTTIN);
551 SIGADDSET(set, SIGTTOU);
552 sigqueue_delete_set_proc(p, &set);
556 * Determine signal that should be delivered to thread td, the current
557 * thread, 0 if none. If there is a pending stop signal with default
558 * action, the process stops in issignal().
561 cursig(struct thread *td)
563 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
564 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
565 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
566 return (SIGPENDING(td) ? issignal(td) : 0);
570 * Arrange for ast() to handle unmasked pending signals on return to user
571 * mode. This must be called whenever a signal is added to td_sigqueue or
572 * unmasked in td_sigmask.
575 signotify(struct thread *td)
581 PROC_LOCK_ASSERT(p, MA_OWNED);
583 if (SIGPENDING(td)) {
585 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
591 sigonstack(size_t sp)
593 struct thread *td = curthread;
595 return ((td->td_pflags & TDP_ALTSTACK) ?
596 #if defined(COMPAT_43)
597 ((td->td_sigstk.ss_size == 0) ?
598 (td->td_sigstk.ss_flags & SS_ONSTACK) :
599 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
601 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
610 if (sig > 0 && sig < NSIG)
611 return (sigproptbl[_SIG_IDX(sig)]);
616 sig_ffs(sigset_t *set)
620 for (i = 0; i < _SIG_WORDS; i++)
622 return (ffs(set->__bits[i]) + (i * 32));
633 kern_sigaction(td, sig, act, oact, flags)
636 struct sigaction *act, *oact;
640 struct proc *p = td->td_proc;
642 if (!_SIG_VALID(sig))
647 mtx_lock(&ps->ps_mtx);
649 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
651 if (SIGISMEMBER(ps->ps_sigonstack, sig))
652 oact->sa_flags |= SA_ONSTACK;
653 if (!SIGISMEMBER(ps->ps_sigintr, sig))
654 oact->sa_flags |= SA_RESTART;
655 if (SIGISMEMBER(ps->ps_sigreset, sig))
656 oact->sa_flags |= SA_RESETHAND;
657 if (SIGISMEMBER(ps->ps_signodefer, sig))
658 oact->sa_flags |= SA_NODEFER;
659 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
660 oact->sa_flags |= SA_SIGINFO;
662 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
664 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
665 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
666 oact->sa_flags |= SA_NOCLDSTOP;
667 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
668 oact->sa_flags |= SA_NOCLDWAIT;
671 if ((sig == SIGKILL || sig == SIGSTOP) &&
672 act->sa_handler != SIG_DFL) {
673 mtx_unlock(&ps->ps_mtx);
679 * Change setting atomically.
682 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
683 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
684 if (act->sa_flags & SA_SIGINFO) {
685 ps->ps_sigact[_SIG_IDX(sig)] =
686 (__sighandler_t *)act->sa_sigaction;
687 SIGADDSET(ps->ps_siginfo, sig);
689 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
690 SIGDELSET(ps->ps_siginfo, sig);
692 if (!(act->sa_flags & SA_RESTART))
693 SIGADDSET(ps->ps_sigintr, sig);
695 SIGDELSET(ps->ps_sigintr, sig);
696 if (act->sa_flags & SA_ONSTACK)
697 SIGADDSET(ps->ps_sigonstack, sig);
699 SIGDELSET(ps->ps_sigonstack, sig);
700 if (act->sa_flags & SA_RESETHAND)
701 SIGADDSET(ps->ps_sigreset, sig);
703 SIGDELSET(ps->ps_sigreset, sig);
704 if (act->sa_flags & SA_NODEFER)
705 SIGADDSET(ps->ps_signodefer, sig);
707 SIGDELSET(ps->ps_signodefer, sig);
708 if (sig == SIGCHLD) {
709 if (act->sa_flags & SA_NOCLDSTOP)
710 ps->ps_flag |= PS_NOCLDSTOP;
712 ps->ps_flag &= ~PS_NOCLDSTOP;
713 if (act->sa_flags & SA_NOCLDWAIT) {
715 * Paranoia: since SA_NOCLDWAIT is implemented
716 * by reparenting the dying child to PID 1 (and
717 * trust it to reap the zombie), PID 1 itself
718 * is forbidden to set SA_NOCLDWAIT.
721 ps->ps_flag &= ~PS_NOCLDWAIT;
723 ps->ps_flag |= PS_NOCLDWAIT;
725 ps->ps_flag &= ~PS_NOCLDWAIT;
726 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
727 ps->ps_flag |= PS_CLDSIGIGN;
729 ps->ps_flag &= ~PS_CLDSIGIGN;
732 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
733 * and for signals set to SIG_DFL where the default is to
734 * ignore. However, don't put SIGCONT in ps_sigignore, as we
735 * have to restart the process.
737 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
738 (sigprop(sig) & SA_IGNORE &&
739 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
740 /* never to be seen again */
741 sigqueue_delete_proc(p, sig);
743 /* easier in psignal */
744 SIGADDSET(ps->ps_sigignore, sig);
745 SIGDELSET(ps->ps_sigcatch, sig);
747 SIGDELSET(ps->ps_sigignore, sig);
748 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
749 SIGDELSET(ps->ps_sigcatch, sig);
751 SIGADDSET(ps->ps_sigcatch, sig);
753 #ifdef COMPAT_FREEBSD4
754 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
755 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
756 (flags & KSA_FREEBSD4) == 0)
757 SIGDELSET(ps->ps_freebsd4, sig);
759 SIGADDSET(ps->ps_freebsd4, sig);
762 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
763 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
764 (flags & KSA_OSIGSET) == 0)
765 SIGDELSET(ps->ps_osigset, sig);
767 SIGADDSET(ps->ps_osigset, sig);
770 mtx_unlock(&ps->ps_mtx);
775 #ifndef _SYS_SYSPROTO_H_
776 struct sigaction_args {
778 struct sigaction *act;
779 struct sigaction *oact;
783 sys_sigaction(td, uap)
785 register struct sigaction_args *uap;
787 struct sigaction act, oact;
788 register struct sigaction *actp, *oactp;
791 actp = (uap->act != NULL) ? &act : NULL;
792 oactp = (uap->oact != NULL) ? &oact : NULL;
794 error = copyin(uap->act, actp, sizeof(act));
798 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
800 error = copyout(oactp, uap->oact, sizeof(oact));
804 #ifdef COMPAT_FREEBSD4
805 #ifndef _SYS_SYSPROTO_H_
806 struct freebsd4_sigaction_args {
808 struct sigaction *act;
809 struct sigaction *oact;
813 freebsd4_sigaction(td, uap)
815 register struct freebsd4_sigaction_args *uap;
817 struct sigaction act, oact;
818 register struct sigaction *actp, *oactp;
822 actp = (uap->act != NULL) ? &act : NULL;
823 oactp = (uap->oact != NULL) ? &oact : NULL;
825 error = copyin(uap->act, actp, sizeof(act));
829 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
831 error = copyout(oactp, uap->oact, sizeof(oact));
834 #endif /* COMAPT_FREEBSD4 */
836 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
837 #ifndef _SYS_SYSPROTO_H_
838 struct osigaction_args {
840 struct osigaction *nsa;
841 struct osigaction *osa;
847 register struct osigaction_args *uap;
849 struct osigaction sa;
850 struct sigaction nsa, osa;
851 register struct sigaction *nsap, *osap;
854 if (uap->signum <= 0 || uap->signum >= ONSIG)
857 nsap = (uap->nsa != NULL) ? &nsa : NULL;
858 osap = (uap->osa != NULL) ? &osa : NULL;
861 error = copyin(uap->nsa, &sa, sizeof(sa));
864 nsap->sa_handler = sa.sa_handler;
865 nsap->sa_flags = sa.sa_flags;
866 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
868 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
869 if (osap && !error) {
870 sa.sa_handler = osap->sa_handler;
871 sa.sa_flags = osap->sa_flags;
872 SIG2OSIG(osap->sa_mask, sa.sa_mask);
873 error = copyout(&sa, uap->osa, sizeof(sa));
878 #if !defined(__i386__)
879 /* Avoid replicating the same stub everywhere */
883 struct osigreturn_args *uap;
886 return (nosys(td, (struct nosys_args *)uap));
889 #endif /* COMPAT_43 */
892 * Initialize signal state for process 0;
893 * set to ignore signals that are ignored by default.
904 mtx_lock(&ps->ps_mtx);
905 for (i = 1; i <= NSIG; i++)
906 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
907 SIGADDSET(ps->ps_sigignore, i);
908 mtx_unlock(&ps->ps_mtx);
913 * Reset signals for an exec of the specified process.
916 execsigs(struct proc *p)
923 * Reset caught signals. Held signals remain held
924 * through td_sigmask (unless they were caught,
925 * and are now ignored by default).
927 PROC_LOCK_ASSERT(p, MA_OWNED);
928 td = FIRST_THREAD_IN_PROC(p);
930 mtx_lock(&ps->ps_mtx);
931 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
932 sig = sig_ffs(&ps->ps_sigcatch);
933 SIGDELSET(ps->ps_sigcatch, sig);
934 if (sigprop(sig) & SA_IGNORE) {
936 SIGADDSET(ps->ps_sigignore, sig);
937 sigqueue_delete_proc(p, sig);
939 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
942 * Reset stack state to the user stack.
943 * Clear set of signals caught on the signal stack.
945 td->td_sigstk.ss_flags = SS_DISABLE;
946 td->td_sigstk.ss_size = 0;
947 td->td_sigstk.ss_sp = 0;
948 td->td_pflags &= ~TDP_ALTSTACK;
950 * Reset no zombies if child dies flag as Solaris does.
952 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
953 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
954 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
955 mtx_unlock(&ps->ps_mtx);
961 * Manipulate signal mask.
964 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
967 sigset_t new_block, oset1;
972 if (!(flags & SIGPROCMASK_PROC_LOCKED))
975 *oset = td->td_sigmask;
982 oset1 = td->td_sigmask;
983 SIGSETOR(td->td_sigmask, *set);
984 new_block = td->td_sigmask;
985 SIGSETNAND(new_block, oset1);
988 SIGSETNAND(td->td_sigmask, *set);
993 oset1 = td->td_sigmask;
994 if (flags & SIGPROCMASK_OLD)
995 SIGSETLO(td->td_sigmask, *set);
997 td->td_sigmask = *set;
998 new_block = td->td_sigmask;
999 SIGSETNAND(new_block, oset1);
1008 * The new_block set contains signals that were not previously
1009 * blocked, but are blocked now.
1011 * In case we block any signal that was not previously blocked
1012 * for td, and process has the signal pending, try to schedule
1013 * signal delivery to some thread that does not block the
1014 * signal, possibly waking it up.
1016 if (p->p_numthreads != 1)
1017 reschedule_signals(p, new_block, flags);
1021 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1026 #ifndef _SYS_SYSPROTO_H_
1027 struct sigprocmask_args {
1029 const sigset_t *set;
1034 sys_sigprocmask(td, uap)
1035 register struct thread *td;
1036 struct sigprocmask_args *uap;
1039 sigset_t *setp, *osetp;
1042 setp = (uap->set != NULL) ? &set : NULL;
1043 osetp = (uap->oset != NULL) ? &oset : NULL;
1045 error = copyin(uap->set, setp, sizeof(set));
1049 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1050 if (osetp && !error) {
1051 error = copyout(osetp, uap->oset, sizeof(oset));
1056 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1057 #ifndef _SYS_SYSPROTO_H_
1058 struct osigprocmask_args {
1064 osigprocmask(td, uap)
1065 register struct thread *td;
1066 struct osigprocmask_args *uap;
1071 OSIG2SIG(uap->mask, set);
1072 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1073 SIG2OSIG(oset, td->td_retval[0]);
1076 #endif /* COMPAT_43 */
1079 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1085 error = copyin(uap->set, &set, sizeof(set));
1087 td->td_retval[0] = error;
1091 error = kern_sigtimedwait(td, set, &ksi, NULL);
1093 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1095 if (error == ERESTART)
1097 td->td_retval[0] = error;
1101 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1102 td->td_retval[0] = error;
1107 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1110 struct timespec *timeout;
1116 error = copyin(uap->timeout, &ts, sizeof(ts));
1124 error = copyin(uap->set, &set, sizeof(set));
1128 error = kern_sigtimedwait(td, set, &ksi, timeout);
1133 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1136 td->td_retval[0] = ksi.ksi_signo;
1141 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1147 error = copyin(uap->set, &set, sizeof(set));
1151 error = kern_sigtimedwait(td, set, &ksi, NULL);
1156 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1159 td->td_retval[0] = ksi.ksi_signo;
1164 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1165 struct timespec *timeout)
1168 sigset_t saved_mask, new_block;
1170 int error, sig, timo, timevalid = 0;
1171 struct timespec rts, ets, ts;
1179 if (timeout != NULL) {
1180 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1182 getnanouptime(&rts);
1184 timespecadd(&ets, timeout);
1188 /* Some signals can not be waited for. */
1189 SIG_CANTMASK(waitset);
1192 saved_mask = td->td_sigmask;
1193 SIGSETNAND(td->td_sigmask, waitset);
1195 mtx_lock(&ps->ps_mtx);
1197 mtx_unlock(&ps->ps_mtx);
1198 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1199 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1200 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1210 * POSIX says this must be checked after looking for pending
1213 if (timeout != NULL) {
1218 getnanouptime(&rts);
1219 if (timespeccmp(&rts, &ets, >=)) {
1224 timespecsub(&ts, &rts);
1225 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1231 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1233 if (timeout != NULL) {
1234 if (error == ERESTART) {
1235 /* Timeout can not be restarted. */
1237 } else if (error == EAGAIN) {
1238 /* We will calculate timeout by ourself. */
1244 new_block = saved_mask;
1245 SIGSETNAND(new_block, td->td_sigmask);
1246 td->td_sigmask = saved_mask;
1248 * Fewer signals can be delivered to us, reschedule signal
1251 if (p->p_numthreads != 1)
1252 reschedule_signals(p, new_block, 0);
1255 SDT_PROBE(proc, kernel, , signal__clear, sig, ksi, 0, 0, 0);
1257 if (ksi->ksi_code == SI_TIMER)
1258 itimer_accept(p, ksi->ksi_timerid, ksi);
1261 if (KTRPOINT(td, KTR_PSIG)) {
1264 mtx_lock(&ps->ps_mtx);
1265 action = ps->ps_sigact[_SIG_IDX(sig)];
1266 mtx_unlock(&ps->ps_mtx);
1267 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1277 #ifndef _SYS_SYSPROTO_H_
1278 struct sigpending_args {
1283 sys_sigpending(td, uap)
1285 struct sigpending_args *uap;
1287 struct proc *p = td->td_proc;
1291 pending = p->p_sigqueue.sq_signals;
1292 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1294 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1297 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1298 #ifndef _SYS_SYSPROTO_H_
1299 struct osigpending_args {
1304 osigpending(td, uap)
1306 struct osigpending_args *uap;
1308 struct proc *p = td->td_proc;
1312 pending = p->p_sigqueue.sq_signals;
1313 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1315 SIG2OSIG(pending, td->td_retval[0]);
1318 #endif /* COMPAT_43 */
1320 #if defined(COMPAT_43)
1322 * Generalized interface signal handler, 4.3-compatible.
1324 #ifndef _SYS_SYSPROTO_H_
1325 struct osigvec_args {
1335 register struct osigvec_args *uap;
1338 struct sigaction nsa, osa;
1339 register struct sigaction *nsap, *osap;
1342 if (uap->signum <= 0 || uap->signum >= ONSIG)
1344 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1345 osap = (uap->osv != NULL) ? &osa : NULL;
1347 error = copyin(uap->nsv, &vec, sizeof(vec));
1350 nsap->sa_handler = vec.sv_handler;
1351 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1352 nsap->sa_flags = vec.sv_flags;
1353 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1355 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1356 if (osap && !error) {
1357 vec.sv_handler = osap->sa_handler;
1358 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1359 vec.sv_flags = osap->sa_flags;
1360 vec.sv_flags &= ~SA_NOCLDWAIT;
1361 vec.sv_flags ^= SA_RESTART;
1362 error = copyout(&vec, uap->osv, sizeof(vec));
1367 #ifndef _SYS_SYSPROTO_H_
1368 struct osigblock_args {
1374 register struct thread *td;
1375 struct osigblock_args *uap;
1379 OSIG2SIG(uap->mask, set);
1380 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1381 SIG2OSIG(oset, td->td_retval[0]);
1385 #ifndef _SYS_SYSPROTO_H_
1386 struct osigsetmask_args {
1391 osigsetmask(td, uap)
1393 struct osigsetmask_args *uap;
1397 OSIG2SIG(uap->mask, set);
1398 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1399 SIG2OSIG(oset, td->td_retval[0]);
1402 #endif /* COMPAT_43 */
1405 * Suspend calling thread until signal, providing mask to be set in the
1408 #ifndef _SYS_SYSPROTO_H_
1409 struct sigsuspend_args {
1410 const sigset_t *sigmask;
1415 sys_sigsuspend(td, uap)
1417 struct sigsuspend_args *uap;
1422 error = copyin(uap->sigmask, &mask, sizeof(mask));
1425 return (kern_sigsuspend(td, mask));
1429 kern_sigsuspend(struct thread *td, sigset_t mask)
1431 struct proc *p = td->td_proc;
1435 * When returning from sigsuspend, we want
1436 * the old mask to be restored after the
1437 * signal handler has finished. Thus, we
1438 * save it here and mark the sigacts structure
1442 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1443 SIGPROCMASK_PROC_LOCKED);
1444 td->td_pflags |= TDP_OLDMASK;
1447 * Process signals now. Otherwise, we can get spurious wakeup
1448 * due to signal entered process queue, but delivered to other
1449 * thread. But sigsuspend should return only on signal
1452 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1453 for (has_sig = 0; !has_sig;) {
1454 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1457 thread_suspend_check(0);
1458 mtx_lock(&p->p_sigacts->ps_mtx);
1459 while ((sig = cursig(td)) != 0)
1460 has_sig += postsig(sig);
1461 mtx_unlock(&p->p_sigacts->ps_mtx);
1464 td->td_errno = EINTR;
1465 td->td_pflags |= TDP_NERRNO;
1466 return (EJUSTRETURN);
1469 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1471 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1472 * convention: libc stub passes mask, not pointer, to save a copyin.
1474 #ifndef _SYS_SYSPROTO_H_
1475 struct osigsuspend_args {
1481 osigsuspend(td, uap)
1483 struct osigsuspend_args *uap;
1487 OSIG2SIG(uap->mask, mask);
1488 return (kern_sigsuspend(td, mask));
1490 #endif /* COMPAT_43 */
1492 #if defined(COMPAT_43)
1493 #ifndef _SYS_SYSPROTO_H_
1494 struct osigstack_args {
1495 struct sigstack *nss;
1496 struct sigstack *oss;
1503 register struct osigstack_args *uap;
1505 struct sigstack nss, oss;
1508 if (uap->nss != NULL) {
1509 error = copyin(uap->nss, &nss, sizeof(nss));
1513 oss.ss_sp = td->td_sigstk.ss_sp;
1514 oss.ss_onstack = sigonstack(cpu_getstack(td));
1515 if (uap->nss != NULL) {
1516 td->td_sigstk.ss_sp = nss.ss_sp;
1517 td->td_sigstk.ss_size = 0;
1518 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1519 td->td_pflags |= TDP_ALTSTACK;
1521 if (uap->oss != NULL)
1522 error = copyout(&oss, uap->oss, sizeof(oss));
1526 #endif /* COMPAT_43 */
1528 #ifndef _SYS_SYSPROTO_H_
1529 struct sigaltstack_args {
1536 sys_sigaltstack(td, uap)
1538 register struct sigaltstack_args *uap;
1543 if (uap->ss != NULL) {
1544 error = copyin(uap->ss, &ss, sizeof(ss));
1548 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1549 (uap->oss != NULL) ? &oss : NULL);
1552 if (uap->oss != NULL)
1553 error = copyout(&oss, uap->oss, sizeof(stack_t));
1558 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1560 struct proc *p = td->td_proc;
1563 oonstack = sigonstack(cpu_getstack(td));
1566 *oss = td->td_sigstk;
1567 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1568 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1574 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1576 if (!(ss->ss_flags & SS_DISABLE)) {
1577 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1580 td->td_sigstk = *ss;
1581 td->td_pflags |= TDP_ALTSTACK;
1583 td->td_pflags &= ~TDP_ALTSTACK;
1590 * Common code for kill process group/broadcast kill.
1591 * cp is calling process.
1594 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 err = p_cansignal(td, p, sig);
1617 pksignal(p, sig, ksi);
1620 else if (ret == ESRCH)
1624 sx_sunlock(&allproc_lock);
1626 sx_slock(&proctree_lock);
1629 * zero pgid means send to my process group.
1631 pgrp = td->td_proc->p_pgrp;
1634 pgrp = pgfind(pgid);
1636 sx_sunlock(&proctree_lock);
1640 sx_sunlock(&proctree_lock);
1641 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1643 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1644 p->p_state == PRS_NEW) {
1648 err = p_cansignal(td, p, sig);
1651 pksignal(p, sig, ksi);
1654 else if (ret == ESRCH)
1663 #ifndef _SYS_SYSPROTO_H_
1671 sys_kill(struct thread *td, struct kill_args *uap)
1678 * A process in capability mode can send signals only to himself.
1679 * The main rationale behind this is that abort(3) is implemented as
1680 * kill(getpid(), SIGABRT).
1682 if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid)
1685 AUDIT_ARG_SIGNUM(uap->signum);
1686 AUDIT_ARG_PID(uap->pid);
1687 if ((u_int)uap->signum > _SIG_MAXSIG)
1690 ksiginfo_init(&ksi);
1691 ksi.ksi_signo = uap->signum;
1692 ksi.ksi_code = SI_USER;
1693 ksi.ksi_pid = td->td_proc->p_pid;
1694 ksi.ksi_uid = td->td_ucred->cr_ruid;
1697 /* kill single process */
1698 if ((p = pfind(uap->pid)) == NULL) {
1699 if ((p = zpfind(uap->pid)) == NULL)
1702 AUDIT_ARG_PROCESS(p);
1703 error = p_cansignal(td, p, uap->signum);
1704 if (error == 0 && uap->signum)
1705 pksignal(p, uap->signum, &ksi);
1710 case -1: /* broadcast signal */
1711 return (killpg1(td, uap->signum, 0, 1, &ksi));
1712 case 0: /* signal own process group */
1713 return (killpg1(td, uap->signum, 0, 0, &ksi));
1714 default: /* negative explicit process group */
1715 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1723 struct pdkill_args *uap;
1726 cap_rights_t rights;
1729 AUDIT_ARG_SIGNUM(uap->signum);
1730 AUDIT_ARG_FD(uap->fd);
1731 if ((u_int)uap->signum > _SIG_MAXSIG)
1734 error = procdesc_find(td, uap->fd,
1735 cap_rights_init(&rights, CAP_PDKILL), &p);
1738 AUDIT_ARG_PROCESS(p);
1739 error = p_cansignal(td, p, uap->signum);
1740 if (error == 0 && uap->signum)
1741 kern_psignal(p, uap->signum);
1746 #if defined(COMPAT_43)
1747 #ifndef _SYS_SYSPROTO_H_
1748 struct okillpg_args {
1755 okillpg(struct thread *td, struct okillpg_args *uap)
1759 AUDIT_ARG_SIGNUM(uap->signum);
1760 AUDIT_ARG_PID(uap->pgid);
1761 if ((u_int)uap->signum > _SIG_MAXSIG)
1764 ksiginfo_init(&ksi);
1765 ksi.ksi_signo = uap->signum;
1766 ksi.ksi_code = SI_USER;
1767 ksi.ksi_pid = td->td_proc->p_pid;
1768 ksi.ksi_uid = td->td_ucred->cr_ruid;
1769 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1771 #endif /* COMPAT_43 */
1773 #ifndef _SYS_SYSPROTO_H_
1774 struct sigqueue_args {
1777 /* union sigval */ void *value;
1781 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1787 if ((u_int)uap->signum > _SIG_MAXSIG)
1791 * Specification says sigqueue can only send signal to
1797 if ((p = pfind(uap->pid)) == NULL) {
1798 if ((p = zpfind(uap->pid)) == NULL)
1801 error = p_cansignal(td, p, uap->signum);
1802 if (error == 0 && uap->signum != 0) {
1803 ksiginfo_init(&ksi);
1804 ksi.ksi_flags = KSI_SIGQ;
1805 ksi.ksi_signo = uap->signum;
1806 ksi.ksi_code = SI_QUEUE;
1807 ksi.ksi_pid = td->td_proc->p_pid;
1808 ksi.ksi_uid = td->td_ucred->cr_ruid;
1809 ksi.ksi_value.sival_ptr = uap->value;
1810 error = pksignal(p, ksi.ksi_signo, &ksi);
1817 * Send a signal to a process group.
1820 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1825 sx_slock(&proctree_lock);
1826 pgrp = pgfind(pgid);
1827 sx_sunlock(&proctree_lock);
1829 pgsignal(pgrp, sig, 0, ksi);
1836 * Send a signal to a process group. If checktty is 1,
1837 * limit to members which have a controlling terminal.
1840 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1845 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1846 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1848 if (p->p_state == PRS_NORMAL &&
1849 (checkctty == 0 || p->p_flag & P_CONTROLT))
1850 pksignal(p, sig, ksi);
1857 * Send a signal caused by a trap to the current thread. If it will be
1858 * caught immediately, deliver it with correct code. Otherwise, post it
1862 trapsignal(struct thread *td, ksiginfo_t *ksi)
1871 sig = ksi->ksi_signo;
1872 code = ksi->ksi_code;
1873 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1877 mtx_lock(&ps->ps_mtx);
1878 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1879 !SIGISMEMBER(td->td_sigmask, sig)) {
1880 td->td_ru.ru_nsignals++;
1882 if (KTRPOINT(curthread, KTR_PSIG))
1883 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1884 &td->td_sigmask, code);
1886 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1887 ksi, &td->td_sigmask);
1888 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1889 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1890 SIGADDSET(mask, sig);
1891 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1892 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1893 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1895 * See kern_sigaction() for origin of this code.
1897 SIGDELSET(ps->ps_sigcatch, sig);
1898 if (sig != SIGCONT &&
1899 sigprop(sig) & SA_IGNORE)
1900 SIGADDSET(ps->ps_sigignore, sig);
1901 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1903 mtx_unlock(&ps->ps_mtx);
1906 * Avoid a possible infinite loop if the thread
1907 * masking the signal or process is ignoring the
1910 if (kern_forcesigexit &&
1911 (SIGISMEMBER(td->td_sigmask, sig) ||
1912 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1913 SIGDELSET(td->td_sigmask, sig);
1914 SIGDELSET(ps->ps_sigcatch, sig);
1915 SIGDELSET(ps->ps_sigignore, sig);
1916 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1918 mtx_unlock(&ps->ps_mtx);
1919 p->p_code = code; /* XXX for core dump/debugger */
1920 p->p_sig = sig; /* XXX to verify code */
1921 tdsendsignal(p, td, sig, ksi);
1926 static struct thread *
1927 sigtd(struct proc *p, int sig, int prop)
1929 struct thread *td, *signal_td;
1931 PROC_LOCK_ASSERT(p, MA_OWNED);
1934 * Check if current thread can handle the signal without
1935 * switching context to another thread.
1937 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1940 FOREACH_THREAD_IN_PROC(p, td) {
1941 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1946 if (signal_td == NULL)
1947 signal_td = FIRST_THREAD_IN_PROC(p);
1952 * Send the signal to the process. If the signal has an action, the action
1953 * is usually performed by the target process rather than the caller; we add
1954 * the signal to the set of pending signals for the process.
1957 * o When a stop signal is sent to a sleeping process that takes the
1958 * default action, the process is stopped without awakening it.
1959 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1960 * regardless of the signal action (eg, blocked or ignored).
1962 * Other ignored signals are discarded immediately.
1964 * NB: This function may be entered from the debugger via the "kill" DDB
1965 * command. There is little that can be done to mitigate the possibly messy
1966 * side effects of this unwise possibility.
1969 kern_psignal(struct proc *p, int sig)
1973 ksiginfo_init(&ksi);
1974 ksi.ksi_signo = sig;
1975 ksi.ksi_code = SI_KERNEL;
1976 (void) tdsendsignal(p, NULL, sig, &ksi);
1980 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1983 return (tdsendsignal(p, NULL, sig, ksi));
1986 /* Utility function for finding a thread to send signal event to. */
1988 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
1992 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1993 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2005 tdsignal(struct thread *td, int sig)
2009 ksiginfo_init(&ksi);
2010 ksi.ksi_signo = sig;
2011 ksi.ksi_code = SI_KERNEL;
2012 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2016 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2019 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2023 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2026 sigqueue_t *sigqueue;
2033 MPASS(td == NULL || p == td->td_proc);
2034 PROC_LOCK_ASSERT(p, MA_OWNED);
2036 if (!_SIG_VALID(sig))
2037 panic("%s(): invalid signal %d", __func__, sig);
2039 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2042 * IEEE Std 1003.1-2001: return success when killing a zombie.
2044 if (p->p_state == PRS_ZOMBIE) {
2045 if (ksi && (ksi->ksi_flags & KSI_INS))
2046 ksiginfo_tryfree(ksi);
2051 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2052 prop = sigprop(sig);
2055 td = sigtd(p, sig, prop);
2056 sigqueue = &p->p_sigqueue;
2058 sigqueue = &td->td_sigqueue;
2060 SDT_PROBE(proc, kernel, , signal__send, td, p, sig, 0, 0 );
2063 * If the signal is being ignored,
2064 * then we forget about it immediately.
2065 * (Note: we don't set SIGCONT in ps_sigignore,
2066 * and if it is set to SIG_IGN,
2067 * action will be SIG_DFL here.)
2069 mtx_lock(&ps->ps_mtx);
2070 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2071 SDT_PROBE(proc, kernel, , signal__discard, td, p, sig, 0, 0 );
2073 mtx_unlock(&ps->ps_mtx);
2074 if (ksi && (ksi->ksi_flags & KSI_INS))
2075 ksiginfo_tryfree(ksi);
2078 if (SIGISMEMBER(td->td_sigmask, sig))
2080 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2084 if (SIGISMEMBER(ps->ps_sigintr, sig))
2088 mtx_unlock(&ps->ps_mtx);
2091 sigqueue_delete_stopmask_proc(p);
2092 else if (prop & SA_STOP) {
2094 * If sending a tty stop signal to a member of an orphaned
2095 * process group, discard the signal here if the action
2096 * is default; don't stop the process below if sleeping,
2097 * and don't clear any pending SIGCONT.
2099 if ((prop & SA_TTYSTOP) &&
2100 (p->p_pgrp->pg_jobc == 0) &&
2101 (action == SIG_DFL)) {
2102 if (ksi && (ksi->ksi_flags & KSI_INS))
2103 ksiginfo_tryfree(ksi);
2106 sigqueue_delete_proc(p, SIGCONT);
2107 if (p->p_flag & P_CONTINUED) {
2108 p->p_flag &= ~P_CONTINUED;
2109 PROC_LOCK(p->p_pptr);
2110 sigqueue_take(p->p_ksi);
2111 PROC_UNLOCK(p->p_pptr);
2115 ret = sigqueue_add(sigqueue, sig, ksi);
2120 * Defer further processing for signals which are held,
2121 * except that stopped processes must be continued by SIGCONT.
2123 if (action == SIG_HOLD &&
2124 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2127 * SIGKILL: Remove procfs STOPEVENTs.
2129 if (sig == SIGKILL) {
2130 /* from procfs_ioctl.c: PIOCBIC */
2132 /* from procfs_ioctl.c: PIOCCONT */
2137 * Some signals have a process-wide effect and a per-thread
2138 * component. Most processing occurs when the process next
2139 * tries to cross the user boundary, however there are some
2140 * times when processing needs to be done immediately, such as
2141 * waking up threads so that they can cross the user boundary.
2142 * We try to do the per-process part here.
2144 if (P_SHOULDSTOP(p)) {
2145 KASSERT(!(p->p_flag & P_WEXIT),
2146 ("signal to stopped but exiting process"));
2147 if (sig == SIGKILL) {
2149 * If traced process is already stopped,
2150 * then no further action is necessary.
2152 if (p->p_flag & P_TRACED)
2155 * SIGKILL sets process running.
2156 * It will die elsewhere.
2157 * All threads must be restarted.
2159 p->p_flag &= ~P_STOPPED_SIG;
2163 if (prop & SA_CONT) {
2165 * If traced process is already stopped,
2166 * then no further action is necessary.
2168 if (p->p_flag & P_TRACED)
2171 * If SIGCONT is default (or ignored), we continue the
2172 * process but don't leave the signal in sigqueue as
2173 * it has no further action. If SIGCONT is held, we
2174 * continue the process and leave the signal in
2175 * sigqueue. If the process catches SIGCONT, let it
2176 * handle the signal itself. If it isn't waiting on
2177 * an event, it goes back to run state.
2178 * Otherwise, process goes back to sleep state.
2180 p->p_flag &= ~P_STOPPED_SIG;
2182 if (p->p_numthreads == p->p_suspcount) {
2184 p->p_flag |= P_CONTINUED;
2185 p->p_xstat = SIGCONT;
2186 PROC_LOCK(p->p_pptr);
2187 childproc_continued(p);
2188 PROC_UNLOCK(p->p_pptr);
2191 if (action == SIG_DFL) {
2192 thread_unsuspend(p);
2194 sigqueue_delete(sigqueue, sig);
2197 if (action == SIG_CATCH) {
2199 * The process wants to catch it so it needs
2200 * to run at least one thread, but which one?
2206 * The signal is not ignored or caught.
2208 thread_unsuspend(p);
2213 if (prop & SA_STOP) {
2215 * If traced process is already stopped,
2216 * then no further action is necessary.
2218 if (p->p_flag & P_TRACED)
2221 * Already stopped, don't need to stop again
2222 * (If we did the shell could get confused).
2223 * Just make sure the signal STOP bit set.
2225 p->p_flag |= P_STOPPED_SIG;
2226 sigqueue_delete(sigqueue, sig);
2231 * All other kinds of signals:
2232 * If a thread is sleeping interruptibly, simulate a
2233 * wakeup so that when it is continued it will be made
2234 * runnable and can look at the signal. However, don't make
2235 * the PROCESS runnable, leave it stopped.
2236 * It may run a bit until it hits a thread_suspend_check().
2241 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2242 wakeup_swapper = sleepq_abort(td, intrval);
2249 * Mutexes are short lived. Threads waiting on them will
2250 * hit thread_suspend_check() soon.
2252 } else if (p->p_state == PRS_NORMAL) {
2253 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2254 tdsigwakeup(td, sig, action, intrval);
2258 MPASS(action == SIG_DFL);
2260 if (prop & SA_STOP) {
2261 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2263 p->p_flag |= P_STOPPED_SIG;
2266 sig_suspend_threads(td, p, 1);
2267 if (p->p_numthreads == p->p_suspcount) {
2269 * only thread sending signal to another
2270 * process can reach here, if thread is sending
2271 * signal to its process, because thread does
2272 * not suspend itself here, p_numthreads
2273 * should never be equal to p_suspcount.
2277 sigqueue_delete_proc(p, p->p_xstat);
2283 /* Not in "NORMAL" state. discard the signal. */
2284 sigqueue_delete(sigqueue, sig);
2289 * The process is not stopped so we need to apply the signal to all the
2293 tdsigwakeup(td, sig, action, intrval);
2295 thread_unsuspend(p);
2298 /* If we jump here, proc slock should not be owned. */
2299 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2304 * The force of a signal has been directed against a single
2305 * thread. We need to see what we can do about knocking it
2306 * out of any sleep it may be in etc.
2309 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2311 struct proc *p = td->td_proc;
2316 PROC_LOCK_ASSERT(p, MA_OWNED);
2317 prop = sigprop(sig);
2322 * Bring the priority of a thread up if we want it to get
2323 * killed in this lifetime.
2325 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2326 sched_prio(td, PUSER);
2327 if (TD_ON_SLEEPQ(td)) {
2329 * If thread is sleeping uninterruptibly
2330 * we can't interrupt the sleep... the signal will
2331 * be noticed when the process returns through
2332 * trap() or syscall().
2334 if ((td->td_flags & TDF_SINTR) == 0)
2337 * If SIGCONT is default (or ignored) and process is
2338 * asleep, we are finished; the process should not
2341 if ((prop & SA_CONT) && action == SIG_DFL) {
2344 sigqueue_delete(&p->p_sigqueue, sig);
2346 * It may be on either list in this state.
2347 * Remove from both for now.
2349 sigqueue_delete(&td->td_sigqueue, sig);
2354 * Don't awaken a sleeping thread for SIGSTOP if the
2355 * STOP signal is deferred.
2357 if ((prop & SA_STOP) && (td->td_flags & TDF_SBDRY))
2361 * Give low priority threads a better chance to run.
2363 if (td->td_priority > PUSER)
2364 sched_prio(td, PUSER);
2366 wakeup_swapper = sleepq_abort(td, intrval);
2369 * Other states do nothing with the signal immediately,
2370 * other than kicking ourselves if we are running.
2371 * It will either never be noticed, or noticed very soon.
2374 if (TD_IS_RUNNING(td) && td != curthread)
2386 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2390 PROC_LOCK_ASSERT(p, MA_OWNED);
2391 PROC_SLOCK_ASSERT(p, MA_OWNED);
2393 FOREACH_THREAD_IN_PROC(p, td2) {
2395 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2396 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2397 (td2->td_flags & TDF_SINTR)) {
2398 if (td2->td_flags & TDF_SBDRY) {
2400 * Once a thread is asleep with
2401 * TDF_SBDRY set, it should never
2402 * become suspended due to this check.
2404 KASSERT(!TD_IS_SUSPENDED(td2),
2405 ("thread with deferred stops suspended"));
2406 } else if (!TD_IS_SUSPENDED(td2)) {
2407 thread_suspend_one(td2);
2409 } else if (!TD_IS_SUSPENDED(td2)) {
2410 if (sending || td != td2)
2411 td2->td_flags |= TDF_ASTPENDING;
2413 if (TD_IS_RUNNING(td2) && td2 != td)
2414 forward_signal(td2);
2422 ptracestop(struct thread *td, int sig)
2424 struct proc *p = td->td_proc;
2426 PROC_LOCK_ASSERT(p, MA_OWNED);
2427 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2428 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2429 &p->p_mtx.lock_object, "Stopping for traced signal");
2431 td->td_dbgflags |= TDB_XSIG;
2434 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2435 if (p->p_flag & P_SINGLE_EXIT) {
2436 td->td_dbgflags &= ~TDB_XSIG;
2441 * Just make wait() to work, the last stopped thread
2446 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2447 sig_suspend_threads(td, p, 0);
2448 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2449 td->td_dbgflags &= ~TDB_STOPATFORK;
2450 cv_broadcast(&p->p_dbgwait);
2453 thread_suspend_switch(td);
2454 if (p->p_xthread == td)
2455 p->p_xthread = NULL;
2456 if (!(p->p_flag & P_TRACED))
2458 if (td->td_dbgflags & TDB_SUSPEND) {
2459 if (p->p_flag & P_SINGLE_EXIT)
2465 return (td->td_xsig);
2469 reschedule_signals(struct proc *p, sigset_t block, int flags)
2475 PROC_LOCK_ASSERT(p, MA_OWNED);
2476 if (SIGISEMPTY(p->p_siglist))
2479 SIGSETAND(block, p->p_siglist);
2480 while ((sig = sig_ffs(&block)) != 0) {
2481 SIGDELSET(block, sig);
2482 td = sigtd(p, sig, 0);
2484 if (!(flags & SIGPROCMASK_PS_LOCKED))
2485 mtx_lock(&ps->ps_mtx);
2486 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2487 tdsigwakeup(td, sig, SIG_CATCH,
2488 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2490 if (!(flags & SIGPROCMASK_PS_LOCKED))
2491 mtx_unlock(&ps->ps_mtx);
2496 tdsigcleanup(struct thread *td)
2502 PROC_LOCK_ASSERT(p, MA_OWNED);
2504 sigqueue_flush(&td->td_sigqueue);
2505 if (p->p_numthreads == 1)
2509 * Since we cannot handle signals, notify signal post code
2510 * about this by filling the sigmask.
2512 * Also, if needed, wake up thread(s) that do not block the
2513 * same signals as the exiting thread, since the thread might
2514 * have been selected for delivery and woken up.
2516 SIGFILLSET(unblocked);
2517 SIGSETNAND(unblocked, td->td_sigmask);
2518 SIGFILLSET(td->td_sigmask);
2519 reschedule_signals(p, unblocked, 0);
2524 * Defer the delivery of SIGSTOP for the current thread. Returns true
2525 * if stops were deferred and false if they were already deferred.
2533 if (td->td_flags & TDF_SBDRY)
2536 td->td_flags |= TDF_SBDRY;
2542 * Permit the delivery of SIGSTOP for the current thread. This does
2543 * not immediately suspend if a stop was posted. Instead, the thread
2544 * will suspend either via ast() or a subsequent interruptible sleep.
2553 td->td_flags &= ~TDF_SBDRY;
2558 * If the current process has received a signal (should be caught or cause
2559 * termination, should interrupt current syscall), return the signal number.
2560 * Stop signals with default action are processed immediately, then cleared;
2561 * they aren't returned. This is checked after each entry to the system for
2562 * a syscall or trap (though this can usually be done without calling issignal
2563 * by checking the pending signal masks in cursig.) The normal call
2566 * while (sig = cursig(curthread))
2570 issignal(struct thread *td)
2574 struct sigqueue *queue;
2575 sigset_t sigpending;
2576 int sig, prop, newsig;
2580 mtx_assert(&ps->ps_mtx, MA_OWNED);
2581 PROC_LOCK_ASSERT(p, MA_OWNED);
2583 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2585 sigpending = td->td_sigqueue.sq_signals;
2586 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2587 SIGSETNAND(sigpending, td->td_sigmask);
2589 if (p->p_flag & P_PPWAIT || td->td_flags & TDF_SBDRY)
2590 SIG_STOPSIGMASK(sigpending);
2591 if (SIGISEMPTY(sigpending)) /* no signal to send */
2593 sig = sig_ffs(&sigpending);
2595 if (p->p_stops & S_SIG) {
2596 mtx_unlock(&ps->ps_mtx);
2597 stopevent(p, S_SIG, sig);
2598 mtx_lock(&ps->ps_mtx);
2602 * We should see pending but ignored signals
2603 * only if P_TRACED was on when they were posted.
2605 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2606 sigqueue_delete(&td->td_sigqueue, sig);
2607 sigqueue_delete(&p->p_sigqueue, sig);
2610 if (p->p_flag & P_TRACED && (p->p_flag & P_PPTRACE) == 0) {
2612 * If traced, always stop.
2613 * Remove old signal from queue before the stop.
2614 * XXX shrug off debugger, it causes siginfo to
2617 queue = &td->td_sigqueue;
2618 td->td_dbgksi.ksi_signo = 0;
2619 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2620 queue = &p->p_sigqueue;
2621 sigqueue_get(queue, sig, &td->td_dbgksi);
2624 mtx_unlock(&ps->ps_mtx);
2625 newsig = ptracestop(td, sig);
2626 mtx_lock(&ps->ps_mtx);
2628 if (sig != newsig) {
2631 * If parent wants us to take the signal,
2632 * then it will leave it in p->p_xstat;
2633 * otherwise we just look for signals again.
2640 * Put the new signal into td_sigqueue. If the
2641 * signal is being masked, look for other
2644 sigqueue_add(queue, sig, NULL);
2645 if (SIGISMEMBER(td->td_sigmask, sig))
2649 if (td->td_dbgksi.ksi_signo != 0) {
2650 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2651 if (sigqueue_add(&td->td_sigqueue, sig,
2652 &td->td_dbgksi) != 0)
2653 td->td_dbgksi.ksi_signo = 0;
2655 if (td->td_dbgksi.ksi_signo == 0)
2656 sigqueue_add(&td->td_sigqueue, sig,
2661 * If the traced bit got turned off, go back up
2662 * to the top to rescan signals. This ensures
2663 * that p_sig* and p_sigact are consistent.
2665 if ((p->p_flag & P_TRACED) == 0)
2669 prop = sigprop(sig);
2672 * Decide whether the signal should be returned.
2673 * Return the signal's number, or fall through
2674 * to clear it from the pending mask.
2676 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2678 case (intptr_t)SIG_DFL:
2680 * Don't take default actions on system processes.
2682 if (p->p_pid <= 1) {
2685 * Are you sure you want to ignore SIGSEGV
2688 printf("Process (pid %lu) got signal %d\n",
2689 (u_long)p->p_pid, sig);
2691 break; /* == ignore */
2694 * If there is a pending stop signal to process
2695 * with default action, stop here,
2696 * then clear the signal. However,
2697 * if process is member of an orphaned
2698 * process group, ignore tty stop signals.
2700 if (prop & SA_STOP) {
2701 if (p->p_flag & (P_TRACED|P_WEXIT) ||
2702 (p->p_pgrp->pg_jobc == 0 &&
2704 break; /* == ignore */
2705 mtx_unlock(&ps->ps_mtx);
2706 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2707 &p->p_mtx.lock_object, "Catching SIGSTOP");
2708 p->p_flag |= P_STOPPED_SIG;
2711 sig_suspend_threads(td, p, 0);
2712 thread_suspend_switch(td);
2714 mtx_lock(&ps->ps_mtx);
2716 } else if (prop & SA_IGNORE) {
2718 * Except for SIGCONT, shouldn't get here.
2719 * Default action is to ignore; drop it.
2721 break; /* == ignore */
2726 case (intptr_t)SIG_IGN:
2728 * Masking above should prevent us ever trying
2729 * to take action on an ignored signal other
2730 * than SIGCONT, unless process is traced.
2732 if ((prop & SA_CONT) == 0 &&
2733 (p->p_flag & P_TRACED) == 0)
2734 printf("issignal\n");
2735 break; /* == ignore */
2739 * This signal has an action, let
2740 * postsig() process it.
2744 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2745 sigqueue_delete(&p->p_sigqueue, sig);
2751 thread_stopped(struct proc *p)
2755 PROC_LOCK_ASSERT(p, MA_OWNED);
2756 PROC_SLOCK_ASSERT(p, MA_OWNED);
2760 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2762 p->p_flag &= ~P_WAITED;
2763 PROC_LOCK(p->p_pptr);
2764 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2765 CLD_TRAPPED : CLD_STOPPED);
2766 PROC_UNLOCK(p->p_pptr);
2772 * Take the action for the specified signal
2773 * from the current set of pending signals.
2779 struct thread *td = curthread;
2780 register struct proc *p = td->td_proc;
2784 sigset_t returnmask, mask;
2786 KASSERT(sig != 0, ("postsig"));
2788 PROC_LOCK_ASSERT(p, MA_OWNED);
2790 mtx_assert(&ps->ps_mtx, MA_OWNED);
2791 ksiginfo_init(&ksi);
2792 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2793 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2795 ksi.ksi_signo = sig;
2796 if (ksi.ksi_code == SI_TIMER)
2797 itimer_accept(p, ksi.ksi_timerid, &ksi);
2798 action = ps->ps_sigact[_SIG_IDX(sig)];
2800 if (KTRPOINT(td, KTR_PSIG))
2801 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2802 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2804 if (p->p_stops & S_SIG) {
2805 mtx_unlock(&ps->ps_mtx);
2806 stopevent(p, S_SIG, sig);
2807 mtx_lock(&ps->ps_mtx);
2810 if (action == SIG_DFL) {
2812 * Default action, where the default is to kill
2813 * the process. (Other cases were ignored above.)
2815 mtx_unlock(&ps->ps_mtx);
2820 * If we get here, the signal must be caught.
2822 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2823 ("postsig action"));
2825 * Set the new mask value and also defer further
2826 * occurrences of this signal.
2828 * Special case: user has done a sigsuspend. Here the
2829 * current mask is not of interest, but rather the
2830 * mask from before the sigsuspend is what we want
2831 * restored after the signal processing is completed.
2833 if (td->td_pflags & TDP_OLDMASK) {
2834 returnmask = td->td_oldsigmask;
2835 td->td_pflags &= ~TDP_OLDMASK;
2837 returnmask = td->td_sigmask;
2839 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2840 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2841 SIGADDSET(mask, sig);
2842 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2843 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2845 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2847 * See kern_sigaction() for origin of this code.
2849 SIGDELSET(ps->ps_sigcatch, sig);
2850 if (sig != SIGCONT &&
2851 sigprop(sig) & SA_IGNORE)
2852 SIGADDSET(ps->ps_sigignore, sig);
2853 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2855 td->td_ru.ru_nsignals++;
2856 if (p->p_sig == sig) {
2860 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2866 * Kill the current process for stated reason.
2874 PROC_LOCK_ASSERT(p, MA_OWNED);
2875 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
2877 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid,
2878 p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2879 p->p_flag |= P_WKILLED;
2880 kern_psignal(p, SIGKILL);
2884 * Force the current process to exit with the specified signal, dumping core
2885 * if appropriate. We bypass the normal tests for masked and caught signals,
2886 * allowing unrecoverable failures to terminate the process without changing
2887 * signal state. Mark the accounting record with the signal termination.
2888 * If dumping core, save the signal number for the debugger. Calls exit and
2896 struct proc *p = td->td_proc;
2898 PROC_LOCK_ASSERT(p, MA_OWNED);
2899 p->p_acflag |= AXSIG;
2901 * We must be single-threading to generate a core dump. This
2902 * ensures that the registers in the core file are up-to-date.
2903 * Also, the ELF dump handler assumes that the thread list doesn't
2904 * change out from under it.
2906 * XXX If another thread attempts to single-thread before us
2907 * (e.g. via fork()), we won't get a dump at all.
2909 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2912 * Log signals which would cause core dumps
2913 * (Log as LOG_INFO to appease those who don't want
2915 * XXX : Todo, as well as euid, write out ruid too
2916 * Note that coredump() drops proc lock.
2918 if (coredump(td) == 0)
2920 if (kern_logsigexit)
2922 "pid %d (%s), uid %d: exited on signal %d%s\n",
2923 p->p_pid, p->p_comm,
2924 td->td_ucred ? td->td_ucred->cr_uid : -1,
2926 sig & WCOREFLAG ? " (core dumped)" : "");
2929 exit1(td, W_EXITCODE(0, sig));
2934 * Send queued SIGCHLD to parent when child process's state
2938 sigparent(struct proc *p, int reason, int status)
2940 PROC_LOCK_ASSERT(p, MA_OWNED);
2941 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2943 if (p->p_ksi != NULL) {
2944 p->p_ksi->ksi_signo = SIGCHLD;
2945 p->p_ksi->ksi_code = reason;
2946 p->p_ksi->ksi_status = status;
2947 p->p_ksi->ksi_pid = p->p_pid;
2948 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2949 if (KSI_ONQ(p->p_ksi))
2952 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
2956 childproc_jobstate(struct proc *p, int reason, int sig)
2960 PROC_LOCK_ASSERT(p, MA_OWNED);
2961 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2964 * Wake up parent sleeping in kern_wait(), also send
2965 * SIGCHLD to parent, but SIGCHLD does not guarantee
2966 * that parent will awake, because parent may masked
2969 p->p_pptr->p_flag |= P_STATCHILD;
2972 ps = p->p_pptr->p_sigacts;
2973 mtx_lock(&ps->ps_mtx);
2974 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2975 mtx_unlock(&ps->ps_mtx);
2976 sigparent(p, reason, sig);
2978 mtx_unlock(&ps->ps_mtx);
2982 childproc_stopped(struct proc *p, int reason)
2984 /* p_xstat is a plain signal number, not a full wait() status here. */
2985 childproc_jobstate(p, reason, p->p_xstat);
2989 childproc_continued(struct proc *p)
2991 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2995 childproc_exited(struct proc *p)
2998 int xstat = p->p_xstat; /* convert to int */
3001 if (WCOREDUMP(xstat))
3002 reason = CLD_DUMPED, status = WTERMSIG(xstat);
3003 else if (WIFSIGNALED(xstat))
3004 reason = CLD_KILLED, status = WTERMSIG(xstat);
3006 reason = CLD_EXITED, status = WEXITSTATUS(xstat);
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, "Compression of user corefiles");
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, "Corefile gzip compression level");
3050 #define GZ_SUFFIX ".gz"
3051 #define GZ_SUFFIX_LEN 3
3054 static char corefilename[MAXPATHLEN] = {"%N.core"};
3055 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3056 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
3057 sizeof(corefilename), "Process corefile name format string");
3060 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3061 * Expand the name described in corefilename, using name, uid, and pid
3062 * and open/create core file.
3063 * corefilename is a printf-like string, with three format specifiers:
3064 * %N name of process ("name")
3065 * %P process id (pid)
3067 * For example, "%N.core" is the default; they can be disabled completely
3068 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3069 * This is controlled by the sysctl variable kern.corefile (see above).
3072 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3073 int compress, struct vnode **vpp, char **namep)
3075 struct nameidata nd;
3078 char *hostname, *name;
3079 int indexpos, i, error, cmode, flags, oflags;
3082 format = corefilename;
3083 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3085 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3086 for (i = 0; format[i] != '\0'; i++) {
3087 switch (format[i]) {
3088 case '%': /* Format character */
3090 switch (format[i]) {
3092 sbuf_putc(&sb, '%');
3094 case 'H': /* hostname */
3095 if (hostname == NULL) {
3096 hostname = malloc(MAXHOSTNAMELEN,
3099 getcredhostname(td->td_ucred, hostname,
3101 sbuf_printf(&sb, "%s", hostname);
3103 case 'I': /* autoincrementing index */
3104 sbuf_printf(&sb, "0");
3105 indexpos = sbuf_len(&sb) - 1;
3107 case 'N': /* process name */
3108 sbuf_printf(&sb, "%s", comm);
3110 case 'P': /* process id */
3111 sbuf_printf(&sb, "%u", pid);
3113 case 'U': /* user id */
3114 sbuf_printf(&sb, "%u", uid);
3118 "Unknown format character %c in "
3119 "corename `%s'\n", format[i], format);
3124 sbuf_putc(&sb, format[i]);
3128 free(hostname, M_TEMP);
3129 #ifdef COMPRESS_USER_CORES
3131 sbuf_printf(&sb, GZ_SUFFIX);
3133 if (sbuf_error(&sb) != 0) {
3134 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3135 "long\n", (long)pid, comm, (u_long)uid);
3143 cmode = S_IRUSR | S_IWUSR;
3144 oflags = VN_OPEN_NOAUDIT | (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3147 * If the core format has a %I in it, then we need to check
3148 * for existing corefiles before returning a name.
3149 * To do this we iterate over 0..num_cores to find a
3150 * non-existing core file name to use.
3152 if (indexpos != -1) {
3153 for (i = 0; i < num_cores; i++) {
3154 flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3155 name[indexpos] = '0' + i;
3156 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3157 error = vn_open_cred(&nd, &flags, cmode, oflags,
3158 td->td_ucred, NULL);
3160 if (error == EEXIST)
3163 "pid %d (%s), uid (%u): Path `%s' failed "
3164 "on initial open test, error = %d\n",
3165 pid, comm, uid, name, error);
3171 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3172 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3173 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL);
3177 audit_proc_coredump(td, name, error);
3182 NDFREE(&nd, NDF_ONLY_PNBUF);
3189 * Dump a process' core. The main routine does some
3190 * policy checking, and creates the name of the coredump;
3191 * then it passes on a vnode and a size limit to the process-specific
3192 * coredump routine if there is one; if there _is not_ one, it returns
3193 * ENOSYS; otherwise it returns the error from the process-specific routine.
3197 coredump(struct thread *td)
3199 struct proc *p = td->td_proc;
3200 struct ucred *cred = td->td_ucred;
3204 int error, error1, locked;
3206 char *name; /* name of corefile */
3210 #ifdef COMPRESS_USER_CORES
3211 compress = compress_user_cores;
3215 PROC_LOCK_ASSERT(p, MA_OWNED);
3216 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3217 _STOPEVENT(p, S_CORE, 0);
3219 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0)) {
3225 * Note that the bulk of limit checking is done after
3226 * the corefile is created. The exception is if the limit
3227 * for corefiles is 0, in which case we don't bother
3228 * creating the corefile at all. This layout means that
3229 * a corefile is truncated instead of not being created,
3230 * if it is larger than the limit.
3232 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3233 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3240 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, compress,
3245 /* Don't dump to non-regular files or files with links. */
3246 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3247 vattr.va_nlink != 1) {
3254 lf.l_whence = SEEK_SET;
3257 lf.l_type = F_WRLCK;
3258 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3260 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3261 lf.l_type = F_UNLCK;
3263 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3264 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3266 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3274 if (set_core_nodump_flag)
3275 vattr.va_flags = UF_NODUMP;
3276 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3277 VOP_SETATTR(vp, &vattr, cred);
3279 vn_finished_write(mp);
3281 p->p_acflag |= ACORE;
3284 if (p->p_sysent->sv_coredump != NULL) {
3285 error = p->p_sysent->sv_coredump(td, vp, limit,
3286 compress ? IMGACT_CORE_COMPRESS : 0);
3292 lf.l_type = F_UNLCK;
3293 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3296 error1 = vn_close(vp, FWRITE, cred, td);
3301 audit_proc_coredump(td, name, error);
3308 * Nonexistent system call-- signal process (may want to handle it). Flag
3309 * error in case process won't see signal immediately (blocked or ignored).
3311 #ifndef _SYS_SYSPROTO_H_
3320 struct nosys_args *args;
3322 struct proc *p = td->td_proc;
3325 tdsignal(td, SIGSYS);
3331 * Send a SIGIO or SIGURG signal to a process or process group using stored
3332 * credentials rather than those of the current process.
3335 pgsigio(sigiop, sig, checkctty)
3336 struct sigio **sigiop;
3340 struct sigio *sigio;
3342 ksiginfo_init(&ksi);
3343 ksi.ksi_signo = sig;
3344 ksi.ksi_code = SI_KERNEL;
3348 if (sigio == NULL) {
3352 if (sigio->sio_pgid > 0) {
3353 PROC_LOCK(sigio->sio_proc);
3354 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3355 kern_psignal(sigio->sio_proc, sig);
3356 PROC_UNLOCK(sigio->sio_proc);
3357 } else if (sigio->sio_pgid < 0) {
3360 PGRP_LOCK(sigio->sio_pgrp);
3361 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3363 if (p->p_state == PRS_NORMAL &&
3364 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3365 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3366 kern_psignal(p, sig);
3369 PGRP_UNLOCK(sigio->sio_pgrp);
3375 filt_sigattach(struct knote *kn)
3377 struct proc *p = curproc;
3379 kn->kn_ptr.p_proc = p;
3380 kn->kn_flags |= EV_CLEAR; /* automatically set */
3382 knlist_add(&p->p_klist, kn, 0);
3388 filt_sigdetach(struct knote *kn)
3390 struct proc *p = kn->kn_ptr.p_proc;
3392 knlist_remove(&p->p_klist, kn, 0);
3396 * signal knotes are shared with proc knotes, so we apply a mask to
3397 * the hint in order to differentiate them from process hints. This
3398 * could be avoided by using a signal-specific knote list, but probably
3399 * isn't worth the trouble.
3402 filt_signal(struct knote *kn, long hint)
3405 if (hint & NOTE_SIGNAL) {
3406 hint &= ~NOTE_SIGNAL;
3408 if (kn->kn_id == hint)
3411 return (kn->kn_data != 0);
3419 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3421 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3426 sigacts_free(struct sigacts *ps)
3429 mtx_lock(&ps->ps_mtx);
3431 if (ps->ps_refcnt == 0) {
3432 mtx_destroy(&ps->ps_mtx);
3433 free(ps, M_SUBPROC);
3435 mtx_unlock(&ps->ps_mtx);
3439 sigacts_hold(struct sigacts *ps)
3441 mtx_lock(&ps->ps_mtx);
3443 mtx_unlock(&ps->ps_mtx);
3448 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3451 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3452 mtx_lock(&src->ps_mtx);
3453 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3454 mtx_unlock(&src->ps_mtx);
3458 sigacts_shared(struct sigacts *ps)
3462 mtx_lock(&ps->ps_mtx);
3463 shared = ps->ps_refcnt > 1;
3464 mtx_unlock(&ps->ps_mtx);