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"
107039e358SDouglas Gregor #include "llvm/Support/FileSystem.h"
117039e358SDouglas Gregor #include "llvm/Support/raw_ostream.h"
127039e358SDouglas Gregor #include <fstream>
137039e358SDouglas Gregor #include <sys/stat.h>
14ed0881b2SChandler Carruth #include <sys/types.h>
157039e358SDouglas Gregor #if LLVM_ON_WIN32
167039e358SDouglas Gregor #include <windows.h>
177039e358SDouglas Gregor #endif
187039e358SDouglas Gregor #if LLVM_ON_UNIX
197039e358SDouglas Gregor #include <unistd.h>
207039e358SDouglas Gregor #endif
217039e358SDouglas Gregor using namespace llvm;
227039e358SDouglas Gregor 
237039e358SDouglas Gregor /// \brief Attempt to read the lock file with the given name, if it exists.
247039e358SDouglas Gregor ///
257039e358SDouglas Gregor /// \param LockFileName The name of the lock file to read.
267039e358SDouglas Gregor ///
277039e358SDouglas Gregor /// \returns The process ID of the process that owns this lock file
287039e358SDouglas Gregor Optional<std::pair<std::string, int> >
297039e358SDouglas Gregor LockFileManager::readLockFile(StringRef LockFileName) {
307039e358SDouglas Gregor   // Check whether the lock file exists. If not, clearly there's nothing
317039e358SDouglas Gregor   // to read, so we just return.
327039e358SDouglas Gregor   bool Exists = false;
337039e358SDouglas Gregor   if (sys::fs::exists(LockFileName, Exists) || !Exists)
34*ef04593dSDavid Blaikie     return None;
357039e358SDouglas Gregor 
367039e358SDouglas Gregor   // Read the owning host and PID out of the lock file. If it appears that the
377039e358SDouglas Gregor   // owning process is dead, the lock file is invalid.
387039e358SDouglas Gregor   int PID = 0;
397039e358SDouglas Gregor   std::string Hostname;
407039e358SDouglas Gregor   std::ifstream Input(LockFileName.str().c_str());
417039e358SDouglas Gregor   if (Input >> Hostname >> PID && PID > 0 &&
427039e358SDouglas Gregor       processStillExecuting(Hostname, PID))
437039e358SDouglas Gregor     return std::make_pair(Hostname, PID);
447039e358SDouglas Gregor 
457039e358SDouglas Gregor   // Delete the lock file. It's invalid anyway.
467039e358SDouglas Gregor   bool Existed;
477039e358SDouglas Gregor   sys::fs::remove(LockFileName, Existed);
48*ef04593dSDavid Blaikie   return None;
497039e358SDouglas Gregor }
507039e358SDouglas Gregor 
517039e358SDouglas Gregor bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
52c439a426SEvgeniy Stepanov #if LLVM_ON_UNIX && !defined(__ANDROID__)
537039e358SDouglas Gregor   char MyHostname[256];
547039e358SDouglas Gregor   MyHostname[255] = 0;
557039e358SDouglas Gregor   MyHostname[0] = 0;
567039e358SDouglas Gregor   gethostname(MyHostname, 255);
577039e358SDouglas Gregor   // Check whether the process is dead. If so, we're done.
587039e358SDouglas Gregor   if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
597039e358SDouglas Gregor     return false;
607039e358SDouglas Gregor #endif
617039e358SDouglas Gregor 
627039e358SDouglas Gregor   return true;
637039e358SDouglas Gregor }
647039e358SDouglas Gregor 
657039e358SDouglas Gregor LockFileManager::LockFileManager(StringRef FileName)
667039e358SDouglas Gregor {
67056eafd4SDouglas Gregor   this->FileName = FileName;
687039e358SDouglas Gregor   LockFileName = FileName;
697039e358SDouglas Gregor   LockFileName += ".lock";
707039e358SDouglas Gregor 
717039e358SDouglas Gregor   // If the lock file already exists, don't bother to try to create our own
727039e358SDouglas Gregor   // lock file; it won't work anyway. Just figure out who owns this lock file.
737039e358SDouglas Gregor   if ((Owner = readLockFile(LockFileName)))
747039e358SDouglas Gregor     return;
757039e358SDouglas Gregor 
767039e358SDouglas Gregor   // Create a lock file that is unique to this instance.
777039e358SDouglas Gregor   UniqueLockFileName = LockFileName;
787039e358SDouglas Gregor   UniqueLockFileName += "-%%%%%%%%";
797039e358SDouglas Gregor   int UniqueLockFileID;
807039e358SDouglas Gregor   if (error_code EC
817039e358SDouglas Gregor         = sys::fs::unique_file(UniqueLockFileName.str(),
827039e358SDouglas Gregor                                      UniqueLockFileID,
837039e358SDouglas Gregor                                      UniqueLockFileName,
847039e358SDouglas Gregor                                      /*makeAbsolute=*/false)) {
857039e358SDouglas Gregor     Error = EC;
867039e358SDouglas Gregor     return;
877039e358SDouglas Gregor   }
887039e358SDouglas Gregor 
897039e358SDouglas Gregor   // Write our process ID to our unique lock file.
907039e358SDouglas Gregor   {
917039e358SDouglas Gregor     raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
927039e358SDouglas Gregor 
937039e358SDouglas Gregor #if LLVM_ON_UNIX
947039e358SDouglas Gregor     // FIXME: move getpid() call into LLVM
957039e358SDouglas Gregor     char hostname[256];
967039e358SDouglas Gregor     hostname[255] = 0;
977039e358SDouglas Gregor     hostname[0] = 0;
987039e358SDouglas Gregor     gethostname(hostname, 255);
997039e358SDouglas Gregor     Out << hostname << ' ' << getpid();
1007039e358SDouglas Gregor #else
1017039e358SDouglas Gregor     Out << "localhost 1";
1027039e358SDouglas Gregor #endif
1037039e358SDouglas Gregor     Out.close();
1047039e358SDouglas Gregor 
1057039e358SDouglas Gregor     if (Out.has_error()) {
1067039e358SDouglas Gregor       // We failed to write out PID, so make up an excuse, remove the
1077039e358SDouglas Gregor       // unique lock file, and fail.
1087039e358SDouglas Gregor       Error = make_error_code(errc::no_space_on_device);
1097039e358SDouglas Gregor       bool Existed;
1107039e358SDouglas Gregor       sys::fs::remove(UniqueLockFileName.c_str(), Existed);
1117039e358SDouglas Gregor       return;
1127039e358SDouglas Gregor     }
1137039e358SDouglas Gregor   }
1147039e358SDouglas Gregor 
1157039e358SDouglas Gregor   // Create a hard link from the lock file name. If this succeeds, we're done.
1167039e358SDouglas Gregor   error_code EC
1177039e358SDouglas Gregor     = sys::fs::create_hard_link(UniqueLockFileName.str(),
1187039e358SDouglas Gregor                                       LockFileName.str());
1197039e358SDouglas Gregor   if (EC == errc::success)
1207039e358SDouglas Gregor     return;
1217039e358SDouglas Gregor 
1227039e358SDouglas Gregor   // Creating the hard link failed.
1237039e358SDouglas Gregor 
1247039e358SDouglas Gregor #ifdef LLVM_ON_UNIX
1257039e358SDouglas Gregor   // The creation of the hard link may appear to fail, but if stat'ing the
1267039e358SDouglas Gregor   // unique file returns a link count of 2, then we can still declare success.
1277039e358SDouglas Gregor   struct stat StatBuf;
1287039e358SDouglas Gregor   if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
1297039e358SDouglas Gregor       StatBuf.st_nlink == 2)
1307039e358SDouglas Gregor     return;
1317039e358SDouglas Gregor #endif
1327039e358SDouglas Gregor 
1337039e358SDouglas Gregor   // Someone else managed to create the lock file first. Wipe out our unique
1347039e358SDouglas Gregor   // lock file (it's useless now) and read the process ID from the lock file.
1357039e358SDouglas Gregor   bool Existed;
1367039e358SDouglas Gregor   sys::fs::remove(UniqueLockFileName.str(), Existed);
1377039e358SDouglas Gregor   if ((Owner = readLockFile(LockFileName)))
1387039e358SDouglas Gregor     return;
1397039e358SDouglas Gregor 
1407039e358SDouglas Gregor   // There is a lock file that nobody owns; try to clean it up and report
1417039e358SDouglas Gregor   // an error.
1427039e358SDouglas Gregor   sys::fs::remove(LockFileName.str(), Existed);
1437039e358SDouglas Gregor   Error = EC;
1447039e358SDouglas Gregor }
1457039e358SDouglas Gregor 
1467039e358SDouglas Gregor LockFileManager::LockFileState LockFileManager::getState() const {
1477039e358SDouglas Gregor   if (Owner)
1487039e358SDouglas Gregor     return LFS_Shared;
1497039e358SDouglas Gregor 
1507039e358SDouglas Gregor   if (Error)
1517039e358SDouglas Gregor     return LFS_Error;
1527039e358SDouglas Gregor 
1537039e358SDouglas Gregor   return LFS_Owned;
1547039e358SDouglas Gregor }
1557039e358SDouglas Gregor 
1567039e358SDouglas Gregor LockFileManager::~LockFileManager() {
1577039e358SDouglas Gregor   if (getState() != LFS_Owned)
1587039e358SDouglas Gregor     return;
1597039e358SDouglas Gregor 
1607039e358SDouglas Gregor   // Since we own the lock, remove the lock file and our own unique lock file.
1617039e358SDouglas Gregor   bool Existed;
1627039e358SDouglas Gregor   sys::fs::remove(LockFileName.str(), Existed);
1637039e358SDouglas Gregor   sys::fs::remove(UniqueLockFileName.str(), Existed);
1647039e358SDouglas Gregor }
1657039e358SDouglas Gregor 
1667039e358SDouglas Gregor void LockFileManager::waitForUnlock() {
1677039e358SDouglas Gregor   if (getState() != LFS_Shared)
1687039e358SDouglas Gregor     return;
1697039e358SDouglas Gregor 
1707039e358SDouglas Gregor #if LLVM_ON_WIN32
1717039e358SDouglas Gregor   unsigned long Interval = 1;
1727039e358SDouglas Gregor #else
1737039e358SDouglas Gregor   struct timespec Interval;
1747039e358SDouglas Gregor   Interval.tv_sec = 0;
1757039e358SDouglas Gregor   Interval.tv_nsec = 1000000;
1767039e358SDouglas Gregor #endif
1777039e358SDouglas Gregor   // Don't wait more than an hour for the file to appear.
1787039e358SDouglas Gregor   const unsigned MaxSeconds = 3600;
179056eafd4SDouglas Gregor   bool LockFileGone = false;
1807039e358SDouglas Gregor   do {
1817039e358SDouglas Gregor     // Sleep for the designated interval, to allow the owning process time to
1827039e358SDouglas Gregor     // finish up and remove the lock file.
1837039e358SDouglas Gregor     // FIXME: Should we hook in to system APIs to get a notification when the
1847039e358SDouglas Gregor     // lock file is deleted?
1857039e358SDouglas Gregor #if LLVM_ON_WIN32
1867039e358SDouglas Gregor     Sleep(Interval);
1877039e358SDouglas Gregor #else
1887039e358SDouglas Gregor     nanosleep(&Interval, NULL);
1897039e358SDouglas Gregor #endif
190056eafd4SDouglas Gregor     // If the lock file no longer exists, wait for the actual file.
1917039e358SDouglas Gregor     bool Exists = false;
192056eafd4SDouglas Gregor     if (!LockFileGone) {
193056eafd4SDouglas Gregor       if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) {
194056eafd4SDouglas Gregor         LockFileGone = true;
195056eafd4SDouglas Gregor         Exists = false;
196056eafd4SDouglas Gregor       }
197056eafd4SDouglas Gregor     }
198056eafd4SDouglas Gregor     if (LockFileGone) {
199056eafd4SDouglas Gregor       if (!sys::fs::exists(FileName.str(), Exists) && Exists)
2007039e358SDouglas Gregor         return;
201056eafd4SDouglas Gregor     }
2027039e358SDouglas Gregor 
2037039e358SDouglas Gregor     if (!processStillExecuting((*Owner).first, (*Owner).second))
2047039e358SDouglas Gregor       return;
2057039e358SDouglas Gregor 
2067039e358SDouglas Gregor     // Exponentially increase the time we wait for the lock to be removed.
2077039e358SDouglas Gregor #if LLVM_ON_WIN32
2087039e358SDouglas Gregor     Interval *= 2;
2097039e358SDouglas Gregor #else
2107039e358SDouglas Gregor     Interval.tv_sec *= 2;
2117039e358SDouglas Gregor     Interval.tv_nsec *= 2;
2127039e358SDouglas Gregor     if (Interval.tv_nsec >= 1000000000) {
2137039e358SDouglas Gregor       ++Interval.tv_sec;
2147039e358SDouglas Gregor       Interval.tv_nsec -= 1000000000;
2157039e358SDouglas Gregor     }
2167039e358SDouglas Gregor #endif
2177039e358SDouglas Gregor   } while (
2187039e358SDouglas Gregor #if LLVM_ON_WIN32
2197039e358SDouglas Gregor            Interval < MaxSeconds * 1000
2207039e358SDouglas Gregor #else
2217039e358SDouglas Gregor            Interval.tv_sec < (time_t)MaxSeconds
2227039e358SDouglas Gregor #endif
2237039e358SDouglas Gregor            );
2247039e358SDouglas Gregor 
2257039e358SDouglas Gregor   // Give up.
2267039e358SDouglas Gregor }
227