]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_sig.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_sig.c
CommitLineData
1c79356b 1/*
39037602 2 * Copyright (c) 1995-2016 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 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.
0a7de745 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.
0a7de745 17 *
2d21ac55
A
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.
0a7de745 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 73
0a7de745 74#define SIGPROP /* include signal properties table */
1c79356b 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>
39037602 91#include <sys/reason.h>
91447636 92
1c79356b 93#include <sys/mount.h>
91447636 94#include <sys/sysproto.h>
1c79356b 95
b0d623f7 96#include <security/audit/audit.h>
e5568f75 97
1c79356b
A
98#include <kern/cpu_number.h>
99
100#include <sys/vm.h>
0a7de745
A
101#include <sys/user.h> /* for coredump */
102#include <kern/ast.h> /* for APC support */
39037602 103#include <kern/kalloc.h>
0a7de745 104#include <kern/task.h> /* extern void *get_bsdtask_info(task_t); */
1c79356b 105#include <kern/thread.h>
9bccf70c 106#include <kern/sched_prim.h>
1c79356b 107#include <kern/thread_call.h>
39037602
A
108#include <kern/policy_internal.h>
109
9bccf70c 110#include <mach/exception.h>
91447636
A
111#include <mach/task.h>
112#include <mach/thread_act.h>
2d21ac55
A
113#include <libkern/OSAtomic.h>
114
115#include <sys/sdt.h>
3e170ce0 116#include <sys/codesign.h>
d9a64523 117#include <sys/random.h>
5ba3f43e
A
118#include <libkern/section_keywords.h>
119
120#if CONFIG_MACF
121#include <security/mac_framework.h>
122#endif
91447636
A
123
124/*
125 * Missing prototypes that Mach should export
126 *
127 * +++
128 */
129extern int thread_enable_fpe(thread_t act, int onoff);
0a7de745 130extern kern_return_t get_signalact(task_t, thread_t *, int);
91447636 131extern unsigned int get_useraddr(void);
743345f9
A
132extern boolean_t task_did_exec(task_t task);
133extern boolean_t task_is_exec_copy(task_t task);
f427ee49 134extern void vm_shared_region_reslide_stale(void);
91447636
A
135
136/*
137 * ---
138 */
9bccf70c 139
0a7de745
A
140extern void doexception(int exc, mach_exception_code_t code,
141 mach_exception_subcode_t sub);
1c79356b 142
2d21ac55 143static void stop(proc_t, proc_t);
a39ff7e2
A
144static int cansignal_nomac(proc_t, kauth_cred_t, proc_t, int);
145int cansignal(proc_t, kauth_cred_t, proc_t, int);
2d21ac55 146int killpg1(proc_t, int, int, int, int);
9bccf70c 147kern_return_t do_bsdexception(int, int, int);
91447636 148void __posix_sem_syscall_return(kern_return_t);
3e170ce0 149char *proc_name_address(void *p);
91447636
A
150
151/* implementations in osfmk/kern/sync_sema.c. We do not want port.h in this scope, so void * them */
2d21ac55
A
152kern_return_t semaphore_timedwait_signal_trap_internal(mach_port_name_t, mach_port_name_t, unsigned int, clock_res_t, void (*)(kern_return_t));
153kern_return_t semaphore_timedwait_trap_internal(mach_port_name_t, unsigned int, clock_res_t, void (*)(kern_return_t));
154kern_return_t semaphore_wait_signal_trap_internal(mach_port_name_t, mach_port_name_t, void (*)(kern_return_t));
155kern_return_t semaphore_wait_trap_internal(mach_port_name_t, void (*)(kern_return_t));
1c79356b 156
cb323159 157static int filt_sigattach(struct knote *kn, struct kevent_qos_s *kev);
0a7de745
A
158static void filt_sigdetach(struct knote *kn);
159static int filt_signal(struct knote *kn, long hint);
cb323159
A
160static int filt_signaltouch(struct knote *kn, struct kevent_qos_s *kev);
161static int filt_signalprocess(struct knote *kn, struct kevent_qos_s *kev);
b0d623f7 162
5ba3f43e 163SECURITY_READ_ONLY_EARLY(struct filterops) sig_filtops = {
0a7de745
A
164 .f_attach = filt_sigattach,
165 .f_detach = filt_sigdetach,
166 .f_event = filt_signal,
167 .f_touch = filt_signaltouch,
168 .f_process = filt_signalprocess,
b0d623f7 169};
55e303ae 170
2d21ac55
A
171/* structures and fns for killpg1 iterartion callback and filters */
172struct killpg1_filtargs {
a39ff7e2
A
173 bool posix;
174 proc_t curproc;
2d21ac55
A
175};
176
177struct killpg1_iterargs {
a39ff7e2 178 proc_t curproc;
2d21ac55
A
179 kauth_cred_t uc;
180 int signum;
a39ff7e2 181 int nfound;
2d21ac55
A
182};
183
a39ff7e2 184static int killpg1_allfilt(proc_t p, void * arg);
2d21ac55
A
185static int killpg1_pgrpfilt(proc_t p, __unused void * arg);
186static int killpg1_callback(proc_t p, void * arg);
187
188static int pgsignal_filt(proc_t p, void * arg);
189static int pgsignal_callback(proc_t p, void * arg);
190static kern_return_t get_signalthread(proc_t, int, thread_t *);
191
192
193/* flags for psignal_internal */
194#define PSIG_LOCKED 0x1
195#define PSIG_VFORK 0x2
196#define PSIG_THREAD 0x4
3e170ce0 197#define PSIG_TRY_THREAD 0x8
2d21ac55 198
39037602
A
199static os_reason_t build_signal_reason(int signum, const char *procname);
200static void psignal_internal(proc_t p, task_t task, thread_t thread, int flavor, int signum, os_reason_t signal_reason);
91447636
A
201
202/*
203 * NOTE: Source and target may *NOT* overlap! (target is smaller)
204 */
205static void
b0d623f7 206sigaltstack_kern_to_user32(struct kern_sigaltstack *in, struct user32_sigaltstack *out)
91447636 207{
0a7de745
A
208 out->ss_sp = CAST_DOWN_EXPLICIT(user32_addr_t, in->ss_sp);
209 out->ss_size = CAST_DOWN_EXPLICIT(user32_size_t, in->ss_size);
210 out->ss_flags = in->ss_flags;
b0d623f7
A
211}
212
213static void
214sigaltstack_kern_to_user64(struct kern_sigaltstack *in, struct user64_sigaltstack *out)
215{
0a7de745
A
216 out->ss_sp = in->ss_sp;
217 out->ss_size = in->ss_size;
218 out->ss_flags = in->ss_flags;
91447636
A
219}
220
221/*
222 * NOTE: Source and target may are permitted to overlap! (source is smaller);
223 * this works because we copy fields in order from the end of the struct to
224 * the beginning.
225 */
226static void
b0d623f7
A
227sigaltstack_user32_to_kern(struct user32_sigaltstack *in, struct kern_sigaltstack *out)
228{
0a7de745
A
229 out->ss_flags = in->ss_flags;
230 out->ss_size = in->ss_size;
231 out->ss_sp = CAST_USER_ADDR_T(in->ss_sp);
b0d623f7
A
232}
233static void
234sigaltstack_user64_to_kern(struct user64_sigaltstack *in, struct kern_sigaltstack *out)
91447636 235{
0a7de745 236 out->ss_flags = in->ss_flags;
f427ee49
A
237 out->ss_size = (user_size_t)in->ss_size;
238 out->ss_sp = (user_addr_t)in->ss_sp;
91447636
A
239}
240
241static void
b0d623f7 242sigaction_kern_to_user32(struct kern_sigaction *in, struct user32_sigaction *out)
91447636
A
243{
244 /* This assumes 32 bit __sa_handler is of type sig_t */
0a7de745 245 out->__sigaction_u.__sa_handler = CAST_DOWN_EXPLICIT(user32_addr_t, in->__sigaction_u.__sa_handler);
b0d623f7
A
246 out->sa_mask = in->sa_mask;
247 out->sa_flags = in->sa_flags;
248}
249static void
250sigaction_kern_to_user64(struct kern_sigaction *in, struct user64_sigaction *out)
251{
252 /* This assumes 32 bit __sa_handler is of type sig_t */
253 out->__sigaction_u.__sa_handler = in->__sigaction_u.__sa_handler;
91447636
A
254 out->sa_mask = in->sa_mask;
255 out->sa_flags = in->sa_flags;
256}
257
258static void
b0d623f7 259__sigaction_user32_to_kern(struct __user32_sigaction *in, struct __kern_sigaction *out)
91447636
A
260{
261 out->__sigaction_u.__sa_handler = CAST_USER_ADDR_T(in->__sigaction_u.__sa_handler);
262 out->sa_tramp = CAST_USER_ADDR_T(in->sa_tramp);
263 out->sa_mask = in->sa_mask;
264 out->sa_flags = in->sa_flags;
d9a64523
A
265
266 kern_return_t kr;
267 kr = machine_thread_function_pointers_convert_from_user(current_thread(),
0a7de745 268 &out->sa_tramp, 1);
d9a64523 269 assert(kr == KERN_SUCCESS);
91447636
A
270}
271
b0d623f7
A
272static void
273__sigaction_user64_to_kern(struct __user64_sigaction *in, struct __kern_sigaction *out)
274{
f427ee49
A
275 out->__sigaction_u.__sa_handler = (user_addr_t)in->__sigaction_u.__sa_handler;
276 out->sa_tramp = (user_addr_t)in->sa_tramp;
b0d623f7
A
277 out->sa_mask = in->sa_mask;
278 out->sa_flags = in->sa_flags;
d9a64523
A
279
280 kern_return_t kr;
281 kr = machine_thread_function_pointers_convert_from_user(current_thread(),
0a7de745 282 &out->sa_tramp, 1);
d9a64523 283 assert(kr == KERN_SUCCESS);
b0d623f7 284}
91447636 285
1c79356b 286#if SIGNAL_DEBUG
91447636 287void ram_printf(int);
0a7de745
A
288int ram_debug = 0;
289unsigned int rdebug_proc = 0;
1c79356b
A
290void
291ram_printf(int x)
292{
0a7de745 293 printf("x is %d", x);
1c79356b 294}
1c79356b 295#endif /* SIGNAL_DEBUG */
9bccf70c 296
1c79356b
A
297
298void
2d21ac55 299signal_setast(thread_t sig_actthread)
1c79356b 300{
9bccf70c 301 act_set_astbsd(sig_actthread);
1c79356b
A
302}
303
a39ff7e2
A
304static int
305cansignal_nomac(proc_t src, kauth_cred_t uc_src, proc_t dst, int signum)
1c79356b 306{
9bccf70c 307 /* you can signal yourself */
a39ff7e2
A
308 if (src == dst) {
309 return 1;
310 }
9bccf70c 311
a39ff7e2
A
312 /* you can't send the init proc SIGKILL, even if root */
313 if (signum == SIGKILL && dst == initproc) {
314 return 0;
315 }
3e170ce0 316
0a7de745 317 /* otherwise, root can always signal */
a39ff7e2
A
318 if (kauth_cred_issuser(uc_src)) {
319 return 1;
320 }
321
322 /* processes in the same session can send SIGCONT to each other */
323 {
324 struct session *sess_src = SESSION_NULL;
325 struct session *sess_dst = SESSION_NULL;
1c79356b 326
a39ff7e2 327 /* The session field is protected by the list lock. */
2d21ac55 328 proc_list_lock();
a39ff7e2
A
329 if (src->p_pgrp != PGRP_NULL) {
330 sess_src = src->p_pgrp->pg_session;
331 }
332 if (dst->p_pgrp != PGRP_NULL) {
333 sess_dst = dst->p_pgrp->pg_session;
334 }
335 proc_list_unlock();
336
337 /* allow SIGCONT within session and for processes without session */
338 if (signum == SIGCONT && sess_src == sess_dst) {
339 return 1;
340 }
2d21ac55
A
341 }
342
a39ff7e2
A
343 /* the source process must be authorized to signal the target */
344 {
345 int allowed = 0;
346 kauth_cred_t uc_dst = NOCRED, uc_ref = NOCRED;
1c79356b 347
a39ff7e2
A
348 uc_dst = uc_ref = kauth_cred_proc_ref(dst);
349
350 /*
351 * If the real or effective UID of the sender matches the real or saved
352 * UID of the target, allow the signal to be sent.
353 */
354 if (kauth_cred_getruid(uc_src) == kauth_cred_getruid(uc_dst) ||
0a7de745
A
355 kauth_cred_getruid(uc_src) == kauth_cred_getsvuid(uc_dst) ||
356 kauth_cred_getuid(uc_src) == kauth_cred_getruid(uc_dst) ||
357 kauth_cred_getuid(uc_src) == kauth_cred_getsvuid(uc_dst)) {
a39ff7e2
A
358 allowed = 1;
359 }
360
361 if (uc_ref != NOCRED) {
362 kauth_cred_unref(&uc_ref);
363 uc_ref = NOCRED;
364 }
365
366 return allowed;
1c79356b 367 }
a39ff7e2 368}
1c79356b 369
a39ff7e2
A
370/*
371 * Can process `src`, with ucred `uc_src`, send the signal `signum` to process
372 * `dst`? The ucred is referenced by the caller so internal fileds can be used
373 * safely.
374 */
375int
376cansignal(proc_t src, kauth_cred_t uc_src, proc_t dst, int signum)
377{
378#if CONFIG_MACF
379 if (mac_proc_check_signal(src, dst, signum)) {
380 return 0;
381 }
382#endif
2d21ac55 383
a39ff7e2 384 return cansignal_nomac(src, uc_src, dst, signum);
1c79356b
A
385}
386
3e170ce0
A
387/*
388 * <rdar://problem/21952708> Some signals can be restricted from being handled,
389 * forcing the default action for that signal. This behavior applies only to
390 * non-root (EUID != 0) processes, and is configured with the "sigrestrict=x"
391 * bootarg:
392 *
393 * 0 (default): Disallow use of restricted signals. Trying to register a handler
394 * returns ENOTSUP, which userspace may use to take special action (e.g. abort).
395 * 1: As above, but return EINVAL. Restricted signals behave similarly to SIGKILL.
396 * 2: Usual POSIX semantics.
397 */
398unsigned sigrestrict_arg = 0;
399
4bd07ac2 400#if PLATFORM_WatchOS
3e170ce0
A
401static int
402sigrestrictmask(void)
403{
404 if (kauth_getuid() != 0 && sigrestrict_arg != 2) {
405 return SIGRESTRICTMASK;
406 }
407 return 0;
408}
409
410static int
411signal_is_restricted(proc_t p, int signum)
412{
413 if (sigmask(signum) & sigrestrictmask()) {
414 if (sigrestrict_arg == 0 &&
0a7de745 415 task_get_apptype(p->task) == TASK_APPTYPE_APP_DEFAULT) {
3e170ce0
A
416 return ENOTSUP;
417 } else {
418 return EINVAL;
419 }
420 }
421 return 0;
422}
423
424#else
425
426static inline int
427signal_is_restricted(proc_t p, int signum)
428{
429 (void)p;
430 (void)signum;
431 return 0;
432}
4bd07ac2 433#endif /* !PLATFORM_WatchOS */
1c79356b 434
2d21ac55
A
435/*
436 * Returns: 0 Success
437 * EINVAL
438 * copyout:EFAULT
439 * copyin:EFAULT
b0d623f7
A
440 *
441 * Notes: Uses current thread as a parameter to inform PPC to enable
442 * FPU exceptions via setsigvec(); this operation is not proxy
443 * safe!
2d21ac55 444 */
1c79356b
A
445/* ARGSUSED */
446int
b0d623f7 447sigaction(proc_t p, struct sigaction_args *uap, __unused int32_t *retval)
1c79356b 448{
b0d623f7
A
449 struct kern_sigaction vec;
450 struct __kern_sigaction __vec;
9bccf70c 451
b0d623f7 452 struct kern_sigaction *sa = &vec;
2d21ac55 453 struct sigacts *ps = p->p_sigacts;
91447636 454
2d21ac55 455 int signum;
0a7de745 456 int bit, error = 0;
d9a64523 457 uint32_t sigreturn_validation = PS_SIGRETURN_VALIDATION_DEFAULT;
1c79356b
A
458
459 signum = uap->signum;
460 if (signum <= 0 || signum >= NSIG ||
0a7de745
A
461 signum == SIGKILL || signum == SIGSTOP) {
462 return EINVAL;
463 }
91447636 464
4bd07ac2
A
465 if (uap->nsa) {
466 if (IS_64BIT_PROCESS(p)) {
0a7de745 467 struct __user64_sigaction __vec64;
4bd07ac2
A
468 error = copyin(uap->nsa, &__vec64, sizeof(__vec64));
469 __sigaction_user64_to_kern(&__vec64, &__vec);
470 } else {
0a7de745 471 struct __user32_sigaction __vec32;
4bd07ac2
A
472 error = copyin(uap->nsa, &__vec32, sizeof(__vec32));
473 __sigaction_user32_to_kern(&__vec32, &__vec);
474 }
0a7de745
A
475 if (error) {
476 return error;
477 }
d9a64523
A
478
479 sigreturn_validation = (__vec.sa_flags & SA_VALIDATE_SIGRETURN_FROM_SIGTRAMP) ?
0a7de745 480 PS_SIGRETURN_VALIDATION_ENABLED : PS_SIGRETURN_VALIDATION_DISABLED;
4bd07ac2
A
481 __vec.sa_flags &= SA_USERSPACE_MASK; /* Only pass on valid sa_flags */
482
483 if ((__vec.sa_flags & SA_SIGINFO) || __vec.sa_handler != SIG_DFL) {
484 if ((error = signal_is_restricted(p, signum))) {
485 if (error == ENOTSUP) {
486 printf("%s(%d): denied attempt to register action for signal %d\n",
0a7de745 487 proc_name_address(p), proc_pid(p), signum);
4bd07ac2
A
488 }
489 return error;
490 }
3e170ce0 491 }
3e170ce0
A
492 }
493
1c79356b
A
494 if (uap->osa) {
495 sa->sa_handler = ps->ps_sigact[signum];
496 sa->sa_mask = ps->ps_catchmask[signum];
497 bit = sigmask(signum);
498 sa->sa_flags = 0;
0a7de745 499 if ((ps->ps_sigonstack & bit) != 0) {
1c79356b 500 sa->sa_flags |= SA_ONSTACK;
0a7de745
A
501 }
502 if ((ps->ps_sigintr & bit) == 0) {
1c79356b 503 sa->sa_flags |= SA_RESTART;
0a7de745
A
504 }
505 if (ps->ps_siginfo & bit) {
9bccf70c 506 sa->sa_flags |= SA_SIGINFO;
0a7de745
A
507 }
508 if (ps->ps_signodefer & bit) {
9bccf70c 509 sa->sa_flags |= SA_NODEFER;
0a7de745
A
510 }
511 if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDSTOP)) {
1c79356b 512 sa->sa_flags |= SA_NOCLDSTOP;
0a7de745
A
513 }
514 if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDWAIT)) {
9bccf70c 515 sa->sa_flags |= SA_NOCLDWAIT;
0a7de745 516 }
91447636
A
517
518 if (IS_64BIT_PROCESS(p)) {
0a7de745 519 struct user64_sigaction vec64 = {};
b0d623f7
A
520 sigaction_kern_to_user64(sa, &vec64);
521 error = copyout(&vec64, uap->osa, sizeof(vec64));
91447636 522 } else {
0a7de745 523 struct user32_sigaction vec32 = {};
b0d623f7
A
524 sigaction_kern_to_user32(sa, &vec32);
525 error = copyout(&vec32, uap->osa, sizeof(vec32));
91447636 526 }
0a7de745
A
527 if (error) {
528 return error;
529 }
1c79356b 530 }
4bd07ac2 531
1c79356b 532 if (uap->nsa) {
d9a64523 533 uint32_t old_sigreturn_validation = atomic_load_explicit(
0a7de745 534 &ps->ps_sigreturn_validation, memory_order_relaxed);
d9a64523
A
535 if (old_sigreturn_validation == PS_SIGRETURN_VALIDATION_DEFAULT) {
536 atomic_compare_exchange_strong_explicit(&ps->ps_sigreturn_validation,
0a7de745
A
537 &old_sigreturn_validation, sigreturn_validation,
538 memory_order_relaxed, memory_order_relaxed);
d9a64523 539 }
b0d623f7 540 error = setsigvec(p, current_thread(), signum, &__vec, FALSE);
1c79356b 541 }
4bd07ac2 542
0a7de745 543 return error;
9bccf70c
A
544}
545
546/* Routines to manipulate bits on all threads */
547int
0a7de745 548clear_procsiglist(proc_t p, int bit, boolean_t in_signalstart)
9bccf70c
A
549{
550 struct uthread * uth;
91447636 551 thread_t thact;
9bccf70c 552
2d21ac55 553 proc_lock(p);
0a7de745 554 if (!in_signalstart) {
b0d623f7 555 proc_signalstart(p, 1);
0a7de745 556 }
9bccf70c 557
2d21ac55 558 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
0a7de745 559 thact = p->p_vforkact;
9bccf70c
A
560 uth = (struct uthread *)get_bsdthread_info(thact);
561 if (uth) {
562 uth->uu_siglist &= ~bit;
563 }
0a7de745 564 if (!in_signalstart) {
b0d623f7 565 proc_signalend(p, 1);
0a7de745 566 }
2d21ac55 567 proc_unlock(p);
0a7de745
A
568 return 0;
569 }
9bccf70c
A
570
571 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
572 uth->uu_siglist &= ~bit;
573 }
b0d623f7 574 p->p_siglist &= ~bit;
0a7de745 575 if (!in_signalstart) {
b0d623f7 576 proc_signalend(p, 1);
0a7de745 577 }
2d21ac55
A
578 proc_unlock(p);
579
0a7de745 580 return 0;
1c79356b
A
581}
582
91447636
A
583
584static int
0a7de745 585unblock_procsigmask(proc_t p, int bit)
1c79356b 586{
9bccf70c 587 struct uthread * uth;
91447636 588 thread_t thact;
9bccf70c 589
2d21ac55
A
590 proc_lock(p);
591 proc_signalstart(p, 1);
592
593 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
0a7de745 594 thact = p->p_vforkact;
9bccf70c
A
595 uth = (struct uthread *)get_bsdthread_info(thact);
596 if (uth) {
597 uth->uu_sigmask &= ~bit;
598 }
599 p->p_sigmask &= ~bit;
2d21ac55
A
600 proc_signalend(p, 1);
601 proc_unlock(p);
0a7de745
A
602 return 0;
603 }
9bccf70c
A
604 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
605 uth->uu_sigmask &= ~bit;
1c79356b 606 }
9bccf70c 607 p->p_sigmask &= ~bit;
2d21ac55
A
608
609 proc_signalend(p, 1);
610 proc_unlock(p);
0a7de745 611 return 0;
1c79356b
A
612}
613
91447636 614static int
0a7de745 615block_procsigmask(proc_t p, int bit)
1c79356b 616{
9bccf70c 617 struct uthread * uth;
91447636 618 thread_t thact;
1c79356b 619
2d21ac55
A
620 proc_lock(p);
621 proc_signalstart(p, 1);
622
623 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
0a7de745 624 thact = p->p_vforkact;
9bccf70c
A
625 uth = (struct uthread *)get_bsdthread_info(thact);
626 if (uth) {
627 uth->uu_sigmask |= bit;
628 }
629 p->p_sigmask |= bit;
2d21ac55
A
630 proc_signalend(p, 1);
631 proc_unlock(p);
0a7de745
A
632 return 0;
633 }
9bccf70c
A
634 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
635 uth->uu_sigmask |= bit;
636 }
637 p->p_sigmask |= bit;
2d21ac55
A
638
639 proc_signalend(p, 1);
640 proc_unlock(p);
0a7de745 641 return 0;
1c79356b 642}
91447636 643
9bccf70c 644int
0a7de745 645set_procsigmask(proc_t p, int bit)
9bccf70c
A
646{
647 struct uthread * uth;
91447636 648 thread_t thact;
1c79356b 649
2d21ac55
A
650 proc_lock(p);
651 proc_signalstart(p, 1);
652
653 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
0a7de745 654 thact = p->p_vforkact;
9bccf70c
A
655 uth = (struct uthread *)get_bsdthread_info(thact);
656 if (uth) {
657 uth->uu_sigmask = bit;
658 }
659 p->p_sigmask = bit;
2d21ac55
A
660 proc_signalend(p, 1);
661 proc_unlock(p);
0a7de745
A
662 return 0;
663 }
9bccf70c
A
664 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
665 uth->uu_sigmask = bit;
666 }
667 p->p_sigmask = bit;
2d21ac55
A
668 proc_signalend(p, 1);
669 proc_unlock(p);
670
0a7de745 671 return 0;
9bccf70c 672}
1c79356b 673
91447636 674/* XXX should be static? */
b0d623f7
A
675/*
676 * Notes: The thread parameter is used in the PPC case to select the
677 * thread on which the floating point exception will be enabled
678 * or disabled. We can't simply take current_thread(), since
679 * this is called from posix_spawn() on the not currently running
680 * process/thread pair.
681 *
682 * We mark thread as unused to alow compilation without warning
6d2010ae 683 * on non-PPC platforms.
b0d623f7 684 */
9bccf70c 685int
b0d623f7 686setsigvec(proc_t p, __unused thread_t thread, int signum, struct __kern_sigaction *sa, boolean_t in_sigstart)
1c79356b 687{
2d21ac55
A
688 struct sigacts *ps = p->p_sigacts;
689 int bit;
1c79356b 690
39037602
A
691 assert(signum < NSIG);
692
9bccf70c 693 if ((signum == SIGKILL || signum == SIGSTOP) &&
0a7de745
A
694 sa->sa_handler != SIG_DFL) {
695 return EINVAL;
696 }
1c79356b
A
697 bit = sigmask(signum);
698 /*
699 * Change setting atomically.
700 */
701 ps->ps_sigact[signum] = sa->sa_handler;
91447636 702 ps->ps_trampact[signum] = sa->sa_tramp;
0a7de745
A
703 ps->ps_catchmask[signum] = sa->sa_mask & ~sigcantmask;
704 if (sa->sa_flags & SA_SIGINFO) {
9bccf70c 705 ps->ps_siginfo |= bit;
0a7de745 706 } else {
9bccf70c 707 ps->ps_siginfo &= ~bit;
0a7de745
A
708 }
709 if ((sa->sa_flags & SA_RESTART) == 0) {
1c79356b 710 ps->ps_sigintr |= bit;
0a7de745 711 } else {
1c79356b 712 ps->ps_sigintr &= ~bit;
0a7de745
A
713 }
714 if (sa->sa_flags & SA_ONSTACK) {
1c79356b 715 ps->ps_sigonstack |= bit;
0a7de745 716 } else {
1c79356b 717 ps->ps_sigonstack &= ~bit;
0a7de745
A
718 }
719 if (sa->sa_flags & SA_RESETHAND) {
9bccf70c 720 ps->ps_sigreset |= bit;
0a7de745 721 } else {
9bccf70c 722 ps->ps_sigreset &= ~bit;
0a7de745
A
723 }
724 if (sa->sa_flags & SA_NODEFER) {
9bccf70c 725 ps->ps_signodefer |= bit;
0a7de745 726 } else {
9bccf70c 727 ps->ps_signodefer &= ~bit;
0a7de745 728 }
1c79356b 729 if (signum == SIGCHLD) {
0a7de745 730 if (sa->sa_flags & SA_NOCLDSTOP) {
b0d623f7 731 OSBitOrAtomic(P_NOCLDSTOP, &p->p_flag);
0a7de745 732 } else {
b0d623f7 733 OSBitAndAtomic(~((uint32_t)P_NOCLDSTOP), &p->p_flag);
0a7de745
A
734 }
735 if ((sa->sa_flags & SA_NOCLDWAIT) || (sa->sa_handler == SIG_IGN)) {
b0d623f7 736 OSBitOrAtomic(P_NOCLDWAIT, &p->p_flag);
0a7de745 737 } else {
b0d623f7 738 OSBitAndAtomic(~((uint32_t)P_NOCLDWAIT), &p->p_flag);
0a7de745 739 }
1c79356b 740 }
9bccf70c 741
1c79356b
A
742 /*
743 * Set bit in p_sigignore for signals that are set to SIG_IGN,
744 * and for signals set to SIG_DFL where the default is to ignore.
745 * However, don't put SIGCONT in p_sigignore,
746 * as we have to restart the process.
747 */
748 if (sa->sa_handler == SIG_IGN ||
749 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
b0d623f7 750 clear_procsiglist(p, bit, in_sigstart);
0a7de745
A
751 if (signum != SIGCONT) {
752 p->p_sigignore |= bit; /* easier in psignal */
753 }
1c79356b
A
754 p->p_sigcatch &= ~bit;
755 } else {
756 p->p_sigignore &= ~bit;
0a7de745 757 if (sa->sa_handler == SIG_DFL) {
1c79356b 758 p->p_sigcatch &= ~bit;
0a7de745 759 } else {
1c79356b 760 p->p_sigcatch |= bit;
0a7de745 761 }
1c79356b 762 }
0a7de745 763 return 0;
1c79356b
A
764}
765
766/*
767 * Initialize signal state for process 0;
768 * set to ignore signals that are ignored by default.
769 */
770void
2d21ac55 771siginit(proc_t p)
1c79356b 772{
2d21ac55 773 int i;
1c79356b 774
0a7de745
A
775 for (i = 1; i < NSIG; i++) {
776 if (sigprop[i] & SA_IGNORE && i != SIGCONT) {
1c79356b 777 p->p_sigignore |= sigmask(i);
0a7de745
A
778 }
779 }
1c79356b
A
780}
781
782/*
783 * Reset signals for an exec of the specified process.
784 */
785void
2d21ac55 786execsigs(proc_t p, thread_t thread)
1c79356b 787{
2d21ac55
A
788 struct sigacts *ps = p->p_sigacts;
789 int nc, mask;
790 struct uthread *ut;
1c79356b 791
2d21ac55 792 ut = (struct uthread *)get_bsdthread_info(thread);
b0d623f7
A
793
794 /*
795 * transfer saved signal states from the process
796 * back to the current thread.
797 *
798 * NOTE: We do this without the process locked,
799 * because we are guaranteed to be single-threaded
800 * by this point in exec and the p_siglist is
801 * only accessed by threads inside the process.
802 */
803 ut->uu_siglist |= p->p_siglist;
804 p->p_siglist = 0;
805
1c79356b
A
806 /*
807 * Reset caught signals. Held signals remain held
808 * through p_sigmask (unless they were caught,
809 * and are now ignored by default).
810 */
811 while (p->p_sigcatch) {
f427ee49 812 nc = ffs((unsigned int)p->p_sigcatch);
1c79356b
A
813 mask = sigmask(nc);
814 p->p_sigcatch &= ~mask;
815 if (sigprop[nc] & SA_IGNORE) {
0a7de745 816 if (nc != SIGCONT) {
1c79356b 817 p->p_sigignore |= mask;
0a7de745 818 }
b0d623f7 819 ut->uu_siglist &= ~mask;
1c79356b
A
820 }
821 ps->ps_sigact[nc] = SIG_DFL;
822 }
b0d623f7 823
d9a64523 824 atomic_store_explicit(&ps->ps_sigreturn_validation,
0a7de745 825 PS_SIGRETURN_VALIDATION_DEFAULT, memory_order_relaxed);
d9a64523
A
826 /* Generate random token value used to validate sigreturn arguments */
827 read_random(&ps->ps_sigreturn_token, sizeof(ps->ps_sigreturn_token));
828
1c79356b
A
829 /*
830 * Reset stack state to the user stack.
831 * Clear set of signals caught on the signal stack.
832 */
2d21ac55
A
833 /* thread */
834 ut->uu_sigstk.ss_flags = SA_DISABLE;
835 ut->uu_sigstk.ss_size = 0;
836 ut->uu_sigstk.ss_sp = USER_ADDR_NULL;
837 ut->uu_flag &= ~UT_ALTSTACK;
838 /* process */
0c530ab8 839 ps->ps_sigonstack = 0;
1c79356b
A
840}
841
842/*
843 * Manipulate signal mask.
844 * Note that we receive new mask, not pointer,
845 * and return old mask as return value;
846 * the library stub does the rest.
847 */
1c79356b 848int
b0d623f7 849sigprocmask(proc_t p, struct sigprocmask_args *uap, __unused int32_t *retval)
1c79356b
A
850{
851 int error = 0;
9bccf70c 852 sigset_t oldmask, nmask;
91447636 853 user_addr_t omask = uap->omask;
9bccf70c 854 struct uthread *ut;
1c79356b 855
91447636 856 ut = (struct uthread *)get_bsdthread_info(current_thread());
9bccf70c
A
857 oldmask = ut->uu_sigmask;
858
91447636 859 if (uap->mask == USER_ADDR_NULL) {
9bccf70c
A
860 /* just want old mask */
861 goto out;
862 }
91447636 863 error = copyin(uap->mask, &nmask, sizeof(sigset_t));
0a7de745 864 if (error) {
9bccf70c 865 goto out;
0a7de745 866 }
1c79356b
A
867
868 switch (uap->how) {
869 case SIG_BLOCK:
9bccf70c 870 block_procsigmask(p, (nmask & ~sigcantmask));
91447636 871 signal_setast(current_thread());
1c79356b
A
872 break;
873
874 case SIG_UNBLOCK:
9bccf70c 875 unblock_procsigmask(p, (nmask & ~sigcantmask));
91447636 876 signal_setast(current_thread());
1c79356b
A
877 break;
878
879 case SIG_SETMASK:
9bccf70c 880 set_procsigmask(p, (nmask & ~sigcantmask));
91447636 881 signal_setast(current_thread());
1c79356b 882 break;
0a7de745 883
1c79356b
A
884 default:
885 error = EINVAL;
886 break;
887 }
9bccf70c 888out:
0a7de745 889 if (!error && omask != USER_ADDR_NULL) {
9bccf70c 890 copyout(&oldmask, omask, sizeof(sigset_t));
0a7de745
A
891 }
892 return error;
1c79356b
A
893}
894
1c79356b 895int
b0d623f7 896sigpending(__unused proc_t p, struct sigpending_args *uap, __unused int32_t *retval)
1c79356b 897{
9bccf70c
A
898 struct uthread *ut;
899 sigset_t pendlist;
1c79356b 900
91447636 901 ut = (struct uthread *)get_bsdthread_info(current_thread());
9bccf70c
A
902 pendlist = ut->uu_siglist;
903
0a7de745 904 if (uap->osv) {
9bccf70c 905 copyout(&pendlist, uap->osv, sizeof(sigset_t));
0a7de745
A
906 }
907 return 0;
1c79356b
A
908}
909
1c79356b
A
910/*
911 * Suspend process until signal, providing mask to be set
912 * in the meantime. Note nonstandard calling convention:
913 * libc stub passes mask, not pointer, to save a copyin.
914 */
915
91447636
A
916static int
917sigcontinue(__unused int error)
1c79356b 918{
91447636 919// struct uthread *ut = get_bsdthread_info(current_thread());
2d21ac55
A
920 unix_syscall_return(EINTR);
921}
922
923int
b0d623f7 924sigsuspend(proc_t p, struct sigsuspend_args *uap, int32_t *retval)
2d21ac55
A
925{
926 __pthread_testcancel(1);
0a7de745 927 return sigsuspend_nocancel(p, (struct sigsuspend_nocancel_args *)uap, retval);
1c79356b
A
928}
929
1c79356b 930int
b0d623f7 931sigsuspend_nocancel(proc_t p, struct sigsuspend_nocancel_args *uap, __unused int32_t *retval)
1c79356b 932{
9bccf70c
A
933 struct uthread *ut;
934
91447636 935 ut = (struct uthread *)get_bsdthread_info(current_thread());
1c79356b
A
936
937 /*
938 * When returning from sigpause, we want
939 * the old mask to be restored after the
940 * signal handler has finished. Thus, we
941 * save it here and mark the sigacts structure
942 * to indicate this.
943 */
9bccf70c 944 ut->uu_oldmask = ut->uu_sigmask;
91447636 945 ut->uu_flag |= UT_SAS_OLDMASK;
9bccf70c 946 ut->uu_sigmask = (uap->mask & ~sigcantmask);
0a7de745 947 (void) tsleep0((caddr_t) p, PPAUSE | PCATCH, "pause", 0, sigcontinue);
1c79356b 948 /* always return EINTR rather than ERESTART... */
0a7de745 949 return EINTR;
1c79356b
A
950}
951
9bccf70c
A
952
953int
2d21ac55 954__disable_threadsignal(__unused proc_t p,
0a7de745
A
955 __unused struct __disable_threadsignal_args *uap,
956 __unused int32_t *retval)
9bccf70c
A
957{
958 struct uthread *uth;
959
91447636 960 uth = (struct uthread *)get_bsdthread_info(current_thread());
9bccf70c
A
961
962 /* No longer valid to have any signal delivered */
2d21ac55 963 uth->uu_flag |= (UT_NO_SIGMASK | UT_CANCELDISABLE);
9bccf70c 964
0a7de745 965 return 0;
9bccf70c
A
966}
967
2d21ac55
A
968void
969__pthread_testcancel(int presyscall)
970{
2d21ac55
A
971 thread_t self = current_thread();
972 struct uthread * uthread;
973
974 uthread = (struct uthread *)get_bsdthread_info(self);
975
0a7de745 976
2d21ac55
A
977 uthread->uu_flag &= ~UT_NOTCANCELPT;
978
979 if ((uthread->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
0a7de745 980 if (presyscall != 0) {
2d21ac55
A
981 unix_syscall_return(EINTR);
982 /* NOTREACHED */
0a7de745 983 } else {
2d21ac55 984 thread_abort_safely(self);
0a7de745 985 }
2d21ac55
A
986 }
987}
988
989
9bccf70c 990
91447636 991int
2d21ac55 992__pthread_markcancel(__unused proc_t p,
0a7de745 993 struct __pthread_markcancel_args *uap, __unused int32_t *retval)
9bccf70c
A
994{
995 thread_act_t target_act;
996 int error = 0;
9bccf70c
A
997 struct uthread *uth;
998
cb323159
A
999 target_act = (thread_act_t)port_name_to_thread(uap->thread_port,
1000 PORT_TO_THREAD_IN_CURRENT_TASK);
9bccf70c 1001
0a7de745
A
1002 if (target_act == THR_ACT_NULL) {
1003 return ESRCH;
1004 }
91447636
A
1005
1006 uth = (struct uthread *)get_bsdthread_info(target_act);
1007
1008 /* if the thread is in vfork do not cancel */
0a7de745 1009 if ((uth->uu_flag & (UT_VFORK | UT_CANCEL | UT_CANCELED)) == 0) {
91447636 1010 uth->uu_flag |= (UT_CANCEL | UT_NO_SIGMASK);
0a7de745
A
1011 if (((uth->uu_flag & UT_NOTCANCELPT) == 0)
1012 && ((uth->uu_flag & UT_CANCELDISABLE) == 0)) {
1013 thread_abort_safely(target_act);
1014 }
91447636
A
1015 }
1016
1017 thread_deallocate(target_act);
0a7de745 1018 return error;
91447636
A
1019}
1020
0a7de745 1021/* if action =0 ; return the cancellation state ,
91447636
A
1022 * if marked for cancellation, make the thread canceled
1023 * if action = 1 ; Enable the cancel handling
1024 * if action = 2; Disable the cancel handling
1025 */
1026int
2d21ac55 1027__pthread_canceled(__unused proc_t p,
0a7de745 1028 struct __pthread_canceled_args *uap, __unused int32_t *retval)
91447636 1029{
2d21ac55 1030 thread_act_t thread;
91447636
A
1031 struct uthread *uth;
1032 int action = uap->action;
1033
2d21ac55
A
1034 thread = current_thread();
1035 uth = (struct uthread *)get_bsdthread_info(thread);
91447636
A
1036
1037 switch (action) {
0a7de745
A
1038 case 1:
1039 uth->uu_flag &= ~UT_CANCELDISABLE;
1040 return 0;
1041 case 2:
1042 uth->uu_flag |= UT_CANCELDISABLE;
1043 return 0;
1044 case 0:
1045 default:
1046 /* if the thread is in vfork do not cancel */
1047 if ((uth->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
1048 uth->uu_flag &= ~UT_CANCEL;
1049 uth->uu_flag |= (UT_CANCELED | UT_NO_SIGMASK);
1050 return 0;
1051 }
1052 return EINVAL;
1053 }
1054 return EINVAL;
91447636
A
1055}
1056
39037602 1057__attribute__((noreturn))
91447636 1058void
0a7de745 1059__posix_sem_syscall_return(kern_return_t kern_result)
91447636
A
1060{
1061 int error = 0;
1062
0a7de745 1063 if (kern_result == KERN_SUCCESS) {
91447636 1064 error = 0;
0a7de745 1065 } else if (kern_result == KERN_ABORTED) {
91447636 1066 error = EINTR;
0a7de745 1067 } else if (kern_result == KERN_OPERATION_TIMED_OUT) {
91447636 1068 error = ETIMEDOUT;
0a7de745 1069 } else {
91447636 1070 error = EINVAL;
0a7de745 1071 }
91447636
A
1072 unix_syscall_return(error);
1073 /* does not return */
1074}
1075
b0d623f7 1076#if OLD_SEMWAIT_SIGNAL
2d21ac55
A
1077/*
1078 * Returns: 0 Success
1079 * EINTR
1080 * ETIMEDOUT
1081 * EINVAL
b0d623f7 1082 * EFAULT if timespec is NULL
2d21ac55
A
1083 */
1084int
b0d623f7 1085__old_semwait_signal(proc_t p, struct __old_semwait_signal_args *uap,
0a7de745 1086 int32_t *retval)
2d21ac55
A
1087{
1088 __pthread_testcancel(0);
0a7de745 1089 return __old_semwait_signal_nocancel(p, (struct __old_semwait_signal_nocancel_args *)uap, retval);
2d21ac55 1090}
91447636
A
1091
1092int
b0d623f7 1093__old_semwait_signal_nocancel(proc_t p, struct __old_semwait_signal_nocancel_args *uap,
0a7de745 1094 __unused int32_t *retval)
91447636 1095{
91447636 1096 kern_return_t kern_result;
b0d623f7 1097 int error;
91447636
A
1098 mach_timespec_t then;
1099 struct timespec now;
b0d623f7
A
1100 struct user_timespec ts;
1101 boolean_t truncated_timeout = FALSE;
0a7de745
A
1102
1103 if (uap->timeout) {
b0d623f7
A
1104 if (IS_64BIT_PROCESS(p)) {
1105 struct user64_timespec ts64;
1106 error = copyin(uap->ts, &ts64, sizeof(ts64));
f427ee49
A
1107 ts.tv_sec = (user_time_t)ts64.tv_sec;
1108 ts.tv_nsec = (user_long_t)ts64.tv_nsec;
b0d623f7
A
1109 } else {
1110 struct user32_timespec ts32;
1111 error = copyin(uap->ts, &ts32, sizeof(ts32));
1112 ts.tv_sec = ts32.tv_sec;
1113 ts.tv_nsec = ts32.tv_nsec;
1114 }
0a7de745 1115
b0d623f7
A
1116 if (error) {
1117 return error;
1118 }
0a7de745 1119
b0d623f7
A
1120 if ((ts.tv_sec & 0xFFFFFFFF00000000ULL) != 0) {
1121 ts.tv_sec = 0xFFFFFFFF;
1122 ts.tv_nsec = 0;
1123 truncated_timeout = TRUE;
1124 }
0a7de745 1125
91447636 1126 if (uap->relative) {
f427ee49
A
1127 then.tv_sec = (unsigned int)ts.tv_sec;
1128 then.tv_nsec = (clock_res_t)ts.tv_nsec;
91447636
A
1129 } else {
1130 nanotime(&now);
0a7de745 1131
2d21ac55 1132 /* if time has elapsed, set time to null timepsec to bailout rightaway */
b0d623f7 1133 if (now.tv_sec == ts.tv_sec ?
0a7de745
A
1134 now.tv_nsec > ts.tv_nsec :
1135 now.tv_sec > ts.tv_sec) {
2d21ac55
A
1136 then.tv_sec = 0;
1137 then.tv_nsec = 0;
b0d623f7 1138 } else {
f427ee49
A
1139 then.tv_sec = (unsigned int)(ts.tv_sec - now.tv_sec);
1140 then.tv_nsec = (clock_res_t)(ts.tv_nsec - now.tv_nsec);
b0d623f7
A
1141 if (then.tv_nsec < 0) {
1142 then.tv_nsec += NSEC_PER_SEC;
0a7de745 1143 then.tv_sec--;
b0d623f7 1144 }
2d21ac55 1145 }
91447636 1146 }
0a7de745
A
1147
1148 if (uap->mutex_sem == 0) {
2d21ac55 1149 kern_result = semaphore_timedwait_trap_internal((mach_port_name_t)uap->cond_sem, then.tv_sec, then.tv_nsec, __posix_sem_syscall_return);
0a7de745 1150 } else {
91447636 1151 kern_result = semaphore_timedwait_signal_trap_internal(uap->cond_sem, uap->mutex_sem, then.tv_sec, then.tv_nsec, __posix_sem_syscall_return);
0a7de745 1152 }
91447636 1153 } else {
0a7de745 1154 if (uap->mutex_sem == 0) {
91447636 1155 kern_result = semaphore_wait_trap_internal(uap->cond_sem, __posix_sem_syscall_return);
0a7de745 1156 } else {
91447636 1157 kern_result = semaphore_wait_signal_trap_internal(uap->cond_sem, uap->mutex_sem, __posix_sem_syscall_return);
0a7de745
A
1158 }
1159 }
1160
1161 if (kern_result == KERN_SUCCESS && !truncated_timeout) {
1162 return 0;
1163 } else if (kern_result == KERN_SUCCESS && truncated_timeout) {
1164 return EINTR; /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1165 } else if (kern_result == KERN_ABORTED) {
1166 return EINTR;
1167 } else if (kern_result == KERN_OPERATION_TIMED_OUT) {
1168 return ETIMEDOUT;
1169 } else {
1170 return EINVAL;
91447636 1171 }
b0d623f7
A
1172}
1173#endif /* OLD_SEMWAIT_SIGNAL*/
1174
1175/*
1176 * Returns: 0 Success
1177 * EINTR
1178 * ETIMEDOUT
1179 * EINVAL
1180 * EFAULT if timespec is NULL
1181 */
1182int
1183__semwait_signal(proc_t p, struct __semwait_signal_args *uap,
0a7de745 1184 int32_t *retval)
b0d623f7
A
1185{
1186 __pthread_testcancel(0);
0a7de745 1187 return __semwait_signal_nocancel(p, (struct __semwait_signal_nocancel_args *)uap, retval);
b0d623f7
A
1188}
1189
1190int
1191__semwait_signal_nocancel(__unused proc_t p, struct __semwait_signal_nocancel_args *uap,
0a7de745 1192 __unused int32_t *retval)
b0d623f7 1193{
b0d623f7
A
1194 kern_return_t kern_result;
1195 mach_timespec_t then;
1196 struct timespec now;
1197 struct user_timespec ts;
0a7de745
A
1198 boolean_t truncated_timeout = FALSE;
1199
1200 if (uap->timeout) {
f427ee49 1201 ts.tv_sec = (user_time_t)uap->tv_sec;
0a7de745
A
1202 ts.tv_nsec = uap->tv_nsec;
1203
1204 if ((ts.tv_sec & 0xFFFFFFFF00000000ULL) != 0) {
1205 ts.tv_sec = 0xFFFFFFFF;
1206 ts.tv_nsec = 0;
1207 truncated_timeout = TRUE;
1208 }
1209
b0d623f7 1210 if (uap->relative) {
f427ee49
A
1211 then.tv_sec = (unsigned int)ts.tv_sec;
1212 then.tv_nsec = (clock_res_t)ts.tv_nsec;
b0d623f7
A
1213 } else {
1214 nanotime(&now);
1215
0a7de745
A
1216 /* if time has elapsed, set time to null timepsec to bailout rightaway */
1217 if (now.tv_sec == ts.tv_sec ?
1218 now.tv_nsec > ts.tv_nsec :
1219 now.tv_sec > ts.tv_sec) {
1220 then.tv_sec = 0;
1221 then.tv_nsec = 0;
1222 } else {
f427ee49
A
1223 then.tv_sec = (unsigned int)(ts.tv_sec - now.tv_sec);
1224 then.tv_nsec = (clock_res_t)(ts.tv_nsec - now.tv_nsec);
0a7de745
A
1225 if (then.tv_nsec < 0) {
1226 then.tv_nsec += NSEC_PER_SEC;
1227 then.tv_sec--;
1228 }
1229 }
1230 }
1231
1232 if (uap->mutex_sem == 0) {
b0d623f7 1233 kern_result = semaphore_timedwait_trap_internal((mach_port_name_t)uap->cond_sem, then.tv_sec, then.tv_nsec, __posix_sem_syscall_return);
0a7de745 1234 } else {
b0d623f7 1235 kern_result = semaphore_timedwait_signal_trap_internal(uap->cond_sem, uap->mutex_sem, then.tv_sec, then.tv_nsec, __posix_sem_syscall_return);
0a7de745 1236 }
b0d623f7 1237 } else {
0a7de745 1238 if (uap->mutex_sem == 0) {
b0d623f7 1239 kern_result = semaphore_wait_trap_internal(uap->cond_sem, __posix_sem_syscall_return);
0a7de745 1240 } else {
b0d623f7 1241 kern_result = semaphore_wait_signal_trap_internal(uap->cond_sem, uap->mutex_sem, __posix_sem_syscall_return);
0a7de745
A
1242 }
1243 }
1244
1245 if (kern_result == KERN_SUCCESS && !truncated_timeout) {
1246 return 0;
1247 } else if (kern_result == KERN_SUCCESS && truncated_timeout) {
1248 return EINTR; /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1249 } else if (kern_result == KERN_ABORTED) {
1250 return EINTR;
1251 } else if (kern_result == KERN_OPERATION_TIMED_OUT) {
1252 return ETIMEDOUT;
1253 } else {
1254 return EINVAL;
b0d623f7 1255 }
91447636
A
1256}
1257
b0d623f7 1258
0a7de745 1259int
2d21ac55 1260__pthread_kill(__unused proc_t p, struct __pthread_kill_args *uap,
0a7de745 1261 __unused int32_t *retval)
91447636
A
1262{
1263 thread_t target_act;
1264 int error = 0;
1265 int signum = uap->sig;
1266 struct uthread *uth;
1267
cb323159
A
1268 target_act = (thread_t)port_name_to_thread(uap->thread_port,
1269 PORT_TO_THREAD_NONE);
91447636 1270
0a7de745
A
1271 if (target_act == THREAD_NULL) {
1272 return ESRCH;
1273 }
9bccf70c
A
1274 if ((u_int)signum >= NSIG) {
1275 error = EINVAL;
1276 goto out;
1277 }
1278
1279 uth = (struct uthread *)get_bsdthread_info(target_act);
55e303ae 1280
91447636 1281 if (uth->uu_flag & UT_NO_SIGMASK) {
9bccf70c
A
1282 error = ESRCH;
1283 goto out;
1284 }
1285
cb323159
A
1286 if ((thread_get_tag(target_act) & THREAD_TAG_WORKQUEUE) && !uth->uu_workq_pthread_kill_allowed) {
1287 error = ENOTSUP;
1288 goto out;
1289 }
1290
0a7de745 1291 if (signum) {
9bccf70c 1292 psignal_uthread(target_act, signum);
0a7de745 1293 }
9bccf70c 1294out:
91447636 1295 thread_deallocate(target_act);
0a7de745 1296 return error;
9bccf70c
A
1297}
1298
1299
0a7de745 1300int
2d21ac55 1301__pthread_sigmask(__unused proc_t p, struct __pthread_sigmask_args *uap,
0a7de745 1302 __unused int32_t *retval)
9bccf70c 1303{
91447636
A
1304 user_addr_t set = uap->set;
1305 user_addr_t oset = uap->oset;
1306 sigset_t nset;
9bccf70c
A
1307 int error = 0;
1308 struct uthread *ut;
1309 sigset_t oldset;
1310
91447636 1311 ut = (struct uthread *)get_bsdthread_info(current_thread());
9bccf70c
A
1312 oldset = ut->uu_sigmask;
1313
91447636 1314 if (set == USER_ADDR_NULL) {
9bccf70c
A
1315 /* need only old mask */
1316 goto out;
1317 }
1318
91447636 1319 error = copyin(set, &nset, sizeof(sigset_t));
0a7de745 1320 if (error) {
9bccf70c 1321 goto out;
0a7de745 1322 }
9bccf70c
A
1323
1324 switch (uap->how) {
1325 case SIG_BLOCK:
1326 ut->uu_sigmask |= (nset & ~sigcantmask);
1327 break;
1328
1329 case SIG_UNBLOCK:
1330 ut->uu_sigmask &= ~(nset);
91447636 1331 signal_setast(current_thread());
9bccf70c
A
1332 break;
1333
1334 case SIG_SETMASK:
1335 ut->uu_sigmask = (nset & ~sigcantmask);
91447636 1336 signal_setast(current_thread());
9bccf70c 1337 break;
0a7de745 1338
9bccf70c
A
1339 default:
1340 error = EINVAL;
9bccf70c
A
1341 }
1342out:
0a7de745 1343 if (!error && oset != USER_ADDR_NULL) {
91447636 1344 copyout(&oldset, oset, sizeof(sigset_t));
0a7de745 1345 }
9bccf70c 1346
0a7de745 1347 return error;
9bccf70c
A
1348}
1349
2d21ac55
A
1350/*
1351 * Returns: 0 Success
1352 * EINVAL
1353 * copyin:EFAULT
1354 * copyout:EFAULT
1355 */
0a7de745 1356int
b0d623f7 1357__sigwait(proc_t p, struct __sigwait_args *uap, int32_t *retval)
2d21ac55
A
1358{
1359 __pthread_testcancel(1);
0a7de745 1360 return __sigwait_nocancel(p, (struct __sigwait_nocancel_args *)uap, retval);
2d21ac55 1361}
9bccf70c 1362
0a7de745 1363int
b0d623f7 1364__sigwait_nocancel(proc_t p, struct __sigwait_nocancel_args *uap, __unused int32_t *retval)
9bccf70c 1365{
9bccf70c
A
1366 struct uthread *ut;
1367 struct uthread *uth;
9bccf70c
A
1368 int error = 0;
1369 sigset_t mask;
1370 sigset_t siglist;
0a7de745 1371 sigset_t sigw = 0;
9bccf70c
A
1372 int signum;
1373
91447636 1374 ut = (struct uthread *)get_bsdthread_info(current_thread());
9bccf70c 1375
0a7de745
A
1376 if (uap->set == USER_ADDR_NULL) {
1377 return EINVAL;
1378 }
9bccf70c 1379
91447636 1380 error = copyin(uap->set, &mask, sizeof(sigset_t));
0a7de745
A
1381 if (error) {
1382 return error;
1383 }
9bccf70c
A
1384
1385 siglist = (mask & ~sigcantmask);
1386
0a7de745
A
1387 if (siglist == 0) {
1388 return EINVAL;
1389 }
9bccf70c 1390
2d21ac55
A
1391 proc_lock(p);
1392 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
1393 proc_unlock(p);
0a7de745 1394 return EINVAL;
9bccf70c 1395 } else {
2d21ac55 1396 proc_signalstart(p, 1);
9bccf70c 1397 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
0a7de745 1398 if ((sigw = uth->uu_siglist & siglist)) {
9bccf70c
A
1399 break;
1400 }
1401 }
2d21ac55 1402 proc_signalend(p, 1);
9bccf70c 1403 }
2d21ac55 1404
9bccf70c
A
1405 if (sigw) {
1406 /* The signal was pending on a thread */
1407 goto sigwait1;
1408 }
1409 /*
1410 * When returning from sigwait, we want
1411 * the old mask to be restored after the
1412 * signal handler has finished. Thus, we
1413 * save it here and mark the sigacts structure
1414 * to indicate this.
1415 */
0a7de745 1416 uth = ut; /* wait for it to be delivered to us */
9bccf70c 1417 ut->uu_oldmask = ut->uu_sigmask;
91447636 1418 ut->uu_flag |= UT_SAS_OLDMASK;
2d21ac55
A
1419 if (siglist == (sigset_t)0) {
1420 proc_unlock(p);
0a7de745 1421 return EINVAL;
2d21ac55 1422 }
9bccf70c 1423 /* SIGKILL and SIGSTOP are not maskable as well */
0a7de745
A
1424 ut->uu_sigmask = ~(siglist | sigcantmask);
1425 ut->uu_sigwait = siglist;
2d21ac55 1426
9bccf70c 1427 /* No Continuations for now */
0a7de745 1428 error = msleep((caddr_t)&ut->uu_sigwait, &p->p_mlock, PPAUSE | PCATCH, "pause", 0);
9bccf70c 1429
0a7de745 1430 if (error == ERESTART) {
9bccf70c 1431 error = 0;
0a7de745 1432 }
9bccf70c
A
1433
1434 sigw = (ut->uu_sigwait & siglist);
1435 ut->uu_sigmask = ut->uu_oldmask;
1436 ut->uu_oldmask = 0;
91447636 1437 ut->uu_flag &= ~UT_SAS_OLDMASK;
9bccf70c
A
1438sigwait1:
1439 ut->uu_sigwait = 0;
1440 if (!error) {
1441 signum = ffs((unsigned int)sigw);
0a7de745 1442 if (!signum) {
9bccf70c 1443 panic("sigwait with no signal wakeup");
0a7de745 1444 }
2d21ac55
A
1445 /* Clear the pending signal in the thread it was delivered */
1446 uth->uu_siglist &= ~(sigmask(signum));
b0d623f7
A
1447
1448#if CONFIG_DTRACE
1449 DTRACE_PROC2(signal__clear, int, signum, siginfo_t *, &(ut->t_dtrace_siginfo));
1450#endif
1451
2d21ac55 1452 proc_unlock(p);
0a7de745
A
1453 if (uap->sig != USER_ADDR_NULL) {
1454 error = copyout(&signum, uap->sig, sizeof(int));
1455 }
1456 } else {
2d21ac55 1457 proc_unlock(p);
0a7de745 1458 }
9bccf70c 1459
0a7de745 1460 return error;
9bccf70c
A
1461}
1462
1c79356b 1463int
b0d623f7 1464sigaltstack(__unused proc_t p, struct sigaltstack_args *uap, __unused int32_t *retval)
1c79356b 1465{
b0d623f7
A
1466 struct kern_sigaltstack ss;
1467 struct kern_sigaltstack *pstk;
1c79356b 1468 int error;
2d21ac55
A
1469 struct uthread *uth;
1470 int onstack;
1c79356b 1471
0c530ab8 1472 uth = (struct uthread *)get_bsdthread_info(current_thread());
0c530ab8 1473
2d21ac55 1474 pstk = &uth->uu_sigstk;
0a7de745 1475 if ((uth->uu_flag & UT_ALTSTACK) == 0) {
2d21ac55 1476 uth->uu_sigstk.ss_flags |= SA_DISABLE;
0a7de745 1477 }
2d21ac55 1478 onstack = pstk->ss_flags & SA_ONSTACK;
91447636
A
1479 if (uap->oss) {
1480 if (IS_64BIT_PROCESS(p)) {
5ba3f43e 1481 struct user64_sigaltstack ss64 = {};
0a7de745 1482 sigaltstack_kern_to_user64(pstk, &ss64);
b0d623f7 1483 error = copyout(&ss64, uap->oss, sizeof(ss64));
91447636 1484 } else {
5ba3f43e 1485 struct user32_sigaltstack ss32 = {};
0a7de745 1486 sigaltstack_kern_to_user32(pstk, &ss32);
b0d623f7 1487 error = copyout(&ss32, uap->oss, sizeof(ss32));
91447636 1488 }
0a7de745
A
1489 if (error) {
1490 return error;
1491 }
1492 }
1493 if (uap->nss == USER_ADDR_NULL) {
1494 return 0;
91447636 1495 }
91447636 1496 if (IS_64BIT_PROCESS(p)) {
b0d623f7
A
1497 struct user64_sigaltstack ss64;
1498 error = copyin(uap->nss, &ss64, sizeof(ss64));
1499 sigaltstack_user64_to_kern(&ss64, &ss);
91447636 1500 } else {
b0d623f7
A
1501 struct user32_sigaltstack ss32;
1502 error = copyin(uap->nss, &ss32, sizeof(ss32));
1503 sigaltstack_user32_to_kern(&ss32, &ss);
91447636 1504 }
0a7de745
A
1505 if (error) {
1506 return error;
1507 }
1508 if ((ss.ss_flags & ~SA_DISABLE) != 0) {
1509 return EINVAL;
9bccf70c
A
1510 }
1511
1c79356b 1512 if (ss.ss_flags & SA_DISABLE) {
2d21ac55 1513 /* if we are here we are not in the signal handler ;so no need to check */
0a7de745
A
1514 if (uth->uu_sigstk.ss_flags & SA_ONSTACK) {
1515 return EINVAL;
1516 }
2d21ac55
A
1517 uth->uu_flag &= ~UT_ALTSTACK;
1518 uth->uu_sigstk.ss_flags = ss.ss_flags;
0a7de745
A
1519 return 0;
1520 }
1521 if (onstack) {
1522 return EPERM;
1c79356b 1523 }
55e303ae
A
1524/* The older stacksize was 8K, enforce that one so no compat problems */
1525#define OLDMINSIGSTKSZ 8*1024
0a7de745
A
1526 if (ss.ss_size < OLDMINSIGSTKSZ) {
1527 return ENOMEM;
1528 }
2d21ac55 1529 uth->uu_flag |= UT_ALTSTACK;
0a7de745
A
1530 uth->uu_sigstk = ss;
1531 return 0;
1c79356b
A
1532}
1533
1c79356b 1534int
b0d623f7 1535kill(proc_t cp, struct kill_args *uap, __unused int32_t *retval)
1c79356b 1536{
2d21ac55 1537 proc_t p;
91447636 1538 kauth_cred_t uc = kauth_cred_get();
0a7de745 1539 int posix = uap->posix; /* !0 if posix behaviour desired */
91447636 1540
a39ff7e2
A
1541 AUDIT_ARG(pid, uap->pid);
1542 AUDIT_ARG(signum, uap->signum);
1c79356b 1543
0a7de745
A
1544 if ((u_int)uap->signum >= NSIG) {
1545 return EINVAL;
1546 }
1c79356b
A
1547 if (uap->pid > 0) {
1548 /* kill single process */
2d21ac55 1549 if ((p = proc_find(uap->pid)) == NULL) {
55e303ae
A
1550 if ((p = pzfind(uap->pid)) != NULL) {
1551 /*
a39ff7e2
A
1552 * POSIX 1003.1-2001 requires returning success when killing a
1553 * zombie; see Rationale for kill(2).
55e303ae 1554 */
0a7de745 1555 return 0;
55e303ae 1556 }
0a7de745 1557 return ESRCH;
55e303ae 1558 }
e5568f75 1559 AUDIT_ARG(process, p);
a39ff7e2 1560 if (!cansignal(cp, uc, p, uap->signum)) {
2d21ac55 1561 proc_rele(p);
0a7de745 1562 return EPERM;
ff6e181a 1563 }
0a7de745 1564 if (uap->signum) {
1c79356b 1565 psignal(p, uap->signum);
0a7de745 1566 }
2d21ac55 1567 proc_rele(p);
0a7de745 1568 return 0;
1c79356b
A
1569 }
1570 switch (uap->pid) {
a39ff7e2 1571 case -1: /* broadcast signal */
0a7de745 1572 return killpg1(cp, uap->signum, 0, 1, posix);
a39ff7e2 1573 case 0: /* signal own process group */
0a7de745 1574 return killpg1(cp, uap->signum, 0, 0, posix);
a39ff7e2 1575 default: /* negative explicit process group */
0a7de745 1576 return killpg1(cp, uap->signum, -(uap->pid), 0, posix);
1c79356b
A
1577 }
1578 /* NOTREACHED */
1579}
1580
39037602
A
1581os_reason_t
1582build_userspace_exit_reason(uint32_t reason_namespace, uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
0a7de745 1583 user_addr_t reason_string, uint64_t reason_flags)
39037602
A
1584{
1585 os_reason_t exit_reason = OS_REASON_NULL;
1586
1587 int error = 0;
1588 int num_items_to_copy = 0;
1589 uint32_t user_data_to_copy = 0;
1590 char *reason_user_desc = NULL;
1591 size_t reason_user_desc_len = 0;
1592
1593 exit_reason = os_reason_create(reason_namespace, reason_code);
1594 if (exit_reason == OS_REASON_NULL) {
1595 printf("build_userspace_exit_reason: failed to allocate exit reason\n");
1596 return exit_reason;
1597 }
1598
1599 exit_reason->osr_flags |= OS_REASON_FLAG_FROM_USERSPACE;
1600
1601 /*
1602 * Only apply flags that are allowed to be passed from userspace.
1603 */
1604 exit_reason->osr_flags |= (reason_flags & OS_REASON_FLAG_MASK_ALLOWED_FROM_USER);
1605 if ((reason_flags & OS_REASON_FLAG_MASK_ALLOWED_FROM_USER) != reason_flags) {
1606 printf("build_userspace_exit_reason: illegal flags passed from userspace (some masked off) 0x%llx, ns: %u, code 0x%llx\n",
0a7de745 1607 reason_flags, reason_namespace, reason_code);
39037602
A
1608 }
1609
1610 if (!(exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASH_REPORT)) {
1611 exit_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1612 }
1613
1614 if (payload != USER_ADDR_NULL) {
1615 if (payload_size == 0) {
1616 printf("build_userspace_exit_reason: exit reason with namespace %u, nonzero payload but zero length\n",
0a7de745 1617 reason_namespace);
39037602
A
1618 exit_reason->osr_flags |= OS_REASON_FLAG_BAD_PARAMS;
1619 payload = USER_ADDR_NULL;
1620 } else {
1621 num_items_to_copy++;
1622
1623 if (payload_size > EXIT_REASON_PAYLOAD_MAX_LEN) {
1624 exit_reason->osr_flags |= OS_REASON_FLAG_PAYLOAD_TRUNCATED;
1625 payload_size = EXIT_REASON_PAYLOAD_MAX_LEN;
1626 }
1627
1628 user_data_to_copy += payload_size;
1629 }
1630 }
1631
1632 if (reason_string != USER_ADDR_NULL) {
f427ee49
A
1633 reason_user_desc = kheap_alloc(KHEAP_TEMP,
1634 EXIT_REASON_USER_DESC_MAX_LEN, Z_WAITOK);
39037602
A
1635
1636 if (reason_user_desc != NULL) {
1637 error = copyinstr(reason_string, (void *) reason_user_desc,
0a7de745 1638 EXIT_REASON_USER_DESC_MAX_LEN, &reason_user_desc_len);
39037602
A
1639
1640 if (error == 0) {
1641 num_items_to_copy++;
1642 user_data_to_copy += reason_user_desc_len;
1643 } else if (error == ENAMETOOLONG) {
1644 num_items_to_copy++;
1645 reason_user_desc[EXIT_REASON_USER_DESC_MAX_LEN - 1] = '\0';
1646 user_data_to_copy += reason_user_desc_len;
1647 } else {
1648 exit_reason->osr_flags |= OS_REASON_FLAG_FAILED_DATA_COPYIN;
f427ee49
A
1649 kheap_free(KHEAP_TEMP, reason_user_desc,
1650 EXIT_REASON_USER_DESC_MAX_LEN);
39037602
A
1651 reason_user_desc = NULL;
1652 reason_user_desc_len = 0;
1653 }
1654 }
1655 }
1656
1657 if (num_items_to_copy != 0) {
1658 uint32_t reason_buffer_size_estimate = 0;
1659 mach_vm_address_t data_addr = 0;
1660
1661 reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(num_items_to_copy, user_data_to_copy);
1662
1663 error = os_reason_alloc_buffer(exit_reason, reason_buffer_size_estimate);
1664 if (error != 0) {
1665 printf("build_userspace_exit_reason: failed to allocate signal reason buffer\n");
1666 goto out_failed_copyin;
1667 }
1668
1669 if (reason_user_desc != NULL && reason_user_desc_len != 0) {
1670 if (KERN_SUCCESS == kcdata_get_memory_addr(&exit_reason->osr_kcd_descriptor,
0a7de745 1671 EXIT_REASON_USER_DESC,
f427ee49 1672 (uint32_t)reason_user_desc_len,
0a7de745 1673 &data_addr)) {
39037602 1674 kcdata_memcpy(&exit_reason->osr_kcd_descriptor, (mach_vm_address_t) data_addr,
f427ee49 1675 reason_user_desc, (uint32_t)reason_user_desc_len);
39037602
A
1676 } else {
1677 printf("build_userspace_exit_reason: failed to allocate space for reason string\n");
1678 goto out_failed_copyin;
1679 }
1680 }
1681
1682 if (payload != USER_ADDR_NULL) {
1683 if (KERN_SUCCESS ==
0a7de745
A
1684 kcdata_get_memory_addr(&exit_reason->osr_kcd_descriptor,
1685 EXIT_REASON_USER_PAYLOAD,
1686 payload_size,
1687 &data_addr)) {
39037602
A
1688 error = copyin(payload, (void *) data_addr, payload_size);
1689 if (error) {
1690 printf("build_userspace_exit_reason: failed to copy in payload data with error %d\n", error);
1691 goto out_failed_copyin;
1692 }
1693 } else {
1694 printf("build_userspace_exit_reason: failed to allocate space for payload data\n");
1695 goto out_failed_copyin;
1696 }
1697 }
1698 }
1699
1700 if (reason_user_desc != NULL) {
f427ee49 1701 kheap_free(KHEAP_TEMP, reason_user_desc, EXIT_REASON_USER_DESC_MAX_LEN);
39037602
A
1702 reason_user_desc = NULL;
1703 reason_user_desc_len = 0;
1704 }
1705
1706 return exit_reason;
1707
1708out_failed_copyin:
1709
1710 if (reason_user_desc != NULL) {
f427ee49 1711 kheap_free(KHEAP_TEMP, reason_user_desc, EXIT_REASON_USER_DESC_MAX_LEN);
39037602
A
1712 reason_user_desc = NULL;
1713 reason_user_desc_len = 0;
1714 }
1715
1716 exit_reason->osr_flags |= OS_REASON_FLAG_FAILED_DATA_COPYIN;
1717 os_reason_alloc_buffer(exit_reason, 0);
1718 return exit_reason;
1719}
1720
1721static int
1722terminate_with_payload_internal(struct proc *cur_proc, int target_pid, uint32_t reason_namespace,
0a7de745
A
1723 uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
1724 user_addr_t reason_string, uint64_t reason_flags)
39037602
A
1725{
1726 proc_t target_proc = PROC_NULL;
1727 kauth_cred_t cur_cred = kauth_cred_get();
39037602
A
1728
1729 os_reason_t signal_reason = OS_REASON_NULL;
1730
1731 AUDIT_ARG(pid, target_pid);
d190cdc3 1732 if ((target_pid <= 0)) {
39037602
A
1733 return EINVAL;
1734 }
1735
1736 target_proc = proc_find(target_pid);
1737 if (target_proc == PROC_NULL) {
1738 return ESRCH;
1739 }
1740
1741 AUDIT_ARG(process, target_proc);
1742
a39ff7e2 1743 if (!cansignal(cur_proc, cur_cred, target_proc, SIGKILL)) {
39037602
A
1744 proc_rele(target_proc);
1745 return EPERM;
1746 }
1747
d9a64523
A
1748 if (target_pid != cur_proc->p_pid) {
1749 /*
1750 * FLAG_ABORT should only be set on terminate_with_reason(getpid()) that
1751 * was a fallback from an unsuccessful abort_with_reason(). In that case
1752 * caller's pid matches the target one. Otherwise remove the flag.
1753 */
1754 reason_flags &= ~((typeof(reason_flags))OS_REASON_FLAG_ABORT);
1755 }
1756
39037602 1757 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
0a7de745
A
1758 target_proc->p_pid, reason_namespace,
1759 reason_code, 0, 0);
39037602
A
1760
1761 signal_reason = build_userspace_exit_reason(reason_namespace, reason_code, payload, payload_size,
0a7de745 1762 reason_string, (reason_flags | OS_REASON_FLAG_NO_CRASHED_TID));
39037602 1763
d190cdc3
A
1764 if (target_pid == cur_proc->p_pid) {
1765 /*
1766 * psignal_thread_with_reason() will pend a SIGKILL on the specified thread or
1767 * return if the thread and/or task are already terminating. Either way, the
1768 * current thread won't return to userspace.
1769 */
1770 psignal_thread_with_reason(target_proc, current_thread(), SIGKILL, signal_reason);
1771 } else {
1772 psignal_with_reason(target_proc, SIGKILL, signal_reason);
1773 }
1774
39037602
A
1775 proc_rele(target_proc);
1776
1777 return 0;
1778}
1779
1780int
1781terminate_with_payload(struct proc *cur_proc, struct terminate_with_payload_args *args,
0a7de745 1782 __unused int32_t *retval)
39037602
A
1783{
1784 return terminate_with_payload_internal(cur_proc, args->pid, args->reason_namespace, args->reason_code, args->payload,
0a7de745 1785 args->payload_size, args->reason_string, args->reason_flags);
39037602
A
1786}
1787
2d21ac55 1788static int
a39ff7e2 1789killpg1_allfilt(proc_t p, void * arg)
2d21ac55
A
1790{
1791 struct killpg1_filtargs * kfargp = (struct killpg1_filtargs *)arg;
2d21ac55 1792
a39ff7e2
A
1793 /*
1794 * Don't signal initproc, a system process, or the current process if POSIX
1795 * isn't specified.
1796 */
0a7de745
A
1797 return p->p_pid > 1 && !(p->p_flag & P_SYSTEM) &&
1798 (kfargp->posix ? true : p != kfargp->curproc);
2d21ac55
A
1799}
1800
2d21ac55
A
1801static int
1802killpg1_pgrpfilt(proc_t p, __unused void * arg)
1803{
a39ff7e2 1804 /* XXX shouldn't this allow signalling zombies? */
0a7de745 1805 return p->p_pid > 1 && !(p->p_flag & P_SYSTEM) && p->p_stat != SZOMB;
2d21ac55
A
1806}
1807
2d21ac55 1808static int
a39ff7e2 1809killpg1_callback(proc_t p, void *arg)
2d21ac55 1810{
a39ff7e2
A
1811 struct killpg1_iterargs *kargp = (struct killpg1_iterargs *)arg;
1812 int signum = kargp->signum;
2d21ac55 1813
a39ff7e2
A
1814 if ((p->p_listflag & P_LIST_EXITED) == P_LIST_EXITED) {
1815 /*
1816 * Count zombies as found for the purposes of signalling, since POSIX
1817 * 1003.1-2001 sees signalling zombies as successful. If killpg(2) or
1818 * kill(2) with pid -1 only finds zombies that can be signalled, it
1819 * shouldn't return ESRCH. See the Rationale for kill(2).
1820 *
1821 * Don't call into MAC -- it's not expecting signal checks for exited
1822 * processes.
1823 */
1824 if (cansignal_nomac(kargp->curproc, kargp->uc, p, signum)) {
1825 kargp->nfound++;
b0d623f7 1826 }
a39ff7e2
A
1827 } else if (cansignal(kargp->curproc, kargp->uc, p, signum)) {
1828 kargp->nfound++;
2d21ac55 1829
a39ff7e2 1830 if (signum != 0) {
b0d623f7 1831 psignal(p, signum);
a39ff7e2 1832 }
2d21ac55 1833 }
2d21ac55 1834
a39ff7e2 1835 return PROC_RETURNED;
2d21ac55 1836}
1c79356b
A
1837
1838/*
1839 * Common code for kill process group/broadcast kill.
1c79356b
A
1840 */
1841int
a39ff7e2 1842killpg1(proc_t curproc, int signum, int pgid, int all, int posix)
1c79356b 1843{
2d21ac55 1844 kauth_cred_t uc;
1c79356b 1845 struct pgrp *pgrp;
2d21ac55 1846 int error = 0;
2d21ac55 1847
a39ff7e2
A
1848 uc = kauth_cred_proc_ref(curproc);
1849 struct killpg1_iterargs karg = {
1850 .curproc = curproc, .uc = uc, .nfound = 0, .signum = signum
1851 };
2d21ac55 1852
a39ff7e2
A
1853 if (all) {
1854 /*
1855 * Broadcast to all processes that the user can signal (pid was -1).
1856 */
1857 struct killpg1_filtargs kfarg = {
1858 .posix = posix, .curproc = curproc
1859 };
1860 proc_iterate(PROC_ALLPROCLIST | PROC_ZOMBPROCLIST, killpg1_callback,
0a7de745 1861 &karg, killpg1_allfilt, &kfarg);
1c79356b 1862 } else {
2d21ac55 1863 if (pgid == 0) {
a39ff7e2
A
1864 /*
1865 * Send to current the current process' process group.
1c79356b 1866 */
a39ff7e2 1867 pgrp = proc_pgrp(curproc);
0a7de745 1868 } else {
1c79356b 1869 pgrp = pgfind(pgid);
2d21ac55
A
1870 if (pgrp == NULL) {
1871 error = ESRCH;
1872 goto out;
1873 }
1c79356b 1874 }
2d21ac55 1875
2d21ac55 1876 /* PGRP_DROPREF drops the pgrp refernce */
39037602 1877 pgrp_iterate(pgrp, PGRP_DROPREF, killpg1_callback, &karg,
0a7de745 1878 killpg1_pgrpfilt, NULL);
1c79356b 1879 }
a39ff7e2 1880 error = (karg.nfound > 0 ? 0 : (posix ? EPERM : ESRCH));
2d21ac55
A
1881out:
1882 kauth_cred_unref(&uc);
0a7de745 1883 return error;
1c79356b
A
1884}
1885
1886/*
1887 * Send a signal to a process group.
1888 */
1889void
2d21ac55 1890gsignal(int pgid, int signum)
1c79356b
A
1891{
1892 struct pgrp *pgrp;
1893
2d21ac55 1894 if (pgid && (pgrp = pgfind(pgid))) {
1c79356b 1895 pgsignal(pgrp, signum, 0);
2d21ac55
A
1896 pg_rele(pgrp);
1897 }
1c79356b
A
1898}
1899
1900/*
2d21ac55 1901 * Send a signal to a process group. If checkctty is 1,
1c79356b
A
1902 * limit to members which have a controlling terminal.
1903 */
2d21ac55
A
1904
1905static int
1906pgsignal_filt(proc_t p, void * arg)
1c79356b 1907{
b0d623f7 1908 int checkctty = *(int*)arg;
1c79356b 1909
0a7de745
A
1910 if ((checkctty == 0) || p->p_flag & P_CONTROLT) {
1911 return 1;
1912 } else {
1913 return 0;
1914 }
1c79356b
A
1915}
1916
2d21ac55
A
1917
1918static int
1919pgsignal_callback(proc_t p, void * arg)
9bccf70c 1920{
0a7de745 1921 int signum = *(int*)arg;
9bccf70c 1922
2d21ac55 1923 psignal(p, signum);
0a7de745 1924 return PROC_RETURNED;
2d21ac55
A
1925}
1926
1927
1928void
1929pgsignal(struct pgrp *pgrp, int signum, int checkctty)
1930{
1931 if (pgrp != PGRP_NULL) {
39037602 1932 pgrp_iterate(pgrp, 0, pgsignal_callback, &signum, pgsignal_filt, &checkctty);
2d21ac55 1933 }
9bccf70c
A
1934}
1935
2d21ac55
A
1936
1937void
1938tty_pgsignal(struct tty *tp, int signum, int checkctty)
1939{
1940 struct pgrp * pg;
1941
1942 pg = tty_pgrp(tp);
1943 if (pg != PGRP_NULL) {
39037602 1944 pgrp_iterate(pg, 0, pgsignal_callback, &signum, pgsignal_filt, &checkctty);
2d21ac55
A
1945 pg_rele(pg);
1946 }
1947}
1c79356b
A
1948/*
1949 * Send a signal caused by a trap to a specific thread.
1950 */
1951void
39037602 1952threadsignal(thread_t sig_actthread, int signum, mach_exception_code_t code, boolean_t set_exitreason)
1c79356b 1953{
2d21ac55
A
1954 struct uthread *uth;
1955 struct task * sig_task;
1956 proc_t p;
1c79356b
A
1957 int mask;
1958
0a7de745 1959 if ((u_int)signum >= NSIG || signum == 0) {
1c79356b 1960 return;
0a7de745 1961 }
1c79356b
A
1962
1963 mask = sigmask(signum);
0a7de745 1964 if ((mask & threadmask) == 0) {
1c79356b 1965 return;
0a7de745 1966 }
1c79356b 1967 sig_task = get_threadtask(sig_actthread);
2d21ac55 1968 p = (proc_t)(get_bsdtask_info(sig_task));
1c79356b 1969
0b4e3aa0 1970 uth = get_bsdthread_info(sig_actthread);
0a7de745 1971 if (uth->uu_flag & UT_VFORK) {
0b4e3aa0 1972 p = uth->uu_proc;
0a7de745 1973 }
0b4e3aa0 1974
2d21ac55
A
1975 proc_lock(p);
1976 if (!(p->p_lflag & P_LTRACED) && (p->p_sigignore & mask)) {
1977 proc_unlock(p);
1c79356b 1978 return;
2d21ac55 1979 }
1c79356b 1980
9bccf70c 1981 uth->uu_siglist |= mask;
1c79356b 1982 uth->uu_code = code;
39037602
A
1983
1984 /* Attempt to establish whether the signal will be fatal (mirrors logic in psignal_internal()) */
1985 if (set_exitreason && ((p->p_lflag & P_LTRACED) || (!(uth->uu_sigwait & mask)
0a7de745
A
1986 && !(uth->uu_sigmask & mask) && !(p->p_sigcatch & mask))) &&
1987 !(mask & stopsigmask) && !(mask & contsigmask)) {
39037602
A
1988 if (uth->uu_exit_reason == OS_REASON_NULL) {
1989 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
0a7de745 1990 p->p_pid, OS_REASON_SIGNAL, signum, 0, 0);
39037602
A
1991
1992 os_reason_t signal_reason = build_signal_reason(signum, "exc handler");
1993
1994 set_thread_exit_reason(sig_actthread, signal_reason, TRUE);
1995
1996 /* We dropped/consumed the reference in set_thread_exit_reason() */
1997 signal_reason = OS_REASON_NULL;
1998 }
1999 }
2000
2d21ac55
A
2001 proc_unlock(p);
2002
1c79356b 2003 /* mark on process as well */
1c79356b
A
2004 signal_setast(sig_actthread);
2005}
2006
f427ee49
A
2007/* Called with proc locked */
2008static void
2009set_thread_extra_flags(struct uthread *uth, os_reason_t reason)
2010{
2011 extern int vm_shared_region_reslide_restrict;
2012 assert(uth != NULL);
2013 /*
2014 * Check whether the userland fault address falls within the shared
2015 * region and notify userland if so. This allows launchd to apply
2016 * special policies around this fault type.
2017 */
2018 if (reason->osr_namespace == OS_REASON_SIGNAL &&
2019 reason->osr_code == SIGSEGV) {
2020 mach_vm_address_t fault_address = uth->uu_subcode;
2021
2022#if defined(__arm64__)
2023 /* taken from osfmk/arm/misc_protos.h */
2024 #define TBI_MASK 0xff00000000000000
2025 #define tbi_clear(addr) ((addr) & ~(TBI_MASK))
2026 fault_address = tbi_clear(fault_address);
2027#endif /* __arm64__ */
2028
2029 if (fault_address >= SHARED_REGION_BASE &&
2030 fault_address <= SHARED_REGION_BASE + SHARED_REGION_SIZE) {
2031 /*
2032 * Always report whether the fault happened within the shared cache
2033 * region, but only stale the slide if the resliding is extended
2034 * to all processes or if the process faulting is a platform one.
2035 */
2036 reason->osr_flags |= OS_REASON_FLAG_SHAREDREGION_FAULT;
2037
2038#if __has_feature(ptrauth_calls)
2039 if (!vm_shared_region_reslide_restrict || csproc_get_platform_binary(current_proc())) {
2040 vm_shared_region_reslide_stale();
2041 }
2042#endif /* __has_feature(ptrauth_calls) */
2043 }
2044 }
2045}
2046
39037602
A
2047void
2048set_thread_exit_reason(void *th, void *reason, boolean_t proc_locked)
2049{
2050 struct uthread *targ_uth = get_bsdthread_info(th);
2051 struct task *targ_task = NULL;
2052 proc_t targ_proc = NULL;
2053
2054 os_reason_t exit_reason = (os_reason_t)reason;
2055
0a7de745 2056 if (exit_reason == OS_REASON_NULL) {
39037602 2057 return;
0a7de745 2058 }
39037602
A
2059
2060 if (!proc_locked) {
2061 targ_task = get_threadtask(th);
2062 targ_proc = (proc_t)(get_bsdtask_info(targ_task));
2063
2064 proc_lock(targ_proc);
2065 }
2066
f427ee49
A
2067 set_thread_extra_flags(targ_uth, exit_reason);
2068
39037602
A
2069 if (targ_uth->uu_exit_reason == OS_REASON_NULL) {
2070 targ_uth->uu_exit_reason = exit_reason;
2071 } else {
2072 /* The caller expects that we drop a reference on the exit reason */
2073 os_reason_free(exit_reason);
2074 }
2075
2076 if (!proc_locked) {
2077 assert(targ_proc != NULL);
2078 proc_unlock(targ_proc);
2079 }
2080}
2081
4bd07ac2
A
2082/*
2083 * get_signalthread
2084 *
2085 * Picks an appropriate thread from a process to target with a signal.
2086 *
2087 * Called with proc locked.
2088 * Returns thread with BSD ast set.
2089 *
2090 * We attempt to deliver a proc-wide signal to the first thread in the task.
2091 * This allows single threaded applications which use signals to
2092 * be able to be linked with multithreaded libraries.
2093 */
2d21ac55
A
2094static kern_return_t
2095get_signalthread(proc_t p, int signum, thread_t * thr)
1c79356b 2096{
2d21ac55
A
2097 struct uthread *uth;
2098 sigset_t mask = sigmask(signum);
2099 thread_t sig_thread;
2100 struct task * sig_task = p->task;
2101 kern_return_t kret;
cb323159 2102 bool skip_wqthreads = true;
3e170ce0 2103
2d21ac55
A
2104 *thr = THREAD_NULL;
2105
2106 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
0a7de745 2107 sig_thread = p->p_vforkact;
2d21ac55 2108 kret = check_actforsig(sig_task, sig_thread, 1);
0a7de745 2109 if (kret == KERN_SUCCESS) {
2d21ac55 2110 *thr = sig_thread;
0a7de745
A
2111 return KERN_SUCCESS;
2112 } else {
2113 return KERN_FAILURE;
2114 }
3e170ce0 2115 }
2d21ac55 2116
cb323159 2117again:
2d21ac55 2118 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
0a7de745
A
2119 if (((uth->uu_flag & UT_NO_SIGMASK) == 0) &&
2120 (((uth->uu_sigmask & mask) == 0) || (uth->uu_sigwait & mask))) {
cb323159
A
2121 thread_t th = uth->uu_context.vc_thread;
2122 if (skip_wqthreads && (thread_get_tag(th) & THREAD_TAG_WORKQUEUE)) {
2123 /* Workqueue threads may be parked in the kernel unable to
2124 * deliver signals for an extended period of time, so skip them
2125 * in favor of pthreads in a first pass. (rdar://50054475). */
2126 } else if (check_actforsig(p->task, th, 1) == KERN_SUCCESS) {
2127 *thr = th;
0a7de745 2128 return KERN_SUCCESS;
2d21ac55
A
2129 }
2130 }
2131 }
cb323159
A
2132 if (skip_wqthreads) {
2133 skip_wqthreads = false;
2134 goto again;
2135 }
2d21ac55 2136 if (get_signalact(p->task, thr, 1) == KERN_SUCCESS) {
0a7de745 2137 return KERN_SUCCESS;
2d21ac55
A
2138 }
2139
0a7de745 2140 return KERN_FAILURE;
1c79356b
A
2141}
2142
39037602
A
2143static os_reason_t
2144build_signal_reason(int signum, const char *procname)
2145{
2146 os_reason_t signal_reason = OS_REASON_NULL;
2147 proc_t sender_proc = current_proc();
2148 uint32_t reason_buffer_size_estimate = 0, proc_name_length = 0;
2149 const char *default_sender_procname = "unknown";
2150 mach_vm_address_t data_addr;
2151 int ret;
2152
2153 signal_reason = os_reason_create(OS_REASON_SIGNAL, signum);
2154 if (signal_reason == OS_REASON_NULL) {
2155 printf("build_signal_reason: unable to allocate signal reason structure.\n");
2156 return signal_reason;
2157 }
2158
2159 reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(2, sizeof(sender_proc->p_name) +
0a7de745 2160 sizeof(sender_proc->p_pid));
39037602 2161
d190cdc3 2162 ret = os_reason_alloc_buffer_noblock(signal_reason, reason_buffer_size_estimate);
39037602
A
2163 if (ret != 0) {
2164 printf("build_signal_reason: unable to allocate signal reason buffer.\n");
2165 return signal_reason;
2166 }
2167
2168 if (KERN_SUCCESS == kcdata_get_memory_addr(&signal_reason->osr_kcd_descriptor, KCDATA_TYPE_PID,
0a7de745 2169 sizeof(sender_proc->p_pid), &data_addr)) {
39037602 2170 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, &sender_proc->p_pid,
0a7de745 2171 sizeof(sender_proc->p_pid));
39037602
A
2172 } else {
2173 printf("build_signal_reason: exceeded space in signal reason buf, unable to log PID\n");
2174 }
2175
2176 proc_name_length = sizeof(sender_proc->p_name);
2177 if (KERN_SUCCESS == kcdata_get_memory_addr(&signal_reason->osr_kcd_descriptor, KCDATA_TYPE_PROCNAME,
0a7de745 2178 proc_name_length, &data_addr)) {
39037602
A
2179 if (procname) {
2180 char truncated_procname[proc_name_length];
2181 strncpy((char *) &truncated_procname, procname, proc_name_length);
2182 truncated_procname[proc_name_length - 1] = '\0';
2183
2184 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, truncated_procname,
f427ee49 2185 (uint32_t)strlen((char *) &truncated_procname));
39037602
A
2186 } else if (*sender_proc->p_name) {
2187 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, &sender_proc->p_name,
0a7de745 2188 sizeof(sender_proc->p_name));
39037602
A
2189 } else {
2190 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, &default_sender_procname,
f427ee49 2191 (uint32_t)strlen(default_sender_procname) + 1);
39037602
A
2192 }
2193 } else {
2194 printf("build_signal_reason: exceeded space in signal reason buf, unable to log procname\n");
2195 }
2196
2197 return signal_reason;
2198}
2199
2d21ac55
A
2200/*
2201 * Send the signal to the process. If the signal has an action, the action
2202 * is usually performed by the target process rather than the caller; we add
2203 * the signal to the set of pending signals for the process.
2204 *
39037602
A
2205 * Always drops a reference on a signal_reason if one is provided, whether via
2206 * passing it to a thread or deallocating directly.
2207 *
2d21ac55
A
2208 * Exceptions:
2209 * o When a stop signal is sent to a sleeping process that takes the
2210 * default action, the process is stopped without awakening it.
2211 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2212 * regardless of the signal action (eg, blocked or ignored).
2213 *
2214 * Other ignored signals are discarded immediately.
2215 */
2216static void
39037602 2217psignal_internal(proc_t p, task_t task, thread_t thread, int flavor, int signum, os_reason_t signal_reason)
0b4e3aa0 2218{
2d21ac55 2219 int prop;
fe8ab488 2220 user_addr_t action = USER_ADDR_NULL;
0a7de745
A
2221 proc_t sig_proc;
2222 thread_t sig_thread;
2223 task_t sig_task;
2224 int mask;
2225 struct uthread *uth;
2226 kern_return_t kret;
2227 uid_t r_uid;
2228 proc_t pp;
2229 kauth_cred_t my_cred;
2230 char *launchd_exit_reason_desc = NULL;
2231 boolean_t update_thread_policy = FALSE;
2232
2233 if ((u_int)signum >= NSIG || signum == 0) {
4bd07ac2 2234 panic("psignal: bad signal number %d", signum);
0a7de745 2235 }
4bd07ac2 2236
0b4e3aa0
A
2237 mask = sigmask(signum);
2238 prop = sigprop[signum];
2239
2240#if SIGNAL_DEBUG
0a7de745
A
2241 if (rdebug_proc && (p != PROC_NULL) && (p == rdebug_proc)) {
2242 ram_printf(3);
2243 }
0b4e3aa0
A
2244#endif /* SIGNAL_DEBUG */
2245
3e170ce0 2246 /* catch unexpected initproc kills early for easier debuggging */
39037602
A
2247 if (signum == SIGKILL && p == initproc) {
2248 if (signal_reason == NULL) {
2249 panic_plain("unexpected SIGKILL of %s %s (no reason provided)",
0a7de745
A
2250 (p->p_name[0] != '\0' ? p->p_name : "initproc"),
2251 ((p->p_csflags & CS_KILLED) ? "(CS_KILLED)" : ""));
39037602
A
2252 } else {
2253 launchd_exit_reason_desc = launchd_exit_reason_get_string_desc(signal_reason);
2254 panic_plain("unexpected SIGKILL of %s %s with reason -- namespace %d code 0x%llx description %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
0a7de745
A
2255 (p->p_name[0] != '\0' ? p->p_name : "initproc"),
2256 ((p->p_csflags & CS_KILLED) ? "(CS_KILLED)" : ""),
39037602
A
2257 signal_reason->osr_namespace, signal_reason->osr_code,
2258 launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
2259 }
2260 }
3e170ce0 2261
1c79356b
A
2262 /*
2263 * We will need the task pointer later. Grab it now to
2264 * check for a zombie process. Also don't send signals
2265 * to kernel internal tasks.
2266 */
2d21ac55
A
2267 if (flavor & PSIG_VFORK) {
2268 sig_task = task;
2269 sig_thread = thread;
6d2010ae 2270 sig_proc = p;
2d21ac55
A
2271 } else if (flavor & PSIG_THREAD) {
2272 sig_task = get_threadtask(thread);
2273 sig_thread = thread;
2274 sig_proc = (proc_t)get_bsdtask_info(sig_task);
3e170ce0 2275 } else if (flavor & PSIG_TRY_THREAD) {
4bd07ac2 2276 assert((thread == current_thread()) && (p == current_proc()));
3e170ce0
A
2277 sig_task = p->task;
2278 sig_thread = thread;
2279 sig_proc = p;
2d21ac55
A
2280 } else {
2281 sig_task = p->task;
4bd07ac2 2282 sig_thread = THREAD_NULL;
6d2010ae 2283 sig_proc = p;
2d21ac55 2284 }
6d2010ae 2285
39037602
A
2286 if ((sig_task == TASK_NULL) || is_kerneltask(sig_task)) {
2287 os_reason_free(signal_reason);
1c79356b 2288 return;
39037602 2289 }
1c79356b
A
2290
2291 /*
2292 * do not send signals to the process that has the thread
2293 * doing a reboot(). Not doing so will mark that thread aborted
6d2010ae
A
2294 * and can cause IO failures wich will cause data loss. There's
2295 * also no need to send a signal to a process that is in the middle
2296 * of being torn down.
1c79356b 2297 */
4bd07ac2
A
2298 if (ISSET(sig_proc->p_flag, P_REBOOT) || ISSET(sig_proc->p_lflag, P_LEXIT)) {
2299 DTRACE_PROC3(signal__discard, thread_t, sig_thread, proc_t, sig_proc, int, signum);
39037602 2300 os_reason_free(signal_reason);
1c79356b 2301 return;
4bd07ac2 2302 }
1c79356b 2303
0a7de745 2304 if ((flavor & (PSIG_VFORK | PSIG_THREAD)) == 0) {
2d21ac55
A
2305 proc_knote(sig_proc, NOTE_SIGNAL | signum);
2306 }
2307
0a7de745 2308 if ((flavor & PSIG_LOCKED) == 0) {
2d21ac55 2309 proc_signalstart(sig_proc, 0);
0a7de745 2310 }
9bccf70c 2311
4bd07ac2 2312 /* Don't send signals to a process that has ignored them. */
2d21ac55
A
2313 if (((flavor & PSIG_VFORK) == 0) && ((sig_proc->p_lflag & P_LTRACED) == 0) && (sig_proc->p_sigignore & mask)) {
2314 DTRACE_PROC3(signal__discard, thread_t, sig_thread, proc_t, sig_proc, int, signum);
4bd07ac2 2315 goto sigout_unlocked;
2d21ac55 2316 }
9bccf70c 2317
4bd07ac2
A
2318 /*
2319 * The proc_lock prevents the targeted thread from being deallocated
2320 * or handling the signal until we're done signaling it.
2321 *
2322 * Once the proc_lock is dropped, we have no guarantee the thread or uthread exists anymore.
2323 *
2324 * XXX: What if the thread goes inactive after the thread passes bsd ast point?
2325 */
2326 proc_lock(sig_proc);
2327
2d21ac55
A
2328 if (flavor & PSIG_VFORK) {
2329 action = SIG_DFL;
2330 act_set_astbsd(sig_thread);
2331 kret = KERN_SUCCESS;
3e170ce0
A
2332 } else if (flavor & PSIG_TRY_THREAD) {
2333 uth = get_bsdthread_info(sig_thread);
2334 if (((uth->uu_flag & UT_NO_SIGMASK) == 0) &&
0a7de745
A
2335 (((uth->uu_sigmask & mask) == 0) || (uth->uu_sigwait & mask)) &&
2336 ((kret = check_actforsig(sig_proc->task, sig_thread, 1)) == KERN_SUCCESS)) {
3e170ce0
A
2337 /* deliver to specified thread */
2338 } else {
2339 /* deliver to any willing thread */
2340 kret = get_signalthread(sig_proc, signum, &sig_thread);
2341 }
2d21ac55
A
2342 } else if (flavor & PSIG_THREAD) {
2343 /* If successful return with ast set */
2344 kret = check_actforsig(sig_task, sig_thread, 1);
2345 } else {
2346 /* If successful return with ast set */
2347 kret = get_signalthread(sig_proc, signum, &sig_thread);
2348 }
4bd07ac2 2349
2d21ac55 2350 if (kret != KERN_SUCCESS) {
4bd07ac2
A
2351 DTRACE_PROC3(signal__discard, thread_t, sig_thread, proc_t, sig_proc, int, signum);
2352 proc_unlock(sig_proc);
2353 goto sigout_unlocked;
1c79356b
A
2354 }
2355
2d21ac55 2356 uth = get_bsdthread_info(sig_thread);
1c79356b
A
2357
2358 /*
2359 * If proc is traced, always give parent a chance.
2360 */
2d21ac55
A
2361
2362 if ((flavor & PSIG_VFORK) == 0) {
0a7de745 2363 if (sig_proc->p_lflag & P_LTRACED) {
1c79356b 2364 action = SIG_DFL;
0a7de745 2365 } else {
2d21ac55
A
2366 /*
2367 * If the signal is being ignored,
2368 * then we forget about it immediately.
2369 * (Note: we don't set SIGCONT in p_sigignore,
2370 * and if it is set to SIG_IGN,
2371 * action will be SIG_DFL here.)
2372 */
0a7de745 2373 if (sig_proc->p_sigignore & mask) {
4bd07ac2 2374 goto sigout_locked;
0a7de745 2375 }
4bd07ac2 2376
0a7de745 2377 if (uth->uu_sigwait & mask) {
2d21ac55 2378 action = KERN_SIG_WAIT;
0a7de745 2379 } else if (uth->uu_sigmask & mask) {
2d21ac55 2380 action = KERN_SIG_HOLD;
0a7de745 2381 } else if (sig_proc->p_sigcatch & mask) {
2d21ac55 2382 action = KERN_SIG_CATCH;
0a7de745 2383 } else {
2d21ac55 2384 action = SIG_DFL;
0a7de745 2385 }
2d21ac55 2386 }
1c79356b
A
2387 }
2388
4bd07ac2 2389 /* TODO: p_nice isn't hooked up to the scheduler... */
2d21ac55 2390 if (sig_proc->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
0a7de745
A
2391 (sig_proc->p_lflag & P_LTRACED) == 0) {
2392 sig_proc->p_nice = NZERO;
2393 }
2d21ac55 2394
0a7de745 2395 if (prop & SA_CONT) {
9bccf70c 2396 uth->uu_siglist &= ~stopsigmask;
0a7de745 2397 }
1c79356b
A
2398
2399 if (prop & SA_STOP) {
2d21ac55 2400 struct pgrp *pg;
1c79356b
A
2401 /*
2402 * If sending a tty stop signal to a member of an orphaned
2403 * process group, discard the signal here if the action
2404 * is default; don't stop the process below if sleeping,
2405 * and don't clear any pending SIGCONT.
2406 */
2d21ac55
A
2407 pg = proc_pgrp(sig_proc);
2408 if (prop & SA_TTYSTOP && pg->pg_jobc == 0 &&
0a7de745 2409 action == SIG_DFL) {
2d21ac55 2410 pg_rele(pg);
4bd07ac2 2411 goto sigout_locked;
2d21ac55
A
2412 }
2413 pg_rele(pg);
9bccf70c 2414 uth->uu_siglist &= ~contsigmask;
1c79356b 2415 }
2d21ac55 2416
9bccf70c 2417 uth->uu_siglist |= mask;
1c79356b
A
2418
2419 /*
2420 * Defer further processing for signals which are held,
2421 * except that stopped processes must be continued by SIGCONT.
2422 */
2d21ac55 2423 /* vfork will not go thru as action is SIG_DFL */
0a7de745 2424 if ((action == KERN_SIG_HOLD) && ((prop & SA_CONT) == 0 || sig_proc->p_stat != SSTOP)) {
4bd07ac2 2425 goto sigout_locked;
0a7de745 2426 }
4bd07ac2 2427
9bccf70c
A
2428 /*
2429 * SIGKILL priority twiddling moved here from above because
2430 * it needs sig_thread. Could merge it into large switch
2431 * below if we didn't care about priority for tracing
2432 * as SIGKILL's action is always SIG_DFL.
4bd07ac2
A
2433 *
2434 * TODO: p_nice isn't hooked up to the scheduler...
9bccf70c 2435 */
2d21ac55
A
2436 if ((signum == SIGKILL) && (sig_proc->p_nice > NZERO)) {
2437 sig_proc->p_nice = NZERO;
9bccf70c
A
2438 }
2439
2440 /*
2441 * Process is traced - wake it up (if not already
2442 * stopped) so that it can discover the signal in
2443 * issig() and stop for the parent.
2444 */
2d21ac55 2445 if (sig_proc->p_lflag & P_LTRACED) {
0a7de745 2446 if (sig_proc->p_stat != SSTOP) {
2d21ac55 2447 goto runlocked;
0a7de745 2448 } else {
4bd07ac2 2449 goto sigout_locked;
0a7de745 2450 }
9bccf70c 2451 }
4bd07ac2 2452
0a7de745 2453 if ((flavor & PSIG_VFORK) != 0) {
2d21ac55 2454 goto runlocked;
0a7de745 2455 }
9bccf70c 2456
91447636 2457 if (action == KERN_SIG_WAIT) {
b0d623f7
A
2458#if CONFIG_DTRACE
2459 /*
2460 * DTrace proc signal-clear returns a siginfo_t. Collect the needed info.
2461 */
2462 r_uid = kauth_getruid(); /* per thread credential; protected by our thread context */
2463
2464 bzero((caddr_t)&(uth->t_dtrace_siginfo), sizeof(uth->t_dtrace_siginfo));
2465
2466 uth->t_dtrace_siginfo.si_signo = signum;
2467 uth->t_dtrace_siginfo.si_pid = current_proc()->p_pid;
2468 uth->t_dtrace_siginfo.si_status = W_EXITCODE(signum, 0);
2469 uth->t_dtrace_siginfo.si_uid = r_uid;
2470 uth->t_dtrace_siginfo.si_code = 0;
2471#endif
9bccf70c
A
2472 uth->uu_sigwait = mask;
2473 uth->uu_siglist &= ~mask;
9bccf70c
A
2474 wakeup(&uth->uu_sigwait);
2475 /* if it is SIGCONT resume whole process */
91447636 2476 if (prop & SA_CONT) {
b0d623f7 2477 OSBitOrAtomic(P_CONTINUED, &sig_proc->p_flag);
2d21ac55 2478 sig_proc->p_contproc = current_proc()->p_pid;
39236c6e 2479 (void) task_resume_internal(sig_task);
91447636 2480 }
4bd07ac2 2481 goto sigout_locked;
9bccf70c
A
2482 }
2483
2484 if (action != SIG_DFL) {
2485 /*
2486 * User wants to catch the signal.
2487 * Wake up the thread, but don't un-suspend it
2488 * (except for SIGCONT).
2489 */
55e303ae 2490 if (prop & SA_CONT) {
b0d623f7 2491 OSBitOrAtomic(P_CONTINUED, &sig_proc->p_flag);
39236c6e 2492 (void) task_resume_internal(sig_task);
2d21ac55 2493 sig_proc->p_stat = SRUN;
0a7de745 2494 } else if (sig_proc->p_stat == SSTOP) {
4bd07ac2 2495 goto sigout_locked;
9bccf70c 2496 }
9bccf70c 2497 /*
2d21ac55
A
2498 * Fill out siginfo structure information to pass to the
2499 * signalled process/thread sigaction handler, when it
2500 * wakes up. si_code is 0 because this is an ordinary
2501 * signal, not a SIGCHLD, and so si_status is the signal
2502 * number itself, instead of the child process exit status.
2503 * We shift this left because it will be shifted right before
2504 * it is passed to user space. kind of ugly to use W_EXITCODE
2505 * this way, but it beats defining a new macro.
2506 *
2507 * Note: Avoid the SIGCHLD recursion case!
9bccf70c 2508 */
2d21ac55 2509 if (signum != SIGCHLD) {
2d21ac55 2510 r_uid = kauth_getruid();
2d21ac55
A
2511
2512 sig_proc->si_pid = current_proc()->p_pid;
2513 sig_proc->si_status = W_EXITCODE(signum, 0);
2514 sig_proc->si_uid = r_uid;
2515 sig_proc->si_code = 0;
91447636 2516 }
1c79356b 2517
2d21ac55 2518 goto runlocked;
1c79356b
A
2519 } else {
2520 /* Default action - varies */
2521 if (mask & stopsigmask) {
39037602 2522 assert(signal_reason == NULL);
1c79356b
A
2523 /*
2524 * These are the signals which by default
2525 * stop a process.
2526 *
2527 * Don't clog system with children of init
2528 * stopped from the keyboard.
2529 */
2d21ac55 2530 if (!(prop & SA_STOP) && sig_proc->p_pptr == initproc) {
9bccf70c 2531 uth->uu_siglist &= ~mask;
2d21ac55 2532 proc_unlock(sig_proc);
4bd07ac2
A
2533 /* siglock still locked, proc_lock not locked */
2534 psignal_locked(sig_proc, SIGKILL);
2535 goto sigout_unlocked;
1c79356b 2536 }
4bd07ac2 2537
1c79356b 2538 /*
9bccf70c
A
2539 * Stop the task
2540 * if task hasn't already been stopped by
2541 * a signal.
1c79356b 2542 */
2d21ac55
A
2543 uth->uu_siglist &= ~mask;
2544 if (sig_proc->p_stat != SSTOP) {
2545 sig_proc->p_xstat = signum;
2546 sig_proc->p_stat = SSTOP;
b0d623f7 2547 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &sig_proc->p_flag);
2d21ac55
A
2548 sig_proc->p_lflag &= ~P_LWAITED;
2549 proc_unlock(sig_proc);
2550
2551 pp = proc_parentholdref(sig_proc);
2552 stop(sig_proc, pp);
0a7de745 2553 if ((pp != PROC_NULL) && ((pp->p_flag & P_NOCLDSTOP) == 0)) {
2d21ac55 2554 my_cred = kauth_cred_proc_ref(sig_proc);
6d2010ae 2555 r_uid = kauth_cred_getruid(my_cred);
2d21ac55
A
2556 kauth_cred_unref(&my_cred);
2557
2558 proc_lock(sig_proc);
2559 pp->si_pid = sig_proc->p_pid;
2560 /*
2561 * POSIX: sigaction for a stopped child
2562 * when sent to the parent must set the
2563 * child's signal number into si_status.
2564 */
0a7de745 2565 if (signum != SIGSTOP) {
2d21ac55 2566 pp->si_status = WEXITSTATUS(sig_proc->p_xstat);
0a7de745 2567 } else {
2d21ac55 2568 pp->si_status = W_EXITCODE(signum, signum);
0a7de745 2569 }
9bccf70c 2570 pp->si_code = CLD_STOPPED;
2d21ac55
A
2571 pp->si_uid = r_uid;
2572 proc_unlock(sig_proc);
2573
9bccf70c 2574 psignal(pp, SIGCHLD);
1c79356b 2575 }
4bd07ac2 2576 if (pp != PROC_NULL) {
2d21ac55 2577 proc_parentdropref(pp, 0);
4bd07ac2
A
2578 }
2579
2580 goto sigout_unlocked;
2581 }
2582
2583 goto sigout_locked;
1c79356b
A
2584 }
2585
2d21ac55
A
2586 DTRACE_PROC3(signal__send, thread_t, sig_thread, proc_t, p, int, signum);
2587
1c79356b 2588 switch (signum) {
0a7de745
A
2589 /*
2590 * Signals ignored by default have been dealt
2591 * with already, since their bits are on in
2592 * p_sigignore.
2593 */
1c79356b
A
2594
2595 case SIGKILL:
2596 /*
2597 * Kill signal always sets process running and
2598 * unsuspends it.
2599 */
2600 /*
2601 * Process will be running after 'run'
2602 */
2d21ac55 2603 sig_proc->p_stat = SRUN;
6d2010ae
A
2604 /*
2605 * In scenarios where suspend/resume are racing
2606 * the signal we are missing AST_BSD by the time
2607 * we get here, set again to avoid races. This
2608 * was the scenario with spindump enabled shutdowns.
2609 * We would need to cover this approp down the line.
2610 */
2611 act_set_astbsd(sig_thread);
39037602
A
2612 kret = thread_abort(sig_thread);
2613 update_thread_policy = (kret == KERN_SUCCESS);
2614
2615 if (uth->uu_exit_reason == OS_REASON_NULL) {
2616 if (signal_reason == OS_REASON_NULL) {
2617 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
0a7de745 2618 sig_proc->p_pid, OS_REASON_SIGNAL, signum, 0, 0);
39037602
A
2619
2620 signal_reason = build_signal_reason(signum, NULL);
2621 }
2622
2623 os_reason_ref(signal_reason);
2624 set_thread_exit_reason(sig_thread, signal_reason, TRUE);
2625 }
1c79356b 2626
4bd07ac2 2627 goto sigout_locked;
1c79356b
A
2628
2629 case SIGCONT:
2630 /*
2631 * Let the process run. If it's sleeping on an
2632 * event, it remains so.
2633 */
39037602 2634 assert(signal_reason == NULL);
b0d623f7 2635 OSBitOrAtomic(P_CONTINUED, &sig_proc->p_flag);
2d21ac55 2636 sig_proc->p_contproc = sig_proc->p_pid;
5ba3f43e 2637 sig_proc->p_xstat = signum;
2d21ac55 2638
39236c6e 2639 (void) task_resume_internal(sig_task);
4bd07ac2 2640
2d21ac55
A
2641 /*
2642 * When processing a SIGCONT, we need to check
2643 * to see if there are signals pending that
2644 * were not delivered because we had been
2645 * previously stopped. If that's the case,
2646 * we need to thread_abort_safely() to trigger
2647 * interruption of the current system call to
2648 * cause their handlers to fire. If it's only
2649 * the SIGCONT, then don't wake up.
2650 */
0a7de745 2651 if (((flavor & (PSIG_VFORK | PSIG_THREAD)) == 0) && (((uth->uu_siglist & ~uth->uu_sigmask) & ~sig_proc->p_sigignore) & ~mask)) {
2d21ac55
A
2652 uth->uu_siglist &= ~mask;
2653 sig_proc->p_stat = SRUN;
2654 goto runlocked;
1c79356b 2655 }
2d21ac55 2656
9bccf70c 2657 uth->uu_siglist &= ~mask;
2d21ac55 2658 sig_proc->p_stat = SRUN;
4bd07ac2 2659 goto sigout_locked;
1c79356b
A
2660
2661 default:
2d21ac55
A
2662 /*
2663 * A signal which has a default action of killing
2664 * the process, and for which there is no handler,
2665 * needs to act like SIGKILL
2666 */
0a7de745 2667 if (((flavor & (PSIG_VFORK | PSIG_THREAD)) == 0) && (action == SIG_DFL) && (prop & SA_KILL)) {
2d21ac55 2668 sig_proc->p_stat = SRUN;
39037602
A
2669 kret = thread_abort(sig_thread);
2670 update_thread_policy = (kret == KERN_SUCCESS);
2671
2672 if (uth->uu_exit_reason == OS_REASON_NULL) {
2673 if (signal_reason == OS_REASON_NULL) {
2674 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
0a7de745 2675 sig_proc->p_pid, OS_REASON_SIGNAL, signum, 0, 0);
39037602
A
2676
2677 signal_reason = build_signal_reason(signum, NULL);
2678 }
2679
2680 os_reason_ref(signal_reason);
2681 set_thread_exit_reason(sig_thread, signal_reason, TRUE);
2682 }
2683
4bd07ac2 2684 goto sigout_locked;
2d21ac55
A
2685 }
2686
1c79356b
A
2687 /*
2688 * All other signals wake up the process, but don't
2689 * resume it.
2690 */
2d21ac55 2691 if (sig_proc->p_stat == SSTOP) {
4bd07ac2 2692 goto sigout_locked;
2d21ac55
A
2693 }
2694 goto runlocked;
1c79356b
A
2695 }
2696 }
2697 /*NOTREACHED*/
2d21ac55
A
2698
2699runlocked:
1c79356b
A
2700 /*
2701 * If we're being traced (possibly because someone attached us
2702 * while we were stopped), check for a signal from the debugger.
2703 */
2d21ac55 2704 if (sig_proc->p_stat == SSTOP) {
0a7de745 2705 if ((sig_proc->p_lflag & P_LTRACED) != 0 && sig_proc->p_xstat != 0) {
4bd07ac2 2706 uth->uu_siglist |= sigmask(sig_proc->p_xstat);
0a7de745 2707 }
4bd07ac2 2708
2d21ac55
A
2709 if ((flavor & PSIG_VFORK) != 0) {
2710 sig_proc->p_stat = SRUN;
9bccf70c
A
2711 }
2712 } else {
2713 /*
0a7de745
A
2714 * setrunnable(p) in BSD and
2715 * Wake up the thread if it is interruptible.
2716 */
2d21ac55 2717 sig_proc->p_stat = SRUN;
0a7de745 2718 if ((flavor & PSIG_VFORK) == 0) {
2d21ac55 2719 thread_abort_safely(sig_thread);
0a7de745 2720 }
2d21ac55 2721 }
4bd07ac2
A
2722
2723sigout_locked:
39037602
A
2724 if (update_thread_policy) {
2725 /*
2726 * Update the thread policy to heading to terminate, increase priority if
2727 * necessary. This needs to be done before we drop the proc lock because the
2728 * thread can take the fatal signal once it's dropped.
2729 */
2730 proc_set_thread_policy(sig_thread, TASK_POLICY_ATTRIBUTE, TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
2731 }
2732
4bd07ac2
A
2733 proc_unlock(sig_proc);
2734
2735sigout_unlocked:
39037602 2736 os_reason_free(signal_reason);
0a7de745 2737 if ((flavor & PSIG_LOCKED) == 0) {
2d21ac55 2738 proc_signalend(sig_proc, 0);
1c79356b 2739 }
1c79356b
A
2740}
2741
2d21ac55
A
2742void
2743psignal(proc_t p, int signum)
1c79356b 2744{
39037602
A
2745 psignal_internal(p, NULL, NULL, 0, signum, NULL);
2746}
2747
2748void
2749psignal_with_reason(proc_t p, int signum, struct os_reason *signal_reason)
2750{
2751 psignal_internal(p, NULL, NULL, 0, signum, signal_reason);
2d21ac55 2752}
1c79356b 2753
cb323159 2754void
f427ee49 2755psignal_sigkill_with_reason(struct proc *p, struct os_reason *signal_reason)
cb323159
A
2756{
2757 psignal_internal(p, NULL, NULL, 0, SIGKILL, signal_reason);
2758}
2759
2d21ac55
A
2760void
2761psignal_locked(proc_t p, int signum)
2762{
39037602
A
2763 psignal_internal(p, NULL, NULL, PSIG_LOCKED, signum, NULL);
2764}
2765
2766void
2767psignal_vfork_with_reason(proc_t p, task_t new_task, thread_t thread, int signum, struct os_reason *signal_reason)
2768{
2769 psignal_internal(p, new_task, thread, PSIG_VFORK, signum, signal_reason);
1c79356b
A
2770}
2771
39037602 2772
2d21ac55
A
2773void
2774psignal_vfork(proc_t p, task_t new_task, thread_t thread, int signum)
1c79356b 2775{
39037602 2776 psignal_internal(p, new_task, thread, PSIG_VFORK, signum, NULL);
2d21ac55 2777}
1c79356b 2778
39037602 2779void
2d21ac55
A
2780psignal_uthread(thread_t thread, int signum)
2781{
39037602 2782 psignal_internal(PROC_NULL, TASK_NULL, thread, PSIG_THREAD, signum, NULL);
1c79356b
A
2783}
2784
3e170ce0 2785/* same as psignal(), but prefer delivery to 'thread' if possible */
39037602 2786void
3e170ce0
A
2787psignal_try_thread(proc_t p, thread_t thread, int signum)
2788{
39037602
A
2789 psignal_internal(p, NULL, thread, PSIG_TRY_THREAD, signum, NULL);
2790}
2791
2792void
2793psignal_try_thread_with_reason(proc_t p, thread_t thread, int signum, struct os_reason *signal_reason)
2794{
2795 psignal_internal(p, TASK_NULL, thread, PSIG_TRY_THREAD, signum, signal_reason);
3e170ce0 2796}
2d21ac55 2797
d190cdc3
A
2798void
2799psignal_thread_with_reason(proc_t p, thread_t thread, int signum, struct os_reason *signal_reason)
2800{
2801 psignal_internal(p, TASK_NULL, thread, PSIG_THREAD, signum, signal_reason);
2802}
2803
1c79356b
A
2804/*
2805 * If the current process has received a signal (should be caught or cause
2806 * termination, should interrupt current syscall), return the signal number.
2807 * Stop signals with default action are processed immediately, then cleared;
2808 * they aren't returned. This is checked after each entry to the system for
2809 * a syscall or trap (though this can usually be done without calling issignal
2810 * by checking the pending signal masks in the CURSIG macro.) The normal call
2811 * sequence is
2812 *
2813 * while (signum = CURSIG(curproc))
2814 * postsig(signum);
2815 */
2816int
316670eb 2817issignal_locked(proc_t p)
1c79356b 2818{
2d21ac55 2819 int signum, mask, prop, sigbits;
91447636 2820 thread_t cur_act;
1c79356b 2821 struct uthread * ut;
2d21ac55
A
2822 proc_t pp;
2823 kauth_cred_t my_cred;
2824 int retval = 0;
2825 uid_t r_uid;
1c79356b 2826
91447636 2827 cur_act = current_thread();
1c79356b 2828
9bccf70c 2829#if SIGNAL_DEBUG
0a7de745 2830 if (rdebug_proc && (p == rdebug_proc)) {
39037602
A
2831 ram_printf(3);
2832 }
9bccf70c 2833#endif /* SIGNAL_DEBUG */
1c79356b 2834
1c79356b
A
2835 /*
2836 * Try to grab the signal lock.
2837 */
2838 if (sig_try_locked(p) <= 0) {
39037602 2839 return 0;
1c79356b
A
2840 }
2841
2d21ac55
A
2842 proc_signalstart(p, 1);
2843
1c79356b 2844 ut = get_bsdthread_info(cur_act);
39037602
A
2845 for (;;) {
2846 sigbits = ut->uu_siglist & ~ut->uu_sigmask;
1c79356b 2847
0a7de745 2848 if (p->p_lflag & P_LPPWAIT) {
1c79356b 2849 sigbits &= ~stopsigmask;
0a7de745
A
2850 }
2851 if (sigbits == 0) { /* no signal to send */
2d21ac55
A
2852 retval = 0;
2853 goto out;
1c79356b 2854 }
2d21ac55 2855
f427ee49 2856 signum = ffs((unsigned int)sigbits);
1c79356b
A
2857 mask = sigmask(signum);
2858 prop = sigprop[signum];
2859
1c79356b
A
2860 /*
2861 * We should see pending but ignored signals
2d21ac55 2862 * only if P_LTRACED was on when they were posted.
1c79356b 2863 */
2d21ac55 2864 if (mask & p->p_sigignore && (p->p_lflag & P_LTRACED) == 0) {
39037602 2865 ut->uu_siglist &= ~mask;
1c79356b
A
2866 continue;
2867 }
39037602 2868
0a7de745 2869 if (p->p_lflag & P_LTRACED && (p->p_lflag & P_LPPWAIT) == 0) {
1c79356b 2870 /*
39037602
A
2871 * If traced, deliver the signal to the debugger, and wait to be
2872 * released.
1c79356b 2873 */
0a7de745 2874 task_t task;
1c79356b 2875 p->p_xstat = signum;
39037602 2876
2d21ac55 2877 if (p->p_lflag & P_LSIGEXC) {
9bccf70c
A
2878 p->sigwait = TRUE;
2879 p->sigwait_thread = cur_act;
2880 p->p_stat = SSTOP;
b0d623f7 2881 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2d21ac55 2882 p->p_lflag &= ~P_LWAITED;
39037602 2883 ut->uu_siglist &= ~mask; /* clear the current signal from the pending list */
2d21ac55
A
2884 proc_signalend(p, 1);
2885 proc_unlock(p);
9bccf70c 2886 do_bsdexception(EXC_SOFTWARE, EXC_SOFT_SIGNAL, signum);
2d21ac55
A
2887 proc_lock(p);
2888 proc_signalstart(p, 1);
9bccf70c 2889 } else {
2d21ac55
A
2890 proc_unlock(p);
2891 my_cred = kauth_cred_proc_ref(p);
6d2010ae 2892 r_uid = kauth_cred_getruid(my_cred);
2d21ac55
A
2893 kauth_cred_unref(&my_cred);
2894
2895 pp = proc_parentholdref(p);
2896 if (pp != PROC_NULL) {
2897 proc_lock(pp);
2898
2899 pp->si_pid = p->p_pid;
5ba3f43e
A
2900 pp->p_xhighbits = p->p_xhighbits;
2901 p->p_xhighbits = 0;
2d21ac55
A
2902 pp->si_status = p->p_xstat;
2903 pp->si_code = CLD_TRAPPED;
2904 pp->si_uid = r_uid;
2905
2906 proc_unlock(pp);
2907 }
2908
9bccf70c 2909 /*
0a7de745
A
2910 * XXX Have to really stop for debuggers;
2911 * XXX stop() doesn't do the right thing.
2912 */
9bccf70c 2913 task = p->task;
39236c6e 2914 task_suspend_internal(task);
2d21ac55
A
2915
2916 proc_lock(p);
9bccf70c
A
2917 p->sigwait = TRUE;
2918 p->sigwait_thread = cur_act;
2919 p->p_stat = SSTOP;
b0d623f7 2920 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2d21ac55 2921 p->p_lflag &= ~P_LWAITED;
39037602 2922 ut->uu_siglist &= ~mask;
9bccf70c 2923
2d21ac55
A
2924 proc_signalend(p, 1);
2925 proc_unlock(p);
2926
2927 if (pp != PROC_NULL) {
2928 psignal(pp, SIGCHLD);
2929 proc_list_lock();
2930 wakeup((caddr_t)pp);
2931 proc_parentdropref(pp, 1);
2932 proc_list_unlock();
2933 }
2934
9bccf70c
A
2935 assert_wait((caddr_t)&p->sigwait, (THREAD_INTERRUPTIBLE));
2936 thread_block(THREAD_CONTINUE_NULL);
2d21ac55
A
2937 proc_lock(p);
2938 proc_signalstart(p, 1);
9bccf70c
A
2939 }
2940
1c79356b
A
2941 p->sigwait = FALSE;
2942 p->sigwait_thread = NULL;
2943 wakeup((caddr_t)&p->sigwait_thread);
2944
39037602 2945 if (signum == SIGKILL || ut->uu_siglist & sigmask(SIGKILL)) {
1c79356b 2946 /*
39037602
A
2947 * Deliver a pending sigkill even if it's not the current signal.
2948 * Necessary for PT_KILL, which should not be delivered to the
2949 * debugger, but we can't differentiate it from any other KILL.
1c79356b 2950 */
39037602
A
2951 signum = SIGKILL;
2952 goto deliver_sig;
1c79356b
A
2953 }
2954
39037602 2955 /* We may have to quit. */
91447636 2956 if (thread_should_abort(current_thread())) {
2d21ac55
A
2957 retval = 0;
2958 goto out;
1c79356b 2959 }
39037602 2960
1c79356b
A
2961 /*
2962 * If parent wants us to take the signal,
2963 * then it will leave it in p->p_xstat;
2964 * otherwise we just look for signals again.
2965 */
2966 signum = p->p_xstat;
0a7de745 2967 if (signum == 0) {
1c79356b 2968 continue;
0a7de745 2969 }
39037602 2970
1c79356b
A
2971 /*
2972 * Put the new signal into p_siglist. If the
2973 * signal is being masked, look for other signals.
2974 */
2975 mask = sigmask(signum);
9bccf70c 2976 ut->uu_siglist |= mask;
0a7de745 2977 if (ut->uu_sigmask & mask) {
1c79356b 2978 continue;
0a7de745 2979 }
1c79356b
A
2980 }
2981
2982 /*
2983 * Decide whether the signal should be returned.
2984 * Return the signal's number, or fall through
2985 * to clear it from the pending mask.
2986 */
2987
2988 switch ((long)p->p_sigacts->ps_sigact[signum]) {
1c79356b 2989 case (long)SIG_DFL:
1c79356b
A
2990 /*
2991 * If there is a pending stop signal to process
2992 * with default action, stop here,
2993 * then clear the signal. However,
2994 * if process is member of an orphaned
2995 * process group, ignore tty stop signals.
2996 */
2997 if (prop & SA_STOP) {
2d21ac55
A
2998 struct pgrp * pg;
2999
3000 proc_unlock(p);
3001 pg = proc_pgrp(p);
3002 if (p->p_lflag & P_LTRACED ||
0a7de745
A
3003 (pg->pg_jobc == 0 &&
3004 prop & SA_TTYSTOP)) {
2d21ac55
A
3005 proc_lock(p);
3006 pg_rele(pg);
39037602 3007 break; /* ignore signal */
2d21ac55
A
3008 }
3009 pg_rele(pg);
9bccf70c 3010 if (p->p_stat != SSTOP) {
2d21ac55 3011 proc_lock(p);
9bccf70c 3012 p->p_xstat = signum;
2d21ac55
A
3013 p->p_stat = SSTOP;
3014 p->p_lflag &= ~P_LWAITED;
3015 proc_unlock(p);
3016
3017 pp = proc_parentholdref(p);
3018 stop(p, pp);
3019 if ((pp != PROC_NULL) && ((pp->p_flag & P_NOCLDSTOP) == 0)) {
3020 my_cred = kauth_cred_proc_ref(p);
6d2010ae 3021 r_uid = kauth_cred_getruid(my_cred);
2d21ac55
A
3022 kauth_cred_unref(&my_cred);
3023
3024 proc_lock(pp);
9bccf70c 3025 pp->si_pid = p->p_pid;
2d21ac55 3026 pp->si_status = WEXITSTATUS(p->p_xstat);
9bccf70c 3027 pp->si_code = CLD_STOPPED;
2d21ac55
A
3028 pp->si_uid = r_uid;
3029 proc_unlock(pp);
3030
9bccf70c
A
3031 psignal(pp, SIGCHLD);
3032 }
0a7de745 3033 if (pp != PROC_NULL) {
2d21ac55 3034 proc_parentdropref(pp, 0);
0a7de745 3035 }
1c79356b 3036 }
2d21ac55 3037 proc_lock(p);
1c79356b
A
3038 break;
3039 } else if (prop & SA_IGNORE) {
3040 /*
3041 * Except for SIGCONT, shouldn't get here.
3042 * Default action is to ignore; drop it.
3043 */
39037602 3044 break; /* ignore signal */
1c79356b 3045 } else {
39037602 3046 goto deliver_sig;
1c79356b 3047 }
2d21ac55 3048
1c79356b
A
3049 case (long)SIG_IGN:
3050 /*
3051 * Masking above should prevent us ever trying
3052 * to take action on an ignored signal other
3053 * than SIGCONT, unless process is traced.
3054 */
3055 if ((prop & SA_CONT) == 0 &&
0a7de745 3056 (p->p_lflag & P_LTRACED) == 0) {
1c79356b 3057 printf("issignal\n");
0a7de745 3058 }
39037602 3059 break; /* ignore signal */
1c79356b
A
3060
3061 default:
39037602
A
3062 /* This signal has an action - deliver it. */
3063 goto deliver_sig;
1c79356b 3064 }
39037602
A
3065
3066 /* If we dropped through, the signal was ignored - remove it from pending list. */
3067 ut->uu_siglist &= ~mask;
39037602
A
3068 } /* for(;;) */
3069
1c79356b 3070 /* NOTREACHED */
39037602
A
3071
3072deliver_sig:
3073 ut->uu_siglist &= ~mask;
3074 retval = signum;
3075
2d21ac55 3076out:
6d2010ae 3077 proc_signalend(p, 1);
39037602 3078 return retval;
1c79356b
A
3079}
3080
3081/* called from _sleep */
3082int
2d21ac55 3083CURSIG(proc_t p)
1c79356b 3084{
2d21ac55 3085 int signum, mask, prop, sigbits;
91447636 3086 thread_t cur_act;
1c79356b
A
3087 struct uthread * ut;
3088 int retnum = 0;
0a7de745 3089
1c79356b 3090
91447636 3091 cur_act = current_thread();
1c79356b
A
3092
3093 ut = get_bsdthread_info(cur_act);
3094
0a7de745
A
3095 if (ut->uu_siglist == 0) {
3096 return 0;
3097 }
9bccf70c 3098
0a7de745
A
3099 if (((ut->uu_siglist & ~ut->uu_sigmask) == 0) && ((p->p_lflag & P_LTRACED) == 0)) {
3100 return 0;
3101 }
9bccf70c
A
3102
3103 sigbits = ut->uu_siglist & ~ut->uu_sigmask;
1c79356b 3104
0a7de745
A
3105 for (;;) {
3106 if (p->p_lflag & P_LPPWAIT) {
1c79356b 3107 sigbits &= ~stopsigmask;
0a7de745
A
3108 }
3109 if (sigbits == 0) { /* no signal to send */
3110 return retnum;
1c79356b
A
3111 }
3112
f427ee49 3113 signum = ffs((unsigned int)sigbits);
1c79356b
A
3114 mask = sigmask(signum);
3115 prop = sigprop[signum];
0a7de745 3116 sigbits &= ~mask; /* take the signal out */
1c79356b
A
3117
3118 /*
3119 * We should see pending but ignored signals
2d21ac55 3120 * only if P_LTRACED was on when they were posted.
1c79356b 3121 */
2d21ac55 3122 if (mask & p->p_sigignore && (p->p_lflag & P_LTRACED) == 0) {
1c79356b
A
3123 continue;
3124 }
6d2010ae 3125
2d21ac55 3126 if (p->p_lflag & P_LTRACED && (p->p_lflag & P_LPPWAIT) == 0) {
0a7de745 3127 return signum;
1c79356b
A
3128 }
3129
3130 /*
3131 * Decide whether the signal should be returned.
3132 * Return the signal's number, or fall through
3133 * to clear it from the pending mask.
3134 */
3135
3136 switch ((long)p->p_sigacts->ps_sigact[signum]) {
1c79356b 3137 case (long)SIG_DFL:
1c79356b
A
3138 /*
3139 * If there is a pending stop signal to process
3140 * with default action, stop here,
3141 * then clear the signal. However,
3142 * if process is member of an orphaned
3143 * process group, ignore tty stop signals.
3144 */
3145 if (prop & SA_STOP) {
2d21ac55
A
3146 struct pgrp *pg;
3147
3148 pg = proc_pgrp(p);
3149
3150 if (p->p_lflag & P_LTRACED ||
0a7de745
A
3151 (pg->pg_jobc == 0 &&
3152 prop & SA_TTYSTOP)) {
2d21ac55 3153 pg_rele(pg);
0a7de745 3154 break; /* == ignore */
2d21ac55
A
3155 }
3156 pg_rele(pg);
1c79356b
A
3157 retnum = signum;
3158 break;
3159 } else if (prop & SA_IGNORE) {
3160 /*
3161 * Except for SIGCONT, shouldn't get here.
3162 * Default action is to ignore; drop it.
3163 */
0a7de745 3164 break; /* == ignore */
1c79356b 3165 } else {
0a7de745 3166 return signum;
1c79356b 3167 }
0a7de745 3168 /*NOTREACHED*/
1c79356b
A
3169
3170 case (long)SIG_IGN:
3171 /*
3172 * Masking above should prevent us ever trying
3173 * to take action on an ignored signal other
3174 * than SIGCONT, unless process is traced.
3175 */
3176 if ((prop & SA_CONT) == 0 &&
0a7de745 3177 (p->p_lflag & P_LTRACED) == 0) {
1c79356b 3178 printf("issignal\n");
0a7de745
A
3179 }
3180 break; /* == ignore */
1c79356b
A
3181
3182 default:
3183 /*
3184 * This signal has an action, let
3185 * postsig() process it.
3186 */
0a7de745 3187 return signum;
1c79356b 3188 }
1c79356b
A
3189 }
3190 /* NOTREACHED */
3191}
3192
3193/*
3194 * Put the argument process into the stopped state and notify the parent
3195 * via wakeup. Signals are handled elsewhere. The process must not be
3196 * on the run queue.
3197 */
2d21ac55
A
3198static void
3199stop(proc_t p, proc_t parent)
1c79356b 3200{
b0d623f7 3201 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2d21ac55
A
3202 if ((parent != PROC_NULL) && (parent->p_stat != SSTOP)) {
3203 proc_list_lock();
3204 wakeup((caddr_t)parent);
3205 proc_list_unlock();
3206 }
39236c6e 3207 (void) task_suspend_internal(p->task);
1c79356b
A
3208}
3209
3210/*
3211 * Take the action for the specified signal
3212 * from the current set of pending signals.
3213 */
3214void
316670eb 3215postsig_locked(int signum)
1c79356b 3216{
2d21ac55 3217 proc_t p = current_proc();
91447636
A
3218 struct sigacts *ps = p->p_sigacts;
3219 user_addr_t catcher;
b0d623f7 3220 uint32_t code;
1c79356b 3221 int mask, returnmask;
9bccf70c 3222 struct uthread * ut;
d9a64523 3223 os_reason_t ut_exit_reason = OS_REASON_NULL;
1c79356b
A
3224
3225#if DIAGNOSTIC
0a7de745 3226 if (signum == 0) {
1c79356b 3227 panic("postsig");
0a7de745 3228 }
1c79356b
A
3229 /*
3230 * This must be called on master cpu
3231 */
0a7de745 3232 if (cpu_number() != master_cpu) {
1c79356b 3233 panic("psig not on master");
0a7de745 3234 }
1c79356b
A
3235#endif
3236
1c79356b
A
3237 /*
3238 * Try to grab the signal lock.
3239 */
3240 if (sig_try_locked(p) <= 0) {
1c79356b
A
3241 return;
3242 }
3243
2d21ac55
A
3244 proc_signalstart(p, 1);
3245
91447636 3246 ut = (struct uthread *)get_bsdthread_info(current_thread());
1c79356b 3247 mask = sigmask(signum);
9bccf70c 3248 ut->uu_siglist &= ~mask;
91447636 3249 catcher = ps->ps_sigact[signum];
91447636 3250 if (catcher == SIG_DFL) {
1c79356b 3251 /*
91447636 3252 * Default catcher, where the default is to kill
1c79356b
A
3253 * the process. (Other cases were ignored above.)
3254 */
2d21ac55 3255 sig_lock_to_exit(p);
d9a64523
A
3256
3257 /*
3258 * exit_with_reason() below will consume a reference to the thread's exit reason, so we take another
3259 * reference so the thread still has one even after we call exit_with_reason(). The thread's reference will
3260 * ultimately be destroyed in uthread_cleanup().
3261 */
3262 ut_exit_reason = ut->uu_exit_reason;
3263 os_reason_ref(ut_exit_reason);
3264
2d21ac55
A
3265 p->p_acflag |= AXSIG;
3266 if (sigprop[signum] & SA_CORE) {
3267 p->p_sigacts->ps_sig = signum;
3268 proc_signalend(p, 1);
3269 proc_unlock(p);
39037602 3270#if CONFIG_COREDUMP
0a7de745 3271 if (coredump(p, 0, 0) == 0) {
2d21ac55 3272 signum |= WCOREFLAG;
0a7de745 3273 }
39037602 3274#endif
0a7de745 3275 } else {
2d21ac55
A
3276 proc_signalend(p, 1);
3277 proc_unlock(p);
3278 }
0a7de745 3279
b0d623f7
A
3280#if CONFIG_DTRACE
3281 bzero((caddr_t)&(ut->t_dtrace_siginfo), sizeof(ut->t_dtrace_siginfo));
3282
3283 ut->t_dtrace_siginfo.si_signo = signum;
3284 ut->t_dtrace_siginfo.si_pid = p->si_pid;
3285 ut->t_dtrace_siginfo.si_uid = p->si_uid;
3286 ut->t_dtrace_siginfo.si_status = WEXITSTATUS(p->si_status);
2d21ac55 3287
316670eb
A
3288 /* Fire DTrace proc:::fault probe when signal is generated by hardware. */
3289 switch (signum) {
3290 case SIGILL: case SIGBUS: case SIGSEGV: case SIGFPE: case SIGTRAP:
3291 DTRACE_PROC2(fault, int, (int)(ut->uu_code), siginfo_t *, &(ut->t_dtrace_siginfo));
3292 break;
3293 default:
3294 break;
3295 }
0a7de745 3296
316670eb 3297
b0d623f7 3298 DTRACE_PROC3(signal__handle, int, signum, siginfo_t *, &(ut->t_dtrace_siginfo),
0a7de745 3299 void (*)(void), SIG_DFL);
b0d623f7 3300#endif
2d21ac55 3301
b0d623f7 3302 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
0a7de745 3303 p->p_pid, W_EXITCODE(0, signum), 3, 0, 0);
39037602 3304
d9a64523 3305 exit_with_reason(p, W_EXITCODE(0, signum), (int *)NULL, TRUE, TRUE, 0, ut_exit_reason);
39037602 3306
316670eb 3307 proc_lock(p);
1c79356b 3308 return;
1c79356b
A
3309 } else {
3310 /*
3311 * If we get here, the signal must be caught.
3312 */
3313#if DIAGNOSTIC
0a7de745 3314 if (catcher == SIG_IGN || (ut->uu_sigmask & mask)) {
1c79356b 3315 log(LOG_WARNING,
0a7de745
A
3316 "postsig: processing masked or ignored signal\n");
3317 }
1c79356b 3318#endif
2d21ac55 3319
1c79356b
A
3320 /*
3321 * Set the new mask value and also defer further
3322 * occurences of this signal.
3323 *
3324 * Special case: user has done a sigpause. Here the
3325 * current mask is not of interest, but rather the
3326 * mask from before the sigpause is what we want
3327 * restored after the signal processing is completed.
3328 */
91447636 3329 if (ut->uu_flag & UT_SAS_OLDMASK) {
9bccf70c 3330 returnmask = ut->uu_oldmask;
91447636 3331 ut->uu_flag &= ~UT_SAS_OLDMASK;
9bccf70c 3332 ut->uu_oldmask = 0;
0a7de745 3333 } else {
9bccf70c 3334 returnmask = ut->uu_sigmask;
0a7de745 3335 }
9bccf70c 3336 ut->uu_sigmask |= ps->ps_catchmask[signum];
0a7de745 3337 if ((ps->ps_signodefer & mask) == 0) {
9bccf70c 3338 ut->uu_sigmask |= mask;
0a7de745 3339 }
cb323159 3340 sigset_t siginfo = ps->ps_siginfo;
9bccf70c 3341 if ((signum != SIGILL) && (signum != SIGTRAP) && (ps->ps_sigreset & mask)) {
0a7de745 3342 if ((signum != SIGCONT) && (sigprop[signum] & SA_IGNORE)) {
9bccf70c 3343 p->p_sigignore |= mask;
0a7de745 3344 }
9bccf70c
A
3345 ps->ps_sigact[signum] = SIG_DFL;
3346 ps->ps_siginfo &= ~mask;
3347 ps->ps_signodefer &= ~mask;
3348 }
9bccf70c 3349
1c79356b
A
3350 if (ps->ps_sig != signum) {
3351 code = 0;
3352 } else {
3353 code = ps->ps_code;
3354 ps->ps_code = 0;
3355 }
b0d623f7 3356 OSIncrementAtomicLong(&p->p_stats->p_ru.ru_nsignals);
cb323159 3357 sendsig(p, catcher, signum, returnmask, code, siginfo);
1c79356b 3358 }
2d21ac55 3359 proc_signalend(p, 1);
1c79356b
A
3360}
3361
3362/*
2d21ac55
A
3363 * Attach a signal knote to the list of knotes for this process.
3364 *
3365 * Signal knotes share the knote list with proc knotes. This
3366 * could be avoided by using a signal-specific knote list, but
3367 * probably isn't worth the trouble.
1c79356b 3368 */
55e303ae
A
3369
3370static int
cb323159 3371filt_sigattach(struct knote *kn, __unused struct kevent_qos_s *kev)
55e303ae 3372{
2d21ac55
A
3373 proc_t p = current_proc(); /* can attach only to oneself */
3374
3375 proc_klist_lock();
55e303ae 3376
cb323159
A
3377 kn->kn_proc = p;
3378 kn->kn_flags |= EV_CLEAR; /* automatically set */
3379 kn->kn_sdata = 0; /* incoming data is ignored */
55e303ae 3380
55e303ae 3381 KNOTE_ATTACH(&p->p_klist, kn);
2d21ac55
A
3382
3383 proc_klist_unlock();
55e303ae 3384
39037602 3385 /* edge-triggered events can't have fired before we attached */
0a7de745 3386 return 0;
55e303ae
A
3387}
3388
2d21ac55
A
3389/*
3390 * remove the knote from the process list, if it hasn't already
0a7de745 3391 * been removed by exit processing.
2d21ac55 3392 */
0a7de745 3393
55e303ae
A
3394static void
3395filt_sigdetach(struct knote *kn)
3396{
cb323159 3397 proc_t p = kn->kn_proc;
55e303ae 3398
2d21ac55 3399 proc_klist_lock();
cb323159 3400 kn->kn_proc = NULL;
55e303ae 3401 KNOTE_DETACH(&p->p_klist, kn);
2d21ac55 3402 proc_klist_unlock();
55e303ae
A
3403}
3404
3405/*
2d21ac55
A
3406 * Post an event to the signal filter. Because we share the same list
3407 * as process knotes, we have to filter out and handle only signal events.
3408 *
3409 * We assume that we process fdfree() before we post the NOTE_EXIT for
3410 * a process during exit. Therefore, since signal filters can only be
3411 * set up "in-process", we should have already torn down the kqueue
3412 * hosting the EVFILT_SIGNAL knote and should never see NOTE_EXIT.
55e303ae
A
3413 */
3414static int
3415filt_signal(struct knote *kn, long hint)
3416{
55e303ae
A
3417 if (hint & NOTE_SIGNAL) {
3418 hint &= ~NOTE_SIGNAL;
3419
0a7de745 3420 if (kn->kn_id == (unsigned int)hint) {
cb323159 3421 kn->kn_hook32++;
0a7de745 3422 }
2d21ac55
A
3423 } else if (hint & NOTE_EXIT) {
3424 panic("filt_signal: detected NOTE_EXIT event");
55e303ae 3425 }
2d21ac55 3426
cb323159 3427 return kn->kn_hook32 != 0;
55e303ae
A
3428}
3429
39037602 3430static int
cb323159 3431filt_signaltouch(struct knote *kn, struct kevent_qos_s *kev)
b0d623f7 3432{
39037602
A
3433#pragma unused(kev)
3434
3435 int res;
3436
b0d623f7 3437 proc_klist_lock();
39037602 3438
d9a64523
A
3439 /*
3440 * No data to save - just capture if it is already fired
39037602 3441 */
cb323159 3442 res = (kn->kn_hook32 > 0);
39037602
A
3443
3444 proc_klist_unlock();
3445
3446 return res;
3447}
3448
3449static int
cb323159 3450filt_signalprocess(struct knote *kn, struct kevent_qos_s *kev)
39037602 3451{
cb323159 3452 int res = 0;
39037602
A
3453
3454 /*
3455 * Snapshot the event data.
39037602 3456 */
39037602 3457
cb323159
A
3458 proc_klist_lock();
3459 if (kn->kn_hook32) {
3460 knote_fill_kevent(kn, kev, kn->kn_hook32);
3461 kn->kn_hook32 = 0;
3462 res = 1;
3463 }
b0d623f7 3464 proc_klist_unlock();
cb323159 3465 return res;
b0d623f7
A
3466}
3467
1c79356b 3468void
2d21ac55 3469bsd_ast(thread_t thread)
1c79356b 3470{
2d21ac55
A
3471 proc_t p = current_proc();
3472 struct uthread *ut = get_bsdthread_info(thread);
0a7de745 3473 int signum;
91447636 3474 static int bsd_init_done = 0;
1c79356b 3475
0a7de745 3476 if (p == NULL) {
1c79356b 3477 return;
0a7de745 3478 }
1c79356b 3479
743345f9
A
3480 /* don't run bsd ast on exec copy or exec'ed tasks */
3481 if (task_did_exec(current_task()) || task_is_exec_copy(current_task())) {
3482 return;
3483 }
3484
2d21ac55 3485 if (timerisset(&p->p_vtimer_user.it_value)) {
0a7de745 3486 uint32_t microsecs;
2d21ac55
A
3487
3488 task_vtimer_update(p->task, TASK_VTIMER_USER, &microsecs);
3489
3490 if (!itimerdecr(p, &p->p_vtimer_user, microsecs)) {
0a7de745 3491 if (timerisset(&p->p_vtimer_user.it_value)) {
2d21ac55 3492 task_vtimer_set(p->task, TASK_VTIMER_USER);
0a7de745 3493 } else {
2d21ac55 3494 task_vtimer_clear(p->task, TASK_VTIMER_USER);
0a7de745 3495 }
2d21ac55 3496
3e170ce0 3497 psignal_try_thread(p, thread, SIGVTALRM);
b0d623f7 3498 }
9bccf70c 3499 }
1c79356b 3500
2d21ac55 3501 if (timerisset(&p->p_vtimer_prof.it_value)) {
0a7de745 3502 uint32_t microsecs;
1c79356b 3503
2d21ac55 3504 task_vtimer_update(p->task, TASK_VTIMER_PROF, &microsecs);
1c79356b 3505
2d21ac55 3506 if (!itimerdecr(p, &p->p_vtimer_prof, microsecs)) {
0a7de745 3507 if (timerisset(&p->p_vtimer_prof.it_value)) {
2d21ac55 3508 task_vtimer_set(p->task, TASK_VTIMER_PROF);
0a7de745 3509 } else {
2d21ac55 3510 task_vtimer_clear(p->task, TASK_VTIMER_PROF);
0a7de745 3511 }
2d21ac55 3512
3e170ce0 3513 psignal_try_thread(p, thread, SIGPROF);
2d21ac55 3514 }
b0d623f7 3515 }
1c79356b 3516
2d21ac55 3517 if (timerisset(&p->p_rlim_cpu)) {
0a7de745 3518 struct timeval tv;
1c79356b 3519
2d21ac55 3520 task_vtimer_update(p->task, TASK_VTIMER_RLIM, (uint32_t *) &tv.tv_usec);
1c79356b 3521
2d21ac55
A
3522 proc_spinlock(p);
3523 if (p->p_rlim_cpu.tv_sec > 0 || p->p_rlim_cpu.tv_usec > tv.tv_usec) {
3524 tv.tv_sec = 0;
3525 timersub(&p->p_rlim_cpu, &tv, &p->p_rlim_cpu);
3526 proc_spinunlock(p);
3527 } else {
2d21ac55
A
3528 timerclear(&p->p_rlim_cpu);
3529 proc_spinunlock(p);
3530
3531 task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
3532
3e170ce0 3533 psignal_try_thread(p, thread, SIGXCPU);
2d21ac55
A
3534 }
3535 }
3536
b0d623f7
A
3537#if CONFIG_DTRACE
3538 if (ut->t_dtrace_sig) {
0a7de745
A
3539 uint8_t dt_action_sig = ut->t_dtrace_sig;
3540 ut->t_dtrace_sig = 0;
3541 psignal(p, dt_action_sig);
b0d623f7 3542 }
6d2010ae 3543
b0d623f7 3544 if (ut->t_dtrace_stop) {
6d2010ae
A
3545 ut->t_dtrace_stop = 0;
3546 proc_lock(p);
3547 p->p_dtrace_stop = 1;
3548 proc_unlock(p);
39236c6e 3549 (void)task_suspend_internal(p->task);
6d2010ae
A
3550 }
3551
3552 if (ut->t_dtrace_resumepid) {
f427ee49 3553 proc_t resumeproc = proc_find((int)ut->t_dtrace_resumepid);
6d2010ae
A
3554 ut->t_dtrace_resumepid = 0;
3555 if (resumeproc != PROC_NULL) {
3556 proc_lock(resumeproc);
3557 /* We only act on processes stopped by dtrace */
3558 if (resumeproc->p_dtrace_stop) {
3559 resumeproc->p_dtrace_stop = 0;
3560 proc_unlock(resumeproc);
39236c6e 3561 task_resume_internal(resumeproc->task);
0a7de745 3562 } else {
6d2010ae
A
3563 proc_unlock(resumeproc);
3564 }
3565 proc_rele(resumeproc);
3566 }
b0d623f7 3567 }
0a7de745 3568
b0d623f7
A
3569#endif /* CONFIG_DTRACE */
3570
316670eb 3571 proc_lock(p);
2d21ac55 3572 if (CHECK_SIGNALS(p, current_thread(), ut)) {
0a7de745 3573 while ((signum = issignal_locked(p))) {
316670eb 3574 postsig_locked(signum);
0a7de745 3575 }
2d21ac55 3576 }
316670eb 3577 proc_unlock(p);
2d21ac55 3578
527f9951
A
3579#ifdef CONFIG_32BIT_TELEMETRY
3580 if (task_consume_32bit_log_flag(p->task)) {
3581 proc_log_32bit_telemetry(p);
3582 }
3583#endif /* CONFIG_32BIT_TELEMETRY */
3584
2d21ac55
A
3585 if (!bsd_init_done) {
3586 bsd_init_done = 1;
3587 bsdinit_task();
3588 }
1c79356b
A
3589}
3590
593a1d5f 3591/* ptrace set runnable */
1c79356b 3592void
2d21ac55 3593pt_setrunnable(proc_t p)
1c79356b 3594{
2d21ac55 3595 task_t task;
1c79356b
A
3596
3597 task = p->task;
3598
2d21ac55
A
3599 if (p->p_lflag & P_LTRACED) {
3600 proc_lock(p);
1c79356b 3601 p->p_stat = SRUN;
2d21ac55 3602 proc_unlock(p);
1c79356b
A
3603 if (p->sigwait) {
3604 wakeup((caddr_t)&(p->sigwait));
0a7de745 3605 if ((p->p_lflag & P_LSIGEXC) == 0) { // 5878479
593a1d5f
A
3606 task_release(task);
3607 }
1c79356b
A
3608 }
3609 }
3610}
9bccf70c
A
3611
3612kern_return_t
3613do_bsdexception(
0a7de745
A
3614 int exc,
3615 int code,
3616 int sub)
9bccf70c 3617{
2d21ac55 3618 mach_exception_data_type_t codes[EXCEPTION_CODE_MAX];
9bccf70c 3619
0a7de745 3620 codes[0] = code;
9bccf70c 3621 codes[1] = sub;
0a7de745 3622 return bsd_exception(exc, codes, 2);
9bccf70c
A
3623}
3624
91447636 3625int
2d21ac55 3626proc_pendingsignals(proc_t p, sigset_t mask)
91447636
A
3627{
3628 struct uthread * uth;
3629 thread_t th;
3630 sigset_t bits = 0;
91447636 3631
2d21ac55 3632 proc_lock(p);
91447636 3633 /* If the process is in proc exit return no signal info */
0a7de745 3634 if (p->p_lflag & P_LPEXIT) {
2d21ac55
A
3635 goto out;
3636 }
91447636 3637
2d21ac55 3638 if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
0a7de745 3639 th = p->p_vforkact;
91447636
A
3640 uth = (struct uthread *)get_bsdthread_info(th);
3641 if (uth) {
3642 bits = (((uth->uu_siglist & ~uth->uu_sigmask) & ~p->p_sigignore) & mask);
3643 }
3644 goto out;
0a7de745 3645 }
91447636
A
3646
3647 bits = 0;
3648 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
3649 bits |= (((uth->uu_siglist & ~uth->uu_sigmask) & ~p->p_sigignore) & mask);
3650 }
3651out:
2d21ac55 3652 proc_unlock(p);
0a7de745 3653 return bits;
91447636
A
3654}
3655
3656int
3657thread_issignal(proc_t p, thread_t th, sigset_t mask)
3658{
3659 struct uthread * uth;
0a7de745 3660 sigset_t bits = 0;
91447636 3661
2d21ac55
A
3662 proc_lock(p);
3663 uth = (struct uthread *)get_bsdthread_info(th);
3664 if (uth) {
3665 bits = (((uth->uu_siglist & ~uth->uu_sigmask) & ~p->p_sigignore) & mask);
3666 }
3667 proc_unlock(p);
0a7de745 3668 return bits;
2d21ac55 3669}
91447636 3670
2d21ac55
A
3671/*
3672 * Allow external reads of the sigprop array.
3673 */
3674int
3675hassigprop(int sig, int prop)
3676{
0a7de745 3677 return sigprop[sig] & prop;
2d21ac55
A
3678}
3679
3680void
3681pgsigio(pid_t pgid, int sig)
0a7de745 3682{
2d21ac55
A
3683 proc_t p = PROC_NULL;
3684
0a7de745 3685 if (pgid < 0) {
2d21ac55 3686 gsignal(-(pgid), sig);
0a7de745 3687 } else if (pgid > 0 && (p = proc_find(pgid)) != 0) {
2d21ac55 3688 psignal(p, sig);
0a7de745
A
3689 }
3690 if (p != PROC_NULL) {
2d21ac55 3691 proc_rele(p);
0a7de745 3692 }
2d21ac55
A
3693}
3694
2d21ac55
A
3695void
3696proc_signalstart(proc_t p, int locked)
3697{
0a7de745 3698 if (!locked) {
2d21ac55 3699 proc_lock(p);
0a7de745
A
3700 }
3701
3702 if (p->p_signalholder == current_thread()) {
3703 panic("proc_signalstart: thread attempting to signal a process for which it holds the signal lock");
3704 }
3705
6d2010ae 3706 p->p_sigwaitcnt++;
0a7de745 3707 while ((p->p_lflag & P_LINSIGNAL) == P_LINSIGNAL) {
2d21ac55 3708 msleep(&p->p_sigmask, &p->p_mlock, 0, "proc_signstart", NULL);
0a7de745 3709 }
6d2010ae
A
3710 p->p_sigwaitcnt--;
3711
2d21ac55 3712 p->p_lflag |= P_LINSIGNAL;
2d21ac55 3713 p->p_signalholder = current_thread();
0a7de745 3714 if (!locked) {
2d21ac55 3715 proc_unlock(p);
0a7de745 3716 }
2d21ac55
A
3717}
3718
3719void
3720proc_signalend(proc_t p, int locked)
3721{
0a7de745 3722 if (!locked) {
2d21ac55 3723 proc_lock(p);
0a7de745 3724 }
2d21ac55
A
3725 p->p_lflag &= ~P_LINSIGNAL;
3726
0a7de745 3727 if (p->p_sigwaitcnt > 0) {
2d21ac55 3728 wakeup(&p->p_sigmask);
0a7de745 3729 }
6d2010ae 3730
2d21ac55 3731 p->p_signalholder = NULL;
0a7de745 3732 if (!locked) {
2d21ac55 3733 proc_unlock(p);
0a7de745 3734 }
2d21ac55
A
3735}
3736
2d21ac55
A
3737void
3738sig_lock_to_exit(proc_t p)
3739{
0a7de745 3740 thread_t self = current_thread();
2d21ac55
A
3741
3742 p->exit_thread = self;
3743 proc_unlock(p);
316670eb
A
3744
3745 task_hold(p->task);
3746 task_wait(p->task, FALSE);
3747
2d21ac55 3748 proc_lock(p);
91447636
A
3749}
3750
2d21ac55
A
3751int
3752sig_try_locked(proc_t p)
3753{
0a7de745 3754 thread_t self = current_thread();
2d21ac55
A
3755
3756 while (p->sigwait || p->exit_thread) {
3757 if (p->exit_thread) {
0a7de745 3758 return 0;
2d21ac55
A
3759 }
3760 msleep((caddr_t)&p->sigwait_thread, &p->p_mlock, PCATCH | PDROP, 0, 0);
3761 if (thread_should_abort(self)) {
3762 /*
3763 * Terminate request - clean up.
3764 */
3765 proc_lock(p);
3766 return -1;
3767 }
3768 proc_lock(p);
3769 }
3770 return 1;
3771}