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 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 FOREACH_THREAD_IN_PROC(p, td0) {
2280 if (TD_IS_SLEEPING(td0) &&
2281 (td0->td_flags & TDF_SINTR) &&
2282 !TD_IS_SUSPENDED(td0)) {
2283 thread_suspend_one(td0);
2285 td0->td_flags |= TDF_ASTPENDING;
2288 if (p->p_numthreads == p->p_suspcount) {
2290 * only thread sending signal to another
2291 * process can reach here, if thread is sending
2292 * signal to its process, because thread does
2293 * not suspend itself here, p_numthreads
2294 * should never be equal to p_suspcount.
2297 mtx_unlock_spin(&sched_lock);
2298 sigqueue_delete_proc(p, p->p_xstat);
2300 mtx_unlock_spin(&sched_lock);
2307 /* Not in "NORMAL" state. discard the signal. */
2308 sigqueue_delete(sigqueue, sig);
2313 * The process is not stopped so we need to apply the signal to all the
2318 mtx_lock_spin(&sched_lock);
2319 tdsigwakeup(td, sig, action);
2320 thread_unsuspend(p);
2321 mtx_unlock_spin(&sched_lock);
2323 /* If we jump here, sched_lock should not be owned. */
2324 mtx_assert(&sched_lock, MA_NOTOWNED);
2329 * The force of a signal has been directed against a single
2330 * thread. We need to see what we can do about knocking it
2331 * out of any sleep it may be in etc.
2334 tdsigwakeup(struct thread *td, int sig, sig_t action)
2336 struct proc *p = td->td_proc;
2339 PROC_LOCK_ASSERT(p, MA_OWNED);
2340 mtx_assert(&sched_lock, MA_OWNED);
2341 prop = sigprop(sig);
2344 * Bring the priority of a thread up if we want it to get
2345 * killed in this lifetime.
2347 if (action == SIG_DFL && (prop & SA_KILL)) {
2349 sched_nice(td->td_proc, 0);
2350 if (td->td_priority > PUSER)
2351 sched_prio(td, PUSER);
2354 if (TD_ON_SLEEPQ(td)) {
2356 * If thread is sleeping uninterruptibly
2357 * we can't interrupt the sleep... the signal will
2358 * be noticed when the process returns through
2359 * trap() or syscall().
2361 if ((td->td_flags & TDF_SINTR) == 0)
2364 * If SIGCONT is default (or ignored) and process is
2365 * asleep, we are finished; the process should not
2368 if ((prop & SA_CONT) && action == SIG_DFL) {
2369 mtx_unlock_spin(&sched_lock);
2370 sigqueue_delete(&p->p_sigqueue, sig);
2372 * It may be on either list in this state.
2373 * Remove from both for now.
2375 sigqueue_delete(&td->td_sigqueue, sig);
2376 mtx_lock_spin(&sched_lock);
2381 * Give low priority threads a better chance to run.
2383 if (td->td_priority > PUSER)
2384 sched_prio(td, PUSER);
2389 * Other states do nothing with the signal immediately,
2390 * other than kicking ourselves if we are running.
2391 * It will either never be noticed, or noticed very soon.
2394 if (TD_IS_RUNNING(td) && td != curthread)
2401 ptracestop(struct thread *td, int sig)
2403 struct proc *p = td->td_proc;
2406 PROC_LOCK_ASSERT(p, MA_OWNED);
2407 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2408 &p->p_mtx.mtx_object, "Stopping for traced signal");
2410 mtx_lock_spin(&sched_lock);
2411 td->td_flags |= TDF_XSIG;
2412 mtx_unlock_spin(&sched_lock);
2414 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2415 if (p->p_flag & P_SINGLE_EXIT) {
2416 mtx_lock_spin(&sched_lock);
2417 td->td_flags &= ~TDF_XSIG;
2418 mtx_unlock_spin(&sched_lock);
2422 * Just make wait() to work, the last stopped thread
2427 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2428 mtx_lock_spin(&sched_lock);
2429 FOREACH_THREAD_IN_PROC(p, td0) {
2430 if (TD_IS_SLEEPING(td0) &&
2431 (td0->td_flags & TDF_SINTR) &&
2432 !TD_IS_SUSPENDED(td0)) {
2433 thread_suspend_one(td0);
2434 } else if (td != td0) {
2435 td0->td_flags |= TDF_ASTPENDING;
2440 thread_suspend_one(td);
2443 mi_switch(SW_VOL, NULL);
2444 mtx_unlock_spin(&sched_lock);
2447 if (!(p->p_flag & P_TRACED))
2449 if (td->td_flags & TDF_DBSUSPEND) {
2450 if (p->p_flag & P_SINGLE_EXIT)
2452 mtx_lock_spin(&sched_lock);
2456 return (td->td_xsig);
2460 * If the current process has received a signal (should be caught or cause
2461 * termination, should interrupt current syscall), return the signal number.
2462 * Stop signals with default action are processed immediately, then cleared;
2463 * they aren't returned. This is checked after each entry to the system for
2464 * a syscall or trap (though this can usually be done without calling issignal
2465 * by checking the pending signal masks in cursig.) The normal call
2468 * while (sig = cursig(curthread))
2477 sigset_t sigpending;
2478 int sig, prop, newsig;
2483 mtx_assert(&ps->ps_mtx, MA_OWNED);
2484 PROC_LOCK_ASSERT(p, MA_OWNED);
2486 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2488 sigpending = td->td_sigqueue.sq_signals;
2489 SIGSETNAND(sigpending, td->td_sigmask);
2491 if (p->p_flag & P_PPWAIT)
2492 SIG_STOPSIGMASK(sigpending);
2493 if (SIGISEMPTY(sigpending)) /* no signal to send */
2495 sig = sig_ffs(&sigpending);
2497 if (p->p_stops & S_SIG) {
2498 mtx_unlock(&ps->ps_mtx);
2499 stopevent(p, S_SIG, sig);
2500 mtx_lock(&ps->ps_mtx);
2504 * We should see pending but ignored signals
2505 * only if P_TRACED was on when they were posted.
2507 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2508 sigqueue_delete(&td->td_sigqueue, sig);
2509 if (td->td_pflags & TDP_SA)
2510 SIGADDSET(td->td_sigmask, sig);
2513 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2515 * If traced, always stop.
2517 mtx_unlock(&ps->ps_mtx);
2518 newsig = ptracestop(td, sig);
2519 mtx_lock(&ps->ps_mtx);
2521 if (td->td_pflags & TDP_SA)
2522 SIGADDSET(td->td_sigmask, sig);
2524 if (sig != newsig) {
2528 * XXX shrug off debugger, it causes siginfo to
2531 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2534 * If parent wants us to take the signal,
2535 * then it will leave it in p->p_xstat;
2536 * otherwise we just look for signals again.
2543 * Put the new signal into td_sigqueue. If the
2544 * signal is being masked, look for other signals.
2546 SIGADDSET(td->td_sigqueue.sq_signals, sig);
2547 if (td->td_pflags & TDP_SA)
2548 SIGDELSET(td->td_sigmask, sig);
2549 if (SIGISMEMBER(td->td_sigmask, sig))
2555 * If the traced bit got turned off, go back up
2556 * to the top to rescan signals. This ensures
2557 * that p_sig* and p_sigact are consistent.
2559 if ((p->p_flag & P_TRACED) == 0)
2563 prop = sigprop(sig);
2566 * Decide whether the signal should be returned.
2567 * Return the signal's number, or fall through
2568 * to clear it from the pending mask.
2570 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2572 case (intptr_t)SIG_DFL:
2574 * Don't take default actions on system processes.
2576 if (p->p_pid <= 1) {
2579 * Are you sure you want to ignore SIGSEGV
2582 printf("Process (pid %lu) got signal %d\n",
2583 (u_long)p->p_pid, sig);
2585 break; /* == ignore */
2588 * If there is a pending stop signal to process
2589 * with default action, stop here,
2590 * then clear the signal. However,
2591 * if process is member of an orphaned
2592 * process group, ignore tty stop signals.
2594 if (prop & SA_STOP) {
2595 if (p->p_flag & P_TRACED ||
2596 (p->p_pgrp->pg_jobc == 0 &&
2598 break; /* == ignore */
2599 mtx_unlock(&ps->ps_mtx);
2600 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2601 &p->p_mtx.mtx_object, "Catching SIGSTOP");
2602 p->p_flag |= P_STOPPED_SIG;
2604 mtx_lock_spin(&sched_lock);
2605 FOREACH_THREAD_IN_PROC(p, td0) {
2606 if (TD_IS_SLEEPING(td0) &&
2607 (td0->td_flags & TDF_SINTR) &&
2608 !TD_IS_SUSPENDED(td0)) {
2609 thread_suspend_one(td0);
2610 } else if (td != td0) {
2611 td0->td_flags |= TDF_ASTPENDING;
2615 thread_suspend_one(td);
2618 mi_switch(SW_INVOL, NULL);
2619 mtx_unlock_spin(&sched_lock);
2622 mtx_lock(&ps->ps_mtx);
2624 } else if (prop & SA_IGNORE) {
2626 * Except for SIGCONT, shouldn't get here.
2627 * Default action is to ignore; drop it.
2629 break; /* == ignore */
2634 case (intptr_t)SIG_IGN:
2636 * Masking above should prevent us ever trying
2637 * to take action on an ignored signal other
2638 * than SIGCONT, unless process is traced.
2640 if ((prop & SA_CONT) == 0 &&
2641 (p->p_flag & P_TRACED) == 0)
2642 printf("issignal\n");
2643 break; /* == ignore */
2647 * This signal has an action, let
2648 * postsig() process it.
2652 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2661 thread_stopped(struct proc *p)
2663 struct proc *p1 = curthread->td_proc;
2666 PROC_LOCK_ASSERT(p, MA_OWNED);
2667 mtx_assert(&sched_lock, MA_OWNED);
2671 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2672 mtx_unlock_spin(&sched_lock);
2673 p->p_flag &= ~P_WAITED;
2674 PROC_LOCK(p->p_pptr);
2675 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2676 CLD_TRAPPED : CLD_STOPPED);
2677 PROC_UNLOCK(p->p_pptr);
2678 mtx_lock_spin(&sched_lock);
2683 * Take the action for the specified signal
2684 * from the current set of pending signals.
2690 struct thread *td = curthread;
2691 register struct proc *p = td->td_proc;
2695 sigset_t returnmask;
2698 KASSERT(sig != 0, ("postsig"));
2700 PROC_LOCK_ASSERT(p, MA_OWNED);
2702 mtx_assert(&ps->ps_mtx, MA_OWNED);
2703 ksiginfo_init(&ksi);
2704 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2705 ksi.ksi_signo = sig;
2706 if (ksi.ksi_code == SI_TIMER)
2707 itimer_accept(p, ksi.ksi_timerid, &ksi);
2708 action = ps->ps_sigact[_SIG_IDX(sig)];
2710 if (KTRPOINT(td, KTR_PSIG))
2711 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2712 &td->td_oldsigmask : &td->td_sigmask, 0);
2714 if (p->p_stops & S_SIG) {
2715 mtx_unlock(&ps->ps_mtx);
2716 stopevent(p, S_SIG, sig);
2717 mtx_lock(&ps->ps_mtx);
2720 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2722 * Default action, where the default is to kill
2723 * the process. (Other cases were ignored above.)
2725 mtx_unlock(&ps->ps_mtx);
2729 if (td->td_pflags & TDP_SA) {
2730 if (sig == SIGKILL) {
2731 mtx_unlock(&ps->ps_mtx);
2737 * If we get here, the signal must be caught.
2739 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2740 ("postsig action"));
2742 * Set the new mask value and also defer further
2743 * occurrences of this signal.
2745 * Special case: user has done a sigsuspend. Here the
2746 * current mask is not of interest, but rather the
2747 * mask from before the sigsuspend is what we want
2748 * restored after the signal processing is completed.
2750 if (td->td_pflags & TDP_OLDMASK) {
2751 returnmask = td->td_oldsigmask;
2752 td->td_pflags &= ~TDP_OLDMASK;
2754 returnmask = td->td_sigmask;
2756 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2757 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2758 SIGADDSET(td->td_sigmask, sig);
2760 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2762 * See kern_sigaction() for origin of this code.
2764 SIGDELSET(ps->ps_sigcatch, sig);
2765 if (sig != SIGCONT &&
2766 sigprop(sig) & SA_IGNORE)
2767 SIGADDSET(ps->ps_sigignore, sig);
2768 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2770 p->p_stats->p_ru.ru_nsignals++;
2771 if (p->p_sig != sig) {
2778 if (td->td_pflags & TDP_SA)
2779 thread_signal_add(curthread, &ksi);
2781 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2786 * Kill the current process for stated reason.
2794 PROC_LOCK_ASSERT(p, MA_OWNED);
2795 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2796 p, p->p_pid, p->p_comm);
2797 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2798 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2799 psignal(p, SIGKILL);
2803 * Force the current process to exit with the specified signal, dumping core
2804 * if appropriate. We bypass the normal tests for masked and caught signals,
2805 * allowing unrecoverable failures to terminate the process without changing
2806 * signal state. Mark the accounting record with the signal termination.
2807 * If dumping core, save the signal number for the debugger. Calls exit and
2817 struct proc *p = td->td_proc;
2819 PROC_LOCK_ASSERT(p, MA_OWNED);
2820 p->p_acflag |= AXSIG;
2822 * We must be single-threading to generate a core dump. This
2823 * ensures that the registers in the core file are up-to-date.
2824 * Also, the ELF dump handler assumes that the thread list doesn't
2825 * change out from under it.
2827 * XXX If another thread attempts to single-thread before us
2828 * (e.g. via fork()), we won't get a dump at all.
2830 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2833 * Log signals which would cause core dumps
2834 * (Log as LOG_INFO to appease those who don't want
2836 * XXX : Todo, as well as euid, write out ruid too
2837 * Note that coredump() drops proc lock.
2839 if (coredump(td) == 0)
2841 if (kern_logsigexit)
2843 "pid %d (%s), uid %d: exited on signal %d%s\n",
2844 p->p_pid, p->p_comm,
2845 td->td_ucred ? td->td_ucred->cr_uid : -1,
2847 sig & WCOREFLAG ? " (core dumped)" : "");
2850 exit1(td, W_EXITCODE(0, sig));
2855 * Send queued SIGCHLD to parent when child process's state
2859 sigparent(struct proc *p, int reason, int status)
2861 PROC_LOCK_ASSERT(p, MA_OWNED);
2862 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2864 if (p->p_ksi != NULL) {
2865 p->p_ksi->ksi_signo = SIGCHLD;
2866 p->p_ksi->ksi_code = reason;
2867 p->p_ksi->ksi_status = status;
2868 p->p_ksi->ksi_pid = p->p_pid;
2869 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2870 if (KSI_ONQ(p->p_ksi))
2873 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2877 childproc_jobstate(struct proc *p, int reason, int status)
2881 PROC_LOCK_ASSERT(p, MA_OWNED);
2882 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2885 * Wake up parent sleeping in kern_wait(), also send
2886 * SIGCHLD to parent, but SIGCHLD does not guarantee
2887 * that parent will awake, because parent may masked
2890 p->p_pptr->p_flag |= P_STATCHILD;
2893 ps = p->p_pptr->p_sigacts;
2894 mtx_lock(&ps->ps_mtx);
2895 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2896 mtx_unlock(&ps->ps_mtx);
2897 sigparent(p, reason, status);
2899 mtx_unlock(&ps->ps_mtx);
2903 childproc_stopped(struct proc *p, int reason)
2905 childproc_jobstate(p, reason, p->p_xstat);
2909 childproc_continued(struct proc *p)
2911 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2915 childproc_exited(struct proc *p)
2918 int status = p->p_xstat; /* convert to int */
2920 reason = CLD_EXITED;
2921 if (WCOREDUMP(status))
2922 reason = CLD_DUMPED;
2923 else if (WIFSIGNALED(status))
2924 reason = CLD_KILLED;
2926 * XXX avoid calling wakeup(p->p_pptr), the work is
2929 sigparent(p, reason, status);
2932 static char corefilename[MAXPATHLEN] = {"%N.core"};
2933 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2934 sizeof(corefilename), "process corefile name format string");
2937 * expand_name(name, uid, pid)
2938 * Expand the name described in corefilename, using name, uid, and pid.
2939 * corefilename is a printf-like string, with three format specifiers:
2940 * %N name of process ("name")
2941 * %P process id (pid)
2943 * For example, "%N.core" is the default; they can be disabled completely
2944 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2945 * This is controlled by the sysctl variable kern.corefile (see above).
2949 expand_name(name, uid, pid)
2954 const char *format, *appendstr;
2956 char buf[11]; /* Buffer for pid/uid -- max 4B */
2959 format = corefilename;
2960 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2963 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2964 switch (format[i]) {
2965 case '%': /* Format character */
2967 switch (format[i]) {
2971 case 'N': /* process name */
2974 case 'P': /* process id */
2975 sprintf(buf, "%u", pid);
2978 case 'U': /* user id */
2979 sprintf(buf, "%u", uid);
2985 "Unknown format character %c in `%s'\n",
2988 l = strlen(appendstr);
2989 if ((n + l) >= MAXPATHLEN)
2991 memcpy(temp + n, appendstr, l);
2995 temp[n++] = format[i];
2998 if (format[i] != '\0')
3002 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
3003 (long)pid, name, (u_long)uid);
3009 * Dump a process' core. The main routine does some
3010 * policy checking, and creates the name of the coredump;
3011 * then it passes on a vnode and a size limit to the process-specific
3012 * coredump routine if there is one; if there _is not_ one, it returns
3013 * ENOSYS; otherwise it returns the error from the process-specific routine.
3017 coredump(struct thread *td)
3019 struct proc *p = td->td_proc;
3020 register struct vnode *vp;
3021 register struct ucred *cred = td->td_ucred;
3023 struct nameidata nd;
3025 int error, error1, flags, locked;
3027 char *name; /* name of corefile */
3030 PROC_LOCK_ASSERT(p, MA_OWNED);
3031 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3032 _STOPEVENT(p, S_CORE, 0);
3034 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
3040 * Note that the bulk of limit checking is done after
3041 * the corefile is created. The exception is if the limit
3042 * for corefiles is 0, in which case we don't bother
3043 * creating the corefile at all. This layout means that
3044 * a corefile is truncated instead of not being created,
3045 * if it is larger than the limit.
3047 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3054 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
3059 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
3060 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3061 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
3067 NDFREE(&nd, NDF_ONLY_PNBUF);
3070 /* Don't dump to non-regular files or files with links. */
3071 if (vp->v_type != VREG ||
3072 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
3073 VOP_UNLOCK(vp, 0, td);
3078 VOP_UNLOCK(vp, 0, td);
3079 lf.l_whence = SEEK_SET;
3082 lf.l_type = F_WRLCK;
3083 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3085 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3086 lf.l_type = F_UNLCK;
3088 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3089 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3091 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3098 if (set_core_nodump_flag)
3099 vattr.va_flags = UF_NODUMP;
3100 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3101 VOP_LEASE(vp, td, cred, LEASE_WRITE);
3102 VOP_SETATTR(vp, &vattr, cred, td);
3103 VOP_UNLOCK(vp, 0, td);
3105 p->p_acflag |= ACORE;
3108 error = p->p_sysent->sv_coredump ?
3109 p->p_sysent->sv_coredump(td, vp, limit) :
3113 lf.l_type = F_UNLCK;
3114 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3116 vn_finished_write(mp);
3118 error1 = vn_close(vp, FWRITE, cred, td);
3126 * Nonexistent system call-- signal process (may want to handle it).
3127 * Flag error in case process won't see signal immediately (blocked or ignored).
3129 #ifndef _SYS_SYSPROTO_H_
3141 struct nosys_args *args;
3143 struct proc *p = td->td_proc;
3152 * Send a SIGIO or SIGURG signal to a process or process group using
3153 * stored credentials rather than those of the current process.
3156 pgsigio(sigiop, sig, checkctty)
3157 struct sigio **sigiop;
3160 struct sigio *sigio;
3164 if (sigio == NULL) {
3168 if (sigio->sio_pgid > 0) {
3169 PROC_LOCK(sigio->sio_proc);
3170 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3171 psignal(sigio->sio_proc, sig);
3172 PROC_UNLOCK(sigio->sio_proc);
3173 } else if (sigio->sio_pgid < 0) {
3176 PGRP_LOCK(sigio->sio_pgrp);
3177 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3179 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3180 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3184 PGRP_UNLOCK(sigio->sio_pgrp);
3190 filt_sigattach(struct knote *kn)
3192 struct proc *p = curproc;
3194 kn->kn_ptr.p_proc = p;
3195 kn->kn_flags |= EV_CLEAR; /* automatically set */
3197 knlist_add(&p->p_klist, kn, 0);
3203 filt_sigdetach(struct knote *kn)
3205 struct proc *p = kn->kn_ptr.p_proc;
3207 knlist_remove(&p->p_klist, kn, 0);
3211 * signal knotes are shared with proc knotes, so we apply a mask to
3212 * the hint in order to differentiate them from process hints. This
3213 * could be avoided by using a signal-specific knote list, but probably
3214 * isn't worth the trouble.
3217 filt_signal(struct knote *kn, long hint)
3220 if (hint & NOTE_SIGNAL) {
3221 hint &= ~NOTE_SIGNAL;
3223 if (kn->kn_id == hint)
3226 return (kn->kn_data != 0);
3234 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3236 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3241 sigacts_free(struct sigacts *ps)
3244 mtx_lock(&ps->ps_mtx);
3246 if (ps->ps_refcnt == 0) {
3247 mtx_destroy(&ps->ps_mtx);
3248 free(ps, M_SUBPROC);
3250 mtx_unlock(&ps->ps_mtx);
3254 sigacts_hold(struct sigacts *ps)
3256 mtx_lock(&ps->ps_mtx);
3258 mtx_unlock(&ps->ps_mtx);
3263 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3266 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3267 mtx_lock(&src->ps_mtx);
3268 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3269 mtx_unlock(&src->ps_mtx);
3273 sigacts_shared(struct sigacts *ps)
3277 mtx_lock(&ps->ps_mtx);
3278 shared = ps->ps_refcnt > 1;
3279 mtx_unlock(&ps->ps_mtx);