2 * Copyright (c) 2000-2013 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 DO_NOT_CRASH
35 //#define DO_NOT_INFORM
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCValidation.h>
39 #include <SystemConfiguration/SCPrivate.h>
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
46 #include <net/if_dl.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
50 #include <mach/mach.h>
51 #include <mach/notify.h>
52 #include <mach/mach_error.h>
60 #if TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
61 #include <CoreFoundation/CFUserNotification.h>
62 #endif // TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
68 #pragma mark Miscellaneous
72 _SC_cfstring_to_cstring(CFStringRef cfstr
, char *buf
, CFIndex bufLen
, CFStringEncoding encoding
)
81 len
= CFStringGetLength(cfstr
);
83 /* how much buffer space will we really need? */
84 converted
= CFStringGetBytes(cfstr
,
92 if (converted
< len
) {
93 /* if full string could not be converted */
101 if (bufLen
< (last
+ 1)) {
102 /* if the size of the provided buffer is too small */
107 /* allocate a buffer */
109 buf
= CFAllocatorAllocate(NULL
, bufLen
, 0);
115 (void)CFStringGetBytes(cfstr
,
130 _SC_sockaddr_to_string(const struct sockaddr
*address
, char *buf
, size_t bufLen
)
133 const struct sockaddr
*sa
;
134 const struct sockaddr_in
*sin
;
135 const struct sockaddr_in6
*sin6
;
136 const struct sockaddr_dl
*sdl
;
142 switch (address
->sa_family
) {
144 (void)inet_ntop(addr
.sin
->sin_family
,
150 (void)inet_ntop(addr
.sin6
->sin6_family
,
151 &addr
.sin6
->sin6_addr
,
154 if (addr
.sin6
->sin6_scope_id
!= 0) {
158 if ((n
+IF_NAMESIZE
+1) <= (int)bufLen
) {
160 if_indextoname(addr
.sin6
->sin6_scope_id
, &buf
[n
]);
166 snprintf(buf
, bufLen
, "unexpected address family %d", address
->sa_family
);
175 _SC_string_to_sockaddr(const char *str
, sa_family_t af
, void *buf
, size_t bufLen
)
180 struct sockaddr_in
*sin
;
181 struct sockaddr_in6
*sin6
;
185 bufLen
= sizeof(struct sockaddr_storage
);
186 addr
.buf
= CFAllocatorAllocate(NULL
, bufLen
, 0);
191 bzero(addr
.buf
, bufLen
);
192 if (((af
== AF_UNSPEC
) || (af
== AF_INET
)) &&
193 (bufLen
>= sizeof(struct sockaddr_in
)) &&
194 inet_aton(str
, &addr
.sin
->sin_addr
) == 1) {
196 addr
.sin
->sin_len
= sizeof(struct sockaddr_in
);
197 addr
.sin
->sin_family
= AF_INET
;
198 } else if (((af
== AF_UNSPEC
) || (af
== AF_INET6
)) &&
199 (bufLen
>= sizeof(struct sockaddr_in6
)) &&
200 inet_pton(AF_INET6
, str
, &addr
.sin6
->sin6_addr
) == 1) {
204 addr
.sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
205 addr
.sin6
->sin6_family
= AF_INET6
;
207 p
= strchr(buf
, '%');
209 addr
.sin6
->sin6_scope_id
= if_nametoindex(p
+ 1);
212 if (IN6_IS_ADDR_LINKLOCAL(&addr
.sin6
->sin6_addr
) ||
213 IN6_IS_ADDR_MC_LINKLOCAL(&addr
.sin6
->sin6_addr
)) {
216 if_index
= ntohs(addr
.sin6
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
217 addr
.sin6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
218 if (addr
.sin6
->sin6_scope_id
== 0) {
219 // use the scope id that was embedded in the [link local] IPv6 address
220 addr
.sin6
->sin6_scope_id
= if_index
;
224 if (addr
.buf
!= buf
) {
225 CFAllocatorDeallocate(NULL
, addr
.buf
);
235 _SC_sendMachMessage(mach_port_t port
, mach_msg_id_t msg_id
)
237 mach_msg_empty_send_t msg
;
238 mach_msg_option_t options
;
239 kern_return_t status
;
241 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
242 msg
.header
.msgh_size
= sizeof(msg
);
243 msg
.header
.msgh_remote_port
= port
;
244 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
245 msg
.header
.msgh_id
= msg_id
;
246 options
= MACH_SEND_TIMEOUT
;
247 status
= mach_msg(&msg
.header
, /* msg */
248 MACH_SEND_MSG
|options
, /* options */
249 msg
.header
.msgh_size
, /* send_size */
251 MACH_PORT_NULL
, /* rcv_name */
253 MACH_PORT_NULL
); /* notify */
254 if (status
!= MACH_MSG_SUCCESS
) {
255 mach_msg_destroy(&msg
.header
);
263 _SC_trimDomain(CFStringRef domain
)
267 if (!isA_CFString(domain
)) {
271 // remove any leading/trailing dots
272 length
= CFStringGetLength(domain
);
274 (CFStringFindWithOptions(domain
,
279 CFStringFindWithOptions(domain
,
281 CFRangeMake(0, length
),
282 kCFCompareAnchored
|kCFCompareBackwards
,
284 CFMutableStringRef trimmed
;
286 trimmed
= CFStringCreateMutableCopy(NULL
, 0, domain
);
287 CFStringTrim(trimmed
, CFSTR("."));
288 domain
= (CFStringRef
)trimmed
;
289 length
= CFStringGetLength(domain
);
304 #pragma mark Serialization
308 _SCSerialize(CFPropertyListRef obj
, CFDataRef
*xml
, void **dataRef
, CFIndex
*dataLen
)
312 if ((xml
== NULL
) && ((dataRef
== NULL
) || (dataLen
== NULL
))) {
313 /* if not keeping track of allocated space */
317 myXml
= CFPropertyListCreateData(NULL
,
319 kCFPropertyListBinaryFormat_v1_0
,
323 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCSerialize() failed"));
327 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
336 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
337 *dataRef
= (void *)CFDataGetBytePtr(myXml
);
338 *dataLen
= CFDataGetLength(myXml
);
341 mach_msg_type_number_t len
;
342 kern_return_t status
;
344 status
= vm_read(mach_task_self(),
345 (vm_address_t
)CFDataGetBytePtr(myXml
), // address
346 (vm_size_t
) CFDataGetLength(myXml
), // size
349 if (status
!= KERN_SUCCESS
) {
350 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCSerialize(): %s"), mach_error_string(status
));
366 _SCUnserialize(CFPropertyListRef
*obj
, CFDataRef xml
, void *dataRef
, CFIndex dataLen
)
368 CFErrorRef error
= NULL
;
371 kern_return_t status
;
373 xml
= CFDataCreateWithBytesNoCopy(NULL
, (void *)dataRef
, dataLen
, kCFAllocatorNull
);
374 *obj
= CFPropertyListCreateWithData(NULL
, xml
, kCFPropertyListImmutable
, NULL
, &error
);
377 status
= vm_deallocate(mach_task_self(), (vm_address_t
)dataRef
, dataLen
);
378 if (status
!= KERN_SUCCESS
) {
379 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("_SCUnserialize(): %s"), mach_error_string(status
));
380 /* non-fatal???, proceed */
383 *obj
= CFPropertyListCreateWithData(NULL
, xml
, kCFPropertyListImmutable
, NULL
, &error
);
388 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCUnserialize(): %@"), error
);
391 _SCErrorSet(kSCStatusFailed
);
400 _SCSerializeString(CFStringRef str
, CFDataRef
*data
, void **dataRef
, CFIndex
*dataLen
)
404 if (!isA_CFString(str
)) {
405 /* if not a CFString */
409 if ((data
== NULL
) && ((dataRef
== NULL
) || (dataLen
== NULL
))) {
410 /* if not keeping track of allocated space */
414 myData
= CFStringCreateExternalRepresentation(NULL
, str
, kCFStringEncodingUTF8
, 0);
415 if (myData
== NULL
) {
416 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCSerializeString() failed"));
420 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
429 if ((dataRef
!= NULL
) && (dataLen
!= NULL
)) {
430 *dataRef
= (void *)CFDataGetBytePtr(myData
);
431 *dataLen
= CFDataGetLength(myData
);
434 mach_msg_type_number_t len
;
435 kern_return_t status
;
437 *dataLen
= CFDataGetLength(myData
);
438 status
= vm_read(mach_task_self(),
439 (vm_address_t
)CFDataGetBytePtr(myData
), // address
443 if (status
!= KERN_SUCCESS
) {
444 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCSerializeString(): %s"), mach_error_string(status
));
460 _SCUnserializeString(CFStringRef
*str
, CFDataRef utf8
, void *dataRef
, CFIndex dataLen
)
463 kern_return_t status
;
465 utf8
= CFDataCreateWithBytesNoCopy(NULL
, dataRef
, dataLen
, kCFAllocatorNull
);
466 *str
= CFStringCreateFromExternalRepresentation(NULL
, utf8
, kCFStringEncodingUTF8
);
469 status
= vm_deallocate(mach_task_self(), (vm_address_t
)dataRef
, dataLen
);
470 if (status
!= KERN_SUCCESS
) {
471 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("_SCUnserializeString(): %s"), mach_error_string(status
));
472 /* non-fatal???, proceed */
475 *str
= CFStringCreateFromExternalRepresentation(NULL
, utf8
, kCFStringEncodingUTF8
);
479 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCUnserializeString() failed"));
488 _SCSerializeData(CFDataRef data
, void **dataRef
, CFIndex
*dataLen
)
490 mach_msg_type_number_t len
;
491 kern_return_t status
;
493 if (!isA_CFData(data
)) {
494 /* if not a CFData */
498 *dataLen
= CFDataGetLength(data
);
499 status
= vm_read(mach_task_self(),
500 (vm_address_t
)CFDataGetBytePtr(data
), // address
504 if (status
!= KERN_SUCCESS
) {
505 SCLog(TRUE
, LOG_ERR
, CFSTR("_SCSerializeData(): %s"), mach_error_string(status
));
518 _SCUnserializeData(CFDataRef
*data
, void *dataRef
, CFIndex dataLen
)
520 kern_return_t status
;
522 *data
= CFDataCreate(NULL
, dataRef
, dataLen
);
523 status
= vm_deallocate(mach_task_self(), (vm_address_t
)dataRef
, dataLen
);
524 if (status
!= KERN_SUCCESS
) {
525 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("_SCUnserializeData(): %s"), mach_error_string(status
));
526 _SCErrorSet(kSCStatusFailed
);
534 CF_RETURNS_RETAINED CFDictionaryRef
535 _SCSerializeMultiple(CFDictionaryRef dict
)
538 const void * keys_q
[N_QUICK
];
539 const void ** keys
= keys_q
;
541 CFDictionaryRef newDict
= NULL
;
542 const void * pLists_q
[N_QUICK
];
543 const void ** pLists
= pLists_q
;
544 const void * values_q
[N_QUICK
];
545 const void ** values
= values_q
;
547 nElements
= CFDictionaryGetCount(dict
);
549 if (nElements
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
550 keys
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
551 values
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
552 pLists
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFDataRef
), 0);
554 bzero(pLists
, nElements
* sizeof(CFDataRef
));
556 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
557 for (i
= 0; i
< nElements
; i
++) {
559 if (!_SCSerialize((CFPropertyListRef
)values
[i
], (CFDataRef
*)&pLists
[i
], NULL
, NULL
)) {
565 newDict
= CFDictionaryCreate(NULL
,
569 &kCFTypeDictionaryKeyCallBacks
,
570 &kCFTypeDictionaryValueCallBacks
);
575 for (i
= 0; i
< nElements
; i
++) {
576 if (pLists
[i
] != NULL
) CFRelease((CFDataRef
)pLists
[i
]);
579 if (keys
!= keys_q
) {
580 CFAllocatorDeallocate(NULL
, keys
);
581 CFAllocatorDeallocate(NULL
, values
);
582 CFAllocatorDeallocate(NULL
, pLists
);
592 _SCUnserializeMultiple(CFDictionaryRef dict
)
594 const void * keys_q
[N_QUICK
];
595 const void ** keys
= keys_q
;
597 CFDictionaryRef newDict
= NULL
;
598 const void * pLists_q
[N_QUICK
];
599 const void ** pLists
= pLists_q
;
600 const void * values_q
[N_QUICK
];
601 const void ** values
= values_q
;
603 nElements
= CFDictionaryGetCount(dict
);
607 if (nElements
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
608 keys
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
609 values
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
610 pLists
= CFAllocatorAllocate(NULL
, nElements
* sizeof(CFTypeRef
), 0);
612 bzero(pLists
, nElements
* sizeof(CFTypeRef
));
614 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
615 for (i
= 0; i
< nElements
; i
++) {
616 if (!_SCUnserialize((CFPropertyListRef
*)&pLists
[i
], values
[i
], NULL
, 0)) {
622 newDict
= CFDictionaryCreate(NULL
,
626 &kCFTypeDictionaryKeyCallBacks
,
627 &kCFTypeDictionaryValueCallBacks
);
634 for (i
= 0; i
< nElements
; i
++) {
635 if (pLists
[i
]) CFRelease(pLists
[i
]);
638 if (keys
!= keys_q
) {
639 CFAllocatorDeallocate(NULL
, keys
);
640 CFAllocatorDeallocate(NULL
, values
);
641 CFAllocatorDeallocate(NULL
, pLists
);
650 #pragma mark CFRunLoop scheduling
653 __private_extern__
void
654 _SC_signalRunLoop(CFTypeRef obj
, CFRunLoopSourceRef rls
, CFArrayRef rlList
)
656 CFRunLoopRef rl
= NULL
;
657 CFRunLoopRef rl1
= NULL
;
659 CFIndex n
= CFArrayGetCount(rlList
);
665 /* get first runLoop for this object */
666 for (i
= 0; i
< n
; i
+= 3) {
667 if (!CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
671 rl1
= (CFRunLoopRef
)CFArrayGetValueAtIndex(rlList
, i
+1);
676 /* if not scheduled */
680 /* check if we have another runLoop for this object */
682 for (i
= i
+3; i
< n
; i
+= 3) {
685 if (!CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
689 rl2
= (CFRunLoopRef
)CFArrayGetValueAtIndex(rlList
, i
+1);
690 if (!CFEqual(rl1
, rl2
)) {
691 /* we've got more than one runLoop */
698 /* if we only have one runLoop */
703 /* more than one different runLoop, so we must pick one */
704 for (i
= 0; i
< n
; i
+=3) {
707 if (!CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
711 rl
= (CFRunLoopRef
)CFArrayGetValueAtIndex(rlList
, i
+1);
712 rlMode
= CFRunLoopCopyCurrentMode(rl
);
713 if (rlMode
!= NULL
) {
716 waiting
= (CFRunLoopIsWaiting(rl
) && CFRunLoopContainsSource(rl
, rls
, rlMode
));
719 /* we've found a runLoop that's "ready" */
726 /* didn't choose one above, so choose first */
727 CFRunLoopWakeUp(rl1
);
732 __private_extern__ Boolean
733 _SC_isScheduled(CFTypeRef obj
, CFRunLoopRef runLoop
, CFStringRef runLoopMode
, CFMutableArrayRef rlList
)
736 CFIndex n
= CFArrayGetCount(rlList
);
738 for (i
= 0; i
< n
; i
+= 3) {
739 if ((obj
!= NULL
) && !CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
742 if ((runLoop
!= NULL
) && !CFEqual(runLoop
, CFArrayGetValueAtIndex(rlList
, i
+1))) {
745 if ((runLoopMode
!= NULL
) && !CFEqual(runLoopMode
, CFArrayGetValueAtIndex(rlList
, i
+2))) {
755 __private_extern__
void
756 _SC_schedule(CFTypeRef obj
, CFRunLoopRef runLoop
, CFStringRef runLoopMode
, CFMutableArrayRef rlList
)
758 CFArrayAppendValue(rlList
, obj
);
759 CFArrayAppendValue(rlList
, runLoop
);
760 CFArrayAppendValue(rlList
, runLoopMode
);
766 __private_extern__ Boolean
767 _SC_unschedule(CFTypeRef obj
, CFRunLoopRef runLoop
, CFStringRef runLoopMode
, CFMutableArrayRef rlList
, Boolean all
)
770 Boolean found
= FALSE
;
771 CFIndex n
= CFArrayGetCount(rlList
);
774 if ((obj
!= NULL
) && !CFEqual(obj
, CFArrayGetValueAtIndex(rlList
, i
))) {
778 if ((runLoop
!= NULL
) && !CFEqual(runLoop
, CFArrayGetValueAtIndex(rlList
, i
+1))) {
782 if ((runLoopMode
!= NULL
) && !CFEqual(runLoopMode
, CFArrayGetValueAtIndex(rlList
, i
+2))) {
789 CFArrayRemoveValueAtIndex(rlList
, i
+ 2);
790 CFArrayRemoveValueAtIndex(rlList
, i
+ 1);
791 CFArrayRemoveValueAtIndex(rlList
, i
);
808 #define SYSTEMCONFIGURATION_BUNDLE_ID CFSTR("com.apple.SystemConfiguration")
809 #define SYSTEMCONFIGURATION_FRAMEWORK_PATH "/System/Library/Frameworks/SystemConfiguration.framework"
810 #define SYSTEMCONFIGURATION_FRAMEWORK_PATH_LEN (sizeof(SYSTEMCONFIGURATION_FRAMEWORK_PATH) - 1)
812 #define SUFFIX_SYM "~sym"
813 #define SUFFIX_SYM_LEN (sizeof(SUFFIX_SYM) - 1)
815 #define SUFFIX_DST "~dst"
819 _SC_CFBundleGet(void)
821 static CFBundleRef bundle
= NULL
;
825 if (bundle
!= NULL
) {
829 bundle
= CFBundleGetBundleWithIdentifier(SYSTEMCONFIGURATION_BUNDLE_ID
);
830 if (bundle
!= NULL
) {
831 CFRetain(bundle
); // we want to hold a reference to the bundle
835 // if appropriate (e.g. when debugging), try a bit harder
837 env
= getenv("DYLD_FRAMEWORK_PATH");
838 len
= (env
!= NULL
) ? strlen(env
) : 0;
840 // trim any trailing slashes
842 if (env
[len
- 1] != '/') {
848 // if DYLD_FRAMEWORK_PATH is ".../xxx~sym" than try ".../xxx~dst"
849 if ((len
> SUFFIX_SYM_LEN
) &&
850 (strncmp(&env
[len
- SUFFIX_SYM_LEN
], SUFFIX_SYM
, SUFFIX_SYM_LEN
) == 0) &&
851 ((len
+ SYSTEMCONFIGURATION_FRAMEWORK_PATH_LEN
) < MAXPATHLEN
)) {
852 char path
[MAXPATHLEN
];
855 strlcpy(path
, env
, sizeof(path
));
856 strlcpy(&path
[len
- SUFFIX_SYM_LEN
], SUFFIX_DST
, sizeof(path
) - (len
- SUFFIX_SYM_LEN
));
857 strlcat(&path
[len
], SYSTEMCONFIGURATION_FRAMEWORK_PATH
, sizeof(path
) - len
);
859 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
861 len
+ SYSTEMCONFIGURATION_FRAMEWORK_PATH_LEN
,
863 bundle
= CFBundleCreate(NULL
, url
);
867 if (bundle
== NULL
) {
868 static Boolean warned
= FALSE
;
870 SCLog(!warned
, LOG_WARNING
,
871 CFSTR("_SC_CFBundleGet(), could not get CFBundle for \"%@\""),
872 SYSTEMCONFIGURATION_BUNDLE_ID
);
881 _SC_CFBundleCopyNonLocalizedString(CFBundleRef bundle
, CFStringRef key
, CFStringRef value
, CFStringRef tableName
)
883 CFDataRef data
= NULL
;
886 CFURLRef resourcesURL
;
887 CFStringRef str
= NULL
;
890 if ((tableName
== NULL
) || CFEqual(tableName
, CFSTR(""))) tableName
= CFSTR("Localizable");
893 * First, try getting the requested string using a manually constructed
894 * URL to <bundle>/Resources/English.lproj/<tableName>.strings. Do this
895 * because CFBundleCopyResourceURLForLocalization() uses CFPreferences
896 * to get the preferred localizations, CFPreferences talks to
897 * OpenDirectory, and OpenDirectory tries to obtain the platform UUID.
898 * On machines where the platform UUID is set by InterfaceNamer, a
899 * deadlock can occur if InterfaceNamer calls
900 * CFBundleCopyResourceURLForLocalization() before setting the
901 * platform UUID in the kernel.
903 resourcesURL
= CFBundleCopyResourcesDirectoryURL(bundle
);
904 if (resourcesURL
!= NULL
) {
905 CFStringRef fileName
= CFStringCreateWithFormat(
906 NULL
, NULL
, CFSTR("%@.strings"), tableName
);
907 CFURLRef enlproj
= CFURLCreateCopyAppendingPathComponent(
908 NULL
, resourcesURL
, CFSTR("English.lproj"), true);
909 url
= CFURLCreateCopyAppendingPathComponent(
910 NULL
, enlproj
, fileName
, false);
913 CFRelease(resourcesURL
);
915 #pragma GCC diagnostic push
916 #pragma GCC diagnostic ignored "-Wdeprecated"
917 ok
= CFURLCreateDataAndPropertiesFromResource(NULL
,
923 #pragma GCC diagnostic pop
926 * Failed to get the data using a manually-constructed URL
927 * for the given strings table. Fall back to using
928 * CFBundleCopyResourceURLForLocalization() below.
936 url
= CFBundleCopyResourceURLForLocalization(bundle
,
942 #pragma GCC diagnostic push
943 #pragma GCC diagnostic ignored "-Wdeprecated"
944 ok
= CFURLCreateDataAndPropertiesFromResource(NULL
,
950 #pragma GCC diagnostic pop
959 CFDictionaryRef table
;
961 table
= CFPropertyListCreateWithData(NULL
,
963 kCFPropertyListImmutable
,
967 if (isA_CFDictionary(table
)) {
968 str
= CFDictionaryGetValue(table
, key
);
981 str
= CFRetain(value
);
989 #pragma mark Mach port / CFMachPort management
993 _SC_CFMachPortCreateWithPort(const char *portDescription
,
995 CFMachPortCallBack callout
,
996 CFMachPortContext
*context
)
999 Boolean shouldFree
= FALSE
;
1001 port
= CFMachPortCreateWithPort(NULL
, portNum
, callout
, context
, &shouldFree
);
1002 if ((port
== NULL
) || shouldFree
) {
1004 char *crash_info
= NULL
;
1006 SCLog(TRUE
, LOG_ERR
,
1007 CFSTR("%s: CFMachPortCreateWithPort() failed , port = %p"),
1011 err
= CFStringCreateWithFormat(NULL
, NULL
,
1012 CFSTR("%s: CFMachPortCreateWithPort recycled, [old] port = %@"),
1013 portDescription
, port
);
1015 err
= CFStringCreateWithFormat(NULL
, NULL
,
1016 CFSTR("%s: CFMachPortCreateWithPort returned NULL"),
1019 crash_info
= _SC_cfstring_to_cstring(err
, NULL
, 0, kCFStringEncodingASCII
);
1020 if (err
!= NULL
) CFRelease(err
);
1023 err
= CFStringCreateWithFormat(NULL
,
1025 CFSTR("A recycled mach_port has been detected by \"%s\"."),
1027 _SC_crash(crash_info
, CFSTR("CFMachPort error"), err
);
1028 CFAllocatorDeallocate(NULL
, crash_info
);
1037 #pragma mark DOS encoding/codepage
1040 #if !TARGET_OS_IPHONE
1042 _SC_dos_encoding_and_codepage(CFStringEncoding macEncoding
,
1044 CFStringEncoding
*dosEncoding
,
1045 UInt32
*dosCodepage
)
1047 switch (macEncoding
) {
1048 case kCFStringEncodingMacRoman
:
1049 if (macRegion
!= 0) /* anything non-zero is not US */
1050 *dosEncoding
= kCFStringEncodingDOSLatin1
;
1051 else /* US region */
1052 *dosEncoding
= kCFStringEncodingDOSLatinUS
;
1055 case kCFStringEncodingMacJapanese
:
1056 *dosEncoding
= kCFStringEncodingDOSJapanese
;
1059 case kCFStringEncodingMacChineseTrad
:
1060 *dosEncoding
= kCFStringEncodingDOSChineseTrad
;
1063 case kCFStringEncodingMacKorean
:
1064 *dosEncoding
= kCFStringEncodingDOSKorean
;
1067 case kCFStringEncodingMacArabic
:
1068 *dosEncoding
= kCFStringEncodingDOSArabic
;
1071 case kCFStringEncodingMacHebrew
:
1072 *dosEncoding
= kCFStringEncodingDOSHebrew
;
1075 case kCFStringEncodingMacGreek
:
1076 *dosEncoding
= kCFStringEncodingDOSGreek
;
1079 case kCFStringEncodingMacCyrillic
:
1080 *dosEncoding
= kCFStringEncodingDOSCyrillic
;
1083 case kCFStringEncodingMacThai
:
1084 *dosEncoding
= kCFStringEncodingDOSThai
;
1087 case kCFStringEncodingMacChineseSimp
:
1088 *dosEncoding
= kCFStringEncodingDOSChineseSimplif
;
1091 case kCFStringEncodingMacCentralEurRoman
:
1092 *dosEncoding
= kCFStringEncodingDOSLatin2
;
1095 case kCFStringEncodingMacTurkish
:
1096 *dosEncoding
= kCFStringEncodingDOSTurkish
;
1099 case kCFStringEncodingMacCroatian
:
1100 *dosEncoding
= kCFStringEncodingDOSLatin2
;
1103 case kCFStringEncodingMacIcelandic
:
1104 *dosEncoding
= kCFStringEncodingDOSIcelandic
;
1107 case kCFStringEncodingMacRomanian
:
1108 *dosEncoding
= kCFStringEncodingDOSLatin2
;
1111 case kCFStringEncodingMacFarsi
:
1112 *dosEncoding
= kCFStringEncodingDOSArabic
;
1115 case kCFStringEncodingMacUkrainian
:
1116 *dosEncoding
= kCFStringEncodingDOSCyrillic
;
1120 *dosEncoding
= kCFStringEncodingDOSLatin1
;
1124 *dosCodepage
= CFStringConvertEncodingToWindowsCodepage(*dosEncoding
);
1127 #endif // !TARGET_OS_IPHONE
1131 #pragma mark Debugging
1135 * print status of in-use mach ports
1138 _SC_logMachPortStatus(void)
1140 kern_return_t status
;
1141 mach_port_name_array_t ports
;
1142 mach_port_type_array_t types
;
1143 mach_msg_type_number_t pi
, pn
, tn
;
1144 CFMutableStringRef str
;
1146 SCLog(TRUE
, LOG_NOTICE
, CFSTR("----------"));
1148 /* report on ALL mach ports associated with this task */
1149 status
= mach_port_names(mach_task_self(), &ports
, &pn
, &types
, &tn
);
1150 if (status
== MACH_MSG_SUCCESS
) {
1151 str
= CFStringCreateMutable(NULL
, 0);
1152 for (pi
= 0; pi
< pn
; pi
++) {
1153 char rights
[16], *rp
= &rights
[0];
1155 if (types
[pi
] != MACH_PORT_TYPE_NONE
) {
1158 if (types
[pi
] & MACH_PORT_TYPE_SEND
)
1160 if (types
[pi
] & MACH_PORT_TYPE_RECEIVE
)
1162 if (types
[pi
] & MACH_PORT_TYPE_SEND_ONCE
)
1164 if (types
[pi
] & MACH_PORT_TYPE_PORT_SET
)
1166 if (types
[pi
] & MACH_PORT_TYPE_DEAD_NAME
)
1171 CFStringAppendFormat(str
, NULL
, CFSTR(" %d%s"), ports
[pi
], rights
);
1173 SCLog(TRUE
, LOG_NOTICE
, CFSTR("Task ports (n=%d):%@"), pn
, str
);
1182 _SC_logMachPortReferences(const char *str
, mach_port_t port
)
1184 const char *blanks
= " ";
1186 mach_port_type_t pt
;
1187 mach_port_status_t recv_status
= { 0 };
1188 mach_port_urefs_t refs_send
= 0;
1189 mach_port_urefs_t refs_recv
= 0;
1190 mach_port_urefs_t refs_once
= 0;
1191 mach_port_urefs_t refs_pset
= 0;
1192 mach_port_urefs_t refs_dead
= 0;
1193 kern_return_t status
;
1197 static int is_configd
= -1;
1199 if (is_configd
== -1) {
1200 is_configd
= (strcmp(getprogname(), _SC_SERVER_PROG
) == 0);
1202 if (is_configd
== 1) {
1203 // if "configd", add indication if this is the M[ain] or [P]lugin thread
1205 (CFRunLoopGetMain() == CFRunLoopGetCurrent()) ? "M " : "P ",
1209 // add provided string
1210 strlcat(buf
, str
, sizeof(buf
));
1213 strlcat(buf
, blanks
, sizeof(buf
));
1214 if (strcmp(&buf
[sizeof(buf
) - 3], " ") == 0) {
1215 buf
[sizeof(buf
) - 3] = ':';
1219 status
= mach_port_type(mach_task_self(), port
, &pt
);
1220 if (status
!= KERN_SUCCESS
) {
1221 SCLog(TRUE
, LOG_NOTICE
,
1222 CFSTR("%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_SEND): %s"),
1225 mach_error_string(status
));
1229 if ((pt
& MACH_PORT_TYPE_SEND
) != 0) {
1230 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_SEND
, &refs_send
);
1231 if (status
!= KERN_SUCCESS
) {
1232 SCLog(TRUE
, LOG_NOTICE
,
1233 CFSTR("%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_SEND): %s"),
1236 mach_error_string(status
));
1241 if ((pt
& MACH_PORT_TYPE_RECEIVE
) != 0) {
1242 mach_msg_type_number_t count
;
1244 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, &refs_recv
);
1245 if (status
!= KERN_SUCCESS
) {
1246 SCLog(TRUE
, LOG_NOTICE
,
1247 CFSTR("%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_RECEIVE): %s"),
1250 mach_error_string(status
));
1254 count
= MACH_PORT_RECEIVE_STATUS_COUNT
;
1255 status
= mach_port_get_attributes(mach_task_self(),
1257 MACH_PORT_RECEIVE_STATUS
,
1258 (mach_port_info_t
)&recv_status
,
1260 if (status
!= KERN_SUCCESS
) {
1261 SCLog(TRUE
, LOG_NOTICE
,
1262 CFSTR("%smach_port_get_attributes(..., 0x%x, MACH_PORT_RECEIVE_STATUS): %s"),
1265 mach_error_string(status
));
1270 if ((pt
& MACH_PORT_TYPE_SEND_ONCE
) != 0) {
1271 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_SEND_ONCE
, &refs_once
);
1272 if (status
!= KERN_SUCCESS
) {
1273 SCLog(TRUE
, LOG_NOTICE
,
1274 CFSTR("%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_SEND_ONCE): %s"),
1277 mach_error_string(status
));
1282 if ((pt
& MACH_PORT_TYPE_PORT_SET
) != 0) {
1283 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_PORT_SET
, &refs_pset
);
1284 if (status
!= KERN_SUCCESS
) {
1285 SCLog(TRUE
, LOG_NOTICE
,
1286 CFSTR("%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_PORT_SET): %s"),
1289 mach_error_string(status
));
1294 if ((pt
& MACH_PORT_TYPE_DEAD_NAME
) != 0) {
1295 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_DEAD_NAME
, &refs_dead
);
1296 if (status
!= KERN_SUCCESS
) {
1297 SCLog(TRUE
, LOG_NOTICE
,
1298 CFSTR("%smach_port_get_refs(..., 0x%x, MACH_PORT_RIGHT_DEAD_NAME): %s"),
1301 mach_error_string(status
));
1306 SCLog(TRUE
, LOG_NOTICE
,
1307 CFSTR("%smach port 0x%x (%d): send=%d, receive=%d, send once=%d, port set=%d, dead name=%d%s%s"),
1316 recv_status
.mps_nsrequest
? ", no more senders" : "",
1317 ((pt
& MACH_PORT_TYPE_DEAD_NAME
) != 0) ? ", dead name request" : "");
1329 CFMutableStringRef trace
;
1331 n
= backtrace(stack
, sizeof(stack
)/sizeof(stack
[0]));
1333 SCLog(TRUE
, LOG_ERR
, CFSTR("backtrace() failed: %s"), strerror(errno
));
1337 trace
= CFStringCreateMutable(NULL
, 0);
1339 symbols
= backtrace_symbols(stack
, n
);
1340 if (symbols
!= NULL
) {
1343 for (i
= 0; i
< n
; i
++) {
1344 CFStringAppendFormat(trace
, NULL
, CFSTR("%s\n"), symbols
[i
]);
1354 /* CrashReporter info */
1355 const char *__crashreporter_info__
= NULL
;
1356 asm(".desc ___crashreporter_info__, 0x10");
1360 _SC_SimulateCrash(const char *crash_info
, CFStringRef notifyHeader
, CFStringRef notifyMessage
)
1364 #if ! TARGET_IPHONE_SIMULATOR
1365 static bool (*dyfunc_SimulateCrash
)(pid_t
, mach_exception_data_type_t
, CFStringRef
) = NULL
;
1366 static void *image
= NULL
;
1368 if ((dyfunc_SimulateCrash
== NULL
) && (image
== NULL
)) {
1369 const char *framework
= "/System/Library/PrivateFrameworks/CrashReporterSupport.framework/CrashReporterSupport";
1370 struct stat statbuf
;
1371 const char *suffix
= getenv("DYLD_IMAGE_SUFFIX");
1372 char path
[MAXPATHLEN
];
1374 strlcpy(path
, framework
, sizeof(path
));
1375 if (suffix
) strlcat(path
, suffix
, sizeof(path
));
1376 if (0 <= stat(path
, &statbuf
)) {
1377 image
= dlopen(path
, RTLD_LAZY
| RTLD_LOCAL
);
1379 image
= dlopen(framework
, RTLD_LAZY
| RTLD_LOCAL
);
1382 if (image
!= NULL
) {
1383 dyfunc_SimulateCrash
= dlsym(image
, "SimulateCrash");
1385 image
= (void *)0x1; // to ensure that we only dlopen() once
1389 if (dyfunc_SimulateCrash
!= NULL
) {
1392 str
= CFStringCreateWithCString(NULL
, crash_info
, kCFStringEncodingUTF8
);
1393 ok
= dyfunc_SimulateCrash(getpid(), 0xbad0005cull
, str
);
1397 #if TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
1398 if (ok
&& (notifyHeader
!= NULL
) && (notifyMessage
!= NULL
)) {
1399 static Boolean warned
= FALSE
;
1402 CFStringRef displayMessage
;
1404 displayMessage
= CFStringCreateWithFormat(NULL
,
1406 CFSTR("%@\n\nPlease collect the crash report and file a Radar."),
1408 CFUserNotificationDisplayNotice(0,
1409 kCFUserNotificationStopAlertLevel
,
1416 CFRelease(displayMessage
);
1420 #endif // TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
1421 #endif /* ! TARGET_IPHONE_SIMULATOR */
1428 _SC_crash(const char *crash_info
, CFStringRef notifyHeader
, CFStringRef notifyMessage
)
1432 if (crash_info
!= NULL
) {
1433 __crashreporter_info__
= crash_info
;
1435 SCLog(TRUE
, LOG_ERR
, CFSTR("%s"), crash_info
);
1438 if (_SC_isAppleInternal()) {
1439 // simulate a crash report
1440 ok
= _SC_SimulateCrash(crash_info
, notifyHeader
, notifyMessage
);
1441 #ifndef DO_NOT_CRASH
1443 // if we could not simulate a crash report, crash for real
1446 #endif // DO_NOT_CRASH
1449 __crashreporter_info__
= NULL
;
1455 _SC_getconninfo(int socket
, struct sockaddr_storage
*src_addr
, struct sockaddr_storage
*dest_addr
, int *if_index
, uint32_t *flags
)
1457 struct so_cinforeq request
;
1459 memset(&request
, 0, sizeof(request
));
1461 if (src_addr
!= NULL
) {
1462 memset(src_addr
, 0, sizeof(*src_addr
));
1463 request
.scir_src
= (struct sockaddr
*)src_addr
;
1464 request
.scir_src_len
= sizeof(*src_addr
);
1467 if (dest_addr
!= NULL
) {
1468 memset(dest_addr
, 0, sizeof(*dest_addr
));
1469 request
.scir_dst
= (struct sockaddr
*)dest_addr
;
1470 request
.scir_dst_len
= sizeof(*dest_addr
);
1473 if (ioctl(socket
, SIOCGCONNINFO
, &request
) != 0) {
1474 SCLog(TRUE
, LOG_WARNING
, CFSTR("SIOCGCONNINFO failed: %s"), strerror(errno
));
1478 if (if_index
!= NULL
) {
1480 *if_index
= request
.scir_ifindex
;
1483 if (flags
!= NULL
) {
1485 *flags
= request
.scir_flags
;