]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configremove.c
7a37c8ca3839bb76052de9ca2615d1dd96f859a2
[apple/configd.git] / configd.tproj / _configremove.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include "configd.h"
37 #include "session.h"
38
39 __private_extern__
40 int
41 __SCDynamicStoreRemoveValue(SCDynamicStoreRef store, CFStringRef key, Boolean internal)
42 {
43 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
44 int sc_status = kSCStatusOK;
45 CFDictionaryRef dict;
46 CFMutableDictionaryRef newDict;
47 CFStringRef sessionKey;
48
49 if (_configd_verbose) {
50 SCLog(TRUE, LOG_DEBUG, CFSTR("__SCDynamicStoreRemoveValue:"));
51 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
52 }
53
54 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
55 return kSCStatusNoStoreSession; /* you must have an open session to play */
56 }
57
58 if (_configd_trace) {
59 SCTrace(TRUE, _configd_trace,
60 CFSTR("%s : %5d : %@\n"),
61 internal ? "*remove" : "remove ",
62 storePrivate->server,
63 key);
64 }
65
66 /*
67 * 1. Ensure that we hold the lock.
68 */
69 sc_status = __SCDynamicStoreLock(store, TRUE);
70 if (sc_status != kSCStatusOK) {
71 return sc_status;
72 }
73
74 /*
75 * 2. Ensure that this key exists.
76 */
77 dict = CFDictionaryGetValue(storeData, key);
78 if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) {
79 /* key doesn't exist (or data never defined) */
80 sc_status = kSCStatusNoKey;
81 goto done;
82 }
83 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
84
85 /*
86 * 3. Mark this key as "changed". Any "watchers" will be
87 * notified as soon as the lock is released.
88 */
89 CFSetAddValue(changedKeys, key);
90
91 /*
92 * 4. Add this key to a deferred cleanup list so that, after
93 * the change notifications are posted, any associated
94 * regex keys can be removed.
95 */
96 CFSetAddValue(deferredRemovals, key);
97
98 /*
99 * 5. Check if this is a session key and, if so, add it
100 * to the (session) removal list
101 */
102 sessionKey = CFDictionaryGetValue(newDict, kSCDSession);
103 if (sessionKey) {
104 CFStringRef removedKey;
105
106 /* We are no longer a session key! */
107 CFDictionaryRemoveValue(newDict, kSCDSession);
108
109 /* add this session key to the (session) removal list */
110 removedKey = CFStringCreateWithFormat(NULL, 0, CFSTR("%@:%@"), sessionKey, key);
111 CFSetAddValue(removedSessionKeys, removedKey);
112 CFRelease(removedKey);
113 }
114
115 /*
116 * 6. Remove data and update/remove the dictionary store entry.
117 */
118 CFDictionaryRemoveValue(newDict, kSCDData);
119 if (CFDictionaryGetCount(newDict) > 0) {
120 /* this key is still being "watched" */
121 CFDictionarySetValue(storeData, key, newDict);
122 } else {
123 /* no information left, remove the empty dictionary */
124 CFDictionaryRemoveValue(storeData, key);
125 }
126 CFRelease(newDict);
127
128 /*
129 * 7. Release our lock.
130 */
131 done:
132 __SCDynamicStoreUnlock(store, TRUE);
133
134 return sc_status;
135 }
136
137
138 __private_extern__
139 kern_return_t
140 _configremove(mach_port_t server,
141 xmlData_t keyRef, /* raw XML bytes */
142 mach_msg_type_number_t keyLen,
143 int *sc_status
144 )
145 {
146 serverSessionRef mySession = getSession(server);
147 CFStringRef key; /* key (un-serialized) */
148
149 if (_configd_verbose) {
150 SCLog(TRUE, LOG_DEBUG, CFSTR("Remove key from configuration database."));
151 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
152 }
153
154 /* un-serialize the key */
155 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
156 *sc_status = kSCStatusFailed;
157 return KERN_SUCCESS;
158 }
159
160 if (!isA_CFString(key)) {
161 *sc_status = kSCStatusInvalidArgument;
162 CFRelease(key);
163 return KERN_SUCCESS;
164 }
165
166 if (!mySession) {
167 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
168 CFRelease(key);
169 return KERN_SUCCESS;
170 }
171
172 *sc_status = __SCDynamicStoreRemoveValue(mySession->store, key, FALSE);
173 CFRelease(key);
174
175 return KERN_SUCCESS;
176 }
177