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/pioctl.h>
61 #include <sys/resourcevar.h>
62 #include <sys/sched.h>
63 #include <sys/sleepqueue.h>
67 #include <sys/syscallsubr.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
70 #include <sys/syslog.h>
71 #include <sys/sysproto.h>
72 #include <sys/timers.h>
73 #include <sys/unistd.h>
76 #include <vm/vm_extern.h>
79 #include <posix4/posix4.h>
80 #include <machine/cpu.h>
82 #if defined (__alpha__) && !defined(COMPAT_43)
83 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
86 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
88 static int coredump(struct thread *);
89 static char *expand_name(const char *, uid_t, pid_t);
90 static int killpg1(struct thread *td, int sig, int pgid, int all);
91 static int issignal(struct thread *p);
92 static int sigprop(int sig);
93 static void tdsigwakeup(struct thread *, int, sig_t);
94 static void sig_suspend_threads(struct thread *, struct proc *, int);
95 static int filt_sigattach(struct knote *kn);
96 static void filt_sigdetach(struct knote *kn);
97 static int filt_signal(struct knote *kn, long hint);
98 static struct thread *sigtd(struct proc *p, int sig, int prop);
99 static int kern_sigtimedwait(struct thread *, sigset_t,
100 ksiginfo_t *, struct timespec *);
101 static int do_tdsignal(struct proc *, struct thread *, int, ksiginfo_t *);
102 static void sigqueue_start(void);
104 static uma_zone_t ksiginfo_zone = NULL;
105 struct filterops sig_filtops =
106 { 0, filt_sigattach, filt_sigdetach, filt_signal };
108 static int kern_logsigexit = 1;
109 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
111 "Log processes quitting on abnormal signals to syslog(3)");
113 static int kern_forcesigexit = 1;
114 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
115 &kern_forcesigexit, 0, "Force trap signal to be handled");
117 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
119 static int max_pending_per_proc = 128;
120 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
121 &max_pending_per_proc, 0, "Max pending signals per proc");
123 static int preallocate_siginfo = 1024;
124 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
125 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
126 &preallocate_siginfo, 0, "Preallocated signal memory size");
128 static int signal_overflow = 0;
129 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
130 &signal_overflow, 0, "Number of signals overflew");
132 static int signal_alloc_fail = 0;
133 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
134 &signal_alloc_fail, 0, "signals failed to be allocated");
136 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
139 * Policy -- Can ucred cr1 send SIGIO to process cr2?
140 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
141 * in the right situations.
143 #define CANSIGIO(cr1, cr2) \
144 ((cr1)->cr_uid == 0 || \
145 (cr1)->cr_ruid == (cr2)->cr_ruid || \
146 (cr1)->cr_uid == (cr2)->cr_ruid || \
147 (cr1)->cr_ruid == (cr2)->cr_uid || \
148 (cr1)->cr_uid == (cr2)->cr_uid)
151 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
152 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
154 static int do_coredump = 1;
155 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
156 &do_coredump, 0, "Enable/Disable coredumps");
158 static int set_core_nodump_flag = 0;
159 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
160 0, "Enable setting the NODUMP flag on coredump files");
163 * Signal properties and actions.
164 * The array below categorizes the signals and their default actions
165 * according to the following properties:
167 #define SA_KILL 0x01 /* terminates process by default */
168 #define SA_CORE 0x02 /* ditto and coredumps */
169 #define SA_STOP 0x04 /* suspend process */
170 #define SA_TTYSTOP 0x08 /* ditto, from tty */
171 #define SA_IGNORE 0x10 /* ignore by default */
172 #define SA_CONT 0x20 /* continue if suspended */
173 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
174 #define SA_PROC 0x80 /* deliverable to any thread */
176 static int sigproptbl[NSIG] = {
177 SA_KILL|SA_PROC, /* SIGHUP */
178 SA_KILL|SA_PROC, /* SIGINT */
179 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
180 SA_KILL|SA_CORE, /* SIGILL */
181 SA_KILL|SA_CORE, /* SIGTRAP */
182 SA_KILL|SA_CORE, /* SIGABRT */
183 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
184 SA_KILL|SA_CORE, /* SIGFPE */
185 SA_KILL|SA_PROC, /* SIGKILL */
186 SA_KILL|SA_CORE, /* SIGBUS */
187 SA_KILL|SA_CORE, /* SIGSEGV */
188 SA_KILL|SA_CORE, /* SIGSYS */
189 SA_KILL|SA_PROC, /* SIGPIPE */
190 SA_KILL|SA_PROC, /* SIGALRM */
191 SA_KILL|SA_PROC, /* SIGTERM */
192 SA_IGNORE|SA_PROC, /* SIGURG */
193 SA_STOP|SA_PROC, /* SIGSTOP */
194 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
195 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
196 SA_IGNORE|SA_PROC, /* SIGCHLD */
197 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
198 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
199 SA_IGNORE|SA_PROC, /* SIGIO */
200 SA_KILL, /* SIGXCPU */
201 SA_KILL, /* SIGXFSZ */
202 SA_KILL|SA_PROC, /* SIGVTALRM */
203 SA_KILL|SA_PROC, /* SIGPROF */
204 SA_IGNORE|SA_PROC, /* SIGWINCH */
205 SA_IGNORE|SA_PROC, /* SIGINFO */
206 SA_KILL|SA_PROC, /* SIGUSR1 */
207 SA_KILL|SA_PROC, /* SIGUSR2 */
213 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
214 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
215 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
216 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
217 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
218 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
222 ksiginfo_alloc(int wait)
229 if (ksiginfo_zone != NULL)
230 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
235 ksiginfo_free(ksiginfo_t *ksi)
237 uma_zfree(ksiginfo_zone, ksi);
241 ksiginfo_tryfree(ksiginfo_t *ksi)
243 if (!(ksi->ksi_flags & KSI_EXT)) {
244 uma_zfree(ksiginfo_zone, ksi);
251 sigqueue_init(sigqueue_t *list, struct proc *p)
253 SIGEMPTYSET(list->sq_signals);
254 TAILQ_INIT(&list->sq_list);
256 list->sq_flags = SQ_INIT;
260 * Get a signal's ksiginfo.
262 * 0 - signal not found
263 * others - signal number
266 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
268 struct proc *p = sq->sq_proc;
269 struct ksiginfo *ksi, *next;
272 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
274 if (!SIGISMEMBER(sq->sq_signals, signo))
277 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
278 next = TAILQ_NEXT(ksi, ksi_link);
279 if (ksi->ksi_signo == signo) {
281 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
282 ksi->ksi_sigq = NULL;
283 ksiginfo_copy(ksi, si);
284 if (ksiginfo_tryfree(ksi) && p != NULL)
292 SIGDELSET(sq->sq_signals, signo);
293 si->ksi_signo = signo;
298 sigqueue_take(ksiginfo_t *ksi)
304 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
308 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
309 ksi->ksi_sigq = NULL;
310 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
313 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
314 kp = TAILQ_NEXT(kp, ksi_link)) {
315 if (kp->ksi_signo == ksi->ksi_signo)
319 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
323 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
325 struct proc *p = sq->sq_proc;
326 struct ksiginfo *ksi;
329 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
331 if (signo == SIGKILL || signo == SIGSTOP || si == NULL)
334 /* directly insert the ksi, don't copy it */
335 if (si->ksi_flags & KSI_INS) {
336 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
341 if (__predict_false(ksiginfo_zone == NULL))
344 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
347 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
353 ksiginfo_copy(si, ksi);
354 ksi->ksi_signo = signo;
355 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
359 if ((si->ksi_flags & KSI_TRAP) != 0) {
368 SIGADDSET(sq->sq_signals, signo);
373 sigqueue_flush(sigqueue_t *sq)
375 struct proc *p = sq->sq_proc;
378 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
381 PROC_LOCK_ASSERT(p, MA_OWNED);
383 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
384 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
385 ksi->ksi_sigq = NULL;
386 if (ksiginfo_tryfree(ksi) && p != NULL)
390 SIGEMPTYSET(sq->sq_signals);
394 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
398 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
400 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
401 SIGADDSET(*set, ksi->ksi_signo);
405 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
408 struct proc *p1, *p2;
409 ksiginfo_t *ksi, *next;
411 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
412 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
414 * make a copy, this allows setp to point to src or dst
415 * sq_signals without trouble.
420 /* Move siginfo to target list */
421 for (ksi = TAILQ_FIRST(&src->sq_list); ksi != NULL; ksi = next) {
422 next = TAILQ_NEXT(ksi, ksi_link);
423 if (SIGISMEMBER(set, ksi->ksi_signo)) {
424 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
427 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
434 /* Move pending bits to target list */
435 tmp = src->sq_signals;
437 SIGSETOR(dst->sq_signals, tmp);
438 SIGSETNAND(src->sq_signals, tmp);
440 /* Finally, rescan src queue and set pending bits for it */
441 sigqueue_collect_set(src, &src->sq_signals);
445 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
450 SIGADDSET(set, signo);
451 sigqueue_move_set(src, dst, &set);
455 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
457 struct proc *p = sq->sq_proc;
458 ksiginfo_t *ksi, *next;
460 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
462 /* Remove siginfo queue */
463 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
464 next = TAILQ_NEXT(ksi, ksi_link);
465 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
466 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
467 ksi->ksi_sigq = NULL;
468 if (ksiginfo_tryfree(ksi) && p != NULL)
472 SIGSETNAND(sq->sq_signals, *set);
473 /* Finally, rescan queue and set pending bits for it */
474 sigqueue_collect_set(sq, &sq->sq_signals);
478 sigqueue_delete(sigqueue_t *sq, int signo)
483 SIGADDSET(set, signo);
484 sigqueue_delete_set(sq, &set);
487 /* Remove a set of signals for a process */
489 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
494 PROC_LOCK_ASSERT(p, MA_OWNED);
496 sigqueue_init(&worklist, NULL);
497 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
499 mtx_lock_spin(&sched_lock);
500 FOREACH_THREAD_IN_PROC(p, td0)
501 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
502 mtx_unlock_spin(&sched_lock);
504 sigqueue_flush(&worklist);
508 sigqueue_delete_proc(struct proc *p, int signo)
513 SIGADDSET(set, signo);
514 sigqueue_delete_set_proc(p, &set);
518 sigqueue_delete_stopmask_proc(struct proc *p)
523 SIGADDSET(set, SIGSTOP);
524 SIGADDSET(set, SIGTSTP);
525 SIGADDSET(set, SIGTTIN);
526 SIGADDSET(set, SIGTTOU);
527 sigqueue_delete_set_proc(p, &set);
531 * Determine signal that should be delivered to process p, the current
532 * process, 0 if none. If there is a pending stop signal with default
533 * action, the process stops in issignal().
538 cursig(struct thread *td)
540 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
541 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
542 mtx_assert(&sched_lock, MA_NOTOWNED);
543 return (SIGPENDING(td) ? issignal(td) : 0);
547 * Arrange for ast() to handle unmasked pending signals on return to user
548 * mode. This must be called whenever a signal is added to td_sigqueue or
549 * unmasked in td_sigmask.
552 signotify(struct thread *td)
559 PROC_LOCK_ASSERT(p, MA_OWNED);
562 * If our mask changed we may have to move signal that were
563 * previously masked by all threads to our sigqueue.
565 set = p->p_sigqueue.sq_signals;
566 if (p->p_flag & P_SA)
567 saved = p->p_sigqueue.sq_signals;
568 SIGSETNAND(set, td->td_sigmask);
569 if (! SIGISEMPTY(set))
570 sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set);
571 if (SIGPENDING(td)) {
572 mtx_lock_spin(&sched_lock);
573 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
574 mtx_unlock_spin(&sched_lock);
576 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
577 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
578 /* pending set changed */
579 p->p_flag |= P_SIGEVENT;
580 wakeup(&p->p_siglist);
586 sigonstack(size_t sp)
588 struct thread *td = curthread;
590 return ((td->td_pflags & TDP_ALTSTACK) ?
591 #if defined(COMPAT_43)
592 ((td->td_sigstk.ss_size == 0) ?
593 (td->td_sigstk.ss_flags & SS_ONSTACK) :
594 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
596 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
605 if (sig > 0 && sig < NSIG)
606 return (sigproptbl[_SIG_IDX(sig)]);
611 sig_ffs(sigset_t *set)
615 for (i = 0; i < _SIG_WORDS; i++)
617 return (ffs(set->__bits[i]) + (i * 32));
630 kern_sigaction(td, sig, act, oact, flags)
633 struct sigaction *act, *oact;
637 struct proc *p = td->td_proc;
639 if (!_SIG_VALID(sig))
644 mtx_lock(&ps->ps_mtx);
646 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
647 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
649 if (SIGISMEMBER(ps->ps_sigonstack, sig))
650 oact->sa_flags |= SA_ONSTACK;
651 if (!SIGISMEMBER(ps->ps_sigintr, sig))
652 oact->sa_flags |= SA_RESTART;
653 if (SIGISMEMBER(ps->ps_sigreset, sig))
654 oact->sa_flags |= SA_RESETHAND;
655 if (SIGISMEMBER(ps->ps_signodefer, sig))
656 oact->sa_flags |= SA_NODEFER;
657 if (SIGISMEMBER(ps->ps_siginfo, sig))
658 oact->sa_flags |= SA_SIGINFO;
659 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
660 oact->sa_flags |= SA_NOCLDSTOP;
661 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
662 oact->sa_flags |= SA_NOCLDWAIT;
665 if ((sig == SIGKILL || sig == SIGSTOP) &&
666 act->sa_handler != SIG_DFL) {
667 mtx_unlock(&ps->ps_mtx);
673 * Change setting atomically.
676 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
677 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
678 if (act->sa_flags & SA_SIGINFO) {
679 ps->ps_sigact[_SIG_IDX(sig)] =
680 (__sighandler_t *)act->sa_sigaction;
681 SIGADDSET(ps->ps_siginfo, sig);
683 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
684 SIGDELSET(ps->ps_siginfo, sig);
686 if (!(act->sa_flags & SA_RESTART))
687 SIGADDSET(ps->ps_sigintr, sig);
689 SIGDELSET(ps->ps_sigintr, sig);
690 if (act->sa_flags & SA_ONSTACK)
691 SIGADDSET(ps->ps_sigonstack, sig);
693 SIGDELSET(ps->ps_sigonstack, sig);
694 if (act->sa_flags & SA_RESETHAND)
695 SIGADDSET(ps->ps_sigreset, sig);
697 SIGDELSET(ps->ps_sigreset, sig);
698 if (act->sa_flags & SA_NODEFER)
699 SIGADDSET(ps->ps_signodefer, sig);
701 SIGDELSET(ps->ps_signodefer, sig);
702 if (sig == SIGCHLD) {
703 if (act->sa_flags & SA_NOCLDSTOP)
704 ps->ps_flag |= PS_NOCLDSTOP;
706 ps->ps_flag &= ~PS_NOCLDSTOP;
707 if (act->sa_flags & SA_NOCLDWAIT) {
709 * Paranoia: since SA_NOCLDWAIT is implemented
710 * by reparenting the dying child to PID 1 (and
711 * trust it to reap the zombie), PID 1 itself
712 * is forbidden to set SA_NOCLDWAIT.
715 ps->ps_flag &= ~PS_NOCLDWAIT;
717 ps->ps_flag |= PS_NOCLDWAIT;
719 ps->ps_flag &= ~PS_NOCLDWAIT;
720 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
721 ps->ps_flag |= PS_CLDSIGIGN;
723 ps->ps_flag &= ~PS_CLDSIGIGN;
726 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
727 * and for signals set to SIG_DFL where the default is to
728 * ignore. However, don't put SIGCONT in ps_sigignore, as we
729 * have to restart the process.
731 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
732 (sigprop(sig) & SA_IGNORE &&
733 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
734 if ((p->p_flag & P_SA) &&
735 SIGISMEMBER(p->p_sigqueue.sq_signals, sig)) {
736 p->p_flag |= P_SIGEVENT;
737 wakeup(&p->p_siglist);
739 /* never to be seen again */
740 sigqueue_delete_proc(p, sig);
742 /* easier in psignal */
743 SIGADDSET(ps->ps_sigignore, sig);
744 SIGDELSET(ps->ps_sigcatch, sig);
746 SIGDELSET(ps->ps_sigignore, sig);
747 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
748 SIGDELSET(ps->ps_sigcatch, sig);
750 SIGADDSET(ps->ps_sigcatch, sig);
752 #ifdef COMPAT_FREEBSD4
753 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
754 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
755 (flags & KSA_FREEBSD4) == 0)
756 SIGDELSET(ps->ps_freebsd4, sig);
758 SIGADDSET(ps->ps_freebsd4, sig);
761 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
762 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
763 (flags & KSA_OSIGSET) == 0)
764 SIGDELSET(ps->ps_osigset, sig);
766 SIGADDSET(ps->ps_osigset, sig);
769 mtx_unlock(&ps->ps_mtx);
774 #ifndef _SYS_SYSPROTO_H_
775 struct sigaction_args {
777 struct sigaction *act;
778 struct sigaction *oact;
787 register struct sigaction_args *uap;
789 struct sigaction act, oact;
790 register struct sigaction *actp, *oactp;
793 actp = (uap->act != NULL) ? &act : NULL;
794 oactp = (uap->oact != NULL) ? &oact : NULL;
796 error = copyin(uap->act, actp, sizeof(act));
800 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
802 error = copyout(oactp, uap->oact, sizeof(oact));
806 #ifdef COMPAT_FREEBSD4
807 #ifndef _SYS_SYSPROTO_H_
808 struct freebsd4_sigaction_args {
810 struct sigaction *act;
811 struct sigaction *oact;
818 freebsd4_sigaction(td, uap)
820 register struct freebsd4_sigaction_args *uap;
822 struct sigaction act, oact;
823 register struct sigaction *actp, *oactp;
827 actp = (uap->act != NULL) ? &act : NULL;
828 oactp = (uap->oact != NULL) ? &oact : NULL;
830 error = copyin(uap->act, actp, sizeof(act));
834 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
836 error = copyout(oactp, uap->oact, sizeof(oact));
839 #endif /* COMAPT_FREEBSD4 */
841 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
842 #ifndef _SYS_SYSPROTO_H_
843 struct osigaction_args {
845 struct osigaction *nsa;
846 struct osigaction *osa;
855 register struct osigaction_args *uap;
857 struct osigaction sa;
858 struct sigaction nsa, osa;
859 register struct sigaction *nsap, *osap;
862 if (uap->signum <= 0 || uap->signum >= ONSIG)
865 nsap = (uap->nsa != NULL) ? &nsa : NULL;
866 osap = (uap->osa != NULL) ? &osa : NULL;
869 error = copyin(uap->nsa, &sa, sizeof(sa));
872 nsap->sa_handler = sa.sa_handler;
873 nsap->sa_flags = sa.sa_flags;
874 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
876 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
877 if (osap && !error) {
878 sa.sa_handler = osap->sa_handler;
879 sa.sa_flags = osap->sa_flags;
880 SIG2OSIG(osap->sa_mask, sa.sa_mask);
881 error = copyout(&sa, uap->osa, sizeof(sa));
886 #if !defined(__i386__) && !defined(__alpha__)
887 /* Avoid replicating the same stub everywhere */
891 struct osigreturn_args *uap;
894 return (nosys(td, (struct nosys_args *)uap));
897 #endif /* COMPAT_43 */
900 * Initialize signal state for process 0;
901 * set to ignore signals that are ignored by default.
912 mtx_lock(&ps->ps_mtx);
913 for (i = 1; i <= NSIG; i++)
914 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
915 SIGADDSET(ps->ps_sigignore, i);
916 mtx_unlock(&ps->ps_mtx);
921 * Reset signals for an exec of the specified process.
924 execsigs(struct proc *p)
931 * Reset caught signals. Held signals remain held
932 * through td_sigmask (unless they were caught,
933 * and are now ignored by default).
935 PROC_LOCK_ASSERT(p, MA_OWNED);
936 td = FIRST_THREAD_IN_PROC(p);
938 mtx_lock(&ps->ps_mtx);
939 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
940 sig = sig_ffs(&ps->ps_sigcatch);
941 SIGDELSET(ps->ps_sigcatch, sig);
942 if (sigprop(sig) & SA_IGNORE) {
944 SIGADDSET(ps->ps_sigignore, sig);
945 sigqueue_delete_proc(p, sig);
947 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
950 * Reset stack state to the user stack.
951 * Clear set of signals caught on the signal stack.
953 td->td_sigstk.ss_flags = SS_DISABLE;
954 td->td_sigstk.ss_size = 0;
955 td->td_sigstk.ss_sp = 0;
956 td->td_pflags &= ~TDP_ALTSTACK;
958 * Reset no zombies if child dies flag as Solaris does.
960 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
961 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
962 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
963 mtx_unlock(&ps->ps_mtx);
969 * Manipulate signal mask.
972 kern_sigprocmask(td, how, set, oset, old)
975 sigset_t *set, *oset;
980 PROC_LOCK(td->td_proc);
982 *oset = td->td_sigmask;
989 SIGSETOR(td->td_sigmask, *set);
992 SIGSETNAND(td->td_sigmask, *set);
998 SIGSETLO(td->td_sigmask, *set);
1000 td->td_sigmask = *set;
1008 PROC_UNLOCK(td->td_proc);
1013 * sigprocmask() - MP SAFE
1016 #ifndef _SYS_SYSPROTO_H_
1017 struct sigprocmask_args {
1019 const sigset_t *set;
1024 sigprocmask(td, uap)
1025 register struct thread *td;
1026 struct sigprocmask_args *uap;
1029 sigset_t *setp, *osetp;
1032 setp = (uap->set != NULL) ? &set : NULL;
1033 osetp = (uap->oset != NULL) ? &oset : NULL;
1035 error = copyin(uap->set, setp, sizeof(set));
1039 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1040 if (osetp && !error) {
1041 error = copyout(osetp, uap->oset, sizeof(oset));
1046 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1048 * osigprocmask() - MP SAFE
1050 #ifndef _SYS_SYSPROTO_H_
1051 struct osigprocmask_args {
1057 osigprocmask(td, uap)
1058 register struct thread *td;
1059 struct osigprocmask_args *uap;
1064 OSIG2SIG(uap->mask, set);
1065 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1066 SIG2OSIG(oset, td->td_retval[0]);
1069 #endif /* COMPAT_43 */
1075 sigwait(struct thread *td, struct sigwait_args *uap)
1081 error = copyin(uap->set, &set, sizeof(set));
1083 td->td_retval[0] = error;
1087 error = kern_sigtimedwait(td, set, &ksi, NULL);
1089 if (error == ERESTART)
1091 td->td_retval[0] = error;
1095 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1096 td->td_retval[0] = error;
1103 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1106 struct timespec *timeout;
1112 error = copyin(uap->timeout, &ts, sizeof(ts));
1120 error = copyin(uap->set, &set, sizeof(set));
1124 error = kern_sigtimedwait(td, set, &ksi, timeout);
1129 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1132 td->td_retval[0] = ksi.ksi_signo;
1140 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1146 error = copyin(uap->set, &set, sizeof(set));
1150 error = kern_sigtimedwait(td, set, &ksi, NULL);
1155 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1158 td->td_retval[0] = ksi.ksi_signo;
1163 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1164 struct timespec *timeout)
1169 int error, sig, hz, i, timevalid = 0;
1170 struct timespec rts, ets, ts;
1176 SIG_CANTMASK(waitset);
1180 savedmask = td->td_sigmask;
1182 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1184 getnanouptime(&rts);
1186 timespecadd(&ets, timeout);
1191 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1192 if (!SIGISMEMBER(waitset, i))
1194 if (SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1195 SIGFILLSET(td->td_sigmask);
1196 SIG_CANTMASK(td->td_sigmask);
1197 SIGDELSET(td->td_sigmask, i);
1198 mtx_lock(&ps->ps_mtx);
1201 mtx_unlock(&ps->ps_mtx);
1202 } else if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
1203 if (p->p_flag & P_SA) {
1204 p->p_flag |= P_SIGEVENT;
1205 wakeup(&p->p_siglist);
1207 sigqueue_move(&p->p_sigqueue, &td->td_sigqueue, i);
1208 SIGFILLSET(td->td_sigmask);
1209 SIG_CANTMASK(td->td_sigmask);
1210 SIGDELSET(td->td_sigmask, i);
1211 mtx_lock(&ps->ps_mtx);
1214 mtx_unlock(&ps->ps_mtx);
1223 * POSIX says this must be checked after looking for pending
1231 getnanouptime(&rts);
1232 if (timespeccmp(&rts, &ets, >=)) {
1237 timespecsub(&ts, &rts);
1238 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1243 td->td_sigmask = savedmask;
1244 SIGSETNAND(td->td_sigmask, waitset);
1246 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1248 if (error == ERESTART) {
1249 /* timeout can not be restarted. */
1251 } else if (error == EAGAIN) {
1252 /* will calculate timeout by ourself. */
1263 sigqueue_get(&td->td_sigqueue, sig, ksi);
1264 ksi->ksi_signo = sig;
1265 if (ksi->ksi_code == SI_TIMER)
1266 itimer_accept(p, ksi->ksi_timerid, ksi);
1268 mtx_lock(&ps->ps_mtx);
1269 action = ps->ps_sigact[_SIG_IDX(sig)];
1270 mtx_unlock(&ps->ps_mtx);
1272 if (KTRPOINT(td, KTR_PSIG))
1273 ktrpsig(sig, action, &td->td_sigmask, 0);
1275 _STOPEVENT(p, S_SIG, sig);
1278 td->td_sigmask = savedmask;
1284 #ifndef _SYS_SYSPROTO_H_
1285 struct sigpending_args {
1295 struct sigpending_args *uap;
1297 struct proc *p = td->td_proc;
1301 pending = p->p_sigqueue.sq_signals;
1302 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1304 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1307 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1308 #ifndef _SYS_SYSPROTO_H_
1309 struct osigpending_args {
1317 osigpending(td, uap)
1319 struct osigpending_args *uap;
1321 struct proc *p = td->td_proc;
1325 pending = p->p_sigqueue.sq_signals;
1326 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1328 SIG2OSIG(pending, td->td_retval[0]);
1331 #endif /* COMPAT_43 */
1333 #if defined(COMPAT_43)
1335 * Generalized interface signal handler, 4.3-compatible.
1337 #ifndef _SYS_SYSPROTO_H_
1338 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 {
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 {
1417 osigsetmask(td, uap)
1419 struct osigsetmask_args *uap;
1421 struct proc *p = td->td_proc;
1424 OSIG2SIG(uap->mask, set);
1427 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1428 SIGSETLO(td->td_sigmask, set);
1433 #endif /* COMPAT_43 */
1436 * Suspend calling thread until signal, providing mask to be set
1439 #ifndef _SYS_SYSPROTO_H_
1440 struct sigsuspend_args {
1441 const sigset_t *sigmask;
1451 struct sigsuspend_args *uap;
1456 error = copyin(uap->sigmask, &mask, sizeof(mask));
1459 return (kern_sigsuspend(td, mask));
1463 kern_sigsuspend(struct thread *td, sigset_t mask)
1465 struct proc *p = td->td_proc;
1468 * When returning from sigsuspend, we want
1469 * the old mask to be restored after the
1470 * signal handler has finished. Thus, we
1471 * save it here and mark the sigacts structure
1475 td->td_oldsigmask = td->td_sigmask;
1476 td->td_pflags |= TDP_OLDMASK;
1478 td->td_sigmask = mask;
1480 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1483 /* always return EINTR rather than ERESTART... */
1487 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1489 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1490 * convention: libc stub passes mask, not pointer, to save a copyin.
1492 #ifndef _SYS_SYSPROTO_H_
1493 struct osigsuspend_args {
1502 osigsuspend(td, uap)
1504 struct osigsuspend_args *uap;
1506 struct proc *p = td->td_proc;
1510 td->td_oldsigmask = td->td_sigmask;
1511 td->td_pflags |= TDP_OLDMASK;
1512 OSIG2SIG(uap->mask, mask);
1514 SIGSETLO(td->td_sigmask, mask);
1516 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1519 /* always return EINTR rather than ERESTART... */
1522 #endif /* COMPAT_43 */
1524 #if defined(COMPAT_43)
1525 #ifndef _SYS_SYSPROTO_H_
1526 struct osigstack_args {
1527 struct sigstack *nss;
1528 struct sigstack *oss;
1538 register struct osigstack_args *uap;
1540 struct sigstack nss, oss;
1543 if (uap->nss != NULL) {
1544 error = copyin(uap->nss, &nss, sizeof(nss));
1548 oss.ss_sp = td->td_sigstk.ss_sp;
1549 oss.ss_onstack = sigonstack(cpu_getstack(td));
1550 if (uap->nss != NULL) {
1551 td->td_sigstk.ss_sp = nss.ss_sp;
1552 td->td_sigstk.ss_size = 0;
1553 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1554 td->td_pflags |= TDP_ALTSTACK;
1556 if (uap->oss != NULL)
1557 error = copyout(&oss, uap->oss, sizeof(oss));
1561 #endif /* COMPAT_43 */
1563 #ifndef _SYS_SYSPROTO_H_
1564 struct sigaltstack_args {
1574 sigaltstack(td, uap)
1576 register struct sigaltstack_args *uap;
1581 if (uap->ss != NULL) {
1582 error = copyin(uap->ss, &ss, sizeof(ss));
1586 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1587 (uap->oss != NULL) ? &oss : NULL);
1590 if (uap->oss != NULL)
1591 error = copyout(&oss, uap->oss, sizeof(stack_t));
1596 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1598 struct proc *p = td->td_proc;
1601 oonstack = sigonstack(cpu_getstack(td));
1604 *oss = td->td_sigstk;
1605 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1606 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1612 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1614 if (!(ss->ss_flags & SS_DISABLE)) {
1615 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1618 td->td_sigstk = *ss;
1619 td->td_pflags |= TDP_ALTSTACK;
1621 td->td_pflags &= ~TDP_ALTSTACK;
1628 * Common code for kill process group/broadcast kill.
1629 * cp is calling process.
1632 killpg1(td, sig, pgid, all)
1633 register struct thread *td;
1636 register struct proc *p;
1644 sx_slock(&allproc_lock);
1645 LIST_FOREACH(p, &allproc, p_list) {
1647 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1652 if (p_cansignal(td, p, sig) == 0) {
1659 sx_sunlock(&allproc_lock);
1661 sx_slock(&proctree_lock);
1664 * zero pgid means send to my process group.
1666 pgrp = td->td_proc->p_pgrp;
1669 pgrp = pgfind(pgid);
1671 sx_sunlock(&proctree_lock);
1675 sx_sunlock(&proctree_lock);
1676 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1678 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1682 if (p_cansignal(td, p, sig) == 0) {
1691 return (nfound ? 0 : ESRCH);
1694 #ifndef _SYS_SYSPROTO_H_
1706 register struct thread *td;
1707 register struct kill_args *uap;
1709 register struct proc *p;
1712 if ((u_int)uap->signum > _SIG_MAXSIG)
1716 /* kill single process */
1717 if ((p = pfind(uap->pid)) == NULL) {
1718 if ((p = zpfind(uap->pid)) == NULL)
1721 error = p_cansignal(td, p, uap->signum);
1722 if (error == 0 && uap->signum)
1723 psignal(p, uap->signum);
1728 case -1: /* broadcast signal */
1729 return (killpg1(td, uap->signum, 0, 1));
1730 case 0: /* signal own process group */
1731 return (killpg1(td, uap->signum, 0, 0));
1732 default: /* negative explicit process group */
1733 return (killpg1(td, uap->signum, -uap->pid, 0));
1738 #if defined(COMPAT_43)
1739 #ifndef _SYS_SYSPROTO_H_
1740 struct okillpg_args {
1752 register struct okillpg_args *uap;
1755 if ((u_int)uap->signum > _SIG_MAXSIG)
1758 return (killpg1(td, uap->signum, uap->pgid, 0));
1760 #endif /* COMPAT_43 */
1762 #ifndef _SYS_SYSPROTO_H_
1763 struct sigqueue_args {
1766 /* union sigval */ void *value;
1771 sigqueue(struct thread *td, struct sigqueue_args *uap)
1777 if ((u_int)uap->signum > _SIG_MAXSIG)
1781 * Specification says sigqueue can only send signal to
1787 if ((p = pfind(uap->pid)) == NULL) {
1788 if ((p = zpfind(uap->pid)) == NULL)
1791 error = p_cansignal(td, p, uap->signum);
1792 if (error == 0 && uap->signum != 0) {
1793 ksiginfo_init(&ksi);
1794 ksi.ksi_signo = uap->signum;
1795 ksi.ksi_code = SI_QUEUE;
1796 ksi.ksi_pid = td->td_proc->p_pid;
1797 ksi.ksi_uid = td->td_ucred->cr_ruid;
1798 ksi.ksi_value.sival_ptr = uap->value;
1799 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1806 * Send a signal to a process group.
1815 sx_slock(&proctree_lock);
1816 pgrp = pgfind(pgid);
1817 sx_sunlock(&proctree_lock);
1819 pgsignal(pgrp, sig, 0);
1826 * Send a signal to a process group. If checktty is 1,
1827 * limit to members which have a controlling terminal.
1830 pgsignal(pgrp, sig, checkctty)
1834 register struct proc *p;
1837 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1838 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1840 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1848 * Send a signal caused by a trap to the current thread.
1849 * If it will be caught immediately, deliver it with correct code.
1850 * Otherwise, post it normally.
1855 trapsignal(struct thread *td, ksiginfo_t *ksi)
1864 sig = ksi->ksi_signo;
1865 code = ksi->ksi_code;
1866 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1868 if (td->td_pflags & TDP_SA) {
1869 if (td->td_mailbox == NULL)
1870 thread_user_enter(td);
1872 SIGDELSET(td->td_sigmask, sig);
1873 mtx_lock_spin(&sched_lock);
1875 * Force scheduling an upcall, so UTS has chance to
1876 * process the signal before thread runs again in
1880 td->td_upcall->ku_flags |= KUF_DOUPCALL;
1881 mtx_unlock_spin(&sched_lock);
1886 mtx_lock(&ps->ps_mtx);
1887 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1888 !SIGISMEMBER(td->td_sigmask, sig)) {
1889 p->p_stats->p_ru.ru_nsignals++;
1891 if (KTRPOINT(curthread, KTR_PSIG))
1892 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1893 &td->td_sigmask, code);
1895 if (!(td->td_pflags & TDP_SA))
1896 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1897 ksi, &td->td_sigmask);
1898 else if (td->td_mailbox == NULL) {
1899 mtx_unlock(&ps->ps_mtx);
1900 /* UTS caused a sync signal */
1901 p->p_code = code; /* XXX for core dump/debugger */
1902 p->p_sig = sig; /* XXX to verify code */
1905 mtx_unlock(&ps->ps_mtx);
1906 SIGADDSET(td->td_sigmask, sig);
1908 error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
1911 /* UTS memory corrupted */
1913 sigexit(td, SIGSEGV);
1914 mtx_lock(&ps->ps_mtx);
1916 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1917 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1918 SIGADDSET(td->td_sigmask, sig);
1919 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1921 * See kern_sigaction() for origin of this code.
1923 SIGDELSET(ps->ps_sigcatch, sig);
1924 if (sig != SIGCONT &&
1925 sigprop(sig) & SA_IGNORE)
1926 SIGADDSET(ps->ps_sigignore, sig);
1927 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1929 mtx_unlock(&ps->ps_mtx);
1932 * Avoid a possible infinite loop if the thread
1933 * masking the signal or process is ignoring the
1936 if (kern_forcesigexit &&
1937 (SIGISMEMBER(td->td_sigmask, sig) ||
1938 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1939 SIGDELSET(td->td_sigmask, sig);
1940 SIGDELSET(ps->ps_sigcatch, sig);
1941 SIGDELSET(ps->ps_sigignore, sig);
1942 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1944 mtx_unlock(&ps->ps_mtx);
1945 p->p_code = code; /* XXX for core dump/debugger */
1946 p->p_sig = sig; /* XXX to verify code */
1947 tdsignal(p, td, sig, ksi);
1952 static struct thread *
1953 sigtd(struct proc *p, int sig, int prop)
1955 struct thread *td, *signal_td;
1957 PROC_LOCK_ASSERT(p, MA_OWNED);
1960 * Check if current thread can handle the signal without
1961 * switching conetxt to another thread.
1963 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1966 mtx_lock_spin(&sched_lock);
1967 FOREACH_THREAD_IN_PROC(p, td) {
1968 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1973 if (signal_td == NULL)
1974 signal_td = FIRST_THREAD_IN_PROC(p);
1975 mtx_unlock_spin(&sched_lock);
1980 * Send the signal to the process. If the signal has an action, the action
1981 * is usually performed by the target process rather than the caller; we add
1982 * the signal to the set of pending signals for the process.
1985 * o When a stop signal is sent to a sleeping process that takes the
1986 * default action, the process is stopped without awakening it.
1987 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1988 * regardless of the signal action (eg, blocked or ignored).
1990 * Other ignored signals are discarded immediately.
1995 psignal(struct proc *p, int sig)
1997 (void) tdsignal(p, NULL, sig, NULL);
2001 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
2003 struct thread *td = NULL;
2005 PROC_LOCK_ASSERT(p, MA_OWNED);
2007 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
2010 * ksi_code and other fields should be set before
2011 * calling this function.
2013 ksi->ksi_signo = sigev->sigev_signo;
2014 ksi->ksi_value = sigev->sigev_value;
2015 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2016 td = thread_find(p, sigev->sigev_notify_thread_id);
2020 return (tdsignal(p, td, ksi->ksi_signo, ksi));
2027 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2032 if (p->p_flag & P_SA)
2033 saved = p->p_sigqueue.sq_signals;
2034 ret = do_tdsignal(p, td, sig, ksi);
2035 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2036 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
2037 /* pending set changed */
2038 p->p_flag |= P_SIGEVENT;
2039 wakeup(&p->p_siglist);
2046 do_tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2049 sigqueue_t *sigqueue;
2054 PROC_LOCK_ASSERT(p, MA_OWNED);
2056 if (!_SIG_VALID(sig))
2057 panic("do_tdsignal(): invalid signal");
2059 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("do_tdsignal: ksi on queue"));
2062 * IEEE Std 1003.1-2001: return success when killing a zombie.
2064 if (p->p_state == PRS_ZOMBIE) {
2065 if (ksi && (ksi->ksi_flags & KSI_INS))
2066 ksiginfo_tryfree(ksi);
2071 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2072 prop = sigprop(sig);
2075 * If the signal is blocked and not destined for this thread, then
2076 * assign it to the process so that we can find it later in the first
2077 * thread that unblocks it. Otherwise, assign it to this thread now.
2080 td = sigtd(p, sig, prop);
2081 if (SIGISMEMBER(td->td_sigmask, sig))
2082 sigqueue = &p->p_sigqueue;
2084 sigqueue = &td->td_sigqueue;
2086 KASSERT(td->td_proc == p, ("invalid thread"));
2087 sigqueue = &td->td_sigqueue;
2091 * If the signal is being ignored,
2092 * or process is exiting or thread is exiting,
2093 * then we forget about it immediately.
2094 * (Note: we don't set SIGCONT in ps_sigignore,
2095 * and if it is set to SIG_IGN,
2096 * action will be SIG_DFL here.)
2098 mtx_lock(&ps->ps_mtx);
2099 if (SIGISMEMBER(ps->ps_sigignore, sig) ||
2100 (p->p_flag & P_WEXIT)) {
2101 mtx_unlock(&ps->ps_mtx);
2102 if (ksi && (ksi->ksi_flags & KSI_INS))
2103 ksiginfo_tryfree(ksi);
2106 if (SIGISMEMBER(td->td_sigmask, sig))
2108 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2112 mtx_unlock(&ps->ps_mtx);
2115 sigqueue_delete_stopmask_proc(p);
2116 else if (prop & SA_STOP) {
2118 * If sending a tty stop signal to a member of an orphaned
2119 * process group, discard the signal here if the action
2120 * is default; don't stop the process below if sleeping,
2121 * and don't clear any pending SIGCONT.
2123 if ((prop & SA_TTYSTOP) &&
2124 (p->p_pgrp->pg_jobc == 0) &&
2125 (action == SIG_DFL)) {
2126 if (ksi && (ksi->ksi_flags & KSI_INS))
2127 ksiginfo_tryfree(ksi);
2130 sigqueue_delete_proc(p, SIGCONT);
2131 if (p->p_flag & P_CONTINUED) {
2132 p->p_flag &= ~P_CONTINUED;
2133 PROC_LOCK(p->p_pptr);
2134 sigqueue_take(p->p_ksi);
2135 PROC_UNLOCK(p->p_pptr);
2139 ret = sigqueue_add(sigqueue, sig, ksi);
2144 * Defer further processing for signals which are held,
2145 * except that stopped processes must be continued by SIGCONT.
2147 if (action == SIG_HOLD &&
2148 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2151 * SIGKILL: Remove procfs STOPEVENTs.
2153 if (sig == SIGKILL) {
2154 /* from procfs_ioctl.c: PIOCBIC */
2156 /* from procfs_ioctl.c: PIOCCONT */
2161 * Some signals have a process-wide effect and a per-thread
2162 * component. Most processing occurs when the process next
2163 * tries to cross the user boundary, however there are some
2164 * times when processing needs to be done immediatly, such as
2165 * waking up threads so that they can cross the user boundary.
2166 * We try do the per-process part here.
2168 if (P_SHOULDSTOP(p)) {
2170 * The process is in stopped mode. All the threads should be
2171 * either winding down or already on the suspended queue.
2173 if (p->p_flag & P_TRACED) {
2175 * The traced process is already stopped,
2176 * so no further action is necessary.
2177 * No signal can restart us.
2182 if (sig == SIGKILL) {
2184 * SIGKILL sets process running.
2185 * It will die elsewhere.
2186 * All threads must be restarted.
2188 p->p_flag &= ~P_STOPPED_SIG;
2192 if (prop & SA_CONT) {
2194 * If SIGCONT is default (or ignored), we continue the
2195 * process but don't leave the signal in sigqueue as
2196 * it has no further action. If SIGCONT is held, we
2197 * continue the process and leave the signal in
2198 * sigqueue. If the process catches SIGCONT, let it
2199 * handle the signal itself. If it isn't waiting on
2200 * an event, it goes back to run state.
2201 * Otherwise, process goes back to sleep state.
2203 p->p_flag &= ~P_STOPPED_SIG;
2204 if (p->p_numthreads == p->p_suspcount) {
2205 p->p_flag |= P_CONTINUED;
2206 p->p_xstat = SIGCONT;
2207 PROC_LOCK(p->p_pptr);
2208 childproc_continued(p);
2209 PROC_UNLOCK(p->p_pptr);
2211 if (action == SIG_DFL) {
2212 sigqueue_delete(sigqueue, sig);
2213 } else if (action == SIG_CATCH) {
2215 * The process wants to catch it so it needs
2216 * to run at least one thread, but which one?
2217 * It would seem that the answer would be to
2218 * run an upcall in the next KSE to run, and
2219 * deliver the signal that way. In a NON KSE
2220 * process, we need to make sure that the
2221 * single thread is runnable asap.
2222 * XXXKSE for now however, make them all run.
2227 * The signal is not ignored or caught.
2229 mtx_lock_spin(&sched_lock);
2230 thread_unsuspend(p);
2231 mtx_unlock_spin(&sched_lock);
2235 if (prop & SA_STOP) {
2237 * Already stopped, don't need to stop again
2238 * (If we did the shell could get confused).
2239 * Just make sure the signal STOP bit set.
2241 p->p_flag |= P_STOPPED_SIG;
2242 sigqueue_delete(sigqueue, sig);
2247 * All other kinds of signals:
2248 * If a thread is sleeping interruptibly, simulate a
2249 * wakeup so that when it is continued it will be made
2250 * runnable and can look at the signal. However, don't make
2251 * the PROCESS runnable, leave it stopped.
2252 * It may run a bit until it hits a thread_suspend_check().
2254 mtx_lock_spin(&sched_lock);
2255 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2257 mtx_unlock_spin(&sched_lock);
2260 * Mutexes are short lived. Threads waiting on them will
2261 * hit thread_suspend_check() soon.
2263 } else if (p->p_state == PRS_NORMAL) {
2264 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2265 mtx_lock_spin(&sched_lock);
2266 tdsigwakeup(td, sig, action);
2267 mtx_unlock_spin(&sched_lock);
2271 MPASS(action == SIG_DFL);
2273 if (prop & SA_STOP) {
2274 if (p->p_flag & P_PPWAIT)
2276 p->p_flag |= P_STOPPED_SIG;
2278 mtx_lock_spin(&sched_lock);
2279 sig_suspend_threads(td, p, 1);
2280 if (p->p_numthreads == p->p_suspcount) {
2282 * only thread sending signal to another
2283 * process can reach here, if thread is sending
2284 * signal to its process, because thread does
2285 * not suspend itself here, p_numthreads
2286 * should never be equal to p_suspcount.
2289 mtx_unlock_spin(&sched_lock);
2290 sigqueue_delete_proc(p, p->p_xstat);
2292 mtx_unlock_spin(&sched_lock);
2299 /* Not in "NORMAL" state. discard the signal. */
2300 sigqueue_delete(sigqueue, sig);
2305 * The process is not stopped so we need to apply the signal to all the
2310 mtx_lock_spin(&sched_lock);
2311 tdsigwakeup(td, sig, action);
2312 thread_unsuspend(p);
2313 mtx_unlock_spin(&sched_lock);
2315 /* If we jump here, sched_lock should not be owned. */
2316 mtx_assert(&sched_lock, MA_NOTOWNED);
2321 * The force of a signal has been directed against a single
2322 * thread. We need to see what we can do about knocking it
2323 * out of any sleep it may be in etc.
2326 tdsigwakeup(struct thread *td, int sig, sig_t action)
2328 struct proc *p = td->td_proc;
2331 PROC_LOCK_ASSERT(p, MA_OWNED);
2332 mtx_assert(&sched_lock, MA_OWNED);
2333 prop = sigprop(sig);
2336 * Bring the priority of a thread up if we want it to get
2337 * killed in this lifetime.
2339 if (action == SIG_DFL && (prop & SA_KILL)) {
2341 sched_nice(td->td_proc, 0);
2342 if (td->td_priority > PUSER)
2343 sched_prio(td, PUSER);
2346 if (TD_ON_SLEEPQ(td)) {
2348 * If thread is sleeping uninterruptibly
2349 * we can't interrupt the sleep... the signal will
2350 * be noticed when the process returns through
2351 * trap() or syscall().
2353 if ((td->td_flags & TDF_SINTR) == 0)
2356 * If SIGCONT is default (or ignored) and process is
2357 * asleep, we are finished; the process should not
2360 if ((prop & SA_CONT) && action == SIG_DFL) {
2361 mtx_unlock_spin(&sched_lock);
2362 sigqueue_delete(&p->p_sigqueue, sig);
2364 * It may be on either list in this state.
2365 * Remove from both for now.
2367 sigqueue_delete(&td->td_sigqueue, sig);
2368 mtx_lock_spin(&sched_lock);
2373 * Give low priority threads a better chance to run.
2375 if (td->td_priority > PUSER)
2376 sched_prio(td, PUSER);
2381 * Other states do nothing with the signal immediately,
2382 * other than kicking ourselves if we are running.
2383 * It will either never be noticed, or noticed very soon.
2386 if (TD_IS_RUNNING(td) && td != curthread)
2393 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2397 PROC_LOCK_ASSERT(p, MA_OWNED);
2398 mtx_assert(&sched_lock, MA_OWNED);
2400 FOREACH_THREAD_IN_PROC(p, td2) {
2401 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2402 (td2->td_flags & TDF_SINTR) &&
2403 !TD_IS_SUSPENDED(td2)) {
2404 thread_suspend_one(td2);
2406 if (sending || td != td2)
2407 td2->td_flags |= TDF_ASTPENDING;
2409 if (TD_IS_RUNNING(td2) && td2 != td)
2410 forward_signal(td2);
2417 ptracestop(struct thread *td, int sig)
2419 struct proc *p = td->td_proc;
2421 PROC_LOCK_ASSERT(p, MA_OWNED);
2422 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2423 &p->p_mtx.mtx_object, "Stopping for traced signal");
2425 mtx_lock_spin(&sched_lock);
2426 td->td_flags |= TDF_XSIG;
2427 mtx_unlock_spin(&sched_lock);
2429 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2430 if (p->p_flag & P_SINGLE_EXIT) {
2431 mtx_lock_spin(&sched_lock);
2432 td->td_flags &= ~TDF_XSIG;
2433 mtx_unlock_spin(&sched_lock);
2437 * Just make wait() to work, the last stopped thread
2442 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2443 mtx_lock_spin(&sched_lock);
2444 sig_suspend_threads(td, p, 0);
2447 thread_suspend_one(td);
2450 mi_switch(SW_VOL, NULL);
2451 mtx_unlock_spin(&sched_lock);
2454 if (!(p->p_flag & P_TRACED))
2456 if (td->td_flags & TDF_DBSUSPEND) {
2457 if (p->p_flag & P_SINGLE_EXIT)
2459 mtx_lock_spin(&sched_lock);
2463 return (td->td_xsig);
2467 * If the current process has received a signal (should be caught or cause
2468 * termination, should interrupt current syscall), return the signal number.
2469 * Stop signals with default action are processed immediately, then cleared;
2470 * they aren't returned. This is checked after each entry to the system for
2471 * a syscall or trap (though this can usually be done without calling issignal
2472 * by checking the pending signal masks in cursig.) The normal call
2475 * while (sig = cursig(curthread))
2484 sigset_t sigpending;
2485 int sig, prop, newsig;
2489 mtx_assert(&ps->ps_mtx, MA_OWNED);
2490 PROC_LOCK_ASSERT(p, MA_OWNED);
2492 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2494 sigpending = td->td_sigqueue.sq_signals;
2495 SIGSETNAND(sigpending, td->td_sigmask);
2497 if (p->p_flag & P_PPWAIT)
2498 SIG_STOPSIGMASK(sigpending);
2499 if (SIGISEMPTY(sigpending)) /* no signal to send */
2501 sig = sig_ffs(&sigpending);
2503 if (p->p_stops & S_SIG) {
2504 mtx_unlock(&ps->ps_mtx);
2505 stopevent(p, S_SIG, sig);
2506 mtx_lock(&ps->ps_mtx);
2510 * We should see pending but ignored signals
2511 * only if P_TRACED was on when they were posted.
2513 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2514 sigqueue_delete(&td->td_sigqueue, sig);
2515 if (td->td_pflags & TDP_SA)
2516 SIGADDSET(td->td_sigmask, sig);
2519 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2521 * If traced, always stop.
2523 mtx_unlock(&ps->ps_mtx);
2524 newsig = ptracestop(td, sig);
2525 mtx_lock(&ps->ps_mtx);
2527 if (td->td_pflags & TDP_SA)
2528 SIGADDSET(td->td_sigmask, sig);
2530 if (sig != newsig) {
2534 * XXX shrug off debugger, it causes siginfo to
2537 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2540 * If parent wants us to take the signal,
2541 * then it will leave it in p->p_xstat;
2542 * otherwise we just look for signals again.
2549 * Put the new signal into td_sigqueue. If the
2550 * signal is being masked, look for other signals.
2552 SIGADDSET(td->td_sigqueue.sq_signals, sig);
2553 if (td->td_pflags & TDP_SA)
2554 SIGDELSET(td->td_sigmask, sig);
2555 if (SIGISMEMBER(td->td_sigmask, sig))
2561 * If the traced bit got turned off, go back up
2562 * to the top to rescan signals. This ensures
2563 * that p_sig* and p_sigact are consistent.
2565 if ((p->p_flag & P_TRACED) == 0)
2569 prop = sigprop(sig);
2572 * Decide whether the signal should be returned.
2573 * Return the signal's number, or fall through
2574 * to clear it from the pending mask.
2576 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2578 case (intptr_t)SIG_DFL:
2580 * Don't take default actions on system processes.
2582 if (p->p_pid <= 1) {
2585 * Are you sure you want to ignore SIGSEGV
2588 printf("Process (pid %lu) got signal %d\n",
2589 (u_long)p->p_pid, sig);
2591 break; /* == ignore */
2594 * If there is a pending stop signal to process
2595 * with default action, stop here,
2596 * then clear the signal. However,
2597 * if process is member of an orphaned
2598 * process group, ignore tty stop signals.
2600 if (prop & SA_STOP) {
2601 if (p->p_flag & P_TRACED ||
2602 (p->p_pgrp->pg_jobc == 0 &&
2604 break; /* == ignore */
2605 mtx_unlock(&ps->ps_mtx);
2606 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2607 &p->p_mtx.mtx_object, "Catching SIGSTOP");
2608 p->p_flag |= P_STOPPED_SIG;
2610 mtx_lock_spin(&sched_lock);
2611 sig_suspend_threads(td, p, 0);
2613 thread_suspend_one(td);
2616 mi_switch(SW_INVOL, NULL);
2617 mtx_unlock_spin(&sched_lock);
2620 mtx_lock(&ps->ps_mtx);
2622 } else if (prop & SA_IGNORE) {
2624 * Except for SIGCONT, shouldn't get here.
2625 * Default action is to ignore; drop it.
2627 break; /* == ignore */
2632 case (intptr_t)SIG_IGN:
2634 * Masking above should prevent us ever trying
2635 * to take action on an ignored signal other
2636 * than SIGCONT, unless process is traced.
2638 if ((prop & SA_CONT) == 0 &&
2639 (p->p_flag & P_TRACED) == 0)
2640 printf("issignal\n");
2641 break; /* == ignore */
2645 * This signal has an action, let
2646 * postsig() process it.
2650 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2659 thread_stopped(struct proc *p)
2661 struct proc *p1 = curthread->td_proc;
2664 PROC_LOCK_ASSERT(p, MA_OWNED);
2665 mtx_assert(&sched_lock, MA_OWNED);
2669 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2670 mtx_unlock_spin(&sched_lock);
2671 p->p_flag &= ~P_WAITED;
2672 PROC_LOCK(p->p_pptr);
2673 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2674 CLD_TRAPPED : CLD_STOPPED);
2675 PROC_UNLOCK(p->p_pptr);
2676 mtx_lock_spin(&sched_lock);
2681 * Take the action for the specified signal
2682 * from the current set of pending signals.
2688 struct thread *td = curthread;
2689 register struct proc *p = td->td_proc;
2693 sigset_t returnmask;
2696 KASSERT(sig != 0, ("postsig"));
2698 PROC_LOCK_ASSERT(p, MA_OWNED);
2700 mtx_assert(&ps->ps_mtx, MA_OWNED);
2701 ksiginfo_init(&ksi);
2702 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2703 ksi.ksi_signo = sig;
2704 if (ksi.ksi_code == SI_TIMER)
2705 itimer_accept(p, ksi.ksi_timerid, &ksi);
2706 action = ps->ps_sigact[_SIG_IDX(sig)];
2708 if (KTRPOINT(td, KTR_PSIG))
2709 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2710 &td->td_oldsigmask : &td->td_sigmask, 0);
2712 if (p->p_stops & S_SIG) {
2713 mtx_unlock(&ps->ps_mtx);
2714 stopevent(p, S_SIG, sig);
2715 mtx_lock(&ps->ps_mtx);
2718 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2720 * Default action, where the default is to kill
2721 * the process. (Other cases were ignored above.)
2723 mtx_unlock(&ps->ps_mtx);
2727 if (td->td_pflags & TDP_SA) {
2728 if (sig == SIGKILL) {
2729 mtx_unlock(&ps->ps_mtx);
2735 * If we get here, the signal must be caught.
2737 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2738 ("postsig action"));
2740 * Set the new mask value and also defer further
2741 * occurrences of this signal.
2743 * Special case: user has done a sigsuspend. Here the
2744 * current mask is not of interest, but rather the
2745 * mask from before the sigsuspend is what we want
2746 * restored after the signal processing is completed.
2748 if (td->td_pflags & TDP_OLDMASK) {
2749 returnmask = td->td_oldsigmask;
2750 td->td_pflags &= ~TDP_OLDMASK;
2752 returnmask = td->td_sigmask;
2754 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2755 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2756 SIGADDSET(td->td_sigmask, sig);
2758 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2760 * See kern_sigaction() for origin of this code.
2762 SIGDELSET(ps->ps_sigcatch, sig);
2763 if (sig != SIGCONT &&
2764 sigprop(sig) & SA_IGNORE)
2765 SIGADDSET(ps->ps_sigignore, sig);
2766 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2768 p->p_stats->p_ru.ru_nsignals++;
2769 if (p->p_sig != sig) {
2776 if (td->td_pflags & TDP_SA)
2777 thread_signal_add(curthread, &ksi);
2779 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2784 * Kill the current process for stated reason.
2792 PROC_LOCK_ASSERT(p, MA_OWNED);
2793 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2794 p, p->p_pid, p->p_comm);
2795 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2796 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2797 psignal(p, SIGKILL);
2801 * Force the current process to exit with the specified signal, dumping core
2802 * if appropriate. We bypass the normal tests for masked and caught signals,
2803 * allowing unrecoverable failures to terminate the process without changing
2804 * signal state. Mark the accounting record with the signal termination.
2805 * If dumping core, save the signal number for the debugger. Calls exit and
2815 struct proc *p = td->td_proc;
2817 PROC_LOCK_ASSERT(p, MA_OWNED);
2818 p->p_acflag |= AXSIG;
2820 * We must be single-threading to generate a core dump. This
2821 * ensures that the registers in the core file are up-to-date.
2822 * Also, the ELF dump handler assumes that the thread list doesn't
2823 * change out from under it.
2825 * XXX If another thread attempts to single-thread before us
2826 * (e.g. via fork()), we won't get a dump at all.
2828 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2831 * Log signals which would cause core dumps
2832 * (Log as LOG_INFO to appease those who don't want
2834 * XXX : Todo, as well as euid, write out ruid too
2835 * Note that coredump() drops proc lock.
2837 if (coredump(td) == 0)
2839 if (kern_logsigexit)
2841 "pid %d (%s), uid %d: exited on signal %d%s\n",
2842 p->p_pid, p->p_comm,
2843 td->td_ucred ? td->td_ucred->cr_uid : -1,
2845 sig & WCOREFLAG ? " (core dumped)" : "");
2848 exit1(td, W_EXITCODE(0, sig));
2853 * Send queued SIGCHLD to parent when child process's state
2857 sigparent(struct proc *p, int reason, int status)
2859 PROC_LOCK_ASSERT(p, MA_OWNED);
2860 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2862 if (p->p_ksi != NULL) {
2863 p->p_ksi->ksi_signo = SIGCHLD;
2864 p->p_ksi->ksi_code = reason;
2865 p->p_ksi->ksi_status = status;
2866 p->p_ksi->ksi_pid = p->p_pid;
2867 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2868 if (KSI_ONQ(p->p_ksi))
2871 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2875 childproc_jobstate(struct proc *p, int reason, int status)
2879 PROC_LOCK_ASSERT(p, MA_OWNED);
2880 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2883 * Wake up parent sleeping in kern_wait(), also send
2884 * SIGCHLD to parent, but SIGCHLD does not guarantee
2885 * that parent will awake, because parent may masked
2888 p->p_pptr->p_flag |= P_STATCHILD;
2891 ps = p->p_pptr->p_sigacts;
2892 mtx_lock(&ps->ps_mtx);
2893 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2894 mtx_unlock(&ps->ps_mtx);
2895 sigparent(p, reason, status);
2897 mtx_unlock(&ps->ps_mtx);
2901 childproc_stopped(struct proc *p, int reason)
2903 childproc_jobstate(p, reason, p->p_xstat);
2907 childproc_continued(struct proc *p)
2909 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2913 childproc_exited(struct proc *p)
2916 int status = p->p_xstat; /* convert to int */
2918 reason = CLD_EXITED;
2919 if (WCOREDUMP(status))
2920 reason = CLD_DUMPED;
2921 else if (WIFSIGNALED(status))
2922 reason = CLD_KILLED;
2924 * XXX avoid calling wakeup(p->p_pptr), the work is
2927 sigparent(p, reason, status);
2930 static char corefilename[MAXPATHLEN] = {"%N.core"};
2931 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2932 sizeof(corefilename), "process corefile name format string");
2935 * expand_name(name, uid, pid)
2936 * Expand the name described in corefilename, using name, uid, and pid.
2937 * corefilename is a printf-like string, with three format specifiers:
2938 * %N name of process ("name")
2939 * %P process id (pid)
2941 * For example, "%N.core" is the default; they can be disabled completely
2942 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2943 * This is controlled by the sysctl variable kern.corefile (see above).
2947 expand_name(name, uid, pid)
2952 const char *format, *appendstr;
2954 char buf[11]; /* Buffer for pid/uid -- max 4B */
2957 format = corefilename;
2958 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2961 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2962 switch (format[i]) {
2963 case '%': /* Format character */
2965 switch (format[i]) {
2969 case 'N': /* process name */
2972 case 'P': /* process id */
2973 sprintf(buf, "%u", pid);
2976 case 'U': /* user id */
2977 sprintf(buf, "%u", uid);
2983 "Unknown format character %c in `%s'\n",
2986 l = strlen(appendstr);
2987 if ((n + l) >= MAXPATHLEN)
2989 memcpy(temp + n, appendstr, l);
2993 temp[n++] = format[i];
2996 if (format[i] != '\0')
3000 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
3001 (long)pid, name, (u_long)uid);
3007 * Dump a process' core. The main routine does some
3008 * policy checking, and creates the name of the coredump;
3009 * then it passes on a vnode and a size limit to the process-specific
3010 * coredump routine if there is one; if there _is not_ one, it returns
3011 * ENOSYS; otherwise it returns the error from the process-specific routine.
3015 coredump(struct thread *td)
3017 struct proc *p = td->td_proc;
3018 register struct vnode *vp;
3019 register struct ucred *cred = td->td_ucred;
3021 struct nameidata nd;
3023 int error, error1, flags, locked;
3025 char *name; /* name of corefile */
3028 PROC_LOCK_ASSERT(p, MA_OWNED);
3029 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3030 _STOPEVENT(p, S_CORE, 0);
3032 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3038 * Note that the bulk of limit checking is done after
3039 * the corefile is created. The exception is if the limit
3040 * for corefiles is 0, in which case we don't bother
3041 * creating the corefile at all. This layout means that
3042 * a corefile is truncated instead of not being created,
3043 * if it is larger than the limit.
3045 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3052 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
3057 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
3058 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3059 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
3065 NDFREE(&nd, NDF_ONLY_PNBUF);
3068 /* Don't dump to non-regular files or files with links. */
3069 if (vp->v_type != VREG ||
3070 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
3071 VOP_UNLOCK(vp, 0, td);
3076 VOP_UNLOCK(vp, 0, td);
3077 lf.l_whence = SEEK_SET;
3080 lf.l_type = F_WRLCK;
3081 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3083 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3084 lf.l_type = F_UNLCK;
3086 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3087 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3089 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3096 if (set_core_nodump_flag)
3097 vattr.va_flags = UF_NODUMP;
3098 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3099 VOP_LEASE(vp, td, cred, LEASE_WRITE);
3100 VOP_SETATTR(vp, &vattr, cred, td);
3101 VOP_UNLOCK(vp, 0, td);
3103 p->p_acflag |= ACORE;
3106 error = p->p_sysent->sv_coredump ?
3107 p->p_sysent->sv_coredump(td, vp, limit) :
3111 lf.l_type = F_UNLCK;
3112 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3114 vn_finished_write(mp);
3116 error1 = vn_close(vp, FWRITE, cred, td);
3124 * Nonexistent system call-- signal process (may want to handle it).
3125 * Flag error in case process won't see signal immediately (blocked or ignored).
3127 #ifndef _SYS_SYSPROTO_H_
3139 struct nosys_args *args;
3141 struct proc *p = td->td_proc;
3150 * Send a SIGIO or SIGURG signal to a process or process group using
3151 * stored credentials rather than those of the current process.
3154 pgsigio(sigiop, sig, checkctty)
3155 struct sigio **sigiop;
3158 struct sigio *sigio;
3162 if (sigio == NULL) {
3166 if (sigio->sio_pgid > 0) {
3167 PROC_LOCK(sigio->sio_proc);
3168 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3169 psignal(sigio->sio_proc, sig);
3170 PROC_UNLOCK(sigio->sio_proc);
3171 } else if (sigio->sio_pgid < 0) {
3174 PGRP_LOCK(sigio->sio_pgrp);
3175 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3177 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3178 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3182 PGRP_UNLOCK(sigio->sio_pgrp);
3188 filt_sigattach(struct knote *kn)
3190 struct proc *p = curproc;
3192 kn->kn_ptr.p_proc = p;
3193 kn->kn_flags |= EV_CLEAR; /* automatically set */
3195 knlist_add(&p->p_klist, kn, 0);
3201 filt_sigdetach(struct knote *kn)
3203 struct proc *p = kn->kn_ptr.p_proc;
3205 knlist_remove(&p->p_klist, kn, 0);
3209 * signal knotes are shared with proc knotes, so we apply a mask to
3210 * the hint in order to differentiate them from process hints. This
3211 * could be avoided by using a signal-specific knote list, but probably
3212 * isn't worth the trouble.
3215 filt_signal(struct knote *kn, long hint)
3218 if (hint & NOTE_SIGNAL) {
3219 hint &= ~NOTE_SIGNAL;
3221 if (kn->kn_id == hint)
3224 return (kn->kn_data != 0);
3232 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3234 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3239 sigacts_free(struct sigacts *ps)
3242 mtx_lock(&ps->ps_mtx);
3244 if (ps->ps_refcnt == 0) {
3245 mtx_destroy(&ps->ps_mtx);
3246 free(ps, M_SUBPROC);
3248 mtx_unlock(&ps->ps_mtx);
3252 sigacts_hold(struct sigacts *ps)
3254 mtx_lock(&ps->ps_mtx);
3256 mtx_unlock(&ps->ps_mtx);
3261 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3264 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3265 mtx_lock(&src->ps_mtx);
3266 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3267 mtx_unlock(&src->ps_mtx);
3271 sigacts_shared(struct sigacts *ps)
3275 mtx_lock(&ps->ps_mtx);
3276 shared = ps->ps_refcnt > 1;
3277 mtx_unlock(&ps->ps_mtx);