]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/wrappers/terminate_with_reason.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / libsyscall / wrappers / terminate_with_reason.c
CommitLineData
39037602
A
1/*
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#include <sys/reason.h>
24#include <sys/types.h>
25#include <stdint.h>
26#include <signal.h>
27#include <unistd.h>
28
29/* System call entry points */
30int __terminate_with_payload(int pid, uint32_t reason_namespace, uint64_t reason_code,
0a7de745
A
31 void *payload, uint32_t payload_size, const char *reason_string,
32 uint64_t reason_flags);
39037602
A
33
34void __abort_with_payload(uint32_t reason_namespace, uint64_t reason_code,
0a7de745
A
35 void *payload, uint32_t payload_size, const char *reason_string,
36 uint64_t reason_flags);
39037602
A
37
38static void abort_with_payload_wrapper_internal(uint32_t reason_namespace, uint64_t reason_code,
0a7de745 39 void *payload, uint32_t payload_size, const char *reason_string,
cb323159 40 uint64_t reason_flags) __attribute__((noreturn, cold));
39037602
A
41
42/* System call wrappers */
43int
44terminate_with_reason(int pid, uint32_t reason_namespace, uint64_t reason_code,
0a7de745 45 const char *reason_string, uint64_t reason_flags)
39037602
A
46{
47 return __terminate_with_payload(pid, reason_namespace, reason_code, 0, 0,
0a7de745 48 reason_string, reason_flags);
39037602
A
49}
50
51int
52terminate_with_payload(int pid, uint32_t reason_namespace, uint64_t reason_code,
0a7de745
A
53 void *payload, uint32_t payload_size,
54 const char *reason_string, uint64_t reason_flags)
39037602
A
55{
56 return __terminate_with_payload(pid, reason_namespace, reason_code, payload,
0a7de745 57 payload_size, reason_string, reason_flags);
39037602
A
58}
59
0a7de745
A
60static void
61abort_with_payload_wrapper_internal(uint32_t reason_namespace, uint64_t reason_code,
62 void *payload, uint32_t payload_size, const char *reason_string,
63 uint64_t reason_flags)
39037602
A
64{
65 sigset_t unmask_signal;
66
67 /* Try to unmask SIGABRT before trapping to the kernel */
68 sigemptyset(&unmask_signal);
69 sigaddset(&unmask_signal, SIGABRT);
70 sigprocmask(SIG_UNBLOCK, &unmask_signal, NULL);
71
72 __abort_with_payload(reason_namespace, reason_code, payload, payload_size,
0a7de745 73 reason_string, reason_flags);
39037602 74
d190cdc3 75 /* If sending a SIGABRT failed, we fall back to SIGKILL */
39037602 76 terminate_with_payload(getpid(), reason_namespace, reason_code, payload, payload_size,
0a7de745 77 reason_string, reason_flags | OS_REASON_FLAG_ABORT);
39037602 78
d190cdc3 79 __builtin_unreachable();
39037602
A
80}
81
82void
83abort_with_reason(uint32_t reason_namespace, uint64_t reason_code, const char *reason_string,
0a7de745 84 uint64_t reason_flags)
39037602
A
85{
86 abort_with_payload_wrapper_internal(reason_namespace, reason_code, 0, 0, reason_string, reason_flags);
87}
88
89void
90abort_with_payload(uint32_t reason_namespace, uint64_t reason_code, void *payload,
0a7de745
A
91 uint32_t payload_size, const char *reason_string,
92 uint64_t reason_flags)
39037602
A
93{
94 abort_with_payload_wrapper_internal(reason_namespace, reason_code, payload, payload_size,
0a7de745 95 reason_string, reason_flags);
39037602 96}