]>
Commit | Line | Data |
---|---|---|
5958d7c0 | 1 | /* |
edebe297 | 2 | * Copyright (c) 2000-2004, 2006 Apple Computer, Inc. All rights reserved. |
5958d7c0 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
009ee3c6 | 5 | * |
009ee3c6 A |
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 | |
5958d7c0 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
009ee3c6 A |
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 | * | |
5958d7c0 A |
21 | * @APPLE_LICENSE_HEADER_END@ |
22 | */ | |
23 | ||
0fae82ee A |
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 | ||
5958d7c0 A |
34 | #include "configd.h" |
35 | #include "session.h" | |
009ee3c6 | 36 | #include "pattern.h" |
5958d7c0 | 37 | |
0fae82ee A |
38 | |
39 | static __inline__ void | |
009ee3c6 A |
40 | my_CFSetApplyFunction(CFSetRef theSet, |
41 | CFSetApplierFunction applier, | |
42 | void *context) | |
5958d7c0 | 43 | { |
0fae82ee | 44 | CFAllocatorRef myAllocator; |
009ee3c6 | 45 | CFSetRef mySet; |
0fae82ee | 46 | |
009ee3c6 A |
47 | myAllocator = CFGetAllocator(theSet); |
48 | mySet = CFSetCreateCopy(myAllocator, theSet); | |
49 | CFSetApplyFunction(mySet, applier, context); | |
50 | CFRelease(mySet); | |
0fae82ee A |
51 | return; |
52 | } | |
53 | ||
54 | ||
009ee3c6 | 55 | __private_extern__ |
0fae82ee | 56 | int |
009ee3c6 | 57 | __SCDynamicStoreAddWatchedKey(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex, Boolean internal) |
0fae82ee | 58 | { |
009ee3c6 A |
59 | int sc_status = kSCStatusOK; |
60 | CFNumberRef sessionNum = NULL; | |
61 | SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store; | |
5958d7c0 | 62 | |
edebe297 | 63 | if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) { |
0fae82ee | 64 | return kSCStatusNoStoreSession; /* you must have an open session to play */ |
5958d7c0 A |
65 | } |
66 | ||
009ee3c6 A |
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); | |
5958d7c0 A |
74 | } |
75 | ||
009ee3c6 | 76 | sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server); |
5958d7c0 | 77 | |
009ee3c6 A |
78 | if (isRegex) { |
79 | if (CFSetContainsValue(storePrivate->patterns, key)) { | |
80 | /* sorry, pattern already exists in notifier list */ | |
81 | sc_status = kSCStatusKeyExists; | |
82 | goto done; | |
5958d7c0 A |
83 | } |
84 | ||
85 | /* | |
009ee3c6 | 86 | * add this session as a pattern watcher |
5958d7c0 | 87 | */ |
009ee3c6 A |
88 | if (!patternAddSession(key, sessionNum)) { |
89 | sc_status = kSCStatusInvalidArgument; | |
90 | goto done; | |
5958d7c0 A |
91 | } |
92 | ||
009ee3c6 A |
93 | /* add pattern to this sessions notifier list */ |
94 | CFSetAddValue(storePrivate->patterns, key); | |
5958d7c0 | 95 | } else { |
009ee3c6 A |
96 | if (CFSetContainsValue(storePrivate->keys, key)) { |
97 | /* sorry, key already exists in notifier list */ | |
98 | sc_status = kSCStatusKeyExists; | |
99 | goto done; | |
100 | } | |
5958d7c0 A |
101 | |
102 | /* | |
103 | * We are watching a specific key. As such, update the | |
0fae82ee | 104 | * store to mark our interest in any changes. |
5958d7c0 | 105 | */ |
5958d7c0 | 106 | _addWatcher(sessionNum, key); |
009ee3c6 A |
107 | |
108 | /* add key to this sessions notifier list */ | |
109 | CFSetAddValue(storePrivate->keys, key); | |
5958d7c0 A |
110 | } |
111 | ||
009ee3c6 A |
112 | done : |
113 | ||
edebe297 | 114 | if (sessionNum != NULL) CFRelease(sessionNum); |
009ee3c6 | 115 | return sc_status; |
5958d7c0 A |
116 | } |
117 | ||
118 | ||
009ee3c6 | 119 | __private_extern__ |
5958d7c0 A |
120 | kern_return_t |
121 | _notifyadd(mach_port_t server, | |
122 | xmlData_t keyRef, /* raw XML bytes */ | |
123 | mach_msg_type_number_t keyLen, | |
0fae82ee A |
124 | int isRegex, |
125 | int *sc_status | |
5958d7c0 A |
126 | ) |
127 | { | |
dbf6a266 A |
128 | CFStringRef key = NULL; /* key (un-serialized) */ |
129 | serverSessionRef mySession = getSession(server); | |
5958d7c0 A |
130 | |
131 | /* un-serialize the key */ | |
009ee3c6 | 132 | if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) { |
0fae82ee | 133 | *sc_status = kSCStatusFailed; |
dbf6a266 | 134 | goto done; |
a5f60add A |
135 | } |
136 | ||
137 | if (!isA_CFString(key)) { | |
0fae82ee | 138 | *sc_status = kSCStatusInvalidArgument; |
dbf6a266 | 139 | goto done; |
009ee3c6 A |
140 | } |
141 | ||
edebe297 | 142 | if (mySession == NULL) { |
009ee3c6 | 143 | *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */ |
dbf6a266 | 144 | goto done; |
5958d7c0 A |
145 | } |
146 | ||
009ee3c6 | 147 | *sc_status = __SCDynamicStoreAddWatchedKey(mySession->store, key, isRegex != 0, FALSE); |
5958d7c0 | 148 | |
dbf6a266 A |
149 | done : |
150 | ||
151 | if (key) CFRelease(key); | |
5958d7c0 A |
152 | return KERN_SUCCESS; |
153 | } | |
009ee3c6 A |
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 | ||
edebe297 | 178 | if ((myContextRef->newKeys == NULL) || |
009ee3c6 A |
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 | ||
edebe297 | 203 | if ((myContextRef->oldKeys == NULL) || |
009ee3c6 A |
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 | ||
edebe297 | 223 | if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) { |
009ee3c6 A |
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); | |
edebe297 | 243 | if (keys != NULL) { |
009ee3c6 A |
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); | |
edebe297 | 256 | if (patterns != NULL) { |
009ee3c6 A |
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 | { | |
009ee3c6 | 278 | CFArrayRef keys = NULL; /* key (un-serialized) */ |
dbf6a266 | 279 | serverSessionRef mySession = getSession(server); |
009ee3c6 A |
280 | CFArrayRef patterns = NULL; /* patterns (un-serialized) */ |
281 | ||
009ee3c6 A |
282 | *sc_status = kSCStatusOK; |
283 | ||
edebe297 | 284 | if ((keysRef != NULL) && (keysLen > 0)) { |
009ee3c6 A |
285 | /* un-serialize the keys */ |
286 | if (!_SCUnserialize((CFPropertyListRef *)&keys, NULL, (void *)keysRef, keysLen)) { | |
287 | *sc_status = kSCStatusFailed; | |
009ee3c6 A |
288 | } |
289 | } | |
290 | ||
edebe297 | 291 | if ((patternsRef != NULL) && (patternsLen > 0)) { |
009ee3c6 A |
292 | /* un-serialize the patterns */ |
293 | if (!_SCUnserialize((CFPropertyListRef *)&patterns, NULL, (void *)patternsRef, patternsLen)) { | |
294 | *sc_status = kSCStatusFailed; | |
009ee3c6 A |
295 | } |
296 | } | |
297 | ||
edebe297 A |
298 | if (*sc_status != kSCStatusOK) { |
299 | goto done; | |
300 | } | |
301 | ||
302 | if ((keys != NULL) && !isA_CFArray(keys)) { | |
303 | *sc_status = kSCStatusInvalidArgument; | |
304 | goto done; | |
305 | } | |
306 | ||
307 | if ((patterns != NULL) && !isA_CFArray(patterns)) { | |
308 | *sc_status = kSCStatusInvalidArgument; | |
309 | goto done; | |
310 | } | |
311 | ||
312 | if (mySession == NULL) { | |
009ee3c6 A |
313 | /* you must have an open session to play */ |
314 | *sc_status = kSCStatusNoStoreSession; | |
edebe297 | 315 | goto done; |
009ee3c6 A |
316 | } |
317 | ||
edebe297 A |
318 | *sc_status = __SCDynamicStoreSetNotificationKeys(mySession->store, keys, patterns); |
319 | ||
320 | done : | |
009ee3c6 | 321 | |
edebe297 A |
322 | if (keys != NULL) CFRelease(keys); |
323 | if (patterns != NULL) CFRelease(patterns); | |
009ee3c6 A |
324 | |
325 | return KERN_SUCCESS; | |
326 | } |