From patchwork Tue Mar 25 18:08:19 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: 23022 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9635EC3213 for ; Tue, 25 Mar 2025 19:21:14 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 508F9353 for ; Tue, 25 Mar 2025 20:19:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742930367; bh=+j2mYamSF9tXXw08/WL/JPliWJ6Adi1nuMH7jWWk/CQ=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Resent-From:Resent-To:From; b=X+SFezx+Hc80w+WNZU/T4mNS1KmJR1ow1o+kPpFxWgb5eh2SPvxYrg2I5o/O/RVpf Iflm7O+tnShF60RreyYfG+CJWXpgrKO7VXdQQgapmZMTbjEDKYOpkOfppvgTJw8cbs oTLQhAJxIwgjV/+r7ZVR46TrXZlm6FquGWkSF/Yc= Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id QN/8JNPw4mcIui8A4E0KoQ (envelope-from ) for ; Tue, 25 Mar 2025 19:07:15 +0100 Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by perceval.ideasonboard.com (Postfix) with ESMTPS id 736B4353; Tue, 25 Mar 2025 19:07:15 +0100 (CET) Authentication-Results: perceval.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.a=rsa-sha256 header.s=mail header.b=coBrJMS2; dkim-atps=neutral Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F3C826895D; Tue, 25 Mar 2025 19:09:00 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="coBrJMS2"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A390468952 for ; Tue, 25 Mar 2025 19:08:47 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 11223353 for ; Tue, 25 Mar 2025 19:06:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926020; bh=hkTcXpSyWx7kGcQfaYLCqweQCjn31N2EjC6G98vUfGM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=coBrJMS29P3vVByeAzCOQCjifZ3KPsClA0eUbEZhLV28qyHX2JXUODyLdKkZX+3w0 uXdSOPicCzFYl0hhW25Ut2ksQHBmBSaAp8N3OGJhFfas2E1/6scCxkvcNzf0XBXV5n SNjnWv79wh+liHwisrn6HAGpBsVjm0rSd6X8xlWM= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 7/9] libcamera: process: Move `closeAllFdsExcept()` Date: Tue, 25 Mar 2025 19:08:19 +0100 Message-ID: <20250325180821.1456154-8-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-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" X-TUID: LfNPeoJK49Vh Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org 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 e4f5bb979..14391d60a 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -44,7 +44,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 5603dc903..01503e485 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()); + std::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);