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