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