]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_snapshot.c
configd-395.6.tar.gz
[apple/configd.git] / configd.tproj / _snapshot.c
1 /*
2 * Copyright (c) 2000-2006, 2009, 2010 Apple 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 * April 14, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <fcntl.h>
35 #include <paths.h>
36 #include <unistd.h>
37
38 #include "configd.h"
39 #include "configd_server.h"
40 #include "session.h"
41 #include "plugin_support.h"
42
43
44 #define SNAPSHOT_PATH_STATE _PATH_VARTMP "configd-state"
45 #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.plist"
46 #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.plist"
47 #define SNAPSHOT_PATH_SESSION _PATH_VARTMP "configd-session.plist"
48
49
50 #define N_QUICK 100
51
52 static CFDictionaryRef
53 _expandStore(CFDictionaryRef storeData)
54 {
55 const void * keys_q[N_QUICK];
56 const void ** keys = keys_q;
57 CFIndex nElements;
58 CFDictionaryRef newStoreData = NULL;
59 const void * nValues_q[N_QUICK];
60 const void ** nValues = nValues_q;
61 const void * oValues_q[N_QUICK];
62 const void ** oValues = oValues_q;
63
64 nElements = CFDictionaryGetCount(storeData);
65 if (nElements > 0) {
66 CFIndex i;
67
68 if (nElements > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
69 keys = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
70 oValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
71 nValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
72 }
73 bzero(nValues, nElements * sizeof(CFTypeRef));
74
75 CFDictionaryGetKeysAndValues(storeData, keys, oValues);
76 for (i = 0; i < nElements; i++) {
77 CFDataRef data;
78
79 data = CFDictionaryGetValue(oValues[i], kSCDData);
80 if (data) {
81 CFPropertyListRef plist;
82
83 nValues[i] = CFDictionaryCreateMutableCopy(NULL, 0, oValues[i]);
84
85 _SCUnserialize(&plist, data, NULL, 0);
86 CFDictionarySetValue((CFMutableDictionaryRef)nValues[i],
87 kSCDData,
88 plist);
89 CFRelease(plist);
90 } else {
91 nValues[i] = CFRetain(oValues[i]);
92 }
93 }
94 }
95
96 newStoreData = CFDictionaryCreate(NULL,
97 keys,
98 nValues,
99 nElements,
100 &kCFTypeDictionaryKeyCallBacks,
101 &kCFTypeDictionaryValueCallBacks);
102
103 if (nElements > 0) {
104 CFIndex i;
105
106 for (i = 0; i < nElements; i++) {
107 CFRelease(nValues[i]);
108 }
109
110 if (keys != keys_q) {
111 CFAllocatorDeallocate(NULL, keys);
112 CFAllocatorDeallocate(NULL, oValues);
113 CFAllocatorDeallocate(NULL, nValues);
114 }
115 }
116
117 return newStoreData;
118 }
119
120
121 __private_extern__
122 int
123 __SCDynamicStoreSnapshot(SCDynamicStoreRef store)
124 {
125 CFDictionaryRef expandedStoreData;
126 FILE *f;
127 int fd;
128 serverSessionRef mySession;
129 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
130 CFDataRef xmlData;
131
132 /* check credentials */
133
134 mySession = getSession(storePrivate->server);
135 if (!hasRootAccess(mySession)) {
136 return kSCStatusAccessError;
137 }
138
139 /* Save a snapshot of configd's "state" */
140
141 (void) unlink(SNAPSHOT_PATH_STATE);
142 fd = open(SNAPSHOT_PATH_STATE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
143 if (fd == -1) {
144 return kSCStatusFailed;
145 }
146 f = fdopen(fd, "w");
147 if (f == NULL) {
148 return kSCStatusFailed;
149 }
150 SCPrint(TRUE, f, CFSTR("Main thread :\n\n"));
151 SCPrint(TRUE, f, CFSTR("%@\n"), CFRunLoopGetCurrent());
152 if (plugin_runLoop != NULL) {
153 SCPrint(TRUE, f, CFSTR("Plug-in thread :\n\n"));
154 SCPrint(TRUE, f, CFSTR("%@\n"), plugin_runLoop);
155 }
156 listSessions(f);
157 (void) fclose(f);
158
159 /* Save a snapshot of the "store" data */
160
161 (void) unlink(SNAPSHOT_PATH_STORE);
162 fd = open(SNAPSHOT_PATH_STORE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
163 if (fd == -1) {
164 return kSCStatusFailed;
165 }
166
167 expandedStoreData = _expandStore(storeData);
168 xmlData = CFPropertyListCreateData(NULL, expandedStoreData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
169 CFRelease(expandedStoreData);
170 if (xmlData == NULL) {
171 SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateData() failed"));
172 close(fd);
173 return kSCStatusFailed;
174 }
175 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
176 (void) close(fd);
177 CFRelease(xmlData);
178
179 /* Save a snapshot of the "pattern" data */
180
181 (void) unlink(SNAPSHOT_PATH_PATTERN);
182 fd = open(SNAPSHOT_PATH_PATTERN, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
183 if (fd == -1) {
184 return kSCStatusFailed;
185 }
186
187 xmlData = CFPropertyListCreateData(NULL, patternData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
188 if (xmlData == NULL) {
189 SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateData() failed"));
190 close(fd);
191 return kSCStatusFailed;
192 }
193 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
194 (void) close(fd);
195 CFRelease(xmlData);
196
197 /* Save a snapshot of the "session" data */
198
199 (void) unlink(SNAPSHOT_PATH_SESSION);
200 fd = open(SNAPSHOT_PATH_SESSION, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
201 if (fd == -1) {
202 return kSCStatusFailed;
203 }
204
205 xmlData = CFPropertyListCreateData(NULL, sessionData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
206 if (xmlData == NULL) {
207 SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateData() failed"));
208 close(fd);
209 return kSCStatusFailed;
210 }
211 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
212 (void) close(fd);
213 CFRelease(xmlData);
214
215 return kSCStatusOK;
216 }
217
218
219 __private_extern__
220 kern_return_t
221 _snapshot(mach_port_t server, int *sc_status)
222 {
223 serverSessionRef mySession = getSession(server);
224
225 if (mySession == NULL) {
226 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
227 return KERN_SUCCESS;
228 }
229
230 *sc_status = __SCDynamicStoreSnapshot(mySession->store);
231 return KERN_SUCCESS;
232 }