[{"id":34365,"web_url":"https://patchwork.libcamera.org/comment/34365/","msgid":"<174836196377.896144.16856988534055084589@ping.linuxembedded.co.uk>","date":"2025-05-27T16:06:03","subject":"Re: [PATCH v4 2/2] libcamera: process: Pass stderr and reserve stdin\n\tand stdout fds","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Julien Vuillaumier (2025-05-27 16:56:15)\n> When a child process is started from Process::start(), the file\n> descriptors inherited from the parent process are closed, except\n> the ones explicitly listed in the fds[] argument.\n> \n> One issue is that the file descriptors for stdin, stdout and stderr\n> being closed, the subsequent file descriptors created by the child\n> process will reuse the values 0, 1 and 2 that are now available.\n> Thus, usage of printf(), assert() or alike may direct its output\n> to the new resource bound to one of these reused file descriptors.\n> The other issue is that the child process can no longer log on\n> the console because stderr has been closed.\n> \n> To address the 2 issues, Process:start() is amended as below:\n> - Child process inherits from parent's stderr fd in order to share\n> the same logging descriptor\n> - Child process stdin, stdout and stderr fds are bound to /dev/null\n> if not inherited from parent. That is to prevent those descriptors\n> to be reused for any other resource, that could be corrupted by\n> the presence of printf(), assert() or alike.\n> \n> Signed-off-by: Julien Vuillaumier <julien.vuillaumier@nxp.com>\n> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\nI'm looking forward to this one being in, especially if it helps report\nissues from isolated IPA modules, which have caused me support pain!\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n>  src/libcamera/process.cpp | 16 +++++++++++++++-\n>  1 file changed, 15 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\n> index 7f3a6518..d836fb07 100644\n> --- a/src/libcamera/process.cpp\n> +++ b/src/libcamera/process.cpp\n> @@ -259,7 +259,21 @@ int Process::start(const std::string &path,\n>                 if (isolate())\n>                         _exit(EXIT_FAILURE);\n>  \n> -               closeAllFdsExcept(fds);\n> +               std::vector<int> v(fds);\n> +               v.push_back(STDERR_FILENO);\n> +               closeAllFdsExcept(v);\n> +\n> +               const auto tryDevNullLowestFd = [](int expected, int oflag) {\n> +                       int fd = open(\"/dev/null\", oflag);\n> +                       if (fd < 0)\n> +                               _exit(EXIT_FAILURE);\n> +                       if (fd != expected)\n> +                               close(fd);\n> +               };\n> +\n> +               tryDevNullLowestFd(STDIN_FILENO, O_RDONLY);\n> +               tryDevNullLowestFd(STDOUT_FILENO, O_WRONLY);\n> +               tryDevNullLowestFd(STDERR_FILENO, O_WRONLY);\n>  \n>                 const char *file = utils::secure_getenv(\"LIBCAMERA_LOG_FILE\");\n>                 if (file && strcmp(file, \"syslog\"))\n> -- \n> 2.34.1\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 DDE8CC3237\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 May 2025 16:06:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E3AA668DA1;\n\tTue, 27 May 2025 18:06:07 +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 2083868D94\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 May 2025 18:06:07 +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 5BF1F725;\n\tTue, 27 May 2025 18:05:41 +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=\"sfa6ehdK\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1748361941;\n\tbh=kUB3ImnR8RK33zpF58qrXWcdNpsCjSPXssd9wMZekD4=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=sfa6ehdKc4gPhu1e4DJNsJE7POFEWEUGDEIV89LsRmuO2TzPEuKMAYYf+BOM3vli7\n\tVPJTtITKk1Mi38WWT1CaGk6tUVJlE3SMIrUOwKSJs7mjaa4Ul1kf0SiMgueYcGG8o+\n\tAU8BiIsLS0OypV4yYSEQZ5LXnSk+0ZUYE29+d660=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250527155615.560463-3-julien.vuillaumier@nxp.com>","References":"<20250527155615.560463-1-julien.vuillaumier@nxp.com>\n\t<20250527155615.560463-3-julien.vuillaumier@nxp.com>","Subject":"Re: [PATCH v4 2/2] libcamera: process: Pass stderr and reserve stdin\n\tand stdout fds","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Julien Vuillaumier <julien.vuillaumier@nxp.com>","To":"Julien Vuillaumier <julien.vuillaumier@nxp.com>,\n\tbarnabas.pocze@ideasonboard.com, libcamera-devel@lists.libcamera.org, \n\tpaul.elder@ideasonboard.com","Date":"Tue, 27 May 2025 17:06:03 +0100","Message-ID":"<174836196377.896144.16856988534055084589@ping.linuxembedded.co.uk>","User-Agent":"alot/0.9.1","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>"}}]