]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDGet.c
configd-84.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDGet.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include <mach/mach.h>
37 #include <mach/mach_error.h>
38
39 #include <SystemConfiguration/SystemConfiguration.h>
40 #include <SystemConfiguration/SCPrivate.h>
41 #include "SCDynamicStoreInternal.h"
42 #include "config.h" /* MiG generated file */
43
44
45 CFDictionaryRef
46 SCDynamicStoreCopyMultiple(SCDynamicStoreRef store,
47 CFArrayRef keys,
48 CFArrayRef patterns)
49 {
50 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
51 kern_return_t status;
52 CFDataRef xmlKeys = NULL; /* keys (XML serialized) */
53 xmlData_t myKeysRef = NULL; /* keys (serialized) */
54 CFIndex myKeysLen = 0;
55 CFDataRef xmlPatterns = NULL; /* patterns (XML serialized) */
56 xmlData_t myPatternsRef = NULL; /* patterns (serialized) */
57 CFIndex myPatternsLen = 0;
58 xmlDataOut_t xmlDictRef; /* dict (serialized) */
59 CFIndex xmlDictLen;
60 CFDictionaryRef dict = NULL; /* dict (un-serialized) */
61 CFDictionaryRef expDict = NULL; /* dict (un-serialized / expanded) */
62 int sc_status;
63
64 if (_sc_verbose) {
65 SCLog(TRUE, LOG_DEBUG, CFSTR("SCDynamicStoreCopyMultiple:"));
66 SCLog(TRUE, LOG_DEBUG, CFSTR(" keys = %@"), keys);
67 SCLog(TRUE, LOG_DEBUG, CFSTR(" patterns = %@"), patterns);
68 }
69
70 if (!store) {
71 /* sorry, you must provide a session */
72 _SCErrorSet(kSCStatusNoStoreSession);
73 return NULL;
74 }
75
76 if (storePrivate->server == MACH_PORT_NULL) {
77 _SCErrorSet(kSCStatusNoStoreServer);
78 return NULL; /* you must have an open session to play */
79 }
80
81 /* serialize the keys */
82 if (keys) {
83 if (!_SCSerialize(keys, &xmlKeys, (void **)&myKeysRef, &myKeysLen)) {
84 _SCErrorSet(kSCStatusFailed);
85 return NULL;
86 }
87 }
88
89 /* serialize the patterns */
90 if (patterns) {
91 if (!_SCSerialize(patterns, &xmlPatterns, (void **)&myPatternsRef, &myPatternsLen)) {
92 CFRelease(xmlKeys);
93 _SCErrorSet(kSCStatusFailed);
94 return NULL;
95 }
96 }
97
98 /* send the keys and patterns, fetch the associated result from the server */
99 status = configget_m(storePrivate->server,
100 myKeysRef,
101 myKeysLen,
102 myPatternsRef,
103 myPatternsLen,
104 &xmlDictRef,
105 (int *)&xmlDictLen,
106 (int *)&sc_status);
107
108 /* clean up */
109 if (xmlKeys) CFRelease(xmlKeys);
110 if (xmlPatterns) CFRelease(xmlPatterns);
111
112 if (status != KERN_SUCCESS) {
113 if (status != MACH_SEND_INVALID_DEST)
114 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("configget_m(): %s"), mach_error_string(status));
115 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
116 storePrivate->server = MACH_PORT_NULL;
117 _SCErrorSet(status);
118 return NULL;
119 }
120
121 if (sc_status != kSCStatusOK) {
122 status = vm_deallocate(mach_task_self(), (vm_address_t)xmlDictRef, xmlDictLen);
123 if (status != KERN_SUCCESS) {
124 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
125 /* non-fatal???, proceed */
126 }
127 _SCErrorSet(sc_status);
128 return NULL;
129 }
130
131 /* un-serialize the dictionary */
132 if (!_SCUnserialize((CFPropertyListRef *)&dict, NULL, xmlDictRef, xmlDictLen)) {
133 _SCErrorSet(kSCStatusFailed);
134 return NULL;
135 }
136
137 expDict = _SCUnserializeMultiple(dict);
138 CFRelease(dict);
139
140 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" value = %@"), expDict);
141
142 return expDict;
143 }
144
145
146 CFPropertyListRef
147 SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key)
148 {
149 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
150 kern_return_t status;
151 CFDataRef utfKey; /* key (XML serialized) */
152 xmlData_t myKeyRef; /* key (serialized) */
153 CFIndex myKeyLen;
154 xmlDataOut_t xmlDataRef; /* data (serialized) */
155 CFIndex xmlDataLen;
156 CFPropertyListRef data; /* data (un-serialized) */
157 int newInstance;
158 int sc_status;
159
160 if (_sc_verbose) {
161 SCLog(TRUE, LOG_DEBUG, CFSTR("SCDynamicStoreCopyValue:"));
162 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
163 }
164
165 if (!store) {
166 /* sorry, you must provide a session */
167 _SCErrorSet(kSCStatusNoStoreSession);
168 return NULL;
169 }
170
171 if (storePrivate->server == MACH_PORT_NULL) {
172 _SCErrorSet(kSCStatusNoStoreServer);
173 return NULL; /* you must have an open session to play */
174 }
175
176 /* serialize the key */
177 if (!_SCSerializeString(key, &utfKey, (void **)&myKeyRef, &myKeyLen)) {
178 _SCErrorSet(kSCStatusFailed);
179 return NULL;
180 }
181
182 /* send the key & fetch the associated data from the server */
183 status = configget(storePrivate->server,
184 myKeyRef,
185 myKeyLen,
186 &xmlDataRef,
187 (int *)&xmlDataLen,
188 &newInstance,
189 (int *)&sc_status);
190
191 /* clean up */
192 CFRelease(utfKey);
193
194 if (status != KERN_SUCCESS) {
195 if (status != MACH_SEND_INVALID_DEST)
196 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("configget(): %s"), mach_error_string(status));
197 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
198 storePrivate->server = MACH_PORT_NULL;
199 _SCErrorSet(status);
200 return NULL;
201 }
202
203 if (sc_status != kSCStatusOK) {
204 status = vm_deallocate(mach_task_self(), (vm_address_t)xmlDataRef, xmlDataLen);
205 if (status != KERN_SUCCESS) {
206 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
207 /* non-fatal???, proceed */
208 }
209 _SCErrorSet(sc_status);
210 return NULL;
211 }
212
213 /* un-serialize the data */
214 if (!_SCUnserialize(&data, NULL, xmlDataRef, xmlDataLen)) {
215 _SCErrorSet(kSCStatusFailed);
216 return NULL;
217 }
218
219 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" value = %@"), data);
220
221 return data;
222 }