Bug Summary

File:bsd/kern/kern_sig.c
Warning:line 1410, column 12
Copies out a struct with uncleared padding (>= 4 bytes)

Annotated Source Code

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