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