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