]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4090] Addressed comments
authorFrancis Dupont <fdupont@isc.org>
Thu, 25 Sep 2025 20:22:46 +0000 (22:22 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 25 Sep 2025 20:22:46 +0000 (22:22 +0200)
src/bin/lfc/tests/lfc_controller_unittests.cc
src/lib/dhcpsrv/memfile_lease_mgr.cc
src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc

index 871fa21ced0f9e8b7f3091ed711b4fc8d059b213..7520da2e21958fed97f4bb24c9a6ad8b889024d5 100644 (file)
@@ -721,7 +721,7 @@ TEST_F(LFCControllerTest, alreadyRunning) {
     };
     int argc = 14;
     PIDFile pid_file(pstr_);
-    string lockname = pstr_ + ".lock";
+    string lockname = pid_file.getLockname();
     int parent_pid = getpid();
     int child_pid;
     LFCController lfc_controller;
index e6f85151d758e0bbb1b041365076dd6f3d835611..32659b231e5b94e3f3e8eff98e453ca9b18e2b86 100644 (file)
@@ -265,7 +265,17 @@ LFCSetup::execute(const std::string& lease_file) {
 
         LOG_INFO(dhcpsrv_logger, DHCPSRV_MEMFILE_LFC_EXECUTE)
             .arg(process_->getCommandLine());
-        pid_ = process_->spawn();
+        try {
+            pid_ = process_->spawn();
+        } catch (const ProcessSpawnError&) {
+            LOG_ERROR(dhcpsrv_logger, DHCPSRV_MEMFILE_LFC_SPAWN_FAIL);
+            try {
+                pid_file.deleteFile();
+            } catch (...) {
+                // Ignore errors.
+            }
+            return;
+        }
 
         // Write the pid of the child in the pid file.
         pid_file.write(pid_);
@@ -273,17 +283,6 @@ LFCSetup::execute(const std::string& lease_file) {
     } catch (const PIDFileError& ex) {
         LOG_ERROR(dhcpsrv_logger, DHCPSRV_MEMFILE_LFC_FAIL_PID_CREATE)
             .arg(ex.what());
-    } catch (const ProcessSpawnError&) {
-        LOG_ERROR(dhcpsrv_logger, DHCPSRV_MEMFILE_LFC_SPAWN_FAIL);
-        // Reacquire the lock to remove the pid file.
-        PIDLock pid_lock(pid_file.getLockname());
-        if (pid_lock.isLocked() && !pid_file.check()) {
-            try {
-                pid_file.deleteFile();
-            } catch (...) {
-                // Ignore errors.
-            }
-        }
     }
 }
 
index c0d3abd8cf3bb867dfd7a18f0aecedfdb9e8c757..10d03fa641aa8674715ed6cf8d59d23a995307e5 100644 (file)
@@ -4665,12 +4665,12 @@ TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerPersistFalse) {
     expected += "was configured to 'false'\" }";
     EXPECT_EQ(expected, response->str());
 
-    // No reschedule.
+    // Verify that LFC reschedule was not logged.
     string msg = "DHCPSRV_MEMFILE_LFC_RESCHEDULED ";
     msg += "rescheduled Lease File Cleanup";
     EXPECT_EQ(0, countFile(msg));
 
-    // No start.
+    // Verify that LFC start was not logged.
     msg = "DHCPSRV_MEMFILE_LFC_START ";
     msg += "starting Lease File Cleanup";
     EXPECT_EQ(0, countFile(msg));
@@ -4706,18 +4706,18 @@ TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerLfcInterval0) {
                                          Memfile_LeaseMgr::FILE_PID)));
     EXPECT_FALSE(file::exists(pid_file_->getFilename()));
 
-    // No reschedule.
+    // Verify that LFC reschedule was not logged.
     string msg = "DHCPSRV_MEMFILE_LFC_RESCHEDULED ";
     msg += "rescheduled Lease File Cleanup";
     EXPECT_EQ(0, countFile(msg));
 
-    // Start.
+    // Verify that LFC start was logged.
     msg = "DHCPSRV_MEMFILE_LFC_START ";
     msg += "starting Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 }
 
