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>
44 SCPreferencesLock(SCPreferencesRef prefs
, Boolean wait
)
46 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
48 Boolean haveLock
= FALSE
;
49 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
50 CFDateRef value
= NULL
;
53 /* sorry, you must provide a session */
54 _SCErrorSet(kSCStatusNoPrefsSession
);
58 if (prefsPrivate
->locked
) {
59 /* sorry, you already have the lock */
60 _SCErrorSet(kSCStatusLocked
);
64 if (!prefsPrivate
->isRoot
) {
65 if (!prefsPrivate
->perUser
) {
66 _SCErrorSet(kSCStatusAccessError
);
69 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
74 pthread_mutex_lock(&prefsPrivate
->lock
);
76 if (prefsPrivate
->session
== NULL
) {
77 __SCPreferencesAddSession(prefs
);
80 if (prefsPrivate
->sessionKeyLock
== NULL
) {
81 /* create the session "lock" key */
82 prefsPrivate
->sessionKeyLock
= _SCPNotificationKey(allocator
,
83 prefsPrivate
->prefsID
,
84 prefsPrivate
->perUser
,
86 kSCPreferencesKeyLock
);
89 pthread_mutex_unlock(&prefsPrivate
->lock
);
91 if (!SCDynamicStoreAddWatchedKey(prefsPrivate
->session
,
92 prefsPrivate
->sessionKeyLock
,
94 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreAddWatchedKey() failed"));
98 value
= CFDateCreate(allocator
, CFAbsoluteTimeGetCurrent());
101 CFArrayRef changedKeys
;
104 * Attempt to acquire the lock
106 if (SCDynamicStoreAddTemporaryValue(prefsPrivate
->session
,
107 prefsPrivate
->sessionKeyLock
,
113 _SCErrorSet(kSCStatusPrefsBusy
);
119 * Wait for the lock to be released
121 if (!SCDynamicStoreNotifyWait(prefsPrivate
->session
)) {
122 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreNotifyWait() failed"));
125 changedKeys
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
127 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreCopyNotifiedKeys() failed"));
130 CFRelease(changedKeys
);
138 if (!SCDynamicStoreRemoveWatchedKey(prefsPrivate
->session
,
139 prefsPrivate
->sessionKeyLock
,
141 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreRemoveWatchedKey() failed"));
145 changes
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
147 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCPreferencesLock SCDynamicStoreCopyNotifiedKeys() failed"));
154 if (prefsPrivate
->accessed
) {
155 CFDataRef currentSignature
;
160 * the preferences have been accessed since the
161 * session was created so we need to compare
162 * the signature of the stored preferences.
164 if (stat(prefsPrivate
->path
, &statBuf
) == -1) {
165 if (errno
== ENOENT
) {
166 bzero(&statBuf
, sizeof(statBuf
));
168 SCLog(TRUE
, LOG_DEBUG
, CFSTR("SCPreferencesLock stat() failed: %s"), strerror(errno
));
169 _SCErrorSet(kSCStatusStale
);
174 currentSignature
= __SCPSignatureFromStatbuf(&statBuf
);
175 match
= CFEqual(prefsPrivate
->signature
, currentSignature
);
176 CFRelease(currentSignature
);
179 * the preferences have been updated since the
180 * session was accessed so we've got no choice
181 * but to deny the lock request.
183 _SCErrorSet(kSCStatusStale
);
188 // * the file contents have changed but since we
189 // * haven't accessed any of the preference data we
190 // * don't need to return an error. Simply proceed.
194 prefsPrivate
->locked
= TRUE
;
200 SCDynamicStoreRemoveValue(prefsPrivate
->session
,
201 prefsPrivate
->sessionKeyLock
);
203 if (value
) CFRelease(value
);