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