kern_sig.c revision 1.50 1 1.50 mycroft /* $NetBSD: kern_sig.c,v 1.50 1995/10/07 06:28:25 mycroft Exp $ */
2 1.29 cgd
3 1.29 cgd /*
4 1.29 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 1.29 cgd * The Regents of the University of California. All rights reserved.
6 1.29 cgd * (c) UNIX System Laboratories, Inc.
7 1.29 cgd * All or some portions of this file are derived from material licensed
8 1.29 cgd * to the University of California by American Telephone and Telegraph
9 1.29 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.29 cgd * the permission of UNIX System Laboratories, Inc.
11 1.29 cgd *
12 1.29 cgd * Redistribution and use in source and binary forms, with or without
13 1.29 cgd * modification, are permitted provided that the following conditions
14 1.29 cgd * are met:
15 1.29 cgd * 1. Redistributions of source code must retain the above copyright
16 1.29 cgd * notice, this list of conditions and the following disclaimer.
17 1.29 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.29 cgd * notice, this list of conditions and the following disclaimer in the
19 1.29 cgd * documentation and/or other materials provided with the distribution.
20 1.29 cgd * 3. All advertising materials mentioning features or use of this software
21 1.29 cgd * must display the following acknowledgement:
22 1.29 cgd * This product includes software developed by the University of
23 1.29 cgd * California, Berkeley and its contributors.
24 1.29 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.29 cgd * may be used to endorse or promote products derived from this software
26 1.29 cgd * without specific prior written permission.
27 1.29 cgd *
28 1.29 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.29 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.29 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.29 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.29 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.29 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.29 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.29 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.29 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.29 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.29 cgd * SUCH DAMAGE.
39 1.29 cgd *
40 1.29 cgd * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
41 1.29 cgd */
42 1.29 cgd
43 1.29 cgd #define SIGPROP /* include signal properties table */
44 1.29 cgd #include <sys/param.h>
45 1.29 cgd #include <sys/signalvar.h>
46 1.29 cgd #include <sys/resourcevar.h>
47 1.29 cgd #include <sys/namei.h>
48 1.29 cgd #include <sys/vnode.h>
49 1.29 cgd #include <sys/proc.h>
50 1.29 cgd #include <sys/systm.h>
51 1.29 cgd #include <sys/timeb.h>
52 1.29 cgd #include <sys/times.h>
53 1.29 cgd #include <sys/buf.h>
54 1.29 cgd #include <sys/acct.h>
55 1.29 cgd #include <sys/file.h>
56 1.29 cgd #include <sys/kernel.h>
57 1.29 cgd #include <sys/wait.h>
58 1.29 cgd #include <sys/ktrace.h>
59 1.29 cgd #include <sys/syslog.h>
60 1.29 cgd #include <sys/stat.h>
61 1.29 cgd #include <sys/core.h>
62 1.29 cgd
63 1.32 cgd #include <sys/mount.h>
64 1.32 cgd #include <sys/syscallargs.h>
65 1.32 cgd
66 1.29 cgd #include <machine/cpu.h>
67 1.29 cgd
68 1.29 cgd #include <vm/vm.h>
69 1.29 cgd #include <sys/user.h> /* for coredump */
70 1.29 cgd
71 1.29 cgd void stop __P((struct proc *p));
72 1.29 cgd
73 1.29 cgd /*
74 1.29 cgd * Can process p, with pcred pc, send the signal signum to process q?
75 1.29 cgd */
76 1.29 cgd #define CANSIGNAL(p, pc, q, signum) \
77 1.29 cgd ((pc)->pc_ucred->cr_uid == 0 || \
78 1.29 cgd (pc)->p_ruid == (q)->p_cred->p_ruid || \
79 1.29 cgd (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
80 1.29 cgd (pc)->p_ruid == (q)->p_ucred->cr_uid || \
81 1.29 cgd (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
82 1.29 cgd ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
83 1.29 cgd
84 1.29 cgd /* ARGSUSED */
85 1.50 mycroft sys_sigaction(p, v, retval)
86 1.29 cgd struct proc *p;
87 1.48 thorpej void *v;
88 1.48 thorpej register_t *retval;
89 1.48 thorpej {
90 1.50 mycroft register struct sys_sigaction_args /* {
91 1.32 cgd syscallarg(int) signum;
92 1.32 cgd syscallarg(struct sigaction *) nsa;
93 1.32 cgd syscallarg(struct sigaction *) osa;
94 1.48 thorpej } */ *uap = v;
95 1.29 cgd struct sigaction vec;
96 1.29 cgd register struct sigaction *sa;
97 1.29 cgd register struct sigacts *ps = p->p_sigacts;
98 1.29 cgd register int signum;
99 1.29 cgd int bit, error;
100 1.29 cgd
101 1.32 cgd signum = SCARG(uap, signum);
102 1.29 cgd if (signum <= 0 || signum >= NSIG ||
103 1.29 cgd signum == SIGKILL || signum == SIGSTOP)
104 1.29 cgd return (EINVAL);
105 1.29 cgd sa = &vec;
106 1.32 cgd if (SCARG(uap, osa)) {
107 1.29 cgd sa->sa_handler = ps->ps_sigact[signum];
108 1.29 cgd sa->sa_mask = ps->ps_catchmask[signum];
109 1.29 cgd bit = sigmask(signum);
110 1.29 cgd sa->sa_flags = 0;
111 1.29 cgd if ((ps->ps_sigonstack & bit) != 0)
112 1.29 cgd sa->sa_flags |= SA_ONSTACK;
113 1.29 cgd if ((ps->ps_sigintr & bit) == 0)
114 1.29 cgd sa->sa_flags |= SA_RESTART;
115 1.45 mycroft if ((ps->ps_sigreset & bit) != 0)
116 1.45 mycroft sa->sa_flags |= SA_RESETHAND;
117 1.45 mycroft if (signum == SIGCHLD) {
118 1.45 mycroft if ((p->p_flag & P_NOCLDSTOP) != 0)
119 1.45 mycroft sa->sa_flags |= SA_NOCLDSTOP;
120 1.45 mycroft }
121 1.45 mycroft if ((sa->sa_mask & bit) == 0)
122 1.45 mycroft sa->sa_flags |= SA_NODEFER;
123 1.45 mycroft sa->sa_mask &= ~bit;
124 1.32 cgd if (error = copyout((caddr_t)sa, (caddr_t)SCARG(uap, osa),
125 1.29 cgd sizeof (vec)))
126 1.29 cgd return (error);
127 1.29 cgd }
128 1.32 cgd if (SCARG(uap, nsa)) {
129 1.32 cgd if (error = copyin((caddr_t)SCARG(uap, nsa), (caddr_t)sa,
130 1.29 cgd sizeof (vec)))
131 1.29 cgd return (error);
132 1.29 cgd setsigvec(p, signum, sa);
133 1.29 cgd }
134 1.29 cgd return (0);
135 1.29 cgd }
136 1.29 cgd
137 1.29 cgd setsigvec(p, signum, sa)
138 1.29 cgd register struct proc *p;
139 1.29 cgd int signum;
140 1.29 cgd register struct sigaction *sa;
141 1.29 cgd {
142 1.29 cgd register struct sigacts *ps = p->p_sigacts;
143 1.29 cgd register int bit;
144 1.29 cgd
145 1.29 cgd bit = sigmask(signum);
146 1.29 cgd /*
147 1.29 cgd * Change setting atomically.
148 1.29 cgd */
149 1.29 cgd (void) splhigh();
150 1.29 cgd ps->ps_sigact[signum] = sa->sa_handler;
151 1.45 mycroft if ((sa->sa_flags & SA_NODEFER) == 0)
152 1.45 mycroft sa->sa_mask |= sigmask(signum);
153 1.29 cgd ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
154 1.45 mycroft if (signum == SIGCHLD) {
155 1.45 mycroft if (sa->sa_flags & SA_NOCLDSTOP)
156 1.45 mycroft p->p_flag |= P_NOCLDSTOP;
157 1.45 mycroft else
158 1.45 mycroft p->p_flag &= ~P_NOCLDSTOP;
159 1.45 mycroft }
160 1.45 mycroft if ((sa->sa_flags & SA_RESETHAND) != 0)
161 1.45 mycroft ps->ps_sigreset |= bit;
162 1.45 mycroft else
163 1.45 mycroft ps->ps_sigreset &= ~bit;
164 1.29 cgd if ((sa->sa_flags & SA_RESTART) == 0)
165 1.29 cgd ps->ps_sigintr |= bit;
166 1.29 cgd else
167 1.29 cgd ps->ps_sigintr &= ~bit;
168 1.45 mycroft if ((sa->sa_flags & SA_ONSTACK) != 0)
169 1.29 cgd ps->ps_sigonstack |= bit;
170 1.29 cgd else
171 1.29 cgd ps->ps_sigonstack &= ~bit;
172 1.29 cgd #ifdef COMPAT_SUNOS
173 1.41 christos {
174 1.41 christos extern struct emul emul_sunos;
175 1.41 christos if (p->p_emul == &emul_sunos && sa->sa_flags & SA_USERTRAMP)
176 1.41 christos ps->ps_usertramp |= bit;
177 1.41 christos else
178 1.41 christos ps->ps_usertramp &= ~bit;
179 1.41 christos }
180 1.29 cgd #endif
181 1.29 cgd /*
182 1.29 cgd * Set bit in p_sigignore for signals that are set to SIG_IGN,
183 1.29 cgd * and for signals set to SIG_DFL where the default is to ignore.
184 1.29 cgd * However, don't put SIGCONT in p_sigignore,
185 1.29 cgd * as we have to restart the process.
186 1.29 cgd */
187 1.29 cgd if (sa->sa_handler == SIG_IGN ||
188 1.29 cgd (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
189 1.29 cgd p->p_siglist &= ~bit; /* never to be seen again */
190 1.29 cgd if (signum != SIGCONT)
191 1.29 cgd p->p_sigignore |= bit; /* easier in psignal */
192 1.29 cgd p->p_sigcatch &= ~bit;
193 1.29 cgd } else {
194 1.29 cgd p->p_sigignore &= ~bit;
195 1.29 cgd if (sa->sa_handler == SIG_DFL)
196 1.29 cgd p->p_sigcatch &= ~bit;
197 1.29 cgd else
198 1.29 cgd p->p_sigcatch |= bit;
199 1.29 cgd }
200 1.29 cgd (void) spl0();
201 1.29 cgd }
202 1.29 cgd
203 1.29 cgd /*
204 1.29 cgd * Initialize signal state for process 0;
205 1.29 cgd * set to ignore signals that are ignored by default.
206 1.29 cgd */
207 1.29 cgd void
208 1.29 cgd siginit(p)
209 1.29 cgd struct proc *p;
210 1.29 cgd {
211 1.29 cgd register int i;
212 1.29 cgd
213 1.29 cgd for (i = 0; i < NSIG; i++)
214 1.29 cgd if (sigprop[i] & SA_IGNORE && i != SIGCONT)
215 1.29 cgd p->p_sigignore |= sigmask(i);
216 1.29 cgd }
217 1.29 cgd
218 1.29 cgd /*
219 1.29 cgd * Reset signals for an exec of the specified process.
220 1.29 cgd */
221 1.29 cgd void
222 1.29 cgd execsigs(p)
223 1.29 cgd register struct proc *p;
224 1.29 cgd {
225 1.29 cgd register struct sigacts *ps = p->p_sigacts;
226 1.29 cgd register int nc, mask;
227 1.29 cgd
228 1.29 cgd /*
229 1.29 cgd * Reset caught signals. Held signals remain held
230 1.29 cgd * through p_sigmask (unless they were caught,
231 1.29 cgd * and are now ignored by default).
232 1.29 cgd */
233 1.29 cgd while (p->p_sigcatch) {
234 1.29 cgd nc = ffs((long)p->p_sigcatch);
235 1.29 cgd mask = sigmask(nc);
236 1.29 cgd p->p_sigcatch &= ~mask;
237 1.29 cgd if (sigprop[nc] & SA_IGNORE) {
238 1.29 cgd if (nc != SIGCONT)
239 1.29 cgd p->p_sigignore |= mask;
240 1.29 cgd p->p_siglist &= ~mask;
241 1.29 cgd }
242 1.29 cgd ps->ps_sigact[nc] = SIG_DFL;
243 1.29 cgd }
244 1.29 cgd /*
245 1.29 cgd * Reset stack state to the user stack.
246 1.29 cgd * Clear set of signals caught on the signal stack.
247 1.29 cgd */
248 1.45 mycroft ps->ps_sigstk.ss_flags = SS_DISABLE;
249 1.29 cgd ps->ps_sigstk.ss_size = 0;
250 1.29 cgd ps->ps_sigstk.ss_base = 0;
251 1.29 cgd ps->ps_flags = 0;
252 1.29 cgd }
253 1.29 cgd
254 1.29 cgd /*
255 1.29 cgd * Manipulate signal mask.
256 1.29 cgd * Note that we receive new mask, not pointer,
257 1.29 cgd * and return old mask as return value;
258 1.29 cgd * the library stub does the rest.
259 1.29 cgd */
260 1.50 mycroft sys_sigprocmask(p, v, retval)
261 1.29 cgd register struct proc *p;
262 1.48 thorpej void *v;
263 1.48 thorpej register_t *retval;
264 1.48 thorpej {
265 1.50 mycroft struct sys_sigprocmask_args /* {
266 1.32 cgd syscallarg(int) how;
267 1.32 cgd syscallarg(sigset_t) mask;
268 1.48 thorpej } */ *uap = v;
269 1.29 cgd int error = 0;
270 1.29 cgd
271 1.29 cgd *retval = p->p_sigmask;
272 1.29 cgd (void) splhigh();
273 1.29 cgd
274 1.32 cgd switch (SCARG(uap, how)) {
275 1.29 cgd case SIG_BLOCK:
276 1.32 cgd p->p_sigmask |= SCARG(uap, mask) &~ sigcantmask;
277 1.29 cgd break;
278 1.29 cgd
279 1.29 cgd case SIG_UNBLOCK:
280 1.32 cgd p->p_sigmask &= ~SCARG(uap, mask);
281 1.29 cgd break;
282 1.29 cgd
283 1.29 cgd case SIG_SETMASK:
284 1.32 cgd p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
285 1.29 cgd break;
286 1.29 cgd
287 1.29 cgd default:
288 1.29 cgd error = EINVAL;
289 1.29 cgd break;
290 1.29 cgd }
291 1.29 cgd (void) spl0();
292 1.29 cgd return (error);
293 1.29 cgd }
294 1.29 cgd
295 1.29 cgd /* ARGSUSED */
296 1.50 mycroft sys_sigpending(p, v, retval)
297 1.29 cgd struct proc *p;
298 1.50 mycroft void *v;
299 1.32 cgd register_t *retval;
300 1.29 cgd {
301 1.29 cgd
302 1.29 cgd *retval = p->p_siglist;
303 1.29 cgd return (0);
304 1.29 cgd }
305 1.29 cgd
306 1.29 cgd /*
307 1.29 cgd * Suspend process until signal, providing mask to be set
308 1.29 cgd * in the meantime. Note nonstandard calling convention:
309 1.29 cgd * libc stub passes mask, not pointer, to save a copyin.
310 1.29 cgd */
311 1.29 cgd /* ARGSUSED */
312 1.29 cgd int
313 1.50 mycroft sys_sigsuspend(p, v, retval)
314 1.29 cgd register struct proc *p;
315 1.48 thorpej void *v;
316 1.48 thorpej register_t *retval;
317 1.48 thorpej {
318 1.50 mycroft struct sys_sigsuspend_args /* {
319 1.32 cgd syscallarg(int) mask;
320 1.48 thorpej } */ *uap = v;
321 1.29 cgd register struct sigacts *ps = p->p_sigacts;
322 1.29 cgd
323 1.29 cgd /*
324 1.29 cgd * When returning from sigpause, we want
325 1.29 cgd * the old mask to be restored after the
326 1.29 cgd * signal handler has finished. Thus, we
327 1.29 cgd * save it here and mark the sigacts structure
328 1.29 cgd * to indicate this.
329 1.29 cgd */
330 1.29 cgd ps->ps_oldmask = p->p_sigmask;
331 1.29 cgd ps->ps_flags |= SAS_OLDMASK;
332 1.32 cgd p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
333 1.29 cgd while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
334 1.29 cgd /* void */;
335 1.29 cgd /* always return EINTR rather than ERESTART... */
336 1.29 cgd return (EINTR);
337 1.29 cgd }
338 1.29 cgd
339 1.29 cgd /* ARGSUSED */
340 1.50 mycroft sys_sigaltstack(p, v, retval)
341 1.29 cgd struct proc *p;
342 1.48 thorpej void *v;
343 1.48 thorpej register_t *retval;
344 1.48 thorpej {
345 1.50 mycroft register struct sys_sigaltstack_args /* {
346 1.32 cgd syscallarg(struct sigaltstack *) nss;
347 1.32 cgd syscallarg(struct sigaltstack *) oss;
348 1.48 thorpej } */ *uap = v;
349 1.29 cgd struct sigacts *psp;
350 1.29 cgd struct sigaltstack ss;
351 1.29 cgd int error;
352 1.29 cgd
353 1.29 cgd psp = p->p_sigacts;
354 1.29 cgd if ((psp->ps_flags & SAS_ALTSTACK) == 0)
355 1.45 mycroft psp->ps_sigstk.ss_flags |= SS_DISABLE;
356 1.32 cgd if (SCARG(uap, oss) && (error = copyout((caddr_t)&psp->ps_sigstk,
357 1.32 cgd (caddr_t)SCARG(uap, oss), sizeof (struct sigaltstack))))
358 1.29 cgd return (error);
359 1.32 cgd if (SCARG(uap, nss) == 0)
360 1.29 cgd return (0);
361 1.32 cgd if (error = copyin((caddr_t)SCARG(uap, nss), (caddr_t)&ss,
362 1.32 cgd sizeof (ss)))
363 1.29 cgd return (error);
364 1.45 mycroft if (ss.ss_flags & SS_DISABLE) {
365 1.45 mycroft if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
366 1.29 cgd return (EINVAL);
367 1.29 cgd psp->ps_flags &= ~SAS_ALTSTACK;
368 1.29 cgd psp->ps_sigstk.ss_flags = ss.ss_flags;
369 1.29 cgd return (0);
370 1.29 cgd }
371 1.29 cgd if (ss.ss_size < MINSIGSTKSZ)
372 1.29 cgd return (ENOMEM);
373 1.29 cgd psp->ps_flags |= SAS_ALTSTACK;
374 1.29 cgd psp->ps_sigstk= ss;
375 1.29 cgd return (0);
376 1.29 cgd }
377 1.29 cgd
378 1.29 cgd /* ARGSUSED */
379 1.29 cgd int
380 1.50 mycroft sys_kill(cp, v, retval)
381 1.29 cgd register struct proc *cp;
382 1.48 thorpej void *v;
383 1.48 thorpej register_t *retval;
384 1.48 thorpej {
385 1.50 mycroft register struct sys_kill_args /* {
386 1.32 cgd syscallarg(int) pid;
387 1.32 cgd syscallarg(int) signum;
388 1.48 thorpej } */ *uap = v;
389 1.29 cgd register struct proc *p;
390 1.29 cgd register struct pcred *pc = cp->p_cred;
391 1.29 cgd
392 1.29 cgd #ifdef COMPAT_09
393 1.32 cgd SCARG(uap, pid) = (short) SCARG(uap, pid);
394 1.29 cgd #endif
395 1.29 cgd
396 1.32 cgd if ((u_int)SCARG(uap, signum) >= NSIG)
397 1.29 cgd return (EINVAL);
398 1.32 cgd if (SCARG(uap, pid) > 0) {
399 1.29 cgd /* kill single process */
400 1.32 cgd if ((p = pfind(SCARG(uap, pid))) == NULL)
401 1.29 cgd return (ESRCH);
402 1.32 cgd if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
403 1.29 cgd return (EPERM);
404 1.32 cgd if (SCARG(uap, signum))
405 1.32 cgd psignal(p, SCARG(uap, signum));
406 1.29 cgd return (0);
407 1.29 cgd }
408 1.32 cgd switch (SCARG(uap, pid)) {
409 1.29 cgd case -1: /* broadcast signal */
410 1.32 cgd return (killpg1(cp, SCARG(uap, signum), 0, 1));
411 1.29 cgd case 0: /* signal own process group */
412 1.32 cgd return (killpg1(cp, SCARG(uap, signum), 0, 0));
413 1.29 cgd default: /* negative explicit process group */
414 1.32 cgd return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
415 1.29 cgd }
416 1.29 cgd /* NOTREACHED */
417 1.29 cgd }
418 1.29 cgd
419 1.29 cgd /*
420 1.29 cgd * Common code for kill process group/broadcast kill.
421 1.29 cgd * cp is calling process.
422 1.29 cgd */
423 1.29 cgd killpg1(cp, signum, pgid, all)
424 1.29 cgd register struct proc *cp;
425 1.29 cgd int signum, pgid, all;
426 1.29 cgd {
427 1.29 cgd register struct proc *p;
428 1.29 cgd register struct pcred *pc = cp->p_cred;
429 1.29 cgd struct pgrp *pgrp;
430 1.29 cgd int nfound = 0;
431 1.29 cgd
432 1.29 cgd if (all)
433 1.29 cgd /*
434 1.29 cgd * broadcast
435 1.29 cgd */
436 1.31 mycroft for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
437 1.29 cgd if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
438 1.29 cgd p == cp || !CANSIGNAL(cp, pc, p, signum))
439 1.29 cgd continue;
440 1.29 cgd nfound++;
441 1.29 cgd if (signum)
442 1.29 cgd psignal(p, signum);
443 1.29 cgd }
444 1.29 cgd else {
445 1.29 cgd if (pgid == 0)
446 1.29 cgd /*
447 1.29 cgd * zero pgid means send to my process group.
448 1.29 cgd */
449 1.29 cgd pgrp = cp->p_pgrp;
450 1.29 cgd else {
451 1.29 cgd pgrp = pgfind(pgid);
452 1.29 cgd if (pgrp == NULL)
453 1.29 cgd return (ESRCH);
454 1.29 cgd }
455 1.31 mycroft for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
456 1.29 cgd if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
457 1.29 cgd p->p_stat == SZOMB ||
458 1.29 cgd !CANSIGNAL(cp, pc, p, signum))
459 1.29 cgd continue;
460 1.29 cgd nfound++;
461 1.29 cgd if (signum)
462 1.29 cgd psignal(p, signum);
463 1.29 cgd }
464 1.29 cgd }
465 1.29 cgd return (nfound ? 0 : ESRCH);
466 1.29 cgd }
467 1.29 cgd
468 1.29 cgd /*
469 1.29 cgd * Send a signal to a process group.
470 1.29 cgd */
471 1.29 cgd void
472 1.29 cgd gsignal(pgid, signum)
473 1.29 cgd int pgid, signum;
474 1.29 cgd {
475 1.29 cgd struct pgrp *pgrp;
476 1.29 cgd
477 1.29 cgd if (pgid && (pgrp = pgfind(pgid)))
478 1.29 cgd pgsignal(pgrp, signum, 0);
479 1.29 cgd }
480 1.29 cgd
481 1.29 cgd /*
482 1.29 cgd * Send a signal to a process group. If checktty is 1,
483 1.29 cgd * limit to members which have a controlling terminal.
484 1.29 cgd */
485 1.29 cgd void
486 1.29 cgd pgsignal(pgrp, signum, checkctty)
487 1.29 cgd struct pgrp *pgrp;
488 1.29 cgd int signum, checkctty;
489 1.29 cgd {
490 1.29 cgd register struct proc *p;
491 1.29 cgd
492 1.29 cgd if (pgrp)
493 1.31 mycroft for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
494 1.29 cgd if (checkctty == 0 || p->p_flag & P_CONTROLT)
495 1.29 cgd psignal(p, signum);
496 1.29 cgd }
497 1.29 cgd
498 1.29 cgd /*
499 1.29 cgd * Send a signal caused by a trap to the current process.
500 1.29 cgd * If it will be caught immediately, deliver it with correct code.
501 1.29 cgd * Otherwise, post it normally.
502 1.29 cgd */
503 1.29 cgd void
504 1.29 cgd trapsignal(p, signum, code)
505 1.29 cgd struct proc *p;
506 1.29 cgd register int signum;
507 1.33 cgd u_long code;
508 1.29 cgd {
509 1.29 cgd register struct sigacts *ps = p->p_sigacts;
510 1.29 cgd int mask;
511 1.29 cgd
512 1.29 cgd mask = sigmask(signum);
513 1.29 cgd if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
514 1.29 cgd (p->p_sigmask & mask) == 0) {
515 1.29 cgd p->p_stats->p_ru.ru_nsignals++;
516 1.29 cgd #ifdef KTRACE
517 1.29 cgd if (KTRPOINT(p, KTR_PSIG))
518 1.29 cgd ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
519 1.29 cgd p->p_sigmask, code);
520 1.29 cgd #endif
521 1.41 christos (*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum,
522 1.45 mycroft p->p_sigmask, code);
523 1.45 mycroft p->p_sigmask |= ps->ps_catchmask[signum];
524 1.45 mycroft if ((ps->ps_sigreset & mask) != 0) {
525 1.46 mycroft p->p_sigcatch &= ~mask;
526 1.45 mycroft if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
527 1.45 mycroft p->p_sigignore |= mask;
528 1.47 mycroft ps->ps_sigact[signum] = SIG_DFL;
529 1.45 mycroft }
530 1.29 cgd } else {
531 1.29 cgd ps->ps_code = code; /* XXX for core dump/debugger */
532 1.29 cgd psignal(p, signum);
533 1.29 cgd }
534 1.29 cgd }
535 1.29 cgd
536 1.29 cgd /*
537 1.29 cgd * Send the signal to the process. If the signal has an action, the action
538 1.29 cgd * is usually performed by the target process rather than the caller; we add
539 1.29 cgd * the signal to the set of pending signals for the process.
540 1.29 cgd *
541 1.29 cgd * Exceptions:
542 1.29 cgd * o When a stop signal is sent to a sleeping process that takes the
543 1.29 cgd * default action, the process is stopped without awakening it.
544 1.29 cgd * o SIGCONT restarts stopped processes (or puts them back to sleep)
545 1.29 cgd * regardless of the signal action (eg, blocked or ignored).
546 1.29 cgd *
547 1.29 cgd * Other ignored signals are discarded immediately.
548 1.29 cgd */
549 1.29 cgd void
550 1.29 cgd psignal(p, signum)
551 1.29 cgd register struct proc *p;
552 1.29 cgd register int signum;
553 1.29 cgd {
554 1.29 cgd register int s, prop;
555 1.29 cgd register sig_t action;
556 1.29 cgd int mask;
557 1.29 cgd
558 1.29 cgd if ((u_int)signum >= NSIG || signum == 0)
559 1.29 cgd panic("psignal signal number");
560 1.29 cgd mask = sigmask(signum);
561 1.29 cgd prop = sigprop[signum];
562 1.29 cgd
563 1.29 cgd /*
564 1.29 cgd * If proc is traced, always give parent a chance.
565 1.29 cgd */
566 1.29 cgd if (p->p_flag & P_TRACED)
567 1.29 cgd action = SIG_DFL;
568 1.29 cgd else {
569 1.29 cgd /*
570 1.29 cgd * If the signal is being ignored,
571 1.29 cgd * then we forget about it immediately.
572 1.29 cgd * (Note: we don't set SIGCONT in p_sigignore,
573 1.29 cgd * and if it is set to SIG_IGN,
574 1.29 cgd * action will be SIG_DFL here.)
575 1.29 cgd */
576 1.29 cgd if (p->p_sigignore & mask)
577 1.29 cgd return;
578 1.29 cgd if (p->p_sigmask & mask)
579 1.29 cgd action = SIG_HOLD;
580 1.29 cgd else if (p->p_sigcatch & mask)
581 1.29 cgd action = SIG_CATCH;
582 1.44 mycroft else {
583 1.29 cgd action = SIG_DFL;
584 1.44 mycroft
585 1.44 mycroft if (prop & SA_KILL && p->p_nice > NZERO)
586 1.44 mycroft p->p_nice = NZERO;
587 1.44 mycroft
588 1.44 mycroft /*
589 1.44 mycroft * If sending a tty stop signal to a member of an
590 1.44 mycroft * orphaned process group, discard the signal here if
591 1.44 mycroft * the action is default; don't stop the process below
592 1.44 mycroft * if sleeping, and don't clear any pending SIGCONT.
593 1.44 mycroft */
594 1.44 mycroft if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
595 1.44 mycroft return;
596 1.44 mycroft }
597 1.29 cgd }
598 1.29 cgd
599 1.29 cgd if (prop & SA_CONT)
600 1.29 cgd p->p_siglist &= ~stopsigmask;
601 1.29 cgd
602 1.44 mycroft if (prop & SA_STOP)
603 1.29 cgd p->p_siglist &= ~contsigmask;
604 1.44 mycroft
605 1.29 cgd p->p_siglist |= mask;
606 1.29 cgd
607 1.29 cgd /*
608 1.29 cgd * Defer further processing for signals which are held,
609 1.29 cgd * except that stopped processes must be continued by SIGCONT.
610 1.29 cgd */
611 1.29 cgd if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
612 1.29 cgd return;
613 1.29 cgd s = splhigh();
614 1.29 cgd switch (p->p_stat) {
615 1.29 cgd
616 1.29 cgd case SSLEEP:
617 1.29 cgd /*
618 1.29 cgd * If process is sleeping uninterruptibly
619 1.29 cgd * we can't interrupt the sleep... the signal will
620 1.29 cgd * be noticed when the process returns through
621 1.29 cgd * trap() or syscall().
622 1.29 cgd */
623 1.29 cgd if ((p->p_flag & P_SINTR) == 0)
624 1.29 cgd goto out;
625 1.29 cgd /*
626 1.29 cgd * Process is sleeping and traced... make it runnable
627 1.29 cgd * so it can discover the signal in issignal() and stop
628 1.29 cgd * for the parent.
629 1.29 cgd */
630 1.29 cgd if (p->p_flag & P_TRACED)
631 1.29 cgd goto run;
632 1.29 cgd /*
633 1.29 cgd * If SIGCONT is default (or ignored) and process is
634 1.29 cgd * asleep, we are finished; the process should not
635 1.29 cgd * be awakened.
636 1.29 cgd */
637 1.29 cgd if ((prop & SA_CONT) && action == SIG_DFL) {
638 1.29 cgd p->p_siglist &= ~mask;
639 1.29 cgd goto out;
640 1.29 cgd }
641 1.29 cgd /*
642 1.29 cgd * When a sleeping process receives a stop
643 1.29 cgd * signal, process immediately if possible.
644 1.29 cgd */
645 1.34 mycroft if ((prop & SA_STOP) && action == SIG_DFL) {
646 1.29 cgd /*
647 1.29 cgd * If a child holding parent blocked,
648 1.29 cgd * stopping could cause deadlock.
649 1.29 cgd */
650 1.29 cgd if (p->p_flag & P_PPWAIT)
651 1.29 cgd goto out;
652 1.29 cgd p->p_siglist &= ~mask;
653 1.29 cgd p->p_xstat = signum;
654 1.29 cgd if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
655 1.29 cgd psignal(p->p_pptr, SIGCHLD);
656 1.29 cgd stop(p);
657 1.29 cgd goto out;
658 1.34 mycroft }
659 1.34 mycroft /*
660 1.34 mycroft * All other (caught or default) signals
661 1.34 mycroft * cause the process to run.
662 1.34 mycroft */
663 1.34 mycroft goto runfast;
664 1.29 cgd /*NOTREACHED*/
665 1.29 cgd
666 1.29 cgd case SSTOP:
667 1.29 cgd /*
668 1.29 cgd * If traced process is already stopped,
669 1.29 cgd * then no further action is necessary.
670 1.29 cgd */
671 1.29 cgd if (p->p_flag & P_TRACED)
672 1.29 cgd goto out;
673 1.29 cgd
674 1.29 cgd /*
675 1.29 cgd * Kill signal always sets processes running.
676 1.29 cgd */
677 1.29 cgd if (signum == SIGKILL)
678 1.29 cgd goto runfast;
679 1.29 cgd
680 1.29 cgd if (prop & SA_CONT) {
681 1.29 cgd /*
682 1.29 cgd * If SIGCONT is default (or ignored), we continue the
683 1.29 cgd * process but don't leave the signal in p_siglist, as
684 1.29 cgd * it has no further action. If SIGCONT is held, we
685 1.29 cgd * continue the process and leave the signal in
686 1.29 cgd * p_siglist. If the process catches SIGCONT, let it
687 1.29 cgd * handle the signal itself. If it isn't waiting on
688 1.29 cgd * an event, then it goes back to run state.
689 1.29 cgd * Otherwise, process goes back to sleep state.
690 1.29 cgd */
691 1.29 cgd if (action == SIG_DFL)
692 1.29 cgd p->p_siglist &= ~mask;
693 1.29 cgd if (action == SIG_CATCH)
694 1.29 cgd goto runfast;
695 1.29 cgd if (p->p_wchan == 0)
696 1.29 cgd goto run;
697 1.29 cgd p->p_stat = SSLEEP;
698 1.29 cgd goto out;
699 1.29 cgd }
700 1.29 cgd
701 1.29 cgd if (prop & SA_STOP) {
702 1.29 cgd /*
703 1.29 cgd * Already stopped, don't need to stop again.
704 1.29 cgd * (If we did the shell could get confused.)
705 1.29 cgd */
706 1.29 cgd p->p_siglist &= ~mask; /* take it away */
707 1.29 cgd goto out;
708 1.29 cgd }
709 1.29 cgd
710 1.29 cgd /*
711 1.29 cgd * If process is sleeping interruptibly, then simulate a
712 1.29 cgd * wakeup so that when it is continued, it will be made
713 1.29 cgd * runnable and can look at the signal. But don't make
714 1.29 cgd * the process runnable, leave it stopped.
715 1.29 cgd */
716 1.29 cgd if (p->p_wchan && p->p_flag & P_SINTR)
717 1.29 cgd unsleep(p);
718 1.29 cgd goto out;
719 1.29 cgd
720 1.29 cgd default:
721 1.29 cgd /*
722 1.29 cgd * SRUN, SIDL, SZOMB do nothing with the signal,
723 1.29 cgd * other than kicking ourselves if we are running.
724 1.29 cgd * It will either never be noticed, or noticed very soon.
725 1.29 cgd */
726 1.29 cgd if (p == curproc)
727 1.29 cgd signotify(p);
728 1.29 cgd goto out;
729 1.29 cgd }
730 1.29 cgd /*NOTREACHED*/
731 1.29 cgd
732 1.29 cgd runfast:
733 1.29 cgd /*
734 1.29 cgd * Raise priority to at least PUSER.
735 1.29 cgd */
736 1.29 cgd if (p->p_priority > PUSER)
737 1.29 cgd p->p_priority = PUSER;
738 1.29 cgd run:
739 1.29 cgd setrunnable(p);
740 1.29 cgd out:
741 1.29 cgd splx(s);
742 1.29 cgd }
743 1.29 cgd
744 1.29 cgd /*
745 1.29 cgd * If the current process has received a signal (should be caught or cause
746 1.29 cgd * termination, should interrupt current syscall), return the signal number.
747 1.29 cgd * Stop signals with default action are processed immediately, then cleared;
748 1.29 cgd * they aren't returned. This is checked after each entry to the system for
749 1.29 cgd * a syscall or trap (though this can usually be done without calling issignal
750 1.29 cgd * by checking the pending signal masks in the CURSIG macro.) The normal call
751 1.29 cgd * sequence is
752 1.29 cgd *
753 1.29 cgd * while (signum = CURSIG(curproc))
754 1.29 cgd * postsig(signum);
755 1.29 cgd */
756 1.29 cgd int
757 1.29 cgd issignal(p)
758 1.29 cgd register struct proc *p;
759 1.29 cgd {
760 1.29 cgd register int signum, mask, prop;
761 1.29 cgd
762 1.29 cgd for (;;) {
763 1.29 cgd mask = p->p_siglist & ~p->p_sigmask;
764 1.29 cgd if (p->p_flag & P_PPWAIT)
765 1.29 cgd mask &= ~stopsigmask;
766 1.29 cgd if (mask == 0) /* no signal to send */
767 1.29 cgd return (0);
768 1.29 cgd signum = ffs((long)mask);
769 1.29 cgd mask = sigmask(signum);
770 1.42 mycroft p->p_siglist &= ~mask; /* take the signal! */
771 1.42 mycroft
772 1.29 cgd /*
773 1.29 cgd * We should see pending but ignored signals
774 1.29 cgd * only if P_TRACED was on when they were posted.
775 1.29 cgd */
776 1.42 mycroft if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0)
777 1.29 cgd continue;
778 1.42 mycroft
779 1.29 cgd if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
780 1.29 cgd /*
781 1.29 cgd * If traced, always stop, and stay
782 1.29 cgd * stopped until released by the debugger.
783 1.29 cgd */
784 1.29 cgd p->p_xstat = signum;
785 1.42 mycroft
786 1.29 cgd if (p->p_flag & P_FSTRACE) {
787 1.29 cgd #ifdef PROCFS
788 1.29 cgd /* procfs debugging */
789 1.29 cgd p->p_stat = SSTOP;
790 1.29 cgd wakeup((caddr_t)p);
791 1.29 cgd mi_switch();
792 1.29 cgd #else
793 1.29 cgd panic("procfs debugging");
794 1.29 cgd #endif
795 1.29 cgd } else {
796 1.29 cgd /* ptrace debugging */
797 1.29 cgd psignal(p->p_pptr, SIGCHLD);
798 1.29 cgd do {
799 1.29 cgd stop(p);
800 1.29 cgd mi_switch();
801 1.29 cgd } while (!trace_req(p) && p->p_flag & P_TRACED);
802 1.29 cgd }
803 1.29 cgd
804 1.29 cgd /*
805 1.42 mycroft * If we are no longer being traced, or the parent
806 1.42 mycroft * didn't give us a signal, look for more signals.
807 1.29 cgd */
808 1.42 mycroft if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
809 1.29 cgd continue;
810 1.29 cgd
811 1.29 cgd /*
812 1.42 mycroft * If the new signal is being masked, look for other
813 1.42 mycroft * signals.
814 1.29 cgd */
815 1.42 mycroft signum = p->p_xstat;
816 1.29 cgd mask = sigmask(signum);
817 1.42 mycroft if ((p->p_sigmask & mask) != 0)
818 1.29 cgd continue;
819 1.42 mycroft p->p_siglist &= ~mask; /* take the signal! */
820 1.29 cgd }
821 1.29 cgd
822 1.42 mycroft prop = sigprop[signum];
823 1.42 mycroft
824 1.29 cgd /*
825 1.29 cgd * Decide whether the signal should be returned.
826 1.29 cgd * Return the signal's number, or fall through
827 1.29 cgd * to clear it from the pending mask.
828 1.29 cgd */
829 1.33 cgd switch ((long)p->p_sigacts->ps_sigact[signum]) {
830 1.29 cgd
831 1.33 cgd case (long)SIG_DFL:
832 1.29 cgd /*
833 1.29 cgd * Don't take default actions on system processes.
834 1.29 cgd */
835 1.29 cgd if (p->p_pid <= 1) {
836 1.29 cgd #ifdef DIAGNOSTIC
837 1.29 cgd /*
838 1.29 cgd * Are you sure you want to ignore SIGSEGV
839 1.29 cgd * in init? XXX
840 1.29 cgd */
841 1.29 cgd printf("Process (pid %d) got signal %d\n",
842 1.29 cgd p->p_pid, signum);
843 1.29 cgd #endif
844 1.29 cgd break; /* == ignore */
845 1.29 cgd }
846 1.29 cgd /*
847 1.29 cgd * If there is a pending stop signal to process
848 1.29 cgd * with default action, stop here,
849 1.29 cgd * then clear the signal. However,
850 1.29 cgd * if process is member of an orphaned
851 1.29 cgd * process group, ignore tty stop signals.
852 1.29 cgd */
853 1.29 cgd if (prop & SA_STOP) {
854 1.29 cgd if (p->p_flag & P_TRACED ||
855 1.29 cgd (p->p_pgrp->pg_jobc == 0 &&
856 1.29 cgd prop & SA_TTYSTOP))
857 1.29 cgd break; /* == ignore */
858 1.29 cgd p->p_xstat = signum;
859 1.29 cgd if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
860 1.29 cgd psignal(p->p_pptr, SIGCHLD);
861 1.34 mycroft stop(p);
862 1.29 cgd mi_switch();
863 1.29 cgd break;
864 1.29 cgd } else if (prop & SA_IGNORE) {
865 1.29 cgd /*
866 1.29 cgd * Except for SIGCONT, shouldn't get here.
867 1.29 cgd * Default action is to ignore; drop it.
868 1.29 cgd */
869 1.29 cgd break; /* == ignore */
870 1.29 cgd } else
871 1.42 mycroft goto keep;
872 1.29 cgd /*NOTREACHED*/
873 1.29 cgd
874 1.33 cgd case (long)SIG_IGN:
875 1.29 cgd /*
876 1.29 cgd * Masking above should prevent us ever trying
877 1.29 cgd * to take action on an ignored signal other
878 1.29 cgd * than SIGCONT, unless process is traced.
879 1.29 cgd */
880 1.29 cgd if ((prop & SA_CONT) == 0 &&
881 1.29 cgd (p->p_flag & P_TRACED) == 0)
882 1.29 cgd printf("issignal\n");
883 1.29 cgd break; /* == ignore */
884 1.29 cgd
885 1.29 cgd default:
886 1.29 cgd /*
887 1.29 cgd * This signal has an action, let
888 1.29 cgd * postsig() process it.
889 1.29 cgd */
890 1.42 mycroft goto keep;
891 1.29 cgd }
892 1.29 cgd }
893 1.29 cgd /* NOTREACHED */
894 1.42 mycroft
895 1.42 mycroft keep:
896 1.42 mycroft p->p_siglist |= mask; /* leave the signal for later */
897 1.42 mycroft return (signum);
898 1.29 cgd }
899 1.29 cgd
900 1.29 cgd /*
901 1.29 cgd * Put the argument process into the stopped state and notify the parent
902 1.29 cgd * via wakeup. Signals are handled elsewhere. The process must not be
903 1.29 cgd * on the run queue.
904 1.29 cgd */
905 1.29 cgd void
906 1.29 cgd stop(p)
907 1.29 cgd register struct proc *p;
908 1.29 cgd {
909 1.29 cgd
910 1.29 cgd p->p_stat = SSTOP;
911 1.29 cgd p->p_flag &= ~P_WAITED;
912 1.29 cgd wakeup((caddr_t)p->p_pptr);
913 1.29 cgd }
914 1.29 cgd
915 1.29 cgd /*
916 1.29 cgd * Take the action for the specified signal
917 1.29 cgd * from the current set of pending signals.
918 1.29 cgd */
919 1.29 cgd void
920 1.29 cgd postsig(signum)
921 1.29 cgd register int signum;
922 1.29 cgd {
923 1.29 cgd register struct proc *p = curproc;
924 1.29 cgd register struct sigacts *ps = p->p_sigacts;
925 1.29 cgd register sig_t action;
926 1.33 cgd u_long code;
927 1.33 cgd int mask, returnmask;
928 1.29 cgd
929 1.29 cgd #ifdef DIAGNOSTIC
930 1.29 cgd if (signum == 0)
931 1.29 cgd panic("postsig");
932 1.29 cgd #endif
933 1.29 cgd mask = sigmask(signum);
934 1.29 cgd p->p_siglist &= ~mask;
935 1.29 cgd action = ps->ps_sigact[signum];
936 1.29 cgd #ifdef KTRACE
937 1.29 cgd if (KTRPOINT(p, KTR_PSIG))
938 1.29 cgd ktrpsig(p->p_tracep,
939 1.29 cgd signum, action, ps->ps_flags & SAS_OLDMASK ?
940 1.29 cgd ps->ps_oldmask : p->p_sigmask, 0);
941 1.29 cgd #endif
942 1.29 cgd if (action == SIG_DFL) {
943 1.29 cgd /*
944 1.29 cgd * Default action, where the default is to kill
945 1.29 cgd * the process. (Other cases were ignored above.)
946 1.29 cgd */
947 1.29 cgd sigexit(p, signum);
948 1.29 cgd /* NOTREACHED */
949 1.29 cgd } else {
950 1.29 cgd /*
951 1.29 cgd * If we get here, the signal must be caught.
952 1.29 cgd */
953 1.29 cgd #ifdef DIAGNOSTIC
954 1.29 cgd if (action == SIG_IGN || (p->p_sigmask & mask))
955 1.29 cgd panic("postsig action");
956 1.29 cgd #endif
957 1.29 cgd /*
958 1.29 cgd * Set the new mask value and also defer further
959 1.29 cgd * occurences of this signal.
960 1.29 cgd *
961 1.29 cgd * Special case: user has done a sigpause. Here the
962 1.29 cgd * current mask is not of interest, but rather the
963 1.29 cgd * mask from before the sigpause is what we want
964 1.29 cgd * restored after the signal processing is completed.
965 1.29 cgd */
966 1.29 cgd (void) splhigh();
967 1.29 cgd if (ps->ps_flags & SAS_OLDMASK) {
968 1.29 cgd returnmask = ps->ps_oldmask;
969 1.29 cgd ps->ps_flags &= ~SAS_OLDMASK;
970 1.29 cgd } else
971 1.29 cgd returnmask = p->p_sigmask;
972 1.45 mycroft p->p_sigmask |= ps->ps_catchmask[signum];
973 1.45 mycroft if ((ps->ps_sigreset & mask) != 0) {
974 1.46 mycroft p->p_sigcatch &= ~mask;
975 1.45 mycroft if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
976 1.45 mycroft p->p_sigignore |= mask;
977 1.47 mycroft ps->ps_sigact[signum] = SIG_DFL;
978 1.45 mycroft }
979 1.29 cgd (void) spl0();
980 1.29 cgd p->p_stats->p_ru.ru_nsignals++;
981 1.29 cgd if (ps->ps_sig != signum) {
982 1.29 cgd code = 0;
983 1.29 cgd } else {
984 1.29 cgd code = ps->ps_code;
985 1.29 cgd ps->ps_code = 0;
986 1.29 cgd }
987 1.41 christos (*p->p_emul->e_sendsig)(action, signum, returnmask, code);
988 1.29 cgd }
989 1.29 cgd }
990 1.29 cgd
991 1.29 cgd /*
992 1.29 cgd * Kill the current process for stated reason.
993 1.29 cgd */
994 1.29 cgd killproc(p, why)
995 1.29 cgd struct proc *p;
996 1.29 cgd char *why;
997 1.29 cgd {
998 1.29 cgd
999 1.29 cgd log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1000 1.29 cgd uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1001 1.29 cgd psignal(p, SIGKILL);
1002 1.29 cgd }
1003 1.29 cgd
1004 1.29 cgd /*
1005 1.29 cgd * Force the current process to exit with the specified signal, dumping core
1006 1.29 cgd * if appropriate. We bypass the normal tests for masked and caught signals,
1007 1.29 cgd * allowing unrecoverable failures to terminate the process without changing
1008 1.29 cgd * signal state. Mark the accounting record with the signal termination.
1009 1.29 cgd * If dumping core, save the signal number for the debugger. Calls exit and
1010 1.29 cgd * does not return.
1011 1.29 cgd */
1012 1.29 cgd int
1013 1.29 cgd sigexit(p, signum)
1014 1.29 cgd register struct proc *p;
1015 1.29 cgd int signum;
1016 1.29 cgd {
1017 1.29 cgd
1018 1.29 cgd p->p_acflag |= AXSIG;
1019 1.29 cgd if (sigprop[signum] & SA_CORE) {
1020 1.29 cgd p->p_sigacts->ps_sig = signum;
1021 1.29 cgd if (coredump(p) == 0)
1022 1.29 cgd signum |= WCOREFLAG;
1023 1.29 cgd }
1024 1.29 cgd exit1(p, W_EXITCODE(0, signum));
1025 1.29 cgd /* NOTREACHED */
1026 1.29 cgd }
1027 1.29 cgd
1028 1.29 cgd /*
1029 1.29 cgd * Dump core, into a file named "progname.core", unless the process was
1030 1.29 cgd * setuid/setgid.
1031 1.29 cgd */
1032 1.29 cgd int
1033 1.29 cgd coredump(p)
1034 1.29 cgd register struct proc *p;
1035 1.29 cgd {
1036 1.29 cgd register struct vnode *vp;
1037 1.29 cgd register struct pcred *pcred = p->p_cred;
1038 1.29 cgd register struct ucred *cred = pcred->pc_ucred;
1039 1.29 cgd register struct vmspace *vm = p->p_vmspace;
1040 1.29 cgd struct nameidata nd;
1041 1.29 cgd struct vattr vattr;
1042 1.29 cgd int error, error1;
1043 1.29 cgd char name[MAXCOMLEN+6]; /* progname.core */
1044 1.29 cgd struct core core;
1045 1.29 cgd
1046 1.29 cgd if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
1047 1.29 cgd return (EFAULT);
1048 1.30 deraadt if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1049 1.29 cgd p->p_rlimit[RLIMIT_CORE].rlim_cur)
1050 1.29 cgd return (EFAULT);
1051 1.29 cgd sprintf(name, "%s.core", p->p_comm);
1052 1.29 cgd NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1053 1.49 mycroft if (error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR))
1054 1.29 cgd return (error);
1055 1.29 cgd vp = nd.ni_vp;
1056 1.29 cgd
1057 1.29 cgd /* Don't dump to non-regular files or files with links. */
1058 1.29 cgd if (vp->v_type != VREG ||
1059 1.29 cgd VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1060 1.29 cgd error = EFAULT;
1061 1.29 cgd goto out;
1062 1.29 cgd }
1063 1.29 cgd VATTR_NULL(&vattr);
1064 1.29 cgd vattr.va_size = 0;
1065 1.37 mycroft VOP_LEASE(vp, p, cred, LEASE_WRITE);
1066 1.29 cgd VOP_SETATTR(vp, &vattr, cred, p);
1067 1.29 cgd p->p_acflag |= ACORE;
1068 1.29 cgd bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1069 1.29 cgd fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1070 1.29 cgd
1071 1.29 cgd core.c_midmag = 0;
1072 1.29 cgd strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1073 1.29 cgd core.c_nseg = 0;
1074 1.29 cgd core.c_signo = p->p_sigacts->ps_sig;
1075 1.29 cgd core.c_ucode = p->p_sigacts->ps_code;
1076 1.29 cgd core.c_cpusize = 0;
1077 1.29 cgd core.c_tsize = (u_long)ctob(vm->vm_tsize);
1078 1.29 cgd core.c_dsize = (u_long)ctob(vm->vm_dsize);
1079 1.29 cgd core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1080 1.29 cgd error = cpu_coredump(p, vp, cred, &core);
1081 1.29 cgd if (error)
1082 1.29 cgd goto out;
1083 1.29 cgd if (core.c_midmag == 0) {
1084 1.29 cgd /* XXX
1085 1.29 cgd * cpu_coredump() didn't bother to set the magic; assume
1086 1.29 cgd * this is a request to do a traditional dump. cpu_coredump()
1087 1.29 cgd * is still responsible for setting sensible values in
1088 1.29 cgd * the core header.
1089 1.29 cgd */
1090 1.29 cgd if (core.c_cpusize == 0)
1091 1.30 deraadt core.c_cpusize = USPACE; /* Just in case */
1092 1.29 cgd error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1093 1.29 cgd (int)core.c_dsize,
1094 1.29 cgd (off_t)core.c_cpusize, UIO_USERSPACE,
1095 1.29 cgd IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1096 1.29 cgd if (error)
1097 1.29 cgd goto out;
1098 1.29 cgd error = vn_rdwr(UIO_WRITE, vp,
1099 1.29 cgd (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1100 1.29 cgd core.c_ssize,
1101 1.29 cgd (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1102 1.29 cgd IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1103 1.29 cgd } else {
1104 1.29 cgd /*
1105 1.29 cgd * vm_coredump() spits out all appropriate segments.
1106 1.29 cgd * All that's left to do is to write the core header.
1107 1.29 cgd */
1108 1.29 cgd error = vm_coredump(p, vp, cred, &core);
1109 1.29 cgd if (error)
1110 1.29 cgd goto out;
1111 1.29 cgd error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1112 1.29 cgd (int)core.c_hdrsize, (off_t)0,
1113 1.29 cgd UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1114 1.29 cgd }
1115 1.29 cgd out:
1116 1.29 cgd VOP_UNLOCK(vp);
1117 1.29 cgd error1 = vn_close(vp, FWRITE, cred, p);
1118 1.29 cgd if (error == 0)
1119 1.29 cgd error = error1;
1120 1.29 cgd return (error);
1121 1.29 cgd }
1122 1.29 cgd
1123 1.29 cgd /*
1124 1.29 cgd * Nonexistent system call-- signal process (may want to handle it).
1125 1.29 cgd * Flag error in case process won't see signal immediately (blocked or ignored).
1126 1.29 cgd */
1127 1.29 cgd /* ARGSUSED */
1128 1.29 cgd int
1129 1.50 mycroft sys_nosys(p, v, retval)
1130 1.29 cgd struct proc *p;
1131 1.50 mycroft void *v;
1132 1.32 cgd register_t *retval;
1133 1.29 cgd {
1134 1.29 cgd
1135 1.29 cgd psignal(p, SIGSYS);
1136 1.36 cgd return (ENOSYS);
1137 1.29 cgd }
1138