2 * Copyright (c) 2000-2004, 2006, 2008 Apple 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
== NULL
) || (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
!= NULL
) 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
;
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
;
142 mySession
= getSession(server
);
143 if (mySession
== NULL
) {
144 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
148 *sc_status
= __SCDynamicStoreAddWatchedKey(mySession
->store
, key
, isRegex
!= 0, FALSE
);
152 if (key
) CFRelease(key
);
158 * "context" argument for removeOldKey() and addNewKey()
161 SCDynamicStoreRef store
;
162 CFSetRef oldKeys
; /* for addNewKey */
163 CFArrayRef newKeys
; /* for removeOldKey */
166 } updateKeysContext
, *updateKeysContextRef
;
170 removeOldKey(const void *value
, void *context
)
172 CFStringRef oldKey
= (CFStringRef
)value
;
173 updateKeysContextRef myContextRef
= (updateKeysContextRef
)context
;
175 if (myContextRef
->sc_status
!= kSCStatusOK
) {
179 if ((myContextRef
->newKeys
== NULL
) ||
180 !CFArrayContainsValue(myContextRef
->newKeys
,
181 CFRangeMake(0, CFArrayGetCount(myContextRef
->newKeys
)),
183 /* the old notification key is not being retained, remove it */
184 myContextRef
->sc_status
= __SCDynamicStoreRemoveWatchedKey(myContextRef
->store
,
186 myContextRef
->isRegex
,
195 addNewKey(const void *value
, void *context
)
197 CFStringRef newKey
= (CFStringRef
)value
;
198 updateKeysContextRef myContextRef
= (updateKeysContextRef
)context
;
200 if (myContextRef
->sc_status
!= kSCStatusOK
) {
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
,
209 myContextRef
->isRegex
,
219 __SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store
, CFArrayRef keys
, CFArrayRef patterns
)
221 updateKeysContext myContext
;
222 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
224 if ((store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
225 return kSCStatusNoStoreSession
; /* you must have an open session to play */
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);
236 myContext
.store
= store
;
237 myContext
.sc_status
= kSCStatusOK
;
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
);
245 CFArrayApplyFunction(keys
,
246 CFRangeMake(0, CFArrayGetCount(keys
)),
250 CFRelease(myContext
.oldKeys
);
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
)),
263 CFRelease(myContext
.oldKeys
);
265 return myContext
.sc_status
;
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
,
279 CFArrayRef keys
= NULL
; /* key (un-serialized) */
280 serverSessionRef mySession
;
281 CFArrayRef patterns
= NULL
; /* patterns (un-serialized) */
283 *sc_status
= kSCStatusOK
;
285 if ((keysRef
!= NULL
) && (keysLen
> 0)) {
286 /* un-serialize the keys */
287 if (!_SCUnserialize((CFPropertyListRef
*)&keys
, NULL
, (void *)keysRef
, keysLen
)) {
288 *sc_status
= kSCStatusFailed
;
292 if ((patternsRef
!= NULL
) && (patternsLen
> 0)) {
293 /* un-serialize the patterns */
294 if (!_SCUnserialize((CFPropertyListRef
*)&patterns
, NULL
, (void *)patternsRef
, patternsLen
)) {
295 *sc_status
= kSCStatusFailed
;
299 if (*sc_status
!= kSCStatusOK
) {
303 if ((keys
!= NULL
) && !isA_CFArray(keys
)) {
304 *sc_status
= kSCStatusInvalidArgument
;
308 if ((patterns
!= NULL
) && !isA_CFArray(patterns
)) {
309 *sc_status
= kSCStatusInvalidArgument
;
313 mySession
= getSession(server
);
314 if (mySession
== NULL
) {
315 /* you must have an open session to play */
316 *sc_status
= kSCStatusNoStoreSession
;
320 *sc_status
= __SCDynamicStoreSetNotificationKeys(mySession
->store
, keys
, patterns
);
324 if (keys
!= NULL
) CFRelease(keys
);
325 if (patterns
!= NULL
) CFRelease(patterns
);