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