-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIOMIGMachPort.c
537 lines (436 loc) · 18.4 KB
/
IOMIGMachPort.c
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* Copyright (c) 2009 Apple, Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* IOMIGMachPort.c
*
* Created by Rob Yepez on 1/29/09.
* Copyright 2008 Apple Inc.. All rights reserved.
*
*/
#include <AssertMacros.h>
#include <CoreFoundation/CFRuntime.h>
#include <dispatch/private.h>
#include <pthread.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <servers/bootstrap.h>
#include "IOMIGMachPort.h"
#include <os/log.h>
typedef struct __IOMIGMachPort {
CFRuntimeBase cfBase; // base CFType information
CFRunLoopRef runLoop;
CFStringRef runLoopMode;
dispatch_queue_t dispatchQueue;
dispatch_mach_t dispatchChannel;
CFMachPortRef port;
CFRunLoopSourceRef source;
CFIndex maxMessageSize;
IOMIGMachPortDemuxCallback demuxCallback;
void * demuxRefcon;
IOMIGMachPortTerminationCallback terminationCallback;
void * terminationRefcon;
} __IOMIGMachPort, *__IOMIGMachPortRef;
static void __IOMIGMachPortRelease(CFTypeRef object);
static void __IOMIGMachPortRegister(void);
static void __IOMIGMachPortPortCallback(CFMachPortRef port, void *msg, CFIndex size __unused, void *info);
static Boolean __NoMoreSenders(mach_msg_header_t *request, mach_msg_header_t *reply);
static os_log_t __IOMIGMachPortLog();
static const CFRuntimeClass __IOMIGMachPortClass = {
0, // version
"IOMIGMachPort", // className
NULL, // init
NULL, // copy
__IOMIGMachPortRelease, // finalize
NULL, // equal
NULL, // hash
NULL, // copyFormattingDesc
NULL,
NULL,
NULL
};
static pthread_once_t __IOMIGMachPortTypeInit = PTHREAD_ONCE_INIT;
static CFTypeID __IOMIGMachPortTypeID = _kCFRuntimeNotATypeID;
static CFMutableDictionaryRef __ioPortCache = NULL;
static pthread_mutex_t __ioPortCacheLock = PTHREAD_MUTEX_INITIALIZER;
//------------------------------------------------------------------------------
// __IOMIGMachPortRegister
//------------------------------------------------------------------------------
void __IOMIGMachPortRegister(void)
{
__ioPortCache = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
__IOMIGMachPortTypeID = _CFRuntimeRegisterClass(&__IOMIGMachPortClass);
}
//------------------------------------------------------------------------------
// __IOMIGMachPortRelease
//------------------------------------------------------------------------------
void __IOMIGMachPortRelease(CFTypeRef object)
{
IOMIGMachPortRef migPort = (IOMIGMachPortRef)object;
if ( migPort->port ) {
CFMachPortInvalidate(migPort->port);
kern_return_t kr = mach_port_mod_refs(mach_task_self(),
CFMachPortGetPort(migPort->port),
MACH_PORT_RIGHT_RECEIVE,
-1);
if (kr != KERN_SUCCESS) {
os_log_error(__IOMIGMachPortLog(), "__IOMIGMachPortRelease mach_port_mod_refs:%s", mach_error_string(kr));
}
CFRelease(migPort->port);
}
if ( migPort->source ) {
CFRelease(migPort->source);
}
if ( migPort->dispatchChannel ) {
dispatch_mach_cancel(migPort->dispatchChannel);
dispatch_release(migPort->dispatchChannel);
migPort->dispatchChannel = NULL;
}
}
//------------------------------------------------------------------------------
// IOMIGMachPortGetTypeID
//------------------------------------------------------------------------------
CFTypeID IOMIGMachPortGetTypeID(void)
{
if ( _kCFRuntimeNotATypeID == __IOMIGMachPortTypeID )
pthread_once(&__IOMIGMachPortTypeInit, __IOMIGMachPortRegister);
return __IOMIGMachPortTypeID;
}
//------------------------------------------------------------------------------
// IOMIGMachPortCreate
//------------------------------------------------------------------------------
IOMIGMachPortRef IOMIGMachPortCreate(CFAllocatorRef allocator, CFIndex maxMessageSize, mach_port_t port)
{
IOMIGMachPortRef migPort = NULL;
void * offset = NULL;
uint32_t size;
require(maxMessageSize > 0, exit);
/* allocate service */
size = sizeof(__IOMIGMachPort) - sizeof(CFRuntimeBase);
migPort = ( IOMIGMachPortRef)_CFRuntimeCreateInstance(allocator, IOMIGMachPortGetTypeID(), size, NULL);
require(migPort, exit);
offset = migPort;
bzero(offset + sizeof(CFRuntimeBase), size);
CFMachPortContext context = {0, migPort, NULL, NULL, NULL};
migPort->port = (port != MACH_PORT_NULL) ?
CFMachPortCreateWithPort(allocator, port, __IOMIGMachPortPortCallback, &context, NULL) :
CFMachPortCreate(allocator, __IOMIGMachPortPortCallback, &context, NULL);
require(migPort->port, exit);
// The following code was previously active. It's redundant, and in fact, will always result in error.
// Receive rights can only ever have user ref count of 1. Attempting to modify the ref count of
// a receive right by a delta other than 0 or -1 will result in KERN_INVALID_VALUE from ipc_right_delta().
/*
mach_port_mod_refs(mach_task_self(),
CFMachPortGetPort(migPort->port),
MACH_PORT_RIGHT_RECEIVE,
1);
*/
migPort->maxMessageSize = maxMessageSize;
return migPort;
exit:
if ( migPort )
CFRelease(migPort);
return NULL;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// __IOMIGMachPortChannelCallback
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static void
__IOMIGMachPortChannelCallback(void *info, dispatch_mach_reason_t reason,
dispatch_mach_msg_t msg, __unused mach_error_t error)
{
IOMIGMachPortRef migPort = (IOMIGMachPortRef)info;
size_t size;
mach_msg_header_t *hdr;
switch (reason) {
case DISPATCH_MACH_MESSAGE_RECEIVED:
hdr = dispatch_mach_msg_get_msg(msg, &size);
__IOMIGMachPortPortCallback(migPort->port, hdr, size, migPort);
break;
case DISPATCH_MACH_CANCELED:
CFRelease(migPort);
break;
default:
break;
}
}
//------------------------------------------------------------------------------
// IOMIGMachPortScheduleWithDispatchQueue
//------------------------------------------------------------------------------
void IOMIGMachPortScheduleWithDispatchQueue(IOMIGMachPortRef migPort, dispatch_queue_t queue)
{
mach_port_t port = CFMachPortGetPort(migPort->port);
migPort->dispatchQueue = queue;
require(migPort->dispatchQueue, exit);
// init the sources
if ( !migPort->dispatchChannel ) {
dispatch_mach_t dm = dispatch_mach_create_f(dispatch_queue_get_label(queue),
queue, migPort, __IOMIGMachPortChannelCallback);
require(dm, exit);
migPort->dispatchChannel = dm;
CFRetain(migPort);
dispatch_mach_connect(dm, port, MACH_PORT_NULL, NULL);
}
exit:
return;
}
//------------------------------------------------------------------------------
// IOMIGMachPortUnscheduleFromDispatchQueue
//------------------------------------------------------------------------------
void IOMIGMachPortUnscheduleFromDispatchQueue(IOMIGMachPortRef migPort, dispatch_queue_t queue)
{
if ( !queue || !migPort->dispatchQueue)
return;
if ( queue != migPort->dispatchQueue )
return;
migPort->dispatchQueue = NULL;
if ( migPort->dispatchChannel ) {
dispatch_mach_cancel(migPort->dispatchChannel);
dispatch_release(migPort->dispatchChannel);
migPort->dispatchChannel = NULL;
}
}
//------------------------------------------------------------------------------
// IOMIGMachPortScheduleWithRunLoop
//------------------------------------------------------------------------------
void IOMIGMachPortScheduleWithRunLoop(IOMIGMachPortRef migPort, CFRunLoopRef runLoop, CFStringRef runLoopMode)
{
migPort->runLoop = runLoop;
migPort->runLoopMode = runLoopMode;
require(migPort->runLoop, exit);
require(migPort->runLoopMode, exit);
// init the sources
if ( !migPort->source ) {
migPort->source = CFMachPortCreateRunLoopSource(CFGetAllocator(migPort), migPort->port, 1);
require(migPort->source, exit);
}
CFRunLoopAddSource(runLoop, migPort->source, runLoopMode);
exit:
return;
}
//------------------------------------------------------------------------------
// IOMIGMachPortUnscheduleFromRunLoop
//------------------------------------------------------------------------------
void IOMIGMachPortUnscheduleFromRunLoop(IOMIGMachPortRef migPort, CFRunLoopRef runLoop, CFStringRef runLoopMode)
{
if ( !runLoop || !runLoopMode || !migPort->runLoop || !migPort->runLoopMode)
return;
if ( !CFEqual(runLoop, migPort->runLoop) || !CFEqual(runLoopMode, migPort->runLoopMode) )
return;
migPort->runLoop = NULL;
migPort->runLoopMode = NULL;
if ( migPort->source )
CFRunLoopRemoveSource(runLoop, migPort->source, runLoopMode);
}
//------------------------------------------------------------------------------
// IOMIGMachPortGetPort
//------------------------------------------------------------------------------
mach_port_t IOMIGMachPortGetPort(IOMIGMachPortRef migPort)
{
return CFMachPortGetPort(migPort->port);
}
//------------------------------------------------------------------------------
// IOMIGMachPortRegisterDemuxCallback
//------------------------------------------------------------------------------
void IOMIGMachPortRegisterDemuxCallback(IOMIGMachPortRef migPort, IOMIGMachPortDemuxCallback callback, void *refcon)
{
migPort->demuxCallback = callback;
migPort->demuxRefcon = refcon;
}
//------------------------------------------------------------------------------
// IOMIGMachPortRegisterTerminationCallback
//------------------------------------------------------------------------------
void IOMIGMachPortRegisterTerminationCallback(IOMIGMachPortRef migPort, IOMIGMachPortTerminationCallback callback, void *refcon)
{
migPort->terminationCallback = callback;
migPort->terminationRefcon = refcon;
}
static void
__IOMIGMachConsumeUnsentMessage(mach_msg_header_t *hdr)
{
mach_port_t port = hdr->msgh_local_port;
if (MACH_PORT_VALID(port)) {
switch (MACH_MSGH_BITS_LOCAL(hdr->msgh_bits)) {
case MACH_MSG_TYPE_MOVE_SEND:
case MACH_MSG_TYPE_MOVE_SEND_ONCE:
mach_port_deallocate(mach_task_self(), port);
break;
}
}
mach_msg_destroy(hdr);
}
//------------------------------------------------------------------------------
// __IOMIGMachPortPortCallback
//------------------------------------------------------------------------------
void __IOMIGMachPortPortCallback(CFMachPortRef port __unused, void *msg, CFIndex size __unused, void *info)
{
IOMIGMachPortRef migPort = (IOMIGMachPortRef)info;
mig_reply_error_t * bufRequest = msg;
mig_reply_error_t * bufReply = NULL;
mach_msg_return_t mr;
int options;
require(migPort, exit);
CFRetain(migPort);
bufReply = CFAllocatorAllocate(NULL, migPort->maxMessageSize, 0);
require(bufReply, exit);
// let's see if we have no more senders
if ( __NoMoreSenders(&bufRequest->Head, &bufReply->Head) ) {
if ( migPort->terminationCallback )
(*migPort->terminationCallback)(migPort, migPort->terminationRefcon);
else {
goto exit;
}
} else {
if ( migPort->demuxCallback )
(*migPort->demuxCallback)(migPort, &bufRequest->Head, &bufReply->Head, migPort->demuxRefcon);
else {
mach_msg_destroy(&bufRequest->Head);
goto exit;
}
}
if (!(bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
(bufReply->RetCode != KERN_SUCCESS)) {
//This return code is a little tricky -- it appears that the
//demux routine found an error of some sort, but since that
//error would not normally get returned either to the local
//user or the remote one, we pretend it's ok.
require(bufReply->RetCode != MIG_NO_REPLY, exit);
// destroy any out-of-line data in the request buffer but don't destroy
// the reply port right (since we need that to send an error message).
bufRequest->Head.msgh_remote_port = MACH_PORT_NULL;
mach_msg_destroy(&bufRequest->Head);
}
if (bufReply->Head.msgh_remote_port == MACH_PORT_NULL) {
//no reply port, so destroy the reply
if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
mach_msg_destroy(&bufReply->Head);
}
goto exit;
}
// send reply.
// We don't want to block indefinitely because the migPort
// isn't receiving messages from the reply port.
// If we have a send-once right for the reply port, then
// this isn't a concern because the send won't block.
// If we have a send right, we need to use MACH_SEND_TIMEOUT.
// To avoid falling off the kernel's fast RPC path unnecessarily,
// we only supply MACH_SEND_TIMEOUT when absolutely necessary.
options = MACH_SEND_MSG;
if (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) != MACH_MSG_TYPE_MOVE_SEND_ONCE) {
options |= MACH_SEND_TIMEOUT;
}
mr = mach_msg(&bufReply->Head,
options,
bufReply->Head.msgh_size,
0,
MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
// Has a message error occurred?
switch (mr) {
case MACH_SEND_INVALID_DEST:
case MACH_SEND_TIMED_OUT:
__IOMIGMachConsumeUnsentMessage(&bufReply->Head);
break;
default :
// Includes success case.
break;
}
exit:
if ( bufReply )
CFAllocatorDeallocate(NULL, bufReply);
if ( migPort )
CFRelease(migPort);
}
//------------------------------------------------------------------------------
// __NoMoreSenders
//------------------------------------------------------------------------------
Boolean __NoMoreSenders(mach_msg_header_t *request, mach_msg_header_t *reply)
{
mach_no_senders_notification_t *Request = (mach_no_senders_notification_t *)request;
mig_reply_error_t *Reply = (mig_reply_error_t *)reply;
reply->msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(request->msgh_bits), 0);
reply->msgh_remote_port = request->msgh_remote_port;
reply->msgh_size = sizeof(mig_reply_error_t); // Minimal size: update as needed
reply->msgh_local_port = MACH_PORT_NULL;
reply->msgh_id = request->msgh_id + 100;
if ((Request->not_header.msgh_id > MACH_NOTIFY_LAST) ||
(Request->not_header.msgh_id < MACH_NOTIFY_FIRST)) {
Reply->NDR = NDR_record;
Reply->RetCode = MIG_BAD_ID;
return FALSE; // if this is not a notification message
}
switch (Request->not_header.msgh_id) {
case MACH_NOTIFY_NO_SENDERS :
Reply->Head.msgh_bits = 0;
Reply->Head.msgh_remote_port = MACH_PORT_NULL;
Reply->RetCode = KERN_SUCCESS;
return TRUE;
default :
break;
}
Reply->NDR = NDR_record;
Reply->RetCode = MIG_BAD_ID;
return FALSE; // if this is not a notification we are handling
}
//------------------------------------------------------------------------------
//IOMIGMachPortCacheAdd
//------------------------------------------------------------------------------
void IOMIGMachPortCacheAdd(mach_port_t port, CFTypeRef server)
{
pthread_mutex_lock(&__ioPortCacheLock);
CFDictionarySetValue(__ioPortCache, (void *)(uintptr_t)port, server);
pthread_mutex_unlock(&__ioPortCacheLock);
}
//------------------------------------------------------------------------------
// IOMIGMachPortCacheRemove
//------------------------------------------------------------------------------
void IOMIGMachPortCacheRemove(mach_port_t port)
{
pthread_mutex_lock(&__ioPortCacheLock);
CFDictionaryRemoveValue(__ioPortCache, (void *)(uintptr_t)port);
pthread_mutex_unlock(&__ioPortCacheLock);
}
//------------------------------------------------------------------------------
// IOMIGMachPortCacheCopy
//------------------------------------------------------------------------------
CFTypeRef IOMIGMachPortCacheCopy(mach_port_t port)
{
CFTypeRef server;
pthread_mutex_lock(&__ioPortCacheLock);
server = (CFTypeRef)CFDictionaryGetValue(__ioPortCache, (void *)(uintptr_t)port);
if ( server ) CFRetain(server);
pthread_mutex_unlock(&__ioPortCacheLock);
return server;
}
//------------------------------------------------------------------------------
// __IOMIGMachPortLog
//------------------------------------------------------------------------------
os_log_t __IOMIGMachPortLog()
{
static os_log_t log;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
log = os_log_create("com.apple.iokit.iomigmachport", "default");
});
return log;
}