static void __CFMessagePortReplyCallBack(CFMachPortRef port, void *msg, CFIndex size, void *info) { CFMessagePortRef ms = info; struct __CFMessagePortMachMessage *msgp = msg; struct __CFMessagePortMachMessage *replymsg; __CFMessagePortLock(ms); if (!__CFMessagePortIsValid(ms)) { __CFMessagePortUnlock(ms); return; } // assert: (int32_t)msgp->head.msgh_id < 0 if (CFDictionaryContainsKey(ms->_replies, (void *)msgp->head.msgh_id)) { CFDataRef reply = NULL; replymsg = (struct __CFMessagePortMachMessage *)msg; if (0 == replymsg->body.msgh_descriptor_count) { int32_t byteslen = CFSwapInt32LittleToHost(replymsg->contents.msg0.byteslen); if (0 <= byteslen) { reply = CFDataCreate(kCFAllocatorSystemDefault, replymsg->contents.msg0.bytes, byteslen); } else { reply = (void *)0xffffffff; // means NULL data } } else { //#warning CF: should create a no-copy data here that has a custom VM-freeing allocator, and not vm_dealloc here reply = CFDataCreate(kCFAllocatorSystemDefault, replymsg->contents.msg1.desc.out_of_line.address, replymsg->contents.msg1.desc.out_of_line.size); vm_deallocate(mach_task_self(), (vm_address_t)replymsg->contents.msg1.desc.out_of_line.address, replymsg->contents.msg1.desc.out_of_line.size); } CFDictionarySetValue(ms->_replies, (void *)msgp->head.msgh_id, (void *)reply); } else { /* discard message */ if (1 == msgp->body.msgh_descriptor_count) { vm_deallocate(mach_task_self(), (vm_address_t)msgp->contents.msg1.desc.out_of_line.address, msgp->contents.msg1.desc.out_of_line.size); } } __CFMessagePortUnlock(ms); }
CFRunLoopSourceRef CFMessagePortCreateRunLoopSource(CFAllocatorRef allocator, CFMessagePortRef ms, CFIndex order) { CFRunLoopSourceRef result = NULL; __CFGenericValidateType(ms, __kCFMessagePortTypeID); //#warning CF: This should be an assert // if (__CFMessagePortIsRemote(ms)) return NULL; __CFMessagePortLock(ms); if (NULL == ms->_source && __CFMessagePortIsValid(ms)) { CFRunLoopSourceContext1 context; context.version = 1; context.info = (void *)ms; context.retain = (const void *(*)(const void *))CFRetain; context.release = (void (*)(const void *))CFRelease; context.copyDescription = (CFStringRef (*)(const void *))__CFMessagePortCopyDescription; context.equal = (Boolean (*)(const void *, const void *))__CFMessagePortEqual; context.hash = (CFHashCode (*)(const void *))__CFMessagePortHash; context.getPort = __CFMessagePortGetPort; context.perform = __CFMessagePortPerform; ms->_source = CFRunLoopSourceCreate(allocator, order, (CFRunLoopSourceContext *)&context); } if (NULL != ms->_source) { result = (CFRunLoopSourceRef)CFRetain(ms->_source); } __CFMessagePortUnlock(ms); return result; }
void CFMessagePortInvalidate(CFMessagePortRef ms) { __CFGenericValidateType(ms, __kCFMessagePortTypeID); if (!__CFMessagePortIsDeallocing(ms)) { CFRetain(ms); } __CFMessagePortLock(ms); if (__CFMessagePortIsValid(ms)) { CFMessagePortInvalidationCallBack callout = ms->_icallout; CFRunLoopSourceRef source = ms->_source; CFMachPortRef replyPort = ms->_replyPort; CFMachPortRef port = ms->_port; CFStringRef name = ms->_name; void *info = NULL; __CFMessagePortUnsetValid(ms); if (!__CFMessagePortIsRemote(ms)) { info = ms->_context.info; ms->_context.info = NULL; } ms->_source = NULL; ms->_replyPort = NULL; __CFMessagePortUnlock(ms); __CFSpinLock(&__CFAllMessagePortsLock); if (NULL != (__CFMessagePortIsRemote(ms) ? __CFAllRemoteMessagePorts : __CFAllLocalMessagePorts)) { CFDictionaryRemoveValue(__CFMessagePortIsRemote(ms) ? __CFAllRemoteMessagePorts : __CFAllLocalMessagePorts, name); } __CFSpinUnlock(&__CFAllMessagePortsLock); if (NULL != callout) { callout(ms, info); } // We already know we're going invalid, don't need this callback // anymore; plus, this solves a reentrancy deadlock; also, this // must be done before the deallocate of the Mach port, to // avoid a race between the notification message which could be // handled in another thread, and this NULL'ing out. CFMachPortSetInvalidationCallBack(port, NULL); // For hashing and equality purposes, cannot get rid of _port here if (!__CFMessagePortIsRemote(ms) && NULL != ms->_context.release) { ms->_context.release(info); } if (NULL != source) { CFRunLoopSourceInvalidate(source); CFRelease(source); } if (NULL != replyPort) { CFMachPortInvalidate(replyPort); CFRelease(replyPort); } if (__CFMessagePortIsRemote(ms)) { // Get rid of our extra ref on the Mach port gotten from bs server mach_port_deallocate(mach_task_self(), CFMachPortGetPort(port)); } } else { __CFMessagePortUnlock(ms); } if (!__CFMessagePortIsDeallocing(ms)) { CFRelease(ms); } }
void CFMessagePortSetInvalidationCallBack(CFMessagePortRef ms, CFMessagePortInvalidationCallBack callout) { __CFGenericValidateType(ms, __kCFMessagePortTypeID); if (!__CFMessagePortIsValid(ms) && NULL != callout) { callout(ms, ms->_context.info); } else { ms->_icallout = callout; } }
Boolean CFMessagePortIsValid(CFMessagePortRef ms) { __CFGenericValidateType(ms, __kCFMessagePortTypeID); if (!__CFMessagePortIsValid(ms)) return false; if (NULL != ms->_port && !CFMachPortIsValid(ms->_port)) { CFMessagePortInvalidate(ms); return false; } if (NULL != ms->_replyPort && !CFMachPortIsValid(ms->_replyPort)) { CFMessagePortInvalidate(ms); return false; } if (NULL != ms->_source && !CFRunLoopSourceIsValid(ms->_source)) { CFMessagePortInvalidate(ms); return false; } return true; }
static void *__CFMessagePortPerform(void *msg, CFIndex size, CFAllocatorRef allocator, void *info) { CFMessagePortRef ms = info; struct __CFMessagePortMachMessage *msgp = msg; struct __CFMessagePortMachMessage *replymsg; void *context_info; void (*context_release)(const void *); CFDataRef returnData, data = NULL; void *return_bytes = NULL; CFIndex return_len = 0; int32_t msgid; __CFMessagePortLock(ms); if (!__CFMessagePortIsValid(ms)) { __CFMessagePortUnlock(ms); return NULL; } // assert: 0 < (int32_t)msgp->head.msgh_id if (NULL != ms->_context.retain) { context_info = (void *)ms->_context.retain(ms->_context.info); context_release = ms->_context.release; } else { context_info = ms->_context.info; context_release = NULL; } __CFMessagePortUnlock(ms); /* Create no-copy, no-free-bytes wrapper CFData */ if (0 == msgp->body.msgh_descriptor_count) { int32_t byteslen = CFSwapInt32LittleToHost(msgp->contents.msg0.byteslen); msgid = CFSwapInt32LittleToHost(msgp->contents.msg0.msgid); if (0 <= byteslen) { data = CFDataCreateWithBytesNoCopy(kCFAllocatorSystemDefault, msgp->contents.msg0.bytes, byteslen, kCFAllocatorNull); } } else { msgid = CFSwapInt32LittleToHost(msgp->contents.msg1.msgid); data = CFDataCreateWithBytesNoCopy(kCFAllocatorSystemDefault, msgp->contents.msg1.desc.out_of_line.address, msgp->contents.msg1.desc.out_of_line.size, kCFAllocatorNull); } returnData = ms->_callout(ms, msgid, data, context_info); /* Now, returnData could be (1) NULL, (2) an ordinary data < MAX_INLINE, (3) ordinary data >= MAX_INLINE, (4) a no-copy data < MAX_INLINE, (5) a no-copy data >= MAX_INLINE. In cases (2) and (4), we send the return bytes inline in the Mach message, so can release the returnData object here. In cases (3) and (5), we'll send the data out-of-line, we need to create a copy of the memory, which we'll have the kernel autodeallocate for us on send. In case (4) also, the bytes in the return data may be part of the bytes in "data" that we sent into the callout, so if the incoming data was received out of line, we wouldn't be able to clean up the out-of-line wad until the message was sent either, if we didn't make the copy. */ if (NULL != returnData) { return_len = CFDataGetLength(returnData); if (return_len < __CFMessagePortMaxInlineBytes) { return_bytes = (void *)CFDataGetBytePtr(returnData); } else { return_bytes = NULL; vm_allocate(mach_task_self(), (vm_address_t *)&return_bytes, return_len, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_MACH_MSG)); /* vm_copy would only be a win here if the source address is page aligned; it is a lose in all other cases, since the kernel will just do the memmove for us (but not in as simple a way). */ memmove(return_bytes, CFDataGetBytePtr(returnData), return_len); } } replymsg = __CFMessagePortCreateMessage(allocator, true, msgp->head.msgh_remote_port, MACH_PORT_NULL, -1 * (int32_t)msgp->head.msgh_id, msgid, return_bytes, return_len); if (1 == replymsg->body.msgh_descriptor_count) { replymsg->contents.msg1.desc.out_of_line.deallocate = true; } if (data) CFRelease(data); if (1 == msgp->body.msgh_descriptor_count) { vm_deallocate(mach_task_self(), (vm_address_t)msgp->contents.msg1.desc.out_of_line.address, msgp->contents.msg1.desc.out_of_line.size); } if (returnData) CFRelease(returnData); if (context_release) { context_release(context_info); } return replymsg; }
SInt32 CFMessagePortSendRequest(CFMessagePortRef remote, SInt32 msgid, CFDataRef data, CFTimeInterval sendTimeout, CFTimeInterval rcvTimeout, CFStringRef replyMode, CFDataRef *returnDatap) { struct __CFMessagePortMachMessage *sendmsg; CFRunLoopRef currentRL = CFRunLoopGetCurrent(); CFRunLoopSourceRef source = NULL; CFDataRef reply = NULL; int64_t termTSR; uint32_t sendOpts = 0, sendTimeOut = 0; int32_t desiredReply; Boolean didRegister = false; kern_return_t ret; //#warning CF: This should be an assert // if (!__CFMessagePortIsRemote(remote)) return -999; if (!__CFMessagePortIsValid(remote)) return kCFMessagePortIsInvalid; __CFMessagePortLock(remote); if (NULL == remote->_replyPort) { CFMachPortContext context; context.version = 0; context.info = remote; context.retain = (const void *(*)(const void *))CFRetain; context.release = (void (*)(const void *))CFRelease; context.copyDescription = (CFStringRef (*)(const void *))__CFMessagePortCopyDescription; remote->_replyPort = CFMachPortCreate(CFGetAllocator(remote), __CFMessagePortReplyCallBack, &context, NULL); } remote->_convCounter++; desiredReply = -remote->_convCounter; CFDictionarySetValue(remote->_replies, (void *)desiredReply, NULL); sendmsg = __CFMessagePortCreateMessage(CFGetAllocator(remote), false, CFMachPortGetPort(remote->_port), (replyMode != NULL ? CFMachPortGetPort(remote->_replyPort) : MACH_PORT_NULL), -desiredReply, msgid, (data ? CFDataGetBytePtr(data) : NULL), (data ? CFDataGetLength(data) : 0)); __CFMessagePortUnlock(remote); if (replyMode != NULL) { source = CFMachPortCreateRunLoopSource(CFGetAllocator(remote), remote->_replyPort, -100); didRegister = !CFRunLoopContainsSource(currentRL, source, replyMode); if (didRegister) { CFRunLoopAddSource(currentRL, source, replyMode); } } if (sendTimeout < 10.0*86400) { // anything more than 10 days is no timeout! sendOpts = MACH_SEND_TIMEOUT; sendTimeout *= 1000.0; if (sendTimeout < 1.0) sendTimeout = 0.0; sendTimeOut = floor(sendTimeout); } ret = mach_msg((mach_msg_header_t *)sendmsg, MACH_SEND_MSG|sendOpts, sendmsg->head.msgh_size, 0, MACH_PORT_NULL, sendTimeOut, MACH_PORT_NULL); CFAllocatorDeallocate(CFGetAllocator(remote), sendmsg); if (KERN_SUCCESS != ret) { if (didRegister) { CFRunLoopRemoveSource(currentRL, source, replyMode); CFRelease(source); } if (MACH_SEND_TIMED_OUT == ret) return kCFMessagePortSendTimeout; return kCFMessagePortTransportError; } if (replyMode == NULL) { return kCFMessagePortSuccess; } CFRetain(remote); // retain during run loop to avoid invalidation causing freeing _CFMachPortInstallNotifyPort(currentRL, replyMode); termTSR = mach_absolute_time() + __CFTimeIntervalToTSR(rcvTimeout); for (;;) { CFRunLoopRunInMode(replyMode, __CFTSRToTimeInterval(termTSR - mach_absolute_time()), true); // warning: what, if anything, should be done if remote is now invalid? reply = CFDictionaryGetValue(remote->_replies, (void *)desiredReply); if (NULL != reply || termTSR < (int64_t)mach_absolute_time()) { break; } if (!CFMessagePortIsValid(remote)) { // no reason that reply port alone should go invalid so we don't check for that break; } } // Should we uninstall the notify port? A complex question... if (didRegister) { CFRunLoopRemoveSource(currentRL, source, replyMode); CFRelease(source); } if (NULL == reply) { CFDictionaryRemoveValue(remote->_replies, (void *)desiredReply); CFRelease(remote); return CFMessagePortIsValid(remote) ? kCFMessagePortReceiveTimeout : -5; } if (NULL != returnDatap) { *returnDatap = ((void *)0xffffffff == reply) ? NULL : reply; } else if ((void *)0xffffffff != reply) { CFRelease(reply); } CFDictionaryRemoveValue(remote->_replies, (void *)desiredReply); CFRelease(remote); return kCFMessagePortSuccess; }
static CFStringRef __CFMessagePortCopyDescription(CFTypeRef cf) { CFMessagePortRef ms = (CFMessagePortRef)cf; CFStringRef result; const char *locked; CFStringRef contextDesc = NULL; locked = ms->_lock ? "Yes" : "No"; if (!__CFMessagePortIsRemote(ms)) { if (NULL != ms->_context.info && NULL != ms->_context.copyDescription) { contextDesc = ms->_context.copyDescription(ms->_context.info); } if (NULL == contextDesc) { contextDesc = CFStringCreateWithFormat(CFGetAllocator(ms), NULL, CFSTR("<CFMessagePort context %p>"), ms->_context.info); } result = CFStringCreateWithFormat(CFGetAllocator(ms), NULL, CFSTR("<CFMessagePort %p [%p]>{locked = %s, valid = %s, remote = %s, name = %@}"), cf, CFGetAllocator(ms), locked, (__CFMessagePortIsValid(ms) ? "Yes" : "No"), (__CFMessagePortIsRemote(ms) ? "Yes" : "No"), ms->_name); } else { result = CFStringCreateWithFormat(CFGetAllocator(ms), NULL, CFSTR("<CFMessagePort %p [%p]>{locked = %s, valid = %s, remote = %s, name = %@, source = %p, callout = %p, context = %@}"), cf, CFGetAllocator(ms), locked, (__CFMessagePortIsValid(ms) ? "Yes" : "No"), (__CFMessagePortIsRemote(ms) ? "Yes" : "No"), ms->_name, ms->_source, ms->_callout, (NULL != contextDesc ? contextDesc : CFSTR("<no description>"))); } if (NULL != contextDesc) { CFRelease(contextDesc); } return result; }
static CFStringRef __CFMessagePortCopyDescription(CFTypeRef cf) { CFMessagePortRef ms = (CFMessagePortRef)cf; CFStringRef result; const char *locked; CFStringRef contextDesc = NULL; locked = "Maybe"; if (__CFMessagePortIsRemote(ms)) { result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFMessagePort %p [%p]>{locked = %s, valid = %s, remote = %s, name = %@}"), cf, CFGetAllocator(ms), locked, (__CFMessagePortIsValid(ms) ? "Yes" : "No"), (__CFMessagePortIsRemote(ms) ? "Yes" : "No"), ms->_name); } else { if (NULL != ms->_context.info && NULL != ms->_context.copyDescription) { contextDesc = ms->_context.copyDescription(ms->_context.info); } if (NULL == contextDesc) { contextDesc = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFMessagePort context %p>"), ms->_context.info); } void *addr = ms->_callout ? (void *)ms->_callout : (void *)ms->_calloutEx; Dl_info info; const char *name = (dladdr(addr, &info) && info.dli_saddr == addr && info.dli_sname) ? info.dli_sname : "???"; result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFMessagePort %p [%p]>{locked = %s, valid = %s, remote = %s, name = %@, source = %p, callout = %s (%p), context = %@}"), cf, CFGetAllocator(ms), locked, (__CFMessagePortIsValid(ms) ? "Yes" : "No"), (__CFMessagePortIsRemote(ms) ? "Yes" : "No"), ms->_name, ms->_source, name, addr, (NULL != contextDesc ? contextDesc : CFSTR("<no description>"))); } if (NULL != contextDesc) { CFRelease(contextDesc); } return result; }