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