Deleted Added
full compact
kern_sig.c (72200) kern_sig.c (72276)
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.
9 *
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.
25 *
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.
37 *
38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
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.
9 *
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.
25 *
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.
37 *
38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 * $FreeBSD: head/sys/kern/kern_sig.c 72200 2001-02-09 06:11:45Z bmilekic $
39 * $FreeBSD: head/sys/kern/kern_sig.c 72276 2001-02-10 02:20:34Z jhb $
40 */
41
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
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/resourcevar.h>
51#include <sys/namei.h>
52#include <sys/vnode.h>
53#include <sys/event.h>
54#include <sys/proc.h>
55#include <sys/pioctl.h>
56#include <sys/acct.h>
57#include <sys/fcntl.h>
58#include <sys/ipl.h>
59#include <sys/condvar.h>
60#include <sys/mutex.h>
61#include <sys/wait.h>
62#include <sys/ktr.h>
63#include <sys/ktrace.h>
64#include <sys/syslog.h>
65#include <sys/stat.h>
66#include <sys/sysent.h>
67#include <sys/sysctl.h>
68#include <sys/malloc.h>
69
70#include <machine/cpu.h>
71#include <machine/smp.h>
72
73#define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
74
75static int coredump __P((struct proc *));
76static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act,
77 struct sigaction *oact, int old));
78static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
79 sigset_t *oset, int old));
80static char *expand_name __P((const char *, uid_t, pid_t));
81static int killpg1 __P((struct proc *cp, int sig, int pgid, int all));
82static int sig_ffs __P((sigset_t *set));
83static int sigprop __P((int sig));
84static void stop __P((struct proc *));
85
86static int filt_sigattach(struct knote *kn);
87static void filt_sigdetach(struct knote *kn);
88static int filt_signal(struct knote *kn, long hint);
89
90struct filterops sig_filtops =
91 { 0, filt_sigattach, filt_sigdetach, filt_signal };
92
93static int kern_logsigexit = 1;
94SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
95 &kern_logsigexit, 0,
96 "Log processes quitting on abnormal signals to syslog(3)");
97
98/*
99 * Can process p, with pcred pc, send the signal sig to process q?
100 */
101#define CANSIGNAL(p, q, sig) \
102 (!p_can(p, q, P_CAN_KILL, NULL) || \
103 ((sig) == SIGCONT && (q)->p_session == (p)->p_session))
104
105/*
106 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
107 */
108#define CANSIGIO(ruid, uc, q) \
109 ((uc)->cr_uid == 0 || \
110 (ruid) == (q)->p_cred->p_ruid || \
111 (uc)->cr_uid == (q)->p_cred->p_ruid || \
112 (ruid) == (q)->p_ucred->cr_uid || \
113 (uc)->cr_uid == (q)->p_ucred->cr_uid)
114
115int sugid_coredump;
116SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
117 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
118
119static int do_coredump = 1;
120SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121 &do_coredump, 0, "Enable/Disable coredumps");
122
123/*
124 * Signal properties and actions.
125 * The array below categorizes the signals and their default actions
126 * according to the following properties:
127 */
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
136static int sigproptbl[NSIG] = {
137 SA_KILL, /* SIGHUP */
138 SA_KILL, /* SIGINT */
139 SA_KILL|SA_CORE, /* SIGQUIT */
140 SA_KILL|SA_CORE, /* SIGILL */
141 SA_KILL|SA_CORE, /* SIGTRAP */
142 SA_KILL|SA_CORE, /* SIGABRT */
143 SA_KILL|SA_CORE, /* SIGEMT */
144 SA_KILL|SA_CORE, /* SIGFPE */
145 SA_KILL, /* SIGKILL */
146 SA_KILL|SA_CORE, /* SIGBUS */
147 SA_KILL|SA_CORE, /* SIGSEGV */
148 SA_KILL|SA_CORE, /* SIGSYS */
149 SA_KILL, /* SIGPIPE */
150 SA_KILL, /* SIGALRM */
151 SA_KILL, /* SIGTERM */
152 SA_IGNORE, /* SIGURG */
153 SA_STOP, /* SIGSTOP */
154 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
155 SA_IGNORE|SA_CONT, /* SIGCONT */
156 SA_IGNORE, /* SIGCHLD */
157 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
158 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
159 SA_IGNORE, /* SIGIO */
160 SA_KILL, /* SIGXCPU */
161 SA_KILL, /* SIGXFSZ */
162 SA_KILL, /* SIGVTALRM */
163 SA_KILL, /* SIGPROF */
164 SA_IGNORE, /* SIGWINCH */
165 SA_IGNORE, /* SIGINFO */
166 SA_KILL, /* SIGUSR1 */
167 SA_KILL, /* SIGUSR2 */
168};
169
170/*
171 * Determine signal that should be delivered to process p, the current
172 * process, 0 if none. If there is a pending stop signal with default
173 * action, the process stops in issignal().
174 *
175 * MP SAFE.
176 */
177int
178CURSIG(struct proc *p)
179{
180 sigset_t tmpset;
181 int r;
182
183 if (SIGISEMPTY(p->p_siglist))
184 return (0);
185 tmpset = p->p_siglist;
186 SIGSETNAND(tmpset, p->p_sigmask);
187 if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
188 return (0);
189 mtx_lock(&Giant);
190 r = issignal(p);
191 mtx_unlock(&Giant);
192 return (r);
193}
194
195static __inline int
196sigprop(int sig)
197{
198
199 if (sig > 0 && sig < NSIG)
200 return (sigproptbl[_SIG_IDX(sig)]);
201 return (0);
202}
203
204static __inline int
205sig_ffs(sigset_t *set)
206{
207 int i;
208
209 for (i = 0; i < _SIG_WORDS; i++)
210 if (set->__bits[i])
211 return (ffs(set->__bits[i]) + (i * 32));
212 return (0);
213}
214
215/*
216 * do_sigaction
217 * sigaction
218 * osigaction
219 */
220static int
221do_sigaction(p, sig, act, oact, old)
222 struct proc *p;
223 register int sig;
224 struct sigaction *act, *oact;
225 int old;
226{
227 register struct sigacts *ps = p->p_sigacts;
228
229 if (sig <= 0 || sig > _SIG_MAXSIG)
230 return (EINVAL);
231
232 if (oact) {
233 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
234 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
235 oact->sa_flags = 0;
236 if (SIGISMEMBER(ps->ps_sigonstack, sig))
237 oact->sa_flags |= SA_ONSTACK;
238 if (!SIGISMEMBER(ps->ps_sigintr, sig))
239 oact->sa_flags |= SA_RESTART;
240 if (SIGISMEMBER(ps->ps_sigreset, sig))
241 oact->sa_flags |= SA_RESETHAND;
242 if (SIGISMEMBER(ps->ps_signodefer, sig))
243 oact->sa_flags |= SA_NODEFER;
244 if (SIGISMEMBER(ps->ps_siginfo, sig))
245 oact->sa_flags |= SA_SIGINFO;
246 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
247 oact->sa_flags |= SA_NOCLDSTOP;
248 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
249 oact->sa_flags |= SA_NOCLDWAIT;
250 }
251 if (act) {
252 if ((sig == SIGKILL || sig == SIGSTOP) &&
253 act->sa_handler != SIG_DFL)
254 return (EINVAL);
255
256 /*
257 * Change setting atomically.
258 */
259 (void) splhigh();
260
261 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
262 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
263 if (act->sa_flags & SA_SIGINFO) {
264 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
265 SIGADDSET(ps->ps_siginfo, sig);
266 } else {
267 ps->ps_sigact[_SIG_IDX(sig)] =
268 (__sighandler_t *)act->sa_sigaction;
269 SIGDELSET(ps->ps_siginfo, sig);
270 }
271 if (!(act->sa_flags & SA_RESTART))
272 SIGADDSET(ps->ps_sigintr, sig);
273 else
274 SIGDELSET(ps->ps_sigintr, sig);
275 if (act->sa_flags & SA_ONSTACK)
276 SIGADDSET(ps->ps_sigonstack, sig);
277 else
278 SIGDELSET(ps->ps_sigonstack, sig);
279 if (act->sa_flags & SA_RESETHAND)
280 SIGADDSET(ps->ps_sigreset, sig);
281 else
282 SIGDELSET(ps->ps_sigreset, sig);
283 if (act->sa_flags & SA_NODEFER)
284 SIGADDSET(ps->ps_signodefer, sig);
285 else
286 SIGDELSET(ps->ps_signodefer, sig);
287#ifdef COMPAT_SUNOS
288 if (act->sa_flags & SA_USERTRAMP)
289 SIGADDSET(ps->ps_usertramp, sig);
290 else
291 SIGDELSET(ps->ps_usertramp, seg);
292#endif
293 if (sig == SIGCHLD) {
294 if (act->sa_flags & SA_NOCLDSTOP)
295 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
296 else
297 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
298 if (act->sa_flags & SA_NOCLDWAIT) {
299 /*
300 * Paranoia: since SA_NOCLDWAIT is implemented
301 * by reparenting the dying child to PID 1 (and
302 * trust it to reap the zombie), PID 1 itself
303 * is forbidden to set SA_NOCLDWAIT.
304 */
305 if (p->p_pid == 1)
306 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
307 else
308 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
309 } else
310 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
311 }
312 /*
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.
317 */
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);
333 }
334 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
335 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
336 SIGDELSET(ps->ps_osigset, sig);
337 else
338 SIGADDSET(ps->ps_osigset, sig);
339
340 (void) spl0();
341 }
342 return (0);
343}
344
345#ifndef _SYS_SYSPROTO_H_
346struct sigaction_args {
347 int sig;
348 struct sigaction *act;
349 struct sigaction *oact;
350};
351#endif
352/* ARGSUSED */
353int
354sigaction(p, uap)
355 struct proc *p;
356 register struct sigaction_args *uap;
357{
358 struct sigaction act, oact;
359 register struct sigaction *actp, *oactp;
360 int error;
361
362 actp = (uap->act != NULL) ? &act : NULL;
363 oactp = (uap->oact != NULL) ? &oact : NULL;
364 if (actp) {
365 error = copyin(uap->act, actp, sizeof(act));
366 if (error)
367 return (error);
368 }
369 error = do_sigaction(p, uap->sig, actp, oactp, 0);
370 if (oactp && !error) {
371 error = copyout(oactp, uap->oact, sizeof(oact));
372 }
373 return (error);
374}
375
376#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
377#ifndef _SYS_SYSPROTO_H_
378struct osigaction_args {
379 int signum;
380 struct osigaction *nsa;
381 struct osigaction *osa;
382};
383#endif
384/* ARGSUSED */
385int
386osigaction(p, uap)
387 struct proc *p;
388 register struct osigaction_args *uap;
389{
390 struct osigaction sa;
391 struct sigaction nsa, osa;
392 register struct sigaction *nsap, *osap;
393 int error;
394
395 if (uap->signum <= 0 || uap->signum >= ONSIG)
396 return (EINVAL);
397 nsap = (uap->nsa != NULL) ? &nsa : NULL;
398 osap = (uap->osa != NULL) ? &osa : NULL;
399 if (nsap) {
400 error = copyin(uap->nsa, &sa, sizeof(sa));
401 if (error)
402 return (error);
403 nsap->sa_handler = sa.sa_handler;
404 nsap->sa_flags = sa.sa_flags;
405 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
406 }
407 error = do_sigaction(p, uap->signum, nsap, osap, 1);
408 if (osap && !error) {
409 sa.sa_handler = osap->sa_handler;
410 sa.sa_flags = osap->sa_flags;
411 SIG2OSIG(osap->sa_mask, sa.sa_mask);
412 error = copyout(&sa, uap->osa, sizeof(sa));
413 }
414 return (error);
415}
416#endif /* COMPAT_43 */
417
418/*
419 * Initialize signal state for process 0;
420 * set to ignore signals that are ignored by default.
421 */
422void
423siginit(p)
424 struct proc *p;
425{
426 register int i;
427
428 for (i = 1; i <= NSIG; i++)
429 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
430 SIGADDSET(p->p_sigignore, i);
431}
432
433/*
434 * Reset signals for an exec of the specified process.
435 */
436void
437execsigs(p)
438 register struct proc *p;
439{
440 register struct sigacts *ps = p->p_sigacts;
441 register int sig;
442
443 /*
444 * Reset caught signals. Held signals remain held
445 * through p_sigmask (unless they were caught,
446 * and are now ignored by default).
447 */
448 while (SIGNOTEMPTY(p->p_sigcatch)) {
449 sig = sig_ffs(&p->p_sigcatch);
450 SIGDELSET(p->p_sigcatch, sig);
451 if (sigprop(sig) & SA_IGNORE) {
452 if (sig != SIGCONT)
453 SIGADDSET(p->p_sigignore, sig);
454 SIGDELSET(p->p_siglist, sig);
455 }
456 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
457 }
458 /*
459 * Reset stack state to the user stack.
460 * Clear set of signals caught on the signal stack.
461 */
462 p->p_sigstk.ss_flags = SS_DISABLE;
463 p->p_sigstk.ss_size = 0;
464 p->p_sigstk.ss_sp = 0;
465 /*
466 * Reset no zombies if child dies flag as Solaris does.
467 */
468 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
469}
470
471/*
472 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
473 *
474 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
475 * p == curproc. Also remember that in order to remain MP SAFE
476 * no spl*() calls may be made.
477 */
478static int
479do_sigprocmask(p, how, set, oset, old)
480 struct proc *p;
481 int how;
482 sigset_t *set, *oset;
483 int old;
484{
485 int error;
486
487 if (oset != NULL)
488 *oset = p->p_sigmask;
489
490 error = 0;
491 if (set != NULL) {
492 switch (how) {
493 case SIG_BLOCK:
494 SIG_CANTMASK(*set);
495 SIGSETOR(p->p_sigmask, *set);
496 break;
497 case SIG_UNBLOCK:
498 SIGSETNAND(p->p_sigmask, *set);
499 break;
500 case SIG_SETMASK:
501 SIG_CANTMASK(*set);
502 if (old)
503 SIGSETLO(p->p_sigmask, *set);
504 else
505 p->p_sigmask = *set;
506 break;
507 default:
508 error = EINVAL;
509 break;
510 }
511 }
512 return (error);
513}
514
515/*
516 * sigprocmask() - MP SAFE
517 */
518
519#ifndef _SYS_SYSPROTO_H_
520struct sigprocmask_args {
521 int how;
522 const sigset_t *set;
523 sigset_t *oset;
524};
525#endif
526int
527sigprocmask(p, uap)
528 register struct proc *p;
529 struct sigprocmask_args *uap;
530{
531 sigset_t set, oset;
532 sigset_t *setp, *osetp;
533 int error;
534
535 setp = (uap->set != NULL) ? &set : NULL;
536 osetp = (uap->oset != NULL) ? &oset : NULL;
537 if (setp) {
538 error = copyin(uap->set, setp, sizeof(set));
539 if (error)
540 return (error);
541 }
542 error = do_sigprocmask(p, uap->how, setp, osetp, 0);
543 if (osetp && !error) {
544 error = copyout(osetp, uap->oset, sizeof(oset));
545 }
546 return (error);
547}
548
549#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
550/*
551 * osigprocmask() - MP SAFE
552 */
553#ifndef _SYS_SYSPROTO_H_
554struct osigprocmask_args {
555 int how;
556 osigset_t mask;
557};
558#endif
559int
560osigprocmask(p, uap)
561 register struct proc *p;
562 struct osigprocmask_args *uap;
563{
564 sigset_t set, oset;
565 int error;
566
567 OSIG2SIG(uap->mask, set);
568 error = do_sigprocmask(p, uap->how, &set, &oset, 1);
569 SIG2OSIG(oset, p->p_retval[0]);
570 return (error);
571}
572#endif /* COMPAT_43 */
573
574#ifndef _SYS_SYSPROTO_H_
575struct sigpending_args {
576 sigset_t *set;
577};
578#endif
579/* ARGSUSED */
580int
581sigpending(p, uap)
582 struct proc *p;
583 struct sigpending_args *uap;
584{
585
586 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
587}
588
589#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
590#ifndef _SYS_SYSPROTO_H_
591struct osigpending_args {
592 int dummy;
593};
594#endif
595/* ARGSUSED */
596int
597osigpending(p, uap)
598 struct proc *p;
599 struct osigpending_args *uap;
600{
601
602 SIG2OSIG(p->p_siglist, p->p_retval[0]);
603 return (0);
604}
605#endif /* COMPAT_43 */
606
607#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
608/*
609 * Generalized interface signal handler, 4.3-compatible.
610 */
611#ifndef _SYS_SYSPROTO_H_
612struct osigvec_args {
613 int signum;
614 struct sigvec *nsv;
615 struct sigvec *osv;
616};
617#endif
618/* ARGSUSED */
619int
620osigvec(p, uap)
621 struct proc *p;
622 register struct osigvec_args *uap;
623{
624 struct sigvec vec;
625 struct sigaction nsa, osa;
626 register struct sigaction *nsap, *osap;
627 int error;
628
629 if (uap->signum <= 0 || uap->signum >= ONSIG)
630 return (EINVAL);
631 nsap = (uap->nsv != NULL) ? &nsa : NULL;
632 osap = (uap->osv != NULL) ? &osa : NULL;
633 if (nsap) {
634 error = copyin(uap->nsv, &vec, sizeof(vec));
635 if (error)
636 return (error);
637 nsap->sa_handler = vec.sv_handler;
638 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
639 nsap->sa_flags = vec.sv_flags;
640 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
641#ifdef COMPAT_SUNOS
642 nsap->sa_flags |= SA_USERTRAMP;
643#endif
644 }
645 error = do_sigaction(p, uap->signum, nsap, osap, 1);
646 if (osap && !error) {
647 vec.sv_handler = osap->sa_handler;
648 SIG2OSIG(osap->sa_mask, vec.sv_mask);
649 vec.sv_flags = osap->sa_flags;
650 vec.sv_flags &= ~SA_NOCLDWAIT;
651 vec.sv_flags ^= SA_RESTART;
652#ifdef COMPAT_SUNOS
653 vec.sv_flags &= ~SA_NOCLDSTOP;
654#endif
655 error = copyout(&vec, uap->osv, sizeof(vec));
656 }
657 return (error);
658}
659
660#ifndef _SYS_SYSPROTO_H_
661struct osigblock_args {
662 int mask;
663};
664#endif
665int
666osigblock(p, uap)
667 register struct proc *p;
668 struct osigblock_args *uap;
669{
670 sigset_t set;
671
672 OSIG2SIG(uap->mask, set);
673 SIG_CANTMASK(set);
674 (void) splhigh();
675 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
676 SIGSETOR(p->p_sigmask, set);
677 (void) spl0();
678 return (0);
679}
680
681#ifndef _SYS_SYSPROTO_H_
682struct osigsetmask_args {
683 int mask;
684};
685#endif
686int
687osigsetmask(p, uap)
688 struct proc *p;
689 struct osigsetmask_args *uap;
690{
691 sigset_t set;
692
693 OSIG2SIG(uap->mask, set);
694 SIG_CANTMASK(set);
695 (void) splhigh();
696 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
697 SIGSETLO(p->p_sigmask, set);
698 (void) spl0();
699 return (0);
700}
701#endif /* COMPAT_43 || COMPAT_SUNOS */
702
703/*
704 * Suspend process until signal, providing mask to be set
705 * in the meantime. Note nonstandard calling convention:
706 * libc stub passes mask, not pointer, to save a copyin.
707 */
708#ifndef _SYS_SYSPROTO_H_
709struct sigsuspend_args {
710 const sigset_t *sigmask;
711};
712#endif
713/* ARGSUSED */
714int
715sigsuspend(p, uap)
716 register struct proc *p;
717 struct sigsuspend_args *uap;
718{
719 sigset_t mask;
720 register struct sigacts *ps = p->p_sigacts;
721 int error;
722
723 error = copyin(uap->sigmask, &mask, sizeof(mask));
724 if (error)
725 return (error);
726
727 /*
728 * When returning from sigsuspend, we want
729 * the old mask to be restored after the
730 * signal handler has finished. Thus, we
731 * save it here and mark the sigacts structure
732 * to indicate this.
733 */
734 p->p_oldsigmask = p->p_sigmask;
735 p->p_flag |= P_OLDMASK;
736
737 SIG_CANTMASK(mask);
738 p->p_sigmask = mask;
739 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
740 /* void */;
741 /* always return EINTR rather than ERESTART... */
742 return (EINTR);
743}
744
745#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
746#ifndef _SYS_SYSPROTO_H_
747struct osigsuspend_args {
748 osigset_t mask;
749};
750#endif
751/* ARGSUSED */
752int
753osigsuspend(p, uap)
754 register struct proc *p;
755 struct osigsuspend_args *uap;
756{
757 sigset_t mask;
758 register struct sigacts *ps = p->p_sigacts;
759
760 p->p_oldsigmask = p->p_sigmask;
761 p->p_flag |= P_OLDMASK;
762 OSIG2SIG(uap->mask, mask);
763 SIG_CANTMASK(mask);
764 SIGSETLO(p->p_sigmask, mask);
765 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
766 /* void */;
767 /* always return EINTR rather than ERESTART... */
768 return (EINTR);
769}
770#endif /* COMPAT_43 */
771
772#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
773#ifndef _SYS_SYSPROTO_H_
774struct osigstack_args {
775 struct sigstack *nss;
776 struct sigstack *oss;
777};
778#endif
779/* ARGSUSED */
780int
781osigstack(p, uap)
782 struct proc *p;
783 register struct osigstack_args *uap;
784{
785 struct sigstack ss;
786 int error;
787
788 if (uap->oss != NULL) {
789 ss.ss_sp = p->p_sigstk.ss_sp;
790 ss.ss_onstack = sigonstack(cpu_getstack(p));
791 error = copyout(&ss, uap->oss, sizeof(struct sigstack));
792 if (error)
793 return (error);
794 }
795
796 if (uap->nss != NULL) {
797 if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
798 return (error);
799 p->p_sigstk.ss_sp = ss.ss_sp;
800 p->p_sigstk.ss_size = 0;
801 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
802 p->p_flag |= P_ALTSTACK;
803 }
804 return (0);
805}
806#endif /* COMPAT_43 || COMPAT_SUNOS */
807
808#ifndef _SYS_SYSPROTO_H_
809struct sigaltstack_args {
810 stack_t *ss;
811 stack_t *oss;
812};
813#endif
814/* ARGSUSED */
815int
816sigaltstack(p, uap)
817 struct proc *p;
818 register struct sigaltstack_args *uap;
819{
820 stack_t ss;
821 int error, oonstack;
822
823 oonstack = sigonstack(cpu_getstack(p));
824
825 if (uap->oss != NULL) {
826 ss = p->p_sigstk;
827 ss.ss_flags = (p->p_flag & P_ALTSTACK)
828 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
829 if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
830 return (error);
831 }
832
833 if (uap->ss != NULL) {
834 if (oonstack)
835 return (EPERM);
836 if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
837 return (error);
838 if ((ss.ss_flags & ~SS_DISABLE) != 0)
839 return (EINVAL);
840 if (!(ss.ss_flags & SS_DISABLE)) {
841 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
842 return (ENOMEM);
843 p->p_sigstk = ss;
844 p->p_flag |= P_ALTSTACK;
845 } else
846 p->p_flag &= ~P_ALTSTACK;
847 }
848 return (0);
849}
850
851/*
852 * Common code for kill process group/broadcast kill.
853 * cp is calling process.
854 */
855int
856killpg1(cp, sig, pgid, all)
857 register struct proc *cp;
858 int sig, pgid, all;
859{
860 register struct proc *p;
861 struct pgrp *pgrp;
862 int nfound = 0;
863
864 if (all) {
865 /*
866 * broadcast
867 */
868 ALLPROC_LOCK(AP_SHARED);
869 LIST_FOREACH(p, &allproc, p_list) {
870 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
871 p == cp || !CANSIGNAL(cp, p, sig))
872 continue;
873 nfound++;
874 if (sig)
875 psignal(p, sig);
876 }
877 ALLPROC_LOCK(AP_RELEASE);
878 } else {
879 if (pgid == 0)
880 /*
881 * zero pgid means send to my process group.
882 */
883 pgrp = cp->p_pgrp;
884 else {
885 pgrp = pgfind(pgid);
886 if (pgrp == NULL)
887 return (ESRCH);
888 }
889 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
890 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
891 p->p_stat == SZOMB ||
892 !CANSIGNAL(cp, p, sig))
893 continue;
894 nfound++;
895 if (sig)
896 psignal(p, sig);
897 }
898 }
899 return (nfound ? 0 : ESRCH);
900}
901
902#ifndef _SYS_SYSPROTO_H_
903struct kill_args {
904 int pid;
905 int signum;
906};
907#endif
908/* ARGSUSED */
909int
910kill(cp, uap)
911 register struct proc *cp;
912 register struct kill_args *uap;
913{
914 register struct proc *p;
915
916 if ((u_int)uap->signum > _SIG_MAXSIG)
917 return (EINVAL);
918 if (uap->pid > 0) {
919 /* kill single process */
920 if ((p = pfind(uap->pid)) == NULL)
921 return (ESRCH);
922 if (!CANSIGNAL(cp, p, uap->signum))
923 return (EPERM);
924 if (uap->signum)
925 psignal(p, uap->signum);
926 return (0);
927 }
928 switch (uap->pid) {
929 case -1: /* broadcast signal */
930 return (killpg1(cp, uap->signum, 0, 1));
931 case 0: /* signal own process group */
932 return (killpg1(cp, uap->signum, 0, 0));
933 default: /* negative explicit process group */
934 return (killpg1(cp, uap->signum, -uap->pid, 0));
935 }
936 /* NOTREACHED */
937}
938
939#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
940#ifndef _SYS_SYSPROTO_H_
941struct okillpg_args {
942 int pgid;
943 int signum;
944};
945#endif
946/* ARGSUSED */
947int
948okillpg(p, uap)
949 struct proc *p;
950 register struct okillpg_args *uap;
951{
952
953 if ((u_int)uap->signum > _SIG_MAXSIG)
954 return (EINVAL);
955 return (killpg1(p, uap->signum, uap->pgid, 0));
956}
957#endif /* COMPAT_43 || COMPAT_SUNOS */
958
959/*
960 * Send a signal to a process group.
961 */
962void
963gsignal(pgid, sig)
964 int pgid, sig;
965{
966 struct pgrp *pgrp;
967
968 if (pgid && (pgrp = pgfind(pgid)))
969 pgsignal(pgrp, sig, 0);
970}
971
972/*
973 * Send a signal to a process group. If checktty is 1,
974 * limit to members which have a controlling terminal.
975 */
976void
977pgsignal(pgrp, sig, checkctty)
978 struct pgrp *pgrp;
979 int sig, checkctty;
980{
981 register struct proc *p;
982
983 if (pgrp)
984 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
985 if (checkctty == 0 || p->p_flag & P_CONTROLT)
986 psignal(p, sig);
987}
988
989/*
990 * Send a signal caused by a trap to the current process.
991 * If it will be caught immediately, deliver it with correct code.
992 * Otherwise, post it normally.
993 */
994void
995trapsignal(p, sig, code)
996 struct proc *p;
997 register int sig;
998 u_long code;
999{
1000 register struct sigacts *ps = p->p_sigacts;
1001
1002 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1003 !SIGISMEMBER(p->p_sigmask, sig)) {
1004 p->p_stats->p_ru.ru_nsignals++;
1005#ifdef KTRACE
1006 if (KTRPOINT(p, KTR_PSIG))
1007 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
1008 &p->p_sigmask, code);
1009#endif
1010 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1011 &p->p_sigmask, code);
1012 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1013 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1014 SIGADDSET(p->p_sigmask, sig);
1015 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1016 /*
1017 * See do_sigaction() for origin of this code.
1018 */
1019 SIGDELSET(p->p_sigcatch, sig);
1020 if (sig != SIGCONT &&
1021 sigprop(sig) & SA_IGNORE)
1022 SIGADDSET(p->p_sigignore, sig);
1023 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1024 }
1025 } else {
1026 p->p_code = code; /* XXX for core dump/debugger */
1027 p->p_sig = sig; /* XXX to verify code */
1028 psignal(p, sig);
1029 }
1030}
1031
1032/*
1033 * Send the signal to the process. If the signal has an action, the action
1034 * is usually performed by the target process rather than the caller; we add
1035 * the signal to the set of pending signals for the process.
1036 *
1037 * Exceptions:
1038 * o When a stop signal is sent to a sleeping process that takes the
1039 * default action, the process is stopped without awakening it.
1040 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1041 * regardless of the signal action (eg, blocked or ignored).
1042 *
1043 * Other ignored signals are discarded immediately.
1044 */
1045void
1046psignal(p, sig)
1047 register struct proc *p;
1048 register int sig;
1049{
1050 register int prop;
1051 register sig_t action;
1052
1053 if (sig > _SIG_MAXSIG || sig <= 0) {
1054 printf("psignal: signal %d\n", sig);
1055 panic("psignal signal number");
1056 }
1057
1058 PROC_LOCK(p);
1059 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1060
1061 prop = sigprop(sig);
1062
1063 /*
1064 * If proc is traced, always give parent a chance;
1065 * if signal event is tracked by procfs, give *that*
1066 * a chance, as well.
1067 */
1068 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1069 action = SIG_DFL;
1070 else {
1071 /*
1072 * If the signal is being ignored,
1073 * then we forget about it immediately.
1074 * (Note: we don't set SIGCONT in p_sigignore,
1075 * and if it is set to SIG_IGN,
1076 * action will be SIG_DFL here.)
1077 */
1078 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) {
1079 PROC_UNLOCK(p);
1080 return;
1081 }
1082 if (SIGISMEMBER(p->p_sigmask, sig))
1083 action = SIG_HOLD;
1084 else if (SIGISMEMBER(p->p_sigcatch, sig))
1085 action = SIG_CATCH;
1086 else
1087 action = SIG_DFL;
1088 }
1089
1090 mtx_lock_spin(&sched_lock);
1091 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1092 (p->p_flag & P_TRACED) == 0)
1093 p->p_nice = NZERO;
1094 mtx_unlock_spin(&sched_lock);
1095
1096 if (prop & SA_CONT)
1097 SIG_STOPSIGMASK(p->p_siglist);
1098
1099 if (prop & SA_STOP) {
1100 /*
1101 * If sending a tty stop signal to a member of an orphaned
1102 * process group, discard the signal here if the action
1103 * is default; don't stop the process below if sleeping,
1104 * and don't clear any pending SIGCONT.
1105 */
1106 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1107 action == SIG_DFL) {
1108 PROC_UNLOCK(p);
1109 return;
1110 }
1111 SIG_CONTSIGMASK(p->p_siglist);
1112 }
1113 SIGADDSET(p->p_siglist, sig);
1114
1115 /*
1116 * Defer further processing for signals which are held,
1117 * except that stopped processes must be continued by SIGCONT.
1118 */
1119 mtx_lock_spin(&sched_lock);
1120 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
1121 mtx_unlock_spin(&sched_lock);
1122 PROC_UNLOCK(p);
1123 return;
1124 }
1125 switch (p->p_stat) {
1126
1127 case SSLEEP:
1128 /*
1129 * If process is sleeping uninterruptibly
1130 * we can't interrupt the sleep... the signal will
1131 * be noticed when the process returns through
1132 * trap() or syscall().
1133 */
1134 if ((p->p_sflag & PS_SINTR) == 0) {
1135 mtx_unlock_spin(&sched_lock);
1136 goto out;
1137 }
1138 /*
1139 * Process is sleeping and traced... make it runnable
1140 * so it can discover the signal in issignal() and stop
1141 * for the parent.
1142 */
1143 if (p->p_flag & P_TRACED)
1144 goto run;
1145 mtx_unlock_spin(&sched_lock);
1146 /*
1147 * If SIGCONT is default (or ignored) and process is
1148 * asleep, we are finished; the process should not
1149 * be awakened.
1150 */
1151 if ((prop & SA_CONT) && action == SIG_DFL) {
1152 SIGDELSET(p->p_siglist, sig);
1153 goto out;
1154 }
1155 /*
1156 * When a sleeping process receives a stop
1157 * signal, process immediately if possible.
1158 * All other (caught or default) signals
1159 * cause the process to run.
1160 */
1161 if (prop & SA_STOP) {
1162 if (action != SIG_DFL)
1163 goto runfast;
1164 /*
1165 * If a child holding parent blocked,
1166 * stopping could cause deadlock.
1167 */
1168 if (p->p_flag & P_PPWAIT)
1169 goto out;
1170 SIGDELSET(p->p_siglist, sig);
1171 p->p_xstat = sig;
1172 PROC_UNLOCK(p);
1173 PROCTREE_LOCK(PT_SHARED);
1174 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1175 psignal(p->p_pptr, SIGCHLD);
1176 stop(p);
1177 PROCTREE_LOCK(PT_RELEASE);
1178 PROC_LOCK(p);
1179 goto out;
1180 } else
1181 goto runfast;
1182 /* NOTREACHED */
1183
1184 case SSTOP:
1185 mtx_unlock_spin(&sched_lock);
1186 /*
1187 * If traced process is already stopped,
1188 * then no further action is necessary.
1189 */
1190 if (p->p_flag & P_TRACED)
1191 goto out;
1192
1193 /*
1194 * Kill signal always sets processes running.
1195 */
1196 if (sig == SIGKILL)
1197 goto runfast;
1198
1199 if (prop & SA_CONT) {
1200 /*
1201 * If SIGCONT is default (or ignored), we continue the
1202 * process but don't leave the signal in p_siglist, as
1203 * it has no further action. If SIGCONT is held, we
1204 * continue the process and leave the signal in
1205 * p_siglist. If the process catches SIGCONT, let it
1206 * handle the signal itself. If it isn't waiting on
1207 * an event, then it goes back to run state.
1208 * Otherwise, process goes back to sleep state.
1209 */
1210 if (action == SIG_DFL)
1211 SIGDELSET(p->p_siglist, sig);
1212 if (action == SIG_CATCH)
1213 goto runfast;
1214 mtx_lock_spin(&sched_lock);
1215 if (p->p_wchan == NULL)
1216 goto run;
1217 p->p_stat = SSLEEP;
1218 mtx_unlock_spin(&sched_lock);
1219 goto out;
1220 }
1221
1222 if (prop & SA_STOP) {
1223 /*
1224 * Already stopped, don't need to stop again.
1225 * (If we did the shell could get confused.)
1226 */
1227 SIGDELSET(p->p_siglist, sig);
1228 goto out;
1229 }
1230
1231 /*
1232 * If process is sleeping interruptibly, then simulate a
1233 * wakeup so that when it is continued, it will be made
1234 * runnable and can look at the signal. But don't make
1235 * the process runnable, leave it stopped.
1236 */
1237 mtx_lock_spin(&sched_lock);
1238 if (p->p_wchan && p->p_sflag & PS_SINTR) {
1239 if (p->p_sflag & PS_CVWAITQ)
1240 cv_waitq_remove(p);
1241 else
1242 unsleep(p);
1243 }
1244 mtx_unlock_spin(&sched_lock);
1245 goto out;
1246
1247 default:
1248 /*
1249 * SRUN, SIDL, SZOMB do nothing with the signal,
1250 * other than kicking ourselves if we are running.
1251 * It will either never be noticed, or noticed very soon.
1252 */
1253 if (p == curproc) {
40 */
41
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
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/resourcevar.h>
51#include <sys/namei.h>
52#include <sys/vnode.h>
53#include <sys/event.h>
54#include <sys/proc.h>
55#include <sys/pioctl.h>
56#include <sys/acct.h>
57#include <sys/fcntl.h>
58#include <sys/ipl.h>
59#include <sys/condvar.h>
60#include <sys/mutex.h>
61#include <sys/wait.h>
62#include <sys/ktr.h>
63#include <sys/ktrace.h>
64#include <sys/syslog.h>
65#include <sys/stat.h>
66#include <sys/sysent.h>
67#include <sys/sysctl.h>
68#include <sys/malloc.h>
69
70#include <machine/cpu.h>
71#include <machine/smp.h>
72
73#define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
74
75static int coredump __P((struct proc *));
76static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act,
77 struct sigaction *oact, int old));
78static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
79 sigset_t *oset, int old));
80static char *expand_name __P((const char *, uid_t, pid_t));
81static int killpg1 __P((struct proc *cp, int sig, int pgid, int all));
82static int sig_ffs __P((sigset_t *set));
83static int sigprop __P((int sig));
84static void stop __P((struct proc *));
85
86static int filt_sigattach(struct knote *kn);
87static void filt_sigdetach(struct knote *kn);
88static int filt_signal(struct knote *kn, long hint);
89
90struct filterops sig_filtops =
91 { 0, filt_sigattach, filt_sigdetach, filt_signal };
92
93static int kern_logsigexit = 1;
94SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
95 &kern_logsigexit, 0,
96 "Log processes quitting on abnormal signals to syslog(3)");
97
98/*
99 * Can process p, with pcred pc, send the signal sig to process q?
100 */
101#define CANSIGNAL(p, q, sig) \
102 (!p_can(p, q, P_CAN_KILL, NULL) || \
103 ((sig) == SIGCONT && (q)->p_session == (p)->p_session))
104
105/*
106 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
107 */
108#define CANSIGIO(ruid, uc, q) \
109 ((uc)->cr_uid == 0 || \
110 (ruid) == (q)->p_cred->p_ruid || \
111 (uc)->cr_uid == (q)->p_cred->p_ruid || \
112 (ruid) == (q)->p_ucred->cr_uid || \
113 (uc)->cr_uid == (q)->p_ucred->cr_uid)
114
115int sugid_coredump;
116SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
117 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
118
119static int do_coredump = 1;
120SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
121 &do_coredump, 0, "Enable/Disable coredumps");
122
123/*
124 * Signal properties and actions.
125 * The array below categorizes the signals and their default actions
126 * according to the following properties:
127 */
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
136static int sigproptbl[NSIG] = {
137 SA_KILL, /* SIGHUP */
138 SA_KILL, /* SIGINT */
139 SA_KILL|SA_CORE, /* SIGQUIT */
140 SA_KILL|SA_CORE, /* SIGILL */
141 SA_KILL|SA_CORE, /* SIGTRAP */
142 SA_KILL|SA_CORE, /* SIGABRT */
143 SA_KILL|SA_CORE, /* SIGEMT */
144 SA_KILL|SA_CORE, /* SIGFPE */
145 SA_KILL, /* SIGKILL */
146 SA_KILL|SA_CORE, /* SIGBUS */
147 SA_KILL|SA_CORE, /* SIGSEGV */
148 SA_KILL|SA_CORE, /* SIGSYS */
149 SA_KILL, /* SIGPIPE */
150 SA_KILL, /* SIGALRM */
151 SA_KILL, /* SIGTERM */
152 SA_IGNORE, /* SIGURG */
153 SA_STOP, /* SIGSTOP */
154 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
155 SA_IGNORE|SA_CONT, /* SIGCONT */
156 SA_IGNORE, /* SIGCHLD */
157 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
158 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
159 SA_IGNORE, /* SIGIO */
160 SA_KILL, /* SIGXCPU */
161 SA_KILL, /* SIGXFSZ */
162 SA_KILL, /* SIGVTALRM */
163 SA_KILL, /* SIGPROF */
164 SA_IGNORE, /* SIGWINCH */
165 SA_IGNORE, /* SIGINFO */
166 SA_KILL, /* SIGUSR1 */
167 SA_KILL, /* SIGUSR2 */
168};
169
170/*
171 * Determine signal that should be delivered to process p, the current
172 * process, 0 if none. If there is a pending stop signal with default
173 * action, the process stops in issignal().
174 *
175 * MP SAFE.
176 */
177int
178CURSIG(struct proc *p)
179{
180 sigset_t tmpset;
181 int r;
182
183 if (SIGISEMPTY(p->p_siglist))
184 return (0);
185 tmpset = p->p_siglist;
186 SIGSETNAND(tmpset, p->p_sigmask);
187 if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
188 return (0);
189 mtx_lock(&Giant);
190 r = issignal(p);
191 mtx_unlock(&Giant);
192 return (r);
193}
194
195static __inline int
196sigprop(int sig)
197{
198
199 if (sig > 0 && sig < NSIG)
200 return (sigproptbl[_SIG_IDX(sig)]);
201 return (0);
202}
203
204static __inline int
205sig_ffs(sigset_t *set)
206{
207 int i;
208
209 for (i = 0; i < _SIG_WORDS; i++)
210 if (set->__bits[i])
211 return (ffs(set->__bits[i]) + (i * 32));
212 return (0);
213}
214
215/*
216 * do_sigaction
217 * sigaction
218 * osigaction
219 */
220static int
221do_sigaction(p, sig, act, oact, old)
222 struct proc *p;
223 register int sig;
224 struct sigaction *act, *oact;
225 int old;
226{
227 register struct sigacts *ps = p->p_sigacts;
228
229 if (sig <= 0 || sig > _SIG_MAXSIG)
230 return (EINVAL);
231
232 if (oact) {
233 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
234 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
235 oact->sa_flags = 0;
236 if (SIGISMEMBER(ps->ps_sigonstack, sig))
237 oact->sa_flags |= SA_ONSTACK;
238 if (!SIGISMEMBER(ps->ps_sigintr, sig))
239 oact->sa_flags |= SA_RESTART;
240 if (SIGISMEMBER(ps->ps_sigreset, sig))
241 oact->sa_flags |= SA_RESETHAND;
242 if (SIGISMEMBER(ps->ps_signodefer, sig))
243 oact->sa_flags |= SA_NODEFER;
244 if (SIGISMEMBER(ps->ps_siginfo, sig))
245 oact->sa_flags |= SA_SIGINFO;
246 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
247 oact->sa_flags |= SA_NOCLDSTOP;
248 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
249 oact->sa_flags |= SA_NOCLDWAIT;
250 }
251 if (act) {
252 if ((sig == SIGKILL || sig == SIGSTOP) &&
253 act->sa_handler != SIG_DFL)
254 return (EINVAL);
255
256 /*
257 * Change setting atomically.
258 */
259 (void) splhigh();
260
261 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
262 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
263 if (act->sa_flags & SA_SIGINFO) {
264 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
265 SIGADDSET(ps->ps_siginfo, sig);
266 } else {
267 ps->ps_sigact[_SIG_IDX(sig)] =
268 (__sighandler_t *)act->sa_sigaction;
269 SIGDELSET(ps->ps_siginfo, sig);
270 }
271 if (!(act->sa_flags & SA_RESTART))
272 SIGADDSET(ps->ps_sigintr, sig);
273 else
274 SIGDELSET(ps->ps_sigintr, sig);
275 if (act->sa_flags & SA_ONSTACK)
276 SIGADDSET(ps->ps_sigonstack, sig);
277 else
278 SIGDELSET(ps->ps_sigonstack, sig);
279 if (act->sa_flags & SA_RESETHAND)
280 SIGADDSET(ps->ps_sigreset, sig);
281 else
282 SIGDELSET(ps->ps_sigreset, sig);
283 if (act->sa_flags & SA_NODEFER)
284 SIGADDSET(ps->ps_signodefer, sig);
285 else
286 SIGDELSET(ps->ps_signodefer, sig);
287#ifdef COMPAT_SUNOS
288 if (act->sa_flags & SA_USERTRAMP)
289 SIGADDSET(ps->ps_usertramp, sig);
290 else
291 SIGDELSET(ps->ps_usertramp, seg);
292#endif
293 if (sig == SIGCHLD) {
294 if (act->sa_flags & SA_NOCLDSTOP)
295 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
296 else
297 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
298 if (act->sa_flags & SA_NOCLDWAIT) {
299 /*
300 * Paranoia: since SA_NOCLDWAIT is implemented
301 * by reparenting the dying child to PID 1 (and
302 * trust it to reap the zombie), PID 1 itself
303 * is forbidden to set SA_NOCLDWAIT.
304 */
305 if (p->p_pid == 1)
306 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
307 else
308 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
309 } else
310 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
311 }
312 /*
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.
317 */
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);
333 }
334 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
335 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
336 SIGDELSET(ps->ps_osigset, sig);
337 else
338 SIGADDSET(ps->ps_osigset, sig);
339
340 (void) spl0();
341 }
342 return (0);
343}
344
345#ifndef _SYS_SYSPROTO_H_
346struct sigaction_args {
347 int sig;
348 struct sigaction *act;
349 struct sigaction *oact;
350};
351#endif
352/* ARGSUSED */
353int
354sigaction(p, uap)
355 struct proc *p;
356 register struct sigaction_args *uap;
357{
358 struct sigaction act, oact;
359 register struct sigaction *actp, *oactp;
360 int error;
361
362 actp = (uap->act != NULL) ? &act : NULL;
363 oactp = (uap->oact != NULL) ? &oact : NULL;
364 if (actp) {
365 error = copyin(uap->act, actp, sizeof(act));
366 if (error)
367 return (error);
368 }
369 error = do_sigaction(p, uap->sig, actp, oactp, 0);
370 if (oactp && !error) {
371 error = copyout(oactp, uap->oact, sizeof(oact));
372 }
373 return (error);
374}
375
376#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
377#ifndef _SYS_SYSPROTO_H_
378struct osigaction_args {
379 int signum;
380 struct osigaction *nsa;
381 struct osigaction *osa;
382};
383#endif
384/* ARGSUSED */
385int
386osigaction(p, uap)
387 struct proc *p;
388 register struct osigaction_args *uap;
389{
390 struct osigaction sa;
391 struct sigaction nsa, osa;
392 register struct sigaction *nsap, *osap;
393 int error;
394
395 if (uap->signum <= 0 || uap->signum >= ONSIG)
396 return (EINVAL);
397 nsap = (uap->nsa != NULL) ? &nsa : NULL;
398 osap = (uap->osa != NULL) ? &osa : NULL;
399 if (nsap) {
400 error = copyin(uap->nsa, &sa, sizeof(sa));
401 if (error)
402 return (error);
403 nsap->sa_handler = sa.sa_handler;
404 nsap->sa_flags = sa.sa_flags;
405 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
406 }
407 error = do_sigaction(p, uap->signum, nsap, osap, 1);
408 if (osap && !error) {
409 sa.sa_handler = osap->sa_handler;
410 sa.sa_flags = osap->sa_flags;
411 SIG2OSIG(osap->sa_mask, sa.sa_mask);
412 error = copyout(&sa, uap->osa, sizeof(sa));
413 }
414 return (error);
415}
416#endif /* COMPAT_43 */
417
418/*
419 * Initialize signal state for process 0;
420 * set to ignore signals that are ignored by default.
421 */
422void
423siginit(p)
424 struct proc *p;
425{
426 register int i;
427
428 for (i = 1; i <= NSIG; i++)
429 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
430 SIGADDSET(p->p_sigignore, i);
431}
432
433/*
434 * Reset signals for an exec of the specified process.
435 */
436void
437execsigs(p)
438 register struct proc *p;
439{
440 register struct sigacts *ps = p->p_sigacts;
441 register int sig;
442
443 /*
444 * Reset caught signals. Held signals remain held
445 * through p_sigmask (unless they were caught,
446 * and are now ignored by default).
447 */
448 while (SIGNOTEMPTY(p->p_sigcatch)) {
449 sig = sig_ffs(&p->p_sigcatch);
450 SIGDELSET(p->p_sigcatch, sig);
451 if (sigprop(sig) & SA_IGNORE) {
452 if (sig != SIGCONT)
453 SIGADDSET(p->p_sigignore, sig);
454 SIGDELSET(p->p_siglist, sig);
455 }
456 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
457 }
458 /*
459 * Reset stack state to the user stack.
460 * Clear set of signals caught on the signal stack.
461 */
462 p->p_sigstk.ss_flags = SS_DISABLE;
463 p->p_sigstk.ss_size = 0;
464 p->p_sigstk.ss_sp = 0;
465 /*
466 * Reset no zombies if child dies flag as Solaris does.
467 */
468 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
469}
470
471/*
472 * do_sigprocmask() - MP SAFE ONLY IF p == curproc
473 *
474 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
475 * p == curproc. Also remember that in order to remain MP SAFE
476 * no spl*() calls may be made.
477 */
478static int
479do_sigprocmask(p, how, set, oset, old)
480 struct proc *p;
481 int how;
482 sigset_t *set, *oset;
483 int old;
484{
485 int error;
486
487 if (oset != NULL)
488 *oset = p->p_sigmask;
489
490 error = 0;
491 if (set != NULL) {
492 switch (how) {
493 case SIG_BLOCK:
494 SIG_CANTMASK(*set);
495 SIGSETOR(p->p_sigmask, *set);
496 break;
497 case SIG_UNBLOCK:
498 SIGSETNAND(p->p_sigmask, *set);
499 break;
500 case SIG_SETMASK:
501 SIG_CANTMASK(*set);
502 if (old)
503 SIGSETLO(p->p_sigmask, *set);
504 else
505 p->p_sigmask = *set;
506 break;
507 default:
508 error = EINVAL;
509 break;
510 }
511 }
512 return (error);
513}
514
515/*
516 * sigprocmask() - MP SAFE
517 */
518
519#ifndef _SYS_SYSPROTO_H_
520struct sigprocmask_args {
521 int how;
522 const sigset_t *set;
523 sigset_t *oset;
524};
525#endif
526int
527sigprocmask(p, uap)
528 register struct proc *p;
529 struct sigprocmask_args *uap;
530{
531 sigset_t set, oset;
532 sigset_t *setp, *osetp;
533 int error;
534
535 setp = (uap->set != NULL) ? &set : NULL;
536 osetp = (uap->oset != NULL) ? &oset : NULL;
537 if (setp) {
538 error = copyin(uap->set, setp, sizeof(set));
539 if (error)
540 return (error);
541 }
542 error = do_sigprocmask(p, uap->how, setp, osetp, 0);
543 if (osetp && !error) {
544 error = copyout(osetp, uap->oset, sizeof(oset));
545 }
546 return (error);
547}
548
549#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
550/*
551 * osigprocmask() - MP SAFE
552 */
553#ifndef _SYS_SYSPROTO_H_
554struct osigprocmask_args {
555 int how;
556 osigset_t mask;
557};
558#endif
559int
560osigprocmask(p, uap)
561 register struct proc *p;
562 struct osigprocmask_args *uap;
563{
564 sigset_t set, oset;
565 int error;
566
567 OSIG2SIG(uap->mask, set);
568 error = do_sigprocmask(p, uap->how, &set, &oset, 1);
569 SIG2OSIG(oset, p->p_retval[0]);
570 return (error);
571}
572#endif /* COMPAT_43 */
573
574#ifndef _SYS_SYSPROTO_H_
575struct sigpending_args {
576 sigset_t *set;
577};
578#endif
579/* ARGSUSED */
580int
581sigpending(p, uap)
582 struct proc *p;
583 struct sigpending_args *uap;
584{
585
586 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
587}
588
589#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
590#ifndef _SYS_SYSPROTO_H_
591struct osigpending_args {
592 int dummy;
593};
594#endif
595/* ARGSUSED */
596int
597osigpending(p, uap)
598 struct proc *p;
599 struct osigpending_args *uap;
600{
601
602 SIG2OSIG(p->p_siglist, p->p_retval[0]);
603 return (0);
604}
605#endif /* COMPAT_43 */
606
607#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
608/*
609 * Generalized interface signal handler, 4.3-compatible.
610 */
611#ifndef _SYS_SYSPROTO_H_
612struct osigvec_args {
613 int signum;
614 struct sigvec *nsv;
615 struct sigvec *osv;
616};
617#endif
618/* ARGSUSED */
619int
620osigvec(p, uap)
621 struct proc *p;
622 register struct osigvec_args *uap;
623{
624 struct sigvec vec;
625 struct sigaction nsa, osa;
626 register struct sigaction *nsap, *osap;
627 int error;
628
629 if (uap->signum <= 0 || uap->signum >= ONSIG)
630 return (EINVAL);
631 nsap = (uap->nsv != NULL) ? &nsa : NULL;
632 osap = (uap->osv != NULL) ? &osa : NULL;
633 if (nsap) {
634 error = copyin(uap->nsv, &vec, sizeof(vec));
635 if (error)
636 return (error);
637 nsap->sa_handler = vec.sv_handler;
638 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
639 nsap->sa_flags = vec.sv_flags;
640 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
641#ifdef COMPAT_SUNOS
642 nsap->sa_flags |= SA_USERTRAMP;
643#endif
644 }
645 error = do_sigaction(p, uap->signum, nsap, osap, 1);
646 if (osap && !error) {
647 vec.sv_handler = osap->sa_handler;
648 SIG2OSIG(osap->sa_mask, vec.sv_mask);
649 vec.sv_flags = osap->sa_flags;
650 vec.sv_flags &= ~SA_NOCLDWAIT;
651 vec.sv_flags ^= SA_RESTART;
652#ifdef COMPAT_SUNOS
653 vec.sv_flags &= ~SA_NOCLDSTOP;
654#endif
655 error = copyout(&vec, uap->osv, sizeof(vec));
656 }
657 return (error);
658}
659
660#ifndef _SYS_SYSPROTO_H_
661struct osigblock_args {
662 int mask;
663};
664#endif
665int
666osigblock(p, uap)
667 register struct proc *p;
668 struct osigblock_args *uap;
669{
670 sigset_t set;
671
672 OSIG2SIG(uap->mask, set);
673 SIG_CANTMASK(set);
674 (void) splhigh();
675 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
676 SIGSETOR(p->p_sigmask, set);
677 (void) spl0();
678 return (0);
679}
680
681#ifndef _SYS_SYSPROTO_H_
682struct osigsetmask_args {
683 int mask;
684};
685#endif
686int
687osigsetmask(p, uap)
688 struct proc *p;
689 struct osigsetmask_args *uap;
690{
691 sigset_t set;
692
693 OSIG2SIG(uap->mask, set);
694 SIG_CANTMASK(set);
695 (void) splhigh();
696 SIG2OSIG(p->p_sigmask, p->p_retval[0]);
697 SIGSETLO(p->p_sigmask, set);
698 (void) spl0();
699 return (0);
700}
701#endif /* COMPAT_43 || COMPAT_SUNOS */
702
703/*
704 * Suspend process until signal, providing mask to be set
705 * in the meantime. Note nonstandard calling convention:
706 * libc stub passes mask, not pointer, to save a copyin.
707 */
708#ifndef _SYS_SYSPROTO_H_
709struct sigsuspend_args {
710 const sigset_t *sigmask;
711};
712#endif
713/* ARGSUSED */
714int
715sigsuspend(p, uap)
716 register struct proc *p;
717 struct sigsuspend_args *uap;
718{
719 sigset_t mask;
720 register struct sigacts *ps = p->p_sigacts;
721 int error;
722
723 error = copyin(uap->sigmask, &mask, sizeof(mask));
724 if (error)
725 return (error);
726
727 /*
728 * When returning from sigsuspend, we want
729 * the old mask to be restored after the
730 * signal handler has finished. Thus, we
731 * save it here and mark the sigacts structure
732 * to indicate this.
733 */
734 p->p_oldsigmask = p->p_sigmask;
735 p->p_flag |= P_OLDMASK;
736
737 SIG_CANTMASK(mask);
738 p->p_sigmask = mask;
739 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
740 /* void */;
741 /* always return EINTR rather than ERESTART... */
742 return (EINTR);
743}
744
745#ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
746#ifndef _SYS_SYSPROTO_H_
747struct osigsuspend_args {
748 osigset_t mask;
749};
750#endif
751/* ARGSUSED */
752int
753osigsuspend(p, uap)
754 register struct proc *p;
755 struct osigsuspend_args *uap;
756{
757 sigset_t mask;
758 register struct sigacts *ps = p->p_sigacts;
759
760 p->p_oldsigmask = p->p_sigmask;
761 p->p_flag |= P_OLDMASK;
762 OSIG2SIG(uap->mask, mask);
763 SIG_CANTMASK(mask);
764 SIGSETLO(p->p_sigmask, mask);
765 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
766 /* void */;
767 /* always return EINTR rather than ERESTART... */
768 return (EINTR);
769}
770#endif /* COMPAT_43 */
771
772#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
773#ifndef _SYS_SYSPROTO_H_
774struct osigstack_args {
775 struct sigstack *nss;
776 struct sigstack *oss;
777};
778#endif
779/* ARGSUSED */
780int
781osigstack(p, uap)
782 struct proc *p;
783 register struct osigstack_args *uap;
784{
785 struct sigstack ss;
786 int error;
787
788 if (uap->oss != NULL) {
789 ss.ss_sp = p->p_sigstk.ss_sp;
790 ss.ss_onstack = sigonstack(cpu_getstack(p));
791 error = copyout(&ss, uap->oss, sizeof(struct sigstack));
792 if (error)
793 return (error);
794 }
795
796 if (uap->nss != NULL) {
797 if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
798 return (error);
799 p->p_sigstk.ss_sp = ss.ss_sp;
800 p->p_sigstk.ss_size = 0;
801 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
802 p->p_flag |= P_ALTSTACK;
803 }
804 return (0);
805}
806#endif /* COMPAT_43 || COMPAT_SUNOS */
807
808#ifndef _SYS_SYSPROTO_H_
809struct sigaltstack_args {
810 stack_t *ss;
811 stack_t *oss;
812};
813#endif
814/* ARGSUSED */
815int
816sigaltstack(p, uap)
817 struct proc *p;
818 register struct sigaltstack_args *uap;
819{
820 stack_t ss;
821 int error, oonstack;
822
823 oonstack = sigonstack(cpu_getstack(p));
824
825 if (uap->oss != NULL) {
826 ss = p->p_sigstk;
827 ss.ss_flags = (p->p_flag & P_ALTSTACK)
828 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
829 if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
830 return (error);
831 }
832
833 if (uap->ss != NULL) {
834 if (oonstack)
835 return (EPERM);
836 if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
837 return (error);
838 if ((ss.ss_flags & ~SS_DISABLE) != 0)
839 return (EINVAL);
840 if (!(ss.ss_flags & SS_DISABLE)) {
841 if (ss.ss_size < p->p_sysent->sv_minsigstksz)
842 return (ENOMEM);
843 p->p_sigstk = ss;
844 p->p_flag |= P_ALTSTACK;
845 } else
846 p->p_flag &= ~P_ALTSTACK;
847 }
848 return (0);
849}
850
851/*
852 * Common code for kill process group/broadcast kill.
853 * cp is calling process.
854 */
855int
856killpg1(cp, sig, pgid, all)
857 register struct proc *cp;
858 int sig, pgid, all;
859{
860 register struct proc *p;
861 struct pgrp *pgrp;
862 int nfound = 0;
863
864 if (all) {
865 /*
866 * broadcast
867 */
868 ALLPROC_LOCK(AP_SHARED);
869 LIST_FOREACH(p, &allproc, p_list) {
870 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
871 p == cp || !CANSIGNAL(cp, p, sig))
872 continue;
873 nfound++;
874 if (sig)
875 psignal(p, sig);
876 }
877 ALLPROC_LOCK(AP_RELEASE);
878 } else {
879 if (pgid == 0)
880 /*
881 * zero pgid means send to my process group.
882 */
883 pgrp = cp->p_pgrp;
884 else {
885 pgrp = pgfind(pgid);
886 if (pgrp == NULL)
887 return (ESRCH);
888 }
889 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
890 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
891 p->p_stat == SZOMB ||
892 !CANSIGNAL(cp, p, sig))
893 continue;
894 nfound++;
895 if (sig)
896 psignal(p, sig);
897 }
898 }
899 return (nfound ? 0 : ESRCH);
900}
901
902#ifndef _SYS_SYSPROTO_H_
903struct kill_args {
904 int pid;
905 int signum;
906};
907#endif
908/* ARGSUSED */
909int
910kill(cp, uap)
911 register struct proc *cp;
912 register struct kill_args *uap;
913{
914 register struct proc *p;
915
916 if ((u_int)uap->signum > _SIG_MAXSIG)
917 return (EINVAL);
918 if (uap->pid > 0) {
919 /* kill single process */
920 if ((p = pfind(uap->pid)) == NULL)
921 return (ESRCH);
922 if (!CANSIGNAL(cp, p, uap->signum))
923 return (EPERM);
924 if (uap->signum)
925 psignal(p, uap->signum);
926 return (0);
927 }
928 switch (uap->pid) {
929 case -1: /* broadcast signal */
930 return (killpg1(cp, uap->signum, 0, 1));
931 case 0: /* signal own process group */
932 return (killpg1(cp, uap->signum, 0, 0));
933 default: /* negative explicit process group */
934 return (killpg1(cp, uap->signum, -uap->pid, 0));
935 }
936 /* NOTREACHED */
937}
938
939#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
940#ifndef _SYS_SYSPROTO_H_
941struct okillpg_args {
942 int pgid;
943 int signum;
944};
945#endif
946/* ARGSUSED */
947int
948okillpg(p, uap)
949 struct proc *p;
950 register struct okillpg_args *uap;
951{
952
953 if ((u_int)uap->signum > _SIG_MAXSIG)
954 return (EINVAL);
955 return (killpg1(p, uap->signum, uap->pgid, 0));
956}
957#endif /* COMPAT_43 || COMPAT_SUNOS */
958
959/*
960 * Send a signal to a process group.
961 */
962void
963gsignal(pgid, sig)
964 int pgid, sig;
965{
966 struct pgrp *pgrp;
967
968 if (pgid && (pgrp = pgfind(pgid)))
969 pgsignal(pgrp, sig, 0);
970}
971
972/*
973 * Send a signal to a process group. If checktty is 1,
974 * limit to members which have a controlling terminal.
975 */
976void
977pgsignal(pgrp, sig, checkctty)
978 struct pgrp *pgrp;
979 int sig, checkctty;
980{
981 register struct proc *p;
982
983 if (pgrp)
984 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
985 if (checkctty == 0 || p->p_flag & P_CONTROLT)
986 psignal(p, sig);
987}
988
989/*
990 * Send a signal caused by a trap to the current process.
991 * If it will be caught immediately, deliver it with correct code.
992 * Otherwise, post it normally.
993 */
994void
995trapsignal(p, sig, code)
996 struct proc *p;
997 register int sig;
998 u_long code;
999{
1000 register struct sigacts *ps = p->p_sigacts;
1001
1002 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1003 !SIGISMEMBER(p->p_sigmask, sig)) {
1004 p->p_stats->p_ru.ru_nsignals++;
1005#ifdef KTRACE
1006 if (KTRPOINT(p, KTR_PSIG))
1007 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
1008 &p->p_sigmask, code);
1009#endif
1010 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1011 &p->p_sigmask, code);
1012 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1013 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1014 SIGADDSET(p->p_sigmask, sig);
1015 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1016 /*
1017 * See do_sigaction() for origin of this code.
1018 */
1019 SIGDELSET(p->p_sigcatch, sig);
1020 if (sig != SIGCONT &&
1021 sigprop(sig) & SA_IGNORE)
1022 SIGADDSET(p->p_sigignore, sig);
1023 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1024 }
1025 } else {
1026 p->p_code = code; /* XXX for core dump/debugger */
1027 p->p_sig = sig; /* XXX to verify code */
1028 psignal(p, sig);
1029 }
1030}
1031
1032/*
1033 * Send the signal to the process. If the signal has an action, the action
1034 * is usually performed by the target process rather than the caller; we add
1035 * the signal to the set of pending signals for the process.
1036 *
1037 * Exceptions:
1038 * o When a stop signal is sent to a sleeping process that takes the
1039 * default action, the process is stopped without awakening it.
1040 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1041 * regardless of the signal action (eg, blocked or ignored).
1042 *
1043 * Other ignored signals are discarded immediately.
1044 */
1045void
1046psignal(p, sig)
1047 register struct proc *p;
1048 register int sig;
1049{
1050 register int prop;
1051 register sig_t action;
1052
1053 if (sig > _SIG_MAXSIG || sig <= 0) {
1054 printf("psignal: signal %d\n", sig);
1055 panic("psignal signal number");
1056 }
1057
1058 PROC_LOCK(p);
1059 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1060
1061 prop = sigprop(sig);
1062
1063 /*
1064 * If proc is traced, always give parent a chance;
1065 * if signal event is tracked by procfs, give *that*
1066 * a chance, as well.
1067 */
1068 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1069 action = SIG_DFL;
1070 else {
1071 /*
1072 * If the signal is being ignored,
1073 * then we forget about it immediately.
1074 * (Note: we don't set SIGCONT in p_sigignore,
1075 * and if it is set to SIG_IGN,
1076 * action will be SIG_DFL here.)
1077 */
1078 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) {
1079 PROC_UNLOCK(p);
1080 return;
1081 }
1082 if (SIGISMEMBER(p->p_sigmask, sig))
1083 action = SIG_HOLD;
1084 else if (SIGISMEMBER(p->p_sigcatch, sig))
1085 action = SIG_CATCH;
1086 else
1087 action = SIG_DFL;
1088 }
1089
1090 mtx_lock_spin(&sched_lock);
1091 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1092 (p->p_flag & P_TRACED) == 0)
1093 p->p_nice = NZERO;
1094 mtx_unlock_spin(&sched_lock);
1095
1096 if (prop & SA_CONT)
1097 SIG_STOPSIGMASK(p->p_siglist);
1098
1099 if (prop & SA_STOP) {
1100 /*
1101 * If sending a tty stop signal to a member of an orphaned
1102 * process group, discard the signal here if the action
1103 * is default; don't stop the process below if sleeping,
1104 * and don't clear any pending SIGCONT.
1105 */
1106 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1107 action == SIG_DFL) {
1108 PROC_UNLOCK(p);
1109 return;
1110 }
1111 SIG_CONTSIGMASK(p->p_siglist);
1112 }
1113 SIGADDSET(p->p_siglist, sig);
1114
1115 /*
1116 * Defer further processing for signals which are held,
1117 * except that stopped processes must be continued by SIGCONT.
1118 */
1119 mtx_lock_spin(&sched_lock);
1120 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
1121 mtx_unlock_spin(&sched_lock);
1122 PROC_UNLOCK(p);
1123 return;
1124 }
1125 switch (p->p_stat) {
1126
1127 case SSLEEP:
1128 /*
1129 * If process is sleeping uninterruptibly
1130 * we can't interrupt the sleep... the signal will
1131 * be noticed when the process returns through
1132 * trap() or syscall().
1133 */
1134 if ((p->p_sflag & PS_SINTR) == 0) {
1135 mtx_unlock_spin(&sched_lock);
1136 goto out;
1137 }
1138 /*
1139 * Process is sleeping and traced... make it runnable
1140 * so it can discover the signal in issignal() and stop
1141 * for the parent.
1142 */
1143 if (p->p_flag & P_TRACED)
1144 goto run;
1145 mtx_unlock_spin(&sched_lock);
1146 /*
1147 * If SIGCONT is default (or ignored) and process is
1148 * asleep, we are finished; the process should not
1149 * be awakened.
1150 */
1151 if ((prop & SA_CONT) && action == SIG_DFL) {
1152 SIGDELSET(p->p_siglist, sig);
1153 goto out;
1154 }
1155 /*
1156 * When a sleeping process receives a stop
1157 * signal, process immediately if possible.
1158 * All other (caught or default) signals
1159 * cause the process to run.
1160 */
1161 if (prop & SA_STOP) {
1162 if (action != SIG_DFL)
1163 goto runfast;
1164 /*
1165 * If a child holding parent blocked,
1166 * stopping could cause deadlock.
1167 */
1168 if (p->p_flag & P_PPWAIT)
1169 goto out;
1170 SIGDELSET(p->p_siglist, sig);
1171 p->p_xstat = sig;
1172 PROC_UNLOCK(p);
1173 PROCTREE_LOCK(PT_SHARED);
1174 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1175 psignal(p->p_pptr, SIGCHLD);
1176 stop(p);
1177 PROCTREE_LOCK(PT_RELEASE);
1178 PROC_LOCK(p);
1179 goto out;
1180 } else
1181 goto runfast;
1182 /* NOTREACHED */
1183
1184 case SSTOP:
1185 mtx_unlock_spin(&sched_lock);
1186 /*
1187 * If traced process is already stopped,
1188 * then no further action is necessary.
1189 */
1190 if (p->p_flag & P_TRACED)
1191 goto out;
1192
1193 /*
1194 * Kill signal always sets processes running.
1195 */
1196 if (sig == SIGKILL)
1197 goto runfast;
1198
1199 if (prop & SA_CONT) {
1200 /*
1201 * If SIGCONT is default (or ignored), we continue the
1202 * process but don't leave the signal in p_siglist, as
1203 * it has no further action. If SIGCONT is held, we
1204 * continue the process and leave the signal in
1205 * p_siglist. If the process catches SIGCONT, let it
1206 * handle the signal itself. If it isn't waiting on
1207 * an event, then it goes back to run state.
1208 * Otherwise, process goes back to sleep state.
1209 */
1210 if (action == SIG_DFL)
1211 SIGDELSET(p->p_siglist, sig);
1212 if (action == SIG_CATCH)
1213 goto runfast;
1214 mtx_lock_spin(&sched_lock);
1215 if (p->p_wchan == NULL)
1216 goto run;
1217 p->p_stat = SSLEEP;
1218 mtx_unlock_spin(&sched_lock);
1219 goto out;
1220 }
1221
1222 if (prop & SA_STOP) {
1223 /*
1224 * Already stopped, don't need to stop again.
1225 * (If we did the shell could get confused.)
1226 */
1227 SIGDELSET(p->p_siglist, sig);
1228 goto out;
1229 }
1230
1231 /*
1232 * If process is sleeping interruptibly, then simulate a
1233 * wakeup so that when it is continued, it will be made
1234 * runnable and can look at the signal. But don't make
1235 * the process runnable, leave it stopped.
1236 */
1237 mtx_lock_spin(&sched_lock);
1238 if (p->p_wchan && p->p_sflag & PS_SINTR) {
1239 if (p->p_sflag & PS_CVWAITQ)
1240 cv_waitq_remove(p);
1241 else
1242 unsleep(p);
1243 }
1244 mtx_unlock_spin(&sched_lock);
1245 goto out;
1246
1247 default:
1248 /*
1249 * SRUN, SIDL, SZOMB do nothing with the signal,
1250 * other than kicking ourselves if we are running.
1251 * It will either never be noticed, or noticed very soon.
1252 */
1253 if (p == curproc) {
1254 mtx_unlock_spin(&sched_lock);
1255 signotify(p);
1254 signotify(p);
1255 mtx_unlock_spin(&sched_lock);
1256 }
1257#ifdef SMP
1258 else if (p->p_stat == SRUN) {
1259 mtx_unlock_spin(&sched_lock);
1260 forward_signal(p);
1261 }
1262#endif
1263 else
1264 mtx_unlock_spin(&sched_lock);
1265 goto out;
1266 }
1267 /*NOTREACHED*/
1268
1269runfast:
1270 /*
1271 * Raise priority to at least PUSER.
1272 */
1273 mtx_lock_spin(&sched_lock);
1274 if (p->p_priority > PUSER)
1275 p->p_priority = PUSER;
1276run:
1277 /* If we jump here, sched_lock has to be owned. */
1278 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1279 setrunnable(p);
1280 mtx_unlock_spin(&sched_lock);
1281out:
1282 /* If we jump here, sched_lock should not be owned. */
1283 mtx_assert(&sched_lock, MA_NOTOWNED);
1284 PROC_UNLOCK(p);
1285}
1286
1287/*
1288 * If the current process has received a signal (should be caught or cause
1289 * termination, should interrupt current syscall), return the signal number.
1290 * Stop signals with default action are processed immediately, then cleared;
1291 * they aren't returned. This is checked after each entry to the system for
1292 * a syscall or trap (though this can usually be done without calling issignal
1293 * by checking the pending signal masks in the CURSIG macro.) The normal call
1294 * sequence is
1295 *
1296 * while (sig = CURSIG(curproc))
1297 * postsig(sig);
1298 */
1299int
1300issignal(p)
1301 register struct proc *p;
1302{
1303 sigset_t mask;
1304 register int sig, prop;
1305
1306 for (;;) {
1307 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1308
1309 mask = p->p_siglist;
1310 SIGSETNAND(mask, p->p_sigmask);
1311 if (p->p_flag & P_PPWAIT)
1312 SIG_STOPSIGMASK(mask);
1313 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1314 return (0);
1315 sig = sig_ffs(&mask);
1316 prop = sigprop(sig);
1317
1318 STOPEVENT(p, S_SIG, sig);
1319
1320 /*
1321 * We should see pending but ignored signals
1322 * only if P_TRACED was on when they were posted.
1323 */
1324 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1325 SIGDELSET(p->p_siglist, sig);
1326 continue;
1327 }
1328 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1329 /*
1330 * If traced, always stop, and stay
1331 * stopped until released by the parent.
1332 */
1333 p->p_xstat = sig;
1334 PROCTREE_LOCK(PT_SHARED);
1335 psignal(p->p_pptr, SIGCHLD);
1336 do {
1337 stop(p);
1338 PROCTREE_LOCK(PT_RELEASE);
1339 mtx_lock_spin(&sched_lock);
1340 DROP_GIANT_NOSWITCH();
1341 mi_switch();
1342 mtx_unlock_spin(&sched_lock);
1343 PICKUP_GIANT();
1344 PROCTREE_LOCK(PT_SHARED);
1345 } while (!trace_req(p)
1346 && p->p_flag & P_TRACED);
1347 PROCTREE_LOCK(PT_RELEASE);
1348
1349 /*
1350 * If the traced bit got turned off, go back up
1351 * to the top to rescan signals. This ensures
1352 * that p_sig* and ps_sigact are consistent.
1353 */
1354 if ((p->p_flag & P_TRACED) == 0)
1355 continue;
1356
1357 /*
1358 * If parent wants us to take the signal,
1359 * then it will leave it in p->p_xstat;
1360 * otherwise we just look for signals again.
1361 */
1362 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1363 sig = p->p_xstat;
1364 if (sig == 0)
1365 continue;
1366
1367 /*
1368 * Put the new signal into p_siglist. If the
1369 * signal is being masked, look for other signals.
1370 */
1371 SIGADDSET(p->p_siglist, sig);
1372 if (SIGISMEMBER(p->p_sigmask, sig))
1373 continue;
1374 }
1375
1376 /*
1377 * Decide whether the signal should be returned.
1378 * Return the signal's number, or fall through
1379 * to clear it from the pending mask.
1380 */
1381 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1382
1383 case (int)SIG_DFL:
1384 /*
1385 * Don't take default actions on system processes.
1386 */
1387 if (p->p_pid <= 1) {
1388#ifdef DIAGNOSTIC
1389 /*
1390 * Are you sure you want to ignore SIGSEGV
1391 * in init? XXX
1392 */
1393 printf("Process (pid %lu) got signal %d\n",
1394 (u_long)p->p_pid, sig);
1395#endif
1396 break; /* == ignore */
1397 }
1398 /*
1399 * If there is a pending stop signal to process
1400 * with default action, stop here,
1401 * then clear the signal. However,
1402 * if process is member of an orphaned
1403 * process group, ignore tty stop signals.
1404 */
1405 if (prop & SA_STOP) {
1406 if (p->p_flag & P_TRACED ||
1407 (p->p_pgrp->pg_jobc == 0 &&
1408 prop & SA_TTYSTOP))
1409 break; /* == ignore */
1410 p->p_xstat = sig;
1411 PROCTREE_LOCK(PT_SHARED);
1412 stop(p);
1413 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1414 psignal(p->p_pptr, SIGCHLD);
1415 PROCTREE_LOCK(PT_RELEASE);
1416 mtx_lock_spin(&sched_lock);
1417 DROP_GIANT_NOSWITCH();
1418 mi_switch();
1419 mtx_unlock_spin(&sched_lock);
1420 PICKUP_GIANT();
1421 break;
1422 } else if (prop & SA_IGNORE) {
1423 /*
1424 * Except for SIGCONT, shouldn't get here.
1425 * Default action is to ignore; drop it.
1426 */
1427 break; /* == ignore */
1428 } else
1429 return (sig);
1430 /*NOTREACHED*/
1431
1432 case (int)SIG_IGN:
1433 /*
1434 * Masking above should prevent us ever trying
1435 * to take action on an ignored signal other
1436 * than SIGCONT, unless process is traced.
1437 */
1438 if ((prop & SA_CONT) == 0 &&
1439 (p->p_flag & P_TRACED) == 0)
1440 printf("issignal\n");
1441 break; /* == ignore */
1442
1443 default:
1444 /*
1445 * This signal has an action, let
1446 * postsig() process it.
1447 */
1448 return (sig);
1449 }
1450 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1451 }
1452 /* NOTREACHED */
1453}
1454
1455/*
1456 * Put the argument process into the stopped state and notify the parent
1457 * via wakeup. Signals are handled elsewhere. The process must not be
1458 * on the run queue. Must be called with at least a shared hold of the
1459 * proctree lock.
1460 */
1461void
1462stop(p)
1463 register struct proc *p;
1464{
1465
1466 PROCTREE_ASSERT(PT_SHARED);
1467 mtx_lock_spin(&sched_lock);
1468 p->p_stat = SSTOP;
1469 p->p_flag &= ~P_WAITED;
1470 wakeup((caddr_t)p->p_pptr);
1471 mtx_unlock_spin(&sched_lock);
1472}
1473
1474/*
1475 * Take the action for the specified signal
1476 * from the current set of pending signals.
1477 */
1478void
1479postsig(sig)
1480 register int sig;
1481{
1482 register struct proc *p = curproc;
1483 struct sigacts *ps = p->p_sigacts;
1484 sig_t action;
1485 sigset_t returnmask;
1486 int code;
1487
1488 KASSERT(sig != 0, ("postsig"));
1489
1490 SIGDELSET(p->p_siglist, sig);
1491 action = ps->ps_sigact[_SIG_IDX(sig)];
1492#ifdef KTRACE
1493 if (KTRPOINT(p, KTR_PSIG))
1494 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1495 &p->p_oldsigmask : &p->p_sigmask, 0);
1496#endif
1497 STOPEVENT(p, S_SIG, sig);
1498
1499 if (action == SIG_DFL) {
1500 /*
1501 * Default action, where the default is to kill
1502 * the process. (Other cases were ignored above.)
1503 */
1504 sigexit(p, sig);
1505 /* NOTREACHED */
1506 } else {
1507 /*
1508 * If we get here, the signal must be caught.
1509 */
1510 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1511 ("postsig action"));
1512 /*
1513 * Set the new mask value and also defer further
1514 * occurrences of this signal.
1515 *
1516 * Special case: user has done a sigsuspend. Here the
1517 * current mask is not of interest, but rather the
1518 * mask from before the sigsuspend is what we want
1519 * restored after the signal processing is completed.
1520 */
1521 (void) splhigh();
1522 if (p->p_flag & P_OLDMASK) {
1523 returnmask = p->p_oldsigmask;
1524 p->p_flag &= ~P_OLDMASK;
1525 } else
1526 returnmask = p->p_sigmask;
1527
1528 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1529 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1530 SIGADDSET(p->p_sigmask, sig);
1531
1532 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1533 /*
1534 * See do_sigaction() for origin of this code.
1535 */
1536 SIGDELSET(p->p_sigcatch, sig);
1537 if (sig != SIGCONT &&
1538 sigprop(sig) & SA_IGNORE)
1539 SIGADDSET(p->p_sigignore, sig);
1540 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1541 }
1542 (void) spl0();
1543 p->p_stats->p_ru.ru_nsignals++;
1544 if (p->p_sig != sig) {
1545 code = 0;
1546 } else {
1547 code = p->p_code;
1548 p->p_code = 0;
1549 p->p_sig = 0;
1550 }
1551 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1552 }
1553}
1554
1555/*
1556 * Kill the current process for stated reason.
1557 */
1558void
1559killproc(p, why)
1560 struct proc *p;
1561 char *why;
1562{
1563 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
1564 p, p->p_pid, p->p_comm);
1565 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1566 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1567 psignal(p, SIGKILL);
1568}
1569
1570/*
1571 * Force the current process to exit with the specified signal, dumping core
1572 * if appropriate. We bypass the normal tests for masked and caught signals,
1573 * allowing unrecoverable failures to terminate the process without changing
1574 * signal state. Mark the accounting record with the signal termination.
1575 * If dumping core, save the signal number for the debugger. Calls exit and
1576 * does not return.
1577 */
1578void
1579sigexit(p, sig)
1580 register struct proc *p;
1581 int sig;
1582{
1583
1584 p->p_acflag |= AXSIG;
1585 if (sigprop(sig) & SA_CORE) {
1586 p->p_sig = sig;
1587 /*
1588 * Log signals which would cause core dumps
1589 * (Log as LOG_INFO to appease those who don't want
1590 * these messages.)
1591 * XXX : Todo, as well as euid, write out ruid too
1592 */
1593 if (coredump(p) == 0)
1594 sig |= WCOREFLAG;
1595 if (kern_logsigexit)
1596 log(LOG_INFO,
1597 "pid %d (%s), uid %d: exited on signal %d%s\n",
1598 p->p_pid, p->p_comm,
1599 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1600 sig &~ WCOREFLAG,
1601 sig & WCOREFLAG ? " (core dumped)" : "");
1602 }
1603 exit1(p, W_EXITCODE(0, sig));
1604 /* NOTREACHED */
1605}
1606
1607static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1608SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1609 sizeof(corefilename), "process corefile name format string");
1610
1611/*
1612 * expand_name(name, uid, pid)
1613 * Expand the name described in corefilename, using name, uid, and pid.
1614 * corefilename is a printf-like string, with three format specifiers:
1615 * %N name of process ("name")
1616 * %P process id (pid)
1617 * %U user id (uid)
1618 * For example, "%N.core" is the default; they can be disabled completely
1619 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1620 * This is controlled by the sysctl variable kern.corefile (see above).
1621 */
1622
1623static char *
1624expand_name(name, uid, pid)
1625const char *name; uid_t uid; pid_t pid; {
1626 char *temp;
1627 char buf[11]; /* Buffer for pid/uid -- max 4B */
1628 int i, n;
1629 char *format = corefilename;
1630 size_t namelen;
1631
1632 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1633 if (temp == NULL)
1634 return NULL;
1635 namelen = strlen(name);
1636 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1637 int l;
1638 switch (format[i]) {
1639 case '%': /* Format character */
1640 i++;
1641 switch (format[i]) {
1642 case '%':
1643 temp[n++] = '%';
1644 break;
1645 case 'N': /* process name */
1646 if ((n + namelen) > MAXPATHLEN) {
1647 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1648 pid, name, uid, temp, name);
1649 free(temp, M_TEMP);
1650 return NULL;
1651 }
1652 memcpy(temp+n, name, namelen);
1653 n += namelen;
1654 break;
1655 case 'P': /* process id */
1656 l = sprintf(buf, "%u", pid);
1657 if ((n + l) > MAXPATHLEN) {
1658 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1659 pid, name, uid, temp, name);
1660 free(temp, M_TEMP);
1661 return NULL;
1662 }
1663 memcpy(temp+n, buf, l);
1664 n += l;
1665 break;
1666 case 'U': /* user id */
1667 l = sprintf(buf, "%u", uid);
1668 if ((n + l) > MAXPATHLEN) {
1669 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1670 pid, name, uid, temp, name);
1671 free(temp, M_TEMP);
1672 return NULL;
1673 }
1674 memcpy(temp+n, buf, l);
1675 n += l;
1676 break;
1677 default:
1678 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1679 }
1680 break;
1681 default:
1682 temp[n++] = format[i];
1683 }
1684 }
1685 temp[n] = '\0';
1686 return temp;
1687}
1688
1689/*
1690 * Dump a process' core. The main routine does some
1691 * policy checking, and creates the name of the coredump;
1692 * then it passes on a vnode and a size limit to the process-specific
1693 * coredump routine if there is one; if there _is not_ one, it returns
1694 * ENOSYS; otherwise it returns the error from the process-specific routine.
1695 */
1696
1697static int
1698coredump(p)
1699 register struct proc *p;
1700{
1701 register struct vnode *vp;
1702 register struct ucred *cred = p->p_ucred;
1703 struct nameidata nd;
1704 struct vattr vattr;
1705 int error, error1, flags;
1706 struct mount *mp;
1707 char *name; /* name of corefile */
1708 off_t limit;
1709
1710 STOPEVENT(p, S_CORE, 0);
1711
1712 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1713 return (EFAULT);
1714
1715 /*
1716 * Note that the bulk of limit checking is done after
1717 * the corefile is created. The exception is if the limit
1718 * for corefiles is 0, in which case we don't bother
1719 * creating the corefile at all. This layout means that
1720 * a corefile is truncated instead of not being created,
1721 * if it is larger than the limit.
1722 */
1723 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1724 if (limit == 0)
1725 return 0;
1726
1727restart:
1728 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1729 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1730 flags = O_CREAT | FWRITE | O_NOFOLLOW;
1731 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1732 free(name, M_TEMP);
1733 if (error)
1734 return (error);
1735 NDFREE(&nd, NDF_ONLY_PNBUF);
1736 vp = nd.ni_vp;
1737 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1738 VOP_UNLOCK(vp, 0, p);
1739 if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1740 return (error);
1741 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1742 return (error);
1743 goto restart;
1744 }
1745
1746 /* Don't dump to non-regular files or files with links. */
1747 if (vp->v_type != VREG ||
1748 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1749 error = EFAULT;
1750 goto out;
1751 }
1752 VATTR_NULL(&vattr);
1753 vattr.va_size = 0;
1754 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1755 VOP_SETATTR(vp, &vattr, cred, p);
1756 p->p_acflag |= ACORE;
1757
1758 error = p->p_sysent->sv_coredump ?
1759 p->p_sysent->sv_coredump(p, vp, limit) :
1760 ENOSYS;
1761
1762out:
1763 VOP_UNLOCK(vp, 0, p);
1764 vn_finished_write(mp);
1765 error1 = vn_close(vp, FWRITE, cred, p);
1766 if (error == 0)
1767 error = error1;
1768 return (error);
1769}
1770
1771/*
1772 * Nonexistent system call-- signal process (may want to handle it).
1773 * Flag error in case process won't see signal immediately (blocked or ignored).
1774 */
1775#ifndef _SYS_SYSPROTO_H_
1776struct nosys_args {
1777 int dummy;
1778};
1779#endif
1780/* ARGSUSED */
1781int
1782nosys(p, args)
1783 struct proc *p;
1784 struct nosys_args *args;
1785{
1786
1787 psignal(p, SIGSYS);
1788 return (EINVAL);
1789}
1790
1791/*
1792 * Send a signal to a SIGIO or SIGURG to a process or process group using
1793 * stored credentials rather than those of the current process.
1794 */
1795void
1796pgsigio(sigio, sig, checkctty)
1797 struct sigio *sigio;
1798 int sig, checkctty;
1799{
1800 if (sigio == NULL)
1801 return;
1802
1803 if (sigio->sio_pgid > 0) {
1804 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1805 sigio->sio_proc))
1806 psignal(sigio->sio_proc, sig);
1807 } else if (sigio->sio_pgid < 0) {
1808 struct proc *p;
1809
1810 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1811 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1812 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1813 psignal(p, sig);
1814 }
1815}
1816
1817static int
1818filt_sigattach(struct knote *kn)
1819{
1820 struct proc *p = curproc;
1821
1822 kn->kn_ptr.p_proc = p;
1823 kn->kn_flags |= EV_CLEAR; /* automatically set */
1824
1825 /* XXX lock the proc here while adding to the list? */
1826 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1827
1828 return (0);
1829}
1830
1831static void
1832filt_sigdetach(struct knote *kn)
1833{
1834 struct proc *p = kn->kn_ptr.p_proc;
1835
1836 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1837}
1838
1839/*
1840 * signal knotes are shared with proc knotes, so we apply a mask to
1841 * the hint in order to differentiate them from process hints. This
1842 * could be avoided by using a signal-specific knote list, but probably
1843 * isn't worth the trouble.
1844 */
1845static int
1846filt_signal(struct knote *kn, long hint)
1847{
1848
1849 if (hint & NOTE_SIGNAL) {
1850 hint &= ~NOTE_SIGNAL;
1851
1852 if (kn->kn_id == hint)
1853 kn->kn_data++;
1854 }
1855 return (kn->kn_data != 0);
1856}
1256 }
1257#ifdef SMP
1258 else if (p->p_stat == SRUN) {
1259 mtx_unlock_spin(&sched_lock);
1260 forward_signal(p);
1261 }
1262#endif
1263 else
1264 mtx_unlock_spin(&sched_lock);
1265 goto out;
1266 }
1267 /*NOTREACHED*/
1268
1269runfast:
1270 /*
1271 * Raise priority to at least PUSER.
1272 */
1273 mtx_lock_spin(&sched_lock);
1274 if (p->p_priority > PUSER)
1275 p->p_priority = PUSER;
1276run:
1277 /* If we jump here, sched_lock has to be owned. */
1278 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1279 setrunnable(p);
1280 mtx_unlock_spin(&sched_lock);
1281out:
1282 /* If we jump here, sched_lock should not be owned. */
1283 mtx_assert(&sched_lock, MA_NOTOWNED);
1284 PROC_UNLOCK(p);
1285}
1286
1287/*
1288 * If the current process has received a signal (should be caught or cause
1289 * termination, should interrupt current syscall), return the signal number.
1290 * Stop signals with default action are processed immediately, then cleared;
1291 * they aren't returned. This is checked after each entry to the system for
1292 * a syscall or trap (though this can usually be done without calling issignal
1293 * by checking the pending signal masks in the CURSIG macro.) The normal call
1294 * sequence is
1295 *
1296 * while (sig = CURSIG(curproc))
1297 * postsig(sig);
1298 */
1299int
1300issignal(p)
1301 register struct proc *p;
1302{
1303 sigset_t mask;
1304 register int sig, prop;
1305
1306 for (;;) {
1307 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1308
1309 mask = p->p_siglist;
1310 SIGSETNAND(mask, p->p_sigmask);
1311 if (p->p_flag & P_PPWAIT)
1312 SIG_STOPSIGMASK(mask);
1313 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1314 return (0);
1315 sig = sig_ffs(&mask);
1316 prop = sigprop(sig);
1317
1318 STOPEVENT(p, S_SIG, sig);
1319
1320 /*
1321 * We should see pending but ignored signals
1322 * only if P_TRACED was on when they were posted.
1323 */
1324 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1325 SIGDELSET(p->p_siglist, sig);
1326 continue;
1327 }
1328 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1329 /*
1330 * If traced, always stop, and stay
1331 * stopped until released by the parent.
1332 */
1333 p->p_xstat = sig;
1334 PROCTREE_LOCK(PT_SHARED);
1335 psignal(p->p_pptr, SIGCHLD);
1336 do {
1337 stop(p);
1338 PROCTREE_LOCK(PT_RELEASE);
1339 mtx_lock_spin(&sched_lock);
1340 DROP_GIANT_NOSWITCH();
1341 mi_switch();
1342 mtx_unlock_spin(&sched_lock);
1343 PICKUP_GIANT();
1344 PROCTREE_LOCK(PT_SHARED);
1345 } while (!trace_req(p)
1346 && p->p_flag & P_TRACED);
1347 PROCTREE_LOCK(PT_RELEASE);
1348
1349 /*
1350 * If the traced bit got turned off, go back up
1351 * to the top to rescan signals. This ensures
1352 * that p_sig* and ps_sigact are consistent.
1353 */
1354 if ((p->p_flag & P_TRACED) == 0)
1355 continue;
1356
1357 /*
1358 * If parent wants us to take the signal,
1359 * then it will leave it in p->p_xstat;
1360 * otherwise we just look for signals again.
1361 */
1362 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1363 sig = p->p_xstat;
1364 if (sig == 0)
1365 continue;
1366
1367 /*
1368 * Put the new signal into p_siglist. If the
1369 * signal is being masked, look for other signals.
1370 */
1371 SIGADDSET(p->p_siglist, sig);
1372 if (SIGISMEMBER(p->p_sigmask, sig))
1373 continue;
1374 }
1375
1376 /*
1377 * Decide whether the signal should be returned.
1378 * Return the signal's number, or fall through
1379 * to clear it from the pending mask.
1380 */
1381 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1382
1383 case (int)SIG_DFL:
1384 /*
1385 * Don't take default actions on system processes.
1386 */
1387 if (p->p_pid <= 1) {
1388#ifdef DIAGNOSTIC
1389 /*
1390 * Are you sure you want to ignore SIGSEGV
1391 * in init? XXX
1392 */
1393 printf("Process (pid %lu) got signal %d\n",
1394 (u_long)p->p_pid, sig);
1395#endif
1396 break; /* == ignore */
1397 }
1398 /*
1399 * If there is a pending stop signal to process
1400 * with default action, stop here,
1401 * then clear the signal. However,
1402 * if process is member of an orphaned
1403 * process group, ignore tty stop signals.
1404 */
1405 if (prop & SA_STOP) {
1406 if (p->p_flag & P_TRACED ||
1407 (p->p_pgrp->pg_jobc == 0 &&
1408 prop & SA_TTYSTOP))
1409 break; /* == ignore */
1410 p->p_xstat = sig;
1411 PROCTREE_LOCK(PT_SHARED);
1412 stop(p);
1413 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1414 psignal(p->p_pptr, SIGCHLD);
1415 PROCTREE_LOCK(PT_RELEASE);
1416 mtx_lock_spin(&sched_lock);
1417 DROP_GIANT_NOSWITCH();
1418 mi_switch();
1419 mtx_unlock_spin(&sched_lock);
1420 PICKUP_GIANT();
1421 break;
1422 } else if (prop & SA_IGNORE) {
1423 /*
1424 * Except for SIGCONT, shouldn't get here.
1425 * Default action is to ignore; drop it.
1426 */
1427 break; /* == ignore */
1428 } else
1429 return (sig);
1430 /*NOTREACHED*/
1431
1432 case (int)SIG_IGN:
1433 /*
1434 * Masking above should prevent us ever trying
1435 * to take action on an ignored signal other
1436 * than SIGCONT, unless process is traced.
1437 */
1438 if ((prop & SA_CONT) == 0 &&
1439 (p->p_flag & P_TRACED) == 0)
1440 printf("issignal\n");
1441 break; /* == ignore */
1442
1443 default:
1444 /*
1445 * This signal has an action, let
1446 * postsig() process it.
1447 */
1448 return (sig);
1449 }
1450 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1451 }
1452 /* NOTREACHED */
1453}
1454
1455/*
1456 * Put the argument process into the stopped state and notify the parent
1457 * via wakeup. Signals are handled elsewhere. The process must not be
1458 * on the run queue. Must be called with at least a shared hold of the
1459 * proctree lock.
1460 */
1461void
1462stop(p)
1463 register struct proc *p;
1464{
1465
1466 PROCTREE_ASSERT(PT_SHARED);
1467 mtx_lock_spin(&sched_lock);
1468 p->p_stat = SSTOP;
1469 p->p_flag &= ~P_WAITED;
1470 wakeup((caddr_t)p->p_pptr);
1471 mtx_unlock_spin(&sched_lock);
1472}
1473
1474/*
1475 * Take the action for the specified signal
1476 * from the current set of pending signals.
1477 */
1478void
1479postsig(sig)
1480 register int sig;
1481{
1482 register struct proc *p = curproc;
1483 struct sigacts *ps = p->p_sigacts;
1484 sig_t action;
1485 sigset_t returnmask;
1486 int code;
1487
1488 KASSERT(sig != 0, ("postsig"));
1489
1490 SIGDELSET(p->p_siglist, sig);
1491 action = ps->ps_sigact[_SIG_IDX(sig)];
1492#ifdef KTRACE
1493 if (KTRPOINT(p, KTR_PSIG))
1494 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1495 &p->p_oldsigmask : &p->p_sigmask, 0);
1496#endif
1497 STOPEVENT(p, S_SIG, sig);
1498
1499 if (action == SIG_DFL) {
1500 /*
1501 * Default action, where the default is to kill
1502 * the process. (Other cases were ignored above.)
1503 */
1504 sigexit(p, sig);
1505 /* NOTREACHED */
1506 } else {
1507 /*
1508 * If we get here, the signal must be caught.
1509 */
1510 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1511 ("postsig action"));
1512 /*
1513 * Set the new mask value and also defer further
1514 * occurrences of this signal.
1515 *
1516 * Special case: user has done a sigsuspend. Here the
1517 * current mask is not of interest, but rather the
1518 * mask from before the sigsuspend is what we want
1519 * restored after the signal processing is completed.
1520 */
1521 (void) splhigh();
1522 if (p->p_flag & P_OLDMASK) {
1523 returnmask = p->p_oldsigmask;
1524 p->p_flag &= ~P_OLDMASK;
1525 } else
1526 returnmask = p->p_sigmask;
1527
1528 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1529 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1530 SIGADDSET(p->p_sigmask, sig);
1531
1532 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1533 /*
1534 * See do_sigaction() for origin of this code.
1535 */
1536 SIGDELSET(p->p_sigcatch, sig);
1537 if (sig != SIGCONT &&
1538 sigprop(sig) & SA_IGNORE)
1539 SIGADDSET(p->p_sigignore, sig);
1540 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1541 }
1542 (void) spl0();
1543 p->p_stats->p_ru.ru_nsignals++;
1544 if (p->p_sig != sig) {
1545 code = 0;
1546 } else {
1547 code = p->p_code;
1548 p->p_code = 0;
1549 p->p_sig = 0;
1550 }
1551 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1552 }
1553}
1554
1555/*
1556 * Kill the current process for stated reason.
1557 */
1558void
1559killproc(p, why)
1560 struct proc *p;
1561 char *why;
1562{
1563 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
1564 p, p->p_pid, p->p_comm);
1565 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1566 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1567 psignal(p, SIGKILL);
1568}
1569
1570/*
1571 * Force the current process to exit with the specified signal, dumping core
1572 * if appropriate. We bypass the normal tests for masked and caught signals,
1573 * allowing unrecoverable failures to terminate the process without changing
1574 * signal state. Mark the accounting record with the signal termination.
1575 * If dumping core, save the signal number for the debugger. Calls exit and
1576 * does not return.
1577 */
1578void
1579sigexit(p, sig)
1580 register struct proc *p;
1581 int sig;
1582{
1583
1584 p->p_acflag |= AXSIG;
1585 if (sigprop(sig) & SA_CORE) {
1586 p->p_sig = sig;
1587 /*
1588 * Log signals which would cause core dumps
1589 * (Log as LOG_INFO to appease those who don't want
1590 * these messages.)
1591 * XXX : Todo, as well as euid, write out ruid too
1592 */
1593 if (coredump(p) == 0)
1594 sig |= WCOREFLAG;
1595 if (kern_logsigexit)
1596 log(LOG_INFO,
1597 "pid %d (%s), uid %d: exited on signal %d%s\n",
1598 p->p_pid, p->p_comm,
1599 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1600 sig &~ WCOREFLAG,
1601 sig & WCOREFLAG ? " (core dumped)" : "");
1602 }
1603 exit1(p, W_EXITCODE(0, sig));
1604 /* NOTREACHED */
1605}
1606
1607static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1608SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1609 sizeof(corefilename), "process corefile name format string");
1610
1611/*
1612 * expand_name(name, uid, pid)
1613 * Expand the name described in corefilename, using name, uid, and pid.
1614 * corefilename is a printf-like string, with three format specifiers:
1615 * %N name of process ("name")
1616 * %P process id (pid)
1617 * %U user id (uid)
1618 * For example, "%N.core" is the default; they can be disabled completely
1619 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1620 * This is controlled by the sysctl variable kern.corefile (see above).
1621 */
1622
1623static char *
1624expand_name(name, uid, pid)
1625const char *name; uid_t uid; pid_t pid; {
1626 char *temp;
1627 char buf[11]; /* Buffer for pid/uid -- max 4B */
1628 int i, n;
1629 char *format = corefilename;
1630 size_t namelen;
1631
1632 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1633 if (temp == NULL)
1634 return NULL;
1635 namelen = strlen(name);
1636 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1637 int l;
1638 switch (format[i]) {
1639 case '%': /* Format character */
1640 i++;
1641 switch (format[i]) {
1642 case '%':
1643 temp[n++] = '%';
1644 break;
1645 case 'N': /* process name */
1646 if ((n + namelen) > MAXPATHLEN) {
1647 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1648 pid, name, uid, temp, name);
1649 free(temp, M_TEMP);
1650 return NULL;
1651 }
1652 memcpy(temp+n, name, namelen);
1653 n += namelen;
1654 break;
1655 case 'P': /* process id */
1656 l = sprintf(buf, "%u", pid);
1657 if ((n + l) > MAXPATHLEN) {
1658 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1659 pid, name, uid, temp, name);
1660 free(temp, M_TEMP);
1661 return NULL;
1662 }
1663 memcpy(temp+n, buf, l);
1664 n += l;
1665 break;
1666 case 'U': /* user id */
1667 l = sprintf(buf, "%u", uid);
1668 if ((n + l) > MAXPATHLEN) {
1669 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1670 pid, name, uid, temp, name);
1671 free(temp, M_TEMP);
1672 return NULL;
1673 }
1674 memcpy(temp+n, buf, l);
1675 n += l;
1676 break;
1677 default:
1678 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1679 }
1680 break;
1681 default:
1682 temp[n++] = format[i];
1683 }
1684 }
1685 temp[n] = '\0';
1686 return temp;
1687}
1688
1689/*
1690 * Dump a process' core. The main routine does some
1691 * policy checking, and creates the name of the coredump;
1692 * then it passes on a vnode and a size limit to the process-specific
1693 * coredump routine if there is one; if there _is not_ one, it returns
1694 * ENOSYS; otherwise it returns the error from the process-specific routine.
1695 */
1696
1697static int
1698coredump(p)
1699 register struct proc *p;
1700{
1701 register struct vnode *vp;
1702 register struct ucred *cred = p->p_ucred;
1703 struct nameidata nd;
1704 struct vattr vattr;
1705 int error, error1, flags;
1706 struct mount *mp;
1707 char *name; /* name of corefile */
1708 off_t limit;
1709
1710 STOPEVENT(p, S_CORE, 0);
1711
1712 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1713 return (EFAULT);
1714
1715 /*
1716 * Note that the bulk of limit checking is done after
1717 * the corefile is created. The exception is if the limit
1718 * for corefiles is 0, in which case we don't bother
1719 * creating the corefile at all. This layout means that
1720 * a corefile is truncated instead of not being created,
1721 * if it is larger than the limit.
1722 */
1723 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1724 if (limit == 0)
1725 return 0;
1726
1727restart:
1728 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1729 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1730 flags = O_CREAT | FWRITE | O_NOFOLLOW;
1731 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1732 free(name, M_TEMP);
1733 if (error)
1734 return (error);
1735 NDFREE(&nd, NDF_ONLY_PNBUF);
1736 vp = nd.ni_vp;
1737 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1738 VOP_UNLOCK(vp, 0, p);
1739 if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1740 return (error);
1741 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1742 return (error);
1743 goto restart;
1744 }
1745
1746 /* Don't dump to non-regular files or files with links. */
1747 if (vp->v_type != VREG ||
1748 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1749 error = EFAULT;
1750 goto out;
1751 }
1752 VATTR_NULL(&vattr);
1753 vattr.va_size = 0;
1754 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1755 VOP_SETATTR(vp, &vattr, cred, p);
1756 p->p_acflag |= ACORE;
1757
1758 error = p->p_sysent->sv_coredump ?
1759 p->p_sysent->sv_coredump(p, vp, limit) :
1760 ENOSYS;
1761
1762out:
1763 VOP_UNLOCK(vp, 0, p);
1764 vn_finished_write(mp);
1765 error1 = vn_close(vp, FWRITE, cred, p);
1766 if (error == 0)
1767 error = error1;
1768 return (error);
1769}
1770
1771/*
1772 * Nonexistent system call-- signal process (may want to handle it).
1773 * Flag error in case process won't see signal immediately (blocked or ignored).
1774 */
1775#ifndef _SYS_SYSPROTO_H_
1776struct nosys_args {
1777 int dummy;
1778};
1779#endif
1780/* ARGSUSED */
1781int
1782nosys(p, args)
1783 struct proc *p;
1784 struct nosys_args *args;
1785{
1786
1787 psignal(p, SIGSYS);
1788 return (EINVAL);
1789}
1790
1791/*
1792 * Send a signal to a SIGIO or SIGURG to a process or process group using
1793 * stored credentials rather than those of the current process.
1794 */
1795void
1796pgsigio(sigio, sig, checkctty)
1797 struct sigio *sigio;
1798 int sig, checkctty;
1799{
1800 if (sigio == NULL)
1801 return;
1802
1803 if (sigio->sio_pgid > 0) {
1804 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1805 sigio->sio_proc))
1806 psignal(sigio->sio_proc, sig);
1807 } else if (sigio->sio_pgid < 0) {
1808 struct proc *p;
1809
1810 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1811 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1812 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1813 psignal(p, sig);
1814 }
1815}
1816
1817static int
1818filt_sigattach(struct knote *kn)
1819{
1820 struct proc *p = curproc;
1821
1822 kn->kn_ptr.p_proc = p;
1823 kn->kn_flags |= EV_CLEAR; /* automatically set */
1824
1825 /* XXX lock the proc here while adding to the list? */
1826 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1827
1828 return (0);
1829}
1830
1831static void
1832filt_sigdetach(struct knote *kn)
1833{
1834 struct proc *p = kn->kn_ptr.p_proc;
1835
1836 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1837}
1838
1839/*
1840 * signal knotes are shared with proc knotes, so we apply a mask to
1841 * the hint in order to differentiate them from process hints. This
1842 * could be avoided by using a signal-specific knote list, but probably
1843 * isn't worth the trouble.
1844 */
1845static int
1846filt_signal(struct knote *kn, long hint)
1847{
1848
1849 if (hint & NOTE_SIGNAL) {
1850 hint &= ~NOTE_SIGNAL;
1851
1852 if (kn->kn_id == hint)
1853 kn->kn_data++;
1854 }
1855 return (kn->kn_data != 0);
1856}