]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd.c
configd-24.tar.gz
[apple/configd.git] / configd.tproj / _configadd.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include "configd.h"
24 #include "session.h"
25
26 SCDStatus
27 _SCDAdd(SCDSessionRef session, CFStringRef key, SCDHandleRef handle)
28 {
29 SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
30 SCDStatus scd_status = SCD_OK;
31 boolean_t wasLocked;
32 SCDHandleRef tempHandle;
33
34 SCDLog(LOG_DEBUG, CFSTR("_SCDAdd:"));
35 SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key);
36 SCDLog(LOG_DEBUG, CFSTR(" data = %@"), SCDHandleGetData(handle));
37
38 if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
39 return SCD_NOSESSION; /* you can't do anything with a closed session */
40 }
41
42 /*
43 * 1. Determine if the cache lock is currently held
44 * and acquire the lock if necessary.
45 */
46 wasLocked = SCDOptionGet(NULL, kSCDOptionIsLocked);
47 if (!wasLocked) {
48 scd_status = _SCDLock(session);
49 if (scd_status != SCD_OK) {
50 SCDLog(LOG_DEBUG, CFSTR(" _SCDLock(): %s"), SCDError(scd_status));
51 return scd_status;
52 }
53 }
54
55 /*
56 * 2. Ensure that this is a new key.
57 */
58 scd_status = _SCDGet(session, key, &tempHandle);
59 switch (scd_status) {
60 case SCD_NOKEY :
61 /* cache key does not exist, proceed */
62 break;
63
64 case SCD_OK :
65 /* cache key exists, sorry */
66 SCDHandleRelease(tempHandle);
67 scd_status = SCD_EXISTS;
68 goto done;
69
70 default :
71 SCDLog(LOG_DEBUG, CFSTR(" _SCDGet(): %s"), SCDError(scd_status));
72 goto done;
73 }
74
75 /*
76 * 3. Save the new key.
77 */
78 scd_status = _SCDSet(session, key, handle);
79
80 /*
81 * 4. Release the lock if we acquired it as part of this request.
82 */
83 done:
84 if (!wasLocked)
85 _SCDUnlock(session);
86
87 return scd_status;
88 }
89
90
91 kern_return_t
92 _configadd(mach_port_t server,
93 xmlData_t keyRef, /* raw XML bytes */
94 mach_msg_type_number_t keyLen,
95 xmlData_t dataRef, /* raw XML bytes */
96 mach_msg_type_number_t dataLen,
97 int *newInstance,
98 int *scd_status
99 )
100 {
101 kern_return_t status;
102 serverSessionRef mySession = getSession(server);
103 CFDataRef xmlKey; /* key (XML serialized) */
104 CFStringRef key; /* key (un-serialized) */
105 CFDataRef xmlData; /* data (XML serialized) */
106 CFPropertyListRef data; /* data (un-serialized) */
107 SCDHandleRef handle;
108 CFStringRef xmlError;
109
110 SCDLog(LOG_DEBUG, CFSTR("Add key to configuration database."));
111 SCDLog(LOG_DEBUG, CFSTR(" server = %d"), server);
112
113 /* un-serialize the key */
114 xmlKey = CFDataCreate(NULL, keyRef, keyLen);
115 status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
116 if (status != KERN_SUCCESS) {
117 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
118 /* non-fatal???, proceed */
119 }
120 key = CFPropertyListCreateFromXMLData(NULL,
121 xmlKey,
122 kCFPropertyListImmutable,
123 &xmlError);
124 CFRelease(xmlKey);
125 if (xmlError) {
126 SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() key: %s"), xmlError);
127 *scd_status = SCD_FAILED;
128 return KERN_SUCCESS;
129 }
130
131 /* un-serialize the data */
132 xmlData = CFDataCreate(NULL, dataRef, dataLen);
133 status = vm_deallocate(mach_task_self(), (vm_address_t)dataRef, dataLen);
134 if (status != KERN_SUCCESS) {
135 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
136 /* non-fatal???, proceed */
137 }
138 data = CFPropertyListCreateFromXMLData(NULL,
139 xmlData,
140 kCFPropertyListImmutable,
141 &xmlError);
142 CFRelease(xmlData);
143 if (xmlError) {
144 SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() data: %s"), xmlError);
145 CFRelease(key);
146 *scd_status = SCD_FAILED;
147 return KERN_SUCCESS;
148 }
149
150 handle = SCDHandleInit();
151 SCDHandleSetData(handle, data);
152 *scd_status = _SCDAdd(mySession->session, key, handle);
153 if (*scd_status == SCD_OK) {
154 *newInstance = SCDHandleGetInstance(handle);
155 }
156 SCDHandleRelease(handle);
157 CFRelease(key);
158 CFRelease(data);
159
160 return KERN_SUCCESS;
161 }