#include "configd.h"
#include "session.h"
SCDStatus
_SCDSet(SCDSessionRef session, CFStringRef key, SCDHandleRef handle)
{
SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
SCDStatus scd_status = SCD_OK;
boolean_t wasLocked;
CFDictionaryRef dict;
CFMutableDictionaryRef newDict;
CFNumberRef num;
int dictInstance;
CFStringRef sessionKey;
CFStringRef cacheSessionKey;
SCDLog(LOG_DEBUG, CFSTR("_SCDSet:"));
SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key);
SCDLog(LOG_DEBUG, CFSTR(" data = %@"), SCDHandleGetData(handle));
SCDLog(LOG_DEBUG, CFSTR(" instance = %d"), SCDHandleGetInstance(handle));
if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
return SCD_NOSESSION;
}
wasLocked = SCDOptionGet(NULL, kSCDOptionIsLocked);
if (!wasLocked) {
scd_status = _SCDLock(session);
if (scd_status != SCD_OK) {
SCDLog(LOG_DEBUG, CFSTR(" _SCDLock(): %s"), SCDError(scd_status));
return scd_status;
}
}
dict = CFDictionaryGetValue(cacheData, key);
if (dict) {
newDict = CFDictionaryCreateMutableCopy(NULL,
0,
dict);
} else {
newDict = CFDictionaryCreateMutable(NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
}
if ((num = CFDictionaryGetValue(newDict, kSCDInstance)) == NULL) {
dictInstance = 0;
_SCDHandleSetInstance(handle, dictInstance);
} else {
(void) CFNumberGetValue(num, kCFNumberIntType, &dictInstance);
}
if (SCDHandleGetInstance(handle) != dictInstance) {
CFRelease(newDict);
scd_status = SCD_STALE;
goto done;
}
CFDictionarySetValue(newDict, kSCDData, SCDHandleGetData(handle));
dictInstance++;
num = CFNumberCreate(NULL, kCFNumberIntType, &dictInstance);
CFDictionarySetValue(newDict, kSCDInstance, num);
CFRelease(num);
_SCDHandleSetInstance(handle, dictInstance);
SCDLog(LOG_DEBUG, CFSTR(" new instance = %d"), SCDHandleGetInstance(handle));
sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), sessionPrivate->server);
if (CFDictionaryGetValueIfPresent(newDict, kSCDSession, (void *)&cacheSessionKey) &&
!CFEqual(sessionKey, cacheSessionKey)) {
CFStringRef removedKey;
CFDictionaryRemoveValue(newDict, kSCDSession);
removedKey = CFStringCreateWithFormat(NULL, 0, CFSTR("%@:%@"), cacheSessionKey, key);
CFSetAddValue(removedSessionKeys, removedKey);
CFRelease(removedKey);
}
CFRelease(sessionKey);
CFDictionarySetValue(cacheData, key, newDict);
CFRelease(newDict);
if (dictInstance == 1) {
if (CFSetContainsValue(deferredRemovals, key)) {
CFSetRemoveValue(deferredRemovals, key);
} else {
CFDictionaryApplyFunction(sessionData,
(CFDictionaryApplierFunction)_addRegexWatchersBySession,
(void *)key);
}
}
CFSetAddValue(changedKeys, key);
done:
if (!wasLocked)
_SCDUnlock(session);
return scd_status;
}
kern_return_t
_configset(mach_port_t server,
xmlData_t keyRef,
mach_msg_type_number_t keyLen,
xmlData_t dataRef,
mach_msg_type_number_t dataLen,
int oldInstance,
int *newInstance,
int *scd_status
)
{
kern_return_t status;
serverSessionRef mySession = getSession(server);
CFDataRef xmlKey;
CFStringRef key;
CFDataRef xmlData;
CFPropertyListRef data;
SCDHandleRef handle;
CFStringRef xmlError;
SCDLog(LOG_DEBUG, CFSTR("Set key to configuration database."));
SCDLog(LOG_DEBUG, CFSTR(" server = %d"), server);
xmlKey = CFDataCreate(NULL, keyRef, keyLen);
status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
if (status != KERN_SUCCESS) {
SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
}
key = CFPropertyListCreateFromXMLData(NULL,
xmlKey,
kCFPropertyListImmutable,
&xmlError);
CFRelease(xmlKey);
if (xmlError) {
SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() key: %s"), xmlError);
*scd_status = SCD_FAILED;
return KERN_SUCCESS;
}
xmlData = CFDataCreate(NULL, dataRef, dataLen);
status = vm_deallocate(mach_task_self(), (vm_address_t)dataRef, dataLen);
if (status != KERN_SUCCESS) {
SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
}
data = CFPropertyListCreateFromXMLData(NULL,
xmlData,
kCFPropertyListImmutable,
&xmlError);
CFRelease(xmlData);
if (xmlError) {
SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() data: %s"), xmlError);
CFRelease(key);
*scd_status = SCD_FAILED;
return KERN_SUCCESS;
}
handle = SCDHandleInit();
SCDHandleSetData(handle, data);
_SCDHandleSetInstance(handle, oldInstance);
*scd_status = _SCDSet(mySession->session, key, handle);
if (*scd_status == SCD_OK) {
*newInstance = SCDHandleGetInstance(handle);
}
SCDHandleRelease(handle);
CFRelease(key);
CFRelease(data);
return KERN_SUCCESS;
}