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