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