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/MemoryBuffer.h"
18#include "llvm/Support/Process.h"
19#include "llvm/Support/Signals.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cerrno>
22#include <chrono>
23#include <ctime>
24#include <memory>
25#include <system_error>
26#include <tuple>
27
28#ifdef _WIN32
29#include <windows.h>
30#endif
31#if LLVM_ON_UNIX
32#include <unistd.h>
33#endif
34
35#if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > 1050)
36#define USE_OSX_GETHOSTUUID 1
37#else
38#define USE_OSX_GETHOSTUUID 0
39#endif
40
41#if USE_OSX_GETHOSTUUID
42#include <uuid/uuid.h>
43#endif
44
45using namespace llvm;
46
47/// Attempt to read the lock file with the given name, if it exists.
48///
49/// \param LockFileName The name of the lock file to read.
50///
51/// \returns The process ID of the process that owns this lock file
52std::optional<LockFileManager::OwnedByAnother>
53LockFileManager::readLockFile(StringRef LockFileName) {
54 // Read the owning host and PID out of the lock file. If it appears that the
55 // owning process is dead, the lock file is invalid.
56 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
57 MemoryBuffer::getFile(Filename: LockFileName);
58 if (!MBOrErr) {
59 sys::fs::remove(path: LockFileName);
60 return std::nullopt;
61 }
62 MemoryBuffer &MB = *MBOrErr.get();
63
64 StringRef Hostname;
65 StringRef PIDStr;
66 std::tie(args&: Hostname, args&: PIDStr) = getToken(Source: MB.getBuffer(), Delimiters: " ");
67 PIDStr = PIDStr.substr(Start: PIDStr.find_first_not_of(C: ' '));
68 int PID;
69 if (!PIDStr.getAsInteger(Radix: 10, Result&: PID)) {
70 OwnedByAnother Owner;
71 Owner.OwnerHostName = Hostname;
72 Owner.OwnerPID = PID;
73 if (processStillExecuting(Hostname: Owner.OwnerHostName, PID: Owner.OwnerPID))
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
82static 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
113bool 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
127namespace {
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.
136class RemoveUniqueLockFileOnSignal {
137 StringRef Filename;
138 bool RemoveImmediately;
139public:
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
160LockFileManager::LockFileManager(StringRef FileName)
161 : FileName(FileName), Owner(OwnerUnknown{}) {}
162
163Expected<bool> LockFileManager::tryLock() {
164 assert(std::holds_alternative<OwnerUnknown>(Owner) &&
165 "lock has already been attempted");
166
167 SmallString<128> AbsoluteFileName(FileName);
168 if (std::error_code EC = sys::fs::make_absolute(path&: AbsoluteFileName))
169 return createStringError(EC, S: "failed to obtain absolute path for " +
170 AbsoluteFileName);
171 LockFileName = AbsoluteFileName;
172 LockFileName += ".lock";
173
174 // If the lock file already exists, don't bother to try to create our own
175 // lock file; it won't work anyway. Just figure out who owns this lock file.
176 if (auto LockFileOwner = readLockFile(LockFileName)) {
177 Owner = std::move(*LockFileOwner);
178 return false;
179 }
180
181 // Create a lock file that is unique to this instance.
182 UniqueLockFileName = LockFileName;
183 UniqueLockFileName += "-%%%%%%%%";
184 int UniqueLockFileID;
185 if (std::error_code EC = sys::fs::createUniqueFile(
186 Model: UniqueLockFileName, ResultFD&: UniqueLockFileID, ResultPath&: UniqueLockFileName))
187 return createStringError(EC, S: "failed to create unique file " +
188 UniqueLockFileName);
189
190 // Clean up the unique file on signal or scope exit.
191 RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
192
193 // Write our process ID to our unique lock file.
194 {
195 SmallString<256> HostID;
196 if (auto EC = getHostID(HostID))
197 return createStringError(EC, S: "failed to get host id");
198
199 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
200 Out << HostID << ' ' << sys::Process::getProcessId();
201 Out.close();
202
203 if (Out.has_error()) {
204 // We failed to write out PID, so report the error and fail.
205 Error Err = createStringError(EC: Out.error(),
206 S: "failed to write to " + UniqueLockFileName);
207 // Don't call report_fatal_error.
208 Out.clear_error();
209 return std::move(Err);
210 }
211 }
212
213 while (true) {
214 // Create a link from the lock file name. If this succeeds, we're done.
215 std::error_code EC =
216 sys::fs::create_link(to: UniqueLockFileName, from: LockFileName);
217 if (!EC) {
218 RemoveUniqueFile.lockAcquired();
219 Owner = OwnedByUs{};
220 return true;
221 }
222
223 if (EC != errc::file_exists)
224 return createStringError(EC, S: "failed to create link " + LockFileName +
225 " to " + UniqueLockFileName);
226
227 // Someone else managed to create the lock file first. Read the process ID
228 // from the lock file.
229 if (auto LockFileOwner = readLockFile(LockFileName)) {
230 Owner = std::move(*LockFileOwner);
231 return false;
232 }
233
234 if (!sys::fs::exists(Path: LockFileName)) {
235 // The previous owner released the lock file before we could read it.
236 // Try to get ownership again.
237 continue;
238 }
239
240 // There is a lock file that nobody owns; try to clean it up and get
241 // ownership.
242 if ((EC = sys::fs::remove(path: LockFileName)))
243 return createStringError(EC, S: "failed to remove lockfile " +
244 UniqueLockFileName);
245 }
246}
247
248LockFileManager::~LockFileManager() {
249 if (!std::holds_alternative<OwnedByUs>(v: Owner))
250 return;
251
252 // Since we own the lock, remove the lock file and our own unique lock file.
253 sys::fs::remove(path: LockFileName);
254 sys::fs::remove(path: UniqueLockFileName);
255 // The unique file is now gone, so remove it from the signal handler. This
256 // matches a sys::RemoveFileOnSignal() in LockFileManager().
257 sys::DontRemoveFileOnSignal(Filename: UniqueLockFileName);
258}
259
260WaitForUnlockResult
261LockFileManager::waitForUnlockFor(std::chrono::seconds MaxSeconds) {
262 auto *LockFileOwner = std::get_if<OwnedByAnother>(ptr: &Owner);
263 assert(LockFileOwner &&
264 "waiting for lock to be unlocked without knowing the owner");
265
266 // Since we don't yet have an event-based method to wait for the lock file,
267 // use randomized exponential backoff, similar to Ethernet collision
268 // algorithm. This improves performance on machines with high core counts
269 // when the file lock is heavily contended by multiple clang processes
270 using namespace std::chrono_literals;
271 ExponentialBackoff Backoff(MaxSeconds, 10ms, 500ms);
272
273 // Wait first as this is only called when the lock is known to be held.
274 while (Backoff.waitForNextAttempt()) {
275 // FIXME: implement event-based waiting
276 if (sys::fs::access(Path: LockFileName.c_str(), Mode: sys::fs::AccessMode::Exist) ==
277 errc::no_such_file_or_directory)
278 return WaitForUnlockResult::Success;
279
280 // If the process owning the lock died without cleaning up, just bail out.
281 if (!processStillExecuting(HostID: LockFileOwner->OwnerHostName,
282 PID: LockFileOwner->OwnerPID))
283 return WaitForUnlockResult::OwnerDied;
284 }
285
286 // Give up.
287 return WaitForUnlockResult::Timeout;
288}
289
290std::error_code LockFileManager::unsafeMaybeUnlock() {
291 return sys::fs::remove(path: LockFileName);
292}
293