Fix a broken array lookup in the old 4.3 BSD mmap compatibility code
[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 $
553ea3c8 40 * $DragonFly: src/sys/kern/kern_sig.c,v 1.38 2005/10/11 09:59:56 corecode 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);
80static void stop(struct proc *);
cb973d15 81#ifdef SMP
8ebcad3f 82static void signotify_remote(void *arg);
cb973d15 83#endif
3a59f0f7
DX
84static int kern_sigtimedwait(sigset_t set, siginfo_t *info,
85 struct timespec *timeout);
984263bc
MD
86
87static int filt_sigattach(struct knote *kn);
88static void filt_sigdetach(struct knote *kn);
89static int filt_signal(struct knote *kn, long hint);
90
91struct filterops sig_filtops =
92 { 0, filt_sigattach, filt_sigdetach, filt_signal };
93
94static int kern_logsigexit = 1;
95SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
96 &kern_logsigexit, 0,
97 "Log processes quitting on abnormal signals to syslog(3)");
98
99/*
100 * Can process p, with pcred pc, send the signal sig to process q?
101 */
41c20dac
MD
102#define CANSIGNAL(q, sig) \
103 (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
104 ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
984263bc
MD
105
106/*
107 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
108 */
109#define CANSIGIO(ruid, uc, q) \
110 ((uc)->cr_uid == 0 || \
41c20dac
MD
111 (ruid) == (q)->p_ucred->cr_ruid || \
112 (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
984263bc
MD
113 (ruid) == (q)->p_ucred->cr_uid || \
114 (uc)->cr_uid == (q)->p_ucred->cr_uid)
115
116int sugid_coredump;
117SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
8ebcad3f 118 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
984263bc
MD
119
120static int do_coredump = 1;
121SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
122 &do_coredump, 0, "Enable/Disable coredumps");
123
124/*
125 * Signal properties and actions.
126 * The array below categorizes the signals and their default actions
127 * according to the following properties:
128 */
129#define SA_KILL 0x01 /* terminates process by default */
130#define SA_CORE 0x02 /* ditto and coredumps */
131#define SA_STOP 0x04 /* suspend process */
132#define SA_TTYSTOP 0x08 /* ditto, from tty */
133#define SA_IGNORE 0x10 /* ignore by default */
134#define SA_CONT 0x20 /* continue if suspended */
135#define SA_CANTMASK 0x40 /* non-maskable, catchable */
9d63d5c0
MD
136#define SA_CKPT 0x80 /* checkpoint process */
137
984263bc
MD
138
139static int sigproptbl[NSIG] = {
140 SA_KILL, /* SIGHUP */
141 SA_KILL, /* SIGINT */
142 SA_KILL|SA_CORE, /* SIGQUIT */
143 SA_KILL|SA_CORE, /* SIGILL */
144 SA_KILL|SA_CORE, /* SIGTRAP */
145 SA_KILL|SA_CORE, /* SIGABRT */
146 SA_KILL|SA_CORE, /* SIGEMT */
147 SA_KILL|SA_CORE, /* SIGFPE */
148 SA_KILL, /* SIGKILL */
149 SA_KILL|SA_CORE, /* SIGBUS */
150 SA_KILL|SA_CORE, /* SIGSEGV */
151 SA_KILL|SA_CORE, /* SIGSYS */
152 SA_KILL, /* SIGPIPE */
153 SA_KILL, /* SIGALRM */
154 SA_KILL, /* SIGTERM */
155 SA_IGNORE, /* SIGURG */
156 SA_STOP, /* SIGSTOP */
157 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
158 SA_IGNORE|SA_CONT, /* SIGCONT */
159 SA_IGNORE, /* SIGCHLD */
160 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
161 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
162 SA_IGNORE, /* SIGIO */
163 SA_KILL, /* SIGXCPU */
164 SA_KILL, /* SIGXFSZ */
165 SA_KILL, /* SIGVTALRM */
166 SA_KILL, /* SIGPROF */
167 SA_IGNORE, /* SIGWINCH */
168 SA_IGNORE, /* SIGINFO */
169 SA_KILL, /* SIGUSR1 */
170 SA_KILL, /* SIGUSR2 */
9d63d5c0
MD
171 SA_IGNORE, /* SIGTHR */
172 SA_CKPT, /* SIGCKPT */
173 SA_KILL|SA_CKPT, /* SIGCKPTEXIT */
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 SA_IGNORE,
204
984263bc
MD
205};
206
207static __inline int
208sigprop(int sig)
209{
210
211 if (sig > 0 && sig < NSIG)
212 return (sigproptbl[_SIG_IDX(sig)]);
213 return (0);
214}
215
216static __inline int
217sig_ffs(sigset_t *set)
218{
219 int i;
220
221 for (i = 0; i < _SIG_WORDS; i++)
222 if (set->__bits[i])
223 return (ffs(set->__bits[i]) + (i * 32));
224 return (0);
225}
226
65957d54
DRJ
227int
228kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
984263bc 229{
65957d54
DRJ
230 struct thread *td = curthread;
231 struct proc *p = td->td_proc;
41c20dac 232 struct sigacts *ps = p->p_sigacts;
984263bc
MD
233
234 if (sig <= 0 || sig > _SIG_MAXSIG)
235 return (EINVAL);
236
237 if (oact) {
238 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
239 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
240 oact->sa_flags = 0;
241 if (SIGISMEMBER(ps->ps_sigonstack, sig))
242 oact->sa_flags |= SA_ONSTACK;
243 if (!SIGISMEMBER(ps->ps_sigintr, sig))
244 oact->sa_flags |= SA_RESTART;
245 if (SIGISMEMBER(ps->ps_sigreset, sig))
246 oact->sa_flags |= SA_RESETHAND;
247 if (SIGISMEMBER(ps->ps_signodefer, sig))
248 oact->sa_flags |= SA_NODEFER;
249 if (SIGISMEMBER(ps->ps_siginfo, sig))
250 oact->sa_flags |= SA_SIGINFO;
251 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
252 oact->sa_flags |= SA_NOCLDSTOP;
253 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
254 oact->sa_flags |= SA_NOCLDWAIT;
255 }
256 if (act) {
257 if ((sig == SIGKILL || sig == SIGSTOP) &&
258 act->sa_handler != SIG_DFL)
259 return (EINVAL);
260
261 /*
262 * Change setting atomically.
263 */
e43a034f 264 crit_enter();
984263bc
MD
265
266 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
267 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
268 if (act->sa_flags & SA_SIGINFO) {
269 ps->ps_sigact[_SIG_IDX(sig)] =
270 (__sighandler_t *)act->sa_sigaction;
271 SIGADDSET(ps->ps_siginfo, sig);
272 } else {
273 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
274 SIGDELSET(ps->ps_siginfo, sig);
275 }
276 if (!(act->sa_flags & SA_RESTART))
277 SIGADDSET(ps->ps_sigintr, sig);
278 else
279 SIGDELSET(ps->ps_sigintr, sig);
280 if (act->sa_flags & SA_ONSTACK)
281 SIGADDSET(ps->ps_sigonstack, sig);
282 else
283 SIGDELSET(ps->ps_sigonstack, sig);
284 if (act->sa_flags & SA_RESETHAND)
285 SIGADDSET(ps->ps_sigreset, sig);
286 else
287 SIGDELSET(ps->ps_sigreset, sig);
288 if (act->sa_flags & SA_NODEFER)
289 SIGADDSET(ps->ps_signodefer, sig);
290 else
291 SIGDELSET(ps->ps_signodefer, sig);
984263bc
MD
292 if (sig == SIGCHLD) {
293 if (act->sa_flags & SA_NOCLDSTOP)
294 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
295 else
296 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
297 if (act->sa_flags & SA_NOCLDWAIT) {
298 /*
299 * Paranoia: since SA_NOCLDWAIT is implemented
300 * by reparenting the dying child to PID 1 (and
301 * trust it to reap the zombie), PID 1 itself
302 * is forbidden to set SA_NOCLDWAIT.
303 */
304 if (p->p_pid == 1)
305 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
306 else
307 p->p_procsig->ps_flag |= PS_NOCLDWAIT;
8ebcad3f 308 } else {
984263bc 309 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
8ebcad3f 310 }
984263bc
MD
311 }
312 /*
313 * Set bit in p_sigignore for signals that are set to SIG_IGN,
314 * and for signals set to SIG_DFL where the default is to
315 * ignore. However, don't put SIGCONT in p_sigignore, as we
316 * have to restart the process.
317 */
318 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
319 (sigprop(sig) & SA_IGNORE &&
320 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
321 /* never to be seen again */
322 SIGDELSET(p->p_siglist, sig);
323 if (sig != SIGCONT)
324 /* easier in psignal */
325 SIGADDSET(p->p_sigignore, sig);
326 SIGDELSET(p->p_sigcatch, sig);
327 } else {
328 SIGDELSET(p->p_sigignore, sig);
329 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
330 SIGDELSET(p->p_sigcatch, sig);
331 else
332 SIGADDSET(p->p_sigcatch, sig);
333 }
984263bc 334
e43a034f 335 crit_exit();
984263bc
MD
336 }
337 return (0);
338}
339
984263bc 340int
41c20dac 341sigaction(struct sigaction_args *uap)
984263bc
MD
342{
343 struct sigaction act, oact;
1fd87d54 344 struct sigaction *actp, *oactp;
984263bc
MD
345 int error;
346
347 actp = (uap->act != NULL) ? &act : NULL;
348 oactp = (uap->oact != NULL) ? &oact : NULL;
349 if (actp) {
350 error = copyin(uap->act, actp, sizeof(act));
351 if (error)
352 return (error);
353 }
65957d54 354 error = kern_sigaction(uap->sig, actp, oactp);
984263bc
MD
355 if (oactp && !error) {
356 error = copyout(oactp, uap->oact, sizeof(oact));
357 }
358 return (error);
359}
360
984263bc
MD
361/*
362 * Initialize signal state for process 0;
363 * set to ignore signals that are ignored by default.
364 */
365void
8ebcad3f 366siginit(struct proc *p)
984263bc 367{
1fd87d54 368 int i;
984263bc
MD
369
370 for (i = 1; i <= NSIG; i++)
371 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
372 SIGADDSET(p->p_sigignore, i);
373}
374
375/*
376 * Reset signals for an exec of the specified process.
377 */
378void
8ebcad3f 379execsigs(struct proc *p)
984263bc 380{
1fd87d54
RG
381 struct sigacts *ps = p->p_sigacts;
382 int sig;
984263bc
MD
383
384 /*
385 * Reset caught signals. Held signals remain held
386 * through p_sigmask (unless they were caught,
387 * and are now ignored by default).
388 */
389 while (SIGNOTEMPTY(p->p_sigcatch)) {
390 sig = sig_ffs(&p->p_sigcatch);
391 SIGDELSET(p->p_sigcatch, sig);
392 if (sigprop(sig) & SA_IGNORE) {
393 if (sig != SIGCONT)
394 SIGADDSET(p->p_sigignore, sig);
395 SIGDELSET(p->p_siglist, sig);
396 }
397 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
398 }
399 /*
400 * Reset stack state to the user stack.
401 * Clear set of signals caught on the signal stack.
402 */
403 p->p_sigstk.ss_flags = SS_DISABLE;
404 p->p_sigstk.ss_size = 0;
405 p->p_sigstk.ss_sp = 0;
406 p->p_flag &= ~P_ALTSTACK;
407 /*
408 * Reset no zombies if child dies flag as Solaris does.
409 */
410 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
411}
412
413/*
65957d54 414 * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
984263bc
MD
415 *
416 * Manipulate signal mask. This routine is MP SAFE *ONLY* if
e43a034f 417 * p == curproc.
984263bc 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{
553ea3c8 772 struct lwp *lp = &p->p_lwp;
e43a034f 773 int prop;
cb973d15 774 sig_t action;
984263bc
MD
775
776 if (sig > _SIG_MAXSIG || sig <= 0) {
777 printf("psignal: signal %d\n", sig);
778 panic("psignal signal number");
779 }
780
e43a034f 781 crit_enter();
984263bc 782 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
e43a034f 783 crit_exit();
984263bc
MD
784
785 prop = sigprop(sig);
786
787 /*
788 * If proc is traced, always give parent a chance;
789 * if signal event is tracked by procfs, give *that*
790 * a chance, as well.
791 */
8ebcad3f 792 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
984263bc 793 action = SIG_DFL;
8ebcad3f 794 } else {
984263bc
MD
795 /*
796 * If the signal is being ignored,
797 * then we forget about it immediately.
798 * (Note: we don't set SIGCONT in p_sigignore,
799 * and if it is set to SIG_IGN,
800 * action will be SIG_DFL here.)
801 */
802 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
803 return;
804 if (SIGISMEMBER(p->p_sigmask, sig))
805 action = SIG_HOLD;
806 else if (SIGISMEMBER(p->p_sigcatch, sig))
807 action = SIG_CATCH;
808 else
809 action = SIG_DFL;
810 }
811
812 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
8ebcad3f 813 (p->p_flag & P_TRACED) == 0) {
984263bc 814 p->p_nice = NZERO;
8ebcad3f 815 }
984263bc
MD
816
817 if (prop & SA_CONT)
818 SIG_STOPSIGMASK(p->p_siglist);
9d63d5c0 819
984263bc
MD
820
821 if (prop & SA_STOP) {
822 /*
823 * If sending a tty stop signal to a member of an orphaned
824 * process group, discard the signal here if the action
825 * is default; don't stop the process below if sleeping,
826 * and don't clear any pending SIGCONT.
827 */
828 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
8ebcad3f 829 action == SIG_DFL) {
984263bc 830 return;
8ebcad3f 831 }
984263bc
MD
832 SIG_CONTSIGMASK(p->p_siglist);
833 }
834 SIGADDSET(p->p_siglist, sig);
835
836 /*
837 * Defer further processing for signals which are held,
838 * except that stopped processes must be continued by SIGCONT.
839 */
840 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
841 return;
e43a034f
MD
842
843 crit_enter();
844
984263bc 845 switch (p->p_stat) {
984263bc
MD
846 case SSLEEP:
847 /*
848 * If process is sleeping uninterruptibly
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;
855 /*
856 * Process is sleeping and traced... make it runnable
857 * so it can discover the signal in issignal() and stop
858 * for the parent.
859 */
860 if (p->p_flag & P_TRACED)
861 goto run;
862 /*
863 * If SIGCONT is default (or ignored) and process is
864 * asleep, we are finished; the process should not
865 * be awakened.
866 */
867 if ((prop & SA_CONT) && action == SIG_DFL) {
868 SIGDELSET(p->p_siglist, sig);
869 goto out;
870 }
871 /*
872 * When a sleeping process receives a stop
873 * signal, process immediately if possible.
874 * All other (caught or default) signals
875 * cause the process to run.
876 */
877 if (prop & SA_STOP) {
878 if (action != SIG_DFL)
26a0694b 879 goto run;
984263bc
MD
880 /*
881 * If a child holding parent blocked,
882 * stopping could cause deadlock.
883 */
884 if (p->p_flag & P_PPWAIT)
885 goto out;
886 SIGDELSET(p->p_siglist, sig);
887 p->p_xstat = sig;
888 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
889 psignal(p->p_pptr, SIGCHLD);
890 stop(p);
891 goto out;
8ebcad3f 892 } else {
26a0694b 893 goto run;
8ebcad3f 894 }
984263bc 895 /*NOTREACHED*/
984263bc
MD
896 case SSTOP:
897 /*
898 * If traced process is already stopped,
899 * then no further action is necessary.
900 */
901 if (p->p_flag & P_TRACED)
902 goto out;
903
904 /*
905 * Kill signal always sets processes running.
906 */
907 if (sig == SIGKILL)
26a0694b 908 goto run;
984263bc
MD
909
910 if (prop & SA_CONT) {
911 /*
912 * If SIGCONT is default (or ignored), we continue the
913 * process but don't leave the signal in p_siglist, as
914 * it has no further action. If SIGCONT is held, we
915 * continue the process and leave the signal in
916 * p_siglist. If the process catches SIGCONT, let it
917 * handle the signal itself. If it isn't waiting on
918 * an event, then it goes back to run state.
919 * Otherwise, process goes back to sleep state.
920 */
921 if (action == SIG_DFL)
922 SIGDELSET(p->p_siglist, sig);
923 if (action == SIG_CATCH)
26a0694b 924 goto run;
984263bc
MD
925 if (p->p_wchan == 0)
926 goto run;
26a0694b 927 clrrunnable(p, SSLEEP);
984263bc
MD
928 goto out;
929 }
930
931 if (prop & SA_STOP) {
932 /*
933 * Already stopped, don't need to stop again.
934 * (If we did the shell could get confused.)
935 */
936 SIGDELSET(p->p_siglist, sig);
937 goto out;
938 }
939
940 /*
941 * If process is sleeping interruptibly, then simulate a
942 * wakeup so that when it is continued, it will be made
943 * runnable and can look at the signal. But don't make
944 * the process runnable, leave it stopped.
945 */
92e4c1e5 946 if (p->p_wchan && (p->p_flag & P_SINTR))
0cfcada1 947 unsleep(p->p_thread);
984263bc 948 goto out;
984263bc
MD
949 default:
950 /*
951 * SRUN, SIDL, SZOMB do nothing with the signal,
952 * other than kicking ourselves if we are running.
953 * It will either never be noticed, or noticed very soon.
cb973d15 954 *
5fd012e0
MD
955 * Note that p_thread may be NULL or may not be completely
956 * initialized if the process is in the SIDL or SZOMB state.
957 *
cb973d15
MD
958 * For SMP we may have to forward the request to another cpu.
959 * YYY the MP lock prevents the target process from moving
960 * to another cpu, see kern/kern_switch.c
60be1d11
MD
961 *
962 * If the target thread is waiting on its message port,
963 * wakeup the target thread so it can check (or ignore)
964 * the new signal. YYY needs cleanup.
984263bc 965 */
984263bc 966#ifdef SMP
553ea3c8 967 if (lp == lwkt_preempted_proc()) {
235957ed 968 signotify();
5fd012e0 969 } else if (p->p_stat == SRUN) {
a72187e9
MD
970 struct thread *td = p->p_thread;
971
5fd012e0
MD
972 KASSERT(td != NULL,
973 ("pid %d NULL p_thread stat %d flags %08x",
974 p->p_pid, p->p_stat, p->p_flag));
975
a72187e9 976 if (td->td_gd != mycpu)
553ea3c8 977 lwkt_send_ipiq(td->td_gd, signotify_remote, lp);
60be1d11
MD
978 else if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
979 lwkt_schedule(td);
cb973d15
MD
980 }
981#else
553ea3c8 982 if (lp == lwkt_preempted_proc()) {
235957ed 983 signotify();
5fd012e0 984 } else if (p->p_stat == SRUN) {
60be1d11 985 struct thread *td = p->p_thread;
5fd012e0
MD
986
987 KASSERT(td != NULL,
988 ("pid %d NULL p_thread stat %d flags %08x",
989 p->p_pid, p->p_stat, p->p_flag));
990
60be1d11
MD
991 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
992 lwkt_schedule(td);
993 }
984263bc
MD
994#endif
995 goto out;
996 }
997 /*NOTREACHED*/
984263bc
MD
998run:
999 setrunnable(p);
1000out:
e43a034f 1001 crit_exit();
984263bc
MD
1002}
1003
cb973d15
MD
1004#ifdef SMP
1005
1006/*
1007 * This function is called via an IPI. We will be in a critical section but
1008 * the MP lock will NOT be held. Also note that by the time the ipi message
1009 * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1010 */
1011static void
1012signotify_remote(void *arg)
1013{
553ea3c8 1014 struct lwp *lp = arg;
60be1d11 1015
553ea3c8 1016 if (lp == lwkt_preempted_proc()) {
235957ed 1017 signotify();
60be1d11 1018 } else {
553ea3c8 1019 struct thread *td = lp->lwp_thread;
60be1d11
MD
1020 if (td->td_msgport.mp_flags & MSGPORTF_WAITING)
1021 lwkt_schedule(td);
1022 }
cb973d15
MD
1023}
1024
1025#endif
1026
3a59f0f7
DX
1027static int
1028kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1029{
1030 sigset_t savedmask, set;
1031 struct proc *p = curproc;
1032 int error, sig, hz, timevalid = 0;
1033 struct timespec rts, ets, ts;
1034 struct timeval tv;
1035
1036 error = 0;
1037 sig = 0;
1038 SIG_CANTMASK(waitset);
1039 savedmask = p->p_sigmask;
1040
1041 if (timeout) {
f8c41806
DX
1042 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1043 timeout->tv_nsec < 1000000000) {
3a59f0f7
DX
1044 timevalid = 1;
1045 getnanouptime(&rts);
1046 ets = rts;
1047 timespecadd(&ets, timeout);
1048 }
1049 }
1050
1051 for (;;) {
1052 set = p->p_siglist;
1053 SIGSETAND(set, waitset);
1054 if ((sig = sig_ffs(&set)) != 0) {
1055 SIGFILLSET(p->p_sigmask);
1056 SIGDELSET(p->p_sigmask, sig);
976f0bc5 1057 SIG_CANTMASK(p->p_sigmask);
3a59f0f7
DX
1058 sig = issignal(p);
1059 /*
1060 * It may be a STOP signal, in the case, issignal
1061 * returns 0, because we may stop there, and new
1062 * signal can come in, we should restart if we got
1063 * nothing.
1064 */
1065 if (sig == 0)
1066 continue;
1067 else
1068 break;
1069 }
1070
1071 /*
1072 * Previous checking got nothing, and we retried but still
1073 * got nothing, we should return the error status.
1074 */
1075 if (error)
1076 break;
1077
1078 /*
1079 * POSIX says this must be checked after looking for pending
1080 * signals.
1081 */
1082 if (timeout) {
1083 if (!timevalid) {
1084 error = EINVAL;
1085 break;
1086 }
1087 getnanouptime(&rts);
1088 if (timespeccmp(&rts, &ets, >=)) {
1089 error = EAGAIN;
1090 break;
1091 }
1092 ts = ets;
1093 timespecsub(&ts, &rts);
1094 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1095 hz = tvtohz_high(&tv);
1096 } else
1097 hz = 0;
1098
1099 p->p_sigmask = savedmask;
1100 SIGSETNAND(p->p_sigmask, waitset);
1101 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1102 if (timeout) {
1103 if (error == ERESTART) {
1104 /* can not restart a timeout wait. */
1105 error = EINTR;
1106 } else if (error == EAGAIN) {
1107 /* will calculate timeout by ourself. */
1108 error = 0;
1109 }
1110 }
1111 /* Retry ... */
1112 }
1113
1114 p->p_sigmask = savedmask;
1115 if (sig) {
1116 error = 0;
1117 bzero(info, sizeof(*info));
1118 info->si_signo = sig;
1119 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1120 }
1121 return (error);
1122}
1123
1124int
1125sigtimedwait(struct sigtimedwait_args *uap)
1126{
1127 struct timespec ts;
1128 struct timespec *timeout;
1129 sigset_t set;
1130 siginfo_t info;
1131 int error;
1132
1133 if (uap->timeout) {
1134 error = copyin(uap->timeout, &ts, sizeof(ts));
1135 if (error)
1136 return (error);
1137 timeout = &ts;
1138 } else {
1139 timeout = NULL;
1140 }
1141 error = copyin(uap->set, &set, sizeof(set));
1142 if (error)
1143 return (error);
1144 error = kern_sigtimedwait(set, &info, timeout);
1145 if (error)
1146 return (error);
1147 if (uap->info)
1148 error = copyout(&info, uap->info, sizeof(info));
1149 /* Repost if we got an error. */
1150 if (error)
1151 psignal(curproc, info.si_signo);
1152 else
1153 uap->sysmsg_result = info.si_signo;
1154 return (error);
1155}
1156
1157int
1158sigwaitinfo(struct sigwaitinfo_args *uap)
1159{
1160 siginfo_t info;
1161 sigset_t set;
1162 int error;
1163
1164 error = copyin(uap->set, &set, sizeof(set));
1165 if (error)
1166 return (error);
1167 error = kern_sigtimedwait(set, &info, NULL);
1168 if (error)
1169 return (error);
1170 if (uap->info)
1171 error = copyout(&info, uap->info, sizeof(info));
1172 /* Repost if we got an error. */
1173 if (error)
1174 psignal(curproc, info.si_signo);
1175 else
1176 uap->sysmsg_result = info.si_signo;
1177 return (error);
1178}
1179
a94976ad
MD
1180/*
1181 * If the current process has received a signal that would interrupt a
1182 * system call, return EINTR or ERESTART as appropriate.
1183 */
1184int
1185iscaught(struct proc *p)
1186{
1187 int sig;
1188
1189 if (p) {
1190 if ((sig = CURSIG(p)) != 0) {
1191 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1192 return (EINTR);
1193 return (ERESTART);
1194 }
1195 }
1196 return(EWOULDBLOCK);
1197}
1198
984263bc
MD
1199/*
1200 * If the current process has received a signal (should be caught or cause
1201 * termination, should interrupt current syscall), return the signal number.
1202 * Stop signals with default action are processed immediately, then cleared;
1203 * they aren't returned. This is checked after each entry to the system for
1204 * a syscall or trap (though this can usually be done without calling issignal
1205 * by checking the pending signal masks in the CURSIG macro.) The normal call
1206 * sequence is
1207 *
1208 * while (sig = CURSIG(curproc))
1209 * postsig(sig);
1210 */
1211int
8ebcad3f 1212issignal(struct proc *p)
984263bc
MD
1213{
1214 sigset_t mask;
1fd87d54 1215 int sig, prop;
984263bc
MD
1216
1217 for (;;) {
1218 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1219
1220 mask = p->p_siglist;
1221 SIGSETNAND(mask, p->p_sigmask);
1222 if (p->p_flag & P_PPWAIT)
1223 SIG_STOPSIGMASK(mask);
1224 if (!SIGNOTEMPTY(mask)) /* no signal to send */
1225 return (0);
1226 sig = sig_ffs(&mask);
1227
1228 STOPEVENT(p, S_SIG, sig);
1229
1230 /*
1231 * We should see pending but ignored signals
1232 * only if P_TRACED was on when they were posted.
1233 */
1234 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1235 SIGDELSET(p->p_siglist, sig);
1236 continue;
1237 }
1238 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1239 /*
1240 * If traced, always stop, and stay
1241 * stopped until released by the parent.
1242 */
1243 p->p_xstat = sig;
1244 psignal(p->p_pptr, SIGCHLD);
1245 do {
1246 stop(p);
37af14fe 1247 mi_switch(p);
26a0694b 1248 } while (!trace_req(p) && p->p_flag & P_TRACED);
984263bc
MD
1249
1250 /*
1251 * If parent wants us to take the signal,
1252 * then it will leave it in p->p_xstat;
1253 * otherwise we just look for signals again.
1254 */
1255 SIGDELSET(p->p_siglist, sig); /* clear old signal */
1256 sig = p->p_xstat;
1257 if (sig == 0)
1258 continue;
1259
1260 /*
1261 * Put the new signal into p_siglist. If the
1262 * signal is being masked, look for other signals.
1263 */
1264 SIGADDSET(p->p_siglist, sig);
1265 if (SIGISMEMBER(p->p_sigmask, sig))
1266 continue;
1267
1268 /*
1269 * If the traced bit got turned off, go back up
1270 * to the top to rescan signals. This ensures
1271 * that p_sig* and ps_sigact are consistent.
1272 */
1273 if ((p->p_flag & P_TRACED) == 0)
1274 continue;
1275 }
1276
1277 prop = sigprop(sig);
1278
1279 /*
1280 * Decide whether the signal should be returned.
1281 * Return the signal's number, or fall through
1282 * to clear it from the pending mask.
1283 */
1284 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1285
1286 case (int)SIG_DFL:
1287 /*
1288 * Don't take default actions on system processes.
1289 */
1290 if (p->p_pid <= 1) {
1291#ifdef DIAGNOSTIC
1292 /*
1293 * Are you sure you want to ignore SIGSEGV
1294 * in init? XXX
1295 */
1296 printf("Process (pid %lu) got signal %d\n",
1297 (u_long)p->p_pid, sig);
1298#endif
1299 break; /* == ignore */
1300 }
f758a39e
MD
1301
1302 /*
1303 * Handle the in-kernel checkpoint action
1304 */
1305 if (prop & SA_CKPT) {
0ba6fbbf 1306 checkpoint_signal_handler(p);
f758a39e
MD
1307 break;
1308 }
1309
984263bc
MD
1310 /*
1311 * If there is a pending stop signal to process
1312 * with default action, stop here,
1313 * then clear the signal. However,
1314 * if process is member of an orphaned
1315 * process group, ignore tty stop signals.
1316 */
1317 if (prop & SA_STOP) {
1318 if (p->p_flag & P_TRACED ||
1319 (p->p_pgrp->pg_jobc == 0 &&
1320 prop & SA_TTYSTOP))
1321 break; /* == ignore */
1322 p->p_xstat = sig;
1323 stop(p);
1324 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1325 psignal(p->p_pptr, SIGCHLD);
37af14fe 1326 mi_switch(p);
984263bc
MD
1327 break;
1328 } else if (prop & SA_IGNORE) {
1329 /*
1330 * Except for SIGCONT, shouldn't get here.
1331 * Default action is to ignore; drop it.
1332 */
1333 break; /* == ignore */
8ebcad3f 1334 } else {
984263bc 1335 return (sig);
8ebcad3f
MD
1336 }
1337
984263bc
MD
1338 /*NOTREACHED*/
1339
1340 case (int)SIG_IGN:
1341 /*
1342 * Masking above should prevent us ever trying
1343 * to take action on an ignored signal other
1344 * than SIGCONT, unless process is traced.
1345 */
1346 if ((prop & SA_CONT) == 0 &&
1347 (p->p_flag & P_TRACED) == 0)
1348 printf("issignal\n");
1349 break; /* == ignore */
1350
1351 default:
1352 /*
1353 * This signal has an action, let
1354 * postsig() process it.
1355 */
1356 return (sig);
1357 }
1358 SIGDELSET(p->p_siglist, sig); /* take the signal! */
1359 }
1360 /* NOTREACHED */
1361}
1362
1363/*
1364 * Put the argument process into the stopped state and notify the parent
1365 * via wakeup. Signals are handled elsewhere. The process must not be
1366 * on the run queue.
1367 */
1368void
8ebcad3f 1369stop(struct proc *p)
984263bc 1370{
984263bc
MD
1371 p->p_stat = SSTOP;
1372 p->p_flag &= ~P_WAITED;
1373 wakeup((caddr_t)p->p_pptr);
1374}
1375
1376/*
1377 * Take the action for the specified signal
1378 * from the current set of pending signals.
1379 */
1380void
8ebcad3f 1381postsig(int sig)
984263bc 1382{
41c20dac 1383 struct proc *p = curproc;
984263bc
MD
1384 struct sigacts *ps = p->p_sigacts;
1385 sig_t action;
1386 sigset_t returnmask;
1387 int code;
1388
1389 KASSERT(sig != 0, ("postsig"));
1390
1391 SIGDELSET(p->p_siglist, sig);
1392 action = ps->ps_sigact[_SIG_IDX(sig)];
1393#ifdef KTRACE
dadab5e9 1394 if (KTRPOINT(p->p_thread, KTR_PSIG))
984263bc
MD
1395 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1396 &p->p_oldsigmask : &p->p_sigmask, 0);
1397#endif
1398 STOPEVENT(p, S_SIG, sig);
1399
1400 if (action == SIG_DFL) {
1401 /*
1402 * Default action, where the default is to kill
1403 * the process. (Other cases were ignored above.)
1404 */
1405 sigexit(p, sig);
1406 /* NOTREACHED */
1407 } else {
1408 /*
1409 * If we get here, the signal must be caught.
1410 */
1411 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1412 ("postsig action"));
1413 /*
1414 * Set the new mask value and also defer further
1415 * occurrences of this signal.
1416 *
1417 * Special case: user has done a sigsuspend. Here the
1418 * current mask is not of interest, but rather the
1419 * mask from before the sigsuspend is what we want
1420 * restored after the signal processing is completed.
1421 */
e43a034f 1422 crit_enter();
984263bc
MD
1423 if (p->p_flag & P_OLDMASK) {
1424 returnmask = p->p_oldsigmask;
1425 p->p_flag &= ~P_OLDMASK;
8ebcad3f 1426 } else {
984263bc 1427 returnmask = p->p_sigmask;
8ebcad3f 1428 }
984263bc
MD
1429
1430 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1431 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1432 SIGADDSET(p->p_sigmask, sig);
1433
1434 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1435 /*
65957d54 1436 * See kern_sigaction() for origin of this code.
984263bc
MD
1437 */
1438 SIGDELSET(p->p_sigcatch, sig);
1439 if (sig != SIGCONT &&
1440 sigprop(sig) & SA_IGNORE)
1441 SIGADDSET(p->p_sigignore, sig);
1442 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1443 }
e43a034f 1444 crit_exit();
984263bc
MD
1445 p->p_stats->p_ru.ru_nsignals++;
1446 if (p->p_sig != sig) {
1447 code = 0;
1448 } else {
1449 code = p->p_code;
1450 p->p_code = 0;
1451 p->p_sig = 0;
1452 }
1453 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1454 }
1455}
1456
1457/*
1458 * Kill the current process for stated reason.
1459 */
1460void
8ebcad3f 1461killproc(struct proc *p, char *why)
984263bc
MD
1462{
1463 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
41c20dac 1464 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
984263bc
MD
1465 psignal(p, SIGKILL);
1466}
1467
1468/*
1469 * Force the current process to exit with the specified signal, dumping core
1470 * if appropriate. We bypass the normal tests for masked and caught signals,
1471 * allowing unrecoverable failures to terminate the process without changing
1472 * signal state. Mark the accounting record with the signal termination.
1473 * If dumping core, save the signal number for the debugger. Calls exit and
1474 * does not return.
1475 */
1476void
41c20dac 1477sigexit(struct proc *p, int sig)
984263bc 1478{
984263bc
MD
1479 p->p_acflag |= AXSIG;
1480 if (sigprop(sig) & SA_CORE) {
1481 p->p_sig = sig;
1482 /*
1483 * Log signals which would cause core dumps
1484 * (Log as LOG_INFO to appease those who don't want
1485 * these messages.)
1486 * XXX : Todo, as well as euid, write out ruid too
1487 */
1488 if (coredump(p) == 0)
1489 sig |= WCOREFLAG;
1490 if (kern_logsigexit)
1491 log(LOG_INFO,
1492 "pid %d (%s), uid %d: exited on signal %d%s\n",
1493 p->p_pid, p->p_comm,
41c20dac 1494 p->p_ucred ? p->p_ucred->cr_uid : -1,
984263bc
MD
1495 sig &~ WCOREFLAG,
1496 sig & WCOREFLAG ? " (core dumped)" : "");
1497 }
41c20dac 1498 exit1(W_EXITCODE(0, sig));
984263bc
MD
1499 /* NOTREACHED */
1500}
1501
1502static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1503SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1504 sizeof(corefilename), "process corefile name format string");
1505
1506/*
1507 * expand_name(name, uid, pid)
1508 * Expand the name described in corefilename, using name, uid, and pid.
1509 * corefilename is a printf-like string, with three format specifiers:
1510 * %N name of process ("name")
1511 * %P process id (pid)
1512 * %U user id (uid)
1513 * For example, "%N.core" is the default; they can be disabled completely
1514 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1515 * This is controlled by the sysctl variable kern.corefile (see above).
1516 */
1517
1518static char *
8ebcad3f
MD
1519expand_name(const char *name, uid_t uid, pid_t pid)
1520{
984263bc
MD
1521 char *temp;
1522 char buf[11]; /* Buffer for pid/uid -- max 4B */
1523 int i, n;
1524 char *format = corefilename;
1525 size_t namelen;
1526
1527 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1528 if (temp == NULL)
1529 return NULL;
1530 namelen = strlen(name);
1531 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1532 int l;
1533 switch (format[i]) {
1534 case '%': /* Format character */
1535 i++;
1536 switch (format[i]) {
1537 case '%':
1538 temp[n++] = '%';
1539 break;
1540 case 'N': /* process name */
1541 if ((n + namelen) > MAXPATHLEN) {
1542 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1543 pid, name, uid, temp, name);
1544 free(temp, M_TEMP);
1545 return NULL;
1546 }
1547 memcpy(temp+n, name, namelen);
1548 n += namelen;
1549 break;
1550 case 'P': /* process id */
1551 l = sprintf(buf, "%u", pid);
1552 if ((n + l) > MAXPATHLEN) {
1553 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1554 pid, name, uid, temp, name);
1555 free(temp, M_TEMP);
1556 return NULL;
1557 }
1558 memcpy(temp+n, buf, l);
1559 n += l;
1560 break;
1561 case 'U': /* user id */
1562 l = sprintf(buf, "%u", uid);
1563 if ((n + l) > MAXPATHLEN) {
1564 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
1565 pid, name, uid, temp, name);
1566 free(temp, M_TEMP);
1567 return NULL;
1568 }
1569 memcpy(temp+n, buf, l);
1570 n += l;
1571 break;
1572 default:
1573 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1574 }
1575 break;
1576 default:
1577 temp[n++] = format[i];
1578 }
1579 }
1580 temp[n] = '\0';
1581 return temp;
1582}
1583
1584/*
1585 * Dump a process' core. The main routine does some
1586 * policy checking, and creates the name of the coredump;
1587 * then it passes on a vnode and a size limit to the process-specific
1588 * coredump routine if there is one; if there _is not_ one, it returns
1589 * ENOSYS; otherwise it returns the error from the process-specific routine.
1590 */
1591
1592static int
dadab5e9 1593coredump(struct proc *p)
984263bc 1594{
dadab5e9
MD
1595 struct vnode *vp;
1596 struct ucred *cred = p->p_ucred;
1597 struct thread *td = p->p_thread;
984263bc 1598 struct flock lf;
fad57d0e 1599 struct nlookupdata nd;
984263bc
MD
1600 struct vattr vattr;
1601 int error, error1;
1602 char *name; /* name of corefile */
1603 off_t limit;
1604
1605 STOPEVENT(p, S_CORE, 0);
1606
1607 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1608 return (EFAULT);
1609
1610 /*
1611 * Note that the bulk of limit checking is done after
1612 * the corefile is created. The exception is if the limit
1613 * for corefiles is 0, in which case we don't bother
1614 * creating the corefile at all. This layout means that
1615 * a corefile is truncated instead of not being created,
1616 * if it is larger than the limit.
1617 */
1618 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1619 if (limit == 0)
50fd5388 1620 return EFBIG;
984263bc
MD
1621
1622 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1623 if (name == NULL)
1624 return (EINVAL);
fad57d0e
MD
1625 error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
1626 if (error == 0)
1627 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
984263bc 1628 free(name, M_TEMP);
fad57d0e
MD
1629 if (error) {
1630 nlookup_done(&nd);
984263bc 1631 return (error);
fad57d0e
MD
1632 }
1633 vp = nd.nl_open_vp;
1634 nd.nl_open_vp = NULL;
1635 nlookup_done(&nd);
984263bc 1636
5fd012e0 1637 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1638 lf.l_whence = SEEK_SET;
1639 lf.l_start = 0;
1640 lf.l_len = 0;
1641 lf.l_type = F_WRLCK;
1642 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1643 if (error)
1644 goto out2;
1645
1646 /* Don't dump to non-regular files or files with links. */
1647 if (vp->v_type != VREG ||
3b568787 1648 VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
984263bc
MD
1649 error = EFAULT;
1650 goto out1;
1651 }
1652
1653 VATTR_NULL(&vattr);
5fd012e0 1654 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc 1655 vattr.va_size = 0;
dadab5e9
MD
1656 VOP_LEASE(vp, td, cred, LEASE_WRITE);
1657 VOP_SETATTR(vp, &vattr, cred, td);
984263bc 1658 p->p_acflag |= ACORE;
5fd012e0 1659 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1660
1661 error = p->p_sysent->sv_coredump ?
fad57d0e 1662 p->p_sysent->sv_coredump(p, vp, limit) : ENOSYS;
984263bc
MD
1663
1664out1:
1665 lf.l_type = F_UNLCK;
1666 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1667out2:
3b568787 1668 error1 = vn_close(vp, FWRITE, td);
984263bc
MD
1669 if (error == 0)
1670 error = error1;
1671 return (error);
1672}
1673
1674/*
1675 * Nonexistent system call-- signal process (may want to handle it).
1676 * Flag error in case process won't see signal immediately (blocked or ignored).
1677 */
984263bc
MD
1678/* ARGSUSED */
1679int
41c20dac 1680nosys(struct nosys_args *args)
984263bc 1681{
41c20dac 1682 psignal(curproc, SIGSYS);
984263bc
MD
1683 return (EINVAL);
1684}
1685
1686/*
1687 * Send a SIGIO or SIGURG signal to a process or process group using
1688 * stored credentials rather than those of the current process.
1689 */
1690void
dadab5e9 1691pgsigio(struct sigio *sigio, int sig, int checkctty)
984263bc
MD
1692{
1693 if (sigio == NULL)
1694 return;
1695
1696 if (sigio->sio_pgid > 0) {
1697 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1698 sigio->sio_proc))
1699 psignal(sigio->sio_proc, sig);
1700 } else if (sigio->sio_pgid < 0) {
1701 struct proc *p;
1702
1703 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1704 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1705 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1706 psignal(p, sig);
1707 }
1708}
1709
1710static int
1711filt_sigattach(struct knote *kn)
1712{
1713 struct proc *p = curproc;
1714
1715 kn->kn_ptr.p_proc = p;
1716 kn->kn_flags |= EV_CLEAR; /* automatically set */
1717
1718 /* XXX lock the proc here while adding to the list? */
1719 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1720
1721 return (0);
1722}
1723
1724static void
1725filt_sigdetach(struct knote *kn)
1726{
1727 struct proc *p = kn->kn_ptr.p_proc;
1728
1729 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1730}
1731
1732/*
1733 * signal knotes are shared with proc knotes, so we apply a mask to
1734 * the hint in order to differentiate them from process hints. This
1735 * could be avoided by using a signal-specific knote list, but probably
1736 * isn't worth the trouble.
1737 */
1738static int
1739filt_signal(struct knote *kn, long hint)
1740{
984263bc
MD
1741 if (hint & NOTE_SIGNAL) {
1742 hint &= ~NOTE_SIGNAL;
1743
1744 if (kn->kn_id == hint)
1745 kn->kn_data++;
1746 }
1747 return (kn->kn_data != 0);
1748}