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