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