2 * Copyright (c) 2000-2004, 2008-2010 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();
247 if (notifyRl
== NULL
) {
248 SCPrint(TRUE
, stdout
, CFSTR(" CFRunLoopGetCurrent() failed\n"));
253 if (!SCDynamicStoreSetDispatchQueue(store
, dispatch_get_current_queue())) {
254 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
258 notifyRls
= (CFRunLoopSourceRef
)kCFNull
;
260 notifyRls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
261 if (notifyRls
== NULL
) {
262 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
266 CFRunLoopAddSource(notifyRl
, notifyRls
, kCFRunLoopDefaultMode
);
269 pthread_setname_np("n.watch");
277 do_notify_watch(int argc
, char **argv
)
279 pthread_attr_t tattr
;
282 if (notifyRl
!= NULL
) {
283 SCPrint(TRUE
, stdout
, CFSTR("already active\n"));
287 pthread_attr_init(&tattr
);
288 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
289 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
290 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
291 pthread_create(&tid
, &tattr
, _watcher
, NULL
);
292 pthread_attr_destroy(&tattr
);
300 do_notify_wait(int argc
, char **argv
)
302 if (!SCDynamicStoreNotifyWait(store
)) {
303 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
312 notificationWatcher(SCDynamicStoreRef store
, void *arg
)
314 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
315 SCPrint(TRUE
, stdout
, CFSTR(" arg = %s.\n"), (char *)arg
);
321 notificationWatcherVerbose(SCDynamicStoreRef store
, void *arg
)
323 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
324 SCPrint(TRUE
, stdout
, CFSTR(" arg = %s.\n"), (char *)arg
);
325 do_notify_changes(0, NULL
); /* report the keys that changed */
333 SCDynamicStoreCallBack_v1 func
= (SCDynamicStoreCallBack_v1
)arg
;
335 notifyRl
= CFRunLoopGetCurrent();
336 if (notifyRl
== NULL
) {
337 SCPrint(TRUE
, stdout
, CFSTR(" CFRunLoopGetCurrent() failed\n"));
341 if (!SCDynamicStoreNotifyCallback(store
, notifyRl
, func
, "Changed detected by callback handler!")) {
342 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
347 pthread_setname_np("n.callback");
356 do_notify_callback(int argc
, char **argv
)
358 SCDynamicStoreCallBack_v1 func
= notificationWatcher
;
359 pthread_attr_t tattr
;
362 if (notifyRl
!= NULL
) {
363 SCPrint(TRUE
, stdout
, CFSTR("already active\n"));
367 if ((argc
== 1) && (strcmp(argv
[0], "verbose") == 0)) {
368 func
= notificationWatcherVerbose
;
371 pthread_attr_init(&tattr
);
372 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
373 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
374 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
375 pthread_create(&tid
, &tattr
, _callback
, (void *)func
);
376 pthread_attr_destroy(&tattr
);
384 do_notify_file(int argc
, char **argv
)
396 if ((sscanf(argv
[0], "%d", &reqID
) != 1)) {
397 SCPrint(TRUE
, stdout
, CFSTR("invalid identifier.\n"));
402 if (!SCDynamicStoreNotifyFileDescriptor(store
, reqID
, &fd
)) {
403 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
407 bzero(buf
.data
, sizeof(buf
.data
));
408 bufPtr
= &buf
.data
[0];
409 needed
= sizeof(buf
.gotID
);
413 got
= read(fd
, bufPtr
, needed
);
415 /* if error detected */
416 SCPrint(TRUE
, stdout
, CFSTR("read() failed: %s.\n"), strerror(errno
));
421 /* if end of file detected */
422 SCPrint(TRUE
, stdout
, CFSTR("read(): detected end of file.\n"));
426 SCPrint(TRUE
, stdout
, CFSTR("Received %d bytes.\n"), got
);
431 if (needed
!= sizeof(buf
.gotID
)) {
432 SCPrint(TRUE
, stdout
, CFSTR(" Received notification, identifier = %d.\n"), buf
.gotID
);
435 /* report the keys that changed */
436 do_notify_changes(0, NULL
);
438 /* this utility only allows processes one notification per "n.file" request */
439 (void) SCDynamicStoreNotifyCancel(store
);
441 (void) close(fd
); /* close my side of the file descriptor */
448 signalCatcher(int signum
)
452 SCPrint(TRUE
, stdout
, CFSTR("Received sig%s (#%d).\n"), sys_signame
[signum
], n
++);
459 do_notify_signal(int argc
, char **argv
)
463 struct sigaction nact
;
465 if (isdigit(*argv
[0])) {
466 if ((sscanf(argv
[0], "%d", &sig
) != 1) || (sig
<= 0) || (sig
>= NSIG
)) {
467 SCPrint(TRUE
, stdout
, CFSTR("signal must be in the range of 1 .. %d.\n"), NSIG
-1);
471 for (sig
= 1; sig
< NSIG
; sig
++) {
472 if (strcasecmp(argv
[0], sys_signame
[sig
]) == 0)
476 CFMutableStringRef str
;
478 SCPrint(TRUE
, stdout
, CFSTR("Signal must be one of the following:\n"));
480 str
= CFStringCreateMutable(NULL
, 0);
481 for (sig
= 1; sig
< NSIG
; sig
++) {
482 CFStringAppendFormat(str
, NULL
, CFSTR(" %-6s"), sys_signame
[sig
]);
483 if ((sig
% 10) == 0) {
484 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
487 if ((sig
% 10) != 0) {
488 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
490 SCPrint(TRUE
, stdout
, CFSTR("%@"), str
);
497 if ((argc
!= 2) || (sscanf(argv
[1], "%d", &pid
) != 1)) {
502 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */
504 oact
= malloc(sizeof(struct sigaction
));
507 nact
.sa_handler
= signalCatcher
;
508 sigemptyset(&nact
.sa_mask
);
509 nact
.sa_flags
= SA_RESTART
;
510 (void) sigaction(sig
, &nact
, oact
);
512 SCPrint(TRUE
, stdout
, CFSTR("signal handler started.\n"));
514 if (!SCDynamicStoreNotifySignal(store
, pid
, sig
)) {
515 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
525 do_notify_cancel(int argc
, char **argv
)
527 if (notifyRls
!= NULL
) {
529 if (!SCDynamicStoreSetDispatchQueue(store
, NULL
)) {
530 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
534 CFRunLoopSourceInvalidate(notifyRls
);
535 CFRelease(notifyRls
);
539 if (!SCDynamicStoreNotifyCancel(store
)) {
540 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
545 if (notifyRl
!= NULL
) {
546 CFRunLoopStop(notifyRl
);
550 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */