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