]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd.c
configd-289.tar.gz
[apple/configd.git] / configd.tproj / _configadd.c
1 /*
2 * Copyright (c) 2000, 2001, 2003, 2004, 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include "configd.h"
35 #include "session.h"
36
37 __private_extern__
38 int
39 __SCDynamicStoreAddValue(SCDynamicStoreRef store, CFStringRef key, CFDataRef value, Boolean internal)
40 {
41 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
42 int sc_status = kSCStatusOK;
43 CFDataRef tempValue;
44
45 if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
46 return kSCStatusNoStoreSession; /* you must have an open session to play */
47 }
48
49 if (_configd_trace) {
50 SCTrace(TRUE, _configd_trace,
51 CFSTR("%s%s : %5d : %@\n"),
52 internal ? "*add " : "add ",
53 storePrivate->useSessionKeys ? "t " : " ",
54 storePrivate->server,
55 key);
56 }
57
58 /*
59 * 1. Ensure that we hold the lock.
60 */
61 sc_status = __SCDynamicStoreLock(store, TRUE);
62 if (sc_status != kSCStatusOK) {
63 return sc_status;
64 }
65
66 /*
67 * 2. Ensure that this is a new key.
68 */
69 sc_status = __SCDynamicStoreCopyValue(store, key, &tempValue, TRUE);
70 switch (sc_status) {
71 case kSCStatusNoKey :
72 /* store key does not exist, proceed */
73 break;
74
75 case kSCStatusOK :
76 /* store key exists, sorry */
77 CFRelease(tempValue);
78 sc_status = kSCStatusKeyExists;
79 goto done;
80
81 default :
82 #ifdef DEBUG
83 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreAddValue __SCDynamicStoreCopyValue(): %s"), SCErrorString(sc_status));
84 #endif /* DEBUG */
85 goto done;
86 }
87
88 /*
89 * 3. Save the new key.
90 */
91 sc_status = __SCDynamicStoreSetValue(store, key, value, TRUE);
92
93 /*
94 * 4. Release our lock.
95 */
96
97 done:
98
99 __SCDynamicStoreUnlock(store, TRUE);
100
101 return sc_status;
102 }
103
104
105 __private_extern__
106 kern_return_t
107 _configadd(mach_port_t server,
108 xmlData_t keyRef, /* raw XML bytes */
109 mach_msg_type_number_t keyLen,
110 xmlData_t dataRef, /* raw XML bytes */
111 mach_msg_type_number_t dataLen,
112 int *newInstance,
113 int *sc_status
114 )
115 {
116 CFStringRef key = NULL; /* key (un-serialized) */
117 CFDataRef data = NULL; /* data (un-serialized) */
118 serverSessionRef mySession;
119
120 *sc_status = kSCStatusOK;
121
122 /* un-serialize the key */
123 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
124 *sc_status = kSCStatusFailed;
125 goto done;
126 }
127
128 /* un-serialize the data */
129 if (!_SCUnserializeData(&data, (void *)dataRef, dataLen)) {
130 *sc_status = kSCStatusFailed;
131 }
132
133 if (*sc_status != kSCStatusOK) {
134 goto done;
135 }
136
137 if (!isA_CFString(key)) {
138 *sc_status = kSCStatusInvalidArgument;
139 goto done;
140 }
141
142 mySession = getSession(server);
143 if (mySession == NULL) {
144 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
145 goto done;
146 }
147
148 *sc_status = __SCDynamicStoreAddValue(mySession->store, key, data, FALSE);
149 if (*sc_status == kSCStatusOK) {
150 *newInstance = 0;
151 }
152
153 done :
154
155 if (key != NULL) CFRelease(key);
156 if (data != NULL) CFRelease(data);
157
158 return KERN_SUCCESS;
159 }
160
161
162 __private_extern__
163 kern_return_t
164 _configadd_s(mach_port_t server,
165 xmlData_t keyRef, /* raw XML bytes */
166 mach_msg_type_number_t keyLen,
167 xmlData_t dataRef, /* raw XML bytes */
168 mach_msg_type_number_t dataLen,
169 int *newInstance,
170 int *sc_status
171 )
172 {
173 CFDataRef data = NULL; /* data (un-serialized) */
174 CFStringRef key = NULL; /* key (un-serialized) */
175 serverSessionRef mySession;
176 SCDynamicStorePrivateRef storePrivate;
177 Boolean useSessionKeys;
178
179 *sc_status = kSCStatusOK;
180
181 /* un-serialize the key */
182 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
183 *sc_status = kSCStatusFailed;
184 }
185
186 /* un-serialize the data */
187 if (!_SCUnserializeData(&data, (void *)dataRef, dataLen)) {
188 *sc_status = kSCStatusFailed;
189 }
190
191 if (*sc_status != kSCStatusOK) {
192 goto done;
193 }
194
195 if (!isA_CFString(key)) {
196 *sc_status = kSCStatusInvalidArgument;
197 goto done;
198 }
199
200 mySession = getSession(server);
201 if (mySession == NULL) {
202 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
203 goto done;
204 }
205
206 // force "useSessionKeys"
207 storePrivate = (SCDynamicStorePrivateRef)mySession->store;
208 useSessionKeys = storePrivate->useSessionKeys;
209 storePrivate->useSessionKeys = TRUE;
210
211 *sc_status = __SCDynamicStoreAddValue(mySession->store, key, data, FALSE);
212 if (*sc_status == kSCStatusOK) {
213 *newInstance = 0;
214 }
215
216 // restore "useSessionKeys"
217 storePrivate->useSessionKeys = useSessionKeys;
218
219 done :
220
221 if (key != NULL) CFRelease(key);
222 if (data != NULL) CFRelease(data);
223
224 return KERN_SUCCESS;
225 }