xref: /llvm-project/llvm/lib/Support/LockFileManager.cpp (revision 89e6a288674c9fae33aeb5448c7b1fe782b2bf53)
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 <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(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_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
55 std::optional<std::pair<std::string, int>>
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.
59   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
60       MemoryBuffer::getFile(LockFileName);
61   if (!MBOrErr) {
62     sys::fs::remove(LockFileName);
63     return std::nullopt;
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 std::nullopt;
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 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(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));
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));
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));
207       setError(Out.error(), S);
208       sys::fs::remove(UniqueLockFileName);
209       // Don't call report_fatal_error.
210       Out.clear_error();
211       return;
212     }
213   }
214 
215   // Clean up the unique file on signal, which also releases the lock if it is
216   // held since the .lock symlink will point to a nonexistent file.
217   RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
218 
219   while (true) {
220     // Create a link from the lock file name. If this succeeds, we're done.
221     std::error_code EC =
222         sys::fs::create_link(UniqueLockFileName, LockFileName);
223     if (!EC) {
224       RemoveUniqueFile.lockAcquired();
225       return;
226     }
227 
228     if (EC != errc::file_exists) {
229       std::string S("failed to create link ");
230       raw_string_ostream OSS(S);
231       OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
232       setError(EC, S);
233       return;
234     }
235 
236     // Someone else managed to create the lock file first. Read the process ID
237     // from the lock file.
238     if ((Owner = readLockFile(LockFileName))) {
239       // Wipe out our unique lock file (it's useless now)
240       sys::fs::remove(UniqueLockFileName);
241       return;
242     }
243 
244     if (!sys::fs::exists(LockFileName)) {
245       // The previous owner released the lock file before we could read it.
246       // Try to get ownership again.
247       continue;
248     }
249 
250     // There is a lock file that nobody owns; try to clean it up and get
251     // ownership.
252     if ((EC = sys::fs::remove(LockFileName))) {
253       std::string S("failed to remove lockfile ");
254       S.append(std::string(UniqueLockFileName));
255       setError(EC, S);
256       return;
257     }
258   }
259 }
260 
261 LockFileManager::LockFileState LockFileManager::getState() const {
262   if (Owner)
263     return LFS_Shared;
264 
265   if (ErrorCode)
266     return LFS_Error;
267 
268   return LFS_Owned;
269 }
270 
271 std::string LockFileManager::getErrorMessage() const {
272   if (ErrorCode) {
273     std::string Str(ErrorDiagMsg);
274     std::string ErrCodeMsg = ErrorCode.message();
275     raw_string_ostream OSS(Str);
276     if (!ErrCodeMsg.empty())
277       OSS << ": " << ErrCodeMsg;
278     return Str;
279   }
280   return "";
281 }
282 
283 LockFileManager::~LockFileManager() {
284   if (getState() != LFS_Owned)
285     return;
286 
287   // Since we own the lock, remove the lock file and our own unique lock file.
288   sys::fs::remove(LockFileName);
289   sys::fs::remove(UniqueLockFileName);
290   // The unique file is now gone, so remove it from the signal handler. This
291   // matches a sys::RemoveFileOnSignal() in LockFileManager().
292   sys::DontRemoveFileOnSignal(UniqueLockFileName);
293 }
294 
295 LockFileManager::WaitForUnlockResult
296 LockFileManager::waitForUnlock(const unsigned MaxSeconds) {
297   if (getState() != LFS_Shared)
298     return Res_Success;
299 
300   // Since we don't yet have an event-based method to wait for the lock file,
301   // use randomized exponential backoff, similar to Ethernet collision
302   // algorithm. This improves performance on machines with high core counts
303   // when the file lock is heavily contended by multiple clang processes
304   using namespace std::chrono_literals;
305   ExponentialBackoff Backoff(std::chrono::seconds(MaxSeconds), 10ms, 500ms);
306 
307   // Wait first as this is only called when the lock is known to be held.
308   while (Backoff.waitForNextAttempt()) {
309     // FIXME: implement event-based waiting
310     if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
311         errc::no_such_file_or_directory) {
312       // If the original file wasn't created, somone thought the lock was dead.
313       if (!sys::fs::exists(FileName))
314         return Res_OwnerDied;
315       return Res_Success;
316     }
317 
318     // If the process owning the lock died without cleaning up, just bail out.
319     if (!processStillExecuting((*Owner).first, (*Owner).second))
320       return Res_OwnerDied;
321   }
322 
323   // Give up.
324   return Res_Timeout;
325 }
326 
327 std::error_code LockFileManager::unsafeRemoveLockFile() {
328   return sys::fs::remove(LockFileName);
329 }
330