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