-/// @brief Verifies that lfcStartHandler reschedules and starts lfc.
+/// @brief Verifies that lfcStartHandler reschedules and starts LFC.
 TEST_F(MemfileLeaseMgrLogTest, lfcStartHandler) {
     DatabaseConnection::ParameterMap pmap;
     pmap["universe"] = "4";
@@ -4747,19 +4747,19 @@ TEST_F(MemfileLeaseMgrLogTest, lfcStartHandler) {
                                          Memfile_LeaseMgr::FILE_PID)));
     EXPECT_FALSE(file::exists(pid_file_->getFilename()));
 
-    // Reschedule.
+    // Verify that LFC reschedule was logged.
     string msg = "DHCPSRV_MEMFILE_LFC_RESCHEDULED ";
     msg += "rescheduled Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // Start.
+    // Verify that LFC start was logged.
     msg = "DHCPSRV_MEMFILE_LFC_START ";
     msg += "starting Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 }
 
-/// @brief Verifies that lfcStartHandler reschedules and but not start lfc
-/// if the pid file lock is already taken.
+/// @brief Verifies that lfcStartHandler reschedules and but does not start
+/// LFC if the pid file lock is already taken.
 TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerLocked) {
     DatabaseConnection::ParameterMap pmap;
     pmap["universe"] = "6";
@@ -4784,24 +4784,24 @@ TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerLocked) {
     string expected = "{ \"result\": 3, \"text\": \"kea-lfc already running\" }";
     EXPECT_EQ(expected, response->str());
 
-    // Reschedule.
+    // Verify that LFC reschedule was logged.
     string msg = "DHCPSRV_MEMFILE_LFC_RESCHEDULED ";
     msg += "rescheduled Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // Start.
+    // Verify that LFC start was logged.
     msg = "DHCPSRV_MEMFILE_LFC_START ";
     msg += "starting Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // Locked.
+    // Locked: verify that LFC running was logged.
     msg = "DHCPSRV_MEMFILE_LFC_RUNNING ";
     msg += "Lease File Cleanup instance already running";
     EXPECT_EQ(1, countFile(msg));
 }
 
-/// @brief Verifies that lfcStartHandler reschedules and but not start lfc
-/// if the pid file check says lfc is already running.
+/// @brief Verifies that lfcStartHandler reschedules and but does not start
+/// LFC if the pid file check says lfc is already running.
 TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerAlreadyRunning) {
     DatabaseConnection::ParameterMap pmap;
     pmap["universe"] = "4";
@@ -4829,24 +4829,24 @@ TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerAlreadyRunning) {
     string expected = "{ \"result\": 3, \"text\": \"kea-lfc already running\" }";
     EXPECT_EQ(expected, response->str());
 
-    // Reschedule.
+    // Verify that LFC reschedule was logged.
     string msg = "DHCPSRV_MEMFILE_LFC_RESCHEDULED ";
     msg += "rescheduled Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // Start.
+    // Verify that LFC start was logged.
     msg = "DHCPSRV_MEMFILE_LFC_START ";
     msg += "starting Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // Already running.
+    // Verify that LFC running was logged.
     msg = "DHCPSRV_MEMFILE_LFC_RUNNING ";
     msg += "Lease File Cleanup instance already running";
     EXPECT_EQ(1, countFile(msg));
 }
 
-/// @brief Verifies that lfcStartHandler reschedules and but not start lfc
-/// if the pid file is not writable.
+/// @brief Verifies that lfcStartHandler reschedules and but does not start
+/// LFC if the pid file is not writable.
 TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerPidNotWritable) {
     DatabaseConnection::ParameterMap pmap;
     pmap["universe"] = "6";
@@ -4875,17 +4875,17 @@ TEST_F(MemfileLeaseMgrLogTest, lfcStartHandlerPidNotWritable) {
     string expected = "{ \"result\": 1, \"text\": \"failed to start kea-lfc\" }";
     EXPECT_EQ(expected, response->str());
 
-    // Reschedule.
+    // Verify that LFC reschedule was logged.
     string msg = "DHCPSRV_MEMFILE_LFC_RESCHEDULED ";
     msg += "rescheduled Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // Start.
+    // Verify that LFC start was logged.
     msg = "DHCPSRV_MEMFILE_LFC_START ";
     msg += "starting Lease File Cleanup";
     EXPECT_EQ(1, countFile(msg));
 
-    // PID file is not writable.
+    // PID file is not writable: verify that PID create failure was logged.
     msg = "DHCPSRV_MEMFILE_LFC_FAIL_PID_CREATE ";
     msg += "Lease File Cleanup pid file create: ";
     msg += "Unable to open PID file '";