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 //===----------------------------------------------------------------------===//
97039e358SDouglas Gregor #include "llvm/Support/LockFileManager.h"
10d78273f4SReid Kleckner #include "llvm/ADT/StringExtras.h"
112a826e40SRafael Espindola #include "llvm/Support/Errc.h"
127039e358SDouglas Gregor #include "llvm/Support/FileSystem.h"
13d78273f4SReid Kleckner #include "llvm/Support/MemoryBuffer.h"
147039e358SDouglas Gregor #include "llvm/Support/raw_ostream.h"
1563aa8c5dSBen Langmuir #include "llvm/Support/Signals.h"
167039e358SDouglas Gregor #include <sys/stat.h>
17ed0881b2SChandler Carruth #include <sys/types.h>
187039e358SDouglas Gregor #if LLVM_ON_WIN32
197039e358SDouglas Gregor #include <windows.h>
207039e358SDouglas Gregor #endif
217039e358SDouglas Gregor #if LLVM_ON_UNIX
227039e358SDouglas Gregor #include <unistd.h>
237039e358SDouglas Gregor #endif
24450461cbSBen Langmuir 
25450461cbSBen Langmuir #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
26450461cbSBen Langmuir #define USE_OSX_GETHOSTUUID 1
27450461cbSBen Langmuir #else
28450461cbSBen Langmuir #define USE_OSX_GETHOSTUUID 0
29450461cbSBen Langmuir #endif
30450461cbSBen Langmuir 
31450461cbSBen Langmuir #if USE_OSX_GETHOSTUUID
32450461cbSBen Langmuir #include <uuid/uuid.h>
33450461cbSBen Langmuir #endif
347039e358SDouglas Gregor using namespace llvm;
357039e358SDouglas Gregor 
367039e358SDouglas Gregor /// \brief Attempt to read the lock file with the given name, if it exists.
377039e358SDouglas Gregor ///
387039e358SDouglas Gregor /// \param LockFileName The name of the lock file to read.
397039e358SDouglas Gregor ///
407039e358SDouglas Gregor /// \returns The process ID of the process that owns this lock file
417039e358SDouglas Gregor Optional<std::pair<std::string, int> >
427039e358SDouglas Gregor LockFileManager::readLockFile(StringRef LockFileName) {
437039e358SDouglas Gregor   // Read the owning host and PID out of the lock file. If it appears that the
447039e358SDouglas Gregor   // owning process is dead, the lock file is invalid.
45adf21f2aSRafael Espindola   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
46adf21f2aSRafael Espindola       MemoryBuffer::getFile(LockFileName);
47adf21f2aSRafael Espindola   if (!MBOrErr) {
4837575693SArgyrios Kyrtzidis     sys::fs::remove(LockFileName);
497de8ea3dSReid Kleckner     return None;
5037575693SArgyrios Kyrtzidis   }
513f6481d0SRafael Espindola   MemoryBuffer &MB = *MBOrErr.get();
527de8ea3dSReid Kleckner 
53d78273f4SReid Kleckner   StringRef Hostname;
54d78273f4SReid Kleckner   StringRef PIDStr;
553f6481d0SRafael Espindola   std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
567de8ea3dSReid Kleckner   PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
57d78273f4SReid Kleckner   int PID;
5844ec0a7dSArgyrios Kyrtzidis   if (!PIDStr.getAsInteger(10, PID)) {
5944ec0a7dSArgyrios Kyrtzidis     auto Owner = std::make_pair(std::string(Hostname), PID);
6044ec0a7dSArgyrios Kyrtzidis     if (processStillExecuting(Owner.first, Owner.second))
6144ec0a7dSArgyrios Kyrtzidis       return Owner;
6244ec0a7dSArgyrios Kyrtzidis   }
637039e358SDouglas Gregor 
647039e358SDouglas Gregor   // Delete the lock file. It's invalid anyway.
65d78273f4SReid Kleckner   sys::fs::remove(LockFileName);
66ef04593dSDavid Blaikie   return None;
677039e358SDouglas Gregor }
687039e358SDouglas Gregor 
69450461cbSBen Langmuir static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
70450461cbSBen Langmuir   HostID.clear();
71450461cbSBen Langmuir 
72450461cbSBen Langmuir #if USE_OSX_GETHOSTUUID
73450461cbSBen Langmuir   // On OS X, use the more stable hardware UUID instead of hostname.
74450461cbSBen Langmuir   struct timespec wait = {1, 0}; // 1 second.
75450461cbSBen Langmuir   uuid_t uuid;
76450461cbSBen Langmuir   if (gethostuuid(uuid, &wait) != 0)
77450461cbSBen Langmuir     return std::error_code(errno, std::system_category());
78450461cbSBen Langmuir 
79450461cbSBen Langmuir   uuid_string_t UUIDStr;
80450461cbSBen Langmuir   uuid_unparse(uuid, UUIDStr);
81450461cbSBen Langmuir   StringRef UUIDRef(UUIDStr);
82450461cbSBen Langmuir   HostID.append(UUIDRef.begin(), UUIDRef.end());
83450461cbSBen Langmuir 
84450461cbSBen Langmuir #elif LLVM_ON_UNIX
85450461cbSBen Langmuir   char HostName[256];
86450461cbSBen Langmuir   HostName[255] = 0;
87450461cbSBen Langmuir   HostName[0] = 0;
88450461cbSBen Langmuir   gethostname(HostName, 255);
89450461cbSBen Langmuir   StringRef HostNameRef(HostName);
90450461cbSBen Langmuir   HostID.append(HostNameRef.begin(), HostNameRef.end());
91450461cbSBen Langmuir 
92450461cbSBen Langmuir #else
93450461cbSBen Langmuir   StringRef Dummy("localhost");
94450461cbSBen Langmuir   HostID.append(Dummy.begin(), Dummy.end());
95450461cbSBen Langmuir #endif
96450461cbSBen Langmuir 
97450461cbSBen Langmuir   return std::error_code();
98450461cbSBen Langmuir }
99450461cbSBen Langmuir 
100450461cbSBen Langmuir bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
101c439a426SEvgeniy Stepanov #if LLVM_ON_UNIX && !defined(__ANDROID__)
102450461cbSBen Langmuir   SmallString<256> StoredHostID;
103450461cbSBen Langmuir   if (getHostID(StoredHostID))
104450461cbSBen Langmuir     return true; // Conservatively assume it's executing on error.
105450461cbSBen Langmuir 
1067039e358SDouglas Gregor   // Check whether the process is dead. If so, we're done.
107450461cbSBen Langmuir   if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
1087039e358SDouglas Gregor     return false;
1097039e358SDouglas Gregor #endif
1107039e358SDouglas Gregor 
1117039e358SDouglas Gregor   return true;
1127039e358SDouglas Gregor }
1137039e358SDouglas Gregor 
11463aa8c5dSBen Langmuir namespace {
11563aa8c5dSBen Langmuir /// An RAII helper object ensure that the unique lock file is removed.
11663aa8c5dSBen Langmuir ///
11763aa8c5dSBen Langmuir /// Ensures that if there is an error or a signal before we finish acquiring the
11863aa8c5dSBen Langmuir /// lock, the unique file will be removed. And if we successfully take the lock,
11963aa8c5dSBen Langmuir /// the signal handler is left in place so that signals while the lock is held
12063aa8c5dSBen Langmuir /// will remove the unique lock file. The caller should ensure there is a
12163aa8c5dSBen Langmuir /// matching call to sys::DontRemoveFileOnSignal when the lock is released.
12263aa8c5dSBen Langmuir class RemoveUniqueLockFileOnSignal {
12363aa8c5dSBen Langmuir   StringRef Filename;
12463aa8c5dSBen Langmuir   bool RemoveImmediately;
12563aa8c5dSBen Langmuir public:
12663aa8c5dSBen Langmuir   RemoveUniqueLockFileOnSignal(StringRef Name)
12763aa8c5dSBen Langmuir   : Filename(Name), RemoveImmediately(true) {
12863aa8c5dSBen Langmuir     sys::RemoveFileOnSignal(Filename, nullptr);
12963aa8c5dSBen Langmuir   }
13063aa8c5dSBen Langmuir   ~RemoveUniqueLockFileOnSignal() {
13163aa8c5dSBen Langmuir     if (!RemoveImmediately) {
13263aa8c5dSBen Langmuir       // Leave the signal handler enabled. It will be removed when the lock is
13363aa8c5dSBen Langmuir       // released.
13463aa8c5dSBen Langmuir       return;
13563aa8c5dSBen Langmuir     }
13663aa8c5dSBen Langmuir     sys::fs::remove(Filename);
13763aa8c5dSBen Langmuir     sys::DontRemoveFileOnSignal(Filename);
13863aa8c5dSBen Langmuir   }
13963aa8c5dSBen Langmuir   void lockAcquired() { RemoveImmediately = false; }
14063aa8c5dSBen Langmuir };
14163aa8c5dSBen Langmuir } // end anonymous namespace
14263aa8c5dSBen Langmuir 
1437039e358SDouglas Gregor LockFileManager::LockFileManager(StringRef FileName)
1447039e358SDouglas Gregor {
145056eafd4SDouglas Gregor   this->FileName = FileName;
146db4ed0bdSRafael Espindola   if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
147*42b1f65fSBruno Cardoso Lopes     std::string S("failed to obtain absolute path for ");
148*42b1f65fSBruno Cardoso Lopes     S.append(this->FileName.str());
149*42b1f65fSBruno Cardoso Lopes     setError(EC, S);
150900e9a3dSArgyrios Kyrtzidis     return;
151900e9a3dSArgyrios Kyrtzidis   }
152900e9a3dSArgyrios Kyrtzidis   LockFileName = this->FileName;
1537039e358SDouglas Gregor   LockFileName += ".lock";
1547039e358SDouglas Gregor 
1557039e358SDouglas Gregor   // If the lock file already exists, don't bother to try to create our own
1567039e358SDouglas Gregor   // lock file; it won't work anyway. Just figure out who owns this lock file.
1577039e358SDouglas Gregor   if ((Owner = readLockFile(LockFileName)))
1587039e358SDouglas Gregor     return;
1597039e358SDouglas Gregor 
1607039e358SDouglas Gregor   // Create a lock file that is unique to this instance.
1617039e358SDouglas Gregor   UniqueLockFileName = LockFileName;
1627039e358SDouglas Gregor   UniqueLockFileName += "-%%%%%%%%";
1637039e358SDouglas Gregor   int UniqueLockFileID;
164db4ed0bdSRafael Espindola   if (std::error_code EC = sys::fs::createUniqueFile(
16592e1b62dSYaron Keren           UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
166*42b1f65fSBruno Cardoso Lopes     std::string S("failed to create unique file ");
167*42b1f65fSBruno Cardoso Lopes     S.append(UniqueLockFileName.str());
168*42b1f65fSBruno Cardoso Lopes     setError(EC, S);
1697039e358SDouglas Gregor     return;
1707039e358SDouglas Gregor   }
1717039e358SDouglas Gregor 
1727039e358SDouglas Gregor   // Write our process ID to our unique lock file.
1737039e358SDouglas Gregor   {
174450461cbSBen Langmuir     SmallString<256> HostID;
175450461cbSBen Langmuir     if (auto EC = getHostID(HostID)) {
176*42b1f65fSBruno Cardoso Lopes       setError(EC, "failed to get host id");
177450461cbSBen Langmuir       return;
178450461cbSBen Langmuir     }
1795123eecdSBen Langmuir 
180450461cbSBen Langmuir     raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
181450461cbSBen Langmuir     Out << HostID << ' ';
1827039e358SDouglas Gregor #if LLVM_ON_UNIX
183450461cbSBen Langmuir     Out << getpid();
1847039e358SDouglas Gregor #else
185450461cbSBen Langmuir     Out << "1";
1867039e358SDouglas Gregor #endif
1877039e358SDouglas Gregor     Out.close();
1887039e358SDouglas Gregor 
1897039e358SDouglas Gregor     if (Out.has_error()) {
1907039e358SDouglas Gregor       // We failed to write out PID, so make up an excuse, remove the
1917039e358SDouglas Gregor       // unique lock file, and fail.
192*42b1f65fSBruno Cardoso Lopes       auto EC = make_error_code(errc::no_space_on_device);
193*42b1f65fSBruno Cardoso Lopes       std::string S("failed to write to ");
194*42b1f65fSBruno Cardoso Lopes       S.append(UniqueLockFileName.str());
195*42b1f65fSBruno Cardoso Lopes       setError(EC, S);
19692e1b62dSYaron Keren       sys::fs::remove(UniqueLockFileName);
1977039e358SDouglas Gregor       return;
1987039e358SDouglas Gregor     }
1997039e358SDouglas Gregor   }
2007039e358SDouglas Gregor 
20163aa8c5dSBen Langmuir   // Clean up the unique file on signal, which also releases the lock if it is
20263aa8c5dSBen Langmuir   // held since the .lock symlink will point to a nonexistent file.
20363aa8c5dSBen Langmuir   RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
20463aa8c5dSBen Langmuir 
2054147978cSArgyrios Kyrtzidis   while (1) {
20683f858e5SRafael Espindola     // Create a link from the lock file name. If this succeeds, we're done.
207db4ed0bdSRafael Espindola     std::error_code EC =
20892e1b62dSYaron Keren         sys::fs::create_link(UniqueLockFileName, LockFileName);
20963aa8c5dSBen Langmuir     if (!EC) {
21063aa8c5dSBen Langmuir       RemoveUniqueFile.lockAcquired();
2117039e358SDouglas Gregor       return;
21263aa8c5dSBen Langmuir     }
2137039e358SDouglas Gregor 
2142a826e40SRafael Espindola     if (EC != errc::file_exists) {
215*42b1f65fSBruno Cardoso Lopes       std::string S("failed to create link ");
216*42b1f65fSBruno Cardoso Lopes       raw_string_ostream OSS(S);
217*42b1f65fSBruno Cardoso Lopes       OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
218*42b1f65fSBruno Cardoso Lopes       setError(EC, OSS.str());
2194147978cSArgyrios Kyrtzidis       return;
2204147978cSArgyrios Kyrtzidis     }
2214147978cSArgyrios Kyrtzidis 
2224147978cSArgyrios Kyrtzidis     // Someone else managed to create the lock file first. Read the process ID
2234147978cSArgyrios Kyrtzidis     // from the lock file.
2244147978cSArgyrios Kyrtzidis     if ((Owner = readLockFile(LockFileName))) {
2254147978cSArgyrios Kyrtzidis       // Wipe out our unique lock file (it's useless now)
22692e1b62dSYaron Keren       sys::fs::remove(UniqueLockFileName);
2274147978cSArgyrios Kyrtzidis       return;
2284147978cSArgyrios Kyrtzidis     }
2294147978cSArgyrios Kyrtzidis 
23092e1b62dSYaron Keren     if (!sys::fs::exists(LockFileName)) {
2314147978cSArgyrios Kyrtzidis       // The previous owner released the lock file before we could read it.
2324147978cSArgyrios Kyrtzidis       // Try to get ownership again.
2334147978cSArgyrios Kyrtzidis       continue;
2344147978cSArgyrios Kyrtzidis     }
2354147978cSArgyrios Kyrtzidis 
2364147978cSArgyrios Kyrtzidis     // There is a lock file that nobody owns; try to clean it up and get
2374147978cSArgyrios Kyrtzidis     // ownership.
23892e1b62dSYaron Keren     if ((EC = sys::fs::remove(LockFileName))) {
239*42b1f65fSBruno Cardoso Lopes       std::string S("failed to remove lockfile ");
240*42b1f65fSBruno Cardoso Lopes       S.append(UniqueLockFileName.str());
241*42b1f65fSBruno Cardoso Lopes       setError(EC, S);
2424147978cSArgyrios Kyrtzidis       return;
2434147978cSArgyrios Kyrtzidis     }
2444147978cSArgyrios Kyrtzidis   }
2457039e358SDouglas Gregor }
2467039e358SDouglas Gregor 
2477039e358SDouglas Gregor LockFileManager::LockFileState LockFileManager::getState() const {
2487039e358SDouglas Gregor   if (Owner)
2497039e358SDouglas Gregor     return LFS_Shared;
2507039e358SDouglas Gregor 
2517039e358SDouglas Gregor   if (Error)
2527039e358SDouglas Gregor     return LFS_Error;
2537039e358SDouglas Gregor 
2547039e358SDouglas Gregor   return LFS_Owned;
2557039e358SDouglas Gregor }
2567039e358SDouglas Gregor 
257*42b1f65fSBruno Cardoso Lopes std::string LockFileManager::getErrorMessage() const {
258*42b1f65fSBruno Cardoso Lopes   if (Error) {
259*42b1f65fSBruno Cardoso Lopes     std::string Str(ErrorDiagMsg);
260*42b1f65fSBruno Cardoso Lopes     std::string ErrCodeMsg = Error->message();
261*42b1f65fSBruno Cardoso Lopes     raw_string_ostream OSS(Str);
262*42b1f65fSBruno Cardoso Lopes     if (!ErrCodeMsg.empty())
263*42b1f65fSBruno Cardoso Lopes       OSS << ": " << Error->message();
264*42b1f65fSBruno Cardoso Lopes     OSS.flush();
265*42b1f65fSBruno Cardoso Lopes     return Str;
266*42b1f65fSBruno Cardoso Lopes   }
267*42b1f65fSBruno Cardoso Lopes   return "";
268*42b1f65fSBruno Cardoso Lopes }
269*42b1f65fSBruno Cardoso Lopes 
2707039e358SDouglas Gregor LockFileManager::~LockFileManager() {
2717039e358SDouglas Gregor   if (getState() != LFS_Owned)
2727039e358SDouglas Gregor     return;
2737039e358SDouglas Gregor 
2747039e358SDouglas Gregor   // Since we own the lock, remove the lock file and our own unique lock file.
27592e1b62dSYaron Keren   sys::fs::remove(LockFileName);
27692e1b62dSYaron Keren   sys::fs::remove(UniqueLockFileName);
27763aa8c5dSBen Langmuir   // The unique file is now gone, so remove it from the signal handler. This
27863aa8c5dSBen Langmuir   // matches a sys::RemoveFileOnSignal() in LockFileManager().
27963aa8c5dSBen Langmuir   sys::DontRemoveFileOnSignal(UniqueLockFileName);
2807039e358SDouglas Gregor }
2817039e358SDouglas Gregor 
28244ec0a7dSArgyrios Kyrtzidis LockFileManager::WaitForUnlockResult LockFileManager::waitForUnlock() {
2837039e358SDouglas Gregor   if (getState() != LFS_Shared)
28444ec0a7dSArgyrios Kyrtzidis     return Res_Success;
2857039e358SDouglas Gregor 
2867039e358SDouglas Gregor #if LLVM_ON_WIN32
2877039e358SDouglas Gregor   unsigned long Interval = 1;
2887039e358SDouglas Gregor #else
2897039e358SDouglas Gregor   struct timespec Interval;
2907039e358SDouglas Gregor   Interval.tv_sec = 0;
2917039e358SDouglas Gregor   Interval.tv_nsec = 1000000;
2927039e358SDouglas Gregor #endif
293dc8f979bSArgyrios Kyrtzidis   // Don't wait more than five minutes per iteration. Total timeout for the file
294dc8f979bSArgyrios Kyrtzidis   // to appear is ~8.5 mins.
295dc8f979bSArgyrios Kyrtzidis   const unsigned MaxSeconds = 5*60;
2967039e358SDouglas Gregor   do {
2977039e358SDouglas Gregor     // Sleep for the designated interval, to allow the owning process time to
2987039e358SDouglas Gregor     // finish up and remove the lock file.
2997039e358SDouglas Gregor     // FIXME: Should we hook in to system APIs to get a notification when the
3007039e358SDouglas Gregor     // lock file is deleted?
3017039e358SDouglas Gregor #if LLVM_ON_WIN32
3027039e358SDouglas Gregor     Sleep(Interval);
3037039e358SDouglas Gregor #else
304c10719f5SCraig Topper     nanosleep(&Interval, nullptr);
3057039e358SDouglas Gregor #endif
3060cb68460SDouglas Gregor 
307281f23adSRafael Espindola     if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
308281f23adSRafael Espindola         errc::no_such_file_or_directory) {
30908970917SBen Langmuir       // If the original file wasn't created, somone thought the lock was dead.
31092e1b62dSYaron Keren       if (!sys::fs::exists(FileName))
31108970917SBen Langmuir         return Res_OwnerDied;
31244ec0a7dSArgyrios Kyrtzidis       return Res_Success;
313056eafd4SDouglas Gregor     }
3147039e358SDouglas Gregor 
31508970917SBen Langmuir     // If the process owning the lock died without cleaning up, just bail out.
31608970917SBen Langmuir     if (!processStillExecuting((*Owner).first, (*Owner).second))
31744ec0a7dSArgyrios Kyrtzidis       return Res_OwnerDied;
3187039e358SDouglas Gregor 
3197039e358SDouglas Gregor     // Exponentially increase the time we wait for the lock to be removed.
3207039e358SDouglas Gregor #if LLVM_ON_WIN32
3217039e358SDouglas Gregor     Interval *= 2;
3227039e358SDouglas Gregor #else
3237039e358SDouglas Gregor     Interval.tv_sec *= 2;
3247039e358SDouglas Gregor     Interval.tv_nsec *= 2;
3257039e358SDouglas Gregor     if (Interval.tv_nsec >= 1000000000) {
3267039e358SDouglas Gregor       ++Interval.tv_sec;
3277039e358SDouglas Gregor       Interval.tv_nsec -= 1000000000;
3287039e358SDouglas Gregor     }
3297039e358SDouglas Gregor #endif
3307039e358SDouglas Gregor   } while (
3317039e358SDouglas Gregor #if LLVM_ON_WIN32
3327039e358SDouglas Gregor            Interval < MaxSeconds * 1000
3337039e358SDouglas Gregor #else
3347039e358SDouglas Gregor            Interval.tv_sec < (time_t)MaxSeconds
3357039e358SDouglas Gregor #endif
3367039e358SDouglas Gregor            );
3377039e358SDouglas Gregor 
3387039e358SDouglas Gregor   // Give up.
33944ec0a7dSArgyrios Kyrtzidis   return Res_Timeout;
3407039e358SDouglas Gregor }
341d2d52de2SBen Langmuir 
342d2d52de2SBen Langmuir std::error_code LockFileManager::unsafeRemoveLockFile() {
34392e1b62dSYaron Keren   return sys::fs::remove(LockFileName);
344d2d52de2SBen Langmuir }
345