2 * Copyright (c) 2000-2006, 2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * April 14, 2000 Allan Nathanson <ajn@apple.com>
39 #include "configd_server.h"
41 #include "plugin_support.h"
44 #define SNAPSHOT_PATH_STATE _PATH_VARTMP "configd-state"
45 #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.xml"
46 #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.xml"
47 #define SNAPSHOT_PATH_SESSION _PATH_VARTMP "configd-session.xml"
52 static CFDictionaryRef
53 _expandStore(CFDictionaryRef storeData
)
55 const void * keys_q
[N_QUICK
];
56 const void ** keys
= keys_q
;
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
;
64 nElements
= CFDictionaryGetCount(storeData
);
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);
73 bzero(nValues
, nElements
* sizeof(CFTypeRef
));
75 CFDictionaryGetKeysAndValues(storeData
, keys
, oValues
);
76 for (i
= 0; i
< nElements
; i
++) {
79 data
= CFDictionaryGetValue(oValues
[i
], kSCDData
);
81 CFPropertyListRef plist
;
83 if (!_SCUnserialize(&plist
, data
, NULL
, 0)) {
87 nValues
[i
] = CFDictionaryCreateMutableCopy(NULL
, 0, oValues
[i
]);
88 CFDictionarySetValue((CFMutableDictionaryRef
)nValues
[i
],
93 nValues
[i
] = CFRetain(oValues
[i
]);
98 newStoreData
= CFDictionaryCreate(NULL
,
102 &kCFTypeDictionaryKeyCallBacks
,
103 &kCFTypeDictionaryValueCallBacks
);
110 for (i
= 0; i
< nElements
; i
++) {
111 if (nValues
[i
]) CFRelease(nValues
[i
]);
114 if (keys
!= keys_q
) {
115 CFAllocatorDeallocate(NULL
, keys
);
116 CFAllocatorDeallocate(NULL
, oValues
);
117 CFAllocatorDeallocate(NULL
, nValues
);
127 __SCDynamicStoreSnapshot(SCDynamicStoreRef store
)
129 CFDictionaryRef expandedStoreData
;
132 serverSessionRef mySession
;
133 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
136 /* check credentials */
138 mySession
= getSession(storePrivate
->server
);
139 if (!hasRootAccess(mySession
)) {
140 return kSCStatusAccessError
;
143 /* Save a snapshot of configd's "state" */
145 (void) unlink(SNAPSHOT_PATH_STATE
);
146 fd
= open(SNAPSHOT_PATH_STATE
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
148 return kSCStatusFailed
;
152 return kSCStatusFailed
;
154 SCPrint(TRUE
, f
, CFSTR("Main thread :\n\n"));
155 SCPrint(TRUE
, f
, CFSTR("%@\n"), CFRunLoopGetCurrent());
156 if (plugin_runLoop
!= NULL
) {
157 SCPrint(TRUE
, f
, CFSTR("Plug-in thread :\n\n"));
158 SCPrint(TRUE
, f
, CFSTR("%@\n"), plugin_runLoop
);
163 /* Save a snapshot of the "store" data */
165 (void) unlink(SNAPSHOT_PATH_STORE
);
166 fd
= open(SNAPSHOT_PATH_STORE
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
168 return kSCStatusFailed
;
171 expandedStoreData
= _expandStore(storeData
);
172 xmlData
= CFPropertyListCreateXMLData(NULL
, expandedStoreData
);
173 CFRelease(expandedStoreData
);
175 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
177 return kSCStatusFailed
;
179 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
183 /* Save a snapshot of the "pattern" data */
185 (void) unlink(SNAPSHOT_PATH_PATTERN
);
186 fd
= open(SNAPSHOT_PATH_PATTERN
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
188 return kSCStatusFailed
;
191 xmlData
= CFPropertyListCreateXMLData(NULL
, patternData
);
193 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
195 return kSCStatusFailed
;
197 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
201 /* Save a snapshot of the "session" data */
203 (void) unlink(SNAPSHOT_PATH_SESSION
);
204 fd
= open(SNAPSHOT_PATH_SESSION
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
206 return kSCStatusFailed
;
209 xmlData
= CFPropertyListCreateXMLData(NULL
, sessionData
);
211 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
213 return kSCStatusFailed
;
215 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
225 _snapshot(mach_port_t server
, int *sc_status
)
227 serverSessionRef mySession
= getSession(server
);
229 if (mySession
== NULL
) {
230 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
234 *sc_status
= __SCDynamicStoreSnapshot(mySession
->store
);