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