]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configclose.c
configd-84.tar.gz
[apple/configd.git] / configd.tproj / _configclose.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include <unistd.h>
37
38 #include "configd.h"
39 #include "session.h"
40
41 #define N_QUICK 16
42
43 static Boolean
44 isMySessionKey(CFStringRef sessionKey, CFStringRef key)
45 {
46 CFDictionaryRef dict;
47 CFStringRef storeSessionKey;
48
49 dict = CFDictionaryGetValue(storeData, key);
50 if (!dict) {
51 /* if key no longer exists */
52 return FALSE;
53 }
54
55 storeSessionKey = CFDictionaryGetValue(dict, kSCDSession);
56 if (!storeSessionKey) {
57 /* if this is not a session key */
58 return FALSE;
59 }
60
61 if (!CFEqual(sessionKey, storeSessionKey)) {
62 /* if this is not "my" session key */
63 return FALSE;
64 }
65
66 return TRUE;
67 }
68
69
70 static void
71 removeAllKeys(SCDynamicStoreRef store, Boolean isRegex)
72 {
73 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
74 CFSetRef keys;
75 CFIndex n;
76
77 keys = isRegex ? storePrivate->patterns : storePrivate->keys;
78 n = CFSetGetCount(keys);
79 if (n > 0) {
80 CFIndex i;
81 CFArrayRef keysToRemove;
82 const void * watchedKeys_q[N_QUICK];
83 const void ** watchedKeys = watchedKeys_q;
84
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),
93 isRegex,
94 TRUE);
95 }
96 CFRelease(keysToRemove);
97 }
98
99 return;
100 }
101
102
103 __private_extern__
104 int
105 __SCDynamicStoreClose(SCDynamicStoreRef *store, Boolean internal)
106 {
107 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)*store;
108 CFIndex keyCnt;
109 CFStringRef sessionKey;
110 CFDictionaryRef dict;
111 CFArrayRef keys;
112 serverSessionRef mySession;
113
114 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreClose:"));
115
116 if ((*store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
117 return kSCStatusNoStoreSession; /* you must have an open session to play */
118 }
119
120 if (_configd_trace) {
121 SCTrace(TRUE, _configd_trace,
122 CFSTR("%s : %5d\n"),
123 internal ? "*close " : "close ",
124 storePrivate->server);
125 }
126
127 /* Remove all notification keys and patterns */
128 removeAllKeys(*store, FALSE); // keys
129 removeAllKeys(*store, TRUE); // patterns
130
131 /* Remove/cancel any outstanding notification requests. */
132 (void) __SCDynamicStoreNotifyCancel(*store);
133
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)) {
139 Boolean wasLocked;
140 CFIndex i;
141
142 /*
143 * if necessary, claim a lock to ensure that we inform
144 * any processes that a session key was removed.
145 */
146 wasLocked = (storeLocked > 0);
147 if (!wasLocked) {
148 (void) __SCDynamicStoreLock(*store, FALSE);
149 }
150
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);
155 }
156 }
157
158 if (wasLocked) {
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);
164 }
165 _swapLockedStoreData();
166 }
167
168 /*
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.
172 */
173 }
174 CFRelease(sessionKey);
175
176 /* release the lock */
177 if (storePrivate->locked) {
178 (void) __SCDynamicStoreUnlock(*store, FALSE);
179 }
180
181 /*
182 * Remove the run loop source on the server port (for this
183 * client). Then, invalidate and release the port.
184 */
185 mySession = getSession(storePrivate->server);
186 if (mySession->serverRunLoopSource) {
187 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
188 mySession->serverRunLoopSource,
189 kCFRunLoopDefaultMode);
190 CFRelease(mySession->serverRunLoopSource);
191 }
192 CFMachPortInvalidate(mySession->serverPort);
193 CFRelease(mySession->serverPort);
194
195 storePrivate->server = MACH_PORT_NULL;
196 CFRelease(*store);
197 *store = NULL;
198
199 return kSCStatusOK;
200 }
201
202
203 __private_extern__
204 kern_return_t
205 _configclose(mach_port_t server, int *sc_status)
206 {
207 serverSessionRef mySession = getSession(server);
208
209 if (_configd_verbose) {
210 SCLog(TRUE, LOG_DEBUG, CFSTR("Close session."));
211 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
212 }
213
214 if (!mySession) {
215 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
216 return KERN_SUCCESS;
217 }
218
219 /*
220 * Close the session.
221 */
222 *sc_status = __SCDynamicStoreClose(&mySession->store, FALSE);
223 if (*sc_status != kSCStatusOK) {
224 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" __SCDynamicStoreClose(): %s"), SCErrorString(*sc_status));
225 return KERN_SUCCESS;
226 }
227
228 /*
229 * Remove the session entry.
230 */
231 removeSession(server);
232
233 return KERN_SUCCESS;
234 }