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 | * |
adb35f79 | 12 | * @(#)kern_sig.c 8.5 (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 | */ |
cf5ef508 KB |
528 | for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { |
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 | } |
8fe87cbb | 547 | for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) { |
cf5ef508 | 548 | if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || |
802236f4 KB |
549 | p->p_stat == SZOMB || |
550 | !CANSIGNAL(cp, pc, p, signum)) | |
8fe87cbb | 551 | continue; |
8429d022 | 552 | nfound++; |
802236f4 KB |
553 | if (signum) |
554 | psignal(p, signum); | |
7713be67 | 555 | } |
687880f9 | 556 | } |
8429d022 | 557 | return (nfound ? 0 : ESRCH); |
4147b3f6 | 558 | } |
687880f9 | 559 | |
e635a749 | 560 | /* |
802236f4 | 561 | * Send a signal to a process group. |
687880f9 | 562 | */ |
8429d022 | 563 | void |
802236f4 KB |
564 | gsignal(pgid, signum) |
565 | int pgid, signum; | |
8fe87cbb | 566 | { |
f4af9a5b | 567 | struct pgrp *pgrp; |
8fe87cbb | 568 | |
f4af9a5b | 569 | if (pgid && (pgrp = pgfind(pgid))) |
802236f4 | 570 | pgsignal(pgrp, signum, 0); |
8fe87cbb | 571 | } |
e635a749 | 572 | |
22585170 | 573 | /* |
802236f4 KB |
574 | * Send a signal to a process group. If checktty is 1, |
575 | * limit to members which have a controlling terminal. | |
22585170 | 576 | */ |
8429d022 | 577 | void |
802236f4 | 578 | pgsignal(pgrp, signum, checkctty) |
f4af9a5b | 579 | struct pgrp *pgrp; |
802236f4 | 580 | int signum, checkctty; |
687880f9 BJ |
581 | { |
582 | register struct proc *p; | |
583 | ||
22585170 MT |
584 | if (pgrp) |
585 | for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) | |
cf5ef508 | 586 | if (checkctty == 0 || p->p_flag & P_CONTROLT) |
802236f4 | 587 | psignal(p, signum); |
f4af9a5b MK |
588 | } |
589 | ||
590 | /* | |
591 | * Send a signal caused by a trap to the current process. | |
592 | * If it will be caught immediately, deliver it with correct code. | |
593 | * Otherwise, post it normally. | |
594 | */ | |
8429d022 | 595 | void |
802236f4 | 596 | trapsignal(p, signum, code) |
8429d022 | 597 | struct proc *p; |
802236f4 KB |
598 | register int signum; |
599 | u_int code; | |
f4af9a5b | 600 | { |
8429d022 | 601 | register struct sigacts *ps = p->p_sigacts; |
f4af9a5b MK |
602 | int mask; |
603 | ||
802236f4 | 604 | mask = sigmask(signum); |
cf5ef508 | 605 | if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 && |
f4af9a5b | 606 | (p->p_sigmask & mask) == 0) { |
8429d022 | 607 | p->p_stats->p_ru.ru_nsignals++; |
22585170 MT |
608 | #ifdef KTRACE |
609 | if (KTRPOINT(p, KTR_PSIG)) | |
802236f4 | 610 | ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum], |
22585170 MT |
611 | p->p_sigmask, code); |
612 | #endif | |
802236f4 KB |
613 | sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, code); |
614 | p->p_sigmask |= ps->ps_catchmask[signum] | mask; | |
f4af9a5b | 615 | } else { |
8429d022 | 616 | ps->ps_code = code; /* XXX for core dump/debugger */ |
802236f4 | 617 | psignal(p, signum); |
f4af9a5b | 618 | } |
687880f9 BJ |
619 | } |
620 | ||
621 | /* | |
802236f4 KB |
622 | * Send the signal to the process. If the signal has an action, the action |
623 | * is usually performed by the target process rather than the caller; we add | |
8429d022 | 624 | * the signal to the set of pending signals for the process. |
802236f4 | 625 | * |
22585170 | 626 | * Exceptions: |
802236f4 KB |
627 | * o When a stop signal is sent to a sleeping process that takes the |
628 | * default action, the process is stopped without awakening it. | |
22585170 MT |
629 | * o SIGCONT restarts stopped processes (or puts them back to sleep) |
630 | * regardless of the signal action (eg, blocked or ignored). | |
802236f4 | 631 | * |
22585170 | 632 | * Other ignored signals are discarded immediately. |
687880f9 | 633 | */ |
8429d022 | 634 | void |
802236f4 | 635 | psignal(p, signum) |
687880f9 | 636 | register struct proc *p; |
802236f4 | 637 | register int signum; |
687880f9 | 638 | { |
8429d022 | 639 | register int s, prop; |
f4af9a5b | 640 | register sig_t action; |
feff6b5a | 641 | int mask; |
687880f9 | 642 | |
802236f4 KB |
643 | if ((u_int)signum >= NSIG || signum == 0) |
644 | panic("psignal signal number"); | |
645 | mask = sigmask(signum); | |
646 | prop = sigprop[signum]; | |
687880f9 BJ |
647 | |
648 | /* | |
649 | * If proc is traced, always give parent a chance. | |
687880f9 | 650 | */ |
cf5ef508 | 651 | if (p->p_flag & P_TRACED) |
687880f9 BJ |
652 | action = SIG_DFL; |
653 | else { | |
687880f9 | 654 | /* |
dd012d1e SL |
655 | * If the signal is being ignored, |
656 | * then we forget about it immediately. | |
f4af9a5b MK |
657 | * (Note: we don't set SIGCONT in p_sigignore, |
658 | * and if it is set to SIG_IGN, | |
659 | * action will be SIG_DFL here.) | |
687880f9 | 660 | */ |
feff6b5a | 661 | if (p->p_sigignore & mask) |
687880f9 | 662 | return; |
feff6b5a | 663 | if (p->p_sigmask & mask) |
dd012d1e | 664 | action = SIG_HOLD; |
feff6b5a | 665 | else if (p->p_sigcatch & mask) |
dd012d1e | 666 | action = SIG_CATCH; |
e51c8e4a | 667 | else |
dd012d1e | 668 | action = SIG_DFL; |
687880f9 | 669 | } |
687880f9 | 670 | |
90f62fff | 671 | if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && |
cf5ef508 | 672 | (p->p_flag & P_TRACED) == 0) |
8429d022 | 673 | p->p_nice = NZERO; |
687880f9 | 674 | |
8429d022 | 675 | if (prop & SA_CONT) |
cf5ef508 | 676 | p->p_siglist &= ~stopsigmask; |
f4af9a5b | 677 | |
8429d022 | 678 | if (prop & SA_STOP) { |
faf9c963 MK |
679 | /* |
680 | * If sending a tty stop signal to a member of an orphaned | |
681 | * process group, discard the signal here if the action | |
682 | * is default; don't stop the process below if sleeping, | |
683 | * and don't clear any pending SIGCONT. | |
684 | */ | |
8429d022 MK |
685 | if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && |
686 | action == SIG_DFL) | |
9db58063 | 687 | return; |
cf5ef508 | 688 | p->p_siglist &= ~contsigmask; |
687880f9 | 689 | } |
cf5ef508 | 690 | p->p_siglist |= mask; |
f4af9a5b | 691 | |
687880f9 | 692 | /* |
f4af9a5b MK |
693 | * Defer further processing for signals which are held, |
694 | * except that stopped processes must be continued by SIGCONT. | |
687880f9 | 695 | */ |
8429d022 | 696 | if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) |
687880f9 | 697 | return; |
feff6b5a | 698 | s = splhigh(); |
687880f9 BJ |
699 | switch (p->p_stat) { |
700 | ||
701 | case SSLEEP: | |
702 | /* | |
22585170 | 703 | * If process is sleeping uninterruptibly |
687880f9 BJ |
704 | * we can't interrupt the sleep... the signal will |
705 | * be noticed when the process returns through | |
706 | * trap() or syscall(). | |
707 | */ | |
cf5ef508 | 708 | if ((p->p_flag & P_SINTR) == 0) |
687880f9 BJ |
709 | goto out; |
710 | /* | |
711 | * Process is sleeping and traced... make it runnable | |
cf5ef508 | 712 | * so it can discover the signal in issignal() and stop |
687880f9 BJ |
713 | * for the parent. |
714 | */ | |
cf5ef508 | 715 | if (p->p_flag & P_TRACED) |
687880f9 | 716 | goto run; |
90f62fff KM |
717 | /* |
718 | * If SIGCONT is default (or ignored) and process is | |
719 | * asleep, we are finished; the process should not | |
720 | * be awakened. | |
721 | */ | |
722 | if ((prop & SA_CONT) && action == SIG_DFL) { | |
cf5ef508 | 723 | p->p_siglist &= ~mask; |
90f62fff KM |
724 | goto out; |
725 | } | |
f4af9a5b MK |
726 | /* |
727 | * When a sleeping process receives a stop | |
728 | * signal, process immediately if possible. | |
729 | * All other (caught or default) signals | |
730 | * cause the process to run. | |
731 | */ | |
8429d022 | 732 | if (prop & SA_STOP) { |
687880f9 | 733 | if (action != SIG_DFL) |
f4af9a5b | 734 | goto runfast; |
687880f9 | 735 | /* |
8429d022 MK |
736 | * If a child holding parent blocked, |
737 | * stopping could cause deadlock. | |
687880f9 | 738 | */ |
cf5ef508 | 739 | if (p->p_flag & P_PPWAIT) |
687880f9 | 740 | goto out; |
cf5ef508 | 741 | p->p_siglist &= ~mask; |
802236f4 | 742 | p->p_xstat = signum; |
cf5ef508 | 743 | if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) |
f4af9a5b | 744 | psignal(p->p_pptr, SIGCHLD); |
687880f9 BJ |
745 | stop(p); |
746 | goto out; | |
f4af9a5b MK |
747 | } else |
748 | goto runfast; | |
687880f9 BJ |
749 | /*NOTREACHED*/ |
750 | ||
751 | case SSTOP: | |
752 | /* | |
753 | * If traced process is already stopped, | |
754 | * then no further action is necessary. | |
755 | */ | |
cf5ef508 | 756 | if (p->p_flag & P_TRACED) |
687880f9 | 757 | goto out; |
687880f9 | 758 | |
8429d022 MK |
759 | /* |
760 | * Kill signal always sets processes running. | |
761 | */ | |
802236f4 | 762 | if (signum == SIGKILL) |
f4af9a5b | 763 | goto runfast; |
687880f9 | 764 | |
8429d022 | 765 | if (prop & SA_CONT) { |
687880f9 | 766 | /* |
cf5ef508 KB |
767 | * If SIGCONT is default (or ignored), we continue the |
768 | * process but don't leave the signal in p_siglist, as | |
769 | * it has no further action. If SIGCONT is held, we | |
770 | * continue the process and leave the signal in | |
771 | * p_siglist. If the process catches SIGCONT, let it | |
772 | * handle the signal itself. If it isn't waiting on | |
687880f9 BJ |
773 | * an event, then it goes back to run state. |
774 | * Otherwise, process goes back to sleep state. | |
775 | */ | |
f4af9a5b | 776 | if (action == SIG_DFL) |
cf5ef508 | 777 | p->p_siglist &= ~mask; |
f4af9a5b MK |
778 | if (action == SIG_CATCH) |
779 | goto runfast; | |
780 | if (p->p_wchan == 0) | |
687880f9 BJ |
781 | goto run; |
782 | p->p_stat = SSLEEP; | |
783 | goto out; | |
8429d022 | 784 | } |
687880f9 | 785 | |
8429d022 | 786 | if (prop & SA_STOP) { |
687880f9 BJ |
787 | /* |
788 | * Already stopped, don't need to stop again. | |
789 | * (If we did the shell could get confused.) | |
790 | */ | |
cf5ef508 | 791 | p->p_siglist &= ~mask; /* take it away */ |
687880f9 | 792 | goto out; |
687880f9 | 793 | } |
8429d022 MK |
794 | |
795 | /* | |
cb84e0ab KB |
796 | * If process is sleeping interruptibly, then simulate a |
797 | * wakeup so that when it is continued, it will be made | |
798 | * runnable and can look at the signal. But don't make | |
799 | * the process runnable, leave it stopped. | |
8429d022 | 800 | */ |
cf5ef508 | 801 | if (p->p_wchan && p->p_flag & P_SINTR) |
8429d022 MK |
802 | unsleep(p); |
803 | goto out; | |
687880f9 BJ |
804 | |
805 | default: | |
806 | /* | |
807 | * SRUN, SIDL, SZOMB do nothing with the signal, | |
808 | * other than kicking ourselves if we are running. | |
809 | * It will either never be noticed, or noticed very soon. | |
810 | */ | |
35493b0f | 811 | if (p == curproc) |
8e6cd807 | 812 | signotify(p); |
687880f9 BJ |
813 | goto out; |
814 | } | |
815 | /*NOTREACHED*/ | |
f4af9a5b MK |
816 | |
817 | runfast: | |
687880f9 BJ |
818 | /* |
819 | * Raise priority to at least PUSER. | |
820 | */ | |
cf5ef508 KB |
821 | if (p->p_priority > PUSER) |
822 | p->p_priority = PUSER; | |
f4af9a5b | 823 | run: |
cb84e0ab | 824 | setrunnable(p); |
687880f9 BJ |
825 | out: |
826 | splx(s); | |
827 | } | |
828 | ||
829 | /* | |
802236f4 KB |
830 | * If the current process has received a signal (should be caught or cause |
831 | * termination, should interrupt current syscall), return the signal number. | |
832 | * Stop signals with default action are processed immediately, then cleared; | |
833 | * they aren't returned. This is checked after each entry to the system for | |
cf5ef508 | 834 | * a syscall or trap (though this can usually be done without calling issignal |
802236f4 KB |
835 | * by checking the pending signal masks in the CURSIG macro.) The normal call |
836 | * sequence is | |
8429d022 | 837 | * |
802236f4 | 838 | * while (signum = CURSIG(curproc)) |
cf5ef508 | 839 | * postsig(signum); |
687880f9 | 840 | */ |
cf5ef508 | 841 | issignal(p) |
8429d022 | 842 | register struct proc *p; |
687880f9 | 843 | { |
802236f4 | 844 | register int signum, mask, prop; |
687880f9 | 845 | |
687880f9 | 846 | for (;;) { |
cf5ef508 KB |
847 | mask = p->p_siglist & ~p->p_sigmask; |
848 | if (p->p_flag & P_PPWAIT) | |
f4af9a5b | 849 | mask &= ~stopsigmask; |
22585170 MT |
850 | if (mask == 0) /* no signal to send */ |
851 | return (0); | |
802236f4 KB |
852 | signum = ffs((long)mask); |
853 | mask = sigmask(signum); | |
854 | prop = sigprop[signum]; | |
22585170 MT |
855 | /* |
856 | * We should see pending but ignored signals | |
cf5ef508 | 857 | * only if P_TRACED was on when they were posted. |
22585170 | 858 | */ |
cf5ef508 KB |
859 | if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) { |
860 | p->p_siglist &= ~mask; | |
22585170 MT |
861 | continue; |
862 | } | |
cf5ef508 | 863 | if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { |
687880f9 BJ |
864 | /* |
865 | * If traced, always stop, and stay | |
866 | * stopped until released by the parent. | |
867 | */ | |
802236f4 | 868 | p->p_xstat = signum; |
ad6c75cb | 869 | psignal(p->p_pptr, SIGCHLD); |
687880f9 BJ |
870 | do { |
871 | stop(p); | |
cf5ef508 KB |
872 | mi_switch(); |
873 | } while (!trace_req(p) && p->p_flag & P_TRACED); | |
687880f9 BJ |
874 | |
875 | /* | |
cf5ef508 KB |
876 | * If the traced bit got turned off, go back up |
877 | * to the top to rescan signals. This ensures | |
878 | * that p_sig* and ps_sigact are consistent. | |
687880f9 | 879 | */ |
cf5ef508 | 880 | if ((p->p_flag & P_TRACED) == 0) |
687880f9 | 881 | continue; |
687880f9 BJ |
882 | |
883 | /* | |
884 | * If parent wants us to take the signal, | |
19dec745 | 885 | * then it will leave it in p->p_xstat; |
687880f9 BJ |
886 | * otherwise we just look for signals again. |
887 | */ | |
cf5ef508 | 888 | p->p_siglist &= ~mask; /* clear the old signal */ |
802236f4 KB |
889 | signum = p->p_xstat; |
890 | if (signum == 0) | |
687880f9 | 891 | continue; |
f99e4a3a SL |
892 | |
893 | /* | |
cf5ef508 KB |
894 | * Put the new signal into p_siglist. If the |
895 | * signal is being masked, look for other signals. | |
f99e4a3a | 896 | */ |
802236f4 | 897 | mask = sigmask(signum); |
cf5ef508 | 898 | p->p_siglist |= mask; |
22585170 | 899 | if (p->p_sigmask & mask) |
f99e4a3a | 900 | continue; |
687880f9 | 901 | } |
22585170 MT |
902 | |
903 | /* | |
904 | * Decide whether the signal should be returned. | |
905 | * Return the signal's number, or fall through | |
906 | * to clear it from the pending mask. | |
907 | */ | |
802236f4 | 908 | switch ((int)p->p_sigacts->ps_sigact[signum]) { |
687880f9 BJ |
909 | |
910 | case SIG_DFL: | |
911 | /* | |
912 | * Don't take default actions on system processes. | |
913 | */ | |
fc080d8d RC |
914 | if (p->p_pid <= 1) { |
915 | #ifdef DIAGNOSTIC | |
916 | /* | |
917 | * Are you sure you want to ignore SIGSEGV | |
918 | * in init? XXX | |
919 | */ | |
920 | printf("Process (pid %d) got signal %d\n", | |
802236f4 | 921 | p->p_pid, signum); |
fc080d8d | 922 | #endif |
22585170 | 923 | break; /* == ignore */ |
fc080d8d | 924 | } |
22585170 MT |
925 | /* |
926 | * If there is a pending stop signal to process | |
927 | * with default action, stop here, | |
e51c8e4a MK |
928 | * then clear the signal. However, |
929 | * if process is member of an orphaned | |
930 | * process group, ignore tty stop signals. | |
22585170 | 931 | */ |
8429d022 | 932 | if (prop & SA_STOP) { |
cf5ef508 | 933 | if (p->p_flag & P_TRACED || |
e51c8e4a | 934 | (p->p_pgrp->pg_jobc == 0 && |
8429d022 | 935 | prop & SA_TTYSTOP)) |
22585170 | 936 | break; /* == ignore */ |
802236f4 | 937 | p->p_xstat = signum; |
687880f9 | 938 | stop(p); |
cf5ef508 | 939 | if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) |
f4af9a5b | 940 | psignal(p->p_pptr, SIGCHLD); |
cf5ef508 | 941 | mi_switch(); |
22585170 | 942 | break; |
8429d022 | 943 | } else if (prop & SA_IGNORE) { |
687880f9 | 944 | /* |
f4af9a5b MK |
945 | * Except for SIGCONT, shouldn't get here. |
946 | * Default action is to ignore; drop it. | |
687880f9 | 947 | */ |
22585170 | 948 | break; /* == ignore */ |
f4af9a5b | 949 | } else |
802236f4 | 950 | return (signum); |
687880f9 BJ |
951 | /*NOTREACHED*/ |
952 | ||
687880f9 BJ |
953 | case SIG_IGN: |
954 | /* | |
f4af9a5b MK |
955 | * Masking above should prevent us ever trying |
956 | * to take action on an ignored signal other | |
957 | * than SIGCONT, unless process is traced. | |
687880f9 | 958 | */ |
cf5ef508 KB |
959 | if ((prop & SA_CONT) == 0 && |
960 | (p->p_flag & P_TRACED) == 0) | |
961 | printf("issignal\n"); | |
22585170 | 962 | break; /* == ignore */ |
687880f9 BJ |
963 | |
964 | default: | |
965 | /* | |
966 | * This signal has an action, let | |
cf5ef508 | 967 | * postsig() process it. |
687880f9 | 968 | */ |
802236f4 | 969 | return (signum); |
687880f9 | 970 | } |
cf5ef508 | 971 | p->p_siglist &= ~mask; /* take the signal! */ |
687880f9 | 972 | } |
22585170 | 973 | /* NOTREACHED */ |
687880f9 BJ |
974 | } |
975 | ||
687880f9 | 976 | /* |
802236f4 KB |
977 | * Put the argument process into the stopped state and notify the parent |
978 | * via wakeup. Signals are handled elsewhere. The process must not be | |
979 | * on the run queue. | |
687880f9 BJ |
980 | */ |
981 | stop(p) | |
982 | register struct proc *p; | |
983 | { | |
984 | ||
985 | p->p_stat = SSTOP; | |
cf5ef508 | 986 | p->p_flag &= ~P_WAITED; |
687880f9 | 987 | wakeup((caddr_t)p->p_pptr); |
687880f9 BJ |
988 | } |
989 | ||
990 | /* | |
8429d022 MK |
991 | * Take the action for the specified signal |
992 | * from the current set of pending signals. | |
687880f9 | 993 | */ |
8429d022 | 994 | void |
cf5ef508 | 995 | postsig(signum) |
802236f4 | 996 | register int signum; |
687880f9 | 997 | { |
8429d022 MK |
998 | register struct proc *p = curproc; |
999 | register struct sigacts *ps = p->p_sigacts; | |
f4af9a5b | 1000 | register sig_t action; |
8429d022 | 1001 | int mask, returnmask; |
f4af9a5b | 1002 | |
22585170 | 1003 | #ifdef DIAGNOSTIC |
802236f4 | 1004 | if (signum == 0) |
cf5ef508 | 1005 | panic("postsig"); |
22585170 | 1006 | #endif |
802236f4 | 1007 | mask = sigmask(signum); |
cf5ef508 | 1008 | p->p_siglist &= ~mask; |
802236f4 | 1009 | action = ps->ps_sigact[signum]; |
22585170 | 1010 | #ifdef KTRACE |
8429d022 | 1011 | if (KTRPOINT(p, KTR_PSIG)) |
802236f4 KB |
1012 | ktrpsig(p->p_tracep, |
1013 | signum, action, ps->ps_flags & SAS_OLDMASK ? | |
8429d022 | 1014 | ps->ps_oldmask : p->p_sigmask, 0); |
22585170 | 1015 | #endif |
8429d022 MK |
1016 | if (action == SIG_DFL) { |
1017 | /* | |
1018 | * Default action, where the default is to kill | |
1019 | * the process. (Other cases were ignored above.) | |
8429d022 | 1020 | */ |
802236f4 | 1021 | sigexit(p, signum); |
8429d022 MK |
1022 | /* NOTREACHED */ |
1023 | } else { | |
1024 | /* | |
1025 | * If we get here, the signal must be caught. | |
1026 | */ | |
f4af9a5b | 1027 | #ifdef DIAGNOSTIC |
8429d022 | 1028 | if (action == SIG_IGN || (p->p_sigmask & mask)) |
cf5ef508 | 1029 | panic("postsig action"); |
f4af9a5b | 1030 | #endif |
8429d022 MK |
1031 | /* |
1032 | * Set the new mask value and also defer further | |
1033 | * occurences of this signal. | |
1034 | * | |
1035 | * Special case: user has done a sigpause. Here the | |
1036 | * current mask is not of interest, but rather the | |
1037 | * mask from before the sigpause is what we want | |
1038 | * restored after the signal processing is completed. | |
1039 | */ | |
1040 | (void) splhigh(); | |
ebf08548 | 1041 | if (ps->ps_flags & SAS_OLDMASK) { |
8429d022 | 1042 | returnmask = ps->ps_oldmask; |
ebf08548 | 1043 | ps->ps_flags &= ~SAS_OLDMASK; |
8429d022 MK |
1044 | } else |
1045 | returnmask = p->p_sigmask; | |
802236f4 | 1046 | p->p_sigmask |= ps->ps_catchmask[signum] | mask; |
8429d022 MK |
1047 | (void) spl0(); |
1048 | p->p_stats->p_ru.ru_nsignals++; | |
802236f4 | 1049 | sendsig(action, signum, returnmask, 0); |
8429d022 | 1050 | } |
687880f9 BJ |
1051 | } |
1052 | ||
055fc5f3 KM |
1053 | /* |
1054 | * Kill the current process for stated reason. | |
1055 | */ | |
1056 | killproc(p, why) | |
1057 | struct proc *p; | |
1058 | char *why; | |
1059 | { | |
1060 | ||
1061 | log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); | |
1062 | uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); | |
1063 | psignal(p, SIGKILL); | |
1064 | } | |
1065 | ||
35493b0f | 1066 | /* |
802236f4 KB |
1067 | * Force the current process to exit with the specified signal, dumping core |
1068 | * if appropriate. We bypass the normal tests for masked and caught signals, | |
1069 | * allowing unrecoverable failures to terminate the process without changing | |
1070 | * signal state. Mark the accounting record with the signal termination. | |
1071 | * If dumping core, save the signal number for the debugger. Calls exit and | |
1072 | * does not return. | |
35493b0f | 1073 | */ |
802236f4 | 1074 | sigexit(p, signum) |
35493b0f | 1075 | register struct proc *p; |
802236f4 | 1076 | int signum; |
35493b0f MK |
1077 | { |
1078 | ||
1079 | p->p_acflag |= AXSIG; | |
802236f4 KB |
1080 | if (sigprop[signum] & SA_CORE) { |
1081 | p->p_sigacts->ps_sig = signum; | |
35493b0f | 1082 | if (coredump(p) == 0) |
802236f4 | 1083 | signum |= WCOREFLAG; |
35493b0f | 1084 | } |
802236f4 | 1085 | exit1(p, W_EXITCODE(0, signum)); |
35493b0f MK |
1086 | /* NOTREACHED */ |
1087 | } | |
1088 | ||
687880f9 | 1089 | /* |
802236f4 KB |
1090 | * Dump core, into a file named "core.progname". |
1091 | * Do not drop core if the process was setuid/setgid. | |
687880f9 | 1092 | */ |
8429d022 MK |
1093 | coredump(p) |
1094 | register struct proc *p; | |
687880f9 | 1095 | { |
c4ec2128 | 1096 | register struct vnode *vp; |
8429d022 MK |
1097 | register struct pcred *pcred = p->p_cred; |
1098 | register struct ucred *cred = pcred->pc_ucred; | |
1099 | register struct vmspace *vm = p->p_vmspace; | |
d86a61ec | 1100 | struct vattr vattr; |
79583d0d | 1101 | int error, error1; |
8429d022 | 1102 | struct nameidata nd; |
802236f4 | 1103 | char name[MAXCOMLEN+6]; /* progname.core */ |
687880f9 | 1104 | |
802236f4 | 1105 | if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid) |
d86a61ec | 1106 | return (EFAULT); |
8429d022 MK |
1107 | if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >= |
1108 | p->p_rlimit[RLIMIT_CORE].rlim_cur) | |
d86a61ec | 1109 | return (EFAULT); |
802236f4 | 1110 | sprintf(name, "%s.core", p->p_comm); |
0c9db509 | 1111 | NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p); |
802236f4 KB |
1112 | if (error = vn_open(&nd, |
1113 | O_CREAT | FWRITE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) | |
d86a61ec | 1114 | return (error); |
8429d022 | 1115 | vp = nd.ni_vp; |
802236f4 KB |
1116 | |
1117 | /* Don't dump to non-regular files or files with links. */ | |
1118 | if (vp->v_type != VREG || | |
1119 | VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { | |
79583d0d KM |
1120 | error = EFAULT; |
1121 | goto out; | |
7b0cb7cb | 1122 | } |
3ee1461b | 1123 | VATTR_NULL(&vattr); |
d86a61ec | 1124 | vattr.va_size = 0; |
4fcc40ae | 1125 | LEASE_CHECK(vp, p, cred, LEASE_WRITE); |
6bcdc4f3 | 1126 | VOP_SETATTR(vp, &vattr, cred, p); |
8429d022 | 1127 | p->p_acflag |= ACORE; |
8e6cd807 MK |
1128 | bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc)); |
1129 | fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); | |
7b2c7fa0 | 1130 | error = cpu_coredump(p, vp, cred); |
d86a61ec | 1131 | if (error == 0) |
8429d022 MK |
1132 | error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr, |
1133 | (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, | |
8e6cd807 | 1134 | IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); |
d86a61ec KM |
1135 | if (error == 0) |
1136 | error = vn_rdwr(UIO_WRITE, vp, | |
8e6cd807 | 1137 | (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)), |
8429d022 MK |
1138 | round_page(ctob(vm->vm_ssize)), |
1139 | (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE, | |
8e6cd807 | 1140 | IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); |
79583d0d KM |
1141 | out: |
1142 | VOP_UNLOCK(vp); | |
1143 | error1 = vn_close(vp, FWRITE, cred, p); | |
bdb4bae9 | 1144 | if (error == 0) |
79583d0d | 1145 | error = error1; |
d86a61ec | 1146 | return (error); |
687880f9 | 1147 | } |
f4af9a5b MK |
1148 | |
1149 | /* | |
1150 | * Nonexistent system call-- signal process (may want to handle it). | |
1151 | * Flag error in case process won't see signal immediately (blocked or ignored). | |
1152 | */ | |
9e97623a CT |
1153 | struct nosys_args { |
1154 | int dummy; | |
1155 | }; | |
8eea23a6 KM |
1156 | /* ARGSUSED */ |
1157 | nosys(p, args, retval) | |
1158 | struct proc *p; | |
9e97623a | 1159 | struct nosys_args *args; |
8eea23a6 | 1160 | int *retval; |
f4af9a5b MK |
1161 | { |
1162 | ||
8eea23a6 | 1163 | psignal(p, SIGSYS); |
d9c2f47f | 1164 | return (EINVAL); |
f4af9a5b | 1165 | } |