LockFileManager.cpp [plain text]
#include "llvm/Support/LockFileManager.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#if LLVM_ON_WIN32
#include <windows.h>
#endif
#if LLVM_ON_UNIX
#include <unistd.h>
#endif
using namespace llvm;
Optional<std::pair<std::string, int> >
LockFileManager::readLockFile(StringRef LockFileName) {
bool Exists = false;
if (sys::fs::exists(LockFileName, Exists) || !Exists)
return Optional<std::pair<std::string, int> >();
int PID = 0;
std::string Hostname;
std::ifstream Input(LockFileName.str().c_str());
if (Input >> Hostname >> PID && PID > 0 &&
processStillExecuting(Hostname, PID))
return std::make_pair(Hostname, PID);
bool Existed;
sys::fs::remove(LockFileName, Existed);
return Optional<std::pair<std::string, int> >();
}
bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
#if LLVM_ON_UNIX && !defined(__ANDROID__)
char MyHostname[256];
MyHostname[255] = 0;
MyHostname[0] = 0;
gethostname(MyHostname, 255);
if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
return false;
#endif
return true;
}
LockFileManager::LockFileManager(StringRef FileName)
{
LockFileName = FileName;
LockFileName += ".lock";
if ((Owner = readLockFile(LockFileName)))
return;
UniqueLockFileName = LockFileName;
UniqueLockFileName += "-%%%%%%%%";
int UniqueLockFileID;
if (error_code EC
= sys::fs::unique_file(UniqueLockFileName.str(),
UniqueLockFileID,
UniqueLockFileName,
false)) {
Error = EC;
return;
}
{
raw_fd_ostream Out(UniqueLockFileID, true);
#if LLVM_ON_UNIX
char hostname[256];
hostname[255] = 0;
hostname[0] = 0;
gethostname(hostname, 255);
Out << hostname << ' ' << getpid();
#else
Out << "localhost 1";
#endif
Out.close();
if (Out.has_error()) {
Error = make_error_code(errc::no_space_on_device);
bool Existed;
sys::fs::remove(UniqueLockFileName.c_str(), Existed);
return;
}
}
error_code EC
= sys::fs::create_hard_link(UniqueLockFileName.str(),
LockFileName.str());
if (EC == errc::success)
return;
#ifdef LLVM_ON_UNIX
struct stat StatBuf;
if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
StatBuf.st_nlink == 2)
return;
#endif
bool Existed;
sys::fs::remove(UniqueLockFileName.str(), Existed);
if ((Owner = readLockFile(LockFileName)))
return;
sys::fs::remove(LockFileName.str(), Existed);
Error = EC;
}
LockFileManager::LockFileState LockFileManager::getState() const {
if (Owner)
return LFS_Shared;
if (Error)
return LFS_Error;
return LFS_Owned;
}
LockFileManager::~LockFileManager() {
if (getState() != LFS_Owned)
return;
bool Existed;
sys::fs::remove(LockFileName.str(), Existed);
sys::fs::remove(UniqueLockFileName.str(), Existed);
}
void LockFileManager::waitForUnlock() {
if (getState() != LFS_Shared)
return;
#if LLVM_ON_WIN32
unsigned long Interval = 1;
#else
struct timespec Interval;
Interval.tv_sec = 0;
Interval.tv_nsec = 1000000;
#endif
const unsigned MaxSeconds = 3600;
do {
#if LLVM_ON_WIN32
Sleep(Interval);
#else
nanosleep(&Interval, NULL);
#endif
bool Exists = false;
if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists)
return;
if (!processStillExecuting((*Owner).first, (*Owner).second))
return;
#if LLVM_ON_WIN32
Interval *= 2;
#else
Interval.tv_sec *= 2;
Interval.tv_nsec *= 2;
if (Interval.tv_nsec >= 1000000000) {
++Interval.tv_sec;
Interval.tv_nsec -= 1000000000;
}
#endif
} while (
#if LLVM_ON_WIN32
Interval < MaxSeconds * 1000
#else
Interval.tv_sec < (time_t)MaxSeconds
#endif
);
}