]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/plugin_support.c
configd-1109.40.9.tar.gz
[apple/configd.git] / configd.tproj / plugin_support.c
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * October 30, 2003 Allan Nathanson <ajn@apple.com>
28 * - add plugin "stop()" function support
29 *
30 * June 11, 2001 Allan Nathanson <ajn@apple.com>
31 * - start using CFBundle code
32 *
33 * June 1, 2001 Allan Nathanson <ajn@apple.com>
34 * - public API conversion
35 *
36 * May 26, 2000 Allan Nathanson <ajn@apple.com>
37 * - initial revision
38 */
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/param.h>
43 #include <sys/wait.h>
44 #include <dirent.h>
45 #include <sysdir.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48
49 #include "configd.h"
50 #include "configd_server.h"
51 #include <SystemConfiguration/SCDPlugin.h>
52 #include "SystemConfigurationInternal.h"
53
54
55 /*
56 * path components, extensions, entry points, ...
57 */
58 #define BUNDLE_DIRECTORY "/SystemConfiguration" /* [/System/Library]/... */
59 #define BUNDLE_DIR_EXTENSION ".bundle"
60
61
62 #define PLUGIN_ALL(p) CFSTR(p)
63 #if !TARGET_OS_IPHONE
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
70
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"),
85 };
86 #define N_PLUGIN_WHITELIST (sizeof(pluginWhitelist) / sizeof(pluginWhitelist[0]))
87
88
89 typedef struct {
90 CFBundleRef bundle;
91 Boolean loaded;
92 Boolean builtin;
93 Boolean enabled;
94 Boolean forced;
95 Boolean verbose;
96 SCDynamicStoreBundleLoadFunction *load;
97 SCDynamicStoreBundleStartFunction *start;
98 SCDynamicStoreBundlePrimeFunction *prime;
99 SCDynamicStoreBundleStopFunction *stop;
100 } *bundleInfoRef;
101
102
103 // all loaded bundles
104 static CFMutableArrayRef allBundles = NULL;
105
106 // exiting bundles
107 static CFMutableDictionaryRef exiting = NULL;
108
109
110 extern SCDynamicStoreBundleLoadFunction load_IPMonitor;
111 extern SCDynamicStoreBundlePrimeFunction prime_IPMonitor;
112 #if !TARGET_OS_SIMULATOR
113 extern SCDynamicStoreBundleLoadFunction load_InterfaceNamer;
114 extern SCDynamicStoreBundleLoadFunction load_KernelEventMonitor;
115 extern SCDynamicStoreBundlePrimeFunction prime_KernelEventMonitor;
116 extern SCDynamicStoreBundleLoadFunction load_LinkConfiguration;
117 extern SCDynamicStoreBundleLoadFunction load_PreferencesMonitor;
118 extern SCDynamicStoreBundlePrimeFunction prime_PreferencesMonitor;
119 extern SCDynamicStoreBundleLoadFunction load_QoSMarking;
120 #endif // !TARGET_OS_SIMULATOR
121
122
123 typedef struct {
124 const CFStringRef bundleID;
125 SCDynamicStoreBundleLoadFunction *load;
126 SCDynamicStoreBundleStartFunction *start;
127 SCDynamicStoreBundlePrimeFunction *prime;
128 SCDynamicStoreBundleStopFunction *stop;
129 } builtin, *builtinRef;
130
131
132 static const builtin builtin_plugins[] = {
133 {
134 CFSTR("com.apple.SystemConfiguration.IPMonitor"),
135 load_IPMonitor,
136 NULL,
137 prime_IPMonitor,
138 NULL
139 },
140 #if !TARGET_OS_SIMULATOR
141 {
142 CFSTR("com.apple.SystemConfiguration.InterfaceNamer"),
143 load_InterfaceNamer,
144 NULL,
145 NULL,
146 NULL
147 },
148 {
149 CFSTR("com.apple.SystemConfiguration.KernelEventMonitor"),
150 load_KernelEventMonitor,
151 NULL,
152 prime_KernelEventMonitor,
153 NULL
154 },
155 {
156 CFSTR("com.apple.SystemConfiguration.LinkConfiguration"),
157 load_LinkConfiguration,
158 NULL,
159 NULL,
160 NULL
161 },
162 {
163 CFSTR("com.apple.SystemConfiguration.PreferencesMonitor"),
164 load_PreferencesMonitor,
165 NULL,
166 prime_PreferencesMonitor,
167 NULL
168 },
169 {
170 CFSTR("com.apple.SystemConfiguration.QoSMarking"),
171 load_QoSMarking,
172 NULL,
173 NULL,
174 NULL
175 },
176 #endif // !TARGET_OS_SIMULATOR
177 };
178
179
180 static void
181 addBundle(CFBundleRef bundle, Boolean forceEnabled)
182 {
183 CFDictionaryRef bundleDict;
184 bundleInfoRef bundleInfo;
185
186 bundleInfo = CFAllocatorAllocate(NULL, sizeof(*bundleInfo), 0);
187 bundleInfo->bundle = (CFBundleRef)CFRetain(bundle);
188 bundleInfo->loaded = FALSE;
189 bundleInfo->builtin = FALSE;
190 bundleInfo->enabled = TRUE;
191 bundleInfo->forced = forceEnabled;
192 bundleInfo->verbose = FALSE;
193 bundleInfo->load = NULL;
194 bundleInfo->start = NULL;
195 bundleInfo->prime = NULL;
196 bundleInfo->stop = NULL;
197
198 bundleDict = CFBundleGetInfoDictionary(bundle);
199 if (isA_CFDictionary(bundleDict)) {
200 CFBooleanRef bVal;
201
202 bVal = CFDictionaryGetValue(bundleDict, kSCBundleIsBuiltinKey);
203 if (isA_CFBoolean(bVal)) {
204 bundleInfo->builtin = CFBooleanGetValue(bVal);
205 }
206
207 bVal = CFDictionaryGetValue(bundleDict, kSCBundleEnabledKey);
208 if (isA_CFBoolean(bVal)) {
209 bundleInfo->enabled = CFBooleanGetValue(bVal);
210 }
211
212 bVal = CFDictionaryGetValue(bundleDict, kSCBundleVerboseKey);
213 if (isA_CFBoolean(bVal)) {
214 bundleInfo->verbose = CFBooleanGetValue(bVal);
215 }
216 }
217
218 CFArrayAppendValue(allBundles, bundleInfo);
219 return;
220 }
221
222
223 static CF_RETURNS_RETAINED CFStringRef
224 shortBundleIdentifier(CFStringRef bundleID)
225 {
226 CFIndex len = CFStringGetLength(bundleID);
227 CFRange range;
228 CFStringRef shortID = NULL;
229
230 if (CFStringFindWithOptions(bundleID,
231 CFSTR("."),
232 CFRangeMake(0, len),
233 kCFCompareBackwards,
234 &range)) {
235 range.location = range.location + range.length;
236 range.length = len - range.location;
237 shortID = CFStringCreateWithSubstring(NULL, bundleID, range);
238 }
239
240 return shortID;
241 }
242
243
244 static void *
245 getBundleSymbol(CFBundleRef bundle, CFStringRef functionName, CFStringRef shortID)
246 {
247 void *func;
248
249 // search for load(), start(), prime(), stop(), ...
250 func = CFBundleGetFunctionPointerForName(bundle, functionName);
251 if (func != NULL) {
252 return func;
253 }
254
255 if (shortID != NULL) {
256 CFStringRef altFunctionName;
257
258 // search for load_XXX(), ...
259 altFunctionName = CFStringCreateWithFormat(NULL,
260 NULL,
261 CFSTR("%@_%@"),
262 functionName,
263 shortID);
264 func = CFBundleGetFunctionPointerForName(bundle, altFunctionName);
265 CFRelease(altFunctionName);
266 }
267
268 return func;
269 }
270
271
272 static const char *
273 getBundleDirNameAndPath(CFBundleRef bundle, char *buf, size_t buf_len)
274 {
275 char *cp;
276 size_t len;
277 Boolean ok;
278 CFURLRef url;
279
280 url = CFBundleCopyBundleURL(bundle);
281 if (url == NULL) {
282 return NULL;
283 }
284
285 ok = CFURLGetFileSystemRepresentation(url, TRUE, (UInt8 *)buf, buf_len);
286 CFRelease(url);
287 if (!ok) {
288 return NULL;
289 }
290
291 cp = strrchr(buf, '/');
292 if (cp != NULL) {
293 cp++;
294 } else {
295 cp = buf;
296 }
297
298 /* check if this directory entry is a valid bundle name */
299 len = strlen(cp);
300 if (len <= sizeof(BUNDLE_DIR_EXTENSION)) {
301 /* if entry name isn't long enough */
302 return NULL;
303 }
304
305 len -= sizeof(BUNDLE_DIR_EXTENSION) - 1;
306 if (strcmp(&cp[len], BUNDLE_DIR_EXTENSION) != 0) {
307 /* if entry name doesn't end with ".bundle" */
308 return NULL;
309 }
310
311 return cp;
312 }
313
314
315 #pragma mark -
316 #pragma mark load
317
318
319 static void
320 loadBundle(const void *value, void *context) {
321 CFStringRef bundleID;
322 Boolean bundleAllowed;
323 bundleInfoRef bundleInfo = (bundleInfoRef)value;
324 Boolean bundleExclude;
325 CFIndex *nLoaded = (CFIndex *)context;
326 CFStringRef shortID;
327
328 bundleID = CFBundleGetIdentifier(bundleInfo->bundle);
329 if (bundleID == NULL) {
330 // sorry, no bundles without a bundle identifier
331 SC_log(LOG_NOTICE, "skipped %@ (no bundle ID)", bundleInfo->bundle);
332 return;
333 }
334
335 shortID = shortBundleIdentifier(bundleID);
336
337 bundleAllowed = ((CFSetGetCount(_plugins_allowed) == 0) || // if no white-listing
338 CFSetContainsValue(_plugins_allowed, bundleID) || // if [bundleID] white-listed
339 ((shortID != NULL) &&
340 CFSetContainsValue(_plugins_allowed, shortID))|| // if [short bundleID] white-listed
341 bundleInfo->forced // if "testing" plugin
342 );
343 if (!bundleAllowed) {
344 SC_log(LOG_INFO, "skipped %@ (not allowed)", bundleID);
345 goto done;
346 }
347
348 bundleExclude = (CFSetContainsValue(_plugins_exclude, bundleID) || // if [bundleID] excluded
349 ((shortID != NULL) &&
350 CFSetContainsValue(_plugins_exclude, shortID)) // if [short bundleID] excluded
351 );
352 if (bundleExclude) {
353 // sorry, this bundle has been excluded
354 SC_log(LOG_INFO, "skipped %@ (excluded)", bundleID);
355 goto done;
356 }
357
358 if (!bundleInfo->enabled && !bundleInfo->forced) {
359 // sorry, this bundle has not been enabled
360 SC_log(LOG_INFO, "skipped %@ (disabled)", bundleID);
361 goto done;
362 }
363
364 if (!bundleInfo->verbose) {
365 bundleInfo->verbose = CFSetContainsValue(_plugins_verbose, bundleID);
366 if (!bundleInfo->verbose) {
367 if (shortID != NULL) {
368 bundleInfo->verbose = CFSetContainsValue(_plugins_verbose, shortID);
369 }
370 }
371 }
372
373 if (bundleInfo->builtin) {
374 SC_log(LOG_INFO, "adding %@", bundleID);
375
376 for (size_t i = 0; i < sizeof(builtin_plugins)/sizeof(builtin_plugins[0]); i++) {
377 if (CFEqual(bundleID, builtin_plugins[i].bundleID)) {
378 bundleInfo->load = builtin_plugins[i].load;
379 bundleInfo->start = builtin_plugins[i].start;
380 bundleInfo->prime = builtin_plugins[i].prime;
381 bundleInfo->stop = builtin_plugins[i].stop;
382 break;
383 }
384 }
385
386 if ((bundleInfo->load == NULL) &&
387 (bundleInfo->start == NULL) &&
388 (bundleInfo->prime == NULL) &&
389 (bundleInfo->stop == NULL)) {
390 SC_log(LOG_NOTICE, "%@ add failed", bundleID);
391 goto done;
392 }
393 } else {
394 CFErrorRef error = NULL;
395
396 SC_log(LOG_INFO, "loading %@", bundleID);
397
398 if (!CFBundleLoadExecutableAndReturnError(bundleInfo->bundle, &error)) {
399 CFDictionaryRef user_info;
400
401 SC_log(LOG_NOTICE, "%@ load failed", bundleID);
402 user_info = CFErrorCopyUserInfo(error);
403 if (user_info != NULL) {
404 CFStringRef link_error_string;
405
406 link_error_string = CFDictionaryGetValue(user_info,
407 CFSTR("NSDebugDescription"));
408 if (link_error_string != NULL) {
409 SC_log(LOG_NOTICE, "%@", link_error_string);
410 }
411 CFRelease(user_info);
412 }
413 CFRelease(error);
414 goto done;
415 }
416
417 // get bundle entry points
418 bundleInfo->load = getBundleSymbol(bundleInfo->bundle, CFSTR("load" ), shortID);
419 bundleInfo->start = getBundleSymbol(bundleInfo->bundle, CFSTR("start"), shortID);
420 bundleInfo->prime = getBundleSymbol(bundleInfo->bundle, CFSTR("prime"), shortID);
421 bundleInfo->stop = getBundleSymbol(bundleInfo->bundle, CFSTR("stop" ), shortID);
422 }
423
424 /* mark this bundle as having been loaded */
425 bundleInfo->loaded = TRUE;
426
427 /* bump the count of loaded bundles */
428 *nLoaded = *nLoaded + 1;
429
430 done :
431
432 if (shortID != NULL) CFRelease(shortID);
433 return;
434 }
435
436
437 void
438 callLoadFunction(const void *value, void *context)
439 {
440 #pragma unused(context)
441 bundleInfoRef bundleInfo = (bundleInfoRef)value;
442
443 if (!bundleInfo->loaded) {
444 return;
445 }
446
447 if (bundleInfo->load == NULL) {
448 // if no load() function
449 return;
450 }
451
452 SC_log(LOG_DEBUG, "calling load() for %@",
453 CFBundleGetIdentifier(bundleInfo->bundle));
454
455 (*bundleInfo->load)(bundleInfo->bundle, bundleInfo->verbose);
456
457 return;
458 }
459
460
461 #pragma mark -
462 #pragma mark start
463
464
465 void
466 callStartFunction(const void *value, void *context)
467 {
468 #pragma unused(context)
469 const char *bundleDirName;
470 bundleInfoRef bundleInfo = (bundleInfoRef)value;
471 char bundleName[MAXNAMLEN + 1];
472 char bundlePath[MAXPATHLEN];
473 size_t len;
474
475 if (!bundleInfo->loaded) {
476 return;
477 }
478
479 if (bundleInfo->start == NULL) {
480 // if no start() function
481 return;
482 }
483
484 /* copy the bundle's path */
485 bundleDirName = getBundleDirNameAndPath(bundleInfo->bundle, bundlePath, sizeof(bundlePath));
486 if (bundleDirName == NULL) {
487 // if we have a problem with the bundle's path
488 return;
489 }
490
491 /* copy (just) the bundle's name */
492 if (strlcpy(bundleName, bundleDirName, sizeof(bundleName)) > sizeof(bundleName)) {
493 // if we have a problem with the bundle's name
494 return;
495 }
496 len = strlen(bundleName) - (sizeof(BUNDLE_DIR_EXTENSION) - 1);
497 bundleName[len] = '\0';
498
499 SC_log(LOG_DEBUG, "calling start() for %@",
500 CFBundleGetIdentifier(bundleInfo->bundle));
501
502 (*bundleInfo->start)(bundleName, bundlePath);
503
504 return;
505 }
506
507
508 #pragma mark -
509 #pragma mark prime
510
511
512 void
513 callPrimeFunction(const void *value, void *context)
514 {
515 #pragma unused(context)
516 bundleInfoRef bundleInfo = (bundleInfoRef)value;
517
518 if (!bundleInfo->loaded) {
519 return;
520 }
521
522 if (bundleInfo->prime == NULL) {
523 // if no prime() function
524 return;
525 }
526
527 SC_log(LOG_DEBUG, "calling prime() for %@",
528 CFBundleGetIdentifier(bundleInfo->bundle));
529
530 (*bundleInfo->prime)();
531
532 return;
533 }
534
535
536 #pragma mark -
537 #pragma mark stop
538
539
540 static void
541 stopComplete(void *info)
542 {
543 CFBundleRef bundle = (CFBundleRef)info;
544 CFStringRef bundleID = CFBundleGetIdentifier(bundle);
545 CFRunLoopSourceRef stopRls;
546
547 SC_log(LOG_INFO, "** %@ complete (%f)", bundleID, CFAbsoluteTimeGetCurrent());
548
549 stopRls = (CFRunLoopSourceRef)CFDictionaryGetValue(exiting, bundle);
550 if (stopRls == NULL) {
551 return;
552 }
553
554 CFRunLoopSourceInvalidate(stopRls);
555
556 CFDictionaryRemoveValue(exiting, bundle);
557
558 if (CFDictionaryGetCount(exiting) == 0) {
559 // if all of the plugins are happy
560 SC_log(LOG_INFO, "server shutdown complete (%f)", CFAbsoluteTimeGetCurrent());
561 exit (EX_OK);
562 }
563
564 return;
565 }
566
567
568 static void
569 stopDelayed(CFRunLoopTimerRef timer, void *info)
570 {
571 #pragma unused(timer)
572 #pragma unused(info)
573 const void **keys;
574 CFIndex i;
575 CFIndex n;
576
577 SC_log(LOG_INFO, "server shutdown was delayed, unresponsive plugins:");
578
579 /*
580 * we've asked our plugins to shutdown but someone
581 * isn't listening.
582 */
583 n = CFDictionaryGetCount(exiting);
584 keys = CFAllocatorAllocate(NULL, n * sizeof(CFTypeRef), 0);
585 CFDictionaryGetKeysAndValues(exiting, keys, NULL);
586 for (i = 0; i < n; i++) {
587 CFBundleRef bundle;
588 CFStringRef bundleID;
589
590 bundle = (CFBundleRef)keys[i];
591 bundleID = CFBundleGetIdentifier(bundle);
592 SC_log(LOG_NOTICE, "** %@", bundleID);
593 }
594 CFAllocatorDeallocate(NULL, keys);
595
596 exit (EX_OK);
597 }
598
599 static CFStringRef
600 stopRLSCopyDescription(const void *info)
601 {
602 CFBundleRef bundle = (CFBundleRef)info;
603
604 return CFStringCreateWithFormat(NULL,
605 NULL,
606 CFSTR("<stopRLS %p> {bundleID = %@}"),
607 info,
608 CFBundleGetIdentifier(bundle));
609 }
610
611
612 static void
613 stopBundle(const void *value, void *context)
614 {
615 #pragma unused(context)
616 bundleInfoRef bundleInfo = (bundleInfoRef)value;
617 CFRunLoopSourceRef stopRls;
618 CFRunLoopSourceContext stopContext = { 0 // version
619 , bundleInfo->bundle // info
620 , CFRetain // retain
621 , CFRelease // release
622 , stopRLSCopyDescription // copyDescription
623 , CFEqual // equal
624 , CFHash // hash
625 , NULL // schedule
626 , NULL // cancel
627 , stopComplete // perform
628 };
629
630 if (!bundleInfo->loaded) {
631 return;
632 }
633
634 if (bundleInfo->stop == NULL) {
635 // if no stop() function
636 return;
637 }
638
639 stopRls = CFRunLoopSourceCreate(NULL, 0, &stopContext);
640 CFRunLoopAddSource(CFRunLoopGetCurrent(), stopRls, kCFRunLoopDefaultMode);
641 CFDictionaryAddValue(exiting, bundleInfo->bundle, stopRls);
642 (*bundleInfo->stop)(stopRls);
643 CFRelease(stopRls);
644
645 return;
646 }
647
648
649 static void
650 stopBundles()
651 {
652 /*
653 * If defined, call each bundles stop() function. This function is
654 * called when configd has been asked to shut down (via a SIGTERM). The
655 * function should signal the provided run loop source when it is "ready"
656 * for the shut down to proceeed.
657 */
658 SC_log(LOG_DEBUG, "calling bundle stop() functions");
659 CFArrayApplyFunction(allBundles,
660 CFRangeMake(0, CFArrayGetCount(allBundles)),
661 stopBundle,
662 NULL);
663
664 if (CFDictionaryGetCount(exiting) == 0) {
665 // if all of the plugins are happy
666 SC_log(LOG_INFO, "server shutdown complete (%f)", CFAbsoluteTimeGetCurrent());
667 exit (EX_OK);
668 } else {
669 CFRunLoopTimerRef timer;
670
671 /*
672 * launchd will only wait 20 seconds before sending us a
673 * SIGKILL and because we want to know what's stuck before
674 * that time so set our own "we're not waiting any longer"
675 * timeout for 15 seconds.
676 */
677 timer = CFRunLoopTimerCreate(NULL, /* allocator */
678 CFAbsoluteTimeGetCurrent() + 15.0, /* fireDate (in 15 seconds) */
679 0.0, /* interval (== one-shot) */
680 0, /* flags */
681 0, /* order */
682 stopDelayed, /* callout */
683 NULL); /* context */
684 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
685 CFRelease(timer);
686 }
687
688 return;
689 }
690
691
692 #pragma mark -
693 #pragma mark term
694
695
696 __private_extern__
697 Boolean
698 plugin_term(int *status)
699 {
700 if (CFArrayGetCount(allBundles) == 0) {
701 // if no plugins
702 *status = EX_OK;
703 return FALSE; // don't delay shutdown
704 }
705
706 if (exiting != NULL) {
707 // if shutdown already active
708 return TRUE;
709 }
710
711 SC_log(LOG_INFO, "starting server shutdown (%f)", CFAbsoluteTimeGetCurrent());
712
713 exiting = CFDictionaryCreateMutable(NULL,
714 0,
715 &kCFTypeDictionaryKeyCallBacks,
716 &kCFTypeDictionaryValueCallBacks);
717
718 stopBundles();
719 return TRUE;
720 }
721
722
723 #pragma mark -
724 #pragma mark initialization
725
726
727 static void
728 sortBundles(CFMutableArrayRef orig)
729 {
730 CFIndex i;
731 CFIndex n;
732 CFMutableArrayRef new;
733 CFMutableSetRef orig_bundleIDs;
734
735 orig_bundleIDs = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
736
737 n = CFArrayGetCount(orig);
738 for (i = 0; i < n; i++) {
739 bundleInfoRef bundleInfo = (bundleInfoRef)CFArrayGetValueAtIndex(orig, i);
740 CFStringRef bundleID = CFBundleGetIdentifier(bundleInfo->bundle);
741
742 if (bundleID != NULL) {
743 CFSetAddValue(orig_bundleIDs, bundleID);
744 }
745 }
746
747 new = CFArrayCreateMutable(NULL, 0, NULL);
748 while (n > 0) {
749 Boolean inserted = FALSE;
750
751 for (i = 0; i < n; i++) {
752 bundleInfoRef bundleInfo1 = (bundleInfoRef)CFArrayGetValueAtIndex(orig, i);
753 CFStringRef bundleID1 = CFBundleGetIdentifier(bundleInfo1->bundle);
754 CFIndex count;
755 CFDictionaryRef dict;
756 CFIndex j;
757 CFIndex nRequires;
758 CFArrayRef requires = NULL;
759
760 dict = isA_CFDictionary(CFBundleGetInfoDictionary(bundleInfo1->bundle));
761 if (dict) {
762 requires = CFDictionaryGetValue(dict, kSCBundleRequiresKey);
763 requires = isA_CFArray(requires);
764 }
765 if (bundleID1 == NULL || requires == NULL) {
766 CFArrayInsertValueAtIndex(new, 0, bundleInfo1);
767 CFArrayRemoveValueAtIndex(orig, i);
768 inserted = TRUE;
769 break;
770 }
771 count = nRequires = CFArrayGetCount(requires);
772 for (j = 0; j < nRequires; j++) {
773 CFIndex k;
774 CFIndex nNew;
775 CFStringRef r = CFArrayGetValueAtIndex(requires, j);
776
777 if (!CFSetContainsValue(orig_bundleIDs, r)) {
778 // if dependency not present
779 count--;
780 continue;
781 }
782
783 nNew = CFArrayGetCount(new);
784 for (k = 0; k < nNew; k++) {
785 bundleInfoRef bundleInfo2 = (bundleInfoRef)CFArrayGetValueAtIndex(new, k);
786 CFStringRef bundleID2 = CFBundleGetIdentifier(bundleInfo2->bundle);
787
788 if (bundleID2 && CFEqual(bundleID2, r)) {
789 count--;
790 }
791 }
792 }
793 if (count == 0) {
794 /* all dependencies are met, append */
795 CFArrayAppendValue(new, bundleInfo1);
796 CFArrayRemoveValueAtIndex(orig, i);
797 inserted = TRUE;
798 break;
799 }
800 }
801
802 if (!inserted) {
803 SC_log(LOG_NOTICE, "Bundles have circular dependency!!!");
804 break;
805 }
806
807 n = CFArrayGetCount(orig);
808 }
809 if (CFArrayGetCount(orig) > 0) {
810 /* we have a circular dependency, append remaining items on new array */
811 CFArrayAppendArray(new, orig, CFRangeMake(0, CFArrayGetCount(orig)));
812 }
813 else {
814 /* new one is a sorted version of original */
815 }
816
817 CFArrayRemoveAllValues(orig);
818 CFArrayAppendArray(orig, new, CFRangeMake(0, CFArrayGetCount(new)));
819 CFRelease(new);
820 CFRelease(orig_bundleIDs);
821 return;
822 }
823
824
825 /*
826 * ALT_CFRelease()
827 *
828 * An alternate CFRelease() that we can use to fake out the
829 * static analyzer.
830 */
831 static __inline__ void
832 ALT_CFRelease(CFTypeRef cf)
833 {
834 CFRelease(cf);
835 }
836
837
838 __private_extern__
839 void
840 plugin_exec(void *arg)
841 {
842 CFIndex nLoaded = 0;
843
844 /* keep track of bundles */
845 allBundles = CFArrayCreateMutable(NULL, 0, NULL);
846
847 /* add white-listed plugins to those we'll allow to be loaded */
848 for (size_t i = 0; i < N_PLUGIN_WHITELIST; i++) {
849 if (pluginWhitelist[i] != NULL) {
850 CFSetSetValue(_plugins_allowed, pluginWhitelist[i]);
851 }
852 }
853
854 /* allow plug-ins to exec child/helper processes */
855 _SCDPluginExecInit();
856
857 if (arg == NULL) {
858 char path[MAXPATHLEN];
859 sysdir_search_path_enumeration_state state;
860
861 /*
862 * identify and load all bundles
863 */
864 state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_LIBRARY,
865 SYSDIR_DOMAIN_MASK_SYSTEM);
866 while ((state = sysdir_get_next_search_path_enumeration(state, path))) {
867 CFArrayRef bundles;
868 CFURLRef url;
869
870 #if TARGET_OS_SIMULATOR && !TARGET_OS_MACCATALYST
871 const char *path_sim_prefix;
872
873 path_sim_prefix = getenv("IPHONE_SIMULATOR_ROOT");
874 if ((path_sim_prefix != NULL) && (strcmp(path_sim_prefix, ".") != 0)) {
875 char path_sim[MAXPATHLEN];
876
877 strlcpy(path_sim, path_sim_prefix, sizeof(path_sim));
878 strlcat(path_sim, path, sizeof(path_sim));
879 strlcpy(path, path_sim, sizeof(path));
880 } else {
881 path[0] = '\0';
882 }
883 #endif // TARGET_OS_SIMULATOR && !TARGET_OS_MACCATALYST
884
885 /* load any available bundle */
886 strlcat(path, BUNDLE_DIRECTORY, sizeof(path));
887 SC_log(LOG_DEBUG, "searching for bundles in \"%s\"", path);
888 url = CFURLCreateFromFileSystemRepresentation(NULL,
889 (UInt8 *)path,
890 strlen(path),
891 TRUE);
892 bundles = CFBundleCreateBundlesFromDirectory(NULL, url, CFSTR(".bundle"));
893 CFRelease(url);
894
895 if (bundles != NULL) {
896 CFIndex i;
897 CFIndex n;
898
899 n = CFArrayGetCount(bundles);
900 for (i = 0; i < n; i++) {
901 CFBundleRef bundle;
902
903 bundle = (CFBundleRef)CFArrayGetValueAtIndex(bundles, i);
904 addBundle(bundle, FALSE);
905
906 // The CFBundleCreateBundlesFromDirectory() API has
907 // a known/outstanding bug in that it over-retains the
908 // returned bundles. Since we do not expect this to
909 // be fixed we release the extra references.
910 //
911 // See <rdar://problems/4912137&6078752> for more info.
912 //
913 // Also, we use the hack below to keep the static
914 // analyzer happy.
915 ALT_CFRelease(bundle);
916 }
917 CFRelease(bundles);
918 }
919 }
920
921 sortBundles(allBundles);
922 } else {
923 CFBundleRef bundle;
924 CFURLRef url;
925
926 /*
927 * load (only) the specified bundle
928 */
929 url = CFURLCreateFromFileSystemRepresentation(NULL,
930 (UInt8 *)arg,
931 strlen((char *)arg),
932 TRUE);
933 bundle = CFBundleCreate(NULL, url);
934 if (bundle != NULL) {
935 addBundle(bundle, TRUE);
936 CFRelease(bundle);
937 }
938 CFRelease(url);
939 }
940
941 /*
942 * Look for the InterfaceNamer plugin, and move it to the start
943 * of the list.
944 *
945 * Load the InterfaceNamer plugin (and thereby start its thread)
946 * first in an attempt to minimize the amount of time that
947 * opendirectoryd has to wait for the platform UUID to appear in
948 * nvram.
949 *
950 * InterfaceNamer is responsible for creating the platform UUID on
951 * platforms without a UUID in ROM. Until the platform UUID is created
952 * and stashed in nvram, all calls to opendirectoryd to do things like
953 * getpwuid() will block, because opendirectoryd will block while trying
954 * to read the platform UUID from the kernel.
955 *
956 * As an example, dlopen() causes XPC to do some intialization, and
957 * part of that initialization involves communicating with xpcd.
958 * Since xpcd calls getpwuid_r() during its initialization, it will
959 * block until the platform UUID is available.
960 */
961 for (CFIndex i = 0; i < CFArrayGetCount(allBundles); i++) {
962 bundleInfoRef bi = (bundleInfoRef)CFArrayGetValueAtIndex(allBundles, i);
963 CFStringRef bundleID = CFBundleGetIdentifier(bi->bundle);
964
965 if (_SC_CFEqual(bundleID,
966 CFSTR("com.apple.SystemConfiguration.InterfaceNamer")))
967 {
968 CFArrayRemoveValueAtIndex(allBundles, i);
969 CFArrayInsertValueAtIndex(allBundles, 0, bi);
970 break;
971 }
972 }
973
974 /*
975 * load each bundle.
976 */
977 SC_log(LOG_DEBUG, "loading bundles");
978 CFArrayApplyFunction(allBundles,
979 CFRangeMake(0, CFArrayGetCount(allBundles)),
980 loadBundle,
981 &nLoaded);
982
983 /*
984 * If defined, call each bundles load() function. This function (or
985 * the start() function) should initialize any variables, open any
986 * sessions with "configd", and register any needed notifications.
987 *
988 * Note: Establishing initial information in the store should be
989 * deferred until the prime() initialization function so that
990 * any bundles which want to receive a notification that the
991 * data has changed will have an opportunity to install a
992 * notification handler.
993 */
994 SC_log(LOG_DEBUG, "calling bundle load() functions");
995 CFArrayApplyFunction(allBundles,
996 CFRangeMake(0, CFArrayGetCount(allBundles)),
997 callLoadFunction,
998 NULL);
999
1000 if (nLoaded == 0) {
1001 // if no bundles loaded
1002 return;
1003 }
1004
1005 /*
1006 * If defined, call each bundles start() function. This function is
1007 * called after the bundle has been loaded and its load() function has
1008 * been called. It should initialize any variables, open any sessions
1009 * with "configd", and register any needed notifications.
1010 *
1011 * Note: Establishing initial information in the store should be
1012 * deferred until the prime() initialization function so that
1013 * any bundles which want to receive a notification that the
1014 * data has changed will have an opportunity to install a
1015 * notification handler.
1016 */
1017 SC_log(LOG_DEBUG, "calling bundle start() functions");
1018 CFArrayApplyFunction(allBundles,
1019 CFRangeMake(0, CFArrayGetCount(allBundles)),
1020 callStartFunction,
1021 NULL);
1022
1023 /*
1024 * If defined, call each bundles prime() function. This function is
1025 * called after the bundle has been loaded and its load() and start()
1026 * functions have been called. It should initialize any configuration
1027 * information and/or state in the store.
1028 */
1029 SC_log(LOG_DEBUG, "calling bundle prime() functions");
1030 CFArrayApplyFunction(allBundles,
1031 CFRangeMake(0, CFArrayGetCount(allBundles)),
1032 callPrimeFunction,
1033 NULL);
1034
1035 /*
1036 * At this point, the assumption is that each loaded plugin will have
1037 * established CFMachPort, CFSocket, and CFRunLoopTimer input sources
1038 * to handle any events and registered these sources with this threads
1039 * run loop and we're ready to go.
1040 *
1041 * Note: it is also assumed that any plugin needing to wait and/or block
1042 * will do so only a private thread (or asynchronously on a non-main
1043 * dispatch queue).
1044 */
1045
1046 return;
1047 }