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