+#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
+// The entry point is in libSystem.B.dylib, but not actually declared
+// If this becomes available in a header (<rdar://problem/4943036>), I need to pull this out
+int gethostuuid(unsigned char *uuid_buf, const struct timespec *timeoutp);
+
+__private_extern__ CFStringRef _CFGetHostUUIDString(void) {
+ static CFStringRef __hostUUIDString = NULL;
+
+ if (!__hostUUIDString) {
+ CFUUIDBytes uuidBytes;
+ int getuuidErr = 0;
+ struct timespec timeout = {0, 0}; // Infinite timeout for gethostuuid()
+
+ getuuidErr = gethostuuid((unsigned char *)&uuidBytes, &timeout);
+ if (getuuidErr == -1) {
+ // An error has occurred trying to get the host UUID string. There's nothing we can do here, so we should just return NULL.
+ CFLog(kCFLogLevelWarning, CFSTR("_CFGetHostUUIDString: unable to determine UUID for host. Error: %d"), errno);
+ return NULL;
+ }
+
+ CFUUIDRef uuidRef = CFUUIDCreateFromUUIDBytes(kCFAllocatorSystemDefault, uuidBytes);
+ CFStringRef uuidAsString = CFUUIDCreateString(kCFAllocatorSystemDefault, uuidRef);
+
+ if (!OSAtomicCompareAndSwapPtrBarrier(NULL, (void *)uuidAsString, (void *)&__hostUUIDString)) {
+ CFRelease(uuidAsString); // someone else made the assignment, so just release the extra string.
+ }
+
+ CFRelease(uuidRef);
+ }
+
+ return __hostUUIDString;
+}
+
+__private_extern__ CFStringRef _CFPreferencesGetByHostIdentifierString(void) {
+ static CFStringRef __byHostIdentifierString = NULL;
+
+ if (!__byHostIdentifierString) {
+ CFStringRef hostID = _CFGetHostUUIDString();
+ if (hostID) {
+ if (CFStringHasPrefix(hostID, CFSTR("00000000-0000-1000-8000-"))) {
+ // If the host UUID is prefixed by "00000000-0000-1000-8000-" then the UUID returned is the "compatible" type. The last field of the string will be the MAC address of the primary ethernet interface of the computer. We use this for compatibility with existing by-host preferences.
+ CFStringRef lastField = CFStringCreateWithSubstring(kCFAllocatorSystemDefault, hostID, CFRangeMake(24, 12));
+ CFMutableStringRef tmpstr = CFStringCreateMutableCopy(kCFAllocatorSystemDefault, 0, lastField);
+ CFStringLowercase(tmpstr, NULL);
+ CFStringRef downcasedField = CFStringCreateCopy(kCFAllocatorSystemDefault, tmpstr);
+
+ if (!OSAtomicCompareAndSwapPtrBarrier(NULL, (void *)downcasedField, (void *)&__byHostIdentifierString)) {
+ CFRelease(downcasedField);
+ }
+
+ CFRelease(tmpstr);
+ CFRelease(lastField);
+ } else {
+ // The host UUID is a full UUID, and we should just use that. This doesn't involve any additional string creation, so we should just be able to do the assignment.
+ __byHostIdentifierString = hostID;
+ }
+ } else {
+ __byHostIdentifierString = CFSTR("UnknownHostID");
+ }
+ }
+
+ return __byHostIdentifierString;
+}
+
+#else