2 * Copyright (c) 2000-2008, 2010, 2011 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 * November 9, 2000 Allan Nathanson <ajn@apple.com>
34 #include <TargetConditionals.h>
35 #include <SystemConfiguration/SystemConfiguration.h>
36 #include <SystemConfiguration/SCPrivate.h>
37 #include "SCPreferencesInternal.h"
38 #include "SCHelper_client.h"
42 #include <sys/errno.h>
45 __SCPreferencesCommitChanges_helper(SCPreferencesRef prefs
)
47 CFDataRef data
= NULL
;
49 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
50 uint32_t status
= kSCStatusOK
;
51 CFDataRef reply
= NULL
;
53 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
55 status
= kSCStatusAccessError
;
59 if (prefsPrivate
->changed
) {
60 ok
= _SCSerialize(prefsPrivate
->prefs
, &data
, NULL
, NULL
);
62 status
= kSCStatusFailed
;
67 // have the helper "commit" the prefs
68 // status = kSCStatusOK;
70 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
71 SCHELPER_MSG_PREFS_COMMIT
,
75 if (data
!= NULL
) CFRelease(data
);
80 if (status
!= kSCStatusOK
) {
84 if (prefsPrivate
->changed
) {
85 if (prefsPrivate
->signature
!= NULL
) CFRelease(prefsPrivate
->signature
);
86 prefsPrivate
->signature
= reply
;
88 if (reply
!= NULL
) CFRelease(reply
);
91 prefsPrivate
->changed
= FALSE
;
97 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
98 _SCHelperClose(&prefsPrivate
->helper_port
);
104 if (reply
!= NULL
) CFRelease(reply
);
111 writen(int ref
, const void *data
, size_t len
)
115 const void *p
= data
;
118 if ((n
= write(ref
, p
, left
)) == -1) {
119 if (errno
!= EINTR
) {
132 SCPreferencesCommitChanges(SCPreferencesRef prefs
)
136 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
142 /* sorry, you must provide a session */
143 _SCErrorSet(kSCStatusNoPrefsSession
);
148 * Determine if the we have exclusive access to the preferences
149 * and acquire the lock if necessary.
151 wasLocked
= prefsPrivate
->locked
;
153 if (!SCPreferencesLock(prefs
, TRUE
)) {
154 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges SCPreferencesLock() failed"));
159 if (prefsPrivate
->authorizationData
!= NULL
) {
160 ok
= __SCPreferencesCommitChanges_helper(prefs
);
162 prefsPrivate
->changed
= FALSE
;
168 * if necessary, apply changes
170 if (!prefsPrivate
->changed
) {
175 * check if the preferences should be removed
177 if (CFDictionaryGetCount(prefsPrivate
->prefs
) == 0) {
181 if ((prefsPrivate
->options
!= NULL
) &&
182 CFDictionaryGetValueIfPresent(prefsPrivate
->options
,
183 kSCPreferencesOptionRemoveWhenEmpty
,
184 (const void **)&val
) &&
185 isA_CFBoolean(val
) &&
186 CFBooleanGetValue(val
)) {
187 /* if we've been asked to remove empty .plists */
192 path
= prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
;
199 if (stat(prefsPrivate
->path
, &statBuf
) == -1) {
200 if (errno
== ENOENT
) {
201 bzero(&statBuf
, sizeof(statBuf
));
202 statBuf
.st_mode
= 0644;
203 statBuf
.st_uid
= geteuid();
204 statBuf
.st_gid
= getegid();
206 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges stat() failed: %s"), strerror(errno
));
211 /* create the (new) preferences file */
212 pathLen
= strlen(path
) + sizeof("-new");
213 thePath
= CFAllocatorAllocate(NULL
, pathLen
, 0);
214 snprintf(thePath
, pathLen
, "%s-new", path
);
216 fd
= open(thePath
, O_WRONLY
|O_CREAT
, statBuf
.st_mode
);
219 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges open() failed: %s"), strerror(errno
));
220 CFAllocatorDeallocate(NULL
, thePath
);
224 /* preserve permissions */
225 (void) fchown(fd
, statBuf
.st_uid
, statBuf
.st_gid
);
226 (void) fchmod(fd
, statBuf
.st_mode
);
228 /* write the new preferences */
229 newPrefs
= CFPropertyListCreateData(NULL
,
232 kCFPropertyListBinaryFormat_v1_0
,
233 #else // TARGET_OS_IPHONE
234 kCFPropertyListXMLFormat_v1_0
,
235 #endif // TARGET_OS_IPHONE
239 _SCErrorSet(kSCStatusFailed
);
240 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges CFPropertyListCreateData() failed"));
241 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" prefs = %s"), path
);
242 CFAllocatorDeallocate(NULL
, thePath
);
246 if (writen(fd
, (const void *)CFDataGetBytePtr(newPrefs
), CFDataGetLength(newPrefs
)) == -1) {
248 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges write() failed: %s"), strerror(errno
));
249 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" path = %s"), thePath
);
250 (void) unlink(thePath
);
251 CFAllocatorDeallocate(NULL
, thePath
);
257 #if !TARGET_OS_IPHONE
258 /* synchronize the file's in-core state with that on disk */
259 if (fsync(fd
) == -1) {
261 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges fsync() failed: %s"), strerror(errno
));
262 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" path = %s"), thePath
);
263 (void) unlink(thePath
);
264 CFAllocatorDeallocate(NULL
, thePath
);
271 * ... and ask the drive to flush to the media
273 * Note: at present, this only works on HFS filesystems
275 (void) fcntl(fd
, F_FULLFSYNC
, 0);
276 #endif // !TARGET_OS_IPHONE
278 /* new preferences have been written */
279 if (close(fd
) == -1) {
281 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges close() failed: %s"), strerror(errno
));
282 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" path = %s"), thePath
);
283 (void) unlink(thePath
);
284 CFAllocatorDeallocate(NULL
, thePath
);
290 /* rename new->old */
291 if (rename(thePath
, path
) == -1) {
293 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges rename() failed: %s"), strerror(errno
));
294 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" path = %s --> %s"), thePath
, path
);
295 CFAllocatorDeallocate(NULL
, thePath
);
298 CFAllocatorDeallocate(NULL
, thePath
);
300 if (prefsPrivate
->newPath
) {
301 /* prefs file saved in "new" directory */
302 (void) unlink(prefsPrivate
->path
);
303 (void) symlink(prefsPrivate
->newPath
, prefsPrivate
->path
);
304 CFAllocatorDeallocate(NULL
, prefsPrivate
->path
);
305 prefsPrivate
->path
= path
;
306 prefsPrivate
->newPath
= NULL
;
309 /* grab the new signature */
310 if (stat(path
, &statBuf
) == -1) {
312 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges stat() failed: %s"), strerror(errno
));
313 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" path = %s"), thePath
);
317 /* remove the empty .plist */
320 /* init the new signature */
321 bzero(&statBuf
, sizeof(statBuf
));
324 /* update signature */
325 if (prefsPrivate
->signature
!= NULL
) CFRelease(prefsPrivate
->signature
);
326 prefsPrivate
->signature
= __SCPSignatureFromStatbuf(&statBuf
);
330 /* post notification */
331 if (prefsPrivate
->session
== NULL
) {
334 ok
= SCDynamicStoreNotifyValue(prefsPrivate
->session
, prefsPrivate
->sessionKeyCommit
);
336 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesCommitChanges SCDynamicStoreNotifyValue() failed"));
337 _SCErrorSet(kSCStatusFailed
);
342 prefsPrivate
->changed
= FALSE
;
349 status
= SCError(); // preserve status across unlock
350 (void) SCPreferencesUnlock(prefs
);