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