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