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"
12*92387a87SBenjamin Kramer #include "llvm/ADT/ScopeExit.h"
1333d7b762SEugene Zelenko #include "llvm/ADT/SmallVector.h"
14d78273f4SReid Kleckner #include "llvm/ADT/StringExtras.h"
152a826e40SRafael Espindola #include "llvm/Support/Errc.h"
1633d7b762SEugene Zelenko #include "llvm/Support/ErrorOr.h"
177039e358SDouglas Gregor #include "llvm/Support/FileSystem.h"
18d78273f4SReid Kleckner #include "llvm/Support/MemoryBuffer.h"
1963aa8c5dSBen Langmuir #include "llvm/Support/Signals.h"
206bda14b3SChandler Carruth #include "llvm/Support/raw_ostream.h"
2133d7b762SEugene Zelenko #include <cerrno>
2233d7b762SEugene Zelenko #include <ctime>
2333d7b762SEugene Zelenko #include <memory>
247039e358SDouglas Gregor #include <sys/stat.h>
25ed0881b2SChandler Carruth #include <sys/types.h>
266bda14b3SChandler Carruth #include <system_error>
276bda14b3SChandler Carruth #include <tuple>
287039e358SDouglas Gregor #if LLVM_ON_WIN32
297039e358SDouglas Gregor #include <windows.h>
307039e358SDouglas Gregor #endif
317039e358SDouglas Gregor #if LLVM_ON_UNIX
327039e358SDouglas Gregor #include <unistd.h>
337039e358SDouglas Gregor #endif
34450461cbSBen Langmuir 
35450461cbSBen Langmuir #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
36450461cbSBen Langmuir #define USE_OSX_GETHOSTUUID 1
37450461cbSBen Langmuir #else
38450461cbSBen Langmuir #define USE_OSX_GETHOSTUUID 0
39450461cbSBen Langmuir #endif
40450461cbSBen Langmuir 
41450461cbSBen Langmuir #if USE_OSX_GETHOSTUUID
42450461cbSBen Langmuir #include <uuid/uuid.h>
43450461cbSBen Langmuir #endif
4433d7b762SEugene Zelenko 
457039e358SDouglas Gregor using namespace llvm;
467039e358SDouglas Gregor 
477039e358SDouglas Gregor /// \brief Attempt to read the lock file with the given name, if it exists.
487039e358SDouglas Gregor ///
497039e358SDouglas Gregor /// \param LockFileName The name of the lock file to read.
507039e358SDouglas Gregor ///
517039e358SDouglas Gregor /// \returns The process ID of the process that owns this lock file
527039e358SDouglas Gregor Optional<std::pair<std::string, int> >
537039e358SDouglas Gregor LockFileManager::readLockFile(StringRef LockFileName) {
547039e358SDouglas Gregor   // Read the owning host and PID out of the lock file. If it appears that the
557039e358SDouglas Gregor   // owning process is dead, the lock file is invalid.
56adf21f2aSRafael Espindola   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
57adf21f2aSRafael Espindola       MemoryBuffer::getFile(LockFileName);
58adf21f2aSRafael Espindola   if (!MBOrErr) {
5937575693SArgyrios Kyrtzidis     sys::fs::remove(LockFileName);
607de8ea3dSReid Kleckner     return None;
6137575693SArgyrios Kyrtzidis   }
623f6481d0SRafael Espindola   MemoryBuffer &MB = *MBOrErr.get();
637de8ea3dSReid Kleckner 
64d78273f4SReid Kleckner   StringRef Hostname;
65d78273f4SReid Kleckner   StringRef PIDStr;
663f6481d0SRafael Espindola   std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
677de8ea3dSReid Kleckner   PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
68d78273f4SReid Kleckner   int PID;
6944ec0a7dSArgyrios Kyrtzidis   if (!PIDStr.getAsInteger(10, PID)) {
7044ec0a7dSArgyrios Kyrtzidis     auto Owner = std::make_pair(std::string(Hostname), PID);
7144ec0a7dSArgyrios Kyrtzidis     if (processStillExecuting(Owner.first, Owner.second))
7244ec0a7dSArgyrios Kyrtzidis       return Owner;
7344ec0a7dSArgyrios Kyrtzidis   }
747039e358SDouglas Gregor 
757039e358SDouglas Gregor   // Delete the lock file. It's invalid anyway.
76d78273f4SReid Kleckner   sys::fs::remove(LockFileName);
77ef04593dSDavid Blaikie   return None;
787039e358SDouglas Gregor }
797039e358SDouglas Gregor 
80450461cbSBen Langmuir static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
81450461cbSBen Langmuir   HostID.clear();
82450461cbSBen Langmuir 
83450461cbSBen Langmuir #if USE_OSX_GETHOSTUUID
84450461cbSBen Langmuir   // On OS X, use the more stable hardware UUID instead of hostname.
85450461cbSBen Langmuir   struct timespec wait = {1, 0}; // 1 second.
86450461cbSBen Langmuir   uuid_t uuid;
87450461cbSBen Langmuir   if (gethostuuid(uuid, &wait) != 0)
88450461cbSBen Langmuir     return std::error_code(errno, std::system_category());
89450461cbSBen Langmuir 
90450461cbSBen Langmuir   uuid_string_t UUIDStr;
91450461cbSBen Langmuir   uuid_unparse(uuid, UUIDStr);
92450461cbSBen Langmuir   StringRef UUIDRef(UUIDStr);
93450461cbSBen Langmuir   HostID.append(UUIDRef.begin(), UUIDRef.end());
94450461cbSBen Langmuir 
95450461cbSBen Langmuir #elif LLVM_ON_UNIX
96450461cbSBen Langmuir   char HostName[256];
97450461cbSBen Langmuir   HostName[255] = 0;
98450461cbSBen Langmuir   HostName[0] = 0;
99450461cbSBen Langmuir   gethostname(HostName, 255);
100450461cbSBen Langmuir   StringRef HostNameRef(HostName);
101450461cbSBen Langmuir   HostID.append(HostNameRef.begin(), HostNameRef.end());
102450461cbSBen Langmuir 
103450461cbSBen Langmuir #else
104450461cbSBen Langmuir   StringRef Dummy("localhost");
105450461cbSBen Langmuir   HostID.append(Dummy.begin(), Dummy.end());
106450461cbSBen Langmuir #endif
107450461cbSBen Langmuir 
108450461cbSBen Langmuir   return std::error_code();
109450461cbSBen Langmuir }
110450461cbSBen Langmuir 
111450461cbSBen Langmuir bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
112c439a426SEvgeniy Stepanov #if LLVM_ON_UNIX && !defined(__ANDROID__)
113450461cbSBen Langmuir   SmallString<256> StoredHostID;
114450461cbSBen Langmuir   if (getHostID(StoredHostID))
115450461cbSBen Langmuir     return true; // Conservatively assume it's executing on error.
116450461cbSBen Langmuir 
1177039e358SDouglas Gregor   // Check whether the process is dead. If so, we're done.
118450461cbSBen Langmuir   if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
1197039e358SDouglas Gregor     return false;
1207039e358SDouglas Gregor #endif
1217039e358SDouglas Gregor 
1227039e358SDouglas Gregor   return true;
1237039e358SDouglas Gregor }
1247039e358SDouglas Gregor 
1257039e358SDouglas Gregor LockFileManager::LockFileManager(StringRef FileName)
1267039e358SDouglas Gregor {
127056eafd4SDouglas Gregor   this->FileName = FileName;
128db4ed0bdSRafael Espindola   if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
12942b1f65fSBruno Cardoso Lopes     std::string S("failed to obtain absolute path for ");
13042b1f65fSBruno Cardoso Lopes     S.append(this->FileName.str());
13142b1f65fSBruno Cardoso Lopes     setError(EC, S);
132900e9a3dSArgyrios Kyrtzidis     return;
133900e9a3dSArgyrios Kyrtzidis   }
134900e9a3dSArgyrios Kyrtzidis   LockFileName = this->FileName;
1357039e358SDouglas Gregor   LockFileName += ".lock";
1367039e358SDouglas Gregor 
1377039e358SDouglas Gregor   // If the lock file already exists, don't bother to try to create our own
1387039e358SDouglas Gregor   // lock file; it won't work anyway. Just figure out who owns this lock file.
1397039e358SDouglas Gregor   if ((Owner = readLockFile(LockFileName)))
1407039e358SDouglas Gregor     return;
1417039e358SDouglas Gregor 
1427039e358SDouglas Gregor   // Create a lock file that is unique to this instance.
14351c63bb7SRafael Espindola   Expected<sys::fs::TempFile> Temp =
14451c63bb7SRafael Espindola       sys::fs::TempFile::create(LockFileName + "-%%%%%%%%");
14551c63bb7SRafael Espindola   if (!Temp) {
14651c63bb7SRafael Espindola     std::error_code EC = errorToErrorCode(Temp.takeError());
14751c63bb7SRafael Espindola     std::string S("failed to create unique file with prefix ");
14851c63bb7SRafael Espindola     S.append(LockFileName.str());
14942b1f65fSBruno Cardoso Lopes     setError(EC, S);
1507039e358SDouglas Gregor     return;
1517039e358SDouglas Gregor   }
15251c63bb7SRafael Espindola   UniqueLockFile = std::move(*Temp);
15351c63bb7SRafael Espindola 
15451c63bb7SRafael Espindola   // Make sure we discard the temporary file on exit.
155*92387a87SBenjamin Kramer   auto RemoveTempFile = llvm::make_scope_exit([&]() {
15651c63bb7SRafael Espindola     if (Error E = UniqueLockFile->discard())
15751c63bb7SRafael Espindola       setError(errorToErrorCode(std::move(E)));
15851c63bb7SRafael Espindola   });
1597039e358SDouglas Gregor 
1607039e358SDouglas Gregor   // Write our process ID to our unique lock file.
1617039e358SDouglas Gregor   {
162450461cbSBen Langmuir     SmallString<256> HostID;
163450461cbSBen Langmuir     if (auto EC = getHostID(HostID)) {
16442b1f65fSBruno Cardoso Lopes       setError(EC, "failed to get host id");
165450461cbSBen Langmuir       return;
166450461cbSBen Langmuir     }
1675123eecdSBen Langmuir 
16851c63bb7SRafael Espindola     raw_fd_ostream Out(UniqueLockFile->FD, /*shouldClose=*/false);
169450461cbSBen Langmuir     Out << HostID << ' ';
1707039e358SDouglas Gregor #if LLVM_ON_UNIX
171450461cbSBen Langmuir     Out << getpid();
1727039e358SDouglas Gregor #else
173450461cbSBen Langmuir     Out << "1";
1747039e358SDouglas Gregor #endif
17551c63bb7SRafael Espindola     Out.flush();
1767039e358SDouglas Gregor 
1777039e358SDouglas Gregor     if (Out.has_error()) {
1789ce2d03eSBob Haarman       // We failed to write out PID, so report the error, remove the
1797039e358SDouglas Gregor       // unique lock file, and fail.
18042b1f65fSBruno Cardoso Lopes       std::string S("failed to write to ");
18151c63bb7SRafael Espindola       S.append(UniqueLockFile->TmpName);
1829ce2d03eSBob Haarman       setError(Out.error(), S);
1837039e358SDouglas Gregor       return;
1847039e358SDouglas Gregor     }
1857039e358SDouglas Gregor   }
1867039e358SDouglas Gregor 
18733d7b762SEugene Zelenko   while (true) {
18883f858e5SRafael Espindola     // Create a link from the lock file name. If this succeeds, we're done.
189db4ed0bdSRafael Espindola     std::error_code EC =
19051c63bb7SRafael Espindola         sys::fs::create_link(UniqueLockFile->TmpName, LockFileName);
19163aa8c5dSBen Langmuir     if (!EC) {
192*92387a87SBenjamin Kramer       RemoveTempFile.release();
1937039e358SDouglas Gregor       return;
19463aa8c5dSBen Langmuir     }
1957039e358SDouglas Gregor 
1962a826e40SRafael Espindola     if (EC != errc::file_exists) {
19742b1f65fSBruno Cardoso Lopes       std::string S("failed to create link ");
19842b1f65fSBruno Cardoso Lopes       raw_string_ostream OSS(S);
19951c63bb7SRafael Espindola       OSS << LockFileName.str() << " to " << UniqueLockFile->TmpName;
20042b1f65fSBruno Cardoso Lopes       setError(EC, OSS.str());
2014147978cSArgyrios Kyrtzidis       return;
2024147978cSArgyrios Kyrtzidis     }
2034147978cSArgyrios Kyrtzidis 
2044147978cSArgyrios Kyrtzidis     // Someone else managed to create the lock file first. Read the process ID
2054147978cSArgyrios Kyrtzidis     // from the lock file.
20651c63bb7SRafael Espindola     if ((Owner = readLockFile(LockFileName)))
20751c63bb7SRafael Espindola       return; // RemoveTempFile will delete out our unique lock file.
2084147978cSArgyrios Kyrtzidis 
20992e1b62dSYaron Keren     if (!sys::fs::exists(LockFileName)) {
2104147978cSArgyrios Kyrtzidis       // The previous owner released the lock file before we could read it.
2114147978cSArgyrios Kyrtzidis       // Try to get ownership again.
2124147978cSArgyrios Kyrtzidis       continue;
2134147978cSArgyrios Kyrtzidis     }
2144147978cSArgyrios Kyrtzidis 
2154147978cSArgyrios Kyrtzidis     // There is a lock file that nobody owns; try to clean it up and get
2164147978cSArgyrios Kyrtzidis     // ownership.
21792e1b62dSYaron Keren     if ((EC = sys::fs::remove(LockFileName))) {
21842b1f65fSBruno Cardoso Lopes       std::string S("failed to remove lockfile ");
21951c63bb7SRafael Espindola       S.append(LockFileName.str());
22042b1f65fSBruno Cardoso Lopes       setError(EC, S);
2214147978cSArgyrios Kyrtzidis       return;
2224147978cSArgyrios Kyrtzidis     }
2234147978cSArgyrios Kyrtzidis   }
2247039e358SDouglas Gregor }
2257039e358SDouglas Gregor 
2267039e358SDouglas Gregor LockFileManager::LockFileState LockFileManager::getState() const {
2277039e358SDouglas Gregor   if (Owner)
2287039e358SDouglas Gregor     return LFS_Shared;
2297039e358SDouglas Gregor 
2308c42d323SRafael Espindola   if (ErrorCode)
2317039e358SDouglas Gregor     return LFS_Error;
2327039e358SDouglas Gregor 
2337039e358SDouglas Gregor   return LFS_Owned;
2347039e358SDouglas Gregor }
2357039e358SDouglas Gregor 
23642b1f65fSBruno Cardoso Lopes std::string LockFileManager::getErrorMessage() const {
2378c42d323SRafael Espindola   if (ErrorCode) {
23842b1f65fSBruno Cardoso Lopes     std::string Str(ErrorDiagMsg);
2398c42d323SRafael Espindola     std::string ErrCodeMsg = ErrorCode.message();
24042b1f65fSBruno Cardoso Lopes     raw_string_ostream OSS(Str);
24142b1f65fSBruno Cardoso Lopes     if (!ErrCodeMsg.empty())
242c8434103SRafael Espindola       OSS << ": " << ErrCodeMsg;
243c8434103SRafael Espindola     return OSS.str();
24442b1f65fSBruno Cardoso Lopes   }
24542b1f65fSBruno Cardoso Lopes   return "";
24642b1f65fSBruno Cardoso Lopes }
24742b1f65fSBruno Cardoso Lopes 
2487039e358SDouglas Gregor LockFileManager::~LockFileManager() {
2497039e358SDouglas Gregor   if (getState() != LFS_Owned)
2507039e358SDouglas Gregor     return;
2517039e358SDouglas Gregor 
2527039e358SDouglas Gregor   // Since we own the lock, remove the lock file and our own unique lock file.
25392e1b62dSYaron Keren   sys::fs::remove(LockFileName);
25451c63bb7SRafael Espindola   consumeError(UniqueLockFile->discard());
2557039e358SDouglas Gregor }
2567039e358SDouglas Gregor 
25744ec0a7dSArgyrios Kyrtzidis LockFileManager::WaitForUnlockResult LockFileManager::waitForUnlock() {
2587039e358SDouglas Gregor   if (getState() != LFS_Shared)
25944ec0a7dSArgyrios Kyrtzidis     return Res_Success;
2607039e358SDouglas Gregor 
2617039e358SDouglas Gregor #if LLVM_ON_WIN32
2627039e358SDouglas Gregor   unsigned long Interval = 1;
2637039e358SDouglas Gregor #else
2647039e358SDouglas Gregor   struct timespec Interval;
2657039e358SDouglas Gregor   Interval.tv_sec = 0;
2667039e358SDouglas Gregor   Interval.tv_nsec = 1000000;
2677039e358SDouglas Gregor #endif
2688fef5556SBruno Cardoso Lopes   // Don't wait more than 40s per iteration. Total timeout for the file
2698fef5556SBruno Cardoso Lopes   // to appear is ~1.5 minutes.
2708fef5556SBruno Cardoso Lopes   const unsigned MaxSeconds = 40;
2717039e358SDouglas Gregor   do {
2727039e358SDouglas Gregor     // Sleep for the designated interval, to allow the owning process time to
2737039e358SDouglas Gregor     // finish up and remove the lock file.
2747039e358SDouglas Gregor     // FIXME: Should we hook in to system APIs to get a notification when the
2757039e358SDouglas Gregor     // lock file is deleted?
2767039e358SDouglas Gregor #if LLVM_ON_WIN32
2777039e358SDouglas Gregor     Sleep(Interval);
2787039e358SDouglas Gregor #else
279c10719f5SCraig Topper     nanosleep(&Interval, nullptr);
2807039e358SDouglas Gregor #endif
2810cb68460SDouglas Gregor 
282281f23adSRafael Espindola     if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
283281f23adSRafael Espindola         errc::no_such_file_or_directory) {
28408970917SBen Langmuir       // If the original file wasn't created, somone thought the lock was dead.
28592e1b62dSYaron Keren       if (!sys::fs::exists(FileName))
28608970917SBen Langmuir         return Res_OwnerDied;
28744ec0a7dSArgyrios Kyrtzidis       return Res_Success;
288056eafd4SDouglas Gregor     }
2897039e358SDouglas Gregor 
29008970917SBen Langmuir     // If the process owning the lock died without cleaning up, just bail out.
29108970917SBen Langmuir     if (!processStillExecuting((*Owner).first, (*Owner).second))
29244ec0a7dSArgyrios Kyrtzidis       return Res_OwnerDied;
2937039e358SDouglas Gregor 
2947039e358SDouglas Gregor     // Exponentially increase the time we wait for the lock to be removed.
2957039e358SDouglas Gregor #if LLVM_ON_WIN32
2967039e358SDouglas Gregor     Interval *= 2;
2977039e358SDouglas Gregor #else
2987039e358SDouglas Gregor     Interval.tv_sec *= 2;
2997039e358SDouglas Gregor     Interval.tv_nsec *= 2;
3007039e358SDouglas Gregor     if (Interval.tv_nsec >= 1000000000) {
3017039e358SDouglas Gregor       ++Interval.tv_sec;
3027039e358SDouglas Gregor       Interval.tv_nsec -= 1000000000;
3037039e358SDouglas Gregor     }
3047039e358SDouglas Gregor #endif
3057039e358SDouglas Gregor   } while (
3067039e358SDouglas Gregor #if LLVM_ON_WIN32
3077039e358SDouglas Gregor            Interval < MaxSeconds * 1000
3087039e358SDouglas Gregor #else
3097039e358SDouglas Gregor            Interval.tv_sec < (time_t)MaxSeconds
3107039e358SDouglas Gregor #endif
3117039e358SDouglas Gregor            );
3127039e358SDouglas Gregor 
3137039e358SDouglas Gregor   // Give up.
31444ec0a7dSArgyrios Kyrtzidis   return Res_Timeout;
3157039e358SDouglas Gregor }
316d2d52de2SBen Langmuir 
317d2d52de2SBen Langmuir std::error_code LockFileManager::unsafeRemoveLockFile() {
31892e1b62dSYaron Keren   return sys::fs::remove(LockFileName);
319d2d52de2SBen Langmuir }
320