]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPLock.c
configd-130.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 * 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 <pthread.h>
41 #include <sys/errno.h>
42
43 Boolean
44 SCPreferencesLock(SCPreferencesRef prefs, Boolean wait)
45 {
46 CFAllocatorRef allocator = CFGetAllocator(prefs);
47 CFArrayRef changes;
48 Boolean haveLock = FALSE;
49 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
50 CFDateRef value = NULL;
51
52 if (prefs == NULL) {
53 /* sorry, you must provide a session */
54 _SCErrorSet(kSCStatusNoPrefsSession);
55 return FALSE;
56 }
57
58 if (prefsPrivate->locked) {
59 /* sorry, you already have the lock */
60 _SCErrorSet(kSCStatusLocked);
61 return FALSE;
62 }
63
64 if (!prefsPrivate->isRoot) {
65 if (!prefsPrivate->perUser) {
66 _SCErrorSet(kSCStatusAccessError);
67 return FALSE;
68 } else {
69 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
70 goto perUser;
71 }
72 }
73
74 pthread_mutex_lock(&prefsPrivate->lock);
75
76 if (prefsPrivate->session == NULL) {
77 __SCPreferencesAddSession(prefs);
78 }
79
80 if (prefsPrivate->sessionKeyLock == NULL) {
81 /* create the session "lock" key */
82 prefsPrivate->sessionKeyLock = _SCPNotificationKey(allocator,
83 prefsPrivate->prefsID,
84 prefsPrivate->perUser,
85 prefsPrivate->user,
86 kSCPreferencesKeyLock);
87 }
88
89 pthread_mutex_unlock(&prefsPrivate->lock);
90
91 if (!SCDynamicStoreAddWatchedKey(prefsPrivate->session,
92 prefsPrivate->sessionKeyLock,
93 FALSE)) {
94 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCPreferencesLock SCDynamicStoreAddWatchedKey() failed"));
95 goto error;
96 }
97
98 value = CFDateCreate(allocator, CFAbsoluteTimeGetCurrent());
99
100 while (TRUE) {
101 CFArrayRef changedKeys;
102
103 /*
104 * Attempt to acquire the lock
105 */
106 if (SCDynamicStoreAddTemporaryValue(prefsPrivate->session,
107 prefsPrivate->sessionKeyLock,
108 value)) {
109 haveLock = TRUE;
110 goto done;
111 } else {
112 if (!wait) {
113 _SCErrorSet(kSCStatusPrefsBusy);
114 goto error;
115 }
116 }
117
118 /*
119 * Wait for the lock to be released
120 */
121 if (!SCDynamicStoreNotifyWait(prefsPrivate->session)) {
122 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCPreferencesLock SCDynamicStoreNotifyWait() failed"));
123 goto error;
124 }
125 changedKeys = SCDynamicStoreCopyNotifiedKeys(prefsPrivate->session);
126 if (!changedKeys) {
127 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCPreferencesLock SCDynamicStoreCopyNotifiedKeys() failed"));
128 goto error;
129 }
130 CFRelease(changedKeys);
131 }
132
133 done :
134
135 CFRelease(value);
136 value = NULL;
137
138 if (!SCDynamicStoreRemoveWatchedKey(prefsPrivate->session,
139 prefsPrivate->sessionKeyLock,
140 0)) {
141 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCPreferencesLock SCDynamicStoreRemoveWatchedKey() failed"));
142 goto error;
143 }
144
145 changes = SCDynamicStoreCopyNotifiedKeys(prefsPrivate->session);
146 if (!changes) {
147 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCPreferencesLock SCDynamicStoreCopyNotifiedKeys() failed"));
148 goto error;
149 }
150 CFRelease(changes);
151
152 perUser:
153
154 if (prefsPrivate->accessed) {
155 CFDataRef currentSignature;
156 Boolean match;
157 struct stat statBuf;
158
159 /*
160 * the preferences have been accessed since the
161 * session was created so we need to compare
162 * the signature of the stored preferences.
163 */
164 if (stat(prefsPrivate->path, &statBuf) == -1) {
165 if (errno == ENOENT) {
166 bzero(&statBuf, sizeof(statBuf));
167 } else {
168 SCLog(TRUE, LOG_DEBUG, CFSTR("SCPreferencesLock stat() failed: %s"), strerror(errno));
169 _SCErrorSet(kSCStatusStale);
170 goto error;
171 }
172 }
173
174 currentSignature = __SCPSignatureFromStatbuf(&statBuf);
175 match = CFEqual(prefsPrivate->signature, currentSignature);
176 CFRelease(currentSignature);
177 if (!match) {
178 /*
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.
182 */
183 _SCErrorSet(kSCStatusStale);
184 goto error;
185 }
186 // } else {
187 // /*
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.
191 // */
192 }
193
194 prefsPrivate->locked = TRUE;
195 return TRUE;
196
197 error :
198
199 if (haveLock) {
200 SCDynamicStoreRemoveValue(prefsPrivate->session,
201 prefsPrivate->sessionKeyLock);
202 }
203 if (value) CFRelease(value);
204
205 return FALSE;
206 }