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 // Check whether the lock file exists. If not, clearly there's nothing
33 // to read, so we just return.
35 if (sys::fs::exists(LockFileName, Exists) || !Exists)
38 // Read the owning host and PID out of the lock file. If it appears that the
39 // owning process is dead, the lock file is invalid.
40 OwningPtr<MemoryBuffer> MB;
41 if (MemoryBuffer::getFile(LockFileName, MB))
46 tie(Hostname, PIDStr) = getToken(MB->getBuffer(), " ");
47 PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
49 if (!PIDStr.getAsInteger(10, PID))
50 return std::make_pair(std::string(Hostname), PID);
52 // Delete the lock file. It's invalid anyway.
53 sys::fs::remove(LockFileName);
57 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
58 #if LLVM_ON_UNIX && !defined(__ANDROID__)
62 gethostname(MyHostname, 255);
63 // Check whether the process is dead. If so, we're done.
64 if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
71 LockFileManager::LockFileManager(StringRef FileName)
73 this->FileName = FileName;
74 LockFileName = FileName;
75 LockFileName += ".lock";
77 // If the lock file already exists, don't bother to try to create our own
78 // lock file; it won't work anyway. Just figure out who owns this lock file.
79 if ((Owner = readLockFile(LockFileName)))
82 // Create a lock file that is unique to this instance.
83 UniqueLockFileName = LockFileName;
84 UniqueLockFileName += "-%%%%%%%%";
87 = sys::fs::createUniqueFile(UniqueLockFileName.str(),
89 UniqueLockFileName)) {
94 // Write our process ID to our unique lock file.
96 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
99 // FIXME: move getpid() call into LLVM
103 gethostname(hostname, 255);
104 Out << hostname << ' ' << getpid();
106 Out << "localhost 1";
110 if (Out.has_error()) {
111 // We failed to write out PID, so make up an excuse, remove the
112 // unique lock file, and fail.
113 Error = make_error_code(errc::no_space_on_device);
114 sys::fs::remove(UniqueLockFileName.c_str());
119 // Create a hard link from the lock file name. If this succeeds, we're done.
121 = sys::fs::create_hard_link(UniqueLockFileName.str(),
123 if (EC == errc::success)
126 // Creating the hard link failed.
129 // The creation of the hard link may appear to fail, but if stat'ing the
130 // unique file returns a link count of 2, then we can still declare success.
132 if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
133 StatBuf.st_nlink == 2)
137 // Someone else managed to create the lock file first. Wipe out our unique
138 // lock file (it's useless now) and read the process ID from the lock file.
139 sys::fs::remove(UniqueLockFileName.str());
140 if ((Owner = readLockFile(LockFileName)))
143 // There is a lock file that nobody owns; try to clean it up and report
145 sys::fs::remove(LockFileName.str());
149 LockFileManager::LockFileState LockFileManager::getState() const {
159 LockFileManager::~LockFileManager() {
160 if (getState() != LFS_Owned)
163 // Since we own the lock, remove the lock file and our own unique lock file.
164 sys::fs::remove(LockFileName.str());
165 sys::fs::remove(UniqueLockFileName.str());
168 void LockFileManager::waitForUnlock() {
169 if (getState() != LFS_Shared)
173 unsigned long Interval = 1;
175 struct timespec Interval;
177 Interval.tv_nsec = 1000000;
179 // Don't wait more than five minutes for the file to appear.
180 unsigned MaxSeconds = 300;
181 bool LockFileGone = false;
183 // Sleep for the designated interval, to allow the owning process time to
184 // finish up and remove the lock file.
185 // FIXME: Should we hook in to system APIs to get a notification when the
186 // lock file is deleted?
190 nanosleep(&Interval, NULL);
193 bool LockFileJustDisappeared = false;
195 // If the lock file is still expected to be there, check whether it still
198 if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) {
200 LockFileJustDisappeared = true;
205 // If the lock file is no longer there, check if the original file is
208 if (!sys::fs::exists(FileName.str(), Exists) && Exists) {
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