]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/notifications.c
configd-289.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 notifyRls = SCDynamicStoreCreateRunLoopSource(NULL, store, 0);
253 if (!notifyRls) {
254 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
255 return NULL;
256 }
257 CFRunLoopAddSource(notifyRl, notifyRls, kCFRunLoopDefaultMode);
258
259 CFRunLoopRun();
260 return NULL;
261 }
262
263 __private_extern__
264 void
265 do_notify_watch(int argc, char **argv)
266 {
267 pthread_attr_t tattr;
268 pthread_t tid;
269
270 if (notifyRl) {
271 return;
272 }
273
274 pthread_attr_init(&tattr);
275 pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
276 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
277 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
278 pthread_create(&tid, &tattr, _watcher, NULL);
279 pthread_attr_destroy(&tattr);
280
281 return;
282 }
283
284
285 __private_extern__
286 void
287 do_notify_wait(int argc, char **argv)
288 {
289 if (!SCDynamicStoreNotifyWait(store)) {
290 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
291 return;
292 }
293
294 return;
295 }
296
297
298 static boolean_t
299 notificationWatcher(SCDynamicStoreRef store, void *arg)
300 {
301 SCPrint(TRUE, stdout, CFSTR("notification callback (store address = %p).\n"), store);
302 SCPrint(TRUE, stdout, CFSTR(" arg = %s.\n"), (char *)arg);
303 return TRUE;
304 }
305
306
307 static boolean_t
308 notificationWatcherVerbose(SCDynamicStoreRef store, void *arg)
309 {
310 SCPrint(TRUE, stdout, CFSTR("notification callback (store address = %p).\n"), store);
311 SCPrint(TRUE, stdout, CFSTR(" arg = %s.\n"), (char *)arg);
312 do_notify_changes(0, NULL); /* report the keys that changed */
313 return TRUE;
314 }
315
316
317 __private_extern__
318 void
319 do_notify_callback(int argc, char **argv)
320 {
321 SCDynamicStoreCallBack_v1 func = notificationWatcher;
322
323 if ((argc == 1) && (strcmp(argv[0], "verbose") == 0)) {
324 func = notificationWatcherVerbose;
325 }
326
327 if (!SCDynamicStoreNotifyCallback(store, CFRunLoopGetCurrent(), func, "Changed detected by callback handler!")) {
328 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
329 return;
330 }
331
332 return;
333 }
334
335
336 __private_extern__
337 void
338 do_notify_file(int argc, char **argv)
339 {
340 int32_t reqID = 0;
341 int fd;
342 union {
343 char data[4];
344 int32_t gotID;
345 } buf;
346 char *bufPtr;
347 int needed;
348
349 if (argc == 1) {
350 if ((sscanf(argv[0], "%d", &reqID) != 1)) {
351 SCPrint(TRUE, stdout, CFSTR("invalid identifier.\n"));
352 return;
353 }
354 }
355
356 if (!SCDynamicStoreNotifyFileDescriptor(store, reqID, &fd)) {
357 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
358 return;
359 }
360
361 bzero(buf.data, sizeof(buf.data));
362 bufPtr = &buf.data[0];
363 needed = sizeof(buf.gotID);
364 while (needed > 0) {
365 int got;
366
367 got = read(fd, bufPtr, needed);
368 if (got == -1) {
369 /* if error detected */
370 SCPrint(TRUE, stdout, CFSTR("read() failed: %s.\n"), strerror(errno));
371 break;
372 }
373
374 if (got == 0) {
375 /* if end of file detected */
376 SCPrint(TRUE, stdout, CFSTR("read(): detected end of file.\n"));
377 break;
378 }
379
380 SCPrint(TRUE, stdout, CFSTR("Received %d bytes.\n"), got);
381 bufPtr += got;
382 needed -= got;
383 }
384
385 if (needed != sizeof(buf.gotID)) {
386 SCPrint(TRUE, stdout, CFSTR(" Received notification, identifier = %d.\n"), buf.gotID);
387 }
388
389 /* report the keys that changed */
390 do_notify_changes(0, NULL);
391
392 /* this utility only allows processes one notification per "n.file" request */
393 (void) SCDynamicStoreNotifyCancel(store);
394
395 (void) close(fd); /* close my side of the file descriptor */
396
397 return;
398 }
399
400
401 static void
402 signalCatcher(int signum)
403 {
404 static int n = 0;
405
406 SCPrint(TRUE, stdout, CFSTR("Received sig%s (#%d).\n"), sys_signame[signum], n++);
407 return;
408 }
409
410
411 __private_extern__
412 void
413 do_notify_signal(int argc, char **argv)
414 {
415 int sig;
416 pid_t pid;
417 struct sigaction nact;
418
419 if (isdigit(*argv[0])) {
420 if ((sscanf(argv[0], "%d", &sig) != 1) || (sig <= 0) || (sig >= NSIG)) {
421 SCPrint(TRUE, stdout, CFSTR("signal must be in the range of 1 .. %d.\n"), NSIG-1);
422 return;
423 }
424 } else {
425 for (sig = 1; sig < NSIG; sig++) {
426 if (strcasecmp(argv[0], sys_signame[sig]) == 0)
427 break;
428 }
429 if (sig >= NSIG) {
430 CFMutableStringRef str;
431
432 SCPrint(TRUE, stdout, CFSTR("Signal must be one of the following:\n"));
433
434 str = CFStringCreateMutable(NULL, 0);
435 for (sig = 1; sig < NSIG; sig++) {
436 CFStringAppendFormat(str, NULL, CFSTR(" %-6s"), sys_signame[sig]);
437 if ((sig % 10) == 0) {
438 CFStringAppendFormat(str, NULL, CFSTR("\n"));
439 }
440 }
441 if ((sig % 10) != 0) {
442 CFStringAppendFormat(str, NULL, CFSTR("\n"));
443 }
444 SCPrint(TRUE, stdout, CFSTR("%@"), str);
445 CFRelease(str);
446 return;
447 }
448
449 }
450
451 if ((argc != 2) || (sscanf(argv[1], "%d", &pid) != 1)) {
452 pid = getpid();
453 }
454
455 if (oact != NULL) {
456 (void) sigaction(osig, oact, NULL); /* restore original signal handler */
457 } else {
458 oact = malloc(sizeof(struct sigaction));
459 }
460
461 nact.sa_handler = signalCatcher;
462 sigemptyset(&nact.sa_mask);
463 nact.sa_flags = SA_RESTART;
464 (void) sigaction(sig, &nact, oact);
465 osig = sig;
466 SCPrint(TRUE, stdout, CFSTR("signal handler started.\n"));
467
468 if (!SCDynamicStoreNotifySignal(store, pid, sig)) {
469 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
470 return;
471 }
472
473 return;
474 }
475
476
477 __private_extern__
478 void
479 do_notify_cancel(int argc, char **argv)
480 {
481 if (notifyRls) {
482 CFRunLoopSourceInvalidate(notifyRls);
483 CFRelease(notifyRls);
484 notifyRls = NULL;
485 }
486
487 if (notifyRl) {
488 CFRunLoopStop(notifyRl);
489 notifyRl = NULL;
490 }
491
492 if (!SCDynamicStoreNotifyCancel(store)) {
493 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
494 return;
495 }
496
497 if (oact != NULL) {
498 (void) sigaction(osig, oact, NULL); /* restore original signal handler */
499 free(oact);
500 oact = NULL;
501 }
502
503 return;
504 }