2 * Copyright (c) 2000-2014 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 * October 30, 2003 Allan Nathanson <ajn@apple.com>
28 * - add plugin "stop()" function support
30 * June 11, 2001 Allan Nathanson <ajn@apple.com>
31 * - start using CFBundle code
33 * June 1, 2001 Allan Nathanson <ajn@apple.com>
34 * - public API conversion
36 * May 26, 2000 Allan Nathanson <ajn@apple.com>
40 #include <sys/types.h>
42 #include <sys/param.h>
47 #include <NSSystemDirectories.h>
50 #include "configd_server.h"
51 #include <SystemConfiguration/SCDPlugin.h>
52 #include "SCNetworkReachabilityInternal.h"
53 void _SCDPluginExecInit();
57 * path components, extensions, entry points, ...
59 #define BUNDLE_DIRECTORY "/SystemConfiguration" /* [/System/Library]/... */
60 #define BUNDLE_DIR_EXTENSION ".bundle"
63 #define PLUGIN_ALL(p) CFSTR(p)
65 #define PLUGIN_MACOSX(p) CFSTR(p)
66 #define PLUGIN_IOS(p) NULL
67 #else // !TARGET_OS_IPHONE
68 #define PLUGIN_MACOSX(p) NULL
69 #define PLUGIN_IOS(p) CFSTR(p)
70 #endif // !TARGET_OS_IPHONE
72 // white-listed (ok-to-load) bundle identifiers
73 static const CFStringRef pluginWhitelist
[] = {
74 PLUGIN_MACOSX("com.apple.SystemConfiguration.ApplicationFirewall"),
75 PLUGIN_ALL ("com.apple.SystemConfiguration.EAPOLController"),
76 PLUGIN_ALL ("com.apple.SystemConfiguration.IPConfiguration"),
77 PLUGIN_ALL ("com.apple.SystemConfiguration.IPMonitor"),
78 PLUGIN_MACOSX("com.apple.SystemConfiguration.ISPreference"),
79 PLUGIN_ALL ("com.apple.SystemConfiguration.InterfaceNamer"),
80 PLUGIN_ALL ("com.apple.SystemConfiguration.KernelEventMonitor"),
81 PLUGIN_ALL ("com.apple.SystemConfiguration.LinkConfiguration"),
82 PLUGIN_ALL ("com.apple.SystemConfiguration.Logger"),
83 PLUGIN_ALL ("com.apple.SystemConfiguration.PPPController"),
84 PLUGIN_ALL ("com.apple.SystemConfiguration.PreferencesMonitor"),
85 PLUGIN_ALL ("com.apple.SystemConfiguration.SCNetworkReachability"),
86 PLUGIN_MACOSX("com.apple.print.notification"),
88 #define N_PLUGIN_WHITELIST (sizeof(pluginWhitelist) / sizeof(pluginWhitelist[0]))
98 SCDynamicStoreBundleLoadFunction load
;
99 SCDynamicStoreBundleStartFunction start
;
100 SCDynamicStoreBundlePrimeFunction prime
;
101 SCDynamicStoreBundleStopFunction stop
;
105 // all loaded bundles
106 static CFMutableArrayRef allBundles
= NULL
;
109 static CFMutableDictionaryRef exiting
= NULL
;
111 // plugin CFRunLoopRef
113 CFRunLoopRef plugin_runLoop
= NULL
;
116 extern SCDynamicStoreBundleLoadFunction load_IPMonitor
;
117 extern SCDynamicStoreBundlePrimeFunction prime_IPMonitor
;
118 #if !TARGET_IPHONE_SIMULATOR
119 extern SCDynamicStoreBundleLoadFunction load_InterfaceNamer
;
120 extern SCDynamicStoreBundleLoadFunction load_KernelEventMonitor
;
121 extern SCDynamicStoreBundlePrimeFunction prime_KernelEventMonitor
;
122 extern SCDynamicStoreBundleLoadFunction load_LinkConfiguration
;
123 extern SCDynamicStoreBundleLoadFunction load_PreferencesMonitor
;
124 extern SCDynamicStoreBundlePrimeFunction prime_PreferencesMonitor
;
125 #endif // !TARGET_IPHONE_SIMULATOR
126 extern SCDynamicStoreBundleLoadFunction load_SCNetworkReachability
;
130 const CFStringRef bundleID
;
131 const void *load
; // SCDynamicStoreBundleLoadFunction
132 const void *start
; // SCDynamicStoreBundleStartFunction
133 const void *prime
; // SCDynamicStoreBundlePrimeFunction
134 const void *stop
; // SCDynamicStoreBundleStopFunction
135 } builtin
, *builtinRef
;
138 static const builtin builtin_plugins
[] = {
140 CFSTR("com.apple.SystemConfiguration.IPMonitor"),
146 #if !TARGET_IPHONE_SIMULATOR
148 CFSTR("com.apple.SystemConfiguration.InterfaceNamer"),
149 &load_InterfaceNamer
,
155 CFSTR("com.apple.SystemConfiguration.KernelEventMonitor"),
156 &load_KernelEventMonitor
,
158 &prime_KernelEventMonitor
,
162 CFSTR("com.apple.SystemConfiguration.LinkConfiguration"),
163 &load_LinkConfiguration
,
169 CFSTR("com.apple.SystemConfiguration.PreferencesMonitor"),
170 &load_PreferencesMonitor
,
172 &prime_PreferencesMonitor
,
175 #endif // !TARGET_IPHONE_SIMULATOR
177 CFSTR("com.apple.SystemConfiguration.SCNetworkReachability"),
178 &load_SCNetworkReachability
,
188 traceBundle(const char *op
, CFBundleRef bundle
)
190 if (_configd_trace
!= NULL
) {
191 if (bundle
!= NULL
) {
192 CFStringRef bundleID
= CFBundleGetIdentifier(bundle
);
194 SCTrace(TRUE
, _configd_trace
,
195 CFSTR("bundle : %s %@\n"),
199 SCTrace(TRUE
, _configd_trace
,
200 CFSTR("bundle : %s\n"),
211 addBundle(CFBundleRef bundle
, Boolean forceEnabled
)
213 CFDictionaryRef bundleDict
;
214 bundleInfoRef bundleInfo
;
216 bundleInfo
= CFAllocatorAllocate(NULL
, sizeof(*bundleInfo
), 0);
217 bundleInfo
->bundle
= (CFBundleRef
)CFRetain(bundle
);
218 bundleInfo
->loaded
= FALSE
;
219 bundleInfo
->builtin
= FALSE
;
220 bundleInfo
->enabled
= TRUE
;
221 bundleInfo
->forced
= forceEnabled
;
222 bundleInfo
->verbose
= FALSE
;
223 bundleInfo
->load
= NULL
;
224 bundleInfo
->start
= NULL
;
225 bundleInfo
->prime
= NULL
;
226 bundleInfo
->stop
= NULL
;
228 bundleDict
= CFBundleGetInfoDictionary(bundle
);
229 if (isA_CFDictionary(bundleDict
)) {
232 bVal
= CFDictionaryGetValue(bundleDict
, kSCBundleIsBuiltinKey
);
233 if (isA_CFBoolean(bVal
)) {
234 bundleInfo
->builtin
= CFBooleanGetValue(bVal
);
237 bVal
= CFDictionaryGetValue(bundleDict
, kSCBundleEnabledKey
);
238 if (isA_CFBoolean(bVal
)) {
239 bundleInfo
->enabled
= CFBooleanGetValue(bVal
);
242 bVal
= CFDictionaryGetValue(bundleDict
, kSCBundleVerboseKey
);
243 if (isA_CFBoolean(bVal
)) {
244 bundleInfo
->verbose
= CFBooleanGetValue(bVal
);
248 CFArrayAppendValue(allBundles
, bundleInfo
);
253 static CF_RETURNS_RETAINED CFStringRef
254 shortBundleIdentifier(CFStringRef bundleID
)
256 CFIndex len
= CFStringGetLength(bundleID
);
258 CFStringRef shortID
= NULL
;
260 if (CFStringFindWithOptions(bundleID
,
265 range
.location
= range
.location
+ range
.length
;
266 range
.length
= len
- range
.location
;
267 shortID
= CFStringCreateWithSubstring(NULL
, bundleID
, range
);
275 getBundleSymbol(CFBundleRef bundle
, CFStringRef functionName
, CFStringRef shortID
)
279 // search for load(), start(), prime(), stop(), ...
280 func
= CFBundleGetFunctionPointerForName(bundle
, functionName
);
285 if (shortID
!= NULL
) {
286 CFStringRef altFunctionName
;
288 // search for load_XXX(), ...
289 altFunctionName
= CFStringCreateWithFormat(NULL
,
294 func
= CFBundleGetFunctionPointerForName(bundle
, altFunctionName
);
295 CFRelease(altFunctionName
);
303 getBundleDirNameAndPath(CFBundleRef bundle
, char *buf
, size_t buf_len
)
310 url
= CFBundleCopyBundleURL(bundle
);
315 ok
= CFURLGetFileSystemRepresentation(url
, TRUE
, (UInt8
*)buf
, buf_len
);
321 cp
= strrchr(buf
, '/');
328 /* check if this directory entry is a valid bundle name */
330 if (len
<= sizeof(BUNDLE_DIR_EXTENSION
)) {
331 /* if entry name isn't long enough */
335 len
-= sizeof(BUNDLE_DIR_EXTENSION
) - 1;
336 if (strcmp(&cp
[len
], BUNDLE_DIR_EXTENSION
) != 0) {
337 /* if entry name doesn't end with ".bundle" */
350 loadBundle(const void *value
, void *context
) {
351 CFStringRef bundleID
;
352 Boolean bundleAllowed
;
353 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
354 Boolean bundleExclude
;
355 CFIndex
*nLoaded
= (CFIndex
*)context
;
358 bundleID
= CFBundleGetIdentifier(bundleInfo
->bundle
);
359 if (bundleID
== NULL
) {
360 // sorry, no bundles without a bundle identifier
361 SCLog(TRUE
, LOG_NOTICE
, CFSTR("skipped %@ (no bundle ID)"), bundleInfo
->bundle
);
365 shortID
= shortBundleIdentifier(bundleID
);
367 bundleAllowed
= ((CFSetGetCount(_plugins_allowed
) == 0) || // if no white-listing
368 CFSetContainsValue(_plugins_allowed
, bundleID
) || // if [bundleID] white-listed
369 ((shortID
!= NULL
) &&
370 CFSetContainsValue(_plugins_allowed
, shortID
))|| // if [short bundleID] white-listed
371 bundleInfo
->forced
// if "testing" plugin
373 if (!bundleAllowed
) {
374 SCLog(TRUE
, LOG_WARNING
, CFSTR("skipped %@ (not allowed)"), bundleID
);
378 bundleExclude
= (CFSetContainsValue(_plugins_exclude
, bundleID
) || // if [bundleID] excluded
379 ((shortID
!= NULL
) &&
380 CFSetContainsValue(_plugins_exclude
, shortID
)) // if [short bundleID] excluded
383 // sorry, this bundle has been excluded
384 SCLog(TRUE
, LOG_NOTICE
, CFSTR("skipped %@ (excluded)"), bundleID
);
388 if (!bundleInfo
->enabled
&& !bundleInfo
->forced
) {
389 // sorry, this bundle has not been enabled
390 SCLog(TRUE
, LOG_INFO
, CFSTR("skipped %@ (disabled)"), bundleID
);
394 if (!bundleInfo
->verbose
) {
395 bundleInfo
->verbose
= CFSetContainsValue(_plugins_verbose
, bundleID
);
396 if (!bundleInfo
->verbose
) {
397 if (shortID
!= NULL
) {
398 bundleInfo
->verbose
= CFSetContainsValue(_plugins_verbose
, shortID
);
403 if (bundleInfo
->builtin
) {
406 SCLog(TRUE
, LOG_DEBUG
, CFSTR("adding %@"), bundleID
);
408 for (i
= 0; i
< sizeof(builtin_plugins
)/sizeof(builtin_plugins
[0]); i
++) {
409 if (CFEqual(bundleID
, builtin_plugins
[i
].bundleID
)) {
410 bundleInfo
->load
= builtin_plugins
[i
].load
;
411 bundleInfo
->start
= builtin_plugins
[i
].start
;
412 bundleInfo
->prime
= builtin_plugins
[i
].prime
;
413 bundleInfo
->stop
= builtin_plugins
[i
].stop
;
418 if ((bundleInfo
->load
== NULL
) &&
419 (bundleInfo
->start
== NULL
) &&
420 (bundleInfo
->prime
== NULL
) &&
421 (bundleInfo
->stop
== NULL
)) {
422 SCLog(TRUE
, LOG_NOTICE
, CFSTR("%@ add failed"), bundleID
);
426 CFErrorRef error
= NULL
;
428 SCLog(TRUE
, LOG_DEBUG
, CFSTR("loading %@"), bundleID
);
431 traceBundle("loading", bundleInfo
->bundle
);
434 if (!CFBundleLoadExecutableAndReturnError(bundleInfo
->bundle
, &error
)) {
435 CFDictionaryRef user_info
;
437 SCLog(TRUE
, LOG_NOTICE
, CFSTR("%@ load failed"), bundleID
);
438 user_info
= CFErrorCopyUserInfo(error
);
439 if (user_info
!= NULL
) {
440 CFStringRef link_error_string
;
442 link_error_string
= CFDictionaryGetValue(user_info
,
443 CFSTR("NSDebugDescription"));
444 if (link_error_string
!= NULL
) {
445 SCLog(TRUE
, LOG_NOTICE
, CFSTR("%@"), link_error_string
);
447 CFRelease(user_info
);
453 // get bundle entry points
454 bundleInfo
->load
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("load" ), shortID
);
455 bundleInfo
->start
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("start"), shortID
);
456 bundleInfo
->prime
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("prime"), shortID
);
457 bundleInfo
->stop
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("stop" ), shortID
);
460 /* mark this bundle as having been loaded */
461 bundleInfo
->loaded
= TRUE
;
463 /* bump the count of loaded bundles */
464 *nLoaded
= *nLoaded
+ 1;
468 if (shortID
!= NULL
) CFRelease(shortID
);
474 callLoadFunction(const void *value
, void *context
) {
475 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
477 if (!bundleInfo
->loaded
) {
481 if (bundleInfo
->load
== NULL
) {
482 // if no load() function
487 traceBundle("calling load() for", bundleInfo
->bundle
);
490 (*bundleInfo
->load
)(bundleInfo
->bundle
, bundleInfo
->verbose
);
501 callStartFunction(const void *value
, void *context
) {
502 const char *bundleDirName
;
503 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
504 char bundleName
[MAXNAMLEN
+ 1];
505 char bundlePath
[MAXPATHLEN
];
508 if (!bundleInfo
->loaded
) {
512 if (bundleInfo
->start
== NULL
) {
513 // if no start() function
517 /* copy the bundle's path */
518 bundleDirName
= getBundleDirNameAndPath(bundleInfo
->bundle
, bundlePath
, sizeof(bundlePath
));
519 if (bundleDirName
== NULL
) {
520 // if we have a problem with the bundle's path
524 /* copy (just) the bundle's name */
525 if (strlcpy(bundleName
, bundleDirName
, sizeof(bundleName
)) > sizeof(bundleName
)) {
526 // if we have a problem with the bundle's name
529 len
= strlen(bundleName
) - (sizeof(BUNDLE_DIR_EXTENSION
) - 1);
530 bundleName
[len
] = '\0';
533 traceBundle("calling start() for", bundleInfo
->bundle
);
536 (*bundleInfo
->start
)(bundleName
, bundlePath
);
547 callPrimeFunction(const void *value
, void *context
) {
548 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
550 if (!bundleInfo
->loaded
) {
554 if (bundleInfo
->prime
== NULL
) {
555 // if no prime() function
560 traceBundle("calling prime() for", bundleInfo
->bundle
);
563 (*bundleInfo
->prime
)();
574 stopComplete(void *info
)
576 CFBundleRef bundle
= (CFBundleRef
)info
;
577 CFStringRef bundleID
= CFBundleGetIdentifier(bundle
);
578 CFRunLoopSourceRef stopRls
;
580 SCLog(TRUE
, LOG_DEBUG
, CFSTR("** %@ complete (%f)"), bundleID
, CFAbsoluteTimeGetCurrent());
582 stopRls
= (CFRunLoopSourceRef
)CFDictionaryGetValue(exiting
, bundle
);
583 if (stopRls
== NULL
) {
587 CFRunLoopSourceInvalidate(stopRls
);
589 CFDictionaryRemoveValue(exiting
, bundle
);
591 if (CFDictionaryGetCount(exiting
) == 0) {
594 // if all of the plugins are happy
595 status
= server_shutdown();
596 SCLog(TRUE
, LOG_DEBUG
, CFSTR("server shutdown complete (%f)"), CFAbsoluteTimeGetCurrent());
605 stopDelayed(CFRunLoopTimerRef timer
, void *info
)
612 SCLog(TRUE
, LOG_ERR
, CFSTR("server shutdown was delayed, unresponsive plugins:"));
615 * we've asked our plugins to shutdown but someone
618 n
= CFDictionaryGetCount(exiting
);
619 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
620 CFDictionaryGetKeysAndValues(exiting
, keys
, NULL
);
621 for (i
= 0; i
< n
; i
++) {
623 CFStringRef bundleID
;
625 bundle
= (CFBundleRef
)keys
[i
];
626 bundleID
= CFBundleGetIdentifier(bundle
);
627 SCLog(TRUE
, LOG_ERR
, CFSTR("** %@"), bundleID
);
629 CFAllocatorDeallocate(NULL
, keys
);
631 status
= server_shutdown();
636 stopRLSCopyDescription(const void *info
)
638 CFBundleRef bundle
= (CFBundleRef
)info
;
640 return CFStringCreateWithFormat(NULL
,
642 CFSTR("<stopRLS %p> {bundleID = %@}"),
644 CFBundleGetIdentifier(bundle
));
649 stopBundle(const void *value
, void *context
) {
650 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
651 CFRunLoopSourceRef stopRls
;
652 CFRunLoopSourceContext stopContext
= { 0 // version
653 , bundleInfo
->bundle
// info
655 , CFRelease
// release
656 , stopRLSCopyDescription
// copyDescription
661 , stopComplete
// perform
664 if (!bundleInfo
->loaded
) {
668 if (bundleInfo
->stop
== NULL
) {
669 // if no stop() function
673 stopRls
= CFRunLoopSourceCreate(NULL
, 0, &stopContext
);
674 CFRunLoopAddSource(CFRunLoopGetCurrent(), stopRls
, kCFRunLoopDefaultMode
);
675 CFDictionaryAddValue(exiting
, bundleInfo
->bundle
, stopRls
);
678 (*bundleInfo
->stop
)(stopRls
);
688 * If defined, call each bundles stop() function. This function is
689 * called when configd has been asked to shut down (via a SIGTERM). The
690 * function should signal the provided run loop source when it is "ready"
691 * for the shut down to proceeed.
693 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle stop() functions"));
694 CFArrayApplyFunction(allBundles
,
695 CFRangeMake(0, CFArrayGetCount(allBundles
)),
699 if (CFDictionaryGetCount(exiting
) == 0) {
702 // if all of the plugins are happy
703 status
= server_shutdown();
704 SCLog(TRUE
, LOG_DEBUG
, CFSTR("server shutdown complete (%f)"), CFAbsoluteTimeGetCurrent());
707 CFRunLoopTimerRef timer
;
710 * launchd will only wait 20 seconds before sending us a
711 * SIGKILL and because we want to know what's stuck before
712 * that time so set our own "we're not waiting any longer"
713 * timeout for 15 seconds.
715 timer
= CFRunLoopTimerCreate(NULL
, /* allocator */
716 CFAbsoluteTimeGetCurrent() + 15.0, /* fireDate (in 15 seconds) */
717 0.0, /* interval (== one-shot) */
720 stopDelayed
, /* callout */
722 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer
, kCFRunLoopDefaultMode
);
735 termRLSCopyDescription(const void *info
)
737 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SIGTERM RLS>"));
743 plugin_term(int *status
)
745 CFRunLoopSourceContext termContext
= { 0 // version
749 , termRLSCopyDescription
// copyDescription
754 , stopBundles
// perform
756 CFRunLoopSourceRef termRls
;
758 if (plugin_runLoop
== NULL
) {
761 return FALSE
; // don't delay shutdown
764 if (exiting
!= NULL
) {
765 // if shutdown already active
769 SCLog(TRUE
, LOG_DEBUG
, CFSTR("starting server shutdown (%f)"), CFAbsoluteTimeGetCurrent());
771 exiting
= CFDictionaryCreateMutable(NULL
,
773 &kCFTypeDictionaryKeyCallBacks
,
774 &kCFTypeDictionaryValueCallBacks
);
776 termRls
= CFRunLoopSourceCreate(NULL
, 0, &termContext
);
777 CFRunLoopAddSource(plugin_runLoop
, termRls
, kCFRunLoopDefaultMode
);
778 CFRunLoopSourceSignal(termRls
);
780 CFRunLoopWakeUp(plugin_runLoop
);
787 #pragma mark initialization
792 timerCallback(CFRunLoopTimerRef timer
, void *info
)
797 if ((pass
> 120) && ((pass
% 60) != 0)) {
801 traceBundle("the [plugin] CFRunLoop is waiting...", NULL
);
808 sortBundles(CFMutableArrayRef orig
)
812 CFMutableArrayRef
new;
813 CFMutableSetRef orig_bundleIDs
;
815 orig_bundleIDs
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
817 n
= CFArrayGetCount(orig
);
818 for (i
= 0; i
< n
; i
++) {
819 bundleInfoRef bundleInfo
= (bundleInfoRef
)CFArrayGetValueAtIndex(orig
, i
);
820 CFStringRef bundleID
= CFBundleGetIdentifier(bundleInfo
->bundle
);
822 if (bundleID
!= NULL
) {
823 CFSetAddValue(orig_bundleIDs
, bundleID
);
827 new = CFArrayCreateMutable(NULL
, 0, NULL
);
829 Boolean inserted
= FALSE
;
831 for (i
= 0; i
< n
; i
++) {
832 bundleInfoRef bundleInfo1
= (bundleInfoRef
)CFArrayGetValueAtIndex(orig
, i
);
833 CFStringRef bundleID1
= CFBundleGetIdentifier(bundleInfo1
->bundle
);
835 CFDictionaryRef dict
;
838 CFArrayRef
requires = NULL
;
840 dict
= isA_CFDictionary(CFBundleGetInfoDictionary(bundleInfo1
->bundle
));
842 requires = CFDictionaryGetValue(dict
, kSCBundleRequiresKey
);
843 requires = isA_CFArray(requires);
845 if (bundleID1
== NULL
|| requires == NULL
) {
846 CFArrayInsertValueAtIndex(new, 0, bundleInfo1
);
847 CFArrayRemoveValueAtIndex(orig
, i
);
851 count
= nRequires
= CFArrayGetCount(requires);
852 for (j
= 0; j
< nRequires
; j
++) {
855 CFStringRef r
= CFArrayGetValueAtIndex(requires, j
);
857 if (!CFSetContainsValue(orig_bundleIDs
, r
)) {
858 // if dependency not present
863 nNew
= CFArrayGetCount(new);
864 for (k
= 0; k
< nNew
; k
++) {
865 bundleInfoRef bundleInfo2
= (bundleInfoRef
)CFArrayGetValueAtIndex(new, k
);
866 CFStringRef bundleID2
= CFBundleGetIdentifier(bundleInfo2
->bundle
);
868 if (bundleID2
&& CFEqual(bundleID2
, r
)) {
874 /* all dependencies are met, append */
875 CFArrayAppendValue(new, bundleInfo1
);
876 CFArrayRemoveValueAtIndex(orig
, i
);
882 if (inserted
== FALSE
) {
883 SCLog(TRUE
, LOG_NOTICE
, CFSTR("Bundles have circular dependency!!!"));
887 n
= CFArrayGetCount(orig
);
889 if (CFArrayGetCount(orig
) > 0) {
890 /* we have a circular dependency, append remaining items on new array */
891 CFArrayAppendArray(new, orig
, CFRangeMake(0, CFArrayGetCount(orig
)));
894 /* new one is a sorted version of original */
897 CFArrayRemoveAllValues(orig
);
898 CFArrayAppendArray(orig
, new, CFRangeMake(0, CFArrayGetCount(new)));
900 CFRelease(orig_bundleIDs
);
908 * An alternate CFRelease() that we can use to fake out the
911 static __inline__
void
912 ALT_CFRelease(CFTypeRef cf
)
920 plugin_exec(void *arg
)
925 /* keep track of bundles */
926 allBundles
= CFArrayCreateMutable(NULL
, 0, NULL
);
928 /* add white-listed plugins to those we'll allow to be loaded */
929 for (i
= 0; i
< N_PLUGIN_WHITELIST
; i
++) {
930 if (pluginWhitelist
[i
] != NULL
) {
931 CFSetSetValue(_plugins_allowed
, pluginWhitelist
[i
]);
935 /* allow plug-ins to exec child/helper processes */
936 _SCDPluginExecInit();
939 char path
[MAXPATHLEN
];
940 NSSearchPathEnumerationState state
;
943 * identify and load all bundles
945 state
= NSStartSearchPathEnumeration(NSLibraryDirectory
,
947 while ((state
= NSGetNextSearchPathEnumeration(state
, path
))) {
951 #if TARGET_IPHONE_SIMULATOR
952 const char *path_sim_prefix
;
954 path_sim_prefix
= getenv("IPHONE_SIMULATOR_ROOT");
955 if ((path_sim_prefix
!= NULL
) && (strcmp(path_sim_prefix
, ".") != 0)) {
956 char path_sim
[MAXPATHLEN
];
958 strlcpy(path_sim
, path_sim_prefix
, sizeof(path_sim
));
959 strlcat(path_sim
, path
, sizeof(path_sim
));
960 strlcpy(path
, path_sim
, sizeof(path
));
964 #endif // TARGET_IPHONE_SIMULATOR
966 /* load any available bundle */
967 strlcat(path
, BUNDLE_DIRECTORY
, sizeof(path
));
968 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("searching for bundles in \"%s\""), path
);
969 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
973 bundles
= CFBundleCreateBundlesFromDirectory(NULL
, url
, CFSTR(".bundle"));
976 if (bundles
!= NULL
) {
980 n
= CFArrayGetCount(bundles
);
981 for (i
= 0; i
< n
; i
++) {
984 bundle
= (CFBundleRef
)CFArrayGetValueAtIndex(bundles
, i
);
985 addBundle(bundle
, FALSE
);
987 // The CFBundleCreateBundlesFromDirectory() API has
988 // a known/outstanding bug in that it over-retains the
989 // returned bundles. Since we do not expect this to
990 // be fixed we release the extra references.
992 // See <rdar://problems/4912137&6078752> for more info.
994 // Also, we use the hack below to keep the static
996 ALT_CFRelease(bundle
);
1002 sortBundles(allBundles
);
1008 * load (only) the specified bundle
1010 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
1012 strlen((char *)arg
),
1014 bundle
= CFBundleCreate(NULL
, url
);
1015 if (bundle
!= NULL
) {
1016 addBundle(bundle
, TRUE
);
1023 * Look for the InterfaceNamer plugin, and move it to the start
1026 * Load the InterfaceNamer plugin (and thereby start its thread)
1027 * first in an attempt to minimize the amount of time that
1028 * opendirectoryd has to wait for the platform UUID to appear in
1031 * InterfaceNamer is responsible for creating the platform UUID on
1032 * platforms without a UUID in ROM. Until the platform UUID is created
1033 * and stashed in nvram, all calls to opendirectoryd to do things like
1034 * getpwuid() will block, because opendirectoryd will block while trying
1035 * to read the platform UUID from the kernel.
1037 * As an example, dlopen() causes XPC to do some intialization, and
1038 * part of that initialization involves communicating with xpcd.
1039 * Since xpcd calls getpwuid_r() during its initialization, it will
1040 * block until the platform UUID is available.
1042 for (i
= 0; i
< CFArrayGetCount(allBundles
); i
++) {
1043 bundleInfoRef bi
= (bundleInfoRef
)CFArrayGetValueAtIndex(allBundles
, i
);
1044 CFStringRef bundleID
= CFBundleGetIdentifier(bi
->bundle
);
1046 if (_SC_CFEqual(bundleID
,
1047 CFSTR("com.apple.SystemConfiguration.InterfaceNamer")))
1049 CFArrayRemoveValueAtIndex(allBundles
, i
);
1050 CFArrayInsertValueAtIndex(allBundles
, 0, bi
);
1056 traceBundle("before loading any plugins", NULL
);
1062 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("loading bundles"));
1063 CFArrayApplyFunction(allBundles
,
1064 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1069 * If defined, call each bundles load() function. This function (or
1070 * the start() function) should initialize any variables, open any
1071 * sessions with "configd", and register any needed notifications.
1073 * Note: Establishing initial information in the store should be
1074 * deferred until the prime() initialization function so that
1075 * any bundles which want to receive a notification that the
1076 * data has changed will have an opportunity to install a
1077 * notification handler.
1079 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle load() functions"));
1080 CFArrayApplyFunction(allBundles
,
1081 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1086 // if no bundles loaded
1091 * If defined, call each bundles start() function. This function is
1092 * called after the bundle has been loaded and its load() function has
1093 * been called. It should initialize any variables, open any sessions
1094 * with "configd", and register any needed notifications.
1096 * Note: Establishing initial information in the store should be
1097 * deferred until the prime() initialization function so that
1098 * any bundles which want to receive a notification that the
1099 * data has changed will have an opportunity to install a
1100 * notification handler.
1102 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle start() functions"));
1103 CFArrayApplyFunction(allBundles
,
1104 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1109 * If defined, call each bundles prime() function. This function is
1110 * called after the bundle has been loaded and its load() and start()
1111 * functions have been called. It should initialize any configuration
1112 * information and/or state in the store.
1114 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle prime() functions"));
1115 CFArrayApplyFunction(allBundles
,
1116 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1121 if (arg
== NULL
&& (nLoaded
> 0)) {
1122 CFRunLoopTimerRef timer
;
1124 /* allocate a periodic event (to help show we're not blocking) */
1125 timer
= CFRunLoopTimerCreate(NULL
, /* allocator */
1126 CFAbsoluteTimeGetCurrent() + 1.0, /* fireDate */
1130 timerCallback
, /* callout */
1131 NULL
); /* context */
1132 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer
, kCFRunLoopDefaultMode
);
1138 traceBundle("about to start plugin CFRunLoop", NULL
);
1142 * The assumption is that each loaded plugin will establish CFMachPortRef,
1143 * CFSocketRef, and CFRunLoopTimerRef input sources to handle any events
1144 * and register these sources with this threads run loop. If the plugin
1145 * needs to wait and/or block at any time it should do so only in its a
1148 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("starting plugin CFRunLoop"));
1149 plugin_runLoop
= CFRunLoopGetCurrent();
1150 pthread_setname_np("Main plugin thread");
1155 SCLog(_configd_verbose
, LOG_INFO
, CFSTR("No more work for the \"configd\" plugins"));
1156 plugin_runLoop
= NULL
;
1165 pthread_attr_t tattr
;
1168 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Starting thread for plug-ins..."));
1169 pthread_attr_init(&tattr
);
1170 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
1171 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
1172 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
1173 pthread_create(&tid
, &tattr
, plugin_exec
, NULL
);
1174 pthread_attr_destroy(&tattr
);
1175 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" thread id=%p"), tid
);