]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4107] Addressed comments
authorFrancis Dupont <fdupont@isc.org>
Mon, 15 Sep 2025 13:47:48 +0000 (15:47 +0200)
committerFrancis Dupont <fdupont@isc.org>
Mon, 15 Sep 2025 13:47:48 +0000 (15:47 +0200)
src/lib/process/daemon.cc
src/lib/util/pid_file.cc
src/lib/util/pid_file.h
src/lib/util/tests/pid_file_unittest.cc

index b0491b80b479b98a4f77053719c40b89535815fc..9e5bd29e5d7cab898b7bae8da8718575af74cf6c 100644 (file)
@@ -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
index c18f9e6b9f87b67304ad5853b852ffa771fe7c11..f0f5ace97282a50eb8772dd1bf6e239a16b36cb0 100644 (file)
@@ -118,6 +118,7 @@ PIDLock::~PIDLock() {
             static_cast<void>(flock(fd_, LOCK_UN));
         }
         static_cast<void>(close(fd_));
+        static_cast<void>(remove(lockname_.c_str()));
     }
     fd_ = -1;
     locked_ = false;
index 7b806c356a7a9518c255a61af5d1b35d57093aab..ecf0a368acadf9024176ae42e354bdbef1db9ad1 100644 (file)
@@ -107,6 +107,10 @@ private:
 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
index ac1cd713010f463fdb4fee4bd3ddda670d1905ac..e843e92e1fef3daa792a8e967918532b82ffe4fb 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <config.h>
 
+#include <util/filesystem.h>
 #include <util/pid_file.h>
 #include <gtest/gtest.h>
 #include <boost/scoped_ptr.hpp>
@@ -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.