#include "configd.h"
#include "session.h"
#include "pattern.h"
static __inline__ void
my_CFSetApplyFunction(CFSetRef theSet,
CFSetApplierFunction applier,
void *context)
{
CFAllocatorRef myAllocator;
CFSetRef mySet;
myAllocator = CFGetAllocator(theSet);
mySet = CFSetCreateCopy(myAllocator, theSet);
CFSetApplyFunction(mySet, applier, context);
CFRelease(mySet);
return;
}
__private_extern__
int
__SCDynamicStoreAddWatchedKey(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex, Boolean internal)
{
int sc_status = kSCStatusOK;
CFNumberRef sessionNum = NULL;
SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
if (!store || (storePrivate->server == MACH_PORT_NULL)) {
return kSCStatusNoStoreSession;
}
if (_configd_trace) {
SCTrace(TRUE, _configd_trace,
CFSTR("%s : %5d : %s : %@\n"),
internal ? "*watch+" : "watch+ ",
storePrivate->server,
isRegex ? "pattern" : "key",
key);
}
sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
if (isRegex) {
if (CFSetContainsValue(storePrivate->patterns, key)) {
sc_status = kSCStatusKeyExists;
goto done;
}
if (!patternAddSession(key, sessionNum)) {
sc_status = kSCStatusInvalidArgument;
goto done;
}
CFSetAddValue(storePrivate->patterns, key);
} else {
if (CFSetContainsValue(storePrivate->keys, key)) {
sc_status = kSCStatusKeyExists;
goto done;
}
_addWatcher(sessionNum, key);
CFSetAddValue(storePrivate->keys, key);
}
done :
if (sessionNum) CFRelease(sessionNum);
return sc_status;
}
__private_extern__
kern_return_t
_notifyadd(mach_port_t server,
xmlData_t keyRef,
mach_msg_type_number_t keyLen,
int isRegex,
int *sc_status
)
{
CFStringRef key = NULL;
serverSessionRef mySession = getSession(server);
if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
*sc_status = kSCStatusFailed;
goto done;
}
if (!isA_CFString(key)) {
*sc_status = kSCStatusInvalidArgument;
goto done;
}
if (!mySession) {
*sc_status = kSCStatusNoStoreSession;
goto done;
}
*sc_status = __SCDynamicStoreAddWatchedKey(mySession->store, key, isRegex != 0, FALSE);
done :
if (key) CFRelease(key);
return KERN_SUCCESS;
}
typedef struct {
SCDynamicStoreRef store;
CFSetRef oldKeys;
CFArrayRef newKeys;
Boolean isRegex;
int sc_status;
} updateKeysContext, *updateKeysContextRef;
static void
removeOldKey(const void *value, void *context)
{
CFStringRef oldKey = (CFStringRef)value;
updateKeysContextRef myContextRef = (updateKeysContextRef)context;
if (myContextRef->sc_status != kSCStatusOK) {
return;
}
if (!myContextRef->newKeys ||
!CFArrayContainsValue(myContextRef->newKeys,
CFRangeMake(0, CFArrayGetCount(myContextRef->newKeys)),
oldKey)) {
myContextRef->sc_status = __SCDynamicStoreRemoveWatchedKey(myContextRef->store,
oldKey,
myContextRef->isRegex,
TRUE);
}
return;
}
static void
addNewKey(const void *value, void *context)
{
CFStringRef newKey = (CFStringRef)value;
updateKeysContextRef myContextRef = (updateKeysContextRef)context;
if (myContextRef->sc_status != kSCStatusOK) {
return;
}
if (!myContextRef->oldKeys ||
!CFSetContainsValue(myContextRef->oldKeys, newKey)) {
myContextRef->sc_status = __SCDynamicStoreAddWatchedKey(myContextRef->store,
newKey,
myContextRef->isRegex,
TRUE);
}
return;
}
__private_extern__
int
__SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns)
{
updateKeysContext myContext;
SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
if (!store || (storePrivate->server == MACH_PORT_NULL)) {
return kSCStatusNoStoreSession;
}
if (_configd_trace) {
SCTrace(TRUE, _configd_trace,
CFSTR("watch : %5d : %d keys, %d patterns\n"),
storePrivate->server,
keys ? CFArrayGetCount(keys) : 0,
patterns ? CFArrayGetCount(patterns) : 0);
}
myContext.store = store;
myContext.sc_status = kSCStatusOK;
myContext.oldKeys = CFSetCreateCopy(NULL, storePrivate->keys);
myContext.newKeys = keys;
myContext.isRegex = FALSE;
my_CFSetApplyFunction(storePrivate->keys, removeOldKey, &myContext);
if (keys) {
CFArrayApplyFunction(keys,
CFRangeMake(0, CFArrayGetCount(keys)),
addNewKey,
&myContext);
}
CFRelease(myContext.oldKeys);
myContext.oldKeys = CFSetCreateCopy(NULL, storePrivate->patterns);
myContext.newKeys = patterns;
myContext.isRegex = TRUE;
my_CFSetApplyFunction(storePrivate->patterns, removeOldKey, &myContext);
if (patterns) {
CFArrayApplyFunction(patterns,
CFRangeMake(0, CFArrayGetCount(patterns)),
addNewKey,
&myContext);
}
CFRelease(myContext.oldKeys);
return myContext.sc_status;
}
__private_extern__
kern_return_t
_notifyset(mach_port_t server,
xmlData_t keysRef,
mach_msg_type_number_t keysLen,
xmlData_t patternsRef,
mach_msg_type_number_t patternsLen,
int *sc_status
)
{
CFArrayRef keys = NULL;
serverSessionRef mySession = getSession(server);
CFArrayRef patterns = NULL;
*sc_status = kSCStatusOK;
if (keysRef && (keysLen > 0)) {
if (!_SCUnserialize((CFPropertyListRef *)&keys, NULL, (void *)keysRef, keysLen)) {
*sc_status = kSCStatusFailed;
} else if (!isA_CFArray(keys)) {
*sc_status = kSCStatusInvalidArgument;
}
}
if (patternsRef && (patternsLen > 0)) {
if (!_SCUnserialize((CFPropertyListRef *)&patterns, NULL, (void *)patternsRef, patternsLen)) {
*sc_status = kSCStatusFailed;
} else if (!isA_CFArray(patterns)) {
*sc_status = kSCStatusInvalidArgument;
}
}
if (!mySession) {
*sc_status = kSCStatusNoStoreSession;
}
if (*sc_status == kSCStatusOK) {
*sc_status = __SCDynamicStoreSetNotificationKeys(mySession->store, keys, patterns);
}
if (keys) CFRelease(keys);
if (patterns) CFRelease(patterns);
return KERN_SUCCESS;
}