2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
29 * June 2, 2000 Allan Nathanson <ajn@apple.com>
37 CFMutableDictionaryRef sessionData
= NULL
;
39 CFMutableDictionaryRef storeData
= NULL
;
40 CFMutableDictionaryRef storeData_s
= NULL
;
42 CFMutableSetRef changedKeys
= NULL
;
43 CFMutableSetRef changedKeys_s
= NULL
;
45 CFMutableSetRef deferredRemovals
= NULL
;
46 CFMutableSetRef deferredRemovals_s
= NULL
;
48 CFMutableSetRef removedSessionKeys
= NULL
;
49 CFMutableSetRef removedSessionKeys_s
= NULL
;
51 CFMutableSetRef needsNotification
= NULL
;
53 int storeLocked
= 0; /* > 0 if dynamic store locked */
57 _swapLockedStoreData()
62 storeData
= storeData_s
;
66 changedKeys
= changedKeys_s
;
69 temp
= deferredRemovals
;
70 deferredRemovals
= deferredRemovals_s
;
71 deferredRemovals_s
= temp
;
73 temp
= removedSessionKeys
;
74 removedSessionKeys
= removedSessionKeys_s
;
75 removedSessionKeys_s
= temp
;
82 _addWatcher(CFNumberRef sessionNum
, CFStringRef watchedKey
)
85 CFMutableDictionaryRef newDict
;
87 CFMutableArrayRef newWatchers
;
88 CFArrayRef watcherRefs
;
89 CFMutableArrayRef newWatcherRefs
;
95 * Get the dictionary associated with this key out of the store
97 dict
= CFDictionaryGetValue(storeData
, watchedKey
);
99 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
101 newDict
= CFDictionaryCreateMutable(NULL
,
103 &kCFTypeDictionaryKeyCallBacks
,
104 &kCFTypeDictionaryValueCallBacks
);
108 * Get the set of watchers out of the keys dictionary
110 watchers
= CFDictionaryGetValue(newDict
, kSCDWatchers
);
111 watcherRefs
= CFDictionaryGetValue(newDict
, kSCDWatcherRefs
);
113 newWatchers
= CFArrayCreateMutableCopy(NULL
, 0, watchers
);
114 newWatcherRefs
= CFArrayCreateMutableCopy(NULL
, 0, watcherRefs
);
116 newWatchers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
117 newWatcherRefs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
121 * Add my session to the set of watchers
123 i
= CFArrayGetFirstIndexOfValue(newWatchers
,
124 CFRangeMake(0, CFArrayGetCount(newWatchers
)),
127 /* if this is the first instance of this session watching this key */
128 CFArrayAppendValue(newWatchers
, sessionNum
);
130 refNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &refCnt
);
131 CFArrayAppendValue(newWatcherRefs
, refNum
);
134 /* if this is another instance of this session watching this key */
135 refNum
= CFArrayGetValueAtIndex(newWatcherRefs
, i
);
136 CFNumberGetValue(refNum
, kCFNumberIntType
, &refCnt
);
138 refNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &refCnt
);
139 CFArraySetValueAtIndex(newWatcherRefs
, i
, refNum
);
144 * Update the keys dictionary
146 CFDictionarySetValue(newDict
, kSCDWatchers
, newWatchers
);
147 CFRelease(newWatchers
);
148 CFDictionarySetValue(newDict
, kSCDWatcherRefs
, newWatcherRefs
);
149 CFRelease(newWatcherRefs
);
152 * Update the store for this key
154 CFDictionarySetValue(storeData
, watchedKey
, newDict
);
157 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" _addWatcher: %@, %@"), sessionNum
, watchedKey
);
164 * _addRegexWatcherByKey()
166 * This is a CFDictionaryApplierFunction which will iterate over each key
167 * defined in the "storeData" dictionary. The arguments are the dictionary
168 * key, it's associated store dictionary, and a context structure which
169 * includes the following:
171 * 1. the session which has just added a regex notification request
172 * 2. the compiled regular expression associated with the above key.
174 * If a (real) dictionary key is found which matches the provided regular
175 * expression then we mark that key as being watched by the session.
178 _addRegexWatcherByKey(const void *key
, void *val
, void *context
)
180 CFStringRef storeStr
= key
;
181 CFDictionaryRef info
= val
;
182 mach_port_t sessionID
= ((addContextRef
)context
)->store
->server
;
183 regex_t
*preg
= ((addContextRef
)context
)->preg
;
186 CFNumberRef sessionNum
;
191 if (CFDictionaryContainsKey(info
, kSCDData
) == FALSE
) {
192 /* if no data (yet) */
196 /* convert store key to C string */
197 storeKeyLen
= CFStringGetLength(storeStr
) + 1;
198 storeKey
= CFAllocatorAllocate(NULL
, storeKeyLen
, 0);
199 if (!CFStringGetCString(storeStr
, storeKey
, storeKeyLen
, kCFStringEncodingMacRoman
)) {
200 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("CFStringGetCString: could not convert store key to C string"));
201 CFAllocatorDeallocate(NULL
, storeKey
);
205 /* compare store key to new notification keys regular expression pattern */
206 reError
= regexec(preg
, storeKey
, 0, NULL
, 0);
209 /* we've got a match */
210 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &sessionID
);
211 _addWatcher(sessionNum
, storeStr
);
212 CFRelease(sessionNum
);
218 reErrStrLen
= regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
219 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regexec(): %s"), reErrBuf
);
222 CFAllocatorDeallocate(NULL
, storeKey
);
227 * _addRegexWatchersBySession()
229 * This is a CFDictionaryApplierFunction which will iterate over each session
230 * defined in the "sessionData" dictionary. The arguments are the session
231 * key, it's associated session dictionary, , and the store key being added.
233 * If an active session includes any regular expression keys which match the
234 * key being added to the "storeData" dictionary then we mark this key as being
235 * watched by the session.
238 _addRegexWatchersBySession(const void *key
, void *val
, void *context
)
240 CFStringRef sessionKey
= key
;
241 CFDictionaryRef info
= val
;
242 CFStringRef addedKey
= context
;
250 /* if no dictionary for this session */
254 rKeys
= CFDictionaryGetValue(info
, kSCDRegexKeys
);
256 /* if no regex keys for this session */
259 rData
= CFDictionaryGetValue(info
, kSCDRegexData
);
261 /* convert new key to C string */
262 newKeyLen
= CFStringGetLength(addedKey
) + 1;
263 newKeyStr
= CFAllocatorAllocate(NULL
, newKeyLen
, 0);
264 if (!CFStringGetCString(addedKey
, newKeyStr
, newKeyLen
, kCFStringEncodingMacRoman
)) {
265 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("CFStringGetCString: could not convert new key to C string"));
266 CFAllocatorDeallocate(NULL
, newKeyStr
);
270 /* iterate over the regex keys looking for an pattern which matches the new key */
271 for (i
=0; i
<CFArrayGetCount(rKeys
); i
++) {
272 CFDataRef regexData
= CFArrayGetValueAtIndex(rData
, i
);
273 regex_t
*preg
= (regex_t
*)CFDataGetBytePtr(regexData
);
278 CFNumberRef sessionNum
;
280 /* check if this key matches the regular expression */
281 reError
= regexec(preg
, newKeyStr
, 0, NULL
, 0);
284 /* we've got a match, add a reference */
285 sessionInt
= CFStringGetIntValue(sessionKey
);
286 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &sessionInt
);
287 _addWatcher(sessionNum
, addedKey
);
288 CFRelease(sessionNum
);
294 reErrStrLen
= regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
295 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regexec(): %s"), reErrBuf
);
300 CFAllocatorDeallocate(NULL
, newKeyStr
);
307 _removeWatcher(CFNumberRef sessionNum
, CFStringRef watchedKey
)
309 CFDictionaryRef dict
;
310 CFMutableDictionaryRef newDict
;
312 CFMutableArrayRef newWatchers
;
313 CFArrayRef watcherRefs
;
314 CFMutableArrayRef newWatcherRefs
;
320 * Get the dictionary associated with this key out of the store
322 dict
= CFDictionaryGetValue(storeData
, watchedKey
);
323 if ((dict
== NULL
) || (CFDictionaryContainsKey(dict
, kSCDWatchers
) == FALSE
)) {
324 /* key doesn't exist (isn't this really fatal?) */
325 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" _removeWatcher: %@, %@, key not watched"), sessionNum
, watchedKey
);
328 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
331 * Get the set of watchers out of the keys dictionary and
332 * remove this session from the list.
334 watchers
= CFDictionaryGetValue(newDict
, kSCDWatchers
);
335 newWatchers
= CFArrayCreateMutableCopy(NULL
, 0, watchers
);
337 watcherRefs
= CFDictionaryGetValue(newDict
, kSCDWatcherRefs
);
338 newWatcherRefs
= CFArrayCreateMutableCopy(NULL
, 0, watcherRefs
);
340 /* locate the session reference */
341 i
= CFArrayGetFirstIndexOfValue(newWatchers
,
342 CFRangeMake(0, CFArrayGetCount(newWatchers
)),
345 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" _removeWatcher: %@, %@, session not watching"), sessionNum
, watchedKey
);
347 CFRelease(newWatchers
);
348 CFRelease(newWatcherRefs
);
352 /* remove one session reference */
353 refNum
= CFArrayGetValueAtIndex(newWatcherRefs
, i
);
354 CFNumberGetValue(refNum
, kCFNumberIntType
, &refCnt
);
356 refNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &refCnt
);
357 CFArraySetValueAtIndex(newWatcherRefs
, i
, refNum
);
360 /* if this was the last reference */
361 CFArrayRemoveValueAtIndex(newWatchers
, i
);
362 CFArrayRemoveValueAtIndex(newWatcherRefs
, i
);
365 if (CFArrayGetCount(newWatchers
) > 0) {
366 /* if this key is still being "watched" */
367 CFDictionarySetValue(newDict
, kSCDWatchers
, newWatchers
);
368 CFDictionarySetValue(newDict
, kSCDWatcherRefs
, newWatcherRefs
);
370 /* no watchers left, remove the empty set */
371 CFDictionaryRemoveValue(newDict
, kSCDWatchers
);
372 CFDictionaryRemoveValue(newDict
, kSCDWatcherRefs
);
374 CFRelease(newWatchers
);
375 CFRelease(newWatcherRefs
);
377 if (CFDictionaryGetCount(newDict
) > 0) {
378 /* if this key is still active */
379 CFDictionarySetValue(storeData
, watchedKey
, newDict
);
381 /* no information left, remove the empty dictionary */
382 CFDictionaryRemoveValue(storeData
, watchedKey
);
386 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" _removeWatcher: %@, %@"), sessionNum
, watchedKey
);
393 * _removeRegexWatcherByKey()
395 * This is a CFDictionaryApplierFunction which will iterate over each key
396 * defined in the "storeData" dictionary. The arguments are the dictionary
397 * key, it's associated store dictionary, and a context structure which
398 * includes the following:
400 * 1. the session which has just removed a regex notification request
401 * 2. the compiled regular expression associated with the above key.
403 * If a key is found and it matches the provided regular expression then
404 * it will its "being watched" status will be cleared.
407 _removeRegexWatcherByKey(const void *key
, void *val
, void *context
)
409 CFStringRef storeStr
= key
;
410 CFDictionaryRef info
= val
;
411 mach_port_t sessionID
= ((removeContextRef
)context
)->store
->server
;
412 regex_t
*preg
= ((removeContextRef
)context
)->preg
;
413 CFNumberRef sessionNum
;
421 if ((info
== NULL
) || (CFDictionaryContainsKey(info
, kSCDWatchers
) == FALSE
)) {
422 /* no dictionary or no watchers */
426 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &sessionID
);
428 watchers
= CFDictionaryGetValue(info
, kSCDWatchers
);
429 if (CFArrayContainsValue(watchers
,
430 CFRangeMake(0, CFArrayGetCount(watchers
)),
431 sessionNum
) == FALSE
) {
432 /* this session is not watching this key */
433 CFRelease(sessionNum
);
437 /* convert key to C string */
438 storeKeyLen
= CFStringGetLength(storeStr
) + 1;
439 storeKey
= CFAllocatorAllocate(NULL
, storeKeyLen
, 0);
440 if (!CFStringGetCString(storeStr
, storeKey
, storeKeyLen
, kCFStringEncodingMacRoman
)) {
441 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("CFStringGetCString: could not convert key to C string"));
442 CFAllocatorDeallocate(NULL
, storeKey
);
443 CFRelease(sessionNum
);
447 /* check if this key matches the regular expression */
448 reError
= regexec(preg
, storeKey
, 0, NULL
, 0);
451 /* we've got a match */
452 _removeWatcher(sessionNum
, storeStr
);
458 reErrStrLen
= regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
459 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regexec(): %s"), reErrBuf
);
462 CFAllocatorDeallocate(NULL
, storeKey
);
463 CFRelease(sessionNum
);
468 * _removeRegexWatchersBySession()
470 * This is a CFDictionaryApplierFunction which will iterate over each session
471 * defined in the "sessionData" dictionary. The arguments are the session
472 * key, it's associated session dictionary, and the store key being removed.
474 * If an active session includes any regular expression keys which match the
475 * key being removed from the "storeData" dictionary then we clear this keys
476 * reference of being watched.
479 _removeRegexWatchersBySession(const void *key
, void *val
, void *context
)
481 CFStringRef sessionKey
= key
;
482 CFDictionaryRef info
= val
;
483 CFStringRef removedKey
= context
;
491 /* if no dictionary for this session */
495 rKeys
= CFDictionaryGetValue(info
, kSCDRegexKeys
);
497 /* if no regex keys for this session */
500 rData
= CFDictionaryGetValue(info
, kSCDRegexData
);
502 /* convert new key to C string */
503 oldKeyLen
= CFStringGetLength(removedKey
) + 1;
504 oldKeyStr
= CFAllocatorAllocate(NULL
, oldKeyLen
, 0);
505 if (!CFStringGetCString(removedKey
, oldKeyStr
, oldKeyLen
, kCFStringEncodingMacRoman
)) {
506 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("CFStringGetCString: could not convert old key to C string"));
507 CFAllocatorDeallocate(NULL
, oldKeyStr
);
511 /* iterate over the regex keys looking for an pattern which matches the old key */
512 for (i
=0; i
<CFArrayGetCount(rKeys
); i
++) {
513 CFDataRef regexData
= CFArrayGetValueAtIndex(rData
, i
);
514 regex_t
*preg
= (regex_t
*)CFDataGetBytePtr(regexData
);
519 CFNumberRef sessionNum
;
521 /* check if this key matches the regular expression */
522 reError
= regexec(preg
, oldKeyStr
, 0, NULL
, 0);
525 /* we've got a match, remove a reference */
526 sessionInt
= CFStringGetIntValue(sessionKey
);
527 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &sessionInt
);
528 _removeWatcher(sessionNum
, removedKey
);
529 CFRelease(sessionNum
);
535 reErrStrLen
= regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
536 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regexec(): %s"), reErrBuf
);
541 CFAllocatorDeallocate(NULL
, oldKeyStr
);