From: Francis Dupont Date: Mon, 2 Sep 2024 08:04:16 +0000 (+0200) Subject: [#3506] Fixed lock code X-Git-Tag: Kea-2.7.3~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9fda4a52bf9b3f16bb51a323a6217fd3fef930a0;p=thirdparty%2Fkea.git [#3506] Fixed lock code --- diff --git a/src/lib/config/unix_command_mgr.cc b/src/lib/config/unix_command_mgr.cc index 77fa87e780..43fc24c789 100644 --- a/src/lib/config/unix_command_mgr.cc +++ b/src/lib/config/unix_command_mgr.cc @@ -573,11 +573,8 @@ UnixCommandMgrImpl::openCommandSocket(const isc::data::ConstElementPtr& socket_i // First let's open lock file. std::string lock_name = getLockName(); - if (lock_fd_ != -1) { - close(lock_fd_); - } - lock_fd_ = open(lock_name.c_str(), O_RDONLY | O_CREAT, 0600); - if (lock_fd_ == -1) { + int new_lock_fd = open(lock_name.c_str(), O_RDONLY | O_CREAT, 0600); + if (new_lock_fd == -1) { std::string errmsg = strerror(errno); isc_throw(SocketError, "cannot create socket lockfile, " << lock_name << ", : " << errmsg); @@ -585,13 +582,12 @@ UnixCommandMgrImpl::openCommandSocket(const isc::data::ConstElementPtr& socket_i // Try to acquire lock. If we can't somebody else is actively // using it. - int ret = flock(lock_fd_, LOCK_EX | LOCK_NB); + int ret = flock(new_lock_fd, LOCK_EX | LOCK_NB); if (ret != 0) { std::string errmsg = strerror(errno); + close(new_lock_fd); isc_throw(SocketError, "cannot lock socket lockfile, " << lock_name << ", : " << errmsg); - close(lock_fd_); - lock_fd_ = -1; } // We have the lock, so let's remove the pre-existing socket @@ -601,6 +597,12 @@ UnixCommandMgrImpl::openCommandSocket(const isc::data::ConstElementPtr& socket_i LOG_INFO(command_logger, COMMAND_ACCEPTOR_START) .arg(socket_name_); + // Close previous lock to avoid file descriptor leak. + if (lock_fd_ != -1) { + close(lock_fd_); + } + lock_fd_ = new_lock_fd; + try { // Start asynchronous acceptor service. acceptor_.reset(new UnixDomainSocketAcceptor(io_service_));