2 * Copyright (c) 2000, 2001, 2003, 2004, 2006-2009 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>
42 isMySessionKey(CFStringRef sessionKey
, CFStringRef key
)
45 CFStringRef storeSessionKey
;
47 dict
= CFDictionaryGetValue(storeData
, key
);
49 /* if key no longer exists */
53 storeSessionKey
= CFDictionaryGetValue(dict
, kSCDSession
);
54 if (!storeSessionKey
) {
55 /* if this is not a session key */
59 if (!CFEqual(sessionKey
, storeSessionKey
)) {
60 /* if this is not "my" session key */
69 removeAllKeys(SCDynamicStoreRef store
, Boolean isRegex
)
71 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
75 keys
= isRegex
? storePrivate
->patterns
: storePrivate
->keys
;
76 n
= CFSetGetCount(keys
);
79 CFArrayRef keysToRemove
;
80 const void * watchedKeys_q
[N_QUICK
];
81 const void ** watchedKeys
= watchedKeys_q
;
83 if (n
> (CFIndex
)(sizeof(watchedKeys_q
) / sizeof(CFStringRef
)))
84 watchedKeys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
85 CFSetGetValues(keys
, watchedKeys
);
86 keysToRemove
= CFArrayCreate(NULL
, watchedKeys
, n
, &kCFTypeArrayCallBacks
);
87 if (watchedKeys
!= watchedKeys_q
) CFAllocatorDeallocate(NULL
, watchedKeys
);
88 for (i
= 0; i
< n
; i
++) {
89 (void) __SCDynamicStoreRemoveWatchedKey(store
,
90 CFArrayGetValueAtIndex(keysToRemove
, i
),
94 CFRelease(keysToRemove
);
103 __SCDynamicStoreClose(SCDynamicStoreRef
*store
, Boolean internal
)
105 CFDictionaryRef dict
;
108 serverSessionRef mySession
;
109 CFStringRef sessionKey
;
110 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)*store
;
112 if ((*store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
113 return kSCStatusNoStoreSession
; /* you must have an open session to play */
116 if (_configd_trace
) {
117 SCTrace(TRUE
, _configd_trace
,
119 internal
? "*close " : "close ",
120 storePrivate
->server
);
123 /* Remove all notification keys and patterns */
124 removeAllKeys(*store
, FALSE
); // keys
125 removeAllKeys(*store
, TRUE
); // patterns
127 /* Remove/cancel any outstanding notification requests. */
128 __MACH_PORT_DEBUG(storePrivate
->notifyPort
!= MACH_PORT_NULL
, "*** __SCDynamicStoreClose", storePrivate
->notifyPort
);
129 (void) __SCDynamicStoreNotifyCancel(*store
);
131 /* Remove any session keys */
132 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), storePrivate
->server
);
133 dict
= CFDictionaryGetValue(sessionData
, sessionKey
);
134 keys
= CFDictionaryGetValue(dict
, kSCDSessionKeys
);
135 if (keys
&& ((keyCnt
= CFArrayGetCount(keys
)) > 0)) {
140 * if necessary, claim a lock to ensure that we inform
141 * any processes that a session key was removed.
143 wasLocked
= (storeLocked
> 0);
145 (void) __SCDynamicStoreLock(*store
, FALSE
);
148 /* remove keys from "locked" store" */
149 for (i
= 0; i
< keyCnt
; i
++) {
150 if (isMySessionKey(sessionKey
, CFArrayGetValueAtIndex(keys
, i
))) {
151 (void) __SCDynamicStoreRemoveValue(*store
, CFArrayGetValueAtIndex(keys
, i
), TRUE
);
156 /* remove keys from "unlocked" store" */
157 _swapLockedStoreData();
158 for (i
= 0; i
< keyCnt
; i
++) {
159 if (isMySessionKey(sessionKey
, CFArrayGetValueAtIndex(keys
, i
)))
160 (void) __SCDynamicStoreRemoveValue(*store
, CFArrayGetValueAtIndex(keys
, i
), TRUE
);
162 _swapLockedStoreData();
166 * Note: everyone who calls __SCDynamicStoreClose() ends
167 * up removing this sessions dictionary. As such,
168 * we don't need to worry about the session keys.
171 CFRelease(sessionKey
);
173 /* release the lock */
174 if (storePrivate
->locked
) {
175 (void) __SCDynamicStoreUnlock(*store
, FALSE
);
179 * invalidate and release our run loop source on the server
180 * port (for this client). Then, release the port.
182 mySession
= getSession(storePrivate
->server
);
183 if (mySession
->serverRunLoopSource
) {
184 CFRunLoopSourceInvalidate(mySession
->serverRunLoopSource
);
185 CFRelease(mySession
->serverRunLoopSource
);
186 mySession
->serverRunLoopSource
= NULL
;
188 if (mySession
->serverPort
!= NULL
) {
189 CFMachPortInvalidate(mySession
->serverPort
);
190 CFRelease(mySession
->serverPort
);
191 mySession
->serverPort
= NULL
;
194 storePrivate
->server
= MACH_PORT_NULL
;
204 _configclose(mach_port_t server
, int *sc_status
)
206 serverSessionRef mySession
= getSession(server
);
208 if (mySession
== NULL
) {
209 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
216 __MACH_PORT_DEBUG(TRUE
, "*** _configclose", server
);
217 *sc_status
= __SCDynamicStoreClose(&mySession
->store
, FALSE
);
218 if (*sc_status
!= kSCStatusOK
) {
220 CFSTR("_configclose __SCDynamicStoreClose() failed, status = %s"),
221 SCErrorString(*sc_status
));
224 __MACH_PORT_DEBUG(TRUE
, "*** _configclose (after __SCDynamicStoreClose)", server
);
227 * Remove our receive right.
229 * Note: there is no need to cancel the notification request because the
230 * kernel will have no way to deliver the notification once the
231 * receive right has been removed.
233 (void) mach_port_mod_refs(mach_task_self(), server
, MACH_PORT_RIGHT_RECEIVE
, -1);
236 * Remove the session entry.
238 removeSession(server
);