1 | //===--- LockFileManager.cpp - File-level Locking Utility------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/Support/LockFileManager.h" |
10 | #include "llvm/ADT/SmallVector.h" |
11 | #include "llvm/ADT/StringExtras.h" |
12 | #include "llvm/Support/Errc.h" |
13 | #include "llvm/Support/ErrorOr.h" |
14 | #include "llvm/Support/ExponentialBackoff.h" |
15 | #include "llvm/Support/FileSystem.h" |
16 | #include "llvm/Support/MemoryBuffer.h" |
17 | #include "llvm/Support/Process.h" |
18 | #include "llvm/Support/Signals.h" |
19 | #include "llvm/Support/raw_ostream.h" |
20 | #include <cerrno> |
21 | #include <chrono> |
22 | #include <ctime> |
23 | #include <memory> |
24 | #include <sys/stat.h> |
25 | #include <sys/types.h> |
26 | #include <system_error> |
27 | #include <thread> |
28 | #include <tuple> |
29 | |
30 | #ifdef _WIN32 |
31 | #include <windows.h> |
32 | #endif |
33 | #if LLVM_ON_UNIX |
34 | #include <unistd.h> |
35 | #endif |
36 | |
37 | #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > 1050) |
38 | #define USE_OSX_GETHOSTUUID 1 |
39 | #else |
40 | #define USE_OSX_GETHOSTUUID 0 |
41 | #endif |
42 | |
43 | #if USE_OSX_GETHOSTUUID |
44 | #include <uuid/uuid.h> |
45 | #endif |
46 | |
47 | using namespace llvm; |
48 | |
49 | /// Attempt to read the lock file with the given name, if it exists. |
50 | /// |
51 | /// \param LockFileName The name of the lock file to read. |
52 | /// |
53 | /// \returns The process ID of the process that owns this lock file |
54 | std::optional<std::pair<std::string, int>> |
55 | LockFileManager::readLockFile(StringRef LockFileName) { |
56 | // Read the owning host and PID out of the lock file. If it appears that the |
57 | // owning process is dead, the lock file is invalid. |
58 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
59 | MemoryBuffer::getFile(Filename: LockFileName); |
60 | if (!MBOrErr) { |
61 | sys::fs::remove(path: LockFileName); |
62 | return std::nullopt; |
63 | } |
64 | MemoryBuffer &MB = *MBOrErr.get(); |
65 | |
66 | StringRef Hostname; |
67 | StringRef PIDStr; |
68 | std::tie(args&: Hostname, args&: PIDStr) = getToken(Source: MB.getBuffer(), Delimiters: " " ); |
69 | PIDStr = PIDStr.substr(Start: PIDStr.find_first_not_of(C: ' ')); |
70 | int PID; |
71 | if (!PIDStr.getAsInteger(Radix: 10, Result&: PID)) { |
72 | auto Owner = std::make_pair(x: std::string(Hostname), y&: PID); |
73 | if (processStillExecuting(Hostname: Owner.first, PID: Owner.second)) |
74 | return Owner; |
75 | } |
76 | |
77 | // Delete the lock file. It's invalid anyway. |
78 | sys::fs::remove(path: LockFileName); |
79 | return std::nullopt; |
80 | } |
81 | |
82 | static std::error_code getHostID(SmallVectorImpl<char> &HostID) { |
83 | HostID.clear(); |
84 | |
85 | #if USE_OSX_GETHOSTUUID |
86 | // On OS X, use the more stable hardware UUID instead of hostname. |
87 | struct timespec wait = {1, 0}; // 1 second. |
88 | uuid_t uuid; |
89 | if (gethostuuid(uuid, &wait) != 0) |
90 | return errnoAsErrorCode(); |
91 | |
92 | uuid_string_t UUIDStr; |
93 | uuid_unparse(uuid, UUIDStr); |
94 | StringRef UUIDRef(UUIDStr); |
95 | HostID.append(UUIDRef.begin(), UUIDRef.end()); |
96 | |
97 | #elif LLVM_ON_UNIX |
98 | char HostName[256]; |
99 | HostName[255] = 0; |
100 | HostName[0] = 0; |
101 | gethostname(name: HostName, len: 255); |
102 | StringRef HostNameRef(HostName); |
103 | HostID.append(in_start: HostNameRef.begin(), in_end: HostNameRef.end()); |
104 | |
105 | #else |
106 | StringRef Dummy("localhost" ); |
107 | HostID.append(Dummy.begin(), Dummy.end()); |
108 | #endif |
109 | |
110 | return std::error_code(); |
111 | } |
112 | |
113 | bool LockFileManager::processStillExecuting(StringRef HostID, int PID) { |
114 | #if LLVM_ON_UNIX && !defined(__ANDROID__) |
115 | SmallString<256> StoredHostID; |
116 | if (getHostID(HostID&: StoredHostID)) |
117 | return true; // Conservatively assume it's executing on error. |
118 | |
119 | // Check whether the process is dead. If so, we're done. |
120 | if (StoredHostID == HostID && getsid(pid: PID) == -1 && errno == ESRCH) |
121 | return false; |
122 | #endif |
123 | |
124 | return true; |
125 | } |
126 | |
127 | namespace { |
128 | |
129 | /// An RAII helper object ensure that the unique lock file is removed. |
130 | /// |
131 | /// Ensures that if there is an error or a signal before we finish acquiring the |
132 | /// lock, the unique file will be removed. And if we successfully take the lock, |
133 | /// the signal handler is left in place so that signals while the lock is held |
134 | /// will remove the unique lock file. The caller should ensure there is a |
135 | /// matching call to sys::DontRemoveFileOnSignal when the lock is released. |
136 | class RemoveUniqueLockFileOnSignal { |
137 | StringRef Filename; |
138 | bool RemoveImmediately; |
139 | public: |
140 | RemoveUniqueLockFileOnSignal(StringRef Name) |
141 | : Filename(Name), RemoveImmediately(true) { |
142 | sys::RemoveFileOnSignal(Filename, ErrMsg: nullptr); |
143 | } |
144 | |
145 | ~RemoveUniqueLockFileOnSignal() { |
146 | if (!RemoveImmediately) { |
147 | // Leave the signal handler enabled. It will be removed when the lock is |
148 | // released. |
149 | return; |
150 | } |
151 | sys::fs::remove(path: Filename); |
152 | sys::DontRemoveFileOnSignal(Filename); |
153 | } |
154 | |
155 | void lockAcquired() { RemoveImmediately = false; } |
156 | }; |
157 | |
158 | } // end anonymous namespace |
159 | |
160 | LockFileManager::LockFileManager(StringRef FileName) |
161 | { |
162 | this->FileName = FileName; |
163 | if (std::error_code EC = sys::fs::make_absolute(path&: this->FileName)) { |
164 | std::string S("failed to obtain absolute path for " ); |
165 | S.append(str: std::string(this->FileName)); |
166 | setError(EC, ErrorMsg: S); |
167 | return; |
168 | } |
169 | LockFileName = this->FileName; |
170 | LockFileName += ".lock" ; |
171 | |
172 | // If the lock file already exists, don't bother to try to create our own |
173 | // lock file; it won't work anyway. Just figure out who owns this lock file. |
174 | if ((Owner = readLockFile(LockFileName))) |
175 | return; |
176 | |
177 | // Create a lock file that is unique to this instance. |
178 | UniqueLockFileName = LockFileName; |
179 | UniqueLockFileName += "-%%%%%%%%" ; |
180 | int UniqueLockFileID; |
181 | if (std::error_code EC = sys::fs::createUniqueFile( |
182 | Model: UniqueLockFileName, ResultFD&: UniqueLockFileID, ResultPath&: UniqueLockFileName)) { |
183 | std::string S("failed to create unique file " ); |
184 | S.append(str: std::string(UniqueLockFileName)); |
185 | setError(EC, ErrorMsg: S); |
186 | return; |
187 | } |
188 | |
189 | // Write our process ID to our unique lock file. |
190 | { |
191 | SmallString<256> HostID; |
192 | if (auto EC = getHostID(HostID)) { |
193 | setError(EC, ErrorMsg: "failed to get host id" ); |
194 | return; |
195 | } |
196 | |
197 | raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); |
198 | Out << HostID << ' ' << sys::Process::getProcessId(); |
199 | Out.close(); |
200 | |
201 | if (Out.has_error()) { |
202 | // We failed to write out PID, so report the error, remove the |
203 | // unique lock file, and fail. |
204 | std::string S("failed to write to " ); |
205 | S.append(str: std::string(UniqueLockFileName)); |
206 | setError(EC: Out.error(), ErrorMsg: S); |
207 | sys::fs::remove(path: UniqueLockFileName); |
208 | // Don't call report_fatal_error. |
209 | Out.clear_error(); |
210 | return; |
211 | } |
212 | } |
213 | |
214 | // Clean up the unique file on signal, which also releases the lock if it is |
215 | // held since the .lock symlink will point to a nonexistent file. |
216 | RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName); |
217 | |
218 | while (true) { |
219 | // Create a link from the lock file name. If this succeeds, we're done. |
220 | std::error_code EC = |
221 | sys::fs::create_link(to: UniqueLockFileName, from: LockFileName); |
222 | if (!EC) { |
223 | RemoveUniqueFile.lockAcquired(); |
224 | return; |
225 | } |
226 | |
227 | if (EC != errc::file_exists) { |
228 | std::string S("failed to create link " ); |
229 | raw_string_ostream OSS(S); |
230 | OSS << LockFileName.str() << " to " << UniqueLockFileName.str(); |
231 | setError(EC, ErrorMsg: S); |
232 | return; |
233 | } |
234 | |
235 | // Someone else managed to create the lock file first. Read the process ID |
236 | // from the lock file. |
237 | if ((Owner = readLockFile(LockFileName))) { |
238 | // Wipe out our unique lock file (it's useless now) |
239 | sys::fs::remove(path: UniqueLockFileName); |
240 | return; |
241 | } |
242 | |
243 | if (!sys::fs::exists(Path: LockFileName)) { |
244 | // The previous owner released the lock file before we could read it. |
245 | // Try to get ownership again. |
246 | continue; |
247 | } |
248 | |
249 | // There is a lock file that nobody owns; try to clean it up and get |
250 | // ownership. |
251 | if ((EC = sys::fs::remove(path: LockFileName))) { |
252 | std::string S("failed to remove lockfile " ); |
253 | S.append(str: std::string(UniqueLockFileName)); |
254 | setError(EC, ErrorMsg: S); |
255 | return; |
256 | } |
257 | } |
258 | } |
259 | |
260 | LockFileManager::LockFileState LockFileManager::getState() const { |
261 | if (Owner) |
262 | return LFS_Shared; |
263 | |
264 | if (ErrorCode) |
265 | return LFS_Error; |
266 | |
267 | return LFS_Owned; |
268 | } |
269 | |
270 | std::string LockFileManager::getErrorMessage() const { |
271 | if (ErrorCode) { |
272 | std::string Str(ErrorDiagMsg); |
273 | std::string ErrCodeMsg = ErrorCode.message(); |
274 | raw_string_ostream OSS(Str); |
275 | if (!ErrCodeMsg.empty()) |
276 | OSS << ": " << ErrCodeMsg; |
277 | return Str; |
278 | } |
279 | return "" ; |
280 | } |
281 | |
282 | LockFileManager::~LockFileManager() { |
283 | if (getState() != LFS_Owned) |
284 | return; |
285 | |
286 | // Since we own the lock, remove the lock file and our own unique lock file. |
287 | sys::fs::remove(path: LockFileName); |
288 | sys::fs::remove(path: UniqueLockFileName); |
289 | // The unique file is now gone, so remove it from the signal handler. This |
290 | // matches a sys::RemoveFileOnSignal() in LockFileManager(). |
291 | sys::DontRemoveFileOnSignal(Filename: UniqueLockFileName); |
292 | } |
293 | |
294 | LockFileManager::WaitForUnlockResult |
295 | LockFileManager::waitForUnlock(const unsigned MaxSeconds) { |
296 | if (getState() != LFS_Shared) |
297 | return Res_Success; |
298 | |
299 | // Since we don't yet have an event-based method to wait for the lock file, |
300 | // use randomized exponential backoff, similar to Ethernet collision |
301 | // algorithm. This improves performance on machines with high core counts |
302 | // when the file lock is heavily contended by multiple clang processes |
303 | using namespace std::chrono_literals; |
304 | ExponentialBackoff Backoff(std::chrono::seconds(MaxSeconds), 10ms, 500ms); |
305 | |
306 | // Wait first as this is only called when the lock is known to be held. |
307 | while (Backoff.waitForNextAttempt()) { |
308 | // FIXME: implement event-based waiting |
309 | if (sys::fs::access(Path: LockFileName.c_str(), Mode: sys::fs::AccessMode::Exist) == |
310 | errc::no_such_file_or_directory) { |
311 | // If the original file wasn't created, somone thought the lock was dead. |
312 | if (!sys::fs::exists(Path: FileName)) |
313 | return Res_OwnerDied; |
314 | return Res_Success; |
315 | } |
316 | |
317 | // If the process owning the lock died without cleaning up, just bail out. |
318 | if (!processStillExecuting(HostID: (*Owner).first, PID: (*Owner).second)) |
319 | return Res_OwnerDied; |
320 | } |
321 | |
322 | // Give up. |
323 | return Res_Timeout; |
324 | } |
325 | |
326 | std::error_code LockFileManager::unsafeRemoveLockFile() { |
327 | return sys::fs::remove(path: LockFileName); |
328 | } |
329 | |