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