]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/mach/port_descriptions.c
xnu-4903.221.2.tar.gz
[apple/xnu.git] / libsyscall / mach / port_descriptions.c
CommitLineData
d9a64523
A
1/*
2 * Copyright (c) 2018 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#include <errno.h>
30#include <mach/host_special_ports.h>
31#include <mach/task_special_ports.h>
32#include <mach/port_descriptions.h>
33#include <stdlib.h>
34#include <strings.h>
35
36const char *
37mach_host_special_port_description(int port)
38{
39 int port_index = (int)port;
40
41 if (port_index < 0 || port_index > HOST_MAX_SPECIAL_PORT) {
42 return NULL;
43 }
44
45 static const char *hsp_descs[] = {
46 [HOST_PORT] = "host (restricted)",
47 [HOST_PRIV_PORT] = "host private (restricted)",
48 [HOST_IO_MASTER_PORT] = "I/O master (restricted)",
49
50 [HOST_DYNAMIC_PAGER_PORT] = "dynamic pager",
51 [HOST_AUDIT_CONTROL_PORT] = "audit control",
52 [HOST_USER_NOTIFICATION_PORT] = "user notification",
53 [HOST_AUTOMOUNTD_PORT] = "automounter",
54 [HOST_LOCKD_PORT] = "lockd",
55 [HOST_KTRACE_BACKGROUND_PORT] = "ktrace background notification",
56 [HOST_SEATBELT_PORT] = "seatbelt",
57 [HOST_KEXTD_PORT] = "kextd",
58 [HOST_LAUNCHCTL_PORT] = "launchctl",
59 [HOST_UNFREED_PORT] = "fairplay",
60 [HOST_AMFID_PORT] = "amfi",
61 [HOST_GSSD_PORT] = "gssd",
62 [HOST_TELEMETRY_PORT] = "telemetry",
63 [HOST_ATM_NOTIFICATION_PORT] = "atm notification",
64 [HOST_COALITION_PORT] = "coalition notification",
65 [HOST_SYSDIAGNOSE_PORT] = "sysdiagnose notification",
66 [HOST_XPC_EXCEPTION_PORT] = "XPC exception",
67 [HOST_CONTAINERD_PORT] = "container manager",
68 [HOST_NODE_PORT] = "node",
69 [HOST_RESOURCE_NOTIFY_PORT] = "resource notify",
70 [HOST_CLOSURED_PORT] = "closured",
71 [HOST_SYSPOLICYD_PORT] = "syspolicyd",
72 };
73 _Static_assert(HOST_SYSPOLICYD_PORT == HOST_MAX_SPECIAL_PORT,
74 "all host special ports must have descriptions");
75
76 return hsp_descs[port_index];
77}
78
79const char *
80mach_task_special_port_description(int port)
81{
82 int port_index = (int)port;
83
84 if (port_index < 0 || port_index > TASK_MAX_SPECIAL_PORT) {
85 return NULL;
86 }
87
88 static const char *tsp_descs[] = {
89 [TASK_KERNEL_PORT] = "kernel",
90 [TASK_HOST_PORT] = "host",
91 [TASK_NAME_PORT] = "name",
92 [TASK_BOOTSTRAP_PORT] = "bootstrap",
93 [TASK_SEATBELT_PORT] = "seatbelt",
94 [TASK_ACCESS_PORT] = "access",
95 [TASK_DEBUG_CONTROL_PORT] = "debug control",
96 [TASK_RESOURCE_NOTIFY_PORT] = "resource notify",
97 };
98 _Static_assert(TASK_RESOURCE_NOTIFY_PORT == TASK_MAX_SPECIAL_PORT,
99 "all task special ports must have descriptions");
100
101 return tsp_descs[port_index];
102}
103
104static int
105port_for_id_internal(const char *id, const char **ids, int nids)
106{
107 if (!id) {
108 errno = EINVAL;
109 return -1;
110 }
111
112 for (int i = 0; i < nids; i++) {
113 if (ids[i] && strcmp(ids[i], id) == 0) {
114 return i;
115 }
116 }
117
118 errno = ENOENT;
119 return -1;
120}
121
122int
123mach_host_special_port_for_id(const char *id)
124{
125 static const char *hsp_ids[] = {
126#define SP_ENTRY(id) [id] = #id
127 SP_ENTRY(HOST_PORT),
128 SP_ENTRY(HOST_PRIV_PORT),
129 SP_ENTRY(HOST_IO_MASTER_PORT),
130 SP_ENTRY(HOST_DYNAMIC_PAGER_PORT),
131 SP_ENTRY(HOST_AUDIT_CONTROL_PORT),
132 SP_ENTRY(HOST_USER_NOTIFICATION_PORT),
133 SP_ENTRY(HOST_AUTOMOUNTD_PORT),
134 SP_ENTRY(HOST_LOCKD_PORT),
135 SP_ENTRY(HOST_KTRACE_BACKGROUND_PORT),
136 SP_ENTRY(HOST_SEATBELT_PORT),
137 SP_ENTRY(HOST_KEXTD_PORT),
138 SP_ENTRY(HOST_LAUNCHCTL_PORT),
139 SP_ENTRY(HOST_UNFREED_PORT),
140 SP_ENTRY(HOST_AMFID_PORT),
141 SP_ENTRY(HOST_GSSD_PORT),
142 SP_ENTRY(HOST_TELEMETRY_PORT),
143 SP_ENTRY(HOST_ATM_NOTIFICATION_PORT),
144 SP_ENTRY(HOST_COALITION_PORT),
145 SP_ENTRY(HOST_SYSDIAGNOSE_PORT),
146 SP_ENTRY(HOST_XPC_EXCEPTION_PORT),
147 SP_ENTRY(HOST_CONTAINERD_PORT),
148 SP_ENTRY(HOST_NODE_PORT),
149 SP_ENTRY(HOST_RESOURCE_NOTIFY_PORT),
150 SP_ENTRY(HOST_CLOSURED_PORT),
151 SP_ENTRY(HOST_SYSPOLICYD_PORT),
152 };
153
154 return port_for_id_internal(id, hsp_ids,
155 sizeof(hsp_ids) / sizeof(hsp_ids[0]));
156}
157
158int
159mach_task_special_port_for_id(const char *id)
160{
161 static const char *tsp_ids[] = {
162 SP_ENTRY(TASK_KERNEL_PORT),
163 SP_ENTRY(TASK_HOST_PORT),
164 SP_ENTRY(TASK_NAME_PORT),
165 SP_ENTRY(TASK_BOOTSTRAP_PORT),
166 SP_ENTRY(TASK_SEATBELT_PORT),
167 SP_ENTRY(TASK_ACCESS_PORT),
168 SP_ENTRY(TASK_DEBUG_CONTROL_PORT),
169 SP_ENTRY(TASK_RESOURCE_NOTIFY_PORT),
170#undef SP_ENTRY
171 };
172
173 return port_for_id_internal(id, tsp_ids,
174 sizeof(tsp_ids) / sizeof(tsp_ids[0]));
175}