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