1 //===--- LockFileManager.cpp - File-level Locking Utility------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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"
16 #include <sys/types.h>
25 /// \brief Attempt to read the lock file with the given name, if it exists.
27 /// \param LockFileName The name of the lock file to read.
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 // Read the owning host and PID out of the lock file. If it appears that the
33 // owning process is dead, the lock file is invalid.
34 std::unique_ptr<MemoryBuffer> MB;
35 if (MemoryBuffer::getFile(LockFileName, MB)) {
36 sys::fs::remove(LockFileName);
42 std::tie(Hostname, PIDStr) = getToken(MB->getBuffer(), " ");
43 PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
45 if (!PIDStr.getAsInteger(10, PID))
46 return std::make_pair(std::string(Hostname), PID);
48 // Delete the lock file. It's invalid anyway.
49 sys::fs::remove(LockFileName);
53 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
54 #if LLVM_ON_UNIX && !defined(__ANDROID__)
58 gethostname(MyHostname, 255);
59 // Check whether the process is dead. If so, we're done.
60 if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
67 LockFileManager::LockFileManager(StringRef FileName)
69 this->FileName = FileName;
70 LockFileName = FileName;
71 LockFileName += ".lock";
73 // If the lock file already exists, don't bother to try to create our own
74 // lock file; it won't work anyway. Just figure out who owns this lock file.
75 if ((Owner = readLockFile(LockFileName)))
78 // Create a lock file that is unique to this instance.
79 UniqueLockFileName = LockFileName;
80 UniqueLockFileName += "-%%%%%%%%";
83 = sys::fs::createUniqueFile(UniqueLockFileName.str(),
85 UniqueLockFileName)) {
90 // Write our process ID to our unique lock file.
92 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
95 // FIXME: move getpid() call into LLVM
99 gethostname(hostname, 255);
100 Out << hostname << ' ' << getpid();
102 Out << "localhost 1";
106 if (Out.has_error()) {
107 // We failed to write out PID, so make up an excuse, remove the
108 // unique lock file, and fail.
109 Error = make_error_code(errc::no_space_on_device);
110 sys::fs::remove(UniqueLockFileName.c_str());
116 // Create a link from the lock file name. If this succeeds, we're done.
118 sys::fs::create_link(UniqueLockFileName.str(), LockFileName.str());
119 if (EC == errc::success)
122 if (EC != errc::file_exists) {
127 // Someone else managed to create the lock file first. Read the process ID
128 // from the lock file.
129 if ((Owner = readLockFile(LockFileName))) {
130 // Wipe out our unique lock file (it's useless now)
131 sys::fs::remove(UniqueLockFileName.str());
135 if (!sys::fs::exists(LockFileName.str())) {
136 // The previous owner released the lock file before we could read it.
137 // Try to get ownership again.
141 // There is a lock file that nobody owns; try to clean it up and get
143 if ((EC = sys::fs::remove(LockFileName.str()))) {
150 LockFileManager::LockFileState LockFileManager::getState() const {
160 LockFileManager::~LockFileManager() {
161 if (getState() != LFS_Owned)
164 // Since we own the lock, remove the lock file and our own unique lock file.
165 sys::fs::remove(LockFileName.str());
166 sys::fs::remove(UniqueLockFileName.str());
169 void LockFileManager::waitForUnlock() {
170 if (getState() != LFS_Shared)
174 unsigned long Interval = 1;
176 struct timespec Interval;
178 Interval.tv_nsec = 1000000;
180 // Don't wait more than five minutes for the file to appear.
181 unsigned MaxSeconds = 300;
182 bool LockFileGone = false;
184 // Sleep for the designated interval, to allow the owning process time to
185 // finish up and remove the lock file.
186 // FIXME: Should we hook in to system APIs to get a notification when the
187 // lock file is deleted?
191 nanosleep(&Interval, NULL);
193 bool LockFileJustDisappeared = false;
195 // If the lock file is still expected to be there, check whether it still
199 if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) {
201 LockFileJustDisappeared = true;
205 // If the lock file is no longer there, check if the original file is
208 if (sys::fs::exists(FileName.str())) {
212 // The lock file is gone, so now we're waiting for the original file to
213 // show up. If this just happened, reset our waiting intervals and keep
215 if (LockFileJustDisappeared) {
222 Interval.tv_nsec = 1000000;
228 // If we're looking for the lock file to disappear, but the process
229 // owning the lock died without cleaning up, just bail out.
231 !processStillExecuting((*Owner).first, (*Owner).second)) {
235 // Exponentially increase the time we wait for the lock to be removed.
239 Interval.tv_sec *= 2;
240 Interval.tv_nsec *= 2;
241 if (Interval.tv_nsec >= 1000000000) {
243 Interval.tv_nsec -= 1000000000;
248 Interval < MaxSeconds * 1000
250 Interval.tv_sec < (time_t)MaxSeconds