From patchwork Thu Apr 24 11:41:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23259 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id EEA10C3321 for ; Thu, 24 Apr 2025 11:41:21 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1502368AD2; Thu, 24 Apr 2025 13:41:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="JT5mr0SB"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 294AF68ADB for ; Thu, 24 Apr 2025 13:41:10 +0200 (CEST) Received: from pb-laptop.local (185.221.143.16.nat.pool.zt.hu [185.221.143.16]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F096019EB; Thu, 24 Apr 2025 13:41:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1745494868; bh=pHOL+k+Dy/DcIWAJ0Djp8YpzXaUs4tOTqsrd2bdl+fA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JT5mr0SBCSHsEHRF4X5fd4y37nc5rf4YXUN54sMoy8dDMHjFooR3c7gC/xjX5ZzQt zj0iDrtyBvRPqa9ulDMPSxLja5+6mIoNHWrGPN6UVsX/TwgGU60KP1CNJfSYRqiRMV PAZzvVGWpaJpohaYnlEfTcvLmY9XHMVR9g+/SD/c= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Laurent Pinchart Subject: [RFC PATCH v5 7/9] libcamera: process: Move `closeAllFdsExcept()` Date: Thu, 24 Apr 2025 13:41:01 +0200 Message-ID: <20250424114103.451395-8-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250424114103.451395-1-barnabas.pocze@ideasonboard.com> References: <20250424114103.451395-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Remove `closeAllFdsExcept()` from the `Process` class and have it as a local function since it is no needed "outside" and does not depend on any part of the `Process` class. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- include/libcamera/internal/process.h | 1 - src/libcamera/process.cpp | 56 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index b8eb7e043..e55d11fa3 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -45,7 +45,6 @@ public: private: LIBCAMERA_DISABLE_COPY_AND_MOVE(Process) - void closeAllFdsExcept(Span fds); int isolate(); void died(int wstatus); diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 99bc3ad36..369fdb12d 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -63,6 +63,34 @@ void sigact(int signal, siginfo_t *info, void *ucontext) } } +void closeAllFdsExcept(Span fds) +{ + std::vector v(fds.begin(), fds.end()); + sort(v.begin(), v.end()); + + ASSERT(v.empty() || v.front() >= 0); + + DIR *dir = opendir("/proc/self/fd"); + if (!dir) + return; + + int dfd = dirfd(dir); + + struct dirent *ent; + while ((ent = readdir(dir)) != nullptr) { + char *endp; + int fd = strtoul(ent->d_name, &endp, 10); + if (*endp) + continue; + + if (fd >= 0 && fd != dfd && + !std::binary_search(v.begin(), v.end(), fd)) + close(fd); + } + + closedir(dir); +} + } /* namespace */ void ProcessManager::sighandler() @@ -282,34 +310,6 @@ int Process::start(const std::string &path, } } -void Process::closeAllFdsExcept(Span fds) -{ - std::vector v(fds.begin(), fds.end()); - sort(v.begin(), v.end()); - - ASSERT(v.empty() || v.front() >= 0); - - DIR *dir = opendir("/proc/self/fd"); - if (!dir) - return; - - int dfd = dirfd(dir); - - struct dirent *ent; - while ((ent = readdir(dir)) != nullptr) { - char *endp; - int fd = strtoul(ent->d_name, &endp, 10); - if (*endp) - continue; - - if (fd >= 0 && fd != dfd && - !std::binary_search(v.begin(), v.end(), fd)) - close(fd); - } - - closedir(dir); -} - int Process::isolate() { int ret = unshare(CLONE_NEWUSER | CLONE_NEWNET);