2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
48 #include <sys/condvar.h>
49 #include <sys/event.h>
50 #include <sys/fcntl.h>
51 #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>
64 #include <sys/sleepqueue.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysent.h>
71 #include <sys/syslog.h>
72 #include <sys/sysproto.h>
73 #include <sys/timers.h>
74 #include <sys/unistd.h>
77 #include <vm/vm_extern.h>
80 #include <machine/cpu.h>
82 #include <security/audit/audit.h>
84 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
86 static int coredump(struct thread *);
87 static char *expand_name(const char *, uid_t, pid_t);
88 static int killpg1(struct thread *td, int sig, int pgid, int all);
89 static int issignal(struct thread *p);
90 static int sigprop(int sig);
91 static void tdsigwakeup(struct thread *, int, sig_t, int);
92 static void sig_suspend_threads(struct thread *, struct proc *, int);
93 static int filt_sigattach(struct knote *kn);
94 static void filt_sigdetach(struct knote *kn);
95 static int filt_signal(struct knote *kn, long hint);
96 static struct thread *sigtd(struct proc *p, int sig, int prop);
98 static int do_tdsignal(struct proc *, struct thread *, int, ksiginfo_t *);
100 static void sigqueue_start(void);
102 static uma_zone_t ksiginfo_zone = NULL;
103 struct filterops sig_filtops =
104 { 0, filt_sigattach, filt_sigdetach, filt_signal };
106 int kern_logsigexit = 1;
107 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
109 "Log processes quitting on abnormal signals to syslog(3)");
111 static int kern_forcesigexit = 1;
112 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
113 &kern_forcesigexit, 0, "Force trap signal to be handled");
115 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
117 static int max_pending_per_proc = 128;
118 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
119 &max_pending_per_proc, 0, "Max pending signals per proc");
121 static int preallocate_siginfo = 1024;
122 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
123 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
124 &preallocate_siginfo, 0, "Preallocated signal memory size");
126 static int signal_overflow = 0;
127 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
128 &signal_overflow, 0, "Number of signals overflew");
130 static int signal_alloc_fail = 0;
131 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
132 &signal_alloc_fail, 0, "signals failed to be allocated");
134 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
137 * Policy -- Can ucred cr1 send SIGIO to process cr2?
138 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
139 * in the right situations.
141 #define CANSIGIO(cr1, cr2) \
142 ((cr1)->cr_uid == 0 || \
143 (cr1)->cr_ruid == (cr2)->cr_ruid || \
144 (cr1)->cr_uid == (cr2)->cr_ruid || \
145 (cr1)->cr_ruid == (cr2)->cr_uid || \
146 (cr1)->cr_uid == (cr2)->cr_uid)
149 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
150 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
152 static int do_coredump = 1;
153 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
154 &do_coredump, 0, "Enable/Disable coredumps");
156 static int set_core_nodump_flag = 0;
157 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
158 0, "Enable setting the NODUMP flag on coredump files");
161 * Signal properties and actions.
162 * The array below categorizes the signals and their default actions
163 * according to the following properties:
165 #define SA_KILL 0x01 /* terminates process by default */
166 #define SA_CORE 0x02 /* ditto and coredumps */
167 #define SA_STOP 0x04 /* suspend process */
168 #define SA_TTYSTOP 0x08 /* ditto, from tty */
169 #define SA_IGNORE 0x10 /* ignore by default */
170 #define SA_CONT 0x20 /* continue if suspended */
171 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
172 #define SA_PROC 0x80 /* deliverable to any thread */
174 static int sigproptbl[NSIG] = {
175 SA_KILL|SA_PROC, /* SIGHUP */
176 SA_KILL|SA_PROC, /* SIGINT */
177 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
178 SA_KILL|SA_CORE, /* SIGILL */
179 SA_KILL|SA_CORE, /* SIGTRAP */
180 SA_KILL|SA_CORE, /* SIGABRT */
181 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
182 SA_KILL|SA_CORE, /* SIGFPE */
183 SA_KILL|SA_PROC, /* SIGKILL */
184 SA_KILL|SA_CORE, /* SIGBUS */
185 SA_KILL|SA_CORE, /* SIGSEGV */
186 SA_KILL|SA_CORE, /* SIGSYS */
187 SA_KILL|SA_PROC, /* SIGPIPE */
188 SA_KILL|SA_PROC, /* SIGALRM */
189 SA_KILL|SA_PROC, /* SIGTERM */
190 SA_IGNORE|SA_PROC, /* SIGURG */
191 SA_STOP|SA_PROC, /* SIGSTOP */
192 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
193 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
194 SA_IGNORE|SA_PROC, /* SIGCHLD */
195 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
196 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
197 SA_IGNORE|SA_PROC, /* SIGIO */
198 SA_KILL, /* SIGXCPU */
199 SA_KILL, /* SIGXFSZ */
200 SA_KILL|SA_PROC, /* SIGVTALRM */
201 SA_KILL|SA_PROC, /* SIGPROF */
202 SA_IGNORE|SA_PROC, /* SIGWINCH */
203 SA_IGNORE|SA_PROC, /* SIGINFO */
204 SA_KILL|SA_PROC, /* SIGUSR1 */
205 SA_KILL|SA_PROC, /* SIGUSR2 */
211 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
212 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
213 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
214 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
215 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
216 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
220 ksiginfo_alloc(int wait)
227 if (ksiginfo_zone != NULL)
228 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
233 ksiginfo_free(ksiginfo_t *ksi)
235 uma_zfree(ksiginfo_zone, ksi);
239 ksiginfo_tryfree(ksiginfo_t *ksi)
241 if (!(ksi->ksi_flags & KSI_EXT)) {
242 uma_zfree(ksiginfo_zone, ksi);
249 sigqueue_init(sigqueue_t *list, struct proc *p)
251 SIGEMPTYSET(list->sq_signals);
252 SIGEMPTYSET(list->sq_kill);
253 TAILQ_INIT(&list->sq_list);
255 list->sq_flags = SQ_INIT;
259 * Get a signal's ksiginfo.
261 * 0 - signal not found
262 * others - signal number
265 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
267 struct proc *p = sq->sq_proc;
268 struct ksiginfo *ksi, *next;
271 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
273 if (!SIGISMEMBER(sq->sq_signals, signo))
276 if (SIGISMEMBER(sq->sq_kill, signo)) {
278 SIGDELSET(sq->sq_kill, signo);
281 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
282 if (ksi->ksi_signo == signo) {
284 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
285 ksi->ksi_sigq = NULL;
286 ksiginfo_copy(ksi, si);
287 if (ksiginfo_tryfree(ksi) && p != NULL)
296 SIGDELSET(sq->sq_signals, signo);
297 si->ksi_signo = signo;
302 sigqueue_take(ksiginfo_t *ksi)
308 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
312 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
313 ksi->ksi_sigq = NULL;
314 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
317 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
318 kp = TAILQ_NEXT(kp, ksi_link)) {
319 if (kp->ksi_signo == ksi->ksi_signo)
322 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
323 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
327 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
329 struct proc *p = sq->sq_proc;
330 struct ksiginfo *ksi;
333 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
335 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
336 SIGADDSET(sq->sq_kill, signo);
340 /* directly insert the ksi, don't copy it */
341 if (si->ksi_flags & KSI_INS) {
342 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
347 if (__predict_false(ksiginfo_zone == NULL)) {
348 SIGADDSET(sq->sq_kill, signo);
352 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
355 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
361 ksiginfo_copy(si, ksi);
362 ksi->ksi_signo = signo;
363 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
367 if ((si->ksi_flags & KSI_TRAP) != 0) {
369 SIGADDSET(sq->sq_kill, signo);
378 SIGADDSET(sq->sq_signals, signo);
383 sigqueue_flush(sigqueue_t *sq)
385 struct proc *p = sq->sq_proc;
388 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
391 PROC_LOCK_ASSERT(p, MA_OWNED);
393 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
394 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
395 ksi->ksi_sigq = NULL;
396 if (ksiginfo_tryfree(ksi) && p != NULL)
400 SIGEMPTYSET(sq->sq_signals);
401 SIGEMPTYSET(sq->sq_kill);
405 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
409 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
411 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
412 SIGADDSET(*set, ksi->ksi_signo);
413 SIGSETOR(*set, sq->sq_kill);
417 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
420 struct proc *p1, *p2;
421 ksiginfo_t *ksi, *next;
423 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
424 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
426 * make a copy, this allows setp to point to src or dst
427 * sq_signals without trouble.
432 /* Move siginfo to target list */
433 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
434 if (SIGISMEMBER(set, ksi->ksi_signo)) {
435 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
438 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
445 /* Move pending bits to target list */
448 SIGSETOR(dst->sq_kill, tmp);
449 SIGSETNAND(src->sq_kill, tmp);
451 tmp = src->sq_signals;
453 SIGSETOR(dst->sq_signals, tmp);
454 SIGSETNAND(src->sq_signals, tmp);
456 /* Finally, rescan src queue and set pending bits for it */
457 sigqueue_collect_set(src, &src->sq_signals);
461 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
466 SIGADDSET(set, signo);
467 sigqueue_move_set(src, dst, &set);
471 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
473 struct proc *p = sq->sq_proc;
474 ksiginfo_t *ksi, *next;
476 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
478 /* Remove siginfo queue */
479 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
480 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
481 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
482 ksi->ksi_sigq = NULL;
483 if (ksiginfo_tryfree(ksi) && p != NULL)
487 SIGSETNAND(sq->sq_kill, *set);
488 SIGSETNAND(sq->sq_signals, *set);
489 /* Finally, rescan queue and set pending bits for it */
490 sigqueue_collect_set(sq, &sq->sq_signals);
494 sigqueue_delete(sigqueue_t *sq, int signo)
499 SIGADDSET(set, signo);
500 sigqueue_delete_set(sq, &set);
503 /* Remove a set of signals for a process */
505 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
510 PROC_LOCK_ASSERT(p, MA_OWNED);
512 sigqueue_init(&worklist, NULL);
513 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
516 FOREACH_THREAD_IN_PROC(p, td0)
517 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
520 sigqueue_flush(&worklist);
524 sigqueue_delete_proc(struct proc *p, int signo)
529 SIGADDSET(set, signo);
530 sigqueue_delete_set_proc(p, &set);
534 sigqueue_delete_stopmask_proc(struct proc *p)
539 SIGADDSET(set, SIGSTOP);
540 SIGADDSET(set, SIGTSTP);
541 SIGADDSET(set, SIGTTIN);
542 SIGADDSET(set, SIGTTOU);
543 sigqueue_delete_set_proc(p, &set);
547 * Determine signal that should be delivered to process p, the current
548 * process, 0 if none. If there is a pending stop signal with default
549 * action, the process stops in issignal().
552 cursig(struct thread *td)
554 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
555 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
556 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
557 return (SIGPENDING(td) ? issignal(td) : 0);
561 * Arrange for ast() to handle unmasked pending signals on return to user
562 * mode. This must be called whenever a signal is added to td_sigqueue or
563 * unmasked in td_sigmask.
566 signotify(struct thread *td)
577 PROC_LOCK_ASSERT(p, MA_OWNED);
580 * If our mask changed we may have to move signal that were
581 * previously masked by all threads to our sigqueue.
583 set = p->p_sigqueue.sq_signals;
585 if (p->p_flag & P_SA)
586 saved = p->p_sigqueue.sq_signals;
588 SIGSETNAND(set, td->td_sigmask);
589 if (! SIGISEMPTY(set))
590 sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set);
591 if (SIGPENDING(td)) {
593 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
597 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
598 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
599 /* pending set changed */
600 p->p_flag |= P_SIGEVENT;
601 wakeup(&p->p_siglist);
608 sigonstack(size_t sp)
610 struct thread *td = curthread;
612 return ((td->td_pflags & TDP_ALTSTACK) ?
613 #if defined(COMPAT_43)
614 ((td->td_sigstk.ss_size == 0) ?
615 (td->td_sigstk.ss_flags & SS_ONSTACK) :
616 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
618 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
627 if (sig > 0 && sig < NSIG)
628 return (sigproptbl[_SIG_IDX(sig)]);
633 sig_ffs(sigset_t *set)
637 for (i = 0; i < _SIG_WORDS; i++)
639 return (ffs(set->__bits[i]) + (i * 32));
650 kern_sigaction(td, sig, act, oact, flags)
653 struct sigaction *act, *oact;
657 struct proc *p = td->td_proc;
659 if (!_SIG_VALID(sig))
664 mtx_lock(&ps->ps_mtx);
666 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
668 if (SIGISMEMBER(ps->ps_sigonstack, sig))
669 oact->sa_flags |= SA_ONSTACK;
670 if (!SIGISMEMBER(ps->ps_sigintr, sig))
671 oact->sa_flags |= SA_RESTART;
672 if (SIGISMEMBER(ps->ps_sigreset, sig))
673 oact->sa_flags |= SA_RESETHAND;
674 if (SIGISMEMBER(ps->ps_signodefer, sig))
675 oact->sa_flags |= SA_NODEFER;
676 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
677 oact->sa_flags |= SA_SIGINFO;
679 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
681 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
682 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
683 oact->sa_flags |= SA_NOCLDSTOP;
684 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
685 oact->sa_flags |= SA_NOCLDWAIT;
688 if ((sig == SIGKILL || sig == SIGSTOP) &&
689 act->sa_handler != SIG_DFL) {
690 mtx_unlock(&ps->ps_mtx);
696 * Change setting atomically.
699 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
700 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
701 if (act->sa_flags & SA_SIGINFO) {
702 ps->ps_sigact[_SIG_IDX(sig)] =
703 (__sighandler_t *)act->sa_sigaction;
704 SIGADDSET(ps->ps_siginfo, sig);
706 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
707 SIGDELSET(ps->ps_siginfo, sig);
709 if (!(act->sa_flags & SA_RESTART))
710 SIGADDSET(ps->ps_sigintr, sig);
712 SIGDELSET(ps->ps_sigintr, sig);
713 if (act->sa_flags & SA_ONSTACK)
714 SIGADDSET(ps->ps_sigonstack, sig);
716 SIGDELSET(ps->ps_sigonstack, sig);
717 if (act->sa_flags & SA_RESETHAND)
718 SIGADDSET(ps->ps_sigreset, sig);
720 SIGDELSET(ps->ps_sigreset, sig);
721 if (act->sa_flags & SA_NODEFER)
722 SIGADDSET(ps->ps_signodefer, sig);
724 SIGDELSET(ps->ps_signodefer, sig);
725 if (sig == SIGCHLD) {
726 if (act->sa_flags & SA_NOCLDSTOP)
727 ps->ps_flag |= PS_NOCLDSTOP;
729 ps->ps_flag &= ~PS_NOCLDSTOP;
730 if (act->sa_flags & SA_NOCLDWAIT) {
732 * Paranoia: since SA_NOCLDWAIT is implemented
733 * by reparenting the dying child to PID 1 (and
734 * trust it to reap the zombie), PID 1 itself
735 * is forbidden to set SA_NOCLDWAIT.
738 ps->ps_flag &= ~PS_NOCLDWAIT;
740 ps->ps_flag |= PS_NOCLDWAIT;
742 ps->ps_flag &= ~PS_NOCLDWAIT;
743 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
744 ps->ps_flag |= PS_CLDSIGIGN;
746 ps->ps_flag &= ~PS_CLDSIGIGN;
749 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
750 * and for signals set to SIG_DFL where the default is to
751 * ignore. However, don't put SIGCONT in ps_sigignore, as we
752 * have to restart the process.
754 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
755 (sigprop(sig) & SA_IGNORE &&
756 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
758 if ((p->p_flag & P_SA) &&
759 SIGISMEMBER(p->p_sigqueue.sq_signals, sig)) {
760 p->p_flag |= P_SIGEVENT;
761 wakeup(&p->p_siglist);
764 /* never to be seen again */
765 sigqueue_delete_proc(p, sig);
767 /* easier in psignal */
768 SIGADDSET(ps->ps_sigignore, sig);
769 SIGDELSET(ps->ps_sigcatch, sig);
771 SIGDELSET(ps->ps_sigignore, sig);
772 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
773 SIGDELSET(ps->ps_sigcatch, sig);
775 SIGADDSET(ps->ps_sigcatch, sig);
777 #ifdef COMPAT_FREEBSD4
778 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
779 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
780 (flags & KSA_FREEBSD4) == 0)
781 SIGDELSET(ps->ps_freebsd4, sig);
783 SIGADDSET(ps->ps_freebsd4, sig);
786 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
787 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
788 (flags & KSA_OSIGSET) == 0)
789 SIGDELSET(ps->ps_osigset, sig);
791 SIGADDSET(ps->ps_osigset, sig);
794 mtx_unlock(&ps->ps_mtx);
799 #ifndef _SYS_SYSPROTO_H_
800 struct sigaction_args {
802 struct sigaction *act;
803 struct sigaction *oact;
809 register struct sigaction_args *uap;
811 struct sigaction act, oact;
812 register struct sigaction *actp, *oactp;
815 actp = (uap->act != NULL) ? &act : NULL;
816 oactp = (uap->oact != NULL) ? &oact : NULL;
818 error = copyin(uap->act, actp, sizeof(act));
822 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
824 error = copyout(oactp, uap->oact, sizeof(oact));
828 #ifdef COMPAT_FREEBSD4
829 #ifndef _SYS_SYSPROTO_H_
830 struct freebsd4_sigaction_args {
832 struct sigaction *act;
833 struct sigaction *oact;
837 freebsd4_sigaction(td, uap)
839 register struct freebsd4_sigaction_args *uap;
841 struct sigaction act, oact;
842 register struct sigaction *actp, *oactp;
846 actp = (uap->act != NULL) ? &act : NULL;
847 oactp = (uap->oact != NULL) ? &oact : NULL;
849 error = copyin(uap->act, actp, sizeof(act));
853 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
855 error = copyout(oactp, uap->oact, sizeof(oact));
858 #endif /* COMAPT_FREEBSD4 */
860 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
861 #ifndef _SYS_SYSPROTO_H_
862 struct osigaction_args {
864 struct osigaction *nsa;
865 struct osigaction *osa;
871 register struct osigaction_args *uap;
873 struct osigaction sa;
874 struct sigaction nsa, osa;
875 register struct sigaction *nsap, *osap;
878 if (uap->signum <= 0 || uap->signum >= ONSIG)
881 nsap = (uap->nsa != NULL) ? &nsa : NULL;
882 osap = (uap->osa != NULL) ? &osa : NULL;
885 error = copyin(uap->nsa, &sa, sizeof(sa));
888 nsap->sa_handler = sa.sa_handler;
889 nsap->sa_flags = sa.sa_flags;
890 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
892 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
893 if (osap && !error) {
894 sa.sa_handler = osap->sa_handler;
895 sa.sa_flags = osap->sa_flags;
896 SIG2OSIG(osap->sa_mask, sa.sa_mask);
897 error = copyout(&sa, uap->osa, sizeof(sa));
902 #if !defined(__i386__)
903 /* Avoid replicating the same stub everywhere */
907 struct osigreturn_args *uap;
910 return (nosys(td, (struct nosys_args *)uap));
913 #endif /* COMPAT_43 */
916 * Initialize signal state for process 0;
917 * set to ignore signals that are ignored by default.
928 mtx_lock(&ps->ps_mtx);
929 for (i = 1; i <= NSIG; i++)
930 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
931 SIGADDSET(ps->ps_sigignore, i);
932 mtx_unlock(&ps->ps_mtx);
937 * Reset signals for an exec of the specified process.
940 execsigs(struct proc *p)
947 * Reset caught signals. Held signals remain held
948 * through td_sigmask (unless they were caught,
949 * and are now ignored by default).
951 PROC_LOCK_ASSERT(p, MA_OWNED);
952 td = FIRST_THREAD_IN_PROC(p);
954 mtx_lock(&ps->ps_mtx);
955 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
956 sig = sig_ffs(&ps->ps_sigcatch);
957 SIGDELSET(ps->ps_sigcatch, sig);
958 if (sigprop(sig) & SA_IGNORE) {
960 SIGADDSET(ps->ps_sigignore, sig);
961 sigqueue_delete_proc(p, sig);
963 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
966 * Reset stack state to the user stack.
967 * Clear set of signals caught on the signal stack.
969 td->td_sigstk.ss_flags = SS_DISABLE;
970 td->td_sigstk.ss_size = 0;
971 td->td_sigstk.ss_sp = 0;
972 td->td_pflags &= ~TDP_ALTSTACK;
974 * Reset no zombies if child dies flag as Solaris does.
976 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
977 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
978 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
979 mtx_unlock(&ps->ps_mtx);
985 * Manipulate signal mask.
988 kern_sigprocmask(td, how, set, oset, old)
991 sigset_t *set, *oset;
996 PROC_LOCK(td->td_proc);
998 *oset = td->td_sigmask;
1005 SIGSETOR(td->td_sigmask, *set);
1008 SIGSETNAND(td->td_sigmask, *set);
1014 SIGSETLO(td->td_sigmask, *set);
1016 td->td_sigmask = *set;
1024 PROC_UNLOCK(td->td_proc);
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)) {
1200 if (p->p_flag & P_SA) {
1201 p->p_flag |= P_SIGEVENT;
1202 wakeup(&p->p_siglist);
1205 sigqueue_move(&p->p_sigqueue,
1206 &td->td_sigqueue, i);
1211 SIGFILLSET(td->td_sigmask);
1212 SIG_CANTMASK(td->td_sigmask);
1213 SIGDELSET(td->td_sigmask, i);
1214 mtx_lock(&ps->ps_mtx);
1216 mtx_unlock(&ps->ps_mtx);
1221 * Because cursig() may have stopped current thread,
1222 * after it is resumed, things may have already been
1223 * changed, it should rescan any pending signals.
1233 * POSIX says this must be checked after looking for pending
1241 getnanouptime(&rts);
1242 if (timespeccmp(&rts, &ets, >=)) {
1247 timespecsub(&ts, &rts);
1248 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1253 td->td_sigmask = savedmask;
1254 SIGSETNAND(td->td_sigmask, waitset);
1256 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1258 if (error == ERESTART) {
1259 /* timeout can not be restarted. */
1261 } else if (error == EAGAIN) {
1262 /* will calculate timeout by ourself. */
1269 td->td_sigmask = savedmask;
1273 sigqueue_get(&td->td_sigqueue, sig, ksi);
1274 ksi->ksi_signo = sig;
1275 if (ksi->ksi_code == SI_TIMER)
1276 itimer_accept(p, ksi->ksi_timerid, ksi);
1280 if (KTRPOINT(td, KTR_PSIG)) {
1283 mtx_lock(&ps->ps_mtx);
1284 action = ps->ps_sigact[_SIG_IDX(sig)];
1285 mtx_unlock(&ps->ps_mtx);
1286 ktrpsig(sig, action, &td->td_sigmask, 0);
1296 #ifndef _SYS_SYSPROTO_H_
1297 struct sigpending_args {
1304 struct sigpending_args *uap;
1306 struct proc *p = td->td_proc;
1310 pending = p->p_sigqueue.sq_signals;
1311 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1313 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1316 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1317 #ifndef _SYS_SYSPROTO_H_
1318 struct osigpending_args {
1323 osigpending(td, uap)
1325 struct osigpending_args *uap;
1327 struct proc *p = td->td_proc;
1331 pending = p->p_sigqueue.sq_signals;
1332 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1334 SIG2OSIG(pending, td->td_retval[0]);
1337 #endif /* COMPAT_43 */
1339 #if defined(COMPAT_43)
1341 * Generalized interface signal handler, 4.3-compatible.
1343 #ifndef _SYS_SYSPROTO_H_
1344 struct osigvec_args {
1354 register struct osigvec_args *uap;
1357 struct sigaction nsa, osa;
1358 register struct sigaction *nsap, *osap;
1361 if (uap->signum <= 0 || uap->signum >= ONSIG)
1363 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1364 osap = (uap->osv != NULL) ? &osa : NULL;
1366 error = copyin(uap->nsv, &vec, sizeof(vec));
1369 nsap->sa_handler = vec.sv_handler;
1370 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1371 nsap->sa_flags = vec.sv_flags;
1372 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1374 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1375 if (osap && !error) {
1376 vec.sv_handler = osap->sa_handler;
1377 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1378 vec.sv_flags = osap->sa_flags;
1379 vec.sv_flags &= ~SA_NOCLDWAIT;
1380 vec.sv_flags ^= SA_RESTART;
1381 error = copyout(&vec, uap->osv, sizeof(vec));
1386 #ifndef _SYS_SYSPROTO_H_
1387 struct osigblock_args {
1393 register struct thread *td;
1394 struct osigblock_args *uap;
1396 struct proc *p = td->td_proc;
1399 OSIG2SIG(uap->mask, set);
1402 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1403 SIGSETOR(td->td_sigmask, set);
1408 #ifndef _SYS_SYSPROTO_H_
1409 struct osigsetmask_args {
1414 osigsetmask(td, uap)
1416 struct osigsetmask_args *uap;
1418 struct proc *p = td->td_proc;
1421 OSIG2SIG(uap->mask, set);
1424 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1425 SIGSETLO(td->td_sigmask, set);
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;
1462 * When returning from sigsuspend, we want
1463 * the old mask to be restored after the
1464 * signal handler has finished. Thus, we
1465 * save it here and mark the sigacts structure
1469 td->td_oldsigmask = td->td_sigmask;
1470 td->td_pflags |= TDP_OLDMASK;
1472 td->td_sigmask = mask;
1474 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1477 /* always return EINTR rather than ERESTART... */
1481 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1483 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1484 * convention: libc stub passes mask, not pointer, to save a copyin.
1486 #ifndef _SYS_SYSPROTO_H_
1487 struct osigsuspend_args {
1493 osigsuspend(td, uap)
1495 struct osigsuspend_args *uap;
1497 struct proc *p = td->td_proc;
1501 td->td_oldsigmask = td->td_sigmask;
1502 td->td_pflags |= TDP_OLDMASK;
1503 OSIG2SIG(uap->mask, mask);
1505 SIGSETLO(td->td_sigmask, mask);
1507 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1510 /* always return EINTR rather than ERESTART... */
1513 #endif /* COMPAT_43 */
1515 #if defined(COMPAT_43)
1516 #ifndef _SYS_SYSPROTO_H_
1517 struct osigstack_args {
1518 struct sigstack *nss;
1519 struct sigstack *oss;
1526 register struct osigstack_args *uap;
1528 struct sigstack nss, oss;
1531 if (uap->nss != NULL) {
1532 error = copyin(uap->nss, &nss, sizeof(nss));
1536 oss.ss_sp = td->td_sigstk.ss_sp;
1537 oss.ss_onstack = sigonstack(cpu_getstack(td));
1538 if (uap->nss != NULL) {
1539 td->td_sigstk.ss_sp = nss.ss_sp;
1540 td->td_sigstk.ss_size = 0;
1541 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1542 td->td_pflags |= TDP_ALTSTACK;
1544 if (uap->oss != NULL)
1545 error = copyout(&oss, uap->oss, sizeof(oss));
1549 #endif /* COMPAT_43 */
1551 #ifndef _SYS_SYSPROTO_H_
1552 struct sigaltstack_args {
1559 sigaltstack(td, uap)
1561 register struct sigaltstack_args *uap;
1566 if (uap->ss != NULL) {
1567 error = copyin(uap->ss, &ss, sizeof(ss));
1571 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1572 (uap->oss != NULL) ? &oss : NULL);
1575 if (uap->oss != NULL)
1576 error = copyout(&oss, uap->oss, sizeof(stack_t));
1581 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1583 struct proc *p = td->td_proc;
1586 oonstack = sigonstack(cpu_getstack(td));
1589 *oss = td->td_sigstk;
1590 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1591 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1597 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1599 if (!(ss->ss_flags & SS_DISABLE)) {
1600 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1603 td->td_sigstk = *ss;
1604 td->td_pflags |= TDP_ALTSTACK;
1606 td->td_pflags &= ~TDP_ALTSTACK;
1613 * Common code for kill process group/broadcast kill.
1614 * cp is calling process.
1617 killpg1(td, sig, pgid, all)
1618 register struct thread *td;
1621 register struct proc *p;
1629 sx_slock(&allproc_lock);
1630 FOREACH_PROC_IN_SYSTEM(p) {
1632 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1633 p == td->td_proc || p->p_state == PRS_NEW) {
1637 if (p_cansignal(td, p, sig) == 0) {
1644 sx_sunlock(&allproc_lock);
1646 sx_slock(&proctree_lock);
1649 * zero pgid means send to my process group.
1651 pgrp = td->td_proc->p_pgrp;
1654 pgrp = pgfind(pgid);
1656 sx_sunlock(&proctree_lock);
1660 sx_sunlock(&proctree_lock);
1661 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1663 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1664 p->p_state == PRS_NEW ) {
1668 if (p_cansignal(td, p, sig) == 0) {
1677 return (nfound ? 0 : ESRCH);
1680 #ifndef _SYS_SYSPROTO_H_
1689 register struct thread *td;
1690 register struct kill_args *uap;
1692 register struct proc *p;
1695 AUDIT_ARG(signum, uap->signum);
1696 AUDIT_ARG(pid, uap->pid);
1697 if ((u_int)uap->signum > _SIG_MAXSIG)
1701 /* kill single process */
1702 if ((p = pfind(uap->pid)) == NULL) {
1703 if ((p = zpfind(uap->pid)) == NULL)
1706 AUDIT_ARG(process, p);
1707 error = p_cansignal(td, p, uap->signum);
1708 if (error == 0 && uap->signum)
1709 psignal(p, uap->signum);
1714 case -1: /* broadcast signal */
1715 return (killpg1(td, uap->signum, 0, 1));
1716 case 0: /* signal own process group */
1717 return (killpg1(td, uap->signum, 0, 0));
1718 default: /* negative explicit process group */
1719 return (killpg1(td, uap->signum, -uap->pid, 0));
1724 #if defined(COMPAT_43)
1725 #ifndef _SYS_SYSPROTO_H_
1726 struct okillpg_args {
1735 register struct okillpg_args *uap;
1738 AUDIT_ARG(signum, uap->signum);
1739 AUDIT_ARG(pid, uap->pgid);
1740 if ((u_int)uap->signum > _SIG_MAXSIG)
1743 return (killpg1(td, uap->signum, uap->pgid, 0));
1745 #endif /* COMPAT_43 */
1747 #ifndef _SYS_SYSPROTO_H_
1748 struct sigqueue_args {
1751 /* union sigval */ void *value;
1755 sigqueue(struct thread *td, struct sigqueue_args *uap)
1761 if ((u_int)uap->signum > _SIG_MAXSIG)
1765 * Specification says sigqueue can only send signal to
1771 if ((p = pfind(uap->pid)) == NULL) {
1772 if ((p = zpfind(uap->pid)) == NULL)
1775 error = p_cansignal(td, p, uap->signum);
1776 if (error == 0 && uap->signum != 0) {
1777 ksiginfo_init(&ksi);
1778 ksi.ksi_signo = uap->signum;
1779 ksi.ksi_code = SI_QUEUE;
1780 ksi.ksi_pid = td->td_proc->p_pid;
1781 ksi.ksi_uid = td->td_ucred->cr_ruid;
1782 ksi.ksi_value.sival_ptr = uap->value;
1783 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1790 * Send a signal to a process group.
1799 sx_slock(&proctree_lock);
1800 pgrp = pgfind(pgid);
1801 sx_sunlock(&proctree_lock);
1803 pgsignal(pgrp, sig, 0);
1810 * Send a signal to a process group. If checktty is 1,
1811 * limit to members which have a controlling terminal.
1814 pgsignal(pgrp, sig, checkctty)
1818 register struct proc *p;
1821 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1822 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1824 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1832 * Send a signal caused by a trap to the current thread. If it will be
1833 * caught immediately, deliver it with correct code. Otherwise, post it
1837 trapsignal(struct thread *td, ksiginfo_t *ksi)
1848 sig = ksi->ksi_signo;
1849 code = ksi->ksi_code;
1850 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1853 if (td->td_pflags & TDP_SA) {
1854 if (td->td_mailbox == NULL)
1855 thread_user_enter(td);
1857 SIGDELSET(td->td_sigmask, sig);
1860 * Force scheduling an upcall, so UTS has chance to
1861 * process the signal before thread runs again in
1865 td->td_upcall->ku_flags |= KUF_DOUPCALL;
1874 mtx_lock(&ps->ps_mtx);
1875 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1876 !SIGISMEMBER(td->td_sigmask, sig)) {
1877 td->td_ru.ru_nsignals++;
1879 if (KTRPOINT(curthread, KTR_PSIG))
1880 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1881 &td->td_sigmask, code);
1884 if (!(td->td_pflags & TDP_SA))
1885 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1886 ksi, &td->td_sigmask);
1888 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1889 ksi, &td->td_sigmask);
1892 else if (td->td_mailbox == NULL) {
1893 mtx_unlock(&ps->ps_mtx);
1894 /* UTS caused a sync signal */
1895 p->p_code = code; /* XXX for core dump/debugger */
1896 p->p_sig = sig; /* XXX to verify code */
1899 mtx_unlock(&ps->ps_mtx);
1900 SIGADDSET(td->td_sigmask, sig);
1902 error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
1905 /* UTS memory corrupted */
1907 sigexit(td, SIGSEGV);
1908 mtx_lock(&ps->ps_mtx);
1911 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1912 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1913 SIGADDSET(td->td_sigmask, sig);
1914 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1916 * See kern_sigaction() for origin of this code.
1918 SIGDELSET(ps->ps_sigcatch, sig);
1919 if (sig != SIGCONT &&
1920 sigprop(sig) & SA_IGNORE)
1921 SIGADDSET(ps->ps_sigignore, sig);
1922 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1924 mtx_unlock(&ps->ps_mtx);
1927 * Avoid a possible infinite loop if the thread
1928 * masking the signal or process is ignoring the
1931 if (kern_forcesigexit &&
1932 (SIGISMEMBER(td->td_sigmask, sig) ||
1933 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1934 SIGDELSET(td->td_sigmask, sig);
1935 SIGDELSET(ps->ps_sigcatch, sig);
1936 SIGDELSET(ps->ps_sigignore, sig);
1937 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1939 mtx_unlock(&ps->ps_mtx);
1940 p->p_code = code; /* XXX for core dump/debugger */
1941 p->p_sig = sig; /* XXX to verify code */
1942 tdsignal(p, td, sig, ksi);
1947 static struct thread *
1948 sigtd(struct proc *p, int sig, int prop)
1950 struct thread *td, *signal_td;
1952 PROC_LOCK_ASSERT(p, MA_OWNED);
1955 * Check if current thread can handle the signal without
1956 * switching conetxt to another thread.
1958 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1962 FOREACH_THREAD_IN_PROC(p, td) {
1963 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1968 if (signal_td == NULL)
1969 signal_td = FIRST_THREAD_IN_PROC(p);
1975 * Send the signal to the process. If the signal has an action, the action
1976 * is usually performed by the target process rather than the caller; we add
1977 * the signal to the set of pending signals for the process.
1980 * o When a stop signal is sent to a sleeping process that takes the
1981 * default action, the process is stopped without awakening it.
1982 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1983 * regardless of the signal action (eg, blocked or ignored).
1985 * Other ignored signals are discarded immediately.
1987 * NB: This function may be entered from the debugger via the "kill" DDB
1988 * command. There is little that can be done to mitigate the possibly messy
1989 * side effects of this unwise possibility.
1992 psignal(struct proc *p, int sig)
1994 (void) tdsignal(p, NULL, sig, NULL);
1998 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
2000 struct thread *td = NULL;
2002 PROC_LOCK_ASSERT(p, MA_OWNED);
2004 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
2007 * ksi_code and other fields should be set before
2008 * calling this function.
2010 ksi->ksi_signo = sigev->sigev_signo;
2011 ksi->ksi_value = sigev->sigev_value;
2012 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2013 td = thread_find(p, sigev->sigev_notify_thread_id);
2017 return (tdsignal(p, td, ksi->ksi_signo, ksi));
2021 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2027 if (p->p_flag & P_SA)
2028 saved = p->p_sigqueue.sq_signals;
2029 ret = do_tdsignal(p, td, sig, ksi);
2030 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2031 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
2032 /* pending set changed */
2033 p->p_flag |= P_SIGEVENT;
2034 wakeup(&p->p_siglist);
2041 do_tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2045 sigqueue_t *sigqueue;
2052 PROC_LOCK_ASSERT(p, MA_OWNED);
2054 if (!_SIG_VALID(sig))
2056 panic("do_tdsignal(): invalid signal %d", sig);
2058 panic("tdsignal(): invalid signal %d", sig);
2062 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("do_tdsignal: ksi on queue"));
2064 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("tdsignal: ksi on queue"));
2068 * IEEE Std 1003.1-2001: return success when killing a zombie.
2070 if (p->p_state == PRS_ZOMBIE) {
2071 if (ksi && (ksi->ksi_flags & KSI_INS))
2072 ksiginfo_tryfree(ksi);
2077 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2078 prop = sigprop(sig);
2081 * If the signal is blocked and not destined for this thread, then
2082 * assign it to the process so that we can find it later in the first
2083 * thread that unblocks it. Otherwise, assign it to this thread now.
2086 td = sigtd(p, sig, prop);
2087 if (SIGISMEMBER(td->td_sigmask, sig))
2088 sigqueue = &p->p_sigqueue;
2090 sigqueue = &td->td_sigqueue;
2092 KASSERT(td->td_proc == p, ("invalid thread"));
2093 sigqueue = &td->td_sigqueue;
2097 * If the signal is being ignored,
2098 * then we forget about it immediately.
2099 * (Note: we don't set SIGCONT in ps_sigignore,
2100 * and if it is set to SIG_IGN,
2101 * action will be SIG_DFL here.)
2103 mtx_lock(&ps->ps_mtx);
2104 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2105 mtx_unlock(&ps->ps_mtx);
2106 if (ksi && (ksi->ksi_flags & KSI_INS))
2107 ksiginfo_tryfree(ksi);
2110 if (SIGISMEMBER(td->td_sigmask, sig))
2112 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2116 if (SIGISMEMBER(ps->ps_sigintr, sig))
2120 mtx_unlock(&ps->ps_mtx);
2123 sigqueue_delete_stopmask_proc(p);
2124 else if (prop & SA_STOP) {
2126 * If sending a tty stop signal to a member of an orphaned
2127 * process group, discard the signal here if the action
2128 * is default; don't stop the process below if sleeping,
2129 * and don't clear any pending SIGCONT.
2131 if ((prop & SA_TTYSTOP) &&
2132 (p->p_pgrp->pg_jobc == 0) &&
2133 (action == SIG_DFL)) {
2134 if (ksi && (ksi->ksi_flags & KSI_INS))
2135 ksiginfo_tryfree(ksi);
2138 sigqueue_delete_proc(p, SIGCONT);
2139 if (p->p_flag & P_CONTINUED) {
2140 p->p_flag &= ~P_CONTINUED;
2141 PROC_LOCK(p->p_pptr);
2142 sigqueue_take(p->p_ksi);
2143 PROC_UNLOCK(p->p_pptr);
2147 ret = sigqueue_add(sigqueue, sig, ksi);
2152 * Defer further processing for signals which are held,
2153 * except that stopped processes must be continued by SIGCONT.
2155 if (action == SIG_HOLD &&
2156 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2159 * SIGKILL: Remove procfs STOPEVENTs.
2161 if (sig == SIGKILL) {
2162 /* from procfs_ioctl.c: PIOCBIC */
2164 /* from procfs_ioctl.c: PIOCCONT */
2169 * Some signals have a process-wide effect and a per-thread
2170 * component. Most processing occurs when the process next
2171 * tries to cross the user boundary, however there are some
2172 * times when processing needs to be done immediatly, such as
2173 * waking up threads so that they can cross the user boundary.
2174 * We try do the per-process part here.
2177 if (P_SHOULDSTOP(p)) {
2179 * The process is in stopped mode. All the threads should be
2180 * either winding down or already on the suspended queue.
2182 if (p->p_flag & P_TRACED) {
2184 * The traced process is already stopped,
2185 * so no further action is necessary.
2186 * No signal can restart us.
2192 if (sig == SIGKILL) {
2194 * SIGKILL sets process running.
2195 * It will die elsewhere.
2196 * All threads must be restarted.
2198 p->p_flag &= ~P_STOPPED_SIG;
2202 if (prop & SA_CONT) {
2204 * If SIGCONT is default (or ignored), we continue the
2205 * process but don't leave the signal in sigqueue as
2206 * it has no further action. If SIGCONT is held, we
2207 * continue the process and leave the signal in
2208 * sigqueue. If the process catches SIGCONT, let it
2209 * handle the signal itself. If it isn't waiting on
2210 * an event, it goes back to run state.
2211 * Otherwise, process goes back to sleep state.
2213 p->p_flag &= ~P_STOPPED_SIG;
2214 if (p->p_numthreads == p->p_suspcount) {
2216 p->p_flag |= P_CONTINUED;
2217 p->p_xstat = SIGCONT;
2218 PROC_LOCK(p->p_pptr);
2219 childproc_continued(p);
2220 PROC_UNLOCK(p->p_pptr);
2223 if (action == SIG_DFL) {
2224 thread_unsuspend(p);
2226 sigqueue_delete(sigqueue, sig);
2229 if (action == SIG_CATCH) {
2232 * The process wants to catch it so it needs
2233 * to run at least one thread, but which one?
2234 * It would seem that the answer would be to
2235 * run an upcall in the next KSE to run, and
2236 * deliver the signal that way. In a NON KSE
2237 * process, we need to make sure that the
2238 * single thread is runnable asap.
2239 * XXXKSE for now however, make them all run.
2243 * The process wants to catch it so it needs
2244 * to run at least one thread, but which one?
2249 * The signal is not ignored or caught.
2251 thread_unsuspend(p);
2256 if (prop & SA_STOP) {
2258 * Already stopped, don't need to stop again
2259 * (If we did the shell could get confused).
2260 * Just make sure the signal STOP bit set.
2263 p->p_flag |= P_STOPPED_SIG;
2264 sigqueue_delete(sigqueue, sig);
2269 * All other kinds of signals:
2270 * If a thread is sleeping interruptibly, simulate a
2271 * wakeup so that when it is continued it will be made
2272 * runnable and can look at the signal. However, don't make
2273 * the PROCESS runnable, leave it stopped.
2274 * It may run a bit until it hits a thread_suspend_check().
2278 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2279 wakeup_swapper = sleepq_abort(td, intrval);
2286 * Mutexes are short lived. Threads waiting on them will
2287 * hit thread_suspend_check() soon.
2289 } else if (p->p_state == PRS_NORMAL) {
2290 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2292 tdsigwakeup(td, sig, action, intrval);
2298 MPASS(action == SIG_DFL);
2300 if (prop & SA_STOP) {
2301 if (p->p_flag & P_PPWAIT) {
2305 p->p_flag |= P_STOPPED_SIG;
2307 sig_suspend_threads(td, p, 1);
2308 if (p->p_numthreads == p->p_suspcount) {
2310 * only thread sending signal to another
2311 * process can reach here, if thread is sending
2312 * signal to its process, because thread does
2313 * not suspend itself here, p_numthreads
2314 * should never be equal to p_suspcount.
2318 sigqueue_delete_proc(p, p->p_xstat);
2327 /* Not in "NORMAL" state. discard the signal. */
2329 sigqueue_delete(sigqueue, sig);
2334 * The process is not stopped so we need to apply the signal to all the
2340 tdsigwakeup(td, sig, action, intrval);
2342 thread_unsuspend(p);
2345 /* If we jump here, proc slock should not be owned. */
2346 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2351 * The force of a signal has been directed against a single
2352 * thread. We need to see what we can do about knocking it
2353 * out of any sleep it may be in etc.
2356 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2358 struct proc *p = td->td_proc;
2363 PROC_LOCK_ASSERT(p, MA_OWNED);
2364 PROC_SLOCK_ASSERT(p, MA_OWNED);
2365 THREAD_LOCK_ASSERT(td, MA_OWNED);
2366 prop = sigprop(sig);
2369 * Bring the priority of a thread up if we want it to get
2370 * killed in this lifetime.
2372 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2373 sched_prio(td, PUSER);
2375 if (TD_ON_SLEEPQ(td)) {
2377 * If thread is sleeping uninterruptibly
2378 * we can't interrupt the sleep... the signal will
2379 * be noticed when the process returns through
2380 * trap() or syscall().
2382 if ((td->td_flags & TDF_SINTR) == 0)
2385 * If SIGCONT is default (or ignored) and process is
2386 * asleep, we are finished; the process should not
2389 if ((prop & SA_CONT) && action == SIG_DFL) {
2392 sigqueue_delete(&p->p_sigqueue, sig);
2394 * It may be on either list in this state.
2395 * Remove from both for now.
2397 sigqueue_delete(&td->td_sigqueue, sig);
2404 * Give low priority threads a better chance to run.
2406 if (td->td_priority > PUSER)
2407 sched_prio(td, PUSER);
2409 wakeup_swapper = sleepq_abort(td, intrval);
2412 * Other states do nothing with the signal immediately,
2413 * other than kicking ourselves if we are running.
2414 * It will either never be noticed, or noticed very soon.
2417 if (TD_IS_RUNNING(td) && td != curthread)
2426 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2430 PROC_LOCK_ASSERT(p, MA_OWNED);
2431 PROC_SLOCK_ASSERT(p, MA_OWNED);
2433 FOREACH_THREAD_IN_PROC(p, td2) {
2435 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2436 (td2->td_flags & TDF_SINTR) &&
2437 !TD_IS_SUSPENDED(td2)) {
2438 thread_suspend_one(td2);
2440 if (sending || td != td2)
2441 td2->td_flags |= TDF_ASTPENDING;
2443 if (TD_IS_RUNNING(td2) && td2 != td)
2444 forward_signal(td2);
2452 ptracestop(struct thread *td, int sig)
2454 struct proc *p = td->td_proc;
2456 PROC_LOCK_ASSERT(p, MA_OWNED);
2457 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2458 &p->p_mtx.lock_object, "Stopping for traced signal");
2461 td->td_flags |= TDF_XSIG;
2465 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2466 if (p->p_flag & P_SINGLE_EXIT) {
2468 td->td_flags &= ~TDF_XSIG;
2474 * Just make wait() to work, the last stopped thread
2479 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2480 sig_suspend_threads(td, p, 0);
2482 thread_suspend_switch(td);
2483 if (!(p->p_flag & P_TRACED)) {
2486 if (td->td_flags & TDF_DBSUSPEND) {
2487 if (p->p_flag & P_SINGLE_EXIT)
2493 return (td->td_xsig);
2497 * If the current process has received a signal (should be caught or cause
2498 * termination, should interrupt current syscall), return the signal number.
2499 * Stop signals with default action are processed immediately, then cleared;
2500 * they aren't returned. This is checked after each entry to the system for
2501 * a syscall or trap (though this can usually be done without calling issignal
2502 * by checking the pending signal masks in cursig.) The normal call
2505 * while (sig = cursig(curthread))
2514 sigset_t sigpending;
2515 int sig, prop, newsig;
2519 mtx_assert(&ps->ps_mtx, MA_OWNED);
2520 PROC_LOCK_ASSERT(p, MA_OWNED);
2522 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2524 sigpending = td->td_sigqueue.sq_signals;
2525 SIGSETNAND(sigpending, td->td_sigmask);
2527 if (p->p_flag & P_PPWAIT)
2528 SIG_STOPSIGMASK(sigpending);
2529 if (SIGISEMPTY(sigpending)) /* no signal to send */
2531 sig = sig_ffs(&sigpending);
2533 if (p->p_stops & S_SIG) {
2534 mtx_unlock(&ps->ps_mtx);
2535 stopevent(p, S_SIG, sig);
2536 mtx_lock(&ps->ps_mtx);
2540 * We should see pending but ignored signals
2541 * only if P_TRACED was on when they were posted.
2543 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2544 sigqueue_delete(&td->td_sigqueue, sig);
2546 if (td->td_pflags & TDP_SA)
2547 SIGADDSET(td->td_sigmask, sig);
2551 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2553 * If traced, always stop.
2555 mtx_unlock(&ps->ps_mtx);
2556 newsig = ptracestop(td, sig);
2557 mtx_lock(&ps->ps_mtx);
2560 if (td->td_pflags & TDP_SA)
2561 SIGADDSET(td->td_sigmask, sig);
2564 if (sig != newsig) {
2568 * XXX shrug off debugger, it causes siginfo to
2571 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2574 * If parent wants us to take the signal,
2575 * then it will leave it in p->p_xstat;
2576 * otherwise we just look for signals again.
2583 * Put the new signal into td_sigqueue. If the
2584 * signal is being masked, look for other signals.
2586 SIGADDSET(td->td_sigqueue.sq_signals, sig);
2588 if (td->td_pflags & TDP_SA)
2589 SIGDELSET(td->td_sigmask, sig);
2591 if (SIGISMEMBER(td->td_sigmask, sig))
2597 * If the traced bit got turned off, go back up
2598 * to the top to rescan signals. This ensures
2599 * that p_sig* and p_sigact are consistent.
2601 if ((p->p_flag & P_TRACED) == 0)
2605 prop = sigprop(sig);
2608 * Decide whether the signal should be returned.
2609 * Return the signal's number, or fall through
2610 * to clear it from the pending mask.
2612 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2614 case (intptr_t)SIG_DFL:
2616 * Don't take default actions on system processes.
2618 if (p->p_pid <= 1) {
2621 * Are you sure you want to ignore SIGSEGV
2624 printf("Process (pid %lu) got signal %d\n",
2625 (u_long)p->p_pid, sig);
2627 break; /* == ignore */
2630 * If there is a pending stop signal to process
2631 * with default action, stop here,
2632 * then clear the signal. However,
2633 * if process is member of an orphaned
2634 * process group, ignore tty stop signals.
2636 if (prop & SA_STOP) {
2637 if (p->p_flag & P_TRACED ||
2638 (p->p_pgrp->pg_jobc == 0 &&
2640 break; /* == ignore */
2641 mtx_unlock(&ps->ps_mtx);
2642 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2643 &p->p_mtx.lock_object, "Catching SIGSTOP");
2644 p->p_flag |= P_STOPPED_SIG;
2647 sig_suspend_threads(td, p, 0);
2648 thread_suspend_switch(td);
2650 mtx_lock(&ps->ps_mtx);
2652 } else if (prop & SA_IGNORE) {
2654 * Except for SIGCONT, shouldn't get here.
2655 * Default action is to ignore; drop it.
2657 break; /* == ignore */
2662 case (intptr_t)SIG_IGN:
2664 * Masking above should prevent us ever trying
2665 * to take action on an ignored signal other
2666 * than SIGCONT, unless process is traced.
2668 if ((prop & SA_CONT) == 0 &&
2669 (p->p_flag & P_TRACED) == 0)
2670 printf("issignal\n");
2671 break; /* == ignore */
2675 * This signal has an action, let
2676 * postsig() process it.
2680 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2686 thread_stopped(struct proc *p)
2690 PROC_LOCK_ASSERT(p, MA_OWNED);
2691 PROC_SLOCK_ASSERT(p, MA_OWNED);
2695 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2697 p->p_flag &= ~P_WAITED;
2698 PROC_LOCK(p->p_pptr);
2699 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2700 CLD_TRAPPED : CLD_STOPPED);
2701 PROC_UNLOCK(p->p_pptr);
2707 * Take the action for the specified signal
2708 * from the current set of pending signals.
2714 struct thread *td = curthread;
2715 register struct proc *p = td->td_proc;
2719 sigset_t returnmask;
2722 KASSERT(sig != 0, ("postsig"));
2724 PROC_LOCK_ASSERT(p, MA_OWNED);
2726 mtx_assert(&ps->ps_mtx, MA_OWNED);
2727 ksiginfo_init(&ksi);
2728 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2729 ksi.ksi_signo = sig;
2730 if (ksi.ksi_code == SI_TIMER)
2731 itimer_accept(p, ksi.ksi_timerid, &ksi);
2732 action = ps->ps_sigact[_SIG_IDX(sig)];
2734 if (KTRPOINT(td, KTR_PSIG))
2735 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2736 &td->td_oldsigmask : &td->td_sigmask, 0);
2738 if (p->p_stops & S_SIG) {
2739 mtx_unlock(&ps->ps_mtx);
2740 stopevent(p, S_SIG, sig);
2741 mtx_lock(&ps->ps_mtx);
2745 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2747 if (action == SIG_DFL) {
2750 * Default action, where the default is to kill
2751 * the process. (Other cases were ignored above.)
2753 mtx_unlock(&ps->ps_mtx);
2758 if (td->td_pflags & TDP_SA) {
2759 if (sig == SIGKILL) {
2760 mtx_unlock(&ps->ps_mtx);
2767 * If we get here, the signal must be caught.
2769 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2770 ("postsig action"));
2772 * Set the new mask value and also defer further
2773 * occurrences of this signal.
2775 * Special case: user has done a sigsuspend. Here the
2776 * current mask is not of interest, but rather the
2777 * mask from before the sigsuspend is what we want
2778 * restored after the signal processing is completed.
2780 if (td->td_pflags & TDP_OLDMASK) {
2781 returnmask = td->td_oldsigmask;
2782 td->td_pflags &= ~TDP_OLDMASK;
2784 returnmask = td->td_sigmask;
2786 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2787 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2788 SIGADDSET(td->td_sigmask, sig);
2790 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2792 * See kern_sigaction() for origin of this code.
2794 SIGDELSET(ps->ps_sigcatch, sig);
2795 if (sig != SIGCONT &&
2796 sigprop(sig) & SA_IGNORE)
2797 SIGADDSET(ps->ps_sigignore, sig);
2798 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2800 td->td_ru.ru_nsignals++;
2801 if (p->p_sig != sig) {
2809 if (td->td_pflags & TDP_SA)
2810 thread_signal_add(curthread, &ksi);
2812 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2814 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2820 * Kill the current process for stated reason.
2828 PROC_LOCK_ASSERT(p, MA_OWNED);
2829 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2830 p, p->p_pid, p->p_comm);
2831 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2832 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2833 psignal(p, SIGKILL);
2837 * Force the current process to exit with the specified signal, dumping core
2838 * if appropriate. We bypass the normal tests for masked and caught signals,
2839 * allowing unrecoverable failures to terminate the process without changing
2840 * signal state. Mark the accounting record with the signal termination.
2841 * If dumping core, save the signal number for the debugger. Calls exit and
2849 struct proc *p = td->td_proc;
2851 PROC_LOCK_ASSERT(p, MA_OWNED);
2852 p->p_acflag |= AXSIG;
2854 * We must be single-threading to generate a core dump. This
2855 * ensures that the registers in the core file are up-to-date.
2856 * Also, the ELF dump handler assumes that the thread list doesn't
2857 * change out from under it.
2859 * XXX If another thread attempts to single-thread before us
2860 * (e.g. via fork()), we won't get a dump at all.
2862 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2865 * Log signals which would cause core dumps
2866 * (Log as LOG_INFO to appease those who don't want
2868 * XXX : Todo, as well as euid, write out ruid too
2869 * Note that coredump() drops proc lock.
2871 if (coredump(td) == 0)
2873 if (kern_logsigexit)
2875 "pid %d (%s), uid %d: exited on signal %d%s\n",
2876 p->p_pid, p->p_comm,
2877 td->td_ucred ? td->td_ucred->cr_uid : -1,
2879 sig & WCOREFLAG ? " (core dumped)" : "");
2882 exit1(td, W_EXITCODE(0, sig));
2887 * Send queued SIGCHLD to parent when child process's state
2891 sigparent(struct proc *p, int reason, int status)
2893 PROC_LOCK_ASSERT(p, MA_OWNED);
2894 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2896 if (p->p_ksi != NULL) {
2897 p->p_ksi->ksi_signo = SIGCHLD;
2898 p->p_ksi->ksi_code = reason;
2899 p->p_ksi->ksi_status = status;
2900 p->p_ksi->ksi_pid = p->p_pid;
2901 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2902 if (KSI_ONQ(p->p_ksi))
2905 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2909 childproc_jobstate(struct proc *p, int reason, int status)
2913 PROC_LOCK_ASSERT(p, MA_OWNED);
2914 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2917 * Wake up parent sleeping in kern_wait(), also send
2918 * SIGCHLD to parent, but SIGCHLD does not guarantee
2919 * that parent will awake, because parent may masked
2922 p->p_pptr->p_flag |= P_STATCHILD;
2925 ps = p->p_pptr->p_sigacts;
2926 mtx_lock(&ps->ps_mtx);
2927 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2928 mtx_unlock(&ps->ps_mtx);
2929 sigparent(p, reason, status);
2931 mtx_unlock(&ps->ps_mtx);
2935 childproc_stopped(struct proc *p, int reason)
2937 childproc_jobstate(p, reason, p->p_xstat);
2941 childproc_continued(struct proc *p)
2943 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2947 childproc_exited(struct proc *p)
2950 int status = p->p_xstat; /* convert to int */
2952 reason = CLD_EXITED;
2953 if (WCOREDUMP(status))
2954 reason = CLD_DUMPED;
2955 else if (WIFSIGNALED(status))
2956 reason = CLD_KILLED;
2958 * XXX avoid calling wakeup(p->p_pptr), the work is
2961 sigparent(p, reason, status);
2964 static char corefilename[MAXPATHLEN] = {"%N.core"};
2965 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2966 sizeof(corefilename), "process corefile name format string");
2969 * expand_name(name, uid, pid)
2970 * Expand the name described in corefilename, using name, uid, and pid.
2971 * corefilename is a printf-like string, with three format specifiers:
2972 * %N name of process ("name")
2973 * %P process id (pid)
2975 * For example, "%N.core" is the default; they can be disabled completely
2976 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2977 * This is controlled by the sysctl variable kern.corefile (see above).
2980 expand_name(name, uid, pid)
2990 format = corefilename;
2991 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2994 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
2995 for (i = 0; format[i]; i++) {
2996 switch (format[i]) {
2997 case '%': /* Format character */
2999 switch (format[i]) {
3001 sbuf_putc(&sb, '%');
3003 case 'N': /* process name */
3004 sbuf_printf(&sb, "%s", name);
3006 case 'P': /* process id */
3007 sbuf_printf(&sb, "%u", pid);
3009 case 'U': /* user id */
3010 sbuf_printf(&sb, "%u", uid);
3014 "Unknown format character %c in "
3015 "corename `%s'\n", format[i], format);
3019 sbuf_putc(&sb, format[i]);
3022 if (sbuf_overflowed(&sb)) {
3024 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3025 "long\n", (long)pid, name, (u_long)uid);
3035 * Dump a process' core. The main routine does some
3036 * policy checking, and creates the name of the coredump;
3037 * then it passes on a vnode and a size limit to the process-specific
3038 * coredump routine if there is one; if there _is not_ one, it returns
3039 * ENOSYS; otherwise it returns the error from the process-specific routine.
3043 coredump(struct thread *td)
3045 struct proc *p = td->td_proc;
3046 register struct vnode *vp;
3047 register struct ucred *cred = td->td_ucred;
3049 struct nameidata nd;
3051 int error, error1, flags, locked;
3053 char *name; /* name of corefile */
3057 PROC_LOCK_ASSERT(p, MA_OWNED);
3058 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3059 _STOPEVENT(p, S_CORE, 0);
3061 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
3065 audit_proc_coredump(td, NULL, EINVAL);
3069 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3072 audit_proc_coredump(td, name, EFAULT);
3079 * Note that the bulk of limit checking is done after
3080 * the corefile is created. The exception is if the limit
3081 * for corefiles is 0, in which case we don't bother
3082 * creating the corefile at all. This layout means that
3083 * a corefile is truncated instead of not being created,
3084 * if it is larger than the limit.
3086 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3090 audit_proc_coredump(td, name, EFBIG);
3097 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
3098 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3099 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, NULL);
3102 audit_proc_coredump(td, name, error);
3107 vfslocked = NDHASGIANT(&nd);
3108 NDFREE(&nd, NDF_ONLY_PNBUF);
3111 /* Don't dump to non-regular files or files with links. */
3112 if (vp->v_type != VREG ||
3113 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
3114 VOP_UNLOCK(vp, 0, td);
3119 VOP_UNLOCK(vp, 0, td);
3120 lf.l_whence = SEEK_SET;
3123 lf.l_type = F_WRLCK;
3124 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3126 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3127 lf.l_type = F_UNLCK;
3129 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3130 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3132 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3134 VFS_UNLOCK_GIANT(vfslocked);
3140 if (set_core_nodump_flag)
3141 vattr.va_flags = UF_NODUMP;
3142 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3143 VOP_LEASE(vp, td, cred, LEASE_WRITE);
3144 VOP_SETATTR(vp, &vattr, cred, td);
3145 VOP_UNLOCK(vp, 0, td);
3146 vn_finished_write(mp);
3148 p->p_acflag |= ACORE;
3151 error = p->p_sysent->sv_coredump ?
3152 p->p_sysent->sv_coredump(td, vp, limit) :
3156 lf.l_type = F_UNLCK;
3157 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3160 error1 = vn_close(vp, FWRITE, cred, td);
3165 audit_proc_coredump(td, name, error);
3168 VFS_UNLOCK_GIANT(vfslocked);
3173 * Nonexistent system call-- signal process (may want to handle it). Flag
3174 * error in case process won't see signal immediately (blocked or ignored).
3176 #ifndef _SYS_SYSPROTO_H_
3185 struct nosys_args *args;
3187 struct proc *p = td->td_proc;
3196 * Send a SIGIO or SIGURG signal to a process or process group using stored
3197 * credentials rather than those of the current process.
3200 pgsigio(sigiop, sig, checkctty)
3201 struct sigio **sigiop;
3204 struct sigio *sigio;
3208 if (sigio == NULL) {
3212 if (sigio->sio_pgid > 0) {
3213 PROC_LOCK(sigio->sio_proc);
3214 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3215 psignal(sigio->sio_proc, sig);
3216 PROC_UNLOCK(sigio->sio_proc);
3217 } else if (sigio->sio_pgid < 0) {
3220 PGRP_LOCK(sigio->sio_pgrp);
3221 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3223 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3224 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3228 PGRP_UNLOCK(sigio->sio_pgrp);
3234 filt_sigattach(struct knote *kn)
3236 struct proc *p = curproc;
3238 kn->kn_ptr.p_proc = p;
3239 kn->kn_flags |= EV_CLEAR; /* automatically set */
3241 knlist_add(&p->p_klist, kn, 0);
3247 filt_sigdetach(struct knote *kn)
3249 struct proc *p = kn->kn_ptr.p_proc;
3251 knlist_remove(&p->p_klist, kn, 0);
3255 * signal knotes are shared with proc knotes, so we apply a mask to
3256 * the hint in order to differentiate them from process hints. This
3257 * could be avoided by using a signal-specific knote list, but probably
3258 * isn't worth the trouble.
3261 filt_signal(struct knote *kn, long hint)
3264 if (hint & NOTE_SIGNAL) {
3265 hint &= ~NOTE_SIGNAL;
3267 if (kn->kn_id == hint)
3270 return (kn->kn_data != 0);
3278 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3280 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3285 sigacts_free(struct sigacts *ps)
3288 mtx_lock(&ps->ps_mtx);
3290 if (ps->ps_refcnt == 0) {
3291 mtx_destroy(&ps->ps_mtx);
3292 free(ps, M_SUBPROC);
3294 mtx_unlock(&ps->ps_mtx);
3298 sigacts_hold(struct sigacts *ps)
3300 mtx_lock(&ps->ps_mtx);
3302 mtx_unlock(&ps->ps_mtx);
3307 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3310 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3311 mtx_lock(&src->ps_mtx);
3312 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3313 mtx_unlock(&src->ps_mtx);
3317 sigacts_shared(struct sigacts *ps)
3321 mtx_lock(&ps->ps_mtx);
3322 shared = ps->ps_refcnt > 1;
3323 mtx_unlock(&ps->ps_mtx);