LLVM  8.0.1
LockFileManager.cpp
Go to the documentation of this file.
1 //===--- LockFileManager.cpp - File-level Locking Utility------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/Signals.h"
20 #include <cerrno>
21 #include <ctime>
22 #include <memory>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <system_error>
26 #include <tuple>
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif
30 #if LLVM_ON_UNIX
31 #include <unistd.h>
32 #endif
33 
34 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
35 #define USE_OSX_GETHOSTUUID 1
36 #else
37 #define USE_OSX_GETHOSTUUID 0
38 #endif
39 
40 #if USE_OSX_GETHOSTUUID
41 #include <uuid/uuid.h>
42 #endif
43 
44 using namespace llvm;
45 
46 /// Attempt to read the lock file with the given name, if it exists.
47 ///
48 /// \param LockFileName The name of the lock file to read.
49 ///
50 /// \returns The process ID of the process that owns this lock file
52 LockFileManager::readLockFile(StringRef LockFileName) {
53  // Read the owning host and PID out of the lock file. If it appears that the
54  // owning process is dead, the lock file is invalid.
56  MemoryBuffer::getFile(LockFileName);
57  if (!MBOrErr) {
58  sys::fs::remove(LockFileName);
59  return None;
60  }
61  MemoryBuffer &MB = *MBOrErr.get();
62 
63  StringRef Hostname;
64  StringRef PIDStr;
65  std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
66  PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
67  int PID;
68  if (!PIDStr.getAsInteger(10, PID)) {
69  auto Owner = std::make_pair(std::string(Hostname), PID);
70  if (processStillExecuting(Owner.first, Owner.second))
71  return Owner;
72  }
73 
74  // Delete the lock file. It's invalid anyway.
75  sys::fs::remove(LockFileName);
76  return None;
77 }
78 
79 static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
80  HostID.clear();
81 
82 #if USE_OSX_GETHOSTUUID
83  // On OS X, use the more stable hardware UUID instead of hostname.
84  struct timespec wait = {1, 0}; // 1 second.
85  uuid_t uuid;
86  if (gethostuuid(uuid, &wait) != 0)
87  return std::error_code(errno, std::system_category());
88 
89  uuid_string_t UUIDStr;
90  uuid_unparse(uuid, UUIDStr);
91  StringRef UUIDRef(UUIDStr);
92  HostID.append(UUIDRef.begin(), UUIDRef.end());
93 
94 #elif LLVM_ON_UNIX
95  char HostName[256];
96  HostName[255] = 0;
97  HostName[0] = 0;
98  gethostname(HostName, 255);
99  StringRef HostNameRef(HostName);
100  HostID.append(HostNameRef.begin(), HostNameRef.end());
101 
102 #else
103  StringRef Dummy("localhost");
104  HostID.append(Dummy.begin(), Dummy.end());
105 #endif
106 
107  return std::error_code();
108 }
109 
110 bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
111 #if LLVM_ON_UNIX && !defined(__ANDROID__)
112  SmallString<256> StoredHostID;
113  if (getHostID(StoredHostID))
114  return true; // Conservatively assume it's executing on error.
115 
116  // Check whether the process is dead. If so, we're done.
117  if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
118  return false;
119 #endif
120 
121  return true;
122 }
123 
124 namespace {
125 
126 /// An RAII helper object ensure that the unique lock file is removed.
127 ///
128 /// Ensures that if there is an error or a signal before we finish acquiring the
129 /// lock, the unique file will be removed. And if we successfully take the lock,
130 /// the signal handler is left in place so that signals while the lock is held
131 /// will remove the unique lock file. The caller should ensure there is a
132 /// matching call to sys::DontRemoveFileOnSignal when the lock is released.
133 class RemoveUniqueLockFileOnSignal {
134  StringRef Filename;
135  bool RemoveImmediately;
136 public:
137  RemoveUniqueLockFileOnSignal(StringRef Name)
138  : Filename(Name), RemoveImmediately(true) {
139  sys::RemoveFileOnSignal(Filename, nullptr);
140  }
141 
142  ~RemoveUniqueLockFileOnSignal() {
143  if (!RemoveImmediately) {
144  // Leave the signal handler enabled. It will be removed when the lock is
145  // released.
146  return;
147  }
148  sys::fs::remove(Filename);
149  sys::DontRemoveFileOnSignal(Filename);
150  }
151 
152  void lockAcquired() { RemoveImmediately = false; }
153 };
154 
155 } // end anonymous namespace
156 
157 LockFileManager::LockFileManager(StringRef FileName)
158 {
159  this->FileName = FileName;
160  if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
161  std::string S("failed to obtain absolute path for ");
162  S.append(this->FileName.str());
163  setError(EC, S);
164  return;
165  }
166  LockFileName = this->FileName;
167  LockFileName += ".lock";
168 
169  // If the lock file already exists, don't bother to try to create our own
170  // lock file; it won't work anyway. Just figure out who owns this lock file.
171  if ((Owner = readLockFile(LockFileName)))
172  return;
173 
174  // Create a lock file that is unique to this instance.
175  UniqueLockFileName = LockFileName;
176  UniqueLockFileName += "-%%%%%%%%";
177  int UniqueLockFileID;
178  if (std::error_code EC = sys::fs::createUniqueFile(
179  UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
180  std::string S("failed to create unique file ");
181  S.append(UniqueLockFileName.str());
182  setError(EC, S);
183  return;
184  }
185 
186  // Write our process ID to our unique lock file.
187  {
188  SmallString<256> HostID;
189  if (auto EC = getHostID(HostID)) {
190  setError(EC, "failed to get host id");
191  return;
192  }
193 
194  raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
195  Out << HostID << ' ';
196 #if LLVM_ON_UNIX
197  Out << getpid();
198 #else
199  Out << "1";
200 #endif
201  Out.close();
202 
203  if (Out.has_error()) {
204  // We failed to write out PID, so report the error, remove the
205  // unique lock file, and fail.
206  std::string S("failed to write to ");
207  S.append(UniqueLockFileName.str());
208  setError(Out.error(), S);
209  sys::fs::remove(UniqueLockFileName);
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(UniqueLockFileName, 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, OSS.str());
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(UniqueLockFileName);
240  return;
241  }
242 
243  if (!sys::fs::exists(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(LockFileName))) {
252  std::string S("failed to remove lockfile ");
253  S.append(UniqueLockFileName.str());
254  setError(EC, S);
255  return;
256  }
257  }
258 }
259 
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 OSS.str();
278  }
279  return "";
280 }
281 
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(LockFileName);
288  sys::fs::remove(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(UniqueLockFileName);
292 }
293 
295  if (getState() != LFS_Shared)
296  return Res_Success;
297 
298 #ifdef _WIN32
299  unsigned long Interval = 1;
300 #else
301  struct timespec Interval;
302  Interval.tv_sec = 0;
303  Interval.tv_nsec = 1000000;
304 #endif
305  // Don't wait more than 40s per iteration. Total timeout for the file
306  // to appear is ~1.5 minutes.
307  const unsigned MaxSeconds = 40;
308  do {
309  // Sleep for the designated interval, to allow the owning process time to
310  // finish up and remove the lock file.
311  // FIXME: Should we hook in to system APIs to get a notification when the
312  // lock file is deleted?
313 #ifdef _WIN32
314  Sleep(Interval);
315 #else
316  nanosleep(&Interval, nullptr);
317 #endif
318 
319  if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
321  // If the original file wasn't created, somone thought the lock was dead.
322  if (!sys::fs::exists(FileName))
323  return Res_OwnerDied;
324  return Res_Success;
325  }
326 
327  // If the process owning the lock died without cleaning up, just bail out.
328  if (!processStillExecuting((*Owner).first, (*Owner).second))
329  return Res_OwnerDied;
330 
331  // Exponentially increase the time we wait for the lock to be removed.
332 #ifdef _WIN32
333  Interval *= 2;
334 #else
335  Interval.tv_sec *= 2;
336  Interval.tv_nsec *= 2;
337  if (Interval.tv_nsec >= 1000000000) {
338  ++Interval.tv_sec;
339  Interval.tv_nsec -= 1000000000;
340  }
341 #endif
342  } while (
343 #ifdef _WIN32
344  Interval < MaxSeconds * 1000
345 #else
346  Interval.tv_sec < (time_t)MaxSeconds
347 #endif
348  );
349 
350  // Give up.
351  return Res_Timeout;
352 }
353 
355  return sys::fs::remove(LockFileName);
356 }
static std::error_code getHostID(SmallVectorImpl< char > &HostID)
Represents either an error or a value T.
Definition: ErrorOr.h:57
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
StringRef getBuffer() const
Definition: MemoryBuffer.h:64
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:766
WaitForUnlockResult
Describes the result of waiting for the owner to release the lock.
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
Definition: Interval.h:37
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:446
amdgpu Simplify well known AMD library false Value Value const Twine & Name
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:852
An error occurred while trying to create or find the lock file.
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
LockFileState getState() const
Determine the state of the lock file.
LockFileState
Describes the state of a lock file.
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:499
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:452
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:42
iterator begin() const
Definition: StringRef.h:106
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:394
Owner died while holding the lock.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:366
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:483
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?
WaitForUnlockResult waitForUnlock()
For a shared lock, wait until the owner releases the lock.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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:1022
iterator end() const
Definition: StringRef.h:108
reference get()
Definition: ErrorOr.h:157
raw_ostream::uuid_t uuid_t
Definition: MachOYAML.h:269