2 * Copyright(c) 2000-2002 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"
41 #include <sys/errno.h>
45 SCPreferencesLock(SCPreferencesRef prefs
, Boolean wait
)
47 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
49 Boolean haveLock
= FALSE
;
50 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
51 CFDateRef value
= NULL
;
54 /* sorry, you must provide a session */
55 _SCErrorSet(kSCStatusNoPrefsSession
);
59 if (prefsPrivate
->locked
) {
60 /* sorry, you already have the lock */
61 _SCErrorSet(kSCStatusLocked
);
65 if (!prefsPrivate
->isRoot
) {
66 if (!prefsPrivate
->perUser
) {
67 _SCErrorSet(kSCStatusAccessError
);
70 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
76 pthread_mutex_lock(&prefsPrivate
->lock
);
78 if (prefsPrivate
->session
== NULL
) {
79 __SCPreferencesAddSession(prefs
);
82 if (prefsPrivate
->sessionKeyLock
== NULL
) {
83 /* create the session "lock" key */
84 prefsPrivate
->sessionKeyLock
= _SCPNotificationKey(allocator
,
85 prefsPrivate
->prefsID
,
86 prefsPrivate
->perUser
,
88 kSCPreferencesKeyLock
);
91 pthread_mutex_unlock(&prefsPrivate
->lock
);
93 if (!SCDynamicStoreAddWatchedKey(prefsPrivate
->session
,
94 prefsPrivate
->sessionKeyLock
,
96 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreAddWatchedKey() failed"));
100 value
= CFDateCreate(allocator
, CFAbsoluteTimeGetCurrent());
103 CFArrayRef changedKeys
;
106 * Attempt to acquire the lock
108 if (SCDynamicStoreAddTemporaryValue(prefsPrivate
->session
,
109 prefsPrivate
->sessionKeyLock
,
115 _SCErrorSet(kSCStatusPrefsBusy
);
121 * Wait for the lock to be released
123 if (!SCDynamicStoreNotifyWait(prefsPrivate
->session
)) {
124 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreNotifyWait() failed"));
127 changedKeys
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
129 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreCopyNotifiedKeys() failed"));
132 CFRelease(changedKeys
);
140 if (!SCDynamicStoreRemoveWatchedKey(prefsPrivate
->session
,
141 prefsPrivate
->sessionKeyLock
,
143 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreRemoveWatchedKey() failed"));
147 changes
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
149 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreCopyNotifiedKeys() failed"));
156 if (prefsPrivate
->accessed
) {
157 CFDataRef currentSignature
;
162 * the preferences have been accessed since the
163 * session was created so we need to compare
164 * the signature of the stored preferences.
166 if (stat(prefsPrivate
->path
, &statBuf
) == -1) {
167 if (errno
== ENOENT
) {
168 bzero(&statBuf
, sizeof(statBuf
));
170 SCLog(TRUE
, LOG_DEBUG
, CFSTR("SCPreferencesLock stat() failed: %s"), strerror(errno
));
171 _SCErrorSet(kSCStatusStale
);
176 currentSignature
= __SCPSignatureFromStatbuf(&statBuf
);
177 match
= CFEqual(prefsPrivate
->signature
, currentSignature
);
178 CFRelease(currentSignature
);
181 * the preferences have been updated since the
182 * session was accessed so we've got no choice
183 * but to deny the lock request.
185 _SCErrorSet(kSCStatusStale
);
190 // * the file contents have changed but since we
191 // * haven't accessed any of the preference data we
192 // * don't need to return an error. Simply proceed.
196 prefsPrivate
->locked
= TRUE
;
202 SCDynamicStoreRemoveValue(prefsPrivate
->session
,
203 prefsPrivate
->sessionKeyLock
);
205 if (value
) CFRelease(value
);