LLVM  12.0.0git
LockFileManager.cpp
Go to the documentation of this file.
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 
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/ErrorOr.h"
17 #include "llvm/Support/Process.h"
18 #include "llvm/Support/Signals.h"
20 #include <cerrno>
21 #include <chrono>
22 #include <ctime>
23 #include <memory>
24 #include <random>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <system_error>
28 #include <thread>
29 #include <tuple>
30 
31 #ifdef _WIN32
32 #include <windows.h>
33 #endif
34 #if LLVM_ON_UNIX
35 #include <unistd.h>
36 #endif
37 
38 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
39 #define USE_OSX_GETHOSTUUID 1
40 #else
41 #define USE_OSX_GETHOSTUUID 0
42 #endif
43 
44 #if USE_OSX_GETHOSTUUID
45 #include <uuid/uuid.h>
46 #endif
47 
48 using namespace llvm;
49 
50 /// Attempt to read the lock file with the given name, if it exists.
51 ///
52 /// \param LockFileName The name of the lock file to read.
53 ///
54 /// \returns The process ID of the process that owns this lock file
56 LockFileManager::readLockFile(StringRef LockFileName) {
57  // Read the owning host and PID out of the lock file. If it appears that the
58  // owning process is dead, the lock file is invalid.
60  MemoryBuffer::getFile(LockFileName);
61  if (!MBOrErr) {
62  sys::fs::remove(LockFileName);
63  return None;
64  }
65  MemoryBuffer &MB = *MBOrErr.get();
66 
67  StringRef Hostname;
68  StringRef PIDStr;
69  std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
70  PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
71  int PID;
72  if (!PIDStr.getAsInteger(10, PID)) {
73  auto Owner = std::make_pair(std::string(Hostname), PID);
74  if (processStillExecuting(Owner.first, Owner.second))
75  return Owner;
76  }
77 
78  // Delete the lock file. It's invalid anyway.
79  sys::fs::remove(LockFileName);
80  return None;
81 }
82 
83 static 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 std::error_code(errno, std::system_category());
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(HostName, 255);
103  StringRef HostNameRef(HostName);
104  HostID.append(HostNameRef.begin(), 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 
114 bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
115 #if LLVM_ON_UNIX && !defined(__ANDROID__)
116  SmallString<256> StoredHostID;
117  if (getHostID(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) == -1 && errno == ESRCH)
122  return false;
123 #endif
124 
125  return true;
126 }
127 
128 namespace {
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.
137 class RemoveUniqueLockFileOnSignal {
138  StringRef Filename;
139  bool RemoveImmediately;
140 public:
141  RemoveUniqueLockFileOnSignal(StringRef Name)
142  : Filename(Name), RemoveImmediately(true) {
143  sys::RemoveFileOnSignal(Filename, 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(Filename);
153  sys::DontRemoveFileOnSignal(Filename);
154  }
155 
156  void lockAcquired() { RemoveImmediately = false; }
157 };
158 
159 } // end anonymous namespace
160 
161 LockFileManager::LockFileManager(StringRef FileName)
162 {
163  this->FileName = FileName;
164  if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
165  std::string S("failed to obtain absolute path for ");
166  S.append(std::string(this->FileName.str()));
167  setError(EC, S);
168  return;
169  }
170  LockFileName = this->FileName;
171  LockFileName += ".lock";
172 
173  // If the lock file already exists, don't bother to try to create our own
174  // lock file; it won't work anyway. Just figure out who owns this lock file.
175  if ((Owner = readLockFile(LockFileName)))
176  return;
177 
178  // Create a lock file that is unique to this instance.
179  UniqueLockFileName = LockFileName;
180  UniqueLockFileName += "-%%%%%%%%";
181  int UniqueLockFileID;
182  if (std::error_code EC = sys::fs::createUniqueFile(
183  UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
184  std::string S("failed to create unique file ");
185  S.append(std::string(UniqueLockFileName.str()));
186  setError(EC, S);
187  return;
188  }
189 
190  // Write our process ID to our unique lock file.
191  {
192  SmallString<256> HostID;
193  if (auto EC = getHostID(HostID)) {
194  setError(EC, "failed to get host id");
195  return;
196  }
197 
198  raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
199  Out << HostID << ' ' << sys::Process::getProcessId();
200  Out.close();
201 
202  if (Out.has_error()) {
203  // We failed to write out PID, so report the error, remove the
204  // unique lock file, and fail.
205  std::string S("failed to write to ");
206  S.append(std::string(UniqueLockFileName.str()));
207  setError(Out.error(), S);
208  sys::fs::remove(UniqueLockFileName);
209  return;
210  }
211  }
212 
213  // Clean up the unique file on signal, which also releases the lock if it is
214  // held since the .lock symlink will point to a nonexistent file.
215  RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
216 
217  while (true) {
218  // Create a link from the lock file name. If this succeeds, we're done.
219  std::error_code EC =
220  sys::fs::create_link(UniqueLockFileName, LockFileName);
221  if (!EC) {
222  RemoveUniqueFile.lockAcquired();
223  return;
224  }
225 
226  if (EC != errc::file_exists) {
227  std::string S("failed to create link ");
228  raw_string_ostream OSS(S);
229  OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
230  setError(EC, OSS.str());
231  return;
232  }
233 
234  // Someone else managed to create the lock file first. Read the process ID
235  // from the lock file.
236  if ((Owner = readLockFile(LockFileName))) {
237  // Wipe out our unique lock file (it's useless now)
238  sys::fs::remove(UniqueLockFileName);
239  return;
240  }
241 
242  if (!sys::fs::exists(LockFileName)) {
243  // The previous owner released the lock file before we could read it.
244  // Try to get ownership again.
245  continue;
246  }
247 
248  // There is a lock file that nobody owns; try to clean it up and get
249  // ownership.
250  if ((EC = sys::fs::remove(LockFileName))) {
251  std::string S("failed to remove lockfile ");
252  S.append(std::string(UniqueLockFileName.str()));
253  setError(EC, S);
254  return;
255  }
256  }
257 }
258 
260  if (Owner)
261  return LFS_Shared;
262 
263  if (ErrorCode)
264  return LFS_Error;
265 
266  return LFS_Owned;
267 }
268 
269 std::string LockFileManager::getErrorMessage() const {
270  if (ErrorCode) {
271  std::string Str(ErrorDiagMsg);
272  std::string ErrCodeMsg = ErrorCode.message();
273  raw_string_ostream OSS(Str);
274  if (!ErrCodeMsg.empty())
275  OSS << ": " << ErrCodeMsg;
276  return OSS.str();
277  }
278  return "";
279 }
280 
282  if (getState() != LFS_Owned)
283  return;
284 
285  // Since we own the lock, remove the lock file and our own unique lock file.
286  sys::fs::remove(LockFileName);
287  sys::fs::remove(UniqueLockFileName);
288  // The unique file is now gone, so remove it from the signal handler. This
289  // matches a sys::RemoveFileOnSignal() in LockFileManager().
290  sys::DontRemoveFileOnSignal(UniqueLockFileName);
291 }
292 
294 LockFileManager::waitForUnlock(const unsigned MaxSeconds) {
295  if (getState() != LFS_Shared)
296  return Res_Success;
297 
298  // Since we don't yet have an event-based method to wait for the lock file,
299  // implement randomized exponential backoff, similar to Ethernet collision
300  // algorithm. This improves performance on machines with high core counts
301  // when the file lock is heavily contended by multiple clang processes
302  const unsigned long MinWaitDurationMS = 10;
303  const unsigned long MaxWaitMultiplier = 50; // 500ms max wait
304  unsigned long WaitMultiplier = 1;
305  unsigned long ElapsedTimeSeconds = 0;
306 
307  std::random_device Device;
308  std::default_random_engine Engine(Device());
309 
310  auto StartTime = std::chrono::steady_clock::now();
311 
312  do {
313  // FIXME: implement event-based waiting
314 
315  // Sleep for the designated interval, to allow the owning process time to
316  // finish up and remove the lock file.
317  std::uniform_int_distribution<unsigned long> Distribution(1,
318  WaitMultiplier);
319  unsigned long WaitDurationMS = MinWaitDurationMS * Distribution(Engine);
320  std::this_thread::sleep_for(std::chrono::milliseconds(WaitDurationMS));
321 
322  if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
324  // If the original file wasn't created, somone thought the lock was dead.
325  if (!sys::fs::exists(FileName))
326  return Res_OwnerDied;
327  return Res_Success;
328  }
329 
330  // If the process owning the lock died without cleaning up, just bail out.
331  if (!processStillExecuting((*Owner).first, (*Owner).second))
332  return Res_OwnerDied;
333 
334  WaitMultiplier *= 2;
335  if (WaitMultiplier > MaxWaitMultiplier) {
336  WaitMultiplier = MaxWaitMultiplier;
337  }
338 
339  ElapsedTimeSeconds = std::chrono::duration_cast<std::chrono::seconds>(
340  std::chrono::steady_clock::now() - StartTime)
341  .count();
342 
343  } while (ElapsedTimeSeconds < MaxSeconds);
344 
345  // Give up.
346  return Res_Timeout;
347 }
348 
350  return sys::fs::remove(LockFileName);
351 }
static std::error_code getHostID(SmallVectorImpl< char > &HostID)
Represents either an error or a value T.
Definition: ErrorOr.h:56
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:248
StringRef getBuffer() const
Definition: MemoryBuffer.h:71
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:805
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
WaitForUnlockResult
Describes the result of waiting for the owner to release the lock.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
std::string getErrorMessage() const
Get error message, or "" if there is no error.
The lock was released successfully.
void setError(const std::error_code &EC, StringRef ErrorMsg="")
Set error and error message.
std::error_code error() const
Definition: raw_ostream.h:481
static Pid getProcessId()
Get the process&#39;s identifier.
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:891
An error occurred while trying to create or find the lock file.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:612
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
std::error_code unsafeRemoveLockFile()
Remove the lock file.
std::error_code create_link(const Twine &to, const Twine &from)
Create a link from from to to.
LockFileState getState() const
Determine the state of the lock file.
LockFileState
Describes the state of a lock file.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:539
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:487
Reached timeout while waiting for the owner to release the lock.
bool RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg=nullptr)
This function registers signal handlers to ensure that if a signal gets delivered that the named file...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
iterator begin() const
Definition: StringRef.h:131
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:433
Owner died while holding the lock.
Provides a library for accessing information about this process and other processes on the operating ...
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:408
WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds=90)
For a shared lock, wait until the owner releases the lock.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1567
void close()
Manually flush the stream and close the file.
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:521
The lock file has been created and is owned by this instance of the object.
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
The lock file already exists and is owned by some other instance.
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1063
iterator end() const
Definition: StringRef.h:133
reference get()
Definition: ErrorOr.h:150
raw_ostream::uuid_t uuid_t
Definition: MachOYAML.h:291