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 * 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>
50 #include "configd_server.h"
51 #include <SystemConfiguration/SCDPlugin.h>
52 #include "SystemConfigurationInternal.h"
56 * path components, extensions, entry points, ...
58 #define BUNDLE_DIRECTORY "/SystemConfiguration" /* [/System/Library]/... */
59 #define BUNDLE_DIR_EXTENSION ".bundle"
62 #define PLUGIN_ALL(p) CFSTR(p)
64 #define PLUGIN_MACOSX(p) CFSTR(p)
65 #define PLUGIN_IOS(p) NULL
66 #else // !TARGET_OS_IPHONE
67 #define PLUGIN_MACOSX(p) NULL
68 #define PLUGIN_IOS(p) CFSTR(p)
69 #endif // !TARGET_OS_IPHONE
71 // white-listed (ok-to-load) bundle identifiers
72 static const CFStringRef pluginWhitelist
[] = {
73 PLUGIN_MACOSX("com.apple.SystemConfiguration.ApplicationFirewall"),
74 PLUGIN_ALL ("com.apple.SystemConfiguration.EAPOLController"),
75 PLUGIN_ALL ("com.apple.SystemConfiguration.IPConfiguration"),
76 PLUGIN_ALL ("com.apple.SystemConfiguration.IPMonitor"),
77 PLUGIN_MACOSX("com.apple.SystemConfiguration.ISPreference"),
78 PLUGIN_ALL ("com.apple.SystemConfiguration.InterfaceNamer"),
79 PLUGIN_ALL ("com.apple.SystemConfiguration.KernelEventMonitor"),
80 PLUGIN_ALL ("com.apple.SystemConfiguration.LinkConfiguration"),
81 PLUGIN_ALL ("com.apple.SystemConfiguration.PPPController"),
82 PLUGIN_ALL ("com.apple.SystemConfiguration.PreferencesMonitor"),
83 PLUGIN_ALL ("com.apple.SystemConfiguration.QoSMarking"),
84 PLUGIN_MACOSX("com.apple.print.notification"),
86 #define N_PLUGIN_WHITELIST (sizeof(pluginWhitelist) / sizeof(pluginWhitelist[0]))
96 SCDynamicStoreBundleLoadFunction
*load
;
97 SCDynamicStoreBundleStartFunction
*start
;
98 SCDynamicStoreBundlePrimeFunction
*prime
;
99 SCDynamicStoreBundleStopFunction
*stop
;
103 // all loaded bundles
104 static CFMutableArrayRef allBundles
= NULL
;
107 static CFMutableDictionaryRef exiting
= NULL
;
109 // plugin CFRunLoopRef
111 CFRunLoopRef plugin_runLoop
= NULL
;
114 extern SCDynamicStoreBundleLoadFunction load_IPMonitor
;
115 extern SCDynamicStoreBundlePrimeFunction prime_IPMonitor
;
116 #if !TARGET_OS_SIMULATOR
117 extern SCDynamicStoreBundleLoadFunction load_InterfaceNamer
;
118 extern SCDynamicStoreBundleLoadFunction load_KernelEventMonitor
;
119 extern SCDynamicStoreBundlePrimeFunction prime_KernelEventMonitor
;
120 extern SCDynamicStoreBundleLoadFunction load_LinkConfiguration
;
121 extern SCDynamicStoreBundleLoadFunction load_PreferencesMonitor
;
122 extern SCDynamicStoreBundlePrimeFunction prime_PreferencesMonitor
;
123 extern SCDynamicStoreBundleLoadFunction load_QoSMarking
;
124 #endif // !TARGET_OS_SIMULATOR
128 const CFStringRef bundleID
;
129 SCDynamicStoreBundleLoadFunction
*load
;
130 SCDynamicStoreBundleStartFunction
*start
;
131 SCDynamicStoreBundlePrimeFunction
*prime
;
132 SCDynamicStoreBundleStopFunction
*stop
;
133 } builtin
, *builtinRef
;
136 static const builtin builtin_plugins
[] = {
138 CFSTR("com.apple.SystemConfiguration.IPMonitor"),
144 #if !TARGET_OS_SIMULATOR
146 CFSTR("com.apple.SystemConfiguration.InterfaceNamer"),
153 CFSTR("com.apple.SystemConfiguration.KernelEventMonitor"),
154 load_KernelEventMonitor
,
156 prime_KernelEventMonitor
,
160 CFSTR("com.apple.SystemConfiguration.LinkConfiguration"),
161 load_LinkConfiguration
,
167 CFSTR("com.apple.SystemConfiguration.PreferencesMonitor"),
168 load_PreferencesMonitor
,
170 prime_PreferencesMonitor
,
174 CFSTR("com.apple.SystemConfiguration.QoSMarking"),
180 #endif // !TARGET_OS_SIMULATOR
185 addBundle(CFBundleRef bundle
, Boolean forceEnabled
)
187 CFDictionaryRef bundleDict
;
188 bundleInfoRef bundleInfo
;
190 bundleInfo
= CFAllocatorAllocate(NULL
, sizeof(*bundleInfo
), 0);
191 bundleInfo
->bundle
= (CFBundleRef
)CFRetain(bundle
);
192 bundleInfo
->loaded
= FALSE
;
193 bundleInfo
->builtin
= FALSE
;
194 bundleInfo
->enabled
= TRUE
;
195 bundleInfo
->forced
= forceEnabled
;
196 bundleInfo
->verbose
= FALSE
;
197 bundleInfo
->load
= NULL
;
198 bundleInfo
->start
= NULL
;
199 bundleInfo
->prime
= NULL
;
200 bundleInfo
->stop
= NULL
;
202 bundleDict
= CFBundleGetInfoDictionary(bundle
);
203 if (isA_CFDictionary(bundleDict
)) {
206 bVal
= CFDictionaryGetValue(bundleDict
, kSCBundleIsBuiltinKey
);
207 if (isA_CFBoolean(bVal
)) {
208 bundleInfo
->builtin
= CFBooleanGetValue(bVal
);
211 bVal
= CFDictionaryGetValue(bundleDict
, kSCBundleEnabledKey
);
212 if (isA_CFBoolean(bVal
)) {
213 bundleInfo
->enabled
= CFBooleanGetValue(bVal
);
216 bVal
= CFDictionaryGetValue(bundleDict
, kSCBundleVerboseKey
);
217 if (isA_CFBoolean(bVal
)) {
218 bundleInfo
->verbose
= CFBooleanGetValue(bVal
);
222 CFArrayAppendValue(allBundles
, bundleInfo
);
227 static CF_RETURNS_RETAINED CFStringRef
228 shortBundleIdentifier(CFStringRef bundleID
)
230 CFIndex len
= CFStringGetLength(bundleID
);
232 CFStringRef shortID
= NULL
;
234 if (CFStringFindWithOptions(bundleID
,
239 range
.location
= range
.location
+ range
.length
;
240 range
.length
= len
- range
.location
;
241 shortID
= CFStringCreateWithSubstring(NULL
, bundleID
, range
);
249 getBundleSymbol(CFBundleRef bundle
, CFStringRef functionName
, CFStringRef shortID
)
253 // search for load(), start(), prime(), stop(), ...
254 func
= CFBundleGetFunctionPointerForName(bundle
, functionName
);
259 if (shortID
!= NULL
) {
260 CFStringRef altFunctionName
;
262 // search for load_XXX(), ...
263 altFunctionName
= CFStringCreateWithFormat(NULL
,
268 func
= CFBundleGetFunctionPointerForName(bundle
, altFunctionName
);
269 CFRelease(altFunctionName
);
277 getBundleDirNameAndPath(CFBundleRef bundle
, char *buf
, size_t buf_len
)
284 url
= CFBundleCopyBundleURL(bundle
);
289 ok
= CFURLGetFileSystemRepresentation(url
, TRUE
, (UInt8
*)buf
, buf_len
);
295 cp
= strrchr(buf
, '/');
302 /* check if this directory entry is a valid bundle name */
304 if (len
<= sizeof(BUNDLE_DIR_EXTENSION
)) {
305 /* if entry name isn't long enough */
309 len
-= sizeof(BUNDLE_DIR_EXTENSION
) - 1;
310 if (strcmp(&cp
[len
], BUNDLE_DIR_EXTENSION
) != 0) {
311 /* if entry name doesn't end with ".bundle" */
324 loadBundle(const void *value
, void *context
) {
325 CFStringRef bundleID
;
326 Boolean bundleAllowed
;
327 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
328 Boolean bundleExclude
;
329 CFIndex
*nLoaded
= (CFIndex
*)context
;
332 bundleID
= CFBundleGetIdentifier(bundleInfo
->bundle
);
333 if (bundleID
== NULL
) {
334 // sorry, no bundles without a bundle identifier
335 SC_log(LOG_NOTICE
, "skipped %@ (no bundle ID)", bundleInfo
->bundle
);
339 shortID
= shortBundleIdentifier(bundleID
);
341 bundleAllowed
= ((CFSetGetCount(_plugins_allowed
) == 0) || // if no white-listing
342 CFSetContainsValue(_plugins_allowed
, bundleID
) || // if [bundleID] white-listed
343 ((shortID
!= NULL
) &&
344 CFSetContainsValue(_plugins_allowed
, shortID
))|| // if [short bundleID] white-listed
345 bundleInfo
->forced
// if "testing" plugin
347 if (!bundleAllowed
) {
348 SC_log(LOG_INFO
, "skipped %@ (not allowed)", bundleID
);
352 bundleExclude
= (CFSetContainsValue(_plugins_exclude
, bundleID
) || // if [bundleID] excluded
353 ((shortID
!= NULL
) &&
354 CFSetContainsValue(_plugins_exclude
, shortID
)) // if [short bundleID] excluded
357 // sorry, this bundle has been excluded
358 SC_log(LOG_INFO
, "skipped %@ (excluded)", bundleID
);
362 if (!bundleInfo
->enabled
&& !bundleInfo
->forced
) {
363 // sorry, this bundle has not been enabled
364 SC_log(LOG_INFO
, "skipped %@ (disabled)", bundleID
);
368 if (!bundleInfo
->verbose
) {
369 bundleInfo
->verbose
= CFSetContainsValue(_plugins_verbose
, bundleID
);
370 if (!bundleInfo
->verbose
) {
371 if (shortID
!= NULL
) {
372 bundleInfo
->verbose
= CFSetContainsValue(_plugins_verbose
, shortID
);
377 if (bundleInfo
->builtin
) {
378 SC_log(LOG_INFO
, "adding %@", bundleID
);
380 for (size_t i
= 0; i
< sizeof(builtin_plugins
)/sizeof(builtin_plugins
[0]); i
++) {
381 if (CFEqual(bundleID
, builtin_plugins
[i
].bundleID
)) {
382 bundleInfo
->load
= builtin_plugins
[i
].load
;
383 bundleInfo
->start
= builtin_plugins
[i
].start
;
384 bundleInfo
->prime
= builtin_plugins
[i
].prime
;
385 bundleInfo
->stop
= builtin_plugins
[i
].stop
;
390 if ((bundleInfo
->load
== NULL
) &&
391 (bundleInfo
->start
== NULL
) &&
392 (bundleInfo
->prime
== NULL
) &&
393 (bundleInfo
->stop
== NULL
)) {
394 SC_log(LOG_NOTICE
, "%@ add failed", bundleID
);
398 CFErrorRef error
= NULL
;
400 SC_log(LOG_INFO
, "loading %@", bundleID
);
402 if (!CFBundleLoadExecutableAndReturnError(bundleInfo
->bundle
, &error
)) {
403 CFDictionaryRef user_info
;
405 SC_log(LOG_NOTICE
, "%@ load failed", bundleID
);
406 user_info
= CFErrorCopyUserInfo(error
);
407 if (user_info
!= NULL
) {
408 CFStringRef link_error_string
;
410 link_error_string
= CFDictionaryGetValue(user_info
,
411 CFSTR("NSDebugDescription"));
412 if (link_error_string
!= NULL
) {
413 SC_log(LOG_NOTICE
, "%@", link_error_string
);
415 CFRelease(user_info
);
421 // get bundle entry points
422 bundleInfo
->load
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("load" ), shortID
);
423 bundleInfo
->start
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("start"), shortID
);
424 bundleInfo
->prime
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("prime"), shortID
);
425 bundleInfo
->stop
= getBundleSymbol(bundleInfo
->bundle
, CFSTR("stop" ), shortID
);
428 /* mark this bundle as having been loaded */
429 bundleInfo
->loaded
= TRUE
;
431 /* bump the count of loaded bundles */
432 *nLoaded
= *nLoaded
+ 1;
436 if (shortID
!= NULL
) CFRelease(shortID
);
442 callLoadFunction(const void *value
, void *context
)
444 #pragma unused(context)
445 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
447 if (!bundleInfo
->loaded
) {
451 if (bundleInfo
->load
== NULL
) {
452 // if no load() function
456 SC_log(LOG_DEBUG
, "calling load() for %@",
457 CFBundleGetIdentifier(bundleInfo
->bundle
));
459 (*bundleInfo
->load
)(bundleInfo
->bundle
, bundleInfo
->verbose
);
470 callStartFunction(const void *value
, void *context
)
472 #pragma unused(context)
473 const char *bundleDirName
;
474 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
475 char bundleName
[MAXNAMLEN
+ 1];
476 char bundlePath
[MAXPATHLEN
];
479 if (!bundleInfo
->loaded
) {
483 if (bundleInfo
->start
== NULL
) {
484 // if no start() function
488 /* copy the bundle's path */
489 bundleDirName
= getBundleDirNameAndPath(bundleInfo
->bundle
, bundlePath
, sizeof(bundlePath
));
490 if (bundleDirName
== NULL
) {
491 // if we have a problem with the bundle's path
495 /* copy (just) the bundle's name */
496 if (strlcpy(bundleName
, bundleDirName
, sizeof(bundleName
)) > sizeof(bundleName
)) {
497 // if we have a problem with the bundle's name
500 len
= strlen(bundleName
) - (sizeof(BUNDLE_DIR_EXTENSION
) - 1);
501 bundleName
[len
] = '\0';
503 SC_log(LOG_DEBUG
, "calling start() for %@",
504 CFBundleGetIdentifier(bundleInfo
->bundle
));
506 (*bundleInfo
->start
)(bundleName
, bundlePath
);
517 callPrimeFunction(const void *value
, void *context
)
519 #pragma unused(context)
520 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
522 if (!bundleInfo
->loaded
) {
526 if (bundleInfo
->prime
== NULL
) {
527 // if no prime() function
531 SC_log(LOG_DEBUG
, "calling prime() for %@",
532 CFBundleGetIdentifier(bundleInfo
->bundle
));
534 (*bundleInfo
->prime
)();
545 stopComplete(void *info
)
547 CFBundleRef bundle
= (CFBundleRef
)info
;
548 CFStringRef bundleID
= CFBundleGetIdentifier(bundle
);
549 CFRunLoopSourceRef stopRls
;
551 SC_log(LOG_INFO
, "** %@ complete (%f)", bundleID
, CFAbsoluteTimeGetCurrent());
553 stopRls
= (CFRunLoopSourceRef
)CFDictionaryGetValue(exiting
, bundle
);
554 if (stopRls
== NULL
) {
558 CFRunLoopSourceInvalidate(stopRls
);
560 CFDictionaryRemoveValue(exiting
, bundle
);
562 if (CFDictionaryGetCount(exiting
) == 0) {
563 // if all of the plugins are happy
564 SC_log(LOG_INFO
, "server shutdown complete (%f)", CFAbsoluteTimeGetCurrent());
573 stopDelayed(CFRunLoopTimerRef timer
, void *info
)
575 #pragma unused(timer)
581 SC_log(LOG_INFO
, "server shutdown was delayed, unresponsive plugins:");
584 * we've asked our plugins to shutdown but someone
587 n
= CFDictionaryGetCount(exiting
);
588 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
589 CFDictionaryGetKeysAndValues(exiting
, keys
, NULL
);
590 for (i
= 0; i
< n
; i
++) {
592 CFStringRef bundleID
;
594 bundle
= (CFBundleRef
)keys
[i
];
595 bundleID
= CFBundleGetIdentifier(bundle
);
596 SC_log(LOG_NOTICE
, "** %@", bundleID
);
598 CFAllocatorDeallocate(NULL
, keys
);
604 stopRLSCopyDescription(const void *info
)
606 CFBundleRef bundle
= (CFBundleRef
)info
;
608 return CFStringCreateWithFormat(NULL
,
610 CFSTR("<stopRLS %p> {bundleID = %@}"),
612 CFBundleGetIdentifier(bundle
));
617 stopBundle(const void *value
, void *context
)
619 #pragma unused(context)
620 bundleInfoRef bundleInfo
= (bundleInfoRef
)value
;
621 CFRunLoopSourceRef stopRls
;
622 CFRunLoopSourceContext stopContext
= { 0 // version
623 , bundleInfo
->bundle
// info
625 , CFRelease
// release
626 , stopRLSCopyDescription
// copyDescription
631 , stopComplete
// perform
634 if (!bundleInfo
->loaded
) {
638 if (bundleInfo
->stop
== NULL
) {
639 // if no stop() function
643 stopRls
= CFRunLoopSourceCreate(NULL
, 0, &stopContext
);
644 CFRunLoopAddSource(CFRunLoopGetCurrent(), stopRls
, kCFRunLoopDefaultMode
);
645 CFDictionaryAddValue(exiting
, bundleInfo
->bundle
, stopRls
);
646 (*bundleInfo
->stop
)(stopRls
);
657 * If defined, call each bundles stop() function. This function is
658 * called when configd has been asked to shut down (via a SIGTERM). The
659 * function should signal the provided run loop source when it is "ready"
660 * for the shut down to proceeed.
662 SC_log(LOG_DEBUG
, "calling bundle stop() functions");
663 CFArrayApplyFunction(allBundles
,
664 CFRangeMake(0, CFArrayGetCount(allBundles
)),
668 if (CFDictionaryGetCount(exiting
) == 0) {
669 // if all of the plugins are happy
670 SC_log(LOG_INFO
, "server shutdown complete (%f)", CFAbsoluteTimeGetCurrent());
673 CFRunLoopTimerRef timer
;
676 * launchd will only wait 20 seconds before sending us a
677 * SIGKILL and because we want to know what's stuck before
678 * that time so set our own "we're not waiting any longer"
679 * timeout for 15 seconds.
681 timer
= CFRunLoopTimerCreate(NULL
, /* allocator */
682 CFAbsoluteTimeGetCurrent() + 15.0, /* fireDate (in 15 seconds) */
683 0.0, /* interval (== one-shot) */
686 stopDelayed
, /* callout */
688 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer
, kCFRunLoopDefaultMode
);
701 termRLSCopyDescription(const void *info
)
704 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SIGTERM RLS>"));
710 plugin_term(int *status
)
712 CFRunLoopSourceContext termContext
= { 0 // version
716 , termRLSCopyDescription
// copyDescription
721 , stopBundles
// perform
723 CFRunLoopSourceRef termRls
;
725 if (plugin_runLoop
== NULL
) {
728 return FALSE
; // don't delay shutdown
731 if (exiting
!= NULL
) {
732 // if shutdown already active
736 SC_log(LOG_INFO
, "starting server shutdown (%f)", CFAbsoluteTimeGetCurrent());
738 exiting
= CFDictionaryCreateMutable(NULL
,
740 &kCFTypeDictionaryKeyCallBacks
,
741 &kCFTypeDictionaryValueCallBacks
);
743 termRls
= CFRunLoopSourceCreate(NULL
, 0, &termContext
);
744 CFRunLoopAddSource(plugin_runLoop
, termRls
, kCFRunLoopDefaultMode
);
745 CFRunLoopSourceSignal(termRls
);
747 CFRunLoopWakeUp(plugin_runLoop
);
754 #pragma mark initialization
758 sortBundles(CFMutableArrayRef orig
)
762 CFMutableArrayRef
new;
763 CFMutableSetRef orig_bundleIDs
;
765 orig_bundleIDs
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
767 n
= CFArrayGetCount(orig
);
768 for (i
= 0; i
< n
; i
++) {
769 bundleInfoRef bundleInfo
= (bundleInfoRef
)CFArrayGetValueAtIndex(orig
, i
);
770 CFStringRef bundleID
= CFBundleGetIdentifier(bundleInfo
->bundle
);
772 if (bundleID
!= NULL
) {
773 CFSetAddValue(orig_bundleIDs
, bundleID
);
777 new = CFArrayCreateMutable(NULL
, 0, NULL
);
779 Boolean inserted
= FALSE
;
781 for (i
= 0; i
< n
; i
++) {
782 bundleInfoRef bundleInfo1
= (bundleInfoRef
)CFArrayGetValueAtIndex(orig
, i
);
783 CFStringRef bundleID1
= CFBundleGetIdentifier(bundleInfo1
->bundle
);
785 CFDictionaryRef dict
;
788 CFArrayRef
requires = NULL
;
790 dict
= isA_CFDictionary(CFBundleGetInfoDictionary(bundleInfo1
->bundle
));
792 requires = CFDictionaryGetValue(dict
, kSCBundleRequiresKey
);
793 requires = isA_CFArray(requires);
795 if (bundleID1
== NULL
|| requires == NULL
) {
796 CFArrayInsertValueAtIndex(new, 0, bundleInfo1
);
797 CFArrayRemoveValueAtIndex(orig
, i
);
801 count
= nRequires
= CFArrayGetCount(requires);
802 for (j
= 0; j
< nRequires
; j
++) {
805 CFStringRef r
= CFArrayGetValueAtIndex(requires, j
);
807 if (!CFSetContainsValue(orig_bundleIDs
, r
)) {
808 // if dependency not present
813 nNew
= CFArrayGetCount(new);
814 for (k
= 0; k
< nNew
; k
++) {
815 bundleInfoRef bundleInfo2
= (bundleInfoRef
)CFArrayGetValueAtIndex(new, k
);
816 CFStringRef bundleID2
= CFBundleGetIdentifier(bundleInfo2
->bundle
);
818 if (bundleID2
&& CFEqual(bundleID2
, r
)) {
824 /* all dependencies are met, append */
825 CFArrayAppendValue(new, bundleInfo1
);
826 CFArrayRemoveValueAtIndex(orig
, i
);
833 SC_log(LOG_NOTICE
, "Bundles have circular dependency!!!");
837 n
= CFArrayGetCount(orig
);
839 if (CFArrayGetCount(orig
) > 0) {
840 /* we have a circular dependency, append remaining items on new array */
841 CFArrayAppendArray(new, orig
, CFRangeMake(0, CFArrayGetCount(orig
)));
844 /* new one is a sorted version of original */
847 CFArrayRemoveAllValues(orig
);
848 CFArrayAppendArray(orig
, new, CFRangeMake(0, CFArrayGetCount(new)));
850 CFRelease(orig_bundleIDs
);
858 * An alternate CFRelease() that we can use to fake out the
861 static __inline__
void
862 ALT_CFRelease(CFTypeRef cf
)
870 plugin_exec(void *arg
)
874 /* keep track of bundles */
875 allBundles
= CFArrayCreateMutable(NULL
, 0, NULL
);
877 /* add white-listed plugins to those we'll allow to be loaded */
878 for (size_t i
= 0; i
< N_PLUGIN_WHITELIST
; i
++) {
879 if (pluginWhitelist
[i
] != NULL
) {
880 CFSetSetValue(_plugins_allowed
, pluginWhitelist
[i
]);
884 /* allow plug-ins to exec child/helper processes */
885 _SCDPluginExecInit();
888 char path
[MAXPATHLEN
];
889 sysdir_search_path_enumeration_state state
;
892 * identify and load all bundles
894 state
= sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_LIBRARY
,
895 SYSDIR_DOMAIN_MASK_SYSTEM
);
896 while ((state
= sysdir_get_next_search_path_enumeration(state
, path
))) {
900 #if TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC
901 const char *path_sim_prefix
;
903 path_sim_prefix
= getenv("IPHONE_SIMULATOR_ROOT");
904 if ((path_sim_prefix
!= NULL
) && (strcmp(path_sim_prefix
, ".") != 0)) {
905 char path_sim
[MAXPATHLEN
];
907 strlcpy(path_sim
, path_sim_prefix
, sizeof(path_sim
));
908 strlcat(path_sim
, path
, sizeof(path_sim
));
909 strlcpy(path
, path_sim
, sizeof(path
));
913 #endif // TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC
915 /* load any available bundle */
916 strlcat(path
, BUNDLE_DIRECTORY
, sizeof(path
));
917 SC_log(LOG_DEBUG
, "searching for bundles in \"%s\"", path
);
918 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
922 bundles
= CFBundleCreateBundlesFromDirectory(NULL
, url
, CFSTR(".bundle"));
925 if (bundles
!= NULL
) {
929 n
= CFArrayGetCount(bundles
);
930 for (i
= 0; i
< n
; i
++) {
933 bundle
= (CFBundleRef
)CFArrayGetValueAtIndex(bundles
, i
);
934 addBundle(bundle
, FALSE
);
936 // The CFBundleCreateBundlesFromDirectory() API has
937 // a known/outstanding bug in that it over-retains the
938 // returned bundles. Since we do not expect this to
939 // be fixed we release the extra references.
941 // See <rdar://problems/4912137&6078752> for more info.
943 // Also, we use the hack below to keep the static
945 ALT_CFRelease(bundle
);
951 sortBundles(allBundles
);
957 * load (only) the specified bundle
959 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
963 bundle
= CFBundleCreate(NULL
, url
);
964 if (bundle
!= NULL
) {
965 addBundle(bundle
, TRUE
);
972 * Look for the InterfaceNamer plugin, and move it to the start
975 * Load the InterfaceNamer plugin (and thereby start its thread)
976 * first in an attempt to minimize the amount of time that
977 * opendirectoryd has to wait for the platform UUID to appear in
980 * InterfaceNamer is responsible for creating the platform UUID on
981 * platforms without a UUID in ROM. Until the platform UUID is created
982 * and stashed in nvram, all calls to opendirectoryd to do things like
983 * getpwuid() will block, because opendirectoryd will block while trying
984 * to read the platform UUID from the kernel.
986 * As an example, dlopen() causes XPC to do some intialization, and
987 * part of that initialization involves communicating with xpcd.
988 * Since xpcd calls getpwuid_r() during its initialization, it will
989 * block until the platform UUID is available.
991 for (CFIndex i
= 0; i
< CFArrayGetCount(allBundles
); i
++) {
992 bundleInfoRef bi
= (bundleInfoRef
)CFArrayGetValueAtIndex(allBundles
, i
);
993 CFStringRef bundleID
= CFBundleGetIdentifier(bi
->bundle
);
995 if (_SC_CFEqual(bundleID
,
996 CFSTR("com.apple.SystemConfiguration.InterfaceNamer")))
998 CFArrayRemoveValueAtIndex(allBundles
, i
);
999 CFArrayInsertValueAtIndex(allBundles
, 0, bi
);
1007 SC_log(LOG_DEBUG
, "loading bundles");
1008 CFArrayApplyFunction(allBundles
,
1009 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1014 * If defined, call each bundles load() function. This function (or
1015 * the start() function) should initialize any variables, open any
1016 * sessions with "configd", and register any needed notifications.
1018 * Note: Establishing initial information in the store should be
1019 * deferred until the prime() initialization function so that
1020 * any bundles which want to receive a notification that the
1021 * data has changed will have an opportunity to install a
1022 * notification handler.
1024 SC_log(LOG_DEBUG
, "calling bundle load() functions");
1025 CFArrayApplyFunction(allBundles
,
1026 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1031 // if no bundles loaded
1036 * If defined, call each bundles start() function. This function is
1037 * called after the bundle has been loaded and its load() function has
1038 * been called. It should initialize any variables, open any sessions
1039 * with "configd", and register any needed notifications.
1041 * Note: Establishing initial information in the store should be
1042 * deferred until the prime() initialization function so that
1043 * any bundles which want to receive a notification that the
1044 * data has changed will have an opportunity to install a
1045 * notification handler.
1047 SC_log(LOG_DEBUG
, "calling bundle start() functions");
1048 CFArrayApplyFunction(allBundles
,
1049 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1054 * If defined, call each bundles prime() function. This function is
1055 * called after the bundle has been loaded and its load() and start()
1056 * functions have been called. It should initialize any configuration
1057 * information and/or state in the store.
1059 SC_log(LOG_DEBUG
, "calling bundle prime() functions");
1060 CFArrayApplyFunction(allBundles
,
1061 CFRangeMake(0, CFArrayGetCount(allBundles
)),
1066 * The assumption is that each loaded plugin will establish CFMachPortRef,
1067 * CFSocketRef, and CFRunLoopTimerRef input sources to handle any events
1068 * and register these sources with this threads run loop. If the plugin
1069 * needs to wait and/or block at any time it should do so only in its a
1072 SC_log(LOG_DEBUG
, "starting plugin CFRunLoop");
1073 plugin_runLoop
= CFRunLoopGetCurrent();
1074 pthread_setname_np("Main plugin thread");
1079 SC_log(LOG_INFO
, "No more work for the \"configd\" plugin thread");
1080 plugin_runLoop
= NULL
;
1089 pthread_attr_t tattr
;
1092 SC_log(LOG_DEBUG
, "Starting \"configd\" plugin thread");
1093 pthread_attr_init(&tattr
);
1094 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
1095 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
1096 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
1097 pthread_create(&tid
, &tattr
, plugin_exec
, NULL
);
1098 pthread_attr_destroy(&tattr
);
1099 SC_log(LOG_DEBUG
, " thread id=%p", tid
);