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