24 #include <system_error>
26 #include <sys/types.h>
34 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
35 #define USE_OSX_GETHOSTUUID 1
37 #define USE_OSX_GETHOSTUUID 0
40 #if USE_OSX_GETHOSTUUID
41 #include <uuid/uuid.h>
52 LockFileManager::readLockFile(
StringRef LockFileName) {
66 PIDStr = PIDStr.
substr(PIDStr.find_first_not_of(
" "));
68 if (!PIDStr.getAsInteger(10, PID)) {
69 auto Owner = std::make_pair(std::string(Hostname), PID);
70 if (processStillExecuting(Owner.first, Owner.second))
82 #if USE_OSX_GETHOSTUUID
84 struct timespec wait = {1, 0};
86 if (gethostuuid(uuid, &wait) != 0)
87 return std::error_code(errno, std::system_category());
89 uuid_string_t UUIDStr;
90 uuid_unparse(uuid, UUIDStr);
98 gethostname(HostName, 255);
107 return std::error_code();
110 bool LockFileManager::processStillExecuting(
StringRef HostID,
int PID) {
111 #if LLVM_ON_UNIX && !defined(__ANDROID__)
117 if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
133 class RemoveUniqueLockFileOnSignal {
135 bool RemoveImmediately;
138 : Filename(Name), RemoveImmediately(
true) {
142 ~RemoveUniqueLockFileOnSignal() {
143 if (!RemoveImmediately) {
152 void lockAcquired() { RemoveImmediately =
false; }
159 this->FileName = FileName;
161 std::string S(
"failed to obtain absolute path for ");
162 S.append(this->FileName.
str());
166 LockFileName = this->FileName;
167 LockFileName +=
".lock";
171 if ((Owner = readLockFile(LockFileName)))
175 UniqueLockFileName = LockFileName;
176 UniqueLockFileName +=
"-%%%%%%%%";
177 int UniqueLockFileID;
179 UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
180 std::string S(
"failed to create unique file ");
181 S.append(UniqueLockFileName.
str());
190 setError(EC,
"failed to get host id");
195 Out << HostID <<
' ';
207 std::string S(
"failed to write to ");
208 S.append(UniqueLockFileName.
str());
217 RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
224 RemoveUniqueFile.lockAcquired();
229 std::string S(
"failed to create link ");
231 OSS << LockFileName.
str() <<
" to " << UniqueLockFileName.
str();
238 if ((Owner = readLockFile(LockFileName))) {
253 std::string S(
"failed to remove lockfile ");
254 S.append(UniqueLockFileName.
str());
273 std::string Str(ErrorDiagMsg);
274 std::string ErrCodeMsg =
Error->message();
276 if (!ErrCodeMsg.empty())
277 OSS <<
": " <<
Error->message();
303 struct timespec Interval;
305 Interval.tv_nsec = 1000000;
309 const unsigned MaxSeconds = 5*60;
318 nanosleep(&Interval,
nullptr);
330 if (!processStillExecuting((*Owner).first, (*Owner).second))
337 Interval.tv_sec *= 2;
338 Interval.tv_nsec *= 2;
339 if (Interval.tv_nsec >= 1000000000) {
341 Interval.tv_nsec -= 1000000000;
346 Interval < MaxSeconds * 1000
348 Interval.tv_sec < (time_t)MaxSeconds
static std::error_code getHostID(SmallVectorImpl< char > &HostID)
Represents either an error or a value T.
void setError(std::error_code &EC, StringRef ErrorMsg="")
Set error and error message.
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 getBuffer() const
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
std::error_code make_absolute(const Twine ¤t_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
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...
StringRef str() const
Explicit conversion to StringRef.
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.
Provides ErrorOr<T> smart pointer.
A raw_ostream that writes to an std::string.
The lock file has been created and is owned by this instance of the object.
Lightweight error class with error context and mandatory checking.
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.
std::string getErrorMessage() const
Get error message, or "" if there is no error.
bool exists(file_status status)
Does file exist?
The lock file already exists and is owned by some other instance.