]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd.c
configd-42.tar.gz
[apple/configd.git] / configd.tproj / _configadd.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 "configd.h"
34 #include "session.h"
35
36 int
37 __SCDynamicStoreAddValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value)
38 {
39 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
40 int sc_status = kSCStatusOK;
41 CFPropertyListRef tempValue;
42
43 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreAddValue:"));
44 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" key = %@"), key);
45 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" value = %@"), value);
46
47 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
48 return kSCStatusNoStoreSession; /* you must have an open session to play */
49 }
50
51 /*
52 * 1. Ensure that we hold the lock.
53 */
54 sc_status = __SCDynamicStoreLock(store, TRUE);
55 if (sc_status != kSCStatusOK) {
56 return sc_status;
57 }
58
59 /*
60 * 2. Ensure that this is a new key.
61 */
62 sc_status = __SCDynamicStoreCopyValue(store, key, &tempValue);
63 switch (sc_status) {
64 case kSCStatusNoKey :
65 /* store key does not exist, proceed */
66 break;
67
68 case kSCStatusOK :
69 /* store key exists, sorry */
70 CFRelease(tempValue);
71 sc_status = kSCStatusKeyExists;
72 goto done;
73
74 default :
75 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" _SCDGet(): %s"), SCErrorString(sc_status));
76 goto done;
77 }
78
79 /*
80 * 3. Save the new key.
81 */
82 sc_status = __SCDynamicStoreSetValue(store, key, value);
83
84 /*
85 * 4. Release our lock.
86 */
87 done:
88 __SCDynamicStoreUnlock(store, TRUE);
89
90 return sc_status;
91 }
92
93
94 kern_return_t
95 _configadd(mach_port_t server,
96 xmlData_t keyRef, /* raw XML bytes */
97 mach_msg_type_number_t keyLen,
98 xmlData_t dataRef, /* raw XML bytes */
99 mach_msg_type_number_t dataLen,
100 int *newInstance,
101 int *sc_status
102 )
103 {
104 kern_return_t status;
105 serverSessionRef mySession = getSession(server);
106 CFDataRef xmlKey; /* key (XML serialized) */
107 CFStringRef key; /* key (un-serialized) */
108 CFDataRef xmlData; /* data (XML serialized) */
109 CFPropertyListRef data; /* data (un-serialized) */
110 CFStringRef xmlError;
111
112 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Add key to configuration database."));
113 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server);
114
115 *sc_status = kSCStatusOK;
116
117 /* un-serialize the key */
118 xmlKey = CFDataCreate(NULL, keyRef, keyLen);
119 status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
120 if (status != KERN_SUCCESS) {
121 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
122 /* non-fatal???, proceed */
123 }
124 key = CFPropertyListCreateFromXMLData(NULL,
125 xmlKey,
126 kCFPropertyListImmutable,
127 &xmlError);
128 CFRelease(xmlKey);
129 if (!key) {
130 if (xmlError) {
131 SCLog(_configd_verbose, LOG_DEBUG,
132 CFSTR("CFPropertyListCreateFromXMLData() key: %@"),
133 xmlError);
134 CFRelease(xmlError);
135 }
136 *sc_status = kSCStatusFailed;
137 } else if (!isA_CFString(key)) {
138 *sc_status = kSCStatusInvalidArgument;
139 }
140
141 /* un-serialize the data */
142 xmlData = CFDataCreate(NULL, dataRef, dataLen);
143 status = vm_deallocate(mach_task_self(), (vm_address_t)dataRef, dataLen);
144 if (status != KERN_SUCCESS) {
145 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
146 /* non-fatal???, proceed */
147 }
148 data = CFPropertyListCreateFromXMLData(NULL,
149 xmlData,
150 kCFPropertyListImmutable,
151 &xmlError);
152 CFRelease(xmlData);
153 if (!data) {
154 if (xmlError) {
155 SCLog(_configd_verbose, LOG_DEBUG,
156 CFSTR("CFPropertyListCreateFromXMLData() data: %@"),
157 xmlError);
158 CFRelease(xmlError);
159 }
160 *sc_status = kSCStatusFailed;
161 } else if (!isA_CFPropertyList(data)) {
162 *sc_status = kSCStatusInvalidArgument;
163 }
164
165 if (*sc_status != kSCStatusOK) {
166 if (key) CFRelease(key);
167 if (data) CFRelease(data);
168 return KERN_SUCCESS;
169 }
170
171 *sc_status = __SCDynamicStoreAddValue(mySession->store, key, data);
172 *newInstance = 0;
173
174 CFRelease(key);
175 CFRelease(data);
176
177 return KERN_SUCCESS;
178 }