2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
26 * June 11, 2001 Allan Nathanson <ajn@apple.com>
27 * - start using CFBundle code
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * May 26, 2000 Allan Nathanson <ajn@apple.com>
36 #include <mach-o/dyld.h>
37 #include <sys/types.h>
39 #include <sys/param.h>
42 #include <NSSystemDirectories.h>
45 #include <SystemConfiguration/SCDPlugin.h>
49 * path components, extensions, entry points, ...
51 #define BUNDLE_DIRECTORY "/SystemConfiguration" /* [/System/Library]/... */
52 #define BUNDLE_DIR_EXTENSION ".bundle"
55 static CFMutableArrayRef allBundles
= NULL
;
58 /* exception handling functions */
59 typedef kern_return_t (*cer_func_t
) (mach_port_t exception_port
,
62 exception_type_t exception
,
63 exception_data_t code
,
64 mach_msg_type_number_t codeCnt
);
66 typedef kern_return_t (*cer_state_func_t
) (mach_port_t exception_port
,
67 exception_type_t exception
,
68 exception_data_t code
,
69 mach_msg_type_number_t codeCnt
,
71 thread_state_t old_state
,
72 mach_msg_type_number_t old_stateCnt
,
73 thread_state_t new_state
,
74 mach_msg_type_number_t
*new_stateCnt
);
76 typedef kern_return_t (*cer_identity_func_t
) (mach_port_t exception_port
,
79 exception_type_t exception
,
80 exception_data_t code
,
81 mach_msg_type_number_t codeCnt
,
83 thread_state_t old_state
,
84 mach_msg_type_number_t old_stateCnt
,
85 thread_state_t new_state
,
86 mach_msg_type_number_t
*new_stateCnt
);
88 static cer_func_t catch_exception_raise_func
= NULL
;
89 static cer_state_func_t catch_exception_raise_state_func
= NULL
;
90 static cer_identity_func_t catch_exception_raise_identity_func
= NULL
;
93 catch_exception_raise(mach_port_t exception_port
,
96 exception_type_t exception
,
97 exception_data_t code
,
98 mach_msg_type_number_t codeCnt
)
101 if (catch_exception_raise_func
== NULL
) {
102 /* The user hasn't defined catch_exception_raise in their binary */
105 return (*catch_exception_raise_func
)(exception_port
,
115 catch_exception_raise_state(mach_port_t exception_port
,
116 exception_type_t exception
,
117 exception_data_t code
,
118 mach_msg_type_number_t codeCnt
,
120 thread_state_t old_state
,
121 mach_msg_type_number_t old_stateCnt
,
122 thread_state_t new_state
,
123 mach_msg_type_number_t
*new_stateCnt
)
125 if (catch_exception_raise_state_func
== 0) {
126 /* The user hasn't defined catch_exception_raise_state in their binary */
129 return (*catch_exception_raise_state_func
)(exception_port
,
142 catch_exception_raise_state_identity(mach_port_t exception_port
,
145 exception_type_t exception
,
146 exception_data_t code
,
147 mach_msg_type_number_t codeCnt
,
149 thread_state_t old_state
,
150 mach_msg_type_number_t old_stateCnt
,
151 thread_state_t new_state
,
152 mach_msg_type_number_t
*new_stateCnt
)
154 if (catch_exception_raise_identity_func
== 0) {
155 /* The user hasn't defined catch_exception_raise_identify in their binary */
158 return (*catch_exception_raise_identity_func
)(exception_port
,
173 shortBundleIdentifier(CFStringRef bundleID
)
175 CFIndex len
= CFStringGetLength(bundleID
);
177 CFStringRef shortID
= NULL
;
179 if (CFStringFindWithOptions(bundleID
,
184 range
.location
= range
.location
+ range
.length
;
185 range
.length
= len
- range
.location
;
186 shortID
= CFStringCreateWithSubstring(NULL
, bundleID
, range
);
194 loadBundle(const void *value
, void *context
) {
195 CFBundleRef bundle
= (CFBundleRef
)value
;
196 CFStringRef bundleID
= CFBundleGetIdentifier(bundle
);
197 Boolean bundleExclude
= FALSE
;
198 Boolean bundleVerbose
= FALSE
;
199 CFDictionaryRef dict
;
201 SCDynamicStoreBundleLoadFunction load
;
203 CFIndex
*nLoaded
= (CFIndex
*)context
;
205 SCLog(TRUE
, LOG_DEBUG
, CFSTR("loading %@"), bundleID
);
207 bundleExclude
= CFSetContainsValue(_plugins_exclude
, bundleID
);
208 if (!bundleExclude
) {
209 CFStringRef shortID
= shortBundleIdentifier(bundleID
);
212 bundleExclude
= CFSetContainsValue(_plugins_exclude
, shortID
);
220 CFSTR("%@ load skipped"),
225 loaded
= CFBundleLoadExecutable(bundle
);
229 CFSTR("%@ load failed"),
234 if (!CFBundleIsExecutableLoaded(bundle
)) {
237 CFSTR("%@ executable not loaded"),
242 /* bump the count of loaded bundles */
243 *nLoaded
= *nLoaded
+ 1;
245 /* identify any exception handling functions */
247 func
= CFBundleGetFunctionPointerForName(bundle
, CFSTR("catch_exception_raise"));
249 catch_exception_raise_func
= func
;
252 func
= CFBundleGetFunctionPointerForName(bundle
, CFSTR("catch_exception_raise_state"));
254 catch_exception_raise_state_func
= func
;
257 func
= CFBundleGetFunctionPointerForName(bundle
, CFSTR("catch_exception_raise_identity"));
259 catch_exception_raise_identity_func
= func
;
262 /* if defined, call the bundles load() function */
264 load
= CFBundleGetFunctionPointerForName(bundle
, CFSTR("load"));
269 bundleVerbose
= CFSetContainsValue(_plugins_verbose
, bundleID
);
270 if (!bundleVerbose
) {
271 CFStringRef shortID
= shortBundleIdentifier(bundleID
);
274 bundleVerbose
= CFSetContainsValue(_plugins_verbose
, shortID
);
279 if (!bundleVerbose
) {
280 dict
= CFBundleGetInfoDictionary(bundle
);
281 if (isA_CFDictionary(dict
)) {
284 bool = CFDictionaryGetValue(dict
, kSCBundleVerbose
);
285 if (isA_CFBoolean(bool) && CFBooleanGetValue(bool)) {
286 bundleVerbose
= TRUE
;
291 (*load
)(bundle
, bundleVerbose
);
297 startBundle(const void *value
, void *context
) {
298 CFBundleRef bundle
= (CFBundleRef
)value
;
300 char bundleName
[MAXNAMLEN
+ 1];
301 char bundlePath
[MAXPATHLEN
];
303 CFDictionaryRef dict
;
306 SCDynamicStoreBundleStartFunction start
;
308 if (!CFBundleIsExecutableLoaded(bundle
)) {
312 start
= CFBundleGetFunctionPointerForName(bundle
, CFSTR("start"));
317 dict
= isA_CFDictionary(CFBundleGetInfoDictionary(bundle
));
322 bundleURL
= CFBundleCopyBundleURL(bundle
);
327 ok
= CFURLGetFileSystemRepresentation(bundleURL
,
329 (UInt8
*)&bundlePath
,
331 CFRelease(bundleURL
);
336 cp
= strrchr(bundlePath
, '/');
343 /* check if this directory entry is a valid bundle name */
345 if (len
<= sizeof(BUNDLE_DIR_EXTENSION
)) {
346 /* if entry name isn't long enough */
350 len
-= sizeof(BUNDLE_DIR_EXTENSION
) - 1;
351 if (strcmp(&cp
[len
], BUNDLE_DIR_EXTENSION
) != 0) {
352 /* if entry name doesn end with ".bundle" */
356 /* get (just) the bundle's name */
357 bundleName
[0] = '\0';
358 (void) strncat(bundleName
, cp
, len
);
360 (*start
)(bundleName
, bundlePath
);
366 primeBundle(const void *value
, void *context
) {
367 CFBundleRef bundle
= (CFBundleRef
)value
;
368 SCDynamicStoreBundlePrimeFunction prime
;
370 if (!CFBundleIsExecutableLoaded(bundle
)) {
374 prime
= CFBundleGetFunctionPointerForName(bundle
, CFSTR("prime"));
385 timerCallback(CFRunLoopTimerRef timer
, void *info
)
387 SCLog(_configd_verbose
,
389 CFSTR("the CFRunLoop is waiting for something to happen...."));
395 sortBundles(CFMutableArrayRef orig
)
397 CFMutableArrayRef
new;
399 new = CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
400 while (CFArrayGetCount(orig
) > 0) {
402 Boolean inserted
= FALSE
;
404 for (i
= 0; i
< CFArrayGetCount(orig
); i
++) {
405 CFBundleRef bundle1
= CFArrayGetValueAtIndex(orig
, i
);
406 CFStringRef bundleID1
= CFBundleGetIdentifier(bundle1
);
408 CFDictionaryRef dict
;
410 CFArrayRef
requires = NULL
;
412 dict
= isA_CFDictionary(CFBundleGetInfoDictionary(bundle1
));
414 requires = CFDictionaryGetValue(dict
, kSCBundleRequires
);
415 requires = isA_CFArray(requires);
417 if (bundleID1
== NULL
|| requires == NULL
) {
418 CFArrayInsertValueAtIndex(new, 0, bundle1
);
419 CFArrayRemoveValueAtIndex(orig
, i
);
423 count
= CFArrayGetCount(requires);
424 for (j
= 0; j
< CFArrayGetCount(requires); j
++) {
426 CFStringRef r
= CFArrayGetValueAtIndex(requires, j
);
428 for (k
= 0; k
< CFArrayGetCount(new); k
++) {
429 CFBundleRef bundle2
= CFArrayGetValueAtIndex(new, k
);
430 CFStringRef bundleID2
= CFBundleGetIdentifier(bundle2
);
432 if (bundleID2
&& CFEqual(bundleID2
, r
)) {
438 /* all dependencies are met, append */
439 CFArrayAppendValue(new, bundle1
);
440 CFArrayRemoveValueAtIndex(orig
, i
);
446 if (inserted
== FALSE
) {
447 SCLog(TRUE
, LOG_NOTICE
, CFSTR("Bundles have circular dependency!!!"));
451 if (CFArrayGetCount(orig
) > 0) {
452 /* we have a circular dependency, append remaining items on new array */
453 CFArrayAppendArray(new, orig
, CFRangeMake(0, CFArrayGetCount(orig
)));
456 /* new one is a sorted version of original */
459 CFArrayRemoveAllValues(orig
);
460 CFArrayAppendArray(orig
, new, CFRangeMake(0, CFArrayGetCount(new)));
467 plugin_exec(void *arg
)
471 /* keep track of bundles */
472 allBundles
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
475 char path
[MAXPATHLEN
];
476 NSSearchPathEnumerationState state
;
479 * identify and load all bundles
481 state
= NSStartSearchPathEnumeration(NSLibraryDirectory
,
482 NSLocalDomainMask
|NSSystemDomainMask
);
483 while ((state
= NSGetNextSearchPathEnumeration(state
, path
))) {
487 /* load any available bundle */
488 strcat(path
, BUNDLE_DIRECTORY
);
489 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("searching for bundles in \".\""));
490 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
494 bundles
= CFBundleCreateBundlesFromDirectory(NULL
, url
, CFSTR(".bundle"));
498 CFArrayAppendArray(allBundles
,
500 CFRangeMake(0, CFArrayGetCount(bundles
)));
505 sortBundles(allBundles
);
511 * load (only) the specified bundle
513 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
517 bundle
= CFBundleCreate(NULL
, url
);
519 CFArrayAppendValue(allBundles
, bundle
);
526 * load each bundle and, if defined, call its load() function. This
527 * function (or the start() function) should initialize any variables,
528 * open any sessions with "configd", and register any needed notifications.
530 * Note: Establishing initial information in the store should be
531 * deferred until the prime() initialization function so that
532 * any bundles which want to receive a notification that the
533 * data has changed will have an opportunity to install a
534 * notification handler.
536 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle load() functions"));
537 CFArrayApplyFunction(allBundles
,
538 CFRangeMake(0, CFArrayGetCount(allBundles
)),
543 * If defined, call each bundles start() function. This function is
544 * called after the bundle has been loaded and its load() function has
545 * been called. It should initialize any variables, open any sessions
546 * with "configd", and register any needed notifications.
548 * Note: Establishing initial information in the store should be
549 * deferred until the prime() initialization function so that
550 * any bundles which want to receive a notification that the
551 * data has changed will have an opportunity to install a
552 * notification handler.
554 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle start() functions"));
555 CFArrayApplyFunction(allBundles
,
556 CFRangeMake(0, CFArrayGetCount(allBundles
)),
561 * If defined, call each bundles prime() function. This function is
562 * called after the bundle has been loaded and its load() and start()
563 * functions have been called. It should initialize any configuration
564 * information and/or state in the store.
566 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("calling bundle prime() functions"));
567 CFArrayApplyFunction(allBundles
,
568 CFRangeMake(0, CFArrayGetCount(allBundles
)),
572 if (arg
== NULL
&& (nLoaded
> 0)) {
573 CFRunLoopTimerRef timer
;
575 /* allocate a periodic event (to help show we're not blocking) */
576 timer
= CFRunLoopTimerCreate(NULL
, /* allocator */
577 CFAbsoluteTimeGetCurrent() + 1.0, /* fireDate */
581 timerCallback
, /* callout */
583 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer
, kCFRunLoopDefaultMode
);
588 /* synchronize with parent process */
589 kill(getppid(), SIGTERM
);
593 * The assumption is that each loaded plugin will establish CFMachPortRef,
594 * CFSocketRef, and CFRunLoopTimerRef input sources to handle any events
595 * and register these sources with this threads run loop. If the plugin
596 * needs to wait and/or block at any time it should do so only in its a
599 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("starting plugin CFRunLoop"));
601 SCLog(_configd_verbose
, LOG_INFO
, CFSTR("what, no more work for the \"configd\" bundles?"));
609 pthread_attr_t tattr
;
612 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Starting thread for plug-ins..."));
613 pthread_attr_init(&tattr
);
614 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
615 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
616 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
617 pthread_create(&tid
, &tattr
, plugin_exec
, NULL
);
618 pthread_attr_destroy(&tattr
);
619 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" thread id=0x%08x"), tid
);