]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configget.c
configd-24.tar.gz
[apple/configd.git] / configd.tproj / _configget.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 #include "configd.h"
24 #include "session.h"
25
26 SCDStatus
27 _SCDGet(SCDSessionRef session, CFStringRef key, SCDHandleRef *handle)
28 {
29 SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
30 CFDictionaryRef dict;
31 CFNumberRef num;
32 int dictInstance;
33
34 SCDLog(LOG_DEBUG, CFSTR("_SCDGet:"));
35 SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key);
36
37 if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
38 return SCD_NOSESSION;
39 }
40
41 dict = CFDictionaryGetValue(cacheData, key);
42 if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) {
43 /* key doesn't exist (or data never defined) */
44 return SCD_NOKEY;
45 }
46
47 /* Create a new handle associated with the cached data */
48 *handle = SCDHandleInit();
49
50 /* Return the data associated with the key */
51 SCDHandleSetData(*handle, CFDictionaryGetValue(dict, kSCDData));
52
53 /* Return the instance number associated with the key */
54 num = CFDictionaryGetValue(dict, kSCDInstance);
55 (void) CFNumberGetValue(num, kCFNumberIntType, &dictInstance);
56 _SCDHandleSetInstance(*handle, dictInstance);
57
58 SCDLog(LOG_DEBUG, CFSTR(" data = %@"), SCDHandleGetData(*handle));
59 SCDLog(LOG_DEBUG, CFSTR(" instance = %d"), SCDHandleGetInstance(*handle));
60
61 return SCD_OK;
62 }
63
64
65 kern_return_t
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,
71 int *newInstance,
72 int *scd_status
73 )
74 {
75 kern_return_t status;
76 serverSessionRef mySession = getSession(server);
77 CFDataRef xmlKey; /* key (XML serialized) */
78 CFStringRef key; /* key (un-serialized) */
79 CFDataRef xmlData; /* data (XML serialized) */
80 SCDHandleRef handle;
81 CFStringRef xmlError;
82
83 SCDLog(LOG_DEBUG, CFSTR("Get key from configuration database."));
84 SCDLog(LOG_DEBUG, CFSTR(" server = %d"), server);
85
86 /* un-serialize the key */
87 xmlKey = CFDataCreate(NULL, keyRef, keyLen);
88 status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
89 if (status != KERN_SUCCESS) {
90 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
91 /* non-fatal???, proceed */
92 }
93 key = CFPropertyListCreateFromXMLData(NULL,
94 xmlKey,
95 kCFPropertyListImmutable,
96 &xmlError);
97 CFRelease(xmlKey);
98 if (xmlError) {
99 SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() key: %s"), xmlError);
100 *scd_status = SCD_FAILED;
101 return KERN_SUCCESS;
102 }
103
104 *scd_status = _SCDGet(mySession->session, key, &handle);
105 CFRelease(key);
106 if (*scd_status != SCD_OK) {
107 *dataRef = NULL;
108 *dataLen = 0;
109 return KERN_SUCCESS;
110 }
111
112 /*
113 * serialize the data, copy it into an allocated buffer which will be
114 * released when it is returned as part of a Mach message.
115 */
116 xmlData = CFPropertyListCreateXMLData(NULL, SCDHandleGetData(handle));
117 *dataLen = CFDataGetLength(xmlData);
118 status = vm_allocate(mach_task_self(), (void *)dataRef, *dataLen, TRUE);
119 if (status != KERN_SUCCESS) {
120 SCDLog(LOG_DEBUG, CFSTR("vm_allocate(): %s"), mach_error_string(status));
121 *scd_status = SCD_FAILED;
122 CFRelease(xmlData);
123 *dataRef = NULL;
124 *dataLen = 0;
125 return KERN_SUCCESS;
126 }
127
128 bcopy((char *)CFDataGetBytePtr(xmlData), *dataRef, *dataLen);
129 CFRelease(xmlData);
130
131 /*
132 * return the instance number associated with the returned data.
133 */
134 *newInstance = SCDHandleGetInstance(handle);
135
136 SCDHandleRelease(handle);
137
138 return KERN_SUCCESS;
139 }