-
Notifications
You must be signed in to change notification settings - Fork 19
/
IOMIGMachPort.c
406 lines (327 loc) · 14 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
/*
* 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 <pthread.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <servers/bootstrap.h>
#include "IOMIGMachPort.h"
typedef struct __IOMIGMachPort {
CFRuntimeBase cfBase; // base CFType information
CFRunLoopRef runLoop;
CFStringRef runLoopMode;
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 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)
{
__IOMIGMachPortTypeID = _CFRuntimeRegisterClass(&__IOMIGMachPortClass);
__ioPortCache = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
}
//------------------------------------------------------------------------------
// __IOMIGMachPortRelease
//------------------------------------------------------------------------------
void __IOMIGMachPortRelease(CFTypeRef object)
{
IOMIGMachPortRef migPort = (IOMIGMachPortRef)object;
if ( migPort->port ) {
CFMachPortInvalidate(migPort->port);
CFRelease(migPort->port);
}
if ( migPort->source ) {
CFRelease(migPort->source);
}
}
//------------------------------------------------------------------------------
// 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(kCFAllocatorDefault, port, __IOMIGMachPortPortCallback, &context, NULL) :
CFMachPortCreate(kCFAllocatorDefault, __IOMIGMachPortPortCallback, &context, NULL);
require(migPort->port, exit);
migPort->maxMessageSize = maxMessageSize;
return migPort;
exit:
if ( migPort )
CFRelease(migPort);
return 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(kCFAllocatorDefault, 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;
}
//------------------------------------------------------------------------------
// __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:
// the reply can't be delivered, so destroy it
mach_msg_destroy(&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 *)port, server);
pthread_mutex_unlock(&__ioPortCacheLock);
}
//------------------------------------------------------------------------------
// IOMIGMachPortCacheRemove
//------------------------------------------------------------------------------
void IOMIGMachPortCacheRemove(mach_port_t port)
{
pthread_mutex_lock(&__ioPortCacheLock);
CFDictionaryRemoveValue(__ioPortCache, (void *)port);
pthread_mutex_unlock(&__ioPortCacheLock);
}
//------------------------------------------------------------------------------
// IOMIGMachPortCacheCopy
//------------------------------------------------------------------------------
CFTypeRef IOMIGMachPortCacheCopy(mach_port_t port)
{
CFTypeRef server;
pthread_mutex_lock(&__ioPortCacheLock);
server = (CFTypeRef)CFDictionaryGetValue(__ioPortCache, (void *)port);
if ( server ) CFRetain(server);
pthread_mutex_unlock(&__ioPortCacheLock);
return server;
}