Add strnvis, which is orthogonal to strvisx by bounding dst, not src.
[dragonfly/netmp.git] / sys / kern / kern_sig.c
blob087cd4a80d79e5a986bdb50fb31e1600109a3965
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.37 2005/06/06 15:02:28 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/wait.h>
60 #include <sys/ktrace.h>
61 #include <sys/syslog.h>
62 #include <sys/stat.h>
63 #include <sys/sysent.h>
64 #include <sys/sysctl.h>
65 #include <sys/malloc.h>
66 #include <sys/unistd.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/thread2.h>
71 #include <machine/ipl.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 static void stop(struct proc *);
81 #ifdef SMP
82 static void signotify_remote(void *arg);
83 #endif
84 static int kern_sigtimedwait(sigset_t set, siginfo_t *info,
85 struct timespec *timeout);
87 static int filt_sigattach(struct knote *kn);
88 static void filt_sigdetach(struct knote *kn);
89 static int filt_signal(struct knote *kn, long hint);
91 struct filterops sig_filtops =
92 { 0, filt_sigattach, filt_sigdetach, filt_signal };
94 static int kern_logsigexit = 1;
95 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
96 &kern_logsigexit, 0,
97 "Log processes quitting on abnormal signals to syslog(3)");
100 * Can process p, with pcred pc, send the signal sig to process q?
102 #define CANSIGNAL(q, sig) \
103 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
104 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
107 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
109 #define CANSIGIO(ruid, uc, q) \
110 ((uc)->cr_uid == 0 || \
111 (ruid) == (q)->p_ucred->cr_ruid || \
112 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
113 (ruid) == (q)->p_ucred->cr_uid || \
114 (uc)->cr_uid == (q)->p_ucred->cr_uid)
116 int sugid_coredump;
117 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
118 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120 static int do_coredump = 1;
121 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
122 &do_coredump, 0, "Enable/Disable coredumps");
125 * Signal properties and actions.
126 * The array below categorizes the signals and their default actions
127 * according to the following properties:
129 #define SA_KILL 0x01 /* terminates process by default */
130 #define SA_CORE 0x02 /* ditto and coredumps */
131 #define SA_STOP 0x04 /* suspend process */
132 #define SA_TTYSTOP 0x08 /* ditto, from tty */
133 #define SA_IGNORE 0x10 /* ignore by default */
134 #define SA_CONT 0x20 /* continue if suspended */
135 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
136 #define SA_CKPT 0x80 /* checkpoint process */
139 static int sigproptbl[NSIG] = {
140 SA_KILL, /* SIGHUP */
141 SA_KILL, /* SIGINT */
142 SA_KILL|SA_CORE, /* SIGQUIT */
143 SA_KILL|SA_CORE, /* SIGILL */
144 SA_KILL|SA_CORE, /* SIGTRAP */
145 SA_KILL|SA_CORE, /* SIGABRT */
146 SA_KILL|SA_CORE, /* SIGEMT */
147 SA_KILL|SA_CORE, /* SIGFPE */
148 SA_KILL, /* SIGKILL */
149 SA_KILL|SA_CORE, /* SIGBUS */
150 SA_KILL|SA_CORE, /* SIGSEGV */
151 SA_KILL|SA_CORE, /* SIGSYS */
152 SA_KILL, /* SIGPIPE */
153 SA_KILL, /* SIGALRM */
154 SA_KILL, /* SIGTERM */
155 SA_IGNORE, /* SIGURG */
156 SA_STOP, /* SIGSTOP */
157 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
158 SA_IGNORE|SA_CONT, /* SIGCONT */
159 SA_IGNORE, /* SIGCHLD */
160 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
161 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
162 SA_IGNORE, /* SIGIO */
163 SA_KILL, /* SIGXCPU */
164 SA_KILL, /* SIGXFSZ */
165 SA_KILL, /* SIGVTALRM */
166 SA_KILL, /* SIGPROF */
167 SA_IGNORE, /* SIGWINCH */
168 SA_IGNORE, /* SIGINFO */
169 SA_KILL, /* SIGUSR1 */
170 SA_KILL, /* SIGUSR2 */
171 SA_IGNORE, /* SIGTHR */
172 SA_CKPT, /* SIGCKPT */
173 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */
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,
203 SA_IGNORE,
207 static __inline int
208 sigprop(int sig)
211 if (sig > 0 && sig < NSIG)
212 return (sigproptbl[_SIG_IDX(sig)]);
213 return (0);
216 static __inline int
217 sig_ffs(sigset_t *set)
219 int i;
221 for (i = 0; i < _SIG_WORDS; i++)
222 if (set->__bits[i])
223 return (ffs(set->__bits[i]) + (i * 32));
224 return (0);
228 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
230 struct thread *td = curthread;
231 struct proc *p = td->td_proc;
232 struct sigacts *ps = p->p_sigacts;
234 if (sig <= 0 || sig > _SIG_MAXSIG)
235 return (EINVAL);
237 if (oact) {
238 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
239 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
240 oact->sa_flags = 0;
241 if (SIGISMEMBER(ps->ps_sigonstack, sig))
242 oact->sa_flags |= SA_ONSTACK;
243 if (!SIGISMEMBER(ps->ps_sigintr, sig))
244 oact->sa_flags |= SA_RESTART;
245 if (SIGISMEMBER(ps->ps_sigreset, sig))
246 oact->sa_flags |= SA_RESETHAND;
247 if (SIGISMEMBER(ps->ps_signodefer, sig))
248 oact->sa_flags |= SA_NODEFER;
249 if (SIGISMEMBER(ps->ps_siginfo, sig))
250 oact->sa_flags |= SA_SIGINFO;
251 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
252 oact->sa_flags |= SA_NOCLDSTOP;
253 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
254 oact->sa_flags |= SA_NOCLDWAIT;
256 if (act) {
257 if ((sig == SIGKILL || sig == SIGSTOP) &&
258 act->sa_handler != SIG_DFL)
259 return (EINVAL);
262 * Change setting atomically.
264 crit_enter();
266 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
267 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
268 if (act->sa_flags & SA_SIGINFO) {
269 ps->ps_sigact[_SIG_IDX(sig)] =
270 (__sighandler_t *)act->sa_sigaction;
271 SIGADDSET(ps->ps_siginfo, sig);
272 } else {
273 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
274 SIGDELSET(ps->ps_siginfo, sig);
276 if (!(act->sa_flags & SA_RESTART))
277 SIGADDSET(ps->ps_sigintr, sig);
278 else
279 SIGDELSET(ps->ps_sigintr, sig);
280 if (act->sa_flags & SA_ONSTACK)
281 SIGADDSET(ps->ps_sigonstack, sig);
282 else
283 SIGDELSET(ps->ps_sigonstack, sig);
284 if (act->sa_flags & SA_RESETHAND)
285 SIGADDSET(ps->ps_sigreset, sig);
286 else
287 SIGDELSET(ps->ps_sigreset, sig);
288 if (act->sa_flags & SA_NODEFER)
289 SIGADDSET(ps->ps_signodefer, sig);
290 else
291 SIGDELSET(ps->ps_signodefer, sig);
292 if (sig == SIGCHLD) {
293 if (act->sa_flags & SA_NOCLDSTOP)
294 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
295 else
296 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
297 if (act->sa_flags & SA_NOCLDWAIT) {
299 * Paranoia: since SA_NOCLDWAIT is implemented
300 * by reparenting the dying child to PID 1 (and
301 * trust it to reap the zombie), PID 1 itself
302 * is forbidden to set SA_NOCLDWAIT.
304 if (p->p_pid == 1)
305 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
306 else
307 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
308 } else {
309 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
313 * Set bit in p_sigignore for signals that are set to SIG_IGN,
314 * and for signals set to SIG_DFL where the default is to
315 * ignore. However, don't put SIGCONT in p_sigignore, as we
316 * have to restart the process.
318 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
319 (sigprop(sig) & SA_IGNORE &&
320 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
321 /* never to be seen again */
322 SIGDELSET(p->p_siglist, sig);
323 if (sig != SIGCONT)
324 /* easier in psignal */
325 SIGADDSET(p->p_sigignore, sig);
326 SIGDELSET(p->p_sigcatch, sig);
327 } else {
328 SIGDELSET(p->p_sigignore, sig);
329 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
330 SIGDELSET(p->p_sigcatch, sig);
331 else
332 SIGADDSET(p->p_sigcatch, sig);
335 crit_exit();
337 return (0);
341 sigaction(struct sigaction_args *uap)
343 struct sigaction act, oact;
344 struct sigaction *actp, *oactp;
345 int error;
347 actp = (uap->act != NULL) ? &act : NULL;
348 oactp = (uap->oact != NULL) ? &oact : NULL;
349 if (actp) {
350 error = copyin(uap->act, actp, sizeof(act));
351 if (error)
352 return (error);
354 error = kern_sigaction(uap->sig, actp, oactp);
355 if (oactp && !error) {
356 error = copyout(oactp, uap->oact, sizeof(oact));
358 return (error);
362 * Initialize signal state for process 0;
363 * set to ignore signals that are ignored by default.
365 void
366 siginit(struct proc *p)
368 int i;
370 for (i = 1; i <= NSIG; i++)
371 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
372 SIGADDSET(p->p_sigignore, i);
376 * Reset signals for an exec of the specified process.
378 void
379 execsigs(struct proc *p)
381 struct sigacts *ps = p->p_sigacts;
382 int sig;
385 * Reset caught signals. Held signals remain held
386 * through p_sigmask (unless they were caught,
387 * and are now ignored by default).
389 while (SIGNOTEMPTY(p->p_sigcatch)) {
390 sig = sig_ffs(&p->p_sigcatch);
391 SIGDELSET(p->p_sigcatch, sig);
392 if (sigprop(sig) & SA_IGNORE) {
393 if (sig != SIGCONT)
394 SIGADDSET(p->p_sigignore, sig);
395 SIGDELSET(p->p_siglist, sig);
397 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
400 * Reset stack state to the user stack.
401 * Clear set of signals caught on the signal stack.
403 p->p_sigstk.ss_flags = SS_DISABLE;
404 p->p_sigstk.ss_size = 0;
405 p->p_sigstk.ss_sp = 0;
406 p->p_flag &= ~P_ALTSTACK;
408 * Reset no zombies if child dies flag as Solaris does.
410 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
414 * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
416 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
417 * p == curproc.
420 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
422 struct thread *td = curthread;
423 struct proc *p = td->td_proc;
424 int error;
426 if (oset != NULL)
427 *oset = p->p_sigmask;
429 error = 0;
430 if (set != NULL) {
431 switch (how) {
432 case SIG_BLOCK:
433 SIG_CANTMASK(*set);
434 SIGSETOR(p->p_sigmask, *set);
435 break;
436 case SIG_UNBLOCK:
437 SIGSETNAND(p->p_sigmask, *set);
438 break;
439 case SIG_SETMASK:
440 SIG_CANTMASK(*set);
441 p->p_sigmask = *set;
442 break;
443 default:
444 error = EINVAL;
445 break;
448 return (error);
452 * sigprocmask() - MP SAFE
455 sigprocmask(struct sigprocmask_args *uap)
457 sigset_t set, oset;
458 sigset_t *setp, *osetp;
459 int error;
461 setp = (uap->set != NULL) ? &set : NULL;
462 osetp = (uap->oset != NULL) ? &oset : NULL;
463 if (setp) {
464 error = copyin(uap->set, setp, sizeof(set));
465 if (error)
466 return (error);
468 error = kern_sigprocmask(uap->how, setp, osetp);
469 if (osetp && !error) {
470 error = copyout(osetp, uap->oset, sizeof(oset));
472 return (error);
476 kern_sigpending(struct __sigset *set)
478 struct thread *td = curthread;
479 struct proc *p = td->td_proc;
481 *set = p->p_siglist;
483 return (0);
487 sigpending(struct sigpending_args *uap)
489 sigset_t set;
490 int error;
492 error = kern_sigpending(&set);
494 if (error == 0)
495 error = copyout(&set, uap->set, sizeof(set));
496 return (error);
500 * Suspend process until signal, providing mask to be set
501 * in the meantime.
504 kern_sigsuspend(struct __sigset *set)
506 struct thread *td = curthread;
507 struct proc *p = td->td_proc;
508 struct sigacts *ps = p->p_sigacts;
511 * When returning from sigsuspend, we want
512 * the old mask to be restored after the
513 * signal handler has finished. Thus, we
514 * save it here and mark the sigacts structure
515 * to indicate this.
517 p->p_oldsigmask = p->p_sigmask;
518 p->p_flag |= P_OLDMASK;
520 SIG_CANTMASK(*set);
521 p->p_sigmask = *set;
522 while (tsleep(ps, PCATCH, "pause", 0) == 0)
523 /* void */;
524 /* always return EINTR rather than ERESTART... */
525 return (EINTR);
529 * Note nonstandard calling convention: libc stub passes mask, not
530 * pointer, to save a copyin.
533 sigsuspend(struct sigsuspend_args *uap)
535 sigset_t mask;
536 int error;
538 error = copyin(uap->sigmask, &mask, sizeof(mask));
539 if (error)
540 return (error);
542 error = kern_sigsuspend(&mask);
544 return (error);
548 kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss)
550 struct thread *td = curthread;
551 struct proc *p = td->td_proc;
553 if ((p->p_flag & P_ALTSTACK) == 0)
554 p->p_sigstk.ss_flags |= SS_DISABLE;
556 if (oss)
557 *oss = p->p_sigstk;
559 if (ss) {
560 if (ss->ss_flags & SS_DISABLE) {
561 if (p->p_sigstk.ss_flags & SS_ONSTACK)
562 return (EINVAL);
563 p->p_flag &= ~P_ALTSTACK;
564 p->p_sigstk.ss_flags = ss->ss_flags;
565 } else {
566 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
567 return (ENOMEM);
568 p->p_flag |= P_ALTSTACK;
569 p->p_sigstk = *ss;
573 return (0);
577 sigaltstack(struct sigaltstack_args *uap)
579 stack_t ss, oss;
580 int error;
582 if (uap->ss) {
583 error = copyin(uap->ss, &ss, sizeof(ss));
584 if (error)
585 return (error);
588 error = kern_sigaltstack(uap->ss ? &ss : NULL,
589 uap->oss ? &oss : NULL);
591 if (error == 0 && uap->oss)
592 error = copyout(&oss, uap->oss, sizeof(*uap->oss));
593 return (error);
597 * Common code for kill process group/broadcast kill.
598 * cp is calling process.
600 static int
601 killpg(int sig, int pgid, int all)
603 struct proc *cp = curproc;
604 struct proc *p;
605 struct pgrp *pgrp;
606 int nfound = 0;
608 if (all) {
610 * broadcast
612 FOREACH_PROC_IN_SYSTEM(p) {
613 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
614 p == cp || !CANSIGNAL(p, sig))
615 continue;
616 nfound++;
617 if (sig)
618 psignal(p, sig);
620 } else {
621 if (pgid == 0) {
623 * zero pgid means send to my process group.
625 pgrp = cp->p_pgrp;
626 } else {
627 pgrp = pgfind(pgid);
628 if (pgrp == NULL)
629 return (ESRCH);
631 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
632 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
633 p->p_stat == SZOMB ||
634 !CANSIGNAL(p, sig))
635 continue;
636 nfound++;
637 if (sig)
638 psignal(p, sig);
641 return (nfound ? 0 : ESRCH);
645 kern_kill(int sig, int pid)
647 struct thread *td = curthread;
648 struct proc *p = td->td_proc;
650 if ((u_int)sig > _SIG_MAXSIG)
651 return (EINVAL);
652 if (pid > 0) {
653 /* kill single process */
654 if ((p = pfind(pid)) == NULL)
655 return (ESRCH);
656 if (!CANSIGNAL(p, sig))
657 return (EPERM);
658 if (sig)
659 psignal(p, sig);
660 return (0);
662 switch (pid) {
663 case -1: /* broadcast signal */
664 return (killpg(sig, 0, 1));
665 case 0: /* signal own process group */
666 return (killpg(sig, 0, 0));
667 default: /* negative explicit process group */
668 return (killpg(sig, -pid, 0));
670 /* NOTREACHED */
674 kill(struct kill_args *uap)
676 int error;
678 error = kern_kill(uap->signum, uap->pid);
680 return (error);
684 * Send a signal to a process group.
686 void
687 gsignal(int pgid, int sig)
689 struct pgrp *pgrp;
691 if (pgid && (pgrp = pgfind(pgid)))
692 pgsignal(pgrp, sig, 0);
696 * Send a signal to a process group. If checktty is 1,
697 * limit to members which have a controlling terminal.
699 void
700 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
702 struct proc *p;
704 if (pgrp)
705 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
706 if (checkctty == 0 || p->p_flag & P_CONTROLT)
707 psignal(p, sig);
711 * Send a signal caused by a trap to the current process.
712 * If it will be caught immediately, deliver it with correct code.
713 * Otherwise, post it normally.
715 void
716 trapsignal(struct proc *p, int sig, u_long code)
718 struct sigacts *ps = p->p_sigacts;
720 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
721 !SIGISMEMBER(p->p_sigmask, sig)) {
722 p->p_stats->p_ru.ru_nsignals++;
723 #ifdef KTRACE
724 if (KTRPOINT(p->p_thread, KTR_PSIG))
725 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
726 &p->p_sigmask, code);
727 #endif
728 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
729 &p->p_sigmask, code);
730 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
731 if (!SIGISMEMBER(ps->ps_signodefer, sig))
732 SIGADDSET(p->p_sigmask, sig);
733 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
735 * See kern_sigaction() for origin of this code.
737 SIGDELSET(p->p_sigcatch, sig);
738 if (sig != SIGCONT &&
739 sigprop(sig) & SA_IGNORE)
740 SIGADDSET(p->p_sigignore, sig);
741 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
743 } else {
744 p->p_code = code; /* XXX for core dump/debugger */
745 p->p_sig = sig; /* XXX to verify code */
746 psignal(p, sig);
751 * Send the signal to the process. If the signal has an action, the action
752 * is usually performed by the target process rather than the caller; we add
753 * the signal to the set of pending signals for the process.
755 * Exceptions:
756 * o When a stop signal is sent to a sleeping process that takes the
757 * default action, the process is stopped without awakening it.
758 * o SIGCONT restarts stopped processes (or puts them back to sleep)
759 * regardless of the signal action (eg, blocked or ignored).
761 * Other ignored signals are discarded immediately.
765 * temporary hack to allow checkpoint code to continue to
766 * be in a module for the moment
769 void
770 psignal(struct proc *p, int sig)
772 int prop;
773 sig_t action;
775 if (sig > _SIG_MAXSIG || sig <= 0) {
776 printf("psignal: signal %d\n", sig);
777 panic("psignal signal number");
780 crit_enter();
781 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
782 crit_exit();
784 prop = sigprop(sig);
787 * If proc is traced, always give parent a chance;
788 * if signal event is tracked by procfs, give *that*
789 * a chance, as well.
791 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
792 action = SIG_DFL;
793 } else {
795 * If the signal is being ignored,
796 * then we forget about it immediately.
797 * (Note: we don't set SIGCONT in p_sigignore,
798 * and if it is set to SIG_IGN,
799 * action will be SIG_DFL here.)
801 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
802 return;
803 if (SIGISMEMBER(p->p_sigmask, sig))
804 action = SIG_HOLD;
805 else if (SIGISMEMBER(p->p_sigcatch, sig))
806 action = SIG_CATCH;
807 else
808 action = SIG_DFL;
811 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
812 (p->p_flag & P_TRACED) == 0) {
813 p->p_nice = NZERO;
816 if (prop & SA_CONT)
817 SIG_STOPSIGMASK(p->p_siglist);
820 if (prop & SA_STOP) {
822 * If sending a tty stop signal to a member of an orphaned
823 * process group, discard the signal here if the action
824 * is default; don't stop the process below if sleeping,
825 * and don't clear any pending SIGCONT.
827 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
828 action == SIG_DFL) {
829 return;
831 SIG_CONTSIGMASK(p->p_siglist);
833 SIGADDSET(p->p_siglist, sig);
836 * Defer further processing for signals which are held,
837 * except that stopped processes must be continued by SIGCONT.
839 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
840 return;
842 crit_enter();
844 switch (p->p_stat) {
845 case SSLEEP:
847 * If process is sleeping uninterruptibly
848 * we can't interrupt the sleep... the signal will
849 * be noticed when the process returns through
850 * trap() or syscall().
852 if ((p->p_flag & P_SINTR) == 0)
853 goto out;
855 * Process is sleeping and traced... make it runnable
856 * so it can discover the signal in issignal() and stop
857 * for the parent.
859 if (p->p_flag & P_TRACED)
860 goto run;
862 * If SIGCONT is default (or ignored) and process is
863 * asleep, we are finished; the process should not
864 * be awakened.
866 if ((prop & SA_CONT) && action == SIG_DFL) {
867 SIGDELSET(p->p_siglist, sig);
868 goto out;
871 * When a sleeping process receives a stop
872 * signal, process immediately if possible.
873 * All other (caught or default) signals
874 * cause the process to run.
876 if (prop & SA_STOP) {
877 if (action != SIG_DFL)
878 goto run;
880 * If a child holding parent blocked,
881 * stopping could cause deadlock.
883 if (p->p_flag & P_PPWAIT)
884 goto out;
885 SIGDELSET(p->p_siglist, sig);
886 p->p_xstat = sig;
887 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
888 psignal(p->p_pptr, SIGCHLD);
889 stop(p);
890 goto out;
891 } else {
892 goto run;
894 /*NOTREACHED*/
895 case SSTOP:
897 * If traced process is already stopped,
898 * then no further action is necessary.
900 if (p->p_flag & P_TRACED)
901 goto out;
904 * Kill signal always sets processes running.
906 if (sig == SIGKILL)
907 goto run;
909 if (prop & SA_CONT) {
911 * If SIGCONT is default (or ignored), we continue the
912 * process but don't leave the signal in p_siglist, as
913 * it has no further action. If SIGCONT is held, we
914 * continue the process and leave the signal in
915 * p_siglist. If the process catches SIGCONT, let it
916 * handle the signal itself. If it isn't waiting on
917 * an event, then it goes back to run state.
918 * Otherwise, process goes back to sleep state.
920 if (action == SIG_DFL)
921 SIGDELSET(p->p_siglist, sig);
922 if (action == SIG_CATCH)
923 goto run;
924 if (p->p_wchan == 0)
925 goto run;
926 clrrunnable(p, SSLEEP);
927 goto out;
930 if (prop & SA_STOP) {
932 * Already stopped, don't need to stop again.
933 * (If we did the shell could get confused.)
935 SIGDELSET(p->p_siglist, sig);
936 goto out;
940 * If process is sleeping interruptibly, then simulate a
941 * wakeup so that when it is continued, it will be made
942 * runnable and can look at the signal. But don't make
943 * the process runnable, leave it stopped.
945 if (p->p_wchan && (p->p_flag & P_SINTR))
946 unsleep(p->p_thread);
947 goto out;
948 default:
950 * SRUN, SIDL, SZOMB do nothing with the signal,
951 * other than kicking ourselves if we are running.
952 * It will either never be noticed, or noticed very soon.
954 * Note that p_thread may be NULL or may not be completely
955 * initialized if the process is in the SIDL or SZOMB state.
957 * For SMP we may have to forward the request to another cpu.
958 * YYY the MP lock prevents the target process from moving
959 * to another cpu, see kern/kern_switch.c
961 * If the target thread is waiting on its message port,
962 * wakeup the target thread so it can check (or ignore)
963 * the new signal. YYY needs cleanup.
965 #ifdef SMP
966 if (p == lwkt_preempted_proc()) {
967 signotify();
968 } else if (p->p_stat == SRUN) {
969 struct thread *td = p->p_thread;
971 KASSERT(td != NULL,
972 ("pid %d NULL p_thread stat %d flags %08x",
973 p->p_pid, p->p_stat, p->p_flag));
975 if (td->td_gd != mycpu)
976 lwkt_send_ipiq(td->td_gd, signotify_remote, p);
977 else if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
978 lwkt_schedule(td);
980 #else
981 if (p == lwkt_preempted_proc()) {
982 signotify();
983 } else if (p->p_stat == SRUN) {
984 struct thread *td = p->p_thread;
986 KASSERT(td != NULL,
987 ("pid %d NULL p_thread stat %d flags %08x",
988 p->p_pid, p->p_stat, p->p_flag));
990 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
991 lwkt_schedule(td);
993 #endif
994 goto out;
996 /*NOTREACHED*/
997 run:
998 setrunnable(p);
999 out:
1000 crit_exit();
1003 #ifdef SMP
1006 * This function is called via an IPI. We will be in a critical section but
1007 * the MP lock will NOT be held. Also note that by the time the ipi message
1008 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1010 static void
1011 signotify_remote(void *arg)
1013 struct proc *p = arg;
1015 if (p == lwkt_preempted_proc()) {
1016 signotify();
1017 } else {
1018 struct thread *td = p->p_thread;
1019 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
1020 lwkt_schedule(td);
1024 #endif
1026 static int
1027 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1029 sigset_t savedmask, set;
1030 struct proc *p = curproc;
1031 int error, sig, hz, timevalid = 0;
1032 struct timespec rts, ets, ts;
1033 struct timeval tv;
1035 error = 0;
1036 sig = 0;
1037 SIG_CANTMASK(waitset);
1038 savedmask = p->p_sigmask;
1040 if (timeout) {
1041 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1042 timeout->tv_nsec < 1000000000) {
1043 timevalid = 1;
1044 getnanouptime(&rts);
1045 ets = rts;
1046 timespecadd(&ets, timeout);
1050 for (;;) {
1051 set = p->p_siglist;
1052 SIGSETAND(set, waitset);
1053 if ((sig = sig_ffs(&set)) != 0) {
1054 SIGFILLSET(p->p_sigmask);
1055 SIGDELSET(p->p_sigmask, sig);
1056 SIG_CANTMASK(p->p_sigmask);
1057 sig = issignal(p);
1059 * It may be a STOP signal, in the case, issignal
1060 * returns 0, because we may stop there, and new
1061 * signal can come in, we should restart if we got
1062 * nothing.
1064 if (sig == 0)
1065 continue;
1066 else
1067 break;
1071 * Previous checking got nothing, and we retried but still
1072 * got nothing, we should return the error status.
1074 if (error)
1075 break;
1078 * POSIX says this must be checked after looking for pending
1079 * signals.
1081 if (timeout) {
1082 if (!timevalid) {
1083 error = EINVAL;
1084 break;
1086 getnanouptime(&rts);
1087 if (timespeccmp(&rts, &ets, >=)) {
1088 error = EAGAIN;
1089 break;
1091 ts = ets;
1092 timespecsub(&ts, &rts);
1093 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1094 hz = tvtohz_high(&tv);
1095 } else
1096 hz = 0;
1098 p->p_sigmask = savedmask;
1099 SIGSETNAND(p->p_sigmask, waitset);
1100 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1101 if (timeout) {
1102 if (error == ERESTART) {
1103 /* can not restart a timeout wait. */
1104 error = EINTR;
1105 } else if (error == EAGAIN) {
1106 /* will calculate timeout by ourself. */
1107 error = 0;
1110 /* Retry ... */
1113 p->p_sigmask = savedmask;
1114 if (sig) {
1115 error = 0;
1116 bzero(info, sizeof(*info));
1117 info->si_signo = sig;
1118 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1120 return (error);
1124 sigtimedwait(struct sigtimedwait_args *uap)
1126 struct timespec ts;
1127 struct timespec *timeout;
1128 sigset_t set;
1129 siginfo_t info;
1130 int error;
1132 if (uap->timeout) {
1133 error = copyin(uap->timeout, &ts, sizeof(ts));
1134 if (error)
1135 return (error);
1136 timeout = &ts;
1137 } else {
1138 timeout = NULL;
1140 error = copyin(uap->set, &set, sizeof(set));
1141 if (error)
1142 return (error);
1143 error = kern_sigtimedwait(set, &info, timeout);
1144 if (error)
1145 return (error);
1146 if (uap->info)
1147 error = copyout(&info, uap->info, sizeof(info));
1148 /* Repost if we got an error. */
1149 if (error)
1150 psignal(curproc, info.si_signo);
1151 else
1152 uap->sysmsg_result = info.si_signo;
1153 return (error);
1157 sigwaitinfo(struct sigwaitinfo_args *uap)
1159 siginfo_t info;
1160 sigset_t set;
1161 int error;
1163 error = copyin(uap->set, &set, sizeof(set));
1164 if (error)
1165 return (error);
1166 error = kern_sigtimedwait(set, &info, NULL);
1167 if (error)
1168 return (error);
1169 if (uap->info)
1170 error = copyout(&info, uap->info, sizeof(info));
1171 /* Repost if we got an error. */
1172 if (error)
1173 psignal(curproc, info.si_signo);
1174 else
1175 uap->sysmsg_result = info.si_signo;
1176 return (error);
1180 * If the current process has received a signal that would interrupt a
1181 * system call, return EINTR or ERESTART as appropriate.
1184 iscaught(struct proc *p)
1186 int sig;
1188 if (p) {
1189 if ((sig = CURSIG(p)) != 0) {
1190 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1191 return (EINTR);
1192 return (ERESTART);
1195 return(EWOULDBLOCK);
1199 * If the current process has received a signal (should be caught or cause
1200 * termination, should interrupt current syscall), return the signal number.
1201 * Stop signals with default action are processed immediately, then cleared;
1202 * they aren't returned. This is checked after each entry to the system for
1203 * a syscall or trap (though this can usually be done without calling issignal
1204 * by checking the pending signal masks in the CURSIG macro.) The normal call
1205 * sequence is
1207 * while (sig = CURSIG(curproc))
1208 * postsig(sig);
1211 issignal(struct proc *p)
1213 sigset_t mask;
1214 int sig, prop;
1216 for (;;) {
1217 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1219 mask = p->p_siglist;
1220 SIGSETNAND(mask, p->p_sigmask);
1221 if (p->p_flag & P_PPWAIT)
1222 SIG_STOPSIGMASK(mask);
1223 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1224 return (0);
1225 sig = sig_ffs(&mask);
1227 STOPEVENT(p, S_SIG, sig);
1230 * We should see pending but ignored signals
1231 * only if P_TRACED was on when they were posted.
1233 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1234 SIGDELSET(p->p_siglist, sig);
1235 continue;
1237 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1239 * If traced, always stop, and stay
1240 * stopped until released by the parent.
1242 p->p_xstat = sig;
1243 psignal(p->p_pptr, SIGCHLD);
1244 do {
1245 stop(p);
1246 mi_switch(p);
1247 } while (!trace_req(p) && p->p_flag & P_TRACED);
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 */
1255 sig = p->p_xstat;
1256 if (sig == 0)
1257 continue;
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))
1265 continue;
1268 * If the traced bit got turned off, go back up
1269 * to the top to rescan signals. This ensures
1270 * that p_sig* and ps_sigact are consistent.
1272 if ((p->p_flag & P_TRACED) == 0)
1273 continue;
1276 prop = sigprop(sig);
1279 * Decide whether the signal should be returned.
1280 * Return the signal's number, or fall through
1281 * to clear it from the pending mask.
1283 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1285 case (int)SIG_DFL:
1287 * Don't take default actions on system processes.
1289 if (p->p_pid <= 1) {
1290 #ifdef DIAGNOSTIC
1292 * Are you sure you want to ignore SIGSEGV
1293 * in init? XXX
1295 printf("Process (pid %lu) got signal %d\n",
1296 (u_long)p->p_pid, sig);
1297 #endif
1298 break; /* == ignore */
1302 * Handle the in-kernel checkpoint action
1304 if (prop & SA_CKPT) {
1305 checkpoint_signal_handler(p);
1306 break;
1310 * If there is a pending stop signal to process
1311 * with default action, stop here,
1312 * then clear the signal. However,
1313 * if process is member of an orphaned
1314 * process group, ignore tty stop signals.
1316 if (prop & SA_STOP) {
1317 if (p->p_flag & P_TRACED ||
1318 (p->p_pgrp->pg_jobc == 0 &&
1319 prop & SA_TTYSTOP))
1320 break; /* == ignore */
1321 p->p_xstat = sig;
1322 stop(p);
1323 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1324 psignal(p->p_pptr, SIGCHLD);
1325 mi_switch(p);
1326 break;
1327 } else if (prop & SA_IGNORE) {
1329 * Except for SIGCONT, shouldn't get here.
1330 * Default action is to ignore; drop it.
1332 break; /* == ignore */
1333 } else {
1334 return (sig);
1337 /*NOTREACHED*/
1339 case (int)SIG_IGN:
1341 * Masking above should prevent us ever trying
1342 * to take action on an ignored signal other
1343 * than SIGCONT, unless process is traced.
1345 if ((prop & SA_CONT) == 0 &&
1346 (p->p_flag & P_TRACED) == 0)
1347 printf("issignal\n");
1348 break; /* == ignore */
1350 default:
1352 * This signal has an action, let
1353 * postsig() process it.
1355 return (sig);
1357 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1359 /* NOTREACHED */
1363 * Put the argument process into the stopped state and notify the parent
1364 * via wakeup. Signals are handled elsewhere. The process must not be
1365 * on the run queue.
1367 void
1368 stop(struct proc *p)
1370 p->p_stat = SSTOP;
1371 p->p_flag &= ~P_WAITED;
1372 wakeup((caddr_t)p->p_pptr);
1376 * Take the action for the specified signal
1377 * from the current set of pending signals.
1379 void
1380 postsig(int sig)
1382 struct proc *p = curproc;
1383 struct sigacts *ps = p->p_sigacts;
1384 sig_t action;
1385 sigset_t returnmask;
1386 int code;
1388 KASSERT(sig != 0, ("postsig"));
1390 SIGDELSET(p->p_siglist, sig);
1391 action = ps->ps_sigact[_SIG_IDX(sig)];
1392 #ifdef KTRACE
1393 if (KTRPOINT(p->p_thread, KTR_PSIG))
1394 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1395 &p->p_oldsigmask : &p->p_sigmask, 0);
1396 #endif
1397 STOPEVENT(p, S_SIG, sig);
1399 if (action == SIG_DFL) {
1401 * Default action, where the default is to kill
1402 * the process. (Other cases were ignored above.)
1404 sigexit(p, sig);
1405 /* NOTREACHED */
1406 } else {
1408 * If we get here, the signal must be caught.
1410 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1411 ("postsig action"));
1413 * Set the new mask value and also defer further
1414 * occurrences of this signal.
1416 * Special case: user has done a sigsuspend. Here the
1417 * current mask is not of interest, but rather the
1418 * mask from before the sigsuspend is what we want
1419 * restored after the signal processing is completed.
1421 crit_enter();
1422 if (p->p_flag & P_OLDMASK) {
1423 returnmask = p->p_oldsigmask;
1424 p->p_flag &= ~P_OLDMASK;
1425 } else {
1426 returnmask = p->p_sigmask;
1429 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1430 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1431 SIGADDSET(p->p_sigmask, sig);
1433 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1435 * See kern_sigaction() for origin of this code.
1437 SIGDELSET(p->p_sigcatch, sig);
1438 if (sig != SIGCONT &&
1439 sigprop(sig) & SA_IGNORE)
1440 SIGADDSET(p->p_sigignore, sig);
1441 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1443 crit_exit();
1444 p->p_stats->p_ru.ru_nsignals++;
1445 if (p->p_sig != sig) {
1446 code = 0;
1447 } else {
1448 code = p->p_code;
1449 p->p_code = 0;
1450 p->p_sig = 0;
1452 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1457 * Kill the current process for stated reason.
1459 void
1460 killproc(struct proc *p, char *why)
1462 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1463 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1464 psignal(p, SIGKILL);
1468 * Force the current process to exit with the specified signal, dumping core
1469 * if appropriate. We bypass the normal tests for masked and caught signals,
1470 * allowing unrecoverable failures to terminate the process without changing
1471 * signal state. Mark the accounting record with the signal termination.
1472 * If dumping core, save the signal number for the debugger. Calls exit and
1473 * does not return.
1475 void
1476 sigexit(struct proc *p, int sig)
1478 p->p_acflag |= AXSIG;
1479 if (sigprop(sig) & SA_CORE) {
1480 p->p_sig = sig;
1482 * Log signals which would cause core dumps
1483 * (Log as LOG_INFO to appease those who don't want
1484 * these messages.)
1485 * XXX : Todo, as well as euid, write out ruid too
1487 if (coredump(p) == 0)
1488 sig |= WCOREFLAG;
1489 if (kern_logsigexit)
1490 log(LOG_INFO,
1491 "pid %d (%s), uid %d: exited on signal %d%s\n",
1492 p->p_pid, p->p_comm,
1493 p->p_ucred ? p->p_ucred->cr_uid : -1,
1494 sig &~ WCOREFLAG,
1495 sig & WCOREFLAG ? " (core dumped)" : "");
1497 exit1(W_EXITCODE(0, sig));
1498 /* NOTREACHED */
1501 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1502 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1503 sizeof(corefilename), "process corefile name format string");
1506 * expand_name(name, uid, pid)
1507 * Expand the name described in corefilename, using name, uid, and pid.
1508 * corefilename is a printf-like string, with three format specifiers:
1509 * %N name of process ("name")
1510 * %P process id (pid)
1511 * %U user id (uid)
1512 * For example, "%N.core" is the default; they can be disabled completely
1513 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1514 * This is controlled by the sysctl variable kern.corefile (see above).
1517 static char *
1518 expand_name(const char *name, uid_t uid, pid_t pid)
1520 char *temp;
1521 char buf[11]; /* Buffer for pid/uid -- max 4B */
1522 int i, n;
1523 char *format = corefilename;
1524 size_t namelen;
1526 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1527 if (temp == NULL)
1528 return NULL;
1529 namelen = strlen(name);
1530 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1531 int l;
1532 switch (format[i]) {
1533 case '%': /* Format character */
1534 i++;
1535 switch (format[i]) {
1536 case '%':
1537 temp[n++] = '%';
1538 break;
1539 case 'N': /* process name */
1540 if ((n + namelen) > MAXPATHLEN) {
1541 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1542 pid, name, uid, temp, name);
1543 free(temp, M_TEMP);
1544 return NULL;
1546 memcpy(temp+n, name, namelen);
1547 n += namelen;
1548 break;
1549 case 'P': /* process id */
1550 l = sprintf(buf, "%u", pid);
1551 if ((n + l) > MAXPATHLEN) {
1552 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1553 pid, name, uid, temp, name);
1554 free(temp, M_TEMP);
1555 return NULL;
1557 memcpy(temp+n, buf, l);
1558 n += l;
1559 break;
1560 case 'U': /* user id */
1561 l = sprintf(buf, "%u", uid);
1562 if ((n + l) > MAXPATHLEN) {
1563 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1564 pid, name, uid, temp, name);
1565 free(temp, M_TEMP);
1566 return NULL;
1568 memcpy(temp+n, buf, l);
1569 n += l;
1570 break;
1571 default:
1572 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1574 break;
1575 default:
1576 temp[n++] = format[i];
1579 temp[n] = '\0';
1580 return temp;
1584 * Dump a process' core. The main routine does some
1585 * policy checking, and creates the name of the coredump;
1586 * then it passes on a vnode and a size limit to the process-specific
1587 * coredump routine if there is one; if there _is not_ one, it returns
1588 * ENOSYS; otherwise it returns the error from the process-specific routine.
1591 static int
1592 coredump(struct proc *p)
1594 struct vnode *vp;
1595 struct ucred *cred = p->p_ucred;
1596 struct thread *td = p->p_thread;
1597 struct flock lf;
1598 struct nlookupdata nd;
1599 struct vattr vattr;
1600 int error, error1;
1601 char *name; /* name of corefile */
1602 off_t limit;
1604 STOPEVENT(p, S_CORE, 0);
1606 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1607 return (EFAULT);
1610 * Note that the bulk of limit checking is done after
1611 * the corefile is created. The exception is if the limit
1612 * for corefiles is 0, in which case we don't bother
1613 * creating the corefile at all. This layout means that
1614 * a corefile is truncated instead of not being created,
1615 * if it is larger than the limit.
1617 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1618 if (limit == 0)
1619 return EFBIG;
1621 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1622 if (name == NULL)
1623 return (EINVAL);
1624 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
1625 if (error == 0)
1626 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1627 free(name, M_TEMP);
1628 if (error) {
1629 nlookup_done(&nd);
1630 return (error);
1632 vp = nd.nl_open_vp;
1633 nd.nl_open_vp = NULL;
1634 nlookup_done(&nd);
1636 VOP_UNLOCK(vp, 0, td);
1637 lf.l_whence = SEEK_SET;
1638 lf.l_start = 0;
1639 lf.l_len = 0;
1640 lf.l_type = F_WRLCK;
1641 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1642 if (error)
1643 goto out2;
1645 /* Don't dump to non-regular files or files with links. */
1646 if (vp->v_type != VREG ||
1647 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1648 error = EFAULT;
1649 goto out1;
1652 VATTR_NULL(&vattr);
1653 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1654 vattr.va_size = 0;
1655 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1656 VOP_SETATTR(vp, &vattr, cred, td);
1657 p->p_acflag |= ACORE;
1658 VOP_UNLOCK(vp, 0, td);
1660 error = p->p_sysent->sv_coredump ?
1661 p->p_sysent->sv_coredump(p, vp, limit) : ENOSYS;
1663 out1:
1664 lf.l_type = F_UNLCK;
1665 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1666 out2:
1667 error1 = vn_close(vp, FWRITE, td);
1668 if (error == 0)
1669 error = error1;
1670 return (error);
1674 * Nonexistent system call-- signal process (may want to handle it).
1675 * Flag error in case process won't see signal immediately (blocked or ignored).
1677 /* ARGSUSED */
1679 nosys(struct nosys_args *args)
1681 psignal(curproc, SIGSYS);
1682 return (EINVAL);
1686 * Send a SIGIO or SIGURG signal to a process or process group using
1687 * stored credentials rather than those of the current process.
1689 void
1690 pgsigio(struct sigio *sigio, int sig, int checkctty)
1692 if (sigio == NULL)
1693 return;
1695 if (sigio->sio_pgid > 0) {
1696 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1697 sigio->sio_proc))
1698 psignal(sigio->sio_proc, sig);
1699 } else if (sigio->sio_pgid < 0) {
1700 struct proc *p;
1702 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1703 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1704 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1705 psignal(p, sig);
1709 static int
1710 filt_sigattach(struct knote *kn)
1712 struct proc *p = curproc;
1714 kn->kn_ptr.p_proc = p;
1715 kn->kn_flags |= EV_CLEAR; /* automatically set */
1717 /* XXX lock the proc here while adding to the list? */
1718 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1720 return (0);
1723 static void
1724 filt_sigdetach(struct knote *kn)
1726 struct proc *p = kn->kn_ptr.p_proc;
1728 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1732 * signal knotes are shared with proc knotes, so we apply a mask to
1733 * the hint in order to differentiate them from process hints. This
1734 * could be avoided by using a signal-specific knote list, but probably
1735 * isn't worth the trouble.
1737 static int
1738 filt_signal(struct knote *kn, long hint)
1740 if (hint & NOTE_SIGNAL) {
1741 hint &= ~NOTE_SIGNAL;
1743 if (kn->kn_id == hint)
1744 kn->kn_data++;
1746 return (kn->kn_data != 0);