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