]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1464] Added RAII tool to fill holes
authorFrancis Dupont <fdupont@isc.org>
Fri, 9 Oct 2020 08:23:07 +0000 (10:23 +0200)
committerFrancis Dupont <fdupont@isc.org>
Fri, 9 Oct 2020 14:11:16 +0000 (14:11 +0000)
src/lib/dhcpsrv/tests/host_mgr_unittest.cc
src/lib/dhcpsrv/tests/test_utils.cc
src/lib/dhcpsrv/tests/test_utils.h

index 84d67e6b9bbe5bbe737c95c025bc9244de4ac28e..c06c21b5f9216d9af8eecd91a8cf02c17af5bc4c 100644 (file)
@@ -1447,6 +1447,9 @@ HostMgrDbLostCallbackTest::testDbLostCallback() {
     // be the next one.
     int last_open_socket = findLastSocketFd();
 
+    // Fill holes.
+    fillFdHoles holes(last_open_socket);
+
     // Connect to the host backend.
     ASSERT_NO_THROW(HostMgr::addBackend(validConnectString()));
 
index 63fb75fe5ed13c9471ca3c12548f1ea15217e5f0..c15f759f76fc0d1f3cc8aa26b50190adcfdd576b 100644 (file)
@@ -123,6 +123,31 @@ int findLastSocketFd() {
     return (last_socket);
 }
 
-}; // namespace test
-}; // namespace dhcp
-}; // namespace isc
+fillFdHoles::fillFdHoles(int limit) : fds_() {
+    if (limit <= 0) {
+        return;
+    }
+    for (;;) {
+        int fd = open("/dev/null", O_RDWR, 0);
+        if (fd == -1) {
+            return;
+        }
+        if (fd < limit) {
+            fds_.push_front(fd);
+        } else {
+            static_cast<void>(close(fd));
+            return;
+        }
+    }
+}
+
+fillFdHoles::~fillFdHoles() {
+    while (!fds_.empty()) {
+        static_cast<void>(close(fds_.back()));
+        fds_.pop_back();
+    }
+}
+
+} // namespace test
+} // namespace dhcp
+} // namespace isc
index 8dd67cde01628eb49f2ef6d66ecb25592fd5084b..0f2d44f3c380557316be909d4969e78b93fb9010 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2020 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -8,6 +8,7 @@
 #define LIBDHCPSRV_TEST_UTILS_H
 
 #include <dhcpsrv/lease_mgr.h>
+#include <list>
 #include <vector>
 
 namespace isc {
@@ -48,9 +49,33 @@ detailCompareLease(const Lease4Ptr& first, const Lease4Ptr& second);
 /// are none.
 int findLastSocketFd();
 
+/// @brief RAII tool which fills holes in the file descriptor sequence
+///
+/// The @ref findLastSocketFd requires new socket descriptors are allocated
+/// after the last open socket descriptor so there is no hole i.e. a free
+/// file descriptor in the sequence.
+/// This tool detects and fills such holes. It uses the RAII idiom to avoid
+/// file descriptor leaks: the destructor called when the object goes out
+/// of scope closes all file descriptors which were opened by the constructor.
+class fillFdHoles {
+public:
+    /// @brief Constructor
+    ///
+    /// @param limit Holes will be filled up to this limit
+    fillFdHoles(int limit);
+
+    /// @brief Destructor
+    ///
+    /// The destructor closes members of the list
+    ~fillFdHoles();
+
+private:
+    /// @brief The list of holes
+    std::list<int> fds_;
+};
 
-}; // namespace test
-}; // namespace dhcp
-}; // namespace isc
+} // namespace test
+} // namespace dhcp
+} // namespace isc
 
 #endif