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