2 * Copyright(c) 2000-2003 Apple Computer, 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 <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
40 #include <sys/errno.h>
43 writen(int d
, const void *buf
, size_t nbytes
)
51 n
= write(d
, p
, left
);
66 SCPreferencesCommitChanges(SCPreferencesRef session
)
68 SCPreferencesPrivateRef sessionPrivate
= (SCPreferencesPrivateRef
)session
;
71 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCPreferencesCommitChanges:"));
74 * Determine if the we have exclusive access to the preferences
75 * and acquire the lock if necessary.
77 wasLocked
= sessionPrivate
->locked
;
79 if (!SCPreferencesLock(session
, TRUE
)) {
80 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" SCPreferencesLock() failed"));
86 * if necessary, apply changes
88 if (sessionPrivate
->changed
) {
96 if (stat(sessionPrivate
->path
, &statBuf
) == -1) {
97 if (errno
== ENOENT
) {
98 bzero(&statBuf
, sizeof(statBuf
));
99 statBuf
.st_mode
= 0644;
100 statBuf
.st_uid
= geteuid();
101 statBuf
.st_gid
= getegid();
103 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("stat() failed: %s"), strerror(errno
));
108 /* create the (new) preferences file */
109 path
= sessionPrivate
->newPath
? sessionPrivate
->newPath
: sessionPrivate
->path
;
110 pathLen
= strlen(path
) + sizeof("-new");
111 thePath
= CFAllocatorAllocate(NULL
, pathLen
, 0);
112 snprintf(thePath
, pathLen
, "%s-new", path
);
114 /* open the (new) preferences file */
116 fd
= open(thePath
, O_WRONLY
|O_CREAT
, statBuf
.st_mode
);
118 if ((errno
== ENOENT
) &&
119 ((sessionPrivate
->prefsID
== NULL
) || !CFStringHasPrefix(sessionPrivate
->prefsID
, CFSTR("/")))) {
122 ch
= strrchr(thePath
, '/');
127 status
= mkdir(thePath
, 0755);
134 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPCommit open() failed: %s"), strerror(errno
));
135 CFAllocatorDeallocate(NULL
, thePath
);
139 /* preserve permissions */
140 (void) fchown(fd
, statBuf
.st_uid
, statBuf
.st_gid
);
141 (void) fchmod(fd
, statBuf
.st_mode
);
143 /* write the new preferences */
144 newPrefs
= CFPropertyListCreateXMLData(NULL
, sessionPrivate
->prefs
);
146 _SCErrorSet(kSCStatusFailed
);
147 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" CFPropertyListCreateXMLData() failed"));
148 CFAllocatorDeallocate(NULL
, thePath
);
152 if (writen(fd
, (void *)CFDataGetBytePtr(newPrefs
), CFDataGetLength(newPrefs
)) == -1) {
154 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("write() failed: %s"), strerror(errno
));
155 (void) unlink(thePath
);
156 CFAllocatorDeallocate(NULL
, thePath
);
164 /* rename new->old */
165 if (rename(thePath
, path
) == -1) {
167 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("rename() failed: %s"), strerror(errno
));
168 CFAllocatorDeallocate(NULL
, thePath
);
171 CFAllocatorDeallocate(NULL
, thePath
);
173 if (sessionPrivate
->newPath
) {
174 /* prefs file saved in "new" directory */
175 (void) unlink(sessionPrivate
->path
);
176 (void) symlink(sessionPrivate
->newPath
, sessionPrivate
->path
);
177 CFAllocatorDeallocate(NULL
, sessionPrivate
->path
);
178 sessionPrivate
->path
= path
;
179 sessionPrivate
->newPath
= NULL
;
182 /* update signature */
183 if (stat(path
, &statBuf
) == -1) {
185 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("stat() failed: %s"), strerror(errno
));
188 CFRelease(sessionPrivate
->signature
);
189 sessionPrivate
->signature
= __SCPSignatureFromStatbuf(&statBuf
);
192 if (!sessionPrivate
->isRoot
) {
193 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
197 /* if necessary, create the session "commit" key */
198 if (sessionPrivate
->sessionKeyCommit
== NULL
) {
199 sessionPrivate
->sessionKeyCommit
= _SCPNotificationKey(NULL
,
200 sessionPrivate
->prefsID
,
201 sessionPrivate
->perUser
,
202 sessionPrivate
->user
,
203 kSCPreferencesKeyCommit
);
206 /* post notification */
207 if (!SCDynamicStoreNotifyValue(sessionPrivate
->session
,
208 sessionPrivate
->sessionKeyCommit
)) {
209 SCLog(_sc_verbose
, LOG_ERR
, CFSTR(" SCDynamicStoreNotifyValue() failed"));
210 _SCErrorSet(kSCStatusFailed
);
216 if (!wasLocked
) (void) SCPreferencesUnlock(session
);
217 sessionPrivate
->changed
= FALSE
;
222 if (!wasLocked
) (void) SCPreferencesUnlock(session
);