]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/scprefs_observer.c
configd-888.1.2.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / scprefs_observer.c
1 /*
2 * Copyright (c) 2012, 2013, 2015, 2016 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 #include <CommonCrypto/CommonDigest.h>
24 #include <dirent.h>
25 #include <notify.h>
26 #include <os/log.h>
27 #include <sys/param.h>
28 #include <sys/queue.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #ifndef SC_LOG_HANDLE
33 #define SC_LOG_HANDLE __log_SCPreferences()
34 #endif //SC_LOG_HANDLE
35 os_log_t SC_LOG_HANDLE;
36
37 #include <SystemConfiguration/SCPrivate.h>
38 #include <SystemConfiguration/scprefs_observer.h>
39
40 #define MANAGED_PREFERENCES_PATH "/Library/Managed Preferences"
41 #if TARGET_OS_IPHONE
42 #define MOBILE_PREFERENCES_PATH "/var/mobile/Library/Preferences"
43 #endif // TARGET_OS_IPHONE
44
45 #if !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
50
51 #pragma mark -
52 #pragma mark Utils
53
54 static void
55 iterate_dir(const char *d_name, const char *f_name,
56 CC_SHA1_CTX *ctxP, Boolean *found)
57 {
58 DIR *dir;
59 struct dirent * dp;
60
61 dir = opendir(d_name);
62
63 if (dir == NULL) {
64 /* if directory path does not exist */
65 return;
66 }
67
68 while ((dp = readdir(dir)) != NULL) {
69 char full_path[MAXPATHLEN];
70 struct stat s;
71
72 if ((strcmp(dp->d_name, ".") == 0) ||
73 (strcmp(dp->d_name, "..") == 0)) {
74 continue;
75 }
76
77 /* check path */
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) {
84 /*
85 * if this is the requested file, include
86 * the path and last modification time in
87 * the digest
88 */
89 CC_SHA1_Update(ctxP, full_path, (CC_LONG)strlen(full_path));
90 CC_SHA1_Update(ctxP,
91 (void *)&s.st_mtimespec.tv_sec,
92 sizeof(s.st_mtimespec.tv_sec));
93 *found = TRUE;
94 }
95 }
96 }
97 closedir(dir);
98 return;
99 }
100
101 static CF_RETURNS_RETAINED CFDataRef
102 build_digest(const char *top_dir, const char *file)
103 {
104 unsigned char bytes[CC_SHA1_DIGEST_LENGTH];
105 CC_SHA1_CTX ctx;
106 CFDataRef digest = NULL;
107 Boolean found = FALSE;
108
109 CC_SHA1_Init(&ctx);
110 iterate_dir(top_dir, file, &ctx, &found);
111 CC_SHA1_Final(bytes, &ctx);
112 if (found) {
113 digest = CFDataCreate(NULL, bytes, sizeof(bytes));
114 }
115 return (digest);
116 }
117
118 #pragma mark -
119 #pragma mark perfs_observer Private
120
121 struct _scprefs_observer_t {
122 _scprefs_observer_type type;
123 dispatch_block_t block;
124 CFDataRef digest;
125 SLIST_ENTRY(_scprefs_observer_t) next;
126 dispatch_queue_t queue;
127 char file[0];
128 };
129
130 static const char *
131 prefs_observer_get_prefs_path(scprefs_observer_t observer)
132 {
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
143 default:
144 return (NULL);
145 }
146 }
147
148 /*
149 * Iterate through all of the directories and subdirectories.
150 * If the file within those directories has changed,
151 * then generate the notification.
152 */
153 static Boolean
154 has_changed(scprefs_observer_t observer) {
155 Boolean changed;
156 CFDataRef digest = NULL;
157 const char * starting_path;
158
159 starting_path = prefs_observer_get_prefs_path(observer);
160
161 digest = build_digest(starting_path, observer->file);
162
163 /* compare old vs. new digest */
164 changed = _SC_CFEqual(digest, observer->digest)?FALSE:TRUE;
165
166 /* save the digest */
167 if (observer->digest != NULL) {
168 CFRelease(observer->digest);
169 }
170
171 observer->digest = digest;
172
173 SC_log(LOG_INFO, "preferences file: \"%s\", %s",
174 observer->file,
175 changed ? "has changed" : "has not changed");
176 return changed;
177 }
178
179 static dispatch_queue_t
180 prefs_observer_queue;
181
182 /* This holds the list of the observers */
183 static SLIST_HEAD(mylist, _scprefs_observer_t) head;
184
185 static void
186 prefs_observer_release(scprefs_observer_t observer)
187 {
188 SLIST_REMOVE(&head, observer, _scprefs_observer_t, next);
189
190 /* Now free the observer */
191 if (observer->digest != NULL) {
192 CFRelease(observer->digest);
193 }
194
195 free(observer);
196 }
197
198 static void
199 prefs_observer_handle_notifications()
200 {
201 scprefs_observer_t observer;
202
203 SC_log(LOG_INFO, "PrefsObserver notification received");
204
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);
210 }
211 }
212 }
213
214 static void
215 _prefs_observer_init()
216 {
217 uint32_t status;
218 static int token;
219
220 prefs_observer_queue = dispatch_queue_create("com.apple.SystemConfiguration.SCPreferencesObserver", NULL);
221
222 SLIST_INIT(&head);
223
224 status = notify_register_dispatch(PREFS_OBSERVER_KEY,
225 &token,
226 prefs_observer_queue,
227 ^(int token) {
228 prefs_observer_handle_notifications();
229 });
230 if (status != NOTIFY_STATUS_OK) {
231 SC_log(LOG_INFO, "notify_register_dispatch() failed: %d", status);
232 }
233 }
234
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)
240 {
241 scprefs_observer_t observer;
242 size_t path_buflen;
243
244 path_buflen = strlen(plist_name) + 1;
245
246 observer = (scprefs_observer_t)malloc(sizeof(struct _scprefs_observer_t) + path_buflen);
247 bzero((void *)observer, sizeof(struct _scprefs_observer_t));
248
249 /* Create the observer */
250 observer->type = type;
251 strlcpy(observer->file, plist_name, path_buflen);
252
253 observer->queue = queue;
254 observer->block = Block_copy(block);
255
256 return (observer);
257 }
258
259 #pragma mark -
260 #pragma mark perfs_observer Public SPI
261 scprefs_observer_t
262 _scprefs_observer_watch(_scprefs_observer_type type, const char *plist_name,
263 dispatch_queue_t queue, dispatch_block_t block)
264 {
265 scprefs_observer_t elem;
266 static dispatch_once_t initialized;
267
268 dispatch_once(&initialized, ^{
269 _prefs_observer_init();
270 });
271
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);
274
275 dispatch_sync(prefs_observer_queue, ^{
276 /* Enqueue the request */
277 SLIST_INSERT_HEAD(&head, elem, next);
278 });
279 return (elem);
280 }
281
282 /* This will cancel/deregister the given watcher. This will be synchronized on the
283 * internally created queue. */
284 void
285 _scprefs_observer_cancel(scprefs_observer_t observer)
286 {
287 dispatch_sync(prefs_observer_queue, ^{
288 prefs_observer_release((scprefs_observer_t)observer);
289 });
290 }
291
292 #pragma mark -
293
294 #ifdef TEST_MAIN
295 int main()
296 {
297 int random = 1;
298
299 _sc_verbose = 1;
300
301 dispatch_queue_t q = dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.mainQ", NULL);
302
303 dispatch_queue_t q1 = dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ1", NULL);
304
305 dispatch_block_t b1 = ^{
306 printf("Block 1 executed \n");
307 };
308
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");
312 };
313
314 dispatch_queue_t q3 = dispatch_queue_create("com.apple.SystemConfiguration.PrefsObserver.testQ2", NULL);
315
316 dispatch_block_t b3 = ^{
317 printf("Block 3 executed \n");
318 };
319
320 __block scprefs_observer_t observer1 = _scprefs_observer_watch(scprefs_observer_type_mcx, "com.apple.SystemConfiguration", q1, b1);
321
322 __block scprefs_observer_t observer2 = _scprefs_observer_watch(scprefs_observer_type_mcx, "foo", q2, b2);
323
324 __block scprefs_observer_t observer3 = _scprefs_observer_watch(scprefs_observer_type_mcx, "bar", q3, b3);
325
326 __block scprefs_observer_t observer = NULL;
327
328 while (1) {
329 switch (random % 3)
330 {
331 case 0:
332 dispatch_async(q, ^{
333 _SC_prefs_observer_cancel(observer1);
334 observer1 = NULL;
335 });
336 dispatch_async(q, ^{
337 if (observer != NULL) _SC_prefs_observer_cancel(observer);
338 observer = _SC_prefs_observer_watch(SC_prefs_observer_type_mcx, "test", q2, b2);
339 });
340 dispatch_sync(q, ^{
341 observer1 = observer;
342 });
343 sleep(random);
344 break;
345 case 1:
346 dispatch_async(q, ^{
347 _SC_prefs_observer_cancel(observer2);
348 });
349 dispatch_async(q, ^{
350 if (observer != NULL) _SC_prefs_observer_cancel(observer);
351 });
352 dispatch_sync(q, ^{
353 observer = _SC_prefs_observer_watch(SC_prefs_observer_type_mcx, "test", q2, b2);
354 });
355 sleep(random);
356 break;
357 case 2:
358 sleep (random);
359 default:
360 break;
361 }
362 random++;
363 }
364 dispatch_main();
365 }
366 #endif