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