]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_sig.c
xnu-201.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, 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 = (struct proc *)(get_bsdtask_info(sig_task));
832
833 uth = get_bsdthread_info(sig_actthread);
834 if (uth && (uth->uu_flag & P_VFORK))
835 p = uth->uu_proc;
836
837 if (!(p->p_flag & P_TRACED) && (p->p_sigignore & mask))
838 return;
839
840 uth->uu_sig |= mask;
841 uth->uu_code = code;
842 /* mark on process as well */
843 p->p_siglist |= mask;
844 signal_setast(sig_actthread);
845 }
846
847
848 void
849 psignal_pend(p)
850 register struct proc *p;
851 {
852 boolean_t funnel_state;
853 register int sigbits, mask, signum;
854
855 thread_funnel_set(kernel_flock, TRUE);
856
857 if (p->p_sigpending == 0)
858 return;
859
860
861 signal_lock(p);
862
863 for (;;) {
864 sigbits = p->p_sigpending;
865 if (sigbits == 0)
866 goto out;
867 signum = ffs((long)sigbits);
868 mask = sigmask(signum);
869 p->p_sigpending &= ~mask;
870
871 psignal_lock(p, signum, 0, 0);
872
873 }
874 out:
875 p->p_flag &= ~P_SIGTHR;
876 signal_unlock(p);
877 thread_funnel_set(kernel_flock, FALSE);
878 }
879
880 void
881 psignal(p, signum)
882 register struct proc *p;
883 register int signum;
884 {
885 psignal_lock(p, signum, 1, 1);
886 }
887
888
889 void
890 psignal_vfork(p, new_task, thr_act, signum)
891 register struct proc *p;
892 task_t new_task;
893 thread_act_t thr_act;
894 register int signum;
895 {
896 int withlock = 1;
897 int pend = 0;
898 register int s, prop;
899 register sig_t action;
900 int mask;
901 kern_return_t kret;
902
903 if ((u_int)signum >= NSIG || signum == 0)
904 panic("psignal signal number");
905 mask = sigmask(signum);
906 prop = sigprop[signum];
907
908 #if SIGNAL_DEBUG
909 if(rdebug_proc && (p == rdebug_proc)) {
910 ram_printf(3);
911 }
912 #endif /* SIGNAL_DEBUG */
913
914 if ((new_task == TASK_NULL) || (thr_act == (thread_act_t)NULL) || is_kerneltask(new_task))
915 return;
916
917
918 signal_lock(p);
919
920 /*
921 * proc is traced, always give parent a chance.
922 */
923 action = SIG_DFL;
924
925 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
926 (p->p_flag & P_TRACED) == 0)
927 p->p_nice = NZERO;
928
929 if (prop & SA_CONT)
930 p->p_siglist &= ~stopsigmask;
931
932 if (prop & SA_STOP) {
933 /*
934 * If sending a tty stop signal to a member of an orphaned
935 * process group, discard the signal here if the action
936 * is default; don't stop the process below if sleeping,
937 * and don't clear any pending SIGCONT.
938 */
939 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
940 action == SIG_DFL)
941 goto psigout;
942 p->p_siglist &= ~contsigmask;
943 }
944 p->p_siglist |= mask;
945
946 /* Deliver signal to the activation passed in */
947 thread_ast_set(thr_act, AST_BSD);
948
949 /*
950 * SIGKILL priority twiddling moved here from above because
951 * it needs sig_thread. Could merge it into large switch
952 * below if we didn't care about priority for tracing
953 * as SIGKILL's action is always SIG_DFL.
954 */
955 if ((signum == SIGKILL) && (p->p_nice > NZERO)) {
956 p->p_nice = NZERO;
957 #if XXX
958 /*
959 * we need to make changes here to get nice to work
960 * reset priority to BASEPRI_USER
961 */
962 #endif
963 }
964
965 /*
966 * This Process is traced - wake it up (if not already
967 * stopped) so that it can discover the signal in
968 * issig() and stop for the parent.
969 */
970 if (p->p_flag & P_TRACED) {
971 if (p->p_stat != SSTOP)
972 goto run;
973 else
974 goto psigout;
975 }
976 run:
977 /*
978 * If we're being traced (possibly because someone attached us
979 * while we were stopped), check for a signal from the debugger.
980 */
981 if (p->p_stat == SSTOP) {
982 if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0)
983 p->p_siglist |= sigmask(p->p_xstat);
984 }
985
986 /*
987 * setrunnable(p) in BSD
988 */
989 p->p_stat = SRUN;
990
991 psigout:
992 signal_unlock(p);
993 }
994
995 /*
996 * Send the signal to the process. If the signal has an action, the action
997 * is usually performed by the target process rather than the caller; we add
998 * the signal to the set of pending signals for the process.
999 *
1000 * Exceptions:
1001 * o When a stop signal is sent to a sleeping process that takes the
1002 * default action, the process is stopped without awakening it.
1003 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1004 * regardless of the signal action (eg, blocked or ignored).
1005 *
1006 * Other ignored signals are discarded immediately.
1007 */
1008 void
1009 psignal_lock(p, signum, withlock, pend)
1010 register struct proc *p;
1011 register int signum;
1012 register int withlock;
1013 register int pend;
1014 {
1015 register int s, prop;
1016 register sig_t action;
1017 thread_act_t sig_thread_act;
1018 thread_t sig_thread;
1019 register task_t sig_task;
1020 register thread_t cur_thread;
1021 thread_act_t *cur_act;
1022 int mask;
1023 kern_return_t kret;
1024
1025 if ((u_int)signum >= NSIG || signum == 0)
1026 panic("psignal signal number");
1027 mask = sigmask(signum);
1028 prop = sigprop[signum];
1029
1030 #if SIGNAL_DEBUG
1031 if(rdebug_proc && (p == rdebug_proc)) {
1032 ram_printf(3);
1033 }
1034 #endif /* SIGNAL_DEBUG */
1035 /*
1036 * We will need the task pointer later. Grab it now to
1037 * check for a zombie process. Also don't send signals
1038 * to kernel internal tasks.
1039 */
1040 if (((sig_task = p->task) == TASK_NULL) || is_kerneltask(sig_task))
1041 return;
1042
1043 /*
1044 * do not send signals to the process that has the thread
1045 * doing a reboot(). Not doing so will mark that thread aborted
1046 * and can cause IO failures wich will cause data loss.
1047 */
1048 if (ISSET(p->p_flag, P_REBOOT))
1049 return;
1050
1051 /*
1052 * if the traced process is blocked waiting for
1053 * gdb then do not block the caller just pend
1054 * the signal. Setup a callout to process the
1055 * pended signal if not alreadu set
1056 */
1057 if (pend && (p->p_flag & P_TRACED) && p->sigwait) {
1058 p->p_sigpending |= mask;
1059 if (!(p->p_flag & P_SIGTHR)) {
1060 p->p_flag |= P_SIGTHR;
1061 thread_call_func((thread_call_func_t)psignal_pend, p,
1062 FALSE);
1063 }
1064 return;
1065 }
1066
1067 if (withlock)
1068 signal_lock(p);
1069
1070 /*
1071 * If proc is traced, always give parent a chance.
1072 */
1073 if (p->p_flag & P_TRACED)
1074 action = SIG_DFL;
1075 else {
1076 /*
1077 * If the signal is being ignored,
1078 * then we forget about it immediately.
1079 * (Note: we don't set SIGCONT in p_sigignore,
1080 * and if it is set to SIG_IGN,
1081 * action will be SIG_DFL here.)
1082 */
1083 if (p->p_sigignore & mask)
1084 goto psigout;
1085 if (p->p_sigmask & mask)
1086 action = SIG_HOLD;
1087 else if (p->p_sigcatch & mask)
1088 action = SIG_CATCH;
1089 else
1090 action = SIG_DFL;
1091 }
1092
1093 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1094 (p->p_flag & P_TRACED) == 0)
1095 p->p_nice = NZERO;
1096
1097 if (prop & SA_CONT)
1098 p->p_siglist &= ~stopsigmask;
1099
1100 if (prop & SA_STOP) {
1101 /*
1102 * If sending a tty stop signal to a member of an orphaned
1103 * process group, discard the signal here if the action
1104 * is default; don't stop the process below if sleeping,
1105 * and don't clear any pending SIGCONT.
1106 */
1107 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1108 action == SIG_DFL)
1109 goto psigout;
1110 p->p_siglist &= ~contsigmask;
1111 }
1112 p->p_siglist |= mask;
1113
1114 /*
1115 * Defer further processing for signals which are held,
1116 * except that stopped processes must be continued by SIGCONT.
1117 */
1118 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
1119 goto psigout;
1120
1121 /*
1122 * Deliver the signal to the first thread in the task. This
1123 * allows single threaded applications which use signals to
1124 * be able to be linked with multithreaded libraries. We have
1125 * an implicit reference to the current_thread, but need
1126 * an explicit one otherwise. The thread reference keeps
1127 * the corresponding task data structures around too. This
1128 * reference is released by thread_deallocate.
1129 */
1130
1131 cur_thread = current_thread(); /* this is a shuttle */
1132 cur_act = current_act();
1133
1134 if ((p->p_flag & P_INVFORK) && p->p_vforkact) {
1135 sig_thread_act = p->p_vforkact;
1136
1137 kret = check_actforsig(sig_task, sig_thread_act, &sig_thread, 1);
1138 if (kret == KERN_SUCCESS) {
1139 goto psig_foundthread;
1140 }
1141 }
1142
1143 /* If successful return with ast set */
1144 kret = (kern_return_t)get_signalact(sig_task,
1145 &sig_thread_act, &sig_thread, 1);
1146
1147 if ((kret != KERN_SUCCESS) || (sig_thread_act == THREAD_NULL)) {
1148 /* XXXX FIXME
1149 /* if it is sigkill, may be we should
1150 * inject a thread to terminate
1151 */
1152 #if DIAGNOSTIC
1153 printf("WARNING: no activation in psignal\n");
1154 #endif
1155 #if SIGNAL_DEBUG
1156 ram_printf(1);
1157 #endif /* SIGNAL_DEBUG */
1158 goto psigout;
1159 }
1160
1161 psig_foundthread:
1162 if (sig_thread == THREAD_NULL) {
1163 #if DIAGNOSTIC
1164 printf("WARNING: valid act; but no shutte in psignal\n");
1165 #endif
1166 #if 0
1167 /* FIXME : NO VALID SHUTTLE */
1168 goto psigout;
1169 #endif
1170 }
1171
1172 /*
1173 * SIGKILL priority twiddling moved here from above because
1174 * it needs sig_thread. Could merge it into large switch
1175 * below if we didn't care about priority for tracing
1176 * as SIGKILL's action is always SIG_DFL.
1177 */
1178 if ((signum == SIGKILL) && (p->p_nice > NZERO)) {
1179 p->p_nice = NZERO;
1180 #if XXX
1181 /*
1182 * we need to make changes here to get nice to work
1183 * reset priority to BASEPRI_USER
1184 */
1185 #endif
1186 }
1187
1188 /*
1189 * Process is traced - wake it up (if not already
1190 * stopped) so that it can discover the signal in
1191 * issig() and stop for the parent.
1192 */
1193 if (p->p_flag & P_TRACED) {
1194 if (p->p_stat != SSTOP)
1195 goto run;
1196 else
1197 goto psigout;
1198 }
1199
1200 if (action != SIG_DFL) {
1201 /*
1202 * User wants to catch the signal.
1203 * Wake up the thread, but don't un-suspend it
1204 * (except for SIGCONT).
1205 */
1206 if (prop & SA_CONT)
1207 (void) task_resume(sig_task);
1208 goto run;
1209 } else {
1210 /* Default action - varies */
1211 if (mask & stopsigmask) {
1212 /*
1213 * These are the signals which by default
1214 * stop a process.
1215 *
1216 * Don't clog system with children of init
1217 * stopped from the keyboard.
1218 */
1219 if (!(prop & SA_STOP) && p->p_pptr == initproc) {
1220 psignal_lock(p, SIGKILL, 0, 1);
1221 p->p_siglist &= ~mask;
1222 goto psigout;
1223 }
1224
1225 /*
1226 * Stop the task.
1227 */
1228 if (!is_thread_running(sig_thread)) {
1229 /* Thread is not running
1230 * If task hasn't already been stopped by
1231 * a signal, stop it.
1232 */
1233 p->p_siglist &= ~mask;
1234 if (get_task_userstop(sig_task) == 0) {
1235 /*
1236 * p_cursig must not be set, because
1237 * it will be psig()'d if it is not
1238 * zero, and the signal is being
1239 * handled here. But save the signal
1240 * in p_stopsig so WUNTRACED
1241 * option to wait can find it.
1242 */
1243 p->p_xstat = signum;
1244 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1245 psignal(p->p_pptr, SIGCHLD);
1246 stop(p);
1247 }
1248 #if 0
1249 /* unconditional check is bad */
1250 signal_clearast(sig_thread_act);
1251 #endif
1252 goto psigout;
1253 } else {
1254 if (p->p_stat != SZOMB)
1255 signal_setast(cur_act);
1256 goto psigout;
1257 }
1258 }
1259
1260 switch (signum) {
1261 /*
1262 * Signals ignored by default have been dealt
1263 * with already, since their bits are on in
1264 * p_sigignore.
1265 */
1266
1267 case SIGKILL:
1268 /*
1269 * Kill signal always sets process running and
1270 * unsuspends it.
1271 */
1272 /*
1273 * Process will be running after 'run'
1274 */
1275 p->p_stat = SRUN;
1276
1277 thread_abort(sig_thread_act);
1278
1279 goto psigout;
1280
1281 case SIGCONT:
1282 /*
1283 * Let the process run. If it's sleeping on an
1284 * event, it remains so.
1285 */
1286 if (p->p_flag & P_TTYSLEEP) {
1287 p->p_flag &= ~P_TTYSLEEP;
1288 wakeup(&p->p_siglist);
1289 } else {
1290 (void) task_resume(sig_task);
1291 }
1292 p->p_siglist &= ~mask;
1293 p->p_stat = SRUN;
1294 #if 0
1295 /* do not clear AST as tcsh is sendig SIGTERM followed by
1296 * SIGCONT and the ast was getting cleared unconditinally
1297 * This is not right.
1298 */
1299 signal_clearast(sig_thread_act);
1300 #endif
1301 goto psigout;
1302
1303 default:
1304 /*
1305 * All other signals wake up the process, but don't
1306 * resume it.
1307 */
1308 goto run;
1309 }
1310 }
1311 /*NOTREACHED*/
1312 run:
1313 /*
1314 * If we're being traced (possibly because someone attached us
1315 * while we were stopped), check for a signal from the debugger.
1316 */
1317 if (p->p_stat == SSTOP) {
1318 if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0)
1319 p->p_siglist |= sigmask(p->p_xstat);
1320 }
1321
1322 /*
1323 * setrunnable(p) in BSD
1324 */
1325 p->p_stat = SRUN;
1326
1327 /*
1328 * Wake up the thread if it is interruptible.
1329 */
1330 thread_abort_safely(sig_thread_act);
1331 psigout:
1332 if (withlock)
1333 signal_unlock(p);
1334 }
1335
1336 __inline__ void
1337 sig_lock_to_exit(
1338 struct proc *p)
1339 {
1340 thread_t self = current_thread();
1341
1342 p->exit_thread = self;
1343 (void) task_suspend(p->task);
1344 }
1345
1346 __inline__ int
1347 sig_try_locked(
1348 struct proc *p)
1349 {
1350 thread_t self = current_thread();
1351
1352 while (p->sigwait || p->exit_thread) {
1353 if (p->exit_thread) {
1354 if (p->exit_thread != self) {
1355 /*
1356 * Already exiting - no signals.
1357 */
1358 thread_abort(current_act());
1359 }
1360 return(0);
1361 }
1362 if(assert_wait_possible()) {
1363 assert_wait((caddr_t)&p->sigwait_thread,
1364 (THREAD_INTERRUPTIBLE));
1365 }
1366 signal_unlock(p);
1367 thread_block(0);
1368 signal_lock(p);
1369 if (thread_should_abort(self)) {
1370 /*
1371 * Terminate request - clean up.
1372 */
1373 return -1;
1374 }
1375 }
1376 return 1;
1377 }
1378
1379 /*
1380 * If the current process has received a signal (should be caught or cause
1381 * termination, should interrupt current syscall), return the signal number.
1382 * Stop signals with default action are processed immediately, then cleared;
1383 * they aren't returned. This is checked after each entry to the system for
1384 * a syscall or trap (though this can usually be done without calling issignal
1385 * by checking the pending signal masks in the CURSIG macro.) The normal call
1386 * sequence is
1387 *
1388 * while (signum = CURSIG(curproc))
1389 * postsig(signum);
1390 */
1391 int
1392 issignal(p)
1393 register struct proc *p;
1394 {
1395 register int signum, mask, prop, sigbits;
1396 task_t task = p->task;
1397 thread_t cur_thread;
1398 thread_act_t cur_act;
1399 int s;
1400 struct uthread * ut;
1401 kern_return_t kret;
1402
1403 cur_thread = current_thread();
1404 cur_act = current_act();
1405
1406 signal_lock(p);
1407
1408
1409 /*
1410 * Try to grab the signal lock.
1411 */
1412 if (sig_try_locked(p) <= 0) {
1413 signal_unlock(p);
1414 return (0);
1415 }
1416
1417 ut = get_bsdthread_info(cur_act);
1418 for(;;) {
1419 sigbits = (ut->uu_sig |p->p_siglist) & ~p->p_sigmask;
1420
1421 if (p->p_flag & P_PPWAIT)
1422 sigbits &= ~stopsigmask;
1423 if (sigbits == 0) { /* no signal to send */
1424 signal_unlock(p);
1425 return (0);
1426 }
1427 signum = ffs((long)sigbits);
1428 mask = sigmask(signum);
1429 prop = sigprop[signum];
1430
1431 if (mask & threadmask) {
1432 /* we can take this signal */
1433 ut->uu_sig &= ~mask;
1434 }
1435
1436 /*
1437 * We should see pending but ignored signals
1438 * only if P_TRACED was on when they were posted.
1439 */
1440 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) {
1441 p->p_siglist &= ~mask; /* take the signal! */
1442 continue;
1443 }
1444 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1445 register int hold;
1446 register task_t task;
1447 /*
1448 * If traced, always stop, and stay
1449 * stopped until released by the debugger.
1450 */
1451 /* ptrace debugging */
1452 p->p_xstat = signum;
1453 psignal(p->p_pptr, SIGCHLD);
1454 /*
1455 * XXX Have to really stop for debuggers;
1456 * XXX stop() doesn't do the right thing.
1457 * XXX Inline the task_suspend because we
1458 * XXX have to diddle Unix state in the
1459 * XXX middle of it.
1460 */
1461 task = p->task;
1462 task_hold(task);
1463 p->sigwait = TRUE;
1464 p->sigwait_thread = cur_act;
1465 p->p_stat = SSTOP;
1466 p->p_flag &= ~P_WAITED;
1467 p->p_siglist &= ~mask; /* clear the old signal */
1468
1469 wakeup((caddr_t)p->p_pptr);
1470 assert_wait((caddr_t)&p->sigwait, (THREAD_INTERRUPTIBLE));
1471 thread_block(0);
1472 p->sigwait = FALSE;
1473 p->sigwait_thread = NULL;
1474 wakeup((caddr_t)&p->sigwait_thread);
1475
1476 /*
1477 * This code is to detect when gdb is killed
1478 * even as the traced program is attached.
1479 * pgsignal would get the SIGKILL to traced program
1480 * That's what we are trying to see (I hope)
1481 */
1482 if (p->p_siglist & sigmask(SIGKILL)) {
1483 /*
1484 * Wait event may still be outstanding;
1485 * clear it, since sig_lock_to_exit will
1486 * wait.
1487 */
1488 clear_wait(current_thread(), THREAD_INTERRUPTED);
1489 sig_lock_to_exit(p);
1490 /*
1491 * Since this thread will be resumed
1492 * to allow the current syscall to
1493 * be completed, must save u_qsave
1494 * before calling exit(). (Since exit()
1495 * calls closef() which can trash u_qsave.)
1496 */
1497 signal_unlock(p);
1498 exit1(p,signum, (int *)NULL);
1499 return(0);
1500 }
1501
1502 /*
1503 * We may have to quit
1504 */
1505 if (thread_should_abort(current_thread())) {
1506 signal_unlock(p);
1507 return(0);
1508 }
1509 /*
1510 * If parent wants us to take the signal,
1511 * then it will leave it in p->p_xstat;
1512 * otherwise we just look for signals again.
1513 */
1514 signum = p->p_xstat;
1515 if (signum == 0)
1516 continue;
1517 /*
1518 * Put the new signal into p_siglist. If the
1519 * signal is being masked, look for other signals.
1520 */
1521 mask = sigmask(signum);
1522 if (mask & threadmask)
1523 ut->uu_sig |= mask;
1524 else
1525 p->p_siglist |= mask;
1526 if (p->p_sigmask & mask)
1527 continue;
1528 }
1529
1530 /*
1531 * Decide whether the signal should be returned.
1532 * Return the signal's number, or fall through
1533 * to clear it from the pending mask.
1534 */
1535
1536 switch ((long)p->p_sigacts->ps_sigact[signum]) {
1537
1538 case (long)SIG_DFL:
1539 /*
1540 * Don't take default actions on system processes.
1541 */
1542 if (p->p_pptr->p_pid == 0) {
1543 #if DIAGNOSTIC
1544 /*
1545 * Are you sure you want to ignore SIGSEGV
1546 * in init? XXX
1547 */
1548 printf("Process (pid %d) got signal %d\n",
1549 p->p_pid, signum);
1550 #endif
1551 break; /* == ignore */
1552 }
1553
1554 /*
1555 * If there is a pending stop signal to process
1556 * with default action, stop here,
1557 * then clear the signal. However,
1558 * if process is member of an orphaned
1559 * process group, ignore tty stop signals.
1560 */
1561 if (prop & SA_STOP) {
1562 if (p->p_flag & P_TRACED ||
1563 (p->p_pgrp->pg_jobc == 0 &&
1564 prop & SA_TTYSTOP))
1565 break; /* == ignore */
1566 p->p_xstat = signum;
1567 stop(p);
1568 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1569 psignal(p->p_pptr, SIGCHLD);
1570 thread_block(0);
1571 /*
1572 * We may have to quit
1573 */
1574 if (thread_should_abort(current_thread())) {
1575 signal_unlock(p);
1576 return(0);
1577 }
1578 break;
1579 } else if (prop & SA_IGNORE) {
1580 /*
1581 * Except for SIGCONT, shouldn't get here.
1582 * Default action is to ignore; drop it.
1583 */
1584 break; /* == ignore */
1585 } else {
1586 p->p_siglist &= ~mask; /* take the signal! */
1587 p->p_sigpending &= ~mask; /* take the pending signal */
1588 signal_unlock(p);
1589 return (signum);
1590 }
1591 /*NOTREACHED*/
1592
1593 case (long)SIG_IGN:
1594 /*
1595 * Masking above should prevent us ever trying
1596 * to take action on an ignored signal other
1597 * than SIGCONT, unless process is traced.
1598 */
1599 if ((prop & SA_CONT) == 0 &&
1600 (p->p_flag & P_TRACED) == 0)
1601 printf("issignal\n");
1602 break; /* == ignore */
1603
1604 default:
1605 /*
1606 * This signal has an action, let
1607 * postsig() process it.
1608 */
1609 p->p_siglist &= ~mask; /* take the signal! */
1610 p->p_sigpending &= ~mask; /* take the pending signal */
1611 signal_unlock(p);
1612 return (signum);
1613 }
1614 p->p_siglist &= ~mask; /* take the signal! */
1615 p->p_sigpending &= ~mask; /* take the pending signal */
1616 }
1617 /* NOTREACHED */
1618 }
1619
1620 /* called from _sleep */
1621 int
1622 CURSIG(p)
1623 register struct proc *p;
1624 {
1625 register int signum, mask, prop, sigbits;
1626 task_t task = p->task;
1627 thread_t cur_thread;
1628 thread_act_t cur_act;
1629 int s;
1630 struct uthread * ut;
1631 int retnum = 0;
1632
1633 if (p->p_siglist == 0)
1634 return (0);
1635
1636 if (((p->p_siglist & ~p->p_sigmask) == 0) && ((p->p_flag & P_TRACED) == 0))
1637 return (0);
1638
1639 cur_thread = current_thread();
1640 cur_act = current_act();
1641
1642 ut = get_bsdthread_info(cur_act);
1643
1644 sigbits = (ut->uu_sig | p->p_siglist) & ~p->p_sigmask;
1645
1646 for(;;) {
1647 if (p->p_flag & P_PPWAIT)
1648 sigbits &= ~stopsigmask;
1649 if (sigbits == 0) { /* no signal to send */
1650 return (retnum);
1651 }
1652
1653 signum = ffs((long)sigbits);
1654 mask = sigmask(signum);
1655 prop = sigprop[signum];
1656
1657 /*
1658 * We should see pending but ignored signals
1659 * only if P_TRACED was on when they were posted.
1660 */
1661 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) {
1662 continue;
1663 }
1664 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1665 /*
1666 * Put the new signal into p_siglist. If the
1667 * signal is being masked, look for other signals.
1668 */
1669 mask = sigmask(signum);
1670 if (p->p_sigmask & mask)
1671 continue;
1672 return(signum);
1673 }
1674
1675 /*
1676 * Decide whether the signal should be returned.
1677 * Return the signal's number, or fall through
1678 * to clear it from the pending mask.
1679 */
1680
1681 switch ((long)p->p_sigacts->ps_sigact[signum]) {
1682
1683 case (long)SIG_DFL:
1684 /*
1685 * Don't take default actions on system processes.
1686 */
1687 if (p->p_pptr->p_pid == 0) {
1688 #if DIAGNOSTIC
1689 /*
1690 * Are you sure you want to ignore SIGSEGV
1691 * in init? XXX
1692 */
1693 printf("Process (pid %d) got signal %d\n",
1694 p->p_pid, signum);
1695 #endif
1696 break; /* == ignore */
1697 }
1698
1699 /*
1700 * If there is a pending stop signal to process
1701 * with default action, stop here,
1702 * then clear the signal. However,
1703 * if process is member of an orphaned
1704 * process group, ignore tty stop signals.
1705 */
1706 if (prop & SA_STOP) {
1707 if (p->p_flag & P_TRACED ||
1708 (p->p_pgrp->pg_jobc == 0 &&
1709 prop & SA_TTYSTOP))
1710 break; /* == ignore */
1711 retnum = signum;
1712 break;
1713 } else if (prop & SA_IGNORE) {
1714 /*
1715 * Except for SIGCONT, shouldn't get here.
1716 * Default action is to ignore; drop it.
1717 */
1718 break; /* == ignore */
1719 } else {
1720 return (signum);
1721 }
1722 /*NOTREACHED*/
1723
1724 case (long)SIG_IGN:
1725 /*
1726 * Masking above should prevent us ever trying
1727 * to take action on an ignored signal other
1728 * than SIGCONT, unless process is traced.
1729 */
1730 if ((prop & SA_CONT) == 0 &&
1731 (p->p_flag & P_TRACED) == 0)
1732 printf("issignal\n");
1733 break; /* == ignore */
1734
1735 default:
1736 /*
1737 * This signal has an action, let
1738 * postsig() process it.
1739 */
1740 return (signum);
1741 }
1742 sigbits &= ~mask; /* take the signal! */
1743 }
1744 /* NOTREACHED */
1745 }
1746
1747 /*
1748 * Put the argument process into the stopped state and notify the parent
1749 * via wakeup. Signals are handled elsewhere. The process must not be
1750 * on the run queue.
1751 */
1752 void
1753 stop(p)
1754 register struct proc *p;
1755 {
1756 p->p_stat = SSTOP;
1757 p->p_flag &= ~P_WAITED;
1758 wakeup((caddr_t)p->p_pptr);
1759 (void) task_suspend(p->task); /*XXX*/
1760 }
1761
1762 /*
1763 * Take the action for the specified signal
1764 * from the current set of pending signals.
1765 */
1766 void
1767 postsig(signum)
1768 register int signum;
1769 {
1770 register struct proc *p = current_proc();
1771 register struct sigacts *ps = p->p_sigacts;
1772 register sig_t action;
1773 u_long code;
1774 int mask, returnmask;
1775
1776 #if DIAGNOSTIC
1777 if (signum == 0)
1778 panic("postsig");
1779 /*
1780 * This must be called on master cpu
1781 */
1782 if (cpu_number() != master_cpu)
1783 panic("psig not on master");
1784 #endif
1785
1786 signal_lock(p);
1787 /*
1788 * Try to grab the signal lock.
1789 */
1790 if (sig_try_locked(p) <= 0) {
1791 signal_unlock(p);
1792 return;
1793 }
1794
1795 mask = sigmask(signum);
1796 p->p_siglist &= ~mask;
1797 action = ps->ps_sigact[signum];
1798 #if KTRACE
1799 if (KTRPOINT(p, KTR_PSIG))
1800 ktrpsig(p->p_tracep,
1801 signum, action, ps->ps_flags & SAS_OLDMASK ?
1802 ps->ps_oldmask : p->p_sigmask, 0);
1803 #endif
1804 if (action == SIG_DFL) {
1805 /*
1806 * Default action, where the default is to kill
1807 * the process. (Other cases were ignored above.)
1808 */
1809 /* called with signal_lock() held */
1810 sigexit_locked(p, signum);
1811 return;
1812 /* NOTREACHED */
1813 } else {
1814 /*
1815 * If we get here, the signal must be caught.
1816 */
1817 #if DIAGNOSTIC
1818 if (action == SIG_IGN || (p->p_sigmask & mask))
1819 log(LOG_WARNING,
1820 "postsig: processing masked or ignored signal\n");
1821 #endif
1822 /*
1823 * Set the new mask value and also defer further
1824 * occurences of this signal.
1825 *
1826 * Special case: user has done a sigpause. Here the
1827 * current mask is not of interest, but rather the
1828 * mask from before the sigpause is what we want
1829 * restored after the signal processing is completed.
1830 */
1831 if (ps->ps_flags & SAS_OLDMASK) {
1832 returnmask = ps->ps_oldmask;
1833 ps->ps_flags &= ~SAS_OLDMASK;
1834 } else
1835 returnmask = p->p_sigmask;
1836 p->p_sigmask |= ps->ps_catchmask[signum] | mask;
1837 if (ps->ps_sig != signum) {
1838 code = 0;
1839 } else {
1840 code = ps->ps_code;
1841 ps->ps_code = 0;
1842 }
1843 p->p_stats->p_ru.ru_nsignals++;
1844 sendsig(p, action, signum, returnmask, code);
1845 }
1846 signal_unlock(p);
1847 }
1848
1849 /*
1850 * Force the current process to exit with the specified signal, dumping core
1851 * if appropriate. We bypass the normal tests for masked and caught signals,
1852 * allowing unrecoverable failures to terminate the process without changing
1853 * signal state. Mark the accounting record with the signal termination.
1854 * If dumping core, save the signal number for the debugger. Calls exit and
1855 * does not return.
1856 */
1857 /* called with signal lock */
1858 void
1859 sigexit_locked(p, signum)
1860 register struct proc *p;
1861 int signum;
1862 {
1863
1864 sig_lock_to_exit(p);
1865 p->p_acflag |= AXSIG;
1866 if (sigprop[signum] & SA_CORE) {
1867 p->p_sigacts->ps_sig = signum;
1868 if (coredump(p) == 0)
1869 signum |= WCOREFLAG;
1870 }
1871 signal_unlock(p);
1872 exit1(p, W_EXITCODE(0, signum), (int *)NULL);
1873 /* NOTREACHED */
1874 }
1875
1876 void
1877 bsd_ast(thread_act_t thr_act)
1878 {
1879 struct proc *p = current_proc();
1880 struct uthread *ut = get_bsdthread_info(thr_act);
1881 int signum;
1882 unsigned int pc;
1883 boolean_t funnel_state;
1884
1885 if (p == NULL)
1886 return;
1887
1888 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1889
1890 if ((p->p_flag & P_OWEUPC) && (p->p_flag & P_PROFIL)) {
1891 pc = get_useraddr();
1892 addupc_task(p, pc, 1);
1893 p->p_flag &= ~P_OWEUPC;
1894 }
1895
1896 if (CHECK_SIGNALS(p, current_thread(), ut)) {
1897 while (signum = issignal(p))
1898 postsig(signum);
1899 }
1900 ast_off(AST_BSD);
1901
1902 (void) thread_funnel_set(kernel_flock, FALSE);
1903 }
1904
1905 /*
1906 * Follwing routines are called using callout from bsd_hardclock
1907 * so that psignals are called in a thread context and are funneled
1908 */
1909 void
1910 psignal_vtalarm(struct proc *p)
1911 {
1912 boolean_t funnel_state;
1913
1914 if (p == NULL)
1915 return;
1916 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1917 psignal_lock(p, SIGVTALRM, 1, 1);
1918 (void) thread_funnel_set(kernel_flock, FALSE);
1919 }
1920
1921 void
1922 psignal_xcpu(struct proc *p)
1923 {
1924 boolean_t funnel_state;
1925
1926 if (p == NULL)
1927 return;
1928 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1929 psignal_lock(p, SIGXCPU, 1, 1);
1930 (void) thread_funnel_set(kernel_flock, FALSE);
1931 }
1932
1933 void
1934 psignal_sigprof(struct proc *p)
1935 {
1936 boolean_t funnel_state;
1937
1938 if (p == NULL)
1939 return;
1940 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1941 psignal_lock(p, SIGPROF, 1, 1);
1942 (void) thread_funnel_set(kernel_flock, FALSE);
1943 }
1944
1945 /* ptrace set runnalbe */
1946 void
1947 pt_setrunnable(struct proc *p)
1948 {
1949 task_t task;
1950
1951 task = p->task;
1952
1953 if (p->p_flag & P_TRACED) {
1954 p->p_stat = SRUN;
1955 if (p->sigwait) {
1956 wakeup((caddr_t)&(p->sigwait));
1957 task_release(task);
1958 }
1959 }
1960 }