]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPLock.c
configd-24.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCPLock.c
1 /*
2 * Copyright(c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1(the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <SystemConfiguration/SCP.h>
24 #include "SCPPrivate.h"
25
26 #include <SystemConfiguration/SCD.h>
27
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/errno.h>
31
32
33 SCPStatus
34 SCPLock(SCPSessionRef session, boolean_t wait)
35 {
36 SCPStatus scp_status;
37 SCDStatus scd_status;
38 SCPSessionPrivateRef sessionPrivate;
39 SCDHandleRef handle = NULL;
40 CFDateRef value;
41 CFArrayRef changes;
42 struct stat statBuf;
43 CFDataRef currentSignature;
44
45 if (session == NULL) {
46 return SCP_FAILED; /* you can't do anything with a closed session */
47 }
48 sessionPrivate = (SCPSessionPrivateRef)session;
49
50 if (!sessionPrivate->isRoot) {
51 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
52 goto notRoot;
53 }
54
55 if (sessionPrivate->session == NULL) {
56 /* open a session */
57 scd_status = SCDOpen(&sessionPrivate->session, sessionPrivate->name);
58 if (scd_status != SCD_OK) {
59 SCDLog(LOG_INFO, CFSTR("SCDOpen() failed: %s"), SCDError(scd_status));
60 return SCP_FAILED;
61 }
62 }
63
64 if (sessionPrivate->sessionKeyLock == NULL) {
65 /* create the session "lock" key */
66 sessionPrivate->sessionKeyLock = _SCPNotificationKey(sessionPrivate->prefsID,
67 sessionPrivate->perUser,
68 sessionPrivate->user,
69 kSCPKeyLock);
70 }
71
72 scd_status = SCDNotifierAdd(sessionPrivate->session,
73 sessionPrivate->sessionKeyLock,
74 0);
75 if (scd_status != SCD_OK) {
76 SCDLog(LOG_INFO, CFSTR("SCDNotifierAdd() failed: %s"), SCDError(scd_status));
77 scp_status = SCP_FAILED;
78 goto error;
79 }
80
81 handle = SCDHandleInit();
82 value = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
83 SCDHandleSetData(handle, value);
84 CFRelease(value);
85
86 while (TRUE) {
87 /*
88 * Attempt to acquire the lock
89 */
90 scd_status = SCDAddSession(sessionPrivate->session,
91 sessionPrivate->sessionKeyLock,
92 handle);
93 switch (scd_status) {
94 case SCD_OK :
95 scp_status = SCP_OK;
96 goto done;
97 case SCD_EXISTS :
98 if (!wait) {
99 scp_status = SCP_BUSY;
100 goto error;
101 }
102 break;
103 default :
104 SCDLog(LOG_INFO, CFSTR("SCDAddSession() failed: %s"), SCDError(scd_status));
105 scp_status = SCP_FAILED;
106 goto error;
107 }
108
109 /*
110 * Wait for the lock to be released
111 */
112 scd_status = SCDNotifierWait(sessionPrivate->session);
113 if (scd_status != SCD_OK) {
114 SCDLog(LOG_INFO, CFSTR("SCDAddSession() failed: %s"), SCDError(scd_status));
115 scp_status = SCP_FAILED;
116 goto error;
117 }
118 }
119
120 done :
121
122 SCDHandleRelease(handle);
123 handle = NULL;
124
125 scd_status = SCDNotifierRemove(sessionPrivate->session,
126 sessionPrivate->sessionKeyLock,
127 0);
128 if (scd_status != SCD_OK) {
129 SCDLog(LOG_INFO, CFSTR("SCDNotifierRemove() failed: %s"), SCDError(scd_status));
130 scp_status = SCP_FAILED;
131 goto error;
132 }
133
134 scd_status = SCDNotifierGetChanges(sessionPrivate->session, &changes);
135 if (scd_status != SCD_OK) {
136 SCDLog(LOG_INFO, CFSTR("SCDNotifierGetChanges() failed: %s"), SCDError(scd_status));
137 scp_status = SCP_FAILED;
138 goto error;
139 }
140 CFRelease(changes);
141
142 notRoot:
143
144 /*
145 * Check the signature
146 */
147 if (stat(sessionPrivate->path, &statBuf) == -1) {
148 if (errno == ENOENT) {
149 bzero(&statBuf, sizeof(statBuf));
150 } else {
151 SCDLog(LOG_DEBUG, CFSTR("stat() failed: %s"), strerror(errno));
152 scp_status = SCP_STALE;
153 goto error;
154 }
155 }
156
157 currentSignature = _SCPSignatureFromStatbuf(&statBuf);
158 if (!CFEqual(sessionPrivate->signature, currentSignature)) {
159 CFRelease(currentSignature);
160 scp_status = SCP_STALE;
161 goto error;
162 }
163 CFRelease(currentSignature);
164
165 sessionPrivate->locked = TRUE;
166 return SCD_OK;
167
168 error :
169
170 if (handle) SCDHandleRelease(handle);
171 return scp_status;
172 }