]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDSet.c
configd-53.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDSet.c
1 /*
2 * Copyright (c) 2000-2002 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 if (!_SCSerialize(keysToSet, &xmlSet, (void **)&mySetRef, &mySetLen)) {
79 _SCErrorSet(kSCStatusFailed);
80 return NULL;
81 }
82 }
83
84 /* serialize the keys to remove */
85 if (keysToRemove) {
86 if (!_SCSerialize(keysToRemove, &xmlRemove, (void **)&myRemoveRef, &myRemoveLen)) {
87 if (xmlSet) CFRelease(xmlSet);
88 _SCErrorSet(kSCStatusFailed);
89 return NULL;
90 }
91 }
92
93 /* serialize the keys to notify */
94 if (keysToNotify) {
95 if (!_SCSerialize(keysToNotify, &xmlNotify, (void **)&myNotifyRef, &myNotifyLen)) {
96 if (xmlSet) CFRelease(xmlSet);
97 if (xmlRemove) CFRelease(xmlRemove);
98 _SCErrorSet(kSCStatusFailed);
99 return NULL;
100 }
101 }
102
103 /* send the keys and patterns, fetch the associated result from the server */
104 status = configset_m(storePrivate->server,
105 mySetRef,
106 mySetLen,
107 myRemoveRef,
108 myRemoveLen,
109 myNotifyRef,
110 myNotifyLen,
111 (int *)&sc_status);
112
113 /* clean up */
114 if (xmlSet) CFRelease(xmlSet);
115 if (xmlRemove) CFRelease(xmlRemove);
116 if (xmlNotify) CFRelease(xmlNotify);
117
118 if (status != KERN_SUCCESS) {
119 if (status != MACH_SEND_INVALID_DEST)
120 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("configset_m(): %s"), mach_error_string(status));
121 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
122 storePrivate->server = MACH_PORT_NULL;
123 _SCErrorSet(status);
124 return FALSE;
125 }
126
127 if (sc_status != kSCStatusOK) {
128 _SCErrorSet(sc_status);
129 return FALSE;
130 }
131
132 return TRUE;
133 }
134
135 Boolean
136 SCDynamicStoreSetValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value)
137 {
138 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
139 kern_return_t status;
140 CFDataRef xmlKey; /* serialized key */
141 xmlData_t myKeyRef;
142 CFIndex myKeyLen;
143 CFDataRef xmlData; /* serialized data */
144 xmlData_t myDataRef;
145 CFIndex myDataLen;
146 int sc_status;
147 int newInstance;
148
149 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCDynamicStoreSetValue:"));
150 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" key = %@"), key);
151 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" value = %@"), value);
152
153 if (!store) {
154 /* sorry, you must provide a session */
155 _SCErrorSet(kSCStatusNoStoreSession);
156 return FALSE;
157 }
158
159 if (storePrivate->server == MACH_PORT_NULL) {
160 /* sorry, you must have an open session to play */
161 _SCErrorSet(kSCStatusNoStoreServer);
162 return FALSE;
163 }
164
165 /* serialize the key */
166 if (!_SCSerialize(key, &xmlKey, (void **)&myKeyRef, &myKeyLen)) {
167 _SCErrorSet(kSCStatusFailed);
168 return NULL;
169 }
170
171 /* serialize the data */
172 if (!_SCSerialize(value, &xmlData, (void **)&myDataRef, &myDataLen)) {
173 CFRelease(xmlKey);
174 _SCErrorSet(kSCStatusFailed);
175 return NULL;
176 }
177
178 /* send the key & data to the server, get new instance id */
179 status = configset(storePrivate->server,
180 myKeyRef,
181 myKeyLen,
182 myDataRef,
183 myDataLen,
184 0,
185 &newInstance,
186 (int *)&sc_status);
187
188 /* clean up */
189 CFRelease(xmlKey);
190 CFRelease(xmlData);
191
192 if (status != KERN_SUCCESS) {
193 if (status != MACH_SEND_INVALID_DEST)
194 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("configset(): %s"), mach_error_string(status));
195 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
196 storePrivate->server = MACH_PORT_NULL;
197 _SCErrorSet(status);
198 return FALSE;
199 }
200
201 if (sc_status != kSCStatusOK) {
202 _SCErrorSet(sc_status);
203 return FALSE;
204 }
205
206 return TRUE;
207 }