1*42e22086SApple OSS Distributions /*
2*42e22086SApple OSS Distributions * Copyright (c) 2018 Apple Inc. All rights reserved.
3*42e22086SApple OSS Distributions *
4*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*42e22086SApple OSS Distributions *
6*42e22086SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*42e22086SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*42e22086SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*42e22086SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*42e22086SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*42e22086SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*42e22086SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*42e22086SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*42e22086SApple OSS Distributions *
15*42e22086SApple OSS Distributions * Please obtain a copy of the License at
16*42e22086SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*42e22086SApple OSS Distributions *
18*42e22086SApple OSS Distributions * The Original Code and all software distributed under the License are
19*42e22086SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*42e22086SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*42e22086SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*42e22086SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*42e22086SApple OSS Distributions * Please see the License for the specific language governing rights and
24*42e22086SApple OSS Distributions * limitations under the License.
25*42e22086SApple OSS Distributions *
26*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*42e22086SApple OSS Distributions */
28*42e22086SApple OSS Distributions
29*42e22086SApple OSS Distributions #include <errno.h>
30*42e22086SApple OSS Distributions #include <mach/host_special_ports.h>
31*42e22086SApple OSS Distributions #include <mach/task_special_ports.h>
32*42e22086SApple OSS Distributions #include <mach/thread_special_ports.h>
33*42e22086SApple OSS Distributions #include <mach/port_descriptions.h>
34*42e22086SApple OSS Distributions #include <stdlib.h>
35*42e22086SApple OSS Distributions #include <strings.h>
36*42e22086SApple OSS Distributions
37*42e22086SApple OSS Distributions const char *
mach_host_special_port_description(int port)38*42e22086SApple OSS Distributions mach_host_special_port_description(int port)
39*42e22086SApple OSS Distributions {
40*42e22086SApple OSS Distributions int port_index = (int)port;
41*42e22086SApple OSS Distributions
42*42e22086SApple OSS Distributions if (port_index < 0 || port_index > HOST_MAX_SPECIAL_PORT) {
43*42e22086SApple OSS Distributions return NULL;
44*42e22086SApple OSS Distributions }
45*42e22086SApple OSS Distributions
46*42e22086SApple OSS Distributions static const char *hsp_descs[] = {
47*42e22086SApple OSS Distributions [HOST_PORT] = "host (restricted)",
48*42e22086SApple OSS Distributions [HOST_PRIV_PORT] = "host private (restricted)",
49*42e22086SApple OSS Distributions [HOST_IO_MAIN_PORT] = "I/O main (restricted)",
50*42e22086SApple OSS Distributions
51*42e22086SApple OSS Distributions [HOST_DYNAMIC_PAGER_PORT] = "dynamic pager",
52*42e22086SApple OSS Distributions [HOST_AUDIT_CONTROL_PORT] = "audit control",
53*42e22086SApple OSS Distributions [HOST_USER_NOTIFICATION_PORT] = "user notification",
54*42e22086SApple OSS Distributions [HOST_AUTOMOUNTD_PORT] = "automounter",
55*42e22086SApple OSS Distributions [HOST_LOCKD_PORT] = "lockd",
56*42e22086SApple OSS Distributions [HOST_KTRACE_BACKGROUND_PORT] = "ktrace background notification",
57*42e22086SApple OSS Distributions [HOST_SEATBELT_PORT] = "seatbelt",
58*42e22086SApple OSS Distributions [HOST_KEXTD_PORT] = "kextd",
59*42e22086SApple OSS Distributions [HOST_LAUNCHCTL_PORT] = "launchctl",
60*42e22086SApple OSS Distributions [HOST_UNFREED_PORT] = "fairplay",
61*42e22086SApple OSS Distributions [HOST_AMFID_PORT] = "amfi",
62*42e22086SApple OSS Distributions [HOST_GSSD_PORT] = "gssd",
63*42e22086SApple OSS Distributions [HOST_TELEMETRY_PORT] = "telemetry",
64*42e22086SApple OSS Distributions [HOST_ATM_NOTIFICATION_PORT] = "atm notification",
65*42e22086SApple OSS Distributions [HOST_COALITION_PORT] = "coalition notification",
66*42e22086SApple OSS Distributions [HOST_SYSDIAGNOSE_PORT] = "sysdiagnose notification",
67*42e22086SApple OSS Distributions [HOST_XPC_EXCEPTION_PORT] = "XPC exception",
68*42e22086SApple OSS Distributions [HOST_CONTAINERD_PORT] = "container manager",
69*42e22086SApple OSS Distributions [HOST_NODE_PORT] = "node",
70*42e22086SApple OSS Distributions [HOST_RESOURCE_NOTIFY_PORT] = "resource notify",
71*42e22086SApple OSS Distributions [HOST_CLOSURED_PORT] = "closured",
72*42e22086SApple OSS Distributions [HOST_SYSPOLICYD_PORT] = "syspolicyd",
73*42e22086SApple OSS Distributions [HOST_FILECOORDINATIOND_PORT] = "filecoordinationd",
74*42e22086SApple OSS Distributions [HOST_FAIRPLAYD_PORT] = "fairplayd",
75*42e22086SApple OSS Distributions [HOST_IOCOMPRESSIONSTATS_PORT] = "I/O compression stats",
76*42e22086SApple OSS Distributions [HOST_MEMORY_ERROR_PORT] = "memory error stats",
77*42e22086SApple OSS Distributions };
78*42e22086SApple OSS Distributions _Static_assert(HOST_MEMORY_ERROR_PORT == HOST_MAX_SPECIAL_PORT,
79*42e22086SApple OSS Distributions "all host special ports must have descriptions");
80*42e22086SApple OSS Distributions
81*42e22086SApple OSS Distributions return hsp_descs[port_index];
82*42e22086SApple OSS Distributions }
83*42e22086SApple OSS Distributions
84*42e22086SApple OSS Distributions const char *
mach_task_special_port_description(int port)85*42e22086SApple OSS Distributions mach_task_special_port_description(int port)
86*42e22086SApple OSS Distributions {
87*42e22086SApple OSS Distributions int port_index = (int)port;
88*42e22086SApple OSS Distributions
89*42e22086SApple OSS Distributions if (port_index < 0 || port_index > TASK_MAX_SPECIAL_PORT) {
90*42e22086SApple OSS Distributions return NULL;
91*42e22086SApple OSS Distributions }
92*42e22086SApple OSS Distributions
93*42e22086SApple OSS Distributions static const char *tsp_descs[] = {
94*42e22086SApple OSS Distributions [TASK_KERNEL_PORT] = "kernel",
95*42e22086SApple OSS Distributions [TASK_HOST_PORT] = "host",
96*42e22086SApple OSS Distributions [TASK_NAME_PORT] = "name",
97*42e22086SApple OSS Distributions [TASK_BOOTSTRAP_PORT] = "bootstrap",
98*42e22086SApple OSS Distributions [TASK_INSPECT_PORT] = "inspect",
99*42e22086SApple OSS Distributions [TASK_READ_PORT] = "read",
100*42e22086SApple OSS Distributions [TASK_ACCESS_PORT] = "access",
101*42e22086SApple OSS Distributions [TASK_DEBUG_CONTROL_PORT] = "debug control",
102*42e22086SApple OSS Distributions [TASK_RESOURCE_NOTIFY_PORT] = "resource notify",
103*42e22086SApple OSS Distributions };
104*42e22086SApple OSS Distributions _Static_assert(TASK_RESOURCE_NOTIFY_PORT == TASK_MAX_SPECIAL_PORT,
105*42e22086SApple OSS Distributions "all task special ports must have descriptions");
106*42e22086SApple OSS Distributions
107*42e22086SApple OSS Distributions return tsp_descs[port_index];
108*42e22086SApple OSS Distributions }
109*42e22086SApple OSS Distributions
110*42e22086SApple OSS Distributions const char *
mach_thread_special_port_description(int port)111*42e22086SApple OSS Distributions mach_thread_special_port_description(int port)
112*42e22086SApple OSS Distributions {
113*42e22086SApple OSS Distributions int port_index = (int)port;
114*42e22086SApple OSS Distributions
115*42e22086SApple OSS Distributions if (port_index < 0 || port_index > THREAD_MAX_SPECIAL_PORT) {
116*42e22086SApple OSS Distributions return NULL;
117*42e22086SApple OSS Distributions }
118*42e22086SApple OSS Distributions
119*42e22086SApple OSS Distributions static const char *tsp_descs[] = {
120*42e22086SApple OSS Distributions [THREAD_KERNEL_PORT] = "kernel",
121*42e22086SApple OSS Distributions [THREAD_INSPECT_PORT] = "inspect",
122*42e22086SApple OSS Distributions [THREAD_READ_PORT] = "read",
123*42e22086SApple OSS Distributions };
124*42e22086SApple OSS Distributions _Static_assert(THREAD_READ_PORT == THREAD_MAX_SPECIAL_PORT,
125*42e22086SApple OSS Distributions "all thread special ports must have descriptions");
126*42e22086SApple OSS Distributions
127*42e22086SApple OSS Distributions return tsp_descs[port_index];
128*42e22086SApple OSS Distributions }
129*42e22086SApple OSS Distributions
130*42e22086SApple OSS Distributions static int
port_for_id_internal(const char * id,const char ** ids,int nids)131*42e22086SApple OSS Distributions port_for_id_internal(const char *id, const char **ids, int nids)
132*42e22086SApple OSS Distributions {
133*42e22086SApple OSS Distributions if (!id) {
134*42e22086SApple OSS Distributions errno = EINVAL;
135*42e22086SApple OSS Distributions return -1;
136*42e22086SApple OSS Distributions }
137*42e22086SApple OSS Distributions
138*42e22086SApple OSS Distributions for (int i = 0; i < nids; i++) {
139*42e22086SApple OSS Distributions if (ids[i] && strcmp(ids[i], id) == 0) {
140*42e22086SApple OSS Distributions return i;
141*42e22086SApple OSS Distributions }
142*42e22086SApple OSS Distributions }
143*42e22086SApple OSS Distributions
144*42e22086SApple OSS Distributions errno = ENOENT;
145*42e22086SApple OSS Distributions return -1;
146*42e22086SApple OSS Distributions }
147*42e22086SApple OSS Distributions
148*42e22086SApple OSS Distributions int
mach_host_special_port_for_id(const char * id)149*42e22086SApple OSS Distributions mach_host_special_port_for_id(const char *id)
150*42e22086SApple OSS Distributions {
151*42e22086SApple OSS Distributions static const char *hsp_ids[] = {
152*42e22086SApple OSS Distributions #define SP_ENTRY(id) [id] = #id
153*42e22086SApple OSS Distributions SP_ENTRY(HOST_PORT),
154*42e22086SApple OSS Distributions SP_ENTRY(HOST_PRIV_PORT),
155*42e22086SApple OSS Distributions SP_ENTRY(HOST_IO_MAIN_PORT),
156*42e22086SApple OSS Distributions SP_ENTRY(HOST_DYNAMIC_PAGER_PORT),
157*42e22086SApple OSS Distributions SP_ENTRY(HOST_AUDIT_CONTROL_PORT),
158*42e22086SApple OSS Distributions SP_ENTRY(HOST_USER_NOTIFICATION_PORT),
159*42e22086SApple OSS Distributions SP_ENTRY(HOST_AUTOMOUNTD_PORT),
160*42e22086SApple OSS Distributions SP_ENTRY(HOST_LOCKD_PORT),
161*42e22086SApple OSS Distributions SP_ENTRY(HOST_KTRACE_BACKGROUND_PORT),
162*42e22086SApple OSS Distributions SP_ENTRY(HOST_SEATBELT_PORT),
163*42e22086SApple OSS Distributions SP_ENTRY(HOST_KEXTD_PORT),
164*42e22086SApple OSS Distributions SP_ENTRY(HOST_LAUNCHCTL_PORT),
165*42e22086SApple OSS Distributions SP_ENTRY(HOST_UNFREED_PORT),
166*42e22086SApple OSS Distributions SP_ENTRY(HOST_AMFID_PORT),
167*42e22086SApple OSS Distributions SP_ENTRY(HOST_GSSD_PORT),
168*42e22086SApple OSS Distributions SP_ENTRY(HOST_TELEMETRY_PORT),
169*42e22086SApple OSS Distributions SP_ENTRY(HOST_ATM_NOTIFICATION_PORT),
170*42e22086SApple OSS Distributions SP_ENTRY(HOST_COALITION_PORT),
171*42e22086SApple OSS Distributions SP_ENTRY(HOST_SYSDIAGNOSE_PORT),
172*42e22086SApple OSS Distributions SP_ENTRY(HOST_XPC_EXCEPTION_PORT),
173*42e22086SApple OSS Distributions SP_ENTRY(HOST_CONTAINERD_PORT),
174*42e22086SApple OSS Distributions SP_ENTRY(HOST_NODE_PORT),
175*42e22086SApple OSS Distributions SP_ENTRY(HOST_RESOURCE_NOTIFY_PORT),
176*42e22086SApple OSS Distributions SP_ENTRY(HOST_CLOSURED_PORT),
177*42e22086SApple OSS Distributions SP_ENTRY(HOST_SYSPOLICYD_PORT),
178*42e22086SApple OSS Distributions SP_ENTRY(HOST_FILECOORDINATIOND_PORT),
179*42e22086SApple OSS Distributions };
180*42e22086SApple OSS Distributions
181*42e22086SApple OSS Distributions return port_for_id_internal(id, hsp_ids,
182*42e22086SApple OSS Distributions sizeof(hsp_ids) / sizeof(hsp_ids[0]));
183*42e22086SApple OSS Distributions }
184*42e22086SApple OSS Distributions
185*42e22086SApple OSS Distributions int
mach_task_special_port_for_id(const char * id)186*42e22086SApple OSS Distributions mach_task_special_port_for_id(const char *id)
187*42e22086SApple OSS Distributions {
188*42e22086SApple OSS Distributions static const char *tsp_ids[] = {
189*42e22086SApple OSS Distributions SP_ENTRY(TASK_KERNEL_PORT),
190*42e22086SApple OSS Distributions SP_ENTRY(TASK_HOST_PORT),
191*42e22086SApple OSS Distributions SP_ENTRY(TASK_NAME_PORT),
192*42e22086SApple OSS Distributions SP_ENTRY(TASK_BOOTSTRAP_PORT),
193*42e22086SApple OSS Distributions SP_ENTRY(TASK_INSPECT_PORT),
194*42e22086SApple OSS Distributions SP_ENTRY(TASK_READ_PORT),
195*42e22086SApple OSS Distributions SP_ENTRY(TASK_ACCESS_PORT),
196*42e22086SApple OSS Distributions SP_ENTRY(TASK_DEBUG_CONTROL_PORT),
197*42e22086SApple OSS Distributions SP_ENTRY(TASK_RESOURCE_NOTIFY_PORT),
198*42e22086SApple OSS Distributions };
199*42e22086SApple OSS Distributions
200*42e22086SApple OSS Distributions return port_for_id_internal(id, tsp_ids,
201*42e22086SApple OSS Distributions sizeof(tsp_ids) / sizeof(tsp_ids[0]));
202*42e22086SApple OSS Distributions }
203*42e22086SApple OSS Distributions
204*42e22086SApple OSS Distributions int
mach_thread_special_port_for_id(const char * id)205*42e22086SApple OSS Distributions mach_thread_special_port_for_id(const char *id)
206*42e22086SApple OSS Distributions {
207*42e22086SApple OSS Distributions static const char *tsp_ids[] = {
208*42e22086SApple OSS Distributions SP_ENTRY(THREAD_KERNEL_PORT),
209*42e22086SApple OSS Distributions SP_ENTRY(THREAD_INSPECT_PORT),
210*42e22086SApple OSS Distributions SP_ENTRY(THREAD_READ_PORT),
211*42e22086SApple OSS Distributions #undef SP_ENTRY
212*42e22086SApple OSS Distributions };
213*42e22086SApple OSS Distributions
214*42e22086SApple OSS Distributions return port_for_id_internal(id, tsp_ids,
215*42e22086SApple OSS Distributions sizeof(tsp_ids) / sizeof(tsp_ids[0]));
216*42e22086SApple OSS Distributions }
217