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>
53 #include <sys/ktrace.h>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/namei.h>
59 #include <sys/posix4.h>
60 #include <sys/pioctl.h>
61 #include <sys/resourcevar.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 <machine/cpu.h>
81 #include <security/audit/audit.h>
83 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
85 static int coredump(struct thread *);
86 static char *expand_name(const char *, uid_t, pid_t);
87 static int killpg1(struct thread *td, int sig, int pgid, int all);
88 static int issignal(struct thread *p);
89 static int sigprop(int sig);
90 static void tdsigwakeup(struct thread *, int, sig_t, int);
91 static void sig_suspend_threads(struct thread *, struct proc *, int);
92 static int filt_sigattach(struct knote *kn);
93 static void filt_sigdetach(struct knote *kn);
94 static int filt_signal(struct knote *kn, long hint);
95 static struct thread *sigtd(struct proc *p, int sig, int prop);
96 static void sigqueue_start(void);
98 static uma_zone_t ksiginfo_zone = NULL;
99 struct filterops sig_filtops =
100 { 0, filt_sigattach, filt_sigdetach, filt_signal };
102 int kern_logsigexit = 1;
103 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
105 "Log processes quitting on abnormal signals to syslog(3)");
107 static int kern_forcesigexit = 1;
108 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
109 &kern_forcesigexit, 0, "Force trap signal to be handled");
111 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
113 static int max_pending_per_proc = 128;
114 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
115 &max_pending_per_proc, 0, "Max pending signals per proc");
117 static int preallocate_siginfo = 1024;
118 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
119 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
120 &preallocate_siginfo, 0, "Preallocated signal memory size");
122 static int signal_overflow = 0;
123 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
124 &signal_overflow, 0, "Number of signals overflew");
126 static int signal_alloc_fail = 0;
127 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
128 &signal_alloc_fail, 0, "signals failed to be allocated");
130 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
133 * Policy -- Can ucred cr1 send SIGIO to process cr2?
134 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
135 * in the right situations.
137 #define CANSIGIO(cr1, cr2) \
138 ((cr1)->cr_uid == 0 || \
139 (cr1)->cr_ruid == (cr2)->cr_ruid || \
140 (cr1)->cr_uid == (cr2)->cr_ruid || \
141 (cr1)->cr_ruid == (cr2)->cr_uid || \
142 (cr1)->cr_uid == (cr2)->cr_uid)
145 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
146 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
148 static int do_coredump = 1;
149 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
150 &do_coredump, 0, "Enable/Disable coredumps");
152 static int set_core_nodump_flag = 0;
153 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
154 0, "Enable setting the NODUMP flag on coredump files");
157 * Signal properties and actions.
158 * The array below categorizes the signals and their default actions
159 * according to the following properties:
161 #define SA_KILL 0x01 /* terminates process by default */
162 #define SA_CORE 0x02 /* ditto and coredumps */
163 #define SA_STOP 0x04 /* suspend process */
164 #define SA_TTYSTOP 0x08 /* ditto, from tty */
165 #define SA_IGNORE 0x10 /* ignore by default */
166 #define SA_CONT 0x20 /* continue if suspended */
167 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
168 #define SA_PROC 0x80 /* deliverable to any thread */
170 static int sigproptbl[NSIG] = {
171 SA_KILL|SA_PROC, /* SIGHUP */
172 SA_KILL|SA_PROC, /* SIGINT */
173 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
174 SA_KILL|SA_CORE, /* SIGILL */
175 SA_KILL|SA_CORE, /* SIGTRAP */
176 SA_KILL|SA_CORE, /* SIGABRT */
177 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
178 SA_KILL|SA_CORE, /* SIGFPE */
179 SA_KILL|SA_PROC, /* SIGKILL */
180 SA_KILL|SA_CORE, /* SIGBUS */
181 SA_KILL|SA_CORE, /* SIGSEGV */
182 SA_KILL|SA_CORE, /* SIGSYS */
183 SA_KILL|SA_PROC, /* SIGPIPE */
184 SA_KILL|SA_PROC, /* SIGALRM */
185 SA_KILL|SA_PROC, /* SIGTERM */
186 SA_IGNORE|SA_PROC, /* SIGURG */
187 SA_STOP|SA_PROC, /* SIGSTOP */
188 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
189 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
190 SA_IGNORE|SA_PROC, /* SIGCHLD */
191 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
192 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
193 SA_IGNORE|SA_PROC, /* SIGIO */
194 SA_KILL, /* SIGXCPU */
195 SA_KILL, /* SIGXFSZ */
196 SA_KILL|SA_PROC, /* SIGVTALRM */
197 SA_KILL|SA_PROC, /* SIGPROF */
198 SA_IGNORE|SA_PROC, /* SIGWINCH */
199 SA_IGNORE|SA_PROC, /* SIGINFO */
200 SA_KILL|SA_PROC, /* SIGUSR1 */
201 SA_KILL|SA_PROC, /* SIGUSR2 */
207 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
208 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
209 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
210 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
211 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
212 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
216 ksiginfo_alloc(int wait)
223 if (ksiginfo_zone != NULL)
224 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
229 ksiginfo_free(ksiginfo_t *ksi)
231 uma_zfree(ksiginfo_zone, ksi);
235 ksiginfo_tryfree(ksiginfo_t *ksi)
237 if (!(ksi->ksi_flags & KSI_EXT)) {
238 uma_zfree(ksiginfo_zone, ksi);
245 sigqueue_init(sigqueue_t *list, struct proc *p)
247 SIGEMPTYSET(list->sq_signals);
248 SIGEMPTYSET(list->sq_kill);
249 TAILQ_INIT(&list->sq_list);
251 list->sq_flags = SQ_INIT;
255 * Get a signal's ksiginfo.
257 * 0 - signal not found
258 * others - signal number
261 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
263 struct proc *p = sq->sq_proc;
264 struct ksiginfo *ksi, *next;
267 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
269 if (!SIGISMEMBER(sq->sq_signals, signo))
272 if (SIGISMEMBER(sq->sq_kill, signo)) {
274 SIGDELSET(sq->sq_kill, signo);
277 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
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)
292 SIGDELSET(sq->sq_signals, signo);
293 si->ksi_signo = signo;
298 sigqueue_take(ksiginfo_t *ksi)
304 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
308 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
309 ksi->ksi_sigq = NULL;
310 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
313 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
314 kp = TAILQ_NEXT(kp, ksi_link)) {
315 if (kp->ksi_signo == ksi->ksi_signo)
318 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
319 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
323 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
325 struct proc *p = sq->sq_proc;
326 struct ksiginfo *ksi;
329 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
331 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
332 SIGADDSET(sq->sq_kill, signo);
336 /* directly insert the ksi, don't copy it */
337 if (si->ksi_flags & KSI_INS) {
338 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
343 if (__predict_false(ksiginfo_zone == NULL)) {
344 SIGADDSET(sq->sq_kill, signo);
348 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
351 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
357 ksiginfo_copy(si, ksi);
358 ksi->ksi_signo = signo;
359 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
363 if ((si->ksi_flags & KSI_TRAP) != 0) {
365 SIGADDSET(sq->sq_kill, signo);
374 SIGADDSET(sq->sq_signals, signo);
379 sigqueue_flush(sigqueue_t *sq)
381 struct proc *p = sq->sq_proc;
384 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
387 PROC_LOCK_ASSERT(p, MA_OWNED);
389 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
390 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
391 ksi->ksi_sigq = NULL;
392 if (ksiginfo_tryfree(ksi) && p != NULL)
396 SIGEMPTYSET(sq->sq_signals);
397 SIGEMPTYSET(sq->sq_kill);
401 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
405 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
407 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
408 SIGADDSET(*set, ksi->ksi_signo);
409 SIGSETOR(*set, sq->sq_kill);
413 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
416 struct proc *p1, *p2;
417 ksiginfo_t *ksi, *next;
419 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
420 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
422 * make a copy, this allows setp to point to src or dst
423 * sq_signals without trouble.
428 /* Move siginfo to target list */
429 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
430 if (SIGISMEMBER(set, ksi->ksi_signo)) {
431 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
434 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
441 /* Move pending bits to target list */
444 SIGSETOR(dst->sq_kill, tmp);
445 SIGSETNAND(src->sq_kill, tmp);
447 tmp = src->sq_signals;
449 SIGSETOR(dst->sq_signals, tmp);
450 SIGSETNAND(src->sq_signals, tmp);
452 /* Finally, rescan src queue and set pending bits for it */
453 sigqueue_collect_set(src, &src->sq_signals);
457 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
462 SIGADDSET(set, signo);
463 sigqueue_move_set(src, dst, &set);
467 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
469 struct proc *p = sq->sq_proc;
470 ksiginfo_t *ksi, *next;
472 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
474 /* Remove siginfo queue */
475 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
476 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
477 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
478 ksi->ksi_sigq = NULL;
479 if (ksiginfo_tryfree(ksi) && p != NULL)
483 SIGSETNAND(sq->sq_kill, *set);
484 SIGSETNAND(sq->sq_signals, *set);
485 /* Finally, rescan queue and set pending bits for it */
486 sigqueue_collect_set(sq, &sq->sq_signals);
490 sigqueue_delete(sigqueue_t *sq, int signo)
495 SIGADDSET(set, signo);
496 sigqueue_delete_set(sq, &set);
499 /* Remove a set of signals for a process */
501 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
506 PROC_LOCK_ASSERT(p, MA_OWNED);
508 sigqueue_init(&worklist, NULL);
509 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
511 FOREACH_THREAD_IN_PROC(p, td0)
512 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
514 sigqueue_flush(&worklist);
518 sigqueue_delete_proc(struct proc *p, int signo)
523 SIGADDSET(set, signo);
524 sigqueue_delete_set_proc(p, &set);
528 sigqueue_delete_stopmask_proc(struct proc *p)
533 SIGADDSET(set, SIGSTOP);
534 SIGADDSET(set, SIGTSTP);
535 SIGADDSET(set, SIGTTIN);
536 SIGADDSET(set, SIGTTOU);
537 sigqueue_delete_set_proc(p, &set);
541 * Determine signal that should be delivered to process p, the current
542 * process, 0 if none. If there is a pending stop signal with default
543 * action, the process stops in issignal().
546 cursig(struct thread *td)
548 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
549 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
550 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
551 return (SIGPENDING(td) ? issignal(td) : 0);
555 * Arrange for ast() to handle unmasked pending signals on return to user
556 * mode. This must be called whenever a signal is added to td_sigqueue or
557 * unmasked in td_sigmask.
560 signotify(struct thread *td)
567 PROC_LOCK_ASSERT(p, MA_OWNED);
570 * If our mask changed we may have to move signal that were
571 * previously masked by all threads to our sigqueue.
573 set = p->p_sigqueue.sq_signals;
574 SIGSETNAND(set, td->td_sigmask);
575 if (! SIGISEMPTY(set))
576 sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set);
577 if (SIGPENDING(td)) {
579 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
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));
627 kern_sigaction(td, sig, act, oact, flags)
630 struct sigaction *act, *oact;
634 struct proc *p = td->td_proc;
636 if (!_SIG_VALID(sig))
641 mtx_lock(&ps->ps_mtx);
643 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
645 if (SIGISMEMBER(ps->ps_sigonstack, sig))
646 oact->sa_flags |= SA_ONSTACK;
647 if (!SIGISMEMBER(ps->ps_sigintr, sig))
648 oact->sa_flags |= SA_RESTART;
649 if (SIGISMEMBER(ps->ps_sigreset, sig))
650 oact->sa_flags |= SA_RESETHAND;
651 if (SIGISMEMBER(ps->ps_signodefer, sig))
652 oact->sa_flags |= SA_NODEFER;
653 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
654 oact->sa_flags |= SA_SIGINFO;
656 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
658 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
659 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
660 oact->sa_flags |= SA_NOCLDSTOP;
661 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
662 oact->sa_flags |= SA_NOCLDWAIT;
665 if ((sig == SIGKILL || sig == SIGSTOP) &&
666 act->sa_handler != SIG_DFL) {
667 mtx_unlock(&ps->ps_mtx);
673 * Change setting atomically.
676 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
677 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
678 if (act->sa_flags & SA_SIGINFO) {
679 ps->ps_sigact[_SIG_IDX(sig)] =
680 (__sighandler_t *)act->sa_sigaction;
681 SIGADDSET(ps->ps_siginfo, sig);
683 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
684 SIGDELSET(ps->ps_siginfo, sig);
686 if (!(act->sa_flags & SA_RESTART))
687 SIGADDSET(ps->ps_sigintr, sig);
689 SIGDELSET(ps->ps_sigintr, sig);
690 if (act->sa_flags & SA_ONSTACK)
691 SIGADDSET(ps->ps_sigonstack, sig);
693 SIGDELSET(ps->ps_sigonstack, sig);
694 if (act->sa_flags & SA_RESETHAND)
695 SIGADDSET(ps->ps_sigreset, sig);
697 SIGDELSET(ps->ps_sigreset, sig);
698 if (act->sa_flags & SA_NODEFER)
699 SIGADDSET(ps->ps_signodefer, sig);
701 SIGDELSET(ps->ps_signodefer, sig);
702 if (sig == SIGCHLD) {
703 if (act->sa_flags & SA_NOCLDSTOP)
704 ps->ps_flag |= PS_NOCLDSTOP;
706 ps->ps_flag &= ~PS_NOCLDSTOP;
707 if (act->sa_flags & SA_NOCLDWAIT) {
709 * Paranoia: since SA_NOCLDWAIT is implemented
710 * by reparenting the dying child to PID 1 (and
711 * trust it to reap the zombie), PID 1 itself
712 * is forbidden to set SA_NOCLDWAIT.
715 ps->ps_flag &= ~PS_NOCLDWAIT;
717 ps->ps_flag |= PS_NOCLDWAIT;
719 ps->ps_flag &= ~PS_NOCLDWAIT;
720 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
721 ps->ps_flag |= PS_CLDSIGIGN;
723 ps->ps_flag &= ~PS_CLDSIGIGN;
726 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
727 * and for signals set to SIG_DFL where the default is to
728 * ignore. However, don't put SIGCONT in ps_sigignore, as we
729 * have to restart the process.
731 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
732 (sigprop(sig) & SA_IGNORE &&
733 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
734 /* never to be seen again */
735 sigqueue_delete_proc(p, sig);
737 /* easier in psignal */
738 SIGADDSET(ps->ps_sigignore, sig);
739 SIGDELSET(ps->ps_sigcatch, sig);
741 SIGDELSET(ps->ps_sigignore, sig);
742 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
743 SIGDELSET(ps->ps_sigcatch, sig);
745 SIGADDSET(ps->ps_sigcatch, sig);
747 #ifdef COMPAT_FREEBSD4
748 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
749 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
750 (flags & KSA_FREEBSD4) == 0)
751 SIGDELSET(ps->ps_freebsd4, sig);
753 SIGADDSET(ps->ps_freebsd4, sig);
756 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
757 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
758 (flags & KSA_OSIGSET) == 0)
759 SIGDELSET(ps->ps_osigset, sig);
761 SIGADDSET(ps->ps_osigset, sig);
764 mtx_unlock(&ps->ps_mtx);
769 #ifndef _SYS_SYSPROTO_H_
770 struct sigaction_args {
772 struct sigaction *act;
773 struct sigaction *oact;
779 register struct sigaction_args *uap;
781 struct sigaction act, oact;
782 register struct sigaction *actp, *oactp;
785 actp = (uap->act != NULL) ? &act : NULL;
786 oactp = (uap->oact != NULL) ? &oact : NULL;
788 error = copyin(uap->act, actp, sizeof(act));
792 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
794 error = copyout(oactp, uap->oact, sizeof(oact));
798 #ifdef COMPAT_FREEBSD4
799 #ifndef _SYS_SYSPROTO_H_
800 struct freebsd4_sigaction_args {
802 struct sigaction *act;
803 struct sigaction *oact;
807 freebsd4_sigaction(td, uap)
809 register struct freebsd4_sigaction_args *uap;
811 struct sigaction act, oact;
812 register struct sigaction *actp, *oactp;
816 actp = (uap->act != NULL) ? &act : NULL;
817 oactp = (uap->oact != NULL) ? &oact : NULL;
819 error = copyin(uap->act, actp, sizeof(act));
823 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
825 error = copyout(oactp, uap->oact, sizeof(oact));
828 #endif /* COMAPT_FREEBSD4 */
830 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
831 #ifndef _SYS_SYSPROTO_H_
832 struct osigaction_args {
834 struct osigaction *nsa;
835 struct osigaction *osa;
841 register struct osigaction_args *uap;
843 struct osigaction sa;
844 struct sigaction nsa, osa;
845 register struct sigaction *nsap, *osap;
848 if (uap->signum <= 0 || uap->signum >= ONSIG)
851 nsap = (uap->nsa != NULL) ? &nsa : NULL;
852 osap = (uap->osa != NULL) ? &osa : NULL;
855 error = copyin(uap->nsa, &sa, sizeof(sa));
858 nsap->sa_handler = sa.sa_handler;
859 nsap->sa_flags = sa.sa_flags;
860 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
862 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
863 if (osap && !error) {
864 sa.sa_handler = osap->sa_handler;
865 sa.sa_flags = osap->sa_flags;
866 SIG2OSIG(osap->sa_mask, sa.sa_mask);
867 error = copyout(&sa, uap->osa, sizeof(sa));
872 #if !defined(__i386__)
873 /* Avoid replicating the same stub everywhere */
877 struct osigreturn_args *uap;
880 return (nosys(td, (struct nosys_args *)uap));
883 #endif /* COMPAT_43 */
886 * Initialize signal state for process 0;
887 * set to ignore signals that are ignored by default.
898 mtx_lock(&ps->ps_mtx);
899 for (i = 1; i <= NSIG; i++)
900 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
901 SIGADDSET(ps->ps_sigignore, i);
902 mtx_unlock(&ps->ps_mtx);
907 * Reset signals for an exec of the specified process.
910 execsigs(struct proc *p)
917 * Reset caught signals. Held signals remain held
918 * through td_sigmask (unless they were caught,
919 * and are now ignored by default).
921 PROC_LOCK_ASSERT(p, MA_OWNED);
922 td = FIRST_THREAD_IN_PROC(p);
924 mtx_lock(&ps->ps_mtx);
925 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
926 sig = sig_ffs(&ps->ps_sigcatch);
927 SIGDELSET(ps->ps_sigcatch, sig);
928 if (sigprop(sig) & SA_IGNORE) {
930 SIGADDSET(ps->ps_sigignore, sig);
931 sigqueue_delete_proc(p, sig);
933 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
936 * Reset stack state to the user stack.
937 * Clear set of signals caught on the signal stack.
939 td->td_sigstk.ss_flags = SS_DISABLE;
940 td->td_sigstk.ss_size = 0;
941 td->td_sigstk.ss_sp = 0;
942 td->td_pflags &= ~TDP_ALTSTACK;
944 * Reset no zombies if child dies flag as Solaris does.
946 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
947 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
948 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
949 mtx_unlock(&ps->ps_mtx);
955 * Manipulate signal mask.
958 kern_sigprocmask(td, how, set, oset, old)
961 sigset_t *set, *oset;
966 PROC_LOCK(td->td_proc);
968 *oset = td->td_sigmask;
975 SIGSETOR(td->td_sigmask, *set);
978 SIGSETNAND(td->td_sigmask, *set);
984 SIGSETLO(td->td_sigmask, *set);
986 td->td_sigmask = *set;
994 PROC_UNLOCK(td->td_proc);
998 #ifndef _SYS_SYSPROTO_H_
999 struct sigprocmask_args {
1001 const sigset_t *set;
1006 sigprocmask(td, uap)
1007 register struct thread *td;
1008 struct sigprocmask_args *uap;
1011 sigset_t *setp, *osetp;
1014 setp = (uap->set != NULL) ? &set : NULL;
1015 osetp = (uap->oset != NULL) ? &oset : NULL;
1017 error = copyin(uap->set, setp, sizeof(set));
1021 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1022 if (osetp && !error) {
1023 error = copyout(osetp, uap->oset, sizeof(oset));
1028 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1029 #ifndef _SYS_SYSPROTO_H_
1030 struct osigprocmask_args {
1036 osigprocmask(td, uap)
1037 register struct thread *td;
1038 struct osigprocmask_args *uap;
1043 OSIG2SIG(uap->mask, set);
1044 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1045 SIG2OSIG(oset, td->td_retval[0]);
1048 #endif /* COMPAT_43 */
1051 sigwait(struct thread *td, struct sigwait_args *uap)
1057 error = copyin(uap->set, &set, sizeof(set));
1059 td->td_retval[0] = error;
1063 error = kern_sigtimedwait(td, set, &ksi, NULL);
1065 if (error == ERESTART)
1067 td->td_retval[0] = error;
1071 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1072 td->td_retval[0] = error;
1077 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1080 struct timespec *timeout;
1086 error = copyin(uap->timeout, &ts, sizeof(ts));
1094 error = copyin(uap->set, &set, sizeof(set));
1098 error = kern_sigtimedwait(td, set, &ksi, timeout);
1103 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1106 td->td_retval[0] = ksi.ksi_signo;
1111 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1117 error = copyin(uap->set, &set, sizeof(set));
1121 error = kern_sigtimedwait(td, set, &ksi, NULL);
1126 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1129 td->td_retval[0] = ksi.ksi_signo;
1134 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1135 struct timespec *timeout)
1140 int error, sig, hz, i, timevalid = 0;
1141 struct timespec rts, ets, ts;
1149 SIG_CANTMASK(waitset);
1153 savedmask = td->td_sigmask;
1155 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1157 getnanouptime(&rts);
1159 timespecadd(&ets, timeout);
1164 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1165 if (!SIGISMEMBER(waitset, i))
1167 if (!SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1168 if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
1169 sigqueue_move(&p->p_sigqueue,
1170 &td->td_sigqueue, i);
1175 SIGFILLSET(td->td_sigmask);
1176 SIG_CANTMASK(td->td_sigmask);
1177 SIGDELSET(td->td_sigmask, i);
1178 mtx_lock(&ps->ps_mtx);
1180 mtx_unlock(&ps->ps_mtx);
1185 * Because cursig() may have stopped current thread,
1186 * after it is resumed, things may have already been
1187 * changed, it should rescan any pending signals.
1197 * POSIX says this must be checked after looking for pending
1205 getnanouptime(&rts);
1206 if (timespeccmp(&rts, &ets, >=)) {
1211 timespecsub(&ts, &rts);
1212 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1217 td->td_sigmask = savedmask;
1218 SIGSETNAND(td->td_sigmask, waitset);
1220 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1222 if (error == ERESTART) {
1223 /* timeout can not be restarted. */
1225 } else if (error == EAGAIN) {
1226 /* will calculate timeout by ourself. */
1233 td->td_sigmask = savedmask;
1237 sigqueue_get(&td->td_sigqueue, sig, ksi);
1238 ksi->ksi_signo = sig;
1239 if (ksi->ksi_code == SI_TIMER)
1240 itimer_accept(p, ksi->ksi_timerid, ksi);
1244 if (KTRPOINT(td, KTR_PSIG)) {
1247 mtx_lock(&ps->ps_mtx);
1248 action = ps->ps_sigact[_SIG_IDX(sig)];
1249 mtx_unlock(&ps->ps_mtx);
1250 ktrpsig(sig, action, &td->td_sigmask, 0);
1260 #ifndef _SYS_SYSPROTO_H_
1261 struct sigpending_args {
1268 struct sigpending_args *uap;
1270 struct proc *p = td->td_proc;
1274 pending = p->p_sigqueue.sq_signals;
1275 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1277 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1280 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1281 #ifndef _SYS_SYSPROTO_H_
1282 struct osigpending_args {
1287 osigpending(td, uap)
1289 struct osigpending_args *uap;
1291 struct proc *p = td->td_proc;
1295 pending = p->p_sigqueue.sq_signals;
1296 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1298 SIG2OSIG(pending, td->td_retval[0]);
1301 #endif /* COMPAT_43 */
1303 #if defined(COMPAT_43)
1305 * Generalized interface signal handler, 4.3-compatible.
1307 #ifndef _SYS_SYSPROTO_H_
1308 struct osigvec_args {
1318 register struct osigvec_args *uap;
1321 struct sigaction nsa, osa;
1322 register struct sigaction *nsap, *osap;
1325 if (uap->signum <= 0 || uap->signum >= ONSIG)
1327 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1328 osap = (uap->osv != NULL) ? &osa : NULL;
1330 error = copyin(uap->nsv, &vec, sizeof(vec));
1333 nsap->sa_handler = vec.sv_handler;
1334 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1335 nsap->sa_flags = vec.sv_flags;
1336 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1338 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1339 if (osap && !error) {
1340 vec.sv_handler = osap->sa_handler;
1341 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1342 vec.sv_flags = osap->sa_flags;
1343 vec.sv_flags &= ~SA_NOCLDWAIT;
1344 vec.sv_flags ^= SA_RESTART;
1345 error = copyout(&vec, uap->osv, sizeof(vec));
1350 #ifndef _SYS_SYSPROTO_H_
1351 struct osigblock_args {
1357 register struct thread *td;
1358 struct osigblock_args *uap;
1360 struct proc *p = td->td_proc;
1363 OSIG2SIG(uap->mask, set);
1366 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1367 SIGSETOR(td->td_sigmask, set);
1372 #ifndef _SYS_SYSPROTO_H_
1373 struct osigsetmask_args {
1378 osigsetmask(td, uap)
1380 struct osigsetmask_args *uap;
1382 struct proc *p = td->td_proc;
1385 OSIG2SIG(uap->mask, set);
1388 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1389 SIGSETLO(td->td_sigmask, set);
1394 #endif /* COMPAT_43 */
1397 * Suspend calling thread until signal, providing mask to be set in the
1400 #ifndef _SYS_SYSPROTO_H_
1401 struct sigsuspend_args {
1402 const sigset_t *sigmask;
1409 struct sigsuspend_args *uap;
1414 error = copyin(uap->sigmask, &mask, sizeof(mask));
1417 return (kern_sigsuspend(td, mask));
1421 kern_sigsuspend(struct thread *td, sigset_t mask)
1423 struct proc *p = td->td_proc;
1426 * When returning from sigsuspend, we want
1427 * the old mask to be restored after the
1428 * signal handler has finished. Thus, we
1429 * save it here and mark the sigacts structure
1433 td->td_oldsigmask = td->td_sigmask;
1434 td->td_pflags |= TDP_OLDMASK;
1436 td->td_sigmask = mask;
1438 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1441 /* always return EINTR rather than ERESTART... */
1445 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1447 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1448 * convention: libc stub passes mask, not pointer, to save a copyin.
1450 #ifndef _SYS_SYSPROTO_H_
1451 struct osigsuspend_args {
1457 osigsuspend(td, uap)
1459 struct osigsuspend_args *uap;
1461 struct proc *p = td->td_proc;
1465 td->td_oldsigmask = td->td_sigmask;
1466 td->td_pflags |= TDP_OLDMASK;
1467 OSIG2SIG(uap->mask, mask);
1469 SIGSETLO(td->td_sigmask, mask);
1471 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1474 /* always return EINTR rather than ERESTART... */
1477 #endif /* COMPAT_43 */
1479 #if defined(COMPAT_43)
1480 #ifndef _SYS_SYSPROTO_H_
1481 struct osigstack_args {
1482 struct sigstack *nss;
1483 struct sigstack *oss;
1490 register struct osigstack_args *uap;
1492 struct sigstack nss, oss;
1495 if (uap->nss != NULL) {
1496 error = copyin(uap->nss, &nss, sizeof(nss));
1500 oss.ss_sp = td->td_sigstk.ss_sp;
1501 oss.ss_onstack = sigonstack(cpu_getstack(td));
1502 if (uap->nss != NULL) {
1503 td->td_sigstk.ss_sp = nss.ss_sp;
1504 td->td_sigstk.ss_size = 0;
1505 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1506 td->td_pflags |= TDP_ALTSTACK;
1508 if (uap->oss != NULL)
1509 error = copyout(&oss, uap->oss, sizeof(oss));
1513 #endif /* COMPAT_43 */
1515 #ifndef _SYS_SYSPROTO_H_
1516 struct sigaltstack_args {
1523 sigaltstack(td, uap)
1525 register struct sigaltstack_args *uap;
1530 if (uap->ss != NULL) {
1531 error = copyin(uap->ss, &ss, sizeof(ss));
1535 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1536 (uap->oss != NULL) ? &oss : NULL);
1539 if (uap->oss != NULL)
1540 error = copyout(&oss, uap->oss, sizeof(stack_t));
1545 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1547 struct proc *p = td->td_proc;
1550 oonstack = sigonstack(cpu_getstack(td));
1553 *oss = td->td_sigstk;
1554 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1555 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1561 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1563 if (!(ss->ss_flags & SS_DISABLE)) {
1564 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1567 td->td_sigstk = *ss;
1568 td->td_pflags |= TDP_ALTSTACK;
1570 td->td_pflags &= ~TDP_ALTSTACK;
1577 * Common code for kill process group/broadcast kill.
1578 * cp is calling process.
1581 killpg1(td, sig, pgid, all)
1582 register struct thread *td;
1585 register struct proc *p;
1593 sx_slock(&allproc_lock);
1594 FOREACH_PROC_IN_SYSTEM(p) {
1596 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1597 p == td->td_proc || p->p_state == PRS_NEW) {
1601 if (p_cansignal(td, p, sig) == 0) {
1608 sx_sunlock(&allproc_lock);
1610 sx_slock(&proctree_lock);
1613 * zero pgid means send to my process group.
1615 pgrp = td->td_proc->p_pgrp;
1618 pgrp = pgfind(pgid);
1620 sx_sunlock(&proctree_lock);
1624 sx_sunlock(&proctree_lock);
1625 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1627 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1628 p->p_state == PRS_NEW ) {
1632 if (p_cansignal(td, p, sig) == 0) {
1641 return (nfound ? 0 : ESRCH);
1644 #ifndef _SYS_SYSPROTO_H_
1653 register struct thread *td;
1654 register struct kill_args *uap;
1656 register struct proc *p;
1659 AUDIT_ARG(signum, uap->signum);
1660 AUDIT_ARG(pid, uap->pid);
1661 if ((u_int)uap->signum > _SIG_MAXSIG)
1665 /* kill single process */
1666 if ((p = pfind(uap->pid)) == NULL) {
1667 if ((p = zpfind(uap->pid)) == NULL)
1670 AUDIT_ARG(process, p);
1671 error = p_cansignal(td, p, uap->signum);
1672 if (error == 0 && uap->signum)
1673 psignal(p, uap->signum);
1678 case -1: /* broadcast signal */
1679 return (killpg1(td, uap->signum, 0, 1));
1680 case 0: /* signal own process group */
1681 return (killpg1(td, uap->signum, 0, 0));
1682 default: /* negative explicit process group */
1683 return (killpg1(td, uap->signum, -uap->pid, 0));
1688 #if defined(COMPAT_43)
1689 #ifndef _SYS_SYSPROTO_H_
1690 struct okillpg_args {
1699 register struct okillpg_args *uap;
1702 AUDIT_ARG(signum, uap->signum);
1703 AUDIT_ARG(pid, uap->pgid);
1704 if ((u_int)uap->signum > _SIG_MAXSIG)
1707 return (killpg1(td, uap->signum, uap->pgid, 0));
1709 #endif /* COMPAT_43 */
1711 #ifndef _SYS_SYSPROTO_H_
1712 struct sigqueue_args {
1715 /* union sigval */ void *value;
1719 sigqueue(struct thread *td, struct sigqueue_args *uap)
1725 if ((u_int)uap->signum > _SIG_MAXSIG)
1729 * Specification says sigqueue can only send signal to
1735 if ((p = pfind(uap->pid)) == NULL) {
1736 if ((p = zpfind(uap->pid)) == NULL)
1739 error = p_cansignal(td, p, uap->signum);
1740 if (error == 0 && uap->signum != 0) {
1741 ksiginfo_init(&ksi);
1742 ksi.ksi_signo = uap->signum;
1743 ksi.ksi_code = SI_QUEUE;
1744 ksi.ksi_pid = td->td_proc->p_pid;
1745 ksi.ksi_uid = td->td_ucred->cr_ruid;
1746 ksi.ksi_value.sival_ptr = uap->value;
1747 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1754 * Send a signal to a process group.
1763 sx_slock(&proctree_lock);
1764 pgrp = pgfind(pgid);
1765 sx_sunlock(&proctree_lock);
1767 pgsignal(pgrp, sig, 0);
1774 * Send a signal to a process group. If checktty is 1,
1775 * limit to members which have a controlling terminal.
1778 pgsignal(pgrp, sig, checkctty)
1782 register struct proc *p;
1785 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1786 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1788 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1796 * Send a signal caused by a trap to the current thread. If it will be
1797 * caught immediately, deliver it with correct code. Otherwise, post it
1801 trapsignal(struct thread *td, ksiginfo_t *ksi)
1809 sig = ksi->ksi_signo;
1810 code = ksi->ksi_code;
1811 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1815 mtx_lock(&ps->ps_mtx);
1816 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1817 !SIGISMEMBER(td->td_sigmask, sig)) {
1818 td->td_ru.ru_nsignals++;
1820 if (KTRPOINT(curthread, KTR_PSIG))
1821 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1822 &td->td_sigmask, code);
1824 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1825 ksi, &td->td_sigmask);
1826 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1827 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1828 SIGADDSET(td->td_sigmask, sig);
1829 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1831 * See kern_sigaction() for origin of this code.
1833 SIGDELSET(ps->ps_sigcatch, sig);
1834 if (sig != SIGCONT &&
1835 sigprop(sig) & SA_IGNORE)
1836 SIGADDSET(ps->ps_sigignore, sig);
1837 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1839 mtx_unlock(&ps->ps_mtx);
1842 * Avoid a possible infinite loop if the thread
1843 * masking the signal or process is ignoring the
1846 if (kern_forcesigexit &&
1847 (SIGISMEMBER(td->td_sigmask, sig) ||
1848 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1849 SIGDELSET(td->td_sigmask, sig);
1850 SIGDELSET(ps->ps_sigcatch, sig);
1851 SIGDELSET(ps->ps_sigignore, sig);
1852 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1854 mtx_unlock(&ps->ps_mtx);
1855 p->p_code = code; /* XXX for core dump/debugger */
1856 p->p_sig = sig; /* XXX to verify code */
1857 tdsignal(p, td, sig, ksi);
1862 static struct thread *
1863 sigtd(struct proc *p, int sig, int prop)
1865 struct thread *td, *signal_td;
1867 PROC_LOCK_ASSERT(p, MA_OWNED);
1870 * Check if current thread can handle the signal without
1871 * switching conetxt to another thread.
1873 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1876 FOREACH_THREAD_IN_PROC(p, td) {
1877 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1882 if (signal_td == NULL)
1883 signal_td = FIRST_THREAD_IN_PROC(p);
1888 * Send the signal to the process. If the signal has an action, the action
1889 * is usually performed by the target process rather than the caller; we add
1890 * the signal to the set of pending signals for the process.
1893 * o When a stop signal is sent to a sleeping process that takes the
1894 * default action, the process is stopped without awakening it.
1895 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1896 * regardless of the signal action (eg, blocked or ignored).
1898 * Other ignored signals are discarded immediately.
1900 * NB: This function may be entered from the debugger via the "kill" DDB
1901 * command. There is little that can be done to mitigate the possibly messy
1902 * side effects of this unwise possibility.
1905 psignal(struct proc *p, int sig)
1907 (void) tdsignal(p, NULL, sig, NULL);
1911 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
1913 struct thread *td = NULL;
1915 PROC_LOCK_ASSERT(p, MA_OWNED);
1917 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
1920 * ksi_code and other fields should be set before
1921 * calling this function.
1923 ksi->ksi_signo = sigev->sigev_signo;
1924 ksi->ksi_value = sigev->sigev_value;
1925 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
1926 td = thread_find(p, sigev->sigev_notify_thread_id);
1930 return (tdsignal(p, td, ksi->ksi_signo, ksi));
1934 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
1937 sigqueue_t *sigqueue;
1943 PROC_LOCK_ASSERT(p, MA_OWNED);
1945 if (!_SIG_VALID(sig))
1946 panic("tdsignal(): invalid signal %d", sig);
1948 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("tdsignal: ksi on queue"));
1951 * IEEE Std 1003.1-2001: return success when killing a zombie.
1953 if (p->p_state == PRS_ZOMBIE) {
1954 if (ksi && (ksi->ksi_flags & KSI_INS))
1955 ksiginfo_tryfree(ksi);
1960 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1961 prop = sigprop(sig);
1964 * If the signal is blocked and not destined for this thread, then
1965 * assign it to the process so that we can find it later in the first
1966 * thread that unblocks it. Otherwise, assign it to this thread now.
1969 td = sigtd(p, sig, prop);
1970 if (SIGISMEMBER(td->td_sigmask, sig))
1971 sigqueue = &p->p_sigqueue;
1973 sigqueue = &td->td_sigqueue;
1975 KASSERT(td->td_proc == p, ("invalid thread"));
1976 sigqueue = &td->td_sigqueue;
1980 * If the signal is being ignored,
1981 * then we forget about it immediately.
1982 * (Note: we don't set SIGCONT in ps_sigignore,
1983 * and if it is set to SIG_IGN,
1984 * action will be SIG_DFL here.)
1986 mtx_lock(&ps->ps_mtx);
1987 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
1988 mtx_unlock(&ps->ps_mtx);
1989 if (ksi && (ksi->ksi_flags & KSI_INS))
1990 ksiginfo_tryfree(ksi);
1993 if (SIGISMEMBER(td->td_sigmask, sig))
1995 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1999 if (SIGISMEMBER(ps->ps_sigintr, sig))
2003 mtx_unlock(&ps->ps_mtx);
2006 sigqueue_delete_stopmask_proc(p);
2007 else if (prop & SA_STOP) {
2009 * If sending a tty stop signal to a member of an orphaned
2010 * process group, discard the signal here if the action
2011 * is default; don't stop the process below if sleeping,
2012 * and don't clear any pending SIGCONT.
2014 if ((prop & SA_TTYSTOP) &&
2015 (p->p_pgrp->pg_jobc == 0) &&
2016 (action == SIG_DFL)) {
2017 if (ksi && (ksi->ksi_flags & KSI_INS))
2018 ksiginfo_tryfree(ksi);
2021 sigqueue_delete_proc(p, SIGCONT);
2022 if (p->p_flag & P_CONTINUED) {
2023 p->p_flag &= ~P_CONTINUED;
2024 PROC_LOCK(p->p_pptr);
2025 sigqueue_take(p->p_ksi);
2026 PROC_UNLOCK(p->p_pptr);
2030 ret = sigqueue_add(sigqueue, sig, ksi);
2035 * Defer further processing for signals which are held,
2036 * except that stopped processes must be continued by SIGCONT.
2038 if (action == SIG_HOLD &&
2039 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2042 * SIGKILL: Remove procfs STOPEVENTs.
2044 if (sig == SIGKILL) {
2045 /* from procfs_ioctl.c: PIOCBIC */
2047 /* from procfs_ioctl.c: PIOCCONT */
2052 * Some signals have a process-wide effect and a per-thread
2053 * component. Most processing occurs when the process next
2054 * tries to cross the user boundary, however there are some
2055 * times when processing needs to be done immediatly, such as
2056 * waking up threads so that they can cross the user boundary.
2057 * We try do the per-process part here.
2059 if (P_SHOULDSTOP(p)) {
2061 * The process is in stopped mode. All the threads should be
2062 * either winding down or already on the suspended queue.
2064 if (p->p_flag & P_TRACED) {
2066 * The traced process is already stopped,
2067 * so no further action is necessary.
2068 * No signal can restart us.
2073 if (sig == SIGKILL) {
2075 * SIGKILL sets process running.
2076 * It will die elsewhere.
2077 * All threads must be restarted.
2079 p->p_flag &= ~P_STOPPED_SIG;
2083 if (prop & SA_CONT) {
2085 * If SIGCONT is default (or ignored), we continue the
2086 * process but don't leave the signal in sigqueue as
2087 * it has no further action. If SIGCONT is held, we
2088 * continue the process and leave the signal in
2089 * sigqueue. If the process catches SIGCONT, let it
2090 * handle the signal itself. If it isn't waiting on
2091 * an event, it goes back to run state.
2092 * Otherwise, process goes back to sleep state.
2094 p->p_flag &= ~P_STOPPED_SIG;
2096 if (p->p_numthreads == p->p_suspcount) {
2098 p->p_flag |= P_CONTINUED;
2099 p->p_xstat = SIGCONT;
2100 PROC_LOCK(p->p_pptr);
2101 childproc_continued(p);
2102 PROC_UNLOCK(p->p_pptr);
2105 if (action == SIG_DFL) {
2106 thread_unsuspend(p);
2108 sigqueue_delete(sigqueue, sig);
2111 if (action == SIG_CATCH) {
2113 * The process wants to catch it so it needs
2114 * to run at least one thread, but which one?
2120 * The signal is not ignored or caught.
2122 thread_unsuspend(p);
2127 if (prop & SA_STOP) {
2129 * Already stopped, don't need to stop again
2130 * (If we did the shell could get confused).
2131 * Just make sure the signal STOP bit set.
2133 p->p_flag |= P_STOPPED_SIG;
2134 sigqueue_delete(sigqueue, sig);
2139 * All other kinds of signals:
2140 * If a thread is sleeping interruptibly, simulate a
2141 * wakeup so that when it is continued it will be made
2142 * runnable and can look at the signal. However, don't make
2143 * the PROCESS runnable, leave it stopped.
2144 * It may run a bit until it hits a thread_suspend_check().
2148 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2149 sleepq_abort(td, intrval);
2154 * Mutexes are short lived. Threads waiting on them will
2155 * hit thread_suspend_check() soon.
2157 } else if (p->p_state == PRS_NORMAL) {
2158 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2159 tdsigwakeup(td, sig, action, intrval);
2163 MPASS(action == SIG_DFL);
2165 if (prop & SA_STOP) {
2166 if (p->p_flag & P_PPWAIT)
2168 p->p_flag |= P_STOPPED_SIG;
2171 sig_suspend_threads(td, p, 1);
2172 if (p->p_numthreads == p->p_suspcount) {
2174 * only thread sending signal to another
2175 * process can reach here, if thread is sending
2176 * signal to its process, because thread does
2177 * not suspend itself here, p_numthreads
2178 * should never be equal to p_suspcount.
2182 sigqueue_delete_proc(p, p->p_xstat);
2188 /* Not in "NORMAL" state. discard the signal. */
2189 sigqueue_delete(sigqueue, sig);
2194 * The process is not stopped so we need to apply the signal to all the
2198 tdsigwakeup(td, sig, action, intrval);
2200 thread_unsuspend(p);
2203 /* If we jump here, proc slock should not be owned. */
2204 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2209 * The force of a signal has been directed against a single
2210 * thread. We need to see what we can do about knocking it
2211 * out of any sleep it may be in etc.
2214 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2216 struct proc *p = td->td_proc;
2219 PROC_LOCK_ASSERT(p, MA_OWNED);
2220 prop = sigprop(sig);
2225 * Bring the priority of a thread up if we want it to get
2226 * killed in this lifetime.
2228 if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
2229 sched_prio(td, PUSER);
2230 if (TD_ON_SLEEPQ(td)) {
2232 * If thread is sleeping uninterruptibly
2233 * we can't interrupt the sleep... the signal will
2234 * be noticed when the process returns through
2235 * trap() or syscall().
2237 if ((td->td_flags & TDF_SINTR) == 0)
2240 * If SIGCONT is default (or ignored) and process is
2241 * asleep, we are finished; the process should not
2244 if ((prop & SA_CONT) && action == SIG_DFL) {
2247 sigqueue_delete(&p->p_sigqueue, sig);
2249 * It may be on either list in this state.
2250 * Remove from both for now.
2252 sigqueue_delete(&td->td_sigqueue, sig);
2257 * Give low priority threads a better chance to run.
2259 if (td->td_priority > PUSER)
2260 sched_prio(td, PUSER);
2262 sleepq_abort(td, intrval);
2265 * Other states do nothing with the signal immediately,
2266 * other than kicking ourselves if we are running.
2267 * It will either never be noticed, or noticed very soon.
2270 if (TD_IS_RUNNING(td) && td != curthread)
2280 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2284 PROC_LOCK_ASSERT(p, MA_OWNED);
2285 PROC_SLOCK_ASSERT(p, MA_OWNED);
2287 FOREACH_THREAD_IN_PROC(p, td2) {
2289 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2290 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2291 (td2->td_flags & TDF_SINTR) &&
2292 !TD_IS_SUSPENDED(td2)) {
2293 thread_suspend_one(td2);
2295 if (sending || td != td2)
2296 td2->td_flags |= TDF_ASTPENDING;
2298 if (TD_IS_RUNNING(td2) && td2 != td)
2299 forward_signal(td2);
2307 ptracestop(struct thread *td, int sig)
2309 struct proc *p = td->td_proc;
2311 PROC_LOCK_ASSERT(p, MA_OWNED);
2312 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2313 &p->p_mtx.lock_object, "Stopping for traced signal");
2316 td->td_flags |= TDF_XSIG;
2320 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2321 if (p->p_flag & P_SINGLE_EXIT) {
2323 td->td_flags &= ~TDF_XSIG;
2329 * Just make wait() to work, the last stopped thread
2334 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2335 sig_suspend_threads(td, p, 0);
2337 thread_suspend_switch(td);
2338 if (!(p->p_flag & P_TRACED)) {
2341 if (td->td_flags & TDF_DBSUSPEND) {
2342 if (p->p_flag & P_SINGLE_EXIT)
2348 return (td->td_xsig);
2352 * If the current process has received a signal (should be caught or cause
2353 * termination, should interrupt current syscall), return the signal number.
2354 * Stop signals with default action are processed immediately, then cleared;
2355 * they aren't returned. This is checked after each entry to the system for
2356 * a syscall or trap (though this can usually be done without calling issignal
2357 * by checking the pending signal masks in cursig.) The normal call
2360 * while (sig = cursig(curthread))
2369 sigset_t sigpending;
2370 int sig, prop, newsig;
2374 mtx_assert(&ps->ps_mtx, MA_OWNED);
2375 PROC_LOCK_ASSERT(p, MA_OWNED);
2377 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2379 sigpending = td->td_sigqueue.sq_signals;
2380 SIGSETNAND(sigpending, td->td_sigmask);
2382 if (p->p_flag & P_PPWAIT)
2383 SIG_STOPSIGMASK(sigpending);
2384 if (SIGISEMPTY(sigpending)) /* no signal to send */
2386 sig = sig_ffs(&sigpending);
2388 if (p->p_stops & S_SIG) {
2389 mtx_unlock(&ps->ps_mtx);
2390 stopevent(p, S_SIG, sig);
2391 mtx_lock(&ps->ps_mtx);
2395 * We should see pending but ignored signals
2396 * only if P_TRACED was on when they were posted.
2398 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2399 sigqueue_delete(&td->td_sigqueue, sig);
2402 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2404 * If traced, always stop.
2406 mtx_unlock(&ps->ps_mtx);
2407 newsig = ptracestop(td, sig);
2408 mtx_lock(&ps->ps_mtx);
2410 if (sig != newsig) {
2414 * XXX shrug off debugger, it causes siginfo to
2417 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2420 * If parent wants us to take the signal,
2421 * then it will leave it in p->p_xstat;
2422 * otherwise we just look for signals again.
2429 * Put the new signal into td_sigqueue. If the
2430 * signal is being masked, look for other signals.
2432 SIGADDSET(td->td_sigqueue.sq_signals, sig);
2433 if (SIGISMEMBER(td->td_sigmask, sig))
2439 * If the traced bit got turned off, go back up
2440 * to the top to rescan signals. This ensures
2441 * that p_sig* and p_sigact are consistent.
2443 if ((p->p_flag & P_TRACED) == 0)
2447 prop = sigprop(sig);
2450 * Decide whether the signal should be returned.
2451 * Return the signal's number, or fall through
2452 * to clear it from the pending mask.
2454 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2456 case (intptr_t)SIG_DFL:
2458 * Don't take default actions on system processes.
2460 if (p->p_pid <= 1) {
2463 * Are you sure you want to ignore SIGSEGV
2466 printf("Process (pid %lu) got signal %d\n",
2467 (u_long)p->p_pid, sig);
2469 break; /* == ignore */
2472 * If there is a pending stop signal to process
2473 * with default action, stop here,
2474 * then clear the signal. However,
2475 * if process is member of an orphaned
2476 * process group, ignore tty stop signals.
2478 if (prop & SA_STOP) {
2479 if (p->p_flag & P_TRACED ||
2480 (p->p_pgrp->pg_jobc == 0 &&
2482 break; /* == ignore */
2483 mtx_unlock(&ps->ps_mtx);
2484 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2485 &p->p_mtx.lock_object, "Catching SIGSTOP");
2486 p->p_flag |= P_STOPPED_SIG;
2489 sig_suspend_threads(td, p, 0);
2490 thread_suspend_switch(td);
2492 mtx_lock(&ps->ps_mtx);
2494 } else if (prop & SA_IGNORE) {
2496 * Except for SIGCONT, shouldn't get here.
2497 * Default action is to ignore; drop it.
2499 break; /* == ignore */
2504 case (intptr_t)SIG_IGN:
2506 * Masking above should prevent us ever trying
2507 * to take action on an ignored signal other
2508 * than SIGCONT, unless process is traced.
2510 if ((prop & SA_CONT) == 0 &&
2511 (p->p_flag & P_TRACED) == 0)
2512 printf("issignal\n");
2513 break; /* == ignore */
2517 * This signal has an action, let
2518 * postsig() process it.
2522 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2528 thread_stopped(struct proc *p)
2532 PROC_LOCK_ASSERT(p, MA_OWNED);
2533 PROC_SLOCK_ASSERT(p, MA_OWNED);
2537 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2539 p->p_flag &= ~P_WAITED;
2540 PROC_LOCK(p->p_pptr);
2541 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2542 CLD_TRAPPED : CLD_STOPPED);
2543 PROC_UNLOCK(p->p_pptr);
2549 * Take the action for the specified signal
2550 * from the current set of pending signals.
2556 struct thread *td = curthread;
2557 register struct proc *p = td->td_proc;
2561 sigset_t returnmask;
2564 KASSERT(sig != 0, ("postsig"));
2566 PROC_LOCK_ASSERT(p, MA_OWNED);
2568 mtx_assert(&ps->ps_mtx, MA_OWNED);
2569 ksiginfo_init(&ksi);
2570 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2571 ksi.ksi_signo = sig;
2572 if (ksi.ksi_code == SI_TIMER)
2573 itimer_accept(p, ksi.ksi_timerid, &ksi);
2574 action = ps->ps_sigact[_SIG_IDX(sig)];
2576 if (KTRPOINT(td, KTR_PSIG))
2577 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2578 &td->td_oldsigmask : &td->td_sigmask, 0);
2580 if (p->p_stops & S_SIG) {
2581 mtx_unlock(&ps->ps_mtx);
2582 stopevent(p, S_SIG, sig);
2583 mtx_lock(&ps->ps_mtx);
2586 if (action == SIG_DFL) {
2588 * Default action, where the default is to kill
2589 * the process. (Other cases were ignored above.)
2591 mtx_unlock(&ps->ps_mtx);
2596 * If we get here, the signal must be caught.
2598 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2599 ("postsig action"));
2601 * Set the new mask value and also defer further
2602 * occurrences of this signal.
2604 * Special case: user has done a sigsuspend. Here the
2605 * current mask is not of interest, but rather the
2606 * mask from before the sigsuspend is what we want
2607 * restored after the signal processing is completed.
2609 if (td->td_pflags & TDP_OLDMASK) {
2610 returnmask = td->td_oldsigmask;
2611 td->td_pflags &= ~TDP_OLDMASK;
2613 returnmask = td->td_sigmask;
2615 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2616 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2617 SIGADDSET(td->td_sigmask, sig);
2619 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2621 * See kern_sigaction() for origin of this code.
2623 SIGDELSET(ps->ps_sigcatch, sig);
2624 if (sig != SIGCONT &&
2625 sigprop(sig) & SA_IGNORE)
2626 SIGADDSET(ps->ps_sigignore, sig);
2627 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2629 td->td_ru.ru_nsignals++;
2630 if (p->p_sig != sig) {
2637 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2642 * Kill the current process for stated reason.
2650 PROC_LOCK_ASSERT(p, MA_OWNED);
2651 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2652 p, p->p_pid, p->p_comm);
2653 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2654 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2655 psignal(p, SIGKILL);
2659 * Force the current process to exit with the specified signal, dumping core
2660 * if appropriate. We bypass the normal tests for masked and caught signals,
2661 * allowing unrecoverable failures to terminate the process without changing
2662 * signal state. Mark the accounting record with the signal termination.
2663 * If dumping core, save the signal number for the debugger. Calls exit and
2671 struct proc *p = td->td_proc;
2673 PROC_LOCK_ASSERT(p, MA_OWNED);
2674 p->p_acflag |= AXSIG;
2676 * We must be single-threading to generate a core dump. This
2677 * ensures that the registers in the core file are up-to-date.
2678 * Also, the ELF dump handler assumes that the thread list doesn't
2679 * change out from under it.
2681 * XXX If another thread attempts to single-thread before us
2682 * (e.g. via fork()), we won't get a dump at all.
2684 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2687 * Log signals which would cause core dumps
2688 * (Log as LOG_INFO to appease those who don't want
2690 * XXX : Todo, as well as euid, write out ruid too
2691 * Note that coredump() drops proc lock.
2693 if (coredump(td) == 0)
2695 if (kern_logsigexit)
2697 "pid %d (%s), uid %d: exited on signal %d%s\n",
2698 p->p_pid, p->p_comm,
2699 td->td_ucred ? td->td_ucred->cr_uid : -1,
2701 sig & WCOREFLAG ? " (core dumped)" : "");
2704 exit1(td, W_EXITCODE(0, sig));
2709 * Send queued SIGCHLD to parent when child process's state
2713 sigparent(struct proc *p, int reason, int status)
2715 PROC_LOCK_ASSERT(p, MA_OWNED);
2716 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2718 if (p->p_ksi != NULL) {
2719 p->p_ksi->ksi_signo = SIGCHLD;
2720 p->p_ksi->ksi_code = reason;
2721 p->p_ksi->ksi_status = status;
2722 p->p_ksi->ksi_pid = p->p_pid;
2723 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2724 if (KSI_ONQ(p->p_ksi))
2727 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
2731 childproc_jobstate(struct proc *p, int reason, int status)
2735 PROC_LOCK_ASSERT(p, MA_OWNED);
2736 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2739 * Wake up parent sleeping in kern_wait(), also send
2740 * SIGCHLD to parent, but SIGCHLD does not guarantee
2741 * that parent will awake, because parent may masked
2744 p->p_pptr->p_flag |= P_STATCHILD;
2747 ps = p->p_pptr->p_sigacts;
2748 mtx_lock(&ps->ps_mtx);
2749 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2750 mtx_unlock(&ps->ps_mtx);
2751 sigparent(p, reason, status);
2753 mtx_unlock(&ps->ps_mtx);
2757 childproc_stopped(struct proc *p, int reason)
2759 childproc_jobstate(p, reason, p->p_xstat);
2763 childproc_continued(struct proc *p)
2765 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
2769 childproc_exited(struct proc *p)
2772 int status = p->p_xstat; /* convert to int */
2774 reason = CLD_EXITED;
2775 if (WCOREDUMP(status))
2776 reason = CLD_DUMPED;
2777 else if (WIFSIGNALED(status))
2778 reason = CLD_KILLED;
2780 * XXX avoid calling wakeup(p->p_pptr), the work is
2783 sigparent(p, reason, status);
2786 static char corefilename[MAXPATHLEN] = {"%N.core"};
2787 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2788 sizeof(corefilename), "process corefile name format string");
2791 * expand_name(name, uid, pid)
2792 * Expand the name described in corefilename, using name, uid, and pid.
2793 * corefilename is a printf-like string, with three format specifiers:
2794 * %N name of process ("name")
2795 * %P process id (pid)
2797 * For example, "%N.core" is the default; they can be disabled completely
2798 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2799 * This is controlled by the sysctl variable kern.corefile (see above).
2802 expand_name(name, uid, pid)
2812 format = corefilename;
2813 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2816 (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
2817 for (i = 0; format[i]; i++) {
2818 switch (format[i]) {
2819 case '%': /* Format character */
2821 switch (format[i]) {
2823 sbuf_putc(&sb, '%');
2825 case 'N': /* process name */
2826 sbuf_printf(&sb, "%s", name);
2828 case 'P': /* process id */
2829 sbuf_printf(&sb, "%u", pid);
2831 case 'U': /* user id */
2832 sbuf_printf(&sb, "%u", uid);
2836 "Unknown format character %c in "
2837 "corename `%s'\n", format[i], format);
2841 sbuf_putc(&sb, format[i]);
2844 if (sbuf_overflowed(&sb)) {
2846 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
2847 "long\n", (long)pid, name, (u_long)uid);
2857 * Dump a process' core. The main routine does some
2858 * policy checking, and creates the name of the coredump;
2859 * then it passes on a vnode and a size limit to the process-specific
2860 * coredump routine if there is one; if there _is not_ one, it returns
2861 * ENOSYS; otherwise it returns the error from the process-specific routine.
2865 coredump(struct thread *td)
2867 struct proc *p = td->td_proc;
2868 register struct vnode *vp;
2869 register struct ucred *cred = td->td_ucred;
2871 struct nameidata nd;
2873 int error, error1, flags, locked;
2875 char *name; /* name of corefile */
2879 PROC_LOCK_ASSERT(p, MA_OWNED);
2880 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
2881 _STOPEVENT(p, S_CORE, 0);
2883 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2887 audit_proc_coredump(td, NULL, EINVAL);
2891 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2894 audit_proc_coredump(td, name, EFAULT);
2901 * Note that the bulk of limit checking is done after
2902 * the corefile is created. The exception is if the limit
2903 * for corefiles is 0, in which case we don't bother
2904 * creating the corefile at all. This layout means that
2905 * a corefile is truncated instead of not being created,
2906 * if it is larger than the limit.
2908 limit = (off_t)lim_cur(p, RLIMIT_CORE);
2912 audit_proc_coredump(td, name, EFBIG);
2919 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
2920 flags = O_CREAT | FWRITE | O_NOFOLLOW;
2921 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, NULL);
2924 audit_proc_coredump(td, name, error);
2929 vfslocked = NDHASGIANT(&nd);
2930 NDFREE(&nd, NDF_ONLY_PNBUF);
2933 /* Don't dump to non-regular files or files with links. */
2934 if (vp->v_type != VREG ||
2935 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2942 lf.l_whence = SEEK_SET;
2945 lf.l_type = F_WRLCK;
2946 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
2948 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2949 lf.l_type = F_UNLCK;
2951 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2952 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2954 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2956 VFS_UNLOCK_GIANT(vfslocked);
2962 if (set_core_nodump_flag)
2963 vattr.va_flags = UF_NODUMP;
2964 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2965 VOP_LEASE(vp, td, cred, LEASE_WRITE);
2966 VOP_SETATTR(vp, &vattr, cred, td);
2968 vn_finished_write(mp);
2970 p->p_acflag |= ACORE;
2973 error = p->p_sysent->sv_coredump ?
2974 p->p_sysent->sv_coredump(td, vp, limit) :
2978 lf.l_type = F_UNLCK;
2979 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2982 error1 = vn_close(vp, FWRITE, cred, td);
2987 audit_proc_coredump(td, name, error);
2990 VFS_UNLOCK_GIANT(vfslocked);
2995 * Nonexistent system call-- signal process (may want to handle it). Flag
2996 * error in case process won't see signal immediately (blocked or ignored).
2998 #ifndef _SYS_SYSPROTO_H_
3007 struct nosys_args *args;
3009 struct proc *p = td->td_proc;
3018 * Send a SIGIO or SIGURG signal to a process or process group using stored
3019 * credentials rather than those of the current process.
3022 pgsigio(sigiop, sig, checkctty)
3023 struct sigio **sigiop;
3026 struct sigio *sigio;
3030 if (sigio == NULL) {
3034 if (sigio->sio_pgid > 0) {
3035 PROC_LOCK(sigio->sio_proc);
3036 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3037 psignal(sigio->sio_proc, sig);
3038 PROC_UNLOCK(sigio->sio_proc);
3039 } else if (sigio->sio_pgid < 0) {
3042 PGRP_LOCK(sigio->sio_pgrp);
3043 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3045 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3046 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3050 PGRP_UNLOCK(sigio->sio_pgrp);
3056 filt_sigattach(struct knote *kn)
3058 struct proc *p = curproc;
3060 kn->kn_ptr.p_proc = p;
3061 kn->kn_flags |= EV_CLEAR; /* automatically set */
3063 knlist_add(&p->p_klist, kn, 0);
3069 filt_sigdetach(struct knote *kn)
3071 struct proc *p = kn->kn_ptr.p_proc;
3073 knlist_remove(&p->p_klist, kn, 0);
3077 * signal knotes are shared with proc knotes, so we apply a mask to
3078 * the hint in order to differentiate them from process hints. This
3079 * could be avoided by using a signal-specific knote list, but probably
3080 * isn't worth the trouble.
3083 filt_signal(struct knote *kn, long hint)
3086 if (hint & NOTE_SIGNAL) {
3087 hint &= ~NOTE_SIGNAL;
3089 if (kn->kn_id == hint)
3092 return (kn->kn_data != 0);
3100 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3102 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3107 sigacts_free(struct sigacts *ps)
3110 mtx_lock(&ps->ps_mtx);
3112 if (ps->ps_refcnt == 0) {
3113 mtx_destroy(&ps->ps_mtx);
3114 free(ps, M_SUBPROC);
3116 mtx_unlock(&ps->ps_mtx);
3120 sigacts_hold(struct sigacts *ps)
3122 mtx_lock(&ps->ps_mtx);
3124 mtx_unlock(&ps->ps_mtx);
3129 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3132 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3133 mtx_lock(&src->ps_mtx);
3134 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3135 mtx_unlock(&src->ps_mtx);
3139 sigacts_shared(struct sigacts *ps)
3143 mtx_lock(&ps->ps_mtx);
3144 shared = ps->ps_refcnt > 1;
3145 mtx_unlock(&ps->ps_mtx);