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