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