]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configremove.c
configd-42.tar.gz
[apple/configd.git] / configd.tproj / _configremove.c
1 /*
2 * Copyright (c) 2000 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 int
37 __SCDynamicStoreRemoveValue(SCDynamicStoreRef store, CFStringRef key)
38 {
39 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
40 int sc_status = kSCStatusOK;
41 CFDictionaryRef dict;
42 CFMutableDictionaryRef newDict;
43 CFStringRef sessionKey;
44
45 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreRemoveValue:"));
46 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" key = %@"), key);
47
48 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
49 return kSCStatusNoStoreSession; /* you must have an open session to play */
50 }
51
52 /*
53 * 1. Ensure that we hold the lock.
54 */
55 sc_status = __SCDynamicStoreLock(store, TRUE);
56 if (sc_status != kSCStatusOK) {
57 return sc_status;
58 }
59
60 /*
61 * 2. Ensure that this key exists.
62 */
63 dict = CFDictionaryGetValue(storeData, key);
64 if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) {
65 /* key doesn't exist (or data never defined) */
66 sc_status = kSCStatusNoKey;
67 goto done;
68 }
69 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
70
71 /*
72 * 3. Mark this key as "changed". Any "watchers" will be
73 * notified as soon as the lock is released.
74 */
75 CFSetAddValue(changedKeys, key);
76
77 /*
78 * 4. Add this key to a deferred cleanup list so that, after
79 * the change notifications are posted, any associated
80 * regex keys can be removed.
81 */
82 CFSetAddValue(deferredRemovals, key);
83
84 /*
85 * 5. Check if this is a session key and, if so, add it
86 * to the (session) removal list
87 */
88 sessionKey = CFDictionaryGetValue(newDict, kSCDSession);
89 if (sessionKey) {
90 CFStringRef removedKey;
91
92 /* We are no longer a session key! */
93 CFDictionaryRemoveValue(newDict, kSCDSession);
94
95 /* add this session key to the (session) removal list */
96 removedKey = CFStringCreateWithFormat(NULL, 0, CFSTR("%@:%@"), sessionKey, key);
97 CFSetAddValue(removedSessionKeys, removedKey);
98 CFRelease(removedKey);
99 }
100
101 /*
102 * 6. Remove data, remove instance, and update/remove
103 * the dictionary store entry.
104 */
105 CFDictionaryRemoveValue(newDict, kSCDData);
106 CFDictionaryRemoveValue(newDict, kSCDInstance);
107 if (CFDictionaryGetCount(newDict) > 0) {
108 /* this key is still being "watched" */
109 CFDictionarySetValue(storeData, key, newDict);
110 } else {
111 /* no information left, remove the empty dictionary */
112 CFDictionaryRemoveValue(storeData, key);
113 }
114 CFRelease(newDict);
115
116 /*
117 * 7. Release our lock.
118 */
119 done:
120 __SCDynamicStoreUnlock(store, TRUE);
121
122 return sc_status;
123 }
124
125
126 kern_return_t
127 _configremove(mach_port_t server,
128 xmlData_t keyRef, /* raw XML bytes */
129 mach_msg_type_number_t keyLen,
130 int *sc_status
131 )
132 {
133 kern_return_t status;
134 serverSessionRef mySession = getSession(server);
135 CFDataRef xmlKey; /* key (XML serialized) */
136 CFStringRef key; /* key (un-serialized) */
137 CFStringRef xmlError;
138
139 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Remove key from configuration database."));
140 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server);
141
142 /* un-serialize the key */
143 xmlKey = CFDataCreate(NULL, keyRef, keyLen);
144 status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
145 if (status != KERN_SUCCESS) {
146 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
147 /* non-fatal???, proceed */
148 }
149 key = CFPropertyListCreateFromXMLData(NULL,
150 xmlKey,
151 kCFPropertyListImmutable,
152 &xmlError);
153 CFRelease(xmlKey);
154 if (!key) {
155 if (xmlError) {
156 SCLog(_configd_verbose, LOG_DEBUG,
157 CFSTR("CFPropertyListCreateFromXMLData() key: %@"),
158 xmlError);
159 CFRelease(xmlError);
160 }
161 *sc_status = kSCStatusFailed;
162 return KERN_SUCCESS;
163 } else if (!isA_CFString(key)) {
164 *sc_status = kSCStatusInvalidArgument;
165 return KERN_SUCCESS;
166 }
167
168 *sc_status = __SCDynamicStoreRemoveValue(mySession->store, key);
169 CFRelease(key);
170
171 return KERN_SUCCESS;
172 }
173