17039e358SDouglas Gregor //===--- LockFileManager.cpp - File-level Locking Utility------------------===//
27039e358SDouglas Gregor //
37039e358SDouglas Gregor //                     The LLVM Compiler Infrastructure
47039e358SDouglas Gregor //
57039e358SDouglas Gregor // This file is distributed under the University of Illinois Open Source
67039e358SDouglas Gregor // License. See LICENSE.TXT for details.
77039e358SDouglas Gregor //
87039e358SDouglas Gregor //===----------------------------------------------------------------------===//
933d7b762SEugene Zelenko 
107039e358SDouglas Gregor #include "llvm/Support/LockFileManager.h"
1133d7b762SEugene Zelenko #include "llvm/ADT/None.h"
1292387a87SBenjamin Kramer #include "llvm/ADT/ScopeExit.h"
1333d7b762SEugene Zelenko #include "llvm/ADT/SmallVector.h"
14d78273f4SReid Kleckner #include "llvm/ADT/StringExtras.h"
15*432a3883SNico Weber #include "llvm/Config/llvm-config.h"
162a826e40SRafael Espindola #include "llvm/Support/Errc.h"
1733d7b762SEugene Zelenko #include "llvm/Support/ErrorOr.h"
187039e358SDouglas Gregor #include "llvm/Support/FileSystem.h"
19d78273f4SReid Kleckner #include "llvm/Support/MemoryBuffer.h"
2063aa8c5dSBen Langmuir #include "llvm/Support/Signals.h"
216bda14b3SChandler Carruth #include "llvm/Support/raw_ostream.h"
2233d7b762SEugene Zelenko #include <cerrno>
2333d7b762SEugene Zelenko #include <ctime>
2433d7b762SEugene Zelenko #include <memory>
257039e358SDouglas Gregor #include <sys/stat.h>
26ed0881b2SChandler Carruth #include <sys/types.h>
276bda14b3SChandler Carruth #include <system_error>
286bda14b3SChandler Carruth #include <tuple>
29712e8d29SNico Weber #if _WIN32
307039e358SDouglas Gregor #include <windows.h>
317039e358SDouglas Gregor #endif
327039e358SDouglas Gregor #if LLVM_ON_UNIX
337039e358SDouglas Gregor #include <unistd.h>
347039e358SDouglas Gregor #endif
35450461cbSBen Langmuir 
36450461cbSBen Langmuir #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
37450461cbSBen Langmuir #define USE_OSX_GETHOSTUUID 1
38450461cbSBen Langmuir #else
39450461cbSBen Langmuir #define USE_OSX_GETHOSTUUID 0
40450461cbSBen Langmuir #endif
41450461cbSBen Langmuir 
42450461cbSBen Langmuir #if USE_OSX_GETHOSTUUID
43450461cbSBen Langmuir #include <uuid/uuid.h>
44450461cbSBen Langmuir #endif
4533d7b762SEugene Zelenko 
467039e358SDouglas Gregor using namespace llvm;
477039e358SDouglas Gregor 
487039e358SDouglas Gregor /// \brief Attempt to read the lock file with the given name, if it exists.
497039e358SDouglas Gregor ///
507039e358SDouglas Gregor /// \param LockFileName The name of the lock file to read.
517039e358SDouglas Gregor ///
527039e358SDouglas Gregor /// \returns The process ID of the process that owns this lock file
537039e358SDouglas Gregor Optional<std::pair<std::string, int> >
547039e358SDouglas Gregor LockFileManager::readLockFile(StringRef LockFileName) {
557039e358SDouglas Gregor   // Read the owning host and PID out of the lock file. If it appears that the
567039e358SDouglas Gregor   // owning process is dead, the lock file is invalid.
57adf21f2aSRafael Espindola   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
58adf21f2aSRafael Espindola       MemoryBuffer::getFile(LockFileName);
59adf21f2aSRafael Espindola   if (!MBOrErr) {
6037575693SArgyrios Kyrtzidis     sys::fs::remove(LockFileName);
617de8ea3dSReid Kleckner     return None;
6237575693SArgyrios Kyrtzidis   }
633f6481d0SRafael Espindola   MemoryBuffer &MB = *MBOrErr.get();
647de8ea3dSReid Kleckner 
65d78273f4SReid Kleckner   StringRef Hostname;
66d78273f4SReid Kleckner   StringRef PIDStr;
673f6481d0SRafael Espindola   std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
687de8ea3dSReid Kleckner   PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
69d78273f4SReid Kleckner   int PID;
7044ec0a7dSArgyrios Kyrtzidis   if (!PIDStr.getAsInteger(10, PID)) {
7144ec0a7dSArgyrios Kyrtzidis     auto Owner = std::make_pair(std::string(Hostname), PID);
7244ec0a7dSArgyrios Kyrtzidis     if (processStillExecuting(Owner.first, Owner.second))
7344ec0a7dSArgyrios Kyrtzidis       return Owner;
7444ec0a7dSArgyrios Kyrtzidis   }
757039e358SDouglas Gregor 
767039e358SDouglas Gregor   // Delete the lock file. It's invalid anyway.
77d78273f4SReid Kleckner   sys::fs::remove(LockFileName);
78ef04593dSDavid Blaikie   return None;
797039e358SDouglas Gregor }
807039e358SDouglas Gregor 
81450461cbSBen Langmuir static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
82450461cbSBen Langmuir   HostID.clear();
83450461cbSBen Langmuir 
84450461cbSBen Langmuir #if USE_OSX_GETHOSTUUID
85450461cbSBen Langmuir   // On OS X, use the more stable hardware UUID instead of hostname.
86450461cbSBen Langmuir   struct timespec wait = {1, 0}; // 1 second.
87450461cbSBen Langmuir   uuid_t uuid;
88450461cbSBen Langmuir   if (gethostuuid(uuid, &wait) != 0)
89450461cbSBen Langmuir     return std::error_code(errno, std::system_category());
90450461cbSBen Langmuir 
91450461cbSBen Langmuir   uuid_string_t UUIDStr;
92450461cbSBen Langmuir   uuid_unparse(uuid, UUIDStr);
93450461cbSBen Langmuir   StringRef UUIDRef(UUIDStr);
94450461cbSBen Langmuir   HostID.append(UUIDRef.begin(), UUIDRef.end());
95450461cbSBen Langmuir 
96450461cbSBen Langmuir #elif LLVM_ON_UNIX
97450461cbSBen Langmuir   char HostName[256];
98450461cbSBen Langmuir   HostName[255] = 0;
99450461cbSBen Langmuir   HostName[0] = 0;
100450461cbSBen Langmuir   gethostname(HostName, 255);
101450461cbSBen Langmuir   StringRef HostNameRef(HostName);
102450461cbSBen Langmuir   HostID.append(HostNameRef.begin(), HostNameRef.end());
103450461cbSBen Langmuir 
104450461cbSBen Langmuir #else
105450461cbSBen Langmuir   StringRef Dummy("localhost");
106450461cbSBen Langmuir   HostID.append(Dummy.begin(), Dummy.end());
107450461cbSBen Langmuir #endif
108450461cbSBen Langmuir 
109450461cbSBen Langmuir   return std::error_code();
110450461cbSBen Langmuir }
111450461cbSBen Langmuir 
112450461cbSBen Langmuir bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
113c439a426SEvgeniy Stepanov #if LLVM_ON_UNIX && !defined(__ANDROID__)
114450461cbSBen Langmuir   SmallString<256> StoredHostID;
115450461cbSBen Langmuir   if (getHostID(StoredHostID))
116450461cbSBen Langmuir     return true; // Conservatively assume it's executing on error.
117450461cbSBen Langmuir 
1187039e358SDouglas Gregor   // Check whether the process is dead. If so, we're done.
119450461cbSBen Langmuir   if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
1207039e358SDouglas Gregor     return false;
1217039e358SDouglas Gregor #endif
1227039e358SDouglas Gregor 
1237039e358SDouglas Gregor   return true;
1247039e358SDouglas Gregor }
1257039e358SDouglas Gregor 
1267039e358SDouglas Gregor LockFileManager::LockFileManager(StringRef FileName)
1277039e358SDouglas Gregor {
128056eafd4SDouglas Gregor   this->FileName = FileName;
129db4ed0bdSRafael Espindola   if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
13042b1f65fSBruno Cardoso Lopes     std::string S("failed to obtain absolute path for ");
13142b1f65fSBruno Cardoso Lopes     S.append(this->FileName.str());
13242b1f65fSBruno Cardoso Lopes     setError(EC, S);
133900e9a3dSArgyrios Kyrtzidis     return;
134900e9a3dSArgyrios Kyrtzidis   }
135900e9a3dSArgyrios Kyrtzidis   LockFileName = this->FileName;
1367039e358SDouglas Gregor   LockFileName += ".lock";
1377039e358SDouglas Gregor 
1387039e358SDouglas Gregor   // If the lock file already exists, don't bother to try to create our own
1397039e358SDouglas Gregor   // lock file; it won't work anyway. Just figure out who owns this lock file.
1407039e358SDouglas Gregor   if ((Owner = readLockFile(LockFileName)))
1417039e358SDouglas Gregor     return;
1427039e358SDouglas Gregor 
1437039e358SDouglas Gregor   // Create a lock file that is unique to this instance.
14451c63bb7SRafael Espindola   Expected<sys::fs::TempFile> Temp =
14551c63bb7SRafael Espindola       sys::fs::TempFile::create(LockFileName + "-%%%%%%%%");
14651c63bb7SRafael Espindola   if (!Temp) {
14751c63bb7SRafael Espindola     std::error_code EC = errorToErrorCode(Temp.takeError());
14851c63bb7SRafael Espindola     std::string S("failed to create unique file with prefix ");
14951c63bb7SRafael Espindola     S.append(LockFileName.str());
15042b1f65fSBruno Cardoso Lopes     setError(EC, S);
1517039e358SDouglas Gregor     return;
1527039e358SDouglas Gregor   }
15351c63bb7SRafael Espindola   UniqueLockFile = std::move(*Temp);
15451c63bb7SRafael Espindola 
15551c63bb7SRafael Espindola   // Make sure we discard the temporary file on exit.
15692387a87SBenjamin Kramer   auto RemoveTempFile = llvm::make_scope_exit([&]() {
15751c63bb7SRafael Espindola     if (Error E = UniqueLockFile->discard())
15851c63bb7SRafael Espindola       setError(errorToErrorCode(std::move(E)));
15951c63bb7SRafael Espindola   });
1607039e358SDouglas Gregor 
1617039e358SDouglas Gregor   // Write our process ID to our unique lock file.
1627039e358SDouglas Gregor   {
163450461cbSBen Langmuir     SmallString<256> HostID;
164450461cbSBen Langmuir     if (auto EC = getHostID(HostID)) {
16542b1f65fSBruno Cardoso Lopes       setError(EC, "failed to get host id");
166450461cbSBen Langmuir       return;
167450461cbSBen Langmuir     }
1685123eecdSBen Langmuir 
16951c63bb7SRafael Espindola     raw_fd_ostream Out(UniqueLockFile->FD, /*shouldClose=*/false);
170450461cbSBen Langmuir     Out << HostID << ' ';
1717039e358SDouglas Gregor #if LLVM_ON_UNIX
172450461cbSBen Langmuir     Out << getpid();
1737039e358SDouglas Gregor #else
174450461cbSBen Langmuir     Out << "1";
1757039e358SDouglas Gregor #endif
17651c63bb7SRafael Espindola     Out.flush();
1777039e358SDouglas Gregor 
1787039e358SDouglas Gregor     if (Out.has_error()) {
1799ce2d03eSBob Haarman       // We failed to write out PID, so report the error, remove the
1807039e358SDouglas Gregor       // unique lock file, and fail.
18142b1f65fSBruno Cardoso Lopes       std::string S("failed to write to ");
18251c63bb7SRafael Espindola       S.append(UniqueLockFile->TmpName);
1839ce2d03eSBob Haarman       setError(Out.error(), S);
1847039e358SDouglas Gregor       return;
1857039e358SDouglas Gregor     }
1867039e358SDouglas Gregor   }
1877039e358SDouglas Gregor 
18833d7b762SEugene Zelenko   while (true) {
18983f858e5SRafael Espindola     // Create a link from the lock file name. If this succeeds, we're done.
190db4ed0bdSRafael Espindola     std::error_code EC =
19151c63bb7SRafael Espindola         sys::fs::create_link(UniqueLockFile->TmpName, LockFileName);
19263aa8c5dSBen Langmuir     if (!EC) {
19392387a87SBenjamin Kramer       RemoveTempFile.release();
1947039e358SDouglas Gregor       return;
19563aa8c5dSBen Langmuir     }
1967039e358SDouglas Gregor 
1972a826e40SRafael Espindola     if (EC != errc::file_exists) {
19842b1f65fSBruno Cardoso Lopes       std::string S("failed to create link ");
19942b1f65fSBruno Cardoso Lopes       raw_string_ostream OSS(S);
20051c63bb7SRafael Espindola       OSS << LockFileName.str() << " to " << UniqueLockFile->TmpName;
20142b1f65fSBruno Cardoso Lopes       setError(EC, OSS.str());
2024147978cSArgyrios Kyrtzidis       return;
2034147978cSArgyrios Kyrtzidis     }
2044147978cSArgyrios Kyrtzidis 
2054147978cSArgyrios Kyrtzidis     // Someone else managed to create the lock file first. Read the process ID
2064147978cSArgyrios Kyrtzidis     // from the lock file.
20751c63bb7SRafael Espindola     if ((Owner = readLockFile(LockFileName)))
20851c63bb7SRafael Espindola       return; // RemoveTempFile will delete out our unique lock file.
2094147978cSArgyrios Kyrtzidis 
21092e1b62dSYaron Keren     if (!sys::fs::exists(LockFileName)) {
2114147978cSArgyrios Kyrtzidis       // The previous owner released the lock file before we could read it.
2124147978cSArgyrios Kyrtzidis       // Try to get ownership again.
2134147978cSArgyrios Kyrtzidis       continue;
2144147978cSArgyrios Kyrtzidis     }
2154147978cSArgyrios Kyrtzidis 
2164147978cSArgyrios Kyrtzidis     // There is a lock file that nobody owns; try to clean it up and get
2174147978cSArgyrios Kyrtzidis     // ownership.
21892e1b62dSYaron Keren     if ((EC = sys::fs::remove(LockFileName))) {
21942b1f65fSBruno Cardoso Lopes       std::string S("failed to remove lockfile ");
22051c63bb7SRafael Espindola       S.append(LockFileName.str());
22142b1f65fSBruno Cardoso Lopes       setError(EC, S);
2224147978cSArgyrios Kyrtzidis       return;
2234147978cSArgyrios Kyrtzidis     }
2244147978cSArgyrios Kyrtzidis   }
2257039e358SDouglas Gregor }
2267039e358SDouglas Gregor 
2277039e358SDouglas Gregor LockFileManager::LockFileState LockFileManager::getState() const {
2287039e358SDouglas Gregor   if (Owner)
2297039e358SDouglas Gregor     return LFS_Shared;
2307039e358SDouglas Gregor 
2318c42d323SRafael Espindola   if (ErrorCode)
2327039e358SDouglas Gregor     return LFS_Error;
2337039e358SDouglas Gregor 
2347039e358SDouglas Gregor   return LFS_Owned;
2357039e358SDouglas Gregor }
2367039e358SDouglas Gregor 
23742b1f65fSBruno Cardoso Lopes std::string LockFileManager::getErrorMessage() const {
2388c42d323SRafael Espindola   if (ErrorCode) {
23942b1f65fSBruno Cardoso Lopes     std::string Str(ErrorDiagMsg);
2408c42d323SRafael Espindola     std::string ErrCodeMsg = ErrorCode.message();
24142b1f65fSBruno Cardoso Lopes     raw_string_ostream OSS(Str);
24242b1f65fSBruno Cardoso Lopes     if (!ErrCodeMsg.empty())
243c8434103SRafael Espindola       OSS << ": " << ErrCodeMsg;
244c8434103SRafael Espindola     return OSS.str();
24542b1f65fSBruno Cardoso Lopes   }
24642b1f65fSBruno Cardoso Lopes   return "";
24742b1f65fSBruno Cardoso Lopes }
24842b1f65fSBruno Cardoso Lopes 
2497039e358SDouglas Gregor LockFileManager::~LockFileManager() {
2507039e358SDouglas Gregor   if (getState() != LFS_Owned)
2517039e358SDouglas Gregor     return;
2527039e358SDouglas Gregor 
2537039e358SDouglas Gregor   // Since we own the lock, remove the lock file and our own unique lock file.
25492e1b62dSYaron Keren   sys::fs::remove(LockFileName);
25551c63bb7SRafael Espindola   consumeError(UniqueLockFile->discard());
2567039e358SDouglas Gregor }
2577039e358SDouglas Gregor 
25844ec0a7dSArgyrios Kyrtzidis LockFileManager::WaitForUnlockResult LockFileManager::waitForUnlock() {
2597039e358SDouglas Gregor   if (getState() != LFS_Shared)
26044ec0a7dSArgyrios Kyrtzidis     return Res_Success;
2617039e358SDouglas Gregor 
262712e8d29SNico Weber #if _WIN32
2637039e358SDouglas Gregor   unsigned long Interval = 1;
2647039e358SDouglas Gregor #else
2657039e358SDouglas Gregor   struct timespec Interval;
2667039e358SDouglas Gregor   Interval.tv_sec = 0;
2677039e358SDouglas Gregor   Interval.tv_nsec = 1000000;
2687039e358SDouglas Gregor #endif
2698fef5556SBruno Cardoso Lopes   // Don't wait more than 40s per iteration. Total timeout for the file
2708fef5556SBruno Cardoso Lopes   // to appear is ~1.5 minutes.
2718fef5556SBruno Cardoso Lopes   const unsigned MaxSeconds = 40;
2727039e358SDouglas Gregor   do {
2737039e358SDouglas Gregor     // Sleep for the designated interval, to allow the owning process time to
2747039e358SDouglas Gregor     // finish up and remove the lock file.
2757039e358SDouglas Gregor     // FIXME: Should we hook in to system APIs to get a notification when the
2767039e358SDouglas Gregor     // lock file is deleted?
277712e8d29SNico Weber #if _WIN32
2787039e358SDouglas Gregor     Sleep(Interval);
2797039e358SDouglas Gregor #else
280c10719f5SCraig Topper     nanosleep(&Interval, nullptr);
2817039e358SDouglas Gregor #endif
2820cb68460SDouglas Gregor 
283281f23adSRafael Espindola     if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
284281f23adSRafael Espindola         errc::no_such_file_or_directory) {
28508970917SBen Langmuir       // If the original file wasn't created, somone thought the lock was dead.
28692e1b62dSYaron Keren       if (!sys::fs::exists(FileName))
28708970917SBen Langmuir         return Res_OwnerDied;
28844ec0a7dSArgyrios Kyrtzidis       return Res_Success;
289056eafd4SDouglas Gregor     }
2907039e358SDouglas Gregor 
29108970917SBen Langmuir     // If the process owning the lock died without cleaning up, just bail out.
29208970917SBen Langmuir     if (!processStillExecuting((*Owner).first, (*Owner).second))
29344ec0a7dSArgyrios Kyrtzidis       return Res_OwnerDied;
2947039e358SDouglas Gregor 
2957039e358SDouglas Gregor     // Exponentially increase the time we wait for the lock to be removed.
296712e8d29SNico Weber #if _WIN32
2977039e358SDouglas Gregor     Interval *= 2;
2987039e358SDouglas Gregor #else
2997039e358SDouglas Gregor     Interval.tv_sec *= 2;
3007039e358SDouglas Gregor     Interval.tv_nsec *= 2;
3017039e358SDouglas Gregor     if (Interval.tv_nsec >= 1000000000) {
3027039e358SDouglas Gregor       ++Interval.tv_sec;
3037039e358SDouglas Gregor       Interval.tv_nsec -= 1000000000;
3047039e358SDouglas Gregor     }
3057039e358SDouglas Gregor #endif
3067039e358SDouglas Gregor   } while (
307712e8d29SNico Weber #if _WIN32
3087039e358SDouglas Gregor            Interval < MaxSeconds * 1000
3097039e358SDouglas Gregor #else
3107039e358SDouglas Gregor            Interval.tv_sec < (time_t)MaxSeconds
3117039e358SDouglas Gregor #endif
3127039e358SDouglas Gregor            );
3137039e358SDouglas Gregor 
3147039e358SDouglas Gregor   // Give up.
31544ec0a7dSArgyrios Kyrtzidis   return Res_Timeout;
3167039e358SDouglas Gregor }
317d2d52de2SBen Langmuir 
318d2d52de2SBen Langmuir std::error_code LockFileManager::unsafeRemoveLockFile() {
31992e1b62dSYaron Keren   return sys::fs::remove(LockFileName);
320d2d52de2SBen Langmuir }
321