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