17 #include <sys/types.h>
25 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
26 #define USE_OSX_GETHOSTUUID 1
28 #define USE_OSX_GETHOSTUUID 0
31 #if USE_OSX_GETHOSTUUID
32 #include <uuid/uuid.h>
42 LockFileManager::readLockFile(
StringRef LockFileName) {
56 PIDStr = PIDStr.
substr(PIDStr.find_first_not_of(
" "));
58 if (!PIDStr.getAsInteger(10, PID)) {
59 auto Owner = std::make_pair(std::string(Hostname), PID);
60 if (processStillExecuting(Owner.first, Owner.second))
72 #if USE_OSX_GETHOSTUUID
74 struct timespec wait = {1, 0};
76 if (gethostuuid(uuid, &wait) != 0)
77 return std::error_code(errno, std::system_category());
79 uuid_string_t UUIDStr;
80 uuid_unparse(uuid, UUIDStr);
88 gethostname(HostName, 255);
97 return std::error_code();
100 bool LockFileManager::processStillExecuting(
StringRef HostID,
int PID) {
101 #if LLVM_ON_UNIX && !defined(__ANDROID__)
107 if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
122 class RemoveUniqueLockFileOnSignal {
124 bool RemoveImmediately;
127 : Filename(Name), RemoveImmediately(
true) {
130 ~RemoveUniqueLockFileOnSignal() {
131 if (!RemoveImmediately) {
139 void lockAcquired() { RemoveImmediately =
false; }
145 this->FileName = FileName;
150 LockFileName = this->FileName;
151 LockFileName +=
".lock";
155 if ((Owner = readLockFile(LockFileName)))
159 UniqueLockFileName = LockFileName;
160 UniqueLockFileName +=
"-%%%%%%%%";
161 int UniqueLockFileID;
163 UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
177 Out << HostID <<
' ';
196 RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
203 RemoveUniqueFile.lockAcquired();
214 if ((Owner = readLockFile(LockFileName))) {
264 struct timespec Interval;
266 Interval.tv_nsec = 1000000;
270 const unsigned MaxSeconds = 5*60;
279 nanosleep(&Interval,
nullptr);
291 if (!processStillExecuting((*Owner).first, (*Owner).second))
298 Interval.tv_sec *= 2;
299 Interval.tv_nsec *= 2;
300 if (Interval.tv_nsec >= 1000000000) {
302 Interval.tv_nsec -= 1000000000;
307 Interval < MaxSeconds * 1000
309 Interval.tv_sec < (time_t)MaxSeconds
static std::error_code getHostID(SmallVectorImpl< char > &HostID)
Represents either an error or a value T.
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
WaitForUnlockResult
Describes the result of waiting for the owner to release the lock.
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
The lock was released successfully.
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
StringRef getBuffer() const
std::error_code make_absolute(SmallVectorImpl< char > &path)
Make path an absolute path.
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
std::error_code make_error_code(BitcodeError E)
An error occurred while trying to create or find the lock file.
std::error_code unsafeRemoveLockFile()
Remove the lock file.
std::error_code create_link(const Twine &to, const Twine &from)
Create a link from from to to.
LockFileState getState() const
Determine the state of the lock file.
LockFileState
Describes the state of a lock file.
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Reached timeout while waiting for the owner to release the lock.
bool RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg=nullptr)
This function registers signal handlers to ensure that if a signal gets delivered that the named file...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Owner died while holding the lock.
A raw_ostream that writes to a file descriptor.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
void close()
Manually flush the stream and close the file.
The lock file has been created and is owned by this instance of the object.
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
WaitForUnlockResult waitForUnlock()
For a shared lock, wait until the owner releases the lock.
StringRef - Represent a constant reference to a string, i.e.
bool exists(file_status status)
Does file exist?
The lock file already exists and is owned by some other instance.