From 48560d72cd675814d1d7718a9a44d2aea1ef6c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Fri, 25 Jul 2025 09:43:05 +0200 Subject: [PATCH] libcamera: base: {unique,shared}_fd: Warn if closing fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the contained file descriptor cannot be successfully closed, that is usually a sign of a more serious invariant violation, which deserves attention, so report those cases. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/libcamera/base/shared_fd.cpp | 7 +++++-- src/libcamera/base/unique_fd.cpp | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/libcamera/base/shared_fd.cpp b/src/libcamera/base/shared_fd.cpp index 7afc8ca5..81b703a8 100644 --- a/src/libcamera/base/shared_fd.cpp +++ b/src/libcamera/base/shared_fd.cpp @@ -284,8 +284,11 @@ SharedFD::Descriptor::Descriptor(int fd, bool duplicate) SharedFD::Descriptor::~Descriptor() { - if (fd_ != -1) - close(fd_); + if (fd_ >= 0 && close(fd_) < 0) { + int ret = -errno; + LOG(SharedFD, Error) << "Failed to close file descriptor " + << fd_ << ": " << strerror(-ret); + } } } /* namespace libcamera */ diff --git a/src/libcamera/base/unique_fd.cpp b/src/libcamera/base/unique_fd.cpp index d0649e4d..178c08c4 100644 --- a/src/libcamera/base/unique_fd.cpp +++ b/src/libcamera/base/unique_fd.cpp @@ -7,6 +7,7 @@ #include +#include #include #include @@ -98,8 +99,11 @@ void UniqueFD::reset(int fd) std::swap(fd, fd_); - if (fd >= 0) - close(fd); + if (fd >= 0 && close(fd) < 0) { + int ret = -errno; + LOG(UniqueFD, Error) << "Failed to close file descriptor " + << fd << ": " << strerror(-ret); + } } /**