From patchwork Wed Dec 18 18:27:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Vuillaumier X-Patchwork-Id: 22403 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 68C5CC32FE for ; Wed, 18 Dec 2024 18:27:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EECB8680B6; Wed, 18 Dec 2024 19:27:55 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=nxp.com header.i=@nxp.com header.b="WIRW2fJV"; dkim-atps=neutral Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2060b.outbound.protection.outlook.com [IPv6:2a01:111:f403:2612::60b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 16D36680B2 for ; Wed, 18 Dec 2024 19:27:53 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IggNntoHIAAmai3zpNjJM+ZhcPMMv6G6XvyAuI9DEsJCAhDnwB5KHYqDhMmEvGCbXc92kP9628dTN5CVLF2zZRhIoZ+TyXE4YYU2SwtKQWm2XDmhVdxACL7nMAwEJjTPRL67aeLYPRoIAxK5nV7ogtihA9+x5Br/voEyXIPZTELRzgWInpL9dCaMbREGY0AQ6tX1yWZXmarMLzNN4OtyWowAMGrrhxYhS/A5E7JZDuZjUtPM+yqtYcB403f0Ztc/S207jh82Dkpqcbfy+jl9H6RHgq1gIAwuOYXgSr31xU4mO9hgBDyZVL2m3ut6HELnDEgQVP+xpBwcf9hvG6AJ4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wTsBvZOuKgIPnmCC4MVWn7uS3EjquLR2sbMhacLX6Ls=; b=EBdojvJMOLcq6vavJ1vMklzXpXr+c2vV6aXfXLnEpVhw+YedzkSZbUAqAWFkH78qI3akMuhrpWaI9yYtSViHwzMHgIRKlJYYRKpYvHn9NC+rVPKkCCDEAMbci6nJr/wqIrmmU/t8+dPPz3TXFaZO0qitPE4+8HWr1fXoKLwVftDy5++t60wPBX/zsi/7ugiwXOXc2LY9yp5GNgcXltVQTC+WoHwl26gUeo+OKRs8HNURLIefHDVk9/Yq/OJAnUuumf6wakS+55dfpVeZxGI027H1B9ir3hE1s2GZBWLvws/tc9yvnZkXc0ICrUXfMySJ4ut0k1240+VbGLc/pbLJEQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wTsBvZOuKgIPnmCC4MVWn7uS3EjquLR2sbMhacLX6Ls=; b=WIRW2fJVNCAczYw9F8hdnzkc1D4Y0eZxWzpZcfNcXb6MSBd97USErnSio2cVgJ2JFzVW1F2uDZFmP6og2zR2obR1aehvDLfO5davTnQx3DJyWU/9C8hpK7F9hqdfW/4+EAq+wZkpSDpoiuhMjQ5cszNl74Oj3geU9Z4ioJGf/oTLHNukaoxTm/T3r43RfEkYjey7hrFMxDHwKJeIv3fX6A0BFMpsVVhgSUXHITSNA26qCGD4cLbIY5+q7X53WEmVkYgxashfYgWW6DJ5T2Yd5CyDOgponu24AjgbZ/AZKJt6CquOe25hGo4cTyBwg/Lg7lstrmWvN73u6+0h5303tA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from AM9PR04MB8147.eurprd04.prod.outlook.com (2603:10a6:20b:3e0::22) by DBBPR04MB7801.eurprd04.prod.outlook.com (2603:10a6:10:1eb::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8272.13; Wed, 18 Dec 2024 18:27:51 +0000 Received: from AM9PR04MB8147.eurprd04.prod.outlook.com ([fe80::eace:e980:28a4:ef8a]) by AM9PR04MB8147.eurprd04.prod.outlook.com ([fe80::eace:e980:28a4:ef8a%5]) with mapi id 15.20.8251.015; Wed, 18 Dec 2024 18:27:51 +0000 From: Julien Vuillaumier To: julien.vuillaumier@nxp.com, libcamera-devel@lists.libcamera.org, laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com, paul.elder@ideasonboard.com Subject: [PATCH v2 1/1] libcamera: process: Pass stderr and reserve stdin and stdout fds Date: Wed, 18 Dec 2024 19:27:54 +0100 Message-Id: <20241218182754.2414920-2-julien.vuillaumier@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241218182754.2414920-1-julien.vuillaumier@nxp.com> References: <20241218182754.2414920-1-julien.vuillaumier@nxp.com> X-ClientProxiedBy: AM4PR0302CA0028.eurprd03.prod.outlook.com (2603:10a6:205:2::41) To AM9PR04MB8147.eurprd04.prod.outlook.com (2603:10a6:20b:3e0::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM9PR04MB8147:EE_|DBBPR04MB7801:EE_ X-MS-Office365-Filtering-Correlation-Id: 760f03b9-54d9-4bcc-c0c2-08dd1f91aea5 X-LD-Processed: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|52116014|1800799024|376014|366016|38350700014; X-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nSbXptsf1hIgYMmJbbS9Wgd/Qjm7C37pUp7A7NVt9KgFecqorkB64q0PxVQ8A0qKwbW4HvEom9uaIL708h/uaSVPWcDnnPx7Zmsp0nJh4FyaULO33G0oMAlbUfwl0J+JlsCr2RijJecKlDO/ThyY3/BF6pX6VQ9ZDiqrnYfL4B5HznZhcKDxlsauDJSWGfUzaJxIUpcE4Foa3JD+XNWvxbWN8hfi6SAD0YjoZO3pse5s1YaIZ2c6sE3Rp3ft2tLAos+s4SFP8ArHRoNGQTVHE4QLbyR9I9FecCbODrFZYPEJarFPCrJcM0oBKrC7k0eReMDt6CLuBcB51+RTa9/5wcGTgsp8lgvZDLlmFyqjpS7BD7RPxYJKSebx5V/b5F+uHUpwTiuDobiB28vgKNG X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM9PR04MB8147.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(52116014)(1800799024)(376014)(366016)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wPbhkObH7vYVTmIPSLEB+HKQSS/672zu1C+uZc4vVZPPifnt3hntSXVwNBQPJxFsUjeEtKHT8dDsyCvvW45F0g6LFJ4VPrLh72nSwlWADnamgfKZLu97O7HAjkxaEd/AdT3qpKXxHI1qNKMXPJP2zU2eFUuWATDQnqWKga3T4YFWUzaPhlx1AG0YjEHRuHuP/Keg8xHkjBMzYHmLsI3I9taHftL0EvtHWzKdBl0R0cB6QHd6h7B/DBjVMNoOn7F6pqiXWWBWdEncUfy1U1N3Pk1Z1UcqwGx1JPq7+3xaWk3I1e7WhIuyBc35HBeaY6bNolxrw8TW/hwRFhKt3oWSUPboAzHiRDDynJ+RsE4sPfGjHxJIkiXQqDeOuw+SaugNUHCavZhihNbnU/vs7hXedX0AUupCcN1FvWMlh3/pppNnYg7YHY8vOVbHeMz4L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Irxl423q/V4xEQjVJX1q2s8xkYqqi8NHH7ukdXDfXYmEYF/uenyYZWIwgKNBk6Aj+SThmFNGDAOqIpOvWi7bTFu23u23hamnYUA== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 760f03b9-54d9-4bcc-c0c2-08dd1f91aea5 X-MS-Exchange-CrossTenant-AuthSource: AM9PR04MB8147.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Dec 2024 18:27:51.8752 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DenrqjQ9xseZVkLnTyuWv7wc/M7l8748U4srAy3QFYQlUVFJCbkWTisDjf1nwsiHroSPlUkU6H5XtyrsYvJY1SKE6dIaUf0b+DUnu/c2rck= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB7801 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" When a child process is started from Process::start(), the file descriptors inherited from the parent process are closed, except the ones explicitly listed in the fds[] argument. One issue is that the file descriptors for stdin, stdout and stderr being closed, the subsequent file descriptors created by the child process will reuse the values 0, 1 and 2 that are now available. Thus usage of printf(), assert() or alike may direct its output to the new resource bound to one of these reused file descriptors. The other issue is that the child process can no longer log on the console because stderr has been closed. To address the 2 issues, Process:start() is amended as below: - Child process stdin and stdout are redirected to /dev/null so that file descriptors 0 and 1 are not reused for any other usage, that could be corrupted by the presence of printf(), assert() or alike. - Child process inherits from parent's stderr in order to share the same logging descriptor. Signed-off-by: Julien Vuillaumier --- src/libcamera/process.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index bc9833f4..86a887fb 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -259,7 +260,15 @@ int Process::start(const std::string &path, if (isolate()) _exit(EXIT_FAILURE); - closeAllFdsExcept(fds); + std::vector v(fds); + v.push_back(STDERR_FILENO); + closeAllFdsExcept(v); + + UniqueFD fd(::open("/dev/null", O_RDWR)); + if (fd.isValid()) { + dup2(fd.get(), STDIN_FILENO); + dup2(fd.get(), STDOUT_FILENO); + } const char *file = utils::secure_getenv("LIBCAMERA_LOG_FILE"); if (file && strcmp(file, "syslog"))