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