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