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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/sysproto.h>
48 #include <sys/signalvar.h>
49 #include <sys/resourcevar.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
53 #include <sys/pioctl.h>
54 #include <sys/systm.h>
56 #include <sys/fcntl.h>
58 #include <sys/ktrace.h>
59 #include <sys/syslog.h>
61 #include <sys/sysent.h>
62 #include <sys/sysctl.h>
63 #include <sys/malloc.h>
65 #include <vm/vm_zone.h>
67 #include <machine/ipl.h>
68 #include <machine/cpu.h>
69 #include <machine/smp.h>
71 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
73 static int coredump __P((struct proc *));
74 static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act,
75 struct sigaction *oact, int old));
76 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
77 sigset_t *oset, int old));
78 static char *expand_name __P((const char *, uid_t, pid_t));
79 static int killpg1 __P((struct proc *cp, int sig, int pgid, int all));
80 static int sig_ffs __P((sigset_t *set));
81 static int sigprop __P((int sig));
82 static void stop __P((struct proc *));
84 static int kern_logsigexit = 1;
85 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
87 "Log processes quitting on abnormal signals to syslog(3)");
90 * Can process p, with pcred pc, send the signal sig to process q?
92 #define CANSIGNAL(p, q, sig) \
93 (!p_trespass(p, q) || \
94 ((sig) == SIGCONT && (q)->p_session == (p)->p_session))
97 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
99 #define CANSIGIO(ruid, uc, q) \
100 ((uc)->cr_uid == 0 || \
101 (ruid) == (q)->p_cred->p_ruid || \
102 (uc)->cr_uid == (q)->p_cred->p_ruid || \
103 (ruid) == (q)->p_ucred->cr_uid || \
104 (uc)->cr_uid == (q)->p_ucred->cr_uid)
107 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
108 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
110 static int do_coredump = 1;
111 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
112 &do_coredump, 0, "Enable/Disable coredumps");
115 * Signal properties and actions.
116 * The array below categorizes the signals and their default actions
117 * according to the following properties:
119 #define SA_KILL 0x01 /* terminates process by default */
120 #define SA_CORE 0x02 /* ditto and coredumps */
121 #define SA_STOP 0x04 /* suspend process */
122 #define SA_TTYSTOP 0x08 /* ditto, from tty */
123 #define SA_IGNORE 0x10 /* ignore by default */
124 #define SA_CONT 0x20 /* continue if suspended */
125 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
127 static int sigproptbl[NSIG] = {
128 SA_KILL, /* SIGHUP */
129 SA_KILL, /* SIGINT */
130 SA_KILL|SA_CORE, /* SIGQUIT */
131 SA_KILL|SA_CORE, /* SIGILL */
132 SA_KILL|SA_CORE, /* SIGTRAP */
133 SA_KILL|SA_CORE, /* SIGABRT */
134 SA_KILL|SA_CORE, /* SIGEMT */
135 SA_KILL|SA_CORE, /* SIGFPE */
136 SA_KILL, /* SIGKILL */
137 SA_KILL|SA_CORE, /* SIGBUS */
138 SA_KILL|SA_CORE, /* SIGSEGV */
139 SA_KILL|SA_CORE, /* SIGSYS */
140 SA_KILL, /* SIGPIPE */
141 SA_KILL, /* SIGALRM */
142 SA_KILL, /* SIGTERM */
143 SA_IGNORE, /* SIGURG */
144 SA_STOP, /* SIGSTOP */
145 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
146 SA_IGNORE|SA_CONT, /* SIGCONT */
147 SA_IGNORE, /* SIGCHLD */
148 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
149 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
150 SA_IGNORE, /* SIGIO */
151 SA_KILL, /* SIGXCPU */
152 SA_KILL, /* SIGXFSZ */
153 SA_KILL, /* SIGVTALRM */
154 SA_KILL, /* SIGPROF */
155 SA_IGNORE, /* SIGWINCH */
156 SA_IGNORE, /* SIGINFO */
157 SA_KILL, /* SIGUSR1 */
158 SA_KILL, /* SIGUSR2 */
165 if (sig > 0 && sig < NSIG)
166 return (sigproptbl[_SIG_IDX(sig)]);
171 sig_ffs(sigset_t *set)
175 for (i = 0; i < _SIG_WORDS; i++)
177 return (ffs(set->__bits[i]) + (i * 32));
187 do_sigaction(p, sig, act, oact, old)
190 struct sigaction *act, *oact;
193 register struct sigacts *ps = p->p_sigacts;
195 if (sig <= 0 || sig > _SIG_MAXSIG)
199 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
200 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
202 if (SIGISMEMBER(ps->ps_sigonstack, sig))
203 oact->sa_flags |= SA_ONSTACK;
204 if (!SIGISMEMBER(ps->ps_sigintr, sig))
205 oact->sa_flags |= SA_RESTART;
206 if (SIGISMEMBER(ps->ps_sigreset, sig))
207 oact->sa_flags |= SA_RESETHAND;
208 if (SIGISMEMBER(ps->ps_signodefer, sig))
209 oact->sa_flags |= SA_NODEFER;
210 if (SIGISMEMBER(ps->ps_siginfo, sig))
211 oact->sa_flags |= SA_SIGINFO;
212 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
213 oact->sa_flags |= SA_NOCLDSTOP;
214 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
215 oact->sa_flags |= SA_NOCLDWAIT;
218 if ((sig == SIGKILL || sig == SIGSTOP) &&
219 act->sa_handler != SIG_DFL)
223 * Change setting atomically.
227 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
228 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
229 if (act->sa_flags & SA_SIGINFO) {
230 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
231 SIGADDSET(ps->ps_siginfo, sig);
233 ps->ps_sigact[_SIG_IDX(sig)] =
234 (__sighandler_t *)act->sa_sigaction;
235 SIGDELSET(ps->ps_siginfo, sig);
237 if (!(act->sa_flags & SA_RESTART))
238 SIGADDSET(ps->ps_sigintr, sig);
240 SIGDELSET(ps->ps_sigintr, sig);
241 if (act->sa_flags & SA_ONSTACK)
242 SIGADDSET(ps->ps_sigonstack, sig);
244 SIGDELSET(ps->ps_sigonstack, sig);
245 if (act->sa_flags & SA_RESETHAND)
246 SIGADDSET(ps->ps_sigreset, sig);
248 SIGDELSET(ps->ps_sigreset, sig);
249 if (act->sa_flags & SA_NODEFER)
250 SIGADDSET(ps->ps_signodefer, sig);
252 SIGDELSET(ps->ps_signodefer, sig);
254 if (act->sa_flags & SA_USERTRAMP)
255 SIGADDSET(ps->ps_usertramp, sig);
257 SIGDELSET(ps->ps_usertramp, seg);
259 if (sig == SIGCHLD) {
260 if (act->sa_flags & SA_NOCLDSTOP)
261 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
263 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
264 if (act->sa_flags & SA_NOCLDWAIT) {
266 * Paranoia: since SA_NOCLDWAIT is implemented
267 * by reparenting the dying child to PID 1 (and
268 * trust it to reap the zombie), PID 1 itself
269 * is forbidden to set SA_NOCLDWAIT.
272 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
274 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
276 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
279 * Set bit in p_sigignore for signals that are set to SIG_IGN,
280 * and for signals set to SIG_DFL where the default is to
281 * ignore. However, don't put SIGCONT in p_sigignore, as we
282 * have to restart the process.
284 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
285 (sigprop(sig) & SA_IGNORE &&
286 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
287 /* never to be seen again */
288 SIGDELSET(p->p_siglist, sig);
290 /* easier in psignal */
291 SIGADDSET(p->p_sigignore, sig);
292 SIGDELSET(p->p_sigcatch, sig);
294 SIGDELSET(p->p_sigignore, sig);
295 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
296 SIGDELSET(p->p_sigcatch, sig);
298 SIGADDSET(p->p_sigcatch, sig);
300 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
301 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
302 SIGDELSET(ps->ps_osigset, sig);
304 SIGADDSET(ps->ps_osigset, sig);
311 #ifndef _SYS_SYSPROTO_H_
312 struct sigaction_args {
314 struct sigaction *act;
315 struct sigaction *oact;
322 register struct sigaction_args *uap;
324 struct sigaction act, oact;
325 register struct sigaction *actp, *oactp;
328 actp = (uap->act != NULL) ? &act : NULL;
329 oactp = (uap->oact != NULL) ? &oact : NULL;
331 error = copyin(uap->act, actp, sizeof(act));
335 error = do_sigaction(p, uap->sig, actp, oactp, 0);
336 if (oactp && !error) {
337 error = copyout(oactp, uap->oact, sizeof(oact));
342 #ifndef _SYS_SYSPROTO_H_
343 struct osigaction_args {
345 struct osigaction *nsa;
346 struct osigaction *osa;
353 register struct osigaction_args *uap;
355 struct osigaction sa;
356 struct sigaction nsa, osa;
357 register struct sigaction *nsap, *osap;
360 if (uap->signum <= 0 || uap->signum >= ONSIG)
362 nsap = (uap->nsa != NULL) ? &nsa : NULL;
363 osap = (uap->osa != NULL) ? &osa : NULL;
365 error = copyin(uap->nsa, &sa, sizeof(sa));
368 nsap->sa_handler = sa.sa_handler;
369 nsap->sa_flags = sa.sa_flags;
370 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
372 error = do_sigaction(p, uap->signum, nsap, osap, 1);
373 if (osap && !error) {
374 sa.sa_handler = osap->sa_handler;
375 sa.sa_flags = osap->sa_flags;
376 SIG2OSIG(osap->sa_mask, sa.sa_mask);
377 error = copyout(&sa, uap->osa, sizeof(sa));
383 * Initialize signal state for process 0;
384 * set to ignore signals that are ignored by default.
392 for (i = 1; i <= NSIG; i++)
393 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
394 SIGADDSET(p->p_sigignore, i);
398 * Reset signals for an exec of the specified process.
402 register struct proc *p;
404 register struct sigacts *ps = p->p_sigacts;
408 * Reset caught signals. Held signals remain held
409 * through p_sigmask (unless they were caught,
410 * and are now ignored by default).
412 while (SIGNOTEMPTY(p->p_sigcatch)) {
413 sig = sig_ffs(&p->p_sigcatch);
414 SIGDELSET(p->p_sigcatch, sig);
415 if (sigprop(sig) & SA_IGNORE) {
417 SIGADDSET(p->p_sigignore, sig);
418 SIGDELSET(p->p_siglist, sig);
420 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
423 * Reset stack state to the user stack.
424 * Clear set of signals caught on the signal stack.
426 p->p_sigstk.ss_flags = SS_DISABLE;
427 p->p_sigstk.ss_size = 0;
428 p->p_sigstk.ss_sp = 0;
430 * Reset no zombies if child dies flag as Solaris does.
432 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
436 * Manipulate signal mask.
437 * Note that we receive new mask, not pointer,
438 * and return old mask as return value;
439 * the library stub does the rest.
442 do_sigprocmask(p, how, set, oset, old)
445 sigset_t *set, *oset;
451 *oset = p->p_sigmask;
459 SIGSETOR(p->p_sigmask, *set);
462 SIGSETNAND(p->p_sigmask, *set);
467 SIGSETLO(p->p_sigmask, *set);
480 #ifndef _SYS_SYSPROTO_H_
481 struct sigprocmask_args {
489 register struct proc *p;
490 struct sigprocmask_args *uap;
493 sigset_t *setp, *osetp;
496 setp = (uap->set != NULL) ? &set : NULL;
497 osetp = (uap->oset != NULL) ? &oset : NULL;
499 error = copyin(uap->set, setp, sizeof(set));
503 error = do_sigprocmask(p, uap->how, setp, osetp, 0);
504 if (osetp && !error) {
505 error = copyout(osetp, uap->oset, sizeof(oset));
510 #ifndef _SYS_SYSPROTO_H_
511 struct osigprocmask_args {
518 register struct proc *p;
519 struct osigprocmask_args *uap;
524 OSIG2SIG(uap->mask, set);
525 error = do_sigprocmask(p, uap->how, &set, &oset, 1);
526 SIG2OSIG(oset, p->p_retval[0]);
530 #ifndef _SYS_SYSPROTO_H_
531 struct sigpending_args {
539 struct sigpending_args *uap;
542 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
545 #ifndef _SYS_SYSPROTO_H_
546 struct osigpending_args {
554 struct osigpending_args *uap;
557 SIG2OSIG(p->p_siglist, p->p_retval[0]);
561 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
563 * Generalized interface signal handler, 4.3-compatible.
565 #ifndef _SYS_SYSPROTO_H_
566 struct osigvec_args {
576 register struct osigvec_args *uap;
579 struct sigaction nsa, osa;
580 register struct sigaction *nsap, *osap;
583 if (uap->signum <= 0 || uap->signum >= ONSIG)
585 nsap = (uap->nsv != NULL) ? &nsa : NULL;
586 osap = (uap->osv != NULL) ? &osa : NULL;
588 error = copyin(uap->nsv, &vec, sizeof(vec));
591 nsap->sa_handler = vec.sv_handler;
592 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
593 nsap->sa_flags = vec.sv_flags;
594 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
596 nsap->sa_flags |= SA_USERTRAMP;
599 error = do_sigaction(p, uap->signum, nsap, osap, 1);
600 if (osap && !error) {
601 vec.sv_handler = osap->sa_handler;
602 SIG2OSIG(osap->sa_mask, vec.sv_mask);
603 vec.sv_flags = osap->sa_flags;
604 vec.sv_flags &= ~SA_NOCLDWAIT;
605 vec.sv_flags ^= SA_RESTART;
607 vec.sv_flags &= ~SA_NOCLDSTOP;
609 error = copyout(&vec, uap->osv, sizeof(vec));
614 #ifndef _SYS_SYSPROTO_H_
615 struct osigblock_args {
621 register struct proc *p;
622 struct osigblock_args *uap;
626 OSIG2SIG(uap->mask, set);
629 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
630 SIGSETOR(p->p_sigmask, set);
635 #ifndef _SYS_SYSPROTO_H_
636 struct osigsetmask_args {
643 struct osigsetmask_args *uap;
647 OSIG2SIG(uap->mask, set);
650 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
651 SIGSETLO(p->p_sigmask, set);
655 #endif /* COMPAT_43 || COMPAT_SUNOS */
658 * Suspend process until signal, providing mask to be set
659 * in the meantime. Note nonstandard calling convention:
660 * libc stub passes mask, not pointer, to save a copyin.
662 #ifndef _SYS_SYSPROTO_H_
663 struct sigsuspend_args {
664 const sigset_t *sigmask;
670 register struct proc *p;
671 struct sigsuspend_args *uap;
674 register struct sigacts *ps = p->p_sigacts;
677 error = copyin(uap->sigmask, &mask, sizeof(mask));
682 * When returning from sigsuspend, we want
683 * the old mask to be restored after the
684 * signal handler has finished. Thus, we
685 * save it here and mark the sigacts structure
688 p->p_oldsigmask = p->p_sigmask;
689 p->p_flag |= P_OLDMASK;
693 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
695 /* always return EINTR rather than ERESTART... */
699 #ifndef _SYS_SYSPROTO_H_
700 struct osigsuspend_args {
707 register struct proc *p;
708 struct osigsuspend_args *uap;
711 register struct sigacts *ps = p->p_sigacts;
713 p->p_oldsigmask = p->p_sigmask;
714 p->p_flag |= P_OLDMASK;
715 OSIG2SIG(uap->mask, mask);
717 SIGSETLO(p->p_sigmask, mask);
718 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
720 /* always return EINTR rather than ERESTART... */
724 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
725 #ifndef _SYS_SYSPROTO_H_
726 struct osigstack_args {
727 struct sigstack *nss;
728 struct sigstack *oss;
735 register struct osigstack_args *uap;
740 ss.ss_sp = p->p_sigstk.ss_sp;
741 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
742 if (uap->oss && (error = copyout(&ss, uap->oss,
743 sizeof(struct sigstack))))
745 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
746 p->p_sigstk.ss_sp = ss.ss_sp;
747 p->p_sigstk.ss_size = 0;
748 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
749 p->p_flag |= P_ALTSTACK;
753 #endif /* COMPAT_43 || COMPAT_SUNOS */
755 #ifndef _SYS_SYSPROTO_H_
756 struct sigaltstack_args {
765 register struct sigaltstack_args *uap;
770 if ((p->p_flag & P_ALTSTACK) == 0)
771 p->p_sigstk.ss_flags |= SS_DISABLE;
772 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
777 if ((error = copyin(uap->ss, &ss, sizeof(ss))))
779 if (ss.ss_flags & SS_DISABLE) {
780 if (p->p_sigstk.ss_flags & SS_ONSTACK)
782 p->p_flag &= ~P_ALTSTACK;
783 p->p_sigstk.ss_flags = ss.ss_flags;
786 if (ss.ss_size < MINSIGSTKSZ)
788 p->p_flag |= P_ALTSTACK;
794 * Common code for kill process group/broadcast kill.
795 * cp is calling process.
798 killpg1(cp, sig, pgid, all)
799 register struct proc *cp;
802 register struct proc *p;
810 LIST_FOREACH(p, &allproc, p_list) {
811 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
812 p == cp || !CANSIGNAL(cp, p, sig))
821 * zero pgid means send to my process group.
829 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
830 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
831 p->p_stat == SZOMB ||
832 !CANSIGNAL(cp, p, sig))
839 return (nfound ? 0 : ESRCH);
842 #ifndef _SYS_SYSPROTO_H_
851 register struct proc *cp;
852 register struct kill_args *uap;
854 register struct proc *p;
856 if ((u_int)uap->signum > _SIG_MAXSIG)
859 /* kill single process */
860 if ((p = pfind(uap->pid)) == NULL)
862 if (!CANSIGNAL(cp, p, uap->signum))
865 psignal(p, uap->signum);
869 case -1: /* broadcast signal */
870 return (killpg1(cp, uap->signum, 0, 1));
871 case 0: /* signal own process group */
872 return (killpg1(cp, uap->signum, 0, 0));
873 default: /* negative explicit process group */
874 return (killpg1(cp, uap->signum, -uap->pid, 0));
879 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
880 #ifndef _SYS_SYSPROTO_H_
881 struct okillpg_args {
890 register struct okillpg_args *uap;
893 if ((u_int)uap->signum > _SIG_MAXSIG)
895 return (killpg1(p, uap->signum, uap->pgid, 0));
897 #endif /* COMPAT_43 || COMPAT_SUNOS */
900 * Send a signal to a process group.
908 if (pgid && (pgrp = pgfind(pgid)))
909 pgsignal(pgrp, sig, 0);
913 * Send a signal to a process group. If checktty is 1,
914 * limit to members which have a controlling terminal.
917 pgsignal(pgrp, sig, checkctty)
921 register struct proc *p;
924 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
925 if (checkctty == 0 || p->p_flag & P_CONTROLT)
930 * Send a signal caused by a trap to the current process.
931 * If it will be caught immediately, deliver it with correct code.
932 * Otherwise, post it normally.
935 trapsignal(p, sig, code)
940 register struct sigacts *ps = p->p_sigacts;
942 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
943 SIGISMEMBER(p->p_sigmask, sig)) {
944 p->p_stats->p_ru.ru_nsignals++;
946 if (KTRPOINT(p, KTR_PSIG))
947 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
948 &p->p_sigmask, code);
950 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
951 &p->p_sigmask, code);
952 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
953 if (!SIGISMEMBER(ps->ps_signodefer, sig))
954 SIGADDSET(p->p_sigmask, sig);
955 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
957 * See do_sigaction() for origin of this code.
959 SIGDELSET(p->p_sigcatch, sig);
960 if (sig != SIGCONT &&
961 sigprop(sig) & SA_IGNORE)
962 SIGADDSET(p->p_sigignore, sig);
963 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
966 p->p_code = code; /* XXX for core dump/debugger */
967 p->p_sig = sig; /* XXX to verify code */
973 * Send the signal to the process. If the signal has an action, the action
974 * is usually performed by the target process rather than the caller; we add
975 * the signal to the set of pending signals for the process.
978 * o When a stop signal is sent to a sleeping process that takes the
979 * default action, the process is stopped without awakening it.
980 * o SIGCONT restarts stopped processes (or puts them back to sleep)
981 * regardless of the signal action (eg, blocked or ignored).
983 * Other ignored signals are discarded immediately.
987 register struct proc *p;
990 register int s, prop;
991 register sig_t action;
993 if (sig > _SIG_MAXSIG || sig <= 0) {
994 printf("psignal: signal %d\n", sig);
995 panic("psignal signal number");
1001 * If proc is traced, always give parent a chance;
1002 * if signal event is tracked by procfs, give *that*
1003 * a chance, as well.
1005 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1009 * If the signal is being ignored,
1010 * then we forget about it immediately.
1011 * (Note: we don't set SIGCONT in p_sigignore,
1012 * and if it is set to SIG_IGN,
1013 * action will be SIG_DFL here.)
1015 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1017 if (SIGISMEMBER(p->p_sigmask, sig))
1019 else if (SIGISMEMBER(p->p_sigcatch, sig))
1025 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1026 (p->p_flag & P_TRACED) == 0)
1030 SIG_STOPSIGMASK(p->p_siglist);
1032 if (prop & SA_STOP) {
1034 * If sending a tty stop signal to a member of an orphaned
1035 * process group, discard the signal here if the action
1036 * is default; don't stop the process below if sleeping,
1037 * and don't clear any pending SIGCONT.
1039 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1042 SIG_CONTSIGMASK(p->p_siglist);
1044 SIGADDSET(p->p_siglist, sig);
1047 * Defer further processing for signals which are held,
1048 * except that stopped processes must be continued by SIGCONT.
1050 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1053 switch (p->p_stat) {
1057 * If process is sleeping uninterruptibly
1058 * we can't interrupt the sleep... the signal will
1059 * be noticed when the process returns through
1060 * trap() or syscall().
1062 if ((p->p_flag & P_SINTR) == 0)
1065 * Process is sleeping and traced... make it runnable
1066 * so it can discover the signal in issignal() and stop
1069 if (p->p_flag & P_TRACED)
1072 * If SIGCONT is default (or ignored) and process is
1073 * asleep, we are finished; the process should not
1076 if ((prop & SA_CONT) && action == SIG_DFL) {
1077 SIGDELSET(p->p_siglist, sig);
1081 * When a sleeping process receives a stop
1082 * signal, process immediately if possible.
1083 * All other (caught or default) signals
1084 * cause the process to run.
1086 if (prop & SA_STOP) {
1087 if (action != SIG_DFL)
1090 * If a child holding parent blocked,
1091 * stopping could cause deadlock.
1093 if (p->p_flag & P_PPWAIT)
1095 SIGDELSET(p->p_siglist, sig);
1097 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1098 psignal(p->p_pptr, SIGCHLD);
1107 * If traced process is already stopped,
1108 * then no further action is necessary.
1110 if (p->p_flag & P_TRACED)
1114 * Kill signal always sets processes running.
1119 if (prop & SA_CONT) {
1121 * If SIGCONT is default (or ignored), we continue the
1122 * process but don't leave the signal in p_siglist, as
1123 * it has no further action. If SIGCONT is held, we
1124 * continue the process and leave the signal in
1125 * p_siglist. If the process catches SIGCONT, let it
1126 * handle the signal itself. If it isn't waiting on
1127 * an event, then it goes back to run state.
1128 * Otherwise, process goes back to sleep state.
1130 if (action == SIG_DFL)
1131 SIGDELSET(p->p_siglist, sig);
1132 if (action == SIG_CATCH)
1134 if (p->p_wchan == 0)
1140 if (prop & SA_STOP) {
1142 * Already stopped, don't need to stop again.
1143 * (If we did the shell could get confused.)
1145 SIGDELSET(p->p_siglist, sig);
1150 * If process is sleeping interruptibly, then simulate a
1151 * wakeup so that when it is continued, it will be made
1152 * runnable and can look at the signal. But don't make
1153 * the process runnable, leave it stopped.
1155 if (p->p_wchan && p->p_flag & P_SINTR)
1161 * SRUN, SIDL, SZOMB do nothing with the signal,
1162 * other than kicking ourselves if we are running.
1163 * It will either never be noticed, or noticed very soon.
1168 else if (p->p_stat == SRUN)
1177 * Raise priority to at least PUSER.
1179 if (p->p_priority > PUSER)
1180 p->p_priority = PUSER;
1188 * If the current process has received a signal (should be caught or cause
1189 * termination, should interrupt current syscall), return the signal number.
1190 * Stop signals with default action are processed immediately, then cleared;
1191 * they aren't returned. This is checked after each entry to the system for
1192 * a syscall or trap (though this can usually be done without calling issignal
1193 * by checking the pending signal masks in the CURSIG macro.) The normal call
1196 * while (sig = CURSIG(curproc))
1201 register struct proc *p;
1204 register int sig, prop;
1207 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1209 mask = p->p_siglist;
1210 SIGSETNAND(mask, p->p_sigmask);
1211 if (p->p_flag & P_PPWAIT)
1212 SIG_STOPSIGMASK(mask);
1213 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1215 sig = sig_ffs(&mask);
1216 prop = sigprop(sig);
1218 STOPEVENT(p, S_SIG, sig);
1221 * We should see pending but ignored signals
1222 * only if P_TRACED was on when they were posted.
1224 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1225 SIGDELSET(p->p_siglist, sig);
1228 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1230 * If traced, always stop, and stay
1231 * stopped until released by the parent.
1234 psignal(p->p_pptr, SIGCHLD);
1238 } while (!trace_req(p)
1239 && p->p_flag & P_TRACED);
1242 * If the traced bit got turned off, go back up
1243 * to the top to rescan signals. This ensures
1244 * that p_sig* and ps_sigact are consistent.
1246 if ((p->p_flag & P_TRACED) == 0)
1250 * If parent wants us to take the signal,
1251 * then it will leave it in p->p_xstat;
1252 * otherwise we just look for signals again.
1254 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1260 * Put the new signal into p_siglist. If the
1261 * signal is being masked, look for other signals.
1263 SIGADDSET(p->p_siglist, sig);
1264 if (SIGISMEMBER(p->p_sigmask, sig))
1269 * Decide whether the signal should be returned.
1270 * Return the signal's number, or fall through
1271 * to clear it from the pending mask.
1273 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1277 * Don't take default actions on system processes.
1279 if (p->p_pid <= 1) {
1282 * Are you sure you want to ignore SIGSEGV
1285 printf("Process (pid %lu) got signal %d\n",
1286 (u_long)p->p_pid, sig);
1288 break; /* == ignore */
1291 * If there is a pending stop signal to process
1292 * with default action, stop here,
1293 * then clear the signal. However,
1294 * if process is member of an orphaned
1295 * process group, ignore tty stop signals.
1297 if (prop & SA_STOP) {
1298 if (p->p_flag & P_TRACED ||
1299 (p->p_pgrp->pg_jobc == 0 &&
1301 break; /* == ignore */
1304 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1305 psignal(p->p_pptr, SIGCHLD);
1308 } else if (prop & SA_IGNORE) {
1310 * Except for SIGCONT, shouldn't get here.
1311 * Default action is to ignore; drop it.
1313 break; /* == ignore */
1320 * Masking above should prevent us ever trying
1321 * to take action on an ignored signal other
1322 * than SIGCONT, unless process is traced.
1324 if ((prop & SA_CONT) == 0 &&
1325 (p->p_flag & P_TRACED) == 0)
1326 printf("issignal\n");
1327 break; /* == ignore */
1331 * This signal has an action, let
1332 * postsig() process it.
1336 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1342 * Put the argument process into the stopped state and notify the parent
1343 * via wakeup. Signals are handled elsewhere. The process must not be
1348 register struct proc *p;
1352 p->p_flag &= ~P_WAITED;
1353 wakeup((caddr_t)p->p_pptr);
1357 * Take the action for the specified signal
1358 * from the current set of pending signals.
1364 register struct proc *p = curproc;
1365 struct sigacts *ps = p->p_sigacts;
1367 sigset_t returnmask;
1370 KASSERT(sig != 0, ("postsig"));
1372 SIGDELSET(p->p_siglist, sig);
1373 action = ps->ps_sigact[_SIG_IDX(sig)];
1375 if (KTRPOINT(p, KTR_PSIG))
1376 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1377 &p->p_oldsigmask : &p->p_sigmask, 0);
1379 STOPEVENT(p, S_SIG, sig);
1381 if (action == SIG_DFL) {
1383 * Default action, where the default is to kill
1384 * the process. (Other cases were ignored above.)
1390 * If we get here, the signal must be caught.
1392 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1393 ("postsig action"));
1395 * Set the new mask value and also defer further
1396 * occurrences of this signal.
1398 * Special case: user has done a sigsuspend. Here the
1399 * current mask is not of interest, but rather the
1400 * mask from before the sigsuspend is what we want
1401 * restored after the signal processing is completed.
1404 if (p->p_flag & P_OLDMASK) {
1405 returnmask = p->p_oldsigmask;
1406 p->p_flag &= ~P_OLDMASK;
1408 returnmask = p->p_sigmask;
1410 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1411 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1412 SIGADDSET(p->p_sigmask, sig);
1414 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1416 * See do_sigaction() for origin of this code.
1418 SIGDELSET(p->p_sigcatch, sig);
1419 if (sig != SIGCONT &&
1420 sigprop(sig) & SA_IGNORE)
1421 SIGADDSET(p->p_sigignore, sig);
1422 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1425 p->p_stats->p_ru.ru_nsignals++;
1426 if (p->p_sig != sig) {
1433 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1438 * Kill the current process for stated reason.
1445 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1446 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1447 psignal(p, SIGKILL);
1451 * Force the current process to exit with the specified signal, dumping core
1452 * if appropriate. We bypass the normal tests for masked and caught signals,
1453 * allowing unrecoverable failures to terminate the process without changing
1454 * signal state. Mark the accounting record with the signal termination.
1455 * If dumping core, save the signal number for the debugger. Calls exit and
1460 register struct proc *p;
1464 p->p_acflag |= AXSIG;
1465 if (sigprop(sig) & SA_CORE) {
1468 * Log signals which would cause core dumps
1469 * (Log as LOG_INFO to appease those who don't want
1471 * XXX : Todo, as well as euid, write out ruid too
1473 if (coredump(p) == 0)
1475 if (kern_logsigexit)
1477 "pid %d (%s), uid %d: exited on signal %d%s\n",
1478 p->p_pid, p->p_comm,
1479 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1481 sig & WCOREFLAG ? " (core dumped)" : "");
1483 exit1(p, W_EXITCODE(0, sig));
1487 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1488 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1489 sizeof(corefilename), "process corefile name format string");
1492 * expand_name(name, uid, pid)
1493 * Expand the name described in corefilename, using name, uid, and pid.
1494 * corefilename is a printf-like string, with three format specifiers:
1495 * %N name of process ("name")
1496 * %P process id (pid)
1498 * For example, "%N.core" is the default; they can be disabled completely
1499 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1500 * This is controlled by the sysctl variable kern.corefile (see above).
1504 expand_name(name, uid, pid)
1505 const char *name; uid_t uid; pid_t pid; {
1507 char buf[11]; /* Buffer for pid/uid -- max 4B */
1509 char *format = corefilename;
1512 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1515 namelen = strlen(name);
1516 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1518 switch (format[i]) {
1519 case '%': /* Format character */
1521 switch (format[i]) {
1525 case 'N': /* process name */
1526 if ((n + namelen) > MAXPATHLEN) {
1527 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1528 pid, name, uid, temp, name);
1532 memcpy(temp+n, name, namelen);
1535 case 'P': /* process id */
1536 l = sprintf(buf, "%u", pid);
1537 if ((n + l) > MAXPATHLEN) {
1538 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1539 pid, name, uid, temp, name);
1543 memcpy(temp+n, buf, l);
1546 case 'U': /* user id */
1547 l = sprintf(buf, "%u", uid);
1548 if ((n + l) > MAXPATHLEN) {
1549 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1550 pid, name, uid, temp, name);
1554 memcpy(temp+n, buf, l);
1558 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1562 temp[n++] = format[i];
1570 * Dump a process' core. The main routine does some
1571 * policy checking, and creates the name of the coredump;
1572 * then it passes on a vnode and a size limit to the process-specific
1573 * coredump routine if there is one; if there _is not_ one, it returns
1574 * ENOSYS; otherwise it returns the error from the process-specific routine.
1579 register struct proc *p;
1581 register struct vnode *vp;
1582 register struct ucred *cred = p->p_ucred;
1583 struct nameidata nd;
1586 char *name; /* name of corefile */
1589 STOPEVENT(p, S_CORE, 0);
1591 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1595 * Note that the bulk of limit checking is done after
1596 * the corefile is created. The exception is if the limit
1597 * for corefiles is 0, in which case we don't bother
1598 * creating the corefile at all. This layout means that
1599 * a corefile is truncated instead of not being created,
1600 * if it is larger than the limit.
1602 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1606 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1607 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1608 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1612 NDFREE(&nd, NDF_ONLY_PNBUF);
1615 /* Don't dump to non-regular files or files with links. */
1616 if (vp->v_type != VREG ||
1617 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1623 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1624 VOP_SETATTR(vp, &vattr, cred, p);
1625 p->p_acflag |= ACORE;
1627 error = p->p_sysent->sv_coredump ?
1628 p->p_sysent->sv_coredump(p, vp, limit) :
1632 VOP_UNLOCK(vp, 0, p);
1633 error1 = vn_close(vp, FWRITE, cred, p);
1640 * Nonexistent system call-- signal process (may want to handle it).
1641 * Flag error in case process won't see signal immediately (blocked or ignored).
1643 #ifndef _SYS_SYSPROTO_H_
1652 struct nosys_args *args;
1660 * Send a signal to a SIGIO or SIGURG to a process or process group using
1661 * stored credentials rather than those of the current process.
1664 pgsigio(sigio, sig, checkctty)
1665 struct sigio *sigio;
1671 if (sigio->sio_pgid > 0) {
1672 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1674 psignal(sigio->sio_proc, sig);
1675 } else if (sigio->sio_pgid < 0) {
1678 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1679 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1680 (checkctty == 0 || (p->p_flag & P_CONTROLT)))