24 #include <sys/types.h> 25 #include <system_error> 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 <<
' ';
206 std::string S(
"failed to write to ");
207 S.append(UniqueLockFileName.str());
216 RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
223 RemoveUniqueFile.lockAcquired();
228 std::string S(
"failed to create link ");
230 OSS << LockFileName.
str() <<
" to " << UniqueLockFileName.str();
237 if ((Owner = readLockFile(LockFileName))) {
252 std::string S(
"failed to remove lockfile ");
253 S.append(UniqueLockFileName.str());
272 std::string Str(ErrorDiagMsg);
273 std::string ErrCodeMsg = ErrorCode.message();
275 if (!ErrCodeMsg.empty())
276 OSS <<
": " << ErrCodeMsg;
301 struct timespec Interval;
303 Interval.tv_nsec = 1000000;
307 const unsigned MaxSeconds = 40;
316 nanosleep(&Interval,
nullptr);
328 if (!processStillExecuting((*Owner).first, (*Owner).second))
335 Interval.tv_sec *= 2;
336 Interval.tv_nsec *= 2;
337 if (Interval.tv_nsec >= 1000000000) {
339 Interval.tv_nsec -= 1000000000;
344 Interval < MaxSeconds * 1000
346 Interval.tv_sec < (time_t)MaxSeconds
static std::error_code getHostID(SmallVectorImpl< char > &HostID)
Represents either an error or a value T.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
StringRef getBuffer() const
This class represents lattice values for constants.
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.
std::string getErrorMessage() const
Get error message, or "" if there is no error.
The lock was released successfully.
void setError(const std::error_code &EC, StringRef ErrorMsg="")
Set error and error message.
std::error_code error() const
amdgpu Simplify well known AMD library false Value Value const Twine & Name
void make_absolute(const Twine ¤t_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
An error occurred while trying to create or find the lock file.
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
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.
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).
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.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
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...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Owner died while holding the lock.
A raw_ostream that writes to a file descriptor.
void close()
Manually flush the stream and close the file.
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
A raw_ostream that writes to an std::string.
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.
The lock file already exists and is owned by some other instance.
bool exists(const basic_file_status &status)
Does file exist?
raw_ostream::uuid_t uuid_t