2 * Copyright (c) 2012, 2013, 2015, 2016 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
;
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_SHA1_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_SHA1_Update(ctxP
, full_path
, (CC_LONG
)strlen(full_path
));
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_SHA1_DIGEST_LENGTH
];
106 CFDataRef digest
= NULL
;
107 Boolean found
= FALSE
;
110 iterate_dir(top_dir
, file
, &ctx
, &found
);
111 CC_SHA1_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(LOG_INFO
, "preferences file: \"%s\", %s",
175 changed
? "has changed" : "has not changed");
179 static dispatch_queue_t
180 prefs_observer_queue
;
182 /* This holds the list of the observers */
183 static SLIST_HEAD(mylist
, _scprefs_observer_t
) head
;
186 prefs_observer_release(scprefs_observer_t observer
)
188 SLIST_REMOVE(&head
, observer
, _scprefs_observer_t
, next
);
190 /* Now free the observer */
191 if (observer
->digest
!= NULL
) {
192 CFRelease(observer
->digest
);
199 prefs_observer_handle_notifications()
201 scprefs_observer_t observer
;
203 SC_log(LOG_INFO
, "PrefsObserver notification received");
205 SLIST_FOREACH(observer
, &head
, next
) {
206 /* if the preferences plist has changed,
207 * called 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 prefs_observer_handle_notifications();
230 if (status
!= NOTIFY_STATUS_OK
) {
231 SC_log(LOG_INFO
, "notify_register_dispatch() failed: %d", status
);
235 static scprefs_observer_t
236 prefs_observer_priv_create(_scprefs_observer_type type
,
237 const char *plist_name
,
238 dispatch_queue_t queue
,
239 dispatch_block_t block
)
241 scprefs_observer_t observer
;
244 path_buflen
= strlen(plist_name
) + 1;
246 observer
= (scprefs_observer_t
)malloc(sizeof(struct _scprefs_observer_t
) + path_buflen
);
247 bzero((void *)observer
, sizeof(struct _scprefs_observer_t
));
249 /* Create the observer */
250 observer
->type
= type
;
251 strlcpy(observer
->file
, plist_name
, path_buflen
);
253 observer
->queue
= queue
;
254 observer
->block
= Block_copy(block
);
260 #pragma mark perfs_observer Public SPI
262 _scprefs_observer_watch(_scprefs_observer_type type
, const char *plist_name
,
263 dispatch_queue_t queue
, dispatch_block_t block
)
265 scprefs_observer_t elem
;
266 static dispatch_once_t initialized
;
268 dispatch_once(&initialized
, ^{
269 _prefs_observer_init();
272 elem
= prefs_observer_priv_create(type
, plist_name
, queue
, block
);
273 SC_log(LOG_INFO
, "Created a new element to watch for %s", elem
->file
);
275 dispatch_sync(prefs_observer_queue
, ^{
276 /* Enqueue the request */
277 SLIST_INSERT_HEAD(&head
, elem
, next
);
282 /* This will cancel/deregister the given watcher. This will be synchronized on the
283 * internally created queue. */
285 _scprefs_observer_cancel(scprefs_observer_t observer
)
287 dispatch_sync(prefs_observer_queue
, ^{
288 prefs_observer_release((scprefs_observer_t
)observer
);
301 dispatch_queue_t q
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.mainQ", NULL
);
303 dispatch_queue_t q1
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ1", NULL
);
305 dispatch_block_t b1
= ^{
306 printf("Block 1 executed \n");
309 dispatch_queue_t q2
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL
);
310 dispatch_block_t b2
= ^{
311 printf("Block 2 executed \n");
314 dispatch_queue_t q3
= dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL
);
316 dispatch_block_t b3
= ^{
317 printf("Block 3 executed \n");
320 __block scprefs_observer_t observer1
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "com.apple.SystemConfiguration", q1
, b1
);
322 __block scprefs_observer_t observer2
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "foo", q2
, b2
);
324 __block scprefs_observer_t observer3
= _scprefs_observer_watch(scprefs_observer_type_mcx
, "bar", q3
, b3
);
326 __block scprefs_observer_t observer
= NULL
;
333 _SC_prefs_observer_cancel(observer1
);
337 if (observer
!= NULL
) _SC_prefs_observer_cancel(observer
);
338 observer
= _SC_prefs_observer_watch(SC_prefs_observer_type_mcx
, "test", q2
, b2
);
341 observer1
= observer
;
347 _SC_prefs_observer_cancel(observer2
);
350 if (observer
!= NULL
) _SC_prefs_observer_cancel(observer
);
353 observer
= _SC_prefs_observer_watch(SC_prefs_observer_type_mcx
, "test", q2
, b2
);