Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 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" |
Reid Kleckner | d78273f | 2013-08-06 22:51:21 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/StringExtras.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Errc.h" |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FileSystem.h" |
Reid Kleckner | d78273f | 2013-08-06 22:51:21 +0000 | [diff] [blame] | 13 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Ben Langmuir | 63aa8c5 | 2015-06-29 17:08:41 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Signals.h" |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 16 | #include <sys/stat.h> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include <sys/types.h> |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 18 | #if LLVM_ON_WIN32 |
| 19 | #include <windows.h> |
| 20 | #endif |
| 21 | #if LLVM_ON_UNIX |
| 22 | #include <unistd.h> |
| 23 | #endif |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 24 | |
| 25 | #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) |
| 26 | #define USE_OSX_GETHOSTUUID 1 |
| 27 | #else |
| 28 | #define USE_OSX_GETHOSTUUID 0 |
| 29 | #endif |
| 30 | |
| 31 | #if USE_OSX_GETHOSTUUID |
| 32 | #include <uuid/uuid.h> |
| 33 | #endif |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | /// \brief Attempt to read the lock file with the given name, if it exists. |
| 37 | /// |
| 38 | /// \param LockFileName The name of the lock file to read. |
| 39 | /// |
| 40 | /// \returns The process ID of the process that owns this lock file |
| 41 | Optional<std::pair<std::string, int> > |
| 42 | LockFileManager::readLockFile(StringRef LockFileName) { |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 43 | // Read the owning host and PID out of the lock file. If it appears that the |
| 44 | // owning process is dead, the lock file is invalid. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 45 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 46 | MemoryBuffer::getFile(LockFileName); |
| 47 | if (!MBOrErr) { |
Argyrios Kyrtzidis | 3757569 | 2014-03-21 01:25:37 +0000 | [diff] [blame] | 48 | sys::fs::remove(LockFileName); |
Reid Kleckner | 7de8ea3 | 2013-08-07 01:22:04 +0000 | [diff] [blame] | 49 | return None; |
Argyrios Kyrtzidis | 3757569 | 2014-03-21 01:25:37 +0000 | [diff] [blame] | 50 | } |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 51 | MemoryBuffer &MB = *MBOrErr.get(); |
Reid Kleckner | 7de8ea3 | 2013-08-07 01:22:04 +0000 | [diff] [blame] | 52 | |
| 53 | StringRef Hostname; |
| 54 | StringRef PIDStr; |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 55 | std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " "); |
Reid Kleckner | 7de8ea3 | 2013-08-07 01:22:04 +0000 | [diff] [blame] | 56 | PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" ")); |
| 57 | int PID; |
Argyrios Kyrtzidis | 44ec0a7 | 2014-04-06 03:19:31 +0000 | [diff] [blame] | 58 | if (!PIDStr.getAsInteger(10, PID)) { |
| 59 | auto Owner = std::make_pair(std::string(Hostname), PID); |
| 60 | if (processStillExecuting(Owner.first, Owner.second)) |
| 61 | return Owner; |
| 62 | } |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 63 | |
| 64 | // Delete the lock file. It's invalid anyway. |
Reid Kleckner | d78273f | 2013-08-06 22:51:21 +0000 | [diff] [blame] | 65 | sys::fs::remove(LockFileName); |
David Blaikie | ef04593 | 2013-02-21 00:27:28 +0000 | [diff] [blame] | 66 | return None; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 69 | static std::error_code getHostID(SmallVectorImpl<char> &HostID) { |
| 70 | HostID.clear(); |
| 71 | |
| 72 | #if USE_OSX_GETHOSTUUID |
| 73 | // On OS X, use the more stable hardware UUID instead of hostname. |
| 74 | struct timespec wait = {1, 0}; // 1 second. |
| 75 | uuid_t uuid; |
| 76 | if (gethostuuid(uuid, &wait) != 0) |
| 77 | return std::error_code(errno, std::system_category()); |
| 78 | |
| 79 | uuid_string_t UUIDStr; |
| 80 | uuid_unparse(uuid, UUIDStr); |
| 81 | StringRef UUIDRef(UUIDStr); |
| 82 | HostID.append(UUIDRef.begin(), UUIDRef.end()); |
| 83 | |
| 84 | #elif LLVM_ON_UNIX |
| 85 | char HostName[256]; |
| 86 | HostName[255] = 0; |
| 87 | HostName[0] = 0; |
| 88 | gethostname(HostName, 255); |
| 89 | StringRef HostNameRef(HostName); |
| 90 | HostID.append(HostNameRef.begin(), HostNameRef.end()); |
| 91 | |
| 92 | #else |
| 93 | StringRef Dummy("localhost"); |
| 94 | HostID.append(Dummy.begin(), Dummy.end()); |
| 95 | #endif |
| 96 | |
| 97 | return std::error_code(); |
| 98 | } |
| 99 | |
| 100 | bool LockFileManager::processStillExecuting(StringRef HostID, int PID) { |
Evgeniy Stepanov | c439a42 | 2012-09-04 09:14:45 +0000 | [diff] [blame] | 101 | #if LLVM_ON_UNIX && !defined(__ANDROID__) |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 102 | SmallString<256> StoredHostID; |
| 103 | if (getHostID(StoredHostID)) |
| 104 | return true; // Conservatively assume it's executing on error. |
| 105 | |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 106 | // Check whether the process is dead. If so, we're done. |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 107 | if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH) |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 108 | return false; |
| 109 | #endif |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
Ben Langmuir | 63aa8c5 | 2015-06-29 17:08:41 +0000 | [diff] [blame] | 114 | namespace { |
| 115 | /// An RAII helper object ensure that the unique lock file is removed. |
| 116 | /// |
| 117 | /// Ensures that if there is an error or a signal before we finish acquiring the |
| 118 | /// lock, the unique file will be removed. And if we successfully take the lock, |
| 119 | /// the signal handler is left in place so that signals while the lock is held |
| 120 | /// will remove the unique lock file. The caller should ensure there is a |
| 121 | /// matching call to sys::DontRemoveFileOnSignal when the lock is released. |
| 122 | class RemoveUniqueLockFileOnSignal { |
| 123 | StringRef Filename; |
| 124 | bool RemoveImmediately; |
| 125 | public: |
| 126 | RemoveUniqueLockFileOnSignal(StringRef Name) |
| 127 | : Filename(Name), RemoveImmediately(true) { |
| 128 | sys::RemoveFileOnSignal(Filename, nullptr); |
| 129 | } |
| 130 | ~RemoveUniqueLockFileOnSignal() { |
| 131 | if (!RemoveImmediately) { |
| 132 | // Leave the signal handler enabled. It will be removed when the lock is |
| 133 | // released. |
| 134 | return; |
| 135 | } |
| 136 | sys::fs::remove(Filename); |
| 137 | sys::DontRemoveFileOnSignal(Filename); |
| 138 | } |
| 139 | void lockAcquired() { RemoveImmediately = false; } |
| 140 | }; |
| 141 | } // end anonymous namespace |
| 142 | |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 143 | LockFileManager::LockFileManager(StringRef FileName) |
| 144 | { |
Douglas Gregor | 056eafd | 2013-01-10 02:01:35 +0000 | [diff] [blame] | 145 | this->FileName = FileName; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 146 | if (std::error_code EC = sys::fs::make_absolute(this->FileName)) { |
Argyrios Kyrtzidis | 900e9a3 | 2014-03-21 21:45:07 +0000 | [diff] [blame] | 147 | Error = EC; |
| 148 | return; |
| 149 | } |
| 150 | LockFileName = this->FileName; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 151 | LockFileName += ".lock"; |
| 152 | |
| 153 | // If the lock file already exists, don't bother to try to create our own |
| 154 | // lock file; it won't work anyway. Just figure out who owns this lock file. |
| 155 | if ((Owner = readLockFile(LockFileName))) |
| 156 | return; |
| 157 | |
| 158 | // Create a lock file that is unique to this instance. |
| 159 | UniqueLockFileName = LockFileName; |
| 160 | UniqueLockFileName += "-%%%%%%%%"; |
| 161 | int UniqueLockFileID; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 162 | if (std::error_code EC = sys::fs::createUniqueFile( |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 163 | UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) { |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 164 | Error = EC; |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // Write our process ID to our unique lock file. |
| 169 | { |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 170 | SmallString<256> HostID; |
| 171 | if (auto EC = getHostID(HostID)) { |
| 172 | Error = EC; |
| 173 | return; |
| 174 | } |
Ben Langmuir | 5123eec | 2015-06-29 21:56:03 +0000 | [diff] [blame] | 175 | |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 176 | raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); |
| 177 | Out << HostID << ' '; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 178 | #if LLVM_ON_UNIX |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 179 | Out << getpid(); |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 180 | #else |
Ben Langmuir | 450461c | 2015-06-29 22:16:39 +0000 | [diff] [blame^] | 181 | Out << "1"; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 182 | #endif |
| 183 | Out.close(); |
| 184 | |
| 185 | if (Out.has_error()) { |
| 186 | // We failed to write out PID, so make up an excuse, remove the |
| 187 | // unique lock file, and fail. |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 188 | Error = make_error_code(errc::no_space_on_device); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 189 | sys::fs::remove(UniqueLockFileName); |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | } |
| 193 | |
Ben Langmuir | 63aa8c5 | 2015-06-29 17:08:41 +0000 | [diff] [blame] | 194 | // Clean up the unique file on signal, which also releases the lock if it is |
| 195 | // held since the .lock symlink will point to a nonexistent file. |
| 196 | RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName); |
| 197 | |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 198 | while (1) { |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 199 | // Create a link from the lock file name. If this succeeds, we're done. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 200 | std::error_code EC = |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 201 | sys::fs::create_link(UniqueLockFileName, LockFileName); |
Ben Langmuir | 63aa8c5 | 2015-06-29 17:08:41 +0000 | [diff] [blame] | 202 | if (!EC) { |
| 203 | RemoveUniqueFile.lockAcquired(); |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 204 | return; |
Ben Langmuir | 63aa8c5 | 2015-06-29 17:08:41 +0000 | [diff] [blame] | 205 | } |
Argyrios Kyrtzidis | 62a979c | 2014-03-06 17:37:10 +0000 | [diff] [blame] | 206 | |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 207 | if (EC != errc::file_exists) { |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 208 | Error = EC; |
| 209 | return; |
| 210 | } |
Argyrios Kyrtzidis | 62a979c | 2014-03-06 17:37:10 +0000 | [diff] [blame] | 211 | |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 212 | // Someone else managed to create the lock file first. Read the process ID |
| 213 | // from the lock file. |
| 214 | if ((Owner = readLockFile(LockFileName))) { |
| 215 | // Wipe out our unique lock file (it's useless now) |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 216 | sys::fs::remove(UniqueLockFileName); |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 220 | if (!sys::fs::exists(LockFileName)) { |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 221 | // The previous owner released the lock file before we could read it. |
| 222 | // Try to get ownership again. |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | // There is a lock file that nobody owns; try to clean it up and get |
| 227 | // ownership. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 228 | if ((EC = sys::fs::remove(LockFileName))) { |
Argyrios Kyrtzidis | 4147978 | 2014-03-06 20:53:58 +0000 | [diff] [blame] | 229 | Error = EC; |
| 230 | return; |
| 231 | } |
| 232 | } |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | LockFileManager::LockFileState LockFileManager::getState() const { |
| 236 | if (Owner) |
| 237 | return LFS_Shared; |
| 238 | |
| 239 | if (Error) |
| 240 | return LFS_Error; |
| 241 | |
| 242 | return LFS_Owned; |
| 243 | } |
| 244 | |
| 245 | LockFileManager::~LockFileManager() { |
| 246 | if (getState() != LFS_Owned) |
| 247 | return; |
| 248 | |
| 249 | // Since we own the lock, remove the lock file and our own unique lock file. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 250 | sys::fs::remove(LockFileName); |
| 251 | sys::fs::remove(UniqueLockFileName); |
Ben Langmuir | 63aa8c5 | 2015-06-29 17:08:41 +0000 | [diff] [blame] | 252 | // The unique file is now gone, so remove it from the signal handler. This |
| 253 | // matches a sys::RemoveFileOnSignal() in LockFileManager(). |
| 254 | sys::DontRemoveFileOnSignal(UniqueLockFileName); |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Argyrios Kyrtzidis | 44ec0a7 | 2014-04-06 03:19:31 +0000 | [diff] [blame] | 257 | LockFileManager::WaitForUnlockResult LockFileManager::waitForUnlock() { |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 258 | if (getState() != LFS_Shared) |
Argyrios Kyrtzidis | 44ec0a7 | 2014-04-06 03:19:31 +0000 | [diff] [blame] | 259 | return Res_Success; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 260 | |
| 261 | #if LLVM_ON_WIN32 |
| 262 | unsigned long Interval = 1; |
| 263 | #else |
| 264 | struct timespec Interval; |
| 265 | Interval.tv_sec = 0; |
| 266 | Interval.tv_nsec = 1000000; |
| 267 | #endif |
Argyrios Kyrtzidis | dc8f979 | 2015-03-04 22:54:38 +0000 | [diff] [blame] | 268 | // Don't wait more than five minutes per iteration. Total timeout for the file |
| 269 | // to appear is ~8.5 mins. |
| 270 | const unsigned MaxSeconds = 5*60; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 271 | do { |
| 272 | // Sleep for the designated interval, to allow the owning process time to |
| 273 | // finish up and remove the lock file. |
| 274 | // FIXME: Should we hook in to system APIs to get a notification when the |
| 275 | // lock file is deleted? |
| 276 | #if LLVM_ON_WIN32 |
| 277 | Sleep(Interval); |
| 278 | #else |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 279 | nanosleep(&Interval, nullptr); |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 280 | #endif |
Douglas Gregor | 0cb6846 | 2013-04-05 20:53:57 +0000 | [diff] [blame] | 281 | |
Ben Langmuir | 0897091 | 2015-02-19 18:22:35 +0000 | [diff] [blame] | 282 | if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) == |
| 283 | errc::no_such_file_or_directory) { |
| 284 | // If the original file wasn't created, somone thought the lock was dead. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 285 | if (!sys::fs::exists(FileName)) |
Ben Langmuir | 0897091 | 2015-02-19 18:22:35 +0000 | [diff] [blame] | 286 | return Res_OwnerDied; |
| 287 | return Res_Success; |
Douglas Gregor | 056eafd | 2013-01-10 02:01:35 +0000 | [diff] [blame] | 288 | } |
Douglas Gregor | 0cb6846 | 2013-04-05 20:53:57 +0000 | [diff] [blame] | 289 | |
Ben Langmuir | 0897091 | 2015-02-19 18:22:35 +0000 | [diff] [blame] | 290 | // If the process owning the lock died without cleaning up, just bail out. |
| 291 | if (!processStillExecuting((*Owner).first, (*Owner).second)) |
Argyrios Kyrtzidis | 44ec0a7 | 2014-04-06 03:19:31 +0000 | [diff] [blame] | 292 | return Res_OwnerDied; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 293 | |
| 294 | // Exponentially increase the time we wait for the lock to be removed. |
| 295 | #if LLVM_ON_WIN32 |
| 296 | Interval *= 2; |
| 297 | #else |
| 298 | Interval.tv_sec *= 2; |
| 299 | Interval.tv_nsec *= 2; |
| 300 | if (Interval.tv_nsec >= 1000000000) { |
| 301 | ++Interval.tv_sec; |
| 302 | Interval.tv_nsec -= 1000000000; |
| 303 | } |
| 304 | #endif |
| 305 | } while ( |
| 306 | #if LLVM_ON_WIN32 |
| 307 | Interval < MaxSeconds * 1000 |
| 308 | #else |
| 309 | Interval.tv_sec < (time_t)MaxSeconds |
| 310 | #endif |
| 311 | ); |
| 312 | |
| 313 | // Give up. |
Argyrios Kyrtzidis | 44ec0a7 | 2014-04-06 03:19:31 +0000 | [diff] [blame] | 314 | return Res_Timeout; |
Douglas Gregor | 7039e35 | 2012-01-29 20:15:10 +0000 | [diff] [blame] | 315 | } |
Ben Langmuir | d2d52de | 2015-02-09 20:34:24 +0000 | [diff] [blame] | 316 | |
| 317 | std::error_code LockFileManager::unsafeRemoveLockFile() { |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 318 | return sys::fs::remove(LockFileName); |
Ben Langmuir | d2d52de | 2015-02-09 20:34:24 +0000 | [diff] [blame] | 319 | } |