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/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"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/Signals.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cerrno>
20 #include <chrono>
21 #include <ctime>
22 #include <memory>
23 #include <random>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <system_error>
27 #include <thread>
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(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__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 
47 using 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
54 Optional<std::pair<std::string, int> >
55 LockFileManager::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(LockFileName);
60   if (!MBOrErr) {
61     sys::fs::remove(LockFileName);
62     return None;
63   }
64   MemoryBuffer &MB = *MBOrErr.get();
65 
66   StringRef Hostname;
67   StringRef PIDStr;
68   std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
69   PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
70   int PID;
71   if (!PIDStr.getAsInteger(10, PID)) {
72     auto Owner = std::make_pair(std::string(Hostname), PID);
73     if (processStillExecuting(Owner.first, Owner.second))
74       return Owner;
75   }
76 
77   // Delete the lock file. It's invalid anyway.
78   sys::fs::remove(LockFileName);
79   return None;
80 }
81 
82 static 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 std::error_code(errno, std::system_category());
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(HostName, 255);
102   StringRef HostNameRef(HostName);
103   HostID.append(HostNameRef.begin(), 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 
113 bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
114 #if LLVM_ON_UNIX && !defined(__ANDROID__)
115   SmallString<256> StoredHostID;
116   if (getHostID(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) == -1 && errno == ESRCH)
121     return false;
122 #endif
123 
124   return true;
125 }
126 
127 namespace {
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.
136 class RemoveUniqueLockFileOnSignal {
137   StringRef Filename;
138   bool RemoveImmediately;
139 public:
140   RemoveUniqueLockFileOnSignal(StringRef Name)
141   : Filename(Name), RemoveImmediately(true) {
142     sys::RemoveFileOnSignal(Filename, 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(Filename);
152     sys::DontRemoveFileOnSignal(Filename);
153   }
154 
155   void lockAcquired() { RemoveImmediately = false; }
156 };
157 
158 } // end anonymous namespace
159 
160 LockFileManager::LockFileManager(StringRef FileName)
161 {
162   this->FileName = FileName;
163   if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
164     std::string S("failed to obtain absolute path for ");
165     S.append(std::string(this->FileName.str()));
166     setError(EC, S);
167     return;
168   }
169   LockFileName = this->FileName;
170   LockFileName += ".lock";
171 
172   // If the lock file already exists, don't bother to try to create our own
173   // lock file; it won't work anyway. Just figure out who owns this lock file.
174   if ((Owner = readLockFile(LockFileName)))
175     return;
176 
177   // Create a lock file that is unique to this instance.
178   UniqueLockFileName = LockFileName;
179   UniqueLockFileName += "-%%%%%%%%";
180   int UniqueLockFileID;
181   if (std::error_code EC = sys::fs::createUniqueFile(
182           UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
183     std::string S("failed to create unique file ");
184     S.append(std::string(UniqueLockFileName.str()));
185     setError(EC, S);
186     return;
187   }
188 
189   // Write our process ID to our unique lock file.
190   {
191     SmallString<256> HostID;
192     if (auto EC = getHostID(HostID)) {
193       setError(EC, "failed to get host id");
194       return;
195     }
196 
197     raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
198     Out << HostID << ' ';
199 #if LLVM_ON_UNIX
200     Out << getpid();
201 #else
202     Out << "1";
203 #endif
204     Out.close();
205 
206     if (Out.has_error()) {
207       // We failed to write out PID, so report the error, remove the
208       // unique lock file, and fail.
209       std::string S("failed to write to ");
210       S.append(std::string(UniqueLockFileName.str()));
211       setError(Out.error(), S);
212       sys::fs::remove(UniqueLockFileName);
213       return;
214     }
215   }
216 
217   // Clean up the unique file on signal, which also releases the lock if it is
218   // held since the .lock symlink will point to a nonexistent file.
219   RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
220 
221   while (true) {
222     // Create a link from the lock file name. If this succeeds, we're done.
223     std::error_code EC =
224         sys::fs::create_link(UniqueLockFileName, LockFileName);
225     if (!EC) {
226       RemoveUniqueFile.lockAcquired();
227       return;
228     }
229 
230     if (EC != errc::file_exists) {
231       std::string S("failed to create link ");
232       raw_string_ostream OSS(S);
233       OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
234       setError(EC, OSS.str());
235       return;
236     }
237 
238     // Someone else managed to create the lock file first. Read the process ID
239     // from the lock file.
240     if ((Owner = readLockFile(LockFileName))) {
241       // Wipe out our unique lock file (it's useless now)
242       sys::fs::remove(UniqueLockFileName);
243       return;
244     }
245 
246     if (!sys::fs::exists(LockFileName)) {
247       // The previous owner released the lock file before we could read it.
248       // Try to get ownership again.
249       continue;
250     }
251 
252     // There is a lock file that nobody owns; try to clean it up and get
253     // ownership.
254     if ((EC = sys::fs::remove(LockFileName))) {
255       std::string S("failed to remove lockfile ");
256       S.append(std::string(UniqueLockFileName.str()));
257       setError(EC, S);
258       return;
259     }
260   }
261 }
262 
263 LockFileManager::LockFileState LockFileManager::getState() const {
264   if (Owner)
265     return LFS_Shared;
266 
267   if (ErrorCode)
268     return LFS_Error;
269 
270   return LFS_Owned;
271 }
272 
273 std::string LockFileManager::getErrorMessage() const {
274   if (ErrorCode) {
275     std::string Str(ErrorDiagMsg);
276     std::string ErrCodeMsg = ErrorCode.message();
277     raw_string_ostream OSS(Str);
278     if (!ErrCodeMsg.empty())
279       OSS << ": " << ErrCodeMsg;
280     return OSS.str();
281   }
282   return "";
283 }
284 
285 LockFileManager::~LockFileManager() {
286   if (getState() != LFS_Owned)
287     return;
288 
289   // Since we own the lock, remove the lock file and our own unique lock file.
290   sys::fs::remove(LockFileName);
291   sys::fs::remove(UniqueLockFileName);
292   // The unique file is now gone, so remove it from the signal handler. This
293   // matches a sys::RemoveFileOnSignal() in LockFileManager().
294   sys::DontRemoveFileOnSignal(UniqueLockFileName);
295 }
296 
297 LockFileManager::WaitForUnlockResult
298 LockFileManager::waitForUnlock(const unsigned MaxSeconds) {
299   if (getState() != LFS_Shared)
300     return Res_Success;
301 
302   // Since we don't yet have an event-based method to wait for the lock file,
303   // implement randomized exponential backoff, similar to Ethernet collision
304   // algorithm. This improves performance on machines with high core counts
305   // when the file lock is heavily contended by multiple clang processes
306   const unsigned long MinWaitDurationMS = 10;
307   const unsigned long MaxWaitMultiplier = 50; // 500ms max wait
308   unsigned long WaitMultiplier = 1;
309   unsigned long ElapsedTimeSeconds = 0;
310 
311   std::random_device Device;
312   std::default_random_engine Engine(Device());
313 
314   auto StartTime = std::chrono::steady_clock::now();
315 
316   do {
317     // FIXME: implement event-based waiting
318 
319     // Sleep for the designated interval, to allow the owning process time to
320     // finish up and remove the lock file.
321     std::uniform_int_distribution<unsigned long> Distribution(1,
322                                                               WaitMultiplier);
323     unsigned long WaitDurationMS = MinWaitDurationMS * Distribution(Engine);
324     std::this_thread::sleep_for(std::chrono::milliseconds(WaitDurationMS));
325 
326     if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
327         errc::no_such_file_or_directory) {
328       // If the original file wasn't created, somone thought the lock was dead.
329       if (!sys::fs::exists(FileName))
330         return Res_OwnerDied;
331       return Res_Success;
332     }
333 
334     // If the process owning the lock died without cleaning up, just bail out.
335     if (!processStillExecuting((*Owner).first, (*Owner).second))
336       return Res_OwnerDied;
337 
338     WaitMultiplier *= 2;
339     if (WaitMultiplier > MaxWaitMultiplier) {
340       WaitMultiplier = MaxWaitMultiplier;
341     }
342 
343     ElapsedTimeSeconds = std::chrono::duration_cast<std::chrono::seconds>(
344                              std::chrono::steady_clock::now() - StartTime)
345                              .count();
346 
347   } while (ElapsedTimeSeconds < MaxSeconds);
348 
349   // Give up.
350   return Res_Timeout;
351 }
352 
353 std::error_code LockFileManager::unsafeRemoveLockFile() {
354   return sys::fs::remove(LockFileName);
355 }
356