]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyremove.c
configd-395.11.tar.gz
[apple/configd.git] / configd.tproj / _notifyremove.c
1 /*
2 * Copyright (c) 2000-2004, 2006, 2008, 2010 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
35 #include "configd.h"
36 #include "session.h"
37 #include "pattern.h"
38
39
40 static int
41 removeKey(CFMutableArrayRef keys, CFStringRef key)
42 {
43 CFIndex i;
44 CFIndex n;
45
46 if (keys == NULL) {
47 /* sorry, empty notifier list */
48 return kSCStatusNoKey;
49 }
50
51 n = CFArrayGetCount(keys);
52 i = CFArrayGetFirstIndexOfValue(keys, CFRangeMake(0, n), key);
53 if (i == kCFNotFound) {
54 /* sorry, key does not exist in notifier list */
55 return kSCStatusNoKey;
56 }
57
58 /* remove key from this sessions notifier list */
59 CFArrayRemoveValueAtIndex(keys, i);
60 return kSCStatusOK;
61 }
62
63
64 __private_extern__
65 int
66 __SCDynamicStoreRemoveWatchedKey(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex, Boolean internal)
67 {
68 int sc_status = kSCStatusOK;
69 CFNumberRef sessionNum;
70 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
71
72 if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
73 return kSCStatusNoStoreSession; /* you must have an open session to play */
74 }
75
76 if (_configd_trace) {
77 SCTrace(TRUE, _configd_trace,
78 CFSTR("%s : %5d : %s : %@\n"),
79 internal ? "*watch-" : "watch- ",
80 storePrivate->server,
81 isRegex ? "pattern" : "key",
82 key);
83 }
84
85 /*
86 * remove key from this sessions notifier list after checking that
87 * it was previously defined.
88 */
89 if (isRegex) {
90 sc_status = removeKey(storePrivate->patterns, key);
91 if (sc_status != kSCStatusOK) {
92 goto done;
93 }
94
95 /* remove this session as a pattern watcher */
96 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
97 patternRemoveSession(key, sessionNum);
98 CFRelease(sessionNum);
99 } else {
100 sc_status = removeKey(storePrivate->keys, key);
101 if (sc_status != kSCStatusOK) {
102 goto done;
103 }
104
105 /*
106 * We are watching a specific key. As such, update the
107 * store to mark our interest in any changes.
108 */
109 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
110 _removeWatcher(sessionNum, key);
111 CFRelease(sessionNum);
112 }
113
114 done :
115
116 return sc_status;
117 }
118
119
120 __private_extern__
121 kern_return_t
122 _notifyremove(mach_port_t server,
123 xmlData_t keyRef, /* raw XML bytes */
124 mach_msg_type_number_t keyLen,
125 int isRegex,
126 int *sc_status
127 )
128 {
129 CFStringRef key = NULL; /* key (un-serialized) */
130 serverSessionRef mySession;
131
132 /* un-serialize the key */
133 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
134 *sc_status = kSCStatusFailed;
135 goto done;
136 }
137
138 if (!isA_CFString(key)) {
139 *sc_status = kSCStatusInvalidArgument;
140 goto done;
141 }
142
143 mySession = getSession(server);
144 if (mySession == NULL) {
145 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
146 goto done;
147 }
148
149 *sc_status = __SCDynamicStoreRemoveWatchedKey(mySession->store,
150 key,
151 isRegex != 0,
152 FALSE);
153
154 done :
155
156 if (key) CFRelease(key);
157 return KERN_SUCCESS;
158 }
159