[{"id":34055,"web_url":"https://patchwork.libcamera.org/comment/34055/","msgid":"<174567303717.1742020.2569312723132398529@ping.linuxembedded.co.uk>","date":"2025-04-26T13:10:37","subject":"Re: [RFC PATCH v5 7/9] libcamera: process: Move\n\t`closeAllFdsExcept()`","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Barnabás Pőcze (2025-04-24 12:41:01)\n> Remove `closeAllFdsExcept()` from the `Process` class and have it as a\n> local function since it is no needed \"outside\" and does not depend on\n\ns/it is no/it is not/\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> any part of the `Process` class.\n> \n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  include/libcamera/internal/process.h |  1 -\n>  src/libcamera/process.cpp            | 56 ++++++++++++++--------------\n>  2 files changed, 28 insertions(+), 29 deletions(-)\n> \n> diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h\n> index b8eb7e043..e55d11fa3 100644\n> --- a/include/libcamera/internal/process.h\n> +++ b/include/libcamera/internal/process.h\n> @@ -45,7 +45,6 @@ public:\n>  private:\n>         LIBCAMERA_DISABLE_COPY_AND_MOVE(Process)\n>  \n> -       void closeAllFdsExcept(Span<const int> fds);\n>         int isolate();\n>         void died(int wstatus);\n>  \n> diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\n> index 99bc3ad36..369fdb12d 100644\n> --- a/src/libcamera/process.cpp\n> +++ b/src/libcamera/process.cpp\n> @@ -63,6 +63,34 @@ void sigact(int signal, siginfo_t *info, void *ucontext)\n>         }\n>  }\n>  \n> +void closeAllFdsExcept(Span<const int> fds)\n> +{\n> +       std::vector<int> v(fds.begin(), fds.end());\n> +       sort(v.begin(), v.end());\n> +\n> +       ASSERT(v.empty() || v.front() >= 0);\n> +\n> +       DIR *dir = opendir(\"/proc/self/fd\");\n> +       if (!dir)\n> +               return;\n> +\n> +       int dfd = dirfd(dir);\n> +\n> +       struct dirent *ent;\n> +       while ((ent = readdir(dir)) != nullptr) {\n> +               char *endp;\n> +               int fd = strtoul(ent->d_name, &endp, 10);\n> +               if (*endp)\n> +                       continue;\n> +\n> +               if (fd >= 0 && fd != dfd &&\n> +                   !std::binary_search(v.begin(), v.end(), fd))\n> +                       close(fd);\n> +       }\n> +\n> +       closedir(dir);\n> +}\n> +\n>  } /* namespace */\n>  \n>  void ProcessManager::sighandler()\n> @@ -282,34 +310,6 @@ int Process::start(const std::string &path,\n>         }\n>  }\n>  \n> -void Process::closeAllFdsExcept(Span<const int> fds)\n> -{\n> -       std::vector<int> v(fds.begin(), fds.end());\n> -       sort(v.begin(), v.end());\n> -\n> -       ASSERT(v.empty() || v.front() >= 0);\n> -\n> -       DIR *dir = opendir(\"/proc/self/fd\");\n> -       if (!dir)\n> -               return;\n> -\n> -       int dfd = dirfd(dir);\n> -\n> -       struct dirent *ent;\n> -       while ((ent = readdir(dir)) != nullptr) {\n> -               char *endp;\n> -               int fd = strtoul(ent->d_name, &endp, 10);\n> -               if (*endp)\n> -                       continue;\n> -\n> -               if (fd >= 0 && fd != dfd &&\n> -                   !std::binary_search(v.begin(), v.end(), fd))\n> -                       close(fd);\n> -       }\n> -\n> -       closedir(dir);\n> -}\n> -\n>  int Process::isolate()\n>  {\n>         int ret = unshare(CLONE_NEWUSER | CLONE_NEWNET);\n> -- \n> 2.49.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id DE93CBE08B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 26 Apr 2025 13:10:43 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EE89868ACD;\n\tSat, 26 Apr 2025 15:10:42 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7041B617E3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 26 Apr 2025 15:10:40 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A4DCB735;\n\tSat, 26 Apr 2025 15:10:36 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"BboxgaDn\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1745673036;\n\tbh=MarvF0nO9l8Uo9tb1sif9rpQWUn20g3zarxgv95GY4U=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=BboxgaDnARxe/HsNQn/4hV1HzMPFOCEOCd8D1RyaRCvrMdpFPVzpAGux3YFp+cr3M\n\teh9rAPvVN31yNsWYkiKZlRAZEAtYfpiPiwo3HuSqn6r2uMkPxqbCuS6VQwTD1i2YTd\n\tK0clz9u0qXWlmouri7queMzVdItZ4oofJOwoVdYc=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250424114103.451395-8-barnabas.pocze@ideasonboard.com>","References":"<20250424114103.451395-1-barnabas.pocze@ideasonboard.com>\n\t<20250424114103.451395-8-barnabas.pocze@ideasonboard.com>","Subject":"Re: [RFC PATCH v5 7/9] libcamera: process: Move\n\t`closeAllFdsExcept()`","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Sat, 26 Apr 2025 14:10:37 +0100","Message-ID":"<174567303717.1742020.2569312723132398529@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]