1 //===--- LockFileManager.cpp - File-level Locking Utility------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #include "llvm/Support/LockFileManager.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/ADT/StringExtras.h" 12 #include "llvm/Support/FileSystem.h" 13 #include "llvm/Support/MemoryBuffer.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include <sys/stat.h> 16 #include <sys/types.h> 17 #if LLVM_ON_WIN32 18 #include <windows.h> 19 #endif 20 #if LLVM_ON_UNIX 21 #include <unistd.h> 22 #endif 23 using namespace llvm; 24 25 /// \brief Attempt to read the lock file with the given name, if it exists. 26 /// 27 /// \param LockFileName The name of the lock file to read. 28 /// 29 /// \returns The process ID of the process that owns this lock file 30 Optional<std::pair<std::string, int> > 31 LockFileManager::readLockFile(StringRef LockFileName) { 32 // Check whether the lock file exists. If not, clearly there's nothing 33 // to read, so we just return. 34 if (!sys::fs::exists(LockFileName)) 35 return None; 36 37 // Read the owning host and PID out of the lock file. If it appears that the 38 // owning process is dead, the lock file is invalid. 39 std::unique_ptr<MemoryBuffer> MB; 40 if (MemoryBuffer::getFile(LockFileName, MB)) 41 return None; 42 43 StringRef Hostname; 44 StringRef PIDStr; 45 std::tie(Hostname, PIDStr) = getToken(MB->getBuffer(), " "); 46 PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" ")); 47 int PID; 48 if (!PIDStr.getAsInteger(10, PID)) 49 return std::make_pair(std::string(Hostname), PID); 50 51 // Delete the lock file. It's invalid anyway. 52 sys::fs::remove(LockFileName); 53 return None; 54 } 55 56 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) { 57 #if LLVM_ON_UNIX && !defined(__ANDROID__) 58 char MyHostname[256]; 59 MyHostname[255] = 0; 60 MyHostname[0] = 0; 61 gethostname(MyHostname, 255); 62 // Check whether the process is dead. If so, we're done. 63 if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH) 64 return false; 65 #endif 66 67 return true; 68 } 69 70 LockFileManager::LockFileManager(StringRef FileName) 71 { 72 this->FileName = FileName; 73 LockFileName = FileName; 74 LockFileName += ".lock"; 75 76 // If the lock file already exists, don't bother to try to create our own 77 // lock file; it won't work anyway. Just figure out who owns this lock file. 78 if ((Owner = readLockFile(LockFileName))) 79 return; 80 81 // Create a lock file that is unique to this instance. 82 UniqueLockFileName = LockFileName; 83 UniqueLockFileName += "-%%%%%%%%"; 84 int UniqueLockFileID; 85 if (error_code EC 86 = sys::fs::createUniqueFile(UniqueLockFileName.str(), 87 UniqueLockFileID, 88 UniqueLockFileName)) { 89 Error = EC; 90 return; 91 } 92 93 // Write our process ID to our unique lock file. 94 { 95 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); 96 97 #if LLVM_ON_UNIX 98 // FIXME: move getpid() call into LLVM 99 char hostname[256]; 100 hostname[255] = 0; 101 hostname[0] = 0; 102 gethostname(hostname, 255); 103 Out << hostname << ' ' << getpid(); 104 #else 105 Out << "localhost 1"; 106 #endif 107 Out.close(); 108 109 if (Out.has_error()) { 110 // We failed to write out PID, so make up an excuse, remove the 111 // unique lock file, and fail. 112 Error = make_error_code(errc::no_space_on_device); 113 sys::fs::remove(UniqueLockFileName.c_str()); 114 return; 115 } 116 } 117 118 // Create a symbolic link from the lock file name. If this succeeds, we're done. 119 // Note that we are using symbolic link because hard links are not supported 120 // by all filesystems. 121 error_code EC 122 = sys::fs::create_symbolic_link(UniqueLockFileName.str(), 123 LockFileName.str()); 124 if (EC == errc::success) 125 return; 126 127 // Someone else managed to create the lock file first. Wipe out our unique 128 // lock file (it's useless now) and read the process ID from the lock file. 129 sys::fs::remove(UniqueLockFileName.str()); 130 if ((Owner = readLockFile(LockFileName))) 131 return; 132 133 // There is a lock file that nobody owns; try to clean it up and report 134 // an error. 135 sys::fs::remove(LockFileName.str()); 136 Error = EC; 137 } 138 139 LockFileManager::LockFileState LockFileManager::getState() const { 140 if (Owner) 141 return LFS_Shared; 142 143 if (Error) 144 return LFS_Error; 145 146 return LFS_Owned; 147 } 148 149 LockFileManager::~LockFileManager() { 150 if (getState() != LFS_Owned) 151 return; 152 153 // Since we own the lock, remove the lock file and our own unique lock file. 154 sys::fs::remove(LockFileName.str()); 155 sys::fs::remove(UniqueLockFileName.str()); 156 } 157 158 void LockFileManager::waitForUnlock() { 159 if (getState() != LFS_Shared) 160 return; 161 162 #if LLVM_ON_WIN32 163 unsigned long Interval = 1; 164 #else 165 struct timespec Interval; 166 Interval.tv_sec = 0; 167 Interval.tv_nsec = 1000000; 168 #endif 169 // Don't wait more than five minutes for the file to appear. 170 unsigned MaxSeconds = 300; 171 bool LockFileGone = false; 172 do { 173 // Sleep for the designated interval, to allow the owning process time to 174 // finish up and remove the lock file. 175 // FIXME: Should we hook in to system APIs to get a notification when the 176 // lock file is deleted? 177 #if LLVM_ON_WIN32 178 Sleep(Interval); 179 #else 180 nanosleep(&Interval, NULL); 181 #endif 182 bool LockFileJustDisappeared = false; 183 184 // If the lock file is still expected to be there, check whether it still 185 // is. 186 if (!LockFileGone) { 187 bool Exists; 188 if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) { 189 LockFileGone = true; 190 LockFileJustDisappeared = true; 191 } 192 } 193 194 // If the lock file is no longer there, check if the original file is 195 // available now. 196 if (LockFileGone) { 197 if (sys::fs::exists(FileName.str())) { 198 return; 199 } 200 201 // The lock file is gone, so now we're waiting for the original file to 202 // show up. If this just happened, reset our waiting intervals and keep 203 // waiting. 204 if (LockFileJustDisappeared) { 205 MaxSeconds = 5; 206 207 #if LLVM_ON_WIN32 208 Interval = 1; 209 #else 210 Interval.tv_sec = 0; 211 Interval.tv_nsec = 1000000; 212 #endif 213 continue; 214 } 215 } 216 217 // If we're looking for the lock file to disappear, but the process 218 // owning the lock died without cleaning up, just bail out. 219 if (!LockFileGone && 220 !processStillExecuting((*Owner).first, (*Owner).second)) { 221 return; 222 } 223 224 // Exponentially increase the time we wait for the lock to be removed. 225 #if LLVM_ON_WIN32 226 Interval *= 2; 227 #else 228 Interval.tv_sec *= 2; 229 Interval.tv_nsec *= 2; 230 if (Interval.tv_nsec >= 1000000000) { 231 ++Interval.tv_sec; 232 Interval.tv_nsec -= 1000000000; 233 } 234 #endif 235 } while ( 236 #if LLVM_ON_WIN32 237 Interval < MaxSeconds * 1000 238 #else 239 Interval.tv_sec < (time_t)MaxSeconds 240 #endif 241 ); 242 243 // Give up. 244 } 245