[{"id":26687,"web_url":"https://patchwork.libcamera.org/comment/26687/","msgid":"<20230320235656.GW20234@pendragon.ideasonboard.com>","date":"2023-03-20T23:56:56","subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Elias,\n\nThank you for the patch.\n\nOn Sun, Mar 12, 2023 at 03:22:05PM -0600, Elias Naur via libcamera-devel wrote:\n> It's generally a good idea to openat(2) with O_CLOEXEC, but this patch\n> also fixes a real (corner-)case: I have an excutable that (1) uses\n> v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).\n> Without O_CLOEXEC of the kernel devices, an update while the\n> camera is opened will result in -EBUSY errors when the update tries to\n> open the camera.\n\nI agree about the change, but I wonder what returns -EBUSY, as V4L2\nallows opening device nodes multiple times. Do you get a -EBUSY error\nfrom V4L2Device::open() after exec(), or from a different location ? If\nthe error comes from V4L2Device::open(), what device does it come from ?\n\n> Signed-off-by: Elias Naur <mail@eliasnaur.com>\n> ---\n> \n> This update fixes a style issue raised through review and clarifies the\n>  motivatition for the fix.\n> \n>  src/libcamera/v4l2_device.cpp | 2 +-\n>  1 file changed, 1 insertion(+), 1 deletion(-)\n> \n> diff --git src/libcamera/v4l2_device.cpp src/libcamera/v4l2_device.cpp\n> index 57a88d96..9eb26839 100644\n> --- src/libcamera/v4l2_device.cpp\n> +++ src/libcamera/v4l2_device.cpp\n> @@ -86,7 +86,7 @@ int V4L2Device::open(unsigned int flags)\n>  \t\treturn -EBUSY;\n>  \t}\n>  \n> -\tUniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags));\n> +\tUniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags | O_CLOEXEC));\n>  \tif (!fd.isValid()) {\n>  \t\tint ret = -errno;\n>  \t\tLOG(V4L2, Error) << \"Failed to open V4L2 device '\"","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 151C2C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 20 Mar 2023 23:56:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3A170626E3;\n\tTue, 21 Mar 2023 00:56:53 +0100 (CET)","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 B5776626CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 21 Mar 2023 00:56:51 +0100 (CET)","from pendragon.ideasonboard.com (213-243-189-158.bb.dnainternet.fi\n\t[213.243.189.158])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1492C496;\n\tTue, 21 Mar 2023 00:56:50 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679356613;\n\tbh=nT+iJzeqZ4WlUd1B5j9hTsMT4SQes7A5hZ5HHDBZn6g=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=u9OSIqXyF+gkzk6GHlmcGH51rssAh/mhZKMUMpnN0MhwTA3q/7qg9Kt2Ir3fNUCjs\n\tZi8YoZoYwW4COrhDS9uNgeNAtkg8EttSBRgsZWeIjP6T2zAnDwb7ZJu70sdc29IHHP\n\tTgxsE6kuBO4lPRNM4yZhy7v8v18K969hT+bU42aVFY5AFDEnnDr4sOEDuGZ7qXkJuB\n\tfcqktKO6Iewg0CcTrfGwUMmMjok9Vxwes/Avhr3ZdIiZFg4ZVJSTiRG+9jqMMdKhYP\n\tBNdUiKaR7C6VFD4qlxgKi25IKeb72qz8TMnsWorJo57mL+WYpdoGdRZSB/Gd9QyjwO\n\t5DbZLcWBoHVKg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1679356611;\n\tbh=nT+iJzeqZ4WlUd1B5j9hTsMT4SQes7A5hZ5HHDBZn6g=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=vqOYlSyeLRG9vLk2uns+LsnWH3oShPRSejTilABV9ehWZx4B9fjqb8SX53c2Df0PA\n\tKbAmoHGpbec6Y+P1ALBYGlSdm1Mp3DTf2upBmIStT2HOn7owO4h7B1IxWTKDe6cqqW\n\tAmbea4wf5Ei78JX9iJnrpk93QCEIe7gq2fPMT7yk="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"vqOYlSye\"; dkim-atps=neutral","Date":"Tue, 21 Mar 2023 01:56:56 +0200","To":"Elias Naur <mail@eliasnaur.com>","Message-ID":"<20230320235656.GW20234@pendragon.ideasonboard.com>","References":"<20230312212205.44168-1-mail@eliasnaur.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20230312212205.44168-1-mail@eliasnaur.com>","Subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26716,"web_url":"https://patchwork.libcamera.org/comment/26716/","msgid":"<CAMAFT9Uz-kMo6BLDAfLv898NY1V7+42Ro7vbDVGD30H1jpaiRg@mail.gmail.com>","date":"2023-03-23T14:05:00","subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","submitter":{"id":159,"url":"https://patchwork.libcamera.org/api/people/159/","name":"Elias Naur","email":"mail@eliasnaur.com"},"content":"On Mon, 20 Mar 2023 at 17:56, Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n\n> On Sun, Mar 12, 2023 at 03:22:05PM -0600, Elias Naur via libcamera-devel wrote:\n> > It's generally a good idea to openat(2) with O_CLOEXEC, but this patch\n> > also fixes a real (corner-)case: I have an excutable that (1) uses\n> > v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).\n> > Without O_CLOEXEC of the kernel devices, an update while the\n> > camera is opened will result in -EBUSY errors when the update tries to\n> > open the camera.\n>\n> I agree about the change, but I wonder what returns -EBUSY, as V4L2\n> allows opening device nodes multiple times. Do you get a -EBUSY error\n> from V4L2Device::open() after exec(), or from a different location ? If\n> the error comes from V4L2Device::open(), what device does it come from ?\n>\n\nEfter exec'ing an update while the camera device is open, the next\nopen results in\n\n[91]  INFO RPI raspberrypi.cpp:1487 Registered camera\n/base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP\ndevice /dev/media1\n[89]  INFO Camera camera.cpp:1028 configuring streams: (0) 960x960-YUV420\n[91] ERROR V4L2 v4l2_videodevice.cpp:1047 /dev/video0[149:cap]: Unable\nto set format: Resource busy\n\nLet me know if you need more.\n\nElias","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 8DA8AC0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Mar 2023 14:05:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A97306270D;\n\tThu, 23 Mar 2023 15:05:15 +0100 (CET)","from mail-qk1-x731.google.com (mail-qk1-x731.google.com\n\t[IPv6:2607:f8b0:4864:20::731])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CCFF7626DB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Mar 2023 15:05:13 +0100 (CET)","by mail-qk1-x731.google.com with SMTP id de37so1308148qkb.7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Mar 2023 07:05:13 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679580315;\n\tbh=i0bzaBiQdLYdf/piqg2zlqJBkx6z/9w+jLZwhXTt8gs=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=pgyUKV0hVRBAB0j9fX6mmXLfkdnhosz8qDL9dDVAJcZ0Xw7H+Be7YY9NeIKMd+6ud\n\tnPN3EwWYjfOPvimGIcJ0rrRyaSX+yNLcm4lGLgPeUl1vFQqlzf6eFG1RexbHx1KzQz\n\t2t4OWHTFIdeF9Fh31b7uk4D9+MqRqTTrCZLq9fpP1wlK1h6ZQLdxOtLKV+uxBCdwY9\n\taP5OkaktZnN7/N48Yz6h4PlZaPdo9bKEjmYkc0YDf7j9V1ilfeIZAQtk/n5W3l6g0T\n\tARBYt8PmRqf5v2i8LbLse8XyXDasBBSXLBg9giO3Hqz1jlIDC6n7kp+ZDtVvRKiyVZ\n\t8o2bC8dtz8YRw==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=eliasnaur.com; s=google; t=1679580311;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=m8TVUdI6SZ6fCJxj10JMWU3USKc5AHL0atfOOuCUS+c=;\n\tb=aiQ8x9hm4EDLjDMj75em0wZwr19LXRmzuRX36vs/FgDemmrO6Fhg6x6r2tjpmdFapc\n\t2YXmBlF62OGTmINpaXzREPwGVB4IehrKX8A36nAYIlBsoOOwIlYpq1hcP3P0VeWluRgR\n\t9Kic/s6z+9uAYtMNxaR0dQu6bmZGIPPTWHmNe/qBfyfN6+vW65+yMZfwbPWqV1ebLCnh\n\t0gJq3x6Ukkl59lwJcRdhEBu7aFYCGooFWD8uZceKPkzrLDv3LIAsrz222uWsfqp6jmcB\n\tZzZwntOqmbIx38rP0bqob6eCYLAX5WouG7ewgAmKKPdkUhanmFoMHQFOAGn8JfNdEY7f\n\tVtQA=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=eliasnaur.com\n\theader.i=@eliasnaur.com\n\theader.b=\"aiQ8x9hm\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1679580311;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=m8TVUdI6SZ6fCJxj10JMWU3USKc5AHL0atfOOuCUS+c=;\n\tb=PSRA7mQJIhTAd8e1H77QL3rbmvT1637U5AJeQPJ8EoQhdyI0P9+xki4JvKinNgnXSn\n\t0g7Hz6NGfqccTLNSdgs/M0ZGe0MeniWxN409jC+OpI55cC49YKz1998rPiPVadnb6YnE\n\tEye7lr3aBmL7zMAZSy9YqXx+rCnyl3J46YpIegGHEFnRTbgl7HJ6FxzB6vHyFEL+3AgX\n\t8tT8aaYmowddouCAbqaDvn3rVAnr2Nqoc479Vhc3QpT0ptZ3pKIvH+7uJquQpfENmgib\n\tEQcADPl/RbdyH71mCJZ27wSfFCjhoncibljePqOCO6s8O4jCp/jmr4tsmNeGgLVOkysZ\n\tJ2Iw==","X-Gm-Message-State":"AO0yUKUP2xuiZJLSqI3S3c7Lf2j7f6yGlX9aybCloawXF4L6dbzfdsmK\n\tGx4K4NsnFji3M6mOuwUiWuBmiOeWCRXEawp3upz4SsNjOI8G7OATnhk=","X-Google-Smtp-Source":"AK7set8tbqa0RUkguHsYkfmTIA0cdV/ZLvMg9TPsbkyUJgI/B0q+FlKZlBLXrSkrOLXmOVRwB+u1cB4x6AAx7VheG3M=","X-Received":"by 2002:a05:620a:47b6:b0:742:9899:98fb with SMTP id\n\tdt54-20020a05620a47b600b00742989998fbmr1248615qkb.7.1679580311489;\n\tThu, 23 Mar 2023 07:05:11 -0700 (PDT)","MIME-Version":"1.0","References":"<20230312212205.44168-1-mail@eliasnaur.com>\n\t<20230320235656.GW20234@pendragon.ideasonboard.com>","In-Reply-To":"<20230320235656.GW20234@pendragon.ideasonboard.com>","Date":"Thu, 23 Mar 2023 08:05:00 -0600","Message-ID":"<CAMAFT9Uz-kMo6BLDAfLv898NY1V7+42Ro7vbDVGD30H1jpaiRg@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","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>","From":"Elias Naur via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Elias Naur <mail@eliasnaur.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26748,"web_url":"https://patchwork.libcamera.org/comment/26748/","msgid":"<20230326090325.GK22214@pendragon.ideasonboard.com>","date":"2023-03-26T09:03:25","subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Elias,\n\nOn Thu, Mar 23, 2023 at 08:05:00AM -0600, Elias Naur wrote:\n> On Mon, 20 Mar 2023 at 17:56, Laurent Pinchart wrote:\n> > On Sun, Mar 12, 2023 at 03:22:05PM -0600, Elias Naur via libcamera-devel wrote:\n> > > It's generally a good idea to openat(2) with O_CLOEXEC, but this patch\n> > > also fixes a real (corner-)case: I have an excutable that (1) uses\n> > > v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).\n> > > Without O_CLOEXEC of the kernel devices, an update while the\n> > > camera is opened will result in -EBUSY errors when the update tries to\n> > > open the camera.\n> >\n> > I agree about the change, but I wonder what returns -EBUSY, as V4L2\n> > allows opening device nodes multiple times. Do you get a -EBUSY error\n> > from V4L2Device::open() after exec(), or from a different location ? If\n> > the error comes from V4L2Device::open(), what device does it come from ?\n> \n> Efter exec'ing an update while the camera device is open, the next\n> open results in\n> \n> [91]  INFO RPI raspberrypi.cpp:1487 Registered camera\n> /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP\n> device /dev/media1\n> [89]  INFO Camera camera.cpp:1028 configuring streams: (0) 960x960-YUV420\n> [91] ERROR V4L2 v4l2_videodevice.cpp:1047 /dev/video0[149:cap]: Unable\n> to set format: Resource busy\n\nAh ok, the error comes from setFormat(), not open(). That makes sense.\nThanks, that's the information I was looking for. I'll update the commit\nmessage to record this information, as follows:\n\nWhen an executable using libcamera calls exec(3) while a camera is in\nuse, file descriptors corresponding to the V4L2 video devices are kept\nopen has they have been created without O_CLOEXEC. This results in the\nvideo devices staying busy, preventing the new executable from using\nthem:\n\n[91] ERROR V4L2 v4l2_videodevice.cpp:1047 /dev/video0[149:cap]: Unableto set format: Resource busy\n\nFix this by opening video devices with O_CLOEXEC, which is generally a\ngood idea in libraries.\n\n> > > It's generally a good idea to openat(2) with O_CLOEXEC, but this patch\n> > > also fixes a real (corner-)case: I have an excutable that (1) uses\n> > > v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).\n> > > Without O_CLOEXEC of the kernel devices, an update while the\n> > > camera is opened will result in -EBUSY errors when the update tries to\n> > > open the camera.\n\n> Let me know if you need more.","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 8B0D7BD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 26 Mar 2023 09:03:21 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E1D6C62720;\n\tSun, 26 Mar 2023 11:03:20 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6DD5561ECD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 26 Mar 2023 11:03:19 +0200 (CEST)","from pendragon.ideasonboard.com (213-243-189-158.bb.dnainternet.fi\n\t[213.243.189.158])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B9AC4741;\n\tSun, 26 Mar 2023 11:03:18 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679821400;\n\tbh=INiGBkUiW59YZ2Q+oJOMyCRqbMD56xZIXOozAcE6BZQ=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=zk2CgPt3srle1XQLgH81TU8v/VE1s4zu8BteoazDV7voBa+47gTHFKe4vhg+b4ro1\n\tCzRjrdz5qoj5lWSLzHeng73jBLc07t4m+Qza0hIPWL8iQMy8JrG8VLMDn2WmwPrTss\n\t2zsyUmBRkfeODtcTWEIojqoknp1GYPPOC8tY+ktE2SdV6N8Dy/V8peUAKA5u/v1e4L\n\tkSi9XBzdpNbnb/FelrT2uTBn1Ljxfy5QRjLeOnnP5Qgd5dOOAEhGNJ5RPTrEYW17g5\n\t2B14S62k0x5VV9qfSr3TaXGRuOVvayHoUNb1ha/TDjwntq0R/NNicOhljETas+Go2K\n\tMokcS8iJWrb3g==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1679821399;\n\tbh=INiGBkUiW59YZ2Q+oJOMyCRqbMD56xZIXOozAcE6BZQ=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ibYfCPik/8C9+ptWkxYKGXL7OmRNWzCSYRs6PRZ35Y7MGsyKhMKnglLk7AYtiYcS0\n\tDUhC0cPoOFs/T8yxeKlFZzx90fT9DZp+pKSiAER5Qbop4K87cadd+3YiDUlJUJnJSZ\n\tigv+m5SkJ28d0H64bmq7LhKnyURNxCbd9AhJA/sE="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ibYfCPik\"; dkim-atps=neutral","Date":"Sun, 26 Mar 2023 12:03:25 +0300","To":"Elias Naur <mail@eliasnaur.com>","Message-ID":"<20230326090325.GK22214@pendragon.ideasonboard.com>","References":"<20230312212205.44168-1-mail@eliasnaur.com>\n\t<20230320235656.GW20234@pendragon.ideasonboard.com>\n\t<CAMAFT9Uz-kMo6BLDAfLv898NY1V7+42Ro7vbDVGD30H1jpaiRg@mail.gmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<CAMAFT9Uz-kMo6BLDAfLv898NY1V7+42Ro7vbDVGD30H1jpaiRg@mail.gmail.com>","Subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26749,"web_url":"https://patchwork.libcamera.org/comment/26749/","msgid":"<20230326093658.GL22214@pendragon.ideasonboard.com>","date":"2023-03-26T09:36:58","subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Elias,\n\nOne more comment, I'd highly recommend stopping the cameras and camera\nmanager before calling exec(), as there may be more side effects due to\nobjects not being destroyed.\n\nOn Sun, Mar 26, 2023 at 12:03:25PM +0300, Laurent Pinchart via libcamera-devel wrote:\n> On Thu, Mar 23, 2023 at 08:05:00AM -0600, Elias Naur wrote:\n> > On Mon, 20 Mar 2023 at 17:56, Laurent Pinchart wrote:\n> > > On Sun, Mar 12, 2023 at 03:22:05PM -0600, Elias Naur via libcamera-devel wrote:\n> > > > It's generally a good idea to openat(2) with O_CLOEXEC, but this patch\n> > > > also fixes a real (corner-)case: I have an excutable that (1) uses\n> > > > v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).\n> > > > Without O_CLOEXEC of the kernel devices, an update while the\n> > > > camera is opened will result in -EBUSY errors when the update tries to\n> > > > open the camera.\n> > >\n> > > I agree about the change, but I wonder what returns -EBUSY, as V4L2\n> > > allows opening device nodes multiple times. Do you get a -EBUSY error\n> > > from V4L2Device::open() after exec(), or from a different location ? If\n> > > the error comes from V4L2Device::open(), what device does it come from ?\n> > \n> > Efter exec'ing an update while the camera device is open, the next\n> > open results in\n> > \n> > [91]  INFO RPI raspberrypi.cpp:1487 Registered camera\n> > /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP\n> > device /dev/media1\n> > [89]  INFO Camera camera.cpp:1028 configuring streams: (0) 960x960-YUV420\n> > [91] ERROR V4L2 v4l2_videodevice.cpp:1047 /dev/video0[149:cap]: Unable\n> > to set format: Resource busy\n> \n> Ah ok, the error comes from setFormat(), not open(). That makes sense.\n> Thanks, that's the information I was looking for. I'll update the commit\n> message to record this information, as follows:\n> \n> When an executable using libcamera calls exec(3) while a camera is in\n> use, file descriptors corresponding to the V4L2 video devices are kept\n> open has they have been created without O_CLOEXEC. This results in the\n> video devices staying busy, preventing the new executable from using\n> them:\n> \n> [91] ERROR V4L2 v4l2_videodevice.cpp:1047 /dev/video0[149:cap]: Unableto set format: Resource busy\n> \n> Fix this by opening video devices with O_CLOEXEC, which is generally a\n> good idea in libraries.\n> \n> > > > It's generally a good idea to openat(2) with O_CLOEXEC, but this patch\n> > > > also fixes a real (corner-)case: I have an excutable that (1) uses\n> > > > v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).\n> > > > Without O_CLOEXEC of the kernel devices, an update while the\n> > > > camera is opened will result in -EBUSY errors when the update tries to\n> > > > open the camera.\n> \n> > Let me know if you need more.","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 8367EC0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 26 Mar 2023 09:36:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D1FCD62720;\n\tSun, 26 Mar 2023 11:36:53 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0361B61ECD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 26 Mar 2023 11:36:51 +0200 (CEST)","from pendragon.ideasonboard.com (213-243-189-158.bb.dnainternet.fi\n\t[213.243.189.158])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7B9FD741;\n\tSun, 26 Mar 2023 11:36:51 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679823413;\n\tbh=ysTdxRXc4zetKMHKwbLflto1oVLfqo4O+faMW4OIkCk=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=aMT1gw+m/f86YsND8UYqhkWhrR1p+ib7NMhOdbEp+wXA4rXmwYL8n+fHh7ruS20fE\n\teYV1yWXnbtUqSTB2vtTaEJYrroXCxMOWC5DmnlTR/masRytFgTEfn3eTeVk8BlxP6O\n\tnvGLU23/hOtoo0m2xvWxUU54UwRa94f1VuvlztF2yDKw/rI1zHTEX/AQ3Rj2ZZrNzv\n\t2pLF5QOrwInoki/MYiXQI0YhPpUdQxnpMdQFveprlNYBPbbD3gaouPXvuetY4RQd7k\n\t/lFIvRQ0ikBKeET8ogStLlXJDSNJHFJh4kneqt31KN+ErUomEmfj1lGHM4JZsmuRLr\n\tYKae0rf2hjSxA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1679823411;\n\tbh=ysTdxRXc4zetKMHKwbLflto1oVLfqo4O+faMW4OIkCk=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=hW12rc0kqpsNdeaTxm86IkSebiJ1Y8XVtDsyDxZx8LE3CFT3c3JyLYE/8cprk56R/\n\tyTsB+lIdnq2VfH8VXeqcipVKLZwzWOwVmYsxmufkW7XjhLC0Lid0310hfVWUU4uwSa\n\tH9eRjBCL7KEPl90bVGGhVnWnQS5MqEtFBx7/94xA="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"hW12rc0k\"; dkim-atps=neutral","Date":"Sun, 26 Mar 2023 12:36:58 +0300","To":"Elias Naur <mail@eliasnaur.com>","Message-ID":"<20230326093658.GL22214@pendragon.ideasonboard.com>","References":"<20230312212205.44168-1-mail@eliasnaur.com>\n\t<20230320235656.GW20234@pendragon.ideasonboard.com>\n\t<CAMAFT9Uz-kMo6BLDAfLv898NY1V7+42Ro7vbDVGD30H1jpaiRg@mail.gmail.com>\n\t<20230326090325.GK22214@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20230326090325.GK22214@pendragon.ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2)\n\twith O_CLOEXEC to cleanup after exec(3)","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]