]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd.c
configd-84.6.tar.gz
[apple/configd.git] / configd.tproj / _configadd.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 * 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 __SCDynamicStoreAddValue(SCDynamicStoreRef store, CFStringRef key, CFDataRef value)
40 {
41 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
42 int sc_status = kSCStatusOK;
43 CFDataRef tempValue;
44
45 if (_configd_verbose) {
46 CFPropertyListRef val;
47
48 (void) _SCUnserialize(&val, value, NULL, NULL);
49 SCLog(TRUE, LOG_DEBUG, CFSTR("__SCDynamicStoreAddValue:"));
50 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
51 SCLog(TRUE, LOG_DEBUG, CFSTR(" value = %@"), val);
52 CFRelease(val);
53 }
54
55 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
56 return kSCStatusNoStoreSession; /* you must have an open session to play */
57 }
58
59 if (_configd_trace) {
60 SCTrace(TRUE, _configd_trace, CFSTR("add : %5d : %@\n"), storePrivate->server, key);
61 }
62
63 /*
64 * 1. Ensure that we hold the lock.
65 */
66 sc_status = __SCDynamicStoreLock(store, TRUE);
67 if (sc_status != kSCStatusOK) {
68 return sc_status;
69 }
70
71 /*
72 * 2. Ensure that this is a new key.
73 */
74 sc_status = __SCDynamicStoreCopyValue(store, key, &tempValue, TRUE);
75 switch (sc_status) {
76 case kSCStatusNoKey :
77 /* store key does not exist, proceed */
78 break;
79
80 case kSCStatusOK :
81 /* store key exists, sorry */
82 CFRelease(tempValue);
83 sc_status = kSCStatusKeyExists;
84 goto done;
85
86 default :
87 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" __SCDynamicStoreCopyValue(): %s"), SCErrorString(sc_status));
88 goto done;
89 }
90
91 /*
92 * 3. Save the new key.
93 */
94 sc_status = __SCDynamicStoreSetValue(store, key, value, TRUE);
95
96 /*
97 * 4. Release our lock.
98 */
99 done:
100 __SCDynamicStoreUnlock(store, TRUE);
101
102 return sc_status;
103 }
104
105
106 __private_extern__
107 kern_return_t
108 _configadd(mach_port_t server,
109 xmlData_t keyRef, /* raw XML bytes */
110 mach_msg_type_number_t keyLen,
111 xmlData_t dataRef, /* raw XML bytes */
112 mach_msg_type_number_t dataLen,
113 int *newInstance,
114 int *sc_status
115 )
116 {
117 serverSessionRef mySession = getSession(server);
118 CFStringRef key; /* key (un-serialized) */
119 CFDataRef data; /* data (un-serialized) */
120
121 if (_configd_verbose) {
122 SCLog(TRUE, LOG_DEBUG, CFSTR("Add key to configuration database."));
123 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
124 }
125
126 *sc_status = kSCStatusOK;
127
128 /* un-serialize the key */
129 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
130 *sc_status = kSCStatusFailed;
131 } else if (!isA_CFString(key)) {
132 *sc_status = kSCStatusInvalidArgument;
133 }
134
135 /* un-serialize the data */
136 if (!_SCUnserializeData(&data, (void *)dataRef, dataLen)) {
137 *sc_status = kSCStatusFailed;
138 }
139
140 if (!mySession) {
141 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
142 }
143
144 if (*sc_status != kSCStatusOK) {
145 if (key) CFRelease(key);
146 if (data) CFRelease(data);
147 return KERN_SUCCESS;
148 }
149
150 *sc_status = __SCDynamicStoreAddValue(mySession->store, key, data);
151 *newInstance = 0;
152
153 CFRelease(key);
154 CFRelease(data);
155
156 return KERN_SUCCESS;
157 }