Rename sprintf -> ksprintf
[dfdiff.git] / sys / kern / kern_sig.c
blob71de599879ab1e9a32ad5591915527a23263008d
1 /*
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
12 * are met:
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
36 * SUCH DAMAGE.
38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 * $FreeBSD: src/sys/kern/kern_sig.c,v 1.72.2.17 2003/05/16 16:34:34 obrien Exp $
40 * $DragonFly: src/sys/kern/kern_sig.c,v 1.57 2006/12/20 18:14:41 dillon Exp $
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysproto.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/vnode.h>
52 #include <sys/event.h>
53 #include <sys/proc.h>
54 #include <sys/nlookup.h>
55 #include <sys/pioctl.h>
56 #include <sys/systm.h>
57 #include <sys/acct.h>
58 #include <sys/fcntl.h>
59 #include <sys/lock.h>
60 #include <sys/wait.h>
61 #include <sys/ktrace.h>
62 #include <sys/syslog.h>
63 #include <sys/stat.h>
64 #include <sys/sysent.h>
65 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
67 #include <sys/interrupt.h>
68 #include <sys/unistd.h>
69 #include <sys/kern_syscall.h>
70 #include <sys/thread2.h>
72 #include <machine/cpu.h>
73 #include <machine/smp.h>
75 static int coredump(struct proc *);
76 static char *expand_name(const char *, uid_t, pid_t);
77 static int killpg(int sig, int pgid, int all);
78 static int sig_ffs(sigset_t *set);
79 static int sigprop(int sig);
80 #ifdef SMP
81 static void signotify_remote(void *arg);
82 #endif
83 static int kern_sigtimedwait(sigset_t set, siginfo_t *info,
84 struct timespec *timeout);
86 static int filt_sigattach(struct knote *kn);
87 static void filt_sigdetach(struct knote *kn);
88 static int filt_signal(struct knote *kn, long hint);
90 struct filterops sig_filtops =
91 { 0, filt_sigattach, filt_sigdetach, filt_signal };
93 static int kern_logsigexit = 1;
94 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
95 &kern_logsigexit, 0,
96 "Log processes quitting on abnormal signals to syslog(3)");
99 * Can process p, with pcred pc, send the signal sig to process q?
101 #define CANSIGNAL(q, sig) \
102 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
103 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
106 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
108 #define CANSIGIO(ruid, uc, q) \
109 ((uc)->cr_uid == 0 || \
110 (ruid) == (q)->p_ucred->cr_ruid || \
111 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
112 (ruid) == (q)->p_ucred->cr_uid || \
113 (uc)->cr_uid == (q)->p_ucred->cr_uid)
115 int sugid_coredump;
116 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
117 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
119 static int do_coredump = 1;
120 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121 &do_coredump, 0, "Enable/Disable coredumps");
124 * Signal properties and actions.
125 * The array below categorizes the signals and their default actions
126 * according to the following properties:
128 #define SA_KILL 0x01 /* terminates process by default */
129 #define SA_CORE 0x02 /* ditto and coredumps */
130 #define SA_STOP 0x04 /* suspend process */
131 #define SA_TTYSTOP 0x08 /* ditto, from tty */
132 #define SA_IGNORE 0x10 /* ignore by default */
133 #define SA_CONT 0x20 /* continue if suspended */
134 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
135 #define SA_CKPT 0x80 /* checkpoint process */
138 static int sigproptbl[NSIG] = {
139 SA_KILL, /* SIGHUP */
140 SA_KILL, /* SIGINT */
141 SA_KILL|SA_CORE, /* SIGQUIT */
142 SA_KILL|SA_CORE, /* SIGILL */
143 SA_KILL|SA_CORE, /* SIGTRAP */
144 SA_KILL|SA_CORE, /* SIGABRT */
145 SA_KILL|SA_CORE, /* SIGEMT */
146 SA_KILL|SA_CORE, /* SIGFPE */
147 SA_KILL, /* SIGKILL */
148 SA_KILL|SA_CORE, /* SIGBUS */
149 SA_KILL|SA_CORE, /* SIGSEGV */
150 SA_KILL|SA_CORE, /* SIGSYS */
151 SA_KILL, /* SIGPIPE */
152 SA_KILL, /* SIGALRM */
153 SA_KILL, /* SIGTERM */
154 SA_IGNORE, /* SIGURG */
155 SA_STOP, /* SIGSTOP */
156 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
157 SA_IGNORE|SA_CONT, /* SIGCONT */
158 SA_IGNORE, /* SIGCHLD */
159 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
160 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
161 SA_IGNORE, /* SIGIO */
162 SA_KILL, /* SIGXCPU */
163 SA_KILL, /* SIGXFSZ */
164 SA_KILL, /* SIGVTALRM */
165 SA_KILL, /* SIGPROF */
166 SA_IGNORE, /* SIGWINCH */
167 SA_IGNORE, /* SIGINFO */
168 SA_KILL, /* SIGUSR1 */
169 SA_KILL, /* SIGUSR2 */
170 SA_IGNORE, /* SIGTHR */
171 SA_CKPT, /* SIGCKPT */
172 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */
173 SA_IGNORE,
174 SA_IGNORE,
175 SA_IGNORE,
176 SA_IGNORE,
177 SA_IGNORE,
178 SA_IGNORE,
179 SA_IGNORE,
180 SA_IGNORE,
181 SA_IGNORE,
182 SA_IGNORE,
183 SA_IGNORE,
184 SA_IGNORE,
185 SA_IGNORE,
186 SA_IGNORE,
187 SA_IGNORE,
188 SA_IGNORE,
189 SA_IGNORE,
190 SA_IGNORE,
191 SA_IGNORE,
192 SA_IGNORE,
193 SA_IGNORE,
194 SA_IGNORE,
195 SA_IGNORE,
196 SA_IGNORE,
197 SA_IGNORE,
198 SA_IGNORE,
199 SA_IGNORE,
200 SA_IGNORE,
201 SA_IGNORE,
202 SA_IGNORE,
206 static __inline int
207 sigprop(int sig)
210 if (sig > 0 && sig < NSIG)
211 return (sigproptbl[_SIG_IDX(sig)]);
212 return (0);
215 static __inline int
216 sig_ffs(sigset_t *set)
218 int i;
220 for (i = 0; i < _SIG_WORDS; i++)
221 if (set->__bits[i])
222 return (ffs(set->__bits[i]) + (i * 32));
223 return (0);
227 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
229 struct thread *td = curthread;
230 struct proc *p = td->td_proc;
231 struct sigacts *ps = p->p_sigacts;
233 if (sig <= 0 || sig > _SIG_MAXSIG)
234 return (EINVAL);
236 if (oact) {
237 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
238 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
239 oact->sa_flags = 0;
240 if (SIGISMEMBER(ps->ps_sigonstack, sig))
241 oact->sa_flags |= SA_ONSTACK;
242 if (!SIGISMEMBER(ps->ps_sigintr, sig))
243 oact->sa_flags |= SA_RESTART;
244 if (SIGISMEMBER(ps->ps_sigreset, sig))
245 oact->sa_flags |= SA_RESETHAND;
246 if (SIGISMEMBER(ps->ps_signodefer, sig))
247 oact->sa_flags |= SA_NODEFER;
248 if (SIGISMEMBER(ps->ps_siginfo, sig))
249 oact->sa_flags |= SA_SIGINFO;
250 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
251 oact->sa_flags |= SA_NOCLDSTOP;
252 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
253 oact->sa_flags |= SA_NOCLDWAIT;
255 if (act) {
256 if ((sig == SIGKILL || sig == SIGSTOP) &&
257 act->sa_handler != SIG_DFL)
258 return (EINVAL);
261 * Change setting atomically.
263 crit_enter();
265 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
266 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
267 if (act->sa_flags & SA_SIGINFO) {
268 ps->ps_sigact[_SIG_IDX(sig)] =
269 (__sighandler_t *)act->sa_sigaction;
270 SIGADDSET(ps->ps_siginfo, sig);
271 } else {
272 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
273 SIGDELSET(ps->ps_siginfo, sig);
275 if (!(act->sa_flags & SA_RESTART))
276 SIGADDSET(ps->ps_sigintr, sig);
277 else
278 SIGDELSET(ps->ps_sigintr, sig);
279 if (act->sa_flags & SA_ONSTACK)
280 SIGADDSET(ps->ps_sigonstack, sig);
281 else
282 SIGDELSET(ps->ps_sigonstack, sig);
283 if (act->sa_flags & SA_RESETHAND)
284 SIGADDSET(ps->ps_sigreset, sig);
285 else
286 SIGDELSET(ps->ps_sigreset, sig);
287 if (act->sa_flags & SA_NODEFER)
288 SIGADDSET(ps->ps_signodefer, sig);
289 else
290 SIGDELSET(ps->ps_signodefer, sig);
291 if (sig == SIGCHLD) {
292 if (act->sa_flags & SA_NOCLDSTOP)
293 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
294 else
295 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
296 if (act->sa_flags & SA_NOCLDWAIT) {
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.
303 if (p->p_pid == 1)
304 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
305 else
306 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
307 } else {
308 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
312 * Set bit in p_sigignore for signals that are set to SIG_IGN,
313 * and for signals set to SIG_DFL where the default is to
314 * ignore. However, don't put SIGCONT in p_sigignore, as we
315 * have to restart the process.
317 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
318 (sigprop(sig) & SA_IGNORE &&
319 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
320 /* never to be seen again */
321 SIGDELSET(p->p_siglist, sig);
322 if (sig != SIGCONT)
323 /* easier in ksignal */
324 SIGADDSET(p->p_sigignore, sig);
325 SIGDELSET(p->p_sigcatch, sig);
326 } else {
327 SIGDELSET(p->p_sigignore, sig);
328 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
329 SIGDELSET(p->p_sigcatch, sig);
330 else
331 SIGADDSET(p->p_sigcatch, sig);
334 crit_exit();
336 return (0);
340 sys_sigaction(struct sigaction_args *uap)
342 struct sigaction act, oact;
343 struct sigaction *actp, *oactp;
344 int error;
346 actp = (uap->act != NULL) ? &act : NULL;
347 oactp = (uap->oact != NULL) ? &oact : NULL;
348 if (actp) {
349 error = copyin(uap->act, actp, sizeof(act));
350 if (error)
351 return (error);
353 error = kern_sigaction(uap->sig, actp, oactp);
354 if (oactp && !error) {
355 error = copyout(oactp, uap->oact, sizeof(oact));
357 return (error);
361 * Initialize signal state for process 0;
362 * set to ignore signals that are ignored by default.
364 void
365 siginit(struct proc *p)
367 int i;
369 for (i = 1; i <= NSIG; i++)
370 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
371 SIGADDSET(p->p_sigignore, i);
375 * Reset signals for an exec of the specified process.
377 void
378 execsigs(struct proc *p)
380 struct sigacts *ps = p->p_sigacts;
381 int sig;
384 * Reset caught signals. Held signals remain held
385 * through p_sigmask (unless they were caught,
386 * and are now ignored by default).
388 while (SIGNOTEMPTY(p->p_sigcatch)) {
389 sig = sig_ffs(&p->p_sigcatch);
390 SIGDELSET(p->p_sigcatch, sig);
391 if (sigprop(sig) & SA_IGNORE) {
392 if (sig != SIGCONT)
393 SIGADDSET(p->p_sigignore, sig);
394 SIGDELSET(p->p_siglist, sig);
396 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
399 * Reset stack state to the user stack.
400 * Clear set of signals caught on the signal stack.
402 p->p_sigstk.ss_flags = SS_DISABLE;
403 p->p_sigstk.ss_size = 0;
404 p->p_sigstk.ss_sp = 0;
405 p->p_flag &= ~P_ALTSTACK;
407 * Reset no zombies if child dies flag as Solaris does.
409 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
413 * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
415 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
416 * p == curproc.
419 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
421 struct thread *td = curthread;
422 struct proc *p = td->td_proc;
423 int error;
425 if (oset != NULL)
426 *oset = p->p_sigmask;
428 error = 0;
429 if (set != NULL) {
430 switch (how) {
431 case SIG_BLOCK:
432 SIG_CANTMASK(*set);
433 SIGSETOR(p->p_sigmask, *set);
434 break;
435 case SIG_UNBLOCK:
436 SIGSETNAND(p->p_sigmask, *set);
437 break;
438 case SIG_SETMASK:
439 SIG_CANTMASK(*set);
440 p->p_sigmask = *set;
441 break;
442 default:
443 error = EINVAL;
444 break;
447 return (error);
451 * sigprocmask() - MP SAFE
454 sys_sigprocmask(struct sigprocmask_args *uap)
456 sigset_t set, oset;
457 sigset_t *setp, *osetp;
458 int error;
460 setp = (uap->set != NULL) ? &set : NULL;
461 osetp = (uap->oset != NULL) ? &oset : NULL;
462 if (setp) {
463 error = copyin(uap->set, setp, sizeof(set));
464 if (error)
465 return (error);
467 error = kern_sigprocmask(uap->how, setp, osetp);
468 if (osetp && !error) {
469 error = copyout(osetp, uap->oset, sizeof(oset));
471 return (error);
475 kern_sigpending(struct __sigset *set)
477 struct thread *td = curthread;
478 struct proc *p = td->td_proc;
480 *set = p->p_siglist;
482 return (0);
486 sys_sigpending(struct sigpending_args *uap)
488 sigset_t set;
489 int error;
491 error = kern_sigpending(&set);
493 if (error == 0)
494 error = copyout(&set, uap->set, sizeof(set));
495 return (error);
499 * Suspend process until signal, providing mask to be set
500 * in the meantime.
503 kern_sigsuspend(struct __sigset *set)
505 struct thread *td = curthread;
506 struct proc *p = td->td_proc;
507 struct sigacts *ps = p->p_sigacts;
510 * When returning from sigsuspend, we want
511 * the old mask to be restored after the
512 * signal handler has finished. Thus, we
513 * save it here and mark the sigacts structure
514 * to indicate this.
516 p->p_oldsigmask = p->p_sigmask;
517 p->p_flag |= P_OLDMASK;
519 SIG_CANTMASK(*set);
520 p->p_sigmask = *set;
521 while (tsleep(ps, PCATCH, "pause", 0) == 0)
522 /* void */;
523 /* always return EINTR rather than ERESTART... */
524 return (EINTR);
528 * Note nonstandard calling convention: libc stub passes mask, not
529 * pointer, to save a copyin.
532 sys_sigsuspend(struct sigsuspend_args *uap)
534 sigset_t mask;
535 int error;
537 error = copyin(uap->sigmask, &mask, sizeof(mask));
538 if (error)
539 return (error);
541 error = kern_sigsuspend(&mask);
543 return (error);
547 kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss)
549 struct thread *td = curthread;
550 struct proc *p = td->td_proc;
552 if ((p->p_flag & P_ALTSTACK) == 0)
553 p->p_sigstk.ss_flags |= SS_DISABLE;
555 if (oss)
556 *oss = p->p_sigstk;
558 if (ss) {
559 if (ss->ss_flags & SS_DISABLE) {
560 if (p->p_sigstk.ss_flags & SS_ONSTACK)
561 return (EINVAL);
562 p->p_flag &= ~P_ALTSTACK;
563 p->p_sigstk.ss_flags = ss->ss_flags;
564 } else {
565 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
566 return (ENOMEM);
567 p->p_flag |= P_ALTSTACK;
568 p->p_sigstk = *ss;
572 return (0);
576 sys_sigaltstack(struct sigaltstack_args *uap)
578 stack_t ss, oss;
579 int error;
581 if (uap->ss) {
582 error = copyin(uap->ss, &ss, sizeof(ss));
583 if (error)
584 return (error);
587 error = kern_sigaltstack(uap->ss ? &ss : NULL,
588 uap->oss ? &oss : NULL);
590 if (error == 0 && uap->oss)
591 error = copyout(&oss, uap->oss, sizeof(*uap->oss));
592 return (error);
596 * Common code for kill process group/broadcast kill.
597 * cp is calling process.
599 struct killpg_info {
600 int nfound;
601 int sig;
604 static int killpg_all_callback(struct proc *p, void *data);
606 static int
607 killpg(int sig, int pgid, int all)
609 struct killpg_info info;
610 struct proc *cp = curproc;
611 struct proc *p;
612 struct pgrp *pgrp;
614 info.nfound = 0;
615 info.sig = sig;
617 if (all) {
619 * broadcast
621 allproc_scan(killpg_all_callback, &info);
622 } else {
623 if (pgid == 0) {
625 * zero pgid means send to my process group.
627 pgrp = cp->p_pgrp;
628 } else {
629 pgrp = pgfind(pgid);
630 if (pgrp == NULL)
631 return (ESRCH);
633 lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
634 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
635 if (p->p_pid <= 1 ||
636 (p->p_flag & (P_SYSTEM | P_ZOMBIE)) ||
637 !CANSIGNAL(p, sig)) {
638 continue;
640 ++info.nfound;
641 if (sig)
642 ksignal(p, sig);
644 lockmgr(&pgrp->pg_lock, LK_RELEASE);
646 return (info.nfound ? 0 : ESRCH);
649 static int
650 killpg_all_callback(struct proc *p, void *data)
652 struct killpg_info *info = data;
654 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) ||
655 p == curproc || !CANSIGNAL(p, info->sig)) {
656 return (0);
658 ++info->nfound;
659 if (info->sig)
660 ksignal(p, info->sig);
661 return(0);
665 kern_kill(int sig, int pid)
667 struct thread *td = curthread;
668 struct proc *p = td->td_proc;
670 if ((u_int)sig > _SIG_MAXSIG)
671 return (EINVAL);
672 if (pid > 0) {
673 /* kill single process */
674 if ((p = pfind(pid)) == NULL)
675 return (ESRCH);
676 if (!CANSIGNAL(p, sig))
677 return (EPERM);
678 if (sig)
679 ksignal(p, sig);
680 return (0);
682 switch (pid) {
683 case -1: /* broadcast signal */
684 return (killpg(sig, 0, 1));
685 case 0: /* signal own process group */
686 return (killpg(sig, 0, 0));
687 default: /* negative explicit process group */
688 return (killpg(sig, -pid, 0));
690 /* NOTREACHED */
694 sys_kill(struct kill_args *uap)
696 int error;
698 error = kern_kill(uap->signum, uap->pid);
700 return (error);
704 * Send a signal to a process group.
706 void
707 gsignal(int pgid, int sig)
709 struct pgrp *pgrp;
711 if (pgid && (pgrp = pgfind(pgid)))
712 pgsignal(pgrp, sig, 0);
716 * Send a signal to a process group. If checktty is 1,
717 * limit to members which have a controlling terminal.
719 * pg_lock interlocks against a fork that might be in progress, to
720 * ensure that the new child process picks up the signal.
722 void
723 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
725 struct proc *p;
727 if (pgrp) {
728 lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
729 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
730 if (checkctty == 0 || p->p_flag & P_CONTROLT)
731 ksignal(p, sig);
733 lockmgr(&pgrp->pg_lock, LK_RELEASE);
738 * Send a signal caused by a trap to the current process.
739 * If it will be caught immediately, deliver it with correct code.
740 * Otherwise, post it normally.
742 void
743 trapsignal(struct proc *p, int sig, u_long code)
745 struct sigacts *ps = p->p_sigacts;
747 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
748 !SIGISMEMBER(p->p_sigmask, sig)) {
749 p->p_stats->p_ru.ru_nsignals++;
750 #ifdef KTRACE
751 if (KTRPOINT(p->p_thread, KTR_PSIG))
752 ktrpsig(p, sig, ps->ps_sigact[_SIG_IDX(sig)],
753 &p->p_sigmask, code);
754 #endif
755 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
756 &p->p_sigmask, code);
757 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
758 if (!SIGISMEMBER(ps->ps_signodefer, sig))
759 SIGADDSET(p->p_sigmask, sig);
760 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
762 * See kern_sigaction() for origin of this code.
764 SIGDELSET(p->p_sigcatch, sig);
765 if (sig != SIGCONT &&
766 sigprop(sig) & SA_IGNORE)
767 SIGADDSET(p->p_sigignore, sig);
768 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
770 } else {
771 p->p_code = code; /* XXX for core dump/debugger */
772 p->p_sig = sig; /* XXX to verify code */
773 ksignal(p, sig);
778 * Send the signal to the process. If the signal has an action, the action
779 * is usually performed by the target process rather than the caller; we add
780 * the signal to the set of pending signals for the process.
782 * Exceptions:
783 * o When a stop signal is sent to a sleeping process that takes the
784 * default action, the process is stopped without awakening it.
785 * o SIGCONT restarts stopped processes (or puts them back to sleep)
786 * regardless of the signal action (eg, blocked or ignored).
788 * Other ignored signals are discarded immediately.
790 void
791 ksignal(struct proc *p, int sig)
793 struct lwp *lp = &p->p_lwp;
794 int prop;
795 sig_t action;
797 if (sig > _SIG_MAXSIG || sig <= 0) {
798 printf("ksignal: signal %d\n", sig);
799 panic("ksignal signal number");
802 crit_enter();
803 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
804 crit_exit();
806 prop = sigprop(sig);
809 * If proc is traced, always give parent a chance;
810 * if signal event is tracked by procfs, give *that*
811 * a chance, as well.
813 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
814 action = SIG_DFL;
815 } else {
817 * If the signal is being ignored,
818 * then we forget about it immediately.
819 * (Note: we don't set SIGCONT in p_sigignore,
820 * and if it is set to SIG_IGN,
821 * action will be SIG_DFL here.)
823 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
824 return;
825 if (SIGISMEMBER(p->p_sigmask, sig))
826 action = SIG_HOLD;
827 else if (SIGISMEMBER(p->p_sigcatch, sig))
828 action = SIG_CATCH;
829 else
830 action = SIG_DFL;
833 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
834 (p->p_flag & P_TRACED) == 0) {
835 p->p_nice = NZERO;
839 * If continuing, clear any pending STOP signals.
841 if (prop & SA_CONT)
842 SIG_STOPSIGMASK(p->p_siglist);
844 if (prop & SA_STOP) {
846 * If sending a tty stop signal to a member of an orphaned
847 * process group, discard the signal here if the action
848 * is default; don't stop the process below if sleeping,
849 * and don't clear any pending SIGCONT.
851 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
852 action == SIG_DFL) {
853 return;
855 SIG_CONTSIGMASK(p->p_siglist);
857 SIGADDSET(p->p_siglist, sig);
860 * Defer further processing for signals which are held,
861 * except that stopped processes must be continued by SIGCONT.
863 if (action == SIG_HOLD) {
864 if ((prop & SA_CONT) == 0 || (p->p_flag & P_STOPPED) == 0)
865 return;
868 crit_enter();
871 * Process is in tsleep and not stopped
873 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED) == 0) {
875 * If the process is sleeping uninterruptibly
876 * we can't interrupt the sleep... the signal will
877 * be noticed when the process returns through
878 * trap() or syscall().
880 if ((p->p_flag & P_SINTR) == 0)
881 goto out;
884 * If the process is sleeping and traced, make it runnable
885 * so it can discover the signal in issignal() and stop
886 * for the parent.
888 * If the process is stopped and traced, no further action
889 * is necessary.
891 if (p->p_flag & P_TRACED)
892 goto run;
895 * If the process is sleeping and SA_CONT, and the signal
896 * mode is SIG_DFL, then make the process runnable.
898 * However, do *NOT* set P_BREAKTSLEEP. We do not want
899 * a SIGCONT to terminate an interruptable tsleep early
900 * and generate a spurious EINTR.
902 if ((prop & SA_CONT) && action == SIG_DFL) {
903 SIGDELSET(p->p_siglist, sig);
904 goto run_no_break;
908 * If the process is sleeping and receives a STOP signal,
909 * process immediately if possible. All other (caught or
910 * default) signals cause the process to run.
912 if (prop & SA_STOP) {
913 if (action != SIG_DFL)
914 goto run;
917 * If a child holding parent blocked, stopping
918 * could cause deadlock. Take no action at this
919 * time.
921 if (p->p_flag & P_PPWAIT)
922 goto out;
925 * Do not actually try to manipulate the process
926 * while it is sleeping, simply set P_STOPPED to
927 * indicate that it should stop as soon as it safely
928 * can.
930 SIGDELSET(p->p_siglist, sig);
931 p->p_flag |= P_STOPPED;
932 p->p_flag &= ~P_WAITED;
933 p->p_xstat = sig;
934 wakeup(p->p_pptr);
935 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
936 ksignal(p->p_pptr, SIGCHLD);
937 goto out;
941 * Otherwise the signal can interrupt the sleep.
943 goto run;
947 * Process is in tsleep and is stopped
949 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED)) {
951 * If the process is stopped and is being traced, then no
952 * further action is necessary.
954 if (p->p_flag & P_TRACED)
955 goto out;
958 * If the process is stopped and receives a KILL signal,
959 * make the process runnable.
961 if (sig == SIGKILL)
962 goto run;
965 * If the process is stopped and receives a CONT signal,
966 * then try to make the process runnable again.
968 if (prop & SA_CONT) {
970 * If SIGCONT is default (or ignored), we continue the
971 * process but don't leave the signal in p_siglist, as
972 * it has no further action. If SIGCONT is held, we
973 * continue the process and leave the signal in
974 * p_siglist. If the process catches SIGCONT, let it
975 * handle the signal itself.
977 if (action == SIG_DFL)
978 SIGDELSET(p->p_siglist, sig);
979 if (action == SIG_CATCH)
980 goto run;
983 * Make runnable but do not break a tsleep unless
984 * some other signal was pending.
986 goto run_no_break;
990 * If the process is stopped and receives another STOP
991 * signal, we do not need to stop it again. If we did
992 * the shell could get confused.
994 if (prop & SA_STOP) {
995 SIGDELSET(p->p_siglist, sig);
996 goto out;
1000 * Otherwise the process is sleeping interruptably but
1001 * is stopped, just set the P_BREAKTSLEEP flag and take
1002 * no further action. The next runnable action will wake
1003 * the process up.
1005 p->p_flag |= P_BREAKTSLEEP;
1006 goto out;
1010 * Otherwise the process is running
1012 * SRUN, SIDL, SZOMB do nothing with the signal,
1013 * other than kicking ourselves if we are running.
1014 * It will either never be noticed, or noticed very soon.
1016 * Note that p_thread may be NULL or may not be completely
1017 * initialized if the process is in the SIDL or SZOMB state.
1019 * For SMP we may have to forward the request to another cpu.
1020 * YYY the MP lock prevents the target process from moving
1021 * to another cpu, see kern/kern_switch.c
1023 * If the target thread is waiting on its message port,
1024 * wakeup the target thread so it can check (or ignore)
1025 * the new signal. YYY needs cleanup.
1027 if (lp == lwkt_preempted_proc()) {
1028 signotify();
1029 } else if (p->p_stat == SRUN) {
1030 struct thread *td = p->p_thread;
1032 KASSERT(td != NULL,
1033 ("pid %d NULL p_thread stat %d flags %08x",
1034 p->p_pid, p->p_stat, p->p_flag));
1036 #ifdef SMP
1037 if (td->td_gd != mycpu)
1038 lwkt_send_ipiq(td->td_gd, signotify_remote, lp);
1039 else
1040 #endif
1041 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
1042 lwkt_schedule(td);
1044 goto out;
1045 /*NOTREACHED*/
1046 run:
1048 * Make runnable and break out of any tsleep as well.
1050 p->p_flag |= P_BREAKTSLEEP;
1051 run_no_break:
1052 setrunnable(p);
1053 out:
1054 crit_exit();
1057 #ifdef SMP
1060 * This function is called via an IPI. We will be in a critical section but
1061 * the MP lock will NOT be held. Also note that by the time the ipi message
1062 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1064 static void
1065 signotify_remote(void *arg)
1067 struct lwp *lp = arg;
1069 if (lp == lwkt_preempted_proc()) {
1070 signotify();
1071 } else {
1072 struct thread *td = lp->lwp_thread;
1073 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
1074 lwkt_schedule(td);
1078 #endif
1080 static int
1081 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1083 sigset_t savedmask, set;
1084 struct proc *p = curproc;
1085 int error, sig, hz, timevalid = 0;
1086 struct timespec rts, ets, ts;
1087 struct timeval tv;
1089 error = 0;
1090 sig = 0;
1091 SIG_CANTMASK(waitset);
1092 savedmask = p->p_sigmask;
1094 if (timeout) {
1095 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1096 timeout->tv_nsec < 1000000000) {
1097 timevalid = 1;
1098 getnanouptime(&rts);
1099 ets = rts;
1100 timespecadd(&ets, timeout);
1104 for (;;) {
1105 set = p->p_siglist;
1106 SIGSETAND(set, waitset);
1107 if ((sig = sig_ffs(&set)) != 0) {
1108 SIGFILLSET(p->p_sigmask);
1109 SIGDELSET(p->p_sigmask, sig);
1110 SIG_CANTMASK(p->p_sigmask);
1111 sig = issignal(p);
1113 * It may be a STOP signal, in the case, issignal
1114 * returns 0, because we may stop there, and new
1115 * signal can come in, we should restart if we got
1116 * nothing.
1118 if (sig == 0)
1119 continue;
1120 else
1121 break;
1125 * Previous checking got nothing, and we retried but still
1126 * got nothing, we should return the error status.
1128 if (error)
1129 break;
1132 * POSIX says this must be checked after looking for pending
1133 * signals.
1135 if (timeout) {
1136 if (!timevalid) {
1137 error = EINVAL;
1138 break;
1140 getnanouptime(&rts);
1141 if (timespeccmp(&rts, &ets, >=)) {
1142 error = EAGAIN;
1143 break;
1145 ts = ets;
1146 timespecsub(&ts, &rts);
1147 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1148 hz = tvtohz_high(&tv);
1149 } else
1150 hz = 0;
1152 p->p_sigmask = savedmask;
1153 SIGSETNAND(p->p_sigmask, waitset);
1154 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1155 if (timeout) {
1156 if (error == ERESTART) {
1157 /* can not restart a timeout wait. */
1158 error = EINTR;
1159 } else if (error == EAGAIN) {
1160 /* will calculate timeout by ourself. */
1161 error = 0;
1164 /* Retry ... */
1167 p->p_sigmask = savedmask;
1168 if (sig) {
1169 error = 0;
1170 bzero(info, sizeof(*info));
1171 info->si_signo = sig;
1172 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1174 if (sig == SIGKILL)
1175 sigexit(p, sig);
1177 return (error);
1181 sys_sigtimedwait(struct sigtimedwait_args *uap)
1183 struct timespec ts;
1184 struct timespec *timeout;
1185 sigset_t set;
1186 siginfo_t info;
1187 int error;
1189 if (uap->timeout) {
1190 error = copyin(uap->timeout, &ts, sizeof(ts));
1191 if (error)
1192 return (error);
1193 timeout = &ts;
1194 } else {
1195 timeout = NULL;
1197 error = copyin(uap->set, &set, sizeof(set));
1198 if (error)
1199 return (error);
1200 error = kern_sigtimedwait(set, &info, timeout);
1201 if (error)
1202 return (error);
1203 if (uap->info)
1204 error = copyout(&info, uap->info, sizeof(info));
1205 /* Repost if we got an error. */
1206 if (error)
1207 ksignal(curproc, info.si_signo);
1208 else
1209 uap->sysmsg_result = info.si_signo;
1210 return (error);
1214 sys_sigwaitinfo(struct sigwaitinfo_args *uap)
1216 siginfo_t info;
1217 sigset_t set;
1218 int error;
1220 error = copyin(uap->set, &set, sizeof(set));
1221 if (error)
1222 return (error);
1223 error = kern_sigtimedwait(set, &info, NULL);
1224 if (error)
1225 return (error);
1226 if (uap->info)
1227 error = copyout(&info, uap->info, sizeof(info));
1228 /* Repost if we got an error. */
1229 if (error)
1230 ksignal(curproc, info.si_signo);
1231 else
1232 uap->sysmsg_result = info.si_signo;
1233 return (error);
1237 * If the current process has received a signal that would interrupt a
1238 * system call, return EINTR or ERESTART as appropriate.
1241 iscaught(struct proc *p)
1243 int sig;
1245 if (p) {
1246 if ((sig = CURSIG(p)) != 0) {
1247 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1248 return (EINTR);
1249 return (ERESTART);
1252 return(EWOULDBLOCK);
1256 * If the current process has received a signal (should be caught or cause
1257 * termination, should interrupt current syscall), return the signal number.
1258 * Stop signals with default action are processed immediately, then cleared;
1259 * they aren't returned. This is checked after each entry to the system for
1260 * a syscall or trap (though this can usually be done without calling issignal
1261 * by checking the pending signal masks in the CURSIG macro.) The normal call
1262 * sequence is
1264 * This routine is called via CURSIG/__cursig and the MP lock might not be
1265 * held. Obtain the MP lock for the duration of the operation.
1267 * while (sig = CURSIG(curproc))
1268 * postsig(sig);
1271 issignal(struct proc *p)
1273 sigset_t mask;
1274 int sig, prop;
1276 get_mplock();
1277 for (;;) {
1278 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1280 mask = p->p_siglist;
1281 SIGSETNAND(mask, p->p_sigmask);
1282 if (p->p_flag & P_PPWAIT)
1283 SIG_STOPSIGMASK(mask);
1284 if (!SIGNOTEMPTY(mask)) { /* no signal to send */
1285 rel_mplock();
1286 return (0);
1288 sig = sig_ffs(&mask);
1290 STOPEVENT(p, S_SIG, sig);
1293 * We should see pending but ignored signals
1294 * only if P_TRACED was on when they were posted.
1296 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1297 SIGDELSET(p->p_siglist, sig);
1298 continue;
1300 if ((p->p_flag & P_TRACED) && (p->p_flag & P_PPWAIT) == 0) {
1302 * If traced, always stop, and stay stopped until
1303 * released by the parent.
1305 * NOTE: P_STOPPED may get cleared during the loop,
1306 * but we do not re-notify the parent if we have
1307 * to loop several times waiting for the parent
1308 * to let us continue.
1310 p->p_xstat = sig;
1311 p->p_flag |= P_STOPPED;
1312 p->p_flag &= ~P_WAITED;
1313 ksignal(p->p_pptr, SIGCHLD);
1314 do {
1315 tstop(p);
1316 } while (!trace_req(p) && (p->p_flag & P_TRACED));
1317 p->p_flag &= ~P_STOPPED;
1320 * If parent wants us to take the signal,
1321 * then it will leave it in p->p_xstat;
1322 * otherwise we just look for signals again.
1324 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1325 sig = p->p_xstat;
1326 if (sig == 0)
1327 continue;
1330 * Put the new signal into p_siglist. If the
1331 * signal is being masked, look for other signals.
1333 SIGADDSET(p->p_siglist, sig);
1334 if (SIGISMEMBER(p->p_sigmask, sig))
1335 continue;
1338 * If the traced bit got turned off, go back up
1339 * to the top to rescan signals. This ensures
1340 * that p_sig* and ps_sigact are consistent.
1342 if ((p->p_flag & P_TRACED) == 0)
1343 continue;
1346 prop = sigprop(sig);
1349 * Decide whether the signal should be returned.
1350 * Return the signal's number, or fall through
1351 * to clear it from the pending mask.
1353 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1354 case (int)SIG_DFL:
1356 * Don't take default actions on system processes.
1358 if (p->p_pid <= 1) {
1359 #ifdef DIAGNOSTIC
1361 * Are you sure you want to ignore SIGSEGV
1362 * in init? XXX
1364 printf("Process (pid %lu) got signal %d\n",
1365 (u_long)p->p_pid, sig);
1366 #endif
1367 break; /* == ignore */
1371 * Handle the in-kernel checkpoint action
1373 if (prop & SA_CKPT) {
1374 checkpoint_signal_handler(p);
1375 break;
1379 * If there is a pending stop signal to process
1380 * with default action, stop here,
1381 * then clear the signal. However,
1382 * if process is member of an orphaned
1383 * process group, ignore tty stop signals.
1385 if (prop & SA_STOP) {
1386 if (p->p_flag & P_TRACED ||
1387 (p->p_pgrp->pg_jobc == 0 &&
1388 prop & SA_TTYSTOP))
1389 break; /* == ignore */
1390 p->p_xstat = sig;
1391 p->p_flag |= P_STOPPED;
1392 p->p_flag &= ~P_WAITED;
1394 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1395 ksignal(p->p_pptr, SIGCHLD);
1396 while (p->p_flag & P_STOPPED) {
1397 tstop(p);
1399 break;
1400 } else if (prop & SA_IGNORE) {
1402 * Except for SIGCONT, shouldn't get here.
1403 * Default action is to ignore; drop it.
1405 break; /* == ignore */
1406 } else {
1407 rel_mplock();
1408 return (sig);
1411 /*NOTREACHED*/
1413 case (int)SIG_IGN:
1415 * Masking above should prevent us ever trying
1416 * to take action on an ignored signal other
1417 * than SIGCONT, unless process is traced.
1419 if ((prop & SA_CONT) == 0 &&
1420 (p->p_flag & P_TRACED) == 0)
1421 printf("issignal\n");
1422 break; /* == ignore */
1424 default:
1426 * This signal has an action, let
1427 * postsig() process it.
1429 rel_mplock();
1430 return (sig);
1432 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1434 /* NOTREACHED */
1438 * Take the action for the specified signal
1439 * from the current set of pending signals.
1441 void
1442 postsig(int sig)
1444 struct thread *td = curthread;
1445 struct proc *p = td->td_proc;
1446 struct sigacts *ps = p->p_sigacts;
1447 sig_t action;
1448 sigset_t returnmask;
1449 int code;
1451 KASSERT(sig != 0, ("postsig"));
1453 SIGDELSET(p->p_siglist, sig);
1454 action = ps->ps_sigact[_SIG_IDX(sig)];
1455 #ifdef KTRACE
1456 if (KTRPOINT(td, KTR_PSIG))
1457 ktrpsig(p, sig, action, p->p_flag & P_OLDMASK ?
1458 &p->p_oldsigmask : &p->p_sigmask, 0);
1459 #endif
1460 STOPEVENT(p, S_SIG, sig);
1462 if (action == SIG_DFL) {
1464 * Default action, where the default is to kill
1465 * the process. (Other cases were ignored above.)
1467 sigexit(p, sig);
1468 /* NOTREACHED */
1469 } else {
1471 * If we get here, the signal must be caught.
1473 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1474 ("postsig action"));
1476 * Set the new mask value and also defer further
1477 * occurrences of this signal.
1479 * Special case: user has done a sigsuspend. Here the
1480 * current mask is not of interest, but rather the
1481 * mask from before the sigsuspend is what we want
1482 * restored after the signal processing is completed.
1484 crit_enter();
1485 if (p->p_flag & P_OLDMASK) {
1486 returnmask = p->p_oldsigmask;
1487 p->p_flag &= ~P_OLDMASK;
1488 } else {
1489 returnmask = p->p_sigmask;
1492 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1493 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1494 SIGADDSET(p->p_sigmask, sig);
1496 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1498 * See kern_sigaction() for origin of this code.
1500 SIGDELSET(p->p_sigcatch, sig);
1501 if (sig != SIGCONT &&
1502 sigprop(sig) & SA_IGNORE)
1503 SIGADDSET(p->p_sigignore, sig);
1504 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1506 crit_exit();
1507 p->p_stats->p_ru.ru_nsignals++;
1508 if (p->p_sig != sig) {
1509 code = 0;
1510 } else {
1511 code = p->p_code;
1512 p->p_code = 0;
1513 p->p_sig = 0;
1515 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1520 * Kill the current process for stated reason.
1522 void
1523 killproc(struct proc *p, char *why)
1525 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1526 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1527 ksignal(p, SIGKILL);
1531 * Force the current process to exit with the specified signal, dumping core
1532 * if appropriate. We bypass the normal tests for masked and caught signals,
1533 * allowing unrecoverable failures to terminate the process without changing
1534 * signal state. Mark the accounting record with the signal termination.
1535 * If dumping core, save the signal number for the debugger. Calls exit and
1536 * does not return.
1538 void
1539 sigexit(struct proc *p, int sig)
1541 p->p_acflag |= AXSIG;
1542 if (sigprop(sig) & SA_CORE) {
1543 p->p_sig = sig;
1545 * Log signals which would cause core dumps
1546 * (Log as LOG_INFO to appease those who don't want
1547 * these messages.)
1548 * XXX : Todo, as well as euid, write out ruid too
1550 if (coredump(p) == 0)
1551 sig |= WCOREFLAG;
1552 if (kern_logsigexit)
1553 log(LOG_INFO,
1554 "pid %d (%s), uid %d: exited on signal %d%s\n",
1555 p->p_pid, p->p_comm,
1556 p->p_ucred ? p->p_ucred->cr_uid : -1,
1557 sig &~ WCOREFLAG,
1558 sig & WCOREFLAG ? " (core dumped)" : "");
1560 exit1(W_EXITCODE(0, sig));
1561 /* NOTREACHED */
1564 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1565 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1566 sizeof(corefilename), "process corefile name format string");
1569 * expand_name(name, uid, pid)
1570 * Expand the name described in corefilename, using name, uid, and pid.
1571 * corefilename is a printf-like string, with three format specifiers:
1572 * %N name of process ("name")
1573 * %P process id (pid)
1574 * %U user id (uid)
1575 * For example, "%N.core" is the default; they can be disabled completely
1576 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1577 * This is controlled by the sysctl variable kern.corefile (see above).
1580 static char *
1581 expand_name(const char *name, uid_t uid, pid_t pid)
1583 char *temp;
1584 char buf[11]; /* Buffer for pid/uid -- max 4B */
1585 int i, n;
1586 char *format = corefilename;
1587 size_t namelen;
1589 temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1590 if (temp == NULL)
1591 return NULL;
1592 namelen = strlen(name);
1593 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1594 int l;
1595 switch (format[i]) {
1596 case '%': /* Format character */
1597 i++;
1598 switch (format[i]) {
1599 case '%':
1600 temp[n++] = '%';
1601 break;
1602 case 'N': /* process name */
1603 if ((n + namelen) > MAXPATHLEN) {
1604 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1605 pid, name, uid, temp, name);
1606 kfree(temp, M_TEMP);
1607 return NULL;
1609 memcpy(temp+n, name, namelen);
1610 n += namelen;
1611 break;
1612 case 'P': /* process id */
1613 l = ksprintf(buf, "%u", pid);
1614 if ((n + l) > MAXPATHLEN) {
1615 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1616 pid, name, uid, temp, name);
1617 kfree(temp, M_TEMP);
1618 return NULL;
1620 memcpy(temp+n, buf, l);
1621 n += l;
1622 break;
1623 case 'U': /* user id */
1624 l = ksprintf(buf, "%u", uid);
1625 if ((n + l) > MAXPATHLEN) {
1626 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1627 pid, name, uid, temp, name);
1628 kfree(temp, M_TEMP);
1629 return NULL;
1631 memcpy(temp+n, buf, l);
1632 n += l;
1633 break;
1634 default:
1635 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1637 break;
1638 default:
1639 temp[n++] = format[i];
1642 temp[n] = '\0';
1643 return temp;
1647 * Dump a process' core. The main routine does some
1648 * policy checking, and creates the name of the coredump;
1649 * then it passes on a vnode and a size limit to the process-specific
1650 * coredump routine if there is one; if there _is not_ one, it returns
1651 * ENOSYS; otherwise it returns the error from the process-specific routine.
1654 static int
1655 coredump(struct proc *p)
1657 struct vnode *vp;
1658 struct ucred *cred = p->p_ucred;
1659 struct flock lf;
1660 struct nlookupdata nd;
1661 struct vattr vattr;
1662 int error, error1;
1663 char *name; /* name of corefile */
1664 off_t limit;
1666 STOPEVENT(p, S_CORE, 0);
1668 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1669 return (EFAULT);
1672 * Note that the bulk of limit checking is done after
1673 * the corefile is created. The exception is if the limit
1674 * for corefiles is 0, in which case we don't bother
1675 * creating the corefile at all. This layout means that
1676 * a corefile is truncated instead of not being created,
1677 * if it is larger than the limit.
1679 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1680 if (limit == 0)
1681 return EFBIG;
1683 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1684 if (name == NULL)
1685 return (EINVAL);
1686 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
1687 if (error == 0)
1688 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1689 kfree(name, M_TEMP);
1690 if (error) {
1691 nlookup_done(&nd);
1692 return (error);
1694 vp = nd.nl_open_vp;
1695 nd.nl_open_vp = NULL;
1696 nlookup_done(&nd);
1698 vn_unlock(vp);
1699 lf.l_whence = SEEK_SET;
1700 lf.l_start = 0;
1701 lf.l_len = 0;
1702 lf.l_type = F_WRLCK;
1703 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, 0);
1704 if (error)
1705 goto out2;
1707 /* Don't dump to non-regular files or files with links. */
1708 if (vp->v_type != VREG ||
1709 VOP_GETATTR(vp, &vattr) || vattr.va_nlink != 1) {
1710 error = EFAULT;
1711 goto out1;
1714 VATTR_NULL(&vattr);
1715 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1716 vattr.va_size = 0;
1717 VOP_SETATTR(vp, &vattr, cred);
1718 p->p_acflag |= ACORE;
1719 vn_unlock(vp);
1721 error = p->p_sysent->sv_coredump ?
1722 p->p_sysent->sv_coredump(p, vp, limit) : ENOSYS;
1724 out1:
1725 lf.l_type = F_UNLCK;
1726 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, 0);
1727 out2:
1728 error1 = vn_close(vp, FWRITE);
1729 if (error == 0)
1730 error = error1;
1731 return (error);
1735 * Nonexistent system call-- signal process (may want to handle it).
1736 * Flag error in case process won't see signal immediately (blocked or ignored).
1738 /* ARGSUSED */
1740 sys_nosys(struct nosys_args *args)
1742 ksignal(curproc, SIGSYS);
1743 return (EINVAL);
1747 * Send a SIGIO or SIGURG signal to a process or process group using
1748 * stored credentials rather than those of the current process.
1750 void
1751 pgsigio(struct sigio *sigio, int sig, int checkctty)
1753 if (sigio == NULL)
1754 return;
1756 if (sigio->sio_pgid > 0) {
1757 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1758 sigio->sio_proc))
1759 ksignal(sigio->sio_proc, sig);
1760 } else if (sigio->sio_pgid < 0) {
1761 struct proc *p;
1763 lockmgr(&sigio->sio_pgrp->pg_lock, LK_EXCLUSIVE);
1764 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
1765 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1766 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1767 ksignal(p, sig);
1769 lockmgr(&sigio->sio_pgrp->pg_lock, LK_RELEASE);
1773 static int
1774 filt_sigattach(struct knote *kn)
1776 struct proc *p = curproc;
1778 kn->kn_ptr.p_proc = p;
1779 kn->kn_flags |= EV_CLEAR; /* automatically set */
1781 /* XXX lock the proc here while adding to the list? */
1782 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1784 return (0);
1787 static void
1788 filt_sigdetach(struct knote *kn)
1790 struct proc *p = kn->kn_ptr.p_proc;
1792 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1796 * signal knotes are shared with proc knotes, so we apply a mask to
1797 * the hint in order to differentiate them from process hints. This
1798 * could be avoided by using a signal-specific knote list, but probably
1799 * isn't worth the trouble.
1801 static int
1802 filt_signal(struct knote *kn, long hint)
1804 if (hint & NOTE_SIGNAL) {
1805 hint &= ~NOTE_SIGNAL;
1807 if (kn->kn_id == hint)
1808 kn->kn_data++;
1810 return (kn->kn_data != 0);