]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyremove.c
configd-53.tar.gz
[apple/configd.git] / configd.tproj / _notifyremove.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * Modification History
25 *
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
28 *
29 * March 24, 2000 Allan Nathanson <ajn@apple.com>
30 * - initial revision
31 */
32
33 #include "configd.h"
34 #include "session.h"
35
36
37 static __inline__ void
38 my_CFDictionaryApplyFunction(CFDictionaryRef theDict,
39 CFDictionaryApplierFunction applier,
40 void *context)
41 {
42 CFAllocatorRef myAllocator;
43 CFDictionaryRef myDict;
44
45 myAllocator = CFGetAllocator(theDict);
46 myDict = CFDictionaryCreateCopy(myAllocator, theDict);
47 CFDictionaryApplyFunction(myDict, applier, context);
48 CFRelease(myDict);
49 return;
50 }
51
52
53 int
54 __SCDynamicStoreRemoveWatchedKey(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex)
55 {
56 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
57
58 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreRemoveWatchedKey:"));
59 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" key = %@"), key);
60 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" isRegex = %s"), isRegex ? "TRUE" : "FALSE");
61
62 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
63 return kSCStatusNoStoreSession; /* you must have an open session to play */
64 }
65
66 /*
67 * remove key from this sessions notifier list after checking that
68 * it was previously defined.
69 */
70 if (isRegex) {
71 if (!CFSetContainsValue(storePrivate->reKeys, key))
72 return kSCStatusNoKey; /* sorry, key does not exist in notifier list */
73 CFSetRemoveValue(storePrivate->reKeys, key); /* remove key from this sessions notifier list */
74 } else {
75 if (!CFSetContainsValue(storePrivate->keys, key))
76 return kSCStatusNoKey; /* sorry, key does not exist in notifier list */
77 CFSetRemoveValue(storePrivate->keys, key); /* remove key from this sessions notifier list */
78 }
79
80 if (isRegex) {
81 CFStringRef sessionKey;
82 CFDictionaryRef info;
83 CFMutableDictionaryRef newInfo;
84 CFArrayRef rKeys;
85 CFMutableArrayRef newRKeys;
86 CFArrayRef rData;
87 CFMutableArrayRef newRData;
88 CFIndex i;
89 CFDataRef regexData;
90 removeContext context;
91
92 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
93
94 info = CFDictionaryGetValue(sessionData, sessionKey);
95 newInfo = CFDictionaryCreateMutableCopy(NULL, 0, info);
96
97 rKeys = CFDictionaryGetValue(newInfo, kSCDRegexKeys);
98 newRKeys = CFArrayCreateMutableCopy(NULL, 0, rKeys);
99
100 rData = CFDictionaryGetValue(newInfo, kSCDRegexData);
101 newRData = CFArrayCreateMutableCopy(NULL, 0, rData);
102
103 i = CFArrayGetFirstIndexOfValue(newRKeys,
104 CFRangeMake(0, CFArrayGetCount(newRData)),
105 key);
106 regexData = CFArrayGetValueAtIndex(newRData, i);
107
108 context.store = storePrivate;
109 context.preg = (regex_t *)CFDataGetBytePtr(regexData);
110 my_CFDictionaryApplyFunction(storeData,
111 (CFDictionaryApplierFunction)_removeRegexWatcherByKey,
112 &context);
113
114 /* remove the regex key */
115 CFArrayRemoveValueAtIndex(newRKeys, i);
116 if (CFArrayGetCount(newRKeys) > 0) {
117 CFDictionarySetValue(newInfo, kSCDRegexKeys, newRKeys);
118 } else {
119 CFDictionaryRemoveValue(newInfo, kSCDRegexKeys);
120 }
121 CFRelease(newRKeys);
122
123 /* ...and the compiled expression */
124 regfree((regex_t *)CFDataGetBytePtr(regexData));
125 CFArrayRemoveValueAtIndex(newRData, i);
126 if (CFArrayGetCount(newRData) > 0) {
127 CFDictionarySetValue(newInfo, kSCDRegexData, newRData);
128 } else {
129 CFDictionaryRemoveValue(newInfo, kSCDRegexData);
130 }
131 CFRelease(newRData);
132
133 /* save the updated session data */
134 CFDictionarySetValue(sessionData, sessionKey, newInfo);
135 CFRelease(newInfo);
136
137 CFRelease(sessionKey);
138 } else {
139 CFNumberRef sessionNum;
140
141 /*
142 * We are watching a specific key. As such, update the
143 * store to mark our interest in any changes.
144 */
145
146 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
147 _removeWatcher(sessionNum, key);
148 CFRelease(sessionNum);
149 }
150
151 return kSCStatusOK;
152 }
153
154
155 kern_return_t
156 _notifyremove(mach_port_t server,
157 xmlData_t keyRef, /* raw XML bytes */
158 mach_msg_type_number_t keyLen,
159 int isRegex,
160 int *sc_status
161 )
162 {
163 serverSessionRef mySession = getSession(server);
164 CFStringRef key; /* key (un-serialized) */
165
166 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Remove notification key for this session."));
167 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server);
168
169 /* un-serialize the key */
170 if (!_SCUnserialize((CFPropertyListRef *)&key, (void *)keyRef, keyLen)) {
171 *sc_status = kSCStatusFailed;
172 return KERN_SUCCESS;
173 }
174
175 if (!isA_CFString(key)) {
176 CFRelease(key);
177 *sc_status = kSCStatusInvalidArgument;
178 return KERN_SUCCESS;
179 }
180
181 *sc_status = __SCDynamicStoreRemoveWatchedKey(mySession->store, key, isRegex);
182 CFRelease(key);
183
184 return KERN_SUCCESS;
185 }
186