From: Francis Dupont Date: Fri, 9 Oct 2020 08:23:07 +0000 (+0200) Subject: [#1464] Added RAII tool to fill holes X-Git-Tag: Kea-1.9.1~101 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f1080950c2adf8a3294dac78ee4fc7d2c0e651a8;p=thirdparty%2Fkea.git [#1464] Added RAII tool to fill holes --- diff --git a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc index 84d67e6b9b..c06c21b5f9 100644 --- a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc @@ -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())); diff --git a/src/lib/dhcpsrv/tests/test_utils.cc b/src/lib/dhcpsrv/tests/test_utils.cc index 63fb75fe5e..c15f759f76 100644 --- a/src/lib/dhcpsrv/tests/test_utils.cc +++ b/src/lib/dhcpsrv/tests/test_utils.cc @@ -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(close(fd)); + return; + } + } +} + +fillFdHoles::~fillFdHoles() { + while (!fds_.empty()) { + static_cast(close(fds_.back())); + fds_.pop_back(); + } +} + +} // namespace test +} // namespace dhcp +} // namespace isc diff --git a/src/lib/dhcpsrv/tests/test_utils.h b/src/lib/dhcpsrv/tests/test_utils.h index 8dd67cde01..0f2d44f3c3 100644 --- a/src/lib/dhcpsrv/tests/test_utils.h +++ b/src/lib/dhcpsrv/tests/test_utils.h @@ -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 +#include #include 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 fds_; +}; -}; // namespace test -}; // namespace dhcp -}; // namespace isc +} // namespace test +} // namespace dhcp +} // namespace isc #endif