]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDSet.c
configd-84.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDSet.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 Boolean
46 SCDynamicStoreSetMultiple(SCDynamicStoreRef store,
47 CFDictionaryRef keysToSet,
48 CFArrayRef keysToRemove,
49 CFArrayRef keysToNotify)
50 {
51 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
52 kern_return_t status;
53 CFDataRef xmlSet = NULL; /* key/value pairs to set (XML serialized) */
54 xmlData_t mySetRef = NULL; /* key/value pairs to set (serialized) */
55 CFIndex mySetLen = 0;
56 CFDataRef xmlRemove = NULL; /* keys to remove (XML serialized) */
57 xmlData_t myRemoveRef = NULL; /* keys to remove (serialized) */
58 CFIndex myRemoveLen = 0;
59 CFDataRef xmlNotify = NULL; /* keys to notify (XML serialized) */
60 xmlData_t myNotifyRef = NULL; /* keys to notify (serialized) */
61 CFIndex myNotifyLen = 0;
62 int sc_status;
63
64 if (_sc_verbose) {
65 SCLog(TRUE, LOG_DEBUG, CFSTR("SCDynamicStoreSetMultiple:"));
66 SCLog(TRUE, LOG_DEBUG, CFSTR(" keysToSet = %@"), keysToSet);
67 SCLog(TRUE, LOG_DEBUG, CFSTR(" keysToRemove = %@"), keysToRemove);
68 SCLog(TRUE, LOG_DEBUG, CFSTR(" keysToNotify = %@"), keysToNotify);
69 }
70
71 if (!store) {
72 /* sorry, you must provide a session */
73 _SCErrorSet(kSCStatusNoStoreSession);
74 return NULL;
75 }
76
77 if (storePrivate->server == MACH_PORT_NULL) {
78 _SCErrorSet(kSCStatusNoStoreServer);
79 return NULL; /* you must have an open session to play */
80 }
81
82 /* serialize the key/value pairs to set*/
83 if (keysToSet) {
84 CFDictionaryRef newInfo;
85 Boolean ok;
86
87 newInfo = _SCSerializeMultiple(keysToSet);
88 if (!newInfo) {
89 _SCErrorSet(kSCStatusFailed);
90 return NULL;
91 }
92
93 ok = _SCSerialize(newInfo, &xmlSet, (void **)&mySetRef, &mySetLen);
94 CFRelease(newInfo);
95
96 if (!ok) {
97 _SCErrorSet(kSCStatusFailed);
98 return NULL;
99 }
100 }
101
102 /* serialize the keys to remove */
103 if (keysToRemove) {
104 if (!_SCSerialize(keysToRemove, &xmlRemove, (void **)&myRemoveRef, &myRemoveLen)) {
105 if (xmlSet) CFRelease(xmlSet);
106 _SCErrorSet(kSCStatusFailed);
107 return NULL;
108 }
109 }
110
111 /* serialize the keys to notify */
112 if (keysToNotify) {
113 if (!_SCSerialize(keysToNotify, &xmlNotify, (void **)&myNotifyRef, &myNotifyLen)) {
114 if (xmlSet) CFRelease(xmlSet);
115 if (xmlRemove) CFRelease(xmlRemove);
116 _SCErrorSet(kSCStatusFailed);
117 return NULL;
118 }
119 }
120
121 /* send the keys and patterns, fetch the associated result from the server */
122 status = configset_m(storePrivate->server,
123 mySetRef,
124 mySetLen,
125 myRemoveRef,
126 myRemoveLen,
127 myNotifyRef,
128 myNotifyLen,
129 (int *)&sc_status);
130
131 /* clean up */
132 if (xmlSet) CFRelease(xmlSet);
133 if (xmlRemove) CFRelease(xmlRemove);
134 if (xmlNotify) CFRelease(xmlNotify);
135
136 if (status != KERN_SUCCESS) {
137 if (status != MACH_SEND_INVALID_DEST)
138 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("configset_m(): %s"), mach_error_string(status));
139 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
140 storePrivate->server = MACH_PORT_NULL;
141 _SCErrorSet(status);
142 return FALSE;
143 }
144
145 if (sc_status != kSCStatusOK) {
146 _SCErrorSet(sc_status);
147 return FALSE;
148 }
149
150 return TRUE;
151 }
152
153 Boolean
154 SCDynamicStoreSetValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value)
155 {
156 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
157 kern_return_t status;
158 CFDataRef utfKey; /* serialized key */
159 xmlData_t myKeyRef;
160 CFIndex myKeyLen;
161 CFDataRef xmlData; /* serialized data */
162 xmlData_t myDataRef;
163 CFIndex myDataLen;
164 int sc_status;
165 int newInstance;
166
167 if (_sc_verbose) {
168 SCLog(TRUE, LOG_DEBUG, CFSTR("SCDynamicStoreSetValue:"));
169 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
170 SCLog(TRUE, LOG_DEBUG, CFSTR(" value = %@"), value);
171 }
172
173 if (!store) {
174 /* sorry, you must provide a session */
175 _SCErrorSet(kSCStatusNoStoreSession);
176 return FALSE;
177 }
178
179 if (storePrivate->server == MACH_PORT_NULL) {
180 /* sorry, you must have an open session to play */
181 _SCErrorSet(kSCStatusNoStoreServer);
182 return FALSE;
183 }
184
185 /* serialize the key */
186 if (!_SCSerializeString(key, &utfKey, (void **)&myKeyRef, &myKeyLen)) {
187 _SCErrorSet(kSCStatusFailed);
188 return FALSE;
189 }
190
191 /* serialize the data */
192 if (!_SCSerialize(value, &xmlData, (void **)&myDataRef, &myDataLen)) {
193 CFRelease(utfKey);
194 _SCErrorSet(kSCStatusFailed);
195 return FALSE;
196 }
197
198 /* send the key & data to the server, get new instance id */
199 status = configset(storePrivate->server,
200 myKeyRef,
201 myKeyLen,
202 myDataRef,
203 myDataLen,
204 0,
205 &newInstance,
206 (int *)&sc_status);
207
208 /* clean up */
209 CFRelease(utfKey);
210 CFRelease(xmlData);
211
212 if (status != KERN_SUCCESS) {
213 if (status != MACH_SEND_INVALID_DEST)
214 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("configset(): %s"), mach_error_string(status));
215 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
216 storePrivate->server = MACH_PORT_NULL;
217 _SCErrorSet(status);
218 return FALSE;
219 }
220
221 if (sc_status != kSCStatusOK) {
222 _SCErrorSet(sc_status);
223 return FALSE;
224 }
225
226 return TRUE;
227 }