static_cast<void>(flock(fd_, LOCK_UN));
}
static_cast<void>(close(fd_));
+ static_cast<void>(remove(lockname_.c_str()));
}
fd_ = -1;
locked_ = false;
typedef boost::shared_ptr<PIDFile> PIDFilePtr;
/// @brief RAII device to handle a lock file to avoid race conditions.
+/// @note If there is a missing component in the path the lock is considered
+/// as being acquired: this does not change the behavior of PIDFile methods
+/// (check() will succeed and write() throw) and does not require unit test
+/// updates as if the constructor throws.
class PIDLock {
public:
/// @brief Constructor
#include <config.h>
+#include <util/filesystem.h>
#include <util/pid_file.h>
#include <gtest/gtest.h>
#include <boost/scoped_ptr.hpp>
}
// get a pid between 40000 and 50000
- pid = randomizePID(10000, 40000);
+ do {
+ pid = randomizePID(10000, 40000);
+ } while (kill(pid, 0) == 0);
// write it
pid_file.write(pid);
PIDLock lock2(absolutePath(TESTLOCKNAME));
EXPECT_FALSE(lock2.isLocked());
+
+ EXPECT_TRUE(file::isFile(absolutePath(TESTLOCKNAME)));
}
/// @brief Test getting and releasing a lock.
PIDLock lock2(absolutePath(TESTLOCKNAME));
EXPECT_TRUE(lock2.isLocked());
}
+ EXPECT_FALSE(file::isFile(absolutePath(TESTLOCKNAME)));
}
/// @brief Test ignoring a path with a missing component.