1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright (C) 2010-2016 Joel Rosdahl
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "ccache.h"
// This function acquires a lockfile for the given path. Returns true if the
// lock was acquired, otherwise false. If the lock has been considered stale
// for the number of microseconds specified by staleness_limit, the function
// will (if possible) break the lock and then try to acquire it again. The
// staleness limit should be reasonably larger than the longest time the lock
// can be expected to be held, and the updates of the locked path should
// probably be made with an atomic rename(2) to avoid corruption in the rare
// case that the lock is broken by another process.
bool
lockfile_acquire(const char *path, unsigned staleness_limit)
{
char *lockfile = format("%s.lock", path);
char *my_content = NULL;
char *content = NULL;
char *initial_content = NULL;
const char *hostname = get_hostname();
bool acquired = false;
unsigned to_sleep = 1000; // Microseconds.
unsigned slept = 0; // Microseconds.
while (true) {
free(my_content);
my_content = format("%s:%d:%d", hostname, (int)getpid(), (int)time(NULL));
#ifdef _WIN32
int fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0666);
if (fd == -1) {
int saved_errno = errno;
cc_log("lockfile_acquire: open WRONLY %s: %s", lockfile, strerror(errno));
if (saved_errno == ENOENT) {
// Directory doesn't exist?
if (create_parent_dirs(lockfile) == 0) {
// OK. Retry.
continue;
}
}
if (saved_errno != EEXIST) {
// Directory doesn't exist or isn't writable?
goto out;
}
// Someone else has the lock.
fd = open(lockfile, O_RDONLY|O_BINARY);
if (fd == -1) {
if (errno == ENOENT) {
// The file was removed after the open() call above, so retry
// acquiring it.
continue;
} else {
cc_log("lockfile_acquire: open RDONLY %s: %s",
lockfile, strerror(errno));
goto out;
}
}
free(content);
const size_t bufsize = 1024;
content = x_malloc(bufsize);
int len = read(fd, content, bufsize - 1);
if (len == -1) {
cc_log("lockfile_acquire: read %s: %s", lockfile, strerror(errno));
close(fd);
goto out;
}
close(fd);
content[len] = '\0';
} else {
// We got the lock.
if (write(fd, my_content, strlen(my_content)) == -1) {
cc_log("lockfile_acquire: write %s: %s", lockfile, strerror(errno));
close(fd);
x_unlink(lockfile);
goto out;
}
close(fd);
acquired = true;
goto out;
}
#else
if (symlink(my_content, lockfile) == 0) {
// We got the lock.
acquired = true;
goto out;
}
int saved_errno = errno;
cc_log("lockfile_acquire: symlink %s: %s", lockfile, strerror(saved_errno));
if (saved_errno == ENOENT) {
// Directory doesn't exist?
if (create_parent_dirs(lockfile) == 0) {
// OK. Retry.
continue;
}
}
if (saved_errno == EPERM) {
// The file system does not support symbolic links. We have no choice but
// to grant the lock anyway.
acquired = true;
goto out;
}
if (saved_errno != EEXIST) {
// Directory doesn't exist or isn't writable?
goto out;
}
free(content);
content = x_readlink(lockfile);
// cppcheck-suppress nullPointer - false positive
if (!content) {
if (errno == ENOENT) {
// The symlink was removed after the symlink() call above, so retry
// acquiring it.
continue;
} else {
cc_log("lockfile_acquire: readlink %s: %s", lockfile, strerror(errno));
goto out;
}
}
#endif
if (str_eq(content, my_content)) {
// Lost NFS reply?
cc_log("lockfile_acquire: symlink %s failed but we got the lock anyway",
lockfile);
acquired = true;
goto out;
}
// A possible improvement here would be to check if the process holding the
// lock is still alive and break the lock early if it isn't.
cc_log("lockfile_acquire: lock info for %s: %s", lockfile, content);
if (!initial_content) {
initial_content = x_strdup(content);
}
if (slept > staleness_limit) {
if (str_eq(content, initial_content)) {
// The lock seems to be stale -- break it.
cc_log("lockfile_acquire: breaking %s", lockfile);
// Try to acquire path.lock.lock:
if (lockfile_acquire(lockfile, staleness_limit)) {
lockfile_release(path); // Remove path.lock
lockfile_release(lockfile); // Remove path.lock.lock
to_sleep = 1000;
slept = 0;
continue;
}
}
cc_log("lockfile_acquire: gave up acquiring %s", lockfile);
goto out;
}
cc_log("lockfile_acquire: failed to acquire %s; sleeping %u microseconds",
lockfile, to_sleep);
usleep(to_sleep);
slept += to_sleep;
to_sleep *= 2;
}
out:
if (acquired) {
cc_log("Acquired lock %s", lockfile);
} else {
cc_log("Failed to acquire lock %s", lockfile);
}
free(lockfile);
free(my_content);
free(initial_content);
free(content);
return acquired;
}
// Release the lockfile for the given path. Assumes that we are the legitimate
// owner.
void
lockfile_release(const char *path)
{
char *lockfile = format("%s.lock", path);
cc_log("Releasing lock %s", lockfile);
tmp_unlink(lockfile);
free(lockfile);
}
#ifdef TEST_LOCKFILE
int
main(int argc, char **argv)
{
extern char *cache_logfile;
cache_logfile = "/dev/stdout";
if (argc == 4) {
unsigned staleness_limit = atoi(argv[1]);
if (str_eq(argv[2], "acquire")) {
return lockfile_acquire(argv[3], staleness_limit) == 0;
} else if (str_eq(argv[2], "release")) {
lockfile_release(argv[3]);
return 0;
}
}
fprintf(stderr,
"Usage: testlockfile <staleness_limit> <acquire|release> <path>\n");
return 1;
}
#endif
|