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