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/unistd.h>
75 #include <vm/vm_extern.h>
78 #include <machine/cpu.h>
80 #if defined (__alpha__) && !defined(COMPAT_43)
81 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
84 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
86 static int coredump(struct thread *);
87 static char *expand_name(const char *, uid_t, pid_t);
88 static int killpg1(struct thread *td, int sig, int pgid, int all);
89 static int issignal(struct thread *p);
90 static int sigprop(int sig);
91 static void tdsigwakeup(struct thread *td, int sig, sig_t action);
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 int kern_sigtimedwait(struct thread *, sigset_t,
97 ksiginfo_t *, struct timespec *);
98 static int do_tdsignal(struct thread *, int, ksiginfo_t *, sigtarget_t);
99 static void sigqueue_start(void);
100 static int psignal_common(struct proc *p, int sig, ksiginfo_t *ksi);
102 static uma_zone_t ksiginfo_zone = NULL;
103 struct filterops sig_filtops =
104 { 0, filt_sigattach, filt_sigdetach, filt_signal };
106 static int kern_logsigexit = 1;
107 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
109 "Log processes quitting on abnormal signals to syslog(3)");
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 queue_rt_signal_only = 1;
118 SYSCTL_INT(_kern_sigqueue, OID_AUTO, queue_rt_signal_only, CTLFLAG_RW,
119 &queue_rt_signal_only, 0, "Only rt signal is queued");
121 static int preallocate_siginfo = 1024;
122 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
123 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
124 &preallocate_siginfo, 0, "Preallocated signal memory size");
126 static int signal_overflow = 0;
127 SYSCTL_INT(_kern_sigqueue, OID_AUTO, signal_overflow, CTLFLAG_RD,
128 &signal_overflow, 0, "Number of signals overflew");
130 static int signal_alloc_fail = 0;
131 SYSCTL_INT(_kern_sigqueue, OID_AUTO, signal_alloc_fail, CTLFLAG_RD,
132 &signal_alloc_fail, 0, "signals failed to be allocated");
134 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
137 * Policy -- Can ucred cr1 send SIGIO to process cr2?
138 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
139 * in the right situations.
141 #define CANSIGIO(cr1, cr2) \
142 ((cr1)->cr_uid == 0 || \
143 (cr1)->cr_ruid == (cr2)->cr_ruid || \
144 (cr1)->cr_uid == (cr2)->cr_ruid || \
145 (cr1)->cr_ruid == (cr2)->cr_uid || \
146 (cr1)->cr_uid == (cr2)->cr_uid)
149 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
150 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
152 static int do_coredump = 1;
153 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
154 &do_coredump, 0, "Enable/Disable coredumps");
156 static int set_core_nodump_flag = 0;
157 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
158 0, "Enable setting the NODUMP flag on coredump files");
161 * Signal properties and actions.
162 * The array below categorizes the signals and their default actions
163 * according to the following properties:
165 #define SA_KILL 0x01 /* terminates process by default */
166 #define SA_CORE 0x02 /* ditto and coredumps */
167 #define SA_STOP 0x04 /* suspend process */
168 #define SA_TTYSTOP 0x08 /* ditto, from tty */
169 #define SA_IGNORE 0x10 /* ignore by default */
170 #define SA_CONT 0x20 /* continue if suspended */
171 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
172 #define SA_PROC 0x80 /* deliverable to any thread */
174 static int sigproptbl[NSIG] = {
175 SA_KILL|SA_PROC, /* SIGHUP */
176 SA_KILL|SA_PROC, /* SIGINT */
177 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
178 SA_KILL|SA_CORE, /* SIGILL */
179 SA_KILL|SA_CORE, /* SIGTRAP */
180 SA_KILL|SA_CORE, /* SIGABRT */
181 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
182 SA_KILL|SA_CORE, /* SIGFPE */
183 SA_KILL|SA_PROC, /* SIGKILL */
184 SA_KILL|SA_CORE, /* SIGBUS */
185 SA_KILL|SA_CORE, /* SIGSEGV */
186 SA_KILL|SA_CORE, /* SIGSYS */
187 SA_KILL|SA_PROC, /* SIGPIPE */
188 SA_KILL|SA_PROC, /* SIGALRM */
189 SA_KILL|SA_PROC, /* SIGTERM */
190 SA_IGNORE|SA_PROC, /* SIGURG */
191 SA_STOP|SA_PROC, /* SIGSTOP */
192 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
193 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
194 SA_IGNORE|SA_PROC, /* SIGCHLD */
195 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
196 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
197 SA_IGNORE|SA_PROC, /* SIGIO */
198 SA_KILL, /* SIGXCPU */
199 SA_KILL, /* SIGXFSZ */
200 SA_KILL|SA_PROC, /* SIGVTALRM */
201 SA_KILL|SA_PROC, /* SIGPROF */
202 SA_IGNORE|SA_PROC, /* SIGWINCH */
203 SA_IGNORE|SA_PROC, /* SIGINFO */
204 SA_KILL|SA_PROC, /* SIGUSR1 */
205 SA_KILL|SA_PROC, /* SIGUSR2 */
211 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
212 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
213 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
219 if (ksiginfo_zone != NULL)
220 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, M_NOWAIT | M_ZERO));
225 ksiginfo_free(ksiginfo_t *ksi)
227 uma_zfree(ksiginfo_zone, ksi);
231 ksiginfo_tryfree(ksiginfo_t *ksi)
233 if (!(ksi->ksi_flags & KSI_EXT)) {
234 uma_zfree(ksiginfo_zone, ksi);
241 sigqueue_init(sigqueue_t *list, struct proc *p)
243 SIGEMPTYSET(list->sq_signals);
244 TAILQ_INIT(&list->sq_list);
246 list->sq_flags = SQ_INIT;
250 * Get a signal's ksiginfo.
252 * 0 - signal not found
253 * others - signal number
256 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
258 struct proc *p = sq->sq_proc;
259 struct ksiginfo *ksi, *next;
262 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
264 if (!SIGISMEMBER(sq->sq_signals, signo))
267 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
268 next = TAILQ_NEXT(ksi, ksi_link);
269 if (ksi->ksi_signo == signo) {
271 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
272 ksi->ksi_sigq = NULL;
273 ksiginfo_copy(ksi, si);
274 if (ksiginfo_tryfree(ksi) && p != NULL)
282 SIGDELSET(sq->sq_signals, signo);
283 si->ksi_signo = signo;
288 sigqueue_take(ksiginfo_t *ksi)
294 if ((sq = ksi->ksi_sigq) == NULL)
298 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
299 ksi->ksi_sigq = NULL;
300 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
303 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
304 kp = TAILQ_NEXT(kp, ksi_link)) {
305 if (kp->ksi_signo == ksi->ksi_signo)
309 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
313 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
315 struct proc *p = sq->sq_proc;
316 struct ksiginfo *ksi;
319 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
321 if (signo == SIGKILL || signo == SIGSTOP || si == NULL)
324 /* directly insert the ksi, don't copy it */
325 if (si->ksi_flags & KSI_INS) {
326 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
331 if (__predict_false(ksiginfo_zone == NULL))
334 if (p != NULL && p->p_pendingcnt > max_pending_per_proc) {
337 } else if ((ksi = ksiginfo_alloc()) == NULL) {
343 ksiginfo_copy(si, ksi);
344 ksi->ksi_signo = signo;
345 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
349 if ((si->ksi_flags & KSI_TRAP) != 0) {
358 SIGADDSET(sq->sq_signals, signo);
363 sigqueue_flush(sigqueue_t *sq)
365 struct proc *p = sq->sq_proc;
368 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
371 PROC_LOCK_ASSERT(p, MA_OWNED);
373 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
374 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
375 ksi->ksi_sigq = NULL;
376 if (ksiginfo_tryfree(ksi) && p != NULL)
380 SIGEMPTYSET(sq->sq_signals);
384 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
388 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
390 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
391 SIGADDSET(*set, ksi->ksi_signo);
395 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
398 struct proc *p1, *p2;
399 ksiginfo_t *ksi, *next;
401 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
402 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
404 * make a copy, this allows setp to point to src or dst
405 * sq_signals without trouble.
410 /* Move siginfo to target list */
411 for (ksi = TAILQ_FIRST(&src->sq_list); ksi != NULL; ksi = next) {
412 next = TAILQ_NEXT(ksi, ksi_link);
413 if (SIGISMEMBER(set, ksi->ksi_signo)) {
414 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
417 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
424 /* Move pending bits to target list */
425 tmp = src->sq_signals;
427 SIGSETOR(dst->sq_signals, tmp);
428 SIGSETNAND(src->sq_signals, tmp);
430 /* Finally, rescan src queue and set pending bits for it */
431 sigqueue_collect_set(src, &src->sq_signals);
435 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
440 SIGADDSET(set, signo);
441 sigqueue_move_set(src, dst, &set);
445 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
447 struct proc *p = sq->sq_proc;
448 ksiginfo_t *ksi, *next;
450 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
452 /* Remove siginfo queue */
453 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) {
454 next = TAILQ_NEXT(ksi, ksi_link);
455 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
456 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
457 ksi->ksi_sigq = NULL;
458 if (ksiginfo_tryfree(ksi) && p != NULL)
462 SIGSETNAND(sq->sq_signals, *set);
463 /* Finally, rescan queue and set pending bits for it */
464 sigqueue_collect_set(sq, &sq->sq_signals);
468 sigqueue_delete(sigqueue_t *sq, int signo)
473 SIGADDSET(set, signo);
474 sigqueue_delete_set(sq, &set);
477 /* Remove a set of signals for a process */
479 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
484 PROC_LOCK_ASSERT(p, MA_OWNED);
486 sigqueue_init(&worklist, NULL);
487 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
489 mtx_lock_spin(&sched_lock);
490 FOREACH_THREAD_IN_PROC(p, td0)
491 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
492 mtx_unlock_spin(&sched_lock);
494 sigqueue_flush(&worklist);
498 sigqueue_delete_proc(struct proc *p, int signo)
503 SIGADDSET(set, signo);
504 sigqueue_delete_set_proc(p, &set);
508 sigqueue_delete_stopmask_proc(struct proc *p)
513 SIGADDSET(set, SIGSTOP);
514 SIGADDSET(set, SIGTSTP);
515 SIGADDSET(set, SIGTTIN);
516 SIGADDSET(set, SIGTTOU);
517 sigqueue_delete_set_proc(p, &set);
521 * Determine signal that should be delivered to process p, the current
522 * process, 0 if none. If there is a pending stop signal with default
523 * action, the process stops in issignal().
528 cursig(struct thread *td)
530 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
531 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
532 mtx_assert(&sched_lock, MA_NOTOWNED);
533 return (SIGPENDING(td) ? issignal(td) : 0);
537 * Arrange for ast() to handle unmasked pending signals on return to user
538 * mode. This must be called whenever a signal is added to td_sigqueue or
539 * unmasked in td_sigmask.
542 signotify(struct thread *td)
549 PROC_LOCK_ASSERT(p, MA_OWNED);
552 * If our mask changed we may have to move signal that were
553 * previously masked by all threads to our sigqueue.
555 set = p->p_sigqueue.sq_signals;
556 if (p->p_flag & P_SA)
557 saved = p->p_sigqueue.sq_signals;
558 SIGSETNAND(set, td->td_sigmask);
559 if (! SIGISEMPTY(set))
560 sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set);
561 if (SIGPENDING(td)) {
562 mtx_lock_spin(&sched_lock);
563 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
564 mtx_unlock_spin(&sched_lock);
566 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
567 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
568 /* pending set changed */
569 p->p_flag |= P_SIGEVENT;
570 wakeup(&p->p_siglist);
576 sigonstack(size_t sp)
578 struct thread *td = curthread;
580 return ((td->td_pflags & TDP_ALTSTACK) ?
581 #if defined(COMPAT_43)
582 ((td->td_sigstk.ss_size == 0) ?
583 (td->td_sigstk.ss_flags & SS_ONSTACK) :
584 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
586 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
595 if (sig > 0 && sig < NSIG)
596 return (sigproptbl[_SIG_IDX(sig)]);
601 sig_ffs(sigset_t *set)
605 for (i = 0; i < _SIG_WORDS; i++)
607 return (ffs(set->__bits[i]) + (i * 32));
620 kern_sigaction(td, sig, act, oact, flags)
623 struct sigaction *act, *oact;
627 struct proc *p = td->td_proc;
629 if (!_SIG_VALID(sig))
634 mtx_lock(&ps->ps_mtx);
636 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
637 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
639 if (SIGISMEMBER(ps->ps_sigonstack, sig))
640 oact->sa_flags |= SA_ONSTACK;
641 if (!SIGISMEMBER(ps->ps_sigintr, sig))
642 oact->sa_flags |= SA_RESTART;
643 if (SIGISMEMBER(ps->ps_sigreset, sig))
644 oact->sa_flags |= SA_RESETHAND;
645 if (SIGISMEMBER(ps->ps_signodefer, sig))
646 oact->sa_flags |= SA_NODEFER;
647 if (SIGISMEMBER(ps->ps_siginfo, sig))
648 oact->sa_flags |= SA_SIGINFO;
649 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
650 oact->sa_flags |= SA_NOCLDSTOP;
651 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
652 oact->sa_flags |= SA_NOCLDWAIT;
655 if ((sig == SIGKILL || sig == SIGSTOP) &&
656 act->sa_handler != SIG_DFL) {
657 mtx_unlock(&ps->ps_mtx);
663 * Change setting atomically.
666 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
667 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
668 if (act->sa_flags & SA_SIGINFO) {
669 ps->ps_sigact[_SIG_IDX(sig)] =
670 (__sighandler_t *)act->sa_sigaction;
671 SIGADDSET(ps->ps_siginfo, sig);
673 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
674 SIGDELSET(ps->ps_siginfo, sig);
676 if (!(act->sa_flags & SA_RESTART))
677 SIGADDSET(ps->ps_sigintr, sig);
679 SIGDELSET(ps->ps_sigintr, sig);
680 if (act->sa_flags & SA_ONSTACK)
681 SIGADDSET(ps->ps_sigonstack, sig);
683 SIGDELSET(ps->ps_sigonstack, sig);
684 if (act->sa_flags & SA_RESETHAND)
685 SIGADDSET(ps->ps_sigreset, sig);
687 SIGDELSET(ps->ps_sigreset, sig);
688 if (act->sa_flags & SA_NODEFER)
689 SIGADDSET(ps->ps_signodefer, sig);
691 SIGDELSET(ps->ps_signodefer, sig);
692 if (sig == SIGCHLD) {
693 if (act->sa_flags & SA_NOCLDSTOP)
694 ps->ps_flag |= PS_NOCLDSTOP;
696 ps->ps_flag &= ~PS_NOCLDSTOP;
697 if (act->sa_flags & SA_NOCLDWAIT) {
699 * Paranoia: since SA_NOCLDWAIT is implemented
700 * by reparenting the dying child to PID 1 (and
701 * trust it to reap the zombie), PID 1 itself
702 * is forbidden to set SA_NOCLDWAIT.
705 ps->ps_flag &= ~PS_NOCLDWAIT;
707 ps->ps_flag |= PS_NOCLDWAIT;
709 ps->ps_flag &= ~PS_NOCLDWAIT;
710 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
711 ps->ps_flag |= PS_CLDSIGIGN;
713 ps->ps_flag &= ~PS_CLDSIGIGN;
716 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
717 * and for signals set to SIG_DFL where the default is to
718 * ignore. However, don't put SIGCONT in ps_sigignore, as we
719 * have to restart the process.
721 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
722 (sigprop(sig) & SA_IGNORE &&
723 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
724 if ((p->p_flag & P_SA) &&
725 SIGISMEMBER(p->p_sigqueue.sq_signals, sig)) {
726 p->p_flag |= P_SIGEVENT;
727 wakeup(&p->p_siglist);
729 /* never to be seen again */
730 sigqueue_delete_proc(p, sig);
732 /* easier in psignal */
733 SIGADDSET(ps->ps_sigignore, sig);
734 SIGDELSET(ps->ps_sigcatch, sig);
736 SIGDELSET(ps->ps_sigignore, sig);
737 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
738 SIGDELSET(ps->ps_sigcatch, sig);
740 SIGADDSET(ps->ps_sigcatch, sig);
742 #ifdef COMPAT_FREEBSD4
743 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
744 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
745 (flags & KSA_FREEBSD4) == 0)
746 SIGDELSET(ps->ps_freebsd4, sig);
748 SIGADDSET(ps->ps_freebsd4, sig);
751 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
752 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
753 (flags & KSA_OSIGSET) == 0)
754 SIGDELSET(ps->ps_osigset, sig);
756 SIGADDSET(ps->ps_osigset, sig);
759 mtx_unlock(&ps->ps_mtx);
764 #ifndef _SYS_SYSPROTO_H_
765 struct sigaction_args {
767 struct sigaction *act;
768 struct sigaction *oact;
777 register struct sigaction_args *uap;
779 struct sigaction act, oact;
780 register struct sigaction *actp, *oactp;
783 actp = (uap->act != NULL) ? &act : NULL;
784 oactp = (uap->oact != NULL) ? &oact : NULL;
786 error = copyin(uap->act, actp, sizeof(act));
790 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
792 error = copyout(oactp, uap->oact, sizeof(oact));
796 #ifdef COMPAT_FREEBSD4
797 #ifndef _SYS_SYSPROTO_H_
798 struct freebsd4_sigaction_args {
800 struct sigaction *act;
801 struct sigaction *oact;
808 freebsd4_sigaction(td, uap)
810 register struct freebsd4_sigaction_args *uap;
812 struct sigaction act, oact;
813 register struct sigaction *actp, *oactp;
817 actp = (uap->act != NULL) ? &act : NULL;
818 oactp = (uap->oact != NULL) ? &oact : NULL;
820 error = copyin(uap->act, actp, sizeof(act));
824 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
826 error = copyout(oactp, uap->oact, sizeof(oact));
829 #endif /* COMAPT_FREEBSD4 */
831 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
832 #ifndef _SYS_SYSPROTO_H_
833 struct osigaction_args {
835 struct osigaction *nsa;
836 struct osigaction *osa;
845 register struct osigaction_args *uap;
847 struct osigaction sa;
848 struct sigaction nsa, osa;
849 register struct sigaction *nsap, *osap;
852 if (uap->signum <= 0 || uap->signum >= ONSIG)
855 nsap = (uap->nsa != NULL) ? &nsa : NULL;
856 osap = (uap->osa != NULL) ? &osa : NULL;
859 error = copyin(uap->nsa, &sa, sizeof(sa));
862 nsap->sa_handler = sa.sa_handler;
863 nsap->sa_flags = sa.sa_flags;
864 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
866 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
867 if (osap && !error) {
868 sa.sa_handler = osap->sa_handler;
869 sa.sa_flags = osap->sa_flags;
870 SIG2OSIG(osap->sa_mask, sa.sa_mask);
871 error = copyout(&sa, uap->osa, sizeof(sa));
876 #if !defined(__i386__) && !defined(__alpha__)
877 /* Avoid replicating the same stub everywhere */
881 struct osigreturn_args *uap;
884 return (nosys(td, (struct nosys_args *)uap));
887 #endif /* COMPAT_43 */
890 * Initialize signal state for process 0;
891 * set to ignore signals that are ignored by default.
902 mtx_lock(&ps->ps_mtx);
903 for (i = 1; i <= NSIG; i++)
904 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
905 SIGADDSET(ps->ps_sigignore, i);
906 mtx_unlock(&ps->ps_mtx);
911 * Reset signals for an exec of the specified process.
914 execsigs(struct proc *p)
921 * Reset caught signals. Held signals remain held
922 * through td_sigmask (unless they were caught,
923 * and are now ignored by default).
925 PROC_LOCK_ASSERT(p, MA_OWNED);
926 td = FIRST_THREAD_IN_PROC(p);
928 mtx_lock(&ps->ps_mtx);
929 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
930 sig = sig_ffs(&ps->ps_sigcatch);
931 SIGDELSET(ps->ps_sigcatch, sig);
932 if (sigprop(sig) & SA_IGNORE) {
934 SIGADDSET(ps->ps_sigignore, sig);
935 sigqueue_delete_proc(p, sig);
937 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
940 * Reset stack state to the user stack.
941 * Clear set of signals caught on the signal stack.
943 td->td_sigstk.ss_flags = SS_DISABLE;
944 td->td_sigstk.ss_size = 0;
945 td->td_sigstk.ss_sp = 0;
946 td->td_pflags &= ~TDP_ALTSTACK;
948 * Reset no zombies if child dies flag as Solaris does.
950 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
951 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
952 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
953 mtx_unlock(&ps->ps_mtx);
959 * Manipulate signal mask.
962 kern_sigprocmask(td, how, set, oset, old)
965 sigset_t *set, *oset;
970 PROC_LOCK(td->td_proc);
972 *oset = td->td_sigmask;
979 SIGSETOR(td->td_sigmask, *set);
982 SIGSETNAND(td->td_sigmask, *set);
988 SIGSETLO(td->td_sigmask, *set);
990 td->td_sigmask = *set;
998 PROC_UNLOCK(td->td_proc);
1003 * sigprocmask() - MP SAFE
1006 #ifndef _SYS_SYSPROTO_H_
1007 struct sigprocmask_args {
1009 const sigset_t *set;
1014 sigprocmask(td, uap)
1015 register struct thread *td;
1016 struct sigprocmask_args *uap;
1019 sigset_t *setp, *osetp;
1022 setp = (uap->set != NULL) ? &set : NULL;
1023 osetp = (uap->oset != NULL) ? &oset : NULL;
1025 error = copyin(uap->set, setp, sizeof(set));
1029 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1030 if (osetp && !error) {
1031 error = copyout(osetp, uap->oset, sizeof(oset));
1036 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1038 * osigprocmask() - MP SAFE
1040 #ifndef _SYS_SYSPROTO_H_
1041 struct osigprocmask_args {
1047 osigprocmask(td, uap)
1048 register struct thread *td;
1049 struct osigprocmask_args *uap;
1054 OSIG2SIG(uap->mask, set);
1055 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1056 SIG2OSIG(oset, td->td_retval[0]);
1059 #endif /* COMPAT_43 */
1065 sigwait(struct thread *td, struct sigwait_args *uap)
1071 error = copyin(uap->set, &set, sizeof(set));
1073 td->td_retval[0] = error;
1077 error = kern_sigtimedwait(td, set, &ksi, NULL);
1079 if (error == ERESTART)
1081 td->td_retval[0] = error;
1085 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1086 td->td_retval[0] = error;
1093 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1096 struct timespec *timeout;
1102 error = copyin(uap->timeout, &ts, sizeof(ts));
1110 error = copyin(uap->set, &set, sizeof(set));
1114 error = kern_sigtimedwait(td, set, &ksi, timeout);
1119 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1122 td->td_retval[0] = ksi.ksi_signo;
1130 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1136 error = copyin(uap->set, &set, sizeof(set));
1140 error = kern_sigtimedwait(td, set, &ksi, NULL);
1145 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1148 td->td_retval[0] = ksi.ksi_signo;
1153 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1154 struct timespec *timeout)
1159 int error, sig, hz, i, timevalid = 0;
1160 struct timespec rts, ets, ts;
1166 SIG_CANTMASK(waitset);
1170 savedmask = td->td_sigmask;
1172 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1174 getnanouptime(&rts);
1176 timespecadd(&ets, timeout);
1181 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1182 if (!SIGISMEMBER(waitset, i))
1184 if (SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
1185 SIGFILLSET(td->td_sigmask);
1186 SIG_CANTMASK(td->td_sigmask);
1187 SIGDELSET(td->td_sigmask, i);
1188 mtx_lock(&ps->ps_mtx);
1191 mtx_unlock(&ps->ps_mtx);
1192 } else if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
1193 if (p->p_flag & P_SA) {
1194 p->p_flag |= P_SIGEVENT;
1195 wakeup(&p->p_siglist);
1197 sigqueue_move(&p->p_sigqueue, &td->td_sigqueue, i);
1198 SIGFILLSET(td->td_sigmask);
1199 SIG_CANTMASK(td->td_sigmask);
1200 SIGDELSET(td->td_sigmask, i);
1201 mtx_lock(&ps->ps_mtx);
1204 mtx_unlock(&ps->ps_mtx);
1213 * POSIX says this must be checked after looking for pending
1221 getnanouptime(&rts);
1222 if (timespeccmp(&rts, &ets, >=)) {
1227 timespecsub(&ts, &rts);
1228 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1233 td->td_sigmask = savedmask;
1234 SIGSETNAND(td->td_sigmask, waitset);
1236 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
1238 if (error == ERESTART) {
1239 /* timeout can not be restarted. */
1241 } else if (error == EAGAIN) {
1242 /* will calculate timeout by ourself. */
1253 sigqueue_get(&td->td_sigqueue, sig, ksi);
1254 ksi->ksi_signo = sig;
1256 mtx_lock(&ps->ps_mtx);
1257 action = ps->ps_sigact[_SIG_IDX(sig)];
1258 mtx_unlock(&ps->ps_mtx);
1260 if (KTRPOINT(td, KTR_PSIG))
1261 ktrpsig(sig, action, &td->td_sigmask, 0);
1263 _STOPEVENT(p, S_SIG, sig);
1266 td->td_sigmask = savedmask;
1272 #ifndef _SYS_SYSPROTO_H_
1273 struct sigpending_args {
1283 struct sigpending_args *uap;
1285 struct proc *p = td->td_proc;
1289 pending = p->p_sigqueue.sq_signals;
1290 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1292 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1295 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1296 #ifndef _SYS_SYSPROTO_H_
1297 struct osigpending_args {
1305 osigpending(td, uap)
1307 struct osigpending_args *uap;
1309 struct proc *p = td->td_proc;
1313 pending = p->p_sigqueue.sq_signals;
1314 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1316 SIG2OSIG(pending, td->td_retval[0]);
1319 #endif /* COMPAT_43 */
1321 #if defined(COMPAT_43)
1323 * Generalized interface signal handler, 4.3-compatible.
1325 #ifndef _SYS_SYSPROTO_H_
1326 struct osigvec_args {
1339 register struct osigvec_args *uap;
1342 struct sigaction nsa, osa;
1343 register struct sigaction *nsap, *osap;
1346 if (uap->signum <= 0 || uap->signum >= ONSIG)
1348 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1349 osap = (uap->osv != NULL) ? &osa : NULL;
1351 error = copyin(uap->nsv, &vec, sizeof(vec));
1354 nsap->sa_handler = vec.sv_handler;
1355 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1356 nsap->sa_flags = vec.sv_flags;
1357 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1359 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1360 if (osap && !error) {
1361 vec.sv_handler = osap->sa_handler;
1362 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1363 vec.sv_flags = osap->sa_flags;
1364 vec.sv_flags &= ~SA_NOCLDWAIT;
1365 vec.sv_flags ^= SA_RESTART;
1366 error = copyout(&vec, uap->osv, sizeof(vec));
1371 #ifndef _SYS_SYSPROTO_H_
1372 struct osigblock_args {
1381 register struct thread *td;
1382 struct osigblock_args *uap;
1384 struct proc *p = td->td_proc;
1387 OSIG2SIG(uap->mask, set);
1390 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1391 SIGSETOR(td->td_sigmask, set);
1396 #ifndef _SYS_SYSPROTO_H_
1397 struct osigsetmask_args {
1405 osigsetmask(td, uap)
1407 struct osigsetmask_args *uap;
1409 struct proc *p = td->td_proc;
1412 OSIG2SIG(uap->mask, set);
1415 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1416 SIGSETLO(td->td_sigmask, set);
1421 #endif /* COMPAT_43 */
1424 * Suspend calling thread until signal, providing mask to be set
1427 #ifndef _SYS_SYSPROTO_H_
1428 struct sigsuspend_args {
1429 const sigset_t *sigmask;
1439 struct sigsuspend_args *uap;
1444 error = copyin(uap->sigmask, &mask, sizeof(mask));
1447 return (kern_sigsuspend(td, mask));
1451 kern_sigsuspend(struct thread *td, sigset_t mask)
1453 struct proc *p = td->td_proc;
1456 * When returning from sigsuspend, we want
1457 * the old mask to be restored after the
1458 * signal handler has finished. Thus, we
1459 * save it here and mark the sigacts structure
1463 td->td_oldsigmask = td->td_sigmask;
1464 td->td_pflags |= TDP_OLDMASK;
1466 td->td_sigmask = mask;
1468 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1471 /* always return EINTR rather than ERESTART... */
1475 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1477 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1478 * convention: libc stub passes mask, not pointer, to save a copyin.
1480 #ifndef _SYS_SYSPROTO_H_
1481 struct osigsuspend_args {
1490 osigsuspend(td, uap)
1492 struct osigsuspend_args *uap;
1494 struct proc *p = td->td_proc;
1498 td->td_oldsigmask = td->td_sigmask;
1499 td->td_pflags |= TDP_OLDMASK;
1500 OSIG2SIG(uap->mask, mask);
1502 SIGSETLO(td->td_sigmask, mask);
1504 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1507 /* always return EINTR rather than ERESTART... */
1510 #endif /* COMPAT_43 */
1512 #if defined(COMPAT_43)
1513 #ifndef _SYS_SYSPROTO_H_
1514 struct osigstack_args {
1515 struct sigstack *nss;
1516 struct sigstack *oss;
1526 register struct osigstack_args *uap;
1528 struct sigstack nss, oss;
1531 if (uap->nss != NULL) {
1532 error = copyin(uap->nss, &nss, sizeof(nss));
1536 oss.ss_sp = td->td_sigstk.ss_sp;
1537 oss.ss_onstack = sigonstack(cpu_getstack(td));
1538 if (uap->nss != NULL) {
1539 td->td_sigstk.ss_sp = nss.ss_sp;
1540 td->td_sigstk.ss_size = 0;
1541 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1542 td->td_pflags |= TDP_ALTSTACK;
1544 if (uap->oss != NULL)
1545 error = copyout(&oss, uap->oss, sizeof(oss));
1549 #endif /* COMPAT_43 */
1551 #ifndef _SYS_SYSPROTO_H_
1552 struct sigaltstack_args {
1562 sigaltstack(td, uap)
1564 register struct sigaltstack_args *uap;
1569 if (uap->ss != NULL) {
1570 error = copyin(uap->ss, &ss, sizeof(ss));
1574 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1575 (uap->oss != NULL) ? &oss : NULL);
1578 if (uap->oss != NULL)
1579 error = copyout(&oss, uap->oss, sizeof(stack_t));
1584 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1586 struct proc *p = td->td_proc;
1589 oonstack = sigonstack(cpu_getstack(td));
1592 *oss = td->td_sigstk;
1593 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1594 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1600 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1602 if (!(ss->ss_flags & SS_DISABLE)) {
1603 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1606 td->td_sigstk = *ss;
1607 td->td_pflags |= TDP_ALTSTACK;
1609 td->td_pflags &= ~TDP_ALTSTACK;
1616 * Common code for kill process group/broadcast kill.
1617 * cp is calling process.
1620 killpg1(td, sig, pgid, all)
1621 register struct thread *td;
1624 register struct proc *p;
1632 sx_slock(&allproc_lock);
1633 LIST_FOREACH(p, &allproc, p_list) {
1635 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1640 if (p_cansignal(td, p, sig) == 0) {
1647 sx_sunlock(&allproc_lock);
1649 sx_slock(&proctree_lock);
1652 * zero pgid means send to my process group.
1654 pgrp = td->td_proc->p_pgrp;
1657 pgrp = pgfind(pgid);
1659 sx_sunlock(&proctree_lock);
1663 sx_sunlock(&proctree_lock);
1664 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1666 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1670 if (p_cansignal(td, p, sig) == 0) {
1679 return (nfound ? 0 : ESRCH);
1682 #ifndef _SYS_SYSPROTO_H_
1694 register struct thread *td;
1695 register struct kill_args *uap;
1697 register struct proc *p;
1700 if ((u_int)uap->signum > _SIG_MAXSIG)
1704 /* kill single process */
1705 if ((p = pfind(uap->pid)) == NULL) {
1706 if ((p = zpfind(uap->pid)) == NULL)
1709 error = p_cansignal(td, p, uap->signum);
1710 if (error == 0 && uap->signum)
1711 psignal(p, uap->signum);
1716 case -1: /* broadcast signal */
1717 return (killpg1(td, uap->signum, 0, 1));
1718 case 0: /* signal own process group */
1719 return (killpg1(td, uap->signum, 0, 0));
1720 default: /* negative explicit process group */
1721 return (killpg1(td, uap->signum, -uap->pid, 0));
1726 #if defined(COMPAT_43)
1727 #ifndef _SYS_SYSPROTO_H_
1728 struct okillpg_args {
1740 register struct okillpg_args *uap;
1743 if ((u_int)uap->signum > _SIG_MAXSIG)
1746 return (killpg1(td, uap->signum, uap->pgid, 0));
1748 #endif /* COMPAT_43 */
1750 #ifndef _SYS_SYSPROTO_H_
1751 struct sigqueue_args {
1754 /* union sigval */ void *value;
1759 sigqueue(struct thread *td, struct sigqueue_args *uap)
1765 if ((u_int)uap->signum > _SIG_MAXSIG)
1769 * Specification says sigqueue can only send signal to
1775 if ((p = pfind(uap->pid)) == NULL) {
1776 if ((p = zpfind(uap->pid)) == NULL)
1779 error = p_cansignal(td, p, uap->signum);
1780 if (error == 0 && uap->signum != 0) {
1781 ksiginfo_init(&ksi);
1782 ksi.ksi_signo = uap->signum;
1783 ksi.ksi_code = SI_QUEUE;
1784 ksi.ksi_pid = td->td_proc->p_pid;
1785 ksi.ksi_uid = td->td_ucred->cr_ruid;
1786 ksi.ksi_value.sigval_ptr = uap->value;
1787 error = psignal_info(p, &ksi);
1794 * Send a signal to a process group.
1803 sx_slock(&proctree_lock);
1804 pgrp = pgfind(pgid);
1805 sx_sunlock(&proctree_lock);
1807 pgsignal(pgrp, sig, 0);
1814 * Send a signal to a process group. If checktty is 1,
1815 * limit to members which have a controlling terminal.
1818 pgsignal(pgrp, sig, checkctty)
1822 register struct proc *p;
1825 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1826 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1828 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1836 * Send a signal caused by a trap to the current thread.
1837 * If it will be caught immediately, deliver it with correct code.
1838 * Otherwise, post it normally.
1843 trapsignal(struct thread *td, ksiginfo_t *ksi)
1852 sig = ksi->ksi_signo;
1853 code = ksi->ksi_code;
1854 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1856 if (td->td_pflags & TDP_SA) {
1857 if (td->td_mailbox == NULL)
1858 thread_user_enter(td);
1860 SIGDELSET(td->td_sigmask, sig);
1861 mtx_lock_spin(&sched_lock);
1863 * Force scheduling an upcall, so UTS has chance to
1864 * process the signal before thread runs again in
1868 td->td_upcall->ku_flags |= KUF_DOUPCALL;
1869 mtx_unlock_spin(&sched_lock);
1874 mtx_lock(&ps->ps_mtx);
1875 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1876 !SIGISMEMBER(td->td_sigmask, sig)) {
1877 p->p_stats->p_ru.ru_nsignals++;
1879 if (KTRPOINT(curthread, KTR_PSIG))
1880 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1881 &td->td_sigmask, code);
1883 if (!(td->td_pflags & TDP_SA))
1884 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1885 ksi, &td->td_sigmask);
1886 else if (td->td_mailbox == NULL) {
1887 mtx_unlock(&ps->ps_mtx);
1888 /* UTS caused a sync signal */
1889 p->p_code = code; /* XXX for core dump/debugger */
1890 p->p_sig = sig; /* XXX to verify code */
1893 mtx_unlock(&ps->ps_mtx);
1894 SIGADDSET(td->td_sigmask, sig);
1896 error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
1899 /* UTS memory corrupted */
1901 sigexit(td, SIGSEGV);
1902 mtx_lock(&ps->ps_mtx);
1904 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1905 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1906 SIGADDSET(td->td_sigmask, sig);
1907 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1909 * See kern_sigaction() for origin of this code.
1911 SIGDELSET(ps->ps_sigcatch, sig);
1912 if (sig != SIGCONT &&
1913 sigprop(sig) & SA_IGNORE)
1914 SIGADDSET(ps->ps_sigignore, sig);
1915 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1917 mtx_unlock(&ps->ps_mtx);
1919 mtx_unlock(&ps->ps_mtx);
1920 p->p_code = code; /* XXX for core dump/debugger */
1921 p->p_sig = sig; /* XXX to verify code */
1922 tdsignal(td, sig, ksi, SIGTARGET_TD);
1927 static struct thread *
1928 sigtd(struct proc *p, int sig, int prop)
1930 struct thread *td, *signal_td;
1932 PROC_LOCK_ASSERT(p, MA_OWNED);
1935 * Check if current thread can handle the signal without
1936 * switching conetxt to another thread.
1938 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1941 mtx_lock_spin(&sched_lock);
1942 FOREACH_THREAD_IN_PROC(p, td) {
1943 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1948 if (signal_td == NULL)
1949 signal_td = FIRST_THREAD_IN_PROC(p);
1950 mtx_unlock_spin(&sched_lock);
1955 * Send the signal to the process. If the signal has an action, the action
1956 * is usually performed by the target process rather than the caller; we add
1957 * the signal to the set of pending signals for the process.
1960 * o When a stop signal is sent to a sleeping process that takes the
1961 * default action, the process is stopped without awakening it.
1962 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1963 * regardless of the signal action (eg, blocked or ignored).
1965 * Other ignored signals are discarded immediately.
1970 psignal(struct proc *p, int sig)
1972 (void) psignal_common(p, sig, NULL);
1976 psignal_info(struct proc *p, ksiginfo_t *ksi)
1978 return (psignal_common(p, ksi->ksi_signo, ksi));
1982 psignal_common(struct proc *p, int sig, ksiginfo_t *ksi)
1987 if (!_SIG_VALID(sig))
1988 panic("psignal(): invalid signal");
1990 PROC_LOCK_ASSERT(p, MA_OWNED);
1992 * IEEE Std 1003.1-2001: return success when killing a zombie.
1994 if (p->p_state == PRS_ZOMBIE)
1996 prop = sigprop(sig);
1999 * Find a thread to deliver the signal to.
2001 td = sigtd(p, sig, prop);
2003 return (tdsignal(td, sig, ksi, SIGTARGET_P));
2010 tdsignal(struct thread *td, int sig, ksiginfo_t *ksi, sigtarget_t target)
2013 struct proc *p = td->td_proc;
2016 if (p->p_flag & P_SA)
2017 saved = p->p_sigqueue.sq_signals;
2018 ret = do_tdsignal(td, sig, ksi, target);
2019 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
2020 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) {
2021 /* pending set changed */
2022 p->p_flag |= P_SIGEVENT;
2023 wakeup(&p->p_siglist);
2030 do_tdsignal(struct thread *td, int sig, ksiginfo_t *ksi, sigtarget_t target)
2034 sigqueue_t *sigqueue;
2040 if (!_SIG_VALID(sig))
2041 panic("do_tdsignal(): invalid signal");
2046 PROC_LOCK_ASSERT(p, MA_OWNED);
2047 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2049 prop = sigprop(sig);
2052 * If the signal is blocked and not destined for this thread, then
2053 * assign it to the process so that we can find it later in the first
2054 * thread that unblocks it. Otherwise, assign it to this thread now.
2056 if (target == SIGTARGET_TD) {
2057 sigqueue = &td->td_sigqueue;
2059 if (!SIGISMEMBER(td->td_sigmask, sig))
2060 sigqueue = &td->td_sigqueue;
2062 sigqueue = &p->p_sigqueue;
2066 * If the signal is being ignored,
2067 * or process is exiting or thread is exiting,
2068 * then we forget about it immediately.
2069 * (Note: we don't set SIGCONT in ps_sigignore,
2070 * and if it is set to SIG_IGN,
2071 * action will be SIG_DFL here.)
2073 mtx_lock(&ps->ps_mtx);
2074 if (SIGISMEMBER(ps->ps_sigignore, sig) ||
2075 (p->p_flag & P_WEXIT)) {
2076 mtx_unlock(&ps->ps_mtx);
2079 if (SIGISMEMBER(td->td_sigmask, sig))
2081 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2085 mtx_unlock(&ps->ps_mtx);
2088 sigqueue_delete_stopmask_proc(p);
2089 else if (prop & SA_STOP) {
2091 * If sending a tty stop signal to a member of an orphaned
2092 * process group, discard the signal here if the action
2093 * is default; don't stop the process below if sleeping,
2094 * and don't clear any pending SIGCONT.
2096 if ((prop & SA_TTYSTOP) &&
2097 (p->p_pgrp->pg_jobc == 0) &&
2098 (action == SIG_DFL))
2100 sigqueue_delete_proc(p, SIGCONT);
2101 p->p_flag &= ~P_CONTINUED;
2104 ret = sigqueue_add(sigqueue, sig, ksi);
2107 signotify(td); /* uses schedlock */
2109 * Defer further processing for signals which are held,
2110 * except that stopped processes must be continued by SIGCONT.
2112 if (action == SIG_HOLD &&
2113 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2116 * SIGKILL: Remove procfs STOPEVENTs.
2118 if (sig == SIGKILL) {
2119 /* from procfs_ioctl.c: PIOCBIC */
2121 /* from procfs_ioctl.c: PIOCCONT */
2126 * Some signals have a process-wide effect and a per-thread
2127 * component. Most processing occurs when the process next
2128 * tries to cross the user boundary, however there are some
2129 * times when processing needs to be done immediatly, such as
2130 * waking up threads so that they can cross the user boundary.
2131 * We try do the per-process part here.
2133 if (P_SHOULDSTOP(p)) {
2135 * The process is in stopped mode. All the threads should be
2136 * either winding down or already on the suspended queue.
2138 if (p->p_flag & P_TRACED) {
2140 * The traced process is already stopped,
2141 * so no further action is necessary.
2142 * No signal can restart us.
2147 if (sig == SIGKILL) {
2149 * SIGKILL sets process running.
2150 * It will die elsewhere.
2151 * All threads must be restarted.
2153 p->p_flag &= ~P_STOPPED_SIG;
2157 if (prop & SA_CONT) {
2159 * If SIGCONT is default (or ignored), we continue the
2160 * process but don't leave the signal in sigqueue as
2161 * it has no further action. If SIGCONT is held, we
2162 * continue the process and leave the signal in
2163 * sigqueue. If the process catches SIGCONT, let it
2164 * handle the signal itself. If it isn't waiting on
2165 * an event, it goes back to run state.
2166 * Otherwise, process goes back to sleep state.
2168 p->p_flag &= ~P_STOPPED_SIG;
2169 p->p_flag |= P_CONTINUED;
2170 if (action == SIG_DFL) {
2171 sigqueue_delete(sigqueue, sig);
2172 } else if (action == SIG_CATCH) {
2174 * The process wants to catch it so it needs
2175 * to run at least one thread, but which one?
2176 * It would seem that the answer would be to
2177 * run an upcall in the next KSE to run, and
2178 * deliver the signal that way. In a NON KSE
2179 * process, we need to make sure that the
2180 * single thread is runnable asap.
2181 * XXXKSE for now however, make them all run.
2186 * The signal is not ignored or caught.
2188 mtx_lock_spin(&sched_lock);
2189 thread_unsuspend(p);
2190 mtx_unlock_spin(&sched_lock);
2194 if (prop & SA_STOP) {
2196 * Already stopped, don't need to stop again
2197 * (If we did the shell could get confused).
2198 * Just make sure the signal STOP bit set.
2200 p->p_flag |= P_STOPPED_SIG;
2201 sigqueue_delete(sigqueue, sig);
2206 * All other kinds of signals:
2207 * If a thread is sleeping interruptibly, simulate a
2208 * wakeup so that when it is continued it will be made
2209 * runnable and can look at the signal. However, don't make
2210 * the PROCESS runnable, leave it stopped.
2211 * It may run a bit until it hits a thread_suspend_check().
2213 mtx_lock_spin(&sched_lock);
2214 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2216 mtx_unlock_spin(&sched_lock);
2219 * Mutexes are short lived. Threads waiting on them will
2220 * hit thread_suspend_check() soon.
2222 } else if (p->p_state == PRS_NORMAL) {
2223 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2224 mtx_lock_spin(&sched_lock);
2225 tdsigwakeup(td, sig, action);
2226 mtx_unlock_spin(&sched_lock);
2230 MPASS(action == SIG_DFL);
2232 if (prop & SA_STOP) {
2233 if (p->p_flag & P_PPWAIT)
2235 p->p_flag |= P_STOPPED_SIG;
2238 mtx_lock_spin(&sched_lock);
2239 FOREACH_THREAD_IN_PROC(p, td0) {
2240 if (TD_IS_SLEEPING(td0) &&
2241 (td0->td_flags & TDF_SINTR) &&
2242 !TD_IS_SUSPENDED(td0)) {
2243 thread_suspend_one(td0);
2244 } else if (td != td0) {
2245 td0->td_flags |= TDF_ASTPENDING;
2249 if (p->p_numthreads == p->p_suspcount) {
2250 mtx_unlock_spin(&sched_lock);
2251 sigqueue_delete_proc(p, p->p_xstat);
2253 mtx_unlock_spin(&sched_lock);
2260 /* Not in "NORMAL" state. discard the signal. */
2261 sigqueue_delete(sigqueue, sig);
2266 * The process is not stopped so we need to apply the signal to all the
2271 mtx_lock_spin(&sched_lock);
2272 tdsigwakeup(td, sig, action);
2273 thread_unsuspend(p);
2274 mtx_unlock_spin(&sched_lock);
2276 /* If we jump here, sched_lock should not be owned. */
2277 mtx_assert(&sched_lock, MA_NOTOWNED);
2282 * The force of a signal has been directed against a single
2283 * thread. We need to see what we can do about knocking it
2284 * out of any sleep it may be in etc.
2287 tdsigwakeup(struct thread *td, int sig, sig_t action)
2289 struct proc *p = td->td_proc;
2292 PROC_LOCK_ASSERT(p, MA_OWNED);
2293 mtx_assert(&sched_lock, MA_OWNED);
2294 prop = sigprop(sig);
2297 * Bring the priority of a thread up if we want it to get
2298 * killed in this lifetime.
2300 if (action == SIG_DFL && (prop & SA_KILL)) {
2302 sched_nice(td->td_proc, 0);
2303 if (td->td_priority > PUSER)
2304 sched_prio(td, PUSER);
2307 if (TD_ON_SLEEPQ(td)) {
2309 * If thread is sleeping uninterruptibly
2310 * we can't interrupt the sleep... the signal will
2311 * be noticed when the process returns through
2312 * trap() or syscall().
2314 if ((td->td_flags & TDF_SINTR) == 0)
2317 * If SIGCONT is default (or ignored) and process is
2318 * asleep, we are finished; the process should not
2321 if ((prop & SA_CONT) && action == SIG_DFL) {
2322 mtx_unlock_spin(&sched_lock);
2323 sigqueue_delete(&p->p_sigqueue, sig);
2325 * It may be on either list in this state.
2326 * Remove from both for now.
2328 sigqueue_delete(&td->td_sigqueue, sig);
2329 mtx_lock_spin(&sched_lock);
2334 * Give low priority threads a better chance to run.
2336 if (td->td_priority > PUSER)
2337 sched_prio(td, PUSER);
2342 * Other states do nothing with the signal immediately,
2343 * other than kicking ourselves if we are running.
2344 * It will either never be noticed, or noticed very soon.
2347 if (TD_IS_RUNNING(td) && td != curthread)
2354 ptracestop(struct thread *td, int sig)
2356 struct proc *p = td->td_proc;
2359 PROC_LOCK_ASSERT(p, MA_OWNED);
2360 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2361 &p->p_mtx.mtx_object, "Stopping for traced signal");
2363 mtx_lock_spin(&sched_lock);
2364 td->td_flags |= TDF_XSIG;
2365 mtx_unlock_spin(&sched_lock);
2367 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2368 if (p->p_flag & P_SINGLE_EXIT) {
2369 mtx_lock_spin(&sched_lock);
2370 td->td_flags &= ~TDF_XSIG;
2371 mtx_unlock_spin(&sched_lock);
2375 * Just make wait() to work, the last stopped thread
2380 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2381 mtx_lock_spin(&sched_lock);
2382 FOREACH_THREAD_IN_PROC(p, td0) {
2383 if (TD_IS_SLEEPING(td0) &&
2384 (td0->td_flags & TDF_SINTR) &&
2385 !TD_IS_SUSPENDED(td0)) {
2386 thread_suspend_one(td0);
2387 } else if (td != td0) {
2388 td0->td_flags |= TDF_ASTPENDING;
2393 thread_suspend_one(td);
2396 mi_switch(SW_VOL, NULL);
2397 mtx_unlock_spin(&sched_lock);
2400 if (!(p->p_flag & P_TRACED))
2402 if (td->td_flags & TDF_DBSUSPEND) {
2403 if (p->p_flag & P_SINGLE_EXIT)
2405 mtx_lock_spin(&sched_lock);
2409 return (td->td_xsig);
2413 * If the current process has received a signal (should be caught or cause
2414 * termination, should interrupt current syscall), return the signal number.
2415 * Stop signals with default action are processed immediately, then cleared;
2416 * they aren't returned. This is checked after each entry to the system for
2417 * a syscall or trap (though this can usually be done without calling issignal
2418 * by checking the pending signal masks in cursig.) The normal call
2421 * while (sig = cursig(curthread))
2430 sigset_t sigpending;
2431 int sig, prop, newsig;
2436 mtx_assert(&ps->ps_mtx, MA_OWNED);
2437 PROC_LOCK_ASSERT(p, MA_OWNED);
2439 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2441 sigpending = td->td_sigqueue.sq_signals;
2442 SIGSETNAND(sigpending, td->td_sigmask);
2444 if (p->p_flag & P_PPWAIT)
2445 SIG_STOPSIGMASK(sigpending);
2446 if (SIGISEMPTY(sigpending)) /* no signal to send */
2448 sig = sig_ffs(&sigpending);
2450 if (p->p_stops & S_SIG) {
2451 mtx_unlock(&ps->ps_mtx);
2452 stopevent(p, S_SIG, sig);
2453 mtx_lock(&ps->ps_mtx);
2457 * We should see pending but ignored signals
2458 * only if P_TRACED was on when they were posted.
2460 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2461 sigqueue_delete(&td->td_sigqueue, sig);
2462 if (td->td_pflags & TDP_SA)
2463 SIGADDSET(td->td_sigmask, sig);
2466 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2468 * If traced, always stop.
2470 mtx_unlock(&ps->ps_mtx);
2471 newsig = ptracestop(td, sig);
2472 mtx_lock(&ps->ps_mtx);
2474 if (td->td_pflags & TDP_SA)
2475 SIGADDSET(td->td_sigmask, sig);
2477 if (sig != newsig) {
2480 * XXX shrug off debugger, it causes siginfo to
2483 sigqueue_delete(&td->td_sigqueue, sig);
2486 * If parent wants us to take the signal,
2487 * then it will leave it in p->p_xstat;
2488 * otherwise we just look for signals again.
2495 * Put the new signal into td_sigqueue. If the
2496 * signal is being masked, look for other signals.
2498 SIGADDSET(td->td_sigqueue.sq_signals, sig);
2499 if (td->td_pflags & TDP_SA)
2500 SIGDELSET(td->td_sigmask, sig);
2501 if (SIGISMEMBER(td->td_sigmask, sig))
2507 * If the traced bit got turned off, go back up
2508 * to the top to rescan signals. This ensures
2509 * that p_sig* and p_sigact are consistent.
2511 if ((p->p_flag & P_TRACED) == 0)
2515 prop = sigprop(sig);
2518 * Decide whether the signal should be returned.
2519 * Return the signal's number, or fall through
2520 * to clear it from the pending mask.
2522 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2524 case (intptr_t)SIG_DFL:
2526 * Don't take default actions on system processes.
2528 if (p->p_pid <= 1) {
2531 * Are you sure you want to ignore SIGSEGV
2534 printf("Process (pid %lu) got signal %d\n",
2535 (u_long)p->p_pid, sig);
2537 break; /* == ignore */
2540 * If there is a pending stop signal to process
2541 * with default action, stop here,
2542 * then clear the signal. However,
2543 * if process is member of an orphaned
2544 * process group, ignore tty stop signals.
2546 if (prop & SA_STOP) {
2547 if (p->p_flag & P_TRACED ||
2548 (p->p_pgrp->pg_jobc == 0 &&
2550 break; /* == ignore */
2551 mtx_unlock(&ps->ps_mtx);
2552 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2553 &p->p_mtx.mtx_object, "Catching SIGSTOP");
2554 p->p_flag |= P_STOPPED_SIG;
2557 mtx_lock_spin(&sched_lock);
2558 FOREACH_THREAD_IN_PROC(p, td0) {
2559 if (TD_IS_SLEEPING(td0) &&
2560 (td0->td_flags & TDF_SINTR) &&
2561 !TD_IS_SUSPENDED(td0)) {
2562 thread_suspend_one(td0);
2563 } else if (td != td0) {
2564 td0->td_flags |= TDF_ASTPENDING;
2568 thread_suspend_one(td);
2571 mi_switch(SW_INVOL, NULL);
2572 mtx_unlock_spin(&sched_lock);
2575 mtx_lock(&ps->ps_mtx);
2577 } else if (prop & SA_IGNORE) {
2579 * Except for SIGCONT, shouldn't get here.
2580 * Default action is to ignore; drop it.
2582 break; /* == ignore */
2587 case (intptr_t)SIG_IGN:
2589 * Masking above should prevent us ever trying
2590 * to take action on an ignored signal other
2591 * than SIGCONT, unless process is traced.
2593 if ((prop & SA_CONT) == 0 &&
2594 (p->p_flag & P_TRACED) == 0)
2595 printf("issignal\n");
2596 break; /* == ignore */
2600 * This signal has an action, let
2601 * postsig() process it.
2605 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2614 thread_stopped(struct proc *p)
2616 struct proc *p1 = curthread->td_proc;
2620 PROC_LOCK_ASSERT(p, MA_OWNED);
2621 mtx_assert(&sched_lock, MA_OWNED);
2625 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2626 mtx_unlock_spin(&sched_lock);
2627 p->p_flag &= ~P_WAITED;
2628 PROC_LOCK(p->p_pptr);
2630 * Wake up parent sleeping in kern_wait(), also send
2631 * SIGCHLD to parent, but SIGCHLD does not guarantee
2632 * that parent will awake, because parent may masked
2635 p->p_pptr->p_flag |= P_STATCHILD;
2637 ps = p->p_pptr->p_sigacts;
2638 mtx_lock(&ps->ps_mtx);
2639 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2640 mtx_unlock(&ps->ps_mtx);
2641 psignal(p->p_pptr, SIGCHLD);
2643 mtx_unlock(&ps->ps_mtx);
2644 PROC_UNLOCK(p->p_pptr);
2645 mtx_lock_spin(&sched_lock);
2650 * Take the action for the specified signal
2651 * from the current set of pending signals.
2657 struct thread *td = curthread;
2658 register struct proc *p = td->td_proc;
2662 sigset_t returnmask;
2665 KASSERT(sig != 0, ("postsig"));
2667 PROC_LOCK_ASSERT(p, MA_OWNED);
2669 mtx_assert(&ps->ps_mtx, MA_OWNED);
2670 ksiginfo_init(&ksi);
2671 sigqueue_get(&td->td_sigqueue, sig, &ksi);
2672 ksi.ksi_signo = sig;
2674 action = ps->ps_sigact[_SIG_IDX(sig)];
2676 if (KTRPOINT(td, KTR_PSIG))
2677 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2678 &td->td_oldsigmask : &td->td_sigmask, 0);
2680 if (p->p_stops & S_SIG) {
2681 mtx_unlock(&ps->ps_mtx);
2682 stopevent(p, S_SIG, sig);
2683 mtx_lock(&ps->ps_mtx);
2686 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2688 * Default action, where the default is to kill
2689 * the process. (Other cases were ignored above.)
2691 mtx_unlock(&ps->ps_mtx);
2695 if (td->td_pflags & TDP_SA) {
2696 if (sig == SIGKILL) {
2697 mtx_unlock(&ps->ps_mtx);
2703 * If we get here, the signal must be caught.
2705 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2706 ("postsig action"));
2708 * Set the new mask value and also defer further
2709 * occurrences of this signal.
2711 * Special case: user has done a sigsuspend. Here the
2712 * current mask is not of interest, but rather the
2713 * mask from before the sigsuspend is what we want
2714 * restored after the signal processing is completed.
2716 if (td->td_pflags & TDP_OLDMASK) {
2717 returnmask = td->td_oldsigmask;
2718 td->td_pflags &= ~TDP_OLDMASK;
2720 returnmask = td->td_sigmask;
2722 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2723 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2724 SIGADDSET(td->td_sigmask, sig);
2726 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2728 * See kern_sigaction() for origin of this code.
2730 SIGDELSET(ps->ps_sigcatch, sig);
2731 if (sig != SIGCONT &&
2732 sigprop(sig) & SA_IGNORE)
2733 SIGADDSET(ps->ps_sigignore, sig);
2734 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2736 p->p_stats->p_ru.ru_nsignals++;
2737 if (p->p_sig != sig) {
2744 if (td->td_pflags & TDP_SA)
2745 thread_signal_add(curthread, &ksi);
2747 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2752 * Kill the current process for stated reason.
2760 PROC_LOCK_ASSERT(p, MA_OWNED);
2761 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2762 p, p->p_pid, p->p_comm);
2763 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2764 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2765 psignal(p, SIGKILL);
2769 * Force the current process to exit with the specified signal, dumping core
2770 * if appropriate. We bypass the normal tests for masked and caught signals,
2771 * allowing unrecoverable failures to terminate the process without changing
2772 * signal state. Mark the accounting record with the signal termination.
2773 * If dumping core, save the signal number for the debugger. Calls exit and
2783 struct proc *p = td->td_proc;
2785 PROC_LOCK_ASSERT(p, MA_OWNED);
2786 p->p_acflag |= AXSIG;
2788 * We must be single-threading to generate a core dump. This
2789 * ensures that the registers in the core file are up-to-date.
2790 * Also, the ELF dump handler assumes that the thread list doesn't
2791 * change out from under it.
2793 * XXX If another thread attempts to single-thread before us
2794 * (e.g. via fork()), we won't get a dump at all.
2796 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2799 * Log signals which would cause core dumps
2800 * (Log as LOG_INFO to appease those who don't want
2802 * XXX : Todo, as well as euid, write out ruid too
2803 * Note that coredump() drops proc lock.
2805 if (coredump(td) == 0)
2807 if (kern_logsigexit)
2809 "pid %d (%s), uid %d: exited on signal %d%s\n",
2810 p->p_pid, p->p_comm,
2811 td->td_ucred ? td->td_ucred->cr_uid : -1,
2813 sig & WCOREFLAG ? " (core dumped)" : "");
2816 exit1(td, W_EXITCODE(0, sig));
2820 static char corefilename[MAXPATHLEN] = {"%N.core"};
2821 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2822 sizeof(corefilename), "process corefile name format string");
2825 * expand_name(name, uid, pid)
2826 * Expand the name described in corefilename, using name, uid, and pid.
2827 * corefilename is a printf-like string, with three format specifiers:
2828 * %N name of process ("name")
2829 * %P process id (pid)
2831 * For example, "%N.core" is the default; they can be disabled completely
2832 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2833 * This is controlled by the sysctl variable kern.corefile (see above).
2837 expand_name(name, uid, pid)
2842 const char *format, *appendstr;
2844 char buf[11]; /* Buffer for pid/uid -- max 4B */
2847 format = corefilename;
2848 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2851 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2852 switch (format[i]) {
2853 case '%': /* Format character */
2855 switch (format[i]) {
2859 case 'N': /* process name */
2862 case 'P': /* process id */
2863 sprintf(buf, "%u", pid);
2866 case 'U': /* user id */
2867 sprintf(buf, "%u", uid);
2873 "Unknown format character %c in `%s'\n",
2876 l = strlen(appendstr);
2877 if ((n + l) >= MAXPATHLEN)
2879 memcpy(temp + n, appendstr, l);
2883 temp[n++] = format[i];
2886 if (format[i] != '\0')
2890 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2891 (long)pid, name, (u_long)uid);
2897 * Dump a process' core. The main routine does some
2898 * policy checking, and creates the name of the coredump;
2899 * then it passes on a vnode and a size limit to the process-specific
2900 * coredump routine if there is one; if there _is not_ one, it returns
2901 * ENOSYS; otherwise it returns the error from the process-specific routine.
2905 coredump(struct thread *td)
2907 struct proc *p = td->td_proc;
2908 register struct vnode *vp;
2909 register struct ucred *cred = td->td_ucred;
2911 struct nameidata nd;
2913 int error, error1, flags, locked;
2915 char *name; /* name of corefile */
2918 PROC_LOCK_ASSERT(p, MA_OWNED);
2919 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
2920 _STOPEVENT(p, S_CORE, 0);
2922 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2928 * Note that the bulk of limit checking is done after
2929 * the corefile is created. The exception is if the limit
2930 * for corefiles is 0, in which case we don't bother
2931 * creating the corefile at all. This layout means that
2932 * a corefile is truncated instead of not being created,
2933 * if it is larger than the limit.
2935 limit = (off_t)lim_cur(p, RLIMIT_CORE);
2942 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2947 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2948 flags = O_CREAT | FWRITE | O_NOFOLLOW;
2949 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2955 NDFREE(&nd, NDF_ONLY_PNBUF);
2958 /* Don't dump to non-regular files or files with links. */
2959 if (vp->v_type != VREG ||
2960 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2961 VOP_UNLOCK(vp, 0, td);
2966 VOP_UNLOCK(vp, 0, td);
2967 lf.l_whence = SEEK_SET;
2970 lf.l_type = F_WRLCK;
2971 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
2973 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2974 lf.l_type = F_UNLCK;
2976 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2977 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2979 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2986 if (set_core_nodump_flag)
2987 vattr.va_flags = UF_NODUMP;
2988 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2989 VOP_LEASE(vp, td, cred, LEASE_WRITE);
2990 VOP_SETATTR(vp, &vattr, cred, td);
2991 VOP_UNLOCK(vp, 0, td);
2993 p->p_acflag |= ACORE;
2996 error = p->p_sysent->sv_coredump ?
2997 p->p_sysent->sv_coredump(td, vp, limit) :
3001 lf.l_type = F_UNLCK;
3002 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3004 vn_finished_write(mp);
3006 error1 = vn_close(vp, FWRITE, cred, td);
3014 * Nonexistent system call-- signal process (may want to handle it).
3015 * Flag error in case process won't see signal immediately (blocked or ignored).
3017 #ifndef _SYS_SYSPROTO_H_
3029 struct nosys_args *args;
3031 struct proc *p = td->td_proc;
3040 * Send a SIGIO or SIGURG signal to a process or process group using
3041 * stored credentials rather than those of the current process.
3044 pgsigio(sigiop, sig, checkctty)
3045 struct sigio **sigiop;
3048 struct sigio *sigio;
3052 if (sigio == NULL) {
3056 if (sigio->sio_pgid > 0) {
3057 PROC_LOCK(sigio->sio_proc);
3058 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3059 psignal(sigio->sio_proc, sig);
3060 PROC_UNLOCK(sigio->sio_proc);
3061 } else if (sigio->sio_pgid < 0) {
3064 PGRP_LOCK(sigio->sio_pgrp);
3065 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3067 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3068 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3072 PGRP_UNLOCK(sigio->sio_pgrp);
3078 filt_sigattach(struct knote *kn)
3080 struct proc *p = curproc;
3082 kn->kn_ptr.p_proc = p;
3083 kn->kn_flags |= EV_CLEAR; /* automatically set */
3085 knlist_add(&p->p_klist, kn, 0);
3091 filt_sigdetach(struct knote *kn)
3093 struct proc *p = kn->kn_ptr.p_proc;
3095 knlist_remove(&p->p_klist, kn, 0);
3099 * signal knotes are shared with proc knotes, so we apply a mask to
3100 * the hint in order to differentiate them from process hints. This
3101 * could be avoided by using a signal-specific knote list, but probably
3102 * isn't worth the trouble.
3105 filt_signal(struct knote *kn, long hint)
3108 if (hint & NOTE_SIGNAL) {
3109 hint &= ~NOTE_SIGNAL;
3111 if (kn->kn_id == hint)
3114 return (kn->kn_data != 0);
3122 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3124 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3129 sigacts_free(struct sigacts *ps)
3132 mtx_lock(&ps->ps_mtx);
3134 if (ps->ps_refcnt == 0) {
3135 mtx_destroy(&ps->ps_mtx);
3136 free(ps, M_SUBPROC);
3138 mtx_unlock(&ps->ps_mtx);
3142 sigacts_hold(struct sigacts *ps)
3144 mtx_lock(&ps->ps_mtx);
3146 mtx_unlock(&ps->ps_mtx);
3151 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3154 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3155 mtx_lock(&src->ps_mtx);
3156 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3157 mtx_unlock(&src->ps_mtx);
3161 sigacts_shared(struct sigacts *ps)
3165 mtx_lock(&ps->ps_mtx);
3166 shared = ps->ps_refcnt > 1;
3167 mtx_unlock(&ps->ps_mtx);