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