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 <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
49 #include <sys/condvar.h>
50 #include <sys/event.h>
51 #include <sys/fcntl.h>
52 #include <sys/kernel.h>
54 #include <sys/ktrace.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
60 #include <sys/posix4.h>
61 #include <sys/pioctl.h>
62 #include <sys/resourcevar.h>
65 #include <sys/sleepqueue.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysent.h>
72 #include <sys/syslog.h>
73 #include <sys/sysproto.h>
74 #include <sys/timers.h>
75 #include <sys/unistd.h>
78 #include <vm/vm_extern.h>
81 #include <machine/cpu.h>
83 #include <security/audit/audit.h>
85 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
87 SDT_PROVIDER_DECLARE(proc);
88 SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send);
89 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *");
90 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *");
91 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int");
92 SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear);
93 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int");
94 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
95 SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard);
96 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
97 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
98 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
100 static int coredump(struct thread *);
101 static char *expand_name(const char *, uid_t, pid_t);
102 static int killpg1(struct thread *td, int sig, int pgid, int all,
104 static int issignal(struct thread *td, int stop_allowed);
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 =
116 { 0, filt_sigattach, filt_sigdetach, filt_signal };
118 int kern_logsigexit = 1;
119 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
121 "Log processes quitting on abnormal signals to syslog(3)");
123 static int kern_forcesigexit = 1;
124 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
125 &kern_forcesigexit, 0, "Force trap signal to be handled");
127 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
129 static int max_pending_per_proc = 128;
130 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
131 &max_pending_per_proc, 0, "Max pending signals per proc");
133 static int preallocate_siginfo = 1024;
134 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
135 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
136 &preallocate_siginfo, 0, "Preallocated signal memory size");
138 static int signal_overflow = 0;
139 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
140 &signal_overflow, 0, "Number of signals overflew");
142 static int signal_alloc_fail = 0;
143 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
144 &signal_alloc_fail, 0, "signals failed to be allocated");
146 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
149 * Policy -- Can ucred cr1 send SIGIO to process cr2?
150 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
151 * in the right situations.
153 #define CANSIGIO(cr1, cr2) \
154 ((cr1)->cr_uid == 0 || \
155 (cr1)->cr_ruid == (cr2)->cr_ruid || \
156 (cr1)->cr_uid == (cr2)->cr_ruid || \
157 (cr1)->cr_ruid == (cr2)->cr_uid || \
158 (cr1)->cr_uid == (cr2)->cr_uid)
161 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
162 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
164 static int do_coredump = 1;
165 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
166 &do_coredump, 0, "Enable/Disable coredumps");
168 static int set_core_nodump_flag = 0;
169 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
170 0, "Enable setting the NODUMP flag on coredump files");
173 * Signal properties and actions.
174 * The array below categorizes the signals and their default actions
175 * according to the following properties:
177 #define SA_KILL 0x01 /* terminates process by default */
178 #define SA_CORE 0x02 /* ditto and coredumps */
179 #define SA_STOP 0x04 /* suspend process */
180 #define SA_TTYSTOP 0x08 /* ditto, from tty */
181 #define SA_IGNORE 0x10 /* ignore by default */
182 #define SA_CONT 0x20 /* continue if suspended */
183 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
184 #define SA_PROC 0x80 /* deliverable to any thread */
186 static int sigproptbl[NSIG] = {
187 SA_KILL|SA_PROC, /* SIGHUP */
188 SA_KILL|SA_PROC, /* SIGINT */
189 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
190 SA_KILL|SA_CORE, /* SIGILL */
191 SA_KILL|SA_CORE, /* SIGTRAP */
192 SA_KILL|SA_CORE, /* SIGABRT */
193 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
194 SA_KILL|SA_CORE, /* SIGFPE */
195 SA_KILL|SA_PROC, /* SIGKILL */
196 SA_KILL|SA_CORE, /* SIGBUS */
197 SA_KILL|SA_CORE, /* SIGSEGV */
198 SA_KILL|SA_CORE, /* SIGSYS */
199 SA_KILL|SA_PROC, /* SIGPIPE */
200 SA_KILL|SA_PROC, /* SIGALRM */
201 SA_KILL|SA_PROC, /* SIGTERM */
202 SA_IGNORE|SA_PROC, /* SIGURG */
203 SA_STOP|SA_PROC, /* SIGSTOP */
204 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
205 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
206 SA_IGNORE|SA_PROC, /* SIGCHLD */
207 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
208 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
209 SA_IGNORE|SA_PROC, /* SIGIO */
210 SA_KILL, /* SIGXCPU */
211 SA_KILL, /* SIGXFSZ */
212 SA_KILL|SA_PROC, /* SIGVTALRM */
213 SA_KILL|SA_PROC, /* SIGPROF */
214 SA_IGNORE|SA_PROC, /* SIGWINCH */
215 SA_IGNORE|SA_PROC, /* SIGINFO */
216 SA_KILL|SA_PROC, /* SIGUSR1 */
217 SA_KILL|SA_PROC, /* SIGUSR2 */
220 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
225 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
226 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
227 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
228 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
229 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
230 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
234 ksiginfo_alloc(int wait)
241 if (ksiginfo_zone != NULL)
242 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
247 ksiginfo_free(ksiginfo_t *ksi)
249 uma_zfree(ksiginfo_zone, ksi);
253 ksiginfo_tryfree(ksiginfo_t *ksi)
255 if (!(ksi->ksi_flags & KSI_EXT)) {
256 uma_zfree(ksiginfo_zone, ksi);
263 sigqueue_init(sigqueue_t *list, struct proc *p)
265 SIGEMPTYSET(list->sq_signals);
266 SIGEMPTYSET(list->sq_kill);
267 TAILQ_INIT(&list->sq_list);
269 list->sq_flags = SQ_INIT;
273 * Get a signal's ksiginfo.
275 * 0 - signal not found
276 * others - signal number
279 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
281 struct proc *p = sq->sq_proc;
282 struct ksiginfo *ksi, *next;
285 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
287 if (!SIGISMEMBER(sq->sq_signals, signo))
290 if (SIGISMEMBER(sq->sq_kill, signo)) {
292 SIGDELSET(sq->sq_kill, signo);
295 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
296 if (ksi->ksi_signo == signo) {
298 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
299 ksi->ksi_sigq = NULL;
300 ksiginfo_copy(ksi, si);
301 if (ksiginfo_tryfree(ksi) && p != NULL)
310 SIGDELSET(sq->sq_signals, signo);
311 si->ksi_signo = signo;
316 sigqueue_take(ksiginfo_t *ksi)
322 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
326 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
327 ksi->ksi_sigq = NULL;
328 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
331 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
332 kp = TAILQ_NEXT(kp, ksi_link)) {
333 if (kp->ksi_signo == ksi->ksi_signo)
336 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
337 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
341 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
343 struct proc *p = sq->sq_proc;
344 struct ksiginfo *ksi;
347 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
349 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
350 SIGADDSET(sq->sq_kill, signo);
354 /* directly insert the ksi, don't copy it */
355 if (si->ksi_flags & KSI_INS) {
356 if (si->ksi_flags & KSI_HEAD)
357 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
359 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
364 if (__predict_false(ksiginfo_zone == NULL)) {
365 SIGADDSET(sq->sq_kill, signo);
369 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
372 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
378 ksiginfo_copy(si, ksi);
379 ksi->ksi_signo = signo;
380 if (si->ksi_flags & KSI_HEAD)
381 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
383 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
387 if ((si->ksi_flags & KSI_TRAP) != 0 ||
388 (si->ksi_flags & KSI_SIGQ) == 0) {
390 SIGADDSET(sq->sq_kill, signo);
399 SIGADDSET(sq->sq_signals, signo);
404 sigqueue_flush(sigqueue_t *sq)
406 struct proc *p = sq->sq_proc;
409 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
412 PROC_LOCK_ASSERT(p, MA_OWNED);
414 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
415 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
416 ksi->ksi_sigq = NULL;
417 if (ksiginfo_tryfree(ksi) && p != NULL)
421 SIGEMPTYSET(sq->sq_signals);
422 SIGEMPTYSET(sq->sq_kill);
426 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
430 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
432 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
433 SIGADDSET(*set, ksi->ksi_signo);
434 SIGSETOR(*set, sq->sq_kill);
438 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
441 struct proc *p1, *p2;
442 ksiginfo_t *ksi, *next;
444 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
445 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
447 * make a copy, this allows setp to point to src or dst
448 * sq_signals without trouble.
453 /* Move siginfo to target list */
454 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
455 if (SIGISMEMBER(set, ksi->ksi_signo)) {
456 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
459 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
466 /* Move pending bits to target list */
469 SIGSETOR(dst->sq_kill, tmp);
470 SIGSETNAND(src->sq_kill, tmp);
472 tmp = src->sq_signals;
474 SIGSETOR(dst->sq_signals, tmp);
475 SIGSETNAND(src->sq_signals, tmp);
477 /* Finally, rescan src queue and set pending bits for it */
478 sigqueue_collect_set(src, &src->sq_signals);
482 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
487 SIGADDSET(set, signo);
488 sigqueue_move_set(src, dst, &set);
492 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
494 struct proc *p = sq->sq_proc;
495 ksiginfo_t *ksi, *next;
497 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
499 /* Remove siginfo queue */
500 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
501 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
502 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
503 ksi->ksi_sigq = NULL;
504 if (ksiginfo_tryfree(ksi) && p != NULL)
508 SIGSETNAND(sq->sq_kill, *set);
509 SIGSETNAND(sq->sq_signals, *set);
510 /* Finally, rescan queue and set pending bits for it */
511 sigqueue_collect_set(sq, &sq->sq_signals);
515 sigqueue_delete(sigqueue_t *sq, int signo)
520 SIGADDSET(set, signo);
521 sigqueue_delete_set(sq, &set);
524 /* Remove a set of signals for a process */
526 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
531 PROC_LOCK_ASSERT(p, MA_OWNED);
533 sigqueue_init(&worklist, NULL);
534 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
536 FOREACH_THREAD_IN_PROC(p, td0)
537 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
539 sigqueue_flush(&worklist);
543 sigqueue_delete_proc(struct proc *p, int signo)
548 SIGADDSET(set, signo);
549 sigqueue_delete_set_proc(p, &set);
553 sigqueue_delete_stopmask_proc(struct proc *p)
558 SIGADDSET(set, SIGSTOP);
559 SIGADDSET(set, SIGTSTP);
560 SIGADDSET(set, SIGTTIN);
561 SIGADDSET(set, SIGTTOU);
562 sigqueue_delete_set_proc(p, &set);
566 * Determine signal that should be delivered to process p, the current
567 * process, 0 if none. If there is a pending stop signal with default
568 * action, the process stops in issignal().
571 cursig(struct thread *td, int stop_allowed)
573 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
574 KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
575 stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
576 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
577 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
578 return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
582 * Arrange for ast() to handle unmasked pending signals on return to user
583 * mode. This must be called whenever a signal is added to td_sigqueue or
584 * unmasked in td_sigmask.
587 signotify(struct thread *td)
593 PROC_LOCK_ASSERT(p, MA_OWNED);
595 if (SIGPENDING(td)) {
597 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
603 sigonstack(size_t sp)
605 struct thread *td = curthread;
607 return ((td->td_pflags & TDP_ALTSTACK) ?
608 #if defined(COMPAT_43)
609 ((td->td_sigstk.ss_size == 0) ?
610 (td->td_sigstk.ss_flags & SS_ONSTACK) :
611 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
613 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
622 if (sig > 0 && sig < NSIG)
623 return (sigproptbl[_SIG_IDX(sig)]);
628 sig_ffs(sigset_t *set)
632 for (i = 0; i < _SIG_WORDS; i++)
634 return (ffs(set->__bits[i]) + (i * 32));
645 kern_sigaction(td, sig, act, oact, flags)
648 struct sigaction *act, *oact;
652 struct proc *p = td->td_proc;
654 if (!_SIG_VALID(sig))
659 mtx_lock(&ps->ps_mtx);
661 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
663 if (SIGISMEMBER(ps->ps_sigonstack, sig))
664 oact->sa_flags |= SA_ONSTACK;
665 if (!SIGISMEMBER(ps->ps_sigintr, sig))
666 oact->sa_flags |= SA_RESTART;
667 if (SIGISMEMBER(ps->ps_sigreset, sig))
668 oact->sa_flags |= SA_RESETHAND;
669 if (SIGISMEMBER(ps->ps_signodefer, sig))
670 oact->sa_flags |= SA_NODEFER;
671 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
672 oact->sa_flags |= SA_SIGINFO;
674 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
676 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
677 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
678 oact->sa_flags |= SA_NOCLDSTOP;
679 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
680 oact->sa_flags |= SA_NOCLDWAIT;
683 if ((sig == SIGKILL || sig == SIGSTOP) &&
684 act->sa_handler != SIG_DFL) {
685 mtx_unlock(&ps->ps_mtx);
691 * Change setting atomically.
694 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
695 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
696 if (act->sa_flags & SA_SIGINFO) {
697 ps->ps_sigact[_SIG_IDX(sig)] =
698 (__sighandler_t *)act->sa_sigaction;
699 SIGADDSET(ps->ps_siginfo, sig);
701 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
702 SIGDELSET(ps->ps_siginfo, sig);
704 if (!(act->sa_flags & SA_RESTART))
705 SIGADDSET(ps->ps_sigintr, sig);
707 SIGDELSET(ps->ps_sigintr, sig);
708 if (act->sa_flags & SA_ONSTACK)
709 SIGADDSET(ps->ps_sigonstack, sig);
711 SIGDELSET(ps->ps_sigonstack, sig);
712 if (act->sa_flags & SA_RESETHAND)
713 SIGADDSET(ps->ps_sigreset, sig);
715 SIGDELSET(ps->ps_sigreset, sig);
716 if (act->sa_flags & SA_NODEFER)
717 SIGADDSET(ps->ps_signodefer, sig);
719 SIGDELSET(ps->ps_signodefer, sig);
720 if (sig == SIGCHLD) {
721 if (act->sa_flags & SA_NOCLDSTOP)
722 ps->ps_flag |= PS_NOCLDSTOP;
724 ps->ps_flag &= ~PS_NOCLDSTOP;
725 if (act->sa_flags & SA_NOCLDWAIT) {
727 * Paranoia: since SA_NOCLDWAIT is implemented
728 * by reparenting the dying child to PID 1 (and
729 * trust it to reap the zombie), PID 1 itself
730 * is forbidden to set SA_NOCLDWAIT.
733 ps->ps_flag &= ~PS_NOCLDWAIT;
735 ps->ps_flag |= PS_NOCLDWAIT;
737 ps->ps_flag &= ~PS_NOCLDWAIT;
738 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
739 ps->ps_flag |= PS_CLDSIGIGN;
741 ps->ps_flag &= ~PS_CLDSIGIGN;
744 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
745 * and for signals set to SIG_DFL where the default is to
746 * ignore. However, don't put SIGCONT in ps_sigignore, as we
747 * have to restart the process.
749 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
750 (sigprop(sig) & SA_IGNORE &&
751 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
752 /* never to be seen again */
753 sigqueue_delete_proc(p, sig);
755 /* easier in psignal */
756 SIGADDSET(ps->ps_sigignore, sig);
757 SIGDELSET(ps->ps_sigcatch, sig);
759 SIGDELSET(ps->ps_sigignore, sig);
760 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
761 SIGDELSET(ps->ps_sigcatch, sig);
763 SIGADDSET(ps->ps_sigcatch, sig);
765 #ifdef COMPAT_FREEBSD4
766 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
767 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
768 (flags & KSA_FREEBSD4) == 0)
769 SIGDELSET(ps->ps_freebsd4, sig);
771 SIGADDSET(ps->ps_freebsd4, sig);
774 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
775 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
776 (flags & KSA_OSIGSET) == 0)
777 SIGDELSET(ps->ps_osigset, sig);
779 SIGADDSET(ps->ps_osigset, sig);
782 mtx_unlock(&ps->ps_mtx);
787 #ifndef _SYS_SYSPROTO_H_
788 struct sigaction_args {
790 struct sigaction *act;
791 struct sigaction *oact;
797 register struct sigaction_args *uap;
799 struct sigaction act, oact;
800 register struct sigaction *actp, *oactp;
803 actp = (uap->act != NULL) ? &act : NULL;
804 oactp = (uap->oact != NULL) ? &oact : NULL;
806 error = copyin(uap->act, actp, sizeof(act));
810 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
812 error = copyout(oactp, uap->oact, sizeof(oact));
816 #ifdef COMPAT_FREEBSD4
817 #ifndef _SYS_SYSPROTO_H_
818 struct freebsd4_sigaction_args {
820 struct sigaction *act;
821 struct sigaction *oact;
825 freebsd4_sigaction(td, uap)
827 register struct freebsd4_sigaction_args *uap;
829 struct sigaction act, oact;
830 register struct sigaction *actp, *oactp;
834 actp = (uap->act != NULL) ? &act : NULL;
835 oactp = (uap->oact != NULL) ? &oact : NULL;
837 error = copyin(uap->act, actp, sizeof(act));
841 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
843 error = copyout(oactp, uap->oact, sizeof(oact));
846 #endif /* COMAPT_FREEBSD4 */
848 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
849 #ifndef _SYS_SYSPROTO_H_
850 struct osigaction_args {
852 struct osigaction *nsa;
853 struct osigaction *osa;
859 register struct osigaction_args *uap;
861 struct osigaction sa;
862 struct sigaction nsa, osa;
863 register struct sigaction *nsap, *osap;
866 if (uap->signum <= 0 || uap->signum >= ONSIG)
869 nsap = (uap->nsa != NULL) ? &nsa : NULL;
870 osap = (uap->osa != NULL) ? &osa : NULL;
873 error = copyin(uap->nsa, &sa, sizeof(sa));
876 nsap->sa_handler = sa.sa_handler;
877 nsap->sa_flags = sa.sa_flags;
878 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
880 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
881 if (osap && !error) {
882 sa.sa_handler = osap->sa_handler;
883 sa.sa_flags = osap->sa_flags;
884 SIG2OSIG(osap->sa_mask, sa.sa_mask);
885 error = copyout(&sa, uap->osa, sizeof(sa));
890 #if !defined(__i386__)
891 /* Avoid replicating the same stub everywhere */
895 struct osigreturn_args *uap;
898 return (nosys(td, (struct nosys_args *)uap));
901 #endif /* COMPAT_43 */
904 * Initialize signal state for process 0;
905 * set to ignore signals that are ignored by default.
916 mtx_lock(&ps->ps_mtx);
917 for (i = 1; i <= NSIG; i++)
918 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
919 SIGADDSET(ps->ps_sigignore, i);
920 mtx_unlock(&ps->ps_mtx);
925 * Reset signals for an exec of the specified process.
928 execsigs(struct proc *p)
935 * Reset caught signals. Held signals remain held
936 * through td_sigmask (unless they were caught,
937 * and are now ignored by default).
939 PROC_LOCK_ASSERT(p, MA_OWNED);
940 td = FIRST_THREAD_IN_PROC(p);
942 mtx_lock(&ps->ps_mtx);
943 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
944 sig = sig_ffs(&ps->ps_sigcatch);
945 SIGDELSET(ps->ps_sigcatch, sig);
946 if (sigprop(sig) & SA_IGNORE) {
948 SIGADDSET(ps->ps_sigignore, sig);
949 sigqueue_delete_proc(p, sig);
951 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
954 * Reset stack state to the user stack.
955 * Clear set of signals caught on the signal stack.
957 td->td_sigstk.ss_flags = SS_DISABLE;
958 td->td_sigstk.ss_size = 0;
959 td->td_sigstk.ss_sp = 0;
960 td->td_pflags &= ~TDP_ALTSTACK;
962 * Reset no zombies if child dies flag as Solaris does.
964 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
965 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
966 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
967 mtx_unlock(&ps->ps_mtx);
973 * Manipulate signal mask.
976 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
979 sigset_t new_block, oset1;
984 if (!(flags & SIGPROCMASK_PROC_LOCKED))
987 *oset = td->td_sigmask;
990 SIGEMPTYSET(new_block);
995 oset1 = td->td_sigmask;
996 SIGSETOR(td->td_sigmask, *set);
997 new_block = td->td_sigmask;
998 SIGSETNAND(new_block, oset1);
1001 SIGSETNAND(td->td_sigmask, *set);
1006 oset1 = td->td_sigmask;
1007 if (flags & SIGPROCMASK_OLD)
1008 SIGSETLO(td->td_sigmask, *set);
1010 td->td_sigmask = *set;
1011 new_block = td->td_sigmask;
1012 SIGSETNAND(new_block, oset1);
1022 * The new_block set contains signals that were not previously
1023 * blocked, but are blocked now.
1025 * In case we block any signal that was not previously blocked
1026 * for td, and process has the signal pending, try to schedule
1027 * signal delivery to some thread that does not block the signal,
1028 * possibly waking it up.
1030 if (p->p_numthreads != 1)
1031 reschedule_signals(p, new_block, flags);
1033 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1038 #ifndef _SYS_SYSPROTO_H_
1039 struct sigprocmask_args {
1041 const sigset_t *set;
1046 sigprocmask(td, uap)
1047 register struct thread *td;
1048 struct sigprocmask_args *uap;
1051 sigset_t *setp, *osetp;
1054 setp = (uap->set != NULL) ? &set : NULL;
1055 osetp = (uap->oset != NULL) ? &oset : NULL;
1057 error = copyin(uap->set, setp, sizeof(set));
1061 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1062 if (osetp && !error) {
1063 error = copyout(osetp, uap->oset, sizeof(oset));
1068 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1069 #ifndef _SYS_SYSPROTO_H_
1070 struct osigprocmask_args {
1076 osigprocmask(td, uap)
1077 register struct thread *td;
1078 struct osigprocmask_args *uap;
1083 OSIG2SIG(uap->mask, set);
1084 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1085 SIG2OSIG(oset, td->td_retval[0]);
1088 #endif /* COMPAT_43 */
1091 sigwait(struct thread *td, struct sigwait_args *uap)
1097 error = copyin(uap->set, &set, sizeof(set));
1099 td->td_retval[0] = error;
1103 error = kern_sigtimedwait(td, set, &ksi, NULL);
1105 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1107 if (error == ERESTART)
1109 td->td_retval[0] = error;
1113 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1114 td->td_retval[0] = error;
1119 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1122 struct timespec *timeout;
1128 error = copyin(uap->timeout, &ts, sizeof(ts));
1136 error = copyin(uap->set, &set, sizeof(set));
1140 error = kern_sigtimedwait(td, set, &ksi, timeout);
1145 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1148 td->td_retval[0] = ksi.ksi_signo;
1153 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1159 error = copyin(uap->set, &set, sizeof(set));
1163 error = kern_sigtimedwait(td, set, &ksi, NULL);
1168 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1171 td->td_retval[0] = ksi.ksi_signo;
1176 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1177 struct timespec *timeout)
1182 int error, sig, hz, i, timevalid = 0;
1183 struct timespec rts, ets, ts;
1191 SIG_CANTMASK(waitset);
1195 savedmask = td->td_sigmask;
1197 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1199 getnanouptime(&rts);
1201 timespecadd(&ets, timeout);
1206 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1207 if (!SIGISMEMBER(waitset, i))
1209 if (!SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1210 if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
1211 sigqueue_move(&p->p_sigqueue,
1212 &td->td_sigqueue, i);
1217 SIGFILLSET(td->td_sigmask);
1218 SIG_CANTMASK(td->td_sigmask);
1219 SIGDELSET(td->td_sigmask, i);
1220 mtx_lock(&ps->ps_mtx);
1221 sig = cursig(td, SIG_STOP_ALLOWED);
1222 mtx_unlock(&ps->ps_mtx);
1227 * Because cursig() may have stopped current thread,
1228 * after it is resumed, things may have already been
1229 * changed, it should rescan any pending signals.
1239 * POSIX says this must be checked after looking for pending
1247 getnanouptime(&rts);
1248 if (timespeccmp(&rts, &ets, >=)) {
1253 timespecsub(&ts, &rts);
1254 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1259 td->td_sigmask = savedmask;
1260 SIGSETNAND(td->td_sigmask, waitset);
1262 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1264 if (error == ERESTART) {
1265 /* timeout can not be restarted. */
1267 } else if (error == EAGAIN) {
1268 /* will calculate timeout by ourself. */
1275 td->td_sigmask = savedmask;
1279 sigqueue_get(&td->td_sigqueue, sig, ksi);
1280 ksi->ksi_signo = sig;
1282 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1284 if (ksi->ksi_code == SI_TIMER)
1285 itimer_accept(p, ksi->ksi_timerid, ksi);
1289 if (KTRPOINT(td, KTR_PSIG)) {
1292 mtx_lock(&ps->ps_mtx);
1293 action = ps->ps_sigact[_SIG_IDX(sig)];
1294 mtx_unlock(&ps->ps_mtx);
1295 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1305 #ifndef _SYS_SYSPROTO_H_
1306 struct sigpending_args {
1313 struct sigpending_args *uap;
1315 struct proc *p = td->td_proc;
1319 pending = p->p_sigqueue.sq_signals;
1320 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1322 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1325 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1326 #ifndef _SYS_SYSPROTO_H_
1327 struct osigpending_args {
1332 osigpending(td, uap)
1334 struct osigpending_args *uap;
1336 struct proc *p = td->td_proc;
1340 pending = p->p_sigqueue.sq_signals;
1341 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1343 SIG2OSIG(pending, td->td_retval[0]);
1346 #endif /* COMPAT_43 */
1348 #if defined(COMPAT_43)
1350 * Generalized interface signal handler, 4.3-compatible.
1352 #ifndef _SYS_SYSPROTO_H_
1353 struct osigvec_args {
1363 register struct osigvec_args *uap;
1366 struct sigaction nsa, osa;
1367 register struct sigaction *nsap, *osap;
1370 if (uap->signum <= 0 || uap->signum >= ONSIG)
1372 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1373 osap = (uap->osv != NULL) ? &osa : NULL;
1375 error = copyin(uap->nsv, &vec, sizeof(vec));
1378 nsap->sa_handler = vec.sv_handler;
1379 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1380 nsap->sa_flags = vec.sv_flags;
1381 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1383 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1384 if (osap && !error) {
1385 vec.sv_handler = osap->sa_handler;
1386 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1387 vec.sv_flags = osap->sa_flags;
1388 vec.sv_flags &= ~SA_NOCLDWAIT;
1389 vec.sv_flags ^= SA_RESTART;
1390 error = copyout(&vec, uap->osv, sizeof(vec));
1395 #ifndef _SYS_SYSPROTO_H_
1396 struct osigblock_args {
1402 register struct thread *td;
1403 struct osigblock_args *uap;
1407 OSIG2SIG(uap->mask, set);
1408 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1409 SIG2OSIG(oset, td->td_retval[0]);
1413 #ifndef _SYS_SYSPROTO_H_
1414 struct osigsetmask_args {
1419 osigsetmask(td, uap)
1421 struct osigsetmask_args *uap;
1425 OSIG2SIG(uap->mask, set);
1426 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1427 SIG2OSIG(oset, td->td_retval[0]);
1430 #endif /* COMPAT_43 */
1433 * Suspend calling thread until signal, providing mask to be set in the
1436 #ifndef _SYS_SYSPROTO_H_
1437 struct sigsuspend_args {
1438 const sigset_t *sigmask;
1445 struct sigsuspend_args *uap;
1450 error = copyin(uap->sigmask, &mask, sizeof(mask));
1453 return (kern_sigsuspend(td, mask));
1457 kern_sigsuspend(struct thread *td, sigset_t mask)
1459 struct proc *p = td->td_proc;
1463 * When returning from sigsuspend, we want
1464 * the old mask to be restored after the
1465 * signal handler has finished. Thus, we
1466 * save it here and mark the sigacts structure
1470 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1471 SIGPROCMASK_PROC_LOCKED);
1472 td->td_pflags |= TDP_OLDMASK;
1475 * Process signals now. Otherwise, we can get spurious wakeup
1476 * due to signal entered process queue, but delivered to other
1477 * thread. But sigsuspend should return only on signal
1480 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1481 for (has_sig = 0; !has_sig;) {
1482 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1485 thread_suspend_check(0);
1486 mtx_lock(&p->p_sigacts->ps_mtx);
1487 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
1488 has_sig += postsig(sig);
1489 mtx_unlock(&p->p_sigacts->ps_mtx);
1492 td->td_errno = EINTR;
1493 td->td_pflags |= TDP_NERRNO;
1494 return (EJUSTRETURN);
1497 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1499 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1500 * convention: libc stub passes mask, not pointer, to save a copyin.
1502 #ifndef _SYS_SYSPROTO_H_
1503 struct osigsuspend_args {
1509 osigsuspend(td, uap)
1511 struct osigsuspend_args *uap;
1515 OSIG2SIG(uap->mask, mask);
1516 return (kern_sigsuspend(td, mask));
1518 #endif /* COMPAT_43 */
1520 #if defined(COMPAT_43)
1521 #ifndef _SYS_SYSPROTO_H_
1522 struct osigstack_args {
1523 struct sigstack *nss;
1524 struct sigstack *oss;
1531 register struct osigstack_args *uap;
1533 struct sigstack nss, oss;
1536 if (uap->nss != NULL) {
1537 error = copyin(uap->nss, &nss, sizeof(nss));
1541 oss.ss_sp = td->td_sigstk.ss_sp;
1542 oss.ss_onstack = sigonstack(cpu_getstack(td));
1543 if (uap->nss != NULL) {
1544 td->td_sigstk.ss_sp = nss.ss_sp;
1545 td->td_sigstk.ss_size = 0;
1546 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1547 td->td_pflags |= TDP_ALTSTACK;
1549 if (uap->oss != NULL)
1550 error = copyout(&oss, uap->oss, sizeof(oss));
1554 #endif /* COMPAT_43 */
1556 #ifndef _SYS_SYSPROTO_H_
1557 struct sigaltstack_args {
1564 sigaltstack(td, uap)
1566 register struct sigaltstack_args *uap;
1571 if (uap->ss != NULL) {
1572 error = copyin(uap->ss, &ss, sizeof(ss));
1576 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1577 (uap->oss != NULL) ? &oss : NULL);
1580 if (uap->oss != NULL)
1581 error = copyout(&oss, uap->oss, sizeof(stack_t));
1586 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1588 struct proc *p = td->td_proc;
1591 oonstack = sigonstack(cpu_getstack(td));
1594 *oss = td->td_sigstk;
1595 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1596 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1602 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1604 if (!(ss->ss_flags & SS_DISABLE)) {
1605 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1608 td->td_sigstk = *ss;
1609 td->td_pflags |= TDP_ALTSTACK;
1611 td->td_pflags &= ~TDP_ALTSTACK;
1618 * Common code for kill process group/broadcast kill.
1619 * cp is calling process.
1622 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1632 sx_slock(&allproc_lock);
1633 FOREACH_PROC_IN_SYSTEM(p) {
1635 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1636 p == td->td_proc || p->p_state == PRS_NEW) {
1640 if (p_cansignal(td, p, sig) == 0) {
1643 pksignal(p, sig, ksi);
1647 sx_sunlock(&allproc_lock);
1649 sx_slock(&proctree_lock);
1652 * zero pgid means send to my process group.
1654 pgrp = td->td_proc->p_pgrp;
1657 pgrp = pgfind(pgid);
1659 sx_sunlock(&proctree_lock);
1663 sx_sunlock(&proctree_lock);
1664 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1666 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1667 p->p_state == PRS_NEW ) {
1671 if (p_cansignal(td, p, sig) == 0) {
1674 pksignal(p, sig, ksi);
1680 return (nfound ? 0 : ESRCH);
1683 #ifndef _SYS_SYSPROTO_H_
1691 kill(struct thread *td, struct kill_args *uap)
1697 AUDIT_ARG_SIGNUM(uap->signum);
1698 AUDIT_ARG_PID(uap->pid);
1699 if ((u_int)uap->signum > _SIG_MAXSIG)
1702 ksiginfo_init(&ksi);
1703 ksi.ksi_signo = uap->signum;
1704 ksi.ksi_code = SI_USER;
1705 ksi.ksi_pid = td->td_proc->p_pid;
1706 ksi.ksi_uid = td->td_ucred->cr_ruid;
1709 /* kill single process */
1710 if ((p = pfind(uap->pid)) == NULL) {
1711 if ((p = zpfind(uap->pid)) == NULL)
1714 AUDIT_ARG_PROCESS(p);
1715 error = p_cansignal(td, p, uap->signum);
1716 if (error == 0 && uap->signum)
1717 pksignal(p, uap->signum, &ksi);
1722 case -1: /* broadcast signal */
1723 return (killpg1(td, uap->signum, 0, 1, &ksi));
1724 case 0: /* signal own process group */
1725 return (killpg1(td, uap->signum, 0, 0, &ksi));
1726 default: /* negative explicit process group */
1727 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1732 #if defined(COMPAT_43)
1733 #ifndef _SYS_SYSPROTO_H_
1734 struct okillpg_args {
1741 okillpg(struct thread *td, struct okillpg_args *uap)
1745 AUDIT_ARG_SIGNUM(uap->signum);
1746 AUDIT_ARG_PID(uap->pgid);
1747 if ((u_int)uap->signum > _SIG_MAXSIG)
1750 ksiginfo_init(&ksi);
1751 ksi.ksi_signo = uap->signum;
1752 ksi.ksi_code = SI_USER;
1753 ksi.ksi_pid = td->td_proc->p_pid;
1754 ksi.ksi_uid = td->td_ucred->cr_ruid;
1755 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1757 #endif /* COMPAT_43 */
1759 #ifndef _SYS_SYSPROTO_H_
1760 struct sigqueue_args {
1763 /* union sigval */ void *value;
1767 sigqueue(struct thread *td, struct sigqueue_args *uap)
1773 if ((u_int)uap->signum > _SIG_MAXSIG)
1777 * Specification says sigqueue can only send signal to
1783 if ((p = pfind(uap->pid)) == NULL) {
1784 if ((p = zpfind(uap->pid)) == NULL)
1787 error = p_cansignal(td, p, uap->signum);
1788 if (error == 0 && uap->signum != 0) {
1789 ksiginfo_init(&ksi);
1790 ksi.ksi_flags = KSI_SIGQ;
1791 ksi.ksi_signo = uap->signum;
1792 ksi.ksi_code = SI_QUEUE;
1793 ksi.ksi_pid = td->td_proc->p_pid;
1794 ksi.ksi_uid = td->td_ucred->cr_ruid;
1795 ksi.ksi_value.sival_ptr = uap->value;
1796 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1803 * Send a signal to a process group.
1806 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1811 sx_slock(&proctree_lock);
1812 pgrp = pgfind(pgid);
1813 sx_sunlock(&proctree_lock);
1815 pgsignal(pgrp, sig, 0, ksi);
1822 * Send a signal to a process group. If checktty is 1,
1823 * limit to members which have a controlling terminal.
1826 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1831 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1832 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1834 if (p->p_state == PRS_NORMAL &&
1835 (checkctty == 0 || p->p_flag & P_CONTROLT))
1836 pksignal(p, sig, ksi);
1843 * Send a signal caused by a trap to the current thread. If it will be
1844 * caught immediately, deliver it with correct code. Otherwise, post it
1848 trapsignal(struct thread *td, ksiginfo_t *ksi)
1857 sig = ksi->ksi_signo;
1858 code = ksi->ksi_code;
1859 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1863 mtx_lock(&ps->ps_mtx);
1864 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1865 !SIGISMEMBER(td->td_sigmask, sig)) {
1866 td->td_ru.ru_nsignals++;
1868 if (KTRPOINT(curthread, KTR_PSIG))
1869 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1870 &td->td_sigmask, code);
1872 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1873 ksi, &td->td_sigmask);
1874 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1875 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1876 SIGADDSET(mask, sig);
1877 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1878 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1879 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1881 * See kern_sigaction() for origin of this code.
1883 SIGDELSET(ps->ps_sigcatch, sig);
1884 if (sig != SIGCONT &&
1885 sigprop(sig) & SA_IGNORE)
1886 SIGADDSET(ps->ps_sigignore, sig);
1887 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1889 mtx_unlock(&ps->ps_mtx);
1892 * Avoid a possible infinite loop if the thread
1893 * masking the signal or process is ignoring the
1896 if (kern_forcesigexit &&
1897 (SIGISMEMBER(td->td_sigmask, sig) ||
1898 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1899 SIGDELSET(td->td_sigmask, sig);
1900 SIGDELSET(ps->ps_sigcatch, sig);
1901 SIGDELSET(ps->ps_sigignore, sig);
1902 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1904 mtx_unlock(&ps->ps_mtx);
1905 p->p_code = code; /* XXX for core dump/debugger */
1906 p->p_sig = sig; /* XXX to verify code */
1907 tdsignal(p, td, sig, ksi);
1912 static struct thread *
1913 sigtd(struct proc *p, int sig, int prop)
1915 struct thread *td, *signal_td;
1917 PROC_LOCK_ASSERT(p, MA_OWNED);
1920 * Check if current thread can handle the signal without
1921 * switching context to another thread.
1923 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1926 FOREACH_THREAD_IN_PROC(p, td) {
1927 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1932 if (signal_td == NULL)
1933 signal_td = FIRST_THREAD_IN_PROC(p);
1938 * Send the signal to the process. If the signal has an action, the action
1939 * is usually performed by the target process rather than the caller; we add
1940 * the signal to the set of pending signals for the process.
1943 * o When a stop signal is sent to a sleeping process that takes the
1944 * default action, the process is stopped without awakening it.
1945 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1946 * regardless of the signal action (eg, blocked or ignored).
1948 * Other ignored signals are discarded immediately.
1950 * NB: This function may be entered from the debugger via the "kill" DDB
1951 * command. There is little that can be done to mitigate the possibly messy
1952 * side effects of this unwise possibility.
1955 psignal(struct proc *p, int sig)
1959 ksiginfo_init(&ksi);
1960 ksi.ksi_signo = sig;
1961 ksi.ksi_code = SI_KERNEL;
1962 (void) tdsignal(p, NULL, sig, &ksi);
1966 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1969 (void) tdsignal(p, NULL, sig, ksi);
1973 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
1975 struct thread *td = NULL;
1977 PROC_LOCK_ASSERT(p, MA_OWNED);
1979 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
1982 * ksi_code and other fields should be set before
1983 * calling this function.
1985 ksi->ksi_signo = sigev->sigev_signo;
1986 ksi->ksi_value = sigev->sigev_value;
1987 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1988 td = thread_find(p, sigev->sigev_notify_thread_id);
1992 return (tdsignal(p, td, ksi->ksi_signo, ksi));
1996 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
1998 ksiginfo_t ksi_thunk;
2001 * If ksi is NULL, use ksi_thunk and provide semantics
2002 * identical to tdsignal() in 9.0+.
2007 ksi->ksi_signo = sig;
2008 ksi->ksi_code = SI_KERNEL;
2010 (void) tdsignal(td->td_proc, td, sig, ksi);
2014 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2017 sigqueue_t *sigqueue;
2024 PROC_LOCK_ASSERT(p, MA_OWNED);
2026 if (!_SIG_VALID(sig))
2027 panic("tdsignal(): invalid signal %d", sig);
2029 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("tdsignal: ksi on queue"));
2032 * IEEE Std 1003.1-2001: return success when killing a zombie.
2034 if (p->p_state == PRS_ZOMBIE) {
2035 if (ksi && (ksi->ksi_flags & KSI_INS))
2036 ksiginfo_tryfree(ksi);
2041 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2042 prop = sigprop(sig);
2045 td = sigtd(p, sig, prop);
2046 sigqueue = &p->p_sigqueue;
2048 KASSERT(td->td_proc == p, ("invalid thread"));
2049 sigqueue = &td->td_sigqueue;
2052 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2055 * If the signal is being ignored,
2056 * then we forget about it immediately.
2057 * (Note: we don't set SIGCONT in ps_sigignore,
2058 * and if it is set to SIG_IGN,
2059 * action will be SIG_DFL here.)
2061 mtx_lock(&ps->ps_mtx);
2062 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2063 SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 );
2065 mtx_unlock(&ps->ps_mtx);
2066 if (ksi && (ksi->ksi_flags & KSI_INS))
2067 ksiginfo_tryfree(ksi);
2070 if (SIGISMEMBER(td->td_sigmask, sig))
2072 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2076 if (SIGISMEMBER(ps->ps_sigintr, sig))
2080 mtx_unlock(&ps->ps_mtx);
2083 sigqueue_delete_stopmask_proc(p);
2084 else if (prop & SA_STOP) {
2086 * If sending a tty stop signal to a member of an orphaned
2087 * process group, discard the signal here if the action
2088 * is default; don't stop the process below if sleeping,
2089 * and don't clear any pending SIGCONT.
2091 if ((prop & SA_TTYSTOP) &&
2092 (p->p_pgrp->pg_jobc == 0) &&
2093 (action == SIG_DFL)) {
2094 if (ksi && (ksi->ksi_flags & KSI_INS))
2095 ksiginfo_tryfree(ksi);
2098 sigqueue_delete_proc(p, SIGCONT);
2099 if (p->p_flag & P_CONTINUED) {
2100 p->p_flag &= ~P_CONTINUED;
2101 PROC_LOCK(p->p_pptr);
2102 sigqueue_take(p->p_ksi);
2103 PROC_UNLOCK(p->p_pptr);
2107 ret = sigqueue_add(sigqueue, sig, ksi);
2112 * Defer further processing for signals which are held,
2113 * except that stopped processes must be continued by SIGCONT.
2115 if (action == SIG_HOLD &&
2116 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2119 * SIGKILL: Remove procfs STOPEVENTs.
2121 if (sig == SIGKILL) {
2122 /* from procfs_ioctl.c: PIOCBIC */
2124 /* from procfs_ioctl.c: PIOCCONT */
2129 * Some signals have a process-wide effect and a per-thread
2130 * component. Most processing occurs when the process next
2131 * tries to cross the user boundary, however there are some
2132 * times when processing needs to be done immediatly, such as
2133 * waking up threads so that they can cross the user boundary.
2134 * We try do the per-process part here.
2136 if (P_SHOULDSTOP(p)) {
2137 if (sig == SIGKILL) {
2139 * If traced process is already stopped,
2140 * then no further action is necessary.
2142 if (p->p_flag & P_TRACED)
2145 * SIGKILL sets process running.
2146 * It will die elsewhere.
2147 * All threads must be restarted.
2149 p->p_flag &= ~P_STOPPED_SIG;
2153 if (prop & SA_CONT) {
2155 * If traced process is already stopped,
2156 * then no further action is necessary.
2158 if (p->p_flag & P_TRACED)
2161 * If SIGCONT is default (or ignored), we continue the
2162 * process but don't leave the signal in sigqueue as
2163 * it has no further action. If SIGCONT is held, we
2164 * continue the process and leave the signal in
2165 * sigqueue. If the process catches SIGCONT, let it
2166 * handle the signal itself. If it isn't waiting on
2167 * an event, it goes back to run state.
2168 * Otherwise, process goes back to sleep state.
2170 p->p_flag &= ~P_STOPPED_SIG;
2172 if (p->p_numthreads == p->p_suspcount) {
2174 p->p_flag |= P_CONTINUED;
2175 p->p_xstat = SIGCONT;
2176 PROC_LOCK(p->p_pptr);
2177 childproc_continued(p);
2178 PROC_UNLOCK(p->p_pptr);
2181 if (action == SIG_DFL) {
2182 thread_unsuspend(p);
2184 sigqueue_delete(sigqueue, sig);
2187 if (action == SIG_CATCH) {
2189 * The process wants to catch it so it needs
2190 * to run at least one thread, but which one?
2196 * The signal is not ignored or caught.
2198 thread_unsuspend(p);
2203 if (prop & SA_STOP) {
2205 * If traced process is already stopped,
2206 * then no further action is necessary.
2208 if (p->p_flag & P_TRACED)
2211 * Already stopped, don't need to stop again
2212 * (If we did the shell could get confused).
2213 * Just make sure the signal STOP bit set.
2215 p->p_flag |= P_STOPPED_SIG;
2216 sigqueue_delete(sigqueue, sig);
2221 * All other kinds of signals:
2222 * If a thread is sleeping interruptibly, simulate a
2223 * wakeup so that when it is continued it will be made
2224 * runnable and can look at the signal. However, don't make
2225 * the PROCESS runnable, leave it stopped.
2226 * It may run a bit until it hits a thread_suspend_check().
2231 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2232 wakeup_swapper = sleepq_abort(td, intrval);
2239 * Mutexes are short lived. Threads waiting on them will
2240 * hit thread_suspend_check() soon.
2242 } else if (p->p_state == PRS_NORMAL) {
2243 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2244 tdsigwakeup(td, sig, action, intrval);
2248 MPASS(action == SIG_DFL);
2250 if (prop & SA_STOP) {
2251 if (p->p_flag & P_PPWAIT)
2253 p->p_flag |= P_STOPPED_SIG;
2256 sig_suspend_threads(td, p, 1);
2257 if (p->p_numthreads == p->p_suspcount) {
2259 * only thread sending signal to another
2260 * process can reach here, if thread is sending
2261 * signal to its process, because thread does
2262 * not suspend itself here, p_numthreads
2263 * should never be equal to p_suspcount.
2267 sigqueue_delete_proc(p, p->p_xstat);
2273 /* Not in "NORMAL" state. discard the signal. */
2274 sigqueue_delete(sigqueue, sig);
2279 * The process is not stopped so we need to apply the signal to all the
2283 tdsigwakeup(td, sig, action, intrval);
2285 thread_unsuspend(p);
2288 /* If we jump here, proc slock should not be owned. */
2289 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2294 * The force of a signal has been directed against a single
2295 * thread. We need to see what we can do about knocking it
2296 * out of any sleep it may be in etc.
2299 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2301 struct proc *p = td->td_proc;
2306 PROC_LOCK_ASSERT(p, MA_OWNED);
2307 prop = sigprop(sig);
2312 * Bring the priority of a thread up if we want it to get
2313 * killed in this lifetime.
2315 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2316 sched_prio(td, PUSER);
2317 if (TD_ON_SLEEPQ(td)) {
2319 * If thread is sleeping uninterruptibly
2320 * we can't interrupt the sleep... the signal will
2321 * be noticed when the process returns through
2322 * trap() or syscall().
2324 if ((td->td_flags & TDF_SINTR) == 0)
2327 * If SIGCONT is default (or ignored) and process is
2328 * asleep, we are finished; the process should not
2331 if ((prop & SA_CONT) && action == SIG_DFL) {
2334 sigqueue_delete(&p->p_sigqueue, sig);
2336 * It may be on either list in this state.
2337 * Remove from both for now.
2339 sigqueue_delete(&td->td_sigqueue, sig);
2344 * Give low priority threads a better chance to run.
2346 if (td->td_priority > PUSER)
2347 sched_prio(td, PUSER);
2349 wakeup_swapper = sleepq_abort(td, intrval);
2352 * Other states do nothing with the signal immediately,
2353 * other than kicking ourselves if we are running.
2354 * It will either never be noticed, or noticed very soon.
2357 if (TD_IS_RUNNING(td) && td != curthread)
2369 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2374 PROC_LOCK_ASSERT(p, MA_OWNED);
2375 PROC_SLOCK_ASSERT(p, MA_OWNED);
2378 FOREACH_THREAD_IN_PROC(p, td2) {
2380 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2381 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2382 (td2->td_flags & TDF_SINTR)) {
2383 if (td2->td_flags & TDF_SBDRY) {
2384 if (TD_IS_SUSPENDED(td2))
2386 thread_unsuspend_one(td2);
2387 if (TD_ON_SLEEPQ(td2))
2389 sleepq_abort(td2, ERESTART);
2390 } else if (!TD_IS_SUSPENDED(td2)) {
2391 thread_suspend_one(td2);
2393 } else if (!TD_IS_SUSPENDED(td2)) {
2394 if (sending || td != td2)
2395 td2->td_flags |= TDF_ASTPENDING;
2397 if (TD_IS_RUNNING(td2) && td2 != td)
2398 forward_signal(td2);
2408 ptracestop(struct thread *td, int sig)
2410 struct proc *p = td->td_proc;
2412 PROC_LOCK_ASSERT(p, MA_OWNED);
2413 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2414 &p->p_mtx.lock_object, "Stopping for traced signal");
2416 td->td_dbgflags |= TDB_XSIG;
2419 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2420 if (p->p_flag & P_SINGLE_EXIT) {
2421 td->td_dbgflags &= ~TDB_XSIG;
2426 * Just make wait() to work, the last stopped thread
2431 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2432 sig_suspend_threads(td, p, 0);
2433 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2434 td->td_dbgflags &= ~TDB_STOPATFORK;
2435 cv_broadcast(&p->p_dbgwait);
2438 thread_suspend_switch(td);
2439 if (!(p->p_flag & P_TRACED)) {
2442 if (td->td_dbgflags & TDB_SUSPEND) {
2443 if (p->p_flag & P_SINGLE_EXIT)
2449 return (td->td_xsig);
2453 reschedule_signals(struct proc *p, sigset_t block, int flags)
2459 PROC_LOCK_ASSERT(p, MA_OWNED);
2462 for (i = 1; !SIGISEMPTY(block); i++) {
2463 if (!SIGISMEMBER(block, i))
2465 SIGDELSET(block, i);
2466 if (!SIGISMEMBER(p->p_siglist, i))
2469 td = sigtd(p, i, 0);
2471 if (!(flags & SIGPROCMASK_PS_LOCKED))
2472 mtx_lock(&ps->ps_mtx);
2473 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, i))
2474 tdsigwakeup(td, i, SIG_CATCH,
2475 (SIGISMEMBER(ps->ps_sigintr, i) ? EINTR :
2477 if (!(flags & SIGPROCMASK_PS_LOCKED))
2478 mtx_unlock(&ps->ps_mtx);
2483 tdsigcleanup(struct thread *td)
2489 PROC_LOCK_ASSERT(p, MA_OWNED);
2491 sigqueue_flush(&td->td_sigqueue);
2492 if (p->p_numthreads == 1)
2496 * Since we cannot handle signals, notify signal post code
2497 * about this by filling the sigmask.
2499 * Also, if needed, wake up thread(s) that do not block the
2500 * same signals as the exiting thread, since the thread might
2501 * have been selected for delivery and woken up.
2503 SIGFILLSET(unblocked);
2504 SIGSETNAND(unblocked, td->td_sigmask);
2505 SIGFILLSET(td->td_sigmask);
2506 reschedule_signals(p, unblocked, 0);
2511 * If the current process has received a signal (should be caught or cause
2512 * termination, should interrupt current syscall), return the signal number.
2513 * Stop signals with default action are processed immediately, then cleared;
2514 * they aren't returned. This is checked after each entry to the system for
2515 * a syscall or trap (though this can usually be done without calling issignal
2516 * by checking the pending signal masks in cursig.) The normal call
2519 * while (sig = cursig(curthread))
2523 issignal(struct thread *td, int stop_allowed)
2527 struct sigqueue *queue;
2528 sigset_t sigpending;
2529 int sig, prop, newsig;
2533 mtx_assert(&ps->ps_mtx, MA_OWNED);
2534 PROC_LOCK_ASSERT(p, MA_OWNED);
2536 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2538 sigpending = td->td_sigqueue.sq_signals;
2539 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2540 SIGSETNAND(sigpending, td->td_sigmask);
2542 if (p->p_flag & P_PPWAIT)
2543 SIG_STOPSIGMASK(sigpending);
2544 if (SIGISEMPTY(sigpending)) /* no signal to send */
2546 sig = sig_ffs(&sigpending);
2548 if (p->p_stops & S_SIG) {
2549 mtx_unlock(&ps->ps_mtx);
2550 stopevent(p, S_SIG, sig);
2551 mtx_lock(&ps->ps_mtx);
2555 * We should see pending but ignored signals
2556 * only if P_TRACED was on when they were posted.
2558 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2559 sigqueue_delete(&td->td_sigqueue, sig);
2560 sigqueue_delete(&p->p_sigqueue, sig);
2563 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2565 * If traced, always stop.
2566 * Remove old signal from queue before the stop.
2567 * XXX shrug off debugger, it causes siginfo to
2570 queue = &td->td_sigqueue;
2571 td->td_dbgksi.ksi_signo = 0;
2572 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2573 queue = &p->p_sigqueue;
2574 sigqueue_get(queue, sig, &td->td_dbgksi);
2577 mtx_unlock(&ps->ps_mtx);
2578 newsig = ptracestop(td, sig);
2579 mtx_lock(&ps->ps_mtx);
2581 if (sig != newsig) {
2584 * If parent wants us to take the signal,
2585 * then it will leave it in p->p_xstat;
2586 * otherwise we just look for signals again.
2593 * Put the new signal into td_sigqueue. If the
2594 * signal is being masked, look for other signals.
2596 sigqueue_add(queue, sig, NULL);
2597 if (SIGISMEMBER(td->td_sigmask, sig))
2601 if (td->td_dbgksi.ksi_signo != 0) {
2602 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2603 if (sigqueue_add(&td->td_sigqueue, sig,
2604 &td->td_dbgksi) != 0)
2605 td->td_dbgksi.ksi_signo = 0;
2607 if (td->td_dbgksi.ksi_signo == 0)
2608 sigqueue_add(&td->td_sigqueue, sig,
2613 * If the traced bit got turned off, go back up
2614 * to the top to rescan signals. This ensures
2615 * that p_sig* and p_sigact are consistent.
2617 if ((p->p_flag & P_TRACED) == 0)
2621 prop = sigprop(sig);
2624 * Decide whether the signal should be returned.
2625 * Return the signal's number, or fall through
2626 * to clear it from the pending mask.
2628 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2630 case (intptr_t)SIG_DFL:
2632 * Don't take default actions on system processes.
2634 if (p->p_pid <= 1) {
2637 * Are you sure you want to ignore SIGSEGV
2640 printf("Process (pid %lu) got signal %d\n",
2641 (u_long)p->p_pid, sig);
2643 break; /* == ignore */
2646 * If there is a pending stop signal to process
2647 * with default action, stop here,
2648 * then clear the signal. However,
2649 * if process is member of an orphaned
2650 * process group, ignore tty stop signals.
2652 if (prop & SA_STOP) {
2653 if (p->p_flag & P_TRACED ||
2654 (p->p_pgrp->pg_jobc == 0 &&
2656 break; /* == ignore */
2658 /* Ignore, but do not drop the stop signal. */
2659 if (stop_allowed != SIG_STOP_ALLOWED)
2661 mtx_unlock(&ps->ps_mtx);
2662 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2663 &p->p_mtx.lock_object, "Catching SIGSTOP");
2664 p->p_flag |= P_STOPPED_SIG;
2667 sig_suspend_threads(td, p, 0);
2668 thread_suspend_switch(td);
2670 mtx_lock(&ps->ps_mtx);
2672 } else if (prop & SA_IGNORE) {
2674 * Except for SIGCONT, shouldn't get here.
2675 * Default action is to ignore; drop it.
2677 break; /* == ignore */
2682 case (intptr_t)SIG_IGN:
2684 * Masking above should prevent us ever trying
2685 * to take action on an ignored signal other
2686 * than SIGCONT, unless process is traced.
2688 if ((prop & SA_CONT) == 0 &&
2689 (p->p_flag & P_TRACED) == 0)
2690 printf("issignal\n");
2691 break; /* == ignore */
2695 * This signal has an action, let
2696 * postsig() process it.
2700 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2701 sigqueue_delete(&p->p_sigqueue, sig);
2707 thread_stopped(struct proc *p)
2711 PROC_LOCK_ASSERT(p, MA_OWNED);
2712 PROC_SLOCK_ASSERT(p, MA_OWNED);
2716 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2718 p->p_flag &= ~P_WAITED;
2719 PROC_LOCK(p->p_pptr);
2720 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2721 CLD_TRAPPED : CLD_STOPPED);
2722 PROC_UNLOCK(p->p_pptr);
2728 * Take the action for the specified signal
2729 * from the current set of pending signals.
2735 struct thread *td = curthread;
2736 register struct proc *p = td->td_proc;
2740 sigset_t returnmask, mask;
2742 KASSERT(sig != 0, ("postsig"));
2744 PROC_LOCK_ASSERT(p, MA_OWNED);
2746 mtx_assert(&ps->ps_mtx, MA_OWNED);
2747 ksiginfo_init(&ksi);
2748 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2749 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2751 ksi.ksi_signo = sig;
2752 if (ksi.ksi_code == SI_TIMER)
2753 itimer_accept(p, ksi.ksi_timerid, &ksi);
2754 action = ps->ps_sigact[_SIG_IDX(sig)];
2756 if (KTRPOINT(td, KTR_PSIG))
2757 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2758 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2760 if (p->p_stops & S_SIG) {
2761 mtx_unlock(&ps->ps_mtx);
2762 stopevent(p, S_SIG, sig);
2763 mtx_lock(&ps->ps_mtx);
2766 if (action == SIG_DFL) {
2768 * Default action, where the default is to kill
2769 * the process. (Other cases were ignored above.)
2771 mtx_unlock(&ps->ps_mtx);
2776 * If we get here, the signal must be caught.
2778 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2779 ("postsig action"));
2781 * Set the new mask value and also defer further
2782 * occurrences of this signal.
2784 * Special case: user has done a sigsuspend. Here the
2785 * current mask is not of interest, but rather the
2786 * mask from before the sigsuspend is what we want
2787 * restored after the signal processing is completed.
2789 if (td->td_pflags & TDP_OLDMASK) {
2790 returnmask = td->td_oldsigmask;
2791 td->td_pflags &= ~TDP_OLDMASK;
2793 returnmask = td->td_sigmask;
2795 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2796 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2797 SIGADDSET(mask, sig);
2798 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2799 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2801 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2803 * See kern_sigaction() for origin of this code.
2805 SIGDELSET(ps->ps_sigcatch, sig);
2806 if (sig != SIGCONT &&
2807 sigprop(sig) & SA_IGNORE)
2808 SIGADDSET(ps->ps_sigignore, sig);
2809 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2811 td->td_ru.ru_nsignals++;
2812 if (p->p_sig == sig) {
2816 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2822 * Kill the current process for stated reason.
2830 PROC_LOCK_ASSERT(p, MA_OWNED);
2831 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2832 p, p->p_pid, p->p_comm);
2833 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2834 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2835 p->p_flag |= P_WKILLED;
2836 psignal(p, SIGKILL);
2840 * Force the current process to exit with the specified signal, dumping core
2841 * if appropriate. We bypass the normal tests for masked and caught signals,
2842 * allowing unrecoverable failures to terminate the process without changing
2843 * signal state. Mark the accounting record with the signal termination.
2844 * If dumping core, save the signal number for the debugger. Calls exit and
2852 struct proc *p = td->td_proc;
2854 PROC_LOCK_ASSERT(p, MA_OWNED);
2855 p->p_acflag |= AXSIG;
2857 * We must be single-threading to generate a core dump. This
2858 * ensures that the registers in the core file are up-to-date.
2859 * Also, the ELF dump handler assumes that the thread list doesn't
2860 * change out from under it.
2862 * XXX If another thread attempts to single-thread before us
2863 * (e.g. via fork()), we won't get a dump at all.
2865 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2868 * Log signals which would cause core dumps
2869 * (Log as LOG_INFO to appease those who don't want
2871 * XXX : Todo, as well as euid, write out ruid too
2872 * Note that coredump() drops proc lock.
2874 if (coredump(td) == 0)
2876 if (kern_logsigexit)
2878 "pid %d (%s), uid %d: exited on signal %d%s\n",
2879 p->p_pid, p->p_comm,
2880 td->td_ucred ? td->td_ucred->cr_uid : -1,
2882 sig & WCOREFLAG ? " (core dumped)" : "");
2885 exit1(td, W_EXITCODE(0, sig));
2890 * Send queued SIGCHLD to parent when child process's state
2894 sigparent(struct proc *p, int reason, int status)
2896 PROC_LOCK_ASSERT(p, MA_OWNED);
2897 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2899 if (p->p_ksi != NULL) {
2900 p->p_ksi->ksi_signo = SIGCHLD;
2901 p->p_ksi->ksi_code = reason;
2902 p->p_ksi->ksi_status = status;
2903 p->p_ksi->ksi_pid = p->p_pid;
2904 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2905 if (KSI_ONQ(p->p_ksi))
2908 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2912 childproc_jobstate(struct proc *p, int reason, int status)
2916 PROC_LOCK_ASSERT(p, MA_OWNED);
2917 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2920 * Wake up parent sleeping in kern_wait(), also send
2921 * SIGCHLD to parent, but SIGCHLD does not guarantee
2922 * that parent will awake, because parent may masked
2925 p->p_pptr->p_flag |= P_STATCHILD;
2928 ps = p->p_pptr->p_sigacts;
2929 mtx_lock(&ps->ps_mtx);
2930 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2931 mtx_unlock(&ps->ps_mtx);
2932 sigparent(p, reason, status);
2934 mtx_unlock(&ps->ps_mtx);
2938 childproc_stopped(struct proc *p, int reason)
2940 childproc_jobstate(p, reason, p->p_xstat);
2944 childproc_continued(struct proc *p)
2946 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2950 childproc_exited(struct proc *p)
2953 int status = p->p_xstat; /* convert to int */
2955 reason = CLD_EXITED;
2956 if (WCOREDUMP(status))
2957 reason = CLD_DUMPED;
2958 else if (WIFSIGNALED(status))
2959 reason = CLD_KILLED;
2961 * XXX avoid calling wakeup(p->p_pptr), the work is
2964 sigparent(p, reason, status);
2967 static char corefilename[MAXPATHLEN] = {"%N.core"};
2968 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2969 sizeof(corefilename), "process corefile name format string");
2972 * expand_name(name, uid, pid)
2973 * Expand the name described in corefilename, using name, uid, and pid.
2974 * corefilename is a printf-like string, with three format specifiers:
2975 * %N name of process ("name")
2976 * %P process id (pid)
2978 * For example, "%N.core" is the default; they can be disabled completely
2979 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2980 * This is controlled by the sysctl variable kern.corefile (see above).
2983 expand_name(name, uid, pid)
2993 format = corefilename;
2994 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2997 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
2998 for (i = 0; format[i]; i++) {
2999 switch (format[i]) {
3000 case '%': /* Format character */
3002 switch (format[i]) {
3004 sbuf_putc(&sb, '%');
3006 case 'N': /* process name */
3007 sbuf_printf(&sb, "%s", name);
3009 case 'P': /* process id */
3010 sbuf_printf(&sb, "%u", pid);
3012 case 'U': /* user id */
3013 sbuf_printf(&sb, "%u", uid);
3017 "Unknown format character %c in "
3018 "corename `%s'\n", format[i], format);
3022 sbuf_putc(&sb, format[i]);
3025 if (sbuf_overflowed(&sb)) {
3027 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3028 "long\n", (long)pid, name, (u_long)uid);
3038 * Dump a process' core. The main routine does some
3039 * policy checking, and creates the name of the coredump;
3040 * then it passes on a vnode and a size limit to the process-specific
3041 * coredump routine if there is one; if there _is not_ one, it returns
3042 * ENOSYS; otherwise it returns the error from the process-specific routine.
3046 coredump(struct thread *td)
3048 struct proc *p = td->td_proc;
3049 register struct vnode *vp;
3050 register struct ucred *cred = td->td_ucred;
3052 struct nameidata nd;
3054 int error, error1, flags, locked;
3056 char *name; /* name of corefile */
3060 PROC_LOCK_ASSERT(p, MA_OWNED);
3061 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3062 _STOPEVENT(p, S_CORE, 0);
3064 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
3068 audit_proc_coredump(td, NULL, EINVAL);
3072 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3075 audit_proc_coredump(td, name, EFAULT);
3082 * Note that the bulk of limit checking is done after
3083 * the corefile is created. The exception is if the limit
3084 * for corefiles is 0, in which case we don't bother
3085 * creating the corefile at all. This layout means that
3086 * a corefile is truncated instead of not being created,
3087 * if it is larger than the limit.
3089 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3093 audit_proc_coredump(td, name, EFBIG);
3100 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3101 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3102 error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
3106 audit_proc_coredump(td, name, error);
3111 vfslocked = NDHASGIANT(&nd);
3112 NDFREE(&nd, NDF_ONLY_PNBUF);
3115 /* Don't dump to non-regular files or files with links. */
3116 if (vp->v_type != VREG ||
3117 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
3124 lf.l_whence = SEEK_SET;
3127 lf.l_type = F_WRLCK;
3128 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3130 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3131 lf.l_type = F_UNLCK;
3133 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3134 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3136 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3138 VFS_UNLOCK_GIANT(vfslocked);
3144 if (set_core_nodump_flag)
3145 vattr.va_flags = UF_NODUMP;
3146 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3147 VOP_SETATTR(vp, &vattr, cred);
3149 vn_finished_write(mp);
3151 p->p_acflag |= ACORE;
3154 error = p->p_sysent->sv_coredump ?
3155 p->p_sysent->sv_coredump(td, vp, limit) :
3159 lf.l_type = F_UNLCK;
3160 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3163 error1 = vn_close(vp, FWRITE, cred, td);
3168 audit_proc_coredump(td, name, error);
3171 VFS_UNLOCK_GIANT(vfslocked);
3176 * Nonexistent system call-- signal process (may want to handle it). Flag
3177 * error in case process won't see signal immediately (blocked or ignored).
3179 #ifndef _SYS_SYSPROTO_H_
3188 struct nosys_args *args;
3190 struct proc *p = td->td_proc;
3199 * Send a SIGIO or SIGURG signal to a process or process group using stored
3200 * credentials rather than those of the current process.
3203 pgsigio(sigiop, sig, checkctty)
3204 struct sigio **sigiop;
3208 struct sigio *sigio;
3210 ksiginfo_init(&ksi);
3211 ksi.ksi_signo = sig;
3212 ksi.ksi_code = SI_KERNEL;
3216 if (sigio == NULL) {
3220 if (sigio->sio_pgid > 0) {
3221 PROC_LOCK(sigio->sio_proc);
3222 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3223 psignal(sigio->sio_proc, sig);
3224 PROC_UNLOCK(sigio->sio_proc);
3225 } else if (sigio->sio_pgid < 0) {
3228 PGRP_LOCK(sigio->sio_pgrp);
3229 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3231 if (p->p_state == PRS_NORMAL &&
3232 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3233 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3237 PGRP_UNLOCK(sigio->sio_pgrp);
3243 filt_sigattach(struct knote *kn)
3245 struct proc *p = curproc;
3247 kn->kn_ptr.p_proc = p;
3248 kn->kn_flags |= EV_CLEAR; /* automatically set */
3250 knlist_add(&p->p_klist, kn, 0);
3256 filt_sigdetach(struct knote *kn)
3258 struct proc *p = kn->kn_ptr.p_proc;
3260 knlist_remove(&p->p_klist, kn, 0);
3264 * signal knotes are shared with proc knotes, so we apply a mask to
3265 * the hint in order to differentiate them from process hints. This
3266 * could be avoided by using a signal-specific knote list, but probably
3267 * isn't worth the trouble.
3270 filt_signal(struct knote *kn, long hint)
3273 if (hint & NOTE_SIGNAL) {
3274 hint &= ~NOTE_SIGNAL;
3276 if (kn->kn_id == hint)
3279 return (kn->kn_data != 0);
3287 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3289 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3294 sigacts_free(struct sigacts *ps)
3297 mtx_lock(&ps->ps_mtx);
3299 if (ps->ps_refcnt == 0) {
3300 mtx_destroy(&ps->ps_mtx);
3301 free(ps, M_SUBPROC);
3303 mtx_unlock(&ps->ps_mtx);
3307 sigacts_hold(struct sigacts *ps)
3309 mtx_lock(&ps->ps_mtx);
3311 mtx_unlock(&ps->ps_mtx);
3316 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3319 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3320 mtx_lock(&src->ps_mtx);
3321 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3322 mtx_unlock(&src->ps_mtx);
3326 sigacts_shared(struct sigacts *ps)
3330 mtx_lock(&ps->ps_mtx);
3331 shared = ps->ps_refcnt > 1;
3332 mtx_unlock(&ps->ps_mtx);