-
Notifications
You must be signed in to change notification settings - Fork 0
/
ISSUtils.m
467 lines (365 loc) · 12.4 KB
/
ISSUtils.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#import "ISSUtils.h"
#import <mach/mach.h>
#import <servers/bootstrap.h>
@implementation ISSUMachPort {
BOOL _destroyMachPort;
CFMachPortRef _port;
CFRunLoopSourceRef _runLoopSource;
ISSUMachPortMessageCallBack _messageCallback;
void *_messageCallbackInfo;
ISSUMachPortInvalidationCallBack _invalidationCallback;
void *_invalidationCallbackInfo;
}
+ (instancetype) portForName: (NSString *) name {
mach_port_t machPort;
{
NSPort *port = [[NSMachBootstrapServer sharedInstance]
portForName: name
];
if (!port || ![port isKindOfClass: [NSMachPort class]])
return nil;
machPort = ((NSMachPort *) port).machPort;
// increase the send right reference count by one so that the mach port
// is not destroyed during invalidation of the owning NSMachPort instance
if (mach_port_mod_refs (mach_task_self (), machPort, MACH_PORT_RIGHT_SEND, 1) != KERN_SUCCESS)
return nil;
// make sure the mach port is not owned by the NSMachPort instance any more
[port invalidate];
}
ISSUMachPort *instance = [[self alloc] initWithMachPort: machPort];
if (!instance) {
// decrease the reference count to balance the earlier increase
mach_port_mod_refs (mach_task_self (), machPort, MACH_PORT_RIGHT_SEND, -1);
return nil;
}
return instance;
}
- (instancetype) init {
mach_port_t machPort;
if (mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &machPort) != KERN_SUCCESS)
return nil;
if (mach_port_insert_right (mach_task_self (), machPort, machPort, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)
goto error_exit;
if (self = [self initWithMachPort: machPort])
return self;
error_exit:
mach_port_destroy (mach_task_self (), machPort);
return nil;
}
- (instancetype) initWithMachPort: (mach_port_t) machPort {
if (self = [super init]) {
_machPort = machPort;
CFMachPortContext context;
memset (&context, 0, sizeof (context));
context.info = (__bridge void *) self;
if (!(_port = CFMachPortCreateWithPort (kCFAllocatorDefault, _machPort, &ISSUMachPortMessageHandler, &context, NULL)))
return nil;
CFMachPortGetContext (_port, &context);
if (context.info != (__bridge void *) self) {
// the mach port is owned by another CFMachPort instance - release it
// but don't invalidate
CFRelease (_port);
_port = NULL;
return nil;
}
_destroyMachPort = YES;
}
return self;
}
- (BOOL) registerName: (NSString *) name {
return [[NSMachBootstrapServer sharedInstance]
registerPort: [NSMachPort
portWithMachPort: _machPort
options: NSMachPortDeallocateNone
]
name: name
];
}
- (void) setMessageCallBack: (ISSUMachPortMessageCallBack) callback andInfo: (void *) info {
_messageCallback = callback;
_messageCallbackInfo = info;
}
- (void) setInvalidationCallBack: (ISSUMachPortInvalidationCallBack) callback andInfo: (void *) info {
_invalidationCallback = callback;
_invalidationCallbackInfo = info;
}
- (BOOL) scheduleInRunLoop: (CFRunLoopRef) runLoop forMode: (CFStringRef) mode {
if (!_runLoopSource && !(_runLoopSource = CFMachPortCreateRunLoopSource (kCFAllocatorDefault, _port, 0)))
return NO;
CFRunLoopAddSource (runLoop, _runLoopSource, mode);
return YES;
}
- (void) unschedule {
if (_runLoopSource) {
CFRunLoopSourceInvalidate (_runLoopSource);
CFRelease (_runLoopSource);
_runLoopSource = NULL;
}
}
- (void) dealloc {
if (_port) {
if (_runLoopSource) {
CFRunLoopSourceInvalidate (_runLoopSource);
CFRelease (_runLoopSource);
}
CFMachPortSetInvalidationCallBack (_port, NULL);
CFMachPortInvalidate (_port);
CFRelease (_port);
}
if (_destroyMachPort)
mach_port_destroy (mach_task_self (), _machPort);
}
static void ISSUMachPortMessageHandler (CFMachPortRef port, void *msg, CFIndex size, void *info) {
ISSUMachPort *instance = (__bridge ISSUMachPort *) info;
ISSUMachPortMessageCallBack callback = instance->_messageCallback;
if (callback)
callback (instance, msg, instance->_messageCallbackInfo);
else
NSLog (@"ISSUMachPort scheduled without callback.");
}
static void ISSUMachPortInvalidationHandler (CFMachPortRef port, void *info) {
ISSUMachPort *instance = (__bridge ISSUMachPort *) info;
ISSUMachPortInvalidationCallBack callback = instance->_invalidationCallback;
if (callback)
callback (instance, instance->_invalidationCallbackInfo);
}
@end
@implementation ISSUMachPortHolder {
ISSUMachPort *_port;
}
+ (instancetype) holderWithPort: (ISSUMachPort *) port {
return [[self alloc] initWithPort: port];
}
- (instancetype) initWithPort: (ISSUMachPort *) port {
if (self = [super init])
_port = port;
return self;
}
- (ISSUMachPort *) get {
ISSUMachPort *port = _port;
_port = nil;
return port;
}
- (void) set: (ISSUMachPort *) port {
_port = port;
}
@end
@interface ISSUSignalHandlerManager : NSObject
+ (instancetype) managerForSignalHandlerTableEntry: (ISSUSignalHandlerTableEntry *) entry;
- (instancetype) init NS_UNAVAILABLE;
- (instancetype) initForSignalHandlerTableEntry: (ISSUSignalHandlerTableEntry *) entry;
@end
@implementation ISSUSignalHandlerManager {
int _signum;
struct sigaction _action;
dispatch_source_t _dispatchSource;
}
+ (instancetype) managerForSignalHandlerTableEntry: (ISSUSignalHandlerTableEntry *) entry {
return [[self alloc] initForSignalHandlerTableEntry: entry];
}
- (instancetype) initForSignalHandlerTableEntry: (ISSUSignalHandlerTableEntry *) entry {
if (!entry)
return nil;
if (self = [super init]) {
int signum = entry->signum;
// setup a noop handler to ensure the signal is delivered
// and doesn't kill the process (note that using SIG_IGN
// instead of a noop handler doesn't work for at least the
// SIGCHLD signal)
{
struct sigaction action;
memset (&action, 0, sizeof (action));
action.sa_handler = &ISSUNoopHandler;
if (sigaction (signum, &action, &_action) != 0)
return nil;
}
_signum = signum; // dealloc resets the signal action if _signum is set
dispatch_source_t source = dispatch_source_create (DISPATCH_SOURCE_TYPE_SIGNAL, signum, 0, dispatch_get_main_queue ());
if (!source)
return nil;
dispatch_set_context (source, (uint8_t *) NULL + signum);
dispatch_source_set_event_handler_f (source, entry->handler);
dispatch_resume (source);
_dispatchSource = source;
}
return self;
}
- (void) dealloc {
if (_dispatchSource)
dispatch_source_cancel (_dispatchSource);
if (_signum)
sigaction (_signum, &_action, NULL);
}
static void ISSUNoopHandler (int signum) {
}
@end
NSArray *ISSUSetupSignalHandlers (ISSUSignalHandlerTableEntry signalHandlerTable[], int entryCount) {
@autoreleasepool {
NSMutableArray *managers = [NSMutableArray arrayWithCapacity: entryCount];
if (!managers)
return nil;
for (; entryCount > 0; --entryCount, ++signalHandlerTable) {
ISSUSignalHandlerManager *manager = [ISSUSignalHandlerManager managerForSignalHandlerTableEntry: signalHandlerTable];
if (!manager)
return nil;
[managers addObject: manager];
}
return managers;
}
}
NSURL *ISSUGetAbsoluteFileURL (char *filePath) {
NSURL *url = [NSURL
fileURLWithPath: NSFileManager.defaultManager.currentDirectoryPath
isDirectory: YES
];
if (!url)
return nil;
return [NSURL
fileURLWithFileSystemRepresentation: filePath
isDirectory: NO
relativeToURL: url
];
}
NSString *ISSUGetPerProcessName (char *name, pid_t pid) {
return [NSString stringWithFormat: @"%s.%d", name, pid];
}
mach_port_t ISSUGetBootstrapPort (void) {
mach_port_t bootstrapPort;
if (task_get_bootstrap_port (mach_task_self (), &bootstrapPort) != KERN_SUCCESS)
return MACH_PORT_NULL;
return bootstrapPort;
}
mach_port_t ISSUCreateBootstrapSubset (mach_port_t requestorPort) {
mach_port_t bootstrapPort = ISSUGetBootstrapPort ();
if (bootstrapPort == MACH_PORT_NULL)
return MACH_PORT_NULL;
mach_port_t subsetPort;
if (
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
bootstrap_subset
#pragma clang diagnostic pop
(
bootstrapPort, requestorPort, &subsetPort
) != BOOTSTRAP_SUCCESS
)
return MACH_PORT_NULL;
if (task_set_special_port (mach_task_self (), TASK_BOOTSTRAP_PORT, subsetPort) != KERN_SUCCESS) {
mach_port_destroy (mach_task_self (), subsetPort);
return MACH_PORT_NULL;
}
return bootstrapPort;
}
BOOL ISSUResetBootstrapPort (mach_port_t bootstrapPort) {
mach_port_t subsetPort = ISSUGetBootstrapPort ();
if (bootstrapPort == MACH_PORT_NULL)
return NO;
if (task_set_special_port (mach_task_self (), TASK_BOOTSTRAP_PORT, bootstrapPort) != KERN_SUCCESS)
return NO;
if (mach_port_destroy (mach_task_self (), subsetPort) != KERN_SUCCESS)
return NO;
return YES;
}
BOOL ISSUGetAuditToken (mach_msg_header_t *msg, audit_token_t **auditToken) {
mach_msg_trailer_t *trailer = (mach_msg_trailer_t *) (((uint8_t *) msg) + round_msg (msg->msgh_size));
if (trailer->msgh_trailer_type != MACH_MSG_TRAILER_FORMAT_0)
return NO;
if (trailer->msgh_trailer_size < sizeof (mach_msg_audit_trailer_t))
return NO;
*auditToken = &((mach_msg_audit_trailer_t *) trailer)->msgh_audit;
return YES;
}
mach_msg_descriptor_t *ISSUNextDescriptor (mach_msg_descriptor_t *current) {
mach_msg_descriptor_type_t type = current->type.type;
switch (type) {
case MACH_MSG_PORT_DESCRIPTOR:
return (mach_msg_descriptor_t *) (¤t->port + 1);
case MACH_MSG_OOL_DESCRIPTOR:
case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
return (mach_msg_descriptor_t *) (¤t->out_of_line + 1);
case MACH_MSG_OOL_PORTS_DESCRIPTOR:
return (mach_msg_descriptor_t *) (¤t->ool_ports + 1);
}
NSLog (@"Unknown descriptor type %u encountered, assuming maximum descriptor length.", type);
return current + 1;
}
BOOL ISSUPortRightsSend (ISSUMachPort *port, mach_port_t rightsArray[], int rightsCount) {
struct {
mach_msg_header_t header;
mach_msg_body_t body;
mach_msg_port_descriptor_t rights[0];
} *msg;
uint8_t buffer[sizeof (*msg) + sizeof (mach_msg_port_descriptor_t) * rightsCount];
memset (buffer, 0, sizeof (buffer));
msg = (void *) buffer;
msg->header.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
msg->header.msgh_size = sizeof (buffer);
msg->header.msgh_remote_port = port.machPort;
msg->header.msgh_local_port = MACH_PORT_NULL;
msg->body.msgh_descriptor_count = rightsCount;
for (mach_msg_port_descriptor_t *descriptor = msg->rights; rightsCount > 0; --rightsCount, ++rightsArray, ++descriptor) {
descriptor->name = *rightsArray;
descriptor->disposition = MACH_MSG_TYPE_COPY_SEND;
descriptor->type = MACH_MSG_PORT_DESCRIPTOR;
}
return (mach_msg_send (&msg->header) == KERN_SUCCESS);
}
int ISSUPortRightsReceive (mach_msg_header_t *msg, mach_port_t rightsArray[], int maxRightsCount) {
if (!(msg->msgh_bits & MACH_MSGH_BITS_COMPLEX))
return -1;
if (msg->msgh_size < sizeof (mach_msg_header_t) + sizeof (mach_msg_body_t))
return -1;
mach_msg_body_t *body = (mach_msg_body_t *) (msg + 1);
int count = body->msgh_descriptor_count;
void *end = ((uint8_t *) msg) + msg->msgh_size;
int remainingRightsCount = maxRightsCount;
for (
mach_msg_descriptor_t *next = (mach_msg_descriptor_t *) (body + 1)
;
remainingRightsCount > 0
;
--remainingRightsCount, ++rightsArray
) {
for (
mach_msg_descriptor_t *current = next
;
count > 0 && (--count, YES) && (void *) (¤t->type + 1) <= end && (void *) (next = ISSUNextDescriptor (current)) <= end
;
current = next
) {
if (current->type.type == MACH_MSG_PORT_DESCRIPTOR) {
*rightsArray = current->port.name;
goto continue_outer;
}
}
break;
continue_outer:
;
}
return (maxRightsCount - remainingRightsCount);
}
BOOL ISSUCommandSend (ISSUMachPort *port, int command) {
struct {
mach_msg_header_t header;
} *msg;
uint8_t buffer[sizeof (*msg)];
memset (buffer, 0, sizeof (buffer));
msg = (void *) buffer;
msg->header.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0);
msg->header.msgh_size = sizeof (buffer);
msg->header.msgh_remote_port = port.machPort;
msg->header.msgh_local_port = MACH_PORT_NULL;
msg->header.msgh_id = command;
return (mach_msg_send (&msg->header) == KERN_SUCCESS);
}
BOOL ISSUCommandReceive (mach_msg_header_t *msg, int *command) {
if (msg->msgh_size < sizeof (mach_msg_header_t))
return NO;
*command = msg->msgh_id;
return YES;
}
int main (int argc, char * const argv[]) {
@autoreleasepool {
return autoreleasedMain (argc, argv);
}
}