]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
9bccf70c | 2 | * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
43866e37 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* Copyright (c) 1995-1998 Apple Computer, Inc. All Rights Reserved */ | |
26 | /* | |
27 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 | |
28 | * The Regents of the University of California. All rights reserved. | |
29 | * (c) UNIX System Laboratories, Inc. | |
30 | * All or some portions of this file are derived from material licensed | |
31 | * to the University of California by American Telephone and Telegraph | |
32 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
33 | * the permission of UNIX System Laboratories, Inc. | |
34 | * | |
35 | * Redistribution and use in source and binary forms, with or without | |
36 | * modification, are permitted provided that the following conditions | |
37 | * are met: | |
38 | * 1. Redistributions of source code must retain the above copyright | |
39 | * notice, this list of conditions and the following disclaimer. | |
40 | * 2. Redistributions in binary form must reproduce the above copyright | |
41 | * notice, this list of conditions and the following disclaimer in the | |
42 | * documentation and/or other materials provided with the distribution. | |
43 | * 3. All advertising materials mentioning features or use of this software | |
44 | * must display the following acknowledgement: | |
45 | * This product includes software developed by the University of | |
46 | * California, Berkeley and its contributors. | |
47 | * 4. Neither the name of the University nor the names of its contributors | |
48 | * may be used to endorse or promote products derived from this software | |
49 | * without specific prior written permission. | |
50 | * | |
51 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
52 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
53 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
54 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
55 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
61 | * SUCH DAMAGE. | |
62 | * | |
63 | * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 | |
64 | */ | |
65 | ||
66 | #define SIGPROP /* include signal properties table */ | |
67 | #include <sys/param.h> | |
1c79356b A |
68 | #include <sys/resourcevar.h> |
69 | #include <sys/namei.h> | |
70 | #include <sys/vnode.h> | |
71 | #include <sys/proc.h> | |
72 | #include <sys/systm.h> | |
73 | #include <sys/timeb.h> | |
74 | #include <sys/times.h> | |
75 | #include <sys/buf.h> | |
76 | #include <sys/acct.h> | |
77 | #include <sys/file.h> | |
78 | #include <sys/kernel.h> | |
79 | #include <sys/wait.h> | |
9bccf70c A |
80 | #include <sys/signalvar.h> |
81 | #if KTRACE | |
1c79356b | 82 | #include <sys/ktrace.h> |
9bccf70c | 83 | #endif |
1c79356b A |
84 | #include <sys/syslog.h> |
85 | #include <sys/stat.h> | |
86 | #include <sys/lock.h> | |
9bccf70c | 87 | #include <sys/kdebug.h> |
1c79356b A |
88 | |
89 | #include <sys/mount.h> | |
90 | ||
91 | #include <kern/cpu_number.h> | |
92 | ||
93 | #include <sys/vm.h> | |
94 | #include <sys/user.h> /* for coredump */ | |
95 | #include <kern/ast.h> /* for APC support */ | |
96 | #include <kern/thread.h> | |
9bccf70c | 97 | #include <kern/sched_prim.h> |
1c79356b | 98 | #include <kern/thread_call.h> |
9bccf70c A |
99 | #include <mach/exception.h> |
100 | ||
101 | extern void doexception(int exc, int code, int sub); | |
1c79356b A |
102 | |
103 | void stop __P((struct proc *p)); | |
104 | int cansignal __P((struct proc *, struct pcred *, struct proc *, int)); | |
105 | int killpg1 __P((struct proc *, int, int, int)); | |
106 | void sigexit_locked __P((struct proc *, int)); | |
9bccf70c | 107 | int setsigvec __P((struct proc *, int, struct __sigaction *)); |
0b4e3aa0 | 108 | void exit1 __P((struct proc *, int, int *)); |
1c79356b A |
109 | int signal_lock __P((struct proc *)); |
110 | int signal_unlock __P((struct proc *)); | |
9bccf70c A |
111 | void signal_setast __P((thread_act_t)); |
112 | void psignal_lock __P((struct proc *, int, int)); | |
113 | void psignal_uthread __P((thread_act_t, int)); | |
114 | kern_return_t do_bsdexception(int, int, int); | |
1c79356b | 115 | |
55e303ae A |
116 | static int filt_sigattach(struct knote *kn); |
117 | static void filt_sigdetach(struct knote *kn); | |
118 | static int filt_signal(struct knote *kn, long hint); | |
119 | ||
120 | struct filterops sig_filtops = | |
121 | { 0, filt_sigattach, filt_sigdetach, filt_signal }; | |
122 | ||
1c79356b A |
123 | #if SIGNAL_DEBUG |
124 | void ram_printf __P((int)); | |
125 | int ram_debug=0; | |
126 | unsigned int rdebug_proc=0; | |
127 | void | |
128 | ram_printf(int x) | |
129 | { | |
130 | printf("x is %d",x); | |
131 | ||
132 | } | |
1c79356b | 133 | #endif /* SIGNAL_DEBUG */ |
9bccf70c | 134 | |
1c79356b A |
135 | int |
136 | signal_lock(struct proc *p) | |
137 | { | |
765c9de3 | 138 | int error = 0; |
9bccf70c | 139 | #if DIAGNOSTIC |
1c79356b A |
140 | #if SIGNAL_DEBUG |
141 | #ifdef __ppc__ | |
142 | { | |
143 | int register sp, *fp, numsaved; | |
144 | ||
145 | __asm__ volatile("mr %0,r1" : "=r" (sp)); | |
146 | ||
147 | fp = (int *)*((int *)sp); | |
148 | for (numsaved = 0; numsaved < 3; numsaved++) { | |
149 | p->lockpc[numsaved] = fp[2]; | |
150 | if ((int)fp <= 0) | |
151 | break; | |
152 | fp = (int *)*fp; | |
153 | } | |
154 | } | |
155 | #endif /* __ppc__ */ | |
156 | #endif /* SIGNAL_DEBUG */ | |
9bccf70c | 157 | #endif /* DIAGNOSTIC */ |
1c79356b | 158 | |
765c9de3 | 159 | siglock_retry: |
9bccf70c A |
160 | /* TBD: check p last arg */ |
161 | error = lockmgr(&p->signal_lock, LK_EXCLUSIVE, 0, (struct proc *)p); | |
765c9de3 A |
162 | if (error == EINTR) |
163 | goto siglock_retry; | |
164 | return(error); | |
1c79356b A |
165 | } |
166 | ||
167 | int | |
168 | signal_unlock(struct proc *p) | |
169 | { | |
9bccf70c | 170 | #if DIAGNOSTIC |
1c79356b A |
171 | #if SIGNAL_DEBUG |
172 | #ifdef __ppc__ | |
173 | { | |
174 | int register sp, *fp, numsaved; | |
175 | ||
176 | __asm__ volatile("mr %0,r1" : "=r" (sp)); | |
177 | ||
178 | fp = (int *)*((int *)sp); | |
179 | for (numsaved = 0; numsaved < 3; numsaved++) { | |
180 | p->unlockpc[numsaved] = fp[2]; | |
181 | if ((int)fp <= 0) | |
182 | break; | |
183 | fp = (int *)*fp; | |
184 | } | |
185 | } | |
186 | #endif /* __ppc__ */ | |
187 | #endif /* SIGNAL_DEBUG */ | |
9bccf70c | 188 | #endif /* DIAGNOSTIC */ |
1c79356b | 189 | |
9bccf70c A |
190 | /* TBD: check p last arg */ |
191 | return(lockmgr(&p->signal_lock, LK_RELEASE, (simple_lock_t)0, (struct proc *)p)); | |
1c79356b A |
192 | } |
193 | ||
194 | void | |
195 | signal_setast(sig_actthread) | |
9bccf70c | 196 | thread_act_t sig_actthread; |
1c79356b | 197 | { |
9bccf70c | 198 | act_set_astbsd(sig_actthread); |
1c79356b A |
199 | } |
200 | ||
201 | /* | |
202 | * Can process p, with pcred pc, send the signal signum to process q? | |
203 | */ | |
204 | int | |
205 | cansignal(p, pc, q, signum) | |
206 | struct proc *p; | |
207 | struct pcred *pc; | |
208 | struct proc *q; | |
209 | int signum; | |
210 | { | |
9bccf70c A |
211 | /* you can signal yourself */ |
212 | if (p == q) | |
213 | return(1); | |
214 | ||
1c79356b A |
215 | if (pc->pc_ucred->cr_uid == 0) |
216 | return (1); /* root can always signal */ | |
217 | ||
218 | if (signum == SIGCONT && q->p_session == p->p_session) | |
219 | return (1); /* SIGCONT in session */ | |
220 | ||
221 | /* | |
222 | * Using kill(), only certain signals can be sent to setugid | |
223 | * child processes | |
224 | */ | |
225 | if (q->p_flag & P_SUGID) { | |
226 | switch (signum) { | |
227 | case 0: | |
228 | case SIGKILL: | |
229 | case SIGINT: | |
230 | case SIGTERM: | |
231 | case SIGSTOP: | |
232 | case SIGTTIN: | |
233 | case SIGTTOU: | |
234 | case SIGTSTP: | |
235 | case SIGHUP: | |
236 | case SIGUSR1: | |
237 | case SIGUSR2: | |
238 | if (pc->p_ruid == q->p_cred->p_ruid || | |
239 | pc->pc_ucred->cr_uid == q->p_cred->p_ruid || | |
240 | pc->p_ruid == q->p_ucred->cr_uid || | |
241 | pc->pc_ucred->cr_uid == q->p_ucred->cr_uid) | |
242 | return (1); | |
243 | } | |
244 | return (0); | |
245 | } | |
246 | ||
247 | /* XXX | |
248 | * because the P_SUGID test exists, this has extra tests which | |
249 | * could be removed. | |
250 | */ | |
251 | if (pc->p_ruid == q->p_cred->p_ruid || | |
252 | pc->p_ruid == q->p_cred->p_svuid || | |
253 | pc->pc_ucred->cr_uid == q->p_cred->p_ruid || | |
254 | pc->pc_ucred->cr_uid == q->p_cred->p_svuid || | |
255 | pc->p_ruid == q->p_ucred->cr_uid || | |
256 | pc->pc_ucred->cr_uid == q->p_ucred->cr_uid) | |
257 | return (1); | |
258 | return (0); | |
259 | } | |
260 | ||
261 | struct sigaction_args { | |
262 | int signum; | |
9bccf70c | 263 | struct __sigaction *nsa; |
1c79356b A |
264 | struct sigaction *osa; |
265 | }; | |
266 | ||
267 | /* ARGSUSED */ | |
268 | int | |
269 | sigaction(p, uap, retval) | |
270 | struct proc *p; | |
271 | register struct sigaction_args *uap; | |
272 | register_t *retval; | |
273 | { | |
274 | struct sigaction vec; | |
9bccf70c A |
275 | struct __sigaction __vec; |
276 | ||
1c79356b A |
277 | register struct sigaction *sa; |
278 | register struct sigacts *ps = p->p_sigacts; | |
279 | register int signum; | |
9bccf70c | 280 | int bit, error=0; |
1c79356b A |
281 | |
282 | signum = uap->signum; | |
283 | if (signum <= 0 || signum >= NSIG || | |
284 | signum == SIGKILL || signum == SIGSTOP) | |
285 | return (EINVAL); | |
286 | sa = &vec; | |
287 | if (uap->osa) { | |
288 | sa->sa_handler = ps->ps_sigact[signum]; | |
289 | sa->sa_mask = ps->ps_catchmask[signum]; | |
290 | bit = sigmask(signum); | |
291 | sa->sa_flags = 0; | |
292 | if ((ps->ps_sigonstack & bit) != 0) | |
293 | sa->sa_flags |= SA_ONSTACK; | |
294 | if ((ps->ps_sigintr & bit) == 0) | |
295 | sa->sa_flags |= SA_RESTART; | |
9bccf70c A |
296 | if (ps->ps_siginfo & bit) |
297 | sa->sa_flags |= SA_SIGINFO; | |
298 | if (ps->ps_signodefer & bit) | |
299 | sa->sa_flags |= SA_NODEFER; | |
55e303ae A |
300 | if (ps->ps_64regset & bit) |
301 | sa->sa_flags |= SA_64REGSET; | |
9bccf70c | 302 | if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDSTOP)) |
1c79356b | 303 | sa->sa_flags |= SA_NOCLDSTOP; |
9bccf70c A |
304 | if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDWAIT)) |
305 | sa->sa_flags |= SA_NOCLDWAIT; | |
1c79356b A |
306 | if (error = copyout((caddr_t)sa, (caddr_t)uap->osa, |
307 | sizeof (vec))) | |
308 | return (error); | |
309 | } | |
310 | if (uap->nsa) { | |
9bccf70c A |
311 | if (error = copyin((caddr_t)uap->nsa, (caddr_t)&__vec, |
312 | sizeof (__vec))) | |
1c79356b | 313 | return (error); |
9bccf70c | 314 | error = setsigvec(p, signum, &__vec); |
1c79356b | 315 | } |
9bccf70c A |
316 | return (error); |
317 | } | |
318 | ||
319 | /* Routines to manipulate bits on all threads */ | |
320 | int | |
321 | clear_procsiglist(struct proc *p, int bit) | |
322 | { | |
323 | struct uthread * uth; | |
324 | thread_act_t thact; | |
325 | ||
326 | signal_lock(p); | |
327 | ||
328 | if ((p->p_flag & P_INVFORK) && p->p_vforkact) { | |
329 | thact = p->p_vforkact; | |
330 | uth = (struct uthread *)get_bsdthread_info(thact); | |
331 | if (uth) { | |
332 | uth->uu_siglist &= ~bit; | |
333 | } | |
334 | p->p_siglist &= ~bit; | |
335 | signal_unlock(p); | |
336 | return(0); | |
337 | } | |
338 | ||
339 | TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) { | |
340 | uth->uu_siglist &= ~bit; | |
341 | } | |
342 | p->p_siglist &= ~bit; | |
343 | signal_unlock(p); | |
344 | return(0); | |
1c79356b A |
345 | } |
346 | ||
9bccf70c A |
347 | int |
348 | unblock_procsigmask(struct proc *p, int bit) | |
1c79356b | 349 | { |
9bccf70c A |
350 | struct uthread * uth; |
351 | thread_act_t thact; | |
352 | ||
353 | signal_lock(p); | |
354 | if ((p->p_flag & P_INVFORK) && p->p_vforkact) { | |
355 | thact = p->p_vforkact; | |
356 | uth = (struct uthread *)get_bsdthread_info(thact); | |
357 | if (uth) { | |
358 | uth->uu_sigmask &= ~bit; | |
359 | } | |
360 | p->p_sigmask &= ~bit; | |
361 | signal_unlock(p); | |
362 | return(0); | |
363 | } | |
364 | TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) { | |
365 | uth->uu_sigmask &= ~bit; | |
1c79356b | 366 | } |
9bccf70c A |
367 | p->p_sigmask &= ~bit; |
368 | signal_unlock(p); | |
369 | return(0); | |
1c79356b A |
370 | } |
371 | ||
9bccf70c | 372 | |
1c79356b | 373 | int |
9bccf70c | 374 | block_procsigmask(struct proc *p, int bit) |
1c79356b | 375 | { |
9bccf70c A |
376 | struct uthread * uth; |
377 | thread_act_t thact; | |
1c79356b | 378 | |
9bccf70c A |
379 | signal_lock(p); |
380 | if ((p->p_flag & P_INVFORK) && p->p_vforkact) { | |
381 | thact = p->p_vforkact; | |
382 | uth = (struct uthread *)get_bsdthread_info(thact); | |
383 | if (uth) { | |
384 | uth->uu_sigmask |= bit; | |
385 | } | |
386 | p->p_sigmask |= bit; | |
387 | signal_unlock(p); | |
388 | return(0); | |
389 | } | |
390 | TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) { | |
391 | uth->uu_sigmask |= bit; | |
392 | } | |
393 | p->p_sigmask |= bit; | |
394 | signal_unlock(p); | |
1c79356b A |
395 | return(0); |
396 | } | |
9bccf70c A |
397 | int |
398 | set_procsigmask(struct proc *p, int bit) | |
399 | { | |
400 | struct uthread * uth; | |
401 | thread_act_t thact; | |
1c79356b | 402 | |
9bccf70c A |
403 | signal_lock(p); |
404 | if ((p->p_flag & P_INVFORK) && p->p_vforkact) { | |
405 | thact = p->p_vforkact; | |
406 | uth = (struct uthread *)get_bsdthread_info(thact); | |
407 | if (uth) { | |
408 | uth->uu_sigmask = bit; | |
409 | } | |
410 | p->p_sigmask = bit; | |
411 | signal_unlock(p); | |
412 | return(0); | |
413 | } | |
414 | TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) { | |
415 | uth->uu_sigmask = bit; | |
416 | } | |
417 | p->p_sigmask = bit; | |
418 | signal_unlock(p); | |
419 | return(0); | |
420 | } | |
1c79356b | 421 | |
9bccf70c | 422 | int |
1c79356b A |
423 | setsigvec(p, signum, sa) |
424 | register struct proc *p; | |
425 | int signum; | |
9bccf70c | 426 | register struct __sigaction *sa; |
1c79356b A |
427 | { |
428 | register struct sigacts *ps = p->p_sigacts; | |
429 | register int bit; | |
430 | ||
9bccf70c A |
431 | if ((signum == SIGKILL || signum == SIGSTOP) && |
432 | sa->sa_handler != SIG_DFL) | |
433 | return(EINVAL); | |
1c79356b A |
434 | bit = sigmask(signum); |
435 | /* | |
436 | * Change setting atomically. | |
437 | */ | |
438 | ps->ps_sigact[signum] = sa->sa_handler; | |
55e303ae | 439 | ps->ps_trampact[signum] = (sig_t) sa->sa_tramp; |
1c79356b | 440 | ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask; |
9bccf70c A |
441 | if (sa->sa_flags & SA_SIGINFO) |
442 | ps->ps_siginfo |= bit; | |
443 | else | |
444 | ps->ps_siginfo &= ~bit; | |
55e303ae A |
445 | if (sa->sa_flags & SA_64REGSET) |
446 | ps->ps_64regset |= bit; | |
447 | else | |
448 | ps->ps_64regset &= ~bit; | |
1c79356b A |
449 | if ((sa->sa_flags & SA_RESTART) == 0) |
450 | ps->ps_sigintr |= bit; | |
451 | else | |
452 | ps->ps_sigintr &= ~bit; | |
453 | if (sa->sa_flags & SA_ONSTACK) | |
454 | ps->ps_sigonstack |= bit; | |
455 | else | |
456 | ps->ps_sigonstack &= ~bit; | |
457 | if (sa->sa_flags & SA_USERTRAMP) | |
458 | ps->ps_usertramp |= bit; | |
459 | else | |
460 | ps->ps_usertramp &= ~bit; | |
9bccf70c A |
461 | if (sa->sa_flags & SA_RESETHAND) |
462 | ps->ps_sigreset |= bit; | |
463 | else | |
464 | ps->ps_sigreset &= ~bit; | |
465 | if (sa->sa_flags & SA_NODEFER) | |
466 | ps->ps_signodefer |= bit; | |
467 | else | |
468 | ps->ps_signodefer &= ~bit; | |
1c79356b A |
469 | if (signum == SIGCHLD) { |
470 | if (sa->sa_flags & SA_NOCLDSTOP) | |
471 | p->p_flag |= P_NOCLDSTOP; | |
472 | else | |
473 | p->p_flag &= ~P_NOCLDSTOP; | |
9bccf70c A |
474 | if ((sa->sa_flags & SA_NOCLDWAIT) || (sa->sa_handler == SIG_IGN)) |
475 | p->p_flag |= P_NOCLDWAIT; | |
476 | else | |
477 | p->p_flag &= ~P_NOCLDWAIT; | |
1c79356b | 478 | } |
9bccf70c A |
479 | |
480 | #ifdef __ppc__ | |
481 | if (signum == SIGFPE) { | |
482 | if (sa->sa_handler == SIG_DFL || sa->sa_handler == SIG_IGN) | |
483 | thread_enable_fpe(current_act(), 0); | |
484 | else | |
485 | thread_enable_fpe(current_act(), 1); | |
486 | } | |
487 | #endif /* __ppc__ */ | |
1c79356b A |
488 | /* |
489 | * Set bit in p_sigignore for signals that are set to SIG_IGN, | |
490 | * and for signals set to SIG_DFL where the default is to ignore. | |
491 | * However, don't put SIGCONT in p_sigignore, | |
492 | * as we have to restart the process. | |
493 | */ | |
494 | if (sa->sa_handler == SIG_IGN || | |
495 | (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) { | |
1c79356b | 496 | |
9bccf70c | 497 | clear_procsiglist(p, bit); |
1c79356b A |
498 | if (signum != SIGCONT) |
499 | p->p_sigignore |= bit; /* easier in psignal */ | |
500 | p->p_sigcatch &= ~bit; | |
501 | } else { | |
502 | p->p_sigignore &= ~bit; | |
503 | if (sa->sa_handler == SIG_DFL) | |
504 | p->p_sigcatch &= ~bit; | |
505 | else | |
506 | p->p_sigcatch |= bit; | |
507 | } | |
9bccf70c | 508 | return(0); |
1c79356b A |
509 | } |
510 | ||
511 | /* | |
512 | * Initialize signal state for process 0; | |
513 | * set to ignore signals that are ignored by default. | |
514 | */ | |
515 | void | |
516 | siginit(p) | |
517 | struct proc *p; | |
518 | { | |
519 | register int i; | |
520 | ||
521 | for (i = 0; i < NSIG; i++) | |
522 | if (sigprop[i] & SA_IGNORE && i != SIGCONT) | |
523 | p->p_sigignore |= sigmask(i); | |
524 | } | |
525 | ||
526 | /* | |
527 | * Reset signals for an exec of the specified process. | |
528 | */ | |
529 | void | |
9bccf70c | 530 | execsigs(p, thr_act) |
1c79356b | 531 | register struct proc *p; |
9bccf70c | 532 | register thread_act_t thr_act; |
1c79356b A |
533 | { |
534 | register struct sigacts *ps = p->p_sigacts; | |
535 | register int nc, mask; | |
9bccf70c | 536 | struct uthread *ut; |
1c79356b A |
537 | |
538 | /* | |
539 | * Reset caught signals. Held signals remain held | |
540 | * through p_sigmask (unless they were caught, | |
541 | * and are now ignored by default). | |
542 | */ | |
543 | while (p->p_sigcatch) { | |
544 | nc = ffs((long)p->p_sigcatch); | |
545 | mask = sigmask(nc); | |
546 | p->p_sigcatch &= ~mask; | |
547 | if (sigprop[nc] & SA_IGNORE) { | |
548 | if (nc != SIGCONT) | |
549 | p->p_sigignore |= mask; | |
9bccf70c A |
550 | if (thr_act){ |
551 | ut = (struct uthread *)get_bsdthread_info(thr_act); | |
552 | ut->uu_siglist &= ~mask; | |
553 | p->p_siglist &= ~mask; | |
554 | } else | |
555 | clear_procsiglist(p, mask); | |
1c79356b A |
556 | } |
557 | ps->ps_sigact[nc] = SIG_DFL; | |
558 | } | |
559 | /* | |
560 | * Reset stack state to the user stack. | |
561 | * Clear set of signals caught on the signal stack. | |
562 | */ | |
563 | ps->ps_sigstk.ss_flags = SA_DISABLE; | |
564 | ps->ps_sigstk.ss_size = 0; | |
565 | ps->ps_sigstk.ss_sp = 0; | |
566 | ps->ps_flags = 0; | |
567 | } | |
568 | ||
569 | /* | |
570 | * Manipulate signal mask. | |
571 | * Note that we receive new mask, not pointer, | |
572 | * and return old mask as return value; | |
573 | * the library stub does the rest. | |
574 | */ | |
575 | struct sigprocmask_args { | |
576 | int how; | |
9bccf70c A |
577 | sigset_t *mask; |
578 | sigset_t * omask; | |
1c79356b A |
579 | }; |
580 | int | |
581 | sigprocmask(p, uap, retval) | |
582 | register struct proc *p; | |
583 | struct sigprocmask_args *uap; | |
584 | register_t *retval; | |
585 | { | |
586 | int error = 0; | |
9bccf70c A |
587 | sigset_t oldmask, nmask; |
588 | sigset_t * omask = uap->omask; | |
589 | struct uthread *ut; | |
1c79356b | 590 | |
9bccf70c A |
591 | ut = (struct uthread *)get_bsdthread_info(current_act()); |
592 | oldmask = ut->uu_sigmask; | |
593 | ||
594 | if (uap->mask == (sigset_t *)0) { | |
595 | /* just want old mask */ | |
596 | goto out; | |
597 | } | |
598 | error = copyin((caddr_t)uap->mask, &nmask, sizeof(sigset_t)); | |
599 | if (error) | |
600 | goto out; | |
1c79356b A |
601 | |
602 | switch (uap->how) { | |
603 | case SIG_BLOCK: | |
9bccf70c A |
604 | block_procsigmask(p, (nmask & ~sigcantmask)); |
605 | signal_setast(current_act()); | |
1c79356b A |
606 | break; |
607 | ||
608 | case SIG_UNBLOCK: | |
9bccf70c | 609 | unblock_procsigmask(p, (nmask & ~sigcantmask)); |
1c79356b A |
610 | signal_setast(current_act()); |
611 | break; | |
612 | ||
613 | case SIG_SETMASK: | |
9bccf70c | 614 | set_procsigmask(p, (nmask & ~sigcantmask)); |
1c79356b A |
615 | signal_setast(current_act()); |
616 | break; | |
617 | ||
618 | default: | |
619 | error = EINVAL; | |
620 | break; | |
621 | } | |
9bccf70c A |
622 | out: |
623 | if (!error && omask) | |
624 | copyout(&oldmask, omask, sizeof(sigset_t)); | |
1c79356b A |
625 | return (error); |
626 | } | |
627 | ||
9bccf70c A |
628 | struct sigpending_args { |
629 | struct sigvec *osv; | |
630 | }; | |
1c79356b A |
631 | int |
632 | sigpending(p, uap, retval) | |
633 | struct proc *p; | |
9bccf70c | 634 | register struct sigpending_args *uap; |
1c79356b A |
635 | register_t *retval; |
636 | { | |
9bccf70c A |
637 | struct uthread *ut; |
638 | sigset_t pendlist; | |
1c79356b | 639 | |
9bccf70c A |
640 | ut = (struct uthread *)get_bsdthread_info(current_act()); |
641 | pendlist = ut->uu_siglist; | |
642 | ||
643 | if (uap->osv) | |
644 | copyout(&pendlist, uap->osv, sizeof(sigset_t)); | |
645 | return(0); | |
1c79356b A |
646 | } |
647 | ||
648 | #if COMPAT_43 | |
649 | /* | |
650 | * Generalized interface signal handler, 4.3-compatible. | |
651 | */ | |
652 | struct osigvec_args { | |
653 | int signum; | |
654 | struct sigvec *nsv; | |
655 | struct sigvec *osv; | |
656 | }; | |
657 | /* ARGSUSED */ | |
658 | int | |
659 | osigvec(p, uap, retval) | |
660 | struct proc *p; | |
661 | register struct osigvec_args *uap; | |
662 | register_t *retval; | |
663 | { | |
9bccf70c | 664 | struct sigvec __vec; |
1c79356b A |
665 | struct sigvec vec; |
666 | register struct sigacts *ps = p->p_sigacts; | |
667 | register struct sigvec *sv; | |
668 | register int signum; | |
9bccf70c | 669 | int bit, error=0; |
1c79356b | 670 | |
9bccf70c | 671 | #if 0 |
1c79356b A |
672 | signum = uap->signum; |
673 | if (signum <= 0 || signum >= NSIG || | |
674 | signum == SIGKILL || signum == SIGSTOP) | |
675 | return (EINVAL); | |
676 | sv = &vec; | |
677 | if (uap->osv) { | |
678 | *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum]; | |
679 | sv->sv_mask = ps->ps_catchmask[signum]; | |
680 | bit = sigmask(signum); | |
681 | sv->sv_flags = 0; | |
682 | if ((ps->ps_sigonstack & bit) != 0) | |
683 | sv->sv_flags |= SV_ONSTACK; | |
684 | if ((ps->ps_sigintr & bit) != 0) | |
685 | sv->sv_flags |= SV_INTERRUPT; | |
686 | if (p->p_flag & P_NOCLDSTOP) | |
687 | sv->sv_flags |= SA_NOCLDSTOP; | |
688 | if (error = copyout((caddr_t)sv, (caddr_t)uap->osv, | |
689 | sizeof (vec))) | |
690 | return (error); | |
691 | } | |
692 | if (uap->nsv) { | |
693 | if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv, | |
694 | sizeof (vec))) | |
695 | return (error); | |
696 | sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ | |
9bccf70c | 697 | error = setsigvec(p, signum, (struct sigaction *)sv); |
1c79356b | 698 | } |
55e303ae A |
699 | #else |
700 | error = ENOSYS; | |
9bccf70c A |
701 | #endif |
702 | return (error); | |
1c79356b A |
703 | } |
704 | ||
705 | struct osigblock_args { | |
706 | int mask; | |
707 | }; | |
708 | int | |
709 | osigblock(p, uap, retval) | |
710 | register struct proc *p; | |
711 | struct osigblock_args *uap; | |
712 | register_t *retval; | |
713 | { | |
9bccf70c | 714 | struct uthread * uth = get_bsdthread_info(current_act()); |
1c79356b | 715 | |
9bccf70c A |
716 | *retval = uth->uu_sigmask; |
717 | uth->uu_sigmask |= (uap->mask & ~sigcantmask); | |
1c79356b A |
718 | return (0); |
719 | } | |
720 | ||
721 | struct osigsetmask_args { | |
722 | int mask; | |
723 | }; | |
724 | int | |
725 | osigsetmask(p, uap, retval) | |
726 | struct proc *p; | |
727 | struct osigsetmask_args *uap; | |
728 | register_t *retval; | |
729 | { | |
9bccf70c | 730 | struct uthread * uth = get_bsdthread_info(current_act()); |
1c79356b | 731 | |
9bccf70c A |
732 | *retval = uth->uu_sigmask; |
733 | uth->uu_sigmask = (uap->mask & ~sigcantmask); | |
1c79356b A |
734 | return (0); |
735 | } | |
736 | #endif /* COMPAT_43 */ | |
737 | ||
738 | /* | |
739 | * Suspend process until signal, providing mask to be set | |
740 | * in the meantime. Note nonstandard calling convention: | |
741 | * libc stub passes mask, not pointer, to save a copyin. | |
742 | */ | |
743 | ||
744 | int | |
745 | sigcontinue(error) | |
746 | { | |
9bccf70c | 747 | struct uthread *ut = get_bsdthread_info(current_act()); |
1c79356b A |
748 | unix_syscall_return(EINTR); |
749 | } | |
750 | ||
751 | struct sigsuspend_args { | |
9bccf70c | 752 | sigset_t mask; |
1c79356b A |
753 | }; |
754 | ||
755 | /* ARGSUSED */ | |
756 | int | |
757 | sigsuspend(p, uap, retval) | |
758 | register struct proc *p; | |
759 | struct sigsuspend_args *uap; | |
760 | register_t *retval; | |
761 | { | |
762 | register struct sigacts *ps = p->p_sigacts; | |
9bccf70c A |
763 | struct uthread *ut; |
764 | ||
765 | ut = (struct uthread *)get_bsdthread_info(current_act()); | |
1c79356b A |
766 | |
767 | /* | |
768 | * When returning from sigpause, we want | |
769 | * the old mask to be restored after the | |
770 | * signal handler has finished. Thus, we | |
771 | * save it here and mark the sigacts structure | |
772 | * to indicate this. | |
773 | */ | |
9bccf70c A |
774 | ut->uu_oldmask = ut->uu_sigmask; |
775 | ut->uu_flag |= USAS_OLDMASK; | |
776 | ut->uu_sigmask = (uap->mask & ~sigcantmask); | |
1c79356b A |
777 | (void) tsleep0((caddr_t) p, PPAUSE|PCATCH, "pause", 0, sigcontinue); |
778 | /* always return EINTR rather than ERESTART... */ | |
779 | return (EINTR); | |
780 | } | |
781 | ||
9bccf70c A |
782 | struct __disable_thsignal_args { |
783 | int value; | |
784 | }; | |
785 | ||
786 | int | |
787 | __disable_threadsignal(p, uap, retval) | |
788 | struct proc *p; | |
789 | register struct __disable_thsignal_args *uap; | |
790 | register_t *retval; | |
791 | { | |
792 | struct uthread *uth; | |
793 | ||
794 | uth = (struct uthread *)get_bsdthread_info(current_act()); | |
795 | ||
796 | /* No longer valid to have any signal delivered */ | |
797 | signal_lock(p); | |
798 | uth->uu_flag |= UNO_SIGMASK; | |
799 | signal_unlock(p); | |
800 | ||
801 | return(0); | |
802 | ||
803 | } | |
804 | ||
805 | struct pthread_kill_args { | |
806 | void * thread_port; | |
807 | int sig; | |
808 | }; | |
809 | ||
810 | int | |
811 | __pthread_kill(p, uap, retval) | |
812 | struct proc *p; | |
813 | register struct pthread_kill_args *uap; | |
814 | register_t *retval; | |
815 | { | |
816 | thread_act_t target_act; | |
817 | int error = 0; | |
818 | int signum = uap->sig; | |
819 | struct uthread *uth; | |
820 | ||
821 | target_act = (thread_act_t)port_name_to_act(uap->thread_port); | |
822 | ||
823 | if (target_act == THR_ACT_NULL) | |
824 | return (ESRCH); | |
825 | if ((u_int)signum >= NSIG) { | |
826 | error = EINVAL; | |
827 | goto out; | |
828 | } | |
829 | ||
830 | uth = (struct uthread *)get_bsdthread_info(target_act); | |
55e303ae | 831 | |
9bccf70c A |
832 | if (uth->uu_flag & UNO_SIGMASK) { |
833 | error = ESRCH; | |
834 | goto out; | |
835 | } | |
836 | ||
837 | if (signum) | |
838 | psignal_uthread(target_act, signum); | |
839 | out: | |
840 | act_deallocate(target_act); | |
841 | return (error); | |
842 | } | |
843 | ||
844 | ||
845 | struct pthread_sigmask_args { | |
846 | int how; | |
847 | const sigset_t *set; | |
848 | sigset_t * oset; | |
849 | }; | |
850 | int | |
851 | pthread_sigmask(p, uap, retval) | |
852 | register struct proc *p; | |
853 | register struct pthread_sigmask_args *uap; | |
854 | register_t *retval; | |
855 | { | |
856 | int how = uap->how; | |
857 | const sigset_t *set = uap->set; | |
858 | sigset_t * oset = uap->oset; | |
859 | const sigset_t nset; | |
860 | int error = 0; | |
861 | struct uthread *ut; | |
862 | sigset_t oldset; | |
863 | ||
864 | ut = (struct uthread *)get_bsdthread_info(current_act()); | |
865 | oldset = ut->uu_sigmask; | |
866 | ||
867 | if (set == (sigset_t *) 0) { | |
868 | /* need only old mask */ | |
869 | goto out; | |
870 | } | |
871 | ||
872 | error = copyin((caddr_t)set, (caddr_t)&nset, sizeof(sigset_t)); | |
873 | if (error) | |
874 | goto out; | |
875 | ||
876 | switch (uap->how) { | |
877 | case SIG_BLOCK: | |
878 | ut->uu_sigmask |= (nset & ~sigcantmask); | |
879 | break; | |
880 | ||
881 | case SIG_UNBLOCK: | |
882 | ut->uu_sigmask &= ~(nset); | |
883 | signal_setast(current_act()); | |
884 | break; | |
885 | ||
886 | case SIG_SETMASK: | |
887 | ut->uu_sigmask = (nset & ~sigcantmask); | |
888 | signal_setast(current_act()); | |
889 | break; | |
890 | ||
891 | default: | |
892 | error = EINVAL; | |
893 | ||
894 | } | |
895 | out: | |
896 | if (!error && oset) | |
897 | copyout((caddr_t)&oldset, (caddr_t)oset, sizeof(sigset_t)); | |
898 | ||
899 | return(error); | |
900 | } | |
901 | ||
902 | ||
903 | struct sigwait_args { | |
904 | const sigset_t *set; | |
905 | int *sig; | |
906 | }; | |
907 | ||
908 | int | |
909 | sigwait(p, uap, retval) | |
910 | register struct proc *p; | |
911 | register struct sigwait_args *uap; | |
912 | register_t *retval; | |
913 | { | |
914 | register struct sigacts *ps = p->p_sigacts; | |
915 | struct uthread *ut; | |
916 | struct uthread *uth; | |
917 | thread_act_t thact; | |
918 | int error = 0; | |
919 | sigset_t mask; | |
920 | sigset_t siglist; | |
921 | sigset_t sigw=0; | |
922 | int signum; | |
923 | ||
924 | ut = (struct uthread *)get_bsdthread_info(current_act()); | |
925 | ||
926 | if (uap->set == (const sigset_t *)0) | |
927 | return(EINVAL); | |
928 | ||
929 | error = copyin((caddr_t)uap->set, (caddr_t)&mask, sizeof(sigset_t)); | |
930 | if (error) | |
931 | return(error); | |
932 | ||
933 | siglist = (mask & ~sigcantmask); | |
934 | ||
935 | if (siglist == 0) | |
936 | return(EINVAL); | |
937 | ||
938 | signal_lock(p); | |
939 | if ((p->p_flag & P_INVFORK) && p->p_vforkact) { | |
940 | signal_unlock(p); | |
941 | return(EINVAL); | |
942 | } else { | |
943 | TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) { | |
944 | if (sigw = uth->uu_siglist & siglist) { | |
945 | break; | |
946 | } | |
947 | } | |
948 | } | |
949 | signal_unlock(p); | |
950 | if (sigw) { | |
951 | /* The signal was pending on a thread */ | |
952 | goto sigwait1; | |
953 | } | |
954 | /* | |
955 | * When returning from sigwait, we want | |
956 | * the old mask to be restored after the | |
957 | * signal handler has finished. Thus, we | |
958 | * save it here and mark the sigacts structure | |
959 | * to indicate this. | |
960 | */ | |
961 | ut->uu_oldmask = ut->uu_sigmask; | |
962 | ut->uu_flag |= USAS_OLDMASK; | |
963 | if (siglist == (sigset_t)0) | |
964 | return(EINVAL); | |
965 | /* SIGKILL and SIGSTOP are not maskable as well */ | |
966 | ut->uu_sigmask = ~(siglist|sigcantmask); | |
967 | ut->uu_sigwait = siglist; | |
968 | /* No Continuations for now */ | |
969 | error = tsleep((caddr_t)&ut->uu_sigwait, PPAUSE|PCATCH, "pause", 0); | |
970 | ||
971 | if ((error == EINTR) || (error == ERESTART)) | |
972 | error = 0; | |
973 | ||
974 | sigw = (ut->uu_sigwait & siglist); | |
975 | ut->uu_sigmask = ut->uu_oldmask; | |
976 | ut->uu_oldmask = 0; | |
977 | ut->uu_flag &= ~USAS_OLDMASK; | |
978 | sigwait1: | |
979 | ut->uu_sigwait = 0; | |
980 | if (!error) { | |
981 | signum = ffs((unsigned int)sigw); | |
982 | if (!signum) | |
983 | panic("sigwait with no signal wakeup"); | |
984 | ut->uu_siglist &= ~(sigmask(signum)); | |
985 | if (uap->sig) | |
986 | error = copyout(&signum, uap->sig, sizeof(int)); | |
987 | } | |
988 | ||
989 | return(error); | |
990 | ||
991 | } | |
992 | ||
1c79356b A |
993 | #if COMPAT_43 |
994 | struct osigstack_args { | |
995 | struct sigstack *nss; | |
996 | struct sigstack *oss; | |
997 | }; | |
9bccf70c | 998 | |
1c79356b A |
999 | /* ARGSUSED */ |
1000 | int | |
1001 | osigstack(p, uap, retval) | |
1002 | struct proc *p; | |
1003 | register struct osigstack_args *uap; | |
1004 | register_t *retval; | |
1005 | { | |
1006 | struct sigstack ss; | |
1007 | struct sigacts *psp; | |
1008 | int error = 0; | |
1009 | ||
1010 | psp = p->p_sigacts; | |
1011 | ss.ss_sp = psp->ps_sigstk.ss_sp; | |
1012 | ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK; | |
1013 | if (uap->oss && (error = copyout((caddr_t)&ss, | |
1014 | (caddr_t)uap->oss, sizeof (struct sigstack)))) | |
1015 | return (error); | |
1016 | if (uap->nss && (error = copyin((caddr_t)uap->nss, | |
1017 | (caddr_t)&ss, sizeof (ss))) == 0) { | |
1018 | psp->ps_sigstk.ss_sp = ss.ss_sp; | |
1019 | psp->ps_sigstk.ss_size = 0; | |
1020 | psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK; | |
1021 | psp->ps_flags |= SAS_ALTSTACK; | |
1022 | } | |
1023 | return (error); | |
1024 | } | |
1025 | #endif /* COMPAT_43 */ | |
1026 | ||
1027 | struct sigaltstack_args { | |
1028 | struct sigaltstack *nss; | |
1029 | struct sigaltstack *oss; | |
1030 | }; | |
1031 | /* ARGSUSED */ | |
1032 | int | |
1033 | sigaltstack(p, uap, retval) | |
1034 | struct proc *p; | |
1035 | register struct sigaltstack_args *uap; | |
1036 | register_t *retval; | |
1037 | { | |
1038 | struct sigacts *psp; | |
1039 | struct sigaltstack ss; | |
1040 | int error; | |
1041 | ||
1042 | psp = p->p_sigacts; | |
1043 | if ((psp->ps_flags & SAS_ALTSTACK) == 0) | |
1044 | psp->ps_sigstk.ss_flags |= SA_DISABLE; | |
1045 | if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk, | |
1046 | (caddr_t)uap->oss, sizeof (struct sigaltstack)))) | |
1047 | return (error); | |
1048 | if (uap->nss == 0) | |
1049 | return (0); | |
1050 | if (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, | |
1051 | sizeof (ss))) | |
1052 | return (error); | |
9bccf70c A |
1053 | if ((ss.ss_flags & ~SA_DISABLE) != 0) { |
1054 | return(EINVAL); | |
1055 | } | |
1056 | ||
1c79356b A |
1057 | if (ss.ss_flags & SA_DISABLE) { |
1058 | if (psp->ps_sigstk.ss_flags & SA_ONSTACK) | |
1059 | return (EINVAL); | |
1060 | psp->ps_flags &= ~SAS_ALTSTACK; | |
1061 | psp->ps_sigstk.ss_flags = ss.ss_flags; | |
1062 | return (0); | |
1063 | } | |
55e303ae A |
1064 | /* The older stacksize was 8K, enforce that one so no compat problems */ |
1065 | #define OLDMINSIGSTKSZ 8*1024 | |
1066 | if (ss.ss_size < OLDMINSIGSTKSZ) | |
1c79356b A |
1067 | return (ENOMEM); |
1068 | psp->ps_flags |= SAS_ALTSTACK; | |
1069 | psp->ps_sigstk= ss; | |
1070 | return (0); | |
1071 | } | |
1072 | ||
1073 | struct kill_args { | |
1074 | int pid; | |
1075 | int signum; | |
1076 | }; | |
1077 | /* ARGSUSED */ | |
1078 | int | |
1079 | kill(cp, uap, retval) | |
1080 | register struct proc *cp; | |
1081 | register struct kill_args *uap; | |
1082 | register_t *retval; | |
1083 | { | |
1084 | register struct proc *p; | |
1085 | register struct pcred *pc = cp->p_cred; | |
1086 | ||
1087 | if ((u_int)uap->signum >= NSIG) | |
1088 | return (EINVAL); | |
1089 | if (uap->pid > 0) { | |
1090 | /* kill single process */ | |
55e303ae A |
1091 | if ((p = pfind(uap->pid)) == NULL) { |
1092 | if ((p = pzfind(uap->pid)) != NULL) { | |
1093 | /* | |
1094 | * IEEE Std 1003.1-2001: return success | |
1095 | * when killing a zombie. | |
1096 | */ | |
1097 | return (0); | |
1098 | } | |
1c79356b | 1099 | return (ESRCH); |
55e303ae | 1100 | } |
1c79356b A |
1101 | if (!cansignal(cp, pc, p, uap->signum)) |
1102 | return (EPERM); | |
1103 | if (uap->signum) | |
1104 | psignal(p, uap->signum); | |
1105 | return (0); | |
1106 | } | |
1107 | switch (uap->pid) { | |
1108 | case -1: /* broadcast signal */ | |
1109 | return (killpg1(cp, uap->signum, 0, 1)); | |
1110 | case 0: /* signal own process group */ | |
1111 | return (killpg1(cp, uap->signum, 0, 0)); | |
1112 | default: /* negative explicit process group */ | |
1113 | return (killpg1(cp, uap->signum, -(uap->pid), 0)); | |
1114 | } | |
1115 | /* NOTREACHED */ | |
1116 | } | |
1117 | ||
1118 | #if COMPAT_43 | |
1119 | struct okillpg_args { | |
1120 | int pgid; | |
1121 | int signum; | |
1122 | }; | |
1123 | /* ARGSUSED */ | |
1124 | int | |
1125 | okillpg(p, uap, retval) | |
1126 | struct proc *p; | |
1127 | register struct okillpg_args *uap; | |
1128 | register_t *retval; | |
1129 | { | |
1130 | ||
1131 | if ((u_int)uap->signum >= NSIG) | |
1132 | return (EINVAL); | |
1133 | return (killpg1(p, uap->signum, uap->pgid, 0)); | |
1134 | } | |
1135 | #endif /* COMPAT_43 */ | |
1136 | ||
1137 | /* | |
1138 | * Common code for kill process group/broadcast kill. | |
1139 | * cp is calling process. | |
1140 | */ | |
1141 | int | |
1142 | killpg1(cp, signum, pgid, all) | |
1143 | register struct proc *cp; | |
1144 | int signum, pgid, all; | |
1145 | { | |
1146 | register struct proc *p; | |
1147 | register struct pcred *pc = cp->p_cred; | |
1148 | struct pgrp *pgrp; | |
1149 | int nfound = 0; | |
1150 | ||
1151 | if (all) { | |
1152 | /* | |
1153 | * broadcast | |
1154 | */ | |
1155 | for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { | |
1156 | if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || | |
1157 | p == cp || !cansignal(cp, pc, p, signum)) | |
1158 | continue; | |
1159 | nfound++; | |
1160 | if (signum) | |
1161 | psignal(p, signum); | |
1162 | } | |
1163 | } else { | |
1164 | if (pgid == 0) | |
1165 | /* | |
1166 | * zero pgid means send to my process group. | |
1167 | */ | |
1168 | pgrp = cp->p_pgrp; | |
1169 | else { | |
1170 | pgrp = pgfind(pgid); | |
1171 | if (pgrp == NULL) | |
1172 | return (ESRCH); | |
1173 | } | |
1174 | for (p = pgrp->pg_members.lh_first; p != 0; | |
1175 | p = p->p_pglist.le_next) { | |
1176 | if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || | |
1177 | p->p_stat == SZOMB || | |
1178 | !cansignal(cp, pc, p, signum)) | |
1179 | continue; | |
1180 | nfound++; | |
1181 | if (signum) | |
1182 | psignal(p, signum); | |
1183 | } | |
1184 | } | |
1185 | return (nfound ? 0 : ESRCH); | |
1186 | } | |
1187 | ||
1188 | /* | |
1189 | * Send a signal to a process group. | |
1190 | */ | |
1191 | void | |
1192 | gsignal(pgid, signum) | |
1193 | int pgid, signum; | |
1194 | { | |
1195 | struct pgrp *pgrp; | |
1196 | ||
1197 | if (pgid && (pgrp = pgfind(pgid))) | |
1198 | pgsignal(pgrp, signum, 0); | |
1199 | } | |
1200 | ||
1201 | /* | |
1202 | * Send a signal to a process group. If checktty is 1, | |
1203 | * limit to members which have a controlling terminal. | |
1204 | */ | |
1205 | void | |
1206 | pgsignal(pgrp, signum, checkctty) | |
1207 | struct pgrp *pgrp; | |
1208 | int signum, checkctty; | |
1209 | { | |
1210 | register struct proc *p; | |
1211 | ||
1212 | if (pgrp) | |
1213 | for (p = pgrp->pg_members.lh_first; p != 0; | |
1214 | p = p->p_pglist.le_next) | |
1215 | if (checkctty == 0 || p->p_flag & P_CONTROLT) | |
1216 | psignal(p, signum); | |
1217 | } | |
1218 | ||
9bccf70c A |
1219 | /* |
1220 | * Send signal to a backgrounded process blocked due to tty access | |
1221 | * In FreeBSD, the backgrounded process wakes up every second and | |
1222 | * discovers whether it is foregounded or not. In our case, we block | |
1223 | * the thread in tsleep as we want to avoid storm of processes as well | |
1224 | * as the suspend is only at AST level | |
1225 | */ | |
1226 | void | |
1227 | tty_pgsignal(pgrp, signum) | |
1228 | struct pgrp *pgrp; | |
1229 | int signum; | |
1230 | { | |
1231 | register struct proc *p; | |
1232 | ||
1233 | if (pgrp) | |
1234 | for (p = pgrp->pg_members.lh_first; p != 0; | |
1235 | p = p->p_pglist.le_next) | |
1236 | if ((p->p_flag & P_TTYSLEEP) && (p->p_flag & P_CONTROLT)) | |
1237 | psignal(p, signum); | |
1238 | } | |
1239 | ||
1c79356b A |
1240 | /* |
1241 | * Send a signal caused by a trap to a specific thread. | |
1242 | */ | |
1243 | void | |
1244 | threadsignal(sig_actthread, signum, code) | |
9bccf70c | 1245 | register thread_act_t sig_actthread; |
1c79356b A |
1246 | register int signum; |
1247 | u_long code; | |
1248 | { | |
1249 | register struct uthread *uth; | |
1250 | register struct task * sig_task; | |
1251 | register struct proc *p ; | |
1252 | int mask; | |
1253 | ||
1254 | if ((u_int)signum >= NSIG || signum == 0) | |
1255 | return; | |
1256 | ||
1257 | mask = sigmask(signum); | |
1258 | if ((mask & threadmask) == 0) | |
1259 | return; | |
1260 | sig_task = get_threadtask(sig_actthread); | |
1c79356b A |
1261 | p = (struct proc *)(get_bsdtask_info(sig_task)); |
1262 | ||
0b4e3aa0 A |
1263 | uth = get_bsdthread_info(sig_actthread); |
1264 | if (uth && (uth->uu_flag & P_VFORK)) | |
1265 | p = uth->uu_proc; | |
1266 | ||
1c79356b A |
1267 | if (!(p->p_flag & P_TRACED) && (p->p_sigignore & mask)) |
1268 | return; | |
1269 | ||
9bccf70c A |
1270 | uth->uu_siglist |= mask; |
1271 | p->p_siglist |= mask; /* just for lame ones looking here */ | |
1c79356b A |
1272 | uth->uu_code = code; |
1273 | /* mark on process as well */ | |
1c79356b A |
1274 | signal_setast(sig_actthread); |
1275 | } | |
1276 | ||
1277 | ||
1c79356b A |
1278 | void |
1279 | psignal(p, signum) | |
1280 | register struct proc *p; | |
1281 | register int signum; | |
1282 | { | |
9bccf70c | 1283 | psignal_lock(p, signum, 1); |
1c79356b A |
1284 | } |
1285 | ||
0b4e3aa0 A |
1286 | void |
1287 | psignal_vfork(p, new_task, thr_act, signum) | |
1288 | register struct proc *p; | |
1289 | task_t new_task; | |
1290 | thread_act_t thr_act; | |
1291 | register int signum; | |
1292 | { | |
1293 | int withlock = 1; | |
1294 | int pend = 0; | |
1295 | register int s, prop; | |
1296 | register sig_t action; | |
1297 | int mask; | |
1298 | kern_return_t kret; | |
9bccf70c | 1299 | struct uthread *uth; |
0b4e3aa0 A |
1300 | |
1301 | if ((u_int)signum >= NSIG || signum == 0) | |
1302 | panic("psignal signal number"); | |
1303 | mask = sigmask(signum); | |
1304 | prop = sigprop[signum]; | |
1305 | ||
1306 | #if SIGNAL_DEBUG | |
1307 | if(rdebug_proc && (p == rdebug_proc)) { | |
1308 | ram_printf(3); | |
1309 | } | |
1310 | #endif /* SIGNAL_DEBUG */ | |
1311 | ||
1312 | if ((new_task == TASK_NULL) || (thr_act == (thread_act_t)NULL) || is_kerneltask(new_task)) | |
1313 | return; | |
1314 | ||
1315 | ||
9bccf70c | 1316 | uth = get_bsdthread_info(thr_act); |
0b4e3aa0 A |
1317 | signal_lock(p); |
1318 | ||
1319 | /* | |
1320 | * proc is traced, always give parent a chance. | |
1321 | */ | |
1322 | action = SIG_DFL; | |
1323 | ||
1324 | if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && | |
1325 | (p->p_flag & P_TRACED) == 0) | |
1326 | p->p_nice = NZERO; | |
1327 | ||
9bccf70c | 1328 | if (prop & SA_CONT) { |
0b4e3aa0 | 1329 | p->p_siglist &= ~stopsigmask; |
9bccf70c A |
1330 | uth->uu_siglist &= ~stopsigmask; |
1331 | } | |
0b4e3aa0 A |
1332 | |
1333 | if (prop & SA_STOP) { | |
1334 | /* | |
1335 | * If sending a tty stop signal to a member of an orphaned | |
1336 | * process group, discard the signal here if the action | |
1337 | * is default; don't stop the process below if sleeping, | |
1338 | * and don't clear any pending SIGCONT. | |
1339 | */ | |
1340 | if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && | |
1341 | action == SIG_DFL) | |
1342 | goto psigout; | |
9bccf70c | 1343 | uth->uu_siglist &= ~contsigmask; |
0b4e3aa0 A |
1344 | p->p_siglist &= ~contsigmask; |
1345 | } | |
9bccf70c A |
1346 | uth->uu_siglist |= mask; |
1347 | p->p_siglist |= mask; /* just for lame ones looking here */ | |
0b4e3aa0 | 1348 | |
9bccf70c A |
1349 | /* Deliver signal to the activation passed in */ |
1350 | act_set_astbsd(thr_act); | |
0b4e3aa0 A |
1351 | |
1352 | /* | |
1353 | * SIGKILL priority twiddling moved here from above because | |
1354 | * it needs sig_thread. Could merge it into large switch | |
1355 | * below if we didn't care about priority for tracing | |
1356 | * as SIGKILL's action is always SIG_DFL. | |
1357 | */ | |
1358 | if ((signum == SIGKILL) && (p->p_nice > NZERO)) { | |
1359 | p->p_nice = NZERO; | |
0b4e3aa0 A |
1360 | } |
1361 | ||
1362 | /* | |
1363 | * This Process is traced - wake it up (if not already | |
1364 | * stopped) so that it can discover the signal in | |
1365 | * issig() and stop for the parent. | |
1366 | */ | |
1367 | if (p->p_flag & P_TRACED) { | |
1368 | if (p->p_stat != SSTOP) | |
1369 | goto run; | |
1370 | else | |
1371 | goto psigout; | |
1372 | } | |
1373 | run: | |
1374 | /* | |
1375 | * If we're being traced (possibly because someone attached us | |
1376 | * while we were stopped), check for a signal from the debugger. | |
1377 | */ | |
1378 | if (p->p_stat == SSTOP) { | |
9bccf70c A |
1379 | if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0) { |
1380 | uth->uu_siglist |= sigmask(p->p_xstat); | |
1381 | p->p_siglist |= mask; /* just for lame ones looking here */ | |
1382 | } | |
0b4e3aa0 A |
1383 | } |
1384 | ||
1385 | /* | |
1386 | * setrunnable(p) in BSD | |
1387 | */ | |
1388 | p->p_stat = SRUN; | |
1389 | ||
1390 | psigout: | |
1391 | signal_unlock(p); | |
1392 | } | |
1393 | ||
9bccf70c A |
1394 | thread_act_t |
1395 | get_signalthread(struct proc *p, int signum) | |
1396 | { | |
1397 | struct uthread *uth; | |
1398 | thread_act_t thr_act; | |
1399 | sigset_t mask = sigmask(signum); | |
1400 | thread_act_t sig_thread_act; | |
1401 | struct task * sig_task = p->task; | |
9bccf70c A |
1402 | kern_return_t kret; |
1403 | ||
1404 | if ((p->p_flag & P_INVFORK) && p->p_vforkact) { | |
1405 | sig_thread_act = p->p_vforkact; | |
55e303ae | 1406 | kret = check_actforsig(sig_task, sig_thread_act, 1); |
9bccf70c A |
1407 | if (kret == KERN_SUCCESS) |
1408 | return(sig_thread_act); | |
1409 | else | |
1410 | return(THR_ACT_NULL); | |
1411 | } | |
1412 | ||
1413 | TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) { | |
1414 | if(((uth->uu_flag & UNO_SIGMASK)== 0) && | |
1415 | (((uth->uu_sigmask & mask) == 0) || (uth->uu_sigwait & mask))) { | |
55e303ae | 1416 | if (check_actforsig(p->task, uth->uu_act, 1) == KERN_SUCCESS) |
9bccf70c A |
1417 | return(uth->uu_act); |
1418 | } | |
1419 | } | |
55e303ae | 1420 | if (get_signalact(p->task, &thr_act, 1) == KERN_SUCCESS) { |
9bccf70c A |
1421 | return(thr_act); |
1422 | } | |
1423 | ||
1424 | return(THR_ACT_NULL); | |
1425 | } | |
1426 | ||
1c79356b A |
1427 | /* |
1428 | * Send the signal to the process. If the signal has an action, the action | |
1429 | * is usually performed by the target process rather than the caller; we add | |
1430 | * the signal to the set of pending signals for the process. | |
1431 | * | |
1432 | * Exceptions: | |
1433 | * o When a stop signal is sent to a sleeping process that takes the | |
1434 | * default action, the process is stopped without awakening it. | |
1435 | * o SIGCONT restarts stopped processes (or puts them back to sleep) | |
1436 | * regardless of the signal action (eg, blocked or ignored). | |
1437 | * | |
1438 | * Other ignored signals are discarded immediately. | |
1439 | */ | |
1440 | void | |
9bccf70c | 1441 | psignal_lock(p, signum, withlock) |
1c79356b A |
1442 | register struct proc *p; |
1443 | register int signum; | |
1444 | register int withlock; | |
1c79356b A |
1445 | { |
1446 | register int s, prop; | |
1447 | register sig_t action; | |
1448 | thread_act_t sig_thread_act; | |
1c79356b | 1449 | register task_t sig_task; |
1c79356b | 1450 | int mask; |
9bccf70c | 1451 | struct uthread *uth; |
1c79356b | 1452 | kern_return_t kret; |
7b1edb79 | 1453 | int sw_funnel = 0; |
1c79356b A |
1454 | |
1455 | if ((u_int)signum >= NSIG || signum == 0) | |
1456 | panic("psignal signal number"); | |
1457 | mask = sigmask(signum); | |
1458 | prop = sigprop[signum]; | |
1459 | ||
1460 | #if SIGNAL_DEBUG | |
1461 | if(rdebug_proc && (p == rdebug_proc)) { | |
1462 | ram_printf(3); | |
1463 | } | |
1464 | #endif /* SIGNAL_DEBUG */ | |
7b1edb79 A |
1465 | |
1466 | if (thread_funnel_get() == (funnel_t *)network_flock) { | |
1467 | sw_funnel = 1; | |
1468 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
1469 | } | |
1c79356b A |
1470 | /* |
1471 | * We will need the task pointer later. Grab it now to | |
1472 | * check for a zombie process. Also don't send signals | |
1473 | * to kernel internal tasks. | |
1474 | */ | |
7b1edb79 A |
1475 | if (((sig_task = p->task) == TASK_NULL) || is_kerneltask(sig_task)) { |
1476 | if (sw_funnel) | |
1477 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
1c79356b | 1478 | return; |
7b1edb79 | 1479 | } |
1c79356b | 1480 | |
55e303ae A |
1481 | s = splhigh(); |
1482 | KNOTE(&p->p_klist, NOTE_SIGNAL | signum); | |
1483 | splx(s); | |
1484 | ||
1c79356b A |
1485 | /* |
1486 | * do not send signals to the process that has the thread | |
1487 | * doing a reboot(). Not doing so will mark that thread aborted | |
1488 | * and can cause IO failures wich will cause data loss. | |
1489 | */ | |
7b1edb79 A |
1490 | if (ISSET(p->p_flag, P_REBOOT)) { |
1491 | if (sw_funnel) | |
1492 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
1c79356b | 1493 | return; |
7b1edb79 | 1494 | } |
1c79356b | 1495 | |
9bccf70c A |
1496 | if (withlock) |
1497 | signal_lock(p); | |
1498 | ||
1499 | /* | |
1500 | * Deliver the signal to the first thread in the task. This | |
1501 | * allows single threaded applications which use signals to | |
1502 | * be able to be linked with multithreaded libraries. We have | |
55e303ae | 1503 | * an implicit reference to the current thread, but need |
9bccf70c A |
1504 | * an explicit one otherwise. The thread reference keeps |
1505 | * the corresponding task data structures around too. This | |
1506 | * reference is released by thread_deallocate. | |
1c79356b | 1507 | */ |
9bccf70c A |
1508 | |
1509 | if (((p->p_flag & P_TRACED) == 0) && (p->p_sigignore & mask)) | |
1510 | goto psigout; | |
1511 | ||
9bccf70c A |
1512 | /* If successful return with ast set */ |
1513 | sig_thread_act = get_signalthread(p, signum); | |
1514 | ||
1515 | if (sig_thread_act == THR_ACT_NULL) { | |
1516 | /* XXXX FIXME | |
1517 | /* if it is sigkill, may be we should | |
1518 | * inject a thread to terminate | |
1519 | */ | |
1520 | #if SIGNAL_DEBUG | |
1521 | ram_printf(1); | |
1522 | #endif /* SIGNAL_DEBUG */ | |
1523 | goto psigout; | |
1c79356b A |
1524 | } |
1525 | ||
9bccf70c | 1526 | uth = get_bsdthread_info(sig_thread_act); |
1c79356b A |
1527 | |
1528 | /* | |
1529 | * If proc is traced, always give parent a chance. | |
1530 | */ | |
1531 | if (p->p_flag & P_TRACED) | |
1532 | action = SIG_DFL; | |
1533 | else { | |
1534 | /* | |
1535 | * If the signal is being ignored, | |
1536 | * then we forget about it immediately. | |
1537 | * (Note: we don't set SIGCONT in p_sigignore, | |
1538 | * and if it is set to SIG_IGN, | |
1539 | * action will be SIG_DFL here.) | |
1540 | */ | |
1541 | if (p->p_sigignore & mask) | |
1542 | goto psigout; | |
9bccf70c A |
1543 | if (uth->uu_sigwait & mask) |
1544 | action = SIG_WAIT; | |
1545 | if (uth->uu_sigmask & mask) | |
1c79356b A |
1546 | action = SIG_HOLD; |
1547 | else if (p->p_sigcatch & mask) | |
1548 | action = SIG_CATCH; | |
1549 | else | |
1550 | action = SIG_DFL; | |
1551 | } | |
1552 | ||
1553 | if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && | |
1554 | (p->p_flag & P_TRACED) == 0) | |
1555 | p->p_nice = NZERO; | |
1556 | ||
9bccf70c A |
1557 | if (prop & SA_CONT) { |
1558 | uth->uu_siglist &= ~stopsigmask; | |
1c79356b | 1559 | p->p_siglist &= ~stopsigmask; |
9bccf70c | 1560 | } |
1c79356b A |
1561 | |
1562 | if (prop & SA_STOP) { | |
1563 | /* | |
1564 | * If sending a tty stop signal to a member of an orphaned | |
1565 | * process group, discard the signal here if the action | |
1566 | * is default; don't stop the process below if sleeping, | |
1567 | * and don't clear any pending SIGCONT. | |
1568 | */ | |
1569 | if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && | |
1570 | action == SIG_DFL) | |
1571 | goto psigout; | |
9bccf70c | 1572 | uth->uu_siglist &= ~contsigmask; |
1c79356b A |
1573 | p->p_siglist &= ~contsigmask; |
1574 | } | |
9bccf70c A |
1575 | uth->uu_siglist |= mask; |
1576 | p->p_siglist |= mask; /* just for lame ones looking here */ | |
1c79356b | 1577 | |
9bccf70c | 1578 | |
1c79356b A |
1579 | /* |
1580 | * Defer further processing for signals which are held, | |
1581 | * except that stopped processes must be continued by SIGCONT. | |
1582 | */ | |
9bccf70c | 1583 | if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) { |
1c79356b | 1584 | goto psigout; |
9bccf70c A |
1585 | } |
1586 | /* | |
1587 | * SIGKILL priority twiddling moved here from above because | |
1588 | * it needs sig_thread. Could merge it into large switch | |
1589 | * below if we didn't care about priority for tracing | |
1590 | * as SIGKILL's action is always SIG_DFL. | |
1591 | */ | |
1592 | if ((signum == SIGKILL) && (p->p_nice > NZERO)) { | |
1593 | p->p_nice = NZERO; | |
1594 | } | |
1595 | ||
1596 | /* | |
1597 | * Process is traced - wake it up (if not already | |
1598 | * stopped) so that it can discover the signal in | |
1599 | * issig() and stop for the parent. | |
1600 | */ | |
1601 | if (p->p_flag & P_TRACED) { | |
1602 | if (p->p_stat != SSTOP) | |
1603 | goto run; | |
1604 | else | |
1605 | goto psigout; | |
1606 | } | |
1607 | ||
1608 | if (action == SIG_WAIT) { | |
1609 | uth->uu_sigwait = mask; | |
1610 | uth->uu_siglist &= ~mask; | |
1611 | p->p_siglist &= ~mask; | |
1612 | wakeup(&uth->uu_sigwait); | |
1613 | /* if it is SIGCONT resume whole process */ | |
1614 | if (prop & SA_CONT) | |
1615 | (void) task_resume(sig_task); | |
1616 | goto psigout; | |
1617 | } | |
1618 | ||
1619 | if (action != SIG_DFL) { | |
1620 | /* | |
1621 | * User wants to catch the signal. | |
1622 | * Wake up the thread, but don't un-suspend it | |
1623 | * (except for SIGCONT). | |
1624 | */ | |
55e303ae A |
1625 | if (prop & SA_CONT) { |
1626 | if (p->p_flag & P_TTYSLEEP) { | |
1627 | p->p_flag &= ~P_TTYSLEEP; | |
1628 | wakeup(&p->p_siglist); | |
1629 | } else { | |
1630 | (void) task_resume(sig_task); | |
1631 | } | |
1632 | p->p_stat = SRUN; | |
1633 | } | |
9bccf70c A |
1634 | goto run; |
1635 | } else { | |
1636 | /* Default action - varies */ | |
1637 | if (mask & stopsigmask) { | |
1638 | /* | |
1639 | * These are the signals which by default | |
1640 | * stop a process. | |
1641 | * | |
1642 | * Don't clog system with children of init | |
1643 | * stopped from the keyboard. | |
1644 | */ | |
1645 | if (!(prop & SA_STOP) && p->p_pptr == initproc) { | |
1646 | psignal_lock(p, SIGKILL, 0); | |
1647 | uth->uu_siglist &= ~mask; | |
1648 | p->p_siglist &= ~mask; | |
1649 | goto psigout; | |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * Stop the task | |
1654 | * if task hasn't already been stopped by | |
1655 | * a signal. | |
1656 | */ | |
1657 | uth->uu_siglist &= ~mask; | |
1658 | p->p_siglist &= ~mask; | |
1659 | if (p->p_stat != SSTOP) { | |
1660 | p->p_xstat = signum; | |
1661 | stop(p); | |
1662 | if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { | |
1663 | struct proc *pp = p->p_pptr; | |
1664 | ||
1665 | pp->si_pid = p->p_pid; | |
1666 | pp->si_status = p->p_xstat; | |
1667 | pp->si_code = CLD_STOPPED; | |
1668 | pp->si_uid = p->p_cred->p_ruid; | |
1669 | psignal(pp, SIGCHLD); | |
1670 | } | |
1671 | } | |
1672 | goto psigout; | |
1673 | } | |
1674 | ||
1675 | switch (signum) { | |
1676 | /* | |
1677 | * Signals ignored by default have been dealt | |
1678 | * with already, since their bits are on in | |
1679 | * p_sigignore. | |
1680 | */ | |
1681 | ||
1682 | case SIGKILL: | |
1683 | /* | |
1684 | * Kill signal always sets process running and | |
1685 | * unsuspends it. | |
1686 | */ | |
1687 | /* | |
1688 | * Process will be running after 'run' | |
1689 | */ | |
1690 | p->p_stat = SRUN; | |
1691 | ||
1692 | thread_abort(sig_thread_act); | |
1693 | ||
1694 | goto psigout; | |
1695 | ||
1696 | case SIGCONT: | |
1697 | /* | |
1698 | * Let the process run. If it's sleeping on an | |
1699 | * event, it remains so. | |
1700 | */ | |
1701 | if (p->p_flag & P_TTYSLEEP) { | |
1702 | p->p_flag &= ~P_TTYSLEEP; | |
1703 | wakeup(&p->p_siglist); | |
1704 | } else { | |
1705 | (void) task_resume(sig_task); | |
1706 | } | |
1707 | uth->uu_siglist &= ~mask; | |
1708 | p->p_siglist &= ~mask; | |
1709 | p->p_stat = SRUN; | |
1710 | ||
1711 | goto psigout; | |
1712 | ||
1713 | default: | |
1714 | /* | |
1715 | * All other signals wake up the process, but don't | |
1716 | * resume it. | |
1717 | */ | |
1718 | goto run; | |
1719 | } | |
1720 | } | |
1721 | /*NOTREACHED*/ | |
1722 | run: | |
1723 | /* | |
1724 | * If we're being traced (possibly because someone attached us | |
1725 | * while we were stopped), check for a signal from the debugger. | |
1726 | */ | |
1727 | if (p->p_stat == SSTOP) { | |
1728 | if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0) | |
1729 | uth->uu_siglist |= sigmask(p->p_xstat); | |
1730 | } else { | |
1731 | /* | |
1732 | * setrunnable(p) in BSD and | |
1733 | * Wake up the thread if it is interruptible. | |
1734 | */ | |
1735 | p->p_stat = SRUN; | |
1736 | thread_abort_safely(sig_thread_act); | |
1737 | } | |
1738 | psigout: | |
1739 | if (withlock) | |
1740 | signal_unlock(p); | |
1741 | if (sw_funnel) | |
1742 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
1743 | } | |
1744 | ||
1745 | ||
1746 | /* psignal_lock(p, signum, withlock ) */ | |
1747 | void | |
1748 | psignal_uthread(thr_act, signum) | |
1749 | thread_act_t thr_act; | |
1750 | int signum; | |
1751 | { | |
1752 | struct proc *p; | |
1753 | register int s, prop; | |
1754 | register sig_t action; | |
1755 | thread_act_t sig_thread_act; | |
9bccf70c | 1756 | register task_t sig_task; |
9bccf70c A |
1757 | int mask; |
1758 | struct uthread *uth; | |
1759 | kern_return_t kret; | |
1760 | int error = 0; | |
1761 | ||
1762 | p = (struct proc *)get_bsdtask_info(get_threadtask(thr_act)); | |
1763 | if ((u_int)signum >= NSIG || signum == 0) | |
1764 | panic("Invalid signal number in psignal_uthread"); | |
1765 | mask = sigmask(signum); | |
1766 | prop = sigprop[signum]; | |
1767 | ||
1768 | #if SIGNAL_DEBUG | |
1769 | if(rdebug_proc && (p == rdebug_proc)) { | |
1770 | ram_printf(3); | |
1771 | } | |
1772 | #endif /* SIGNAL_DEBUG */ | |
1773 | ||
1774 | /* | |
1775 | * We will need the task pointer later. Grab it now to | |
1776 | * check for a zombie process. Also don't send signals | |
1777 | * to kernel internal tasks. | |
1778 | */ | |
1779 | if (((sig_task = p->task) == TASK_NULL) || is_kerneltask(sig_task)) { | |
1780 | return; | |
1781 | } | |
1782 | ||
1783 | sig_thread_act = thr_act; | |
1784 | /* | |
1785 | * do not send signals to the process that has the thread | |
1786 | * doing a reboot(). Not doing so will mark that thread aborted | |
1787 | * and can cause IO failures wich will cause data loss. | |
1788 | */ | |
1789 | if (ISSET(p->p_flag, P_REBOOT)) { | |
1790 | return; | |
1791 | } | |
1792 | ||
1793 | signal_lock(p); | |
1794 | ||
1c79356b A |
1795 | /* |
1796 | * Deliver the signal to the first thread in the task. This | |
1797 | * allows single threaded applications which use signals to | |
1798 | * be able to be linked with multithreaded libraries. We have | |
55e303ae | 1799 | * an implicit reference to the current thread, but need |
1c79356b A |
1800 | * an explicit one otherwise. The thread reference keeps |
1801 | * the corresponding task data structures around too. This | |
1802 | * reference is released by thread_deallocate. | |
1803 | */ | |
1804 | ||
9bccf70c A |
1805 | if (((p->p_flag & P_TRACED) == 0) && (p->p_sigignore & mask)) |
1806 | goto puthout; | |
1807 | ||
55e303ae | 1808 | kret = check_actforsig(sig_task, sig_thread_act, 1); |
0b4e3aa0 | 1809 | |
9bccf70c A |
1810 | if (kret != KERN_SUCCESS) { |
1811 | error = EINVAL; | |
1812 | goto puthout; | |
1813 | } | |
0b4e3aa0 | 1814 | |
1c79356b | 1815 | |
9bccf70c | 1816 | uth = get_bsdthread_info(sig_thread_act); |
1c79356b | 1817 | |
9bccf70c A |
1818 | /* |
1819 | * If proc is traced, always give parent a chance. | |
1820 | */ | |
1821 | if (p->p_flag & P_TRACED) | |
1822 | action = SIG_DFL; | |
1823 | else { | |
1824 | /* | |
1825 | * If the signal is being ignored, | |
1826 | * then we forget about it immediately. | |
1827 | * (Note: we don't set SIGCONT in p_sigignore, | |
1828 | * and if it is set to SIG_IGN, | |
1829 | * action will be SIG_DFL here.) | |
1830 | */ | |
1831 | if (p->p_sigignore & mask) | |
1832 | goto puthout; | |
1833 | if (uth->uu_sigwait & mask) | |
1834 | action = SIG_WAIT; | |
1835 | if (uth->uu_sigmask & mask) | |
1836 | action = SIG_HOLD; | |
1837 | else if (p->p_sigcatch & mask) | |
1838 | action = SIG_CATCH; | |
1839 | else | |
1840 | action = SIG_DFL; | |
1c79356b A |
1841 | } |
1842 | ||
9bccf70c A |
1843 | if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && |
1844 | (p->p_flag & P_TRACED) == 0) | |
1845 | p->p_nice = NZERO; | |
1846 | ||
1847 | if (prop & SA_CONT) { | |
1848 | uth->uu_siglist &= ~stopsigmask; | |
1849 | p->p_siglist &= ~stopsigmask; | |
1850 | } | |
1851 | ||
1852 | if (prop & SA_STOP) { | |
1853 | /* | |
1854 | * If sending a tty stop signal to a member of an orphaned | |
1855 | * process group, discard the signal here if the action | |
1856 | * is default; don't stop the process below if sleeping, | |
1857 | * and don't clear any pending SIGCONT. | |
1858 | */ | |
1859 | if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && | |
1860 | action == SIG_DFL) | |
1861 | goto puthout; | |
1862 | uth->uu_siglist &= ~contsigmask; | |
1863 | p->p_siglist &= ~contsigmask; | |
1864 | } | |
1865 | uth->uu_siglist |= mask; | |
1866 | p->p_siglist |= mask; /* just for lame ones looking here */ | |
1867 | ||
1868 | /* | |
1869 | * Defer further processing for signals which are held, | |
1870 | * except that stopped processes must be continued by SIGCONT. | |
1871 | */ | |
1872 | if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) | |
1873 | goto puthout; | |
1874 | ||
1c79356b A |
1875 | /* |
1876 | * SIGKILL priority twiddling moved here from above because | |
1877 | * it needs sig_thread. Could merge it into large switch | |
1878 | * below if we didn't care about priority for tracing | |
1879 | * as SIGKILL's action is always SIG_DFL. | |
1880 | */ | |
1881 | if ((signum == SIGKILL) && (p->p_nice > NZERO)) { | |
1882 | p->p_nice = NZERO; | |
1c79356b A |
1883 | } |
1884 | ||
1885 | /* | |
1886 | * Process is traced - wake it up (if not already | |
1887 | * stopped) so that it can discover the signal in | |
1888 | * issig() and stop for the parent. | |
1889 | */ | |
1890 | if (p->p_flag & P_TRACED) { | |
1891 | if (p->p_stat != SSTOP) | |
9bccf70c | 1892 | goto psurun; |
1c79356b | 1893 | else |
9bccf70c A |
1894 | goto puthout; |
1895 | } | |
1896 | ||
1897 | if (action == SIG_WAIT) { | |
1898 | uth->uu_sigwait = mask; | |
1899 | uth->uu_siglist &= ~mask; | |
1900 | p->p_siglist &= ~mask; | |
1901 | wakeup(&uth->uu_sigwait); | |
1902 | /* if it is SIGCONT resume whole process */ | |
1903 | if (prop & SA_CONT) | |
1904 | (void) task_resume(sig_task); | |
1905 | goto puthout; | |
1c79356b A |
1906 | } |
1907 | ||
1908 | if (action != SIG_DFL) { | |
1909 | /* | |
1910 | * User wants to catch the signal. | |
1911 | * Wake up the thread, but don't un-suspend it | |
1912 | * (except for SIGCONT). | |
1913 | */ | |
1914 | if (prop & SA_CONT) | |
1915 | (void) task_resume(sig_task); | |
9bccf70c | 1916 | goto psurun; |
1c79356b A |
1917 | } else { |
1918 | /* Default action - varies */ | |
1919 | if (mask & stopsigmask) { | |
1920 | /* | |
1921 | * These are the signals which by default | |
1922 | * stop a process. | |
1923 | * | |
1924 | * Don't clog system with children of init | |
1925 | * stopped from the keyboard. | |
1926 | */ | |
1927 | if (!(prop & SA_STOP) && p->p_pptr == initproc) { | |
9bccf70c A |
1928 | psignal_lock(p, SIGKILL, 0); |
1929 | uth->uu_siglist &= ~mask; | |
1c79356b | 1930 | p->p_siglist &= ~mask; |
9bccf70c | 1931 | goto puthout; |
1c79356b A |
1932 | } |
1933 | ||
1934 | /* | |
9bccf70c A |
1935 | * Stop the task |
1936 | * if task hasn't already been stopped by | |
1937 | * a signal. | |
1c79356b | 1938 | */ |
9bccf70c A |
1939 | uth->uu_siglist &= ~mask; |
1940 | p->p_siglist &= ~mask; | |
1941 | if (p->p_stat != SSTOP) { | |
1942 | p->p_xstat = signum; | |
1943 | if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { | |
1944 | struct proc *pp = p->p_pptr; | |
1945 | ||
1946 | pp->si_pid = p->p_pid; | |
1947 | pp->si_status = p->p_xstat; | |
1948 | pp->si_code = CLD_STOPPED; | |
1949 | pp->si_uid = p->p_cred->p_ruid; | |
1950 | psignal(pp, SIGCHLD); | |
1c79356b | 1951 | } |
9bccf70c | 1952 | stop(p); |
1c79356b | 1953 | } |
9bccf70c | 1954 | goto puthout; |
1c79356b A |
1955 | } |
1956 | ||
1957 | switch (signum) { | |
1958 | /* | |
1959 | * Signals ignored by default have been dealt | |
1960 | * with already, since their bits are on in | |
1961 | * p_sigignore. | |
1962 | */ | |
1963 | ||
1964 | case SIGKILL: | |
1965 | /* | |
1966 | * Kill signal always sets process running and | |
1967 | * unsuspends it. | |
1968 | */ | |
1969 | /* | |
1970 | * Process will be running after 'run' | |
1971 | */ | |
1972 | p->p_stat = SRUN; | |
1973 | ||
1974 | thread_abort(sig_thread_act); | |
1975 | ||
9bccf70c | 1976 | goto puthout; |
1c79356b A |
1977 | |
1978 | case SIGCONT: | |
1979 | /* | |
1980 | * Let the process run. If it's sleeping on an | |
1981 | * event, it remains so. | |
1982 | */ | |
1983 | if (p->p_flag & P_TTYSLEEP) { | |
1984 | p->p_flag &= ~P_TTYSLEEP; | |
1985 | wakeup(&p->p_siglist); | |
1986 | } else { | |
1987 | (void) task_resume(sig_task); | |
1988 | } | |
9bccf70c | 1989 | uth->uu_siglist &= ~mask; |
1c79356b A |
1990 | p->p_siglist &= ~mask; |
1991 | p->p_stat = SRUN; | |
9bccf70c | 1992 | goto puthout; |
1c79356b A |
1993 | |
1994 | default: | |
1995 | /* | |
1996 | * All other signals wake up the process, but don't | |
1997 | * resume it. | |
1998 | */ | |
9bccf70c | 1999 | goto psurun; |
1c79356b A |
2000 | } |
2001 | } | |
2002 | /*NOTREACHED*/ | |
9bccf70c | 2003 | psurun: |
1c79356b A |
2004 | /* |
2005 | * If we're being traced (possibly because someone attached us | |
2006 | * while we were stopped), check for a signal from the debugger. | |
2007 | */ | |
2008 | if (p->p_stat == SSTOP) { | |
9bccf70c A |
2009 | if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0) { |
2010 | uth->uu_siglist |= sigmask(p->p_xstat); | |
1c79356b | 2011 | p->p_siglist |= sigmask(p->p_xstat); |
9bccf70c A |
2012 | } |
2013 | } else { | |
2014 | /* | |
2015 | * setrunnable(p) in BSD and | |
2016 | * Wake up the thread if it is interruptible. | |
2017 | */ | |
2018 | p->p_stat = SRUN; | |
2019 | thread_abort_safely(sig_thread_act); | |
1c79356b A |
2020 | } |
2021 | ||
9bccf70c | 2022 | puthout: |
1c79356b A |
2023 | signal_unlock(p); |
2024 | } | |
2025 | ||
9bccf70c | 2026 | |
1c79356b A |
2027 | __inline__ void |
2028 | sig_lock_to_exit( | |
2029 | struct proc *p) | |
2030 | { | |
55e303ae | 2031 | thread_t self = current_act(); |
1c79356b A |
2032 | |
2033 | p->exit_thread = self; | |
2034 | (void) task_suspend(p->task); | |
2035 | } | |
2036 | ||
2037 | __inline__ int | |
2038 | sig_try_locked( | |
2039 | struct proc *p) | |
2040 | { | |
55e303ae | 2041 | thread_t self = current_act(); |
1c79356b A |
2042 | |
2043 | while (p->sigwait || p->exit_thread) { | |
2044 | if (p->exit_thread) { | |
2045 | if (p->exit_thread != self) { | |
2046 | /* | |
2047 | * Already exiting - no signals. | |
2048 | */ | |
55e303ae | 2049 | thread_abort(self); |
1c79356b A |
2050 | } |
2051 | return(0); | |
2052 | } | |
2053 | if(assert_wait_possible()) { | |
2054 | assert_wait((caddr_t)&p->sigwait_thread, | |
2055 | (THREAD_INTERRUPTIBLE)); | |
2056 | } | |
2057 | signal_unlock(p); | |
9bccf70c | 2058 | thread_block(THREAD_CONTINUE_NULL); |
1c79356b A |
2059 | signal_lock(p); |
2060 | if (thread_should_abort(self)) { | |
2061 | /* | |
2062 | * Terminate request - clean up. | |
2063 | */ | |
2064 | return -1; | |
2065 | } | |
2066 | } | |
2067 | return 1; | |
2068 | } | |
2069 | ||
2070 | /* | |
2071 | * If the current process has received a signal (should be caught or cause | |
2072 | * termination, should interrupt current syscall), return the signal number. | |
2073 | * Stop signals with default action are processed immediately, then cleared; | |
2074 | * they aren't returned. This is checked after each entry to the system for | |
2075 | * a syscall or trap (though this can usually be done without calling issignal | |
2076 | * by checking the pending signal masks in the CURSIG macro.) The normal call | |
2077 | * sequence is | |
2078 | * | |
2079 | * while (signum = CURSIG(curproc)) | |
2080 | * postsig(signum); | |
2081 | */ | |
2082 | int | |
2083 | issignal(p) | |
2084 | register struct proc *p; | |
2085 | { | |
2086 | register int signum, mask, prop, sigbits; | |
2087 | task_t task = p->task; | |
1c79356b A |
2088 | thread_act_t cur_act; |
2089 | int s; | |
2090 | struct uthread * ut; | |
2091 | kern_return_t kret; | |
9bccf70c | 2092 | struct proc *pp; |
1c79356b | 2093 | |
1c79356b A |
2094 | cur_act = current_act(); |
2095 | ||
9bccf70c A |
2096 | #if SIGNAL_DEBUG |
2097 | if(rdebug_proc && (p == rdebug_proc)) { | |
2098 | ram_printf(3); | |
2099 | } | |
2100 | #endif /* SIGNAL_DEBUG */ | |
1c79356b A |
2101 | signal_lock(p); |
2102 | ||
1c79356b A |
2103 | /* |
2104 | * Try to grab the signal lock. | |
2105 | */ | |
2106 | if (sig_try_locked(p) <= 0) { | |
2107 | signal_unlock(p); | |
2108 | return (0); | |
2109 | } | |
2110 | ||
2111 | ut = get_bsdthread_info(cur_act); | |
2112 | for(;;) { | |
9bccf70c | 2113 | sigbits = ut->uu_siglist & ~ut->uu_sigmask; |
1c79356b A |
2114 | |
2115 | if (p->p_flag & P_PPWAIT) | |
2116 | sigbits &= ~stopsigmask; | |
2117 | if (sigbits == 0) { /* no signal to send */ | |
2118 | signal_unlock(p); | |
2119 | return (0); | |
2120 | } | |
2121 | signum = ffs((long)sigbits); | |
2122 | mask = sigmask(signum); | |
2123 | prop = sigprop[signum]; | |
2124 | ||
1c79356b A |
2125 | /* |
2126 | * We should see pending but ignored signals | |
2127 | * only if P_TRACED was on when they were posted. | |
2128 | */ | |
2129 | if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) { | |
9bccf70c | 2130 | ut->uu_siglist &= ~mask; /* take the signal! */ |
1c79356b A |
2131 | p->p_siglist &= ~mask; /* take the signal! */ |
2132 | continue; | |
2133 | } | |
9bccf70c | 2134 | if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { |
1c79356b A |
2135 | register int hold; |
2136 | register task_t task; | |
2137 | /* | |
2138 | * If traced, always stop, and stay | |
2139 | * stopped until released by the debugger. | |
2140 | */ | |
2141 | /* ptrace debugging */ | |
2142 | p->p_xstat = signum; | |
9bccf70c A |
2143 | pp = p->p_pptr; |
2144 | if (p->p_flag & P_SIGEXC) { | |
2145 | p->sigwait = TRUE; | |
2146 | p->sigwait_thread = cur_act; | |
2147 | p->p_stat = SSTOP; | |
2148 | p->p_flag &= ~P_WAITED; | |
2149 | ut->uu_siglist &= ~mask; /* clear the old signal */ | |
2150 | p->p_siglist &= ~mask; /* clear the old signal */ | |
2151 | signal_unlock(p); | |
2152 | do_bsdexception(EXC_SOFTWARE, EXC_SOFT_SIGNAL, signum); | |
2153 | signal_lock(p); | |
2154 | } else { | |
55e303ae | 2155 | // panic("Unsupportef gdb option \n");; |
9bccf70c A |
2156 | pp->si_pid = p->p_pid; |
2157 | pp->si_status = p->p_xstat; | |
2158 | pp->si_code = CLD_TRAPPED; | |
2159 | pp->si_uid = p->p_cred->p_ruid; | |
2160 | psignal(pp, SIGCHLD); | |
2161 | /* | |
2162 | * XXX Have to really stop for debuggers; | |
2163 | * XXX stop() doesn't do the right thing. | |
2164 | * XXX Inline the task_suspend because we | |
2165 | * XXX have to diddle Unix state in the | |
2166 | * XXX middle of it. | |
2167 | */ | |
2168 | task = p->task; | |
2169 | task_hold(task); | |
2170 | p->sigwait = TRUE; | |
2171 | p->sigwait_thread = cur_act; | |
2172 | p->p_stat = SSTOP; | |
2173 | p->p_flag &= ~P_WAITED; | |
2174 | ut->uu_siglist &= ~mask; /* clear the old signal */ | |
2175 | p->p_siglist &= ~mask; /* clear the old signal */ | |
2176 | ||
2177 | wakeup((caddr_t)p->p_pptr); | |
2178 | signal_unlock(p); | |
2179 | assert_wait((caddr_t)&p->sigwait, (THREAD_INTERRUPTIBLE)); | |
2180 | thread_block(THREAD_CONTINUE_NULL); | |
2181 | signal_lock(p); | |
2182 | } | |
2183 | ||
1c79356b A |
2184 | p->sigwait = FALSE; |
2185 | p->sigwait_thread = NULL; | |
2186 | wakeup((caddr_t)&p->sigwait_thread); | |
2187 | ||
2188 | /* | |
2189 | * This code is to detect when gdb is killed | |
2190 | * even as the traced program is attached. | |
2191 | * pgsignal would get the SIGKILL to traced program | |
2192 | * That's what we are trying to see (I hope) | |
2193 | */ | |
9bccf70c | 2194 | if (ut->uu_siglist & sigmask(SIGKILL)) { |
1c79356b A |
2195 | /* |
2196 | * Wait event may still be outstanding; | |
2197 | * clear it, since sig_lock_to_exit will | |
2198 | * wait. | |
2199 | */ | |
55e303ae | 2200 | clear_wait(current_act(), THREAD_INTERRUPTED); |
1c79356b A |
2201 | sig_lock_to_exit(p); |
2202 | /* | |
2203 | * Since this thread will be resumed | |
2204 | * to allow the current syscall to | |
2205 | * be completed, must save u_qsave | |
2206 | * before calling exit(). (Since exit() | |
2207 | * calls closef() which can trash u_qsave.) | |
2208 | */ | |
2209 | signal_unlock(p); | |
0b4e3aa0 | 2210 | exit1(p,signum, (int *)NULL); |
1c79356b A |
2211 | return(0); |
2212 | } | |
2213 | ||
2214 | /* | |
2215 | * We may have to quit | |
2216 | */ | |
55e303ae | 2217 | if (thread_should_abort(current_act())) { |
1c79356b A |
2218 | signal_unlock(p); |
2219 | return(0); | |
2220 | } | |
2221 | /* | |
2222 | * If parent wants us to take the signal, | |
2223 | * then it will leave it in p->p_xstat; | |
2224 | * otherwise we just look for signals again. | |
2225 | */ | |
2226 | signum = p->p_xstat; | |
2227 | if (signum == 0) | |
2228 | continue; | |
2229 | /* | |
2230 | * Put the new signal into p_siglist. If the | |
2231 | * signal is being masked, look for other signals. | |
2232 | */ | |
2233 | mask = sigmask(signum); | |
9bccf70c A |
2234 | ut->uu_siglist |= mask; |
2235 | p->p_siglist |= mask; /* just for lame ones looking here */ | |
2236 | if (ut->uu_sigmask & mask) | |
1c79356b A |
2237 | continue; |
2238 | } | |
2239 | ||
2240 | /* | |
2241 | * Decide whether the signal should be returned. | |
2242 | * Return the signal's number, or fall through | |
2243 | * to clear it from the pending mask. | |
2244 | */ | |
2245 | ||
2246 | switch ((long)p->p_sigacts->ps_sigact[signum]) { | |
2247 | ||
2248 | case (long)SIG_DFL: | |
2249 | /* | |
2250 | * Don't take default actions on system processes. | |
2251 | */ | |
2252 | if (p->p_pptr->p_pid == 0) { | |
2253 | #if DIAGNOSTIC | |
2254 | /* | |
2255 | * Are you sure you want to ignore SIGSEGV | |
2256 | * in init? XXX | |
2257 | */ | |
2258 | printf("Process (pid %d) got signal %d\n", | |
2259 | p->p_pid, signum); | |
2260 | #endif | |
2261 | break; /* == ignore */ | |
2262 | } | |
2263 | ||
2264 | /* | |
2265 | * If there is a pending stop signal to process | |
2266 | * with default action, stop here, | |
2267 | * then clear the signal. However, | |
2268 | * if process is member of an orphaned | |
2269 | * process group, ignore tty stop signals. | |
2270 | */ | |
2271 | if (prop & SA_STOP) { | |
2272 | if (p->p_flag & P_TRACED || | |
2273 | (p->p_pgrp->pg_jobc == 0 && | |
2274 | prop & SA_TTYSTOP)) | |
2275 | break; /* == ignore */ | |
9bccf70c A |
2276 | if (p->p_stat != SSTOP) { |
2277 | p->p_xstat = signum; | |
2278 | stop(p); | |
2279 | if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { | |
2280 | pp = p->p_pptr; | |
2281 | pp->si_pid = p->p_pid; | |
2282 | pp->si_status = p->p_xstat; | |
2283 | pp->si_code = CLD_STOPPED; | |
2284 | pp->si_uid = p->p_cred->p_ruid; | |
2285 | psignal(pp, SIGCHLD); | |
2286 | } | |
1c79356b A |
2287 | } |
2288 | break; | |
2289 | } else if (prop & SA_IGNORE) { | |
2290 | /* | |
2291 | * Except for SIGCONT, shouldn't get here. | |
2292 | * Default action is to ignore; drop it. | |
2293 | */ | |
2294 | break; /* == ignore */ | |
2295 | } else { | |
9bccf70c A |
2296 | ut->uu_siglist &= ~mask; /* take the signal! */ |
2297 | p->p_siglist &= ~mask; /* take the signal! */ | |
1c79356b A |
2298 | signal_unlock(p); |
2299 | return (signum); | |
2300 | } | |
2301 | /*NOTREACHED*/ | |
2302 | ||
2303 | case (long)SIG_IGN: | |
2304 | /* | |
2305 | * Masking above should prevent us ever trying | |
2306 | * to take action on an ignored signal other | |
2307 | * than SIGCONT, unless process is traced. | |
2308 | */ | |
2309 | if ((prop & SA_CONT) == 0 && | |
2310 | (p->p_flag & P_TRACED) == 0) | |
2311 | printf("issignal\n"); | |
2312 | break; /* == ignore */ | |
2313 | ||
2314 | default: | |
2315 | /* | |
2316 | * This signal has an action, let | |
2317 | * postsig() process it. | |
2318 | */ | |
9bccf70c | 2319 | ut->uu_siglist &= ~mask; /* take the signal! */ |
0b4e3aa0 | 2320 | p->p_siglist &= ~mask; /* take the signal! */ |
1c79356b A |
2321 | signal_unlock(p); |
2322 | return (signum); | |
2323 | } | |
9bccf70c | 2324 | ut->uu_siglist &= ~mask; /* take the signal! */ |
1c79356b | 2325 | p->p_siglist &= ~mask; /* take the signal! */ |
1c79356b A |
2326 | } |
2327 | /* NOTREACHED */ | |
2328 | } | |
2329 | ||
2330 | /* called from _sleep */ | |
2331 | int | |
2332 | CURSIG(p) | |
2333 | register struct proc *p; | |
2334 | { | |
2335 | register int signum, mask, prop, sigbits; | |
2336 | task_t task = p->task; | |
1c79356b A |
2337 | thread_act_t cur_act; |
2338 | int s; | |
2339 | struct uthread * ut; | |
2340 | int retnum = 0; | |
2341 | ||
1c79356b | 2342 | |
1c79356b A |
2343 | cur_act = current_act(); |
2344 | ||
2345 | ut = get_bsdthread_info(cur_act); | |
2346 | ||
9bccf70c A |
2347 | if (ut->uu_siglist == 0) |
2348 | return (0); | |
2349 | ||
2350 | if (((ut->uu_siglist & ~ut->uu_sigmask) == 0) && ((p->p_flag & P_TRACED) == 0)) | |
2351 | return (0); | |
2352 | ||
2353 | sigbits = ut->uu_siglist & ~ut->uu_sigmask; | |
1c79356b A |
2354 | |
2355 | for(;;) { | |
2356 | if (p->p_flag & P_PPWAIT) | |
2357 | sigbits &= ~stopsigmask; | |
2358 | if (sigbits == 0) { /* no signal to send */ | |
2359 | return (retnum); | |
2360 | } | |
2361 | ||
2362 | signum = ffs((long)sigbits); | |
2363 | mask = sigmask(signum); | |
2364 | prop = sigprop[signum]; | |
2365 | ||
2366 | /* | |
2367 | * We should see pending but ignored signals | |
2368 | * only if P_TRACED was on when they were posted. | |
2369 | */ | |
2370 | if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) { | |
2371 | continue; | |
2372 | } | |
2373 | if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { | |
2374 | /* | |
2375 | * Put the new signal into p_siglist. If the | |
2376 | * signal is being masked, look for other signals. | |
2377 | */ | |
2378 | mask = sigmask(signum); | |
9bccf70c | 2379 | if (ut->uu_sigmask & mask) |
1c79356b A |
2380 | continue; |
2381 | return(signum); | |
2382 | } | |
2383 | ||
2384 | /* | |
2385 | * Decide whether the signal should be returned. | |
2386 | * Return the signal's number, or fall through | |
2387 | * to clear it from the pending mask. | |
2388 | */ | |
2389 | ||
2390 | switch ((long)p->p_sigacts->ps_sigact[signum]) { | |
2391 | ||
2392 | case (long)SIG_DFL: | |
2393 | /* | |
2394 | * Don't take default actions on system processes. | |
2395 | */ | |
2396 | if (p->p_pptr->p_pid == 0) { | |
2397 | #if DIAGNOSTIC | |
2398 | /* | |
2399 | * Are you sure you want to ignore SIGSEGV | |
2400 | * in init? XXX | |
2401 | */ | |
2402 | printf("Process (pid %d) got signal %d\n", | |
2403 | p->p_pid, signum); | |
2404 | #endif | |
2405 | break; /* == ignore */ | |
2406 | } | |
2407 | ||
2408 | /* | |
2409 | * If there is a pending stop signal to process | |
2410 | * with default action, stop here, | |
2411 | * then clear the signal. However, | |
2412 | * if process is member of an orphaned | |
2413 | * process group, ignore tty stop signals. | |
2414 | */ | |
2415 | if (prop & SA_STOP) { | |
2416 | if (p->p_flag & P_TRACED || | |
2417 | (p->p_pgrp->pg_jobc == 0 && | |
2418 | prop & SA_TTYSTOP)) | |
2419 | break; /* == ignore */ | |
2420 | retnum = signum; | |
2421 | break; | |
2422 | } else if (prop & SA_IGNORE) { | |
2423 | /* | |
2424 | * Except for SIGCONT, shouldn't get here. | |
2425 | * Default action is to ignore; drop it. | |
2426 | */ | |
2427 | break; /* == ignore */ | |
2428 | } else { | |
2429 | return (signum); | |
2430 | } | |
2431 | /*NOTREACHED*/ | |
2432 | ||
2433 | case (long)SIG_IGN: | |
2434 | /* | |
2435 | * Masking above should prevent us ever trying | |
2436 | * to take action on an ignored signal other | |
2437 | * than SIGCONT, unless process is traced. | |
2438 | */ | |
2439 | if ((prop & SA_CONT) == 0 && | |
2440 | (p->p_flag & P_TRACED) == 0) | |
2441 | printf("issignal\n"); | |
2442 | break; /* == ignore */ | |
2443 | ||
2444 | default: | |
2445 | /* | |
2446 | * This signal has an action, let | |
2447 | * postsig() process it. | |
2448 | */ | |
2449 | return (signum); | |
2450 | } | |
2451 | sigbits &= ~mask; /* take the signal! */ | |
2452 | } | |
2453 | /* NOTREACHED */ | |
2454 | } | |
2455 | ||
2456 | /* | |
2457 | * Put the argument process into the stopped state and notify the parent | |
2458 | * via wakeup. Signals are handled elsewhere. The process must not be | |
2459 | * on the run queue. | |
2460 | */ | |
2461 | void | |
2462 | stop(p) | |
2463 | register struct proc *p; | |
2464 | { | |
2465 | p->p_stat = SSTOP; | |
2466 | p->p_flag &= ~P_WAITED; | |
2467 | wakeup((caddr_t)p->p_pptr); | |
2468 | (void) task_suspend(p->task); /*XXX*/ | |
2469 | } | |
2470 | ||
2471 | /* | |
2472 | * Take the action for the specified signal | |
2473 | * from the current set of pending signals. | |
2474 | */ | |
2475 | void | |
2476 | postsig(signum) | |
2477 | register int signum; | |
2478 | { | |
2479 | register struct proc *p = current_proc(); | |
2480 | register struct sigacts *ps = p->p_sigacts; | |
2481 | register sig_t action; | |
2482 | u_long code; | |
2483 | int mask, returnmask; | |
9bccf70c | 2484 | struct uthread * ut; |
1c79356b A |
2485 | |
2486 | #if DIAGNOSTIC | |
2487 | if (signum == 0) | |
2488 | panic("postsig"); | |
2489 | /* | |
2490 | * This must be called on master cpu | |
2491 | */ | |
2492 | if (cpu_number() != master_cpu) | |
2493 | panic("psig not on master"); | |
2494 | #endif | |
2495 | ||
2496 | signal_lock(p); | |
2497 | /* | |
2498 | * Try to grab the signal lock. | |
2499 | */ | |
2500 | if (sig_try_locked(p) <= 0) { | |
9bccf70c | 2501 | signal_unlock(p); |
1c79356b A |
2502 | return; |
2503 | } | |
2504 | ||
9bccf70c | 2505 | ut = (struct uthread *)get_bsdthread_info(current_act()); |
1c79356b | 2506 | mask = sigmask(signum); |
9bccf70c | 2507 | ut->uu_siglist &= ~mask; |
1c79356b A |
2508 | p->p_siglist &= ~mask; |
2509 | action = ps->ps_sigact[signum]; | |
2510 | #if KTRACE | |
2511 | if (KTRPOINT(p, KTR_PSIG)) | |
2512 | ktrpsig(p->p_tracep, | |
9bccf70c A |
2513 | signum, action, ut->uu_flag & USAS_OLDMASK ? |
2514 | &ut->uu_oldmask : &ut->uu_sigmask, 0, -1); | |
1c79356b A |
2515 | #endif |
2516 | if (action == SIG_DFL) { | |
2517 | /* | |
2518 | * Default action, where the default is to kill | |
2519 | * the process. (Other cases were ignored above.) | |
2520 | */ | |
2521 | /* called with signal_lock() held */ | |
2522 | sigexit_locked(p, signum); | |
2523 | return; | |
2524 | /* NOTREACHED */ | |
2525 | } else { | |
2526 | /* | |
2527 | * If we get here, the signal must be caught. | |
2528 | */ | |
2529 | #if DIAGNOSTIC | |
9bccf70c | 2530 | if (action == SIG_IGN || (ut->uu_sigmask & mask)) |
1c79356b A |
2531 | log(LOG_WARNING, |
2532 | "postsig: processing masked or ignored signal\n"); | |
2533 | #endif | |
2534 | /* | |
2535 | * Set the new mask value and also defer further | |
2536 | * occurences of this signal. | |
2537 | * | |
2538 | * Special case: user has done a sigpause. Here the | |
2539 | * current mask is not of interest, but rather the | |
2540 | * mask from before the sigpause is what we want | |
2541 | * restored after the signal processing is completed. | |
2542 | */ | |
9bccf70c A |
2543 | if (ut->uu_flag & USAS_OLDMASK) { |
2544 | returnmask = ut->uu_oldmask; | |
2545 | ut->uu_flag &= ~USAS_OLDMASK; | |
2546 | ut->uu_oldmask = 0; | |
1c79356b | 2547 | } else |
9bccf70c A |
2548 | returnmask = ut->uu_sigmask; |
2549 | ut->uu_sigmask |= ps->ps_catchmask[signum]; | |
2550 | if ((ps->ps_signodefer & mask) == 0) | |
2551 | ut->uu_sigmask |= mask; | |
2552 | if ((signum != SIGILL) && (signum != SIGTRAP) && (ps->ps_sigreset & mask)) { | |
2553 | if ((signum != SIGCONT) && (sigprop[signum] & SA_IGNORE)) | |
2554 | p->p_sigignore |= mask; | |
2555 | ps->ps_sigact[signum] = SIG_DFL; | |
2556 | ps->ps_siginfo &= ~mask; | |
2557 | ps->ps_signodefer &= ~mask; | |
2558 | } | |
2559 | #ifdef __ppc__ | |
2560 | /* Needs to disable to run in user mode */ | |
2561 | if (signum == SIGFPE) { | |
2562 | thread_enable_fpe(current_act(), 0); | |
2563 | } | |
2564 | #endif /* __ppc__ */ | |
2565 | ||
1c79356b A |
2566 | if (ps->ps_sig != signum) { |
2567 | code = 0; | |
2568 | } else { | |
2569 | code = ps->ps_code; | |
2570 | ps->ps_code = 0; | |
2571 | } | |
2572 | p->p_stats->p_ru.ru_nsignals++; | |
2573 | sendsig(p, action, signum, returnmask, code); | |
2574 | } | |
2575 | signal_unlock(p); | |
2576 | } | |
2577 | ||
2578 | /* | |
2579 | * Force the current process to exit with the specified signal, dumping core | |
2580 | * if appropriate. We bypass the normal tests for masked and caught signals, | |
2581 | * allowing unrecoverable failures to terminate the process without changing | |
2582 | * signal state. Mark the accounting record with the signal termination. | |
2583 | * If dumping core, save the signal number for the debugger. Calls exit and | |
2584 | * does not return. | |
2585 | */ | |
2586 | /* called with signal lock */ | |
2587 | void | |
2588 | sigexit_locked(p, signum) | |
2589 | register struct proc *p; | |
2590 | int signum; | |
2591 | { | |
2592 | ||
2593 | sig_lock_to_exit(p); | |
2594 | p->p_acflag |= AXSIG; | |
2595 | if (sigprop[signum] & SA_CORE) { | |
2596 | p->p_sigacts->ps_sig = signum; | |
2597 | if (coredump(p) == 0) | |
2598 | signum |= WCOREFLAG; | |
2599 | } | |
2600 | signal_unlock(p); | |
0b4e3aa0 | 2601 | exit1(p, W_EXITCODE(0, signum), (int *)NULL); |
1c79356b A |
2602 | /* NOTREACHED */ |
2603 | } | |
2604 | ||
55e303ae A |
2605 | |
2606 | static int | |
2607 | filt_sigattach(struct knote *kn) | |
2608 | { | |
2609 | struct proc *p = current_proc(); | |
2610 | ||
2611 | kn->kn_ptr.p_proc = p; | |
2612 | kn->kn_flags |= EV_CLEAR; /* automatically set */ | |
2613 | ||
2614 | /* XXX lock the proc here while adding to the list? */ | |
2615 | KNOTE_ATTACH(&p->p_klist, kn); | |
2616 | ||
2617 | return (0); | |
2618 | } | |
2619 | ||
2620 | static void | |
2621 | filt_sigdetach(struct knote *kn) | |
2622 | { | |
2623 | struct proc *p = kn->kn_ptr.p_proc; | |
2624 | ||
2625 | KNOTE_DETACH(&p->p_klist, kn); | |
2626 | } | |
2627 | ||
2628 | /* | |
2629 | * signal knotes are shared with proc knotes, so we apply a mask to | |
2630 | * the hint in order to differentiate them from process hints. This | |
2631 | * could be avoided by using a signal-specific knote list, but probably | |
2632 | * isn't worth the trouble. | |
2633 | */ | |
2634 | static int | |
2635 | filt_signal(struct knote *kn, long hint) | |
2636 | { | |
2637 | ||
2638 | if (hint & NOTE_SIGNAL) { | |
2639 | hint &= ~NOTE_SIGNAL; | |
2640 | ||
2641 | if (kn->kn_id == hint) | |
2642 | kn->kn_data++; | |
2643 | } | |
2644 | return (kn->kn_data != 0); | |
2645 | } | |
2646 | ||
1c79356b A |
2647 | void |
2648 | bsd_ast(thread_act_t thr_act) | |
2649 | { | |
2650 | struct proc *p = current_proc(); | |
2651 | struct uthread *ut = get_bsdthread_info(thr_act); | |
2652 | int signum; | |
2653 | unsigned int pc; | |
2654 | boolean_t funnel_state; | |
9bccf70c | 2655 | static bsd_init_done = 0; |
1c79356b A |
2656 | |
2657 | if (p == NULL) | |
2658 | return; | |
2659 | ||
2660 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
2661 | ||
2662 | if ((p->p_flag & P_OWEUPC) && (p->p_flag & P_PROFIL)) { | |
2663 | pc = get_useraddr(); | |
2664 | addupc_task(p, pc, 1); | |
2665 | p->p_flag &= ~P_OWEUPC; | |
2666 | } | |
2667 | ||
55e303ae | 2668 | if (CHECK_SIGNALS(p, current_act(), ut)) { |
1c79356b A |
2669 | while (signum = issignal(p)) |
2670 | postsig(signum); | |
2671 | } | |
9bccf70c A |
2672 | if (!bsd_init_done) { |
2673 | extern void bsdinit_task(void); | |
2674 | ||
2675 | bsd_init_done = 1; | |
2676 | bsdinit_task(); | |
2677 | } | |
1c79356b A |
2678 | |
2679 | (void) thread_funnel_set(kernel_flock, FALSE); | |
2680 | } | |
2681 | ||
2682 | /* | |
2683 | * Follwing routines are called using callout from bsd_hardclock | |
2684 | * so that psignals are called in a thread context and are funneled | |
2685 | */ | |
2686 | void | |
2687 | psignal_vtalarm(struct proc *p) | |
2688 | { | |
2689 | boolean_t funnel_state; | |
2690 | ||
2691 | if (p == NULL) | |
2692 | return; | |
2693 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
9bccf70c | 2694 | psignal_lock(p, SIGVTALRM, 1); |
1c79356b A |
2695 | (void) thread_funnel_set(kernel_flock, FALSE); |
2696 | } | |
2697 | ||
2698 | void | |
2699 | psignal_xcpu(struct proc *p) | |
2700 | { | |
2701 | boolean_t funnel_state; | |
2702 | ||
2703 | if (p == NULL) | |
2704 | return; | |
2705 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
9bccf70c | 2706 | psignal_lock(p, SIGXCPU, 1); |
1c79356b A |
2707 | (void) thread_funnel_set(kernel_flock, FALSE); |
2708 | } | |
2709 | ||
2710 | void | |
2711 | psignal_sigprof(struct proc *p) | |
2712 | { | |
2713 | boolean_t funnel_state; | |
2714 | ||
2715 | if (p == NULL) | |
2716 | return; | |
2717 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
9bccf70c | 2718 | psignal_lock(p, SIGPROF, 1); |
1c79356b A |
2719 | (void) thread_funnel_set(kernel_flock, FALSE); |
2720 | } | |
2721 | ||
2722 | /* ptrace set runnalbe */ | |
2723 | void | |
2724 | pt_setrunnable(struct proc *p) | |
2725 | { | |
2726 | task_t task; | |
2727 | ||
2728 | task = p->task; | |
2729 | ||
2730 | if (p->p_flag & P_TRACED) { | |
2731 | p->p_stat = SRUN; | |
2732 | if (p->sigwait) { | |
2733 | wakeup((caddr_t)&(p->sigwait)); | |
2734 | task_release(task); | |
2735 | } | |
2736 | } | |
2737 | } | |
9bccf70c A |
2738 | |
2739 | kern_return_t | |
2740 | do_bsdexception( | |
2741 | int exc, | |
2742 | int code, | |
2743 | int sub) | |
2744 | { | |
2745 | exception_data_type_t codes[EXCEPTION_CODE_MAX]; | |
2746 | extern kern_return_t bsd_exception(int, exception_data_type_t codes[], int); | |
2747 | ||
2748 | codes[0] = code; | |
2749 | codes[1] = sub; | |
2750 | return(bsd_exception(exc, codes, 2)); | |
2751 | } | |
2752 |