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