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