2 * Copyright (c) 2000-2006, 2008, 2011, 2013, 2014 Apple 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
;
45 SCTrace(TRUE
, _configd_trace
,
46 CFSTR("%s : %5d : %@\n"),
47 internal
? "*copy " : "copy ",
52 dict
= CFDictionaryGetValue(storeData
, key
);
53 if ((dict
== NULL
) || (CFDictionaryContainsKey(dict
, kSCDData
) == FALSE
)) {
54 /* key doesn't exist (or data never defined) */
55 return kSCStatusNoKey
;
58 /* Return the data associated with the key */
59 *value
= CFRetain(CFDictionaryGetValue(dict
, kSCDData
));
66 _configget(mach_port_t server
,
67 xmlData_t keyRef
, /* raw XML bytes */
68 mach_msg_type_number_t keyLen
,
69 xmlDataOut_t
*dataRef
, /* raw XML bytes */
70 mach_msg_type_number_t
*dataLen
,
73 audit_token_t audit_token
)
75 CFStringRef key
= NULL
; /* key (un-serialized) */
77 serverSessionRef mySession
;
84 /* un-serialize the key */
85 if (!_SCUnserializeString(&key
, NULL
, (void *)keyRef
, keyLen
)) {
86 *sc_status
= kSCStatusFailed
;
90 if (!isA_CFString(key
)) {
91 *sc_status
= kSCStatusInvalidArgument
;
95 mySession
= getSession(server
);
96 if (mySession
== NULL
) {
97 mySession
= tempSession(server
, CFSTR("SCDynamicStoreCopyValue"), audit_token
);
98 if (mySession
== NULL
) {
99 /* you must have an open session to play */
100 *sc_status
= kSCStatusNoStoreSession
;
105 *sc_status
= __SCDynamicStoreCopyValue(mySession
->store
, key
, &value
, FALSE
);
106 if (*sc_status
!= kSCStatusOK
) {
110 /* serialize the data */
111 ok
= _SCSerializeData(value
, (void **)dataRef
, &len
);
112 *dataLen
= (mach_msg_type_number_t
)len
;
115 *sc_status
= kSCStatusFailed
;
120 * return the instance number associated with the returned data.
126 if (key
!= NULL
) CFRelease(key
);
131 * "context" argument for addSpecificKey() and addSpecificPattern()
134 SCDynamicStoreRef store
;
135 CFMutableDictionaryRef dict
;
136 } addSpecific
, *addSpecificRef
;
139 addSpecificKey(const void *value
, void *context
)
142 CFStringRef key
= (CFStringRef
)value
;
143 addSpecificRef myContextRef
= (addSpecificRef
)context
;
146 if (!isA_CFString(key
)) {
150 sc_status
= __SCDynamicStoreCopyValue(myContextRef
->store
, key
, &data
, TRUE
);
151 if (sc_status
== kSCStatusOK
) {
152 CFDictionaryAddValue(myContextRef
->dict
, key
, data
);
160 addSpecificPattern(const void *value
, void *context
)
162 CFStringRef pattern
= (CFStringRef
)value
;
163 addSpecificRef myContextRef
= (addSpecificRef
)context
;
167 if (!isA_CFString(pattern
)) {
171 sc_status
= __SCDynamicStoreCopyKeyList(myContextRef
->store
, pattern
, TRUE
, &keys
);
172 if (sc_status
== kSCStatusOK
) {
173 CFArrayApplyFunction(keys
,
174 CFRangeMake(0, CFArrayGetCount(keys
)),
185 __SCDynamicStoreCopyMultiple(SCDynamicStoreRef store
, CFArrayRef keys
, CFArrayRef patterns
, CFDictionaryRef
*values
)
187 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
188 addSpecific myContext
;
190 if (_configd_trace
) {
191 SCTrace(TRUE
, _configd_trace
,
192 CFSTR("copy m : %5d : %ld keys, %ld patterns\n"),
193 storePrivate
->server
,
194 keys
? CFArrayGetCount(keys
) : 0,
195 patterns
? CFArrayGetCount(patterns
) : 0);
198 myContext
.store
= store
;
199 myContext
.dict
= CFDictionaryCreateMutable(NULL
,
201 &kCFTypeDictionaryKeyCallBacks
,
202 &kCFTypeDictionaryValueCallBacks
);
205 CFArrayApplyFunction(keys
,
206 CFRangeMake(0, CFArrayGetCount(keys
)),
212 CFArrayApplyFunction(patterns
,
213 CFRangeMake(0, CFArrayGetCount(patterns
)),
218 /* Return the keys/values associated with the key */
219 *values
= myContext
.dict
;
226 _configget_m(mach_port_t server
,
228 mach_msg_type_number_t keysLen
,
229 xmlData_t patternsRef
,
230 mach_msg_type_number_t patternsLen
,
231 xmlDataOut_t
*dataRef
,
232 mach_msg_type_number_t
*dataLen
,
234 audit_token_t audit_token
)
236 CFDictionaryRef dict
= NULL
; /* keys/values (un-serialized) */
237 CFArrayRef keys
= NULL
; /* keys (un-serialized) */
239 serverSessionRef mySession
;
241 CFArrayRef patterns
= NULL
; /* patterns (un-serialized) */
246 *sc_status
= kSCStatusOK
;
248 if (keysRef
&& (keysLen
> 0)) {
249 /* un-serialize the keys */
250 if (!_SCUnserialize((CFPropertyListRef
*)&keys
, NULL
, (void *)keysRef
, keysLen
)) {
251 *sc_status
= kSCStatusFailed
;
255 if (patternsRef
&& (patternsLen
> 0)) {
256 /* un-serialize the patterns */
257 if (!_SCUnserialize((CFPropertyListRef
*)&patterns
, NULL
, (void *)patternsRef
, patternsLen
)) {
258 *sc_status
= kSCStatusFailed
;
262 if (*sc_status
!= kSCStatusOK
) {
266 if ((keys
!= NULL
) && !isA_CFArray(keys
)) {
267 *sc_status
= kSCStatusInvalidArgument
;
271 if ((patterns
!= NULL
) && !isA_CFArray(patterns
)) {
272 *sc_status
= kSCStatusInvalidArgument
;
276 mySession
= getSession(server
);
277 if (mySession
== NULL
) {
278 mySession
= tempSession(server
, CFSTR("SCDynamicStoreCopyMultiple"), audit_token
);
279 if (mySession
== NULL
) {
280 /* you must have an open session to play */
281 *sc_status
= kSCStatusNoStoreSession
;
286 /* fetch the requested information */
287 *sc_status
= __SCDynamicStoreCopyMultiple(mySession
->store
, keys
, patterns
, &dict
);
289 /* serialize the dictionary of matching keys/patterns */
290 ok
= _SCSerialize(dict
, NULL
, (void **)dataRef
, &len
);
291 *dataLen
= (mach_msg_type_number_t
)len
;
294 *sc_status
= kSCStatusFailed
;
299 if (keys
) CFRelease(keys
);
300 if (patterns
) CFRelease(patterns
);