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