2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
34 #define SC_LOG_HANDLE _SC_LOG_DEFAULT()
35 #include <SystemConfiguration/SystemConfiguration.h>
36 #include <SystemConfiguration/SCValidation.h>
37 #include <SystemConfiguration/SCPrivate.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
44 #include <net/if_dl.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #include <dispatch/dispatch.h>
50 #include <mach/mach.h>
51 #include <mach/notify.h>
52 #include <mach/mach_error.h>
60 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !defined(DO_NOT_INFORM)
61 #include <CoreFoundation/CFUserNotification.h>
62 #endif // TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !defined(DO_NOT_INFORM)
64 /* CrashReporter "Application Specific Information" */
65 #include <CrashReporterClient.h>
71 #pragma mark Miscellaneous
74 __private_extern__
char *
75 _SC_cfstring_to_cstring_ext(CFStringRef cfstr
, char *buf
, CFIndex bufLen
, CFStringEncoding encoding
, UInt8 lossByte
, CFIndex
*usedBufLen
)
84 len
= CFStringGetLength(cfstr
);
86 /* how much buffer space will we really need? */
87 converted
= CFStringGetBytes(cfstr
,
95 if (converted
< len
) {
96 /* if full string could not be converted */
104 if (bufLen
< (last
+ 1)) {
105 /* if the size of the provided buffer is too small */
110 /* allocate a buffer */
112 buf
= CFAllocatorAllocate(NULL
, bufLen
, 0);
118 (void)CFStringGetBytes(cfstr
,
128 if (usedBufLen
!= NULL
) {
137 _SC_cfstring_to_cstring(CFStringRef cfstr
, char *buf
, CFIndex bufLen
, CFStringEncoding encoding
)
139 return _SC_cfstring_to_cstring_ext(cfstr
, buf
, bufLen
, encoding
, 0, NULL
);
145 _SC_sockaddr_to_string(const struct sockaddr
*address
, char *buf
, size_t bufLen
)
148 const struct sockaddr
*sa
;
149 const struct sockaddr_in
*sin
;
150 const struct sockaddr_in6
*sin6
;
151 const struct sockaddr_dl
*sdl
;
157 switch (address
->sa_family
) {
159 (void)inet_ntop(addr
.sin
->sin_family
,
165 (void)inet_ntop(addr
.sin6
->sin6_family
,
166 &addr
.sin6
->sin6_addr
,
169 if (addr
.sin6
->sin6_scope_id
!= 0) {
173 if ((n
+IF_NAMESIZE
+1) <= bufLen
) {
175 if_indextoname(addr
.sin6
->sin6_scope_id
, &buf
[n
]);
181 snprintf(buf
, bufLen
, "unexpected address family %d", address
->sa_family
);
190 _SC_string_to_sockaddr(const char *str
, sa_family_t af
, void *buf
, size_t bufLen
)
195 struct sockaddr_in
*sin
;
196 struct sockaddr_in6
*sin6
;
200 bufLen
= sizeof(struct sockaddr_storage
);
201 addr
.buf
= CFAllocatorAllocate(NULL
, bufLen
, 0);
206 bzero(addr
.buf
, bufLen
);
207 if (((af
== AF_UNSPEC
) || (af
== AF_INET
)) &&
208 (bufLen
>= sizeof(struct sockaddr_in
)) &&
209 inet_aton(str
, &addr
.sin
->sin_addr
) == 1) {
211 addr
.sin
->sin_len
= sizeof(struct sockaddr_in
);
212 addr
.sin
->sin_family
= AF_INET
;
213 } else if (((af
== AF_UNSPEC
) || (af
== AF_INET6
)) &&
214 (bufLen
>= sizeof(struct sockaddr_in6
)) &&
215 inet_pton(AF_INET6
, str
, &addr
.sin6
->sin6_addr
) == 1) {
219 addr
.sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
220 addr
.sin6
->sin6_family
= AF_INET6
;
222 p
= strchr(str
, '%');
224 addr
.sin6
->sin6_scope_id
= if_nametoindex(p
+ 1);
227 if (IN6_IS_ADDR_LINKLOCAL(&addr
.sin6
->sin6_addr
) ||
228 IN6_IS_ADDR_MC_LINKLOCAL(&addr
.sin6
->sin6_addr
)) {
231 if_index
= ntohs(addr
.sin6
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
232 addr
.sin6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
233 if (addr
.sin6
->sin6_scope_id
== 0) {
234 // use the scope id that was embedded in the [link local] IPv6 address
235 addr
.sin6
->sin6_scope_id
= if_index
;
239 if (addr
.buf
!= buf
) {
240 CFAllocatorDeallocate(NULL
, addr
.buf
);
250 _SC_sendMachMessage(mach_port_t port
, mach_msg_id_t msg_id
)
252 mach_msg_empty_send_t msg
;
253 mach_msg_option_t options
;
254 kern_return_t status
;
256 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
257 msg
.header
.msgh_size
= sizeof(msg
);
258 msg
.header
.msgh_remote_port
= port
;
259 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
260 msg
.header
.msgh_id
= msg_id
;
261 options
= MACH_SEND_TIMEOUT
;
262 status
= mach_msg(&msg
.header
, /* msg */
263 MACH_SEND_MSG
|options
, /* options */
264 msg
.header
.msgh_size
, /* send_size */
266 MACH_PORT_NULL
, /* rcv_name */
268 MACH_PORT_NULL
); /* notify */
269 if (status
!= MACH_MSG_SUCCESS
) {
270 mach_msg_destroy(&msg
.header
);
278 _SC_trimDomain(CFStringRef domain
)
282 if (!isA_CFString(domain
)) {
286 // remove any leading/trailing dots
287 length
= CFStringGetLength(domain
);
289 (CFStringFindWithOptions(domain
,
294 CFStringFindWithOptions(domain
,
296 CFRangeMake(0, length
),
297 kCFCompareAnchored
|kCFCompareBackwards
,
300 CFMutableStringRef trimmed
;
302 trimmed
= CFStringCreateMutableCopy(NULL
, 0, domain
);
303 CFStringTrim(trimmed
, CFSTR("."));
304 domain
= (CFStringRef
)trimmed
;
305 length
= CFStringGetLength(domain
);
320 _SC_hw_model(Boolean trim
)
322 static CFStringRef model
= NULL
;
323 static CFStringRef model_trimmed
= NULL
;
324 static dispatch_once_t once
;
326 dispatch_once(&once
, ^{
329 int mib
[] = { CTL_HW
, HW_MODEL
};
330 size_t n
= sizeof(hwModel
);
334 bzero(&hwModel
, sizeof(hwModel
));
335 ret
= sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), &hwModel
, &n
, NULL
, 0);
337 SC_log(LOG_NOTICE
, "sysctl() CTL_HW/HW_MODEL failed: %s", strerror(errno
));
340 hwModel
[sizeof(hwModel
) - 1] = '\0';
341 model
= CFStringCreateWithCString(NULL
, hwModel
, kCFStringEncodingASCII
);
343 // and the "trimmed" name
344 // ... remove everything after (and including) a comma
345 // ... and then any trailing digits
346 cp
= index(hwModel
, ',');
350 n
= strlen(hwModel
) - 1;
352 if (!isdigit(hwModel
[n
])) {
357 model_trimmed
= CFStringCreateWithCString(NULL
, hwModel
, kCFStringEncodingASCII
);
360 return trim
? model_trimmed
: model
;
365 #pragma mark Serialization
369 __CFDataCopyVMData(CFDataRef data
, void **dataRef
, CFIndex
*dataLen
)
373 vm_address_t vm_address
;
376 vm_address
= (vm_address_t
)CFDataGetBytePtr(data
);
377 vm_size
= (vm_size_t
)CFDataGetLength(data
);
378 kr
= vm_allocate(mach_task_self(), &vm_address
, vm_size
, VM_FLAGS_ANYWHERE
);
379 if (kr
!= KERN_SUCCESS
) {
385 bcopy((char *)CFDataGetBytePtr(data
), (void *)vm_address
, vm_size
);
386 *dataRef
= (void *)vm_address
;
394 _SCSerialize(CFPropertyListRef obj
, CFDataRef
*xml
, void **dataRef
, CFIndex
*dataLen
)
398 if ((xml
== NULL
) && ((dataRef
== NULL
) || (dataLen
== NULL
))) {
399 /* if not keeping track of allocated space */
403 myXml
= CFPropertyListCreateData(NULL
,
405 kCFPropertyListBinaryFormat_v1_0
,
409 SC_log(LOG_NOTICE
, "CFPropertyListCreateData() failed");
413 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
422 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
423 *dataRef
= (void *)CFDataGetBytePtr(myXml
);
424 *dataLen
= CFDataGetLength(myXml
);
429 kr
= __CFDataCopyVMData(myXml
, dataRef
, dataLen
);
431 if (kr
!= KERN_SUCCESS
) {
432 SC_log(LOG_NOTICE
, "__CFDataCreateVMData() failed: %s", mach_error_string(kr
));
442 _SCUnserialize(CFPropertyListRef
*obj
, CFDataRef xml
, void *dataRef
, CFIndex dataLen
)
444 CFErrorRef error
= NULL
;
447 kern_return_t status
;
449 xml
= CFDataCreateWithBytesNoCopy(NULL
, (void *)dataRef
, dataLen
, kCFAllocatorNull
);
450 *obj
= CFPropertyListCreateWithData(NULL
, xml
, kCFPropertyListImmutable
, NULL
, &error
);
453 status
= vm_deallocate(mach_task_self(), (vm_address_t
)dataRef
, dataLen
);
454 if (status
!= KERN_SUCCESS
) {
455 SC_log(LOG_NOTICE
, "vm_deallocate() failed: %s", mach_error_string(status
));
456 /* non-fatal???, proceed */
459 *obj
= CFPropertyListCreateWithData(NULL
, xml
, kCFPropertyListImmutable
, NULL
, &error
);
464 SC_log(LOG_NOTICE
, "CFPropertyListCreateWithData() failed: %@", error
);
467 _SCErrorSet(kSCStatusFailed
);
476 _SCSerializeString(CFStringRef str
, CFDataRef
*data
, void **dataRef
, CFIndex
*dataLen
)
480 if (!isA_CFString(str
)) {
481 /* if not a CFString */
485 if ((data
== NULL
) && ((dataRef
== NULL
) || (dataLen
== NULL
))) {
486 /* if not keeping track of allocated space */
490 myData
= CFStringCreateExternalRepresentation(NULL
, str
, kCFStringEncodingUTF8
, 0);
491 if (myData
== NULL
) {
492 SC_log(LOG_NOTICE
, "CFStringCreateExternalRepresentation() failed");
496 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
505 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
506 *dataRef
= (void *)CFDataGetBytePtr(myData
);
507 *dataLen
= CFDataGetLength(myData
);
512 kr
= __CFDataCopyVMData(myData
, dataRef
, dataLen
);
514 if (kr
!= KERN_SUCCESS
) {
515 SC_log(LOG_NOTICE
, "__CFDataCreateVMData() failed: %s", mach_error_string(kr
));
525 _SCUnserializeString(CFStringRef
*str
, CFDataRef utf8
, void *dataRef
, CFIndex dataLen
)
528 kern_return_t status
;
530 utf8
= CFDataCreateWithBytesNoCopy(NULL
, dataRef
, dataLen
, kCFAllocatorNull
);
531 *str
= CFStringCreateFromExternalRepresentation(NULL
, utf8
, kCFStringEncodingUTF8
);
534 status
= vm_deallocate(mach_task_self(), (vm_address_t
)dataRef
, dataLen
);
535 if (status
!= KERN_SUCCESS
) {
536 SC_log(LOG_NOTICE
, "vm_deallocate() failed: %s", mach_error_string(status
));
537 /* non-fatal???, proceed */
540 *str
= CFStringCreateFromExternalRepresentation(NULL
, utf8
, kCFStringEncodingUTF8
);
544 SC_log(LOG_NOTICE
, "CFStringCreateFromExternalRepresentation() failed");
553 _SCSerializeData(CFDataRef data
, void **dataRef
, CFIndex
*dataLen
)
557 if (!isA_CFData(data
)) {
558 /* if not a CFData */
562 kr
= __CFDataCopyVMData(data
, dataRef
, dataLen
);
563 if (kr
!= KERN_SUCCESS
) {
564 SC_log(LOG_NOTICE
, "__CFDataCreateVMData() failed: %s", mach_error_string(kr
));
573 _SCUnserializeData(CFDataRef
*data
, void *dataRef
, CFIndex dataLen
)
575 kern_return_t status
;
577 *data
= CFDataCreate(NULL
, dataRef
, dataLen
);
578 status
= vm_deallocate(mach_task_self(), (vm_address_t
)dataRef
, dataLen
);
579 if (status
!= KERN_SUCCESS
) {
580 SC_log(LOG_NOTICE
, "vm_deallocate() failed: %s", mach_error_string(status
));
581 _SCErrorSet(kSCStatusFailed
);
589 CF_RETURNS_RETAINED CFDictionaryRef
590 _SCSerializeMultiple(CFDictionaryRef dict
)
593 const void * keys_q
[N_QUICK
];
594 const void ** keys
= keys_q
;
596 CFDictionaryRef newDict
= NULL
;
597 const void * pLists_q
[N_QUICK
];
598 const void ** pLists
= pLists_q
;
599 const void * values_q
[N_QUICK
];
600 const void ** values
= values_q
;
602 nElements
= CFDictionaryGetCount(dict
);
604 if (nElements
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
605 keys
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
606 values
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
607 pLists
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFDataRef
), 0);
609 bzero(pLists
, nElements
* sizeof(CFDataRef
));
611 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
612 for (i
= 0; i
< nElements
; i
++) {
614 if (!_SCSerialize((CFPropertyListRef
)values
[i
], (CFDataRef
*)&pLists
[i
], NULL
, NULL
)) {
620 newDict
= CFDictionaryCreate(NULL
,
624 &kCFTypeDictionaryKeyCallBacks
,
625 &kCFTypeDictionaryValueCallBacks
);
630 for (i
= 0; i
< nElements
; i
++) {
631 if (pLists
[i
] != NULL
) CFRelease((CFDataRef
)pLists
[i
]);
634 if (keys
!= keys_q
) {
635 CFAllocatorDeallocate(NULL
, keys
);
636 CFAllocatorDeallocate(NULL
, values
);
637 CFAllocatorDeallocate(NULL
, pLists
);
647 _SCUnserializeMultiple(CFDictionaryRef dict
)
649 const void * keys_q
[N_QUICK
];
650 const void ** keys
= keys_q
;
652 CFDictionaryRef newDict
= NULL
;
653 const void * pLists_q
[N_QUICK
];
654 const void ** pLists
= pLists_q
;
655 const void * values_q
[N_QUICK
];
656 const void ** values
= values_q
;
658 nElements
= CFDictionaryGetCount(dict
);
662 if (nElements
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
663 keys
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
664 values
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
665 pLists
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
667 bzero(pLists
, nElements
* sizeof(CFTypeRef
));
669 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
670 for (i
= 0; i
< nElements
; i
++) {
671 if (!_SCUnserialize((CFPropertyListRef
*)&pLists
[i
], values
[i
], NULL
, 0)) {
677 newDict
= CFDictionaryCreate(NULL
,
681 &kCFTypeDictionaryKeyCallBacks
,
682 &kCFTypeDictionaryValueCallBacks
);
689 for (i
= 0; i
< nElements
; i
++) {
690 if (pLists
[i
]) CFRelease(pLists
[i
]);
693 if (keys
!= keys_q
) {
694 CFAllocatorDeallocate(NULL
, keys
);
695 CFAllocatorDeallocate(NULL
, values
);
696 CFAllocatorDeallocate(NULL
, pLists
);
708 _SCCreatePropertyListFromResource(CFURLRef url
)
710 CFDictionaryRef dict
= NULL
;
713 CFReadStreamRef readStream
;
714 CFNumberRef val
= NULL
;
716 if (!CFURLCopyResourcePropertyForKey(url
, kCFURLFileSizeKey
, &val
, NULL
) ||
718 // if size not available
719 SC_log(LOG_NOTICE
, "CFURLCopyResourcePropertyForKey() size not available: %@", url
);
723 ok
= CFNumberGetValue(val
, kCFNumberSInt64Type
, &fileLen
);
725 if (!ok
|| (fileLen
== 0)) {
726 // if empty or size too large
727 SC_log(LOG_INFO
, "_SCCreatePropertyListFromResource() improper size: %@", url
);
731 readStream
= CFReadStreamCreateWithFile(NULL
, url
);
732 if (readStream
!= NULL
) {
733 if (CFReadStreamOpen(readStream
)) {
737 buffer
= CFAllocatorAllocate(NULL
, (CFIndex
)fileLen
, 0);
738 dataLen
= CFReadStreamRead(readStream
, buffer
, (CFIndex
)fileLen
);
739 if (dataLen
== (CFIndex
)fileLen
) {
742 data
= CFDataCreateWithBytesNoCopy(NULL
, buffer
, (CFIndex
)fileLen
, kCFAllocatorNull
);
744 dict
= CFPropertyListCreateWithData(NULL
,
746 kCFPropertyListImmutable
,
752 CFAllocatorDeallocate(NULL
, buffer
);
753 CFReadStreamClose(readStream
);
755 CFRelease(readStream
);
763 #pragma mark CFRunLoop scheduling
766 __private_extern__
void
767 _SC_signalRunLoop(CFTypeRef obj
, CFRunLoopSourceRef rls
, CFArrayRef rlList
)
769 CFRunLoopRef rl
= NULL
;
770 CFRunLoopRef rl1
= NULL
;
772 CFIndex n
= CFArrayGetCount(rlList
);
778 /* get first runLoop for this object */
779 for (i
= 0; i
< n
; i
+= 3) {
780 if (!CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
784 rl1
= (CFRunLoopRef
)CFArrayGetValueAtIndex(rlList
, i
+1);
789 /* if not scheduled */
793 /* check if we have another runLoop for this object */
795 for (i
= i
+3; i
< n
; i
+= 3) {
798 if (!CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
802 rl2
= (CFRunLoopRef
)CFArrayGetValueAtIndex(rlList
, i
+1);
803 if (!CFEqual(rl1
, rl2
)) {
804 /* we've got more than one runLoop */
811 /* if we only have one runLoop */
816 /* more than one different runLoop, so we must pick one */
817 for (i
= 0; i
< n
; i
+=3) {
820 if (!CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
824 rl
= (CFRunLoopRef
)CFArrayGetValueAtIndex(rlList
, i
+1);
825 rlMode
= CFRunLoopCopyCurrentMode(rl
);
826 if (rlMode
!= NULL
) {
829 waiting
= (CFRunLoopIsWaiting(rl
) && CFRunLoopContainsSource(rl
, rls
, rlMode
));
832 /* we've found a runLoop that's "ready" */
839 /* didn't choose one above, so choose first */
840 CFRunLoopWakeUp(rl1
);
845 __private_extern__ Boolean
846 _SC_isScheduled(CFTypeRef obj
, CFRunLoopRef runLoop
, CFStringRef runLoopMode
, CFMutableArrayRef rlList
)
849 CFIndex n
= CFArrayGetCount(rlList
);
851 for (i
= 0; i
< n
; i
+= 3) {
852 if ((obj
!= NULL
) && !CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
855 if ((runLoop
!= NULL
) && !CFEqual(runLoop
, CFArrayGetValueAtIndex(rlList
, i
+1))) {
858 if ((runLoopMode
!= NULL
) && !CFEqual(runLoopMode
, CFArrayGetValueAtIndex(rlList
, i
+2))) {
868 __private_extern__
void
869 _SC_schedule(CFTypeRef obj
, CFRunLoopRef runLoop
, CFStringRef runLoopMode
, CFMutableArrayRef rlList
)
871 CFArrayAppendValue(rlList
, obj
);
872 CFArrayAppendValue(rlList
, runLoop
);
873 CFArrayAppendValue(rlList
, runLoopMode
);
879 __private_extern__ Boolean
880 _SC_unschedule(CFTypeRef obj
, CFRunLoopRef runLoop
, CFStringRef runLoopMode
, CFMutableArrayRef rlList
, Boolean all
)
883 Boolean found
= FALSE
;
884 CFIndex n
= CFArrayGetCount(rlList
);
887 if ((obj
!= NULL
) && !CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
891 if ((runLoop
!= NULL
) && !CFEqual(runLoop
, CFArrayGetValueAtIndex(rlList
, i
+1))) {
895 if ((runLoopMode
!= NULL
) && !CFEqual(runLoopMode
, CFArrayGetValueAtIndex(rlList
, i
+2))) {
902 CFArrayRemoveValueAtIndex(rlList
, i
+ 2);
903 CFArrayRemoveValueAtIndex(rlList
, i
+ 1);
904 CFArrayRemoveValueAtIndex(rlList
, i
);
921 #define SYSTEMCONFIGURATION_BUNDLE_ID CFSTR("com.apple.SystemConfiguration")
922 #define SYSTEMCONFIGURATION_FRAMEWORK_PATH_LEN (sizeof(SYSTEMCONFIGURATION_FRAMEWORK_PATH) - 1)
924 #define SUFFIX_SYM "~sym"
925 #define SUFFIX_SYM_LEN (sizeof(SUFFIX_SYM) - 1)
927 #define SUFFIX_DST "~dst"
931 _SC_CFBundleGet(void)
933 static CFBundleRef bundle
= NULL
;
938 if (bundle
!= NULL
) {
942 bundle
= CFBundleGetBundleWithIdentifier(SYSTEMCONFIGURATION_BUNDLE_ID
);
943 if (bundle
!= NULL
) {
944 CFRetain(bundle
); // we want to hold a reference to the bundle
947 SC_log(LOG_NOTICE
, "could not get CFBundle for \"%@\". Trying harder...",
948 SYSTEMCONFIGURATION_BUNDLE_ID
);
951 // if appropriate (e.g. when debugging), try a bit harder
953 env
= getenv("DYLD_FRAMEWORK_PATH");
954 len
= (env
!= NULL
) ? strlen(env
) : 0;
956 if (len
> 0) { /* We are debugging */
958 // trim any trailing slashes
960 if (env
[len
- 1] != '/') {
966 // if DYLD_FRAMEWORK_PATH is ".../xxx~sym" than try ".../xxx~dst"
967 if ((len
> SUFFIX_SYM_LEN
) &&
968 (strncmp(&env
[len
- SUFFIX_SYM_LEN
], SUFFIX_SYM
, SUFFIX_SYM_LEN
) == 0) &&
969 ((len
+ SYSTEMCONFIGURATION_FRAMEWORK_PATH_LEN
) < MAXPATHLEN
)) {
970 char path
[MAXPATHLEN
];
972 strlcpy(path
, env
, sizeof(path
));
973 strlcpy(&path
[len
- SUFFIX_SYM_LEN
], SUFFIX_DST
, sizeof(path
) - (len
- SUFFIX_SYM_LEN
));
974 strlcat(&path
[len
], SYSTEMCONFIGURATION_FRAMEWORK_PATH
, sizeof(path
) - len
);
976 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
978 len
+ SYSTEMCONFIGURATION_FRAMEWORK_PATH_LEN
,
980 bundle
= CFBundleCreate(NULL
, url
);
985 if (bundle
== NULL
) { /* Try a more "direct" route to get the bundle */
987 url
= CFURLCreateWithFileSystemPath(NULL
,
988 CFSTR(SYSTEMCONFIGURATION_FRAMEWORK_PATH
),
989 kCFURLPOSIXPathStyle
,
992 bundle
= CFBundleCreate(NULL
, url
);
996 if (bundle
== NULL
) {
997 SC_log(LOG_ERR
, "could not get CFBundle for \"%@\"", SYSTEMCONFIGURATION_BUNDLE_ID
);
1008 * <key>bundleID-tableName</key>
1010 * ... property list from non-localized bundle URL
1014 static CFMutableDictionaryRef cachedInfo
= NULL
;
1017 static dispatch_queue_t
1018 _SC_CFBundleCachedInfoQueue()
1020 static dispatch_once_t once
;
1021 static dispatch_queue_t q
;
1023 dispatch_once(&once
, ^{
1024 q
= dispatch_queue_create("_SC_CFBundleCachedInfo", NULL
);
1032 _SC_CFBundleCachedInfoCopyTableKey(CFBundleRef bundle
, CFStringRef tableName
)
1034 CFStringRef bundleID
;
1035 CFStringRef tableKey
;
1037 bundleID
= CFBundleGetIdentifier(bundle
);
1038 tableKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@: %@"), bundleID
, tableName
);
1043 static CFDictionaryRef
1044 _SC_CFBundleCachedInfoCopyTable(CFBundleRef bundle
, CFStringRef tableName
)
1046 __block CFDictionaryRef dict
= NULL
;
1048 dispatch_sync(_SC_CFBundleCachedInfoQueue(), ^{
1049 if (cachedInfo
!= NULL
) {
1050 CFStringRef tableKey
;
1052 tableKey
= _SC_CFBundleCachedInfoCopyTableKey(bundle
, tableName
);
1053 dict
= CFDictionaryGetValue(cachedInfo
, tableKey
);
1057 CFRelease(tableKey
);
1066 _SC_CFBundleCachedInfoSaveTable(CFBundleRef bundle
, CFStringRef tableName
, CFDictionaryRef table
)
1069 dispatch_sync(_SC_CFBundleCachedInfoQueue(), ^{
1070 CFStringRef tableKey
;
1072 tableKey
= _SC_CFBundleCachedInfoCopyTableKey(bundle
, tableName
);
1073 SC_log(LOG_DEBUG
, "Caching %@", tableKey
);
1075 if (cachedInfo
== NULL
) {
1076 cachedInfo
= CFDictionaryCreateMutable(NULL
,
1078 &kCFTypeDictionaryKeyCallBacks
,
1079 &kCFTypeDictionaryValueCallBacks
);
1081 CFDictionarySetValue(cachedInfo
, tableKey
, table
);
1082 CFRelease(tableKey
);
1090 _SC_CFBundleCopyNonLocalizedString(CFBundleRef bundle
, CFStringRef key
, CFStringRef value
, CFStringRef tableName
)
1092 CFStringRef str
= NULL
;
1093 CFDictionaryRef table
= NULL
;
1096 if ((tableName
== NULL
) || CFEqual(tableName
, CFSTR(""))) {
1097 tableName
= CFSTR("Localizable");
1100 table
= _SC_CFBundleCachedInfoCopyTable(bundle
, tableName
);
1101 if (table
== NULL
) {
1102 url
= CFBundleCopyResourceURLForLocalization(bundle
,
1108 table
= _SCCreatePropertyListFromResource(url
);
1110 if (table
!= NULL
) {
1111 _SC_CFBundleCachedInfoSaveTable(bundle
, tableName
, table
);
1114 SC_log(LOG_ERR
, "failed to get resource url: {bundle:%@, table: %@}", bundle
, tableName
);
1118 if (table
!= NULL
) {
1119 if (isA_CFDictionary(table
)) {
1120 str
= CFDictionaryGetValue(table
, key
);
1130 str
= CFRetain(value
);
1138 #pragma mark Mach port / CFMachPort management
1142 _SC_CFMachPortCreateWithPort(const char *portDescription
,
1143 mach_port_t portNum
,
1144 CFMachPortCallBack callout
,
1145 CFMachPortContext
*context
)
1148 Boolean shouldFree
= FALSE
;
1150 port
= CFMachPortCreateWithPort(NULL
, portNum
, callout
, context
, &shouldFree
);
1151 if ((port
== NULL
) || shouldFree
) {
1153 char *crash_info
= NULL
;
1155 SC_log(LOG_NOTICE
, "%s: CFMachPortCreateWithPort() failed , port = %p",
1157 (void *)(uintptr_t)portNum
);
1159 err
= CFStringCreateWithFormat(NULL
, NULL
,
1160 CFSTR("%s: CFMachPortCreateWithPort recycled, [old] port = %@"),
1161 portDescription
, port
);
1163 err
= CFStringCreateWithFormat(NULL
, NULL
,
1164 CFSTR("%s: CFMachPortCreateWithPort returned NULL"),
1167 crash_info
= _SC_cfstring_to_cstring(err
, NULL
, 0, kCFStringEncodingASCII
);
1168 if (err
!= NULL
) CFRelease(err
);
1171 err
= CFStringCreateWithFormat(NULL
,
1173 CFSTR("A recycled mach_port has been detected by \"%s\"."),
1175 _SC_crash(crash_info
, CFSTR("CFMachPort error"), err
);
1176 CFAllocatorDeallocate(NULL
, crash_info
);
1177 if (err
!= NULL
) CFRelease(err
);
1185 #pragma mark DOS encoding/codepage
1188 #if !TARGET_OS_IPHONE
1190 _SC_dos_encoding_and_codepage(CFStringEncoding macEncoding
,
1192 CFStringEncoding
*dosEncoding
,
1193 UInt32
*dosCodepage
)
1195 switch (macEncoding
) {
1196 case kCFStringEncodingMacRoman
:
1197 if (macRegion
!= 0) /* anything non-zero is not US */
1198 *dosEncoding
= kCFStringEncodingDOSLatin1
;
1199 else /* US region */
1200 *dosEncoding
= kCFStringEncodingDOSLatinUS
;
1203 case kCFStringEncodingMacJapanese
:
1204 *dosEncoding
= kCFStringEncodingDOSJapanese
;
1207 case kCFStringEncodingMacChineseTrad
:
1208 *dosEncoding
= kCFStringEncodingDOSChineseTrad
;
1211 case kCFStringEncodingMacKorean
:
1212 *dosEncoding
= kCFStringEncodingDOSKorean
;
1215 case kCFStringEncodingMacArabic
:
1216 *dosEncoding
= kCFStringEncodingDOSArabic
;
1219 case kCFStringEncodingMacHebrew
:
1220 *dosEncoding
= kCFStringEncodingDOSHebrew
;
1223 case kCFStringEncodingMacGreek
:
1224 *dosEncoding
= kCFStringEncodingDOSGreek
;
1227 case kCFStringEncodingMacCyrillic
:
1228 *dosEncoding
= kCFStringEncodingDOSCyrillic
;
1231 case kCFStringEncodingMacThai
:
1232 *dosEncoding
= kCFStringEncodingDOSThai
;
1235 case kCFStringEncodingMacChineseSimp
:
1236 *dosEncoding
= kCFStringEncodingDOSChineseSimplif
;
1239 case kCFStringEncodingMacCentralEurRoman
:
1240 *dosEncoding
= kCFStringEncodingDOSLatin2
;
1243 case kCFStringEncodingMacTurkish
:
1244 *dosEncoding
= kCFStringEncodingDOSTurkish
;
1247 case kCFStringEncodingMacCroatian
:
1248 *dosEncoding
= kCFStringEncodingDOSLatin2
;
1251 case kCFStringEncodingMacIcelandic
:
1252 *dosEncoding
= kCFStringEncodingDOSIcelandic
;
1255 case kCFStringEncodingMacRomanian
:
1256 *dosEncoding
= kCFStringEncodingDOSLatin2
;
1259 case kCFStringEncodingMacFarsi
:
1260 *dosEncoding
= kCFStringEncodingDOSArabic
;
1263 case kCFStringEncodingMacUkrainian
:
1264 *dosEncoding
= kCFStringEncodingDOSCyrillic
;
1268 *dosEncoding
= kCFStringEncodingDOSLatin1
;
1272 *dosCodepage
= CFStringConvertEncodingToWindowsCodepage(*dosEncoding
);
1275 #endif // !TARGET_OS_IPHONE
1279 #pragma mark Debugging
1283 * print status of in-use mach ports
1286 _SC_logMachPortStatus(void)
1288 kern_return_t status
;
1289 mach_port_name_array_t ports
;
1290 mach_port_type_array_t types
;
1291 mach_msg_type_number_t pi
, pn
, tn
;
1292 CFMutableStringRef str
;
1294 SC_log(LOG_DEBUG
, "----------");
1296 /* report on ALL mach ports associated with this task */
1297 status
= mach_port_names(mach_task_self(), &ports
, &pn
, &types
, &tn
);
1298 if (status
== MACH_MSG_SUCCESS
) {
1299 str
= CFStringCreateMutable(NULL
, 0);
1300 for (pi
= 0; pi
< pn
; pi
++) {
1301 char rights
[16], *rp
= &rights
[0];
1303 if (types
[pi
] != MACH_PORT_TYPE_NONE
) {
1306 if (types
[pi
] & MACH_PORT_TYPE_SEND
)
1308 if (types
[pi
] & MACH_PORT_TYPE_RECEIVE
)
1310 if (types
[pi
] & MACH_PORT_TYPE_SEND_ONCE
)
1312 if (types
[pi
] & MACH_PORT_TYPE_PORT_SET
)
1314 if (types
[pi
] & MACH_PORT_TYPE_DEAD_NAME
)
1319 CFStringAppendFormat(str
, NULL
, CFSTR(" %d%s"), ports
[pi
], rights
);
1321 SC_log(LOG_DEBUG
, "Task ports (n=%d):%@", pn
, str
);
1331 _SC_getMachPortReferences(mach_port_t port
,
1332 mach_port_type_t
*pt
,
1333 mach_port_urefs_t
*refs_send
,
1334 mach_port_urefs_t
*refs_recv
,
1335 mach_port_status_t
*recv_status
,
1336 mach_port_urefs_t
*refs_once
,
1337 mach_port_urefs_t
*refs_pset
,
1338 mach_port_urefs_t
*refs_dead
,
1339 const char *err_prefix
)
1341 kern_return_t status
;
1343 status
= mach_port_type(mach_task_self(), port
, pt
);
1344 if (status
!= KERN_SUCCESS
) {
1345 SC_log(LOG_DEBUG
, "%smach_port_type(..., 0x%x): %s",
1348 mach_error_string(status
));
1352 if ((refs_send
!= NULL
) && ((*pt
& MACH_PORT_TYPE_SEND
) != 0)) {
1353 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_SEND
, refs_send
);
1354 if (status
!= KERN_SUCCESS
) {
1355 SC_log(LOG_DEBUG
, "%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_SEND): %s",
1358 mach_error_string(status
));
1363 if ((refs_recv
!= NULL
) && (recv_status
!= NULL
) && ((*pt
& MACH_PORT_TYPE_RECEIVE
) != 0)) {
1364 mach_msg_type_number_t count
;
1366 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, refs_recv
);
1367 if (status
!= KERN_SUCCESS
) {
1368 SC_log(LOG_DEBUG
, "%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_RECEIVE): %s",
1371 mach_error_string(status
));
1375 count
= MACH_PORT_RECEIVE_STATUS_COUNT
;
1376 status
= mach_port_get_attributes(mach_task_self(),
1378 MACH_PORT_RECEIVE_STATUS
,
1379 (mach_port_info_t
)recv_status
,
1381 if (status
!= KERN_SUCCESS
) {
1382 SC_log(LOG_DEBUG
, "%smach_port_get_attributes(..., 0x%x, MACH_PORT_RECEIVE_STATUS): %s",
1385 mach_error_string(status
));
1390 if ((refs_once
!= NULL
) && ((*pt
& MACH_PORT_TYPE_SEND_ONCE
) != 0)) {
1391 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_SEND_ONCE
, refs_once
);
1392 if (status
!= KERN_SUCCESS
) {
1393 SC_log(LOG_DEBUG
, "%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_SEND_ONCE): %s",
1396 mach_error_string(status
));
1401 if ((refs_pset
!= NULL
) && ((*pt
& MACH_PORT_TYPE_PORT_SET
) != 0)) {
1402 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_PORT_SET
, refs_pset
);
1403 if (status
!= KERN_SUCCESS
) {
1404 SC_log(LOG_DEBUG
, "%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_PORT_SET): %s",
1407 mach_error_string(status
));
1412 if ((refs_dead
!= NULL
) && ((*pt
& MACH_PORT_TYPE_DEAD_NAME
) != 0)) {
1413 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_DEAD_NAME
, refs_dead
);
1414 if (status
!= KERN_SUCCESS
) {
1415 SC_log(LOG_DEBUG
, "%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_DEAD_NAME): %s",
1418 mach_error_string(status
));
1423 return KERN_SUCCESS
;
1428 _SC_logMachPortReferences(const char *str
, mach_port_t port
)
1430 const char *blanks
= " ";
1432 mach_port_type_t pt
;
1433 mach_port_status_t recv_status
= { .mps_nsrequest
= 0, };
1434 mach_port_urefs_t refs_send
= 0;
1435 mach_port_urefs_t refs_recv
= 0;
1436 mach_port_urefs_t refs_once
= 0;
1437 mach_port_urefs_t refs_pset
= 0;
1438 mach_port_urefs_t refs_dead
= 0;
1439 kern_return_t status
;
1443 static int is_configd
= -1;
1445 if (is_configd
== -1) {
1446 is_configd
= (strcmp(getprogname(), _SC_SERVER_PROG
) == 0);
1448 if (is_configd
== 1) {
1449 // if "configd", add indication if this is the M[ain] or [P]lugin thread
1451 (CFRunLoopGetMain() == CFRunLoopGetCurrent()) ? "M " : "P ",
1455 // add provided string
1456 strlcat(buf
, str
, sizeof(buf
));
1459 strlcat(buf
, blanks
, sizeof(buf
));
1460 if (strcmp(&buf
[sizeof(buf
) - 3], " ") == 0) {
1461 buf
[sizeof(buf
) - 3] = ':';
1465 status
= _SC_getMachPortReferences(port
,
1474 if (status
!= KERN_SUCCESS
) {
1478 SC_log(LOG_DEBUG
, "%smach port 0x%x (%d): send=%d, receive=%d, send once=%d, port set=%d, dead name=%d%s%s",
1487 recv_status
.mps_nsrequest
? ", no more senders" : "",
1488 ((pt
& MACH_PORT_TYPE_DEAD_NAME
) != 0) ? ", dead name request" : "");
1500 CFMutableStringRef trace
;
1502 n
= backtrace(stack
, sizeof(stack
)/sizeof(stack
[0]));
1504 SC_log(LOG_NOTICE
, "backtrace() failed: %s", strerror(errno
));
1508 trace
= CFStringCreateMutable(NULL
, 0);
1510 symbols
= backtrace_symbols(stack
, n
);
1511 if (symbols
!= NULL
) {
1514 for (i
= 0; i
< n
; i
++) {
1515 CFStringAppendFormat(trace
, NULL
, CFSTR("%s\n"), symbols
[i
]);
1526 _SC_SimulateCrash(const char *crash_info
, CFStringRef notifyHeader
, CFStringRef notifyMessage
)
1528 #if !defined(DO_NOT_INFORM)
1529 #pragma unused(notifyHeader)
1530 #pragma unused(notifyMessage)
1531 #endif // !defined(DO_NOT_INFORM)
1532 #if TARGET_OS_SIMULATOR
1533 #pragma unused(crash_info)
1534 #endif // TARGET_OS_SIMULATOR
1537 #if !TARGET_OS_SIMULATOR
1538 static bool (*dyfunc_SimulateCrash
)(pid_t
, mach_exception_data_type_t
, CFStringRef
) = NULL
;
1539 static void *image
= NULL
;
1540 static dispatch_once_t once
;
1542 dispatch_once(&once
, ^{
1543 image
= _SC_dlopen("/System/Library/PrivateFrameworks/CrashReporterSupport.framework/CrashReporterSupport");
1544 if (image
!= NULL
) {
1545 dyfunc_SimulateCrash
= dlsym(image
, "SimulateCrash");
1549 if (dyfunc_SimulateCrash
!= NULL
) {
1552 str
= CFStringCreateWithCString(NULL
, crash_info
, kCFStringEncodingUTF8
);
1553 ok
= dyfunc_SimulateCrash(getpid(), 0xbad0005cull
, str
);
1557 #if !defined(DO_NOT_INFORM)
1558 if (ok
&& (notifyHeader
!= NULL
) && (notifyMessage
!= NULL
)) {
1559 static Boolean warned
= FALSE
;
1562 CFStringRef displayMessage
;
1564 displayMessage
= CFStringCreateWithFormat(NULL
,
1566 CFSTR("%@\n\nPlease collect the crash report and file a Radar."),
1568 CFUserNotificationDisplayNotice(0,
1569 kCFUserNotificationStopAlertLevel
,
1576 CFRelease(displayMessage
);
1580 #endif // !defined(DO_NOT_INFORM)
1581 #endif // !TARGET_OS_SIMULATOR
1588 _SC_crash(const char *crash_info
, CFStringRef notifyHeader
, CFStringRef notifyMessage
)
1592 if (crash_info
!= NULL
) {
1593 CRSetCrashLogMessage(crash_info
);
1594 SC_log(LOG_NOTICE
, "%s", crash_info
);
1597 if (_SC_isAppleInternal()) {
1598 // simulate a crash report
1599 ok
= _SC_SimulateCrash(crash_info
, notifyHeader
, notifyMessage
);
1600 #ifndef DO_NOT_CRASH
1602 // if we could not simulate a crash report, crash for real
1605 #endif // DO_NOT_CRASH
1608 CRSetCrashLogMessage(NULL
);
1614 _SC_getconninfo(int socket
, struct sockaddr_storage
*src_addr
, struct sockaddr_storage
*dest_addr
, int *if_index
, uint32_t *flags
)
1616 struct so_cinforeq request
;
1618 memset(&request
, 0, sizeof(request
));
1620 if (src_addr
!= NULL
) {
1621 memset(src_addr
, 0, sizeof(*src_addr
));
1622 request
.scir_src
= (struct sockaddr
*)src_addr
;
1623 request
.scir_src_len
= sizeof(*src_addr
);
1626 if (dest_addr
!= NULL
) {
1627 memset(dest_addr
, 0, sizeof(*dest_addr
));
1628 request
.scir_dst
= (struct sockaddr
*)dest_addr
;
1629 request
.scir_dst_len
= sizeof(*dest_addr
);
1632 if (ioctl(socket
, SIOCGCONNINFO
, &request
) != 0) {
1633 SC_log(LOG_NOTICE
, "SIOCGCONNINFO failed: %s", strerror(errno
));
1637 if (if_index
!= NULL
) {
1639 *if_index
= request
.scir_ifindex
;
1642 if (flags
!= NULL
) {
1644 *flags
= request
.scir_flags
;