#include <mach/mach.h>
#include <mach/mach_error.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <SystemConfiguration/SCPrivate.h>
#include "SCDynamicStoreInternal.h"
#include "config.h"
static void
addKey(CFMutableArrayRef *keysP, CFStringRef key)
{
if (*keysP == NULL) {
*keysP = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
}
CFArrayAppendValue(*keysP, key);
return;
}
Boolean
SCDynamicStoreAddWatchedKey(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex)
{
SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
kern_return_t status;
CFDataRef utfKey;
xmlData_t myKeyRef;
CFIndex myKeyLen;
int sc_status;
if (store == NULL) {
_SCErrorSet(kSCStatusNoStoreSession);
return FALSE;
}
if (storePrivate->server == MACH_PORT_NULL) {
_SCErrorSet(kSCStatusNoStoreServer);
return FALSE;
}
if (!_SCSerializeString(key, &utfKey, (void **)&myKeyRef, &myKeyLen)) {
_SCErrorSet(kSCStatusFailed);
return FALSE;
}
retry :
status = notifyadd(storePrivate->server,
myKeyRef,
myKeyLen,
isRegex,
(int *)&sc_status);
if (status != KERN_SUCCESS) {
if ((status == MACH_SEND_INVALID_DEST) || (status == MIG_SERVER_DIED)) {
(void) mach_port_deallocate(mach_task_self(), storePrivate->server);
} else {
SCLog(TRUE, LOG_ERR, CFSTR("SCDynamicStoreAddWatchedKey notifyadd(): %s"), mach_error_string(status));
}
storePrivate->server = MACH_PORT_NULL;
if ((status == MACH_SEND_INVALID_DEST) || (status == MIG_SERVER_DIED)) {
if (__SCDynamicStoreReconnect(store)) {
goto retry;
}
}
sc_status = status;
}
CFRelease(utfKey);
if (sc_status != kSCStatusOK) {
_SCErrorSet(sc_status);
return FALSE;
}
if (isRegex) {
addKey(&storePrivate->patterns, key);
} else {
addKey(&storePrivate->keys, key);
}
return TRUE;
}