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/Config/llvm-config.h" // for LLVM_ON_UNIX
13#include "llvm/Support/Errc.h"
14#include "llvm/Support/ErrorOr.h"
15#include "llvm/Support/ExponentialBackoff.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/IOSandbox.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/Process.h"
21#include "llvm/Support/Signals.h"
22#include "llvm/Support/raw_ostream.h"
23#include <cerrno>
24#include <chrono>
25#include <ctime>
26#include <memory>
27#include <system_error>
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
47using 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
54std::optional<LockFileManager::OwnedByAnother>
55LockFileManager::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 OwnedByAnother Owner;
73 Owner.OwnerHostName = Hostname;
74 Owner.OwnerPID = PID;
75 if (processStillExecuting(Hostname: Owner.OwnerHostName, PID: Owner.OwnerPID))
76 return Owner;
77 }
78
79 // Delete the lock file. It's invalid anyway.
80 sys::fs::remove(path: LockFileName);
81 return std::nullopt;
82}
83
84static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
85 HostID.clear();
86
87#if USE_OSX_GETHOSTUUID
88 // On OS X, use the more stable hardware UUID instead of hostname.
89 struct timespec wait = {1, 0}; // 1 second.
90 uuid_t uuid;
91 if (gethostuuid(uuid, &wait) != 0)
92 return errnoAsErrorCode();
93
94 uuid_string_t UUIDStr;
95 uuid_unparse(uuid, UUIDStr);
96 StringRef UUIDRef(UUIDStr);
97 HostID.append(UUIDRef.begin(), UUIDRef.end());
98
99#elif LLVM_ON_UNIX
100 char HostName[256];
101 HostName[255] = 0;
102 HostName[0] = 0;
103 gethostname(name: HostName, len: 255);
104 StringRef HostNameRef(HostName);
105 HostID.append(in_start: HostNameRef.begin(), in_end: HostNameRef.end());
106
107#else
108 StringRef Dummy("localhost");
109 HostID.append(Dummy.begin(), Dummy.end());
110#endif
111
112 return std::error_code();
113}
114
115bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
116#if LLVM_ON_UNIX && !defined(__ANDROID__)
117 SmallString<256> StoredHostID;
118 if (getHostID(HostID&: StoredHostID))
119 return true; // Conservatively assume it's executing on error.
120
121 // Check whether the process is dead. If so, we're done.
122 if (StoredHostID == HostID && getsid(pid: PID) == -1 && errno == ESRCH)
123 return false;
124#endif
125
126 return true;
127}
128
129namespace {
130
131/// An RAII helper object ensure that the unique lock file is removed.
132///
133/// Ensures that if there is an error or a signal before we finish acquiring the
134/// lock, the unique file will be removed. And if we successfully take the lock,
135/// the signal handler is left in place so that signals while the lock is held
136/// will remove the unique lock file. The caller should ensure there is a
137/// matching call to sys::DontRemoveFileOnSignal when the lock is released.
138class RemoveUniqueLockFileOnSignal {
139 StringRef Filename;
140 bool RemoveImmediately;
141public:
142 RemoveUniqueLockFileOnSignal(StringRef Name)
143 : Filename(Name), RemoveImmediately(true) {
144 sys::RemoveFileOnSignal(Filename, ErrMsg: nullptr);
145 }
146
147 ~RemoveUniqueLockFileOnSignal() {
148 if (!RemoveImmediately) {
149 // Leave the signal handler enabled. It will be removed when the lock is
150 // released.
151 return;
152 }
153 sys::fs::remove(path: Filename);
154 sys::DontRemoveFileOnSignal(Filename);
155 }
156
157 void lockAcquired() { RemoveImmediately = false; }
158};
159
160} // end anonymous namespace
161
162LockFileManager::LockFileManager(StringRef FileName)
163 : FileName(FileName), Owner(OwnerUnknown{}) {}
164
165Expected<bool> LockFileManager::tryLock() {
166 auto BypassSandbox = sys::sandbox::scopedDisable();
167
168 assert(std::holds_alternative<OwnerUnknown>(Owner) &&
169 "lock has already been attempted");
170
171 SmallString<128> AbsoluteFileName(FileName);
172 if (std::error_code EC = sys::fs::make_absolute(path&: AbsoluteFileName))
173 return createStringError(EC, S: "failed to obtain absolute path for " +
174 AbsoluteFileName);
175 LockFileName = AbsoluteFileName;
176 LockFileName += ".lock";
177
178 // If the lock file already exists, don't bother to try to create our own
179 // lock file; it won't work anyway. Just figure out who owns this lock file.
180 if (auto LockFileOwner = readLockFile(LockFileName)) {
181 Owner = std::move(*LockFileOwner);
182 return false;
183 }
184
185 // Create a lock file that is unique to this instance.
186 int UniqueLockFileID;
187 {
188 SmallString<128> UniqueLockFilePattern = LockFileName;
189 UniqueLockFilePattern += "-%%%%%%%%";
190 SmallString<128> UniquePath;
191 std::error_code EC = sys::fs::createUniqueFile(
192 Model: UniqueLockFilePattern, ResultFD&: UniqueLockFileID, ResultPath&: UniquePath);
193 if (EC == errc::no_such_file_or_directory) {
194 SmallString<128> Dir = sys::path::parent_path(path: UniqueLockFilePattern);
195 if (!Dir.empty()) {
196 if (std::error_code DirEC = sys::fs::create_directories(path: Dir))
197 return createStringError(EC: DirEC,
198 S: "failed to create lock directory " + Dir);
199 }
200
201 // Retry creating lock file
202 EC = sys::fs::createUniqueFile(Model: UniqueLockFilePattern, ResultFD&: UniqueLockFileID,
203 ResultPath&: UniquePath);
204 }
205
206 if (EC)
207 return createStringError(EC,
208 S: "failed to create unique file " + UniquePath);
209
210 UniqueLockFileName = UniquePath;
211 }
212
213 // Clean up the unique file on signal or scope exit.
214 RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
215
216 // Write our process ID to our unique lock file.
217 {
218 SmallString<256> HostID;
219 if (auto EC = getHostID(HostID))
220 return createStringError(EC, S: "failed to get host id");
221
222 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
223 Out << HostID << ' ' << sys::Process::getProcessId();
224 Out.close();
225
226 if (Out.has_error()) {
227 // We failed to write out PID, so report the error and fail.
228 Error Err = createStringError(EC: Out.error(),
229 S: "failed to write to " + UniqueLockFileName);
230 // Don't call report_fatal_error.
231 Out.clear_error();
232 return std::move(Err);
233 }
234 }
235
236 while (true) {
237 // Create a link from the lock file name. If this succeeds, we're done.
238 std::error_code EC =
239 sys::fs::create_link(to: UniqueLockFileName, from: LockFileName);
240 if (!EC) {
241 RemoveUniqueFile.lockAcquired();
242 Owner = OwnedByUs{};
243 return true;
244 }
245
246 if (EC != errc::file_exists)
247 return createStringError(EC, S: "failed to create link " + LockFileName +
248 " to " + UniqueLockFileName);
249
250 // Someone else managed to create the lock file first. Read the process ID
251 // from the lock file.
252 if (auto LockFileOwner = readLockFile(LockFileName)) {
253 Owner = std::move(*LockFileOwner);
254 return false;
255 }
256
257 if (!sys::fs::exists(Path: LockFileName)) {
258 // The previous owner released the lock file before we could read it.
259 // Try to get ownership again.
260 continue;
261 }
262
263 // There is a lock file that nobody owns; try to clean it up and get
264 // ownership.
265 if ((EC = sys::fs::remove(path: LockFileName)))
266 return createStringError(EC, S: "failed to remove lockfile " +
267 UniqueLockFileName);
268 }
269}
270
271LockFileManager::~LockFileManager() {
272 auto BypassSandbox = sys::sandbox::scopedDisable();
273
274 if (!std::holds_alternative<OwnedByUs>(v: Owner))
275 return;
276
277 // Since we own the lock, remove the lock file and our own unique lock file.
278 sys::fs::remove(path: LockFileName);
279 sys::fs::remove(path: UniqueLockFileName);
280 // The unique file is now gone, so remove it from the signal handler. This
281 // matches a sys::RemoveFileOnSignal() in LockFileManager().
282 sys::DontRemoveFileOnSignal(Filename: UniqueLockFileName);
283}
284
285WaitForUnlockResult
286LockFileManager::waitForUnlockFor(std::chrono::seconds MaxSeconds) {
287 auto BypassSandbox = sys::sandbox::scopedDisable();
288
289 auto *LockFileOwner = std::get_if<OwnedByAnother>(ptr: &Owner);
290 assert(LockFileOwner &&
291 "waiting for lock to be unlocked without knowing the owner");
292
293 // Since we don't yet have an event-based method to wait for the lock file,
294 // use randomized exponential backoff, similar to Ethernet collision
295 // algorithm. This improves performance on machines with high core counts
296 // when the file lock is heavily contended by multiple clang processes
297 using namespace std::chrono_literals;
298 ExponentialBackoff Backoff(MaxSeconds, 10ms, 500ms);
299
300 // Wait first as this is only called when the lock is known to be held.
301 while (Backoff.waitForNextAttempt()) {
302 // FIXME: implement event-based waiting
303 if (sys::fs::access(Path: LockFileName.c_str(), Mode: sys::fs::AccessMode::Exist) ==
304 errc::no_such_file_or_directory)
305 return WaitForUnlockResult::Success;
306
307 // If the process owning the lock died without cleaning up, just bail out.
308 if (!processStillExecuting(HostID: LockFileOwner->OwnerHostName,
309 PID: LockFileOwner->OwnerPID))
310 return WaitForUnlockResult::OwnerDied;
311 }
312
313 // Give up.
314 return WaitForUnlockResult::Timeout;
315}
316
317std::error_code LockFileManager::unsafeUnlock() {
318 auto BypassSandbox = sys::sandbox::scopedDisable();
319
320 return sys::fs::remove(path: LockFileName);
321}
322