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/STLExtras.h" 11d78273f4SReid Kleckner #include "llvm/ADT/StringExtras.h" 127039e358SDouglas Gregor #include "llvm/Support/FileSystem.h" 13d78273f4SReid Kleckner #include "llvm/Support/MemoryBuffer.h" 147039e358SDouglas Gregor #include "llvm/Support/raw_ostream.h" 157039e358SDouglas Gregor #include <sys/stat.h> 16ed0881b2SChandler Carruth #include <sys/types.h> 177039e358SDouglas Gregor #if LLVM_ON_WIN32 187039e358SDouglas Gregor #include <windows.h> 197039e358SDouglas Gregor #endif 207039e358SDouglas Gregor #if LLVM_ON_UNIX 217039e358SDouglas Gregor #include <unistd.h> 227039e358SDouglas Gregor #endif 237039e358SDouglas Gregor using namespace llvm; 247039e358SDouglas Gregor 257039e358SDouglas Gregor /// \brief Attempt to read the lock file with the given name, if it exists. 267039e358SDouglas Gregor /// 277039e358SDouglas Gregor /// \param LockFileName The name of the lock file to read. 287039e358SDouglas Gregor /// 297039e358SDouglas Gregor /// \returns The process ID of the process that owns this lock file 307039e358SDouglas Gregor Optional<std::pair<std::string, int> > 317039e358SDouglas Gregor LockFileManager::readLockFile(StringRef LockFileName) { 327039e358SDouglas Gregor // Check whether the lock file exists. If not, clearly there's nothing 337039e358SDouglas Gregor // to read, so we just return. 349e7a638bSRafael Espindola if (!sys::fs::exists(LockFileName)) 35ef04593dSDavid Blaikie return None; 367039e358SDouglas Gregor 377039e358SDouglas Gregor // Read the owning host and PID out of the lock file. If it appears that the 387039e358SDouglas Gregor // owning process is dead, the lock file is invalid. 3956440fd8SAhmed Charles std::unique_ptr<MemoryBuffer> MB; 407de8ea3dSReid Kleckner if (MemoryBuffer::getFile(LockFileName, MB)) 417de8ea3dSReid Kleckner return None; 427de8ea3dSReid Kleckner 43d78273f4SReid Kleckner StringRef Hostname; 44d78273f4SReid Kleckner StringRef PIDStr; 45d6f1f84fSBenjamin Kramer std::tie(Hostname, PIDStr) = getToken(MB->getBuffer(), " "); 467de8ea3dSReid Kleckner PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" ")); 47d78273f4SReid Kleckner int PID; 487de8ea3dSReid Kleckner if (!PIDStr.getAsInteger(10, PID)) 49d78273f4SReid Kleckner return std::make_pair(std::string(Hostname), PID); 507039e358SDouglas Gregor 517039e358SDouglas Gregor // Delete the lock file. It's invalid anyway. 52d78273f4SReid Kleckner sys::fs::remove(LockFileName); 53ef04593dSDavid Blaikie return None; 547039e358SDouglas Gregor } 557039e358SDouglas Gregor 567039e358SDouglas Gregor bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) { 57c439a426SEvgeniy Stepanov #if LLVM_ON_UNIX && !defined(__ANDROID__) 587039e358SDouglas Gregor char MyHostname[256]; 597039e358SDouglas Gregor MyHostname[255] = 0; 607039e358SDouglas Gregor MyHostname[0] = 0; 617039e358SDouglas Gregor gethostname(MyHostname, 255); 627039e358SDouglas Gregor // Check whether the process is dead. If so, we're done. 637039e358SDouglas Gregor if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH) 647039e358SDouglas Gregor return false; 657039e358SDouglas Gregor #endif 667039e358SDouglas Gregor 677039e358SDouglas Gregor return true; 687039e358SDouglas Gregor } 697039e358SDouglas Gregor 70*4147978cSArgyrios Kyrtzidis #if LLVM_ON_UNIX 71*4147978cSArgyrios Kyrtzidis static error_code unix_create_symbolic_link(const Twine &to, 72*4147978cSArgyrios Kyrtzidis const Twine &from) { 73*4147978cSArgyrios Kyrtzidis // Get arguments. 74*4147978cSArgyrios Kyrtzidis SmallString<128> from_storage; 75*4147978cSArgyrios Kyrtzidis SmallString<128> to_storage; 76*4147978cSArgyrios Kyrtzidis StringRef f = from.toNullTerminatedStringRef(from_storage); 77*4147978cSArgyrios Kyrtzidis StringRef t = to.toNullTerminatedStringRef(to_storage); 78*4147978cSArgyrios Kyrtzidis 79*4147978cSArgyrios Kyrtzidis if (::symlink(t.begin(), f.begin()) == -1) 80*4147978cSArgyrios Kyrtzidis return error_code(errno, system_category()); 81*4147978cSArgyrios Kyrtzidis 82*4147978cSArgyrios Kyrtzidis return error_code::success(); 83*4147978cSArgyrios Kyrtzidis } 84*4147978cSArgyrios Kyrtzidis #endif 85*4147978cSArgyrios Kyrtzidis 867039e358SDouglas Gregor LockFileManager::LockFileManager(StringRef FileName) 877039e358SDouglas Gregor { 88056eafd4SDouglas Gregor this->FileName = FileName; 897039e358SDouglas Gregor LockFileName = FileName; 907039e358SDouglas Gregor LockFileName += ".lock"; 917039e358SDouglas Gregor 927039e358SDouglas Gregor // If the lock file already exists, don't bother to try to create our own 937039e358SDouglas Gregor // lock file; it won't work anyway. Just figure out who owns this lock file. 947039e358SDouglas Gregor if ((Owner = readLockFile(LockFileName))) 957039e358SDouglas Gregor return; 967039e358SDouglas Gregor 977039e358SDouglas Gregor // Create a lock file that is unique to this instance. 987039e358SDouglas Gregor UniqueLockFileName = LockFileName; 997039e358SDouglas Gregor UniqueLockFileName += "-%%%%%%%%"; 1007039e358SDouglas Gregor int UniqueLockFileID; 1017039e358SDouglas Gregor if (error_code EC 102c9d2e5b8SRafael Espindola = sys::fs::createUniqueFile(UniqueLockFileName.str(), 1037039e358SDouglas Gregor UniqueLockFileID, 104c9d2e5b8SRafael Espindola UniqueLockFileName)) { 1057039e358SDouglas Gregor Error = EC; 1067039e358SDouglas Gregor return; 1077039e358SDouglas Gregor } 1087039e358SDouglas Gregor 1097039e358SDouglas Gregor // Write our process ID to our unique lock file. 1107039e358SDouglas Gregor { 1117039e358SDouglas Gregor raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); 1127039e358SDouglas Gregor 1137039e358SDouglas Gregor #if LLVM_ON_UNIX 1147039e358SDouglas Gregor // FIXME: move getpid() call into LLVM 1157039e358SDouglas Gregor char hostname[256]; 1167039e358SDouglas Gregor hostname[255] = 0; 1177039e358SDouglas Gregor hostname[0] = 0; 1187039e358SDouglas Gregor gethostname(hostname, 255); 1197039e358SDouglas Gregor Out << hostname << ' ' << getpid(); 1207039e358SDouglas Gregor #else 1217039e358SDouglas Gregor Out << "localhost 1"; 1227039e358SDouglas Gregor #endif 1237039e358SDouglas Gregor Out.close(); 1247039e358SDouglas Gregor 1257039e358SDouglas Gregor if (Out.has_error()) { 1267039e358SDouglas Gregor // We failed to write out PID, so make up an excuse, remove the 1277039e358SDouglas Gregor // unique lock file, and fail. 1287039e358SDouglas Gregor Error = make_error_code(errc::no_space_on_device); 12981e7fd01SRafael Espindola sys::fs::remove(UniqueLockFileName.c_str()); 1307039e358SDouglas Gregor return; 1317039e358SDouglas Gregor } 1327039e358SDouglas Gregor } 1337039e358SDouglas Gregor 134*4147978cSArgyrios Kyrtzidis while (1) { 135*4147978cSArgyrios Kyrtzidis #if LLVM_ON_UNIX 136*4147978cSArgyrios Kyrtzidis // Create a symbolic link from the lock file name. If this succeeds, we're 137*4147978cSArgyrios Kyrtzidis // done. Note that we are using symbolic link because hard links are not 138*4147978cSArgyrios Kyrtzidis // supported by all filesystems. 139*4147978cSArgyrios Kyrtzidis error_code EC 140*4147978cSArgyrios Kyrtzidis = unix_create_symbolic_link(UniqueLockFileName.str(), 141*4147978cSArgyrios Kyrtzidis LockFileName.str()); 142*4147978cSArgyrios Kyrtzidis #else 143*4147978cSArgyrios Kyrtzidis // We can't use symbolic links for windows. 1444357f645SReid Kleckner // Create a hard link from the lock file name. If this succeeds, we're done. 1457039e358SDouglas Gregor error_code EC 1464357f645SReid Kleckner = sys::fs::create_hard_link(UniqueLockFileName.str(), 1477039e358SDouglas Gregor LockFileName.str()); 148*4147978cSArgyrios Kyrtzidis #endif 1497039e358SDouglas Gregor if (EC == errc::success) 1507039e358SDouglas Gregor return; 1517039e358SDouglas Gregor 152*4147978cSArgyrios Kyrtzidis if (EC != errc::file_exists) { 15362a979ccSArgyrios Kyrtzidis Error = EC; 154*4147978cSArgyrios Kyrtzidis return; 155*4147978cSArgyrios Kyrtzidis } 156*4147978cSArgyrios Kyrtzidis 157*4147978cSArgyrios Kyrtzidis // Someone else managed to create the lock file first. Read the process ID 158*4147978cSArgyrios Kyrtzidis // from the lock file. 159*4147978cSArgyrios Kyrtzidis if ((Owner = readLockFile(LockFileName))) { 160*4147978cSArgyrios Kyrtzidis // Wipe out our unique lock file (it's useless now) 161*4147978cSArgyrios Kyrtzidis sys::fs::remove(UniqueLockFileName.str()); 162*4147978cSArgyrios Kyrtzidis return; 163*4147978cSArgyrios Kyrtzidis } 164*4147978cSArgyrios Kyrtzidis 165*4147978cSArgyrios Kyrtzidis if (!sys::fs::exists(LockFileName.str())) { 166*4147978cSArgyrios Kyrtzidis // The previous owner released the lock file before we could read it. 167*4147978cSArgyrios Kyrtzidis // Try to get ownership again. 168*4147978cSArgyrios Kyrtzidis continue; 169*4147978cSArgyrios Kyrtzidis } 170*4147978cSArgyrios Kyrtzidis 171*4147978cSArgyrios Kyrtzidis // There is a lock file that nobody owns; try to clean it up and get 172*4147978cSArgyrios Kyrtzidis // ownership. 173*4147978cSArgyrios Kyrtzidis if ((EC = sys::fs::remove(LockFileName.str()))) { 174*4147978cSArgyrios Kyrtzidis Error = EC; 175*4147978cSArgyrios Kyrtzidis return; 176*4147978cSArgyrios Kyrtzidis } 177*4147978cSArgyrios Kyrtzidis } 1787039e358SDouglas Gregor } 1797039e358SDouglas Gregor 1807039e358SDouglas Gregor LockFileManager::LockFileState LockFileManager::getState() const { 1817039e358SDouglas Gregor if (Owner) 1827039e358SDouglas Gregor return LFS_Shared; 1837039e358SDouglas Gregor 1847039e358SDouglas Gregor if (Error) 1857039e358SDouglas Gregor return LFS_Error; 1867039e358SDouglas Gregor 1877039e358SDouglas Gregor return LFS_Owned; 1887039e358SDouglas Gregor } 1897039e358SDouglas Gregor 1907039e358SDouglas Gregor LockFileManager::~LockFileManager() { 1917039e358SDouglas Gregor if (getState() != LFS_Owned) 1927039e358SDouglas Gregor return; 1937039e358SDouglas Gregor 1947039e358SDouglas Gregor // Since we own the lock, remove the lock file and our own unique lock file. 19581e7fd01SRafael Espindola sys::fs::remove(LockFileName.str()); 19681e7fd01SRafael Espindola sys::fs::remove(UniqueLockFileName.str()); 1977039e358SDouglas Gregor } 1987039e358SDouglas Gregor 1997039e358SDouglas Gregor void LockFileManager::waitForUnlock() { 2007039e358SDouglas Gregor if (getState() != LFS_Shared) 2017039e358SDouglas Gregor return; 2027039e358SDouglas Gregor 2037039e358SDouglas Gregor #if LLVM_ON_WIN32 2047039e358SDouglas Gregor unsigned long Interval = 1; 2057039e358SDouglas Gregor #else 2067039e358SDouglas Gregor struct timespec Interval; 2077039e358SDouglas Gregor Interval.tv_sec = 0; 2087039e358SDouglas Gregor Interval.tv_nsec = 1000000; 2097039e358SDouglas Gregor #endif 2100cb68460SDouglas Gregor // Don't wait more than five minutes for the file to appear. 2110cb68460SDouglas Gregor unsigned MaxSeconds = 300; 212056eafd4SDouglas Gregor bool LockFileGone = false; 2137039e358SDouglas Gregor do { 2147039e358SDouglas Gregor // Sleep for the designated interval, to allow the owning process time to 2157039e358SDouglas Gregor // finish up and remove the lock file. 2167039e358SDouglas Gregor // FIXME: Should we hook in to system APIs to get a notification when the 2177039e358SDouglas Gregor // lock file is deleted? 2187039e358SDouglas Gregor #if LLVM_ON_WIN32 2197039e358SDouglas Gregor Sleep(Interval); 2207039e358SDouglas Gregor #else 2217039e358SDouglas Gregor nanosleep(&Interval, NULL); 2227039e358SDouglas Gregor #endif 2230cb68460SDouglas Gregor bool LockFileJustDisappeared = false; 2240cb68460SDouglas Gregor 2250cb68460SDouglas Gregor // If the lock file is still expected to be there, check whether it still 2260cb68460SDouglas Gregor // is. 227056eafd4SDouglas Gregor if (!LockFileGone) { 2289e7a638bSRafael Espindola bool Exists; 229056eafd4SDouglas Gregor if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) { 230056eafd4SDouglas Gregor LockFileGone = true; 2310cb68460SDouglas Gregor LockFileJustDisappeared = true; 232056eafd4SDouglas Gregor } 233056eafd4SDouglas Gregor } 2340cb68460SDouglas Gregor 2350cb68460SDouglas Gregor // If the lock file is no longer there, check if the original file is 2360cb68460SDouglas Gregor // available now. 237056eafd4SDouglas Gregor if (LockFileGone) { 2389e7a638bSRafael Espindola if (sys::fs::exists(FileName.str())) { 2397039e358SDouglas Gregor return; 240056eafd4SDouglas Gregor } 2417039e358SDouglas Gregor 2420cb68460SDouglas Gregor // The lock file is gone, so now we're waiting for the original file to 2430cb68460SDouglas Gregor // show up. If this just happened, reset our waiting intervals and keep 2440cb68460SDouglas Gregor // waiting. 2450cb68460SDouglas Gregor if (LockFileJustDisappeared) { 2460cb68460SDouglas Gregor MaxSeconds = 5; 2470cb68460SDouglas Gregor 2480cb68460SDouglas Gregor #if LLVM_ON_WIN32 2490cb68460SDouglas Gregor Interval = 1; 2500cb68460SDouglas Gregor #else 2510cb68460SDouglas Gregor Interval.tv_sec = 0; 2520cb68460SDouglas Gregor Interval.tv_nsec = 1000000; 2530cb68460SDouglas Gregor #endif 2540cb68460SDouglas Gregor continue; 2550cb68460SDouglas Gregor } 2560cb68460SDouglas Gregor } 2570cb68460SDouglas Gregor 2580cb68460SDouglas Gregor // If we're looking for the lock file to disappear, but the process 2590cb68460SDouglas Gregor // owning the lock died without cleaning up, just bail out. 2600cb68460SDouglas Gregor if (!LockFileGone && 2610cb68460SDouglas Gregor !processStillExecuting((*Owner).first, (*Owner).second)) { 2627039e358SDouglas Gregor return; 2630cb68460SDouglas Gregor } 2647039e358SDouglas Gregor 2657039e358SDouglas Gregor // Exponentially increase the time we wait for the lock to be removed. 2667039e358SDouglas Gregor #if LLVM_ON_WIN32 2677039e358SDouglas Gregor Interval *= 2; 2687039e358SDouglas Gregor #else 2697039e358SDouglas Gregor Interval.tv_sec *= 2; 2707039e358SDouglas Gregor Interval.tv_nsec *= 2; 2717039e358SDouglas Gregor if (Interval.tv_nsec >= 1000000000) { 2727039e358SDouglas Gregor ++Interval.tv_sec; 2737039e358SDouglas Gregor Interval.tv_nsec -= 1000000000; 2747039e358SDouglas Gregor } 2757039e358SDouglas Gregor #endif 2767039e358SDouglas Gregor } while ( 2777039e358SDouglas Gregor #if LLVM_ON_WIN32 2787039e358SDouglas Gregor Interval < MaxSeconds * 1000 2797039e358SDouglas Gregor #else 2807039e358SDouglas Gregor Interval.tv_sec < (time_t)MaxSeconds 2817039e358SDouglas Gregor #endif 2827039e358SDouglas Gregor ); 2837039e358SDouglas Gregor 2847039e358SDouglas Gregor // Give up. 2857039e358SDouglas Gregor } 286