2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
39 __SCDynamicStoreCopyValue(SCDynamicStoreRef store
, CFStringRef key
, CFDataRef
*value
, Boolean internal
)
41 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
44 if (_configd_verbose
) {
45 SCLog(TRUE
, LOG_DEBUG
, CFSTR("__SCDynamicStoreCopyValue:"));
46 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" key = %@"), key
);
49 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
50 return kSCStatusNoStoreSession
; /* you must have an open session to play */
54 SCTrace(TRUE
, _configd_trace
,
55 CFSTR("%s : %5d : %@\n"),
56 internal
? "*copy " : "copy ",
61 dict
= CFDictionaryGetValue(storeData
, key
);
62 if ((dict
== NULL
) || (CFDictionaryContainsKey(dict
, kSCDData
) == FALSE
)) {
63 /* key doesn't exist (or data never defined) */
64 return kSCStatusNoKey
;
67 /* Return the data associated with the key */
68 *value
= CFRetain(CFDictionaryGetValue(dict
, kSCDData
));
70 if (_configd_verbose
) {
71 CFPropertyListRef val
;
73 (void) _SCUnserialize(&val
, *value
, NULL
, NULL
);
74 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" value = %@"), val
);
83 _configget(mach_port_t server
,
84 xmlData_t keyRef
, /* raw XML bytes */
85 mach_msg_type_number_t keyLen
,
86 xmlDataOut_t
*dataRef
, /* raw XML bytes */
87 mach_msg_type_number_t
*dataLen
,
92 CFStringRef key
; /* key (un-serialized) */
93 serverSessionRef mySession
= getSession(server
);
97 if (_configd_verbose
) {
98 SCLog(TRUE
, LOG_DEBUG
, CFSTR("Get key from configuration database."));
99 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
105 /* un-serialize the key */
106 if (!_SCUnserializeString(&key
, NULL
, (void *)keyRef
, keyLen
)) {
107 *sc_status
= kSCStatusFailed
;
111 if (!isA_CFString(key
)) {
112 *sc_status
= kSCStatusInvalidArgument
;
118 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
123 *sc_status
= __SCDynamicStoreCopyValue(mySession
->store
, key
, &value
, FALSE
);
125 if (*sc_status
!= kSCStatusOK
) {
129 /* serialize the data */
130 ok
= _SCSerializeData(value
, (void **)dataRef
, (CFIndex
*)dataLen
);
133 *sc_status
= kSCStatusFailed
;
138 * return the instance number associated with the returned data.
146 * "context" argument for addSpecificKey() and addSpecificPattern()
149 SCDynamicStoreRef store
;
150 CFMutableDictionaryRef dict
;
151 } addSpecific
, *addSpecificRef
;
154 addSpecificKey(const void *value
, void *context
)
157 CFStringRef key
= (CFStringRef
)value
;
158 addSpecificRef myContextRef
= (addSpecificRef
)context
;
161 if (!isA_CFString(key
)) {
165 sc_status
= __SCDynamicStoreCopyValue(myContextRef
->store
, key
, &data
, TRUE
);
166 if (sc_status
== kSCStatusOK
) {
167 CFDictionaryAddValue(myContextRef
->dict
, key
, data
);
175 addSpecificPattern(const void *value
, void *context
)
177 CFStringRef pattern
= (CFStringRef
)value
;
178 addSpecificRef myContextRef
= (addSpecificRef
)context
;
182 if (!isA_CFString(pattern
)) {
186 sc_status
= __SCDynamicStoreCopyKeyList(myContextRef
->store
, pattern
, TRUE
, &keys
);
187 if (sc_status
== kSCStatusOK
) {
188 CFArrayApplyFunction(keys
,
189 CFRangeMake(0, CFArrayGetCount(keys
)),
200 __SCDynamicStoreCopyMultiple(SCDynamicStoreRef store
, CFArrayRef keys
, CFArrayRef patterns
, CFDictionaryRef
*values
)
202 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
203 addSpecific myContext
;
205 if (_configd_verbose
) {
206 SCLog(TRUE
, LOG_DEBUG
, CFSTR("__SCDynamicStoreCopyMultiple:"));
207 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" keys = %@"), keys
);
208 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" patterns = %@"), patterns
);
211 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
212 return kSCStatusNoStoreSession
; /* you must have an open session to play */
215 if (_configd_trace
) {
216 SCTrace(TRUE
, _configd_trace
,
217 CFSTR("copy m : %5d : %d keys, %d patterns\n"),
218 storePrivate
->server
,
219 keys
? CFArrayGetCount(keys
) : 0,
220 patterns
? CFArrayGetCount(patterns
) : 0);
223 myContext
.store
= store
;
224 myContext
.dict
= CFDictionaryCreateMutable(NULL
,
226 &kCFTypeDictionaryKeyCallBacks
,
227 &kCFTypeDictionaryValueCallBacks
);
230 CFArrayApplyFunction(keys
,
231 CFRangeMake(0, CFArrayGetCount(keys
)),
237 CFArrayApplyFunction(patterns
,
238 CFRangeMake(0, CFArrayGetCount(patterns
)),
243 /* Return the keys/values associated with the key */
244 *values
= myContext
.dict
;
246 if (_configd_verbose
) {
247 CFDictionaryRef expDict
;
249 expDict
= _SCUnserializeMultiple(*values
);
250 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" values = %@"), expDict
);
259 _configget_m(mach_port_t server
,
261 mach_msg_type_number_t keysLen
,
262 xmlData_t patternsRef
,
263 mach_msg_type_number_t patternsLen
,
264 xmlDataOut_t
*dataRef
,
265 mach_msg_type_number_t
*dataLen
,
268 CFDictionaryRef dict
= NULL
; /* keys/values (un-serialized) */
269 CFArrayRef keys
= NULL
; /* keys (un-serialized) */
270 serverSessionRef mySession
= getSession(server
);
272 CFArrayRef patterns
= NULL
; /* patterns (un-serialized) */
274 if (_configd_verbose
) {
275 SCLog(TRUE
, LOG_DEBUG
, CFSTR("Get keys from configuration database."));
276 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
281 *sc_status
= kSCStatusOK
;
283 if (keysRef
&& (keysLen
> 0)) {
284 /* un-serialize the keys */
285 if (!_SCUnserialize((CFPropertyListRef
*)&keys
, NULL
, (void *)keysRef
, keysLen
)) {
286 *sc_status
= kSCStatusFailed
;
289 if (!isA_CFArray(keys
)) {
290 *sc_status
= kSCStatusInvalidArgument
;
294 if (patternsRef
&& (patternsLen
> 0)) {
295 /* un-serialize the patterns */
296 if (!_SCUnserialize((CFPropertyListRef
*)&patterns
, NULL
, (void *)patternsRef
, patternsLen
)) {
297 *sc_status
= kSCStatusFailed
;
300 if (!isA_CFArray(patterns
)) {
301 *sc_status
= kSCStatusInvalidArgument
;
306 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
309 if (*sc_status
!= kSCStatusOK
) {
310 if (keys
) CFRelease(keys
);
311 if (patterns
) CFRelease(patterns
);
315 *sc_status
= __SCDynamicStoreCopyMultiple(mySession
->store
, keys
, patterns
, &dict
);
316 if (keys
) CFRelease(keys
);
317 if (patterns
) CFRelease(patterns
);
319 /* serialize the dictionary of matching keys/patterns */
320 ok
= _SCSerialize(dict
, NULL
, (void **)dataRef
, (CFIndex
*)dataLen
);
323 *sc_status
= kSCStatusFailed
;