]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configclose.c
configd-1061.120.2.tar.gz
[apple/configd.git] / configd.tproj / _configclose.c
1 /*
2 * Copyright (c) 2000, 2001, 2003, 2004, 2006-2012, 2015, 2016, 2019 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 static Boolean
40 isMySessionKey(CFStringRef sessionKey, CFStringRef key)
41 {
42 CFDictionaryRef dict;
43 CFStringRef storeSessionKey;
44
45 dict = CFDictionaryGetValue(storeData, key);
46 if (!dict) {
47 /* if key no longer exists */
48 return FALSE;
49 }
50
51 storeSessionKey = CFDictionaryGetValue(dict, kSCDSession);
52 if (!storeSessionKey) {
53 /* if this is not a session key */
54 return FALSE;
55 }
56
57 if (!CFEqual(sessionKey, storeSessionKey)) {
58 /* if this is not "my" session key */
59 return FALSE;
60 }
61
62 return TRUE;
63 }
64
65
66 static void
67 removeAllKeys(SCDynamicStoreRef store, Boolean isRegex)
68 {
69 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
70 CFArrayRef keys;
71 CFIndex n;
72
73 keys = isRegex ? storePrivate->patterns : storePrivate->keys;
74 n = (keys != NULL) ? CFArrayGetCount(keys) : 0;
75 if (n > 0) {
76 CFIndex i;
77 CFArrayRef keysToRemove;
78
79 keysToRemove = CFArrayCreateCopy(NULL, keys);
80 for (i = 0; i < n; i++) {
81 (void) __SCDynamicStoreRemoveWatchedKey(store,
82 CFArrayGetValueAtIndex(keysToRemove, i),
83 isRegex,
84 TRUE);
85 }
86 CFRelease(keysToRemove);
87 }
88
89 return;
90 }
91
92
93 __private_extern__
94 int
95 __SCDynamicStoreClose(SCDynamicStoreRef *store)
96 {
97 serverSessionRef mySession;
98 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)*store;
99
100 SC_trace("close : %5d",
101 storePrivate->server);
102
103 /* Remove all notification keys and patterns */
104 removeAllKeys(*store, FALSE); // keys
105 removeAllKeys(*store, TRUE); // patterns
106
107 /* Remove/cancel any outstanding notification requests. */
108 __MACH_PORT_DEBUG(storePrivate->notifyPort != MACH_PORT_NULL, "*** __SCDynamicStoreClose", storePrivate->notifyPort);
109 (void) __SCDynamicStoreNotifyCancel(*store);
110
111 /* Remove any session keys */
112 mySession = getSession(storePrivate->server);
113 if (mySession->sessionKeys != NULL) {
114 CFIndex n = CFArrayGetCount(mySession->sessionKeys);
115 Boolean push = FALSE;
116 CFStringRef sessionKey;
117
118 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
119 for (CFIndex i = 0; i < n; i++) {
120 CFStringRef key = CFArrayGetValueAtIndex(mySession->sessionKeys, i);
121
122 if (isMySessionKey(sessionKey, key)) {
123 (void) __SCDynamicStoreRemoveValue(*store, key, TRUE);
124 push = TRUE;
125 }
126 }
127 CFRelease(sessionKey);
128
129 if (push) {
130 /* push changes */
131 (void) __SCDynamicStorePush();
132 }
133 }
134
135 storePrivate->server = MACH_PORT_NULL;
136
137 CFRelease(*store);
138 *store = NULL;
139
140 return kSCStatusOK;
141 }