]>
Commit | Line | Data |
---|---|---|
5958d7c0 | 1 | /* |
f715d946 | 2 | * Copyright (c) 2000-2005, 2008-2015, 2017, 2018 Apple Inc. All rights reserved. |
5958d7c0 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
942cecd7 | 5 | * |
009ee3c6 A |
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. | |
942cecd7 | 12 | * |
009ee3c6 A |
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 | |
5958d7c0 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
009ee3c6 A |
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. | |
942cecd7 | 20 | * |
5958d7c0 A |
21 | * @APPLE_LICENSE_HEADER_END@ |
22 | */ | |
23 | ||
0fae82ee A |
24 | /* |
25 | * Modification History | |
26 | * | |
27 | * June 1, 2001 Allan Nathanson <ajn@apple.com> | |
28 | * - public API conversion | |
29 | * | |
30 | * November 9, 2000 Allan Nathanson <ajn@apple.com> | |
31 | * - initial revision | |
32 | */ | |
33 | ||
009ee3c6 | 34 | #include <pthread.h> |
5958d7c0 A |
35 | #include <sys/types.h> |
36 | #include <sys/socket.h> | |
17d3ee29 | 37 | #include <sys/time.h> |
5958d7c0 A |
38 | #include <sys/un.h> |
39 | #include <unistd.h> | |
40 | ||
41 | #include "scutil.h" | |
dbf6a266 | 42 | #include "notifications.h" |
0fae82ee A |
43 | |
44 | ||
17d3ee29 A |
45 | static char * |
46 | elapsed() | |
47 | { | |
1ef45fa4 | 48 | size_t n; |
17d3ee29 A |
49 | static char str[128]; |
50 | struct tm tm_diff; | |
51 | struct tm tm_now; | |
52 | struct timeval tv_diff; | |
53 | struct timeval tv_now; | |
54 | static struct timeval tv_then = { 0, 0 }; | |
55 | ||
56 | (void)gettimeofday(&tv_now, NULL); | |
57 | ||
58 | (void)localtime_r(&tv_now.tv_sec, &tm_now); | |
59 | ||
60 | timersub(&tv_now, &tv_then, &tv_diff); | |
61 | (void)localtime_r(&tv_diff.tv_sec, &tm_diff); | |
62 | n = snprintf(str, sizeof(str), "%2d:%02d:%02d.%03d", | |
63 | tm_now.tm_hour, | |
64 | tm_now.tm_min, | |
65 | tm_now.tm_sec, | |
66 | tv_now.tv_usec / 1000); | |
67 | if (((tv_then.tv_sec != 0) || (tv_then.tv_usec != 0)) && (n < sizeof(str))) { | |
68 | snprintf(&str[n], sizeof(str) - n, " (+%ld.%03d)", | |
69 | tv_diff.tv_sec, | |
70 | tv_diff.tv_usec / 1000); | |
71 | } | |
72 | ||
73 | tv_then = tv_now; | |
74 | return str; | |
75 | } | |
76 | ||
77 | ||
0fae82ee | 78 | static CFComparisonResult |
1ef45fa4 A |
79 | sort_keys(const void *p1, const void *p2, void *context) |
80 | { | |
81 | #pragma unused(context) | |
0fae82ee A |
82 | CFStringRef key1 = (CFStringRef)p1; |
83 | CFStringRef key2 = (CFStringRef)p2; | |
84 | return CFStringCompare(key1, key2, 0); | |
85 | } | |
86 | ||
5958d7c0 | 87 | |
dbf6a266 | 88 | __private_extern__ |
5958d7c0 | 89 | void |
0fae82ee | 90 | storeCallback(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info) |
5958d7c0 | 91 | { |
1ef45fa4 | 92 | #pragma unused(info) |
5958d7c0 | 93 | int i; |
0fae82ee A |
94 | CFIndex n; |
95 | ||
96 | SCPrint(TRUE, stdout, CFSTR("notification callback (store address = %p).\n"), store); | |
97 | ||
98 | n = CFArrayGetCount(changedKeys); | |
99 | if (n > 0) { | |
009ee3c6 | 100 | for (i = 0; i < n; i++) { |
0fae82ee A |
101 | SCPrint(TRUE, |
102 | stdout, | |
17d3ee29 A |
103 | CFSTR(" %s changedKey [%d] = %@\n"), |
104 | elapsed(), | |
0fae82ee A |
105 | i, |
106 | CFArrayGetValueAtIndex(changedKeys, i)); | |
107 | } | |
108 | } else { | |
a5f60add | 109 | SCPrint(TRUE, stdout, CFSTR(" no changed key's.\n")); |
0fae82ee A |
110 | } |
111 | ||
112 | return; | |
113 | } | |
114 | ||
115 | ||
dbf6a266 | 116 | __private_extern__ |
0fae82ee A |
117 | void |
118 | do_notify_list(int argc, char **argv) | |
119 | { | |
1ef45fa4 | 120 | #pragma unused(argv) |
0fae82ee A |
121 | int i; |
122 | CFArrayRef list; | |
123 | CFIndex listCnt; | |
124 | Boolean isRegex = FALSE; | |
125 | CFMutableArrayRef sortedList; | |
5958d7c0 | 126 | |
dbf6a266 | 127 | if (store == NULL) { |
009ee3c6 A |
128 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession)); |
129 | return; | |
130 | } | |
131 | ||
5958d7c0 | 132 | if (argc == 1) |
0fae82ee | 133 | isRegex = TRUE; |
5958d7c0 | 134 | |
009ee3c6 | 135 | list = isRegex ? watchedPatterns : watchedKeys; |
0fae82ee | 136 | if (!list) { |
009ee3c6 A |
137 | SCPrint(TRUE, |
138 | stdout, | |
139 | CFSTR(" no notifier %s.\n"), | |
140 | isRegex ? "patterns" : "keys"); | |
5958d7c0 A |
141 | return; |
142 | } | |
143 | ||
144 | listCnt = CFArrayGetCount(list); | |
0fae82ee | 145 | sortedList = CFArrayCreateMutableCopy(NULL, listCnt, list); |
0fae82ee A |
146 | CFArraySortValues(sortedList, |
147 | CFRangeMake(0, listCnt), | |
148 | sort_keys, | |
149 | NULL); | |
150 | ||
5958d7c0 | 151 | if (listCnt > 0) { |
009ee3c6 | 152 | for (i = 0; i < listCnt; i++) { |
0fae82ee A |
153 | SCPrint(TRUE, |
154 | stdout, | |
a5f60add A |
155 | CFSTR(" notifier %s [%d] = %@\n"), |
156 | isRegex ? "pattern" : "key", | |
0fae82ee A |
157 | i, |
158 | CFArrayGetValueAtIndex(sortedList, i)); | |
5958d7c0 A |
159 | } |
160 | } else { | |
a5f60add A |
161 | SCPrint(TRUE, |
162 | stdout, | |
163 | CFSTR(" no notifier %s.\n"), | |
164 | isRegex ? "patterns" : "keys"); | |
5958d7c0 | 165 | } |
0fae82ee | 166 | CFRelease(sortedList); |
5958d7c0 A |
167 | |
168 | return; | |
169 | } | |
170 | ||
171 | ||
dbf6a266 | 172 | __private_extern__ |
5958d7c0 A |
173 | void |
174 | do_notify_add(int argc, char **argv) | |
175 | { | |
009ee3c6 A |
176 | CFStringRef key; |
177 | CFMutableArrayRef keys; | |
178 | Boolean isRegex = FALSE; | |
179 | ||
dbf6a266 | 180 | if (store == NULL) { |
009ee3c6 A |
181 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession)); |
182 | return; | |
183 | } | |
5958d7c0 | 184 | |
dbf6a266 | 185 | key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8); |
5958d7c0 A |
186 | |
187 | if (argc == 2) | |
0fae82ee | 188 | isRegex = TRUE; |
5958d7c0 | 189 | |
009ee3c6 A |
190 | keys = isRegex ? watchedPatterns : watchedKeys; |
191 | if (CFArrayContainsValue(keys, CFRangeMake(0, CFArrayGetCount(keys)), key)) { | |
192 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusKeyExists)); | |
193 | CFRelease(key); | |
194 | return; | |
5958d7c0 | 195 | } |
009ee3c6 A |
196 | |
197 | CFArrayAppendValue(keys, key); | |
0fae82ee | 198 | CFRelease(key); |
009ee3c6 A |
199 | |
200 | if (!SCDynamicStoreSetNotificationKeys(store, watchedKeys, watchedPatterns)) { | |
201 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); | |
202 | } | |
203 | ||
5958d7c0 A |
204 | return; |
205 | } | |
206 | ||
207 | ||
dbf6a266 | 208 | __private_extern__ |
5958d7c0 A |
209 | void |
210 | do_notify_remove(int argc, char **argv) | |
211 | { | |
009ee3c6 A |
212 | CFStringRef key; |
213 | CFMutableArrayRef keys; | |
214 | CFIndex i; | |
215 | Boolean isRegex = FALSE; | |
216 | ||
dbf6a266 | 217 | if (store == NULL) { |
009ee3c6 A |
218 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession)); |
219 | return; | |
220 | } | |
5958d7c0 | 221 | |
dbf6a266 | 222 | key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8); |
5958d7c0 A |
223 | |
224 | if (argc == 2) | |
0fae82ee | 225 | isRegex = TRUE; |
5958d7c0 | 226 | |
009ee3c6 A |
227 | keys = isRegex ? watchedPatterns : watchedKeys; |
228 | i = CFArrayGetFirstIndexOfValue(keys, CFRangeMake(0, CFArrayGetCount(keys)), key); | |
229 | CFRelease(key); | |
230 | ||
231 | if (i == kCFNotFound) { | |
232 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoKey)); | |
233 | return; | |
234 | } | |
235 | ||
236 | CFArrayRemoveValueAtIndex(keys, i); | |
237 | ||
238 | if (!SCDynamicStoreSetNotificationKeys(store, watchedKeys, watchedPatterns)) { | |
0fae82ee | 239 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); |
5958d7c0 | 240 | } |
009ee3c6 | 241 | |
5958d7c0 A |
242 | return; |
243 | } | |
244 | ||
245 | ||
dbf6a266 | 246 | __private_extern__ |
5958d7c0 A |
247 | void |
248 | do_notify_changes(int argc, char **argv) | |
249 | { | |
1ef45fa4 A |
250 | #pragma unused(argc) |
251 | #pragma unused(argv) | |
5958d7c0 A |
252 | CFArrayRef list; |
253 | CFIndex listCnt; | |
5958d7c0 A |
254 | int i; |
255 | ||
0fae82ee A |
256 | list = SCDynamicStoreCopyNotifiedKeys(store); |
257 | if (!list) { | |
258 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); | |
5958d7c0 A |
259 | return; |
260 | } | |
261 | ||
262 | listCnt = CFArrayGetCount(list); | |
263 | if (listCnt > 0) { | |
009ee3c6 | 264 | for (i = 0; i < listCnt; i++) { |
0fae82ee A |
265 | SCPrint(TRUE, |
266 | stdout, | |
17d3ee29 A |
267 | CFSTR(" %s changedKey [%d] = %@\n"), |
268 | elapsed(), | |
0fae82ee A |
269 | i, |
270 | CFArrayGetValueAtIndex(list, i)); | |
5958d7c0 A |
271 | } |
272 | } else { | |
0fae82ee | 273 | SCPrint(TRUE, stdout, CFSTR(" no changedKey's.\n")); |
5958d7c0 A |
274 | } |
275 | CFRelease(list); | |
276 | ||
277 | return; | |
278 | } | |
279 | ||
280 | ||
009ee3c6 A |
281 | static void * |
282 | _watcher(void *arg) | |
5958d7c0 | 283 | { |
1ef45fa4 | 284 | #pragma unused(arg) |
009ee3c6 | 285 | notifyRl = CFRunLoopGetCurrent(); |
6bb65964 | 286 | if (notifyRl == NULL) { |
009ee3c6 A |
287 | SCPrint(TRUE, stdout, CFSTR(" CFRunLoopGetCurrent() failed\n")); |
288 | return NULL; | |
289 | } | |
290 | ||
d0784775 | 291 | if (doDispatch) { |
5e9ce69e | 292 | if (!SCDynamicStoreSetDispatchQueue(store, dispatch_get_main_queue())) { |
d0784775 | 293 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); |
6bb65964 | 294 | notifyRl = NULL; |
d0784775 A |
295 | return NULL; |
296 | } | |
297 | notifyRls = (CFRunLoopSourceRef)kCFNull; | |
6bb65964 | 298 | } else { |
d0784775 | 299 | notifyRls = SCDynamicStoreCreateRunLoopSource(NULL, store, 0); |
6bb65964 | 300 | if (notifyRls == NULL) { |
d0784775 | 301 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); |
6bb65964 | 302 | notifyRl = NULL; |
d0784775 A |
303 | return NULL; |
304 | } | |
305 | CFRunLoopAddSource(notifyRl, notifyRls, kCFRunLoopDefaultMode); | |
009ee3c6 | 306 | } |
009ee3c6 | 307 | |
6bb65964 | 308 | pthread_setname_np("n.watch"); |
009ee3c6 | 309 | CFRunLoopRun(); |
6bb65964 | 310 | notifyRl = NULL; |
009ee3c6 A |
311 | return NULL; |
312 | } | |
313 | ||
dbf6a266 | 314 | __private_extern__ |
009ee3c6 A |
315 | void |
316 | do_notify_watch(int argc, char **argv) | |
317 | { | |
1ef45fa4 A |
318 | #pragma unused(argc) |
319 | #pragma unused(argv) | |
009ee3c6 A |
320 | pthread_attr_t tattr; |
321 | pthread_t tid; | |
322 | ||
6bb65964 A |
323 | if (notifyRl != NULL) { |
324 | SCPrint(TRUE, stdout, CFSTR("already active\n")); | |
0fae82ee A |
325 | return; |
326 | } | |
5958d7c0 | 327 | |
009ee3c6 A |
328 | pthread_attr_init(&tattr); |
329 | pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM); | |
330 | pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); | |
331 | // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack | |
332 | pthread_create(&tid, &tattr, _watcher, NULL); | |
333 | pthread_attr_destroy(&tattr); | |
334 | ||
0fae82ee A |
335 | return; |
336 | } | |
337 | ||
338 | ||
dbf6a266 | 339 | __private_extern__ |
0fae82ee A |
340 | void |
341 | do_notify_wait(int argc, char **argv) | |
342 | { | |
1ef45fa4 A |
343 | #pragma unused(argc) |
344 | #pragma unused(argv) | |
0fae82ee A |
345 | if (!SCDynamicStoreNotifyWait(store)) { |
346 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); | |
5958d7c0 A |
347 | return; |
348 | } | |
349 | ||
5958d7c0 A |
350 | return; |
351 | } | |
352 | ||
353 | ||
dbf6a266 | 354 | __private_extern__ |
5958d7c0 A |
355 | void |
356 | do_notify_file(int argc, char **argv) | |
357 | { | |
358 | int32_t reqID = 0; | |
5958d7c0 A |
359 | int fd; |
360 | union { | |
361 | char data[4]; | |
362 | int32_t gotID; | |
363 | } buf; | |
364 | char *bufPtr; | |
365 | int needed; | |
366 | ||
367 | if (argc == 1) { | |
368 | if ((sscanf(argv[0], "%d", &reqID) != 1)) { | |
0fae82ee | 369 | SCPrint(TRUE, stdout, CFSTR("invalid identifier.\n")); |
5958d7c0 A |
370 | return; |
371 | } | |
372 | } | |
373 | ||
0fae82ee A |
374 | if (!SCDynamicStoreNotifyFileDescriptor(store, reqID, &fd)) { |
375 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); | |
5958d7c0 A |
376 | return; |
377 | } | |
378 | ||
afb19109 | 379 | memset(buf.data, 0, sizeof(buf.data)); |
5958d7c0 A |
380 | bufPtr = &buf.data[0]; |
381 | needed = sizeof(buf.gotID); | |
382 | while (needed > 0) { | |
78403150 | 383 | ssize_t got; |
5958d7c0 A |
384 | |
385 | got = read(fd, bufPtr, needed); | |
386 | if (got == -1) { | |
387 | /* if error detected */ | |
0fae82ee | 388 | SCPrint(TRUE, stdout, CFSTR("read() failed: %s.\n"), strerror(errno)); |
5958d7c0 A |
389 | break; |
390 | } | |
391 | ||
392 | if (got == 0) { | |
393 | /* if end of file detected */ | |
0fae82ee | 394 | SCPrint(TRUE, stdout, CFSTR("read(): detected end of file.\n")); |
5958d7c0 A |
395 | break; |
396 | } | |
397 | ||
78403150 | 398 | SCPrint(TRUE, stdout, CFSTR("Received %ld bytes.\n"), got); |
5958d7c0 A |
399 | bufPtr += got; |
400 | needed -= got; | |
401 | } | |
402 | ||
403 | if (needed != sizeof(buf.gotID)) { | |
0fae82ee | 404 | SCPrint(TRUE, stdout, CFSTR(" Received notification, identifier = %d.\n"), buf.gotID); |
5958d7c0 A |
405 | } |
406 | ||
a40a14f8 A |
407 | /* report the keys that changed */ |
408 | do_notify_changes(0, NULL); | |
409 | ||
5958d7c0 | 410 | /* this utility only allows processes one notification per "n.file" request */ |
0fae82ee | 411 | (void) SCDynamicStoreNotifyCancel(store); |
5958d7c0 A |
412 | |
413 | (void) close(fd); /* close my side of the file descriptor */ | |
414 | ||
415 | return; | |
416 | } | |
417 | ||
418 | ||
dbf6a266 | 419 | __private_extern__ |
5958d7c0 A |
420 | void |
421 | do_notify_cancel(int argc, char **argv) | |
422 | { | |
1ef45fa4 A |
423 | #pragma unused(argc) |
424 | #pragma unused(argv) | |
6bb65964 | 425 | if (notifyRls != NULL) { |
d0784775 A |
426 | if (doDispatch) { |
427 | if (!SCDynamicStoreSetDispatchQueue(store, NULL)) { | |
428 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); | |
429 | return; | |
430 | } | |
6bb65964 | 431 | } else { |
d0784775 A |
432 | CFRunLoopSourceInvalidate(notifyRls); |
433 | CFRelease(notifyRls); | |
434 | } | |
0fae82ee | 435 | notifyRls = NULL; |
6bb65964 A |
436 | } else { |
437 | if (!SCDynamicStoreNotifyCancel(store)) { | |
438 | SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError())); | |
439 | return; | |
440 | } | |
0fae82ee A |
441 | } |
442 | ||
6bb65964 | 443 | if (notifyRl != NULL) { |
009ee3c6 | 444 | CFRunLoopStop(notifyRl); |
5958d7c0 A |
445 | } |
446 | ||
5958d7c0 A |
447 | return; |
448 | } |