]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyadd.c
configd-130.tar.gz
[apple/configd.git] / configd.tproj / _notifyadd.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, 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 || (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) 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 = getSession(server);
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 if (!mySession) {
143 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
144 goto done;
145 }
146
147 *sc_status = __SCDynamicStoreAddWatchedKey(mySession->store, key, isRegex != 0, FALSE);
148
149 done :
150
151 if (key) CFRelease(key);
152 return KERN_SUCCESS;
153 }
154
155
156 /*
157 * "context" argument for removeOldKey() and addNewKey()
158 */
159 typedef struct {
160 SCDynamicStoreRef store;
161 CFSetRef oldKeys; /* for addNewKey */
162 CFArrayRef newKeys; /* for removeOldKey */
163 Boolean isRegex;
164 int sc_status;
165 } updateKeysContext, *updateKeysContextRef;
166
167
168 static void
169 removeOldKey(const void *value, void *context)
170 {
171 CFStringRef oldKey = (CFStringRef)value;
172 updateKeysContextRef myContextRef = (updateKeysContextRef)context;
173
174 if (myContextRef->sc_status != kSCStatusOK) {
175 return;
176 }
177
178 if (!myContextRef->newKeys ||
179 !CFArrayContainsValue(myContextRef->newKeys,
180 CFRangeMake(0, CFArrayGetCount(myContextRef->newKeys)),
181 oldKey)) {
182 /* the old notification key is not being retained, remove it */
183 myContextRef->sc_status = __SCDynamicStoreRemoveWatchedKey(myContextRef->store,
184 oldKey,
185 myContextRef->isRegex,
186 TRUE);
187 }
188
189 return;
190 }
191
192
193 static void
194 addNewKey(const void *value, void *context)
195 {
196 CFStringRef newKey = (CFStringRef)value;
197 updateKeysContextRef myContextRef = (updateKeysContextRef)context;
198
199 if (myContextRef->sc_status != kSCStatusOK) {
200 return;
201 }
202
203 if (!myContextRef->oldKeys ||
204 !CFSetContainsValue(myContextRef->oldKeys, newKey)) {
205 /* if this is a new notification key */
206 myContextRef->sc_status = __SCDynamicStoreAddWatchedKey(myContextRef->store,
207 newKey,
208 myContextRef->isRegex,
209 TRUE);
210 }
211
212 return;
213 }
214
215
216 __private_extern__
217 int
218 __SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns)
219 {
220 updateKeysContext myContext;
221 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
222
223 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
224 return kSCStatusNoStoreSession; /* you must have an open session to play */
225 }
226
227 if (_configd_trace) {
228 SCTrace(TRUE, _configd_trace,
229 CFSTR("watch : %5d : %d keys, %d patterns\n"),
230 storePrivate->server,
231 keys ? CFArrayGetCount(keys) : 0,
232 patterns ? CFArrayGetCount(patterns) : 0);
233 }
234
235 myContext.store = store;
236 myContext.sc_status = kSCStatusOK;
237
238 /* remove any previously registered keys, register any new keys */
239 myContext.oldKeys = CFSetCreateCopy(NULL, storePrivate->keys);
240 myContext.newKeys = keys;
241 myContext.isRegex = FALSE;
242 my_CFSetApplyFunction(storePrivate->keys, removeOldKey, &myContext);
243 if (keys) {
244 CFArrayApplyFunction(keys,
245 CFRangeMake(0, CFArrayGetCount(keys)),
246 addNewKey,
247 &myContext);
248 }
249 CFRelease(myContext.oldKeys);
250
251 /* remove any previously registered patterns, register any new patterns */
252 myContext.oldKeys = CFSetCreateCopy(NULL, storePrivate->patterns);
253 myContext.newKeys = patterns;
254 myContext.isRegex = TRUE;
255 my_CFSetApplyFunction(storePrivate->patterns, removeOldKey, &myContext);
256 if (patterns) {
257 CFArrayApplyFunction(patterns,
258 CFRangeMake(0, CFArrayGetCount(patterns)),
259 addNewKey,
260 &myContext);
261 }
262 CFRelease(myContext.oldKeys);
263
264 return myContext.sc_status;
265 }
266
267
268 __private_extern__
269 kern_return_t
270 _notifyset(mach_port_t server,
271 xmlData_t keysRef, /* raw XML bytes */
272 mach_msg_type_number_t keysLen,
273 xmlData_t patternsRef, /* raw XML bytes */
274 mach_msg_type_number_t patternsLen,
275 int *sc_status
276 )
277 {
278 CFArrayRef keys = NULL; /* key (un-serialized) */
279 serverSessionRef mySession = getSession(server);
280 CFArrayRef patterns = NULL; /* patterns (un-serialized) */
281
282 *sc_status = kSCStatusOK;
283
284 if (keysRef && (keysLen > 0)) {
285 /* un-serialize the keys */
286 if (!_SCUnserialize((CFPropertyListRef *)&keys, NULL, (void *)keysRef, keysLen)) {
287 *sc_status = kSCStatusFailed;
288 } else if (!isA_CFArray(keys)) {
289 *sc_status = kSCStatusInvalidArgument;
290 }
291 }
292
293 if (patternsRef && (patternsLen > 0)) {
294 /* un-serialize the patterns */
295 if (!_SCUnserialize((CFPropertyListRef *)&patterns, NULL, (void *)patternsRef, patternsLen)) {
296 *sc_status = kSCStatusFailed;
297 } else if (!isA_CFArray(patterns)) {
298 *sc_status = kSCStatusInvalidArgument;
299 }
300 }
301
302 if (!mySession) {
303 /* you must have an open session to play */
304 *sc_status = kSCStatusNoStoreSession;
305 }
306
307 if (*sc_status == kSCStatusOK) {
308 *sc_status = __SCDynamicStoreSetNotificationKeys(mySession->store, keys, patterns);
309 }
310
311 if (keys) CFRelease(keys);
312 if (patterns) CFRelease(patterns);
313
314 return KERN_SUCCESS;
315 }