From patchwork Thu Apr 24 11:22:00 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: 23249 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 003FFC3322 for ; Thu, 24 Apr 2025 11:22:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3DB7A68AD1; Thu, 24 Apr 2025 13:22:21 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="I9xSrmvo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D162168AD5 for ; Thu, 24 Apr 2025 13:22:08 +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 D59BE16A for ; Thu, 24 Apr 2025 13:22:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1745493726; bh=X2o2EEnbixf3rrcrZfDkTDGdmSlEUbfzYv236RR2Je0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=I9xSrmvon3Q0Vqm89bsWvuNGJw8HPjQUsbKobazSJCDlxk8oRGil6kt+OVI4JvGsG k0iZTeyNdxdoTFUpmzQ3yOQ8VqOTQ8Mo9EqfKbzYP7fczFiPawDKU07mDESi3kWubu kBni70pejWlgm9geUsGHmA77AfVEpjmb36l7pZnY= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v4 7/9] libcamera: process: Move `closeAllFdsExcept()` Date: Thu, 24 Apr 2025 13:22:00 +0200 Message-ID: <20250424112203.445351-8-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250424112203.445351-1-barnabas.pocze@ideasonboard.com> References: <20250424112203.445351-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 --- 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);