cred may be NULL due to a prior error code. crhold() handles NULL creds,
[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 $
bf189c26 40 * $DragonFly: src/sys/kern/kern_sig.c,v 1.41 2005/12/01 18:54:20 dillon 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>
e43a034f 68#include <sys/thread2.h>
984263bc
MD
69
70
71#include <machine/ipl.h>
72#include <machine/cpu.h>
73#include <machine/smp.h>
74
8ebcad3f
MD
75static int coredump(struct proc *);
76static char *expand_name(const char *, uid_t, pid_t);
77static int killpg(int sig, int pgid, int all);
78static int sig_ffs(sigset_t *set);
79static int sigprop(int sig);
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 */
e43a034f 263 crit_enter();
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
e43a034f 334 crit_exit();
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
e43a034f 416 * p == curproc.
984263bc 417 */
65957d54
DRJ
418int
419kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
984263bc 420{
65957d54
DRJ
421 struct thread *td = curthread;
422 struct proc *p = td->td_proc;
984263bc
MD
423 int error;
424
425 if (oset != NULL)
426 *oset = p->p_sigmask;
427
428 error = 0;
429 if (set != NULL) {
430 switch (how) {
431 case SIG_BLOCK:
432 SIG_CANTMASK(*set);
433 SIGSETOR(p->p_sigmask, *set);
434 break;
435 case SIG_UNBLOCK:
436 SIGSETNAND(p->p_sigmask, *set);
437 break;
438 case SIG_SETMASK:
439 SIG_CANTMASK(*set);
65957d54 440 p->p_sigmask = *set;
984263bc
MD
441 break;
442 default:
443 error = EINVAL;
444 break;
445 }
446 }
447 return (error);
448}
449
450/*
451 * sigprocmask() - MP SAFE
452 */
984263bc 453int
41c20dac 454sigprocmask(struct sigprocmask_args *uap)
984263bc
MD
455{
456 sigset_t set, oset;
457 sigset_t *setp, *osetp;
458 int error;
459
460 setp = (uap->set != NULL) ? &set : NULL;
461 osetp = (uap->oset != NULL) ? &oset : NULL;
462 if (setp) {
463 error = copyin(uap->set, setp, sizeof(set));
464 if (error)
465 return (error);
466 }
65957d54 467 error = kern_sigprocmask(uap->how, setp, osetp);
984263bc
MD
468 if (osetp && !error) {
469 error = copyout(osetp, uap->oset, sizeof(oset));
470 }
471 return (error);
472}
473
984263bc 474int
65957d54 475kern_sigpending(struct __sigset *set)
984263bc 476{
65957d54
DRJ
477 struct thread *td = curthread;
478 struct proc *p = td->td_proc;
984263bc 479
65957d54 480 *set = p->p_siglist;
984263bc 481
984263bc
MD
482 return (0);
483}
484
984263bc 485int
65957d54 486sigpending(struct sigpending_args *uap)
984263bc
MD
487{
488 sigset_t set;
65957d54 489 int error;
984263bc 490
65957d54 491 error = kern_sigpending(&set);
984263bc 492
65957d54
DRJ
493 if (error == 0)
494 error = copyout(&set, uap->set, sizeof(set));
495 return (error);
984263bc 496}
984263bc
MD
497
498/*
499 * Suspend process until signal, providing mask to be set
65957d54 500 * in the meantime.
984263bc 501 */
984263bc 502int
65957d54 503kern_sigsuspend(struct __sigset *set)
984263bc 504{
65957d54
DRJ
505 struct thread *td = curthread;
506 struct proc *p = td->td_proc;
41c20dac 507 struct sigacts *ps = p->p_sigacts;
984263bc
MD
508
509 /*
510 * When returning from sigsuspend, we want
511 * the old mask to be restored after the
512 * signal handler has finished. Thus, we
513 * save it here and mark the sigacts structure
514 * to indicate this.
515 */
516 p->p_oldsigmask = p->p_sigmask;
517 p->p_flag |= P_OLDMASK;
518
65957d54
DRJ
519 SIG_CANTMASK(*set);
520 p->p_sigmask = *set;
521 while (tsleep(ps, PCATCH, "pause", 0) == 0)
984263bc
MD
522 /* void */;
523 /* always return EINTR rather than ERESTART... */
524 return (EINTR);
525}
526
65957d54
DRJ
527/*
528 * Note nonstandard calling convention: libc stub passes mask, not
529 * pointer, to save a copyin.
530 */
984263bc 531int
65957d54 532sigsuspend(struct sigsuspend_args *uap)
984263bc
MD
533{
534 sigset_t mask;
65957d54 535 int error;
984263bc 536
65957d54
DRJ
537 error = copyin(uap->sigmask, &mask, sizeof(mask));
538 if (error)
539 return (error);
540
541 error = kern_sigsuspend(&mask);
542
543 return (error);
984263bc
MD
544}
545
984263bc 546int
65957d54 547kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss)
984263bc 548{
65957d54
DRJ
549 struct thread *td = curthread;
550 struct proc *p = td->td_proc;
984263bc 551
65957d54
DRJ
552 if ((p->p_flag & P_ALTSTACK) == 0)
553 p->p_sigstk.ss_flags |= SS_DISABLE;
554
555 if (oss)
556 *oss = p->p_sigstk;
557
558 if (ss) {
559 if (ss->ss_flags & SS_DISABLE) {
560 if (p->p_sigstk.ss_flags & SS_ONSTACK)
561 return (EINVAL);
562 p->p_flag &= ~P_ALTSTACK;
563 p->p_sigstk.ss_flags = ss->ss_flags;
564 } else {
565 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
566 return (ENOMEM);
567 p->p_flag |= P_ALTSTACK;
568 p->p_sigstk = *ss;
569 }
984263bc 570 }
65957d54
DRJ
571
572 return (0);
984263bc 573}
984263bc 574
984263bc 575int
41c20dac 576sigaltstack(struct sigaltstack_args *uap)
984263bc 577{
65957d54 578 stack_t ss, oss;
984263bc
MD
579 int error;
580
65957d54
DRJ
581 if (uap->ss) {
582 error = copyin(uap->ss, &ss, sizeof(ss));
583 if (error)
584 return (error);
984263bc 585 }
65957d54
DRJ
586
587 error = kern_sigaltstack(uap->ss ? &ss : NULL,
588 uap->oss ? &oss : NULL);
589
590 if (error == 0 && uap->oss)
591 error = copyout(&oss, uap->oss, sizeof(*uap->oss));
592 return (error);
984263bc
MD
593}
594
595/*
596 * Common code for kill process group/broadcast kill.
597 * cp is calling process.
598 */
65957d54
DRJ
599static int
600killpg(int sig, int pgid, int all)
984263bc 601{
41c20dac
MD
602 struct proc *cp = curproc;
603 struct proc *p;
984263bc
MD
604 struct pgrp *pgrp;
605 int nfound = 0;
606
8ebcad3f 607 if (all) {
984263bc
MD
608 /*
609 * broadcast
610 */
f62004ad 611 FOREACH_PROC_IN_SYSTEM(p) {
984263bc 612 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
41c20dac 613 p == cp || !CANSIGNAL(p, sig))
984263bc
MD
614 continue;
615 nfound++;
616 if (sig)
617 psignal(p, sig);
618 }
8ebcad3f
MD
619 } else {
620 if (pgid == 0) {
984263bc
MD
621 /*
622 * zero pgid means send to my process group.
623 */
624 pgrp = cp->p_pgrp;
8ebcad3f 625 } else {
984263bc
MD
626 pgrp = pgfind(pgid);
627 if (pgrp == NULL)
628 return (ESRCH);
629 }
630 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
f026f3b0
MD
631 if (p->p_pid <= 1 ||
632 (p->p_flag & (P_SYSTEM | P_ZOMBIE)) ||
633 !CANSIGNAL(p, sig)) {
984263bc 634 continue;
f026f3b0 635 }
984263bc
MD
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 */
763void
8ebcad3f 764psignal(struct proc *p, int sig)
984263bc 765{
553ea3c8 766 struct lwp *lp = &p->p_lwp;
e43a034f 767 int prop;
cb973d15 768 sig_t action;
984263bc
MD
769
770 if (sig > _SIG_MAXSIG || sig <= 0) {
771 printf("psignal: signal %d\n", sig);
772 panic("psignal signal number");
773 }
774
e43a034f 775 crit_enter();
984263bc 776 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
e43a034f 777 crit_exit();
984263bc
MD
778
779 prop = sigprop(sig);
780
781 /*
782 * If proc is traced, always give parent a chance;
783 * if signal event is tracked by procfs, give *that*
784 * a chance, as well.
785 */
8ebcad3f 786 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
984263bc 787 action = SIG_DFL;
8ebcad3f 788 } else {
984263bc
MD
789 /*
790 * If the signal is being ignored,
791 * then we forget about it immediately.
792 * (Note: we don't set SIGCONT in p_sigignore,
793 * and if it is set to SIG_IGN,
794 * action will be SIG_DFL here.)
795 */
796 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
797 return;
798 if (SIGISMEMBER(p->p_sigmask, sig))
799 action = SIG_HOLD;
800 else if (SIGISMEMBER(p->p_sigcatch, sig))
801 action = SIG_CATCH;
802 else
803 action = SIG_DFL;
804 }
805
806 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
8ebcad3f 807 (p->p_flag & P_TRACED) == 0) {
984263bc 808 p->p_nice = NZERO;
8ebcad3f 809 }
984263bc 810
344ad853
MD
811 /*
812 * If continuing, clear any pending STOP signals.
813 */
984263bc
MD
814 if (prop & SA_CONT)
815 SIG_STOPSIGMASK(p->p_siglist);
9d63d5c0 816
984263bc
MD
817 if (prop & SA_STOP) {
818 /*
819 * If sending a tty stop signal to a member of an orphaned
820 * process group, discard the signal here if the action
821 * is default; don't stop the process below if sleeping,
822 * and don't clear any pending SIGCONT.
823 */
824 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
8ebcad3f 825 action == SIG_DFL) {
984263bc 826 return;
8ebcad3f 827 }
984263bc
MD
828 SIG_CONTSIGMASK(p->p_siglist);
829 }
830 SIGADDSET(p->p_siglist, sig);
831
832 /*
833 * Defer further processing for signals which are held,
834 * except that stopped processes must be continued by SIGCONT.
835 */
344ad853
MD
836 if (action == SIG_HOLD) {
837 if ((prop & SA_CONT) == 0 || (p->p_flag & P_STOPPED) == 0)
838 return;
839 }
e43a034f
MD
840
841 crit_enter();
842
344ad853
MD
843 /*
844 * Process is in tsleep and not stopped
845 */
846 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED) == 0) {
984263bc 847 /*
344ad853 848 * If the process is sleeping uninterruptibly
984263bc
MD
849 * we can't interrupt the sleep... the signal will
850 * be noticed when the process returns through
851 * trap() or syscall().
852 */
853 if ((p->p_flag & P_SINTR) == 0)
854 goto out;
344ad853 855
984263bc 856 /*
344ad853 857 * If the process is sleeping and traced, make it runnable
984263bc
MD
858 * so it can discover the signal in issignal() and stop
859 * for the parent.
344ad853
MD
860 *
861 * If the process is stopped and traced, no further action
862 * is necessary.
984263bc
MD
863 */
864 if (p->p_flag & P_TRACED)
865 goto run;
344ad853 866
984263bc 867 /*
344ad853
MD
868 * If the process is sleeping and SA_CONT, and the signal
869 * mode is SIG_DFL, then make the process runnable.
870 *
871 * However, do *NOT* set P_BREAKTSLEEP. We do not want
872 * a SIGCONT to terminate an interruptable tsleep early
873 * and generate a spurious EINTR.
984263bc
MD
874 */
875 if ((prop & SA_CONT) && action == SIG_DFL) {
876 SIGDELSET(p->p_siglist, sig);
344ad853 877 goto run_no_break;
984263bc 878 }
344ad853 879
984263bc 880 /*
344ad853
MD
881 * If the process is sleeping and receives a STOP signal,
882 * process immediately if possible. All other (caught or
883 * default) signals cause the process to run.
984263bc
MD
884 */
885 if (prop & SA_STOP) {
886 if (action != SIG_DFL)
26a0694b 887 goto run;
344ad853 888
984263bc 889 /*
344ad853
MD
890 * If a child holding parent blocked, stopping
891 * could cause deadlock. Take no action at this
892 * time.
984263bc
MD
893 */
894 if (p->p_flag & P_PPWAIT)
895 goto out;
344ad853
MD
896
897 /*
898 * Do not actually try to manipulate the process
899 * while it is sleeping, simply set P_STOPPED to
900 * indicate that it should stop as soon as it safely
901 * can.
902 */
984263bc 903 SIGDELSET(p->p_siglist, sig);
344ad853
MD
904 p->p_flag |= P_STOPPED;
905 p->p_flag &= ~P_WAITED;
984263bc
MD
906 p->p_xstat = sig;
907 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
908 psignal(p->p_pptr, SIGCHLD);
984263bc 909 goto out;
8ebcad3f 910 }
344ad853 911
984263bc 912 /*
344ad853
MD
913 * Otherwise the signal can interrupt the sleep.
914 */
915 goto run;
916 }
917
918 /*
919 * Process is in tsleep and is stopped
920 */
921 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED)) {
922 /*
923 * If the process is stopped and is being traced, then no
924 * further action is necessary.
984263bc
MD
925 */
926 if (p->p_flag & P_TRACED)
927 goto out;
928
929 /*
344ad853
MD
930 * If the process is stopped and receives a KILL signal,
931 * make the process runnable.
984263bc
MD
932 */
933 if (sig == SIGKILL)
26a0694b 934 goto run;
984263bc 935
344ad853
MD
936 /*
937 * If the process is stopped and receives a CONT signal,
938 * then try to make the process runnable again.
939 */
984263bc
MD
940 if (prop & SA_CONT) {
941 /*
942 * If SIGCONT is default (or ignored), we continue the
943 * process but don't leave the signal in p_siglist, as
944 * it has no further action. If SIGCONT is held, we
945 * continue the process and leave the signal in
946 * p_siglist. If the process catches SIGCONT, let it
344ad853 947 * handle the signal itself.
984263bc
MD
948 */
949 if (action == SIG_DFL)
950 SIGDELSET(p->p_siglist, sig);
951 if (action == SIG_CATCH)
26a0694b 952 goto run;
984263bc 953
984263bc 954 /*
344ad853
MD
955 * Make runnable but do not break a tsleep unless
956 * some other signal was pending.
984263bc 957 */
344ad853
MD
958 goto run_no_break;
959 }
960
961 /*
962 * If the process is stopped and receives another STOP
963 * signal, we do not need to stop it again. If we did
964 * the shell could get confused.
965 */
966 if (prop & SA_STOP) {
984263bc
MD
967 SIGDELSET(p->p_siglist, sig);
968 goto out;
969 }
970
971 /*
344ad853
MD
972 * Otherwise the process is sleeping interruptably but
973 * is stopped, just set the P_BREAKTSLEEP flag and take
974 * no further action. The next runnable action will wake
975 * the process up.
984263bc 976 */
344ad853 977 p->p_flag |= P_BREAKTSLEEP;
984263bc 978 goto out;
344ad853
MD
979 }
980
981 /*
982 * Otherwise the process is running
983 *
984 * SRUN, SIDL, SZOMB do nothing with the signal,
985 * other than kicking ourselves if we are running.
986 * It will either never be noticed, or noticed very soon.
987 *
988 * Note that p_thread may be NULL or may not be completely
989 * initialized if the process is in the SIDL or SZOMB state.
990 *
991 * For SMP we may have to forward the request to another cpu.
992 * YYY the MP lock prevents the target process from moving
993 * to another cpu, see kern/kern_switch.c
994 *
995 * If the target thread is waiting on its message port,
996 * wakeup the target thread so it can check (or ignore)
997 * the new signal. YYY needs cleanup.
998 */
999 if (lp == lwkt_preempted_proc()) {
1000 signotify();
1001 } else if (p->p_stat == SRUN) {
1002 struct thread *td = p->p_thread;
1003
1004 KASSERT(td != NULL,
1005 ("pid %d NULL p_thread stat %d flags %08x",
1006 p->p_pid, p->p_stat, p->p_flag));
1007
984263bc 1008#ifdef SMP
344ad853
MD
1009 if (td->td_gd != mycpu)
1010 lwkt_send_ipiq(td->td_gd, signotify_remote, lp);
1011 else
984263bc 1012#endif
344ad853
MD
1013 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
1014 lwkt_schedule(td);
984263bc 1015 }
344ad853 1016 goto out;
984263bc 1017 /*NOTREACHED*/
984263bc 1018run:
344ad853
MD
1019 /*
1020 * Make runnable and break out of any tsleep as well.
1021 */
1022 p->p_flag |= P_BREAKTSLEEP;
1023run_no_break:
984263bc
MD
1024 setrunnable(p);
1025out:
e43a034f 1026 crit_exit();
984263bc
MD
1027}
1028
cb973d15
MD
1029#ifdef SMP
1030
1031/*
1032 * This function is called via an IPI. We will be in a critical section but
1033 * the MP lock will NOT be held. Also note that by the time the ipi message
1034 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1035 */
1036static void
1037signotify_remote(void *arg)
1038{
553ea3c8 1039 struct lwp *lp = arg;
60be1d11 1040
553ea3c8 1041 if (lp == lwkt_preempted_proc()) {
235957ed 1042 signotify();
60be1d11 1043 } else {
553ea3c8 1044 struct thread *td = lp->lwp_thread;
60be1d11
MD
1045 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
1046 lwkt_schedule(td);
1047 }
cb973d15
MD
1048}
1049
1050#endif
1051
3a59f0f7
DX
1052static int
1053kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1054{
1055 sigset_t savedmask, set;
1056 struct proc *p = curproc;
1057 int error, sig, hz, timevalid = 0;
1058 struct timespec rts, ets, ts;
1059 struct timeval tv;
1060
1061 error = 0;
1062 sig = 0;
1063 SIG_CANTMASK(waitset);
1064 savedmask = p->p_sigmask;
1065
1066 if (timeout) {
f8c41806
DX
1067 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1068 timeout->tv_nsec < 1000000000) {
3a59f0f7
DX
1069 timevalid = 1;
1070 getnanouptime(&rts);
1071 ets = rts;
1072 timespecadd(&ets, timeout);
1073 }
1074 }
1075
1076 for (;;) {
1077 set = p->p_siglist;
1078 SIGSETAND(set, waitset);
1079 if ((sig = sig_ffs(&set)) != 0) {
1080 SIGFILLSET(p->p_sigmask);
1081 SIGDELSET(p->p_sigmask, sig);
976f0bc5 1082 SIG_CANTMASK(p->p_sigmask);
3a59f0f7
DX
1083 sig = issignal(p);
1084 /*
1085 * It may be a STOP signal, in the case, issignal
1086 * returns 0, because we may stop there, and new
1087 * signal can come in, we should restart if we got
1088 * nothing.
1089 */
1090 if (sig == 0)
1091 continue;
1092 else
1093 break;
1094 }
1095
1096 /*
1097 * Previous checking got nothing, and we retried but still
1098 * got nothing, we should return the error status.
1099 */
1100 if (error)
1101 break;
1102
1103 /*
1104 * POSIX says this must be checked after looking for pending
1105 * signals.
1106 */
1107 if (timeout) {
1108 if (!timevalid) {
1109 error = EINVAL;
1110 break;
1111 }
1112 getnanouptime(&rts);
1113 if (timespeccmp(&rts, &ets, >=)) {
1114 error = EAGAIN;
1115 break;
1116 }
1117 ts = ets;
1118 timespecsub(&ts, &rts);
1119 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1120 hz = tvtohz_high(&tv);
1121 } else
1122 hz = 0;
1123
1124 p->p_sigmask = savedmask;
1125 SIGSETNAND(p->p_sigmask, waitset);
1126 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1127 if (timeout) {
1128 if (error == ERESTART) {
1129 /* can not restart a timeout wait. */
1130 error = EINTR;
1131 } else if (error == EAGAIN) {
1132 /* will calculate timeout by ourself. */
1133 error = 0;
1134 }
1135 }
1136 /* Retry ... */
1137 }
1138
1139 p->p_sigmask = savedmask;
1140 if (sig) {
1141 error = 0;
1142 bzero(info, sizeof(*info));
1143 info->si_signo = sig;
1144 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1145 }
1146 return (error);
1147}
1148
1149int
1150sigtimedwait(struct sigtimedwait_args *uap)
1151{
1152 struct timespec ts;
1153 struct timespec *timeout;
1154 sigset_t set;
1155 siginfo_t info;
1156 int error;
1157
1158 if (uap->timeout) {
1159 error = copyin(uap->timeout, &ts, sizeof(ts));
1160 if (error)
1161 return (error);
1162 timeout = &ts;
1163 } else {
1164 timeout = NULL;
1165 }
1166 error = copyin(uap->set, &set, sizeof(set));
1167 if (error)
1168 return (error);
1169 error = kern_sigtimedwait(set, &info, timeout);
1170 if (error)
1171 return (error);
1172 if (uap->info)
1173 error = copyout(&info, uap->info, sizeof(info));
1174 /* Repost if we got an error. */
1175 if (error)
1176 psignal(curproc, info.si_signo);
1177 else
1178 uap->sysmsg_result = info.si_signo;
1179 return (error);
1180}
1181
1182int
1183sigwaitinfo(struct sigwaitinfo_args *uap)
1184{
1185 siginfo_t info;
1186 sigset_t set;
1187 int error;
1188
1189 error = copyin(uap->set, &set, sizeof(set));
1190 if (error)
1191 return (error);
1192 error = kern_sigtimedwait(set, &info, NULL);
1193 if (error)
1194 return (error);
1195 if (uap->info)
1196 error = copyout(&info, uap->info, sizeof(info));
1197 /* Repost if we got an error. */
1198 if (error)
1199 psignal(curproc, info.si_signo);
1200 else
1201 uap->sysmsg_result = info.si_signo;
1202 return (error);
1203}
1204
a94976ad
MD
1205/*
1206 * If the current process has received a signal that would interrupt a
1207 * system call, return EINTR or ERESTART as appropriate.
1208 */
1209int
1210iscaught(struct proc *p)
1211{
1212 int sig;
1213
1214 if (p) {
1215 if ((sig = CURSIG(p)) != 0) {
1216 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1217 return (EINTR);
1218 return (ERESTART);
1219 }
1220 }
1221 return(EWOULDBLOCK);
1222}
1223
984263bc
MD
1224/*
1225 * If the current process has received a signal (should be caught or cause
1226 * termination, should interrupt current syscall), return the signal number.
1227 * Stop signals with default action are processed immediately, then cleared;
1228 * they aren't returned. This is checked after each entry to the system for
1229 * a syscall or trap (though this can usually be done without calling issignal
1230 * by checking the pending signal masks in the CURSIG macro.) The normal call
1231 * sequence is
1232 *
bf189c26
MD
1233 * This routine is called via CURSIG/__cursig and the MP lock might not be
1234 * held. Obtain the MP lock for the duration of the operation.
1235 *
984263bc
MD
1236 * while (sig = CURSIG(curproc))
1237 * postsig(sig);
1238 */
1239int
8ebcad3f 1240issignal(struct proc *p)
984263bc
MD
1241{
1242 sigset_t mask;
1fd87d54 1243 int sig, prop;
984263bc 1244
bf189c26 1245 get_mplock();
984263bc
MD
1246 for (;;) {
1247 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1248
1249 mask = p->p_siglist;
1250 SIGSETNAND(mask, p->p_sigmask);
1251 if (p->p_flag & P_PPWAIT)
1252 SIG_STOPSIGMASK(mask);
bf189c26
MD
1253 if (!SIGNOTEMPTY(mask)) { /* no signal to send */
1254 rel_mplock();
984263bc 1255 return (0);
bf189c26 1256 }
984263bc
MD
1257 sig = sig_ffs(&mask);
1258
1259 STOPEVENT(p, S_SIG, sig);
1260
1261 /*
1262 * We should see pending but ignored signals
1263 * only if P_TRACED was on when they were posted.
1264 */
1265 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1266 SIGDELSET(p->p_siglist, sig);
1267 continue;
1268 }
344ad853 1269 if ((p->p_flag & P_TRACED) && (p->p_flag & P_PPWAIT) == 0) {
984263bc 1270 /*
344ad853
MD
1271 * If traced, always stop, and stay stopped until
1272 * released by the parent.
1273 *
1274 * NOTE: P_STOPPED may get cleared during the loop,
1275 * but we do not re-notify the parent if we have
1276 * to loop several times waiting for the parent
1277 * to let us continue.
984263bc
MD
1278 */
1279 p->p_xstat = sig;
344ad853
MD
1280 p->p_flag |= P_STOPPED;
1281 p->p_flag &= ~P_WAITED;
984263bc
MD
1282 psignal(p->p_pptr, SIGCHLD);
1283 do {
344ad853
MD
1284 tstop(p);
1285 } while (!trace_req(p) && (p->p_flag & P_TRACED));
1286 p->p_flag &= ~P_STOPPED;
984263bc
MD
1287
1288 /*
1289 * If parent wants us to take the signal,
1290 * then it will leave it in p->p_xstat;
1291 * otherwise we just look for signals again.
1292 */
1293 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1294 sig = p->p_xstat;
1295 if (sig == 0)
1296 continue;
1297
1298 /*
1299 * Put the new signal into p_siglist. If the
1300 * signal is being masked, look for other signals.
1301 */
1302 SIGADDSET(p->p_siglist, sig);
1303 if (SIGISMEMBER(p->p_sigmask, sig))
1304 continue;
1305
1306 /*
1307 * If the traced bit got turned off, go back up
1308 * to the top to rescan signals. This ensures
1309 * that p_sig* and ps_sigact are consistent.
1310 */
1311 if ((p->p_flag & P_TRACED) == 0)
1312 continue;
1313 }
1314
1315 prop = sigprop(sig);
1316
1317 /*
1318 * Decide whether the signal should be returned.
1319 * Return the signal's number, or fall through
1320 * to clear it from the pending mask.
1321 */
1322 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
984263bc
MD
1323 case (int)SIG_DFL:
1324 /*
1325 * Don't take default actions on system processes.
1326 */
1327 if (p->p_pid <= 1) {
1328#ifdef DIAGNOSTIC
1329 /*
1330 * Are you sure you want to ignore SIGSEGV
1331 * in init? XXX
1332 */
1333 printf("Process (pid %lu) got signal %d\n",
1334 (u_long)p->p_pid, sig);
1335#endif
1336 break; /* == ignore */
1337 }
f758a39e
MD
1338
1339 /*
1340 * Handle the in-kernel checkpoint action
1341 */
1342 if (prop & SA_CKPT) {
0ba6fbbf 1343 checkpoint_signal_handler(p);
f758a39e
MD
1344 break;
1345 }
1346
984263bc
MD
1347 /*
1348 * If there is a pending stop signal to process
1349 * with default action, stop here,
1350 * then clear the signal. However,
1351 * if process is member of an orphaned
1352 * process group, ignore tty stop signals.
1353 */
1354 if (prop & SA_STOP) {
1355 if (p->p_flag & P_TRACED ||
1356 (p->p_pgrp->pg_jobc == 0 &&
1357 prop & SA_TTYSTOP))
1358 break; /* == ignore */
1359 p->p_xstat = sig;
344ad853
MD
1360 p->p_flag |= P_STOPPED;
1361 p->p_flag &= ~P_WAITED;
1362
984263bc
MD
1363 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1364 psignal(p->p_pptr, SIGCHLD);
344ad853
MD
1365 while (p->p_flag & P_STOPPED) {
1366 tstop(p);
1367 }
984263bc
MD
1368 break;
1369 } else if (prop & SA_IGNORE) {
1370 /*
1371 * Except for SIGCONT, shouldn't get here.
1372 * Default action is to ignore; drop it.
1373 */
1374 break; /* == ignore */
8ebcad3f 1375 } else {
bf189c26 1376 rel_mplock();
984263bc 1377 return (sig);
8ebcad3f
MD
1378 }
1379
984263bc
MD
1380 /*NOTREACHED*/
1381
1382 case (int)SIG_IGN:
1383 /*
1384 * Masking above should prevent us ever trying
1385 * to take action on an ignored signal other
1386 * than SIGCONT, unless process is traced.
1387 */
1388 if ((prop & SA_CONT) == 0 &&
1389 (p->p_flag & P_TRACED) == 0)
1390 printf("issignal\n");
1391 break; /* == ignore */
1392
1393 default:
1394 /*
1395 * This signal has an action, let
1396 * postsig() process it.
1397 */
bf189c26 1398 rel_mplock();
984263bc
MD
1399 return (sig);
1400 }
1401 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1402 }
1403 /* NOTREACHED */
1404}
1405
984263bc
MD
1406/*
1407 * Take the action for the specified signal
1408 * from the current set of pending signals.
1409 */
1410void
8ebcad3f 1411postsig(int sig)
984263bc 1412{
41c20dac 1413 struct proc *p = curproc;
984263bc
MD
1414 struct sigacts *ps = p->p_sigacts;
1415 sig_t action;
1416 sigset_t returnmask;
1417 int code;
1418
1419 KASSERT(sig != 0, ("postsig"));
1420
1421 SIGDELSET(p->p_siglist, sig);
1422 action = ps->ps_sigact[_SIG_IDX(sig)];
1423#ifdef KTRACE
dadab5e9 1424 if (KTRPOINT(p->p_thread, KTR_PSIG))
984263bc
MD
1425 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1426 &p->p_oldsigmask : &p->p_sigmask, 0);
1427#endif
1428 STOPEVENT(p, S_SIG, sig);
1429
1430 if (action == SIG_DFL) {
1431 /*
1432 * Default action, where the default is to kill
1433 * the process. (Other cases were ignored above.)
1434 */
1435 sigexit(p, sig);
1436 /* NOTREACHED */
1437 } else {
1438 /*
1439 * If we get here, the signal must be caught.
1440 */
1441 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1442 ("postsig action"));
1443 /*
1444 * Set the new mask value and also defer further
1445 * occurrences of this signal.
1446 *
1447 * Special case: user has done a sigsuspend. Here the
1448 * current mask is not of interest, but rather the
1449 * mask from before the sigsuspend is what we want
1450 * restored after the signal processing is completed.
1451 */
e43a034f 1452 crit_enter();
984263bc
MD
1453 if (p->p_flag & P_OLDMASK) {
1454 returnmask = p->p_oldsigmask;
1455 p->p_flag &= ~P_OLDMASK;
8ebcad3f 1456 } else {
984263bc 1457 returnmask = p->p_sigmask;
8ebcad3f 1458 }
984263bc
MD
1459
1460 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1461 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1462 SIGADDSET(p->p_sigmask, sig);
1463
1464 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1465 /*
65957d54 1466 * See kern_sigaction() for origin of this code.
984263bc
MD
1467 */
1468 SIGDELSET(p->p_sigcatch, sig);
1469 if (sig != SIGCONT &&
1470 sigprop(sig) & SA_IGNORE)
1471 SIGADDSET(p->p_sigignore, sig);
1472 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1473 }
e43a034f 1474 crit_exit();
984263bc
MD
1475 p->p_stats->p_ru.ru_nsignals++;
1476 if (p->p_sig != sig) {
1477 code = 0;
1478 } else {
1479 code = p->p_code;
1480 p->p_code = 0;
1481 p->p_sig = 0;
1482 }
1483 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1484 }
1485}
1486
1487/*
1488 * Kill the current process for stated reason.
1489 */
1490void
8ebcad3f 1491killproc(struct proc *p, char *why)
984263bc
MD
1492{
1493 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
41c20dac 1494 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
984263bc
MD
1495 psignal(p, SIGKILL);
1496}
1497
1498/*
1499 * Force the current process to exit with the specified signal, dumping core
1500 * if appropriate. We bypass the normal tests for masked and caught signals,
1501 * allowing unrecoverable failures to terminate the process without changing
1502 * signal state. Mark the accounting record with the signal termination.
1503 * If dumping core, save the signal number for the debugger. Calls exit and
1504 * does not return.
1505 */
1506void
41c20dac 1507sigexit(struct proc *p, int sig)
984263bc 1508{
984263bc
MD
1509 p->p_acflag |= AXSIG;
1510 if (sigprop(sig) & SA_CORE) {
1511 p->p_sig = sig;
1512 /*
1513 * Log signals which would cause core dumps
1514 * (Log as LOG_INFO to appease those who don't want
1515 * these messages.)
1516 * XXX : Todo, as well as euid, write out ruid too
1517 */
1518 if (coredump(p) == 0)
1519 sig |= WCOREFLAG;
1520 if (kern_logsigexit)
1521 log(LOG_INFO,
1522 "pid %d (%s), uid %d: exited on signal %d%s\n",
1523 p->p_pid, p->p_comm,
41c20dac 1524 p->p_ucred ? p->p_ucred->cr_uid : -1,
984263bc
MD
1525 sig &~ WCOREFLAG,
1526 sig & WCOREFLAG ? " (core dumped)" : "");
1527 }
41c20dac 1528 exit1(W_EXITCODE(0, sig));
984263bc
MD
1529 /* NOTREACHED */
1530}
1531
1532static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1533SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1534 sizeof(corefilename), "process corefile name format string");
1535
1536/*
1537 * expand_name(name, uid, pid)
1538 * Expand the name described in corefilename, using name, uid, and pid.
1539 * corefilename is a printf-like string, with three format specifiers:
1540 * %N name of process ("name")
1541 * %P process id (pid)
1542 * %U user id (uid)
1543 * For example, "%N.core" is the default; they can be disabled completely
1544 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1545 * This is controlled by the sysctl variable kern.corefile (see above).
1546 */
1547
1548static char *
8ebcad3f
MD
1549expand_name(const char *name, uid_t uid, pid_t pid)
1550{
984263bc
MD
1551 char *temp;
1552 char buf[11]; /* Buffer for pid/uid -- max 4B */
1553 int i, n;
1554 char *format = corefilename;
1555 size_t namelen;
1556
1557 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1558 if (temp == NULL)
1559 return NULL;
1560 namelen = strlen(name);
1561 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1562 int l;
1563 switch (format[i]) {
1564 case '%': /* Format character */
1565 i++;
1566 switch (format[i]) {
1567 case '%':
1568 temp[n++] = '%';
1569 break;
1570 case 'N': /* process name */
1571 if ((n + namelen) > MAXPATHLEN) {
1572 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1573 pid, name, uid, temp, name);
1574 free(temp, M_TEMP);
1575 return NULL;
1576 }
1577 memcpy(temp+n, name, namelen);
1578 n += namelen;
1579 break;
1580 case 'P': /* process id */
1581 l = sprintf(buf, "%u", pid);
1582 if ((n + l) > MAXPATHLEN) {
1583 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1584 pid, name, uid, temp, name);
1585 free(temp, M_TEMP);
1586 return NULL;
1587 }
1588 memcpy(temp+n, buf, l);
1589 n += l;
1590 break;
1591 case 'U': /* user id */
1592 l = sprintf(buf, "%u", uid);
1593 if ((n + l) > MAXPATHLEN) {
1594 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1595 pid, name, uid, temp, name);
1596 free(temp, M_TEMP);
1597 return NULL;
1598 }
1599 memcpy(temp+n, buf, l);
1600 n += l;
1601 break;
1602 default:
1603 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1604 }
1605 break;
1606 default:
1607 temp[n++] = format[i];
1608 }
1609 }
1610 temp[n] = '\0';
1611 return temp;
1612}
1613
1614/*
1615 * Dump a process' core. The main routine does some
1616 * policy checking, and creates the name of the coredump;
1617 * then it passes on a vnode and a size limit to the process-specific
1618 * coredump routine if there is one; if there _is not_ one, it returns
1619 * ENOSYS; otherwise it returns the error from the process-specific routine.
1620 */
1621
1622static int
dadab5e9 1623coredump(struct proc *p)
984263bc 1624{
dadab5e9
MD
1625 struct vnode *vp;
1626 struct ucred *cred = p->p_ucred;
1627 struct thread *td = p->p_thread;
984263bc 1628 struct flock lf;
fad57d0e 1629 struct nlookupdata nd;
984263bc
MD
1630 struct vattr vattr;
1631 int error, error1;
1632 char *name; /* name of corefile */
1633 off_t limit;
1634
1635 STOPEVENT(p, S_CORE, 0);
1636
1637 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1638 return (EFAULT);
1639
1640 /*
1641 * Note that the bulk of limit checking is done after
1642 * the corefile is created. The exception is if the limit
1643 * for corefiles is 0, in which case we don't bother
1644 * creating the corefile at all. This layout means that
1645 * a corefile is truncated instead of not being created,
1646 * if it is larger than the limit.
1647 */
1648 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1649 if (limit == 0)
50fd5388 1650 return EFBIG;
984263bc
MD
1651
1652 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1653 if (name == NULL)
1654 return (EINVAL);
fad57d0e
MD
1655 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
1656 if (error == 0)
1657 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
984263bc 1658 free(name, M_TEMP);
fad57d0e
MD
1659 if (error) {
1660 nlookup_done(&nd);
984263bc 1661 return (error);
fad57d0e
MD
1662 }
1663 vp = nd.nl_open_vp;
1664 nd.nl_open_vp = NULL;
1665 nlookup_done(&nd);
984263bc 1666
5fd012e0 1667 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1668 lf.l_whence = SEEK_SET;
1669 lf.l_start = 0;
1670 lf.l_len = 0;
1671 lf.l_type = F_WRLCK;
1672 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1673 if (error)
1674 goto out2;
1675
1676 /* Don't dump to non-regular files or files with links. */
1677 if (vp->v_type != VREG ||
3b568787 1678 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
984263bc
MD
1679 error = EFAULT;
1680 goto out1;
1681 }
1682
1683 VATTR_NULL(&vattr);
5fd012e0 1684 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc 1685 vattr.va_size = 0;
dadab5e9
MD
1686 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1687 VOP_SETATTR(vp, &vattr, cred, td);
984263bc 1688 p->p_acflag |= ACORE;
5fd012e0 1689 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1690
1691 error = p->p_sysent->sv_coredump ?
fad57d0e 1692 p->p_sysent->sv_coredump(p, vp, limit) : ENOSYS;
984263bc
MD
1693
1694out1:
1695 lf.l_type = F_UNLCK;
1696 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1697out2:
3b568787 1698 error1 = vn_close(vp, FWRITE, td);
984263bc
MD
1699 if (error == 0)
1700 error = error1;
1701 return (error);
1702}
1703
1704/*
1705 * Nonexistent system call-- signal process (may want to handle it).
1706 * Flag error in case process won't see signal immediately (blocked or ignored).
1707 */
984263bc
MD
1708/* ARGSUSED */
1709int
41c20dac 1710nosys(struct nosys_args *args)
984263bc 1711{
41c20dac 1712 psignal(curproc, SIGSYS);
984263bc
MD
1713 return (EINVAL);
1714}
1715
1716/*
1717 * Send a SIGIO or SIGURG signal to a process or process group using
1718 * stored credentials rather than those of the current process.
1719 */
1720void
dadab5e9 1721pgsigio(struct sigio *sigio, int sig, int checkctty)
984263bc
MD
1722{
1723 if (sigio == NULL)
1724 return;
1725
1726 if (sigio->sio_pgid > 0) {
1727 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1728 sigio->sio_proc))
1729 psignal(sigio->sio_proc, sig);
1730 } else if (sigio->sio_pgid < 0) {
1731 struct proc *p;
1732
1733 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1734 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1735 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1736 psignal(p, sig);
1737 }
1738}
1739
1740static int
1741filt_sigattach(struct knote *kn)
1742{
1743 struct proc *p = curproc;
1744
1745 kn->kn_ptr.p_proc = p;
1746 kn->kn_flags |= EV_CLEAR; /* automatically set */
1747
1748 /* XXX lock the proc here while adding to the list? */
1749 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1750
1751 return (0);
1752}
1753
1754static void
1755filt_sigdetach(struct knote *kn)
1756{
1757 struct proc *p = kn->kn_ptr.p_proc;
1758
1759 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1760}
1761
1762/*
1763 * signal knotes are shared with proc knotes, so we apply a mask to
1764 * the hint in order to differentiate them from process hints. This
1765 * could be avoided by using a signal-specific knote list, but probably
1766 * isn't worth the trouble.
1767 */
1768static int
1769filt_signal(struct knote *kn, long hint)
1770{
984263bc
MD
1771 if (hint & NOTE_SIGNAL) {
1772 hint &= ~NOTE_SIGNAL;
1773
1774 if (kn->kn_id == hint)
1775 kn->kn_data++;
1776 }
1777 return (kn->kn_data != 0);
1778}