2 * Copyright (c) 2000-2004, 2006, 2008, 2010 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_CFArrayApplyFunction(CFArrayRef theArray
,
41 CFArrayApplierFunction applier
,
44 CFAllocatorRef myAllocator
;
47 myAllocator
= CFGetAllocator(theArray
);
48 myArray
= CFArrayCreateCopy(myAllocator
, theArray
);
49 CFArrayApplyFunction(myArray
, CFRangeMake(0, CFArrayGetCount(myArray
)), applier
, context
);
56 hasKey(CFMutableArrayRef keys
, CFStringRef key
)
61 n
= CFArrayGetCount(keys
);
62 if (CFArrayContainsValue(keys
, CFRangeMake(0, n
), key
)) {
63 /* sorry, pattern already exists in notifier list */
64 return kSCStatusKeyExists
;
73 addKey(CFMutableArrayRef
*keysP
, CFStringRef key
)
76 *keysP
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
79 CFArrayAppendValue(*keysP
, key
);
86 __SCDynamicStoreAddWatchedKey(SCDynamicStoreRef store
, CFStringRef key
, Boolean isRegex
, Boolean internal
)
88 int sc_status
= kSCStatusOK
;
89 CFNumberRef sessionNum
= NULL
;
90 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
92 if ((store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
93 return kSCStatusNoStoreSession
; /* you must have an open session to play */
97 SCTrace(TRUE
, _configd_trace
,
98 CFSTR("%s : %5d : %s : %@\n"),
99 internal
? "*watch+" : "watch+ ",
100 storePrivate
->server
,
101 isRegex
? "pattern" : "key",
105 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
108 sc_status
= hasKey(storePrivate
->patterns
, key
);
109 if (sc_status
!= kSCStatusOK
) {
114 * add this session as a pattern watcher
116 if (!patternAddSession(key
, sessionNum
)) {
117 sc_status
= kSCStatusInvalidArgument
;
121 /* add pattern to this sessions notifier list */
122 addKey(&storePrivate
->patterns
, key
);
124 sc_status
= hasKey(storePrivate
->keys
, key
);
125 if (sc_status
!= kSCStatusOK
) {
130 * We are watching a specific key. As such, update the
131 * store to mark our interest in any changes.
133 _addWatcher(sessionNum
, key
);
135 /* add key to this sessions notifier list */
136 addKey(&storePrivate
->keys
, key
);
141 if (sessionNum
!= NULL
) CFRelease(sessionNum
);
148 _notifyadd(mach_port_t server
,
149 xmlData_t keyRef
, /* raw XML bytes */
150 mach_msg_type_number_t keyLen
,
155 CFStringRef key
= NULL
; /* key (un-serialized) */
156 serverSessionRef mySession
;
158 /* un-serialize the key */
159 if (!_SCUnserializeString(&key
, NULL
, (void *)keyRef
, keyLen
)) {
160 *sc_status
= kSCStatusFailed
;
164 if (!isA_CFString(key
)) {
165 *sc_status
= kSCStatusInvalidArgument
;
169 mySession
= getSession(server
);
170 if (mySession
== NULL
) {
171 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
175 *sc_status
= __SCDynamicStoreAddWatchedKey(mySession
->store
, key
, isRegex
!= 0, FALSE
);
179 if (key
) CFRelease(key
);
185 * "context" argument for removeOldKey() and addNewKey()
188 SCDynamicStoreRef store
;
189 CFArrayRef oldKeys
; /* for addNewKey */
190 CFArrayRef newKeys
; /* for removeOldKey */
193 } updateKeysContext
, *updateKeysContextRef
;
197 removeOldKey(const void *value
, void *context
)
199 CFStringRef oldKey
= (CFStringRef
)value
;
200 updateKeysContextRef myContextRef
= (updateKeysContextRef
)context
;
202 if (myContextRef
->sc_status
!= kSCStatusOK
) {
206 if ((myContextRef
->newKeys
== NULL
) ||
207 !CFArrayContainsValue(myContextRef
->newKeys
,
208 CFRangeMake(0, CFArrayGetCount(myContextRef
->newKeys
)),
210 /* the old notification key is not being retained, remove it */
211 myContextRef
->sc_status
= __SCDynamicStoreRemoveWatchedKey(myContextRef
->store
,
213 myContextRef
->isRegex
,
222 addNewKey(const void *value
, void *context
)
224 CFStringRef newKey
= (CFStringRef
)value
;
225 updateKeysContextRef myContextRef
= (updateKeysContextRef
)context
;
227 if (myContextRef
->sc_status
!= kSCStatusOK
) {
231 if ((myContextRef
->oldKeys
== NULL
) ||
232 !CFArrayContainsValue(myContextRef
->oldKeys
,
233 CFRangeMake(0, CFArrayGetCount(myContextRef
->oldKeys
)),
235 /* if this is a new notification key */
236 myContextRef
->sc_status
= __SCDynamicStoreAddWatchedKey(myContextRef
->store
,
238 myContextRef
->isRegex
,
248 __SCDynamicStoreSetNotificationKeys(SCDynamicStoreRef store
, CFArrayRef keys
, CFArrayRef patterns
)
250 updateKeysContext myContext
;
251 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
253 if ((store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
254 return kSCStatusNoStoreSession
; /* you must have an open session to play */
257 if (_configd_trace
) {
258 SCTrace(TRUE
, _configd_trace
,
259 CFSTR("watch : %5d : %d keys, %d patterns\n"),
260 storePrivate
->server
,
261 keys
? CFArrayGetCount(keys
) : 0,
262 patterns
? CFArrayGetCount(patterns
) : 0);
265 myContext
.store
= store
;
266 myContext
.sc_status
= kSCStatusOK
;
268 /* remove any previously registered keys, register any new keys */
269 myContext
.oldKeys
= NULL
;
270 myContext
.newKeys
= keys
;
271 myContext
.isRegex
= FALSE
;
272 if (storePrivate
->keys
!= NULL
) {
273 myContext
.oldKeys
= CFArrayCreateCopy(NULL
, storePrivate
->keys
);
274 my_CFArrayApplyFunction(storePrivate
->keys
, removeOldKey
, &myContext
);
277 CFArrayApplyFunction(keys
,
278 CFRangeMake(0, CFArrayGetCount(keys
)),
282 if (myContext
.oldKeys
!= NULL
) CFRelease(myContext
.oldKeys
);
284 /* remove any previously registered patterns, register any new patterns */
285 myContext
.oldKeys
= NULL
;
286 myContext
.newKeys
= patterns
;
287 myContext
.isRegex
= TRUE
;
288 if (storePrivate
->patterns
!= NULL
) {
289 myContext
.oldKeys
= CFArrayCreateCopy(NULL
, storePrivate
->patterns
);
290 my_CFArrayApplyFunction(storePrivate
->patterns
, removeOldKey
, &myContext
);
292 if (patterns
!= NULL
) {
293 CFArrayApplyFunction(patterns
,
294 CFRangeMake(0, CFArrayGetCount(patterns
)),
298 if (myContext
.oldKeys
!= NULL
) CFRelease(myContext
.oldKeys
);
300 return myContext
.sc_status
;
306 _notifyset(mach_port_t server
,
307 xmlData_t keysRef
, /* raw XML bytes */
308 mach_msg_type_number_t keysLen
,
309 xmlData_t patternsRef
, /* raw XML bytes */
310 mach_msg_type_number_t patternsLen
,
314 CFArrayRef keys
= NULL
; /* key (un-serialized) */
315 serverSessionRef mySession
;
316 CFArrayRef patterns
= NULL
; /* patterns (un-serialized) */
318 *sc_status
= kSCStatusOK
;
320 if ((keysRef
!= NULL
) && (keysLen
> 0)) {
321 /* un-serialize the keys */
322 if (!_SCUnserialize((CFPropertyListRef
*)&keys
, NULL
, (void *)keysRef
, keysLen
)) {
323 *sc_status
= kSCStatusFailed
;
327 if ((patternsRef
!= NULL
) && (patternsLen
> 0)) {
328 /* un-serialize the patterns */
329 if (!_SCUnserialize((CFPropertyListRef
*)&patterns
, NULL
, (void *)patternsRef
, patternsLen
)) {
330 *sc_status
= kSCStatusFailed
;
334 if (*sc_status
!= kSCStatusOK
) {
338 if ((keys
!= NULL
) && !isA_CFArray(keys
)) {
339 *sc_status
= kSCStatusInvalidArgument
;
343 if ((patterns
!= NULL
) && !isA_CFArray(patterns
)) {
344 *sc_status
= kSCStatusInvalidArgument
;
348 mySession
= getSession(server
);
349 if (mySession
== NULL
) {
350 /* you must have an open session to play */
351 *sc_status
= kSCStatusNoStoreSession
;
355 *sc_status
= __SCDynamicStoreSetNotificationKeys(mySession
->store
, keys
, patterns
);
359 if (keys
!= NULL
) CFRelease(keys
);
360 if (patterns
!= NULL
) CFRelease(patterns
);