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