2 * Copyright (c) 2000-2005, 2008-2015, 2017, 2018 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
)
85 #pragma unused(context)
86 CFStringRef key1
= (CFStringRef
)p1
;
87 CFStringRef key2
= (CFStringRef
)p2
;
88 return CFStringCompare(key1
, key2
, 0);
94 storeCallback(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
100 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
102 n
= CFArrayGetCount(changedKeys
);
104 for (i
= 0; i
< n
; i
++) {
107 CFSTR(" %s changedKey [%d] = %@\n"),
110 CFArrayGetValueAtIndex(changedKeys
, i
));
113 SCPrint(TRUE
, stdout
, CFSTR(" no changed key's.\n"));
122 do_notify_list(int argc
, char **argv
)
128 Boolean isRegex
= FALSE
;
129 CFMutableArrayRef sortedList
;
132 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
139 list
= isRegex
? watchedPatterns
: watchedKeys
;
143 CFSTR(" no notifier %s.\n"),
144 isRegex
? "patterns" : "keys");
148 listCnt
= CFArrayGetCount(list
);
149 sortedList
= CFArrayCreateMutableCopy(NULL
, listCnt
, list
);
150 CFArraySortValues(sortedList
,
151 CFRangeMake(0, listCnt
),
156 for (i
= 0; i
< listCnt
; i
++) {
159 CFSTR(" notifier %s [%d] = %@\n"),
160 isRegex
? "pattern" : "key",
162 CFArrayGetValueAtIndex(sortedList
, i
));
167 CFSTR(" no notifier %s.\n"),
168 isRegex
? "patterns" : "keys");
170 CFRelease(sortedList
);
178 do_notify_add(int argc
, char **argv
)
181 CFMutableArrayRef keys
;
182 Boolean isRegex
= FALSE
;
185 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
189 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
194 keys
= isRegex
? watchedPatterns
: watchedKeys
;
195 if (CFArrayContainsValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
)) {
196 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusKeyExists
));
201 CFArrayAppendValue(keys
, key
);
204 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
205 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
214 do_notify_remove(int argc
, char **argv
)
217 CFMutableArrayRef keys
;
219 Boolean isRegex
= FALSE
;
222 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
226 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
231 keys
= isRegex
? watchedPatterns
: watchedKeys
;
232 i
= CFArrayGetFirstIndexOfValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
);
235 if (i
== kCFNotFound
) {
236 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoKey
));
240 CFArrayRemoveValueAtIndex(keys
, i
);
242 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
243 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
252 do_notify_changes(int argc
, char **argv
)
260 list
= SCDynamicStoreCopyNotifiedKeys(store
);
262 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
266 listCnt
= CFArrayGetCount(list
);
268 for (i
= 0; i
< listCnt
; i
++) {
271 CFSTR(" %s changedKey [%d] = %@\n"),
274 CFArrayGetValueAtIndex(list
, i
));
277 SCPrint(TRUE
, stdout
, CFSTR(" no changedKey's.\n"));
289 notifyRl
= CFRunLoopGetCurrent();
290 if (notifyRl
== NULL
) {
291 SCPrint(TRUE
, stdout
, CFSTR(" CFRunLoopGetCurrent() failed\n"));
296 if (!SCDynamicStoreSetDispatchQueue(store
, dispatch_get_main_queue())) {
297 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
301 notifyRls
= (CFRunLoopSourceRef
)kCFNull
;
303 notifyRls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
304 if (notifyRls
== NULL
) {
305 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
309 CFRunLoopAddSource(notifyRl
, notifyRls
, kCFRunLoopDefaultMode
);
312 pthread_setname_np("n.watch");
320 do_notify_watch(int argc
, char **argv
)
324 pthread_attr_t tattr
;
327 if (notifyRl
!= NULL
) {
328 SCPrint(TRUE
, stdout
, CFSTR("already active\n"));
332 pthread_attr_init(&tattr
);
333 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
334 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
335 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
336 pthread_create(&tid
, &tattr
, _watcher
, NULL
);
337 pthread_attr_destroy(&tattr
);
345 do_notify_wait(int argc
, char **argv
)
349 if (!SCDynamicStoreNotifyWait(store
)) {
350 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
360 do_notify_file(int argc
, char **argv
)
372 if ((sscanf(argv
[0], "%d", &reqID
) != 1)) {
373 SCPrint(TRUE
, stdout
, CFSTR("invalid identifier.\n"));
378 if (!SCDynamicStoreNotifyFileDescriptor(store
, reqID
, &fd
)) {
379 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
383 bzero(buf
.data
, sizeof(buf
.data
));
384 bufPtr
= &buf
.data
[0];
385 needed
= sizeof(buf
.gotID
);
389 got
= read(fd
, bufPtr
, needed
);
391 /* if error detected */
392 SCPrint(TRUE
, stdout
, CFSTR("read() failed: %s.\n"), strerror(errno
));
397 /* if end of file detected */
398 SCPrint(TRUE
, stdout
, CFSTR("read(): detected end of file.\n"));
402 SCPrint(TRUE
, stdout
, CFSTR("Received %ld bytes.\n"), got
);
407 if (needed
!= sizeof(buf
.gotID
)) {
408 SCPrint(TRUE
, stdout
, CFSTR(" Received notification, identifier = %d.\n"), buf
.gotID
);
411 /* report the keys that changed */
412 do_notify_changes(0, NULL
);
414 /* this utility only allows processes one notification per "n.file" request */
415 (void) SCDynamicStoreNotifyCancel(store
);
417 (void) close(fd
); /* close my side of the file descriptor */
424 signalCatcher(int signum
)
428 SCPrint(TRUE
, stdout
, CFSTR("Received sig%s (#%d).\n"), sys_signame
[signum
], n
++);
435 do_notify_signal(int argc
, char **argv
)
439 struct sigaction nact
;
441 if (isdigit(*argv
[0])) {
442 if ((sscanf(argv
[0], "%d", &sig
) != 1) || (sig
<= 0) || (sig
>= NSIG
)) {
443 SCPrint(TRUE
, stdout
, CFSTR("signal must be in the range of 1 .. %d.\n"), NSIG
-1);
447 for (sig
= 1; sig
< NSIG
; sig
++) {
448 if (strcasecmp(argv
[0], sys_signame
[sig
]) == 0)
452 CFMutableStringRef str
;
454 SCPrint(TRUE
, stdout
, CFSTR("Signal must be one of the following:\n"));
456 str
= CFStringCreateMutable(NULL
, 0);
457 for (sig
= 1; sig
< NSIG
; sig
++) {
458 CFStringAppendFormat(str
, NULL
, CFSTR(" %-6s"), sys_signame
[sig
]);
459 if ((sig
% 10) == 0) {
460 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
463 if ((sig
% 10) != 0) {
464 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
466 SCPrint(TRUE
, stdout
, CFSTR("%@"), str
);
473 if ((argc
!= 2) || (sscanf(argv
[1], "%d", &pid
) != 1)) {
478 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */
480 oact
= malloc(sizeof(struct sigaction
));
483 nact
.sa_handler
= signalCatcher
;
484 sigemptyset(&nact
.sa_mask
);
485 nact
.sa_flags
= SA_RESTART
;
486 (void) sigaction(sig
, &nact
, oact
);
488 SCPrint(TRUE
, stdout
, CFSTR("signal handler started.\n"));
490 if (!SCDynamicStoreNotifySignal(store
, pid
, sig
)) {
491 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
501 do_notify_cancel(int argc
, char **argv
)
505 if (notifyRls
!= NULL
) {
507 if (!SCDynamicStoreSetDispatchQueue(store
, NULL
)) {
508 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
512 CFRunLoopSourceInvalidate(notifyRls
);
513 CFRelease(notifyRls
);
517 if (!SCDynamicStoreNotifyCancel(store
)) {
518 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
523 if (notifyRl
!= NULL
) {
524 CFRunLoopStop(notifyRl
);
528 (void) sigaction(osig
, oact
, NULL
); /* restore original signal handler */