xref: /xnu-10002.81.5/libsyscall/mach/port_descriptions.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
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/thread_special_ports.h>
33 #include <mach/port_descriptions.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 
37 const char *
mach_host_special_port_description(int port)38 mach_host_special_port_description(int port)
39 {
40 	int port_index = (int)port;
41 
42 	if (port_index < 0 || port_index > HOST_MAX_SPECIAL_PORT) {
43 		return NULL;
44 	}
45 
46 	static const char *hsp_descs[] = {
47 		[HOST_PORT] = "host (restricted)",
48 		[HOST_PRIV_PORT] = "host private (restricted)",
49 		[HOST_IO_MAIN_PORT] = "I/O main (restricted)",
50 
51 		[HOST_DYNAMIC_PAGER_PORT] = "dynamic pager",
52 		[HOST_AUDIT_CONTROL_PORT] = "audit control",
53 		[HOST_USER_NOTIFICATION_PORT] = "user notification",
54 		[HOST_AUTOMOUNTD_PORT] = "automounter",
55 		[HOST_LOCKD_PORT] = "lockd",
56 		[HOST_KTRACE_BACKGROUND_PORT] = "ktrace background notification",
57 		[HOST_SEATBELT_PORT] = "seatbelt",
58 		[HOST_KEXTD_PORT] = "kextd",
59 		[HOST_LAUNCHCTL_PORT] = "launchctl",
60 		[HOST_UNFREED_PORT] = "fairplay",
61 		[HOST_AMFID_PORT] = "amfi",
62 		[HOST_GSSD_PORT] = "gssd",
63 		[HOST_TELEMETRY_PORT] = "telemetry",
64 		[HOST_ATM_NOTIFICATION_PORT] = "atm notification",
65 		[HOST_COALITION_PORT] = "coalition notification",
66 		[HOST_SYSDIAGNOSE_PORT] = "sysdiagnose notification",
67 		[HOST_XPC_EXCEPTION_PORT] = "XPC exception",
68 		[HOST_CONTAINERD_PORT] = "container manager",
69 		[HOST_NODE_PORT] = "node",
70 		[HOST_RESOURCE_NOTIFY_PORT] = "resource notify",
71 		[HOST_CLOSURED_PORT] = "closured",
72 		[HOST_SYSPOLICYD_PORT] = "syspolicyd",
73 		[HOST_FILECOORDINATIOND_PORT] = "filecoordinationd",
74 		[HOST_FAIRPLAYD_PORT] = "fairplayd",
75 		[HOST_IOCOMPRESSIONSTATS_PORT] = "I/O compression stats",
76 		[HOST_MEMORY_ERROR_PORT] = "memory error stats",
77 	};
78 	_Static_assert(HOST_MEMORY_ERROR_PORT == HOST_MAX_SPECIAL_PORT,
79 	    "all host special ports must have descriptions");
80 
81 	return hsp_descs[port_index];
82 }
83 
84 const char *
mach_task_special_port_description(int port)85 mach_task_special_port_description(int port)
86 {
87 	int port_index = (int)port;
88 
89 	if (port_index < 0 || port_index > TASK_MAX_SPECIAL_PORT) {
90 		return NULL;
91 	}
92 
93 	static const char *tsp_descs[] = {
94 		[TASK_KERNEL_PORT] = "kernel",
95 		[TASK_HOST_PORT] = "host",
96 		[TASK_NAME_PORT] = "name",
97 		[TASK_BOOTSTRAP_PORT] = "bootstrap",
98 		[TASK_INSPECT_PORT] = "inspect",
99 		[TASK_READ_PORT] = "read",
100 		[TASK_ACCESS_PORT] = "access",
101 		[TASK_DEBUG_CONTROL_PORT] = "debug control",
102 		[TASK_RESOURCE_NOTIFY_PORT] = "resource notify",
103 	};
104 	_Static_assert(TASK_RESOURCE_NOTIFY_PORT == TASK_MAX_SPECIAL_PORT,
105 	    "all task special ports must have descriptions");
106 
107 	return tsp_descs[port_index];
108 }
109 
110 const char *
mach_thread_special_port_description(int port)111 mach_thread_special_port_description(int port)
112 {
113 	int port_index = (int)port;
114 
115 	if (port_index < 0 || port_index > THREAD_MAX_SPECIAL_PORT) {
116 		return NULL;
117 	}
118 
119 	static const char *tsp_descs[] = {
120 		[THREAD_KERNEL_PORT] = "kernel",
121 		[THREAD_INSPECT_PORT] = "inspect",
122 		[THREAD_READ_PORT] = "read",
123 	};
124 	_Static_assert(THREAD_READ_PORT == THREAD_MAX_SPECIAL_PORT,
125 	    "all thread special ports must have descriptions");
126 
127 	return tsp_descs[port_index];
128 }
129 
130 static int
port_for_id_internal(const char * id,const char ** ids,int nids)131 port_for_id_internal(const char *id, const char **ids, int nids)
132 {
133 	if (!id) {
134 		errno = EINVAL;
135 		return -1;
136 	}
137 
138 	for (int i = 0; i < nids; i++) {
139 		if (ids[i] && strcmp(ids[i], id) == 0) {
140 			return i;
141 		}
142 	}
143 
144 	errno = ENOENT;
145 	return -1;
146 }
147 
148 int
mach_host_special_port_for_id(const char * id)149 mach_host_special_port_for_id(const char *id)
150 {
151 	static const char *hsp_ids[] = {
152 #define SP_ENTRY(id) [id] = #id
153 		SP_ENTRY(HOST_PORT),
154 		SP_ENTRY(HOST_PRIV_PORT),
155 		SP_ENTRY(HOST_IO_MAIN_PORT),
156 		SP_ENTRY(HOST_DYNAMIC_PAGER_PORT),
157 		SP_ENTRY(HOST_AUDIT_CONTROL_PORT),
158 		SP_ENTRY(HOST_USER_NOTIFICATION_PORT),
159 		SP_ENTRY(HOST_AUTOMOUNTD_PORT),
160 		SP_ENTRY(HOST_LOCKD_PORT),
161 		SP_ENTRY(HOST_KTRACE_BACKGROUND_PORT),
162 		SP_ENTRY(HOST_SEATBELT_PORT),
163 		SP_ENTRY(HOST_KEXTD_PORT),
164 		SP_ENTRY(HOST_LAUNCHCTL_PORT),
165 		SP_ENTRY(HOST_UNFREED_PORT),
166 		SP_ENTRY(HOST_AMFID_PORT),
167 		SP_ENTRY(HOST_GSSD_PORT),
168 		SP_ENTRY(HOST_TELEMETRY_PORT),
169 		SP_ENTRY(HOST_ATM_NOTIFICATION_PORT),
170 		SP_ENTRY(HOST_COALITION_PORT),
171 		SP_ENTRY(HOST_SYSDIAGNOSE_PORT),
172 		SP_ENTRY(HOST_XPC_EXCEPTION_PORT),
173 		SP_ENTRY(HOST_CONTAINERD_PORT),
174 		SP_ENTRY(HOST_NODE_PORT),
175 		SP_ENTRY(HOST_RESOURCE_NOTIFY_PORT),
176 		SP_ENTRY(HOST_CLOSURED_PORT),
177 		SP_ENTRY(HOST_SYSPOLICYD_PORT),
178 		SP_ENTRY(HOST_FILECOORDINATIOND_PORT),
179 	};
180 
181 	return port_for_id_internal(id, hsp_ids,
182 	           sizeof(hsp_ids) / sizeof(hsp_ids[0]));
183 }
184 
185 int
mach_task_special_port_for_id(const char * id)186 mach_task_special_port_for_id(const char *id)
187 {
188 	static const char *tsp_ids[] = {
189 		SP_ENTRY(TASK_KERNEL_PORT),
190 		SP_ENTRY(TASK_HOST_PORT),
191 		SP_ENTRY(TASK_NAME_PORT),
192 		SP_ENTRY(TASK_BOOTSTRAP_PORT),
193 		SP_ENTRY(TASK_INSPECT_PORT),
194 		SP_ENTRY(TASK_READ_PORT),
195 		SP_ENTRY(TASK_ACCESS_PORT),
196 		SP_ENTRY(TASK_DEBUG_CONTROL_PORT),
197 		SP_ENTRY(TASK_RESOURCE_NOTIFY_PORT),
198 	};
199 
200 	return port_for_id_internal(id, tsp_ids,
201 	           sizeof(tsp_ids) / sizeof(tsp_ids[0]));
202 }
203 
204 int
mach_thread_special_port_for_id(const char * id)205 mach_thread_special_port_for_id(const char *id)
206 {
207 	static const char *tsp_ids[] = {
208 		SP_ENTRY(THREAD_KERNEL_PORT),
209 		SP_ENTRY(THREAD_INSPECT_PORT),
210 		SP_ENTRY(THREAD_READ_PORT),
211 #undef SP_ENTRY
212 	};
213 
214 	return port_for_id_internal(id, tsp_ids,
215 	           sizeof(tsp_ids) / sizeof(tsp_ids[0]));
216 }
217