]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPLock.c
88bd2819c8c0d890a53b3f3f4a447848be5bf9c4
[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 * 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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
37
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <sys/errno.h>
41
42 Boolean
43 SCPreferencesLock(SCPreferencesRef session, Boolean wait)
44 {
45 CFAllocatorRef allocator = CFGetAllocator(session);
46 CFArrayRef changes;
47 CFDataRef currentSignature = NULL;
48 Boolean haveLock = FALSE;
49 SCPreferencesPrivateRef sessionPrivate = (SCPreferencesPrivateRef)session;
50 struct stat statBuf;
51 CFDateRef value = NULL;
52
53 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCPreferencesLock:"));
54
55 if (sessionPrivate->locked) {
56 /* sorry, you already have the lock */
57 _SCErrorSet(kSCStatusLocked);
58 return FALSE;
59 }
60
61 if (!sessionPrivate->isRoot) {
62 if (!sessionPrivate->perUser) {
63 _SCErrorSet(kSCStatusAccessError);
64 return FALSE;
65 } else {
66 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
67 goto perUser;
68 }
69 }
70
71 if (sessionPrivate->session == NULL) {
72 /* open a session */
73 sessionPrivate->session = SCDynamicStoreCreate(allocator,
74 CFSTR("SCPreferencesLock"),
75 NULL,
76 NULL);
77 if (!sessionPrivate->session) {
78 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCreate() failed"));
79 return FALSE;
80 }
81 }
82
83 if (sessionPrivate->sessionKeyLock == NULL) {
84 /* create the session "lock" key */
85 sessionPrivate->sessionKeyLock = _SCPNotificationKey(allocator,
86 sessionPrivate->prefsID,
87 sessionPrivate->perUser,
88 sessionPrivate->user,
89 kSCPreferencesKeyLock);
90 }
91
92 if (!SCDynamicStoreAddWatchedKey(sessionPrivate->session,
93 sessionPrivate->sessionKeyLock,
94 FALSE)) {
95 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreAddWatchedKey() failed"));
96 goto error;
97 }
98
99 value = CFDateCreate(allocator, CFAbsoluteTimeGetCurrent());
100
101 while (TRUE) {
102 CFArrayRef changedKeys;
103
104 /*
105 * Attempt to acquire the lock
106 */
107 if (SCDynamicStoreAddTemporaryValue(sessionPrivate->session,
108 sessionPrivate->sessionKeyLock,
109 value)) {
110 haveLock = TRUE;
111 goto done;
112 } else {
113 if (!wait) {
114 _SCErrorSet(kSCStatusPrefsBusy);
115 goto error;
116 }
117 }
118
119 /*
120 * Wait for the lock to be released
121 */
122 if (!SCDynamicStoreNotifyWait(sessionPrivate->session)) {
123 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreNotifyWait() failed"));
124 goto error;
125 }
126 changedKeys = SCDynamicStoreCopyNotifiedKeys(sessionPrivate->session);
127 if (!changedKeys) {
128 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCopyNotifiedKeys() failed"));
129 goto error;
130 }
131 CFRelease(changedKeys);
132 }
133
134 done :
135
136 CFRelease(value);
137 value = NULL;
138
139 if (!SCDynamicStoreRemoveWatchedKey(sessionPrivate->session,
140 sessionPrivate->sessionKeyLock,
141 0)) {
142 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreRemoveWatchedKey() failed"));
143 goto error;
144 }
145
146 changes = SCDynamicStoreCopyNotifiedKeys(sessionPrivate->session);
147 if (!changes) {
148 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCopyNotifiedKeys() failed"));
149 goto error;
150 }
151 CFRelease(changes);
152
153 perUser:
154
155 /*
156 * Check the signature
157 */
158 if (stat(sessionPrivate->path, &statBuf) == -1) {
159 if (errno == ENOENT) {
160 bzero(&statBuf, sizeof(statBuf));
161 } else {
162 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("stat() failed: %s"), strerror(errno));
163 _SCErrorSet(kSCStatusStale);
164 goto error;
165 }
166 }
167
168 currentSignature = __SCPSignatureFromStatbuf(&statBuf);
169 if (!CFEqual(sessionPrivate->signature, currentSignature)) {
170 if (sessionPrivate->accessed) {
171 /*
172 * the preferences have been accessed since the
173 * session was created so we've got no choice
174 * but to deny the lock request.
175 */
176 _SCErrorSet(kSCStatusStale);
177 goto error;
178 } else {
179 /*
180 * the file contents have changed but since we
181 * haven't accessed any of the preferences we
182 * don't need to return an error. Simply reload
183 * the stored data and proceed.
184 */
185 SCPreferencesRef newPrefs;
186 SCPreferencesPrivateRef newPrivate;
187
188 newPrefs = __SCPreferencesCreate(allocator,
189 sessionPrivate->name,
190 sessionPrivate->prefsID,
191 sessionPrivate->perUser,
192 sessionPrivate->user);
193 if (!newPrefs) {
194 /* if updated preferences could not be loaded */
195 _SCErrorSet(kSCStatusStale);
196 goto error;
197 }
198
199 /* synchronize this sessions prefs/signature */
200 newPrivate = (SCPreferencesPrivateRef)newPrefs;
201 CFRelease(sessionPrivate->prefs);
202 sessionPrivate->prefs = newPrivate->prefs;
203 CFRetain(sessionPrivate->prefs);
204 CFRelease(sessionPrivate->signature);
205 sessionPrivate->signature = CFRetain(newPrivate->signature);
206 CFRelease(newPrefs);
207 }
208 }
209 CFRelease(currentSignature);
210
211 sessionPrivate->locked = TRUE;
212 return TRUE;
213
214 error :
215
216 if (haveLock) {
217 SCDynamicStoreRemoveValue(sessionPrivate->session,
218 sessionPrivate->sessionKeyLock);
219 }
220 if (currentSignature) CFRelease(currentSignature);
221 if (value) CFRelease(value);
222
223 return FALSE;
224 }