-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchControl.m
407 lines (288 loc) · 14.5 KB
/
LaunchControl.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#import "LaunchControl.h"
@implementation LaunchControl
- (instancetype)init {
if (self = [super init]) {
port = MACH_PORT_NULL;
task_get_bootstrap_port(mach_task_self(), &port);
global_data = (struct xpc_global_data *)_os_alloc_once_table[1].ptr;
}
return self;
}
- (int64_t)managerUID {
return [self managerUIDPID:3];
}
- (int64_t)managerPID {
return [self managerUIDPID:4];
}
- (int64_t)managerUIDPID:(int)outgsk {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_OUTGSK, DICT_KEY_SELF, DICT_KEY_TYPE, DICT_KEY_GET},
(xpc_object_t []){xpc_uint64_create(0), xpc_uint64_create(outgsk), XPC_BOOL_TRUE, xpc_uint64_create(DOMAIN_LEGACY), XPC_BOOL_TRUE},
5);
xpc_dictionary_set_mach_send(dict, DICT_KEY_DOMAINPORT, port);
xpc_object_t reply;
[self pipeRoutine:ROUTINE_MANAGER subsystem:6 reply:&reply error:nil];
if (reply) {
int64_t uid_pid = xpc_dictionary_get_int64(reply, DICT_KEY_OUT);
if (uid_pid) {
return uid_pid;
}
}
return -1;
}
- (NSString *)managerName {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_OUTGSK, DICT_KEY_SELF, DICT_KEY_TYPE, DICT_KEY_GET},
(xpc_object_t []){xpc_uint64_create(0), xpc_uint64_create(6), XPC_BOOL_TRUE, xpc_uint64_create(DOMAIN_LEGACY), XPC_BOOL_TRUE},
5);
xpc_dictionary_set_mach_send(dict, DICT_KEY_DOMAINPORT, port);
xpc_object_t reply;
[self pipeRoutine:ROUTINE_MANAGER subsystem:6 reply:&reply error:nil];
if (reply) {
const char *manager_name = xpc_dictionary_get_string(reply, DICT_KEY_OUT);
if (manager_name) {
return [NSString stringWithUTF8String:manager_name];
}
}
return nil;
}
- (BOOL)kickstart:(NSString * _Nonnull)name domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
if (domain == DOMAIN_SYSTEM) {
handle = 0;
}
else if (domain == DOMAIN_GUI) {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_TYPE},
(xpc_object_t []){xpc_uint64_create(handle), xpc_uint64_create(DOMAIN_USER)},
2);
xpc_object_t reply;
if ([self pipeRoutine:ROUTINE_0x33b subsystem:3 reply:&reply error:&*error]) {
if (reply) {
uint64_t asid = xpc_dictionary_get_uint64(reply, DICT_KEY_ASID);
handle = asid;
domain = DOMAIN_LOGIN;
}
}
}
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_NAME, DICT_KEY_TYPE},
(xpc_object_t []){xpc_uint64_create(handle), xpc_string_create([name UTF8String]), xpc_uint64_create(domain)},
3);
xpc_object_t reply;
return [self pipeRoutine:ROUTINE_KICKSTART subsystem:2 reply:&reply error:&*error];
}
- (BOOL)bootstrap:(NSString * _Nonnull)path domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
return [self bootstrap:path bootout:FALSE domain:domain handle:handle error:&*error];
}
- (BOOL)bootout:(NSString * _Nonnull)path domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
return [self bootstrap:path bootout:TRUE domain:domain handle:handle error:&*error];
}
- (BOOL)bootstrap:(NSString * _Nonnull)path bootout:(BOOL)bootout domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
xpc_object_t paths = xpc_array_create((xpc_object_t []){xpc_string_create([path UTF8String])}, 1);
if (domain == DOMAIN_SYSTEM) {
handle = 0;
}
else if (domain == DOMAIN_GUI) {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_TYPE},
(xpc_object_t []){xpc_uint64_create(handle), xpc_uint64_create(DOMAIN_USER)},
2);
xpc_object_t reply;
if ([self pipeRoutine:ROUTINE_0x33b subsystem:3 reply:&reply error:&*error]) {
if (reply) {
uint64_t asid = xpc_dictionary_get_uint64(reply, DICT_KEY_ASID);
if (asid) {
handle = asid;
domain = DOMAIN_LOGIN;
}
}
}
}
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_PATHS, DICT_KEY_TYPE},
(xpc_object_t []){xpc_uint64_create(handle), paths, xpc_uint64_create(domain)},
3);
xpc_object_t reply;
return [self pipeRoutine:(bootout ? ROUTINE_UNLOAD : ROUTINE_LOAD) subsystem:3 reply:&reply error:&*error];
}
- (BOOL)load:(NSString * _Nonnull)path error:(NSError **)error {
return [self load:path unload:FALSE error:&*error];
}
- (BOOL)unload:(NSString * _Nonnull)path error:(NSError **)error {
return [self load:path unload:TRUE error:&*error];
}
- (BOOL)load:(NSString * _Nonnull)path unload:(BOOL)unload error:(NSError **)error {
xpc_object_t paths = xpc_array_create((xpc_object_t []){xpc_string_create([path UTF8String])}, 1);
dict = xpc_dictionary_create((const char *[]){DICT_KEY_PATHS, DICT_KEY_SESSION, (unload ? "disable" : "enable")},
(xpc_object_t []){paths, xpc_string_create("Aqua"), XPC_BOOL_TRUE},
3);
[self setLegacyValues];
xpc_object_t reply;
return [self pipeRoutine:(unload ? ROUTINE_UNLOAD : ROUTINE_LOAD) subsystem:3 reply:&reply error:&*error];
}
- (BOOL)enable:(NSString * _Nonnull)name domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
return [self enable:name disable:FALSE domain:domain handle:handle error:&*error];
}
- (BOOL)disable:(NSString * _Nonnull)name domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
return [self enable:name disable:TRUE domain:domain handle:handle error:&*error];
}
- (BOOL)enable:(NSString * _Nonnull)name disable:(BOOL)disable domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
xpc_object_t names = xpc_array_create((xpc_object_t []){xpc_string_create([name UTF8String])}, 1);
dict = xpc_dictionary_create((const char *[]){DICT_KEY_HANDLE, DICT_KEY_NAME, DICT_KEY_TYPE, DICT_KEY_NAMES},
(xpc_object_t []){xpc_uint64_create(handle), xpc_string_create([name UTF8String]), xpc_uint64_create(domain), names},
4);
xpc_object_t reply;
return [self pipeRoutine:(disable ? ROUTINE_DISABLE : ROUTINE_ENABLE) subsystem:3 reply:&reply error:&*error];
}
- (BOOL)kill:(NSString * _Nonnull)name signal:(int)signal domain:(domain_t)domain handle:(uint64_t)handle error:(NSError **)error {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_NAME, DICT_KEY_SIGNAL, DICT_KEY_HANDLE, DICT_KEY_TYPE},
(xpc_object_t []){xpc_string_create([name UTF8String]), xpc_int64_create(signal), xpc_uint64_create(handle), xpc_uint64_create(domain)},
4);
xpc_object_t reply;
return [self pipeRoutine:ROUTINE_KILL subsystem:3 reply:&reply error:&*error];
}
- (BOOL)start:(NSString * _Nonnull)name error:(NSError **)error {
return [self start:name stop:FALSE error:&*error];
}
- (BOOL)stop:(NSString * _Nonnull)name error:(NSError **)error {
return [self start:name stop:TRUE error:&*error];
}
- (BOOL)start:(NSString * _Nonnull)name stop:(BOOL)stop error:(NSError **)error {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_NAME},
(xpc_object_t []){xpc_string_create([name UTF8String])},
1);
[self setLegacyValues];
xpc_object_t reply;
return [self pipeRoutine:(stop ? ROUTINE_STOP : ROUTINE_START) subsystem:3 reply:&reply error:&*error];
}
- (NSArray <Service *>*)list {
dict = xpc_dictionary_create(NULL, NULL, 0);
[self setLegacyValues];
xpc_object_t reply;
[self pipeRoutine:ROUTINE_LIST subsystem:3 reply:&reply error:nil];
if (reply) {
xpc_object_t services = xpc_dictionary_get_value(reply, "services");
if (services) {
NSMutableArray <Service *>*servicesMutableArray = [NSMutableArray arrayWithCapacity:xpc_dictionary_get_count(services)];
xpc_dictionary_apply(services, ^bool(const char *key, xpc_object_t value) {
xpc_object_t service = xpc_dictionary_get_value(services, key);
if (service) {
Service *s = [Service new];
[s setLabel:[NSString stringWithUTF8String:key]];
[s setPid:xpc_dictionary_get_int64(service, "pid")];
[s setStatus:xpc_dictionary_get_int64(service, "status")];
[servicesMutableArray addObject:s];
}
return TRUE;
});
return [servicesMutableArray copy];
}
}
return nil;
}
- (BOOL)remove:(NSString * _Nonnull)name error:(NSError **)error {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_NAME},
(xpc_object_t []){xpc_string_create([name UTF8String])},
1);
[self setLegacyValues];
xpc_object_t reply;
return [self pipeRoutine:ROUTINE_REMOVE subsystem:3 reply:&reply error:&*error];
}
- (BOOL)setEnvironmentVariable:(NSString * _Nonnull)key value:(NSString *)value error:(NSError **)error {
xpc_object_t envvar = xpc_dictionary_create((const char *[]){[key UTF8String]},
(xpc_object_t []){(value ? xpc_string_create([value UTF8String]) : xpc_null_create())},
1);
dict = xpc_dictionary_create((const char *[]){"envvars"},
(xpc_object_t []){envvar},
1);
[self setLegacyValues];
xpc_object_t reply;
return [self pipeRoutine:ROUTINE_SETUNSETENV subsystem:3 reply:&reply error:&*error];
}
- (BOOL)unsetEnvironmentVariable:(NSString * _Nonnull)key error:(NSError **)error {
return [self setEnvironmentVariable:key value:nil error:&*error];
}
- (NSString *)getEnvironmentVariable:(NSString * _Nonnull)key error:(NSError **)error {
dict = xpc_dictionary_create((const char *[]){"envvar"},
(xpc_object_t []){xpc_string_create([key UTF8String])},
1);
[self setLegacyValues];
xpc_object_t reply;
if ([self pipeRoutine:ROUTINE_GETENV subsystem:3 reply:&reply error:&*error]) {
const char *value = xpc_dictionary_get_string(reply, DICT_KEY_VALUE);
if (value) {
return [NSString stringWithUTF8String:value];
}
}
return nil;
}
- (NSString *)version {
return [self versionVariant:"version"];
}
- (NSString *)variant {
return [self versionVariant:"variant"];
}
- (NSString *)versionVariant:(const char *)version_variant {
dict = xpc_dictionary_create((const char *[]){DICT_KEY_TYPE, DICT_KEY_HANDLE, version_variant},
(xpc_object_t []){xpc_uint64_create(DOMAIN_SYSTEM), xpc_uint64_create(0), XPC_BOOL_TRUE},
3);
int fd[2];
pipe(fd);
xpc_dictionary_set_fd(dict, DICT_KEY_FD, fd[1]);
NSFileHandle *fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd[0]];
xpc_object_t reply;
[self pipeRoutine:ROUTINE_VERSION subsystem:3 reply:&reply error:nil];
NSString *output = [[NSString alloc] initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
if (output) {
return output;
}
return nil;
}
- (BOOL)pipeRoutine:(int)routine subsystem:(int)subsystem reply:(xpc_object_t *)reply error:(NSError **)error {
if (error) {
*error = nil;
}
xpc_dictionary_set_uint64(dict, DICT_KEY_SUBSYSTEM, subsystem);
xpc_dictionary_set_uint64(dict, DICT_KEY_ROUTINE, routine);
#if LAUNCHCONTROL_LOGGING
NSLog(@"Message: %s", xpc_copy_description(dict));
#endif
int result = xpc_pipe_routine(global_data->xpc_bootstrap_pipe, dict, &*reply);
if (result == 0) {
#if LAUNCHCONTROL_LOGGING
NSLog(@"Reply: %s", xpc_copy_description(*reply));
#endif
int64_t error_code = xpc_dictionary_get_int64(*reply, DICT_KEY_ERROR);
if (error_code) {
#if LAUNCHCONTROL_LOGGING
NSLog(@"Error description: %s", xpc_strerror(error_code));
#endif
if (error) {
*error = [NSError errorWithDomain:@"com.haltepunkt.LaunchControl" code:error_code userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithUTF8String:xpc_strerror(error_code)]}];
}
return FALSE;
}
xpc_object_t errors_dict = xpc_dictionary_get_dictionary(*reply, DICT_KEY_ERRORS);
if (errors_dict) {
if (xpc_dictionary_get_count(errors_dict)) {
__block uint64_t code;
xpc_dictionary_apply(errors_dict, ^bool(const char * _Nonnull key, xpc_object_t _Nonnull value) {
code = xpc_dictionary_get_int64(errors_dict, key);
return FALSE;
});
if (code) {
#if LAUNCHCONTROL_LOGGING
NSLog(@"Error description: %s", xpc_strerror(error_code));
#endif
if (error) {
*error = [NSError errorWithDomain:@"com.haltepunkt.LaunchControl" code:code userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithUTF8String:xpc_strerror(code)]}];
}
}
}
return FALSE;
}
return TRUE;
}
return FALSE;
}
- (void)setLegacyValues {
xpc_dictionary_set_uint64(dict, DICT_KEY_TYPE, DOMAIN_LEGACY);
xpc_dictionary_set_uint64(dict, DICT_KEY_HANDLE, 0);
xpc_dictionary_set_mach_send(dict, DICT_KEY_DOMAINPORT, port);
xpc_dictionary_set_bool(dict, DICT_KEY_LEGACY, TRUE);
}
@end