]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_sig.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_sig.c
1 /*
2 * Copyright (c) 2000 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/signalvar.h>
66 #include <sys/resourcevar.h>
67 #include <sys/namei.h>
68 #include <sys/vnode.h>
69 #include <sys/proc.h>
70 #include <sys/systm.h>
71 #include <sys/timeb.h>
72 #include <sys/times.h>
73 #include <sys/buf.h>
74 #include <sys/acct.h>
75 #include <sys/file.h>
76 #include <sys/kernel.h>
77 #include <sys/wait.h>
78 #include <sys/ktrace.h>
79 #include <sys/syslog.h>
80 #include <sys/stat.h>
81 #include <sys/lock.h>
82
83 #include <sys/mount.h>
84
85 #include <kern/cpu_number.h>
86
87 #include <sys/vm.h>
88 #include <sys/user.h> /* for coredump */
89 #include <kern/ast.h> /* for APC support */
90 #include <kern/thread.h>
91 #include <kern/thread_call.h>
92
93 void stop __P((struct proc *p));
94 int cansignal __P((struct proc *, struct pcred *, struct proc *, int));
95 int killpg1 __P((struct proc *, int, int, int));
96 void sigexit_locked __P((struct proc *, int));
97 void setsigvec __P((struct proc *, int, struct sigaction *));
98 void exit1 __P((struct proc *, int));
99 int signal_lock __P((struct proc *));
100 int signal_unlock __P((struct proc *));
101 void signal_setast __P((thread_act_t *));
102 void signal_clearast __P((thread_act_t *));
103 void psignal_lock __P((struct proc *, int, int, int));
104
105 #if SIGNAL_DEBUG
106 void ram_printf __P((int));
107 int ram_debug=0;
108 unsigned int rdebug_proc=0;
109 void
110 ram_printf(int x)
111 {
112 printf("x is %d",x);
113
114 }
115
116 #endif /* SIGNAL_DEBUG */
117 int
118 signal_lock(struct proc *p)
119 {
120 #if SIGNAL_DEBUG
121 #ifdef __ppc__
122 {
123 int register sp, *fp, numsaved;
124
125 __asm__ volatile("mr %0,r1" : "=r" (sp));
126
127 fp = (int *)*((int *)sp);
128 for (numsaved = 0; numsaved < 3; numsaved++) {
129 p->lockpc[numsaved] = fp[2];
130 if ((int)fp <= 0)
131 break;
132 fp = (int *)*fp;
133 }
134 }
135 #endif /* __ppc__ */
136 #endif /* SIGNAL_DEBUG */
137
138 return(lockmgr(&p->signal_lock, LK_EXCLUSIVE, 0, (struct proc *)0));
139 }
140
141 int
142 signal_unlock(struct proc *p)
143 {
144 #if SIGNAL_DEBUG
145 #ifdef __ppc__
146 {
147 int register sp, *fp, numsaved;
148
149 __asm__ volatile("mr %0,r1" : "=r" (sp));
150
151 fp = (int *)*((int *)sp);
152 for (numsaved = 0; numsaved < 3; numsaved++) {
153 p->unlockpc[numsaved] = fp[2];
154 if ((int)fp <= 0)
155 break;
156 fp = (int *)*fp;
157 }
158 }
159 #endif /* __ppc__ */
160 #endif /* SIGNAL_DEBUG */
161
162 return(lockmgr(&p->signal_lock, LK_RELEASE, (simple_lock_t)0, (struct proc *)0));
163 }
164
165 void
166 signal_setast(sig_actthread)
167 thread_act_t *sig_actthread;
168 {
169 thread_ast_set(sig_actthread, AST_BSD);
170 if ((thread_act_t *)current_act() == sig_actthread)
171 ast_on(AST_BSD);
172 }
173
174 void
175 signal_clearast(sig_actthread)
176 thread_act_t *sig_actthread;
177 {
178 thread_ast_clear(sig_actthread, AST_BSD);
179 if ((thread_act_t *)current_act() == sig_actthread)
180 ast_off(AST_BSD);
181 }
182
183 /*
184 * Can process p, with pcred pc, send the signal signum to process q?
185 */
186 int
187 cansignal(p, pc, q, signum)
188 struct proc *p;
189 struct pcred *pc;
190 struct proc *q;
191 int signum;
192 {
193 if (pc->pc_ucred->cr_uid == 0)
194 return (1); /* root can always signal */
195
196 if (signum == SIGCONT && q->p_session == p->p_session)
197 return (1); /* SIGCONT in session */
198
199 /*
200 * Using kill(), only certain signals can be sent to setugid
201 * child processes
202 */
203 if (q->p_flag & P_SUGID) {
204 switch (signum) {
205 case 0:
206 case SIGKILL:
207 case SIGINT:
208 case SIGTERM:
209 case SIGSTOP:
210 case SIGTTIN:
211 case SIGTTOU:
212 case SIGTSTP:
213 case SIGHUP:
214 case SIGUSR1:
215 case SIGUSR2:
216 if (pc->p_ruid == q->p_cred->p_ruid ||
217 pc->pc_ucred->cr_uid == q->p_cred->p_ruid ||
218 pc->p_ruid == q->p_ucred->cr_uid ||
219 pc->pc_ucred->cr_uid == q->p_ucred->cr_uid)
220 return (1);
221 }
222 return (0);
223 }
224
225 /* XXX
226 * because the P_SUGID test exists, this has extra tests which
227 * could be removed.
228 */
229 if (pc->p_ruid == q->p_cred->p_ruid ||
230 pc->p_ruid == q->p_cred->p_svuid ||
231 pc->pc_ucred->cr_uid == q->p_cred->p_ruid ||
232 pc->pc_ucred->cr_uid == q->p_cred->p_svuid ||
233 pc->p_ruid == q->p_ucred->cr_uid ||
234 pc->pc_ucred->cr_uid == q->p_ucred->cr_uid)
235 return (1);
236 return (0);
237 }
238
239 struct sigaction_args {
240 int signum;
241 struct sigaction *nsa;
242 struct sigaction *osa;
243 };
244
245 /* ARGSUSED */
246 int
247 sigaction(p, uap, retval)
248 struct proc *p;
249 register struct sigaction_args *uap;
250 register_t *retval;
251 {
252 struct sigaction vec;
253 register struct sigaction *sa;
254 register struct sigacts *ps = p->p_sigacts;
255 register int signum;
256 int bit, error;
257
258 signum = uap->signum;
259 if (signum <= 0 || signum >= NSIG ||
260 signum == SIGKILL || signum == SIGSTOP)
261 return (EINVAL);
262 sa = &vec;
263 if (uap->osa) {
264 sa->sa_handler = ps->ps_sigact[signum];
265 sa->sa_mask = ps->ps_catchmask[signum];
266 bit = sigmask(signum);
267 sa->sa_flags = 0;
268 if ((ps->ps_sigonstack & bit) != 0)
269 sa->sa_flags |= SA_ONSTACK;
270 if ((ps->ps_sigintr & bit) == 0)
271 sa->sa_flags |= SA_RESTART;
272 if (p->p_flag & P_NOCLDSTOP)
273 sa->sa_flags |= SA_NOCLDSTOP;
274 if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
275 sizeof (vec)))
276 return (error);
277 }
278 if (uap->nsa) {
279 if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
280 sizeof (vec)))
281 return (error);
282 setsigvec(p, signum, sa);
283 }
284 return (0);
285 }
286
287 static int
288 reset_sigbits(thread_act_t th_act, int bit)
289 {
290 struct uthread *ut;
291 ut = get_bsdthread_info(th_act);
292 if (ut) {
293 ut->uu_sig &= ~bit;
294 }
295 }
296
297 int
298 clear_sigbits (struct proc *p, int bit)
299 {
300 task_t task = p->task;
301
302 p->p_siglist &= ~(bit);
303 task_act_iterate_wth_args(task, reset_sigbits, bit);
304 return(0);
305 }
306
307
308 void
309 setsigvec(p, signum, sa)
310 register struct proc *p;
311 int signum;
312 register struct sigaction *sa;
313 {
314 register struct sigacts *ps = p->p_sigacts;
315 register int bit;
316
317 bit = sigmask(signum);
318 /*
319 * Change setting atomically.
320 */
321 ps->ps_sigact[signum] = sa->sa_handler;
322 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
323 if ((sa->sa_flags & SA_RESTART) == 0)
324 ps->ps_sigintr |= bit;
325 else
326 ps->ps_sigintr &= ~bit;
327 if (sa->sa_flags & SA_ONSTACK)
328 ps->ps_sigonstack |= bit;
329 else
330 ps->ps_sigonstack &= ~bit;
331 if (sa->sa_flags & SA_USERTRAMP)
332 ps->ps_usertramp |= bit;
333 else
334 ps->ps_usertramp &= ~bit;
335 if (signum == SIGCHLD) {
336 if (sa->sa_flags & SA_NOCLDSTOP)
337 p->p_flag |= P_NOCLDSTOP;
338 else
339 p->p_flag &= ~P_NOCLDSTOP;
340 }
341 /*
342 * Set bit in p_sigignore for signals that are set to SIG_IGN,
343 * and for signals set to SIG_DFL where the default is to ignore.
344 * However, don't put SIGCONT in p_sigignore,
345 * as we have to restart the process.
346 */
347 if (sa->sa_handler == SIG_IGN ||
348 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
349 p->p_siglist &= ~bit; /* never to be seen again */
350 /*
351 * If this is a thread signal, clean out the
352 * threads as well.
353 */
354 if (bit & threadmask) {
355 register task_t task = p->task;
356
357 task_act_iterate_wth_args(task, reset_sigbits, bit);
358 }
359 if (signum != SIGCONT)
360 p->p_sigignore |= bit; /* easier in psignal */
361 p->p_sigcatch &= ~bit;
362 } else {
363 p->p_sigignore &= ~bit;
364 if (sa->sa_handler == SIG_DFL)
365 p->p_sigcatch &= ~bit;
366 else
367 p->p_sigcatch |= bit;
368 }
369 }
370
371 /*
372 * Initialize signal state for process 0;
373 * set to ignore signals that are ignored by default.
374 */
375 void
376 siginit(p)
377 struct proc *p;
378 {
379 register int i;
380
381 for (i = 0; i < NSIG; i++)
382 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
383 p->p_sigignore |= sigmask(i);
384 }
385
386 /*
387 * Reset signals for an exec of the specified process.
388 */
389 void
390 execsigs(p)
391 register struct proc *p;
392 {
393 register struct sigacts *ps = p->p_sigacts;
394 register int nc, mask;
395
396 /*
397 * Reset caught signals. Held signals remain held
398 * through p_sigmask (unless they were caught,
399 * and are now ignored by default).
400 */
401 while (p->p_sigcatch) {
402 nc = ffs((long)p->p_sigcatch);
403 mask = sigmask(nc);
404 p->p_sigcatch &= ~mask;
405 if (sigprop[nc] & SA_IGNORE) {
406 if (nc != SIGCONT)
407 p->p_sigignore |= mask;
408 p->p_siglist &= ~mask;
409 }
410 ps->ps_sigact[nc] = SIG_DFL;
411 }
412 /*
413 * Reset stack state to the user stack.
414 * Clear set of signals caught on the signal stack.
415 */
416 ps->ps_sigstk.ss_flags = SA_DISABLE;
417 ps->ps_sigstk.ss_size = 0;
418 ps->ps_sigstk.ss_sp = 0;
419 ps->ps_flags = 0;
420 }
421
422 /*
423 * Manipulate signal mask.
424 * Note that we receive new mask, not pointer,
425 * and return old mask as return value;
426 * the library stub does the rest.
427 */
428 struct sigprocmask_args {
429 int how;
430 sigset_t mask;
431 };
432 int
433 sigprocmask(p, uap, retval)
434 register struct proc *p;
435 struct sigprocmask_args *uap;
436 register_t *retval;
437 {
438 int error = 0;
439
440 *retval = p->p_sigmask;
441
442 switch (uap->how) {
443 case SIG_BLOCK:
444 p->p_sigmask |= uap->mask &~ sigcantmask;
445 break;
446
447 case SIG_UNBLOCK:
448 p->p_sigmask &= ~(uap->mask);
449 signal_setast(current_act());
450 break;
451
452 case SIG_SETMASK:
453 p->p_sigmask = uap->mask &~ sigcantmask;
454 signal_setast(current_act());
455 break;
456
457 default:
458 error = EINVAL;
459 break;
460 }
461 return (error);
462 }
463
464 /* ARGSUSED */
465 int
466 sigpending(p, uap, retval)
467 struct proc *p;
468 void *uap;
469 register_t *retval;
470 {
471
472 *retval = p->p_siglist;
473 return (0);
474 }
475
476 #if COMPAT_43
477 /*
478 * Generalized interface signal handler, 4.3-compatible.
479 */
480 struct osigvec_args {
481 int signum;
482 struct sigvec *nsv;
483 struct sigvec *osv;
484 };
485 /* ARGSUSED */
486 int
487 osigvec(p, uap, retval)
488 struct proc *p;
489 register struct osigvec_args *uap;
490 register_t *retval;
491 {
492 struct sigvec vec;
493 register struct sigacts *ps = p->p_sigacts;
494 register struct sigvec *sv;
495 register int signum;
496 int bit, error;
497
498 signum = uap->signum;
499 if (signum <= 0 || signum >= NSIG ||
500 signum == SIGKILL || signum == SIGSTOP)
501 return (EINVAL);
502 sv = &vec;
503 if (uap->osv) {
504 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
505 sv->sv_mask = ps->ps_catchmask[signum];
506 bit = sigmask(signum);
507 sv->sv_flags = 0;
508 if ((ps->ps_sigonstack & bit) != 0)
509 sv->sv_flags |= SV_ONSTACK;
510 if ((ps->ps_sigintr & bit) != 0)
511 sv->sv_flags |= SV_INTERRUPT;
512 if (p->p_flag & P_NOCLDSTOP)
513 sv->sv_flags |= SA_NOCLDSTOP;
514 if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
515 sizeof (vec)))
516 return (error);
517 }
518 if (uap->nsv) {
519 if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
520 sizeof (vec)))
521 return (error);
522 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
523 setsigvec(p, signum, (struct sigaction *)sv);
524 }
525 return (0);
526 }
527
528 struct osigblock_args {
529 int mask;
530 };
531 int
532 osigblock(p, uap, retval)
533 register struct proc *p;
534 struct osigblock_args *uap;
535 register_t *retval;
536 {
537
538 *retval = p->p_sigmask;
539 p->p_sigmask |= uap->mask &~ sigcantmask;
540 return (0);
541 }
542
543 struct osigsetmask_args {
544 int mask;
545 };
546 int
547 osigsetmask(p, uap, retval)
548 struct proc *p;
549 struct osigsetmask_args *uap;
550 register_t *retval;
551 {
552
553 *retval = p->p_sigmask;
554 p->p_sigmask = uap->mask &~ sigcantmask;
555 return (0);
556 }
557 #endif /* COMPAT_43 */
558
559 /*
560 * Suspend process until signal, providing mask to be set
561 * in the meantime. Note nonstandard calling convention:
562 * libc stub passes mask, not pointer, to save a copyin.
563 */
564
565 int
566 sigcontinue(error)
567 {
568 unix_syscall_return(EINTR);
569 }
570
571 struct sigsuspend_args {
572 int mask;
573 };
574
575 /* ARGSUSED */
576 int
577 sigsuspend(p, uap, retval)
578 register struct proc *p;
579 struct sigsuspend_args *uap;
580 register_t *retval;
581 {
582 register struct sigacts *ps = p->p_sigacts;
583
584 /*
585 * When returning from sigpause, we want
586 * the old mask to be restored after the
587 * signal handler has finished. Thus, we
588 * save it here and mark the sigacts structure
589 * to indicate this.
590 */
591 ps->ps_oldmask = p->p_sigmask;
592 ps->ps_flags |= SAS_OLDMASK;
593 p->p_sigmask = uap->mask &~ sigcantmask;
594 (void) tsleep0((caddr_t) p, PPAUSE|PCATCH, "pause", 0, sigcontinue);
595 /* always return EINTR rather than ERESTART... */
596 return (EINTR);
597 }
598
599 #if COMPAT_43
600 struct osigstack_args {
601 struct sigstack *nss;
602 struct sigstack *oss;
603 };
604 /* ARGSUSED */
605 int
606 osigstack(p, uap, retval)
607 struct proc *p;
608 register struct osigstack_args *uap;
609 register_t *retval;
610 {
611 struct sigstack ss;
612 struct sigacts *psp;
613 int error = 0;
614
615 psp = p->p_sigacts;
616 ss.ss_sp = psp->ps_sigstk.ss_sp;
617 ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
618 if (uap->oss && (error = copyout((caddr_t)&ss,
619 (caddr_t)uap->oss, sizeof (struct sigstack))))
620 return (error);
621 if (uap->nss && (error = copyin((caddr_t)uap->nss,
622 (caddr_t)&ss, sizeof (ss))) == 0) {
623 psp->ps_sigstk.ss_sp = ss.ss_sp;
624 psp->ps_sigstk.ss_size = 0;
625 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
626 psp->ps_flags |= SAS_ALTSTACK;
627 }
628 return (error);
629 }
630 #endif /* COMPAT_43 */
631
632 struct sigaltstack_args {
633 struct sigaltstack *nss;
634 struct sigaltstack *oss;
635 };
636 /* ARGSUSED */
637 int
638 sigaltstack(p, uap, retval)
639 struct proc *p;
640 register struct sigaltstack_args *uap;
641 register_t *retval;
642 {
643 struct sigacts *psp;
644 struct sigaltstack ss;
645 int error;
646
647 psp = p->p_sigacts;
648 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
649 psp->ps_sigstk.ss_flags |= SA_DISABLE;
650 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
651 (caddr_t)uap->oss, sizeof (struct sigaltstack))))
652 return (error);
653 if (uap->nss == 0)
654 return (0);
655 if (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
656 sizeof (ss)))
657 return (error);
658 if (ss.ss_flags & SA_DISABLE) {
659 if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
660 return (EINVAL);
661 psp->ps_flags &= ~SAS_ALTSTACK;
662 psp->ps_sigstk.ss_flags = ss.ss_flags;
663 return (0);
664 }
665 if (ss.ss_size < MINSIGSTKSZ)
666 return (ENOMEM);
667 psp->ps_flags |= SAS_ALTSTACK;
668 psp->ps_sigstk= ss;
669 return (0);
670 }
671
672 struct kill_args {
673 int pid;
674 int signum;
675 };
676 /* ARGSUSED */
677 int
678 kill(cp, uap, retval)
679 register struct proc *cp;
680 register struct kill_args *uap;
681 register_t *retval;
682 {
683 register struct proc *p;
684 register struct pcred *pc = cp->p_cred;
685
686 if ((u_int)uap->signum >= NSIG)
687 return (EINVAL);
688 if (uap->pid > 0) {
689 /* kill single process */
690 if ((p = pfind(uap->pid)) == NULL)
691 return (ESRCH);
692 if (!cansignal(cp, pc, p, uap->signum))
693 return (EPERM);
694 if (uap->signum)
695 psignal(p, uap->signum);
696 return (0);
697 }
698 switch (uap->pid) {
699 case -1: /* broadcast signal */
700 return (killpg1(cp, uap->signum, 0, 1));
701 case 0: /* signal own process group */
702 return (killpg1(cp, uap->signum, 0, 0));
703 default: /* negative explicit process group */
704 return (killpg1(cp, uap->signum, -(uap->pid), 0));
705 }
706 /* NOTREACHED */
707 }
708
709 #if COMPAT_43
710 struct okillpg_args {
711 int pgid;
712 int signum;
713 };
714 /* ARGSUSED */
715 int
716 okillpg(p, uap, retval)
717 struct proc *p;
718 register struct okillpg_args *uap;
719 register_t *retval;
720 {
721
722 if ((u_int)uap->signum >= NSIG)
723 return (EINVAL);
724 return (killpg1(p, uap->signum, uap->pgid, 0));
725 }
726 #endif /* COMPAT_43 */
727
728 /*
729 * Common code for kill process group/broadcast kill.
730 * cp is calling process.
731 */
732 int
733 killpg1(cp, signum, pgid, all)
734 register struct proc *cp;
735 int signum, pgid, all;
736 {
737 register struct proc *p;
738 register struct pcred *pc = cp->p_cred;
739 struct pgrp *pgrp;
740 int nfound = 0;
741
742 if (all) {
743 /*
744 * broadcast
745 */
746 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
747 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
748 p == cp || !cansignal(cp, pc, p, signum))
749 continue;
750 nfound++;
751 if (signum)
752 psignal(p, signum);
753 }
754 } else {
755 if (pgid == 0)
756 /*
757 * zero pgid means send to my process group.
758 */
759 pgrp = cp->p_pgrp;
760 else {
761 pgrp = pgfind(pgid);
762 if (pgrp == NULL)
763 return (ESRCH);
764 }
765 for (p = pgrp->pg_members.lh_first; p != 0;
766 p = p->p_pglist.le_next) {
767 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
768 p->p_stat == SZOMB ||
769 !cansignal(cp, pc, p, signum))
770 continue;
771 nfound++;
772 if (signum)
773 psignal(p, signum);
774 }
775 }
776 return (nfound ? 0 : ESRCH);
777 }
778
779 /*
780 * Send a signal to a process group.
781 */
782 void
783 gsignal(pgid, signum)
784 int pgid, signum;
785 {
786 struct pgrp *pgrp;
787
788 if (pgid && (pgrp = pgfind(pgid)))
789 pgsignal(pgrp, signum, 0);
790 }
791
792 /*
793 * Send a signal to a process group. If checktty is 1,
794 * limit to members which have a controlling terminal.
795 */
796 void
797 pgsignal(pgrp, signum, checkctty)
798 struct pgrp *pgrp;
799 int signum, checkctty;
800 {
801 register struct proc *p;
802
803 if (pgrp)
804 for (p = pgrp->pg_members.lh_first; p != 0;
805 p = p->p_pglist.le_next)
806 if (checkctty == 0 || p->p_flag & P_CONTROLT)
807 psignal(p, signum);
808 }
809
810 /*
811 * Send a signal caused by a trap to a specific thread.
812 */
813 void
814 threadsignal(sig_actthread, signum, code)
815 register thread_act_t *sig_actthread;
816 register int signum;
817 u_long code;
818 {
819 register struct uthread *uth;
820 register struct task * sig_task;
821 register struct proc *p ;
822 int mask;
823
824 if ((u_int)signum >= NSIG || signum == 0)
825 return;
826
827 mask = sigmask(signum);
828 if ((mask & threadmask) == 0)
829 return;
830 sig_task = get_threadtask(sig_actthread);
831 /* p = sig_task->proc; */
832 p = (struct proc *)(get_bsdtask_info(sig_task));
833
834 if (!(p->p_flag & P_TRACED) && (p->p_sigignore & mask))
835 return;
836
837 uth = get_bsdthread_info(sig_actthread);
838 uth->uu_sig |= mask;
839 uth->uu_code = code;
840 /* mark on process as well */
841 p->p_siglist |= mask;
842 signal_setast(sig_actthread);
843 }
844
845
846 void
847 psignal_pend(p)
848 register struct proc *p;
849 {
850 boolean_t funnel_state;
851 register int sigbits, mask, signum;
852
853 thread_funnel_set(kernel_flock, TRUE);
854
855 if (p->p_sigpending == 0)
856 return;
857
858
859 signal_lock(p);
860
861 for (;;) {
862 sigbits = p->p_sigpending;
863 if (sigbits == 0)
864 goto out;
865 signum = ffs((long)sigbits);
866 mask = sigmask(signum);
867 p->p_sigpending &= ~mask;
868
869 psignal_lock(p, signum, 0, 0);
870
871 }
872 out:
873 p->p_flag &= ~P_SIGTHR;
874 signal_unlock(p);
875 thread_funnel_set(kernel_flock, FALSE);
876 }
877
878 void
879 psignal(p, signum)
880 register struct proc *p;
881 register int signum;
882 {
883 psignal_lock(p, signum, 1, 1);
884 }
885
886
887 /*
888 * Send the signal to the process. If the signal has an action, the action
889 * is usually performed by the target process rather than the caller; we add
890 * the signal to the set of pending signals for the process.
891 *
892 * Exceptions:
893 * o When a stop signal is sent to a sleeping process that takes the
894 * default action, the process is stopped without awakening it.
895 * o SIGCONT restarts stopped processes (or puts them back to sleep)
896 * regardless of the signal action (eg, blocked or ignored).
897 *
898 * Other ignored signals are discarded immediately.
899 */
900 void
901 psignal_lock(p, signum, withlock, pend)
902 register struct proc *p;
903 register int signum;
904 register int withlock;
905 register int pend;
906 {
907 register int s, prop;
908 register sig_t action;
909 thread_act_t sig_thread_act;
910 thread_t sig_thread;
911 register task_t sig_task;
912 register thread_t cur_thread;
913 thread_act_t *cur_act;
914 int mask;
915 kern_return_t kret;
916
917 if ((u_int)signum >= NSIG || signum == 0)
918 panic("psignal signal number");
919 mask = sigmask(signum);
920 prop = sigprop[signum];
921
922 #if SIGNAL_DEBUG
923 if(rdebug_proc && (p == rdebug_proc)) {
924 ram_printf(3);
925 }
926 #endif /* SIGNAL_DEBUG */
927 /*
928 * We will need the task pointer later. Grab it now to
929 * check for a zombie process. Also don't send signals
930 * to kernel internal tasks.
931 */
932 if (((sig_task = p->task) == TASK_NULL) || is_kerneltask(sig_task))
933 return;
934
935 /*
936 * do not send signals to the process that has the thread
937 * doing a reboot(). Not doing so will mark that thread aborted
938 * and can cause IO failures wich will cause data loss.
939 */
940 if (ISSET(p->p_flag, P_REBOOT))
941 return;
942
943 /*
944 * if the traced process is blocked waiting for
945 * gdb then do not block the caller just pend
946 * the signal. Setup a callout to process the
947 * pended signal if not alreadu set
948 */
949 if (pend && (p->p_flag & P_TRACED) && p->sigwait) {
950 p->p_sigpending |= mask;
951 if (!(p->p_flag & P_SIGTHR)) {
952 p->p_flag |= P_SIGTHR;
953 thread_call_func((thread_call_func_t)psignal_pend, p,
954 FALSE);
955 }
956 return;
957 }
958
959 if (withlock)
960 signal_lock(p);
961
962 /*
963 * If proc is traced, always give parent a chance.
964 */
965 if (p->p_flag & P_TRACED)
966 action = SIG_DFL;
967 else {
968 /*
969 * If the signal is being ignored,
970 * then we forget about it immediately.
971 * (Note: we don't set SIGCONT in p_sigignore,
972 * and if it is set to SIG_IGN,
973 * action will be SIG_DFL here.)
974 */
975 if (p->p_sigignore & mask)
976 goto psigout;
977 if (p->p_sigmask & mask)
978 action = SIG_HOLD;
979 else if (p->p_sigcatch & mask)
980 action = SIG_CATCH;
981 else
982 action = SIG_DFL;
983 }
984
985 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
986 (p->p_flag & P_TRACED) == 0)
987 p->p_nice = NZERO;
988
989 if (prop & SA_CONT)
990 p->p_siglist &= ~stopsigmask;
991
992 if (prop & SA_STOP) {
993 /*
994 * If sending a tty stop signal to a member of an orphaned
995 * process group, discard the signal here if the action
996 * is default; don't stop the process below if sleeping,
997 * and don't clear any pending SIGCONT.
998 */
999 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1000 action == SIG_DFL)
1001 goto psigout;
1002 p->p_siglist &= ~contsigmask;
1003 }
1004 p->p_siglist |= mask;
1005
1006 /*
1007 * Defer further processing for signals which are held,
1008 * except that stopped processes must be continued by SIGCONT.
1009 */
1010 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
1011 goto psigout;
1012
1013 /*
1014 * Deliver the signal to the first thread in the task. This
1015 * allows single threaded applications which use signals to
1016 * be able to be linked with multithreaded libraries. We have
1017 * an implicit reference to the current_thread, but need
1018 * an explicit one otherwise. The thread reference keeps
1019 * the corresponding task data structures around too. This
1020 * reference is released by thread_deallocate.
1021 */
1022
1023 cur_thread = current_thread(); /* this is a shuttle */
1024 cur_act = current_act();
1025
1026 /* If successful return with ast set */
1027 kret = (kern_return_t)get_signalact(sig_task,
1028 &sig_thread_act, &sig_thread, 1);
1029
1030 if ((kret != KERN_SUCCESS) || (sig_thread_act == THREAD_NULL)) {
1031 /* XXXX FIXME
1032 /* if it is sigkill, may be we should
1033 * inject a thread to terminate
1034 */
1035 printf("WARNING: no activation in psignal\n");
1036 #if SIGNAL_DEBUG
1037 ram_printf(1);
1038 #endif /* SIGNAL_DEBUG */
1039 goto psigout;
1040 }
1041
1042 if (sig_thread == THREAD_NULL) {
1043 printf("WARNING: valid act; but no shutte in psignal\n");
1044 #if 0
1045 /* FIXME : NO VALID SHUTTLE */
1046 goto psigout;
1047 #endif
1048 }
1049
1050 /*
1051 * SIGKILL priority twiddling moved here from above because
1052 * it needs sig_thread. Could merge it into large switch
1053 * below if we didn't care about priority for tracing
1054 * as SIGKILL's action is always SIG_DFL.
1055 */
1056 if ((signum == SIGKILL) && (p->p_nice > NZERO)) {
1057 p->p_nice = NZERO;
1058 #if XXX
1059 /*
1060 * we need to make changes here to get nice to work
1061 * reset priority to BASEPRI_USER
1062 */
1063 #endif
1064 }
1065
1066 /*
1067 * Process is traced - wake it up (if not already
1068 * stopped) so that it can discover the signal in
1069 * issig() and stop for the parent.
1070 */
1071 if (p->p_flag & P_TRACED) {
1072 if (p->p_stat != SSTOP)
1073 goto run;
1074 else
1075 goto psigout;
1076 }
1077
1078 if (action != SIG_DFL) {
1079 /*
1080 * User wants to catch the signal.
1081 * Wake up the thread, but don't un-suspend it
1082 * (except for SIGCONT).
1083 */
1084 if (prop & SA_CONT)
1085 (void) task_resume(sig_task);
1086 goto run;
1087 } else {
1088 /* Default action - varies */
1089 if (mask & stopsigmask) {
1090 /*
1091 * These are the signals which by default
1092 * stop a process.
1093 *
1094 * Don't clog system with children of init
1095 * stopped from the keyboard.
1096 */
1097 if (!(prop & SA_STOP) && p->p_pptr == initproc) {
1098 psignal_lock(p, SIGKILL, 0, 1);
1099 p->p_siglist &= ~mask;
1100 goto psigout;
1101 }
1102
1103 /*
1104 * Stop the task.
1105 */
1106 if (!is_thread_running(sig_thread)) {
1107 /* Thread is not running
1108 * If task hasn't already been stopped by
1109 * a signal, stop it.
1110 */
1111 p->p_siglist &= ~mask;
1112 if (get_task_userstop(sig_task) == 0) {
1113 /*
1114 * p_cursig must not be set, because
1115 * it will be psig()'d if it is not
1116 * zero, and the signal is being
1117 * handled here. But save the signal
1118 * in p_stopsig so WUNTRACED
1119 * option to wait can find it.
1120 */
1121 p->p_xstat = signum;
1122 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1123 psignal(p->p_pptr, SIGCHLD);
1124 stop(p);
1125 }
1126 #if 0
1127 /* unconditional check is bad */
1128 signal_clearast(sig_thread_act);
1129 #endif
1130 goto psigout;
1131 } else {
1132 if (p->p_stat != SZOMB)
1133 signal_setast(cur_act);
1134 goto psigout;
1135 }
1136 }
1137
1138 switch (signum) {
1139 /*
1140 * Signals ignored by default have been dealt
1141 * with already, since their bits are on in
1142 * p_sigignore.
1143 */
1144
1145 case SIGKILL:
1146 /*
1147 * Kill signal always sets process running and
1148 * unsuspends it.
1149 */
1150 /*
1151 * Process will be running after 'run'
1152 */
1153 p->p_stat = SRUN;
1154
1155 thread_abort(sig_thread_act);
1156
1157 goto psigout;
1158
1159 case SIGCONT:
1160 /*
1161 * Let the process run. If it's sleeping on an
1162 * event, it remains so.
1163 */
1164 if (p->p_flag & P_TTYSLEEP) {
1165 p->p_flag &= ~P_TTYSLEEP;
1166 wakeup(&p->p_siglist);
1167 } else {
1168 (void) task_resume(sig_task);
1169 }
1170 p->p_siglist &= ~mask;
1171 p->p_stat = SRUN;
1172 #if 0
1173 /* do not clear AST as tcsh is sendig SIGTERM followed by
1174 * SIGCONT and the ast was getting cleared unconditinally
1175 * This is not right.
1176 */
1177 signal_clearast(sig_thread_act);
1178 #endif
1179 goto psigout;
1180
1181 default:
1182 /*
1183 * All other signals wake up the process, but don't
1184 * resume it.
1185 */
1186 goto run;
1187 }
1188 }
1189 /*NOTREACHED*/
1190 run:
1191 /*
1192 * If we're being traced (possibly because someone attached us
1193 * while we were stopped), check for a signal from the debugger.
1194 */
1195 if (p->p_stat == SSTOP) {
1196 if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0)
1197 p->p_siglist |= sigmask(p->p_xstat);
1198 }
1199
1200 /*
1201 * setrunnable(p) in BSD
1202 */
1203 p->p_stat = SRUN;
1204
1205 /*
1206 * Wake up the thread if it is interruptible.
1207 */
1208 thread_abort_safely(sig_thread_act);
1209 psigout:
1210 if (withlock)
1211 signal_unlock(p);
1212 }
1213
1214 __inline__ void
1215 sig_lock_to_exit(
1216 struct proc *p)
1217 {
1218 thread_t self = current_thread();
1219
1220 p->exit_thread = self;
1221 (void) task_suspend(p->task);
1222 }
1223
1224 __inline__ int
1225 sig_try_locked(
1226 struct proc *p)
1227 {
1228 thread_t self = current_thread();
1229
1230 while (p->sigwait || p->exit_thread) {
1231 if (p->exit_thread) {
1232 if (p->exit_thread != self) {
1233 /*
1234 * Already exiting - no signals.
1235 */
1236 thread_abort(current_act());
1237 }
1238 return(0);
1239 }
1240 if(assert_wait_possible()) {
1241 assert_wait((caddr_t)&p->sigwait_thread,
1242 (THREAD_INTERRUPTIBLE));
1243 }
1244 signal_unlock(p);
1245 thread_block(0);
1246 signal_lock(p);
1247 if (thread_should_abort(self)) {
1248 /*
1249 * Terminate request - clean up.
1250 */
1251 return -1;
1252 }
1253 }
1254 return 1;
1255 }
1256
1257 /*
1258 * If the current process has received a signal (should be caught or cause
1259 * termination, should interrupt current syscall), return the signal number.
1260 * Stop signals with default action are processed immediately, then cleared;
1261 * they aren't returned. This is checked after each entry to the system for
1262 * a syscall or trap (though this can usually be done without calling issignal
1263 * by checking the pending signal masks in the CURSIG macro.) The normal call
1264 * sequence is
1265 *
1266 * while (signum = CURSIG(curproc))
1267 * postsig(signum);
1268 */
1269 int
1270 issignal(p)
1271 register struct proc *p;
1272 {
1273 register int signum, mask, prop, sigbits;
1274 task_t task = p->task;
1275 thread_t cur_thread;
1276 thread_act_t cur_act;
1277 int s;
1278 struct uthread * ut;
1279 kern_return_t kret;
1280
1281 cur_thread = current_thread();
1282 cur_act = current_act();
1283
1284 signal_lock(p);
1285
1286
1287 /*
1288 * Try to grab the signal lock.
1289 */
1290 if (sig_try_locked(p) <= 0) {
1291 signal_unlock(p);
1292 return (0);
1293 }
1294
1295 ut = get_bsdthread_info(cur_act);
1296 for(;;) {
1297 sigbits = (ut->uu_sig |p->p_siglist) & ~p->p_sigmask;
1298
1299 if (p->p_flag & P_PPWAIT)
1300 sigbits &= ~stopsigmask;
1301 if (sigbits == 0) { /* no signal to send */
1302 signal_unlock(p);
1303 return (0);
1304 }
1305 signum = ffs((long)sigbits);
1306 mask = sigmask(signum);
1307 prop = sigprop[signum];
1308
1309 if (mask & threadmask) {
1310 /* we can take this signal */
1311 ut->uu_sig &= ~mask;
1312 }
1313
1314 /*
1315 * We should see pending but ignored signals
1316 * only if P_TRACED was on when they were posted.
1317 */
1318 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) {
1319 p->p_siglist &= ~mask; /* take the signal! */
1320 continue;
1321 }
1322 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1323 register int hold;
1324 register task_t task;
1325 /*
1326 * If traced, always stop, and stay
1327 * stopped until released by the debugger.
1328 */
1329 /* ptrace debugging */
1330 p->p_xstat = signum;
1331 psignal(p->p_pptr, SIGCHLD);
1332 /*
1333 * XXX Have to really stop for debuggers;
1334 * XXX stop() doesn't do the right thing.
1335 * XXX Inline the task_suspend because we
1336 * XXX have to diddle Unix state in the
1337 * XXX middle of it.
1338 */
1339 task = p->task;
1340 task_hold(task);
1341 p->sigwait = TRUE;
1342 p->sigwait_thread = cur_act;
1343 p->p_stat = SSTOP;
1344 p->p_flag &= ~P_WAITED;
1345 p->p_siglist &= ~mask; /* clear the old signal */
1346
1347 wakeup((caddr_t)p->p_pptr);
1348 assert_wait((caddr_t)&p->sigwait, (THREAD_INTERRUPTIBLE));
1349 thread_block(0);
1350 p->sigwait = FALSE;
1351 p->sigwait_thread = NULL;
1352 wakeup((caddr_t)&p->sigwait_thread);
1353
1354 /*
1355 * This code is to detect when gdb is killed
1356 * even as the traced program is attached.
1357 * pgsignal would get the SIGKILL to traced program
1358 * That's what we are trying to see (I hope)
1359 */
1360 if (p->p_siglist & sigmask(SIGKILL)) {
1361 /*
1362 * Wait event may still be outstanding;
1363 * clear it, since sig_lock_to_exit will
1364 * wait.
1365 */
1366 clear_wait(current_thread(), THREAD_INTERRUPTED);
1367 sig_lock_to_exit(p);
1368 /*
1369 * Since this thread will be resumed
1370 * to allow the current syscall to
1371 * be completed, must save u_qsave
1372 * before calling exit(). (Since exit()
1373 * calls closef() which can trash u_qsave.)
1374 */
1375 signal_unlock(p);
1376 exit1(p,signum);
1377 return(0);
1378 }
1379
1380 /*
1381 * We may have to quit
1382 */
1383 if (thread_should_abort(current_thread())) {
1384 signal_unlock(p);
1385 return(0);
1386 }
1387 /*
1388 * If parent wants us to take the signal,
1389 * then it will leave it in p->p_xstat;
1390 * otherwise we just look for signals again.
1391 */
1392 signum = p->p_xstat;
1393 if (signum == 0)
1394 continue;
1395 /*
1396 * Put the new signal into p_siglist. If the
1397 * signal is being masked, look for other signals.
1398 */
1399 mask = sigmask(signum);
1400 if (mask & threadmask)
1401 ut->uu_sig |= mask;
1402 else
1403 p->p_siglist |= mask;
1404 if (p->p_sigmask & mask)
1405 continue;
1406 }
1407
1408 /*
1409 * Decide whether the signal should be returned.
1410 * Return the signal's number, or fall through
1411 * to clear it from the pending mask.
1412 */
1413
1414 switch ((long)p->p_sigacts->ps_sigact[signum]) {
1415
1416 case (long)SIG_DFL:
1417 /*
1418 * Don't take default actions on system processes.
1419 */
1420 if (p->p_pptr->p_pid == 0) {
1421 #if DIAGNOSTIC
1422 /*
1423 * Are you sure you want to ignore SIGSEGV
1424 * in init? XXX
1425 */
1426 printf("Process (pid %d) got signal %d\n",
1427 p->p_pid, signum);
1428 #endif
1429 break; /* == ignore */
1430 }
1431
1432 /*
1433 * If there is a pending stop signal to process
1434 * with default action, stop here,
1435 * then clear the signal. However,
1436 * if process is member of an orphaned
1437 * process group, ignore tty stop signals.
1438 */
1439 if (prop & SA_STOP) {
1440 if (p->p_flag & P_TRACED ||
1441 (p->p_pgrp->pg_jobc == 0 &&
1442 prop & SA_TTYSTOP))
1443 break; /* == ignore */
1444 p->p_xstat = signum;
1445 stop(p);
1446 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1447 psignal(p->p_pptr, SIGCHLD);
1448 thread_block(0);
1449 /*
1450 * We may have to quit
1451 */
1452 if (thread_should_abort(current_thread())) {
1453 signal_unlock(p);
1454 return(0);
1455 }
1456 break;
1457 } else if (prop & SA_IGNORE) {
1458 /*
1459 * Except for SIGCONT, shouldn't get here.
1460 * Default action is to ignore; drop it.
1461 */
1462 break; /* == ignore */
1463 } else {
1464 signal_unlock(p);
1465 return (signum);
1466 }
1467 /*NOTREACHED*/
1468
1469 case (long)SIG_IGN:
1470 /*
1471 * Masking above should prevent us ever trying
1472 * to take action on an ignored signal other
1473 * than SIGCONT, unless process is traced.
1474 */
1475 if ((prop & SA_CONT) == 0 &&
1476 (p->p_flag & P_TRACED) == 0)
1477 printf("issignal\n");
1478 break; /* == ignore */
1479
1480 default:
1481 /*
1482 * This signal has an action, let
1483 * postsig() process it.
1484 */
1485 signal_unlock(p);
1486 return (signum);
1487 }
1488 p->p_siglist &= ~mask; /* take the signal! */
1489 p->p_sigpending &= ~mask; /* take the pending signal */
1490 }
1491 /* NOTREACHED */
1492 }
1493
1494 /* called from _sleep */
1495 int
1496 CURSIG(p)
1497 register struct proc *p;
1498 {
1499 register int signum, mask, prop, sigbits;
1500 task_t task = p->task;
1501 thread_t cur_thread;
1502 thread_act_t cur_act;
1503 int s;
1504 struct uthread * ut;
1505 int retnum = 0;
1506
1507 if (p->p_siglist == 0)
1508 return (0);
1509
1510 if (((p->p_siglist & ~p->p_sigmask) == 0) && ((p->p_flag & P_TRACED) == 0))
1511 return (0);
1512
1513 cur_thread = current_thread();
1514 cur_act = current_act();
1515
1516 ut = get_bsdthread_info(cur_act);
1517
1518 sigbits = (ut->uu_sig | p->p_siglist) & ~p->p_sigmask;
1519
1520 for(;;) {
1521 if (p->p_flag & P_PPWAIT)
1522 sigbits &= ~stopsigmask;
1523 if (sigbits == 0) { /* no signal to send */
1524 return (retnum);
1525 }
1526
1527 signum = ffs((long)sigbits);
1528 mask = sigmask(signum);
1529 prop = sigprop[signum];
1530
1531 /*
1532 * We should see pending but ignored signals
1533 * only if P_TRACED was on when they were posted.
1534 */
1535 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) {
1536 continue;
1537 }
1538 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1539 /*
1540 * Put the new signal into p_siglist. If the
1541 * signal is being masked, look for other signals.
1542 */
1543 mask = sigmask(signum);
1544 if (p->p_sigmask & mask)
1545 continue;
1546 return(signum);
1547 }
1548
1549 /*
1550 * Decide whether the signal should be returned.
1551 * Return the signal's number, or fall through
1552 * to clear it from the pending mask.
1553 */
1554
1555 switch ((long)p->p_sigacts->ps_sigact[signum]) {
1556
1557 case (long)SIG_DFL:
1558 /*
1559 * Don't take default actions on system processes.
1560 */
1561 if (p->p_pptr->p_pid == 0) {
1562 #if DIAGNOSTIC
1563 /*
1564 * Are you sure you want to ignore SIGSEGV
1565 * in init? XXX
1566 */
1567 printf("Process (pid %d) got signal %d\n",
1568 p->p_pid, signum);
1569 #endif
1570 break; /* == ignore */
1571 }
1572
1573 /*
1574 * If there is a pending stop signal to process
1575 * with default action, stop here,
1576 * then clear the signal. However,
1577 * if process is member of an orphaned
1578 * process group, ignore tty stop signals.
1579 */
1580 if (prop & SA_STOP) {
1581 if (p->p_flag & P_TRACED ||
1582 (p->p_pgrp->pg_jobc == 0 &&
1583 prop & SA_TTYSTOP))
1584 break; /* == ignore */
1585 retnum = signum;
1586 break;
1587 } else if (prop & SA_IGNORE) {
1588 /*
1589 * Except for SIGCONT, shouldn't get here.
1590 * Default action is to ignore; drop it.
1591 */
1592 break; /* == ignore */
1593 } else {
1594 return (signum);
1595 }
1596 /*NOTREACHED*/
1597
1598 case (long)SIG_IGN:
1599 /*
1600 * Masking above should prevent us ever trying
1601 * to take action on an ignored signal other
1602 * than SIGCONT, unless process is traced.
1603 */
1604 if ((prop & SA_CONT) == 0 &&
1605 (p->p_flag & P_TRACED) == 0)
1606 printf("issignal\n");
1607 break; /* == ignore */
1608
1609 default:
1610 /*
1611 * This signal has an action, let
1612 * postsig() process it.
1613 */
1614 return (signum);
1615 }
1616 sigbits &= ~mask; /* take the signal! */
1617 }
1618 /* NOTREACHED */
1619 }
1620
1621 /*
1622 * Put the argument process into the stopped state and notify the parent
1623 * via wakeup. Signals are handled elsewhere. The process must not be
1624 * on the run queue.
1625 */
1626 void
1627 stop(p)
1628 register struct proc *p;
1629 {
1630 p->p_stat = SSTOP;
1631 p->p_flag &= ~P_WAITED;
1632 wakeup((caddr_t)p->p_pptr);
1633 (void) task_suspend(p->task); /*XXX*/
1634 }
1635
1636 /*
1637 * Take the action for the specified signal
1638 * from the current set of pending signals.
1639 */
1640 void
1641 postsig(signum)
1642 register int signum;
1643 {
1644 register struct proc *p = current_proc();
1645 register struct sigacts *ps = p->p_sigacts;
1646 register sig_t action;
1647 u_long code;
1648 int mask, returnmask;
1649
1650 #if DIAGNOSTIC
1651 if (signum == 0)
1652 panic("postsig");
1653 /*
1654 * This must be called on master cpu
1655 */
1656 if (cpu_number() != master_cpu)
1657 panic("psig not on master");
1658 #endif
1659
1660 signal_lock(p);
1661 /*
1662 * Try to grab the signal lock.
1663 */
1664 if (sig_try_locked(p) <= 0) {
1665 signal_unlock(p);
1666 return;
1667 }
1668
1669 mask = sigmask(signum);
1670 p->p_siglist &= ~mask;
1671 action = ps->ps_sigact[signum];
1672 #if KTRACE
1673 if (KTRPOINT(p, KTR_PSIG))
1674 ktrpsig(p->p_tracep,
1675 signum, action, ps->ps_flags & SAS_OLDMASK ?
1676 ps->ps_oldmask : p->p_sigmask, 0);
1677 #endif
1678 if (action == SIG_DFL) {
1679 /*
1680 * Default action, where the default is to kill
1681 * the process. (Other cases were ignored above.)
1682 */
1683 /* called with signal_lock() held */
1684 sigexit_locked(p, signum);
1685 return;
1686 /* NOTREACHED */
1687 } else {
1688 /*
1689 * If we get here, the signal must be caught.
1690 */
1691 #if DIAGNOSTIC
1692 if (action == SIG_IGN || (p->p_sigmask & mask))
1693 log(LOG_WARNING,
1694 "postsig: processing masked or ignored signal\n");
1695 #endif
1696 /*
1697 * Set the new mask value and also defer further
1698 * occurences of this signal.
1699 *
1700 * Special case: user has done a sigpause. Here the
1701 * current mask is not of interest, but rather the
1702 * mask from before the sigpause is what we want
1703 * restored after the signal processing is completed.
1704 */
1705 if (ps->ps_flags & SAS_OLDMASK) {
1706 returnmask = ps->ps_oldmask;
1707 ps->ps_flags &= ~SAS_OLDMASK;
1708 } else
1709 returnmask = p->p_sigmask;
1710 p->p_sigmask |= ps->ps_catchmask[signum] | mask;
1711 if (ps->ps_sig != signum) {
1712 code = 0;
1713 } else {
1714 code = ps->ps_code;
1715 ps->ps_code = 0;
1716 }
1717 p->p_stats->p_ru.ru_nsignals++;
1718 sendsig(p, action, signum, returnmask, code);
1719 }
1720 signal_unlock(p);
1721 }
1722
1723 /*
1724 * Force the current process to exit with the specified signal, dumping core
1725 * if appropriate. We bypass the normal tests for masked and caught signals,
1726 * allowing unrecoverable failures to terminate the process without changing
1727 * signal state. Mark the accounting record with the signal termination.
1728 * If dumping core, save the signal number for the debugger. Calls exit and
1729 * does not return.
1730 */
1731 /* called with signal lock */
1732 void
1733 sigexit_locked(p, signum)
1734 register struct proc *p;
1735 int signum;
1736 {
1737
1738 sig_lock_to_exit(p);
1739 p->p_acflag |= AXSIG;
1740 if (sigprop[signum] & SA_CORE) {
1741 p->p_sigacts->ps_sig = signum;
1742 if (coredump(p) == 0)
1743 signum |= WCOREFLAG;
1744 }
1745 signal_unlock(p);
1746 exit1(p, W_EXITCODE(0, signum));
1747 /* NOTREACHED */
1748 }
1749
1750 void
1751 bsd_ast(thread_act_t thr_act)
1752 {
1753 struct proc *p = current_proc();
1754 struct uthread *ut = get_bsdthread_info(thr_act);
1755 int signum;
1756 unsigned int pc;
1757 boolean_t funnel_state;
1758
1759 if (p == NULL)
1760 return;
1761
1762 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1763
1764 if ((p->p_flag & P_OWEUPC) && (p->p_flag & P_PROFIL)) {
1765 pc = get_useraddr();
1766 addupc_task(p, pc, 1);
1767 p->p_flag &= ~P_OWEUPC;
1768 }
1769
1770 if (CHECK_SIGNALS(p, current_thread(), ut)) {
1771 while (signum = issignal(p))
1772 postsig(signum);
1773 }
1774 ast_off(AST_BSD);
1775
1776 (void) thread_funnel_set(kernel_flock, FALSE);
1777 }
1778
1779 /*
1780 * Follwing routines are called using callout from bsd_hardclock
1781 * so that psignals are called in a thread context and are funneled
1782 */
1783 void
1784 psignal_vtalarm(struct proc *p)
1785 {
1786 boolean_t funnel_state;
1787
1788 if (p == NULL)
1789 return;
1790 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1791 psignal_lock(p, SIGVTALRM, 1, 1);
1792 (void) thread_funnel_set(kernel_flock, FALSE);
1793 }
1794
1795 void
1796 psignal_xcpu(struct proc *p)
1797 {
1798 boolean_t funnel_state;
1799
1800 if (p == NULL)
1801 return;
1802 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1803 psignal_lock(p, SIGXCPU, 1, 1);
1804 (void) thread_funnel_set(kernel_flock, FALSE);
1805 }
1806
1807 void
1808 psignal_sigprof(struct proc *p)
1809 {
1810 boolean_t funnel_state;
1811
1812 if (p == NULL)
1813 return;
1814 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1815 psignal_lock(p, SIGPROF, 1, 1);
1816 (void) thread_funnel_set(kernel_flock, FALSE);
1817 }
1818
1819 /* ptrace set runnalbe */
1820 void
1821 pt_setrunnable(struct proc *p)
1822 {
1823 task_t task;
1824
1825 task = p->task;
1826
1827 if (p->p_flag & P_TRACED) {
1828 p->p_stat = SRUN;
1829 if (p->sigwait) {
1830 wakeup((caddr_t)&(p->sigwait));
1831 task_release(task);
1832 }
1833 }
1834 }