]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDSet.c
configd-888.1.2.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDSet.c
1 /*
2 * Copyright (c) 2000-2006, 2009-2011, 2013, 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include "SCDynamicStoreInternal.h"
35 #include "config.h" /* MiG generated file */
36
37
38 Boolean
39 SCDynamicStoreSetMultiple(SCDynamicStoreRef store,
40 CFDictionaryRef keysToSet,
41 CFArrayRef keysToRemove,
42 CFArrayRef keysToNotify)
43 {
44 SCDynamicStorePrivateRef storePrivate;
45 kern_return_t status;
46 CFDataRef xmlSet = NULL; /* key/value pairs to set (XML serialized) */
47 xmlData_t mySetRef = NULL; /* key/value pairs to set (serialized) */
48 CFIndex mySetLen = 0;
49 CFDataRef xmlRemove = NULL; /* keys to remove (XML serialized) */
50 xmlData_t myRemoveRef = NULL; /* keys to remove (serialized) */
51 CFIndex myRemoveLen = 0;
52 CFDataRef xmlNotify = NULL; /* keys to notify (XML serialized) */
53 xmlData_t myNotifyRef = NULL; /* keys to notify (serialized) */
54 CFIndex myNotifyLen = 0;
55 int sc_status;
56
57 if (store == NULL) {
58 store = __SCDynamicStoreNullSession();
59 if (store == NULL) {
60 /* sorry, you must provide a session */
61 _SCErrorSet(kSCStatusNoStoreSession);
62 return FALSE;
63 }
64 }
65
66 storePrivate = (SCDynamicStorePrivateRef)store;
67 if (storePrivate->server == MACH_PORT_NULL) {
68 _SCErrorSet(kSCStatusNoStoreServer);
69 return FALSE; /* you must have an open session to play */
70 }
71
72 /* serialize the key/value pairs to set*/
73 if (keysToSet != NULL) {
74 CFDictionaryRef newInfo;
75 Boolean ok;
76
77 newInfo = _SCSerializeMultiple(keysToSet);
78 if (newInfo == NULL) {
79 _SCErrorSet(kSCStatusInvalidArgument);
80 return FALSE;
81 }
82
83 ok = _SCSerialize(newInfo, &xmlSet, (void **)&mySetRef, &mySetLen);
84 CFRelease(newInfo);
85 if (!ok) {
86 _SCErrorSet(kSCStatusInvalidArgument);
87 return FALSE;
88 }
89 }
90
91 /* serialize the keys to remove */
92 if (keysToRemove != NULL) {
93 if (!_SCSerialize(keysToRemove, &xmlRemove, (void **)&myRemoveRef, &myRemoveLen)) {
94 if (xmlSet != NULL) CFRelease(xmlSet);
95 _SCErrorSet(kSCStatusInvalidArgument);
96 return FALSE;
97 }
98 }
99
100 /* serialize the keys to notify */
101 if (keysToNotify != NULL) {
102 if (!_SCSerialize(keysToNotify, &xmlNotify, (void **)&myNotifyRef, &myNotifyLen)) {
103 if (xmlSet != NULL) CFRelease(xmlSet);
104 if (xmlRemove != NULL) CFRelease(xmlRemove);
105 _SCErrorSet(kSCStatusInvalidArgument);
106 return FALSE;
107 }
108 }
109
110 os_activity_scope(storePrivate->activity);
111
112 retry :
113
114 /* send the keys and patterns, fetch the associated result from the server */
115 status = configset_m(storePrivate->server,
116 mySetRef,
117 (mach_msg_type_number_t)mySetLen,
118 myRemoveRef,
119 (mach_msg_type_number_t)myRemoveLen,
120 myNotifyRef,
121 (mach_msg_type_number_t)myNotifyLen,
122 (int *)&sc_status);
123
124 if (__SCDynamicStoreCheckRetryAndHandleError(store,
125 status,
126 &sc_status,
127 "SCDynamicStoreSetMultiple configset_m()")) {
128 goto retry;
129 }
130
131 /* clean up */
132 if (xmlSet != NULL) CFRelease(xmlSet);
133 if (xmlRemove != NULL) CFRelease(xmlRemove);
134 if (xmlNotify != NULL) CFRelease(xmlNotify);
135
136 if (sc_status != kSCStatusOK) {
137 _SCErrorSet(sc_status);
138 return FALSE;
139 }
140
141 return TRUE;
142 }
143
144 Boolean
145 SCDynamicStoreSetValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value)
146 {
147 SCDynamicStorePrivateRef storePrivate;
148 kern_return_t status;
149 CFDataRef utfKey; /* serialized key */
150 xmlData_t myKeyRef;
151 CFIndex myKeyLen;
152 CFDataRef xmlData; /* serialized data */
153 xmlData_t myDataRef;
154 CFIndex myDataLen;
155 int sc_status;
156 int newInstance;
157
158 if (store == NULL) {
159 store = __SCDynamicStoreNullSession();
160 if (store == NULL) {
161 /* sorry, you must provide a session */
162 _SCErrorSet(kSCStatusNoStoreSession);
163 return FALSE;
164 }
165 }
166
167 storePrivate = (SCDynamicStorePrivateRef)store;
168 if (storePrivate->server == MACH_PORT_NULL) {
169 /* sorry, you must have an open session to play */
170 _SCErrorSet(kSCStatusNoStoreServer);
171 return FALSE;
172 }
173
174 /* serialize the key */
175 if (!_SCSerializeString(key, &utfKey, (void **)&myKeyRef, &myKeyLen)) {
176 _SCErrorSet(kSCStatusInvalidArgument);
177 return FALSE;
178 }
179
180 /* serialize the data */
181 if (!_SCSerialize(value, &xmlData, (void **)&myDataRef, &myDataLen)) {
182 CFRelease(utfKey);
183 _SCErrorSet(kSCStatusInvalidArgument);
184 return FALSE;
185 }
186
187 os_activity_scope(storePrivate->activity);
188
189 retry :
190
191 /* send the key & data to the server, get new instance id */
192 status = configset(storePrivate->server,
193 myKeyRef,
194 (mach_msg_type_number_t)myKeyLen,
195 myDataRef,
196 (mach_msg_type_number_t)myDataLen,
197 0,
198 &newInstance,
199 (int *)&sc_status);
200
201 if (__SCDynamicStoreCheckRetryAndHandleError(store,
202 status,
203 &sc_status,
204 "SCDynamicStoreSetValue configset()")) {
205 goto retry;
206 }
207
208 /* clean up */
209 CFRelease(utfKey);
210 CFRelease(xmlData);
211
212 if (sc_status != kSCStatusOK) {
213 _SCErrorSet(sc_status);
214 return FALSE;
215 }
216
217 return TRUE;
218 }