]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configclose.c
configd-453.16.tar.gz
[apple/configd.git] / configd.tproj / _configclose.c
1 /*
2 * Copyright (c) 2000, 2001, 2003, 2004, 2006-2011 Apple 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 CFArrayRef keys;
73 CFIndex n;
74
75 keys = isRegex ? storePrivate->patterns : storePrivate->keys;
76 n = (keys != NULL) ? CFArrayGetCount(keys) : 0;
77 if (n > 0) {
78 CFIndex i;
79 CFArrayRef keysToRemove;
80
81 keysToRemove = CFArrayCreateCopy(NULL, keys);
82 for (i = 0; i < n; i++) {
83 (void) __SCDynamicStoreRemoveWatchedKey(store,
84 CFArrayGetValueAtIndex(keysToRemove, i),
85 isRegex,
86 TRUE);
87 }
88 CFRelease(keysToRemove);
89 }
90
91 return;
92 }
93
94
95 __private_extern__
96 int
97 __SCDynamicStoreClose(SCDynamicStoreRef *store)
98 {
99 CFDictionaryRef dict;
100 CFArrayRef keys;
101 CFIndex keyCnt;
102 serverSessionRef mySession;
103 CFStringRef sessionKey;
104 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)*store;
105
106 if (_configd_trace) {
107 SCTrace(TRUE, _configd_trace,
108 CFSTR("close : %5d\n"),
109 storePrivate->server);
110 }
111
112 /* Remove all notification keys and patterns */
113 removeAllKeys(*store, FALSE); // keys
114 removeAllKeys(*store, TRUE); // patterns
115
116 /* Remove/cancel any outstanding notification requests. */
117 __MACH_PORT_DEBUG(storePrivate->notifyPort != MACH_PORT_NULL, "*** __SCDynamicStoreClose", storePrivate->notifyPort);
118 (void) __SCDynamicStoreNotifyCancel(*store);
119
120 /* Remove any session keys */
121 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
122 dict = CFDictionaryGetValue(sessionData, sessionKey);
123 keys = CFDictionaryGetValue(dict, kSCDSessionKeys);
124 if (keys && ((keyCnt = CFArrayGetCount(keys)) > 0)) {
125 CFIndex i;
126 Boolean push = FALSE;
127
128 /* remove session keys */
129 for (i = 0; i < keyCnt; i++) {
130 if (isMySessionKey(sessionKey, CFArrayGetValueAtIndex(keys, i))) {
131 (void) __SCDynamicStoreRemoveValue(*store, CFArrayGetValueAtIndex(keys, i), TRUE);
132 push = TRUE;
133 }
134 }
135
136 if (push) {
137 /* push changes */
138 (void) __SCDynamicStorePush();
139 }
140 }
141 CFRelease(sessionKey);
142
143 /*
144 * invalidate and release our run loop source on the server
145 * port (for this client). Then, release the port.
146 */
147 mySession = getSession(storePrivate->server);
148 if (mySession->serverRunLoopSource) {
149 CFRunLoopSourceInvalidate(mySession->serverRunLoopSource);
150 CFRelease(mySession->serverRunLoopSource);
151 mySession->serverRunLoopSource = NULL;
152 }
153 if (mySession->serverPort != NULL) {
154 CFMachPortInvalidate(mySession->serverPort);
155 CFRelease(mySession->serverPort);
156 mySession->serverPort = NULL;
157 }
158
159 storePrivate->server = MACH_PORT_NULL;
160 CFRelease(*store);
161 *store = NULL;
162
163 return kSCStatusOK;
164 }