2 * Copyright (c) 2012, 2013, 2015 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>
26 #include <sys/param.h>
27 #include <sys/queue.h>
28 #include <sys/types.h>
30 #include <SystemConfiguration/SCPrivate.h>
31 #include <SystemConfiguration/scprefs_observer.h>
33 #define MANAGED_PREFERENCES_PATH "/Library/Managed Preferences"
35 #define MOBILE_PREFERENCES_PATH "/var/mobile/Library/Preferences"
36 #endif // TARGET_OS_IPHONE
39 #define PREFS_OBSERVER_KEY "com.apple.MCX._managementStatusChangedForDomains"
40 #else // !TARGET_OS_IPHONE
41 #define PREFS_OBSERVER_KEY "com.apple.ManagedConfiguration.profileListChanged"
42 #endif // !TARGET_OS_IPHONE
48 iterate_dir(const char *d_name
, const char *f_name
,
49 CC_SHA1_CTX
*ctxP
, Boolean
*found
)
54 dir
= opendir(d_name
);
57 /* if directory path does not exist */
61 while ((dp
= readdir(dir
)) != NULL
) {
62 char full_path
[MAXPATHLEN
];
65 if ((strcmp(dp
->d_name
, ".") == 0) ||
66 (strcmp(dp
->d_name
, "..") == 0)) {
71 snprintf(full_path
, sizeof(full_path
), "%s/%s", d_name
, dp
->d_name
);
72 if (stat(full_path
, &s
) == 0) {
73 if (S_ISDIR(s
.st_mode
)) {
74 // if sub-directory, iterate
75 iterate_dir(full_path
, f_name
, ctxP
, found
);
76 } else if (strcmp(f_name
, dp
->d_name
) == 0) {
78 * if this is the requested file, include
79 * the path and last modification time in
82 CC_SHA1_Update(ctxP
, full_path
, (CC_LONG
)strlen(full_path
));
84 (void *)&s
.st_mtimespec
.tv_sec
,
85 sizeof(s
.st_mtimespec
.tv_sec
));
94 static CF_RETURNS_RETAINED CFDataRef
95 build_digest(const char *top_dir
, const char *file
)
97 unsigned char bytes
[CC_SHA1_DIGEST_LENGTH
];
99 CFDataRef digest
= NULL
;
100 Boolean found
= FALSE
;
103 iterate_dir(top_dir
, file
, &ctx
, &found
);
104 CC_SHA1_Final(bytes
, &ctx
);
106 digest
= CFDataCreate(NULL
, bytes
, sizeof(bytes
));
112 #pragma mark perfs_observer Private
114 struct _scprefs_observer_t
{
115 _scprefs_observer_type type
;
116 dispatch_block_t block
;
118 SLIST_ENTRY(_scprefs_observer_t
) next
;
119 dispatch_queue_t queue
;
124 prefs_observer_get_prefs_path(scprefs_observer_t observer
)
126 switch (observer
->type
) {
127 #if !TARGET_OS_IPHONE
128 case scprefs_observer_type_mcx
:
129 return MANAGED_PREFERENCES_PATH
;
130 #else // !TARGET_OS_IPHONE
131 case scprefs_observer_type_global
:
132 return MANAGED_PREFERENCES_PATH
;
133 case scprefs_observer_type_profile
:
134 return MOBILE_PREFERENCES_PATH
;
135 #endif // !TARGET_OS_IPHONE
142 * Iterate through all of the directories and subdirectories.
143 * If the file within those directories has changed,
144 * then generate the notification.
147 has_changed(scprefs_observer_t observer
) {
149 CFDataRef digest
= NULL
;
150 const char * starting_path
;
152 starting_path
= prefs_observer_get_prefs_path(observer
);
154 digest
= build_digest(starting_path
, observer
->file
);
156 /* compare old vs. new digest */
157 changed
= _SC_CFEqual(digest
, observer
->digest
)?FALSE
:TRUE
;
159 /* save the digest */
160 if (observer
->digest
!= NULL
) {
161 CFRelease(observer
->digest
);
164 observer
->digest
= digest
;
166 SC_log(LOG_INFO
, "preferences file: \"%s\", %s",
168 changed
? "has changed" : "has not changed");
172 static dispatch_queue_t
173 prefs_observer_queue
;
175 /* This holds the list of the observers */
176 static SLIST_HEAD(mylist
, _scprefs_observer_t
) head
;
179 prefs_observer_release(scprefs_observer_t observer
)
181 SLIST_REMOVE(&head
, observer
, _scprefs_observer_t
, next
);
183 /* Now free the observer */
184 if (observer
->digest
!= NULL
) {
185 CFRelease(observer
->digest
);
192 prefs_observer_handle_notifications()
194 scprefs_observer_t observer
;
196 SC_log(LOG_INFO
, "PrefsObserver notification received");
198 SLIST_FOREACH(observer
, &head
, next
) {
199 /* if the preferences plist has changed,
200 * called the block */
201 if (has_changed(observer
)) {
202 dispatch_async(observer
->queue
, observer
->block
);
208 _prefs_observer_init()
212 prefs_observer_queue
= dispatch_queue_create("com.apple.SystemConfiguration.SCPreferencesObserver", NULL
);
216 notify_register_dispatch(PREFS_OBSERVER_KEY
,
218 prefs_observer_queue
,
219 ^(int token
) { prefs_observer_handle_notifications(); });
222 static scprefs_observer_t
223 prefs_observer_priv_create(_scprefs_observer_type type
,
224 const char *plist_name
,
225 dispatch_queue_t queue
,
226 dispatch_block_t block
)
228 scprefs_observer_t observer
;
231 path_buflen
= strlen(plist_name
) + 1;
233 observer
= (scprefs_observer_t
)malloc(sizeof(struct _scprefs_observer_t
) + path_buflen
);
234 bzero((void *)observer
, sizeof(struct _scprefs_observer_t
));
236 /* Create the observer */
237 observer
->type
= type
;
238 strlcpy(observer
->file
, plist_name
, path_buflen
);
240 observer
->queue
= queue
;
241 observer
->block
= Block_copy(block
);
247 #pragma mark perfs_observer Public SPI
249 _scprefs_observer_watch(_scprefs_observer_type type
, const char *plist_name
,
250 dispatch_queue_t queue
, dispatch_block_t block
)
252 scprefs_observer_t elem
;
253 static dispatch_once_t initialized
;
255 dispatch_once(&initialized
, ^{
256 _prefs_observer_init();
259 elem
= prefs_observer_priv_create(type
, plist_name
, queue
, block
);
260 SC_log(LOG_INFO
, "Created a new element to watch for %s", elem
->file
);
262 dispatch_sync(prefs_observer_queue
, ^{
263 /* Enqueue the request */
264 SLIST_INSERT_HEAD(&head
, elem
, next
);
269 /* This will cancel/deregister the given watcher. This will be synchronized on the
270 * internally created queue. */
272 _scprefs_observer_cancel(scprefs_observer_t observer
)
274 dispatch_sync(prefs_observer_queue
, ^{
275 prefs_observer_release((scprefs_observer_t
)observer
);
288 dispatch_queue_t q
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.mainQ", NULL
);
290 dispatch_queue_t q1
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ1", NULL
);
292 dispatch_block_t b1
= ^{
293 printf("Block 1 executed \n");
296 dispatch_queue_t q2
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL
);
297 dispatch_block_t b2
= ^{
298 printf("Block 2 executed \n");
301 dispatch_queue_t q3
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL
);
303 dispatch_block_t b3
= ^{
304 printf("Block 3 executed \n");
307 __block scprefs_observer_t observer1
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "com.apple.SystemConfiguration", q1
, b1
);
309 __block scprefs_observer_t observer2
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "foo", q2
, b2
);
311 __block scprefs_observer_t observer3
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "bar", q3
, b3
);
313 __block scprefs_observer_t observer
= NULL
;
320 _SC_prefs_observer_cancel(observer1
);
324 if (observer
!= NULL
) _SC_prefs_observer_cancel(observer
);
325 observer
= _SC_prefs_observer_watch(SC_prefs_observer_type_mcx
, "test", q2
, b2
);
328 observer1
= observer
;
334 _SC_prefs_observer_cancel(observer2
);
337 if (observer
!= NULL
) _SC_prefs_observer_cancel(observer
);
340 observer
= _SC_prefs_observer_watch(SC_prefs_observer_type_mcx
, "test", q2
, b2
);