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 *td, int sig, sig_t action);
94 static int filt_sigattach(struct knote *kn);
95 static void filt_sigdetach(struct knote *kn);
96 static int filt_signal(struct knote *kn, long hint);
97 static struct thread *sigtd(struct proc *p, int sig, int prop);
98 static int kern_sigtimedwait(struct thread *, sigset_t,
99 ksiginfo_t *, struct timespec *);
100 static int do_tdsignal(struct proc *, struct thread *, int, ksiginfo_t *);
101 static void sigqueue_start(void);
103 static uma_zone_t ksiginfo_zone = NULL;
104 struct filterops sig_filtops =
105 { 0, filt_sigattach, filt_sigdetach, filt_signal };
107 static int kern_logsigexit = 1;
108 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
110 "Log processes quitting on abnormal signals to syslog(3)");
112 static int kern_forcesigexit = 1;
113 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
114 &kern_forcesigexit, 0, "Force trap signal to be handled");
116 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
118 static int max_pending_per_proc = 128;
119 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
120 &max_pending_per_proc, 0, "Max pending signals per proc");
122 static int preallocate_siginfo = 1024;
123 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
124 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
125 &preallocate_siginfo, 0, "Preallocated signal memory size");
127 static int signal_overflow = 0;
128 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
129 &signal_overflow, 0, "Number of signals overflew");
131 static int signal_alloc_fail = 0;
132 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
133 &signal_alloc_fail, 0, "signals failed to be allocated");
135 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
138 * Policy -- Can ucred cr1 send SIGIO to process cr2?
139 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
140 * in the right situations.
142 #define CANSIGIO(cr1, cr2) \
143 ((cr1)->cr_uid == 0 || \
144 (cr1)->cr_ruid == (cr2)->cr_ruid || \
145 (cr1)->cr_uid == (cr2)->cr_ruid || \
146 (cr1)->cr_ruid == (cr2)->cr_uid || \
147 (cr1)->cr_uid == (cr2)->cr_uid)
150 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
151 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
153 static int do_coredump = 1;
154 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
155 &do_coredump, 0, "Enable/Disable coredumps");
157 static int set_core_nodump_flag = 0;
158 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
159 0, "Enable setting the NODUMP flag on coredump files");
162 * Signal properties and actions.
163 * The array below categorizes the signals and their default actions
164 * according to the following properties:
166 #define SA_KILL 0x01 /* terminates process by default */
167 #define SA_CORE 0x02 /* ditto and coredumps */
168 #define SA_STOP 0x04 /* suspend process */
169 #define SA_TTYSTOP 0x08 /* ditto, from tty */
170 #define SA_IGNORE 0x10 /* ignore by default */
171 #define SA_CONT 0x20 /* continue if suspended */
172 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
173 #define SA_PROC 0x80 /* deliverable to any thread */
175 static int sigproptbl[NSIG] = {
176 SA_KILL|SA_PROC, /* SIGHUP */
177 SA_KILL|SA_PROC, /* SIGINT */
178 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
179 SA_KILL|SA_CORE, /* SIGILL */
180 SA_KILL|SA_CORE, /* SIGTRAP */
181 SA_KILL|SA_CORE, /* SIGABRT */
182 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
183 SA_KILL|SA_CORE, /* SIGFPE */
184 SA_KILL|SA_PROC, /* SIGKILL */
185 SA_KILL|SA_CORE, /* SIGBUS */
186 SA_KILL|SA_CORE, /* SIGSEGV */
187 SA_KILL|SA_CORE, /* SIGSYS */
188 SA_KILL|SA_PROC, /* SIGPIPE */
189 SA_KILL|SA_PROC, /* SIGALRM */
190 SA_KILL|SA_PROC, /* SIGTERM */
191 SA_IGNORE|SA_PROC, /* SIGURG */
192 SA_STOP|SA_PROC, /* SIGSTOP */
193 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
194 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
195 SA_IGNORE|SA_PROC, /* SIGCHLD */
196 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
197 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
198 SA_IGNORE|SA_PROC, /* SIGIO */
199 SA_KILL, /* SIGXCPU */
200 SA_KILL, /* SIGXFSZ */
201 SA_KILL|SA_PROC, /* SIGVTALRM */
202 SA_KILL|SA_PROC, /* SIGPROF */
203 SA_IGNORE|SA_PROC, /* SIGWINCH */
204 SA_IGNORE|SA_PROC, /* SIGINFO */
205 SA_KILL|SA_PROC, /* SIGUSR1 */
206 SA_KILL|SA_PROC, /* SIGUSR2 */
212 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
213 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
214 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
215 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
216 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
217 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
221 ksiginfo_alloc(int wait)
228 if (ksiginfo_zone != NULL)
229 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
234 ksiginfo_free(ksiginfo_t *ksi)
236 uma_zfree(ksiginfo_zone, ksi);
240 ksiginfo_tryfree(ksiginfo_t *ksi)
242 if (!(ksi->ksi_flags & KSI_EXT)) {
243 uma_zfree(ksiginfo_zone, ksi);
250 sigqueue_init(sigqueue_t *list, struct proc *p)
252 SIGEMPTYSET(list->sq_signals);
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 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
277 next = TAILQ_NEXT(ksi, ksi_link);
278 if (ksi->ksi_signo == signo) {
280 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
281 ksi->ksi_sigq = NULL;
282 ksiginfo_copy(ksi, si);
283 if (ksiginfo_tryfree(ksi) && p != NULL)
291 SIGDELSET(sq->sq_signals, signo);
292 si->ksi_signo = signo;
297 sigqueue_take(ksiginfo_t *ksi)
303 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
307 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
308 ksi->ksi_sigq = NULL;
309 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
312 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
313 kp = TAILQ_NEXT(kp, ksi_link)) {
314 if (kp->ksi_signo == ksi->ksi_signo)
318 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
322 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
324 struct proc *p = sq->sq_proc;
325 struct ksiginfo *ksi;
328 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
330 if (signo == SIGKILL || signo == SIGSTOP || si == NULL)
333 /* directly insert the ksi, don't copy it */
334 if (si->ksi_flags & KSI_INS) {
335 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
340 if (__predict_false(ksiginfo_zone == NULL))
343 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
346 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
352 ksiginfo_copy(si, ksi);
353 ksi->ksi_signo = signo;
354 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
358 if ((si->ksi_flags & KSI_TRAP) != 0) {
367 SIGADDSET(sq->sq_signals, signo);
372 sigqueue_flush(sigqueue_t *sq)
374 struct proc *p = sq->sq_proc;
377 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
380 PROC_LOCK_ASSERT(p, MA_OWNED);
382 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
383 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
384 ksi->ksi_sigq = NULL;
385 if (ksiginfo_tryfree(ksi) && p != NULL)
389 SIGEMPTYSET(sq->sq_signals);
393 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
397 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
399 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
400 SIGADDSET(*set, ksi->ksi_signo);
404 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
407 struct proc *p1, *p2;
408 ksiginfo_t *ksi, *next;
410 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
411 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
413 * make a copy, this allows setp to point to src or dst
414 * sq_signals without trouble.
419 /* Move siginfo to target list */
420 for (ksi = TAILQ_FIRST(&src->sq_list); ksi != NULL; ksi = next) {
421 next = TAILQ_NEXT(ksi, ksi_link);
422 if (SIGISMEMBER(set, ksi->ksi_signo)) {
423 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
426 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
433 /* Move pending bits to target list */
434 tmp = src->sq_signals;
436 SIGSETOR(dst->sq_signals, tmp);
437 SIGSETNAND(src->sq_signals, tmp);
439 /* Finally, rescan src queue and set pending bits for it */
440 sigqueue_collect_set(src, &src->sq_signals);
444 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
449 SIGADDSET(set, signo);
450 sigqueue_move_set(src, dst, &set);
454 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
456 struct proc *p = sq->sq_proc;
457 ksiginfo_t *ksi, *next;
459 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
461 /* Remove siginfo queue */
462 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
463 next = TAILQ_NEXT(ksi, ksi_link);
464 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
465 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
466 ksi->ksi_sigq = NULL;
467 if (ksiginfo_tryfree(ksi) && p != NULL)
471 SIGSETNAND(sq->sq_signals, *set);
472 /* Finally, rescan queue and set pending bits for it */
473 sigqueue_collect_set(sq, &sq->sq_signals);
477 sigqueue_delete(sigqueue_t *sq, int signo)
482 SIGADDSET(set, signo);
483 sigqueue_delete_set(sq, &set);
486 /* Remove a set of signals for a process */
488 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
493 PROC_LOCK_ASSERT(p, MA_OWNED);
495 sigqueue_init(&worklist, NULL);
496 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
498 mtx_lock_spin(&sched_lock);
499 FOREACH_THREAD_IN_PROC(p, td0)
500 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
501 mtx_unlock_spin(&sched_lock);
503 sigqueue_flush(&worklist);
507 sigqueue_delete_proc(struct proc *p, int signo)
512 SIGADDSET(set, signo);
513 sigqueue_delete_set_proc(p, &set);
517 sigqueue_delete_stopmask_proc(struct proc *p)
522 SIGADDSET(set, SIGSTOP);
523 SIGADDSET(set, SIGTSTP);
524 SIGADDSET(set, SIGTTIN);
525 SIGADDSET(set, SIGTTOU);
526 sigqueue_delete_set_proc(p, &set);
530 * Determine signal that should be delivered to process p, the current
531 * process, 0 if none. If there is a pending stop signal with default
532 * action, the process stops in issignal().
537 cursig(struct thread *td)
539 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
540 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
541 mtx_assert(&sched_lock, MA_NOTOWNED);
542 return (SIGPENDING(td) ? issignal(td) : 0);
546 * Arrange for ast() to handle unmasked pending signals on return to user
547 * mode. This must be called whenever a signal is added to td_sigqueue or
548 * unmasked in td_sigmask.
551 signotify(struct thread *td)
558 PROC_LOCK_ASSERT(p, MA_OWNED);
561 * If our mask changed we may have to move signal that were
562 * previously masked by all threads to our sigqueue.
564 set = p->p_sigqueue.sq_signals;
565 if (p->p_flag & P_SA)
566 saved = p->p_sigqueue.sq_signals;
567 SIGSETNAND(set, td->td_sigmask);
568 if (! SIGISEMPTY(set))
569 sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set);
570 if (SIGPENDING(td)) {
571 mtx_lock_spin(&sched_lock);
572 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
573 mtx_unlock_spin(&sched_lock);
575 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
576 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
577 /* pending set changed */
578 p->p_flag |= P_SIGEVENT;
579 wakeup(&p->p_siglist);
585 sigonstack(size_t sp)
587 struct thread *td = curthread;
589 return ((td->td_pflags & TDP_ALTSTACK) ?
590 #if defined(COMPAT_43)
591 ((td->td_sigstk.ss_size == 0) ?
592 (td->td_sigstk.ss_flags & SS_ONSTACK) :
593 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
595 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
604 if (sig > 0 && sig < NSIG)
605 return (sigproptbl[_SIG_IDX(sig)]);
610 sig_ffs(sigset_t *set)
614 for (i = 0; i < _SIG_WORDS; i++)
616 return (ffs(set->__bits[i]) + (i * 32));
629 kern_sigaction(td, sig, act, oact, flags)
632 struct sigaction *act, *oact;
636 struct proc *p = td->td_proc;
638 if (!_SIG_VALID(sig))
643 mtx_lock(&ps->ps_mtx);
645 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
646 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
648 if (SIGISMEMBER(ps->ps_sigonstack, sig))
649 oact->sa_flags |= SA_ONSTACK;
650 if (!SIGISMEMBER(ps->ps_sigintr, sig))
651 oact->sa_flags |= SA_RESTART;
652 if (SIGISMEMBER(ps->ps_sigreset, sig))
653 oact->sa_flags |= SA_RESETHAND;
654 if (SIGISMEMBER(ps->ps_signodefer, sig))
655 oact->sa_flags |= SA_NODEFER;
656 if (SIGISMEMBER(ps->ps_siginfo, sig))
657 oact->sa_flags |= SA_SIGINFO;
658 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
659 oact->sa_flags |= SA_NOCLDSTOP;
660 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
661 oact->sa_flags |= SA_NOCLDWAIT;
664 if ((sig == SIGKILL || sig == SIGSTOP) &&
665 act->sa_handler != SIG_DFL) {
666 mtx_unlock(&ps->ps_mtx);
672 * Change setting atomically.
675 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
676 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
677 if (act->sa_flags & SA_SIGINFO) {
678 ps->ps_sigact[_SIG_IDX(sig)] =
679 (__sighandler_t *)act->sa_sigaction;
680 SIGADDSET(ps->ps_siginfo, sig);
682 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
683 SIGDELSET(ps->ps_siginfo, sig);
685 if (!(act->sa_flags & SA_RESTART))
686 SIGADDSET(ps->ps_sigintr, sig);
688 SIGDELSET(ps->ps_sigintr, sig);
689 if (act->sa_flags & SA_ONSTACK)
690 SIGADDSET(ps->ps_sigonstack, sig);
692 SIGDELSET(ps->ps_sigonstack, sig);
693 if (act->sa_flags & SA_RESETHAND)
694 SIGADDSET(ps->ps_sigreset, sig);
696 SIGDELSET(ps->ps_sigreset, sig);
697 if (act->sa_flags & SA_NODEFER)
698 SIGADDSET(ps->ps_signodefer, sig);
700 SIGDELSET(ps->ps_signodefer, sig);
701 if (sig == SIGCHLD) {
702 if (act->sa_flags & SA_NOCLDSTOP)
703 ps->ps_flag |= PS_NOCLDSTOP;
705 ps->ps_flag &= ~PS_NOCLDSTOP;
706 if (act->sa_flags & SA_NOCLDWAIT) {
708 * Paranoia: since SA_NOCLDWAIT is implemented
709 * by reparenting the dying child to PID 1 (and
710 * trust it to reap the zombie), PID 1 itself
711 * is forbidden to set SA_NOCLDWAIT.
714 ps->ps_flag &= ~PS_NOCLDWAIT;
716 ps->ps_flag |= PS_NOCLDWAIT;
718 ps->ps_flag &= ~PS_NOCLDWAIT;
719 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
720 ps->ps_flag |= PS_CLDSIGIGN;
722 ps->ps_flag &= ~PS_CLDSIGIGN;
725 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
726 * and for signals set to SIG_DFL where the default is to
727 * ignore. However, don't put SIGCONT in ps_sigignore, as we
728 * have to restart the process.
730 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
731 (sigprop(sig) & SA_IGNORE &&
732 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
733 if ((p->p_flag & P_SA) &&
734 SIGISMEMBER(p->p_sigqueue.sq_signals, sig)) {
735 p->p_flag |= P_SIGEVENT;
736 wakeup(&p->p_siglist);
738 /* never to be seen again */
739 sigqueue_delete_proc(p, sig);
741 /* easier in psignal */
742 SIGADDSET(ps->ps_sigignore, sig);
743 SIGDELSET(ps->ps_sigcatch, sig);
745 SIGDELSET(ps->ps_sigignore, sig);
746 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
747 SIGDELSET(ps->ps_sigcatch, sig);
749 SIGADDSET(ps->ps_sigcatch, sig);
751 #ifdef COMPAT_FREEBSD4
752 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
753 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
754 (flags & KSA_FREEBSD4) == 0)
755 SIGDELSET(ps->ps_freebsd4, sig);
757 SIGADDSET(ps->ps_freebsd4, sig);
760 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
761 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
762 (flags & KSA_OSIGSET) == 0)
763 SIGDELSET(ps->ps_osigset, sig);
765 SIGADDSET(ps->ps_osigset, sig);
768 mtx_unlock(&ps->ps_mtx);
773 #ifndef _SYS_SYSPROTO_H_
774 struct sigaction_args {
776 struct sigaction *act;
777 struct sigaction *oact;
786 register struct sigaction_args *uap;
788 struct sigaction act, oact;
789 register struct sigaction *actp, *oactp;
792 actp = (uap->act != NULL) ? &act : NULL;
793 oactp = (uap->oact != NULL) ? &oact : NULL;
795 error = copyin(uap->act, actp, sizeof(act));
799 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
801 error = copyout(oactp, uap->oact, sizeof(oact));
805 #ifdef COMPAT_FREEBSD4
806 #ifndef _SYS_SYSPROTO_H_
807 struct freebsd4_sigaction_args {
809 struct sigaction *act;
810 struct sigaction *oact;
817 freebsd4_sigaction(td, uap)
819 register struct freebsd4_sigaction_args *uap;
821 struct sigaction act, oact;
822 register struct sigaction *actp, *oactp;
826 actp = (uap->act != NULL) ? &act : NULL;
827 oactp = (uap->oact != NULL) ? &oact : NULL;
829 error = copyin(uap->act, actp, sizeof(act));
833 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
835 error = copyout(oactp, uap->oact, sizeof(oact));
838 #endif /* COMAPT_FREEBSD4 */
840 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
841 #ifndef _SYS_SYSPROTO_H_
842 struct osigaction_args {
844 struct osigaction *nsa;
845 struct osigaction *osa;
854 register struct osigaction_args *uap;
856 struct osigaction sa;
857 struct sigaction nsa, osa;
858 register struct sigaction *nsap, *osap;
861 if (uap->signum <= 0 || uap->signum >= ONSIG)
864 nsap = (uap->nsa != NULL) ? &nsa : NULL;
865 osap = (uap->osa != NULL) ? &osa : NULL;
868 error = copyin(uap->nsa, &sa, sizeof(sa));
871 nsap->sa_handler = sa.sa_handler;
872 nsap->sa_flags = sa.sa_flags;
873 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
875 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
876 if (osap && !error) {
877 sa.sa_handler = osap->sa_handler;
878 sa.sa_flags = osap->sa_flags;
879 SIG2OSIG(osap->sa_mask, sa.sa_mask);
880 error = copyout(&sa, uap->osa, sizeof(sa));
885 #if !defined(__i386__) && !defined(__alpha__)
886 /* Avoid replicating the same stub everywhere */
890 struct osigreturn_args *uap;
893 return (nosys(td, (struct nosys_args *)uap));
896 #endif /* COMPAT_43 */
899 * Initialize signal state for process 0;
900 * set to ignore signals that are ignored by default.
911 mtx_lock(&ps->ps_mtx);
912 for (i = 1; i <= NSIG; i++)
913 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
914 SIGADDSET(ps->ps_sigignore, i);
915 mtx_unlock(&ps->ps_mtx);
920 * Reset signals for an exec of the specified process.
923 execsigs(struct proc *p)
930 * Reset caught signals. Held signals remain held
931 * through td_sigmask (unless they were caught,
932 * and are now ignored by default).
934 PROC_LOCK_ASSERT(p, MA_OWNED);
935 td = FIRST_THREAD_IN_PROC(p);
937 mtx_lock(&ps->ps_mtx);
938 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
939 sig = sig_ffs(&ps->ps_sigcatch);
940 SIGDELSET(ps->ps_sigcatch, sig);
941 if (sigprop(sig) & SA_IGNORE) {
943 SIGADDSET(ps->ps_sigignore, sig);
944 sigqueue_delete_proc(p, sig);
946 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
949 * Reset stack state to the user stack.
950 * Clear set of signals caught on the signal stack.
952 td->td_sigstk.ss_flags = SS_DISABLE;
953 td->td_sigstk.ss_size = 0;
954 td->td_sigstk.ss_sp = 0;
955 td->td_pflags &= ~TDP_ALTSTACK;
957 * Reset no zombies if child dies flag as Solaris does.
959 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
960 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
961 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
962 mtx_unlock(&ps->ps_mtx);
968 * Manipulate signal mask.
971 kern_sigprocmask(td, how, set, oset, old)
974 sigset_t *set, *oset;
979 PROC_LOCK(td->td_proc);
981 *oset = td->td_sigmask;
988 SIGSETOR(td->td_sigmask, *set);
991 SIGSETNAND(td->td_sigmask, *set);
997 SIGSETLO(td->td_sigmask, *set);
999 td->td_sigmask = *set;
1007 PROC_UNLOCK(td->td_proc);
1012 * sigprocmask() - MP SAFE
1015 #ifndef _SYS_SYSPROTO_H_
1016 struct sigprocmask_args {
1018 const sigset_t *set;
1023 sigprocmask(td, uap)
1024 register struct thread *td;
1025 struct sigprocmask_args *uap;
1028 sigset_t *setp, *osetp;
1031 setp = (uap->set != NULL) ? &set : NULL;
1032 osetp = (uap->oset != NULL) ? &oset : NULL;
1034 error = copyin(uap->set, setp, sizeof(set));
1038 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1039 if (osetp && !error) {
1040 error = copyout(osetp, uap->oset, sizeof(oset));
1045 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1047 * osigprocmask() - MP SAFE
1049 #ifndef _SYS_SYSPROTO_H_
1050 struct osigprocmask_args {
1056 osigprocmask(td, uap)
1057 register struct thread *td;
1058 struct osigprocmask_args *uap;
1063 OSIG2SIG(uap->mask, set);
1064 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1065 SIG2OSIG(oset, td->td_retval[0]);
1068 #endif /* COMPAT_43 */
1074 sigwait(struct thread *td, struct sigwait_args *uap)
1080 error = copyin(uap->set, &set, sizeof(set));
1082 td->td_retval[0] = error;
1086 error = kern_sigtimedwait(td, set, &ksi, NULL);
1088 if (error == ERESTART)
1090 td->td_retval[0] = error;
1094 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1095 td->td_retval[0] = error;
1102 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1105 struct timespec *timeout;
1111 error = copyin(uap->timeout, &ts, sizeof(ts));
1119 error = copyin(uap->set, &set, sizeof(set));
1123 error = kern_sigtimedwait(td, set, &ksi, timeout);
1128 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1131 td->td_retval[0] = ksi.ksi_signo;
1139 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1145 error = copyin(uap->set, &set, sizeof(set));
1149 error = kern_sigtimedwait(td, set, &ksi, NULL);
1154 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1157 td->td_retval[0] = ksi.ksi_signo;
1162 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1163 struct timespec *timeout)
1168 int error, sig, hz, i, timevalid = 0;
1169 struct timespec rts, ets, ts;
1175 SIG_CANTMASK(waitset);
1179 savedmask = td->td_sigmask;
1181 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1183 getnanouptime(&rts);
1185 timespecadd(&ets, timeout);
1190 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1191 if (!SIGISMEMBER(waitset, i))
1193 if (SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1194 SIGFILLSET(td->td_sigmask);
1195 SIG_CANTMASK(td->td_sigmask);
1196 SIGDELSET(td->td_sigmask, i);
1197 mtx_lock(&ps->ps_mtx);
1200 mtx_unlock(&ps->ps_mtx);
1201 } else if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
1202 if (p->p_flag & P_SA) {
1203 p->p_flag |= P_SIGEVENT;
1204 wakeup(&p->p_siglist);
1206 sigqueue_move(&p->p_sigqueue, &td->td_sigqueue, i);
1207 SIGFILLSET(td->td_sigmask);
1208 SIG_CANTMASK(td->td_sigmask);
1209 SIGDELSET(td->td_sigmask, i);
1210 mtx_lock(&ps->ps_mtx);
1213 mtx_unlock(&ps->ps_mtx);
1222 * POSIX says this must be checked after looking for pending
1230 getnanouptime(&rts);
1231 if (timespeccmp(&rts, &ets, >=)) {
1236 timespecsub(&ts, &rts);
1237 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1242 td->td_sigmask = savedmask;
1243 SIGSETNAND(td->td_sigmask, waitset);
1245 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1247 if (error == ERESTART) {
1248 /* timeout can not be restarted. */
1250 } else if (error == EAGAIN) {
1251 /* will calculate timeout by ourself. */
1262 sigqueue_get(&td->td_sigqueue, sig, ksi);
1263 ksi->ksi_signo = sig;
1264 if (ksi->ksi_code == SI_TIMER)
1265 itimer_accept(p, ksi->ksi_timerid, ksi);
1267 mtx_lock(&ps->ps_mtx);
1268 action = ps->ps_sigact[_SIG_IDX(sig)];
1269 mtx_unlock(&ps->ps_mtx);
1271 if (KTRPOINT(td, KTR_PSIG))
1272 ktrpsig(sig, action, &td->td_sigmask, 0);
1274 _STOPEVENT(p, S_SIG, sig);
1277 td->td_sigmask = savedmask;
1283 #ifndef _SYS_SYSPROTO_H_
1284 struct sigpending_args {
1294 struct sigpending_args *uap;
1296 struct proc *p = td->td_proc;
1300 pending = p->p_sigqueue.sq_signals;
1301 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1303 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1306 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1307 #ifndef _SYS_SYSPROTO_H_
1308 struct osigpending_args {
1316 osigpending(td, uap)
1318 struct osigpending_args *uap;
1320 struct proc *p = td->td_proc;
1324 pending = p->p_sigqueue.sq_signals;
1325 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1327 SIG2OSIG(pending, td->td_retval[0]);
1330 #endif /* COMPAT_43 */
1332 #if defined(COMPAT_43)
1334 * Generalized interface signal handler, 4.3-compatible.
1336 #ifndef _SYS_SYSPROTO_H_
1337 struct osigvec_args {
1350 register struct osigvec_args *uap;
1353 struct sigaction nsa, osa;
1354 register struct sigaction *nsap, *osap;
1357 if (uap->signum <= 0 || uap->signum >= ONSIG)
1359 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1360 osap = (uap->osv != NULL) ? &osa : NULL;
1362 error = copyin(uap->nsv, &vec, sizeof(vec));
1365 nsap->sa_handler = vec.sv_handler;
1366 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1367 nsap->sa_flags = vec.sv_flags;
1368 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1370 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1371 if (osap && !error) {
1372 vec.sv_handler = osap->sa_handler;
1373 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1374 vec.sv_flags = osap->sa_flags;
1375 vec.sv_flags &= ~SA_NOCLDWAIT;
1376 vec.sv_flags ^= SA_RESTART;
1377 error = copyout(&vec, uap->osv, sizeof(vec));
1382 #ifndef _SYS_SYSPROTO_H_
1383 struct osigblock_args {
1392 register struct thread *td;
1393 struct osigblock_args *uap;
1395 struct proc *p = td->td_proc;
1398 OSIG2SIG(uap->mask, set);
1401 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1402 SIGSETOR(td->td_sigmask, set);
1407 #ifndef _SYS_SYSPROTO_H_
1408 struct osigsetmask_args {
1416 osigsetmask(td, uap)
1418 struct osigsetmask_args *uap;
1420 struct proc *p = td->td_proc;
1423 OSIG2SIG(uap->mask, set);
1426 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1427 SIGSETLO(td->td_sigmask, set);
1432 #endif /* COMPAT_43 */
1435 * Suspend calling thread until signal, providing mask to be set
1438 #ifndef _SYS_SYSPROTO_H_
1439 struct sigsuspend_args {
1440 const sigset_t *sigmask;
1450 struct sigsuspend_args *uap;
1455 error = copyin(uap->sigmask, &mask, sizeof(mask));
1458 return (kern_sigsuspend(td, mask));
1462 kern_sigsuspend(struct thread *td, sigset_t mask)
1464 struct proc *p = td->td_proc;
1467 * When returning from sigsuspend, we want
1468 * the old mask to be restored after the
1469 * signal handler has finished. Thus, we
1470 * save it here and mark the sigacts structure
1474 td->td_oldsigmask = td->td_sigmask;
1475 td->td_pflags |= TDP_OLDMASK;
1477 td->td_sigmask = mask;
1479 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1482 /* always return EINTR rather than ERESTART... */
1486 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1488 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1489 * convention: libc stub passes mask, not pointer, to save a copyin.
1491 #ifndef _SYS_SYSPROTO_H_
1492 struct osigsuspend_args {
1501 osigsuspend(td, uap)
1503 struct osigsuspend_args *uap;
1505 struct proc *p = td->td_proc;
1509 td->td_oldsigmask = td->td_sigmask;
1510 td->td_pflags |= TDP_OLDMASK;
1511 OSIG2SIG(uap->mask, mask);
1513 SIGSETLO(td->td_sigmask, mask);
1515 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1518 /* always return EINTR rather than ERESTART... */
1521 #endif /* COMPAT_43 */
1523 #if defined(COMPAT_43)
1524 #ifndef _SYS_SYSPROTO_H_
1525 struct osigstack_args {
1526 struct sigstack *nss;
1527 struct sigstack *oss;
1537 register struct osigstack_args *uap;
1539 struct sigstack nss, oss;
1542 if (uap->nss != NULL) {
1543 error = copyin(uap->nss, &nss, sizeof(nss));
1547 oss.ss_sp = td->td_sigstk.ss_sp;
1548 oss.ss_onstack = sigonstack(cpu_getstack(td));
1549 if (uap->nss != NULL) {
1550 td->td_sigstk.ss_sp = nss.ss_sp;
1551 td->td_sigstk.ss_size = 0;
1552 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1553 td->td_pflags |= TDP_ALTSTACK;
1555 if (uap->oss != NULL)
1556 error = copyout(&oss, uap->oss, sizeof(oss));
1560 #endif /* COMPAT_43 */
1562 #ifndef _SYS_SYSPROTO_H_
1563 struct sigaltstack_args {
1573 sigaltstack(td, uap)
1575 register struct sigaltstack_args *uap;
1580 if (uap->ss != NULL) {
1581 error = copyin(uap->ss, &ss, sizeof(ss));
1585 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1586 (uap->oss != NULL) ? &oss : NULL);
1589 if (uap->oss != NULL)
1590 error = copyout(&oss, uap->oss, sizeof(stack_t));
1595 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1597 struct proc *p = td->td_proc;
1600 oonstack = sigonstack(cpu_getstack(td));
1603 *oss = td->td_sigstk;
1604 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1605 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1611 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1613 if (!(ss->ss_flags & SS_DISABLE)) {
1614 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1617 td->td_sigstk = *ss;
1618 td->td_pflags |= TDP_ALTSTACK;
1620 td->td_pflags &= ~TDP_ALTSTACK;
1627 * Common code for kill process group/broadcast kill.
1628 * cp is calling process.
1631 killpg1(td, sig, pgid, all)
1632 register struct thread *td;
1635 register struct proc *p;
1643 sx_slock(&allproc_lock);
1644 LIST_FOREACH(p, &allproc, p_list) {
1646 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1651 if (p_cansignal(td, p, sig) == 0) {
1658 sx_sunlock(&allproc_lock);
1660 sx_slock(&proctree_lock);
1663 * zero pgid means send to my process group.
1665 pgrp = td->td_proc->p_pgrp;
1668 pgrp = pgfind(pgid);
1670 sx_sunlock(&proctree_lock);
1674 sx_sunlock(&proctree_lock);
1675 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1677 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1681 if (p_cansignal(td, p, sig) == 0) {
1690 return (nfound ? 0 : ESRCH);
1693 #ifndef _SYS_SYSPROTO_H_
1705 register struct thread *td;
1706 register struct kill_args *uap;
1708 register struct proc *p;
1711 if ((u_int)uap->signum > _SIG_MAXSIG)
1715 /* kill single process */
1716 if ((p = pfind(uap->pid)) == NULL) {
1717 if ((p = zpfind(uap->pid)) == NULL)
1720 error = p_cansignal(td, p, uap->signum);
1721 if (error == 0 && uap->signum)
1722 psignal(p, uap->signum);
1727 case -1: /* broadcast signal */
1728 return (killpg1(td, uap->signum, 0, 1));
1729 case 0: /* signal own process group */
1730 return (killpg1(td, uap->signum, 0, 0));
1731 default: /* negative explicit process group */
1732 return (killpg1(td, uap->signum, -uap->pid, 0));
1737 #if defined(COMPAT_43)
1738 #ifndef _SYS_SYSPROTO_H_
1739 struct okillpg_args {
1751 register struct okillpg_args *uap;
1754 if ((u_int)uap->signum > _SIG_MAXSIG)
1757 return (killpg1(td, uap->signum, uap->pgid, 0));
1759 #endif /* COMPAT_43 */
1761 #ifndef _SYS_SYSPROTO_H_
1762 struct sigqueue_args {
1765 /* union sigval */ void *value;
1770 sigqueue(struct thread *td, struct sigqueue_args *uap)
1776 if ((u_int)uap->signum > _SIG_MAXSIG)
1780 * Specification says sigqueue can only send signal to
1786 if ((p = pfind(uap->pid)) == NULL) {
1787 if ((p = zpfind(uap->pid)) == NULL)
1790 error = p_cansignal(td, p, uap->signum);
1791 if (error == 0 && uap->signum != 0) {
1792 ksiginfo_init(&ksi);
1793 ksi.ksi_signo = uap->signum;
1794 ksi.ksi_code = SI_QUEUE;
1795 ksi.ksi_pid = td->td_proc->p_pid;
1796 ksi.ksi_uid = td->td_ucred->cr_ruid;
1797 ksi.ksi_value.sival_ptr = uap->value;
1798 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1805 * Send a signal to a process group.
1814 sx_slock(&proctree_lock);
1815 pgrp = pgfind(pgid);
1816 sx_sunlock(&proctree_lock);
1818 pgsignal(pgrp, sig, 0);
1825 * Send a signal to a process group. If checktty is 1,
1826 * limit to members which have a controlling terminal.
1829 pgsignal(pgrp, sig, checkctty)
1833 register struct proc *p;
1836 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1837 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1839 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1847 * Send a signal caused by a trap to the current thread.
1848 * If it will be caught immediately, deliver it with correct code.
1849 * Otherwise, post it normally.
1854 trapsignal(struct thread *td, ksiginfo_t *ksi)
1863 sig = ksi->ksi_signo;
1864 code = ksi->ksi_code;
1865 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1867 if (td->td_pflags & TDP_SA) {
1868 if (td->td_mailbox == NULL)
1869 thread_user_enter(td);
1871 SIGDELSET(td->td_sigmask, sig);
1872 mtx_lock_spin(&sched_lock);
1874 * Force scheduling an upcall, so UTS has chance to
1875 * process the signal before thread runs again in
1879 td->td_upcall->ku_flags |= KUF_DOUPCALL;
1880 mtx_unlock_spin(&sched_lock);
1885 mtx_lock(&ps->ps_mtx);
1886 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1887 !SIGISMEMBER(td->td_sigmask, sig)) {
1888 p->p_stats->p_ru.ru_nsignals++;
1890 if (KTRPOINT(curthread, KTR_PSIG))
1891 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1892 &td->td_sigmask, code);
1894 if (!(td->td_pflags & TDP_SA))
1895 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1896 ksi, &td->td_sigmask);
1897 else if (td->td_mailbox == NULL) {
1898 mtx_unlock(&ps->ps_mtx);
1899 /* UTS caused a sync signal */
1900 p->p_code = code; /* XXX for core dump/debugger */
1901 p->p_sig = sig; /* XXX to verify code */
1904 mtx_unlock(&ps->ps_mtx);
1905 SIGADDSET(td->td_sigmask, sig);
1907 error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
1910 /* UTS memory corrupted */
1912 sigexit(td, SIGSEGV);
1913 mtx_lock(&ps->ps_mtx);
1915 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1916 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1917 SIGADDSET(td->td_sigmask, sig);
1918 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1920 * See kern_sigaction() for origin of this code.
1922 SIGDELSET(ps->ps_sigcatch, sig);
1923 if (sig != SIGCONT &&
1924 sigprop(sig) & SA_IGNORE)
1925 SIGADDSET(ps->ps_sigignore, sig);
1926 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1928 mtx_unlock(&ps->ps_mtx);
1931 * Avoid a possible infinite loop if the thread
1932 * masking the signal or process is ignoring the
1935 if (kern_forcesigexit &&
1936 (SIGISMEMBER(td->td_sigmask, sig) ||
1937 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1938 SIGDELSET(td->td_sigmask, sig);
1939 SIGDELSET(ps->ps_sigcatch, sig);
1940 SIGDELSET(ps->ps_sigignore, sig);
1941 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1943 mtx_unlock(&ps->ps_mtx);
1944 p->p_code = code; /* XXX for core dump/debugger */
1945 p->p_sig = sig; /* XXX to verify code */
1946 tdsignal(p, td, sig, ksi);
1951 static struct thread *
1952 sigtd(struct proc *p, int sig, int prop)
1954 struct thread *td, *signal_td;
1956 PROC_LOCK_ASSERT(p, MA_OWNED);
1959 * Check if current thread can handle the signal without
1960 * switching conetxt to another thread.
1962 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1965 mtx_lock_spin(&sched_lock);
1966 FOREACH_THREAD_IN_PROC(p, td) {
1967 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1972 if (signal_td == NULL)
1973 signal_td = FIRST_THREAD_IN_PROC(p);
1974 mtx_unlock_spin(&sched_lock);
1979 * Send the signal to the process. If the signal has an action, the action
1980 * is usually performed by the target process rather than the caller; we add
1981 * the signal to the set of pending signals for the process.
1984 * o When a stop signal is sent to a sleeping process that takes the
1985 * default action, the process is stopped without awakening it.
1986 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1987 * regardless of the signal action (eg, blocked or ignored).
1989 * Other ignored signals are discarded immediately.
1994 psignal(struct proc *p, int sig)
1996 (void) tdsignal(p, NULL, sig, NULL);
2000 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
2002 struct thread *td = NULL;
2004 PROC_LOCK_ASSERT(p, MA_OWNED);
2006 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
2009 * ksi_code and other fields should be set before
2010 * calling this function.
2012 ksi->ksi_signo = sigev->sigev_signo;
2013 ksi->ksi_value = sigev->sigev_value;
2014 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2015 td = thread_find(p, sigev->sigev_notify_thread_id);
2019 return (tdsignal(p, td, ksi->ksi_signo, ksi));
2026 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2031 if (p->p_flag & P_SA)
2032 saved = p->p_sigqueue.sq_signals;
2033 ret = do_tdsignal(p, td, sig, ksi);
2034 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2035 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
2036 /* pending set changed */
2037 p->p_flag |= P_SIGEVENT;
2038 wakeup(&p->p_siglist);
2045 do_tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2048 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 childproc_continued(p);
2208 if (action == SIG_DFL) {
2209 sigqueue_delete(sigqueue, sig);
2210 } else if (action == SIG_CATCH) {
2212 * The process wants to catch it so it needs
2213 * to run at least one thread, but which one?
2214 * It would seem that the answer would be to
2215 * run an upcall in the next KSE to run, and
2216 * deliver the signal that way. In a NON KSE
2217 * process, we need to make sure that the
2218 * single thread is runnable asap.
2219 * XXXKSE for now however, make them all run.
2224 * The signal is not ignored or caught.
2226 mtx_lock_spin(&sched_lock);
2227 thread_unsuspend(p);
2228 mtx_unlock_spin(&sched_lock);
2232 if (prop & SA_STOP) {
2234 * Already stopped, don't need to stop again
2235 * (If we did the shell could get confused).
2236 * Just make sure the signal STOP bit set.
2238 p->p_flag |= P_STOPPED_SIG;
2239 sigqueue_delete(sigqueue, sig);
2244 * All other kinds of signals:
2245 * If a thread is sleeping interruptibly, simulate a
2246 * wakeup so that when it is continued it will be made
2247 * runnable and can look at the signal. However, don't make
2248 * the PROCESS runnable, leave it stopped.
2249 * It may run a bit until it hits a thread_suspend_check().
2251 mtx_lock_spin(&sched_lock);
2252 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2254 mtx_unlock_spin(&sched_lock);
2257 * Mutexes are short lived. Threads waiting on them will
2258 * hit thread_suspend_check() soon.
2260 } else if (p->p_state == PRS_NORMAL) {
2261 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2262 mtx_lock_spin(&sched_lock);
2263 tdsigwakeup(td, sig, action);
2264 mtx_unlock_spin(&sched_lock);
2268 MPASS(action == SIG_DFL);
2270 if (prop & SA_STOP) {
2271 if (p->p_flag & P_PPWAIT)
2273 p->p_flag |= P_STOPPED_SIG;
2275 mtx_lock_spin(&sched_lock);
2276 FOREACH_THREAD_IN_PROC(p, td0) {
2277 if (TD_IS_SLEEPING(td0) &&
2278 (td0->td_flags & TDF_SINTR) &&
2279 !TD_IS_SUSPENDED(td0)) {
2280 thread_suspend_one(td0);
2282 td0->td_flags |= TDF_ASTPENDING;
2285 if (p->p_numthreads == p->p_suspcount) {
2287 * only thread sending signal to another
2288 * process can reach here, if thread is sending
2289 * signal to its process, because thread does
2290 * not suspend itself here, p_numthreads
2291 * should never be equal to p_suspcount.
2294 mtx_unlock_spin(&sched_lock);
2295 sigqueue_delete_proc(p, p->p_xstat);
2297 mtx_unlock_spin(&sched_lock);
2304 /* Not in "NORMAL" state. discard the signal. */
2305 sigqueue_delete(sigqueue, sig);
2310 * The process is not stopped so we need to apply the signal to all the
2315 mtx_lock_spin(&sched_lock);
2316 tdsigwakeup(td, sig, action);
2317 thread_unsuspend(p);
2318 mtx_unlock_spin(&sched_lock);
2320 /* If we jump here, sched_lock should not be owned. */
2321 mtx_assert(&sched_lock, MA_NOTOWNED);
2326 * The force of a signal has been directed against a single
2327 * thread. We need to see what we can do about knocking it
2328 * out of any sleep it may be in etc.
2331 tdsigwakeup(struct thread *td, int sig, sig_t action)
2333 struct proc *p = td->td_proc;
2336 PROC_LOCK_ASSERT(p, MA_OWNED);
2337 mtx_assert(&sched_lock, MA_OWNED);
2338 prop = sigprop(sig);
2341 * Bring the priority of a thread up if we want it to get
2342 * killed in this lifetime.
2344 if (action == SIG_DFL && (prop & SA_KILL)) {
2346 sched_nice(td->td_proc, 0);
2347 if (td->td_priority > PUSER)
2348 sched_prio(td, PUSER);
2351 if (TD_ON_SLEEPQ(td)) {
2353 * If thread is sleeping uninterruptibly
2354 * we can't interrupt the sleep... the signal will
2355 * be noticed when the process returns through
2356 * trap() or syscall().
2358 if ((td->td_flags & TDF_SINTR) == 0)
2361 * If SIGCONT is default (or ignored) and process is
2362 * asleep, we are finished; the process should not
2365 if ((prop & SA_CONT) && action == SIG_DFL) {
2366 mtx_unlock_spin(&sched_lock);
2367 sigqueue_delete(&p->p_sigqueue, sig);
2369 * It may be on either list in this state.
2370 * Remove from both for now.
2372 sigqueue_delete(&td->td_sigqueue, sig);
2373 mtx_lock_spin(&sched_lock);
2378 * Give low priority threads a better chance to run.
2380 if (td->td_priority > PUSER)
2381 sched_prio(td, PUSER);
2386 * Other states do nothing with the signal immediately,
2387 * other than kicking ourselves if we are running.
2388 * It will either never be noticed, or noticed very soon.
2391 if (TD_IS_RUNNING(td) && td != curthread)
2398 ptracestop(struct thread *td, int sig)
2400 struct proc *p = td->td_proc;
2403 PROC_LOCK_ASSERT(p, MA_OWNED);
2404 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2405 &p->p_mtx.mtx_object, "Stopping for traced signal");
2407 mtx_lock_spin(&sched_lock);
2408 td->td_flags |= TDF_XSIG;
2409 mtx_unlock_spin(&sched_lock);
2411 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2412 if (p->p_flag & P_SINGLE_EXIT) {
2413 mtx_lock_spin(&sched_lock);
2414 td->td_flags &= ~TDF_XSIG;
2415 mtx_unlock_spin(&sched_lock);
2419 * Just make wait() to work, the last stopped thread
2424 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2425 mtx_lock_spin(&sched_lock);
2426 FOREACH_THREAD_IN_PROC(p, td0) {
2427 if (TD_IS_SLEEPING(td0) &&
2428 (td0->td_flags & TDF_SINTR) &&
2429 !TD_IS_SUSPENDED(td0)) {
2430 thread_suspend_one(td0);
2431 } else if (td != td0) {
2432 td0->td_flags |= TDF_ASTPENDING;
2437 thread_suspend_one(td);
2440 mi_switch(SW_VOL, NULL);
2441 mtx_unlock_spin(&sched_lock);
2444 if (!(p->p_flag & P_TRACED))
2446 if (td->td_flags & TDF_DBSUSPEND) {
2447 if (p->p_flag & P_SINGLE_EXIT)
2449 mtx_lock_spin(&sched_lock);
2453 return (td->td_xsig);
2457 * If the current process has received a signal (should be caught or cause
2458 * termination, should interrupt current syscall), return the signal number.
2459 * Stop signals with default action are processed immediately, then cleared;
2460 * they aren't returned. This is checked after each entry to the system for
2461 * a syscall or trap (though this can usually be done without calling issignal
2462 * by checking the pending signal masks in cursig.) The normal call
2465 * while (sig = cursig(curthread))
2474 sigset_t sigpending;
2475 int sig, prop, newsig;
2480 mtx_assert(&ps->ps_mtx, MA_OWNED);
2481 PROC_LOCK_ASSERT(p, MA_OWNED);
2483 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2485 sigpending = td->td_sigqueue.sq_signals;
2486 SIGSETNAND(sigpending, td->td_sigmask);
2488 if (p->p_flag & P_PPWAIT)
2489 SIG_STOPSIGMASK(sigpending);
2490 if (SIGISEMPTY(sigpending)) /* no signal to send */
2492 sig = sig_ffs(&sigpending);
2494 if (p->p_stops & S_SIG) {
2495 mtx_unlock(&ps->ps_mtx);
2496 stopevent(p, S_SIG, sig);
2497 mtx_lock(&ps->ps_mtx);
2501 * We should see pending but ignored signals
2502 * only if P_TRACED was on when they were posted.
2504 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2505 sigqueue_delete(&td->td_sigqueue, sig);
2506 if (td->td_pflags & TDP_SA)
2507 SIGADDSET(td->td_sigmask, sig);
2510 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2512 * If traced, always stop.
2514 mtx_unlock(&ps->ps_mtx);
2515 newsig = ptracestop(td, sig);
2516 mtx_lock(&ps->ps_mtx);
2518 if (td->td_pflags & TDP_SA)
2519 SIGADDSET(td->td_sigmask, sig);
2521 if (sig != newsig) {
2525 * XXX shrug off debugger, it causes siginfo to
2528 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2531 * If parent wants us to take the signal,
2532 * then it will leave it in p->p_xstat;
2533 * otherwise we just look for signals again.
2540 * Put the new signal into td_sigqueue. If the
2541 * signal is being masked, look for other signals.
2543 SIGADDSET(td->td_sigqueue.sq_signals, sig);
2544 if (td->td_pflags & TDP_SA)
2545 SIGDELSET(td->td_sigmask, sig);
2546 if (SIGISMEMBER(td->td_sigmask, sig))
2552 * If the traced bit got turned off, go back up
2553 * to the top to rescan signals. This ensures
2554 * that p_sig* and p_sigact are consistent.
2556 if ((p->p_flag & P_TRACED) == 0)
2560 prop = sigprop(sig);
2563 * Decide whether the signal should be returned.
2564 * Return the signal's number, or fall through
2565 * to clear it from the pending mask.
2567 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2569 case (intptr_t)SIG_DFL:
2571 * Don't take default actions on system processes.
2573 if (p->p_pid <= 1) {
2576 * Are you sure you want to ignore SIGSEGV
2579 printf("Process (pid %lu) got signal %d\n",
2580 (u_long)p->p_pid, sig);
2582 break; /* == ignore */
2585 * If there is a pending stop signal to process
2586 * with default action, stop here,
2587 * then clear the signal. However,
2588 * if process is member of an orphaned
2589 * process group, ignore tty stop signals.
2591 if (prop & SA_STOP) {
2592 if (p->p_flag & P_TRACED ||
2593 (p->p_pgrp->pg_jobc == 0 &&
2595 break; /* == ignore */
2596 mtx_unlock(&ps->ps_mtx);
2597 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2598 &p->p_mtx.mtx_object, "Catching SIGSTOP");
2599 p->p_flag |= P_STOPPED_SIG;
2601 mtx_lock_spin(&sched_lock);
2602 FOREACH_THREAD_IN_PROC(p, td0) {
2603 if (TD_IS_SLEEPING(td0) &&
2604 (td0->td_flags & TDF_SINTR) &&
2605 !TD_IS_SUSPENDED(td0)) {
2606 thread_suspend_one(td0);
2607 } else if (td != td0) {
2608 td0->td_flags |= TDF_ASTPENDING;
2612 thread_suspend_one(td);
2615 mi_switch(SW_INVOL, NULL);
2616 mtx_unlock_spin(&sched_lock);
2619 mtx_lock(&ps->ps_mtx);
2621 } else if (prop & SA_IGNORE) {
2623 * Except for SIGCONT, shouldn't get here.
2624 * Default action is to ignore; drop it.
2626 break; /* == ignore */
2631 case (intptr_t)SIG_IGN:
2633 * Masking above should prevent us ever trying
2634 * to take action on an ignored signal other
2635 * than SIGCONT, unless process is traced.
2637 if ((prop & SA_CONT) == 0 &&
2638 (p->p_flag & P_TRACED) == 0)
2639 printf("issignal\n");
2640 break; /* == ignore */
2644 * This signal has an action, let
2645 * postsig() process it.
2649 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2658 thread_stopped(struct proc *p)
2660 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);
2674 * Wake up parent sleeping in kern_wait(), also send
2675 * SIGCHLD to parent, but SIGCHLD does not guarantee
2676 * that parent will awake, because parent may masked
2679 p->p_pptr->p_flag |= P_STATCHILD;
2681 ps = p->p_pptr->p_sigacts;
2682 mtx_lock(&ps->ps_mtx);
2683 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2684 mtx_unlock(&ps->ps_mtx);
2685 childproc_stopped(p,
2686 (p->p_flag & P_TRACED) ?
2687 CLD_TRAPPED : CLD_STOPPED);
2689 mtx_unlock(&ps->ps_mtx);
2690 PROC_UNLOCK(p->p_pptr);
2691 mtx_lock_spin(&sched_lock);
2696 * Take the action for the specified signal
2697 * from the current set of pending signals.
2703 struct thread *td = curthread;
2704 register struct proc *p = td->td_proc;
2708 sigset_t returnmask;
2711 KASSERT(sig != 0, ("postsig"));
2713 PROC_LOCK_ASSERT(p, MA_OWNED);
2715 mtx_assert(&ps->ps_mtx, MA_OWNED);
2716 ksiginfo_init(&ksi);
2717 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2718 ksi.ksi_signo = sig;
2719 if (ksi.ksi_code == SI_TIMER)
2720 itimer_accept(p, ksi.ksi_timerid, &ksi);
2721 action = ps->ps_sigact[_SIG_IDX(sig)];
2723 if (KTRPOINT(td, KTR_PSIG))
2724 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2725 &td->td_oldsigmask : &td->td_sigmask, 0);
2727 if (p->p_stops & S_SIG) {
2728 mtx_unlock(&ps->ps_mtx);
2729 stopevent(p, S_SIG, sig);
2730 mtx_lock(&ps->ps_mtx);
2733 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2735 * Default action, where the default is to kill
2736 * the process. (Other cases were ignored above.)
2738 mtx_unlock(&ps->ps_mtx);
2742 if (td->td_pflags & TDP_SA) {
2743 if (sig == SIGKILL) {
2744 mtx_unlock(&ps->ps_mtx);
2750 * If we get here, the signal must be caught.
2752 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2753 ("postsig action"));
2755 * Set the new mask value and also defer further
2756 * occurrences of this signal.
2758 * Special case: user has done a sigsuspend. Here the
2759 * current mask is not of interest, but rather the
2760 * mask from before the sigsuspend is what we want
2761 * restored after the signal processing is completed.
2763 if (td->td_pflags & TDP_OLDMASK) {
2764 returnmask = td->td_oldsigmask;
2765 td->td_pflags &= ~TDP_OLDMASK;
2767 returnmask = td->td_sigmask;
2769 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2770 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2771 SIGADDSET(td->td_sigmask, sig);
2773 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2775 * See kern_sigaction() for origin of this code.
2777 SIGDELSET(ps->ps_sigcatch, sig);
2778 if (sig != SIGCONT &&
2779 sigprop(sig) & SA_IGNORE)
2780 SIGADDSET(ps->ps_sigignore, sig);
2781 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2783 p->p_stats->p_ru.ru_nsignals++;
2784 if (p->p_sig != sig) {
2791 if (td->td_pflags & TDP_SA)
2792 thread_signal_add(curthread, &ksi);
2794 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2799 * Kill the current process for stated reason.
2807 PROC_LOCK_ASSERT(p, MA_OWNED);
2808 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2809 p, p->p_pid, p->p_comm);
2810 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2811 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2812 psignal(p, SIGKILL);
2816 * Force the current process to exit with the specified signal, dumping core
2817 * if appropriate. We bypass the normal tests for masked and caught signals,
2818 * allowing unrecoverable failures to terminate the process without changing
2819 * signal state. Mark the accounting record with the signal termination.
2820 * If dumping core, save the signal number for the debugger. Calls exit and
2830 struct proc *p = td->td_proc;
2832 PROC_LOCK_ASSERT(p, MA_OWNED);
2833 p->p_acflag |= AXSIG;
2835 * We must be single-threading to generate a core dump. This
2836 * ensures that the registers in the core file are up-to-date.
2837 * Also, the ELF dump handler assumes that the thread list doesn't
2838 * change out from under it.
2840 * XXX If another thread attempts to single-thread before us
2841 * (e.g. via fork()), we won't get a dump at all.
2843 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2846 * Log signals which would cause core dumps
2847 * (Log as LOG_INFO to appease those who don't want
2849 * XXX : Todo, as well as euid, write out ruid too
2850 * Note that coredump() drops proc lock.
2852 if (coredump(td) == 0)
2854 if (kern_logsigexit)
2856 "pid %d (%s), uid %d: exited on signal %d%s\n",
2857 p->p_pid, p->p_comm,
2858 td->td_ucred ? td->td_ucred->cr_uid : -1,
2860 sig & WCOREFLAG ? " (core dumped)" : "");
2863 exit1(td, W_EXITCODE(0, sig));
2868 * Send queued SIGCHLD to parent when child process is stopped
2872 childproc_stopped(struct proc *p, int reason)
2874 PROC_LOCK_ASSERT(p, MA_OWNED);
2875 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2877 if (p->p_ksi != NULL) {
2878 p->p_ksi->ksi_signo = SIGCHLD;
2879 p->p_ksi->ksi_code = reason;
2880 p->p_ksi->ksi_status = p->p_xstat;
2881 p->p_ksi->ksi_pid = p->p_pid;
2882 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2883 if (KSI_ONQ(p->p_ksi))
2886 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2890 childproc_continued(struct proc *p)
2892 PROC_LOCK_ASSERT(p, MA_OWNED);
2893 PROC_LOCK_ASSERT(p->p_pptr, MA_NOTOWNED);
2895 PROC_LOCK(p->p_pptr);
2896 if (p->p_ksi != NULL) {
2897 p->p_ksi->ksi_signo = SIGCHLD;
2898 p->p_ksi->ksi_code = CLD_CONTINUED;
2899 p->p_ksi->ksi_status = SIGCONT;
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)) {
2903 PROC_UNLOCK(p->p_pptr);
2907 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2908 PROC_UNLOCK(p->p_pptr);
2912 childproc_exited(struct proc *p)
2915 int status = p->p_xstat; /* convert to int */
2917 reason = CLD_EXITED;
2918 if (WCOREDUMP(status))
2919 reason = CLD_DUMPED;
2920 else if (WIFSIGNALED(status))
2921 reason = CLD_KILLED;
2922 childproc_stopped(p, reason);
2925 static char corefilename[MAXPATHLEN] = {"%N.core"};
2926 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2927 sizeof(corefilename), "process corefile name format string");
2930 * expand_name(name, uid, pid)
2931 * Expand the name described in corefilename, using name, uid, and pid.
2932 * corefilename is a printf-like string, with three format specifiers:
2933 * %N name of process ("name")
2934 * %P process id (pid)
2936 * For example, "%N.core" is the default; they can be disabled completely
2937 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2938 * This is controlled by the sysctl variable kern.corefile (see above).
2942 expand_name(name, uid, pid)
2947 const char *format, *appendstr;
2949 char buf[11]; /* Buffer for pid/uid -- max 4B */
2952 format = corefilename;
2953 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2956 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2957 switch (format[i]) {
2958 case '%': /* Format character */
2960 switch (format[i]) {
2964 case 'N': /* process name */
2967 case 'P': /* process id */
2968 sprintf(buf, "%u", pid);
2971 case 'U': /* user id */
2972 sprintf(buf, "%u", uid);
2978 "Unknown format character %c in `%s'\n",
2981 l = strlen(appendstr);
2982 if ((n + l) >= MAXPATHLEN)
2984 memcpy(temp + n, appendstr, l);
2988 temp[n++] = format[i];
2991 if (format[i] != '\0')
2995 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2996 (long)pid, name, (u_long)uid);
3002 * Dump a process' core. The main routine does some
3003 * policy checking, and creates the name of the coredump;
3004 * then it passes on a vnode and a size limit to the process-specific
3005 * coredump routine if there is one; if there _is not_ one, it returns
3006 * ENOSYS; otherwise it returns the error from the process-specific routine.
3010 coredump(struct thread *td)
3012 struct proc *p = td->td_proc;
3013 register struct vnode *vp;
3014 register struct ucred *cred = td->td_ucred;
3016 struct nameidata nd;
3018 int error, error1, flags, locked;
3020 char *name; /* name of corefile */
3023 PROC_LOCK_ASSERT(p, MA_OWNED);
3024 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3025 _STOPEVENT(p, S_CORE, 0);
3027 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3033 * Note that the bulk of limit checking is done after
3034 * the corefile is created. The exception is if the limit
3035 * for corefiles is 0, in which case we don't bother
3036 * creating the corefile at all. This layout means that
3037 * a corefile is truncated instead of not being created,
3038 * if it is larger than the limit.
3040 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3047 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
3052 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
3053 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3054 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
3060 NDFREE(&nd, NDF_ONLY_PNBUF);
3063 /* Don't dump to non-regular files or files with links. */
3064 if (vp->v_type != VREG ||
3065 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
3066 VOP_UNLOCK(vp, 0, td);
3071 VOP_UNLOCK(vp, 0, td);
3072 lf.l_whence = SEEK_SET;
3075 lf.l_type = F_WRLCK;
3076 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3078 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3079 lf.l_type = F_UNLCK;
3081 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3082 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3084 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3091 if (set_core_nodump_flag)
3092 vattr.va_flags = UF_NODUMP;
3093 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3094 VOP_LEASE(vp, td, cred, LEASE_WRITE);
3095 VOP_SETATTR(vp, &vattr, cred, td);
3096 VOP_UNLOCK(vp, 0, td);
3098 p->p_acflag |= ACORE;
3101 error = p->p_sysent->sv_coredump ?
3102 p->p_sysent->sv_coredump(td, vp, limit) :
3106 lf.l_type = F_UNLCK;
3107 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3109 vn_finished_write(mp);
3111 error1 = vn_close(vp, FWRITE, cred, td);
3119 * Nonexistent system call-- signal process (may want to handle it).
3120 * Flag error in case process won't see signal immediately (blocked or ignored).
3122 #ifndef _SYS_SYSPROTO_H_
3134 struct nosys_args *args;
3136 struct proc *p = td->td_proc;
3145 * Send a SIGIO or SIGURG signal to a process or process group using
3146 * stored credentials rather than those of the current process.
3149 pgsigio(sigiop, sig, checkctty)
3150 struct sigio **sigiop;
3153 struct sigio *sigio;
3157 if (sigio == NULL) {
3161 if (sigio->sio_pgid > 0) {
3162 PROC_LOCK(sigio->sio_proc);
3163 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3164 psignal(sigio->sio_proc, sig);
3165 PROC_UNLOCK(sigio->sio_proc);
3166 } else if (sigio->sio_pgid < 0) {
3169 PGRP_LOCK(sigio->sio_pgrp);
3170 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3172 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3173 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3177 PGRP_UNLOCK(sigio->sio_pgrp);
3183 filt_sigattach(struct knote *kn)
3185 struct proc *p = curproc;
3187 kn->kn_ptr.p_proc = p;
3188 kn->kn_flags |= EV_CLEAR; /* automatically set */
3190 knlist_add(&p->p_klist, kn, 0);
3196 filt_sigdetach(struct knote *kn)
3198 struct proc *p = kn->kn_ptr.p_proc;
3200 knlist_remove(&p->p_klist, kn, 0);
3204 * signal knotes are shared with proc knotes, so we apply a mask to
3205 * the hint in order to differentiate them from process hints. This
3206 * could be avoided by using a signal-specific knote list, but probably
3207 * isn't worth the trouble.
3210 filt_signal(struct knote *kn, long hint)
3213 if (hint & NOTE_SIGNAL) {
3214 hint &= ~NOTE_SIGNAL;
3216 if (kn->kn_id == hint)
3219 return (kn->kn_data != 0);
3227 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3229 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3234 sigacts_free(struct sigacts *ps)
3237 mtx_lock(&ps->ps_mtx);
3239 if (ps->ps_refcnt == 0) {
3240 mtx_destroy(&ps->ps_mtx);
3241 free(ps, M_SUBPROC);
3243 mtx_unlock(&ps->ps_mtx);
3247 sigacts_hold(struct sigacts *ps)
3249 mtx_lock(&ps->ps_mtx);
3251 mtx_unlock(&ps->ps_mtx);
3256 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3259 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3260 mtx_lock(&src->ps_mtx);
3261 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3262 mtx_unlock(&src->ps_mtx);
3266 sigacts_shared(struct sigacts *ps)
3270 mtx_lock(&ps->ps_mtx);
3271 shared = ps->ps_refcnt > 1;
3272 mtx_unlock(&ps->ps_mtx);