2 * Copyright (c) 2012, 2013, 2015-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@
23 #include <CommonCrypto/CommonDigest.h>
27 #include <sys/param.h>
28 #include <sys/queue.h>
29 #include <sys/types.h>
33 #define SC_LOG_HANDLE __log_SCPreferences
34 #endif //SC_LOG_HANDLE
35 os_log_t
SC_LOG_HANDLE(void);
37 #include <SystemConfiguration/SCPrivate.h>
38 #include <SystemConfiguration/scprefs_observer.h>
40 #define MANAGED_PREFERENCES_PATH "/Library/Managed Preferences"
42 #define MOBILE_PREFERENCES_PATH "/var/mobile/Library/Preferences"
43 #endif // TARGET_OS_IPHONE
46 #define PREFS_OBSERVER_KEY "com.apple.MCX._managementStatusChangedForDomains"
47 #else // !TARGET_OS_IPHONE
48 #define PREFS_OBSERVER_KEY "com.apple.ManagedConfiguration.profileListChanged"
49 #endif // !TARGET_OS_IPHONE
55 iterate_dir(const char *d_name
, const char *f_name
,
56 CC_SHA256_CTX
*ctxP
, Boolean
*found
)
61 dir
= opendir(d_name
);
64 /* if directory path does not exist */
68 while ((dp
= readdir(dir
)) != NULL
) {
69 char full_path
[MAXPATHLEN
];
72 if ((strcmp(dp
->d_name
, ".") == 0) ||
73 (strcmp(dp
->d_name
, "..") == 0)) {
78 snprintf(full_path
, sizeof(full_path
), "%s/%s", d_name
, dp
->d_name
);
79 if (stat(full_path
, &s
) == 0) {
80 if (S_ISDIR(s
.st_mode
)) {
81 // if sub-directory, iterate
82 iterate_dir(full_path
, f_name
, ctxP
, found
);
83 } else if (strcmp(f_name
, dp
->d_name
) == 0) {
85 * if this is the requested file, include
86 * the path and last modification time in
89 CC_SHA256_Update(ctxP
, full_path
, (CC_LONG
)strlen(full_path
));
90 CC_SHA256_Update(ctxP
,
91 (void *)&s
.st_mtimespec
.tv_sec
,
92 sizeof(s
.st_mtimespec
.tv_sec
));
101 static CF_RETURNS_RETAINED CFDataRef
102 build_digest(const char *top_dir
, const char *file
)
104 unsigned char bytes
[CC_SHA256_DIGEST_LENGTH
];
106 CFDataRef digest
= NULL
;
107 Boolean found
= FALSE
;
109 CC_SHA256_Init(&ctx
);
110 iterate_dir(top_dir
, file
, &ctx
, &found
);
111 CC_SHA256_Final(bytes
, &ctx
);
113 digest
= CFDataCreate(NULL
, bytes
, sizeof(bytes
));
119 #pragma mark perfs_observer Private
121 struct _scprefs_observer_t
{
122 _scprefs_observer_type type
;
123 dispatch_block_t block
;
125 SLIST_ENTRY(_scprefs_observer_t
) next
;
126 dispatch_queue_t queue
;
131 prefs_observer_get_prefs_path(scprefs_observer_t observer
)
133 switch (observer
->type
) {
134 #if !TARGET_OS_IPHONE
135 case scprefs_observer_type_mcx
:
136 return MANAGED_PREFERENCES_PATH
;
137 #else // !TARGET_OS_IPHONE
138 case scprefs_observer_type_global
:
139 return MANAGED_PREFERENCES_PATH
;
140 case scprefs_observer_type_profile
:
141 return MOBILE_PREFERENCES_PATH
;
142 #endif // !TARGET_OS_IPHONE
149 * Iterate through all of the directories and subdirectories.
150 * If the file within those directories has changed,
151 * then generate the notification.
154 has_changed(scprefs_observer_t observer
) {
156 CFDataRef digest
= NULL
;
157 const char * starting_path
;
159 starting_path
= prefs_observer_get_prefs_path(observer
);
161 digest
= build_digest(starting_path
, observer
->file
);
163 /* compare old vs. new digest */
164 changed
= _SC_CFEqual(digest
, observer
->digest
)?FALSE
:TRUE
;
166 /* save the digest */
167 if (observer
->digest
!= NULL
) {
168 CFRelease(observer
->digest
);
171 observer
->digest
= digest
;
173 SC_log(changed
? LOG_INFO
: LOG_DEBUG
,
174 "preferences file: \"%s\" %s",
176 changed
? "changed" : "did not change");
180 static dispatch_queue_t
181 prefs_observer_queue
;
183 /* This holds the list of the observers */
184 static SLIST_HEAD(mylist
, _scprefs_observer_t
) head
;
187 prefs_observer_release(scprefs_observer_t observer
)
189 SLIST_REMOVE(&head
, observer
, _scprefs_observer_t
, next
);
191 /* Now free the observer */
192 if (observer
->digest
!= NULL
) {
193 CFRelease(observer
->digest
);
200 prefs_observer_handle_notifications()
202 scprefs_observer_t observer
;
204 SC_log(LOG_DEBUG
, "PrefsObserver notification received");
206 SLIST_FOREACH(observer
, &head
, next
) {
207 /* if the preferences plist changed, call the block */
208 if (has_changed(observer
)) {
209 dispatch_async(observer
->queue
, observer
->block
);
215 _prefs_observer_init()
220 prefs_observer_queue
= dispatch_queue_create("com.apple.SystemConfiguration.SCPreferencesObserver", NULL
);
224 status
= notify_register_dispatch(PREFS_OBSERVER_KEY
,
226 prefs_observer_queue
,
228 #pragma unused(token)
229 prefs_observer_handle_notifications();
231 if (status
!= NOTIFY_STATUS_OK
) {
232 SC_log(LOG_INFO
, "notify_register_dispatch() failed: %d", status
);
236 static scprefs_observer_t
237 prefs_observer_priv_create(_scprefs_observer_type type
,
238 const char *plist_name
,
239 dispatch_queue_t queue
,
240 dispatch_block_t block
)
242 scprefs_observer_t observer
;
245 path_buflen
= strlen(plist_name
) + 1;
247 observer
= (scprefs_observer_t
)malloc(sizeof(struct _scprefs_observer_t
) + path_buflen
);
248 memset((void *)observer
, 0, sizeof(struct _scprefs_observer_t
));
250 /* Create the observer */
251 observer
->type
= type
;
252 strlcpy(observer
->file
, plist_name
, path_buflen
);
254 observer
->queue
= queue
;
255 observer
->block
= Block_copy(block
);
261 #pragma mark perfs_observer Public SPI
263 _scprefs_observer_watch(_scprefs_observer_type type
, const char *plist_name
,
264 dispatch_queue_t queue
, dispatch_block_t block
)
266 scprefs_observer_t elem
;
267 static dispatch_once_t initialized
;
269 dispatch_once(&initialized
, ^{
270 _prefs_observer_init();
273 elem
= prefs_observer_priv_create(type
, plist_name
, queue
, block
);
274 SC_log(LOG_INFO
, "Created a new element to watch for %s", elem
->file
);
276 dispatch_sync(prefs_observer_queue
, ^{
277 /* Enqueue the request */
278 SLIST_INSERT_HEAD(&head
, elem
, next
);
283 /* This will cancel/deregister the given watcher. This will be synchronized on the
284 * internally created queue. */
286 _scprefs_observer_cancel(scprefs_observer_t observer
)
288 dispatch_sync(prefs_observer_queue
, ^{
289 prefs_observer_release((scprefs_observer_t
)observer
);
302 dispatch_queue_t q
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.mainQ", NULL
);
304 dispatch_queue_t q1
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ1", NULL
);
306 dispatch_block_t b1
= ^{
307 printf("Block 1 executed\n");
310 dispatch_queue_t q2
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL
);
311 dispatch_block_t b2
= ^{
312 printf("Block 2 executed\n");
315 dispatch_queue_t q3
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL
);
317 dispatch_block_t b3
= ^{
318 printf("Block 3 executed\n");
321 __block scprefs_observer_t observer1
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "com.apple.SystemConfiguration", q1
, b1
);
323 __block scprefs_observer_t observer2
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "foo", q2
, b2
);
325 __block scprefs_observer_t observer3
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "bar", q3
, b3
);
327 __block scprefs_observer_t observer
= NULL
;
334 _SC_prefs_observer_cancel(observer1
);
338 if (observer
!= NULL
) _SC_prefs_observer_cancel(observer
);
339 observer
= _SC_prefs_observer_watch(SC_prefs_observer_type_mcx
, "test", q2
, b2
);
342 observer1
= observer
;
348 _SC_prefs_observer_cancel(observer2
);
351 if (observer
!= NULL
) _SC_prefs_observer_cancel(observer
);
354 observer
= _SC_prefs_observer_watch(SC_prefs_observer_type_mcx
, "test", q2
, b2
);