2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
39 static __inline__
void
40 my_CFSetApplyFunction(CFSetRef theSet
,
41 CFSetApplierFunction applier
,
44 CFAllocatorRef myAllocator
;
47 myAllocator
= CFGetAllocator(theSet
);
48 mySet
= CFSetCreateCopy(myAllocator
, theSet
);
49 CFSetApplyFunction(mySet
, applier
, context
);
57 __SCDynamicStoreAddWatchedKey(SCDynamicStoreRef store
, CFStringRef key
, Boolean isRegex
, Boolean internal
)
59 int sc_status
= kSCStatusOK
;
60 CFNumberRef sessionNum
= NULL
;
61 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
63 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
64 return kSCStatusNoStoreSession
; /* you must have an open session to play */
68 SCTrace(TRUE
, _configd_trace
,
69 CFSTR("%s : %5d : %s : %@\n"),
70 internal
? "*watch+" : "watch+ ",
72 isRegex
? "pattern" : "key",
76 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
79 if (CFSetContainsValue(storePrivate
->patterns
, key
)) {
80 /* sorry, pattern already exists in notifier list */
81 sc_status
= kSCStatusKeyExists
;
86 * add this session as a pattern watcher
88 if (!patternAddSession(key
, sessionNum
)) {
89 sc_status
= kSCStatusInvalidArgument
;
93 /* add pattern to this sessions notifier list */
94 CFSetAddValue(storePrivate
->patterns
, key
);
96 if (CFSetContainsValue(storePrivate
->keys
, key
)) {
97 /* sorry, key already exists in notifier list */
98 sc_status
= kSCStatusKeyExists
;
103 * We are watching a specific key. As such, update the
104 * store to mark our interest in any changes.
106 _addWatcher(sessionNum
, key
);
108 /* add key to this sessions notifier list */
109 CFSetAddValue(storePrivate
->keys
, key
);
114 if (sessionNum
) CFRelease(sessionNum
);
121 _notifyadd(mach_port_t server
,
122 xmlData_t keyRef
, /* raw XML bytes */
123 mach_msg_type_number_t keyLen
,
128 CFStringRef key
= NULL
; /* key (un-serialized) */
129 serverSessionRef mySession
= getSession(server
);
131 /* un-serialize the key */
132 if (!_SCUnserializeString(&key
, NULL
, (void *)keyRef
, keyLen
)) {
133 *sc_status
= kSCStatusFailed
;
137 if (!isA_CFString(key
)) {
138 *sc_status
= kSCStatusInvalidArgument
;
143 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
147 *sc_status
= __SCDynamicStoreAddWatchedKey(mySession
->store
, key
, isRegex
!= 0, FALSE
);
151 if (key
) CFRelease(key
);
157 * "context" argument for removeOldKey() and addNewKey()
160 SCDynamicStoreRef store
;
161 CFSetRef oldKeys
; /* for addNewKey */
162 CFArrayRef newKeys
; /* for removeOldKey */
165 } updateKeysContext
, *updateKeysContextRef
;
169 removeOldKey(const void *value
, void *context
)
171 CFStringRef oldKey
= (CFStringRef
)value
;
172 updateKeysContextRef myContextRef
= (updateKeysContextRef
)context
;
174 if (myContextRef
->sc_status
!= kSCStatusOK
) {
178 if (!myContextRef
->newKeys
||
179 !CFArrayContainsValue(myContextRef
->newKeys
,
180 CFRangeMake(0, CFArrayGetCount(myContextRef
->newKeys
)),
182 /* the old notification key is not being retained, remove it */
183 myContextRef
->sc_status
= __SCDynamicStoreRemoveWatchedKey(myContextRef
->store
,
185 myContextRef
->isRegex
,
194 addNewKey(const void *value
, void *context
)
196 CFStringRef newKey
= (CFStringRef
)value
;
197 updateKeysContextRef myContextRef
= (updateKeysContextRef
)context
;
199 if (myContextRef
->sc_status
!= kSCStatusOK
) {
203 if (!myContextRef
->oldKeys
||
204 !CFSetContainsValue(myContextRef
->oldKeys
, newKey
)) {
205 /* if this is a new notification key */
206 myContextRef
->sc_status
= __SCDynamicStoreAddWatchedKey(myContextRef
->store
,
208 myContextRef
->isRegex
,
218 __SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store
, CFArrayRef keys
, CFArrayRef patterns
)
220 updateKeysContext myContext
;
221 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
223 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
224 return kSCStatusNoStoreSession
; /* you must have an open session to play */
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);
235 myContext
.store
= store
;
236 myContext
.sc_status
= kSCStatusOK
;
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
);
244 CFArrayApplyFunction(keys
,
245 CFRangeMake(0, CFArrayGetCount(keys
)),
249 CFRelease(myContext
.oldKeys
);
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
);
257 CFArrayApplyFunction(patterns
,
258 CFRangeMake(0, CFArrayGetCount(patterns
)),
262 CFRelease(myContext
.oldKeys
);
264 return myContext
.sc_status
;
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
,
278 CFArrayRef keys
= NULL
; /* key (un-serialized) */
279 serverSessionRef mySession
= getSession(server
);
280 CFArrayRef patterns
= NULL
; /* patterns (un-serialized) */
282 *sc_status
= kSCStatusOK
;
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
;
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
;
303 /* you must have an open session to play */
304 *sc_status
= kSCStatusNoStoreSession
;
307 if (*sc_status
== kSCStatusOK
) {
308 *sc_status
= __SCDynamicStoreSetNotificationKeys(mySession
->store
, keys
, patterns
);
311 if (keys
) CFRelease(keys
);
312 if (patterns
) CFRelease(patterns
);