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