23 #include <sys/types.h> 24 #include <system_error> 33 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) 34 #define USE_OSX_GETHOSTUUID 1 36 #define USE_OSX_GETHOSTUUID 0 39 #if USE_OSX_GETHOSTUUID 40 #include <uuid/uuid.h> 51 LockFileManager::readLockFile(
StringRef LockFileName) {
65 PIDStr = PIDStr.
substr(PIDStr.find_first_not_of(
" "));
67 if (!PIDStr.getAsInteger(10, PID)) {
68 auto Owner = std::make_pair(std::string(Hostname), PID);
69 if (processStillExecuting(Owner.first, Owner.second))
81 #if USE_OSX_GETHOSTUUID 83 struct timespec wait = {1, 0};
85 if (gethostuuid(uuid, &wait) != 0)
86 return std::error_code(errno, std::system_category());
88 uuid_string_t UUIDStr;
89 uuid_unparse(uuid, UUIDStr);
97 gethostname(HostName, 255);
106 return std::error_code();
109 bool LockFileManager::processStillExecuting(
StringRef HostID,
int PID) {
110 #if LLVM_ON_UNIX && !defined(__ANDROID__) 116 if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH)
132 class RemoveUniqueLockFileOnSignal {
134 bool RemoveImmediately;
137 : Filename(Name), RemoveImmediately(
true) {
141 ~RemoveUniqueLockFileOnSignal() {
142 if (!RemoveImmediately) {
151 void lockAcquired() { RemoveImmediately =
false; }
158 this->FileName = FileName;
160 std::string S(
"failed to obtain absolute path for ");
161 S.append(this->FileName.
str());
165 LockFileName = this->FileName;
166 LockFileName +=
".lock";
170 if ((Owner = readLockFile(LockFileName)))
174 UniqueLockFileName = LockFileName;
175 UniqueLockFileName +=
"-%%%%%%%%";
176 int UniqueLockFileID;
178 UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
179 std::string S(
"failed to create unique file ");
180 S.append(UniqueLockFileName.str());
189 setError(EC,
"failed to get host id");
194 Out << HostID <<
' ';
205 std::string S(
"failed to write to ");
206 S.append(UniqueLockFileName.str());
215 RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
222 RemoveUniqueFile.lockAcquired();
227 std::string S(
"failed to create link ");
229 OSS << LockFileName.
str() <<
" to " << UniqueLockFileName.str();
236 if ((Owner = readLockFile(LockFileName))) {
251 std::string S(
"failed to remove lockfile ");
252 S.append(UniqueLockFileName.str());
271 std::string Str(ErrorDiagMsg);
272 std::string ErrCodeMsg = ErrorCode.message();
274 if (!ErrCodeMsg.empty())
275 OSS <<
": " << ErrCodeMsg;
301 struct timespec Interval;
303 Interval.tv_nsec = 1000000;
313 nanosleep(&Interval,
nullptr);
325 if (!processStillExecuting((*Owner).first, (*Owner).second))
332 Interval.tv_sec *= 2;
333 Interval.tv_nsec *= 2;
334 if (Interval.tv_nsec >= 1000000000) {
336 Interval.tv_nsec -= 1000000000;
341 Interval < MaxSeconds * 1000
343 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.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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
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.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
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.
WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds=40)
For a shared lock, wait until the owner releases the lock.
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.
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?
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