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