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