]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPLock.c
configd-84.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCPLock.c
1 /*
2 * Copyright(c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * November 9, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include "SCPreferencesInternal.h"
39
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <sys/errno.h>
43
44 Boolean
45 SCPreferencesLock(SCPreferencesRef session, Boolean wait)
46 {
47 CFAllocatorRef allocator = CFGetAllocator(session);
48 CFArrayRef changes;
49 CFDataRef currentSignature = NULL;
50 Boolean haveLock = FALSE;
51 SCPreferencesPrivateRef sessionPrivate = (SCPreferencesPrivateRef)session;
52 struct stat statBuf;
53 CFDateRef value = NULL;
54
55 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCPreferencesLock:"));
56
57 if (sessionPrivate->locked) {
58 /* sorry, you already have the lock */
59 _SCErrorSet(kSCStatusLocked);
60 return FALSE;
61 }
62
63 if (!sessionPrivate->isRoot) {
64 if (!sessionPrivate->perUser) {
65 _SCErrorSet(kSCStatusAccessError);
66 return FALSE;
67 } else {
68 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
69 goto perUser;
70 }
71 }
72
73 if (sessionPrivate->session == NULL) {
74 /* open a session */
75 sessionPrivate->session = SCDynamicStoreCreate(allocator,
76 CFSTR("SCPreferencesLock"),
77 NULL,
78 NULL);
79 if (!sessionPrivate->session) {
80 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCreate() failed"));
81 return FALSE;
82 }
83 }
84
85 if (sessionPrivate->sessionKeyLock == NULL) {
86 /* create the session "lock" key */
87 sessionPrivate->sessionKeyLock = _SCPNotificationKey(allocator,
88 sessionPrivate->prefsID,
89 sessionPrivate->perUser,
90 sessionPrivate->user,
91 kSCPreferencesKeyLock);
92 }
93
94 if (!SCDynamicStoreAddWatchedKey(sessionPrivate->session,
95 sessionPrivate->sessionKeyLock,
96 FALSE)) {
97 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreAddWatchedKey() failed"));
98 goto error;
99 }
100
101 value = CFDateCreate(allocator, CFAbsoluteTimeGetCurrent());
102
103 while (TRUE) {
104 CFArrayRef changedKeys;
105
106 /*
107 * Attempt to acquire the lock
108 */
109 if (SCDynamicStoreAddTemporaryValue(sessionPrivate->session,
110 sessionPrivate->sessionKeyLock,
111 value)) {
112 haveLock = TRUE;
113 goto done;
114 } else {
115 if (!wait) {
116 _SCErrorSet(kSCStatusPrefsBusy);
117 goto error;
118 }
119 }
120
121 /*
122 * Wait for the lock to be released
123 */
124 if (!SCDynamicStoreNotifyWait(sessionPrivate->session)) {
125 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreNotifyWait() failed"));
126 goto error;
127 }
128 changedKeys = SCDynamicStoreCopyNotifiedKeys(sessionPrivate->session);
129 if (!changedKeys) {
130 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCopyNotifiedKeys() failed"));
131 goto error;
132 }
133 CFRelease(changedKeys);
134 }
135
136 done :
137
138 CFRelease(value);
139 value = NULL;
140
141 if (!SCDynamicStoreRemoveWatchedKey(sessionPrivate->session,
142 sessionPrivate->sessionKeyLock,
143 0)) {
144 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreRemoveWatchedKey() failed"));
145 goto error;
146 }
147
148 changes = SCDynamicStoreCopyNotifiedKeys(sessionPrivate->session);
149 if (!changes) {
150 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCopyNotifiedKeys() failed"));
151 goto error;
152 }
153 CFRelease(changes);
154
155 perUser:
156
157 /*
158 * Check the signature
159 */
160 if (stat(sessionPrivate->path, &statBuf) == -1) {
161 if (errno == ENOENT) {
162 bzero(&statBuf, sizeof(statBuf));
163 } else {
164 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("stat() failed: %s"), strerror(errno));
165 _SCErrorSet(kSCStatusStale);
166 goto error;
167 }
168 }
169
170 currentSignature = __SCPSignatureFromStatbuf(&statBuf);
171 if (!CFEqual(sessionPrivate->signature, currentSignature)) {
172 if (sessionPrivate->accessed) {
173 /*
174 * the preferences have been accessed since the
175 * session was created so we've got no choice
176 * but to deny the lock request.
177 */
178 _SCErrorSet(kSCStatusStale);
179 goto error;
180 } else {
181 /*
182 * the file contents have changed but since we
183 * haven't accessed any of the preferences we
184 * don't need to return an error. Simply reload
185 * the stored data and proceed.
186 */
187 SCPreferencesRef newPrefs;
188 SCPreferencesPrivateRef newPrivate;
189
190 newPrefs = __SCPreferencesCreate(allocator,
191 sessionPrivate->name,
192 sessionPrivate->prefsID,
193 sessionPrivate->perUser,
194 sessionPrivate->user);
195 if (!newPrefs) {
196 /* if updated preferences could not be loaded */
197 _SCErrorSet(kSCStatusStale);
198 goto error;
199 }
200
201 /* synchronize this sessions prefs/signature */
202 newPrivate = (SCPreferencesPrivateRef)newPrefs;
203 CFRelease(sessionPrivate->prefs);
204 sessionPrivate->prefs = newPrivate->prefs;
205 CFRetain(sessionPrivate->prefs);
206 CFRelease(sessionPrivate->signature);
207 sessionPrivate->signature = CFRetain(newPrivate->signature);
208 CFRelease(newPrefs);
209 }
210 }
211 CFRelease(currentSignature);
212
213 sessionPrivate->locked = TRUE;
214 return TRUE;
215
216 error :
217
218 if (haveLock) {
219 SCDynamicStoreRemoveValue(sessionPrivate->session,
220 sessionPrivate->sessionKeyLock);
221 }
222 if (currentSignature) CFRelease(currentSignature);
223 if (value) CFRelease(value);
224
225 return FALSE;
226 }