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);
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);
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);
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);
103 static int issignal(struct thread *td, int stop_allowed);
104 static int sigprop(int sig);
105 static void tdsigwakeup(struct thread *, int, sig_t, int);
106 static void sig_suspend_threads(struct thread *, struct proc *, int);
107 static int filt_sigattach(struct knote *kn);
108 static void filt_sigdetach(struct knote *kn);
109 static int filt_signal(struct knote *kn, long hint);
110 static struct thread *sigtd(struct proc *p, int sig, int prop);
111 static void sigqueue_start(void);
113 static uma_zone_t ksiginfo_zone = NULL;
114 struct filterops sig_filtops =
115 { 0, filt_sigattach, filt_sigdetach, filt_signal };
117 int kern_logsigexit = 1;
118 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
120 "Log processes quitting on abnormal signals to syslog(3)");
122 static int kern_forcesigexit = 1;
123 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
124 &kern_forcesigexit, 0, "Force trap signal to be handled");
126 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
128 static int max_pending_per_proc = 128;
129 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
130 &max_pending_per_proc, 0, "Max pending signals per proc");
132 static int preallocate_siginfo = 1024;
133 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
134 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
135 &preallocate_siginfo, 0, "Preallocated signal memory size");
137 static int signal_overflow = 0;
138 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
139 &signal_overflow, 0, "Number of signals overflew");
141 static int signal_alloc_fail = 0;
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
143 &signal_alloc_fail, 0, "signals failed to be allocated");
145 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
148 * Policy -- Can ucred cr1 send SIGIO to process cr2?
149 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
150 * in the right situations.
152 #define CANSIGIO(cr1, cr2) \
153 ((cr1)->cr_uid == 0 || \
154 (cr1)->cr_ruid == (cr2)->cr_ruid || \
155 (cr1)->cr_uid == (cr2)->cr_ruid || \
156 (cr1)->cr_ruid == (cr2)->cr_uid || \
157 (cr1)->cr_uid == (cr2)->cr_uid)
160 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
161 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
163 static int do_coredump = 1;
164 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
165 &do_coredump, 0, "Enable/Disable coredumps");
167 static int set_core_nodump_flag = 0;
168 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
169 0, "Enable setting the NODUMP flag on coredump files");
172 * Signal properties and actions.
173 * The array below categorizes the signals and their default actions
174 * according to the following properties:
176 #define SA_KILL 0x01 /* terminates process by default */
177 #define SA_CORE 0x02 /* ditto and coredumps */
178 #define SA_STOP 0x04 /* suspend process */
179 #define SA_TTYSTOP 0x08 /* ditto, from tty */
180 #define SA_IGNORE 0x10 /* ignore by default */
181 #define SA_CONT 0x20 /* continue if suspended */
182 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
183 #define SA_PROC 0x80 /* deliverable to any thread */
185 static int sigproptbl[NSIG] = {
186 SA_KILL|SA_PROC, /* SIGHUP */
187 SA_KILL|SA_PROC, /* SIGINT */
188 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
189 SA_KILL|SA_CORE, /* SIGILL */
190 SA_KILL|SA_CORE, /* SIGTRAP */
191 SA_KILL|SA_CORE, /* SIGABRT */
192 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
193 SA_KILL|SA_CORE, /* SIGFPE */
194 SA_KILL|SA_PROC, /* SIGKILL */
195 SA_KILL|SA_CORE, /* SIGBUS */
196 SA_KILL|SA_CORE, /* SIGSEGV */
197 SA_KILL|SA_CORE, /* SIGSYS */
198 SA_KILL|SA_PROC, /* SIGPIPE */
199 SA_KILL|SA_PROC, /* SIGALRM */
200 SA_KILL|SA_PROC, /* SIGTERM */
201 SA_IGNORE|SA_PROC, /* SIGURG */
202 SA_STOP|SA_PROC, /* SIGSTOP */
203 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
204 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
205 SA_IGNORE|SA_PROC, /* SIGCHLD */
206 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
207 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
208 SA_IGNORE|SA_PROC, /* SIGIO */
209 SA_KILL, /* SIGXCPU */
210 SA_KILL, /* SIGXFSZ */
211 SA_KILL|SA_PROC, /* SIGVTALRM */
212 SA_KILL|SA_PROC, /* SIGPROF */
213 SA_IGNORE|SA_PROC, /* SIGWINCH */
214 SA_IGNORE|SA_PROC, /* SIGINFO */
215 SA_KILL|SA_PROC, /* SIGUSR1 */
216 SA_KILL|SA_PROC, /* SIGUSR2 */
219 static void reschedule_signals(struct proc *p, sigset_t block);
224 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
225 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
226 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
227 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
228 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
229 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
233 ksiginfo_alloc(int wait)
240 if (ksiginfo_zone != NULL)
241 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
246 ksiginfo_free(ksiginfo_t *ksi)
248 uma_zfree(ksiginfo_zone, ksi);
252 ksiginfo_tryfree(ksiginfo_t *ksi)
254 if (!(ksi->ksi_flags & KSI_EXT)) {
255 uma_zfree(ksiginfo_zone, ksi);
262 sigqueue_init(sigqueue_t *list, struct proc *p)
264 SIGEMPTYSET(list->sq_signals);
265 SIGEMPTYSET(list->sq_kill);
266 TAILQ_INIT(&list->sq_list);
268 list->sq_flags = SQ_INIT;
272 * Get a signal's ksiginfo.
274 * 0 - signal not found
275 * others - signal number
278 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
280 struct proc *p = sq->sq_proc;
281 struct ksiginfo *ksi, *next;
284 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
286 if (!SIGISMEMBER(sq->sq_signals, signo))
289 if (SIGISMEMBER(sq->sq_kill, signo)) {
291 SIGDELSET(sq->sq_kill, signo);
294 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
295 if (ksi->ksi_signo == signo) {
297 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
298 ksi->ksi_sigq = NULL;
299 ksiginfo_copy(ksi, si);
300 if (ksiginfo_tryfree(ksi) && p != NULL)
309 SIGDELSET(sq->sq_signals, signo);
310 si->ksi_signo = signo;
315 sigqueue_take(ksiginfo_t *ksi)
321 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
325 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
326 ksi->ksi_sigq = NULL;
327 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
330 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
331 kp = TAILQ_NEXT(kp, ksi_link)) {
332 if (kp->ksi_signo == ksi->ksi_signo)
335 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
336 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
340 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
342 struct proc *p = sq->sq_proc;
343 struct ksiginfo *ksi;
346 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
348 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
349 SIGADDSET(sq->sq_kill, signo);
353 /* directly insert the ksi, don't copy it */
354 if (si->ksi_flags & KSI_INS) {
355 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
360 if (__predict_false(ksiginfo_zone == NULL)) {
361 SIGADDSET(sq->sq_kill, signo);
365 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
368 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
374 ksiginfo_copy(si, ksi);
375 ksi->ksi_signo = signo;
376 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
380 if ((si->ksi_flags & KSI_TRAP) != 0) {
382 SIGADDSET(sq->sq_kill, signo);
391 SIGADDSET(sq->sq_signals, signo);
396 sigqueue_flush(sigqueue_t *sq)
398 struct proc *p = sq->sq_proc;
401 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
404 PROC_LOCK_ASSERT(p, MA_OWNED);
406 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
407 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
408 ksi->ksi_sigq = NULL;
409 if (ksiginfo_tryfree(ksi) && p != NULL)
413 SIGEMPTYSET(sq->sq_signals);
414 SIGEMPTYSET(sq->sq_kill);
418 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
422 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
424 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
425 SIGADDSET(*set, ksi->ksi_signo);
426 SIGSETOR(*set, sq->sq_kill);
430 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
433 struct proc *p1, *p2;
434 ksiginfo_t *ksi, *next;
436 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
437 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
439 * make a copy, this allows setp to point to src or dst
440 * sq_signals without trouble.
445 /* Move siginfo to target list */
446 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
447 if (SIGISMEMBER(set, ksi->ksi_signo)) {
448 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
451 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
458 /* Move pending bits to target list */
461 SIGSETOR(dst->sq_kill, tmp);
462 SIGSETNAND(src->sq_kill, tmp);
464 tmp = src->sq_signals;
466 SIGSETOR(dst->sq_signals, tmp);
467 SIGSETNAND(src->sq_signals, tmp);
469 /* Finally, rescan src queue and set pending bits for it */
470 sigqueue_collect_set(src, &src->sq_signals);
474 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
479 SIGADDSET(set, signo);
480 sigqueue_move_set(src, dst, &set);
484 sigqueue_delete_set(sigqueue_t *sq, 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);
502 /* Finally, rescan queue and set pending bits for it */
503 sigqueue_collect_set(sq, &sq->sq_signals);
507 sigqueue_delete(sigqueue_t *sq, int signo)
512 SIGADDSET(set, signo);
513 sigqueue_delete_set(sq, &set);
516 /* Remove a set of signals for a process */
518 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
523 PROC_LOCK_ASSERT(p, MA_OWNED);
525 sigqueue_init(&worklist, NULL);
526 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
528 FOREACH_THREAD_IN_PROC(p, td0)
529 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
531 sigqueue_flush(&worklist);
535 sigqueue_delete_proc(struct proc *p, int signo)
540 SIGADDSET(set, signo);
541 sigqueue_delete_set_proc(p, &set);
545 sigqueue_delete_stopmask_proc(struct proc *p)
550 SIGADDSET(set, SIGSTOP);
551 SIGADDSET(set, SIGTSTP);
552 SIGADDSET(set, SIGTTIN);
553 SIGADDSET(set, SIGTTOU);
554 sigqueue_delete_set_proc(p, &set);
558 * Determine signal that should be delivered to process p, the current
559 * process, 0 if none. If there is a pending stop signal with default
560 * action, the process stops in issignal().
563 cursig(struct thread *td, int stop_allowed)
565 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
566 KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
567 stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
568 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
569 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
570 return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
574 * Arrange for ast() to handle unmasked pending signals on return to user
575 * mode. This must be called whenever a signal is added to td_sigqueue or
576 * unmasked in td_sigmask.
579 signotify(struct thread *td)
585 PROC_LOCK_ASSERT(p, MA_OWNED);
587 if (SIGPENDING(td)) {
589 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
595 sigonstack(size_t sp)
597 struct thread *td = curthread;
599 return ((td->td_pflags & TDP_ALTSTACK) ?
600 #if defined(COMPAT_43)
601 ((td->td_sigstk.ss_size == 0) ?
602 (td->td_sigstk.ss_flags & SS_ONSTACK) :
603 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
605 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
614 if (sig > 0 && sig < NSIG)
615 return (sigproptbl[_SIG_IDX(sig)]);
620 sig_ffs(sigset_t *set)
624 for (i = 0; i < _SIG_WORDS; i++)
626 return (ffs(set->__bits[i]) + (i * 32));
637 kern_sigaction(td, sig, act, oact, flags)
640 struct sigaction *act, *oact;
644 struct proc *p = td->td_proc;
646 if (!_SIG_VALID(sig))
651 mtx_lock(&ps->ps_mtx);
653 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
655 if (SIGISMEMBER(ps->ps_sigonstack, sig))
656 oact->sa_flags |= SA_ONSTACK;
657 if (!SIGISMEMBER(ps->ps_sigintr, sig))
658 oact->sa_flags |= SA_RESTART;
659 if (SIGISMEMBER(ps->ps_sigreset, sig))
660 oact->sa_flags |= SA_RESETHAND;
661 if (SIGISMEMBER(ps->ps_signodefer, sig))
662 oact->sa_flags |= SA_NODEFER;
663 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
664 oact->sa_flags |= SA_SIGINFO;
666 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
668 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
669 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
670 oact->sa_flags |= SA_NOCLDSTOP;
671 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
672 oact->sa_flags |= SA_NOCLDWAIT;
675 if ((sig == SIGKILL || sig == SIGSTOP) &&
676 act->sa_handler != SIG_DFL) {
677 mtx_unlock(&ps->ps_mtx);
683 * Change setting atomically.
686 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
687 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
688 if (act->sa_flags & SA_SIGINFO) {
689 ps->ps_sigact[_SIG_IDX(sig)] =
690 (__sighandler_t *)act->sa_sigaction;
691 SIGADDSET(ps->ps_siginfo, sig);
693 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
694 SIGDELSET(ps->ps_siginfo, sig);
696 if (!(act->sa_flags & SA_RESTART))
697 SIGADDSET(ps->ps_sigintr, sig);
699 SIGDELSET(ps->ps_sigintr, sig);
700 if (act->sa_flags & SA_ONSTACK)
701 SIGADDSET(ps->ps_sigonstack, sig);
703 SIGDELSET(ps->ps_sigonstack, sig);
704 if (act->sa_flags & SA_RESETHAND)
705 SIGADDSET(ps->ps_sigreset, sig);
707 SIGDELSET(ps->ps_sigreset, sig);
708 if (act->sa_flags & SA_NODEFER)
709 SIGADDSET(ps->ps_signodefer, sig);
711 SIGDELSET(ps->ps_signodefer, sig);
712 if (sig == SIGCHLD) {
713 if (act->sa_flags & SA_NOCLDSTOP)
714 ps->ps_flag |= PS_NOCLDSTOP;
716 ps->ps_flag &= ~PS_NOCLDSTOP;
717 if (act->sa_flags & SA_NOCLDWAIT) {
719 * Paranoia: since SA_NOCLDWAIT is implemented
720 * by reparenting the dying child to PID 1 (and
721 * trust it to reap the zombie), PID 1 itself
722 * is forbidden to set SA_NOCLDWAIT.
725 ps->ps_flag &= ~PS_NOCLDWAIT;
727 ps->ps_flag |= PS_NOCLDWAIT;
729 ps->ps_flag &= ~PS_NOCLDWAIT;
730 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
731 ps->ps_flag |= PS_CLDSIGIGN;
733 ps->ps_flag &= ~PS_CLDSIGIGN;
736 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
737 * and for signals set to SIG_DFL where the default is to
738 * ignore. However, don't put SIGCONT in ps_sigignore, as we
739 * have to restart the process.
741 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
742 (sigprop(sig) & SA_IGNORE &&
743 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
744 /* never to be seen again */
745 sigqueue_delete_proc(p, sig);
747 /* easier in psignal */
748 SIGADDSET(ps->ps_sigignore, sig);
749 SIGDELSET(ps->ps_sigcatch, sig);
751 SIGDELSET(ps->ps_sigignore, sig);
752 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
753 SIGDELSET(ps->ps_sigcatch, sig);
755 SIGADDSET(ps->ps_sigcatch, sig);
757 #ifdef COMPAT_FREEBSD4
758 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
759 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
760 (flags & KSA_FREEBSD4) == 0)
761 SIGDELSET(ps->ps_freebsd4, sig);
763 SIGADDSET(ps->ps_freebsd4, sig);
766 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
767 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
768 (flags & KSA_OSIGSET) == 0)
769 SIGDELSET(ps->ps_osigset, sig);
771 SIGADDSET(ps->ps_osigset, sig);
774 mtx_unlock(&ps->ps_mtx);
779 #ifndef _SYS_SYSPROTO_H_
780 struct sigaction_args {
782 struct sigaction *act;
783 struct sigaction *oact;
789 register struct sigaction_args *uap;
791 struct sigaction act, oact;
792 register struct sigaction *actp, *oactp;
795 actp = (uap->act != NULL) ? &act : NULL;
796 oactp = (uap->oact != NULL) ? &oact : NULL;
798 error = copyin(uap->act, actp, sizeof(act));
802 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
804 error = copyout(oactp, uap->oact, sizeof(oact));
808 #ifdef COMPAT_FREEBSD4
809 #ifndef _SYS_SYSPROTO_H_
810 struct freebsd4_sigaction_args {
812 struct sigaction *act;
813 struct sigaction *oact;
817 freebsd4_sigaction(td, uap)
819 register struct freebsd4_sigaction_args *uap;
821 struct sigaction act, oact;
822 register struct sigaction *actp, *oactp;
826 actp = (uap->act != NULL) ? &act : NULL;
827 oactp = (uap->oact != NULL) ? &oact : NULL;
829 error = copyin(uap->act, actp, sizeof(act));
833 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
835 error = copyout(oactp, uap->oact, sizeof(oact));
838 #endif /* COMAPT_FREEBSD4 */
840 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
841 #ifndef _SYS_SYSPROTO_H_
842 struct osigaction_args {
844 struct osigaction *nsa;
845 struct osigaction *osa;
851 register struct osigaction_args *uap;
853 struct osigaction sa;
854 struct sigaction nsa, osa;
855 register struct sigaction *nsap, *osap;
858 if (uap->signum <= 0 || uap->signum >= ONSIG)
861 nsap = (uap->nsa != NULL) ? &nsa : NULL;
862 osap = (uap->osa != NULL) ? &osa : NULL;
865 error = copyin(uap->nsa, &sa, sizeof(sa));
868 nsap->sa_handler = sa.sa_handler;
869 nsap->sa_flags = sa.sa_flags;
870 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
872 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
873 if (osap && !error) {
874 sa.sa_handler = osap->sa_handler;
875 sa.sa_flags = osap->sa_flags;
876 SIG2OSIG(osap->sa_mask, sa.sa_mask);
877 error = copyout(&sa, uap->osa, sizeof(sa));
882 #if !defined(__i386__)
883 /* Avoid replicating the same stub everywhere */
887 struct osigreturn_args *uap;
890 return (nosys(td, (struct nosys_args *)uap));
893 #endif /* COMPAT_43 */
896 * Initialize signal state for process 0;
897 * set to ignore signals that are ignored by default.
908 mtx_lock(&ps->ps_mtx);
909 for (i = 1; i <= NSIG; i++)
910 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
911 SIGADDSET(ps->ps_sigignore, i);
912 mtx_unlock(&ps->ps_mtx);
917 * Reset signals for an exec of the specified process.
920 execsigs(struct proc *p)
927 * Reset caught signals. Held signals remain held
928 * through td_sigmask (unless they were caught,
929 * and are now ignored by default).
931 PROC_LOCK_ASSERT(p, MA_OWNED);
932 td = FIRST_THREAD_IN_PROC(p);
934 mtx_lock(&ps->ps_mtx);
935 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
936 sig = sig_ffs(&ps->ps_sigcatch);
937 SIGDELSET(ps->ps_sigcatch, sig);
938 if (sigprop(sig) & SA_IGNORE) {
940 SIGADDSET(ps->ps_sigignore, sig);
941 sigqueue_delete_proc(p, sig);
943 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
946 * Reset stack state to the user stack.
947 * Clear set of signals caught on the signal stack.
949 td->td_sigstk.ss_flags = SS_DISABLE;
950 td->td_sigstk.ss_size = 0;
951 td->td_sigstk.ss_sp = 0;
952 td->td_pflags &= ~TDP_ALTSTACK;
954 * Reset no zombies if child dies flag as Solaris does.
956 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
957 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
958 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
959 mtx_unlock(&ps->ps_mtx);
965 * Manipulate signal mask.
968 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
971 sigset_t new_block, oset1;
978 *oset = td->td_sigmask;
981 SIGEMPTYSET(new_block);
986 oset1 = td->td_sigmask;
987 SIGSETOR(td->td_sigmask, *set);
988 new_block = td->td_sigmask;
989 SIGSETNAND(new_block, oset1);
992 SIGSETNAND(td->td_sigmask, *set);
997 oset1 = td->td_sigmask;
999 SIGSETLO(td->td_sigmask, *set);
1001 td->td_sigmask = *set;
1002 new_block = td->td_sigmask;
1003 SIGSETNAND(new_block, oset1);
1013 * The new_block set contains signals that were not previously
1014 * blocked, but are blocked now.
1016 * In case we block any signal that was not previously blocked
1017 * for td, and process has the signal pending, try to schedule
1018 * signal delivery to some thread that does not block the signal,
1019 * possibly waking it up.
1021 if (p->p_numthreads != 1)
1022 reschedule_signals(p, new_block);
1028 #ifndef _SYS_SYSPROTO_H_
1029 struct sigprocmask_args {
1031 const sigset_t *set;
1036 sigprocmask(td, uap)
1037 register struct thread *td;
1038 struct sigprocmask_args *uap;
1041 sigset_t *setp, *osetp;
1044 setp = (uap->set != NULL) ? &set : NULL;
1045 osetp = (uap->oset != NULL) ? &oset : NULL;
1047 error = copyin(uap->set, setp, sizeof(set));
1051 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1052 if (osetp && !error) {
1053 error = copyout(osetp, uap->oset, sizeof(oset));
1058 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1059 #ifndef _SYS_SYSPROTO_H_
1060 struct osigprocmask_args {
1066 osigprocmask(td, uap)
1067 register struct thread *td;
1068 struct osigprocmask_args *uap;
1073 OSIG2SIG(uap->mask, set);
1074 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1075 SIG2OSIG(oset, td->td_retval[0]);
1078 #endif /* COMPAT_43 */
1081 sigwait(struct thread *td, struct sigwait_args *uap)
1087 error = copyin(uap->set, &set, sizeof(set));
1089 td->td_retval[0] = error;
1093 error = kern_sigtimedwait(td, set, &ksi, NULL);
1095 if (error == 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 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 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)
1170 int error, sig, hz, i, timevalid = 0;
1171 struct timespec rts, ets, ts;
1179 SIG_CANTMASK(waitset);
1183 savedmask = td->td_sigmask;
1185 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1187 getnanouptime(&rts);
1189 timespecadd(&ets, timeout);
1194 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1195 if (!SIGISMEMBER(waitset, i))
1197 if (!SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1198 if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
1199 sigqueue_move(&p->p_sigqueue,
1200 &td->td_sigqueue, i);
1205 SIGFILLSET(td->td_sigmask);
1206 SIG_CANTMASK(td->td_sigmask);
1207 SIGDELSET(td->td_sigmask, i);
1208 mtx_lock(&ps->ps_mtx);
1209 sig = cursig(td, SIG_STOP_ALLOWED);
1210 mtx_unlock(&ps->ps_mtx);
1215 * Because cursig() may have stopped current thread,
1216 * after it is resumed, things may have already been
1217 * changed, it should rescan any pending signals.
1227 * POSIX says this must be checked after looking for pending
1235 getnanouptime(&rts);
1236 if (timespeccmp(&rts, &ets, >=)) {
1241 timespecsub(&ts, &rts);
1242 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1247 td->td_sigmask = savedmask;
1248 SIGSETNAND(td->td_sigmask, waitset);
1250 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1252 if (error == ERESTART) {
1253 /* timeout can not be restarted. */
1255 } else if (error == EAGAIN) {
1256 /* will calculate timeout by ourself. */
1263 td->td_sigmask = savedmask;
1267 sigqueue_get(&td->td_sigqueue, sig, ksi);
1268 ksi->ksi_signo = sig;
1270 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
1272 if (ksi->ksi_code == SI_TIMER)
1273 itimer_accept(p, ksi->ksi_timerid, ksi);
1277 if (KTRPOINT(td, KTR_PSIG)) {
1280 mtx_lock(&ps->ps_mtx);
1281 action = ps->ps_sigact[_SIG_IDX(sig)];
1282 mtx_unlock(&ps->ps_mtx);
1283 ktrpsig(sig, action, &td->td_sigmask, 0);
1293 #ifndef _SYS_SYSPROTO_H_
1294 struct sigpending_args {
1301 struct sigpending_args *uap;
1303 struct proc *p = td->td_proc;
1307 pending = p->p_sigqueue.sq_signals;
1308 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1310 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1313 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1314 #ifndef _SYS_SYSPROTO_H_
1315 struct osigpending_args {
1320 osigpending(td, uap)
1322 struct osigpending_args *uap;
1324 struct proc *p = td->td_proc;
1328 pending = p->p_sigqueue.sq_signals;
1329 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1331 SIG2OSIG(pending, td->td_retval[0]);
1334 #endif /* COMPAT_43 */
1336 #if defined(COMPAT_43)
1338 * Generalized interface signal handler, 4.3-compatible.
1340 #ifndef _SYS_SYSPROTO_H_
1341 struct osigvec_args {
1351 register struct osigvec_args *uap;
1354 struct sigaction nsa, osa;
1355 register struct sigaction *nsap, *osap;
1358 if (uap->signum <= 0 || uap->signum >= ONSIG)
1360 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1361 osap = (uap->osv != NULL) ? &osa : NULL;
1363 error = copyin(uap->nsv, &vec, sizeof(vec));
1366 nsap->sa_handler = vec.sv_handler;
1367 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1368 nsap->sa_flags = vec.sv_flags;
1369 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1371 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1372 if (osap && !error) {
1373 vec.sv_handler = osap->sa_handler;
1374 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1375 vec.sv_flags = osap->sa_flags;
1376 vec.sv_flags &= ~SA_NOCLDWAIT;
1377 vec.sv_flags ^= SA_RESTART;
1378 error = copyout(&vec, uap->osv, sizeof(vec));
1383 #ifndef _SYS_SYSPROTO_H_
1384 struct osigblock_args {
1390 register struct thread *td;
1391 struct osigblock_args *uap;
1393 struct proc *p = td->td_proc;
1396 OSIG2SIG(uap->mask, set);
1399 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1400 SIGSETOR(td->td_sigmask, set);
1405 #ifndef _SYS_SYSPROTO_H_
1406 struct osigsetmask_args {
1411 osigsetmask(td, uap)
1413 struct osigsetmask_args *uap;
1415 struct proc *p = td->td_proc;
1418 OSIG2SIG(uap->mask, set);
1421 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1422 SIGSETLO(td->td_sigmask, set);
1427 #endif /* COMPAT_43 */
1430 * Suspend calling thread until signal, providing mask to be set in the
1433 #ifndef _SYS_SYSPROTO_H_
1434 struct sigsuspend_args {
1435 const sigset_t *sigmask;
1442 struct sigsuspend_args *uap;
1447 error = copyin(uap->sigmask, &mask, sizeof(mask));
1450 return (kern_sigsuspend(td, mask));
1454 kern_sigsuspend(struct thread *td, sigset_t mask)
1456 struct proc *p = td->td_proc;
1459 * When returning from sigsuspend, we want
1460 * the old mask to be restored after the
1461 * signal handler has finished. Thus, we
1462 * save it here and mark the sigacts structure
1466 td->td_oldsigmask = td->td_sigmask;
1467 td->td_pflags |= TDP_OLDMASK;
1469 td->td_sigmask = mask;
1471 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1474 /* always return EINTR rather than ERESTART... */
1478 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1480 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1481 * convention: libc stub passes mask, not pointer, to save a copyin.
1483 #ifndef _SYS_SYSPROTO_H_
1484 struct osigsuspend_args {
1490 osigsuspend(td, uap)
1492 struct osigsuspend_args *uap;
1494 struct proc *p = td->td_proc;
1498 td->td_oldsigmask = td->td_sigmask;
1499 td->td_pflags |= TDP_OLDMASK;
1500 OSIG2SIG(uap->mask, mask);
1502 SIGSETLO(td->td_sigmask, mask);
1504 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1507 /* always return EINTR rather than ERESTART... */
1510 #endif /* COMPAT_43 */
1512 #if defined(COMPAT_43)
1513 #ifndef _SYS_SYSPROTO_H_
1514 struct osigstack_args {
1515 struct sigstack *nss;
1516 struct sigstack *oss;
1523 register struct osigstack_args *uap;
1525 struct sigstack nss, oss;
1528 if (uap->nss != NULL) {
1529 error = copyin(uap->nss, &nss, sizeof(nss));
1533 oss.ss_sp = td->td_sigstk.ss_sp;
1534 oss.ss_onstack = sigonstack(cpu_getstack(td));
1535 if (uap->nss != NULL) {
1536 td->td_sigstk.ss_sp = nss.ss_sp;
1537 td->td_sigstk.ss_size = 0;
1538 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1539 td->td_pflags |= TDP_ALTSTACK;
1541 if (uap->oss != NULL)
1542 error = copyout(&oss, uap->oss, sizeof(oss));
1546 #endif /* COMPAT_43 */
1548 #ifndef _SYS_SYSPROTO_H_
1549 struct sigaltstack_args {
1556 sigaltstack(td, uap)
1558 register struct sigaltstack_args *uap;
1563 if (uap->ss != NULL) {
1564 error = copyin(uap->ss, &ss, sizeof(ss));
1568 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1569 (uap->oss != NULL) ? &oss : NULL);
1572 if (uap->oss != NULL)
1573 error = copyout(&oss, uap->oss, sizeof(stack_t));
1578 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1580 struct proc *p = td->td_proc;
1583 oonstack = sigonstack(cpu_getstack(td));
1586 *oss = td->td_sigstk;
1587 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1588 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1594 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1596 if (!(ss->ss_flags & SS_DISABLE)) {
1597 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1600 td->td_sigstk = *ss;
1601 td->td_pflags |= TDP_ALTSTACK;
1603 td->td_pflags &= ~TDP_ALTSTACK;
1610 * Common code for kill process group/broadcast kill.
1611 * cp is calling process.
1614 killpg1(td, sig, pgid, all)
1615 register struct thread *td;
1618 register struct proc *p;
1626 sx_slock(&allproc_lock);
1627 FOREACH_PROC_IN_SYSTEM(p) {
1629 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1630 p == td->td_proc || p->p_state == PRS_NEW) {
1634 if (p_cansignal(td, p, sig) == 0) {
1641 sx_sunlock(&allproc_lock);
1643 sx_slock(&proctree_lock);
1646 * zero pgid means send to my process group.
1648 pgrp = td->td_proc->p_pgrp;
1651 pgrp = pgfind(pgid);
1653 sx_sunlock(&proctree_lock);
1657 sx_sunlock(&proctree_lock);
1658 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1660 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1661 p->p_state == PRS_NEW ) {
1665 if (p_cansignal(td, p, sig) == 0) {
1674 return (nfound ? 0 : ESRCH);
1677 #ifndef _SYS_SYSPROTO_H_
1686 register struct thread *td;
1687 register struct kill_args *uap;
1689 register struct proc *p;
1692 AUDIT_ARG_SIGNUM(uap->signum);
1693 AUDIT_ARG_PID(uap->pid);
1694 if ((u_int)uap->signum > _SIG_MAXSIG)
1698 /* kill single process */
1699 if ((p = pfind(uap->pid)) == NULL) {
1700 if ((p = zpfind(uap->pid)) == NULL)
1703 AUDIT_ARG_PROCESS(p);
1704 error = p_cansignal(td, p, uap->signum);
1705 if (error == 0 && uap->signum)
1706 psignal(p, uap->signum);
1711 case -1: /* broadcast signal */
1712 return (killpg1(td, uap->signum, 0, 1));
1713 case 0: /* signal own process group */
1714 return (killpg1(td, uap->signum, 0, 0));
1715 default: /* negative explicit process group */
1716 return (killpg1(td, uap->signum, -uap->pid, 0));
1721 #if defined(COMPAT_43)
1722 #ifndef _SYS_SYSPROTO_H_
1723 struct okillpg_args {
1732 register struct okillpg_args *uap;
1735 AUDIT_ARG_SIGNUM(uap->signum);
1736 AUDIT_ARG_PID(uap->pgid);
1737 if ((u_int)uap->signum > _SIG_MAXSIG)
1740 return (killpg1(td, uap->signum, uap->pgid, 0));
1742 #endif /* COMPAT_43 */
1744 #ifndef _SYS_SYSPROTO_H_
1745 struct sigqueue_args {
1748 /* union sigval */ void *value;
1752 sigqueue(struct thread *td, struct sigqueue_args *uap)
1758 if ((u_int)uap->signum > _SIG_MAXSIG)
1762 * Specification says sigqueue can only send signal to
1768 if ((p = pfind(uap->pid)) == NULL) {
1769 if ((p = zpfind(uap->pid)) == NULL)
1772 error = p_cansignal(td, p, uap->signum);
1773 if (error == 0 && uap->signum != 0) {
1774 ksiginfo_init(&ksi);
1775 ksi.ksi_signo = uap->signum;
1776 ksi.ksi_code = SI_QUEUE;
1777 ksi.ksi_pid = td->td_proc->p_pid;
1778 ksi.ksi_uid = td->td_ucred->cr_ruid;
1779 ksi.ksi_value.sival_ptr = uap->value;
1780 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1787 * Send a signal to a process group.
1796 sx_slock(&proctree_lock);
1797 pgrp = pgfind(pgid);
1798 sx_sunlock(&proctree_lock);
1800 pgsignal(pgrp, sig, 0);
1807 * Send a signal to a process group. If checktty is 1,
1808 * limit to members which have a controlling terminal.
1811 pgsignal(pgrp, sig, checkctty)
1815 register struct proc *p;
1818 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1819 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1821 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1829 * Send a signal caused by a trap to the current thread. If it will be
1830 * caught immediately, deliver it with correct code. Otherwise, post it
1834 trapsignal(struct thread *td, ksiginfo_t *ksi)
1842 sig = ksi->ksi_signo;
1843 code = ksi->ksi_code;
1844 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1848 mtx_lock(&ps->ps_mtx);
1849 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1850 !SIGISMEMBER(td->td_sigmask, sig)) {
1851 td->td_ru.ru_nsignals++;
1853 if (KTRPOINT(curthread, KTR_PSIG))
1854 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1855 &td->td_sigmask, code);
1857 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1858 ksi, &td->td_sigmask);
1859 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1860 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1861 SIGADDSET(td->td_sigmask, sig);
1862 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1864 * See kern_sigaction() for origin of this code.
1866 SIGDELSET(ps->ps_sigcatch, sig);
1867 if (sig != SIGCONT &&
1868 sigprop(sig) & SA_IGNORE)
1869 SIGADDSET(ps->ps_sigignore, sig);
1870 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1872 mtx_unlock(&ps->ps_mtx);
1875 * Avoid a possible infinite loop if the thread
1876 * masking the signal or process is ignoring the
1879 if (kern_forcesigexit &&
1880 (SIGISMEMBER(td->td_sigmask, sig) ||
1881 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1882 SIGDELSET(td->td_sigmask, sig);
1883 SIGDELSET(ps->ps_sigcatch, sig);
1884 SIGDELSET(ps->ps_sigignore, sig);
1885 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1887 mtx_unlock(&ps->ps_mtx);
1888 p->p_code = code; /* XXX for core dump/debugger */
1889 p->p_sig = sig; /* XXX to verify code */
1890 tdsignal(p, td, sig, ksi);
1895 static struct thread *
1896 sigtd(struct proc *p, int sig, int prop)
1898 struct thread *td, *signal_td;
1900 PROC_LOCK_ASSERT(p, MA_OWNED);
1903 * Check if current thread can handle the signal without
1904 * switching context to another thread.
1906 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1909 FOREACH_THREAD_IN_PROC(p, td) {
1910 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1915 if (signal_td == NULL)
1916 signal_td = FIRST_THREAD_IN_PROC(p);
1921 * Send the signal to the process. If the signal has an action, the action
1922 * is usually performed by the target process rather than the caller; we add
1923 * the signal to the set of pending signals for the process.
1926 * o When a stop signal is sent to a sleeping process that takes the
1927 * default action, the process is stopped without awakening it.
1928 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1929 * regardless of the signal action (eg, blocked or ignored).
1931 * Other ignored signals are discarded immediately.
1933 * NB: This function may be entered from the debugger via the "kill" DDB
1934 * command. There is little that can be done to mitigate the possibly messy
1935 * side effects of this unwise possibility.
1938 psignal(struct proc *p, int sig)
1940 (void) tdsignal(p, NULL, sig, NULL);
1944 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
1946 struct thread *td = NULL;
1948 PROC_LOCK_ASSERT(p, MA_OWNED);
1950 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
1953 * ksi_code and other fields should be set before
1954 * calling this function.
1956 ksi->ksi_signo = sigev->sigev_signo;
1957 ksi->ksi_value = sigev->sigev_value;
1958 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1959 td = thread_find(p, sigev->sigev_notify_thread_id);
1963 return (tdsignal(p, td, ksi->ksi_signo, ksi));
1967 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
1970 sigqueue_t *sigqueue;
1977 PROC_LOCK_ASSERT(p, MA_OWNED);
1979 if (!_SIG_VALID(sig))
1980 panic("tdsignal(): invalid signal %d", sig);
1982 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("tdsignal: ksi on queue"));
1985 * IEEE Std 1003.1-2001: return success when killing a zombie.
1987 if (p->p_state == PRS_ZOMBIE) {
1988 if (ksi && (ksi->ksi_flags & KSI_INS))
1989 ksiginfo_tryfree(ksi);
1994 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1995 prop = sigprop(sig);
1998 td = sigtd(p, sig, prop);
1999 sigqueue = &p->p_sigqueue;
2001 KASSERT(td->td_proc == p, ("invalid thread"));
2002 sigqueue = &td->td_sigqueue;
2005 SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
2008 * If the signal is being ignored,
2009 * then we forget about it immediately.
2010 * (Note: we don't set SIGCONT in ps_sigignore,
2011 * and if it is set to SIG_IGN,
2012 * action will be SIG_DFL here.)
2014 mtx_lock(&ps->ps_mtx);
2015 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2016 SDT_PROBE(proc, kernel, , signal_discard, ps, td, sig, 0, 0 );
2018 mtx_unlock(&ps->ps_mtx);
2019 if (ksi && (ksi->ksi_flags & KSI_INS))
2020 ksiginfo_tryfree(ksi);
2023 if (SIGISMEMBER(td->td_sigmask, sig))
2025 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2029 if (SIGISMEMBER(ps->ps_sigintr, sig))
2033 mtx_unlock(&ps->ps_mtx);
2036 sigqueue_delete_stopmask_proc(p);
2037 else if (prop & SA_STOP) {
2039 * If sending a tty stop signal to a member of an orphaned
2040 * process group, discard the signal here if the action
2041 * is default; don't stop the process below if sleeping,
2042 * and don't clear any pending SIGCONT.
2044 if ((prop & SA_TTYSTOP) &&
2045 (p->p_pgrp->pg_jobc == 0) &&
2046 (action == SIG_DFL)) {
2047 if (ksi && (ksi->ksi_flags & KSI_INS))
2048 ksiginfo_tryfree(ksi);
2051 sigqueue_delete_proc(p, SIGCONT);
2052 if (p->p_flag & P_CONTINUED) {
2053 p->p_flag &= ~P_CONTINUED;
2054 PROC_LOCK(p->p_pptr);
2055 sigqueue_take(p->p_ksi);
2056 PROC_UNLOCK(p->p_pptr);
2060 ret = sigqueue_add(sigqueue, sig, ksi);
2065 * Defer further processing for signals which are held,
2066 * except that stopped processes must be continued by SIGCONT.
2068 if (action == SIG_HOLD &&
2069 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2072 * SIGKILL: Remove procfs STOPEVENTs.
2074 if (sig == SIGKILL) {
2075 /* from procfs_ioctl.c: PIOCBIC */
2077 /* from procfs_ioctl.c: PIOCCONT */
2082 * Some signals have a process-wide effect and a per-thread
2083 * component. Most processing occurs when the process next
2084 * tries to cross the user boundary, however there are some
2085 * times when processing needs to be done immediatly, such as
2086 * waking up threads so that they can cross the user boundary.
2087 * We try do the per-process part here.
2089 if (P_SHOULDSTOP(p)) {
2091 * The process is in stopped mode. All the threads should be
2092 * either winding down or already on the suspended queue.
2094 if (p->p_flag & P_TRACED) {
2096 * The traced process is already stopped,
2097 * so no further action is necessary.
2098 * No signal can restart us.
2103 if (sig == SIGKILL) {
2105 * SIGKILL sets process running.
2106 * It will die elsewhere.
2107 * All threads must be restarted.
2109 p->p_flag &= ~P_STOPPED_SIG;
2113 if (prop & SA_CONT) {
2115 * If SIGCONT is default (or ignored), we continue the
2116 * process but don't leave the signal in sigqueue as
2117 * it has no further action. If SIGCONT is held, we
2118 * continue the process and leave the signal in
2119 * sigqueue. If the process catches SIGCONT, let it
2120 * handle the signal itself. If it isn't waiting on
2121 * an event, it goes back to run state.
2122 * Otherwise, process goes back to sleep state.
2124 p->p_flag &= ~P_STOPPED_SIG;
2126 if (p->p_numthreads == p->p_suspcount) {
2128 p->p_flag |= P_CONTINUED;
2129 p->p_xstat = SIGCONT;
2130 PROC_LOCK(p->p_pptr);
2131 childproc_continued(p);
2132 PROC_UNLOCK(p->p_pptr);
2135 if (action == SIG_DFL) {
2136 thread_unsuspend(p);
2138 sigqueue_delete(sigqueue, sig);
2141 if (action == SIG_CATCH) {
2143 * The process wants to catch it so it needs
2144 * to run at least one thread, but which one?
2150 * The signal is not ignored or caught.
2152 thread_unsuspend(p);
2157 if (prop & SA_STOP) {
2159 * Already stopped, don't need to stop again
2160 * (If we did the shell could get confused).
2161 * Just make sure the signal STOP bit set.
2163 p->p_flag |= P_STOPPED_SIG;
2164 sigqueue_delete(sigqueue, sig);
2169 * All other kinds of signals:
2170 * If a thread is sleeping interruptibly, simulate a
2171 * wakeup so that when it is continued it will be made
2172 * runnable and can look at the signal. However, don't make
2173 * the PROCESS runnable, leave it stopped.
2174 * It may run a bit until it hits a thread_suspend_check().
2179 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2180 wakeup_swapper = sleepq_abort(td, intrval);
2187 * Mutexes are short lived. Threads waiting on them will
2188 * hit thread_suspend_check() soon.
2190 } else if (p->p_state == PRS_NORMAL) {
2191 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2192 tdsigwakeup(td, sig, action, intrval);
2196 MPASS(action == SIG_DFL);
2198 if (prop & SA_STOP) {
2199 if (p->p_flag & P_PPWAIT)
2201 p->p_flag |= P_STOPPED_SIG;
2204 sig_suspend_threads(td, p, 1);
2205 if (p->p_numthreads == p->p_suspcount) {
2207 * only thread sending signal to another
2208 * process can reach here, if thread is sending
2209 * signal to its process, because thread does
2210 * not suspend itself here, p_numthreads
2211 * should never be equal to p_suspcount.
2215 sigqueue_delete_proc(p, p->p_xstat);
2221 /* Not in "NORMAL" state. discard the signal. */
2222 sigqueue_delete(sigqueue, sig);
2227 * The process is not stopped so we need to apply the signal to all the
2231 tdsigwakeup(td, sig, action, intrval);
2233 thread_unsuspend(p);
2236 /* If we jump here, proc slock should not be owned. */
2237 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2242 * The force of a signal has been directed against a single
2243 * thread. We need to see what we can do about knocking it
2244 * out of any sleep it may be in etc.
2247 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2249 struct proc *p = td->td_proc;
2254 PROC_LOCK_ASSERT(p, MA_OWNED);
2255 prop = sigprop(sig);
2260 * Bring the priority of a thread up if we want it to get
2261 * killed in this lifetime.
2263 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2264 sched_prio(td, PUSER);
2265 if (TD_ON_SLEEPQ(td)) {
2267 * If thread is sleeping uninterruptibly
2268 * we can't interrupt the sleep... the signal will
2269 * be noticed when the process returns through
2270 * trap() or syscall().
2272 if ((td->td_flags & TDF_SINTR) == 0)
2275 * If SIGCONT is default (or ignored) and process is
2276 * asleep, we are finished; the process should not
2279 if ((prop & SA_CONT) && action == SIG_DFL) {
2282 sigqueue_delete(&p->p_sigqueue, sig);
2284 * It may be on either list in this state.
2285 * Remove from both for now.
2287 sigqueue_delete(&td->td_sigqueue, sig);
2292 * Give low priority threads a better chance to run.
2294 if (td->td_priority > PUSER)
2295 sched_prio(td, PUSER);
2297 wakeup_swapper = sleepq_abort(td, intrval);
2300 * Other states do nothing with the signal immediately,
2301 * other than kicking ourselves if we are running.
2302 * It will either never be noticed, or noticed very soon.
2305 if (TD_IS_RUNNING(td) && td != curthread)
2317 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2322 PROC_LOCK_ASSERT(p, MA_OWNED);
2323 PROC_SLOCK_ASSERT(p, MA_OWNED);
2326 FOREACH_THREAD_IN_PROC(p, td2) {
2328 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2329 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2330 (td2->td_flags & TDF_SINTR)) {
2331 if (td2->td_flags & TDF_SBDRY) {
2332 if (TD_IS_SUSPENDED(td2))
2334 thread_unsuspend_one(td2);
2335 if (TD_ON_SLEEPQ(td2))
2337 sleepq_abort(td2, ERESTART);
2338 } else if (!TD_IS_SUSPENDED(td2)) {
2339 thread_suspend_one(td2);
2341 } else if (!TD_IS_SUSPENDED(td2)) {
2342 if (sending || td != td2)
2343 td2->td_flags |= TDF_ASTPENDING;
2345 if (TD_IS_RUNNING(td2) && td2 != td)
2346 forward_signal(td2);
2356 ptracestop(struct thread *td, int sig)
2358 struct proc *p = td->td_proc;
2360 PROC_LOCK_ASSERT(p, MA_OWNED);
2361 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2362 &p->p_mtx.lock_object, "Stopping for traced signal");
2364 td->td_dbgflags |= TDB_XSIG;
2367 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2368 if (p->p_flag & P_SINGLE_EXIT) {
2369 td->td_dbgflags &= ~TDB_XSIG;
2374 * Just make wait() to work, the last stopped thread
2379 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2380 sig_suspend_threads(td, p, 0);
2382 thread_suspend_switch(td);
2383 if (!(p->p_flag & P_TRACED)) {
2386 if (td->td_dbgflags & TDB_SUSPEND) {
2387 if (p->p_flag & P_SINGLE_EXIT)
2393 return (td->td_xsig);
2397 reschedule_signals(struct proc *p, sigset_t block)
2403 PROC_LOCK_ASSERT(p, MA_OWNED);
2406 for (i = 1; !SIGISEMPTY(block); i++) {
2407 if (!SIGISMEMBER(block, i))
2409 SIGDELSET(block, i);
2410 if (!SIGISMEMBER(p->p_siglist, i))
2413 td = sigtd(p, i, 0);
2415 mtx_lock(&ps->ps_mtx);
2416 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, i))
2417 tdsigwakeup(td, i, SIG_CATCH,
2418 (SIGISMEMBER(ps->ps_sigintr, i) ? EINTR :
2420 mtx_unlock(&ps->ps_mtx);
2425 tdsigcleanup(struct thread *td)
2431 PROC_LOCK_ASSERT(p, MA_OWNED);
2433 sigqueue_flush(&td->td_sigqueue);
2434 if (p->p_numthreads == 1)
2438 * Since we cannot handle signals, notify signal post code
2439 * about this by filling the sigmask.
2441 * Also, if needed, wake up thread(s) that do not block the
2442 * same signals as the exiting thread, since the thread might
2443 * have been selected for delivery and woken up.
2445 SIGFILLSET(unblocked);
2446 SIGSETNAND(unblocked, td->td_sigmask);
2447 SIGFILLSET(td->td_sigmask);
2448 reschedule_signals(p, unblocked);
2453 * If the current process has received a signal (should be caught or cause
2454 * termination, should interrupt current syscall), return the signal number.
2455 * Stop signals with default action are processed immediately, then cleared;
2456 * they aren't returned. This is checked after each entry to the system for
2457 * a syscall or trap (though this can usually be done without calling issignal
2458 * by checking the pending signal masks in cursig.) The normal call
2461 * while (sig = cursig(curthread))
2465 issignal(struct thread *td, int stop_allowed)
2469 struct sigqueue *queue;
2470 sigset_t sigpending;
2471 int sig, prop, newsig;
2475 mtx_assert(&ps->ps_mtx, MA_OWNED);
2476 PROC_LOCK_ASSERT(p, MA_OWNED);
2478 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2480 sigpending = td->td_sigqueue.sq_signals;
2481 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2482 SIGSETNAND(sigpending, td->td_sigmask);
2484 if (p->p_flag & P_PPWAIT)
2485 SIG_STOPSIGMASK(sigpending);
2486 if (SIGISEMPTY(sigpending)) /* no signal to send */
2488 sig = sig_ffs(&sigpending);
2490 if (p->p_stops & S_SIG) {
2491 mtx_unlock(&ps->ps_mtx);
2492 stopevent(p, S_SIG, sig);
2493 mtx_lock(&ps->ps_mtx);
2497 * We should see pending but ignored signals
2498 * only if P_TRACED was on when they were posted.
2500 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2501 sigqueue_delete(&td->td_sigqueue, sig);
2502 sigqueue_delete(&p->p_sigqueue, sig);
2505 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2507 * If traced, always stop.
2509 mtx_unlock(&ps->ps_mtx);
2510 newsig = ptracestop(td, sig);
2511 mtx_lock(&ps->ps_mtx);
2513 if (sig != newsig) {
2516 queue = &td->td_sigqueue;
2519 * XXX shrug off debugger, it causes siginfo to
2522 if (sigqueue_get(queue, sig, &ksi) == 0) {
2523 queue = &p->p_sigqueue;
2524 sigqueue_get(queue, sig, &ksi);
2528 * If parent wants us to take the signal,
2529 * then it will leave it in p->p_xstat;
2530 * otherwise we just look for signals again.
2537 * Put the new signal into td_sigqueue. If the
2538 * signal is being masked, look for other signals.
2540 SIGADDSET(queue->sq_signals, sig);
2541 if (SIGISMEMBER(td->td_sigmask, sig))
2547 * If the traced bit got turned off, go back up
2548 * to the top to rescan signals. This ensures
2549 * that p_sig* and p_sigact are consistent.
2551 if ((p->p_flag & P_TRACED) == 0)
2555 prop = sigprop(sig);
2558 * Decide whether the signal should be returned.
2559 * Return the signal's number, or fall through
2560 * to clear it from the pending mask.
2562 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2564 case (intptr_t)SIG_DFL:
2566 * Don't take default actions on system processes.
2568 if (p->p_pid <= 1) {
2571 * Are you sure you want to ignore SIGSEGV
2574 printf("Process (pid %lu) got signal %d\n",
2575 (u_long)p->p_pid, sig);
2577 break; /* == ignore */
2580 * If there is a pending stop signal to process
2581 * with default action, stop here,
2582 * then clear the signal. However,
2583 * if process is member of an orphaned
2584 * process group, ignore tty stop signals.
2586 if (prop & SA_STOP) {
2587 if (p->p_flag & P_TRACED ||
2588 (p->p_pgrp->pg_jobc == 0 &&
2590 break; /* == ignore */
2592 /* Ignore, but do not drop the stop signal. */
2593 if (stop_allowed != SIG_STOP_ALLOWED)
2595 mtx_unlock(&ps->ps_mtx);
2596 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2597 &p->p_mtx.lock_object, "Catching SIGSTOP");
2598 p->p_flag |= P_STOPPED_SIG;
2601 sig_suspend_threads(td, p, 0);
2602 thread_suspend_switch(td);
2604 mtx_lock(&ps->ps_mtx);
2606 } else if (prop & SA_IGNORE) {
2608 * Except for SIGCONT, shouldn't get here.
2609 * Default action is to ignore; drop it.
2611 break; /* == ignore */
2616 case (intptr_t)SIG_IGN:
2618 * Masking above should prevent us ever trying
2619 * to take action on an ignored signal other
2620 * than SIGCONT, unless process is traced.
2622 if ((prop & SA_CONT) == 0 &&
2623 (p->p_flag & P_TRACED) == 0)
2624 printf("issignal\n");
2625 break; /* == ignore */
2629 * This signal has an action, let
2630 * postsig() process it.
2634 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2635 sigqueue_delete(&p->p_sigqueue, sig);
2641 thread_stopped(struct proc *p)
2645 PROC_LOCK_ASSERT(p, MA_OWNED);
2646 PROC_SLOCK_ASSERT(p, MA_OWNED);
2650 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2652 p->p_flag &= ~P_WAITED;
2653 PROC_LOCK(p->p_pptr);
2654 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2655 CLD_TRAPPED : CLD_STOPPED);
2656 PROC_UNLOCK(p->p_pptr);
2662 * Take the action for the specified signal
2663 * from the current set of pending signals.
2669 struct thread *td = curthread;
2670 register struct proc *p = td->td_proc;
2674 sigset_t returnmask;
2676 KASSERT(sig != 0, ("postsig"));
2678 PROC_LOCK_ASSERT(p, MA_OWNED);
2680 mtx_assert(&ps->ps_mtx, MA_OWNED);
2681 ksiginfo_init(&ksi);
2682 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2683 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2685 ksi.ksi_signo = sig;
2686 if (ksi.ksi_code == SI_TIMER)
2687 itimer_accept(p, ksi.ksi_timerid, &ksi);
2688 action = ps->ps_sigact[_SIG_IDX(sig)];
2690 if (KTRPOINT(td, KTR_PSIG))
2691 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2692 &td->td_oldsigmask : &td->td_sigmask, 0);
2694 if (p->p_stops & S_SIG) {
2695 mtx_unlock(&ps->ps_mtx);
2696 stopevent(p, S_SIG, sig);
2697 mtx_lock(&ps->ps_mtx);
2700 if (action == SIG_DFL) {
2702 * Default action, where the default is to kill
2703 * the process. (Other cases were ignored above.)
2705 mtx_unlock(&ps->ps_mtx);
2710 * If we get here, the signal must be caught.
2712 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2713 ("postsig action"));
2715 * Set the new mask value and also defer further
2716 * occurrences of this signal.
2718 * Special case: user has done a sigsuspend. Here the
2719 * current mask is not of interest, but rather the
2720 * mask from before the sigsuspend is what we want
2721 * restored after the signal processing is completed.
2723 if (td->td_pflags & TDP_OLDMASK) {
2724 returnmask = td->td_oldsigmask;
2725 td->td_pflags &= ~TDP_OLDMASK;
2727 returnmask = td->td_sigmask;
2729 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2730 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2731 SIGADDSET(td->td_sigmask, sig);
2733 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2735 * See kern_sigaction() for origin of this code.
2737 SIGDELSET(ps->ps_sigcatch, sig);
2738 if (sig != SIGCONT &&
2739 sigprop(sig) & SA_IGNORE)
2740 SIGADDSET(ps->ps_sigignore, sig);
2741 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2743 td->td_ru.ru_nsignals++;
2744 if (p->p_sig == sig) {
2748 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2753 * Kill the current process for stated reason.
2761 PROC_LOCK_ASSERT(p, MA_OWNED);
2762 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2763 p, p->p_pid, p->p_comm);
2764 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2765 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2766 psignal(p, SIGKILL);
2770 * Force the current process to exit with the specified signal, dumping core
2771 * if appropriate. We bypass the normal tests for masked and caught signals,
2772 * allowing unrecoverable failures to terminate the process without changing
2773 * signal state. Mark the accounting record with the signal termination.
2774 * If dumping core, save the signal number for the debugger. Calls exit and
2782 struct proc *p = td->td_proc;
2784 PROC_LOCK_ASSERT(p, MA_OWNED);
2785 p->p_acflag |= AXSIG;
2787 * We must be single-threading to generate a core dump. This
2788 * ensures that the registers in the core file are up-to-date.
2789 * Also, the ELF dump handler assumes that the thread list doesn't
2790 * change out from under it.
2792 * XXX If another thread attempts to single-thread before us
2793 * (e.g. via fork()), we won't get a dump at all.
2795 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2798 * Log signals which would cause core dumps
2799 * (Log as LOG_INFO to appease those who don't want
2801 * XXX : Todo, as well as euid, write out ruid too
2802 * Note that coredump() drops proc lock.
2804 if (coredump(td) == 0)
2806 if (kern_logsigexit)
2808 "pid %d (%s), uid %d: exited on signal %d%s\n",
2809 p->p_pid, p->p_comm,
2810 td->td_ucred ? td->td_ucred->cr_uid : -1,
2812 sig & WCOREFLAG ? " (core dumped)" : "");
2815 exit1(td, W_EXITCODE(0, sig));
2820 * Send queued SIGCHLD to parent when child process's state
2824 sigparent(struct proc *p, int reason, int status)
2826 PROC_LOCK_ASSERT(p, MA_OWNED);
2827 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2829 if (p->p_ksi != NULL) {
2830 p->p_ksi->ksi_signo = SIGCHLD;
2831 p->p_ksi->ksi_code = reason;
2832 p->p_ksi->ksi_status = status;
2833 p->p_ksi->ksi_pid = p->p_pid;
2834 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2835 if (KSI_ONQ(p->p_ksi))
2838 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2842 childproc_jobstate(struct proc *p, int reason, int status)
2846 PROC_LOCK_ASSERT(p, MA_OWNED);
2847 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2850 * Wake up parent sleeping in kern_wait(), also send
2851 * SIGCHLD to parent, but SIGCHLD does not guarantee
2852 * that parent will awake, because parent may masked
2855 p->p_pptr->p_flag |= P_STATCHILD;
2858 ps = p->p_pptr->p_sigacts;
2859 mtx_lock(&ps->ps_mtx);
2860 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2861 mtx_unlock(&ps->ps_mtx);
2862 sigparent(p, reason, status);
2864 mtx_unlock(&ps->ps_mtx);
2868 childproc_stopped(struct proc *p, int reason)
2870 childproc_jobstate(p, reason, p->p_xstat);
2874 childproc_continued(struct proc *p)
2876 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2880 childproc_exited(struct proc *p)
2883 int status = p->p_xstat; /* convert to int */
2885 reason = CLD_EXITED;
2886 if (WCOREDUMP(status))
2887 reason = CLD_DUMPED;
2888 else if (WIFSIGNALED(status))
2889 reason = CLD_KILLED;
2891 * XXX avoid calling wakeup(p->p_pptr), the work is
2894 sigparent(p, reason, status);
2897 static char corefilename[MAXPATHLEN] = {"%N.core"};
2898 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2899 sizeof(corefilename), "process corefile name format string");
2902 * expand_name(name, uid, pid)
2903 * Expand the name described in corefilename, using name, uid, and pid.
2904 * corefilename is a printf-like string, with three format specifiers:
2905 * %N name of process ("name")
2906 * %P process id (pid)
2908 * For example, "%N.core" is the default; they can be disabled completely
2909 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2910 * This is controlled by the sysctl variable kern.corefile (see above).
2913 expand_name(name, uid, pid)
2923 format = corefilename;
2924 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2927 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
2928 for (i = 0; format[i]; i++) {
2929 switch (format[i]) {
2930 case '%': /* Format character */
2932 switch (format[i]) {
2934 sbuf_putc(&sb, '%');
2936 case 'N': /* process name */
2937 sbuf_printf(&sb, "%s", name);
2939 case 'P': /* process id */
2940 sbuf_printf(&sb, "%u", pid);
2942 case 'U': /* user id */
2943 sbuf_printf(&sb, "%u", uid);
2947 "Unknown format character %c in "
2948 "corename `%s'\n", format[i], format);
2952 sbuf_putc(&sb, format[i]);
2955 if (sbuf_overflowed(&sb)) {
2957 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
2958 "long\n", (long)pid, name, (u_long)uid);
2968 * Dump a process' core. The main routine does some
2969 * policy checking, and creates the name of the coredump;
2970 * then it passes on a vnode and a size limit to the process-specific
2971 * coredump routine if there is one; if there _is not_ one, it returns
2972 * ENOSYS; otherwise it returns the error from the process-specific routine.
2976 coredump(struct thread *td)
2978 struct proc *p = td->td_proc;
2979 register struct vnode *vp;
2980 register struct ucred *cred = td->td_ucred;
2982 struct nameidata nd;
2984 int error, error1, flags, locked;
2986 char *name; /* name of corefile */
2990 PROC_LOCK_ASSERT(p, MA_OWNED);
2991 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
2992 _STOPEVENT(p, S_CORE, 0);
2994 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2998 audit_proc_coredump(td, NULL, EINVAL);
3002 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3005 audit_proc_coredump(td, name, EFAULT);
3012 * Note that the bulk of limit checking is done after
3013 * the corefile is created. The exception is if the limit
3014 * for corefiles is 0, in which case we don't bother
3015 * creating the corefile at all. This layout means that
3016 * a corefile is truncated instead of not being created,
3017 * if it is larger than the limit.
3019 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3023 audit_proc_coredump(td, name, EFBIG);
3030 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3031 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3032 error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
3036 audit_proc_coredump(td, name, error);
3041 vfslocked = NDHASGIANT(&nd);
3042 NDFREE(&nd, NDF_ONLY_PNBUF);
3045 /* Don't dump to non-regular files or files with links. */
3046 if (vp->v_type != VREG ||
3047 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
3054 lf.l_whence = SEEK_SET;
3057 lf.l_type = F_WRLCK;
3058 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3060 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3061 lf.l_type = F_UNLCK;
3063 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3064 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3066 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3068 VFS_UNLOCK_GIANT(vfslocked);
3074 if (set_core_nodump_flag)
3075 vattr.va_flags = UF_NODUMP;
3076 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3077 VOP_SETATTR(vp, &vattr, cred);
3079 vn_finished_write(mp);
3081 p->p_acflag |= ACORE;
3084 error = p->p_sysent->sv_coredump ?
3085 p->p_sysent->sv_coredump(td, vp, limit) :
3089 lf.l_type = F_UNLCK;
3090 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3093 error1 = vn_close(vp, FWRITE, cred, td);
3098 audit_proc_coredump(td, name, error);
3101 VFS_UNLOCK_GIANT(vfslocked);
3106 * Nonexistent system call-- signal process (may want to handle it). Flag
3107 * error in case process won't see signal immediately (blocked or ignored).
3109 #ifndef _SYS_SYSPROTO_H_
3118 struct nosys_args *args;
3120 struct proc *p = td->td_proc;
3129 * Send a SIGIO or SIGURG signal to a process or process group using stored
3130 * credentials rather than those of the current process.
3133 pgsigio(sigiop, sig, checkctty)
3134 struct sigio **sigiop;
3137 struct sigio *sigio;
3141 if (sigio == NULL) {
3145 if (sigio->sio_pgid > 0) {
3146 PROC_LOCK(sigio->sio_proc);
3147 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3148 psignal(sigio->sio_proc, sig);
3149 PROC_UNLOCK(sigio->sio_proc);
3150 } else if (sigio->sio_pgid < 0) {
3153 PGRP_LOCK(sigio->sio_pgrp);
3154 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3156 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3157 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3161 PGRP_UNLOCK(sigio->sio_pgrp);
3167 filt_sigattach(struct knote *kn)
3169 struct proc *p = curproc;
3171 kn->kn_ptr.p_proc = p;
3172 kn->kn_flags |= EV_CLEAR; /* automatically set */
3174 knlist_add(&p->p_klist, kn, 0);
3180 filt_sigdetach(struct knote *kn)
3182 struct proc *p = kn->kn_ptr.p_proc;
3184 knlist_remove(&p->p_klist, kn, 0);
3188 * signal knotes are shared with proc knotes, so we apply a mask to
3189 * the hint in order to differentiate them from process hints. This
3190 * could be avoided by using a signal-specific knote list, but probably
3191 * isn't worth the trouble.
3194 filt_signal(struct knote *kn, long hint)
3197 if (hint & NOTE_SIGNAL) {
3198 hint &= ~NOTE_SIGNAL;
3200 if (kn->kn_id == hint)
3203 return (kn->kn_data != 0);
3211 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3213 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3218 sigacts_free(struct sigacts *ps)
3221 mtx_lock(&ps->ps_mtx);
3223 if (ps->ps_refcnt == 0) {
3224 mtx_destroy(&ps->ps_mtx);
3225 free(ps, M_SUBPROC);
3227 mtx_unlock(&ps->ps_mtx);
3231 sigacts_hold(struct sigacts *ps)
3233 mtx_lock(&ps->ps_mtx);
3235 mtx_unlock(&ps->ps_mtx);
3240 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3243 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3244 mtx_lock(&src->ps_mtx);
3245 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3246 mtx_unlock(&src->ps_mtx);
3250 sigacts_shared(struct sigacts *ps)
3254 mtx_lock(&ps->ps_mtx);
3255 shared = ps->ps_refcnt > 1;
3256 mtx_unlock(&ps->ps_mtx);