]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd_s.c
configd-84.6.tar.gz
[apple/configd.git] / configd.tproj / _configadd_s.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, 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 * October 17, 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 __SCDynamicStoreAddTemporaryValue(SCDynamicStoreRef store, CFStringRef key, CFDataRef value)
40 {
41 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
42 int sc_status = kSCStatusOK;
43 CFStringRef sessionKey;
44 CFDictionaryRef dict;
45 CFMutableDictionaryRef newDict;
46 CFArrayRef keys;
47 CFMutableArrayRef newKeys;
48
49 if (_configd_verbose) {
50 CFPropertyListRef val;
51
52 (void) _SCUnserialize(&val, value, NULL, NULL);
53 SCLog(TRUE, LOG_DEBUG, CFSTR("__SCDynamicStoreAddTemporaryValue:"));
54 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
55 SCLog(TRUE, LOG_DEBUG, CFSTR(" value = %@"), val);
56 CFRelease(val);
57 }
58
59 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
60 return kSCStatusNoStoreSession; /* you must have an open session to play */
61 }
62
63 /*
64 * 1. Add the key
65 */
66 sc_status = __SCDynamicStoreAddValue(store, key, value);
67 if (sc_status != kSCStatusOK) {
68 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" __SCDynamicStoreAddValue(): %s"), SCErrorString(sc_status));
69 return sc_status;
70 }
71
72 /*
73 * 2. Create the session key
74 */
75 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
76
77 /*
78 * 3. Add this key to my list of per-session keys
79 */
80 dict = CFDictionaryGetValue(sessionData, sessionKey);
81 keys = CFDictionaryGetValue(dict, kSCDSessionKeys);
82 if ((keys == NULL) ||
83 (CFArrayGetFirstIndexOfValue(keys,
84 CFRangeMake(0, CFArrayGetCount(keys)),
85 key) == -1)) {
86 /*
87 * if no session keys defined "or" keys defined but not
88 * this one...
89 */
90 if (keys) {
91 /* this is a new session key */
92 newKeys = CFArrayCreateMutableCopy(NULL, 0, keys);
93 } else {
94 /* this is an additional session key */
95 newKeys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
96 }
97 CFArrayAppendValue(newKeys, key);
98
99 /* update session dictionary */
100 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
101 CFDictionarySetValue(newDict, kSCDSessionKeys, newKeys);
102 CFRelease(newKeys);
103 CFDictionarySetValue(sessionData, sessionKey, newDict);
104 CFRelease(newDict);
105 }
106
107 /*
108 * 4. Mark the key as a "session" key and track the creator.
109 */
110 dict = CFDictionaryGetValue(storeData, key);
111 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
112 CFDictionarySetValue(newDict, kSCDSession, sessionKey);
113 CFDictionarySetValue(storeData, key, newDict);
114 CFRelease(newDict);
115
116 CFRelease(sessionKey);
117 return sc_status;
118 }
119
120
121 __private_extern__
122 kern_return_t
123 _configadd_s(mach_port_t server,
124 xmlData_t keyRef, /* raw XML bytes */
125 mach_msg_type_number_t keyLen,
126 xmlData_t dataRef, /* raw XML bytes */
127 mach_msg_type_number_t dataLen,
128 int *newInstance,
129 int *sc_status
130 )
131 {
132 serverSessionRef mySession = getSession(server);
133 CFStringRef key; /* key (un-serialized) */
134 CFDataRef data; /* data (un-serialized) */
135
136 if (_configd_verbose) {
137 SCLog(TRUE, LOG_DEBUG, CFSTR("Add (session) key to configuration database."));
138 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
139 }
140
141 *sc_status = kSCStatusOK;
142
143 /* un-serialize the key */
144 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
145 *sc_status = kSCStatusFailed;
146 } else if (!isA_CFString(key)) {
147 *sc_status = kSCStatusInvalidArgument;
148 }
149
150 /* un-serialize the data */
151 if (!_SCUnserializeData(&data, (void *)dataRef, dataLen)) {
152 *sc_status = kSCStatusFailed;
153 }
154
155 if (!mySession) {
156 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
157 }
158
159 if (*sc_status != kSCStatusOK) {
160 if (key) CFRelease(key);
161 if (data) CFRelease(data);
162 return KERN_SUCCESS;
163 }
164
165 *sc_status = __SCDynamicStoreAddTemporaryValue(mySession->store, key, data);
166 if (*sc_status == kSCStatusOK) {
167 *newInstance = 1;
168 }
169 CFRelease(key);
170 CFRelease(data);
171
172 return KERN_SUCCESS;
173 }