2 * Copyright (c) 2000-2003 Apple Computer, 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>
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);
57 storeCallback(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
62 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
64 n
= CFArrayGetCount(changedKeys
);
66 for (i
= 0; i
< n
; i
++) {
69 CFSTR(" changed key [%d] = %@\n"),
71 CFArrayGetValueAtIndex(changedKeys
, i
));
74 SCPrint(TRUE
, stdout
, CFSTR(" no changed key's.\n"));
82 do_notify_list(int argc
, char **argv
)
87 Boolean isRegex
= FALSE
;
88 CFMutableArrayRef sortedList
;
91 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
98 list
= isRegex
? watchedPatterns
: watchedKeys
;
102 CFSTR(" no notifier %s.\n"),
103 isRegex
? "patterns" : "keys");
107 listCnt
= CFArrayGetCount(list
);
108 sortedList
= CFArrayCreateMutableCopy(NULL
, listCnt
, list
);
109 CFArraySortValues(sortedList
,
110 CFRangeMake(0, listCnt
),
115 for (i
= 0; i
< listCnt
; i
++) {
118 CFSTR(" notifier %s [%d] = %@\n"),
119 isRegex
? "pattern" : "key",
121 CFArrayGetValueAtIndex(sortedList
, i
));
126 CFSTR(" no notifier %s.\n"),
127 isRegex
? "patterns" : "keys");
129 CFRelease(sortedList
);
136 do_notify_add(int argc
, char **argv
)
139 CFMutableArrayRef keys
;
140 Boolean isRegex
= FALSE
;
143 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
147 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingMacRoman
);
152 keys
= isRegex
? watchedPatterns
: watchedKeys
;
153 if (CFArrayContainsValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
)) {
154 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusKeyExists
));
159 CFArrayAppendValue(keys
, key
);
162 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
163 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
171 do_notify_remove(int argc
, char **argv
)
174 CFMutableArrayRef keys
;
176 Boolean isRegex
= FALSE
;
179 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoStoreSession
));
183 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingMacRoman
);
188 keys
= isRegex
? watchedPatterns
: watchedKeys
;
189 i
= CFArrayGetFirstIndexOfValue(keys
, CFRangeMake(0, CFArrayGetCount(keys
)), key
);
192 if (i
== kCFNotFound
) {
193 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(kSCStatusNoKey
));
197 CFArrayRemoveValueAtIndex(keys
, i
);
199 if (!SCDynamicStoreSetNotificationKeys(store
, watchedKeys
, watchedPatterns
)) {
200 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
208 do_notify_changes(int argc
, char **argv
)
214 list
= SCDynamicStoreCopyNotifiedKeys(store
);
216 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
220 listCnt
= CFArrayGetCount(list
);
222 for (i
= 0; i
< listCnt
; i
++) {
225 CFSTR(" changedKey [%d] = %@\n"),
227 CFArrayGetValueAtIndex(list
, i
));
230 SCPrint(TRUE
, stdout
, CFSTR(" no changedKey's.\n"));
241 notifyRl
= CFRunLoopGetCurrent();
243 SCPrint(TRUE
, stdout
, CFSTR(" CFRunLoopGetCurrent() failed\n"));
247 notifyRls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
249 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
252 CFRunLoopAddSource(notifyRl
, notifyRls
, kCFRunLoopDefaultMode
);
259 do_notify_watch(int argc
, char **argv
)
261 pthread_attr_t tattr
;
268 pthread_attr_init(&tattr
);
269 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
270 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
271 // pthread_attr_setstacksize(&tattr, 96 * 1024); // each thread gets a 96K stack
272 pthread_create(&tid
, &tattr
, _watcher
, NULL
);
273 pthread_attr_destroy(&tattr
);
280 do_notify_wait(int argc
, char **argv
)
282 if (!SCDynamicStoreNotifyWait(store
)) {
283 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
292 notificationWatcher(SCDynamicStoreRef store
, void *arg
)
294 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
295 SCPrint(TRUE
, stdout
, CFSTR(" arg = %s.\n"), (char *)arg
);
301 notificationWatcherVerbose(SCDynamicStoreRef store
, void *arg
)
303 SCPrint(TRUE
, stdout
, CFSTR("notification callback (store address = %p).\n"), store
);
304 SCPrint(TRUE
, stdout
, CFSTR(" arg = %s.\n"), (char *)arg
);
305 do_notify_changes(0, NULL
); /* report the keys which changed */
311 do_notify_callback(int argc
, char **argv
)
313 SCDynamicStoreCallBack_v1 func
= notificationWatcher
;
315 if ((argc
== 1) && (strcmp(argv
[0], "verbose") == 0)) {
316 func
= notificationWatcherVerbose
;
319 if (!SCDynamicStoreNotifyCallback(store
, CFRunLoopGetCurrent(), func
, "Changed detected by callback handler!")) {
320 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
329 do_notify_file(int argc
, char **argv
)
341 if ((sscanf(argv
[0], "%d", &reqID
) != 1)) {
342 SCPrint(TRUE
, stdout
, CFSTR("invalid identifier.\n"));
347 if (!SCDynamicStoreNotifyFileDescriptor(store
, reqID
, &fd
)) {
348 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
352 bzero(buf
.data
, sizeof(buf
.data
));
353 bufPtr
= &buf
.data
[0];
354 needed
= sizeof(buf
.gotID
);
358 got
= read(fd
, bufPtr
, needed
);
360 /* if error detected */
361 SCPrint(TRUE
, stdout
, CFSTR("read() failed: %s.\n"), strerror(errno
));
366 /* if end of file detected */
367 SCPrint(TRUE
, stdout
, CFSTR("read(): detected end of file.\n"));
371 SCPrint(TRUE
, stdout
, CFSTR("Received %d bytes.\n"), got
);
376 if (needed
!= sizeof(buf
.gotID
)) {
377 SCPrint(TRUE
, stdout
, CFSTR(" Received notification, identifier = %d.\n"), buf
.gotID
);
380 /* this utility only allows processes one notification per "n.file" request */
381 (void) SCDynamicStoreNotifyCancel(store
);
383 (void) close(fd
); /* close my side of the file descriptor */
390 signalCatcher(int signum
)
394 SCPrint(TRUE
, stdout
, CFSTR("Received sig%s (#%d).\n"), sys_signame
[signum
], n
++);
400 do_notify_signal(int argc
, char **argv
)
404 struct sigaction nact
;
407 if (isdigit(*argv
[0])) {
408 if ((sscanf(argv
[0], "%d", &sig
) != 1) || (sig
<= 0) || (sig
>= NSIG
)) {
409 SCPrint(TRUE
, stdout
, CFSTR("signal must be in the range of 1 .. %d.\n"), NSIG
-1);
413 for (sig
= 1; sig
< NSIG
; sig
++) {
414 if (strcasecmp(argv
[0], sys_signame
[sig
]) == 0)
418 CFMutableStringRef str
;
420 SCPrint(TRUE
, stdout
, CFSTR("Signal must be one of the following:\n"));
422 str
= CFStringCreateMutable(NULL
, 0);
423 for (sig
= 1; sig
< NSIG
; sig
++) {
424 CFStringAppendFormat(str
, NULL
, CFSTR(" %-6s"), sys_signame
[sig
]);
425 if ((sig
% 10) == 0) {
426 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
429 if ((sig
% 10) != 0) {
430 CFStringAppendFormat(str
, NULL
, CFSTR("\n"));
432 SCPrint(TRUE
, stdout
, CFSTR("%@"), str
);
439 if ((argc
!= 2) || (sscanf(argv
[1], "%d", &pid
) != 1)) {
444 ret
= sigaction(osig
, oact
, NULL
); /* restore original signal handler */
446 oact
= malloc(sizeof(struct sigaction
));
449 nact
.sa_handler
= signalCatcher
;
450 sigemptyset(&nact
.sa_mask
);
451 nact
.sa_flags
= SA_RESTART
;
452 ret
= sigaction(sig
, &nact
, oact
);
454 SCPrint(TRUE
, stdout
, CFSTR("signal handler started.\n"));
456 if (!SCDynamicStoreNotifySignal(store
, pid
, sig
)) {
457 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
466 do_notify_cancel(int argc
, char **argv
)
471 CFRunLoopRemoveSource(notifyRl
, notifyRls
, kCFRunLoopDefaultMode
);
472 CFRelease(notifyRls
);
477 CFRunLoopStop(notifyRl
);
481 if (!SCDynamicStoreNotifyCancel(store
)) {
482 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
487 ret
= sigaction(osig
, oact
, NULL
); /* restore original signal handler */