]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd_s.c
configd-24.tar.gz
[apple/configd.git] / configd.tproj / _configadd_s.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include "configd.h"
24 #include "session.h"
25
26 SCDStatus
27 _SCDAddSession(SCDSessionRef session, CFStringRef key, SCDHandleRef handle)
28 {
29 SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
30 SCDStatus scd_status = SCD_OK;
31 CFStringRef sessionKey;
32 CFDictionaryRef dict;
33 CFMutableDictionaryRef newDict;
34 CFArrayRef keys;
35 CFMutableArrayRef newKeys;
36
37 SCDLog(LOG_DEBUG, CFSTR("_SCDAddSession:"));
38 SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key);
39 SCDLog(LOG_DEBUG, CFSTR(" data = %@"), SCDHandleGetData(handle));
40
41 if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
42 return SCD_NOSESSION; /* you can't do anything with a closed session */
43 }
44
45 /*
46 * 1. Add the key
47 */
48 scd_status = _SCDAdd(session, key, handle);
49 if (scd_status != SCD_OK) {
50 SCDLog(LOG_DEBUG, CFSTR(" _SCDAdd(): %s"), SCDError(scd_status));
51 return scd_status;
52 }
53
54 /*
55 * 2. Create the session key
56 */
57 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), sessionPrivate->server);
58
59 /*
60 * 3. Add this key to my list of per-session keys
61 */
62 dict = CFDictionaryGetValue(sessionData, sessionKey);
63 keys = CFDictionaryGetValue(dict, kSCDSessionKeys);
64 if ((keys == NULL) ||
65 (CFArrayGetFirstIndexOfValue(keys,
66 CFRangeMake(0, CFArrayGetCount(keys)),
67 key) == -1)) {
68 /*
69 * if no session keys defined "or" keys defined but not
70 * this one...
71 */
72 if (keys) {
73 /* this is a new session key */
74 newKeys = CFArrayCreateMutableCopy(NULL, 0, keys);
75 } else {
76 /* this is an additional session key */
77 newKeys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
78 }
79 CFArrayAppendValue(newKeys, key);
80
81 /* update session dictionary */
82 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
83 CFDictionarySetValue(newDict, kSCDSessionKeys, newKeys);
84 CFRelease(newKeys);
85 CFDictionarySetValue(sessionData, sessionKey, newDict);
86 CFRelease(newDict);
87 }
88
89 /*
90 * 4. Mark the key as a "session" key and track the creator.
91 */
92 dict = CFDictionaryGetValue(cacheData, key);
93 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
94 CFDictionarySetValue(newDict, kSCDSession, sessionKey);
95 CFDictionarySetValue(cacheData, key, newDict);
96 CFRelease(newDict);
97
98 CFRelease(sessionKey);
99 return scd_status;
100 }
101
102
103 kern_return_t
104 _configadd_s(mach_port_t server,
105 xmlData_t keyRef, /* raw XML bytes */
106 mach_msg_type_number_t keyLen,
107 xmlData_t dataRef, /* raw XML bytes */
108 mach_msg_type_number_t dataLen,
109 int *newInstance,
110 int *scd_status
111 )
112 {
113 kern_return_t status;
114 serverSessionRef mySession = getSession(server);
115 CFDataRef xmlKey; /* key (XML serialized) */
116 CFStringRef key; /* key (un-serialized) */
117 CFDataRef xmlData; /* data (XML serialized) */
118 CFPropertyListRef data; /* data (un-serialized) */
119 SCDHandleRef handle;
120 CFStringRef xmlError;
121
122 SCDLog(LOG_DEBUG, CFSTR("Add (session) key to configuration database."));
123 SCDLog(LOG_DEBUG, CFSTR(" server = %d"), server);
124
125 /* un-serialize the key */
126 xmlKey = CFDataCreate(NULL, keyRef, keyLen);
127 status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
128 if (status != KERN_SUCCESS) {
129 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
130 /* non-fatal???, proceed */
131 }
132 key = CFPropertyListCreateFromXMLData(NULL,
133 xmlKey,
134 kCFPropertyListImmutable,
135 &xmlError);
136 CFRelease(xmlKey);
137 if (xmlError) {
138 SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() key: %s"), xmlError);
139 *scd_status = SCD_FAILED;
140 return KERN_SUCCESS;
141 }
142
143 /* un-serialize the data */
144 xmlData = CFDataCreate(NULL, dataRef, dataLen);
145 status = vm_deallocate(mach_task_self(), (vm_address_t)dataRef, dataLen);
146 if (status != KERN_SUCCESS) {
147 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
148 /* non-fatal???, proceed */
149 }
150 data = CFPropertyListCreateFromXMLData(NULL,
151 xmlData,
152 kCFPropertyListImmutable,
153 &xmlError);
154 CFRelease(xmlData);
155 if (xmlError) {
156 SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() data: %s"), xmlError);
157 CFRelease(key);
158 *scd_status = SCD_FAILED;
159 return KERN_SUCCESS;
160 }
161
162 handle = SCDHandleInit();
163 SCDHandleSetData(handle, data);
164 *scd_status = _SCDAddSession(mySession->session, key, handle);
165 if (*scd_status == SCD_OK) {
166 *newInstance = SCDHandleGetInstance(handle);
167 }
168 SCDHandleRelease(handle);
169 CFRelease(key);
170 CFRelease(data);
171
172 return KERN_SUCCESS;
173 }