]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDGet.c
configd-1061.141.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDGet.c
1 /*
2 * Copyright (c) 2000-2005, 2009-2011, 2013, 2016-2019 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 "SCDynamicStoreInternal.h"
35 #include "config.h" /* MiG generated file */
36
37
38 CFDictionaryRef
39 SCDynamicStoreCopyMultiple(SCDynamicStoreRef store,
40 CFArrayRef keys,
41 CFArrayRef patterns)
42 {
43 SCDynamicStorePrivateRef storePrivate;
44 kern_return_t status;
45 CFDataRef xmlKeys = NULL; /* keys (XML serialized) */
46 xmlData_t myKeysRef = NULL; /* keys (serialized) */
47 CFIndex myKeysLen = 0;
48 CFDataRef xmlPatterns = NULL; /* patterns (XML serialized) */
49 xmlData_t myPatternsRef = NULL; /* patterns (serialized) */
50 CFIndex myPatternsLen = 0;
51 xmlDataOut_t xmlDictRef = NULL; /* dict (serialized) */
52 mach_msg_type_number_t xmlDictLen = 0;
53 CFDictionaryRef dict = NULL; /* dict (un-serialized) */
54 CFDictionaryRef expDict = NULL; /* dict (un-serialized / expanded) */
55 int sc_status;
56
57 if (store == NULL) {
58 store = __SCDynamicStoreNullSession();
59 if (store == NULL) {
60 /* sorry, you must provide a session */
61 _SCErrorSet(kSCStatusNoStoreSession);
62 return NULL;
63 }
64 }
65
66 storePrivate = (SCDynamicStorePrivateRef)store;
67 if (storePrivate->server == MACH_PORT_NULL) {
68 _SCErrorSet(kSCStatusNoStoreServer);
69 return NULL; /* you must have an open session to play */
70 }
71
72 /* serialize the keys */
73 if (keys != NULL) {
74 if (!_SCSerialize(keys, &xmlKeys, (void **)&myKeysRef, &myKeysLen)) {
75 _SCErrorSet(kSCStatusFailed);
76 return NULL;
77 }
78 }
79
80 /* serialize the patterns */
81 if (patterns != NULL) {
82 if (!_SCSerialize(patterns, &xmlPatterns, (void **)&myPatternsRef, &myPatternsLen)) {
83 if (xmlKeys != NULL) CFRelease(xmlKeys);
84 _SCErrorSet(kSCStatusFailed);
85 return NULL;
86 }
87 }
88
89 retry :
90
91 /* send the keys and patterns, fetch the associated result from the server */
92 status = configget_m(storePrivate->server,
93 myKeysRef,
94 (mach_msg_type_number_t)myKeysLen,
95 myPatternsRef,
96 (mach_msg_type_number_t)myPatternsLen,
97 &xmlDictRef,
98 &xmlDictLen,
99 (int *)&sc_status);
100
101 if (__SCDynamicStoreCheckRetryAndHandleError(store,
102 status,
103 &sc_status,
104 "SCDynamicStoreCopyMultiple configget_m()")) {
105 goto retry;
106 }
107
108 if (sc_status != kSCStatusOK) {
109 if (xmlDictRef != NULL) {
110 (void) vm_deallocate(mach_task_self(), (vm_address_t)xmlDictRef, xmlDictLen);
111 }
112 _SCErrorSet(sc_status);
113 goto done;
114 }
115
116 /* un-serialize the dictionary */
117 if (!_SCUnserialize((CFPropertyListRef *)&dict, NULL, xmlDictRef, xmlDictLen)) {
118 _SCErrorSet(kSCStatusFailed);
119 goto done;
120 }
121
122 expDict = _SCUnserializeMultiple(dict);
123 CFRelease(dict);
124
125 done:
126
127 /* clean up */
128 if (xmlKeys != NULL) CFRelease(xmlKeys);
129 if (xmlPatterns != NULL) CFRelease(xmlPatterns);
130
131 return expDict;
132 }
133
134
135 CFPropertyListRef
136 SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key)
137 {
138 SCDynamicStorePrivateRef storePrivate;
139 kern_return_t status;
140 CFDataRef utfKey; /* key (XML serialized) */
141 xmlData_t myKeyRef; /* key (serialized) */
142 CFIndex myKeyLen;
143 xmlDataOut_t xmlDataRef = NULL; /* data (serialized) */
144 mach_msg_type_number_t xmlDataLen = 0;
145 CFPropertyListRef data; /* data (un-serialized) */
146 int newInstance;
147 int sc_status;
148
149 if (store == NULL) {
150 store = __SCDynamicStoreNullSession();
151 if (store == NULL) {
152 /* sorry, you must provide a session */
153 _SCErrorSet(kSCStatusNoStoreSession);
154 return NULL;
155 }
156 }
157
158 storePrivate = (SCDynamicStorePrivateRef)store;
159 if (storePrivate->server == MACH_PORT_NULL) {
160 _SCErrorSet(kSCStatusNoStoreServer);
161 return NULL; /* you must have an open session to play */
162 }
163
164 if (storePrivate->cache_active) {
165 if ((storePrivate->cached_set != NULL) &&
166 CFDictionaryGetValueIfPresent(storePrivate->cached_set, key, (const void **)&data)) {
167 // if we have "set" a new value
168 return (CFRetain(data));
169 }
170
171 if ((storePrivate->cached_removals != NULL) &&
172 CFArrayContainsValue(storePrivate->cached_removals,
173 CFRangeMake(0, CFArrayGetCount(storePrivate->cached_removals)),
174 key)) {
175 // if we have "removed" the key
176 _SCErrorSet(kSCStatusNoKey);
177 return NULL;
178 }
179
180 if ((storePrivate->cached_keys != NULL) &&
181 CFDictionaryGetValueIfPresent(storePrivate->cached_keys, key, (const void **)&data)) {
182 // if we have a cached value
183 return (CFRetain(data));
184 }
185 }
186
187 /* serialize the key */
188 if (!_SCSerializeString(key, &utfKey, (void **)&myKeyRef, &myKeyLen)) {
189 _SCErrorSet(kSCStatusFailed);
190 return NULL;
191 }
192
193 retry :
194
195 /* send the key & fetch the associated data from the server */
196 status = configget(storePrivate->server,
197 myKeyRef,
198 (mach_msg_type_number_t)myKeyLen,
199 &xmlDataRef,
200 &xmlDataLen,
201 &newInstance,
202 (int *)&sc_status);
203
204 if (__SCDynamicStoreCheckRetryAndHandleError(store,
205 status,
206 &sc_status,
207 "SCDynamicStoreCopyValue configget()")) {
208 goto retry;
209 }
210
211 /* clean up */
212 CFRelease(utfKey);
213
214 if (sc_status != kSCStatusOK) {
215 if (xmlDataRef != NULL) {
216 (void) vm_deallocate(mach_task_self(), (vm_address_t)xmlDataRef, xmlDataLen);
217 }
218 _SCErrorSet(sc_status);
219 return NULL;
220 }
221
222 /* un-serialize the data */
223 if (!_SCUnserialize(&data, NULL, xmlDataRef, xmlDataLen)) {
224 _SCErrorSet(kSCStatusFailed);
225 return NULL;
226 }
227
228 if (storePrivate->cache_active && (data != NULL)) {
229 if (storePrivate->cached_keys == NULL) {
230 storePrivate->cached_keys = CFDictionaryCreateMutable(NULL,
231 0,
232 &kCFTypeDictionaryKeyCallBacks,
233 &kCFTypeDictionaryValueCallBacks);
234 }
235 CFDictionarySetValue(storePrivate->cached_keys, key, data);
236 }
237
238 return data;
239 }