2 * Copyright (c) 2000-2004, 2008-2011 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>
42 #include "notifications.h"
46 static struct sigaction
*oact
= NULL
;
56 struct timeval tv_diff
;
57 struct timeval tv_now
;
58 static struct timeval tv_then
= { 0, 0 };
60 (void)gettimeofday(&tv_now
, NULL
);
62 (void)localtime_r(&tv_now
.tv_sec
, &tm_now
);
64 timersub(&tv_now
, &tv_then
, &tv_diff
);
65 (void)localtime_r(&tv_diff
.tv_sec
, &tm_diff
);
66 n
= snprintf(str
, sizeof(str
), "%2d:%02d:%02d.%03d",
70 tv_now
.tv_usec
/ 1000);
71 if (((tv_then
.tv_sec
!= 0) || (tv_then
.tv_usec
!= 0)) && (n
< sizeof(str
))) {
72 snprintf(&str
[n
], sizeof(str
) - n
, " (+%ld.%03d)",
74 tv_diff
.tv_usec
/ 1000);
82 static CFComparisonResult
83 sort_keys(const void *p1
, const void *p2
, void *context
) {
84 CFStringRef key1
= (CFStringRef
)p1
;
85 CFStringRef key2
= (CFStringRef
)p2
;
86 return CFStringCompare(key1
, key2
, 0);
92 storeCallback(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
97 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
99 n
= CFArrayGetCount(changedKeys
);
101 for (i
= 0; i
< n
; i
++) {
104 CFSTR(" %s changedKey [%d] = %@\n"),
107 CFArrayGetValueAtIndex(changedKeys
, i
));
110 SCPrint(TRUE
, stdout
, CFSTR(" no changed key's.\n"));
119 do_notify_list(int argc
, char **argv
)
124 Boolean isRegex
= FALSE
;
125 CFMutableArrayRef sortedList
;
128 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
135 list
= isRegex
? watchedPatterns
: watchedKeys
;
139 CFSTR(" no notifier %s.\n"),
140 isRegex
? "patterns" : "keys");
144 listCnt
= CFArrayGetCount(list
);
145 sortedList
= CFArrayCreateMutableCopy(NULL
, listCnt
, list
);
146 CFArraySortValues(sortedList
,
147 CFRangeMake(0, listCnt
),
152 for (i
= 0; i
< listCnt
; i
++) {
155 CFSTR(" notifier %s [%d] = %@\n"),
156 isRegex
? "pattern" : "key",
158 CFArrayGetValueAtIndex(sortedList
, i
));
163 CFSTR(" no notifier %s.\n"),
164 isRegex
? "patterns" : "keys");
166 CFRelease(sortedList
);
174 do_notify_add(int argc
, char **argv
)
177 CFMutableArrayRef keys
;
178 Boolean isRegex
= FALSE
;
181 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
185 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
190 keys
= isRegex
? watchedPatterns
: watchedKeys
;
191 if (CFArrayContainsValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
)) {
192 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusKeyExists
));
197 CFArrayAppendValue(keys
, key
);
200 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
201 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
210 do_notify_remove(int argc
, char **argv
)
213 CFMutableArrayRef keys
;
215 Boolean isRegex
= FALSE
;
218 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
222 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
227 keys
= isRegex
? watchedPatterns
: watchedKeys
;
228 i
= CFArrayGetFirstIndexOfValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
);
231 if (i
== kCFNotFound
) {
232 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoKey
));
236 CFArrayRemoveValueAtIndex(keys
, i
);
238 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
239 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
248 do_notify_changes(int argc
, char **argv
)
254 list
= SCDynamicStoreCopyNotifiedKeys(store
);
256 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
260 listCnt
= CFArrayGetCount(list
);
262 for (i
= 0; i
< listCnt
; i
++) {
265 CFSTR(" %s changedKey [%d] = %@\n"),
268 CFArrayGetValueAtIndex(list
, i
));
271 SCPrint(TRUE
, stdout
, CFSTR(" no changedKey's.\n"));
282 notifyRl
= CFRunLoopGetCurrent();
283 if (notifyRl
== NULL
) {
284 SCPrint(TRUE
, stdout
, CFSTR(" CFRunLoopGetCurrent() failed\n"));
289 if (!SCDynamicStoreSetDispatchQueue(store
, dispatch_get_current_queue())) {
290 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
294 notifyRls
= (CFRunLoopSourceRef
)kCFNull
;
296 notifyRls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
297 if (notifyRls
== NULL
) {
298 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
302 CFRunLoopAddSource(notifyRl
, notifyRls
, kCFRunLoopDefaultMode
);
305 pthread_setname_np("n.watch");
313 do_notify_watch(int argc
, char **argv
)
315 pthread_attr_t tattr
;
318 if (notifyRl
!= NULL
) {
319 SCPrint(TRUE
, stdout
, CFSTR("already active\n"));
323 pthread_attr_init(&tattr
);
324 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
325 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
326 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
327 pthread_create(&tid
, &tattr
, _watcher
, NULL
);
328 pthread_attr_destroy(&tattr
);
336 do_notify_wait(int argc
, char **argv
)
338 if (!SCDynamicStoreNotifyWait(store
)) {
339 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
349 do_notify_file(int argc
, char **argv
)
361 if ((sscanf(argv
[0], "%d", &reqID
) != 1)) {
362 SCPrint(TRUE
, stdout
, CFSTR("invalid identifier.\n"));
367 if (!SCDynamicStoreNotifyFileDescriptor(store
, reqID
, &fd
)) {
368 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
372 bzero(buf
.data
, sizeof(buf
.data
));
373 bufPtr
= &buf
.data
[0];
374 needed
= sizeof(buf
.gotID
);
378 got
= read(fd
, bufPtr
, needed
);
380 /* if error detected */
381 SCPrint(TRUE
, stdout
, CFSTR("read() failed: %s.\n"), strerror(errno
));
386 /* if end of file detected */
387 SCPrint(TRUE
, stdout
, CFSTR("read(): detected end of file.\n"));
391 SCPrint(TRUE
, stdout
, CFSTR("Received %d bytes.\n"), got
);
396 if (needed
!= sizeof(buf
.gotID
)) {
397 SCPrint(TRUE
, stdout
, CFSTR(" Received notification, identifier = %d.\n"), buf
.gotID
);
400 /* report the keys that changed */
401 do_notify_changes(0, NULL
);
403 /* this utility only allows processes one notification per "n.file" request */
404 (void) SCDynamicStoreNotifyCancel(store
);
406 (void) close(fd
); /* close my side of the file descriptor */
413 signalCatcher(int signum
)
417 SCPrint(TRUE
, stdout
, CFSTR("Received sig%s (#%d).\n"), sys_signame
[signum
], n
++);
424 do_notify_signal(int argc
, char **argv
)
428 struct sigaction nact
;
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);
436 for (sig
= 1; sig
< NSIG
; sig
++) {
437 if (strcasecmp(argv
[0], sys_signame
[sig
]) == 0)
441 CFMutableStringRef str
;
443 SCPrint(TRUE
, stdout
, CFSTR("Signal must be one of the following:\n"));
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"));
452 if ((sig
% 10) != 0) {
453 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
455 SCPrint(TRUE
, stdout
, CFSTR("%@"), str
);
462 if ((argc
!= 2) || (sscanf(argv
[1], "%d", &pid
) != 1)) {
467 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */
469 oact
= malloc(sizeof(struct sigaction
));
472 nact
.sa_handler
= signalCatcher
;
473 sigemptyset(&nact
.sa_mask
);
474 nact
.sa_flags
= SA_RESTART
;
475 (void) sigaction(sig
, &nact
, oact
);
477 SCPrint(TRUE
, stdout
, CFSTR("signal handler started.\n"));
479 if (!SCDynamicStoreNotifySignal(store
, pid
, sig
)) {
480 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
490 do_notify_cancel(int argc
, char **argv
)
492 if (notifyRls
!= NULL
) {
494 if (!SCDynamicStoreSetDispatchQueue(store
, NULL
)) {
495 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
499 CFRunLoopSourceInvalidate(notifyRls
);
500 CFRelease(notifyRls
);
504 if (!SCDynamicStoreNotifyCancel(store
)) {
505 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
510 if (notifyRl
!= NULL
) {
511 CFRunLoopStop(notifyRl
);
515 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */