From 9bc2e6239b4e57611f6cdac3997cd2bf934a0b2d Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Mon, 15 Sep 2025 15:47:48 +0200 Subject: [PATCH] [#4107] Addressed comments --- src/lib/process/daemon.cc | 2 +- src/lib/util/pid_file.cc | 1 + src/lib/util/pid_file.h | 4 ++++ src/lib/util/tests/pid_file_unittest.cc | 8 +++++++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib/process/daemon.cc b/src/lib/process/daemon.cc index b0491b80b4..9e5bd29e5d 100644 --- a/src/lib/process/daemon.cc +++ b/src/lib/process/daemon.cc @@ -198,7 +198,7 @@ Daemon::setPIDFileName(const std::string& pid_file_name) { " file name may not be empty"); } - pid_file_.reset(new util::PIDFile(pid_file_name)); + pid_file_.reset(new PIDFile(pid_file_name)); } std::string diff --git a/src/lib/util/pid_file.cc b/src/lib/util/pid_file.cc index c18f9e6b9f..f0f5ace972 100644 --- a/src/lib/util/pid_file.cc +++ b/src/lib/util/pid_file.cc @@ -118,6 +118,7 @@ PIDLock::~PIDLock() { static_cast(flock(fd_, LOCK_UN)); } static_cast(close(fd_)); + static_cast(remove(lockname_.c_str())); } fd_ = -1; locked_ = false; diff --git a/src/lib/util/pid_file.h b/src/lib/util/pid_file.h index 7b806c356a..ecf0a368ac 100644 --- a/src/lib/util/pid_file.h +++ b/src/lib/util/pid_file.h @@ -107,6 +107,10 @@ private: typedef boost::shared_ptr 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 diff --git a/src/lib/util/tests/pid_file_unittest.cc b/src/lib/util/tests/pid_file_unittest.cc index ac1cd71301..e843e92e1f 100644 --- a/src/lib/util/tests/pid_file_unittest.cc +++ b/src/lib/util/tests/pid_file_unittest.cc @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -157,7 +158,9 @@ TEST_F(PIDFileTest, pidNotInUse) { } // 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); @@ -217,6 +220,8 @@ TEST_F(PIDFileTest, lock) { PIDLock lock2(absolutePath(TESTLOCKNAME)); EXPECT_FALSE(lock2.isLocked()); + + EXPECT_TRUE(file::isFile(absolutePath(TESTLOCKNAME))); } /// @brief Test getting and releasing a lock. @@ -229,6 +234,7 @@ TEST_F(PIDFileTest, lock2) { PIDLock lock2(absolutePath(TESTLOCKNAME)); EXPECT_TRUE(lock2.isLocked()); } + EXPECT_FALSE(file::isFile(absolutePath(TESTLOCKNAME))); } /// @brief Test ignoring a path with a missing component. -- 2.47.3