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