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