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