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