]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyadd.c
configd-293.8.tar.gz
[apple/configd.git] / configd.tproj / _notifyadd.c
1 /*
2 * Copyright (c) 2000-2004, 2006, 2008 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 "configd.h"
35 #include "session.h"
36 #include "pattern.h"
37
38
39 static __inline__ void
40 my_CFSetApplyFunction(CFSetRef theSet,
41 CFSetApplierFunction applier,
42 void *context)
43 {
44 CFAllocatorRef myAllocator;
45 CFSetRef mySet;
46
47 myAllocator = CFGetAllocator(theSet);
48 mySet = CFSetCreateCopy(myAllocator, theSet);
49 CFSetApplyFunction(mySet, applier, context);
50 CFRelease(mySet);
51 return;
52 }
53
54
55 __private_extern__
56 int
57 __SCDynamicStoreAddWatchedKey(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex, Boolean internal)
58 {
59 int sc_status = kSCStatusOK;
60 CFNumberRef sessionNum = NULL;
61 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
62
63 if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
64 return kSCStatusNoStoreSession; /* you must have an open session to play */
65 }
66
67 if (_configd_trace) {
68 SCTrace(TRUE, _configd_trace,
69 CFSTR("%s : %5d : %s : %@\n"),
70 internal ? "*watch+" : "watch+ ",
71 storePrivate->server,
72 isRegex ? "pattern" : "key",
73 key);
74 }
75
76 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
77
78 if (isRegex) {
79 if (CFSetContainsValue(storePrivate->patterns, key)) {
80 /* sorry, pattern already exists in notifier list */
81 sc_status = kSCStatusKeyExists;
82 goto done;
83 }
84
85 /*
86 * add this session as a pattern watcher
87 */
88 if (!patternAddSession(key, sessionNum)) {
89 sc_status = kSCStatusInvalidArgument;
90 goto done;
91 }
92
93 /* add pattern to this sessions notifier list */
94 CFSetAddValue(storePrivate->patterns, key);
95 } else {
96 if (CFSetContainsValue(storePrivate->keys, key)) {
97 /* sorry, key already exists in notifier list */
98 sc_status = kSCStatusKeyExists;
99 goto done;
100 }
101
102 /*
103 * We are watching a specific key. As such, update the
104 * store to mark our interest in any changes.
105 */
106 _addWatcher(sessionNum, key);
107
108 /* add key to this sessions notifier list */
109 CFSetAddValue(storePrivate->keys, key);
110 }
111
112 done :
113
114 if (sessionNum != NULL) CFRelease(sessionNum);
115 return sc_status;
116 }
117
118
119 __private_extern__
120 kern_return_t
121 _notifyadd(mach_port_t server,
122 xmlData_t keyRef, /* raw XML bytes */
123 mach_msg_type_number_t keyLen,
124 int isRegex,
125 int *sc_status
126 )
127 {
128 CFStringRef key = NULL; /* key (un-serialized) */
129 serverSessionRef mySession;
130
131 /* un-serialize the key */
132 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
133 *sc_status = kSCStatusFailed;
134 goto done;
135 }
136
137 if (!isA_CFString(key)) {
138 *sc_status = kSCStatusInvalidArgument;
139 goto done;
140 }
141
142 mySession = getSession(server);
143 if (mySession == NULL) {
144 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
145 goto done;
146 }
147
148 *sc_status = __SCDynamicStoreAddWatchedKey(mySession->store, key, isRegex != 0, FALSE);
149
150 done :
151
152 if (key) CFRelease(key);
153 return KERN_SUCCESS;
154 }
155
156
157 /*
158 * "context" argument for removeOldKey() and addNewKey()
159 */
160 typedef struct {
161 SCDynamicStoreRef store;
162 CFSetRef oldKeys; /* for addNewKey */
163 CFArrayRef newKeys; /* for removeOldKey */
164 Boolean isRegex;
165 int sc_status;
166 } updateKeysContext, *updateKeysContextRef;
167
168
169 static void
170 removeOldKey(const void *value, void *context)
171 {
172 CFStringRef oldKey = (CFStringRef)value;
173 updateKeysContextRef myContextRef = (updateKeysContextRef)context;
174
175 if (myContextRef->sc_status != kSCStatusOK) {
176 return;
177 }
178
179 if ((myContextRef->newKeys == NULL) ||
180 !CFArrayContainsValue(myContextRef->newKeys,
181 CFRangeMake(0, CFArrayGetCount(myContextRef->newKeys)),
182 oldKey)) {
183 /* the old notification key is not being retained, remove it */
184 myContextRef->sc_status = __SCDynamicStoreRemoveWatchedKey(myContextRef->store,
185 oldKey,
186 myContextRef->isRegex,
187 TRUE);
188 }
189
190 return;
191 }
192
193
194 static void
195 addNewKey(const void *value, void *context)
196 {
197 CFStringRef newKey = (CFStringRef)value;
198 updateKeysContextRef myContextRef = (updateKeysContextRef)context;
199
200 if (myContextRef->sc_status != kSCStatusOK) {
201 return;
202 }
203
204 if ((myContextRef->oldKeys == NULL) ||
205 !CFSetContainsValue(myContextRef->oldKeys, newKey)) {
206 /* if this is a new notification key */
207 myContextRef->sc_status = __SCDynamicStoreAddWatchedKey(myContextRef->store,
208 newKey,
209 myContextRef->isRegex,
210 TRUE);
211 }
212
213 return;
214 }
215
216
217 __private_extern__
218 int
219 __SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns)
220 {
221 updateKeysContext myContext;
222 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
223
224 if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
225 return kSCStatusNoStoreSession; /* you must have an open session to play */
226 }
227
228 if (_configd_trace) {
229 SCTrace(TRUE, _configd_trace,
230 CFSTR("watch : %5d : %d keys, %d patterns\n"),
231 storePrivate->server,
232 keys ? CFArrayGetCount(keys) : 0,
233 patterns ? CFArrayGetCount(patterns) : 0);
234 }
235
236 myContext.store = store;
237 myContext.sc_status = kSCStatusOK;
238
239 /* remove any previously registered keys, register any new keys */
240 myContext.oldKeys = CFSetCreateCopy(NULL, storePrivate->keys);
241 myContext.newKeys = keys;
242 myContext.isRegex = FALSE;
243 my_CFSetApplyFunction(storePrivate->keys, removeOldKey, &myContext);
244 if (keys != NULL) {
245 CFArrayApplyFunction(keys,
246 CFRangeMake(0, CFArrayGetCount(keys)),
247 addNewKey,
248 &myContext);
249 }
250 CFRelease(myContext.oldKeys);
251
252 /* remove any previously registered patterns, register any new patterns */
253 myContext.oldKeys = CFSetCreateCopy(NULL, storePrivate->patterns);
254 myContext.newKeys = patterns;
255 myContext.isRegex = TRUE;
256 my_CFSetApplyFunction(storePrivate->patterns, removeOldKey, &myContext);
257 if (patterns != NULL) {
258 CFArrayApplyFunction(patterns,
259 CFRangeMake(0, CFArrayGetCount(patterns)),
260 addNewKey,
261 &myContext);
262 }
263 CFRelease(myContext.oldKeys);
264
265 return myContext.sc_status;
266 }
267
268
269 __private_extern__
270 kern_return_t
271 _notifyset(mach_port_t server,
272 xmlData_t keysRef, /* raw XML bytes */
273 mach_msg_type_number_t keysLen,
274 xmlData_t patternsRef, /* raw XML bytes */
275 mach_msg_type_number_t patternsLen,
276 int *sc_status
277 )
278 {
279 CFArrayRef keys = NULL; /* key (un-serialized) */
280 serverSessionRef mySession;
281 CFArrayRef patterns = NULL; /* patterns (un-serialized) */
282
283 *sc_status = kSCStatusOK;
284
285 if ((keysRef != NULL) && (keysLen > 0)) {
286 /* un-serialize the keys */
287 if (!_SCUnserialize((CFPropertyListRef *)&keys, NULL, (void *)keysRef, keysLen)) {
288 *sc_status = kSCStatusFailed;
289 }
290 }
291
292 if ((patternsRef != NULL) && (patternsLen > 0)) {
293 /* un-serialize the patterns */
294 if (!_SCUnserialize((CFPropertyListRef *)&patterns, NULL, (void *)patternsRef, patternsLen)) {
295 *sc_status = kSCStatusFailed;
296 }
297 }
298
299 if (*sc_status != kSCStatusOK) {
300 goto done;
301 }
302
303 if ((keys != NULL) && !isA_CFArray(keys)) {
304 *sc_status = kSCStatusInvalidArgument;
305 goto done;
306 }
307
308 if ((patterns != NULL) && !isA_CFArray(patterns)) {
309 *sc_status = kSCStatusInvalidArgument;
310 goto done;
311 }
312
313 mySession = getSession(server);
314 if (mySession == NULL) {
315 /* you must have an open session to play */
316 *sc_status = kSCStatusNoStoreSession;
317 goto done;
318 }
319
320 *sc_status = __SCDynamicStoreSetNotificationKeys(mySession->store, keys, patterns);
321
322 done :
323
324 if (keys != NULL) CFRelease(keys);
325 if (patterns != NULL) CFRelease(patterns);
326
327 return KERN_SUCCESS;
328 }