2 * Copyright (c) 2000-2004, 2008, 2009 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@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
35 #include <sys/types.h>
36 #include <sys/socket.h>
41 #include "notifications.h"
45 static struct sigaction
*oact
= NULL
;
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);
58 storeCallback(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
63 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
65 n
= CFArrayGetCount(changedKeys
);
67 for (i
= 0; i
< n
; i
++) {
70 CFSTR(" changed key [%d] = %@\n"),
72 CFArrayGetValueAtIndex(changedKeys
, i
));
75 SCPrint(TRUE
, stdout
, CFSTR(" no changed key's.\n"));
84 do_notify_list(int argc
, char **argv
)
89 Boolean isRegex
= FALSE
;
90 CFMutableArrayRef sortedList
;
93 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
100 list
= isRegex
? watchedPatterns
: watchedKeys
;
104 CFSTR(" no notifier %s.\n"),
105 isRegex
? "patterns" : "keys");
109 listCnt
= CFArrayGetCount(list
);
110 sortedList
= CFArrayCreateMutableCopy(NULL
, listCnt
, list
);
111 CFArraySortValues(sortedList
,
112 CFRangeMake(0, listCnt
),
117 for (i
= 0; i
< listCnt
; i
++) {
120 CFSTR(" notifier %s [%d] = %@\n"),
121 isRegex
? "pattern" : "key",
123 CFArrayGetValueAtIndex(sortedList
, i
));
128 CFSTR(" no notifier %s.\n"),
129 isRegex
? "patterns" : "keys");
131 CFRelease(sortedList
);
139 do_notify_add(int argc
, char **argv
)
142 CFMutableArrayRef keys
;
143 Boolean isRegex
= FALSE
;
146 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
150 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
155 keys
= isRegex
? watchedPatterns
: watchedKeys
;
156 if (CFArrayContainsValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
)) {
157 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusKeyExists
));
162 CFArrayAppendValue(keys
, key
);
165 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
166 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
175 do_notify_remove(int argc
, char **argv
)
178 CFMutableArrayRef keys
;
180 Boolean isRegex
= FALSE
;
183 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
187 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
192 keys
= isRegex
? watchedPatterns
: watchedKeys
;
193 i
= CFArrayGetFirstIndexOfValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
);
196 if (i
== kCFNotFound
) {
197 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoKey
));
201 CFArrayRemoveValueAtIndex(keys
, i
);
203 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
204 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
213 do_notify_changes(int argc
, char **argv
)
219 list
= SCDynamicStoreCopyNotifiedKeys(store
);
221 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
225 listCnt
= CFArrayGetCount(list
);
227 for (i
= 0; i
< listCnt
; i
++) {
230 CFSTR(" changedKey [%d] = %@\n"),
232 CFArrayGetValueAtIndex(list
, i
));
235 SCPrint(TRUE
, stdout
, CFSTR(" no changedKey's.\n"));
246 notifyRl
= CFRunLoopGetCurrent();
248 SCPrint(TRUE
, stdout
, CFSTR(" CFRunLoopGetCurrent() failed\n"));
252 notifyRls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
254 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
257 CFRunLoopAddSource(notifyRl
, notifyRls
, kCFRunLoopDefaultMode
);
265 do_notify_watch(int argc
, char **argv
)
267 pthread_attr_t tattr
;
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
);
287 do_notify_wait(int argc
, char **argv
)
289 if (!SCDynamicStoreNotifyWait(store
)) {
290 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
299 notificationWatcher(SCDynamicStoreRef store
, void *arg
)
301 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
302 SCPrint(TRUE
, stdout
, CFSTR(" arg = %s.\n"), (char *)arg
);
308 notificationWatcherVerbose(SCDynamicStoreRef store
, void *arg
)
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 */
319 do_notify_callback(int argc
, char **argv
)
321 SCDynamicStoreCallBack_v1 func
= notificationWatcher
;
323 if ((argc
== 1) && (strcmp(argv
[0], "verbose") == 0)) {
324 func
= notificationWatcherVerbose
;
327 if (!SCDynamicStoreNotifyCallback(store
, CFRunLoopGetCurrent(), func
, "Changed detected by callback handler!")) {
328 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
338 do_notify_file(int argc
, char **argv
)
350 if ((sscanf(argv
[0], "%d", &reqID
) != 1)) {
351 SCPrint(TRUE
, stdout
, CFSTR("invalid identifier.\n"));
356 if (!SCDynamicStoreNotifyFileDescriptor(store
, reqID
, &fd
)) {
357 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
361 bzero(buf
.data
, sizeof(buf
.data
));
362 bufPtr
= &buf
.data
[0];
363 needed
= sizeof(buf
.gotID
);
367 got
= read(fd
, bufPtr
, needed
);
369 /* if error detected */
370 SCPrint(TRUE
, stdout
, CFSTR("read() failed: %s.\n"), strerror(errno
));
375 /* if end of file detected */
376 SCPrint(TRUE
, stdout
, CFSTR("read(): detected end of file.\n"));
380 SCPrint(TRUE
, stdout
, CFSTR("Received %d bytes.\n"), got
);
385 if (needed
!= sizeof(buf
.gotID
)) {
386 SCPrint(TRUE
, stdout
, CFSTR(" Received notification, identifier = %d.\n"), buf
.gotID
);
389 /* report the keys that changed */
390 do_notify_changes(0, NULL
);
392 /* this utility only allows processes one notification per "n.file" request */
393 (void) SCDynamicStoreNotifyCancel(store
);
395 (void) close(fd
); /* close my side of the file descriptor */
402 signalCatcher(int signum
)
406 SCPrint(TRUE
, stdout
, CFSTR("Received sig%s (#%d).\n"), sys_signame
[signum
], n
++);
413 do_notify_signal(int argc
, char **argv
)
417 struct sigaction nact
;
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);
425 for (sig
= 1; sig
< NSIG
; sig
++) {
426 if (strcasecmp(argv
[0], sys_signame
[sig
]) == 0)
430 CFMutableStringRef str
;
432 SCPrint(TRUE
, stdout
, CFSTR("Signal must be one of the following:\n"));
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"));
441 if ((sig
% 10) != 0) {
442 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
444 SCPrint(TRUE
, stdout
, CFSTR("%@"), str
);
451 if ((argc
!= 2) || (sscanf(argv
[1], "%d", &pid
) != 1)) {
456 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */
458 oact
= malloc(sizeof(struct sigaction
));
461 nact
.sa_handler
= signalCatcher
;
462 sigemptyset(&nact
.sa_mask
);
463 nact
.sa_flags
= SA_RESTART
;
464 (void) sigaction(sig
, &nact
, oact
);
466 SCPrint(TRUE
, stdout
, CFSTR("signal handler started.\n"));
468 if (!SCDynamicStoreNotifySignal(store
, pid
, sig
)) {
469 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
479 do_notify_cancel(int argc
, char **argv
)
482 CFRunLoopSourceInvalidate(notifyRls
);
483 CFRelease(notifyRls
);
488 CFRunLoopStop(notifyRl
);
492 if (!SCDynamicStoreNotifyCancel(store
)) {
493 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
498 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */