2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
44 isMySessionKey(CFStringRef sessionKey
, CFStringRef key
)
47 CFStringRef storeSessionKey
;
49 dict
= CFDictionaryGetValue(storeData
, key
);
51 /* if key no longer exists */
55 storeSessionKey
= CFDictionaryGetValue(dict
, kSCDSession
);
56 if (!storeSessionKey
) {
57 /* if this is not a session key */
61 if (!CFEqual(sessionKey
, storeSessionKey
)) {
62 /* if this is not "my" session key */
71 removeAllKeys(SCDynamicStoreRef store
, Boolean isRegex
)
73 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
77 keys
= isRegex
? storePrivate
->patterns
: storePrivate
->keys
;
78 n
= CFSetGetCount(keys
);
81 CFArrayRef keysToRemove
;
82 const void * watchedKeys_q
[N_QUICK
];
83 const void ** watchedKeys
= watchedKeys_q
;
85 if (n
> (CFIndex
)(sizeof(watchedKeys_q
) / sizeof(CFStringRef
)))
86 watchedKeys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
87 CFSetGetValues(keys
, watchedKeys
);
88 keysToRemove
= CFArrayCreate(NULL
, watchedKeys
, n
, &kCFTypeArrayCallBacks
);
89 if (watchedKeys
!= watchedKeys_q
) CFAllocatorDeallocate(NULL
, watchedKeys
);
90 for (i
= 0; i
< n
; i
++) {
91 (void) __SCDynamicStoreRemoveWatchedKey(store
,
92 CFArrayGetValueAtIndex(keysToRemove
, i
),
96 CFRelease(keysToRemove
);
105 __SCDynamicStoreClose(SCDynamicStoreRef
*store
, Boolean internal
)
107 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)*store
;
109 CFStringRef sessionKey
;
110 CFDictionaryRef dict
;
112 serverSessionRef mySession
;
114 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreClose:"));
116 if ((*store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
117 return kSCStatusNoStoreSession
; /* you must have an open session to play */
120 if (_configd_trace
) {
121 SCTrace(TRUE
, _configd_trace
,
123 internal
? "*close " : "close ",
124 storePrivate
->server
);
127 /* Remove all notification keys and patterns */
128 removeAllKeys(*store
, FALSE
); // keys
129 removeAllKeys(*store
, TRUE
); // patterns
131 /* Remove/cancel any outstanding notification requests. */
132 (void) __SCDynamicStoreNotifyCancel(*store
);
134 /* Remove any session keys */
135 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), storePrivate
->server
);
136 dict
= CFDictionaryGetValue(sessionData
, sessionKey
);
137 keys
= CFDictionaryGetValue(dict
, kSCDSessionKeys
);
138 if (keys
&& ((keyCnt
= CFArrayGetCount(keys
)) > 0)) {
143 * if necessary, claim a lock to ensure that we inform
144 * any processes that a session key was removed.
146 wasLocked
= (storeLocked
> 0);
148 (void) __SCDynamicStoreLock(*store
, FALSE
);
151 /* remove keys from "locked" store" */
152 for (i
= 0; i
< keyCnt
; i
++) {
153 if (isMySessionKey(sessionKey
, CFArrayGetValueAtIndex(keys
, i
))) {
154 (void) __SCDynamicStoreRemoveValue(*store
, CFArrayGetValueAtIndex(keys
, i
), TRUE
);
159 /* remove keys from "unlocked" store" */
160 _swapLockedStoreData();
161 for (i
= 0; i
< keyCnt
; i
++) {
162 if (isMySessionKey(sessionKey
, CFArrayGetValueAtIndex(keys
, i
)))
163 (void) __SCDynamicStoreRemoveValue(*store
, CFArrayGetValueAtIndex(keys
, i
), TRUE
);
165 _swapLockedStoreData();
169 * Note: everyone who calls __SCDynamicStoreClose() ends
170 * up removing this sessions dictionary. As such,
171 * we don't need to worry about the session keys.
174 CFRelease(sessionKey
);
176 /* release the lock */
177 if (storePrivate
->locked
) {
178 (void) __SCDynamicStoreUnlock(*store
, FALSE
);
182 * Remove the run loop source on the server port (for this
183 * client). Then, invalidate and release the port.
185 mySession
= getSession(storePrivate
->server
);
186 if (mySession
->serverRunLoopSource
) {
187 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
188 mySession
->serverRunLoopSource
,
189 kCFRunLoopDefaultMode
);
190 CFRelease(mySession
->serverRunLoopSource
);
192 CFMachPortInvalidate(mySession
->serverPort
);
193 CFRelease(mySession
->serverPort
);
195 storePrivate
->server
= MACH_PORT_NULL
;
205 _configclose(mach_port_t server
, int *sc_status
)
207 serverSessionRef mySession
= getSession(server
);
209 if (_configd_verbose
) {
210 SCLog(TRUE
, LOG_DEBUG
, CFSTR("Close session."));
211 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
215 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
222 *sc_status
= __SCDynamicStoreClose(&mySession
->store
, FALSE
);
223 if (*sc_status
!= kSCStatusOK
) {
224 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" __SCDynamicStoreClose(): %s"), SCErrorString(*sc_status
));
229 * Remove the session entry.
231 removeSession(server
);