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/Support/FileSystem.h"
11 #include "llvm/Support/raw_ostream.h"
14 #include <sys/types.h>
23 /// \brief Attempt to read the lock file with the given name, if it exists.
25 /// \param LockFileName The name of the lock file to read.
27 /// \returns The process ID of the process that owns this lock file
28 Optional<std::pair<std::string, int> >
29 LockFileManager::readLockFile(StringRef LockFileName) {
30 // Check whether the lock file exists. If not, clearly there's nothing
31 // to read, so we just return.
33 if (sys::fs::exists(LockFileName, Exists) || !Exists)
36 // Read the owning host and PID out of the lock file. If it appears that the
37 // owning process is dead, the lock file is invalid.
40 std::ifstream Input(LockFileName.str().c_str());
41 if (Input >> Hostname >> PID && PID > 0 &&
42 processStillExecuting(Hostname, PID))
43 return std::make_pair(Hostname, PID);
45 // Delete the lock file. It's invalid anyway.
47 sys::fs::remove(LockFileName, Existed);
51 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
52 #if LLVM_ON_UNIX && !defined(__ANDROID__)
56 gethostname(MyHostname, 255);
57 // Check whether the process is dead. If so, we're done.
58 if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
65 LockFileManager::LockFileManager(StringRef FileName)
67 this->FileName = FileName;
68 LockFileName = FileName;
69 LockFileName += ".lock";
71 // If the lock file already exists, don't bother to try to create our own
72 // lock file; it won't work anyway. Just figure out who owns this lock file.
73 if ((Owner = readLockFile(LockFileName)))
76 // Create a lock file that is unique to this instance.
77 UniqueLockFileName = LockFileName;
78 UniqueLockFileName += "-%%%%%%%%";
81 = sys::fs::unique_file(UniqueLockFileName.str(),
84 /*makeAbsolute=*/false)) {
89 // Write our process ID to our unique lock file.
91 raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
94 // FIXME: move getpid() call into LLVM
98 gethostname(hostname, 255);
99 Out << hostname << ' ' << getpid();
101 Out << "localhost 1";
105 if (Out.has_error()) {
106 // We failed to write out PID, so make up an excuse, remove the
107 // unique lock file, and fail.
108 Error = make_error_code(errc::no_space_on_device);
110 sys::fs::remove(UniqueLockFileName.c_str(), Existed);
115 // Create a hard link from the lock file name. If this succeeds, we're done.
117 = sys::fs::create_hard_link(UniqueLockFileName.str(),
119 if (EC == errc::success)
122 // Creating the hard link failed.
125 // The creation of the hard link may appear to fail, but if stat'ing the
126 // unique file returns a link count of 2, then we can still declare success.
128 if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
129 StatBuf.st_nlink == 2)
133 // Someone else managed to create the lock file first. Wipe out our unique
134 // lock file (it's useless now) and read the process ID from the lock file.
136 sys::fs::remove(UniqueLockFileName.str(), Existed);
137 if ((Owner = readLockFile(LockFileName)))
140 // There is a lock file that nobody owns; try to clean it up and report
142 sys::fs::remove(LockFileName.str(), Existed);
146 LockFileManager::LockFileState LockFileManager::getState() const {
156 LockFileManager::~LockFileManager() {
157 if (getState() != LFS_Owned)
160 // Since we own the lock, remove the lock file and our own unique lock file.
162 sys::fs::remove(LockFileName.str(), Existed);
163 sys::fs::remove(UniqueLockFileName.str(), Existed);
166 void LockFileManager::waitForUnlock() {
167 if (getState() != LFS_Shared)
171 unsigned long Interval = 1;
173 struct timespec Interval;
175 Interval.tv_nsec = 1000000;
177 // Don't wait more than an hour for the file to appear.
178 const unsigned MaxSeconds = 3600;
179 bool LockFileGone = false;
181 // Sleep for the designated interval, to allow the owning process time to
182 // finish up and remove the lock file.
183 // FIXME: Should we hook in to system APIs to get a notification when the
184 // lock file is deleted?
188 nanosleep(&Interval, NULL);
190 // If the lock file no longer exists, wait for the actual file.
193 if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) {
199 if (!sys::fs::exists(FileName.str(), Exists) && Exists)
203 if (!processStillExecuting((*Owner).first, (*Owner).second))
206 // Exponentially increase the time we wait for the lock to be removed.
210 Interval.tv_sec *= 2;
211 Interval.tv_nsec *= 2;
212 if (Interval.tv_nsec >= 1000000000) {
214 Interval.tv_nsec -= 1000000000;
219 Interval < MaxSeconds * 1000
221 Interval.tv_sec < (time_t)MaxSeconds