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