2 * Copyright (c) 2000-2008, 2010, 2012-2017 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 * April 2, 2004 Allan Nathanson <ajn@apple.com>
28 * - use SCPreference notification APIs
30 * June 24, 2001 Allan Nathanson <ajn@apple.com>
31 * - update to public SystemConfiguration.framework APIs
33 * November 10, 2000 Allan Nathanson <ajn@apple.com>
38 #include <TargetConditionals.h>
41 #include <sys/types.h>
46 #define SC_LOG_HANDLE __log_PreferencesMonitor()
47 #define SC_LOG_HANDLE_TYPE static
48 #include <SystemConfiguration/SystemConfiguration.h>
49 #include <SystemConfiguration/SCPrivate.h>
50 #include <SystemConfiguration/SCValidation.h>
51 #include "plugin_shared.h"
54 #include <CommonCrypto/CommonDigest.h>
58 static SCPreferencesRef prefs
= NULL
;
59 static SCDynamicStoreRef store
= NULL
;
61 /* InterfaceNamer[.plugin] monitoring globals */
62 Boolean haveConfiguration
= FALSE
;
63 static CFStringRef namerKey
= NULL
;
64 static CFMutableArrayRef preconfigured_names
= NULL
; // of CFStringRef (BSD name)
65 static CFMutableArrayRef preconfigured_interfaces
= NULL
; // of SCNetworkInterfaceRef
67 /* KernelEventMonitor[.plugin] monitoring globals */
68 static CFStringRef interfacesKey
= NULL
;
70 /* SCDynamicStore (Setup:) */
71 static CFMutableDictionaryRef currentPrefs
; /* current prefs */
72 static CFMutableDictionaryRef newPrefs
; /* new prefs */
73 static CFMutableArrayRef unchangedPrefsKeys
; /* new prefs keys which match current */
74 static CFMutableArrayRef removedPrefsKeys
; /* old prefs keys to be removed */
76 static Boolean rofs
= FALSE
;
77 static Boolean restorePrefs
= FALSE
;
79 #define MY_PLUGIN_NAME "PreferencesMonitor"
80 #define MY_PLUGIN_ID CFSTR("com.apple.SystemConfiguration." MY_PLUGIN_NAME)
84 updateConfiguration(SCPreferencesRef prefs
,
85 SCPreferencesNotification notificationType
,
90 __log_PreferencesMonitor()
92 static os_log_t log
= NULL
;
95 log
= os_log_create("com.apple.SystemConfiguration", "PreferencesMonitor");
106 CFStringRef currentModel
= NULL
;
107 CFMutableStringRef modelPrefixStr
= NULL
;
108 CFArrayRef keyList
= NULL
;
109 CFIndex keyListCount
;
111 Boolean modified
= FALSE
;
112 int sc_status
= kSCStatusFailed
;
115 ok
= SCPreferencesLock(prefs
, TRUE
);
120 sc_status
= SCError();
121 if (sc_status
== kSCStatusStale
) {
122 SCPreferencesSynchronize(prefs
);
124 SC_log(LOG_NOTICE
, "Could not acquire network configuration lock: %s",
125 SCErrorString(sc_status
));
130 keyList
= SCPreferencesCopyKeyList(prefs
);
131 if (keyList
== NULL
) {
135 currentModel
= _SC_hw_model(FALSE
);
136 if (currentModel
== NULL
) {
140 /* Create "model:" string for prefix-check */
141 modelPrefixStr
= CFStringCreateMutableCopy(NULL
, 0, currentModel
);
142 CFStringAppend(modelPrefixStr
, CFSTR(":"));
144 keyListCount
= CFArrayGetCount(keyList
);
145 for (idx
= 0; idx
< keyListCount
; idx
++) {
146 CFStringRef existingKey
= CFArrayGetValueAtIndex(keyList
, idx
);
148 CFArrayRef splitKey
= NULL
;
149 CFPropertyListRef value
;
151 if (isA_CFString(existingKey
) == NULL
) {
155 if (!CFStringHasPrefix(existingKey
, modelPrefixStr
)) {
159 splitKey
= CFStringCreateArrayBySeparatingStrings(NULL
, existingKey
, CFSTR(":"));
160 key
= CFArrayGetValueAtIndex(splitKey
, 1);
161 value
= SCPreferencesGetValue(prefs
, existingKey
);
162 SCPreferencesSetValue(prefs
, key
, value
);
163 SCPreferencesRemoveValue(prefs
, existingKey
);
169 SCPreferencesRef ni_prefs
= NULL
;
170 ni_prefs
= SCPreferencesCreate(NULL
, MY_PLUGIN_ID
, CFSTR("NetworkInterfaces.plist"));
171 if (ni_prefs
== NULL
) {
175 ok
= _SCNetworkConfigurationCheckValidityWithPreferences(prefs
, ni_prefs
, NULL
);
178 //Commit the changes only if prefs files valid
180 if (!SCPreferencesCommitChanges(prefs
)) {
181 if (SCError() != EROFS
) {
182 SC_log(LOG_NOTICE
, "SCPreferencesCommitChanges() failed: %s",
183 SCErrorString(SCError()));
189 (void) SCPreferencesApplyChanges(prefs
);
194 (void) SCPreferencesUnlock(prefs
);
196 if (keyList
!= NULL
) {
199 if (modelPrefixStr
!= NULL
) {
200 CFRelease(modelPrefixStr
);
207 establishNewPreferences()
209 SCNetworkSetRef current
= NULL
;
210 CFStringRef new_model
;
212 int sc_status
= kSCStatusFailed
;
213 SCNetworkSetRef set
= NULL
;
214 Boolean updated
= FALSE
;
217 ok
= SCPreferencesLock(prefs
, TRUE
);
222 sc_status
= SCError();
223 if (sc_status
== kSCStatusStale
) {
224 SCPreferencesSynchronize(prefs
);
226 SC_log(LOG_NOTICE
, "Could not acquire network configuration lock: %s",
227 SCErrorString(sc_status
));
232 /* Ensure that the preferences has the new model */
233 new_model
= _SC_hw_model(FALSE
);
235 /* Need to regenerate the new configuration for new model */
236 if (new_model
!= NULL
) {
237 CFStringRef old_model
;
239 old_model
= SCPreferencesGetValue(prefs
, MODEL
);
240 if ((old_model
!= NULL
) && !_SC_CFEqual(old_model
, new_model
)) {
245 keys
= SCPreferencesCopyKeyList(prefs
);
246 count
= (keys
!= NULL
) ? CFArrayGetCount(keys
) : 0;
248 for (index
= 0; index
< count
; index
++) {
249 CFStringRef existing_key
;
251 existing_key
= CFArrayGetValueAtIndex(keys
, index
);
252 if (isA_CFString(existing_key
) != NULL
) {
254 CFPropertyListRef value
;
256 /* If it already contains a Model
257 or if it already contains a MODEL:KEY key skip it*/
258 if (CFEqual(existing_key
, MODEL
)
259 || CFStringFind(existing_key
, CFSTR(":"), 0).location
264 value
= SCPreferencesGetValue(prefs
, existing_key
);
266 /* Create a new key as OLD_MODEL:OLD_KEY */
267 new_key
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@:%@"),
268 old_model
, existing_key
);
269 SCPreferencesSetValue(prefs
, new_key
, value
);
270 if (!CFEqual(existing_key
, kSCPrefSystem
)) {
271 /* preserve existing host names */
272 SCPreferencesRemoveValue(prefs
, existing_key
);
282 /* Set the new model */
283 SCPreferencesSetValue(prefs
, MODEL
, new_model
);
286 current
= SCNetworkSetCopyCurrent(prefs
);
287 if (current
!= NULL
) {
292 set
= _SCNetworkSetCreateDefault(prefs
);
295 sc_status
= SCError();
300 ok
= SCNetworkSetEstablishDefaultConfiguration(set
);
302 sc_status
= SCError();
309 ok
= SCPreferencesCommitChanges(prefs
);
311 SC_log(LOG_NOTICE
, "New network configuration saved");
314 sc_status
= SCError();
315 if (sc_status
== EROFS
) {
316 /* a read-only fileysstem is OK */
319 /* ... but we don't want to synchronize */
324 /* apply (committed or temporary/read-only) changes */
325 (void) SCPreferencesApplyChanges(prefs
);
326 } else if ((current
== NULL
) && (set
!= NULL
)) {
327 (void) SCNetworkSetRemove(set
);
331 if (sc_status
== kSCStatusOK
) {
332 SC_log(LOG_NOTICE
, "Network configuration not updated");
334 SC_log(LOG_NOTICE
, "Could not establish network configuration: %s",
335 SCErrorString(sc_status
));
339 (void)SCPreferencesUnlock(prefs
);
340 if (set
!= NULL
) CFRelease(set
);
346 watchSCDynamicStore()
348 CFMutableArrayRef keys
;
350 CFRunLoopSourceRef rls
;
353 * watch for KernelEventMonitor[.bundle] changes (the list of
354 * active network interfaces)
356 interfacesKey
= SCDynamicStoreKeyCreateNetworkInterface(NULL
,
357 kSCDynamicStoreDomainState
);
360 * watch for InterfaceNamer[.bundle] changes (quiet, timeout,
361 * and the list of pre-configured interfaces)
363 namerKey
= SCDynamicStoreKeyCreate(NULL
,
364 CFSTR("%@" "InterfaceNamer"),
365 kSCDynamicStoreDomainPlugin
);
367 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
369 SC_log(LOG_NOTICE
, "SCDynamicStoreCreateRunLoopSource() failed: %s", SCErrorString(SCError()));
370 haveConfiguration
= TRUE
;
373 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
376 keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
377 CFArrayAppendValue(keys
, interfacesKey
);
378 CFArrayAppendValue(keys
, namerKey
);
379 ok
= SCDynamicStoreSetNotificationKeys(store
, keys
, NULL
);
382 SC_log(LOG_NOTICE
, "SCDynamicStoreSetNotificationKeys() failed: %s", SCErrorString(SCError()));
383 haveConfiguration
= TRUE
;
391 previousConfigurationAvailable()
393 CFStringRef backupKey
= NULL
;
394 CFStringRef currentModel
= NULL
;
395 CFPropertyListRef properties
= NULL
;
397 currentModel
= _SC_hw_model(FALSE
);
398 if (currentModel
== NULL
) {
402 /* Currently relying only if a backup of "Sets" is present */
403 backupKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@:Sets"), currentModel
);
404 properties
= SCPreferencesGetValue(prefs
, backupKey
);
405 CFRelease(backupKey
);
407 return (properties
!= NULL
);
412 storeCallback(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
415 CFDictionaryRef dict
;
416 Boolean quiet
= FALSE
;
417 Boolean timeout
= FALSE
;
418 Boolean updated
= FALSE
;
421 * Capture/process InterfaceNamer[.bundle] info
422 * 1. check if IORegistry "quiet", "timeout"
423 * 2. update list of named pre-configured interfaces
425 dict
= SCDynamicStoreCopyValue(store
, namerKey
);
427 if (isA_CFDictionary(dict
)) {
428 CFArrayRef preconfigured
;
430 if (CFDictionaryContainsKey(dict
, kInterfaceNamerKey_Quiet
)) {
433 if (CFDictionaryContainsKey(dict
, kInterfaceNamerKey_Timeout
)) {
437 preconfigured
= CFDictionaryGetValue(dict
, kInterfaceNamerKey_PreConfiguredInterfaces
);
438 preconfigured
= isA_CFArray(preconfigured
);
439 if (!_SC_CFEqual(preconfigured
, preconfigured_names
)) {
444 if (preconfigured_names
!= NULL
) {
445 nx
= CFArrayGetCount(preconfigured_names
);
446 CFRelease(preconfigured_names
);
447 preconfigured_names
= NULL
;
449 if (preconfigured_interfaces
!= NULL
) {
450 CFRelease(preconfigured_interfaces
);
451 preconfigured_interfaces
= NULL
;
454 // add pre-configured interfaces
455 n
= (preconfigured
!= NULL
) ? CFArrayGetCount(preconfigured
) : 0;
456 for (CFIndex i
= 0; i
< n
; i
++) {
457 CFStringRef bsdName
= CFArrayGetValueAtIndex(preconfigured
, i
);
458 SCNetworkInterfaceRef interface
;
460 for (int retry
= 0; retry
< 10; retry
++) {
462 // add short delay (before retry)
463 usleep(20 * 1000); // 20ms
466 interface
= _SCNetworkInterfaceCreateWithBSDName(NULL
, bsdName
, kIncludeNoVirtualInterfaces
);
467 if (interface
== NULL
) {
468 SC_log(LOG_ERR
, "could not create network interface for %@", bsdName
);
469 } else if (_SCNetworkInterfaceGetIOPath(interface
) == NULL
) {
470 SC_log(LOG_ERR
, "could not get IOPath for %@", bsdName
);
471 CFRelease(interface
);
475 if (interface
!= NULL
) {
476 // if we have an interface
481 if (interface
== NULL
) {
482 // if SCNetworkInterface not [currently] available
486 // keep track of the interface name (quicker than having to iterate the list
487 // of SCNetworkInterfaces, extract the name, and compare).
488 if (preconfigured_names
== NULL
) {
489 preconfigured_names
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
491 CFArrayAppendValue(preconfigured_names
, bsdName
);
493 if (preconfigured_interfaces
== NULL
) {
494 preconfigured_interfaces
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
496 CFArrayAppendValue(preconfigured_interfaces
, interface
);
497 CFRelease(interface
);
502 // check if all pre-configured interfaces were detached
503 n
= (preconfigured_names
!= NULL
) ? CFArrayGetCount(preconfigured_names
) : 0;
504 if ((nx
> 0) && (n
== 0)) {
509 CFStringRef interfaces
= CFSTR("<empty>");
511 // report [updated] pre-configured interfaces
512 if (preconfigured_names
!= NULL
) {
513 interfaces
= CFStringCreateByCombiningStrings(NULL
, preconfigured_names
, CFSTR(","));
515 CFRetain(interfaces
);
517 SC_log(LOG_INFO
, "pre-configured interface list changed: %@", interfaces
);
518 CFRelease(interfaces
);
526 if (!haveConfiguration
&& (quiet
|| timeout
)) {
527 static int logged
= 0;
530 #if !TARGET_OS_IPHONE
532 #endif /* !TARGET_OS_IPHONE */
534 haveConfiguration
= TRUE
;
537 (void) establishNewPreferences();
540 (void) restorePreferences();
541 restorePrefs
= FALSE
;
544 if (timeout
&& (logged
++ == 0)) {
545 SC_log(LOG_ERR
, "Network configuration creation timed out waiting for IORegistry");
549 if (updated
&& (changedKeys
!= NULL
)) {
550 // if pre-configured interface list changed
551 updateConfiguration(prefs
, kSCPreferencesNotificationApply
, (void *)store
);
559 updateCache(const void *key
, const void *value
, void *context
)
561 #pragma unused(context)
562 CFStringRef configKey
= (CFStringRef
)key
;
563 CFPropertyListRef configData
= (CFPropertyListRef
)value
;
564 CFPropertyListRef cacheData
;
567 cacheData
= CFDictionaryGetValue(currentPrefs
, configKey
);
570 if (CFEqual(cacheData
, configData
)) {
572 * if the old & new property list values have
573 * not changed then we don't need to update
576 CFArrayAppendValue(unchangedPrefsKeys
, configKey
);
580 /* in any case, this key should not be removed */
581 i
= CFArrayGetFirstIndexOfValue(removedPrefsKeys
,
582 CFRangeMake(0, CFArrayGetCount(removedPrefsKeys
)),
584 if (i
!= kCFNotFound
) {
585 CFArrayRemoveValueAtIndex(removedPrefsKeys
, i
);
593 flatten(SCPreferencesRef prefs
,
595 CFDictionaryRef base
)
597 CFDictionaryRef subset
;
599 CFMutableDictionaryRef myDict
;
606 if (!CFDictionaryGetValueIfPresent(base
, kSCResvLink
, (const void **)&link
)) {
607 /* if this dictionary is not linked */
610 /* if __LINK__ key is present */
611 subset
= SCPreferencesPathGetValue(prefs
, link
);
613 /* if error with link */
614 SC_log(LOG_NOTICE
, "SCPreferencesPathGetValue(,%@,) failed: %s",
616 SCErrorString(SCError()));
621 if (CFDictionaryContainsKey(subset
, kSCResvInactive
)) {
622 /* if __INACTIVE__ key is present */
626 myKey
= CFStringCreateWithFormat(NULL
,
629 kSCDynamicStoreDomainSetup
,
632 myDict
= (CFMutableDictionaryRef
)CFDictionaryGetValue(newPrefs
, myKey
);
634 myDict
= CFDictionaryCreateMutableCopy(NULL
,
636 (CFDictionaryRef
)myDict
);
638 myDict
= CFDictionaryCreateMutable(NULL
,
640 &kCFTypeDictionaryKeyCallBacks
,
641 &kCFTypeDictionaryValueCallBacks
);
644 nKeys
= CFDictionaryGetCount(subset
);
646 keys
= CFAllocatorAllocate(NULL
, nKeys
* sizeof(CFStringRef
) , 0);
647 vals
= CFAllocatorAllocate(NULL
, nKeys
* sizeof(CFPropertyListRef
), 0);
648 CFDictionaryGetKeysAndValues(subset
, keys
, vals
);
649 for (i
= 0; i
< nKeys
; i
++) {
650 if (CFGetTypeID((CFTypeRef
)vals
[i
]) != CFDictionaryGetTypeID()) {
651 /* add this key/value to the current dictionary */
652 CFDictionarySetValue(myDict
, keys
[i
], vals
[i
]);
656 /* flatten [sub]dictionaries */
657 subKey
= CFStringCreateWithFormat(NULL
,
661 CFEqual(key
, CFSTR("/")) ? "" : "/",
663 flatten(prefs
, subKey
, vals
[i
]);
667 CFAllocatorDeallocate(NULL
, keys
);
668 CFAllocatorDeallocate(NULL
, vals
);
671 if (CFDictionaryGetCount(myDict
) > 0) {
672 /* add this dictionary to the new preferences */
673 CFDictionarySetValue(newPrefs
, myKey
, myDict
);
683 static CF_RETURNS_RETAINED CFStringRef
684 copyInterfaceUUID(CFStringRef bsdName
)
687 unsigned char sha1_bytes
[CC_SHA1_DIGEST_LENGTH
];
688 CFUUIDBytes uuid_bytes
;
691 char if_name
[IF_NAMESIZE
];
693 CFStringRef uuid_str
;
695 // start with interface name
696 bzero(&if_name
, sizeof(if_name
));
697 (void) _SC_cfstring_to_cstring(bsdName
,
700 kCFStringEncodingASCII
);
703 bzero(&bytes
, sizeof(bytes
));
708 CC_SHA1_Final(bytes
.sha1_bytes
, &ctx
);
710 // create UUID string
711 uuid
= CFUUIDCreateFromUUIDBytes(NULL
, bytes
.uuid_bytes
);
712 uuid_str
= CFUUIDCreateString(NULL
, uuid
);
720 updatePreConfiguredConfiguration(SCPreferencesRef prefs
)
726 Boolean updated
= FALSE
;
728 range
= CFRangeMake(0,
729 (preconfigured_names
!= NULL
) ? CFArrayGetCount(preconfigured_names
) : 0);
730 if (range
.length
== 0) {
731 // if no [pre-configured] interfaces
735 set
= SCNetworkSetCopyCurrent(prefs
);
742 * Check for (and remove) any network services associated with
743 * a pre-configured interface from the prefs.
745 services
= SCNetworkServiceCopyAll(prefs
);
746 if (services
!= NULL
) {
749 n
= CFArrayGetCount(services
);
750 for (CFIndex i
= 0; i
< n
; i
++) {
752 SCNetworkInterfaceRef interface
;
753 SCNetworkServiceRef service
;
755 service
= CFArrayGetValueAtIndex(services
, i
);
757 interface
= SCNetworkServiceGetInterface(service
);
758 if (interface
== NULL
) {
763 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
764 if (bsdName
== NULL
) {
765 // if no interface name
769 if (!CFArrayContainsValue(preconfigured_names
, range
, bsdName
)) {
770 // if not preconfigured
774 // remove [preconfigured] network service from the prefs
775 SC_log(LOG_NOTICE
, "removing network service for %@", bsdName
);
776 ok
= SCNetworkServiceRemove(service
);
778 SC_log(LOG_ERR
, "SCNetworkServiceRemove() failed: %s",
779 SCErrorString(SCError()));
788 // commit the updated prefs ... but don't apply
789 ok
= SCPreferencesCommitChanges(prefs
);
791 if (SCError() != EROFS
) {
792 SC_log(LOG_ERR
, "SCPreferencesCommitChanges() failed: %s",
793 SCErrorString(SCError()));
799 * Now, add a new network service for each pre-configured interface
801 for (CFIndex i
= 0; i
< range
.length
; i
++) {
803 SCNetworkInterfaceRef interface
= CFArrayGetValueAtIndex(preconfigured_interfaces
, i
);
804 SCNetworkServiceRef service
;
805 CFStringRef serviceID
;
807 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
809 // create network service
810 service
= SCNetworkServiceCreate(prefs
, interface
);
811 if (service
== NULL
) {
812 SC_log(LOG_ERR
, "could not create network service for \"%@\": %s",
814 SCErrorString(SCError()));
818 // update network service to use a consistent serviceID
819 serviceID
= copyInterfaceUUID(bsdName
);
820 if (serviceID
!= NULL
) {
821 ok
= _SCNetworkServiceSetServiceID(service
, serviceID
);
822 CFRelease(serviceID
);
824 SC_log(LOG_ERR
, "_SCNetworkServiceSetServiceID() failed: %s",
825 SCErrorString(SCError()));
826 // ... and keep whatever random UUID was created for the service
829 SC_log(LOG_ERR
, "could not create serviceID for \"%@\"", bsdName
);
830 // ... and we'll use whatever random UUID was created for the service
833 // establish [template] configuration
834 ok
= SCNetworkServiceEstablishDefaultConfiguration(service
);
836 SC_log(LOG_ERR
, "could not establish network service for \"%@\": %s",
838 SCErrorString(SCError()));
839 SCNetworkServiceRemove(service
);
844 // add network service to the current set
845 ok
= SCNetworkSetAddService(set
, service
);
847 SC_log(LOG_ERR
, "could not add service for \"%@\": %s",
849 SCErrorString(SCError()));
850 SCNetworkServiceRemove(service
);
855 SC_log(LOG_INFO
, "network service %@ added for \"%@\"",
856 SCNetworkServiceGetServiceID(service
),
868 updateSCDynamicStore(SCPreferencesRef prefs
)
870 CFStringRef current
= NULL
;
871 CFDateRef date
= NULL
;
872 CFMutableDictionaryRef dict
= NULL
;
873 CFDictionaryRef global
= NULL
;
878 CFMutableArrayRef patterns
;
879 CFDictionaryRef set
= NULL
;
882 * initialize old preferences, new preferences, an array
883 * of keys which have not changed, and an array of keys
884 * to be removed (cleaned up).
887 patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
888 pattern
= CFStringCreateWithFormat(NULL
,
891 kSCDynamicStoreDomainSetup
);
892 CFArrayAppendValue(patterns
, pattern
);
893 dict
= (CFMutableDictionaryRef
)SCDynamicStoreCopyMultiple(store
, NULL
, patterns
);
897 currentPrefs
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
900 currentPrefs
= CFDictionaryCreateMutable(NULL
,
902 &kCFTypeDictionaryKeyCallBacks
,
903 &kCFTypeDictionaryValueCallBacks
);
906 unchangedPrefsKeys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
908 i
= CFDictionaryGetCount(currentPrefs
);
910 const void **currentKeys
;
913 currentKeys
= CFAllocatorAllocate(NULL
, i
* sizeof(CFStringRef
), 0);
914 CFDictionaryGetKeysAndValues(currentPrefs
, currentKeys
, NULL
);
915 array
= CFArrayCreate(NULL
, currentKeys
, i
, &kCFTypeArrayCallBacks
);
916 removedPrefsKeys
= CFArrayCreateMutableCopy(NULL
, 0, array
);
918 CFAllocatorDeallocate(NULL
, currentKeys
);
920 removedPrefsKeys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
924 * The "newPrefs" dictionary will contain the new / updated
925 * configuration which will be written to the configuration cache.
927 newPrefs
= CFDictionaryCreateMutable(NULL
,
929 &kCFTypeDictionaryKeyCallBacks
,
930 &kCFTypeDictionaryValueCallBacks
);
933 * create status dictionary associated with current configuration
934 * information including:
935 * - current set "name" to cache
936 * - time stamp indicating when the cache preferences were
939 dict
= CFDictionaryCreateMutable(NULL
,
941 &kCFTypeDictionaryKeyCallBacks
,
942 &kCFTypeDictionaryValueCallBacks
);
943 date
= CFDateCreate(NULL
, CFAbsoluteTimeGetCurrent());
948 keys
= SCPreferencesCopyKeyList(prefs
);
949 if ((keys
== NULL
) || (CFArrayGetCount(keys
) == 0)) {
950 SC_log(LOG_NOTICE
, "updateConfiguration(): no preferences");
955 * get "global" system preferences
957 global
= SCPreferencesGetValue(prefs
, kSCPrefSystem
);
959 /* if no global preferences are defined */
963 if (!isA_CFDictionary(global
)) {
964 SC_log(LOG_NOTICE
, "updateConfiguration(): %@ is not a dictionary",
969 /* flatten property list */
970 flatten(prefs
, CFSTR("/"), global
);
975 * get current set name
977 current
= SCPreferencesGetValue(prefs
, kSCPrefCurrentSet
);
979 /* if current set not defined */
983 if (!isA_CFString(current
)) {
984 SC_log(LOG_NOTICE
, "updateConfiguration(): %@ is not a string",
992 set
= SCPreferencesPathGetValue(prefs
, current
);
994 /* if error with path */
995 SC_log(LOG_NOTICE
, "%@ value (%@) not valid",
1001 if (!isA_CFDictionary(set
)) {
1002 SC_log(LOG_NOTICE
, "updateConfiguration(): %@ is not a dictionary",
1007 /* flatten property list */
1008 flatten(prefs
, CFSTR("/"), set
);
1010 CFDictionarySetValue(dict
, kSCDynamicStorePropSetupCurrentSet
, current
);
1014 /* add last updated time stamp */
1015 CFDictionarySetValue(dict
, kSCDynamicStorePropSetupLastUpdated
, date
);
1017 /* add Setup: key */
1018 CFDictionarySetValue(newPrefs
, kSCDynamicStoreDomainSetup
, dict
);
1020 /* compare current and new preferences */
1021 CFDictionaryApplyFunction(newPrefs
, updateCache
, NULL
);
1023 /* remove those keys which have not changed from the update */
1024 n
= CFArrayGetCount(unchangedPrefsKeys
);
1025 for (i
= 0; i
< n
; i
++) {
1028 key
= CFArrayGetValueAtIndex(unchangedPrefsKeys
, i
);
1029 CFDictionaryRemoveValue(newPrefs
, key
);
1032 /* Update the dynamic store */
1034 if (!SCDynamicStoreSetMultiple(store
, newPrefs
, removedPrefsKeys
, NULL
)) {
1035 SC_log(LOG_NOTICE
, "SCDynamicStoreSetMultiple() failed: %s", SCErrorString(SCError()));
1038 SC_log(LOG_DEBUG
, "SCDynamicStore\nset: %@\nremove: %@",
1043 CFRelease(currentPrefs
);
1044 CFRelease(newPrefs
);
1045 CFRelease(unchangedPrefsKeys
);
1046 CFRelease(removedPrefsKeys
);
1047 if (dict
) CFRelease(dict
);
1048 if (date
) CFRelease(date
);
1049 if (keys
) CFRelease(keys
);
1055 updateConfiguration(SCPreferencesRef prefs
,
1056 SCPreferencesNotification notificationType
,
1059 #pragma unused(info)
1060 os_activity_t activity
;
1062 activity
= os_activity_create("processing [SC] preferences.plist changes",
1063 OS_ACTIVITY_CURRENT
,
1064 OS_ACTIVITY_FLAG_DEFAULT
);
1065 os_activity_scope(activity
);
1067 #if !TARGET_OS_IPHONE
1068 if ((notificationType
& kSCPreferencesNotificationCommit
) == kSCPreferencesNotificationCommit
) {
1069 SCNetworkSetRef current
;
1071 current
= SCNetworkSetCopyCurrent(prefs
);
1072 if (current
!= NULL
) {
1073 /* network configuration available, disable template creation */
1074 haveConfiguration
= TRUE
;
1078 #endif /* !TARGET_OS_IPHONE */
1080 if ((notificationType
& kSCPreferencesNotificationApply
) != kSCPreferencesNotificationApply
) {
1084 SC_log(LOG_INFO
, "updating configuration");
1086 /* add any [Apple] pre-configured network services */
1087 updatePreConfiguredConfiguration(prefs
);
1089 /* update SCDynamicStore (Setup:) */
1090 updateSCDynamicStore(prefs
);
1092 /* finished with current prefs, wait for changes */
1094 SCPreferencesSynchronize(prefs
);
1099 os_release(activity
);
1107 prime_PreferencesMonitor()
1109 SC_log(LOG_DEBUG
, "prime() called");
1111 /* load the initial configuration from the database */
1112 updateConfiguration(prefs
, kSCPreferencesNotificationApply
, (void *)store
);
1120 load_PreferencesMonitor(CFBundleRef bundle
, Boolean bundleVerbose
)
1122 #pragma unused(bundle)
1123 #pragma unused(bundleVerbose)
1124 SC_log(LOG_DEBUG
, "load() called");
1125 SC_log(LOG_DEBUG
, " bundle ID = %@", CFBundleGetIdentifier(bundle
));
1127 /* open a SCDynamicStore session to allow cache updates */
1128 store
= SCDynamicStoreCreate(NULL
,
1129 CFSTR("PreferencesMonitor.bundle"),
1132 if (store
== NULL
) {
1133 SC_log(LOG_NOTICE
, "SCDynamicStoreCreate() failed: %s", SCErrorString(SCError()));
1137 /* open a SCPreferences session */
1139 prefs
= SCPreferencesCreate(NULL
, CFSTR("PreferencesMonitor.bundle"), NULL
);
1141 prefs
= SCPreferencesCreate(NULL
, CFSTR("PreferencesMonitor.bundle"), CFSTR("/tmp/preferences.plist"));
1143 if (prefs
!= NULL
) {
1144 Boolean need_update
= FALSE
;
1145 CFStringRef new_model
;
1147 new_model
= _SC_hw_model(FALSE
);
1149 /* Need to regenerate the new configuration for new model */
1150 if (new_model
!= NULL
) {
1151 CFStringRef old_model
;
1153 old_model
= SCPreferencesGetValue(prefs
, MODEL
);
1154 if (old_model
!= NULL
&& !_SC_CFEqual(old_model
, new_model
)) {
1157 restorePrefs
= previousConfigurationAvailable();
1162 SCNetworkSetRef current
;
1164 current
= SCNetworkSetCopyCurrent(prefs
);
1165 if (current
!= NULL
) {
1166 /* network configuration available, disable template creation */
1167 haveConfiguration
= TRUE
;
1172 SC_log(LOG_NOTICE
, "SCPreferencesCreate() failed: %s", SCErrorString(SCError()));
1177 * register for change notifications.
1179 if (!SCPreferencesSetCallback(prefs
, updateConfiguration
, NULL
)) {
1180 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
1184 if (!SCPreferencesScheduleWithRunLoop(prefs
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
)) {
1185 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
1190 * watch InterfaceNamer and KernelEventMonitor changes to know when
1191 * the IORegistry has quiesced (to create the initial configuration
1192 * template), to track any pre-configured interfaces, and to ensure
1193 * that we create a network service for any active interfaces.
1195 watchSCDynamicStore();
1196 storeCallback(store
, NULL
, NULL
);
1202 if (store
!= NULL
) CFRelease(store
);
1203 if (prefs
!= NULL
) CFRelease(prefs
);
1204 haveConfiguration
= TRUE
;
1212 main(int argc
, char **argv
)
1215 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
1217 load_PreferencesMonitor(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
1218 prime_PreferencesMonitor();