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