]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2002 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 | /* | |
24 | * Modification History | |
25 | * | |
26 | * June 1, 2001 Allan Nathanson <ajn@apple.com> | |
27 | * - public API conversion | |
28 | * | |
29 | * March 24, 2000 Allan Nathanson <ajn@apple.com> | |
30 | * - initial revision | |
31 | */ | |
32 | ||
33 | #include "configd.h" | |
34 | #include "session.h" | |
35 | ||
36 | int | |
37 | __SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef *value) | |
38 | { | |
39 | SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store; | |
40 | CFDictionaryRef dict; | |
41 | ||
42 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreCopyValue:")); | |
43 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" key = %@"), key); | |
44 | ||
45 | if (!store || (storePrivate->server == MACH_PORT_NULL)) { | |
46 | return kSCStatusNoStoreSession; /* you must have an open session to play */ | |
47 | } | |
48 | ||
49 | dict = CFDictionaryGetValue(storeData, key); | |
50 | if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) { | |
51 | /* key doesn't exist (or data never defined) */ | |
52 | return kSCStatusNoKey; | |
53 | } | |
54 | ||
55 | /* Return the data associated with the key */ | |
56 | *value = CFRetain(CFDictionaryGetValue(dict, kSCDData)); | |
57 | ||
58 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" value = %@"), *value); | |
59 | ||
60 | return kSCStatusOK; | |
61 | } | |
62 | ||
63 | kern_return_t | |
64 | _configget(mach_port_t server, | |
65 | xmlData_t keyRef, /* raw XML bytes */ | |
66 | mach_msg_type_number_t keyLen, | |
67 | xmlDataOut_t *dataRef, /* raw XML bytes */ | |
68 | mach_msg_type_number_t *dataLen, | |
69 | int *newInstance, | |
70 | int *sc_status | |
71 | ) | |
72 | { | |
73 | CFStringRef key; /* key (un-serialized) */ | |
74 | serverSessionRef mySession = getSession(server); | |
75 | Boolean ok; | |
76 | CFPropertyListRef value; | |
77 | ||
78 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Get key from configuration database.")); | |
79 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server); | |
80 | ||
81 | *dataRef = NULL; | |
82 | *dataLen = 0; | |
83 | ||
84 | /* un-serialize the key */ | |
85 | if (!_SCUnserialize((CFPropertyListRef *)&key, (void *)keyRef, keyLen)) { | |
86 | *sc_status = kSCStatusFailed; | |
87 | return KERN_SUCCESS; | |
88 | } | |
89 | ||
90 | if (!isA_CFString(key)) { | |
91 | CFRelease(key); | |
92 | *sc_status = kSCStatusInvalidArgument; | |
93 | return KERN_SUCCESS; | |
94 | } | |
95 | ||
96 | *sc_status = __SCDynamicStoreCopyValue(mySession->store, key, &value); | |
97 | CFRelease(key); | |
98 | if (*sc_status != kSCStatusOK) { | |
99 | return KERN_SUCCESS; | |
100 | } | |
101 | ||
102 | /* serialize the data */ | |
103 | ok = _SCSerialize(value, NULL, (void **)dataRef, (CFIndex *)dataLen); | |
104 | CFRelease(value); | |
105 | if (!ok) { | |
106 | *sc_status = kSCStatusFailed; | |
107 | return KERN_SUCCESS; | |
108 | } | |
109 | ||
110 | /* | |
111 | * return the instance number associated with the returned data. | |
112 | */ | |
113 | *newInstance = 1; | |
114 | ||
115 | return KERN_SUCCESS; | |
116 | } | |
117 | ||
118 | /* | |
119 | * "context" argument for addSpecificKey() and addSpecificPattern() | |
120 | */ | |
121 | typedef struct { | |
122 | SCDynamicStoreRef store; | |
123 | CFMutableDictionaryRef dict; | |
124 | } addSpecific, *addSpecificRef; | |
125 | ||
126 | static void | |
127 | addSpecificKey(const void *value, void *context) | |
128 | { | |
129 | CFStringRef key = (CFStringRef)value; | |
130 | addSpecificRef myContextRef = (addSpecificRef)context; | |
131 | int sc_status; | |
132 | CFPropertyListRef data; | |
133 | ||
134 | if (!isA_CFString(key)) { | |
135 | return; | |
136 | } | |
137 | ||
138 | sc_status = __SCDynamicStoreCopyValue(myContextRef->store, key, &data); | |
139 | if (sc_status == kSCStatusOK) { | |
140 | CFDictionaryAddValue(myContextRef->dict, key, data); | |
141 | CFRelease(data); | |
142 | } | |
143 | ||
144 | return; | |
145 | } | |
146 | ||
147 | static void | |
148 | addSpecificPattern(const void *value, void *context) | |
149 | { | |
150 | CFStringRef pattern = (CFStringRef)value; | |
151 | addSpecificRef myContextRef = (addSpecificRef)context; | |
152 | int sc_status; | |
153 | CFArrayRef keys; | |
154 | ||
155 | if (!isA_CFString(pattern)) { | |
156 | return; | |
157 | } | |
158 | ||
159 | sc_status = __SCDynamicStoreCopyKeyList(myContextRef->store, pattern, TRUE, &keys); | |
160 | if (sc_status == kSCStatusOK) { | |
161 | CFArrayApplyFunction(keys, | |
162 | CFRangeMake(0, CFArrayGetCount(keys)), | |
163 | addSpecificKey, | |
164 | context); | |
165 | CFRelease(keys); | |
166 | } | |
167 | ||
168 | return; | |
169 | } | |
170 | ||
171 | kern_return_t | |
172 | _configget_m(mach_port_t server, | |
173 | xmlData_t keysRef, | |
174 | mach_msg_type_number_t keysLen, | |
175 | xmlData_t patternsRef, | |
176 | mach_msg_type_number_t patternsLen, | |
177 | xmlDataOut_t *dataRef, | |
178 | mach_msg_type_number_t *dataLen, | |
179 | int *sc_status) | |
180 | { | |
181 | CFArrayRef keys = NULL; /* keys (un-serialized) */ | |
182 | addSpecific myContext; | |
183 | serverSessionRef mySession = getSession(server); | |
184 | Boolean ok; | |
185 | CFArrayRef patterns = NULL; /* patterns (un-serialized) */ | |
186 | ||
187 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Get key from configuration database.")); | |
188 | SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server); | |
189 | ||
190 | *dataRef = NULL; | |
191 | *dataLen = 0; | |
192 | *sc_status = kSCStatusOK; | |
193 | ||
194 | if (keysRef && (keysLen > 0)) { | |
195 | /* un-serialize the keys */ | |
196 | if (!_SCUnserialize((CFPropertyListRef *)&keys, (void *)keysRef, keysLen)) { | |
197 | *sc_status = kSCStatusFailed; | |
198 | } | |
199 | ||
200 | if (!isA_CFArray(keys)) { | |
201 | *sc_status = kSCStatusInvalidArgument; | |
202 | } | |
203 | } | |
204 | ||
205 | if (patternsRef && (patternsLen > 0)) { | |
206 | /* un-serialize the patterns */ | |
207 | if (!_SCUnserialize((CFPropertyListRef *)&patterns, (void *)patternsRef, patternsLen)) { | |
208 | *sc_status = kSCStatusFailed; | |
209 | } | |
210 | ||
211 | if (!isA_CFArray(patterns)) { | |
212 | *sc_status = kSCStatusInvalidArgument; | |
213 | } | |
214 | } | |
215 | ||
216 | if (*sc_status != kSCStatusOK) { | |
217 | if (keys) CFRelease(keys); | |
218 | if (patterns) CFRelease(patterns); | |
219 | return KERN_SUCCESS; | |
220 | } | |
221 | ||
222 | myContext.store = mySession->store; | |
223 | myContext.dict = CFDictionaryCreateMutable(NULL, | |
224 | 0, | |
225 | &kCFTypeDictionaryKeyCallBacks, | |
226 | &kCFTypeDictionaryValueCallBacks); | |
227 | ||
228 | if (keys) { | |
229 | CFArrayApplyFunction(keys, | |
230 | CFRangeMake(0, CFArrayGetCount(keys)), | |
231 | addSpecificKey, | |
232 | &myContext); | |
233 | CFRelease(keys); | |
234 | } | |
235 | ||
236 | if (patterns) { | |
237 | CFArrayApplyFunction(patterns, | |
238 | CFRangeMake(0, CFArrayGetCount(patterns)), | |
239 | addSpecificPattern, | |
240 | &myContext); | |
241 | CFRelease(patterns); | |
242 | } | |
243 | ||
244 | /* serialize the dictionary of matching keys/patterns */ | |
245 | ok = _SCSerialize(myContext.dict, NULL, (void **)dataRef, (CFIndex *)dataLen); | |
246 | CFRelease(myContext.dict); | |
247 | if (!ok) { | |
248 | *sc_status = kSCStatusFailed; | |
249 | return KERN_SUCCESS; | |
250 | } | |
251 | ||
252 | return KERN_SUCCESS; | |
253 | } |