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/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/event.h>
54 #include <sys/pioctl.h>
56 #include <sys/fcntl.h>
57 #include <sys/condvar.h>
59 #include <sys/mutex.h>
62 #include <sys/ktrace.h>
63 #include <sys/resourcevar.h>
67 #include <sys/syslog.h>
68 #include <sys/sysent.h>
69 #include <sys/sysctl.h>
70 #include <sys/malloc.h>
71 #include <sys/unistd.h>
73 #include <machine/cpu.h>
75 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
77 static int coredump __P((struct thread *));
78 static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act,
79 struct sigaction *oact, int old));
80 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
81 sigset_t *oset, int old));
82 static char *expand_name __P((const char *, uid_t, pid_t));
83 static int killpg1 __P((struct proc *cp, int sig, int pgid, int all));
84 static int sig_ffs __P((sigset_t *set));
85 static int sigprop __P((int sig));
86 static void stop __P((struct proc *));
88 static int filt_sigattach(struct knote *kn);
89 static void filt_sigdetach(struct knote *kn);
90 static int filt_signal(struct knote *kn, long hint);
92 struct filterops sig_filtops =
93 { 0, filt_sigattach, filt_sigdetach, filt_signal };
95 static int kern_logsigexit = 1;
96 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
98 "Log processes quitting on abnormal signals to syslog(3)");
101 * Policy -- Can ucred cr1 send SIGIO to process cr2?
102 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
103 * in the right situations.
105 #define CANSIGIO(cr1, cr2) \
106 ((cr1)->cr_uid == 0 || \
107 (cr1)->cr_ruid == (cr2)->cr_ruid || \
108 (cr1)->cr_uid == (cr2)->cr_ruid || \
109 (cr1)->cr_ruid == (cr2)->cr_uid || \
110 (cr1)->cr_uid == (cr2)->cr_uid)
113 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
114 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
116 static int do_coredump = 1;
117 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
118 &do_coredump, 0, "Enable/Disable coredumps");
121 * Signal properties and actions.
122 * The array below categorizes the signals and their default actions
123 * according to the following properties:
125 #define SA_KILL 0x01 /* terminates process by default */
126 #define SA_CORE 0x02 /* ditto and coredumps */
127 #define SA_STOP 0x04 /* suspend process */
128 #define SA_TTYSTOP 0x08 /* ditto, from tty */
129 #define SA_IGNORE 0x10 /* ignore by default */
130 #define SA_CONT 0x20 /* continue if suspended */
131 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
133 static int sigproptbl[NSIG] = {
134 SA_KILL, /* SIGHUP */
135 SA_KILL, /* SIGINT */
136 SA_KILL|SA_CORE, /* SIGQUIT */
137 SA_KILL|SA_CORE, /* SIGILL */
138 SA_KILL|SA_CORE, /* SIGTRAP */
139 SA_KILL|SA_CORE, /* SIGABRT */
140 SA_KILL|SA_CORE, /* SIGEMT */
141 SA_KILL|SA_CORE, /* SIGFPE */
142 SA_KILL, /* SIGKILL */
143 SA_KILL|SA_CORE, /* SIGBUS */
144 SA_KILL|SA_CORE, /* SIGSEGV */
145 SA_KILL|SA_CORE, /* SIGSYS */
146 SA_KILL, /* SIGPIPE */
147 SA_KILL, /* SIGALRM */
148 SA_KILL, /* SIGTERM */
149 SA_IGNORE, /* SIGURG */
150 SA_STOP, /* SIGSTOP */
151 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
152 SA_IGNORE|SA_CONT, /* SIGCONT */
153 SA_IGNORE, /* SIGCHLD */
154 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
155 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
156 SA_IGNORE, /* SIGIO */
157 SA_KILL, /* SIGXCPU */
158 SA_KILL, /* SIGXFSZ */
159 SA_KILL, /* SIGVTALRM */
160 SA_KILL, /* SIGPROF */
161 SA_IGNORE, /* SIGWINCH */
162 SA_IGNORE, /* SIGINFO */
163 SA_KILL, /* SIGUSR1 */
164 SA_KILL, /* SIGUSR2 */
168 * Determine signal that should be delivered to process p, the current
169 * process, 0 if none. If there is a pending stop signal with default
170 * action, the process stops in issignal().
175 CURSIG(struct proc *p)
179 PROC_LOCK_ASSERT(p, MA_OWNED);
180 if (SIGISEMPTY(p->p_siglist))
182 tmpset = p->p_siglist;
183 SIGSETNAND(tmpset, p->p_sigmask);
184 if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
186 return (issignal(p));
193 if (sig > 0 && sig < NSIG)
194 return (sigproptbl[_SIG_IDX(sig)]);
199 sig_ffs(sigset_t *set)
203 for (i = 0; i < _SIG_WORDS; i++)
205 return (ffs(set->__bits[i]) + (i * 32));
215 do_sigaction(p, sig, act, oact, old)
218 struct sigaction *act, *oact;
221 register struct sigacts *ps;
223 if (!_SIG_VALID(sig))
229 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
230 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
232 if (SIGISMEMBER(ps->ps_sigonstack, sig))
233 oact->sa_flags |= SA_ONSTACK;
234 if (!SIGISMEMBER(ps->ps_sigintr, sig))
235 oact->sa_flags |= SA_RESTART;
236 if (SIGISMEMBER(ps->ps_sigreset, sig))
237 oact->sa_flags |= SA_RESETHAND;
238 if (SIGISMEMBER(ps->ps_signodefer, sig))
239 oact->sa_flags |= SA_NODEFER;
240 if (SIGISMEMBER(ps->ps_siginfo, sig))
241 oact->sa_flags |= SA_SIGINFO;
242 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
243 oact->sa_flags |= SA_NOCLDSTOP;
244 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
245 oact->sa_flags |= SA_NOCLDWAIT;
248 if ((sig == SIGKILL || sig == SIGSTOP) &&
249 act->sa_handler != SIG_DFL) {
255 * Change setting atomically.
258 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
259 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
260 if (act->sa_flags & SA_SIGINFO) {
261 ps->ps_sigact[_SIG_IDX(sig)] =
262 (__sighandler_t *)act->sa_sigaction;
263 SIGADDSET(ps->ps_siginfo, sig);
265 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
266 SIGDELSET(ps->ps_siginfo, sig);
268 if (!(act->sa_flags & SA_RESTART))
269 SIGADDSET(ps->ps_sigintr, sig);
271 SIGDELSET(ps->ps_sigintr, sig);
272 if (act->sa_flags & SA_ONSTACK)
273 SIGADDSET(ps->ps_sigonstack, sig);
275 SIGDELSET(ps->ps_sigonstack, sig);
276 if (act->sa_flags & SA_RESETHAND)
277 SIGADDSET(ps->ps_sigreset, sig);
279 SIGDELSET(ps->ps_sigreset, sig);
280 if (act->sa_flags & SA_NODEFER)
281 SIGADDSET(ps->ps_signodefer, sig);
283 SIGDELSET(ps->ps_signodefer, sig);
285 if (act->sa_flags & SA_USERTRAMP)
286 SIGADDSET(ps->ps_usertramp, sig);
288 SIGDELSET(ps->ps_usertramp, sig);
290 if (sig == SIGCHLD) {
291 if (act->sa_flags & SA_NOCLDSTOP)
292 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
294 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
295 if ((act->sa_flags & SA_NOCLDWAIT) ||
296 ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) {
298 * Paranoia: since SA_NOCLDWAIT is implemented
299 * by reparenting the dying child to PID 1 (and
300 * trust it to reap the zombie), PID 1 itself
301 * is forbidden to set SA_NOCLDWAIT.
304 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
306 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
308 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
311 * Set bit in p_sigignore for signals that are set to SIG_IGN,
312 * and for signals set to SIG_DFL where the default is to
313 * ignore. However, don't put SIGCONT in p_sigignore, as we
314 * have to restart the process.
316 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
317 (sigprop(sig) & SA_IGNORE &&
318 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
319 /* never to be seen again */
320 SIGDELSET(p->p_siglist, sig);
322 /* easier in psignal */
323 SIGADDSET(p->p_sigignore, sig);
324 SIGDELSET(p->p_sigcatch, sig);
326 SIGDELSET(p->p_sigignore, sig);
327 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
328 SIGDELSET(p->p_sigcatch, sig);
330 SIGADDSET(p->p_sigcatch, sig);
333 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
334 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
335 SIGDELSET(ps->ps_osigset, sig);
337 SIGADDSET(ps->ps_osigset, sig);
344 #ifndef _SYS_SYSPROTO_H_
345 struct sigaction_args {
347 struct sigaction *act;
348 struct sigaction *oact;
358 register struct sigaction_args *uap;
360 struct proc *p = td->td_proc;
361 struct sigaction act, oact;
362 register struct sigaction *actp, *oactp;
367 actp = (uap->act != NULL) ? &act : NULL;
368 oactp = (uap->oact != NULL) ? &oact : NULL;
370 error = copyin(uap->act, actp, sizeof(act));
374 error = do_sigaction(p, uap->sig, actp, oactp, 0);
375 if (oactp && !error) {
376 error = copyout(oactp, uap->oact, sizeof(oact));
383 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
384 #ifndef _SYS_SYSPROTO_H_
385 struct osigaction_args {
387 struct osigaction *nsa;
388 struct osigaction *osa;
398 register struct osigaction_args *uap;
400 struct proc *p = td->td_proc;
401 struct osigaction sa;
402 struct sigaction nsa, osa;
403 register struct sigaction *nsap, *osap;
406 if (uap->signum <= 0 || uap->signum >= ONSIG)
409 nsap = (uap->nsa != NULL) ? &nsa : NULL;
410 osap = (uap->osa != NULL) ? &osa : NULL;
415 error = copyin(uap->nsa, &sa, sizeof(sa));
418 nsap->sa_handler = sa.sa_handler;
419 nsap->sa_flags = sa.sa_flags;
420 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
422 error = do_sigaction(p, uap->signum, nsap, osap, 1);
423 if (osap && !error) {
424 sa.sa_handler = osap->sa_handler;
425 sa.sa_flags = osap->sa_flags;
426 SIG2OSIG(osap->sa_mask, sa.sa_mask);
427 error = copyout(&sa, uap->osa, sizeof(sa));
433 #endif /* COMPAT_43 */
436 * Initialize signal state for process 0;
437 * set to ignore signals that are ignored by default.
446 for (i = 1; i <= NSIG; i++)
447 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
448 SIGADDSET(p->p_sigignore, i);
453 * Reset signals for an exec of the specified process.
457 register struct proc *p;
459 register struct sigacts *ps;
463 * Reset caught signals. Held signals remain held
464 * through p_sigmask (unless they were caught,
465 * and are now ignored by default).
469 while (SIGNOTEMPTY(p->p_sigcatch)) {
470 sig = sig_ffs(&p->p_sigcatch);
471 SIGDELSET(p->p_sigcatch, sig);
472 if (sigprop(sig) & SA_IGNORE) {
474 SIGADDSET(p->p_sigignore, sig);
475 SIGDELSET(p->p_siglist, sig);
477 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
480 * Reset stack state to the user stack.
481 * Clear set of signals caught on the signal stack.
483 p->p_sigstk.ss_flags = SS_DISABLE;
484 p->p_sigstk.ss_size = 0;
485 p->p_sigstk.ss_sp = 0;
486 p->p_flag &= ~P_ALTSTACK;
488 * Reset no zombies if child dies flag as Solaris does.
490 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
491 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
492 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
499 * Manipulate signal mask.
502 do_sigprocmask(p, how, set, oset, old)
505 sigset_t *set, *oset;
512 *oset = p->p_sigmask;
519 SIGSETOR(p->p_sigmask, *set);
522 SIGSETNAND(p->p_sigmask, *set);
527 SIGSETLO(p->p_sigmask, *set);
541 * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't)
544 #ifndef _SYS_SYSPROTO_H_
545 struct sigprocmask_args {
553 register struct thread *td;
554 struct sigprocmask_args *uap;
556 struct proc *p = td->td_proc;
558 sigset_t *setp, *osetp;
561 setp = (uap->set != NULL) ? &set : NULL;
562 osetp = (uap->oset != NULL) ? &oset : NULL;
564 error = copyin(uap->set, setp, sizeof(set));
568 error = do_sigprocmask(p, uap->how, setp, osetp, 0);
569 if (osetp && !error) {
570 error = copyout(osetp, uap->oset, sizeof(oset));
575 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
577 * osigprocmask() - MP SAFE
579 #ifndef _SYS_SYSPROTO_H_
580 struct osigprocmask_args {
586 osigprocmask(td, uap)
587 register struct thread *td;
588 struct osigprocmask_args *uap;
590 struct proc *p = td->td_proc;
594 OSIG2SIG(uap->mask, set);
595 error = do_sigprocmask(p, uap->how, &set, &oset, 1);
596 SIG2OSIG(oset, td->td_retval[0]);
599 #endif /* COMPAT_43 */
601 #ifndef _SYS_SYSPROTO_H_
602 struct sigpending_args {
613 struct sigpending_args *uap;
615 struct proc *p = td->td_proc;
621 siglist = p->p_siglist;
624 error = copyout(&siglist, uap->set, sizeof(sigset_t));
628 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
629 #ifndef _SYS_SYSPROTO_H_
630 struct osigpending_args {
641 struct osigpending_args *uap;
643 struct proc *p = td->td_proc;
647 SIG2OSIG(p->p_siglist, td->td_retval[0]);
652 #endif /* COMPAT_43 */
654 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
656 * Generalized interface signal handler, 4.3-compatible.
658 #ifndef _SYS_SYSPROTO_H_
659 struct osigvec_args {
672 register struct osigvec_args *uap;
674 struct proc *p = td->td_proc;
676 struct sigaction nsa, osa;
677 register struct sigaction *nsap, *osap;
680 if (uap->signum <= 0 || uap->signum >= ONSIG)
682 nsap = (uap->nsv != NULL) ? &nsa : NULL;
683 osap = (uap->osv != NULL) ? &osa : NULL;
685 error = copyin(uap->nsv, &vec, sizeof(vec));
688 nsap->sa_handler = vec.sv_handler;
689 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
690 nsap->sa_flags = vec.sv_flags;
691 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
693 nsap->sa_flags |= SA_USERTRAMP;
697 error = do_sigaction(p, uap->signum, nsap, osap, 1);
699 if (osap && !error) {
700 vec.sv_handler = osap->sa_handler;
701 SIG2OSIG(osap->sa_mask, vec.sv_mask);
702 vec.sv_flags = osap->sa_flags;
703 vec.sv_flags &= ~SA_NOCLDWAIT;
704 vec.sv_flags ^= SA_RESTART;
706 vec.sv_flags &= ~SA_NOCLDSTOP;
708 error = copyout(&vec, uap->osv, sizeof(vec));
713 #ifndef _SYS_SYSPROTO_H_
714 struct osigblock_args {
723 register struct thread *td;
724 struct osigblock_args *uap;
726 struct proc *p = td->td_proc;
729 OSIG2SIG(uap->mask, set);
733 SIG2OSIG(p->p_sigmask, td->td_retval[0]);
734 SIGSETOR(p->p_sigmask, set);
740 #ifndef _SYS_SYSPROTO_H_
741 struct osigsetmask_args {
751 struct osigsetmask_args *uap;
753 struct proc *p = td->td_proc;
756 OSIG2SIG(uap->mask, set);
760 SIG2OSIG(p->p_sigmask, td->td_retval[0]);
761 SIGSETLO(p->p_sigmask, set);
766 #endif /* COMPAT_43 || COMPAT_SUNOS */
769 * Suspend process until signal, providing mask to be set
770 * in the meantime. Note nonstandard calling convention:
771 * libc stub passes mask, not pointer, to save a copyin.
772 ***** XXXKSE this doesn't make sense under KSE.
773 ***** Do we suspend the thread or all threads in the process?
774 ***** How do we suspend threads running NOW on another processor?
776 #ifndef _SYS_SYSPROTO_H_
777 struct sigsuspend_args {
778 const sigset_t *sigmask;
788 struct sigsuspend_args *uap;
790 struct proc *p = td->td_proc;
792 register struct sigacts *ps;
795 error = copyin(uap->sigmask, &mask, sizeof(mask));
800 * When returning from sigsuspend, we want
801 * the old mask to be restored after the
802 * signal handler has finished. Thus, we
803 * save it here and mark the sigacts structure
809 p->p_oldsigmask = p->p_sigmask;
810 p->p_flag |= P_OLDMASK;
814 while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
818 /* always return EINTR rather than ERESTART... */
822 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
823 #ifndef _SYS_SYSPROTO_H_
824 struct osigsuspend_args {
835 struct osigsuspend_args *uap;
837 struct proc *p = td->td_proc;
839 register struct sigacts *ps;
844 p->p_oldsigmask = p->p_sigmask;
845 p->p_flag |= P_OLDMASK;
846 OSIG2SIG(uap->mask, mask);
848 SIGSETLO(p->p_sigmask, mask);
849 while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
853 /* always return EINTR rather than ERESTART... */
856 #endif /* COMPAT_43 */
858 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
859 #ifndef _SYS_SYSPROTO_H_
860 struct osigstack_args {
861 struct sigstack *nss;
862 struct sigstack *oss;
872 register struct osigstack_args *uap;
874 struct proc *p = td->td_proc;
880 if (uap->oss != NULL) {
882 ss.ss_sp = p->p_sigstk.ss_sp;
883 ss.ss_onstack = sigonstack(cpu_getstack(td));
885 error = copyout(&ss, uap->oss, sizeof(struct sigstack));
890 if (uap->nss != NULL) {
891 if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
894 p->p_sigstk.ss_sp = ss.ss_sp;
895 p->p_sigstk.ss_size = 0;
896 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
897 p->p_flag |= P_ALTSTACK;
904 #endif /* COMPAT_43 || COMPAT_SUNOS */
906 #ifndef _SYS_SYSPROTO_H_
907 struct sigaltstack_args {
919 register struct sigaltstack_args *uap;
921 struct proc *p = td->td_proc;
928 oonstack = sigonstack(cpu_getstack(td));
930 if (uap->oss != NULL) {
933 ss.ss_flags = (p->p_flag & P_ALTSTACK)
934 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
936 if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
940 if (uap->ss != NULL) {
945 if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
947 if ((ss.ss_flags & ~SS_DISABLE) != 0) {
951 if (!(ss.ss_flags & SS_DISABLE)) {
952 if (ss.ss_size < p->p_sysent->sv_minsigstksz) {
958 p->p_flag |= P_ALTSTACK;
962 p->p_flag &= ~P_ALTSTACK;
972 * Common code for kill process group/broadcast kill.
973 * cp is calling process.
976 killpg1(cp, sig, pgid, all)
977 register struct proc *cp;
980 register struct proc *p;
988 sx_slock(&allproc_lock);
989 LIST_FOREACH(p, &allproc, p_list) {
991 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) {
995 if (p_cansignal(cp, p, sig) == 0) {
1002 sx_sunlock(&allproc_lock);
1007 * zero pgid means send to my process group.
1012 pgrp = pgfind(pgid);
1019 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1021 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1025 mtx_lock_spin(&sched_lock);
1026 if (p->p_stat == SZOMB) {
1027 mtx_unlock_spin(&sched_lock);
1031 mtx_unlock_spin(&sched_lock);
1032 if (p_cansignal(cp, p, sig) == 0) {
1041 return (nfound ? 0 : ESRCH);
1044 #ifndef _SYS_SYSPROTO_H_
1056 register struct thread *td;
1057 register struct kill_args *uap;
1059 register struct proc *cp = td->td_proc;
1060 register struct proc *p;
1063 if ((u_int)uap->signum > _SIG_MAXSIG)
1068 /* kill single process */
1069 if ((p = pfind(uap->pid)) == NULL) {
1071 } else if (p_cansignal(cp, p, uap->signum)) {
1076 psignal(p, uap->signum);
1082 case -1: /* broadcast signal */
1083 error = killpg1(cp, uap->signum, 0, 1);
1085 case 0: /* signal own process group */
1086 error = killpg1(cp, uap->signum, 0, 0);
1088 default: /* negative explicit process group */
1089 error = killpg1(cp, uap->signum, -uap->pid, 0);
1097 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1098 #ifndef _SYS_SYSPROTO_H_
1099 struct okillpg_args {
1111 register struct okillpg_args *uap;
1115 if ((u_int)uap->signum > _SIG_MAXSIG)
1118 error = killpg1(td->td_proc, uap->signum, uap->pgid, 0);
1122 #endif /* COMPAT_43 || COMPAT_SUNOS */
1125 * Send a signal to a process group.
1135 pgrp = pgfind(pgid);
1138 pgsignal(pgrp, sig, 0);
1145 * Send a signal to a process group. If checktty is 1,
1146 * limit to members which have a controlling terminal.
1149 pgsignal(pgrp, sig, checkctty)
1153 register struct proc *p;
1156 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1157 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1159 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1167 * Send a signal caused by a trap to the current process.
1168 * If it will be caught immediately, deliver it with correct code.
1169 * Otherwise, post it normally.
1174 trapsignal(p, sig, code)
1179 register struct sigacts *ps = p->p_sigacts;
1183 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1184 !SIGISMEMBER(p->p_sigmask, sig)) {
1185 p->p_stats->p_ru.ru_nsignals++;
1187 if (KTRPOINT(p, KTR_PSIG))
1188 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
1189 &p->p_sigmask, code);
1191 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1192 &p->p_sigmask, code);
1193 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1194 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1195 SIGADDSET(p->p_sigmask, sig);
1196 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1198 * See do_sigaction() for origin of this code.
1200 SIGDELSET(p->p_sigcatch, sig);
1201 if (sig != SIGCONT &&
1202 sigprop(sig) & SA_IGNORE)
1203 SIGADDSET(p->p_sigignore, sig);
1204 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1207 p->p_code = code; /* XXX for core dump/debugger */
1208 p->p_sig = sig; /* XXX to verify code */
1216 * Send the signal to the process. If the signal has an action, the action
1217 * is usually performed by the target process rather than the caller; we add
1218 * the signal to the set of pending signals for the process.
1221 * o When a stop signal is sent to a sleeping process that takes the
1222 * default action, the process is stopped without awakening it.
1223 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1224 * regardless of the signal action (eg, blocked or ignored).
1226 * Other ignored signals are discarded immediately.
1230 register struct proc *p;
1234 register sig_t action;
1240 KASSERT(_SIG_VALID(sig),
1241 ("psignal(): invalid signal %d\n", sig));
1243 PROC_LOCK_ASSERT(p, MA_OWNED);
1244 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1246 prop = sigprop(sig);
1249 * If proc is traced, always give parent a chance;
1250 * if signal event is tracked by procfs, give *that*
1251 * a chance, as well.
1253 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1257 * If the signal is being ignored,
1258 * then we forget about it immediately.
1259 * (Note: we don't set SIGCONT in p_sigignore,
1260 * and if it is set to SIG_IGN,
1261 * action will be SIG_DFL here.)
1263 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1265 if (SIGISMEMBER(p->p_sigmask, sig))
1267 else if (SIGISMEMBER(p->p_sigcatch, sig))
1274 * bring the priority of a process up if we want it to get
1275 * killed in this lifetime.
1276 * XXXKSE think if a better way to do this.
1278 * What we need to do is see if there is a thread that will
1279 * be able to accept the signal. e.g.
1280 * FOREACH_THREAD_IN_PROC() {
1281 * if runnable, we're done
1282 * else pick one at random.
1286 * For now there is one thread per proc.
1287 * Effectively select one sucker thread..
1289 td = FIRST_THREAD_IN_PROC(p);
1290 mtx_lock_spin(&sched_lock);
1291 if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1292 (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1293 p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
1294 mtx_unlock_spin(&sched_lock);
1297 SIG_STOPSIGMASK(p->p_siglist);
1299 if (prop & SA_STOP) {
1301 * If sending a tty stop signal to a member of an orphaned
1302 * process group, discard the signal here if the action
1303 * is default; don't stop the process below if sleeping,
1304 * and don't clear any pending SIGCONT.
1306 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1309 SIG_CONTSIGMASK(p->p_siglist);
1311 SIGADDSET(p->p_siglist, sig);
1314 * Defer further processing for signals which are held,
1315 * except that stopped processes must be continued by SIGCONT.
1317 mtx_lock_spin(&sched_lock);
1318 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
1319 mtx_unlock_spin(&sched_lock);
1323 switch (p->p_stat) {
1327 * If process is sleeping uninterruptibly
1328 * we can't interrupt the sleep... the signal will
1329 * be noticed when the process returns through
1330 * trap() or syscall().
1332 if ((td->td_flags & TDF_SINTR) == 0) {
1333 mtx_unlock_spin(&sched_lock);
1337 * Process is sleeping and traced... make it runnable
1338 * so it can discover the signal in issignal() and stop
1341 if (p->p_flag & P_TRACED)
1343 mtx_unlock_spin(&sched_lock);
1345 * If SIGCONT is default (or ignored) and process is
1346 * asleep, we are finished; the process should not
1349 if ((prop & SA_CONT) && action == SIG_DFL) {
1350 SIGDELSET(p->p_siglist, sig);
1354 * When a sleeping process receives a stop
1355 * signal, process immediately if possible.
1356 * All other (caught or default) signals
1357 * cause the process to run.
1359 if (prop & SA_STOP) {
1360 if (action != SIG_DFL)
1363 * If a child holding parent blocked,
1364 * stopping could cause deadlock.
1366 if (p->p_flag & P_PPWAIT)
1368 SIGDELSET(p->p_siglist, sig);
1370 PROC_LOCK(p->p_pptr);
1371 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1372 psignal(p->p_pptr, SIGCHLD);
1373 PROC_UNLOCK(p->p_pptr);
1374 mtx_lock_spin(&sched_lock);
1376 mtx_unlock_spin(&sched_lock);
1383 mtx_unlock_spin(&sched_lock);
1385 * If traced process is already stopped,
1386 * then no further action is necessary.
1388 if (p->p_flag & P_TRACED)
1392 * Kill signal always sets processes running.
1397 if (prop & SA_CONT) {
1399 * If SIGCONT is default (or ignored), we continue the
1400 * process but don't leave the signal in p_siglist, as
1401 * it has no further action. If SIGCONT is held, we
1402 * continue the process and leave the signal in
1403 * p_siglist. If the process catches SIGCONT, let it
1404 * handle the signal itself. If it isn't waiting on
1405 * an event, then it goes back to run state.
1406 * Otherwise, process goes back to sleep state.
1408 if (action == SIG_DFL)
1409 SIGDELSET(p->p_siglist, sig);
1410 if (action == SIG_CATCH)
1412 mtx_lock_spin(&sched_lock);
1415 * do this for each thread.
1417 if (p->p_flag & P_KSES) {
1418 mtx_assert(&sched_lock,
1419 MA_OWNED | MA_NOTRECURSED);
1420 FOREACH_THREAD_IN_PROC(p, td) {
1421 if (td->td_wchan == NULL) {
1422 setrunnable(td); /* XXXKSE */
1424 /* mark it as sleeping */
1427 mtx_unlock_spin(&sched_lock);
1429 if (td->td_wchan == NULL)
1432 mtx_unlock_spin(&sched_lock);
1437 if (prop & SA_STOP) {
1439 * Already stopped, don't need to stop again.
1440 * (If we did the shell could get confused.)
1442 SIGDELSET(p->p_siglist, sig);
1447 * If process is sleeping interruptibly, then simulate a
1448 * wakeup so that when it is continued, it will be made
1449 * runnable and can look at the signal. But don't make
1450 * the process runnable, leave it stopped.
1451 * XXXKSE should we wake ALL blocked threads?
1453 mtx_lock_spin(&sched_lock);
1454 if (p->p_flag & P_KSES) {
1455 FOREACH_THREAD_IN_PROC(p, td) {
1456 if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1457 if (td->td_flags & TDF_CVWAITQ)
1458 cv_waitq_remove(td);
1460 unsleep(td); /* XXXKSE */
1464 if (td->td_wchan && td->td_flags & TDF_SINTR) {
1465 if (td->td_flags & TDF_CVWAITQ)
1466 cv_waitq_remove(td);
1468 unsleep(td); /* XXXKSE */
1471 mtx_unlock_spin(&sched_lock);
1476 * SRUN, SIDL, SZOMB do nothing with the signal,
1477 * other than kicking ourselves if we are running.
1478 * It will either never be noticed, or noticed very soon.
1480 if (p->p_stat == SRUN) {
1483 struct thread *td = curthread;
1484 signotify(&p->p_kse); /* XXXKSE */
1485 /* we should only deliver to one thread.. but which one? */
1486 FOREACH_KSEGRP_IN_PROC(p, kg) {
1487 FOREACH_KSE_IN_GROUP(kg, ke) {
1488 if (ke->ke_thread == td) {
1491 forward_signal(ke->ke_thread);
1495 signotify(&p->p_kse); /* XXXKSE */
1498 mtx_unlock_spin(&sched_lock);
1505 * Raise priority to at least PUSER.
1506 * XXXKSE Should we make them all run fast?
1507 * Maybe just one would be enough?
1509 mtx_lock_spin(&sched_lock);
1511 if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
1512 FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1515 /* If we jump here, sched_lock has to be owned. */
1516 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1517 setrunnable(td); /* XXXKSE */
1518 mtx_unlock_spin(&sched_lock);
1520 /* If we jump here, sched_lock should not be owned. */
1521 mtx_assert(&sched_lock, MA_NOTOWNED);
1525 * If the current process has received a signal (should be caught or cause
1526 * termination, should interrupt current syscall), return the signal number.
1527 * Stop signals with default action are processed immediately, then cleared;
1528 * they aren't returned. This is checked after each entry to the system for
1529 * a syscall or trap (though this can usually be done without calling issignal
1530 * by checking the pending signal masks in the CURSIG macro.) The normal call
1533 * while (sig = CURSIG(curproc))
1538 register struct proc *p;
1541 register int sig, prop;
1543 PROC_LOCK_ASSERT(p, MA_OWNED);
1545 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1547 mask = p->p_siglist;
1548 SIGSETNAND(mask, p->p_sigmask);
1549 if (p->p_flag & P_PPWAIT)
1550 SIG_STOPSIGMASK(mask);
1551 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1553 sig = sig_ffs(&mask);
1554 prop = sigprop(sig);
1556 _STOPEVENT(p, S_SIG, sig);
1559 * We should see pending but ignored signals
1560 * only if P_TRACED was on when they were posted.
1562 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1563 SIGDELSET(p->p_siglist, sig);
1566 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1568 * If traced, always stop, and stay
1569 * stopped until released by the parent.
1572 PROC_LOCK(p->p_pptr);
1573 psignal(p->p_pptr, SIGCHLD);
1574 PROC_UNLOCK(p->p_pptr);
1576 mtx_lock_spin(&sched_lock);
1580 p->p_stats->p_ru.ru_nivcsw++;
1582 mtx_unlock_spin(&sched_lock);
1585 } while (!trace_req(p)
1586 && p->p_flag & P_TRACED);
1589 * If the traced bit got turned off, go back up
1590 * to the top to rescan signals. This ensures
1591 * that p_sig* and ps_sigact are consistent.
1593 if ((p->p_flag & P_TRACED) == 0)
1597 * If parent wants us to take the signal,
1598 * then it will leave it in p->p_xstat;
1599 * otherwise we just look for signals again.
1601 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1607 * Put the new signal into p_siglist. If the
1608 * signal is being masked, look for other signals.
1610 SIGADDSET(p->p_siglist, sig);
1611 if (SIGISMEMBER(p->p_sigmask, sig))
1616 * Decide whether the signal should be returned.
1617 * Return the signal's number, or fall through
1618 * to clear it from the pending mask.
1620 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1624 * Don't take default actions on system processes.
1626 if (p->p_pid <= 1) {
1629 * Are you sure you want to ignore SIGSEGV
1632 printf("Process (pid %lu) got signal %d\n",
1633 (u_long)p->p_pid, sig);
1635 break; /* == ignore */
1638 * If there is a pending stop signal to process
1639 * with default action, stop here,
1640 * then clear the signal. However,
1641 * if process is member of an orphaned
1642 * process group, ignore tty stop signals.
1644 if (prop & SA_STOP) {
1645 if (p->p_flag & P_TRACED ||
1646 (p->p_pgrp->pg_jobc == 0 &&
1648 break; /* == ignore */
1650 PROC_LOCK(p->p_pptr);
1651 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1652 psignal(p->p_pptr, SIGCHLD);
1653 PROC_UNLOCK(p->p_pptr);
1654 mtx_lock_spin(&sched_lock);
1658 p->p_stats->p_ru.ru_nivcsw++;
1660 mtx_unlock_spin(&sched_lock);
1664 } else if (prop & SA_IGNORE) {
1666 * Except for SIGCONT, shouldn't get here.
1667 * Default action is to ignore; drop it.
1669 break; /* == ignore */
1676 * Masking above should prevent us ever trying
1677 * to take action on an ignored signal other
1678 * than SIGCONT, unless process is traced.
1680 if ((prop & SA_CONT) == 0 &&
1681 (p->p_flag & P_TRACED) == 0)
1682 printf("issignal\n");
1683 break; /* == ignore */
1687 * This signal has an action, let
1688 * postsig() process it.
1692 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1698 * Put the argument process into the stopped state and notify the parent
1699 * via wakeup. Signals are handled elsewhere. The process must not be
1700 * on the run queue. Must be called with the proc p locked and the scheduler
1705 register struct proc *p;
1708 PROC_LOCK_ASSERT(p, MA_OWNED);
1709 mtx_assert(&sched_lock, MA_OWNED);
1711 p->p_flag &= ~P_WAITED;
1712 wakeup((caddr_t)p->p_pptr);
1716 * Take the action for the specified signal
1717 * from the current set of pending signals.
1723 struct thread *td = curthread;
1724 register struct proc *p = td->td_proc;
1727 sigset_t returnmask;
1730 KASSERT(sig != 0, ("postsig"));
1732 PROC_LOCK_ASSERT(p, MA_OWNED);
1734 SIGDELSET(p->p_siglist, sig);
1735 action = ps->ps_sigact[_SIG_IDX(sig)];
1737 if (KTRPOINT(p, KTR_PSIG))
1738 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1739 &p->p_oldsigmask : &p->p_sigmask, 0);
1741 _STOPEVENT(p, S_SIG, sig);
1743 if (action == SIG_DFL) {
1745 * Default action, where the default is to kill
1746 * the process. (Other cases were ignored above.)
1752 * If we get here, the signal must be caught.
1754 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1755 ("postsig action"));
1757 * Set the new mask value and also defer further
1758 * occurrences of this signal.
1760 * Special case: user has done a sigsuspend. Here the
1761 * current mask is not of interest, but rather the
1762 * mask from before the sigsuspend is what we want
1763 * restored after the signal processing is completed.
1765 if (p->p_flag & P_OLDMASK) {
1766 returnmask = p->p_oldsigmask;
1767 p->p_flag &= ~P_OLDMASK;
1769 returnmask = p->p_sigmask;
1771 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1772 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1773 SIGADDSET(p->p_sigmask, sig);
1775 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1777 * See do_sigaction() for origin of this code.
1779 SIGDELSET(p->p_sigcatch, sig);
1780 if (sig != SIGCONT &&
1781 sigprop(sig) & SA_IGNORE)
1782 SIGADDSET(p->p_sigignore, sig);
1783 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1785 p->p_stats->p_ru.ru_nsignals++;
1786 if (p->p_sig != sig) {
1793 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1798 * Kill the current process for stated reason.
1806 PROC_LOCK_ASSERT(p, MA_OWNED);
1807 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
1808 p, p->p_pid, p->p_comm);
1809 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1810 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1811 psignal(p, SIGKILL);
1815 * Force the current process to exit with the specified signal, dumping core
1816 * if appropriate. We bypass the normal tests for masked and caught signals,
1817 * allowing unrecoverable failures to terminate the process without changing
1818 * signal state. Mark the accounting record with the signal termination.
1819 * If dumping core, save the signal number for the debugger. Calls exit and
1827 struct proc *p = td->td_proc;
1829 PROC_LOCK_ASSERT(p, MA_OWNED);
1830 p->p_acflag |= AXSIG;
1831 if (sigprop(sig) & SA_CORE) {
1834 * Log signals which would cause core dumps
1835 * (Log as LOG_INFO to appease those who don't want
1837 * XXX : Todo, as well as euid, write out ruid too
1840 if (!mtx_owned(&Giant))
1842 if (coredump(td) == 0)
1844 if (kern_logsigexit)
1846 "pid %d (%s), uid %d: exited on signal %d%s\n",
1847 p->p_pid, p->p_comm,
1848 p->p_ucred ? p->p_ucred->cr_uid : -1,
1850 sig & WCOREFLAG ? " (core dumped)" : "");
1853 if (!mtx_owned(&Giant))
1856 exit1(td, W_EXITCODE(0, sig));
1860 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1861 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1862 sizeof(corefilename), "process corefile name format string");
1865 * expand_name(name, uid, pid)
1866 * Expand the name described in corefilename, using name, uid, and pid.
1867 * corefilename is a printf-like string, with three format specifiers:
1868 * %N name of process ("name")
1869 * %P process id (pid)
1871 * For example, "%N.core" is the default; they can be disabled completely
1872 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1873 * This is controlled by the sysctl variable kern.corefile (see above).
1877 expand_name(name, uid, pid)
1878 const char *name; uid_t uid; pid_t pid; {
1880 char buf[11]; /* Buffer for pid/uid -- max 4B */
1882 char *format = corefilename;
1885 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1888 namelen = strlen(name);
1889 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1891 switch (format[i]) {
1892 case '%': /* Format character */
1894 switch (format[i]) {
1898 case 'N': /* process name */
1899 if ((n + namelen) > MAXPATHLEN) {
1900 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1901 pid, name, uid, temp, name);
1905 memcpy(temp+n, name, namelen);
1908 case 'P': /* process id */
1909 l = sprintf(buf, "%u", pid);
1910 if ((n + l) > MAXPATHLEN) {
1911 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1912 pid, name, uid, temp, name);
1916 memcpy(temp+n, buf, l);
1919 case 'U': /* user id */
1920 l = sprintf(buf, "%u", uid);
1921 if ((n + l) > MAXPATHLEN) {
1922 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1923 pid, name, uid, temp, name);
1927 memcpy(temp+n, buf, l);
1931 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1935 temp[n++] = format[i];
1943 * Dump a process' core. The main routine does some
1944 * policy checking, and creates the name of the coredump;
1945 * then it passes on a vnode and a size limit to the process-specific
1946 * coredump routine if there is one; if there _is not_ one, it returns
1947 * ENOSYS; otherwise it returns the error from the process-specific routine.
1949 * XXX: VOP_GETATTR() here requires holding the vnode lock.
1953 coredump(struct thread *td)
1955 struct proc *p = td->td_proc;
1956 register struct vnode *vp;
1957 register struct ucred *cred = p->p_ucred;
1959 struct nameidata nd;
1961 int error, error1, flags;
1963 char *name; /* name of corefile */
1967 _STOPEVENT(p, S_CORE, 0);
1969 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1975 * Note that the bulk of limit checking is done after
1976 * the corefile is created. The exception is if the limit
1977 * for corefiles is 0, in which case we don't bother
1978 * creating the corefile at all. This layout means that
1979 * a corefile is truncated instead of not being created,
1980 * if it is larger than the limit.
1982 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1990 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1993 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1994 flags = O_CREAT | FWRITE | O_NOFOLLOW;
1995 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1999 NDFREE(&nd, NDF_ONLY_PNBUF);
2002 VOP_UNLOCK(vp, 0, td);
2003 lf.l_whence = SEEK_SET;
2006 lf.l_type = F_WRLCK;
2007 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
2011 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2012 lf.l_type = F_UNLCK;
2013 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2014 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2016 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2021 /* Don't dump to non-regular files or files with links. */
2022 if (vp->v_type != VREG ||
2023 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2029 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2030 VOP_LEASE(vp, td, cred, LEASE_WRITE);
2031 VOP_SETATTR(vp, &vattr, cred, td);
2032 VOP_UNLOCK(vp, 0, td);
2034 p->p_acflag |= ACORE;
2037 error = p->p_sysent->sv_coredump ?
2038 p->p_sysent->sv_coredump(td, vp, limit) :
2042 lf.l_type = F_UNLCK;
2043 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2044 vn_finished_write(mp);
2046 error1 = vn_close(vp, FWRITE, cred, td);
2053 * Nonexistent system call-- signal process (may want to handle it).
2054 * Flag error in case process won't see signal immediately (blocked or ignored).
2056 #ifndef _SYS_SYSPROTO_H_
2068 struct nosys_args *args;
2070 struct proc *p = td->td_proc;
2081 * Send a SIGIO or SIGURG signal to a process or process group using
2082 * stored credentials rather than those of the current process.
2085 pgsigio(sigio, sig, checkctty)
2086 struct sigio *sigio;
2092 if (sigio->sio_pgid > 0) {
2093 PROC_LOCK(sigio->sio_proc);
2094 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2095 psignal(sigio->sio_proc, sig);
2096 PROC_UNLOCK(sigio->sio_proc);
2097 } else if (sigio->sio_pgid < 0) {
2100 PGRP_LOCK(sigio->sio_pgrp);
2101 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2103 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2104 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2108 PGRP_UNLOCK(sigio->sio_pgrp);
2113 filt_sigattach(struct knote *kn)
2115 struct proc *p = curproc;
2117 kn->kn_ptr.p_proc = p;
2118 kn->kn_flags |= EV_CLEAR; /* automatically set */
2121 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2128 filt_sigdetach(struct knote *kn)
2130 struct proc *p = kn->kn_ptr.p_proc;
2133 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2138 * signal knotes are shared with proc knotes, so we apply a mask to
2139 * the hint in order to differentiate them from process hints. This
2140 * could be avoided by using a signal-specific knote list, but probably
2141 * isn't worth the trouble.
2144 filt_signal(struct knote *kn, long hint)
2147 if (hint & NOTE_SIGNAL) {
2148 hint &= ~NOTE_SIGNAL;
2150 if (kn->kn_id == hint)
2153 return (kn->kn_data != 0);