Deleted Added
full compact
kern_sig.c (46155) kern_sig.c (46381)
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
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 * $Id: kern_sig.c,v 1.54 1999/01/26 02:38:10 julian Exp $
39 * $Id: kern_sig.c,v 1.55 1999/04/28 11:36:59 phk Exp $
40 */
41
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
45#define SIGPROP /* include signal properties table */
46#include <sys/param.h>
47#include <sys/kernel.h>
48#include <sys/sysproto.h>
49#include <sys/signalvar.h>
50#include <sys/resourcevar.h>
51#include <sys/namei.h>
52#include <sys/vnode.h>
53#include <sys/proc.h>
54#include <sys/pioctl.h>
55#include <sys/systm.h>
56#include <sys/acct.h>
57#include <sys/fcntl.h>
58#include <sys/wait.h>
59#include <sys/ktrace.h>
60#include <sys/syslog.h>
61#include <sys/stat.h>
62#include <sys/sysent.h>
63#include <sys/sysctl.h>
64#include <sys/malloc.h>
65
66#include <machine/cpu.h>
67#ifdef SMP
68#include <machine/smp.h>
69#endif
70
71static int killpg1 __P((struct proc *cp, int signum, int pgid, int all));
72static void setsigvec __P((struct proc *p, int signum, struct sigaction *sa));
73static void stop __P((struct proc *));
74
75static int kern_logsigexit = 1;
40 */
41
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
45#define SIGPROP /* include signal properties table */
46#include <sys/param.h>
47#include <sys/kernel.h>
48#include <sys/sysproto.h>
49#include <sys/signalvar.h>
50#include <sys/resourcevar.h>
51#include <sys/namei.h>
52#include <sys/vnode.h>
53#include <sys/proc.h>
54#include <sys/pioctl.h>
55#include <sys/systm.h>
56#include <sys/acct.h>
57#include <sys/fcntl.h>
58#include <sys/wait.h>
59#include <sys/ktrace.h>
60#include <sys/syslog.h>
61#include <sys/stat.h>
62#include <sys/sysent.h>
63#include <sys/sysctl.h>
64#include <sys/malloc.h>
65
66#include <machine/cpu.h>
67#ifdef SMP
68#include <machine/smp.h>
69#endif
70
71static int killpg1 __P((struct proc *cp, int signum, int pgid, int all));
72static void setsigvec __P((struct proc *p, int signum, struct sigaction *sa));
73static void stop __P((struct proc *));
74
75static int kern_logsigexit = 1;
76SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, &kern_logsigexit, 0, "");
76SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
77 &kern_logsigexit, 0,
78 "Log processes quitting on abnormal signals to syslog(3)");
77
78/*
79 * Can process p, with pcred pc, send the signal signum to process q?
80 */
81#define CANSIGNAL(p, pc, q, signum) \
82 (PRISON_CHECK(p, q) && ((pc)->pc_ucred->cr_uid == 0 || \
83 (pc)->p_ruid == (q)->p_cred->p_ruid || \
84 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
85 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
86 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
87 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)))
88
89/*
90 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
91 */
92#define CANSIGIO(ruid, uc, q) \
93 ((uc)->cr_uid == 0 || \
94 (ruid) == (q)->p_cred->p_ruid || \
95 (uc)->cr_uid == (q)->p_cred->p_ruid || \
96 (ruid) == (q)->p_ucred->cr_uid || \
97 (uc)->cr_uid == (q)->p_ucred->cr_uid)
98
99int sugid_coredump;
79
80/*
81 * Can process p, with pcred pc, send the signal signum to process q?
82 */
83#define CANSIGNAL(p, pc, q, signum) \
84 (PRISON_CHECK(p, q) && ((pc)->pc_ucred->cr_uid == 0 || \
85 (pc)->p_ruid == (q)->p_cred->p_ruid || \
86 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
87 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
88 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
89 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)))
90
91/*
92 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
93 */
94#define CANSIGIO(ruid, uc, q) \
95 ((uc)->cr_uid == 0 || \
96 (ruid) == (q)->p_cred->p_ruid || \
97 (uc)->cr_uid == (q)->p_cred->p_ruid || \
98 (ruid) == (q)->p_ucred->cr_uid || \
99 (uc)->cr_uid == (q)->p_ucred->cr_uid)
100
101int sugid_coredump;
100SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, &sugid_coredump, 0, "");
102SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
103 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
101
102#ifndef _SYS_SYSPROTO_H_
103struct sigaction_args {
104 int signum;
105 struct sigaction *nsa;
106 struct sigaction *osa;
107};
108#endif
109/* ARGSUSED */
110int
111sigaction(p, uap)
112 struct proc *p;
113 register struct sigaction_args *uap;
114{
115 struct sigaction vec;
116 register struct sigaction *sa;
117 register struct sigacts *ps = p->p_sigacts;
118 register int signum;
119 int bit, error;
120
121 signum = uap->signum;
122 if (signum <= 0 || signum >= NSIG)
123 return (EINVAL);
124 sa = &vec;
125 if (uap->osa) {
126 sa->sa_handler = ps->ps_sigact[signum];
127 sa->sa_mask = ps->ps_catchmask[signum];
128 bit = sigmask(signum);
129 sa->sa_flags = 0;
130 if ((ps->ps_sigonstack & bit) != 0)
131 sa->sa_flags |= SA_ONSTACK;
132 if ((ps->ps_sigintr & bit) == 0)
133 sa->sa_flags |= SA_RESTART;
134 if ((ps->ps_sigreset & bit) != 0)
135 sa->sa_flags |= SA_RESETHAND;
136 if ((ps->ps_signodefer & bit) != 0)
137 sa->sa_flags |= SA_NODEFER;
138 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
139 sa->sa_flags |= SA_NOCLDSTOP;
140 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDWAIT)
141 sa->sa_flags |= SA_NOCLDWAIT;
142 if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
143 sizeof (vec))))
144 return (error);
145 }
146 if (uap->nsa) {
147 if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
148 sizeof (vec))))
149 return (error);
150 if ((signum == SIGKILL || signum == SIGSTOP) &&
151 sa->sa_handler != SIG_DFL)
152 return (EINVAL);
153 setsigvec(p, signum, sa);
154 }
155 return (0);
156}
157
158static void
159setsigvec(p, signum, sa)
160 register struct proc *p;
161 int signum;
162 register struct sigaction *sa;
163{
164 register struct sigacts *ps = p->p_sigacts;
165 register int bit;
166
167 bit = sigmask(signum);
168 /*
169 * Change setting atomically.
170 */
171 (void) splhigh();
172 ps->ps_sigact[signum] = sa->sa_handler;
173 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
174 if ((sa->sa_flags & SA_RESTART) == 0)
175 ps->ps_sigintr |= bit;
176 else
177 ps->ps_sigintr &= ~bit;
178 if (sa->sa_flags & SA_ONSTACK)
179 ps->ps_sigonstack |= bit;
180 else
181 ps->ps_sigonstack &= ~bit;
182 if (sa->sa_flags & SA_RESETHAND)
183 ps->ps_sigreset |= bit;
184 else
185 ps->ps_sigreset &= ~bit;
186 if (sa->sa_flags & SA_NODEFER)
187 ps->ps_signodefer |= bit;
188 else
189 ps->ps_signodefer &= ~bit;
190#ifdef COMPAT_SUNOS
191 if (sa->sa_flags & SA_USERTRAMP)
192 ps->ps_usertramp |= bit;
193 else
194 ps->ps_usertramp &= ~bit;
195#endif
196 if (signum == SIGCHLD) {
197 if (sa->sa_flags & SA_NOCLDSTOP)
198 p->p_procsig->ps_flag |= P_NOCLDSTOP;
199 else
200 p->p_procsig->ps_flag &= ~P_NOCLDSTOP;
201 if (sa->sa_flags & SA_NOCLDWAIT) {
202 /*
203 * Paranoia: since SA_NOCLDWAIT is implemented by
204 * reparenting the dying child to PID 1 (and
205 * trust it to reap the zombie), PID 1 itself is
206 * forbidden to set SA_NOCLDWAIT.
207 */
208 if (p->p_pid == 1)
209 p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
210 else
211 p->p_procsig->ps_flag |= P_NOCLDWAIT;
212 } else
213 p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
214 }
215 /*
216 * Set bit in p_sigignore for signals that are set to SIG_IGN,
217 * and for signals set to SIG_DFL where the default is to ignore.
218 * However, don't put SIGCONT in p_sigignore,
219 * as we have to restart the process.
220 */
221 if (sa->sa_handler == SIG_IGN ||
222 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
223 p->p_siglist &= ~bit; /* never to be seen again */
224 if (signum != SIGCONT)
225 p->p_sigignore |= bit; /* easier in psignal */
226 p->p_sigcatch &= ~bit;
227 } else {
228 p->p_sigignore &= ~bit;
229 if (sa->sa_handler == SIG_DFL)
230 p->p_sigcatch &= ~bit;
231 else
232 p->p_sigcatch |= bit;
233 }
234 (void) spl0();
235}
236
237/*
238 * Initialize signal state for process 0;
239 * set to ignore signals that are ignored by default.
240 */
241void
242siginit(p)
243 struct proc *p;
244{
245 register int i;
246
247 for (i = 0; i < NSIG; i++)
248 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
249 p->p_sigignore |= sigmask(i);
250}
251
252/*
253 * Reset signals for an exec of the specified process.
254 */
255void
256execsigs(p)
257 register struct proc *p;
258{
259 register struct sigacts *ps = p->p_sigacts;
260 register int nc, mask;
261
262 /*
263 * Reset caught signals. Held signals remain held
264 * through p_sigmask (unless they were caught,
265 * and are now ignored by default).
266 */
267 while (p->p_sigcatch) {
268 nc = ffs((long)p->p_sigcatch);
269 mask = sigmask(nc);
270 p->p_sigcatch &= ~mask;
271 if (sigprop[nc] & SA_IGNORE) {
272 if (nc != SIGCONT)
273 p->p_sigignore |= mask;
274 p->p_siglist &= ~mask;
275 }
276 ps->ps_sigact[nc] = SIG_DFL;
277 }
278 /*
279 * Reset stack state to the user stack.
280 * Clear set of signals caught on the signal stack.
281 */
282 ps->ps_sigstk.ss_flags = SS_DISABLE;
283 ps->ps_sigstk.ss_size = 0;
284 ps->ps_sigstk.ss_sp = 0;
285 ps->ps_flags = 0;
286}
287
288/*
289 * Manipulate signal mask.
290 * Note that we receive new mask, not pointer,
291 * and return old mask as return value;
292 * the library stub does the rest.
293 */
294#ifndef _SYS_SYSPROTO_H_
295struct sigprocmask_args {
296 int how;
297 sigset_t mask;
298};
299#endif
300int
301sigprocmask(p, uap)
302 register struct proc *p;
303 struct sigprocmask_args *uap;
304{
305 int error = 0;
306
307 p->p_retval[0] = p->p_sigmask;
308 (void) splhigh();
309
310 switch (uap->how) {
311 case SIG_BLOCK:
312 p->p_sigmask |= uap->mask &~ sigcantmask;
313 break;
314
315 case SIG_UNBLOCK:
316 p->p_sigmask &= ~uap->mask;
317 break;
318
319 case SIG_SETMASK:
320 p->p_sigmask = uap->mask &~ sigcantmask;
321 break;
322
323 default:
324 error = EINVAL;
325 break;
326 }
327 (void) spl0();
328 return (error);
329}
330
331#ifndef _SYS_SYSPROTO_H_
332struct sigpending_args {
333 int dummy;
334};
335#endif
336/* ARGSUSED */
337int
338sigpending(p, uap)
339 struct proc *p;
340 struct sigpending_args *uap;
341{
342
343 p->p_retval[0] = p->p_siglist;
344 return (0);
345}
346
347#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
348/*
349 * Generalized interface signal handler, 4.3-compatible.
350 */
351#ifndef _SYS_SYSPROTO_H_
352struct osigvec_args {
353 int signum;
354 struct sigvec *nsv;
355 struct sigvec *osv;
356};
357#endif
358/* ARGSUSED */
359int
360osigvec(p, uap)
361 struct proc *p;
362 register struct osigvec_args *uap;
363{
364 struct sigvec vec;
365 register struct sigacts *ps = p->p_sigacts;
366 register struct sigvec *sv;
367 register int signum;
368 int bit, error;
369
370 signum = uap->signum;
371 if (signum <= 0 || signum >= NSIG)
372 return (EINVAL);
373 sv = &vec;
374 if (uap->osv) {
375 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
376 sv->sv_mask = ps->ps_catchmask[signum];
377 bit = sigmask(signum);
378 sv->sv_flags = 0;
379 if ((ps->ps_sigonstack & bit) != 0)
380 sv->sv_flags |= SV_ONSTACK;
381 if ((ps->ps_sigintr & bit) != 0)
382 sv->sv_flags |= SV_INTERRUPT;
383 if ((ps->ps_sigreset & bit) != 0)
384 sv->sv_flags |= SV_RESETHAND;
385 if ((ps->ps_signodefer & bit) != 0)
386 sv->sv_flags |= SV_NODEFER;
387#ifndef COMPAT_SUNOS
388 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
389 sv->sv_flags |= SV_NOCLDSTOP;
390#endif
391 if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
392 sizeof (vec))))
393 return (error);
394 }
395 if (uap->nsv) {
396 if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
397 sizeof (vec))))
398 return (error);
399 if ((signum == SIGKILL || signum == SIGSTOP) &&
400 sv->sv_handler != SIG_DFL)
401 return (EINVAL);
402#ifdef COMPAT_SUNOS
403 sv->sv_flags |= SA_USERTRAMP;
404#endif
405 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
406 setsigvec(p, signum, (struct sigaction *)sv);
407 }
408 return (0);
409}
410
411#ifndef _SYS_SYSPROTO_H_
412struct osigblock_args {
413 int mask;
414};
415#endif
416int
417osigblock(p, uap)
418 register struct proc *p;
419 struct osigblock_args *uap;
420{
421
422 (void) splhigh();
423 p->p_retval[0] = p->p_sigmask;
424 p->p_sigmask |= uap->mask &~ sigcantmask;
425 (void) spl0();
426 return (0);
427}
428
429#ifndef _SYS_SYSPROTO_H_
430struct osigsetmask_args {
431 int mask;
432};
433#endif
434int
435osigsetmask(p, uap)
436 struct proc *p;
437 struct osigsetmask_args *uap;
438{
439
440 (void) splhigh();
441 p->p_retval[0] = p->p_sigmask;
442 p->p_sigmask = uap->mask &~ sigcantmask;
443 (void) spl0();
444 return (0);
445}
446#endif /* COMPAT_43 || COMPAT_SUNOS */
447
448/*
449 * Suspend process until signal, providing mask to be set
450 * in the meantime. Note nonstandard calling convention:
451 * libc stub passes mask, not pointer, to save a copyin.
452 */
453#ifndef _SYS_SYSPROTO_H_
454struct sigsuspend_args {
455 sigset_t mask;
456};
457#endif
458/* ARGSUSED */
459int
460sigsuspend(p, uap)
461 register struct proc *p;
462 struct sigsuspend_args *uap;
463{
464 register struct sigacts *ps = p->p_sigacts;
465
466 /*
467 * When returning from sigpause, we want
468 * the old mask to be restored after the
469 * signal handler has finished. Thus, we
470 * save it here and mark the sigacts structure
471 * to indicate this.
472 */
473 p->p_oldsigmask = p->p_sigmask;
474 p->p_sigmask = uap->mask &~ sigcantmask;
475 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
476 /* void */;
477 /* always return EINTR rather than ERESTART... */
478 return (EINTR);
479}
480
481#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
482#ifndef _SYS_SYSPROTO_H_
483struct osigstack_args {
484 struct sigstack *nss;
485 struct sigstack *oss;
486};
487#endif
488/* ARGSUSED */
489int
490osigstack(p, uap)
491 struct proc *p;
492 register struct osigstack_args *uap;
493{
494 struct sigstack ss;
495 struct sigacts *psp;
496 int error = 0;
497
498 psp = p->p_sigacts;
499 ss.ss_sp = psp->ps_sigstk.ss_sp;
500 ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
501 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
502 sizeof (struct sigstack))))
503 return (error);
504 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
505 sizeof (ss))) == 0) {
506 psp->ps_sigstk.ss_sp = ss.ss_sp;
507 psp->ps_sigstk.ss_size = 0;
508 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
509 psp->ps_flags |= SAS_ALTSTACK;
510 }
511 return (error);
512}
513#endif /* COMPAT_43 || COMPAT_SUNOS */
514
515#ifndef _SYS_SYSPROTO_H_
516struct sigaltstack_args {
517 struct sigaltstack *nss;
518 struct sigaltstack *oss;
519};
520#endif
521/* ARGSUSED */
522int
523sigaltstack(p, uap)
524 struct proc *p;
525 register struct sigaltstack_args *uap;
526{
527 struct sigacts *psp;
528 struct sigaltstack ss;
529 int error;
530
531 psp = p->p_sigacts;
532 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
533 psp->ps_sigstk.ss_flags |= SS_DISABLE;
534 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
535 (caddr_t)uap->oss, sizeof (struct sigaltstack))))
536 return (error);
537 if (uap->nss == 0)
538 return (0);
539 if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
540 return (error);
541 if (ss.ss_flags & SS_DISABLE) {
542 if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
543 return (EINVAL);
544 psp->ps_flags &= ~SAS_ALTSTACK;
545 psp->ps_sigstk.ss_flags = ss.ss_flags;
546 return (0);
547 }
548 if (ss.ss_size < MINSIGSTKSZ)
549 return (ENOMEM);
550 psp->ps_flags |= SAS_ALTSTACK;
551 psp->ps_sigstk= ss;
552 return (0);
553}
554
555/*
556 * Common code for kill process group/broadcast kill.
557 * cp is calling process.
558 */
559int
560killpg1(cp, signum, pgid, all)
561 register struct proc *cp;
562 int signum, pgid, all;
563{
564 register struct proc *p;
565 register struct pcred *pc = cp->p_cred;
566 struct pgrp *pgrp;
567 int nfound = 0;
568
569 if (all)
570 /*
571 * broadcast
572 */
573 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
574 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
575 p == cp || !CANSIGNAL(cp, pc, p, signum))
576 continue;
577 nfound++;
578 if (signum)
579 psignal(p, signum);
580 }
581 else {
582 if (pgid == 0)
583 /*
584 * zero pgid means send to my process group.
585 */
586 pgrp = cp->p_pgrp;
587 else {
588 pgrp = pgfind(pgid);
589 if (pgrp == NULL)
590 return (ESRCH);
591 }
592 for (p = pgrp->pg_members.lh_first; p != 0;
593 p = p->p_pglist.le_next) {
594 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
595 p->p_stat == SZOMB ||
596 !CANSIGNAL(cp, pc, p, signum))
597 continue;
598 nfound++;
599 if (signum)
600 psignal(p, signum);
601 }
602 }
603 return (nfound ? 0 : ESRCH);
604}
605
606#ifndef _SYS_SYSPROTO_H_
607struct kill_args {
608 int pid;
609 int signum;
610};
611#endif
612/* ARGSUSED */
613int
614kill(cp, uap)
615 register struct proc *cp;
616 register struct kill_args *uap;
617{
618 register struct proc *p;
619 register struct pcred *pc = cp->p_cred;
620
621 if ((u_int)uap->signum >= NSIG)
622 return (EINVAL);
623 if (uap->pid > 0) {
624 /* kill single process */
625 if ((p = pfind(uap->pid)) == NULL)
626 return (ESRCH);
627 if (!CANSIGNAL(cp, pc, p, uap->signum))
628 return (EPERM);
629 if (uap->signum)
630 psignal(p, uap->signum);
631 return (0);
632 }
633 switch (uap->pid) {
634 case -1: /* broadcast signal */
635 return (killpg1(cp, uap->signum, 0, 1));
636 case 0: /* signal own process group */
637 return (killpg1(cp, uap->signum, 0, 0));
638 default: /* negative explicit process group */
639 return (killpg1(cp, uap->signum, -uap->pid, 0));
640 }
641 /* NOTREACHED */
642}
643
644#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
645#ifndef _SYS_SYSPROTO_H_
646struct okillpg_args {
647 int pgid;
648 int signum;
649};
650#endif
651/* ARGSUSED */
652int
653okillpg(p, uap)
654 struct proc *p;
655 register struct okillpg_args *uap;
656{
657
658 if ((u_int)uap->signum >= NSIG)
659 return (EINVAL);
660 return (killpg1(p, uap->signum, uap->pgid, 0));
661}
662#endif /* COMPAT_43 || COMPAT_SUNOS */
663
664/*
665 * Send a signal to a process group.
666 */
667void
668gsignal(pgid, signum)
669 int pgid, signum;
670{
671 struct pgrp *pgrp;
672
673 if (pgid && (pgrp = pgfind(pgid)))
674 pgsignal(pgrp, signum, 0);
675}
676
677/*
678 * Send a signal to a process group. If checktty is 1,
679 * limit to members which have a controlling terminal.
680 */
681void
682pgsignal(pgrp, signum, checkctty)
683 struct pgrp *pgrp;
684 int signum, checkctty;
685{
686 register struct proc *p;
687
688 if (pgrp)
689 for (p = pgrp->pg_members.lh_first; p != 0;
690 p = p->p_pglist.le_next)
691 if (checkctty == 0 || p->p_flag & P_CONTROLT)
692 psignal(p, signum);
693}
694
695/*
696 * Send a signal caused by a trap to the current process.
697 * If it will be caught immediately, deliver it with correct code.
698 * Otherwise, post it normally.
699 */
700void
701trapsignal(p, signum, code)
702 struct proc *p;
703 register int signum;
704 u_long code;
705{
706 register struct sigacts *ps = p->p_sigacts;
707 int mask;
708
709 mask = sigmask(signum);
710 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
711 (p->p_sigmask & mask) == 0) {
712 p->p_stats->p_ru.ru_nsignals++;
713#ifdef KTRACE
714 if (KTRPOINT(p, KTR_PSIG))
715 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
716 p->p_sigmask, code);
717#endif
718 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum,
719 p->p_sigmask, code);
720 p->p_sigmask |= ps->ps_catchmask[signum] |
721 (mask & ~ps->ps_signodefer);
722 if ((ps->ps_sigreset & mask) != 0) {
723 /*
724 * See setsigvec() for origin of this code.
725 */
726 p->p_sigcatch &= ~mask;
727 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
728 p->p_sigignore |= mask;
729 ps->ps_sigact[signum] = SIG_DFL;
730 }
731 } else {
732 p->p_code = code; /* XXX for core dump/debugger */
733 p->p_sig = signum; /* XXX to verify code */
734 psignal(p, signum);
735 }
736}
737
738/*
739 * Send the signal to the process. If the signal has an action, the action
740 * is usually performed by the target process rather than the caller; we add
741 * the signal to the set of pending signals for the process.
742 *
743 * Exceptions:
744 * o When a stop signal is sent to a sleeping process that takes the
745 * default action, the process is stopped without awakening it.
746 * o SIGCONT restarts stopped processes (or puts them back to sleep)
747 * regardless of the signal action (eg, blocked or ignored).
748 *
749 * Other ignored signals are discarded immediately.
750 */
751void
752psignal(p, signum)
753 register struct proc *p;
754 register int signum;
755{
756 register int s, prop;
757 register sig_t action;
758 int mask;
759
760 if ((u_int)signum >= NSIG || signum == 0) {
761 printf("psignal: signum %d\n", signum);
762 panic("psignal signal number");
763 }
764 mask = sigmask(signum);
765 prop = sigprop[signum];
766
767 /*
768 * If proc is traced, always give parent a chance;
769 * if signal event is tracked by procfs, give *that*
770 * a chance, as well.
771 */
772 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
773 action = SIG_DFL;
774 else {
775 /*
776 * If the signal is being ignored,
777 * then we forget about it immediately.
778 * (Note: we don't set SIGCONT in p_sigignore,
779 * and if it is set to SIG_IGN,
780 * action will be SIG_DFL here.)
781 */
782 if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT))
783 return;
784 if (p->p_sigmask & mask)
785 action = SIG_HOLD;
786 else if (p->p_sigcatch & mask)
787 action = SIG_CATCH;
788 else
789 action = SIG_DFL;
790 }
791
792 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
793 (p->p_flag & P_TRACED) == 0)
794 p->p_nice = NZERO;
795
796 if (prop & SA_CONT)
797 p->p_siglist &= ~stopsigmask;
798
799 if (prop & SA_STOP) {
800 /*
801 * If sending a tty stop signal to a member of an orphaned
802 * process group, discard the signal here if the action
803 * is default; don't stop the process below if sleeping,
804 * and don't clear any pending SIGCONT.
805 */
806 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
807 action == SIG_DFL)
808 return;
809 p->p_siglist &= ~contsigmask;
810 }
811 p->p_siglist |= mask;
812
813 /*
814 * Defer further processing for signals which are held,
815 * except that stopped processes must be continued by SIGCONT.
816 */
817 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
818 return;
819 s = splhigh();
820 switch (p->p_stat) {
821
822 case SSLEEP:
823 /*
824 * If process is sleeping uninterruptibly
825 * we can't interrupt the sleep... the signal will
826 * be noticed when the process returns through
827 * trap() or syscall().
828 */
829 if ((p->p_flag & P_SINTR) == 0)
830 goto out;
831 /*
832 * Process is sleeping and traced... make it runnable
833 * so it can discover the signal in issignal() and stop
834 * for the parent.
835 */
836 if (p->p_flag & P_TRACED)
837 goto run;
838 /*
839 * If SIGCONT is default (or ignored) and process is
840 * asleep, we are finished; the process should not
841 * be awakened.
842 */
843 if ((prop & SA_CONT) && action == SIG_DFL) {
844 p->p_siglist &= ~mask;
845 goto out;
846 }
847 /*
848 * When a sleeping process receives a stop
849 * signal, process immediately if possible.
850 * All other (caught or default) signals
851 * cause the process to run.
852 */
853 if (prop & SA_STOP) {
854 if (action != SIG_DFL)
855 goto runfast;
856 /*
857 * If a child holding parent blocked,
858 * stopping could cause deadlock.
859 */
860 if (p->p_flag & P_PPWAIT)
861 goto out;
862 p->p_siglist &= ~mask;
863 p->p_xstat = signum;
864 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
865 psignal(p->p_pptr, SIGCHLD);
866 stop(p);
867 goto out;
868 } else
869 goto runfast;
870 /*NOTREACHED*/
871
872 case SSTOP:
873 /*
874 * If traced process is already stopped,
875 * then no further action is necessary.
876 */
877 if (p->p_flag & P_TRACED)
878 goto out;
879
880 /*
881 * Kill signal always sets processes running.
882 */
883 if (signum == SIGKILL)
884 goto runfast;
885
886 if (prop & SA_CONT) {
887 /*
888 * If SIGCONT is default (or ignored), we continue the
889 * process but don't leave the signal in p_siglist, as
890 * it has no further action. If SIGCONT is held, we
891 * continue the process and leave the signal in
892 * p_siglist. If the process catches SIGCONT, let it
893 * handle the signal itself. If it isn't waiting on
894 * an event, then it goes back to run state.
895 * Otherwise, process goes back to sleep state.
896 */
897 if (action == SIG_DFL)
898 p->p_siglist &= ~mask;
899 if (action == SIG_CATCH)
900 goto runfast;
901 if (p->p_wchan == 0)
902 goto run;
903 p->p_stat = SSLEEP;
904 goto out;
905 }
906
907 if (prop & SA_STOP) {
908 /*
909 * Already stopped, don't need to stop again.
910 * (If we did the shell could get confused.)
911 */
912 p->p_siglist &= ~mask; /* take it away */
913 goto out;
914 }
915
916 /*
917 * If process is sleeping interruptibly, then simulate a
918 * wakeup so that when it is continued, it will be made
919 * runnable and can look at the signal. But don't make
920 * the process runnable, leave it stopped.
921 */
922 if (p->p_wchan && p->p_flag & P_SINTR)
923 unsleep(p);
924 goto out;
925
926 default:
927 /*
928 * SRUN, SIDL, SZOMB do nothing with the signal,
929 * other than kicking ourselves if we are running.
930 * It will either never be noticed, or noticed very soon.
931 */
932 if (p == curproc)
933 signotify(p);
934#ifdef SMP
935 else if (p->p_stat == SRUN)
936 forward_signal(p);
937#endif
938 goto out;
939 }
940 /*NOTREACHED*/
941
942runfast:
943 /*
944 * Raise priority to at least PUSER.
945 */
946 if (p->p_priority > PUSER)
947 p->p_priority = PUSER;
948run:
949 setrunnable(p);
950out:
951 splx(s);
952}
953
954/*
955 * If the current process has received a signal (should be caught or cause
956 * termination, should interrupt current syscall), return the signal number.
957 * Stop signals with default action are processed immediately, then cleared;
958 * they aren't returned. This is checked after each entry to the system for
959 * a syscall or trap (though this can usually be done without calling issignal
960 * by checking the pending signal masks in the CURSIG macro.) The normal call
961 * sequence is
962 *
963 * while (signum = CURSIG(curproc))
964 * postsig(signum);
965 */
966int
967issignal(p)
968 register struct proc *p;
969{
970 register int signum, mask, prop;
971
972 for (;;) {
973 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
974
975 mask = p->p_siglist & ~p->p_sigmask;
976 if (p->p_flag & P_PPWAIT)
977 mask &= ~stopsigmask;
978 if (mask == 0) /* no signal to send */
979 return (0);
980 signum = ffs((long)mask);
981 mask = sigmask(signum);
982 prop = sigprop[signum];
983
984 STOPEVENT(p, S_SIG, signum);
985
986 /*
987 * We should see pending but ignored signals
988 * only if P_TRACED was on when they were posted.
989 */
990 if ((mask & p->p_sigignore) && (traced == 0)) {
991 p->p_siglist &= ~mask;
992 continue;
993 }
994 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
995 /*
996 * If traced, always stop, and stay
997 * stopped until released by the parent.
998 */
999 p->p_xstat = signum;
1000 psignal(p->p_pptr, SIGCHLD);
1001 do {
1002 stop(p);
1003 mi_switch();
1004 } while (!trace_req(p)
1005 && p->p_flag & P_TRACED);
1006
1007 /*
1008 * If the traced bit got turned off, go back up
1009 * to the top to rescan signals. This ensures
1010 * that p_sig* and ps_sigact are consistent.
1011 */
1012 if ((p->p_flag & P_TRACED) == 0)
1013 continue;
1014
1015 /*
1016 * If parent wants us to take the signal,
1017 * then it will leave it in p->p_xstat;
1018 * otherwise we just look for signals again.
1019 */
1020 p->p_siglist &= ~mask; /* clear the old signal */
1021 signum = p->p_xstat;
1022 if (signum == 0)
1023 continue;
1024
1025 /*
1026 * Put the new signal into p_siglist. If the
1027 * signal is being masked, look for other signals.
1028 */
1029 mask = sigmask(signum);
1030 p->p_siglist |= mask;
1031 if (p->p_sigmask & mask)
1032 continue;
1033 }
1034
1035 /*
1036 * Decide whether the signal should be returned.
1037 * Return the signal's number, or fall through
1038 * to clear it from the pending mask.
1039 */
1040 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) {
1041
1042 case (int)SIG_DFL:
1043 /*
1044 * Don't take default actions on system processes.
1045 */
1046 if (p->p_pid <= 1) {
1047#ifdef DIAGNOSTIC
1048 /*
1049 * Are you sure you want to ignore SIGSEGV
1050 * in init? XXX
1051 */
1052 printf("Process (pid %lu) got signal %d\n",
1053 (u_long)p->p_pid, signum);
1054#endif
1055 break; /* == ignore */
1056 }
1057 /*
1058 * If there is a pending stop signal to process
1059 * with default action, stop here,
1060 * then clear the signal. However,
1061 * if process is member of an orphaned
1062 * process group, ignore tty stop signals.
1063 */
1064 if (prop & SA_STOP) {
1065 if (p->p_flag & P_TRACED ||
1066 (p->p_pgrp->pg_jobc == 0 &&
1067 prop & SA_TTYSTOP))
1068 break; /* == ignore */
1069 p->p_xstat = signum;
1070 stop(p);
1071 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
1072 psignal(p->p_pptr, SIGCHLD);
1073 mi_switch();
1074 break;
1075 } else if (prop & SA_IGNORE) {
1076 /*
1077 * Except for SIGCONT, shouldn't get here.
1078 * Default action is to ignore; drop it.
1079 */
1080 break; /* == ignore */
1081 } else
1082 return (signum);
1083 /*NOTREACHED*/
1084
1085 case (int)SIG_IGN:
1086 /*
1087 * Masking above should prevent us ever trying
1088 * to take action on an ignored signal other
1089 * than SIGCONT, unless process is traced.
1090 */
1091 if ((prop & SA_CONT) == 0 &&
1092 (p->p_flag & P_TRACED) == 0)
1093 printf("issignal\n");
1094 break; /* == ignore */
1095
1096 default:
1097 /*
1098 * This signal has an action, let
1099 * postsig() process it.
1100 */
1101 return (signum);
1102 }
1103 p->p_siglist &= ~mask; /* take the signal! */
1104 }
1105 /* NOTREACHED */
1106}
1107
1108/*
1109 * Put the argument process into the stopped state and notify the parent
1110 * via wakeup. Signals are handled elsewhere. The process must not be
1111 * on the run queue.
1112 */
1113void
1114stop(p)
1115 register struct proc *p;
1116{
1117
1118 p->p_stat = SSTOP;
1119 p->p_flag &= ~P_WAITED;
1120 wakeup((caddr_t)p->p_pptr);
1121}
1122
1123/*
1124 * Take the action for the specified signal
1125 * from the current set of pending signals.
1126 */
1127void
1128postsig(signum)
1129 register int signum;
1130{
1131 register struct proc *p = curproc;
1132 register struct sigacts *ps = p->p_sigacts;
1133 register sig_t action;
1134 int code, mask, returnmask;
1135
1136 KASSERT(signum != 0, ("postsig"));
1137
1138 mask = sigmask(signum);
1139 p->p_siglist &= ~mask;
1140 action = ps->ps_sigact[signum];
1141#ifdef KTRACE
1142 if (KTRPOINT(p, KTR_PSIG))
1143 ktrpsig(p->p_tracep,
1144 signum, action, p->p_oldsigmask ?
1145 p->p_oldsigmask : p->p_sigmask, 0);
1146#endif
1147 STOPEVENT(p, S_SIG, signum);
1148
1149 if (action == SIG_DFL) {
1150 /*
1151 * Default action, where the default is to kill
1152 * the process. (Other cases were ignored above.)
1153 */
1154 sigexit(p, signum);
1155 /* NOTREACHED */
1156 } else {
1157 /*
1158 * If we get here, the signal must be caught.
1159 */
1160 KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0,
1161 ("postsig action"));
1162 /*
1163 * Set the new mask value and also defer further
1164 * occurences of this signal.
1165 *
1166 * Special case: user has done a sigpause. Here the
1167 * current mask is not of interest, but rather the
1168 * mask from before the sigpause is what we want
1169 * restored after the signal processing is completed.
1170 */
1171 (void) splhigh();
1172 if (p->p_oldsigmask) {
1173 returnmask = p->p_oldsigmask;
1174 p->p_oldsigmask = 0;
1175 } else
1176 returnmask = p->p_sigmask;
1177 p->p_sigmask |= ps->ps_catchmask[signum] |
1178 (mask & ~ps->ps_signodefer);
1179 if ((ps->ps_sigreset & mask) != 0) {
1180 /*
1181 * See setsigvec() for origin of this code.
1182 */
1183 p->p_sigcatch &= ~mask;
1184 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1185 p->p_sigignore |= mask;
1186 ps->ps_sigact[signum] = SIG_DFL;
1187 }
1188 (void) spl0();
1189 p->p_stats->p_ru.ru_nsignals++;
1190 if (p->p_sig != signum) {
1191 code = 0;
1192 } else {
1193 code = p->p_code;
1194 p->p_code = 0;
1195 p->p_sig = 0;
1196 }
1197 (*p->p_sysent->sv_sendsig)(action, signum, returnmask, code);
1198 }
1199}
1200
1201/*
1202 * Kill the current process for stated reason.
1203 */
1204void
1205killproc(p, why)
1206 struct proc *p;
1207 char *why;
1208{
1209 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1210 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1211 psignal(p, SIGKILL);
1212}
1213
1214/*
1215 * Force the current process to exit with the specified signal, dumping core
1216 * if appropriate. We bypass the normal tests for masked and caught signals,
1217 * allowing unrecoverable failures to terminate the process without changing
1218 * signal state. Mark the accounting record with the signal termination.
1219 * If dumping core, save the signal number for the debugger. Calls exit and
1220 * does not return.
1221 */
1222void
1223sigexit(p, signum)
1224 register struct proc *p;
1225 int signum;
1226{
1227
1228 p->p_acflag |= AXSIG;
1229 if (sigprop[signum] & SA_CORE) {
1230 p->p_sig = signum;
1231 /*
1232 * Log signals which would cause core dumps
1233 * (Log as LOG_INFO to appease those who don't want
1234 * these messages.)
1235 * XXX : Todo, as well as euid, write out ruid too
1236 */
1237 if (p->p_sysent->sv_coredump != NULL &&
1238 (*p->p_sysent->sv_coredump)(p) == 0)
1239 signum |= WCOREFLAG;
1240 if (kern_logsigexit)
1241 log(LOG_INFO,
1242 "pid %d (%s), uid %d: exited on signal %d%s\n",
1243 p->p_pid, p->p_comm,
1244 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1245 signum &~ WCOREFLAG,
1246 signum & WCOREFLAG ? " (core dumped)" : "");
1247 }
1248 exit1(p, W_EXITCODE(0, signum));
1249 /* NOTREACHED */
1250}
1251
1252static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1253SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1254 sizeof(corefilename), "process corefile name format string");
1255
1256/*
1257 * expand_name(name, uid, pid)
1258 * Expand the name described in corefilename, using name, uid, and pid.
1259 * corefilename is a printf-like string, with three format specifiers:
1260 * %N name of process ("name")
1261 * %P process id (pid)
1262 * %U user id (uid)
1263 * For example, "%N.core" is the default; they can be disabled completely
1264 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1265 * This is controlled by the sysctl variable kern.corefile (see above).
1266 */
1267
1268char *
1269expand_name(name, uid, pid)
1270const char *name; int uid; int pid; {
1271 char *temp;
1272 char buf[11]; /* Buffer for pid/uid -- max 4B */
1273 int i, n;
1274 char *format = corefilename;
1275
1276 temp = malloc(MAXPATHLEN + 3, M_TEMP, M_NOWAIT);
1277 if (temp == NULL)
1278 return NULL;
1279 bzero(temp, MAXPATHLEN+3);
1280 for (i = 0, n = 0; i < MAXPATHLEN && format[i]; i++) {
1281 int l;
1282 switch (format[i]) {
1283 case '%': /* Format character */
1284 i++;
1285 switch (format[i]) {
1286 case '%':
1287 temp[n++] = '%';
1288 break;
1289 case 'N': /* process name */
1290 l = strlen(name);
1291 if ((n + l) > MAXPATHLEN) {
1292 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1293 pid, name, uid, temp, name);
1294 free(temp, M_TEMP);
1295 return NULL;
1296 }
1297 memcpy(temp+n, name, l);
1298 n += l;
1299 break;
1300 case 'P': /* process id */
1301 sprintf(buf, "%u", pid);
1302 l = strlen(buf);
1303 if ((n + l) > MAXPATHLEN) {
1304 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1305 pid, name, uid, temp, name);
1306 free(temp, M_TEMP);
1307 return NULL;
1308 }
1309 memcpy(temp+n, buf, l);
1310 n += l;
1311 break;
1312 case 'U': /* user id */
1313 sprintf(buf, "%u", uid);
1314 l = strlen(buf);
1315 if ((n + l) > MAXPATHLEN) {
1316 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1317 pid, name, uid, temp, name);
1318 free(temp, M_TEMP);
1319 return NULL;
1320 }
1321 memcpy(temp+n, buf, l);
1322 n += l;
1323 break;
1324 default:
1325 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1326 }
1327 break;
1328 default:
1329 temp[n++] = format[i];
1330 }
1331 }
1332 return temp;
1333}
1334
1335/*
1336 * Nonexistent system call-- signal process (may want to handle it).
1337 * Flag error in case process won't see signal immediately (blocked or ignored).
1338 */
1339#ifndef _SYS_SYSPROTO_H_
1340struct nosys_args {
1341 int dummy;
1342};
1343#endif
1344/* ARGSUSED */
1345int
1346nosys(p, args)
1347 struct proc *p;
1348 struct nosys_args *args;
1349{
1350
1351 psignal(p, SIGSYS);
1352 return (EINVAL);
1353}
1354
1355/*
1356 * Send a signal to a SIGIO or SIGURG to a process or process group using
1357 * stored credentials rather than those of the current process.
1358 */
1359void
1360pgsigio(sigio, signum, checkctty)
1361 struct sigio *sigio;
1362 int signum, checkctty;
1363{
1364 if (sigio == NULL)
1365 return;
1366
1367 if (sigio->sio_pgid > 0) {
1368 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1369 sigio->sio_proc))
1370 psignal(sigio->sio_proc, signum);
1371 } else if (sigio->sio_pgid < 0) {
1372 struct proc *p;
1373
1374 for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL;
1375 p = p->p_pglist.le_next)
1376 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1377 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1378 psignal(p, signum);
1379 }
1380}
104
105#ifndef _SYS_SYSPROTO_H_
106struct sigaction_args {
107 int signum;
108 struct sigaction *nsa;
109 struct sigaction *osa;
110};
111#endif
112/* ARGSUSED */
113int
114sigaction(p, uap)
115 struct proc *p;
116 register struct sigaction_args *uap;
117{
118 struct sigaction vec;
119 register struct sigaction *sa;
120 register struct sigacts *ps = p->p_sigacts;
121 register int signum;
122 int bit, error;
123
124 signum = uap->signum;
125 if (signum <= 0 || signum >= NSIG)
126 return (EINVAL);
127 sa = &vec;
128 if (uap->osa) {
129 sa->sa_handler = ps->ps_sigact[signum];
130 sa->sa_mask = ps->ps_catchmask[signum];
131 bit = sigmask(signum);
132 sa->sa_flags = 0;
133 if ((ps->ps_sigonstack & bit) != 0)
134 sa->sa_flags |= SA_ONSTACK;
135 if ((ps->ps_sigintr & bit) == 0)
136 sa->sa_flags |= SA_RESTART;
137 if ((ps->ps_sigreset & bit) != 0)
138 sa->sa_flags |= SA_RESETHAND;
139 if ((ps->ps_signodefer & bit) != 0)
140 sa->sa_flags |= SA_NODEFER;
141 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
142 sa->sa_flags |= SA_NOCLDSTOP;
143 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDWAIT)
144 sa->sa_flags |= SA_NOCLDWAIT;
145 if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
146 sizeof (vec))))
147 return (error);
148 }
149 if (uap->nsa) {
150 if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
151 sizeof (vec))))
152 return (error);
153 if ((signum == SIGKILL || signum == SIGSTOP) &&
154 sa->sa_handler != SIG_DFL)
155 return (EINVAL);
156 setsigvec(p, signum, sa);
157 }
158 return (0);
159}
160
161static void
162setsigvec(p, signum, sa)
163 register struct proc *p;
164 int signum;
165 register struct sigaction *sa;
166{
167 register struct sigacts *ps = p->p_sigacts;
168 register int bit;
169
170 bit = sigmask(signum);
171 /*
172 * Change setting atomically.
173 */
174 (void) splhigh();
175 ps->ps_sigact[signum] = sa->sa_handler;
176 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
177 if ((sa->sa_flags & SA_RESTART) == 0)
178 ps->ps_sigintr |= bit;
179 else
180 ps->ps_sigintr &= ~bit;
181 if (sa->sa_flags & SA_ONSTACK)
182 ps->ps_sigonstack |= bit;
183 else
184 ps->ps_sigonstack &= ~bit;
185 if (sa->sa_flags & SA_RESETHAND)
186 ps->ps_sigreset |= bit;
187 else
188 ps->ps_sigreset &= ~bit;
189 if (sa->sa_flags & SA_NODEFER)
190 ps->ps_signodefer |= bit;
191 else
192 ps->ps_signodefer &= ~bit;
193#ifdef COMPAT_SUNOS
194 if (sa->sa_flags & SA_USERTRAMP)
195 ps->ps_usertramp |= bit;
196 else
197 ps->ps_usertramp &= ~bit;
198#endif
199 if (signum == SIGCHLD) {
200 if (sa->sa_flags & SA_NOCLDSTOP)
201 p->p_procsig->ps_flag |= P_NOCLDSTOP;
202 else
203 p->p_procsig->ps_flag &= ~P_NOCLDSTOP;
204 if (sa->sa_flags & SA_NOCLDWAIT) {
205 /*
206 * Paranoia: since SA_NOCLDWAIT is implemented by
207 * reparenting the dying child to PID 1 (and
208 * trust it to reap the zombie), PID 1 itself is
209 * forbidden to set SA_NOCLDWAIT.
210 */
211 if (p->p_pid == 1)
212 p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
213 else
214 p->p_procsig->ps_flag |= P_NOCLDWAIT;
215 } else
216 p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
217 }
218 /*
219 * Set bit in p_sigignore for signals that are set to SIG_IGN,
220 * and for signals set to SIG_DFL where the default is to ignore.
221 * However, don't put SIGCONT in p_sigignore,
222 * as we have to restart the process.
223 */
224 if (sa->sa_handler == SIG_IGN ||
225 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
226 p->p_siglist &= ~bit; /* never to be seen again */
227 if (signum != SIGCONT)
228 p->p_sigignore |= bit; /* easier in psignal */
229 p->p_sigcatch &= ~bit;
230 } else {
231 p->p_sigignore &= ~bit;
232 if (sa->sa_handler == SIG_DFL)
233 p->p_sigcatch &= ~bit;
234 else
235 p->p_sigcatch |= bit;
236 }
237 (void) spl0();
238}
239
240/*
241 * Initialize signal state for process 0;
242 * set to ignore signals that are ignored by default.
243 */
244void
245siginit(p)
246 struct proc *p;
247{
248 register int i;
249
250 for (i = 0; i < NSIG; i++)
251 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
252 p->p_sigignore |= sigmask(i);
253}
254
255/*
256 * Reset signals for an exec of the specified process.
257 */
258void
259execsigs(p)
260 register struct proc *p;
261{
262 register struct sigacts *ps = p->p_sigacts;
263 register int nc, mask;
264
265 /*
266 * Reset caught signals. Held signals remain held
267 * through p_sigmask (unless they were caught,
268 * and are now ignored by default).
269 */
270 while (p->p_sigcatch) {
271 nc = ffs((long)p->p_sigcatch);
272 mask = sigmask(nc);
273 p->p_sigcatch &= ~mask;
274 if (sigprop[nc] & SA_IGNORE) {
275 if (nc != SIGCONT)
276 p->p_sigignore |= mask;
277 p->p_siglist &= ~mask;
278 }
279 ps->ps_sigact[nc] = SIG_DFL;
280 }
281 /*
282 * Reset stack state to the user stack.
283 * Clear set of signals caught on the signal stack.
284 */
285 ps->ps_sigstk.ss_flags = SS_DISABLE;
286 ps->ps_sigstk.ss_size = 0;
287 ps->ps_sigstk.ss_sp = 0;
288 ps->ps_flags = 0;
289}
290
291/*
292 * Manipulate signal mask.
293 * Note that we receive new mask, not pointer,
294 * and return old mask as return value;
295 * the library stub does the rest.
296 */
297#ifndef _SYS_SYSPROTO_H_
298struct sigprocmask_args {
299 int how;
300 sigset_t mask;
301};
302#endif
303int
304sigprocmask(p, uap)
305 register struct proc *p;
306 struct sigprocmask_args *uap;
307{
308 int error = 0;
309
310 p->p_retval[0] = p->p_sigmask;
311 (void) splhigh();
312
313 switch (uap->how) {
314 case SIG_BLOCK:
315 p->p_sigmask |= uap->mask &~ sigcantmask;
316 break;
317
318 case SIG_UNBLOCK:
319 p->p_sigmask &= ~uap->mask;
320 break;
321
322 case SIG_SETMASK:
323 p->p_sigmask = uap->mask &~ sigcantmask;
324 break;
325
326 default:
327 error = EINVAL;
328 break;
329 }
330 (void) spl0();
331 return (error);
332}
333
334#ifndef _SYS_SYSPROTO_H_
335struct sigpending_args {
336 int dummy;
337};
338#endif
339/* ARGSUSED */
340int
341sigpending(p, uap)
342 struct proc *p;
343 struct sigpending_args *uap;
344{
345
346 p->p_retval[0] = p->p_siglist;
347 return (0);
348}
349
350#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
351/*
352 * Generalized interface signal handler, 4.3-compatible.
353 */
354#ifndef _SYS_SYSPROTO_H_
355struct osigvec_args {
356 int signum;
357 struct sigvec *nsv;
358 struct sigvec *osv;
359};
360#endif
361/* ARGSUSED */
362int
363osigvec(p, uap)
364 struct proc *p;
365 register struct osigvec_args *uap;
366{
367 struct sigvec vec;
368 register struct sigacts *ps = p->p_sigacts;
369 register struct sigvec *sv;
370 register int signum;
371 int bit, error;
372
373 signum = uap->signum;
374 if (signum <= 0 || signum >= NSIG)
375 return (EINVAL);
376 sv = &vec;
377 if (uap->osv) {
378 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
379 sv->sv_mask = ps->ps_catchmask[signum];
380 bit = sigmask(signum);
381 sv->sv_flags = 0;
382 if ((ps->ps_sigonstack & bit) != 0)
383 sv->sv_flags |= SV_ONSTACK;
384 if ((ps->ps_sigintr & bit) != 0)
385 sv->sv_flags |= SV_INTERRUPT;
386 if ((ps->ps_sigreset & bit) != 0)
387 sv->sv_flags |= SV_RESETHAND;
388 if ((ps->ps_signodefer & bit) != 0)
389 sv->sv_flags |= SV_NODEFER;
390#ifndef COMPAT_SUNOS
391 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
392 sv->sv_flags |= SV_NOCLDSTOP;
393#endif
394 if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
395 sizeof (vec))))
396 return (error);
397 }
398 if (uap->nsv) {
399 if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
400 sizeof (vec))))
401 return (error);
402 if ((signum == SIGKILL || signum == SIGSTOP) &&
403 sv->sv_handler != SIG_DFL)
404 return (EINVAL);
405#ifdef COMPAT_SUNOS
406 sv->sv_flags |= SA_USERTRAMP;
407#endif
408 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
409 setsigvec(p, signum, (struct sigaction *)sv);
410 }
411 return (0);
412}
413
414#ifndef _SYS_SYSPROTO_H_
415struct osigblock_args {
416 int mask;
417};
418#endif
419int
420osigblock(p, uap)
421 register struct proc *p;
422 struct osigblock_args *uap;
423{
424
425 (void) splhigh();
426 p->p_retval[0] = p->p_sigmask;
427 p->p_sigmask |= uap->mask &~ sigcantmask;
428 (void) spl0();
429 return (0);
430}
431
432#ifndef _SYS_SYSPROTO_H_
433struct osigsetmask_args {
434 int mask;
435};
436#endif
437int
438osigsetmask(p, uap)
439 struct proc *p;
440 struct osigsetmask_args *uap;
441{
442
443 (void) splhigh();
444 p->p_retval[0] = p->p_sigmask;
445 p->p_sigmask = uap->mask &~ sigcantmask;
446 (void) spl0();
447 return (0);
448}
449#endif /* COMPAT_43 || COMPAT_SUNOS */
450
451/*
452 * Suspend process until signal, providing mask to be set
453 * in the meantime. Note nonstandard calling convention:
454 * libc stub passes mask, not pointer, to save a copyin.
455 */
456#ifndef _SYS_SYSPROTO_H_
457struct sigsuspend_args {
458 sigset_t mask;
459};
460#endif
461/* ARGSUSED */
462int
463sigsuspend(p, uap)
464 register struct proc *p;
465 struct sigsuspend_args *uap;
466{
467 register struct sigacts *ps = p->p_sigacts;
468
469 /*
470 * When returning from sigpause, we want
471 * the old mask to be restored after the
472 * signal handler has finished. Thus, we
473 * save it here and mark the sigacts structure
474 * to indicate this.
475 */
476 p->p_oldsigmask = p->p_sigmask;
477 p->p_sigmask = uap->mask &~ sigcantmask;
478 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
479 /* void */;
480 /* always return EINTR rather than ERESTART... */
481 return (EINTR);
482}
483
484#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
485#ifndef _SYS_SYSPROTO_H_
486struct osigstack_args {
487 struct sigstack *nss;
488 struct sigstack *oss;
489};
490#endif
491/* ARGSUSED */
492int
493osigstack(p, uap)
494 struct proc *p;
495 register struct osigstack_args *uap;
496{
497 struct sigstack ss;
498 struct sigacts *psp;
499 int error = 0;
500
501 psp = p->p_sigacts;
502 ss.ss_sp = psp->ps_sigstk.ss_sp;
503 ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
504 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
505 sizeof (struct sigstack))))
506 return (error);
507 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
508 sizeof (ss))) == 0) {
509 psp->ps_sigstk.ss_sp = ss.ss_sp;
510 psp->ps_sigstk.ss_size = 0;
511 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
512 psp->ps_flags |= SAS_ALTSTACK;
513 }
514 return (error);
515}
516#endif /* COMPAT_43 || COMPAT_SUNOS */
517
518#ifndef _SYS_SYSPROTO_H_
519struct sigaltstack_args {
520 struct sigaltstack *nss;
521 struct sigaltstack *oss;
522};
523#endif
524/* ARGSUSED */
525int
526sigaltstack(p, uap)
527 struct proc *p;
528 register struct sigaltstack_args *uap;
529{
530 struct sigacts *psp;
531 struct sigaltstack ss;
532 int error;
533
534 psp = p->p_sigacts;
535 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
536 psp->ps_sigstk.ss_flags |= SS_DISABLE;
537 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
538 (caddr_t)uap->oss, sizeof (struct sigaltstack))))
539 return (error);
540 if (uap->nss == 0)
541 return (0);
542 if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
543 return (error);
544 if (ss.ss_flags & SS_DISABLE) {
545 if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
546 return (EINVAL);
547 psp->ps_flags &= ~SAS_ALTSTACK;
548 psp->ps_sigstk.ss_flags = ss.ss_flags;
549 return (0);
550 }
551 if (ss.ss_size < MINSIGSTKSZ)
552 return (ENOMEM);
553 psp->ps_flags |= SAS_ALTSTACK;
554 psp->ps_sigstk= ss;
555 return (0);
556}
557
558/*
559 * Common code for kill process group/broadcast kill.
560 * cp is calling process.
561 */
562int
563killpg1(cp, signum, pgid, all)
564 register struct proc *cp;
565 int signum, pgid, all;
566{
567 register struct proc *p;
568 register struct pcred *pc = cp->p_cred;
569 struct pgrp *pgrp;
570 int nfound = 0;
571
572 if (all)
573 /*
574 * broadcast
575 */
576 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
577 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
578 p == cp || !CANSIGNAL(cp, pc, p, signum))
579 continue;
580 nfound++;
581 if (signum)
582 psignal(p, signum);
583 }
584 else {
585 if (pgid == 0)
586 /*
587 * zero pgid means send to my process group.
588 */
589 pgrp = cp->p_pgrp;
590 else {
591 pgrp = pgfind(pgid);
592 if (pgrp == NULL)
593 return (ESRCH);
594 }
595 for (p = pgrp->pg_members.lh_first; p != 0;
596 p = p->p_pglist.le_next) {
597 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
598 p->p_stat == SZOMB ||
599 !CANSIGNAL(cp, pc, p, signum))
600 continue;
601 nfound++;
602 if (signum)
603 psignal(p, signum);
604 }
605 }
606 return (nfound ? 0 : ESRCH);
607}
608
609#ifndef _SYS_SYSPROTO_H_
610struct kill_args {
611 int pid;
612 int signum;
613};
614#endif
615/* ARGSUSED */
616int
617kill(cp, uap)
618 register struct proc *cp;
619 register struct kill_args *uap;
620{
621 register struct proc *p;
622 register struct pcred *pc = cp->p_cred;
623
624 if ((u_int)uap->signum >= NSIG)
625 return (EINVAL);
626 if (uap->pid > 0) {
627 /* kill single process */
628 if ((p = pfind(uap->pid)) == NULL)
629 return (ESRCH);
630 if (!CANSIGNAL(cp, pc, p, uap->signum))
631 return (EPERM);
632 if (uap->signum)
633 psignal(p, uap->signum);
634 return (0);
635 }
636 switch (uap->pid) {
637 case -1: /* broadcast signal */
638 return (killpg1(cp, uap->signum, 0, 1));
639 case 0: /* signal own process group */
640 return (killpg1(cp, uap->signum, 0, 0));
641 default: /* negative explicit process group */
642 return (killpg1(cp, uap->signum, -uap->pid, 0));
643 }
644 /* NOTREACHED */
645}
646
647#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
648#ifndef _SYS_SYSPROTO_H_
649struct okillpg_args {
650 int pgid;
651 int signum;
652};
653#endif
654/* ARGSUSED */
655int
656okillpg(p, uap)
657 struct proc *p;
658 register struct okillpg_args *uap;
659{
660
661 if ((u_int)uap->signum >= NSIG)
662 return (EINVAL);
663 return (killpg1(p, uap->signum, uap->pgid, 0));
664}
665#endif /* COMPAT_43 || COMPAT_SUNOS */
666
667/*
668 * Send a signal to a process group.
669 */
670void
671gsignal(pgid, signum)
672 int pgid, signum;
673{
674 struct pgrp *pgrp;
675
676 if (pgid && (pgrp = pgfind(pgid)))
677 pgsignal(pgrp, signum, 0);
678}
679
680/*
681 * Send a signal to a process group. If checktty is 1,
682 * limit to members which have a controlling terminal.
683 */
684void
685pgsignal(pgrp, signum, checkctty)
686 struct pgrp *pgrp;
687 int signum, checkctty;
688{
689 register struct proc *p;
690
691 if (pgrp)
692 for (p = pgrp->pg_members.lh_first; p != 0;
693 p = p->p_pglist.le_next)
694 if (checkctty == 0 || p->p_flag & P_CONTROLT)
695 psignal(p, signum);
696}
697
698/*
699 * Send a signal caused by a trap to the current process.
700 * If it will be caught immediately, deliver it with correct code.
701 * Otherwise, post it normally.
702 */
703void
704trapsignal(p, signum, code)
705 struct proc *p;
706 register int signum;
707 u_long code;
708{
709 register struct sigacts *ps = p->p_sigacts;
710 int mask;
711
712 mask = sigmask(signum);
713 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
714 (p->p_sigmask & mask) == 0) {
715 p->p_stats->p_ru.ru_nsignals++;
716#ifdef KTRACE
717 if (KTRPOINT(p, KTR_PSIG))
718 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
719 p->p_sigmask, code);
720#endif
721 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum,
722 p->p_sigmask, code);
723 p->p_sigmask |= ps->ps_catchmask[signum] |
724 (mask & ~ps->ps_signodefer);
725 if ((ps->ps_sigreset & mask) != 0) {
726 /*
727 * See setsigvec() for origin of this code.
728 */
729 p->p_sigcatch &= ~mask;
730 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
731 p->p_sigignore |= mask;
732 ps->ps_sigact[signum] = SIG_DFL;
733 }
734 } else {
735 p->p_code = code; /* XXX for core dump/debugger */
736 p->p_sig = signum; /* XXX to verify code */
737 psignal(p, signum);
738 }
739}
740
741/*
742 * Send the signal to the process. If the signal has an action, the action
743 * is usually performed by the target process rather than the caller; we add
744 * the signal to the set of pending signals for the process.
745 *
746 * Exceptions:
747 * o When a stop signal is sent to a sleeping process that takes the
748 * default action, the process is stopped without awakening it.
749 * o SIGCONT restarts stopped processes (or puts them back to sleep)
750 * regardless of the signal action (eg, blocked or ignored).
751 *
752 * Other ignored signals are discarded immediately.
753 */
754void
755psignal(p, signum)
756 register struct proc *p;
757 register int signum;
758{
759 register int s, prop;
760 register sig_t action;
761 int mask;
762
763 if ((u_int)signum >= NSIG || signum == 0) {
764 printf("psignal: signum %d\n", signum);
765 panic("psignal signal number");
766 }
767 mask = sigmask(signum);
768 prop = sigprop[signum];
769
770 /*
771 * If proc is traced, always give parent a chance;
772 * if signal event is tracked by procfs, give *that*
773 * a chance, as well.
774 */
775 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
776 action = SIG_DFL;
777 else {
778 /*
779 * If the signal is being ignored,
780 * then we forget about it immediately.
781 * (Note: we don't set SIGCONT in p_sigignore,
782 * and if it is set to SIG_IGN,
783 * action will be SIG_DFL here.)
784 */
785 if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT))
786 return;
787 if (p->p_sigmask & mask)
788 action = SIG_HOLD;
789 else if (p->p_sigcatch & mask)
790 action = SIG_CATCH;
791 else
792 action = SIG_DFL;
793 }
794
795 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
796 (p->p_flag & P_TRACED) == 0)
797 p->p_nice = NZERO;
798
799 if (prop & SA_CONT)
800 p->p_siglist &= ~stopsigmask;
801
802 if (prop & SA_STOP) {
803 /*
804 * If sending a tty stop signal to a member of an orphaned
805 * process group, discard the signal here if the action
806 * is default; don't stop the process below if sleeping,
807 * and don't clear any pending SIGCONT.
808 */
809 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
810 action == SIG_DFL)
811 return;
812 p->p_siglist &= ~contsigmask;
813 }
814 p->p_siglist |= mask;
815
816 /*
817 * Defer further processing for signals which are held,
818 * except that stopped processes must be continued by SIGCONT.
819 */
820 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
821 return;
822 s = splhigh();
823 switch (p->p_stat) {
824
825 case SSLEEP:
826 /*
827 * If process is sleeping uninterruptibly
828 * we can't interrupt the sleep... the signal will
829 * be noticed when the process returns through
830 * trap() or syscall().
831 */
832 if ((p->p_flag & P_SINTR) == 0)
833 goto out;
834 /*
835 * Process is sleeping and traced... make it runnable
836 * so it can discover the signal in issignal() and stop
837 * for the parent.
838 */
839 if (p->p_flag & P_TRACED)
840 goto run;
841 /*
842 * If SIGCONT is default (or ignored) and process is
843 * asleep, we are finished; the process should not
844 * be awakened.
845 */
846 if ((prop & SA_CONT) && action == SIG_DFL) {
847 p->p_siglist &= ~mask;
848 goto out;
849 }
850 /*
851 * When a sleeping process receives a stop
852 * signal, process immediately if possible.
853 * All other (caught or default) signals
854 * cause the process to run.
855 */
856 if (prop & SA_STOP) {
857 if (action != SIG_DFL)
858 goto runfast;
859 /*
860 * If a child holding parent blocked,
861 * stopping could cause deadlock.
862 */
863 if (p->p_flag & P_PPWAIT)
864 goto out;
865 p->p_siglist &= ~mask;
866 p->p_xstat = signum;
867 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
868 psignal(p->p_pptr, SIGCHLD);
869 stop(p);
870 goto out;
871 } else
872 goto runfast;
873 /*NOTREACHED*/
874
875 case SSTOP:
876 /*
877 * If traced process is already stopped,
878 * then no further action is necessary.
879 */
880 if (p->p_flag & P_TRACED)
881 goto out;
882
883 /*
884 * Kill signal always sets processes running.
885 */
886 if (signum == SIGKILL)
887 goto runfast;
888
889 if (prop & SA_CONT) {
890 /*
891 * If SIGCONT is default (or ignored), we continue the
892 * process but don't leave the signal in p_siglist, as
893 * it has no further action. If SIGCONT is held, we
894 * continue the process and leave the signal in
895 * p_siglist. If the process catches SIGCONT, let it
896 * handle the signal itself. If it isn't waiting on
897 * an event, then it goes back to run state.
898 * Otherwise, process goes back to sleep state.
899 */
900 if (action == SIG_DFL)
901 p->p_siglist &= ~mask;
902 if (action == SIG_CATCH)
903 goto runfast;
904 if (p->p_wchan == 0)
905 goto run;
906 p->p_stat = SSLEEP;
907 goto out;
908 }
909
910 if (prop & SA_STOP) {
911 /*
912 * Already stopped, don't need to stop again.
913 * (If we did the shell could get confused.)
914 */
915 p->p_siglist &= ~mask; /* take it away */
916 goto out;
917 }
918
919 /*
920 * If process is sleeping interruptibly, then simulate a
921 * wakeup so that when it is continued, it will be made
922 * runnable and can look at the signal. But don't make
923 * the process runnable, leave it stopped.
924 */
925 if (p->p_wchan && p->p_flag & P_SINTR)
926 unsleep(p);
927 goto out;
928
929 default:
930 /*
931 * SRUN, SIDL, SZOMB do nothing with the signal,
932 * other than kicking ourselves if we are running.
933 * It will either never be noticed, or noticed very soon.
934 */
935 if (p == curproc)
936 signotify(p);
937#ifdef SMP
938 else if (p->p_stat == SRUN)
939 forward_signal(p);
940#endif
941 goto out;
942 }
943 /*NOTREACHED*/
944
945runfast:
946 /*
947 * Raise priority to at least PUSER.
948 */
949 if (p->p_priority > PUSER)
950 p->p_priority = PUSER;
951run:
952 setrunnable(p);
953out:
954 splx(s);
955}
956
957/*
958 * If the current process has received a signal (should be caught or cause
959 * termination, should interrupt current syscall), return the signal number.
960 * Stop signals with default action are processed immediately, then cleared;
961 * they aren't returned. This is checked after each entry to the system for
962 * a syscall or trap (though this can usually be done without calling issignal
963 * by checking the pending signal masks in the CURSIG macro.) The normal call
964 * sequence is
965 *
966 * while (signum = CURSIG(curproc))
967 * postsig(signum);
968 */
969int
970issignal(p)
971 register struct proc *p;
972{
973 register int signum, mask, prop;
974
975 for (;;) {
976 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
977
978 mask = p->p_siglist & ~p->p_sigmask;
979 if (p->p_flag & P_PPWAIT)
980 mask &= ~stopsigmask;
981 if (mask == 0) /* no signal to send */
982 return (0);
983 signum = ffs((long)mask);
984 mask = sigmask(signum);
985 prop = sigprop[signum];
986
987 STOPEVENT(p, S_SIG, signum);
988
989 /*
990 * We should see pending but ignored signals
991 * only if P_TRACED was on when they were posted.
992 */
993 if ((mask & p->p_sigignore) && (traced == 0)) {
994 p->p_siglist &= ~mask;
995 continue;
996 }
997 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
998 /*
999 * If traced, always stop, and stay
1000 * stopped until released by the parent.
1001 */
1002 p->p_xstat = signum;
1003 psignal(p->p_pptr, SIGCHLD);
1004 do {
1005 stop(p);
1006 mi_switch();
1007 } while (!trace_req(p)
1008 && p->p_flag & P_TRACED);
1009
1010 /*
1011 * If the traced bit got turned off, go back up
1012 * to the top to rescan signals. This ensures
1013 * that p_sig* and ps_sigact are consistent.
1014 */
1015 if ((p->p_flag & P_TRACED) == 0)
1016 continue;
1017
1018 /*
1019 * If parent wants us to take the signal,
1020 * then it will leave it in p->p_xstat;
1021 * otherwise we just look for signals again.
1022 */
1023 p->p_siglist &= ~mask; /* clear the old signal */
1024 signum = p->p_xstat;
1025 if (signum == 0)
1026 continue;
1027
1028 /*
1029 * Put the new signal into p_siglist. If the
1030 * signal is being masked, look for other signals.
1031 */
1032 mask = sigmask(signum);
1033 p->p_siglist |= mask;
1034 if (p->p_sigmask & mask)
1035 continue;
1036 }
1037
1038 /*
1039 * Decide whether the signal should be returned.
1040 * Return the signal's number, or fall through
1041 * to clear it from the pending mask.
1042 */
1043 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) {
1044
1045 case (int)SIG_DFL:
1046 /*
1047 * Don't take default actions on system processes.
1048 */
1049 if (p->p_pid <= 1) {
1050#ifdef DIAGNOSTIC
1051 /*
1052 * Are you sure you want to ignore SIGSEGV
1053 * in init? XXX
1054 */
1055 printf("Process (pid %lu) got signal %d\n",
1056 (u_long)p->p_pid, signum);
1057#endif
1058 break; /* == ignore */
1059 }
1060 /*
1061 * If there is a pending stop signal to process
1062 * with default action, stop here,
1063 * then clear the signal. However,
1064 * if process is member of an orphaned
1065 * process group, ignore tty stop signals.
1066 */
1067 if (prop & SA_STOP) {
1068 if (p->p_flag & P_TRACED ||
1069 (p->p_pgrp->pg_jobc == 0 &&
1070 prop & SA_TTYSTOP))
1071 break; /* == ignore */
1072 p->p_xstat = signum;
1073 stop(p);
1074 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
1075 psignal(p->p_pptr, SIGCHLD);
1076 mi_switch();
1077 break;
1078 } else if (prop & SA_IGNORE) {
1079 /*
1080 * Except for SIGCONT, shouldn't get here.
1081 * Default action is to ignore; drop it.
1082 */
1083 break; /* == ignore */
1084 } else
1085 return (signum);
1086 /*NOTREACHED*/
1087
1088 case (int)SIG_IGN:
1089 /*
1090 * Masking above should prevent us ever trying
1091 * to take action on an ignored signal other
1092 * than SIGCONT, unless process is traced.
1093 */
1094 if ((prop & SA_CONT) == 0 &&
1095 (p->p_flag & P_TRACED) == 0)
1096 printf("issignal\n");
1097 break; /* == ignore */
1098
1099 default:
1100 /*
1101 * This signal has an action, let
1102 * postsig() process it.
1103 */
1104 return (signum);
1105 }
1106 p->p_siglist &= ~mask; /* take the signal! */
1107 }
1108 /* NOTREACHED */
1109}
1110
1111/*
1112 * Put the argument process into the stopped state and notify the parent
1113 * via wakeup. Signals are handled elsewhere. The process must not be
1114 * on the run queue.
1115 */
1116void
1117stop(p)
1118 register struct proc *p;
1119{
1120
1121 p->p_stat = SSTOP;
1122 p->p_flag &= ~P_WAITED;
1123 wakeup((caddr_t)p->p_pptr);
1124}
1125
1126/*
1127 * Take the action for the specified signal
1128 * from the current set of pending signals.
1129 */
1130void
1131postsig(signum)
1132 register int signum;
1133{
1134 register struct proc *p = curproc;
1135 register struct sigacts *ps = p->p_sigacts;
1136 register sig_t action;
1137 int code, mask, returnmask;
1138
1139 KASSERT(signum != 0, ("postsig"));
1140
1141 mask = sigmask(signum);
1142 p->p_siglist &= ~mask;
1143 action = ps->ps_sigact[signum];
1144#ifdef KTRACE
1145 if (KTRPOINT(p, KTR_PSIG))
1146 ktrpsig(p->p_tracep,
1147 signum, action, p->p_oldsigmask ?
1148 p->p_oldsigmask : p->p_sigmask, 0);
1149#endif
1150 STOPEVENT(p, S_SIG, signum);
1151
1152 if (action == SIG_DFL) {
1153 /*
1154 * Default action, where the default is to kill
1155 * the process. (Other cases were ignored above.)
1156 */
1157 sigexit(p, signum);
1158 /* NOTREACHED */
1159 } else {
1160 /*
1161 * If we get here, the signal must be caught.
1162 */
1163 KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0,
1164 ("postsig action"));
1165 /*
1166 * Set the new mask value and also defer further
1167 * occurences of this signal.
1168 *
1169 * Special case: user has done a sigpause. Here the
1170 * current mask is not of interest, but rather the
1171 * mask from before the sigpause is what we want
1172 * restored after the signal processing is completed.
1173 */
1174 (void) splhigh();
1175 if (p->p_oldsigmask) {
1176 returnmask = p->p_oldsigmask;
1177 p->p_oldsigmask = 0;
1178 } else
1179 returnmask = p->p_sigmask;
1180 p->p_sigmask |= ps->ps_catchmask[signum] |
1181 (mask & ~ps->ps_signodefer);
1182 if ((ps->ps_sigreset & mask) != 0) {
1183 /*
1184 * See setsigvec() for origin of this code.
1185 */
1186 p->p_sigcatch &= ~mask;
1187 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1188 p->p_sigignore |= mask;
1189 ps->ps_sigact[signum] = SIG_DFL;
1190 }
1191 (void) spl0();
1192 p->p_stats->p_ru.ru_nsignals++;
1193 if (p->p_sig != signum) {
1194 code = 0;
1195 } else {
1196 code = p->p_code;
1197 p->p_code = 0;
1198 p->p_sig = 0;
1199 }
1200 (*p->p_sysent->sv_sendsig)(action, signum, returnmask, code);
1201 }
1202}
1203
1204/*
1205 * Kill the current process for stated reason.
1206 */
1207void
1208killproc(p, why)
1209 struct proc *p;
1210 char *why;
1211{
1212 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1213 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1214 psignal(p, SIGKILL);
1215}
1216
1217/*
1218 * Force the current process to exit with the specified signal, dumping core
1219 * if appropriate. We bypass the normal tests for masked and caught signals,
1220 * allowing unrecoverable failures to terminate the process without changing
1221 * signal state. Mark the accounting record with the signal termination.
1222 * If dumping core, save the signal number for the debugger. Calls exit and
1223 * does not return.
1224 */
1225void
1226sigexit(p, signum)
1227 register struct proc *p;
1228 int signum;
1229{
1230
1231 p->p_acflag |= AXSIG;
1232 if (sigprop[signum] & SA_CORE) {
1233 p->p_sig = signum;
1234 /*
1235 * Log signals which would cause core dumps
1236 * (Log as LOG_INFO to appease those who don't want
1237 * these messages.)
1238 * XXX : Todo, as well as euid, write out ruid too
1239 */
1240 if (p->p_sysent->sv_coredump != NULL &&
1241 (*p->p_sysent->sv_coredump)(p) == 0)
1242 signum |= WCOREFLAG;
1243 if (kern_logsigexit)
1244 log(LOG_INFO,
1245 "pid %d (%s), uid %d: exited on signal %d%s\n",
1246 p->p_pid, p->p_comm,
1247 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1248 signum &~ WCOREFLAG,
1249 signum & WCOREFLAG ? " (core dumped)" : "");
1250 }
1251 exit1(p, W_EXITCODE(0, signum));
1252 /* NOTREACHED */
1253}
1254
1255static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1256SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1257 sizeof(corefilename), "process corefile name format string");
1258
1259/*
1260 * expand_name(name, uid, pid)
1261 * Expand the name described in corefilename, using name, uid, and pid.
1262 * corefilename is a printf-like string, with three format specifiers:
1263 * %N name of process ("name")
1264 * %P process id (pid)
1265 * %U user id (uid)
1266 * For example, "%N.core" is the default; they can be disabled completely
1267 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1268 * This is controlled by the sysctl variable kern.corefile (see above).
1269 */
1270
1271char *
1272expand_name(name, uid, pid)
1273const char *name; int uid; int pid; {
1274 char *temp;
1275 char buf[11]; /* Buffer for pid/uid -- max 4B */
1276 int i, n;
1277 char *format = corefilename;
1278
1279 temp = malloc(MAXPATHLEN + 3, M_TEMP, M_NOWAIT);
1280 if (temp == NULL)
1281 return NULL;
1282 bzero(temp, MAXPATHLEN+3);
1283 for (i = 0, n = 0; i < MAXPATHLEN && format[i]; i++) {
1284 int l;
1285 switch (format[i]) {
1286 case '%': /* Format character */
1287 i++;
1288 switch (format[i]) {
1289 case '%':
1290 temp[n++] = '%';
1291 break;
1292 case 'N': /* process name */
1293 l = strlen(name);
1294 if ((n + l) > MAXPATHLEN) {
1295 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1296 pid, name, uid, temp, name);
1297 free(temp, M_TEMP);
1298 return NULL;
1299 }
1300 memcpy(temp+n, name, l);
1301 n += l;
1302 break;
1303 case 'P': /* process id */
1304 sprintf(buf, "%u", pid);
1305 l = strlen(buf);
1306 if ((n + l) > MAXPATHLEN) {
1307 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1308 pid, name, uid, temp, name);
1309 free(temp, M_TEMP);
1310 return NULL;
1311 }
1312 memcpy(temp+n, buf, l);
1313 n += l;
1314 break;
1315 case 'U': /* user id */
1316 sprintf(buf, "%u", uid);
1317 l = strlen(buf);
1318 if ((n + l) > MAXPATHLEN) {
1319 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1320 pid, name, uid, temp, name);
1321 free(temp, M_TEMP);
1322 return NULL;
1323 }
1324 memcpy(temp+n, buf, l);
1325 n += l;
1326 break;
1327 default:
1328 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1329 }
1330 break;
1331 default:
1332 temp[n++] = format[i];
1333 }
1334 }
1335 return temp;
1336}
1337
1338/*
1339 * Nonexistent system call-- signal process (may want to handle it).
1340 * Flag error in case process won't see signal immediately (blocked or ignored).
1341 */
1342#ifndef _SYS_SYSPROTO_H_
1343struct nosys_args {
1344 int dummy;
1345};
1346#endif
1347/* ARGSUSED */
1348int
1349nosys(p, args)
1350 struct proc *p;
1351 struct nosys_args *args;
1352{
1353
1354 psignal(p, SIGSYS);
1355 return (EINVAL);
1356}
1357
1358/*
1359 * Send a signal to a SIGIO or SIGURG to a process or process group using
1360 * stored credentials rather than those of the current process.
1361 */
1362void
1363pgsigio(sigio, signum, checkctty)
1364 struct sigio *sigio;
1365 int signum, checkctty;
1366{
1367 if (sigio == NULL)
1368 return;
1369
1370 if (sigio->sio_pgid > 0) {
1371 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1372 sigio->sio_proc))
1373 psignal(sigio->sio_proc, signum);
1374 } else if (sigio->sio_pgid < 0) {
1375 struct proc *p;
1376
1377 for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL;
1378 p = p->p_pglist.le_next)
1379 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1380 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1381 psignal(p, signum);
1382 }
1383}