[ARM] Rejig MVE load store tests. NFC
[llvm-core.git] / lib / Support / LockFileManager.cpp
blob10181192afbd8da9eccb4069808ac98a49ded660
1 //===--- LockFileManager.cpp - File-level Locking Utility------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/LockFileManager.h"
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/ErrorOr.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/Signals.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cerrno>
20 #include <ctime>
21 #include <memory>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <system_error>
25 #include <tuple>
26 #ifdef _WIN32
27 #include <windows.h>
28 #endif
29 #if LLVM_ON_UNIX
30 #include <unistd.h>
31 #endif
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
39 #if USE_OSX_GETHOSTUUID
40 #include <uuid/uuid.h>
41 #endif
43 using namespace llvm;
45 /// Attempt to read the lock file with the given name, if it exists.
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) {
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.
54 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
55 MemoryBuffer::getFile(LockFileName);
56 if (!MBOrErr) {
57 sys::fs::remove(LockFileName);
58 return None;
60 MemoryBuffer &MB = *MBOrErr.get();
62 StringRef Hostname;
63 StringRef PIDStr;
64 std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
65 PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
66 int PID;
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;
73 // Delete the lock file. It's invalid anyway.
74 sys::fs::remove(LockFileName);
75 return None;
78 static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
79 HostID.clear();
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());
88 uuid_string_t UUIDStr;
89 uuid_unparse(uuid, UUIDStr);
90 StringRef UUIDRef(UUIDStr);
91 HostID.append(UUIDRef.begin(), UUIDRef.end());
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());
101 #else
102 StringRef Dummy("localhost");
103 HostID.append(Dummy.begin(), Dummy.end());
104 #endif
106 return std::error_code();
109 bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
110 #if LLVM_ON_UNIX && !defined(__ANDROID__)
111 SmallString<256> StoredHostID;
112 if (getHostID(StoredHostID))
113 return true; // Conservatively assume it's executing on error.
115 // Check whether the process is dead. If so, we're done.
116 if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
117 return false;
118 #endif
120 return true;
123 namespace {
125 /// An RAII helper object ensure that the unique lock file is removed.
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);
141 ~RemoveUniqueLockFileOnSignal() {
142 if (!RemoveImmediately) {
143 // Leave the signal handler enabled. It will be removed when the lock is
144 // released.
145 return;
147 sys::fs::remove(Filename);
148 sys::DontRemoveFileOnSignal(Filename);
151 void lockAcquired() { RemoveImmediately = false; }
154 } // end anonymous namespace
156 LockFileManager::LockFileManager(StringRef FileName)
158 this->FileName = FileName;
159 if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
160 std::string S("failed to obtain absolute path for ");
161 S.append(this->FileName.str());
162 setError(EC, S);
163 return;
165 LockFileName = this->FileName;
166 LockFileName += ".lock";
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;
173 // Create a lock file that is unique to this instance.
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());
181 setError(EC, S);
182 return;
185 // Write our process ID to our unique lock file.
187 SmallString<256> HostID;
188 if (auto EC = getHostID(HostID)) {
189 setError(EC, "failed to get host id");
190 return;
193 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
194 Out << HostID << ' ';
195 #if LLVM_ON_UNIX
196 Out << getpid();
197 #else
198 Out << "1";
199 #endif
200 Out.close();
202 if (Out.has_error()) {
203 // We failed to write out PID, so report the error, remove the
204 // unique lock file, and fail.
205 std::string S("failed to write to ");
206 S.append(UniqueLockFileName.str());
207 setError(Out.error(), S);
208 sys::fs::remove(UniqueLockFileName);
209 return;
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);
217 while (true) {
218 // Create a link from the lock file name. If this succeeds, we're done.
219 std::error_code EC =
220 sys::fs::create_link(UniqueLockFileName, LockFileName);
221 if (!EC) {
222 RemoveUniqueFile.lockAcquired();
223 return;
226 if (EC != errc::file_exists) {
227 std::string S("failed to create link ");
228 raw_string_ostream OSS(S);
229 OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
230 setError(EC, OSS.str());
231 return;
234 // Someone else managed to create the lock file first. Read the process ID
235 // from the lock file.
236 if ((Owner = readLockFile(LockFileName))) {
237 // Wipe out our unique lock file (it's useless now)
238 sys::fs::remove(UniqueLockFileName);
239 return;
242 if (!sys::fs::exists(LockFileName)) {
243 // The previous owner released the lock file before we could read it.
244 // Try to get ownership again.
245 continue;
248 // There is a lock file that nobody owns; try to clean it up and get
249 // ownership.
250 if ((EC = sys::fs::remove(LockFileName))) {
251 std::string S("failed to remove lockfile ");
252 S.append(UniqueLockFileName.str());
253 setError(EC, S);
254 return;
259 LockFileManager::LockFileState LockFileManager::getState() const {
260 if (Owner)
261 return LFS_Shared;
263 if (ErrorCode)
264 return LFS_Error;
266 return LFS_Owned;
269 std::string LockFileManager::getErrorMessage() const {
270 if (ErrorCode) {
271 std::string Str(ErrorDiagMsg);
272 std::string ErrCodeMsg = ErrorCode.message();
273 raw_string_ostream OSS(Str);
274 if (!ErrCodeMsg.empty())
275 OSS << ": " << ErrCodeMsg;
276 return OSS.str();
278 return "";
281 LockFileManager::~LockFileManager() {
282 if (getState() != LFS_Owned)
283 return;
285 // Since we own the lock, remove the lock file and our own unique lock file.
286 sys::fs::remove(LockFileName);
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);
293 LockFileManager::WaitForUnlockResult LockFileManager::waitForUnlock() {
294 if (getState() != LFS_Shared)
295 return Res_Success;
297 #ifdef _WIN32
298 unsigned long Interval = 1;
299 #else
300 struct timespec Interval;
301 Interval.tv_sec = 0;
302 Interval.tv_nsec = 1000000;
303 #endif
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;
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?
312 #ifdef _WIN32
313 Sleep(Interval);
314 #else
315 nanosleep(&Interval, nullptr);
316 #endif
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.
321 if (!sys::fs::exists(FileName))
322 return Res_OwnerDied;
323 return Res_Success;
326 // If the process owning the lock died without cleaning up, just bail out.
327 if (!processStillExecuting((*Owner).first, (*Owner).second))
328 return Res_OwnerDied;
330 // Exponentially increase the time we wait for the lock to be removed.
331 #ifdef _WIN32
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;
340 #endif
341 } while (
342 #ifdef _WIN32
343 Interval < MaxSeconds * 1000
344 #else
345 Interval.tv_sec < (time_t)MaxSeconds
346 #endif
349 // Give up.
350 return Res_Timeout;
353 std::error_code LockFileManager::unsafeRemoveLockFile() {
354 return sys::fs::remove(LockFileName);