]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/notifications.c
configd-293.4.tar.gz
[apple/configd.git] / scutil.tproj / notifications.c
1 /*
2 * Copyright (c) 2000-2004, 2008, 2009 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
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
34 #include <pthread.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <unistd.h>
39
40 #include "scutil.h"
41 #include "notifications.h"
42
43
44 static int osig;
45 static struct sigaction *oact = NULL;
46
47
48 static CFComparisonResult
49 sort_keys(const void *p1, const void *p2, void *context) {
50 CFStringRef key1 = (CFStringRef)p1;
51 CFStringRef key2 = (CFStringRef)p2;
52 return CFStringCompare(key1, key2, 0);
53 }
54
55
56 __private_extern__
57 void
58 storeCallback(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info)
59 {
60 int i;
61 CFIndex n;
62
63 SCPrint(TRUE, stdout, CFSTR("notification callback (store address = %p).\n"), store);
64
65 n = CFArrayGetCount(changedKeys);
66 if (n > 0) {
67 for (i = 0; i < n; i++) {
68 SCPrint(TRUE,
69 stdout,
70 CFSTR(" changed key [%d] = %@\n"),
71 i,
72 CFArrayGetValueAtIndex(changedKeys, i));
73 }
74 } else {
75 SCPrint(TRUE, stdout, CFSTR(" no changed key's.\n"));
76 }
77
78 return;
79 }
80
81
82 __private_extern__
83 void
84 do_notify_list(int argc, char **argv)
85 {
86 int i;
87 CFArrayRef list;
88 CFIndex listCnt;
89 Boolean isRegex = FALSE;
90 CFMutableArrayRef sortedList;
91
92 if (store == NULL) {
93 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession));
94 return;
95 }
96
97 if (argc == 1)
98 isRegex = TRUE;
99
100 list = isRegex ? watchedPatterns : watchedKeys;
101 if (!list) {
102 SCPrint(TRUE,
103 stdout,
104 CFSTR(" no notifier %s.\n"),
105 isRegex ? "patterns" : "keys");
106 return;
107 }
108
109 listCnt = CFArrayGetCount(list);
110 sortedList = CFArrayCreateMutableCopy(NULL, listCnt, list);
111 CFArraySortValues(sortedList,
112 CFRangeMake(0, listCnt),
113 sort_keys,
114 NULL);
115
116 if (listCnt > 0) {
117 for (i = 0; i < listCnt; i++) {
118 SCPrint(TRUE,
119 stdout,
120 CFSTR(" notifier %s [%d] = %@\n"),
121 isRegex ? "pattern" : "key",
122 i,
123 CFArrayGetValueAtIndex(sortedList, i));
124 }
125 } else {
126 SCPrint(TRUE,
127 stdout,
128 CFSTR(" no notifier %s.\n"),
129 isRegex ? "patterns" : "keys");
130 }
131 CFRelease(sortedList);
132
133 return;
134 }
135
136
137 __private_extern__
138 void
139 do_notify_add(int argc, char **argv)
140 {
141 CFStringRef key;
142 CFMutableArrayRef keys;
143 Boolean isRegex = FALSE;
144
145 if (store == NULL) {
146 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession));
147 return;
148 }
149
150 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
151
152 if (argc == 2)
153 isRegex = TRUE;
154
155 keys = isRegex ? watchedPatterns : watchedKeys;
156 if (CFArrayContainsValue(keys, CFRangeMake(0, CFArrayGetCount(keys)), key)) {
157 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusKeyExists));
158 CFRelease(key);
159 return;
160 }
161
162 CFArrayAppendValue(keys, key);
163 CFRelease(key);
164
165 if (!SCDynamicStoreSetNotificationKeys(store, watchedKeys, watchedPatterns)) {
166 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
167 }
168
169 return;
170 }
171
172
173 __private_extern__
174 void
175 do_notify_remove(int argc, char **argv)
176 {
177 CFStringRef key;
178 CFMutableArrayRef keys;
179 CFIndex i;
180 Boolean isRegex = FALSE;
181
182 if (store == NULL) {
183 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession));
184 return;
185 }
186
187 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
188
189 if (argc == 2)
190 isRegex = TRUE;
191
192 keys = isRegex ? watchedPatterns : watchedKeys;
193 i = CFArrayGetFirstIndexOfValue(keys, CFRangeMake(0, CFArrayGetCount(keys)), key);
194 CFRelease(key);
195
196 if (i == kCFNotFound) {
197 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(kSCStatusNoKey));
198 return;
199 }
200
201 CFArrayRemoveValueAtIndex(keys, i);
202
203 if (!SCDynamicStoreSetNotificationKeys(store, watchedKeys, watchedPatterns)) {
204 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
205 }
206
207 return;
208 }
209
210
211 __private_extern__
212 void
213 do_notify_changes(int argc, char **argv)
214 {
215 CFArrayRef list;
216 CFIndex listCnt;
217 int i;
218
219 list = SCDynamicStoreCopyNotifiedKeys(store);
220 if (!list) {
221 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
222 return;
223 }
224
225 listCnt = CFArrayGetCount(list);
226 if (listCnt > 0) {
227 for (i = 0; i < listCnt; i++) {
228 SCPrint(TRUE,
229 stdout,
230 CFSTR(" changedKey [%d] = %@\n"),
231 i,
232 CFArrayGetValueAtIndex(list, i));
233 }
234 } else {
235 SCPrint(TRUE, stdout, CFSTR(" no changedKey's.\n"));
236 }
237 CFRelease(list);
238
239 return;
240 }
241
242
243 static void *
244 _watcher(void *arg)
245 {
246 notifyRl = CFRunLoopGetCurrent();
247 if (!notifyRl) {
248 SCPrint(TRUE, stdout, CFSTR(" CFRunLoopGetCurrent() failed\n"));
249 return NULL;
250 }
251
252 #if !TARGET_OS_IPHONE
253 if (doDispatch) {
254 if (!SCDynamicStoreSetDispatchQueue(store, dispatch_get_current_queue())) {
255 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
256 return NULL;
257 }
258 notifyRls = (CFRunLoopSourceRef)kCFNull;
259 } else
260 #endif // !TARGET_OS_IPHONE
261 {
262 notifyRls = SCDynamicStoreCreateRunLoopSource(NULL, store, 0);
263 if (!notifyRls) {
264 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
265 return NULL;
266 }
267 CFRunLoopAddSource(notifyRl, notifyRls, kCFRunLoopDefaultMode);
268 }
269
270 CFRunLoopRun();
271 return NULL;
272 }
273
274 __private_extern__
275 void
276 do_notify_watch(int argc, char **argv)
277 {
278 pthread_attr_t tattr;
279 pthread_t tid;
280
281 if (notifyRl) {
282 return;
283 }
284
285 pthread_attr_init(&tattr);
286 pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
287 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
288 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
289 pthread_create(&tid, &tattr, _watcher, NULL);
290 pthread_attr_destroy(&tattr);
291
292 return;
293 }
294
295
296 __private_extern__
297 void
298 do_notify_wait(int argc, char **argv)
299 {
300 if (!SCDynamicStoreNotifyWait(store)) {
301 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
302 return;
303 }
304
305 return;
306 }
307
308
309 static boolean_t
310 notificationWatcher(SCDynamicStoreRef store, void *arg)
311 {
312 SCPrint(TRUE, stdout, CFSTR("notification callback (store address = %p).\n"), store);
313 SCPrint(TRUE, stdout, CFSTR(" arg = %s.\n"), (char *)arg);
314 return TRUE;
315 }
316
317
318 static boolean_t
319 notificationWatcherVerbose(SCDynamicStoreRef store, void *arg)
320 {
321 SCPrint(TRUE, stdout, CFSTR("notification callback (store address = %p).\n"), store);
322 SCPrint(TRUE, stdout, CFSTR(" arg = %s.\n"), (char *)arg);
323 do_notify_changes(0, NULL); /* report the keys that changed */
324 return TRUE;
325 }
326
327
328 __private_extern__
329 void
330 do_notify_callback(int argc, char **argv)
331 {
332 SCDynamicStoreCallBack_v1 func = notificationWatcher;
333
334 if ((argc == 1) && (strcmp(argv[0], "verbose") == 0)) {
335 func = notificationWatcherVerbose;
336 }
337
338 if (!SCDynamicStoreNotifyCallback(store, CFRunLoopGetCurrent(), func, "Changed detected by callback handler!")) {
339 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
340 return;
341 }
342
343 return;
344 }
345
346
347 __private_extern__
348 void
349 do_notify_file(int argc, char **argv)
350 {
351 int32_t reqID = 0;
352 int fd;
353 union {
354 char data[4];
355 int32_t gotID;
356 } buf;
357 char *bufPtr;
358 int needed;
359
360 if (argc == 1) {
361 if ((sscanf(argv[0], "%d", &reqID) != 1)) {
362 SCPrint(TRUE, stdout, CFSTR("invalid identifier.\n"));
363 return;
364 }
365 }
366
367 if (!SCDynamicStoreNotifyFileDescriptor(store, reqID, &fd)) {
368 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
369 return;
370 }
371
372 bzero(buf.data, sizeof(buf.data));
373 bufPtr = &buf.data[0];
374 needed = sizeof(buf.gotID);
375 while (needed > 0) {
376 int got;
377
378 got = read(fd, bufPtr, needed);
379 if (got == -1) {
380 /* if error detected */
381 SCPrint(TRUE, stdout, CFSTR("read() failed: %s.\n"), strerror(errno));
382 break;
383 }
384
385 if (got == 0) {
386 /* if end of file detected */
387 SCPrint(TRUE, stdout, CFSTR("read(): detected end of file.\n"));
388 break;
389 }
390
391 SCPrint(TRUE, stdout, CFSTR("Received %d bytes.\n"), got);
392 bufPtr += got;
393 needed -= got;
394 }
395
396 if (needed != sizeof(buf.gotID)) {
397 SCPrint(TRUE, stdout, CFSTR(" Received notification, identifier = %d.\n"), buf.gotID);
398 }
399
400 /* report the keys that changed */
401 do_notify_changes(0, NULL);
402
403 /* this utility only allows processes one notification per "n.file" request */
404 (void) SCDynamicStoreNotifyCancel(store);
405
406 (void) close(fd); /* close my side of the file descriptor */
407
408 return;
409 }
410
411
412 static void
413 signalCatcher(int signum)
414 {
415 static int n = 0;
416
417 SCPrint(TRUE, stdout, CFSTR("Received sig%s (#%d).\n"), sys_signame[signum], n++);
418 return;
419 }
420
421
422 __private_extern__
423 void
424 do_notify_signal(int argc, char **argv)
425 {
426 int sig;
427 pid_t pid;
428 struct sigaction nact;
429
430 if (isdigit(*argv[0])) {
431 if ((sscanf(argv[0], "%d", &sig) != 1) || (sig <= 0) || (sig >= NSIG)) {
432 SCPrint(TRUE, stdout, CFSTR("signal must be in the range of 1 .. %d.\n"), NSIG-1);
433 return;
434 }
435 } else {
436 for (sig = 1; sig < NSIG; sig++) {
437 if (strcasecmp(argv[0], sys_signame[sig]) == 0)
438 break;
439 }
440 if (sig >= NSIG) {
441 CFMutableStringRef str;
442
443 SCPrint(TRUE, stdout, CFSTR("Signal must be one of the following:\n"));
444
445 str = CFStringCreateMutable(NULL, 0);
446 for (sig = 1; sig < NSIG; sig++) {
447 CFStringAppendFormat(str, NULL, CFSTR(" %-6s"), sys_signame[sig]);
448 if ((sig % 10) == 0) {
449 CFStringAppendFormat(str, NULL, CFSTR("\n"));
450 }
451 }
452 if ((sig % 10) != 0) {
453 CFStringAppendFormat(str, NULL, CFSTR("\n"));
454 }
455 SCPrint(TRUE, stdout, CFSTR("%@"), str);
456 CFRelease(str);
457 return;
458 }
459
460 }
461
462 if ((argc != 2) || (sscanf(argv[1], "%d", &pid) != 1)) {
463 pid = getpid();
464 }
465
466 if (oact != NULL) {
467 (void) sigaction(osig, oact, NULL); /* restore original signal handler */
468 } else {
469 oact = malloc(sizeof(struct sigaction));
470 }
471
472 nact.sa_handler = signalCatcher;
473 sigemptyset(&nact.sa_mask);
474 nact.sa_flags = SA_RESTART;
475 (void) sigaction(sig, &nact, oact);
476 osig = sig;
477 SCPrint(TRUE, stdout, CFSTR("signal handler started.\n"));
478
479 if (!SCDynamicStoreNotifySignal(store, pid, sig)) {
480 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
481 return;
482 }
483
484 return;
485 }
486
487
488 __private_extern__
489 void
490 do_notify_cancel(int argc, char **argv)
491 {
492 if (notifyRls) {
493 #if !TARGET_OS_IPHONE
494 if (doDispatch) {
495 if (!SCDynamicStoreSetDispatchQueue(store, NULL)) {
496 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
497 return;
498 }
499 } else
500 #endif // !TARGET_OS_IPHONE
501 {
502 CFRunLoopSourceInvalidate(notifyRls);
503 CFRelease(notifyRls);
504 }
505 notifyRls = NULL;
506 }
507
508 if (notifyRl) {
509 CFRunLoopStop(notifyRl);
510 notifyRl = NULL;
511 }
512
513 if (!SCDynamicStoreNotifyCancel(store)) {
514 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
515 return;
516 }
517
518 if (oact != NULL) {
519 (void) sigaction(osig, oact, NULL); /* restore original signal handler */
520 free(oact);
521 oact = NULL;
522 }
523
524 return;
525 }