From patchwork Sun Mar 26 10:01:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 18463 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 DF619BD160 for ; Sun, 26 Mar 2023 10:01:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2CE1F626E2; Sun, 26 Mar 2023 12:01:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1679824912; bh=wWT3AxhRArawgpvSdgRo4L7ABYeN0sOitNgPnWxb+8g=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ssf9U2zhnUqb8CvoWUkwyP/+f3cnAX27nU6m6Ko20bNYiCQyXEW5A3HbJH6G1Hq/D kGGXIcOc2sYHwhbESOYeILh57xAewPeYPZN5YDjA2P5ut2PhDU01Neh/8iBRTADyUR PfVVHOtrQ/BJpq0WJ4cT1Nzfm17felFUf3MHhFI4rXMa7iAJpscz80wdw7K2q8cXJa zaMK+lGWiL0Qeensog9ymIOD9dxte6MJT+pIeXdrephgN8b1qQ4+Y0rCOp03xqQ8GO fqR9j8eo7yhBNKY9BK8aqlU/nbQHXHJzICJ3L2oXgre9qe8NO+j2CNHy0MC5rHyXyT fIrivvBC5dojA== 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 06670626DA for ; Sun, 26 Mar 2023 12:01:50 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="nnMBwdhR"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (213-243-189-158.bb.dnainternet.fi [213.243.189.158]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4F1C7741 for ; Sun, 26 Mar 2023 12:01:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1679824909; bh=wWT3AxhRArawgpvSdgRo4L7ABYeN0sOitNgPnWxb+8g=; h=From:To:Subject:Date:From; b=nnMBwdhR6xF1jj3RoLDQZxBS8g7yhaawoBrlnjJ51FMt7trqOCe4Pt3RurBMFByeL zLbDuDD7d8u/c8anKulzKQzlHcZHsaEZiy/rlk5DkpRu5pRxIt494Jtlv9Hip7tP62 kn23rBBkAlGXrKzokWWWQXTdaFyNkrm/a/9LI40Q= To: libcamera-devel@lists.libcamera.org Date: Sun, 26 Mar 2023 13:01:55 +0300 Message-Id: <20230326100155.7532-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: Open files with O_CLOEXEC 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Files opened internally in libcamera without the O_CLOEXEC file will remain open upon a call to one of the exec(3) functions. As exec() doesn't destroy local or global objects, this can lead to various side effects. Avoid this by opening file descriptors with O_CLOEXEC for all internal files. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Kieran Bingham --- src/ipa/vimc/vimc.cpp | 2 +- src/libcamera/base/file.cpp | 5 ++++- src/libcamera/media_device.cpp | 2 +- src/libcamera/pipeline/raspberrypi/dma_heaps.cpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ipa/vimc/vimc.cpp b/src/ipa/vimc/vimc.cpp index d5fabaf13ec6..2c255778990a 100644 --- a/src/ipa/vimc/vimc.cpp +++ b/src/ipa/vimc/vimc.cpp @@ -168,7 +168,7 @@ void IPAVimc::initTrace() if (ret) return; - ret = ::open(ipa::vimc::VimcIPAFIFOPath.c_str(), O_WRONLY); + ret = ::open(ipa::vimc::VimcIPAFIFOPath.c_str(), O_WRONLY | O_CLOEXEC); if (ret < 0) { ret = errno; LOG(IPAVimc, Error) << "Failed to open vimc IPA test FIFO: " diff --git a/src/libcamera/base/file.cpp b/src/libcamera/base/file.cpp index fb3e276d6ccd..d1ab1aa57462 100644 --- a/src/libcamera/base/file.cpp +++ b/src/libcamera/base/file.cpp @@ -163,6 +163,9 @@ bool File::exists() const * attempt to create the file with initial permissions set to 0666 (modified by * the process' umask). * + * The file is opened with the O_CLOEXEC flag, and will be closed automatically + * when a new binary is executed with one of the exec(3) functions. + * * The error() status is updated. * * \return True on success, false otherwise @@ -178,7 +181,7 @@ bool File::open(File::OpenMode mode) if (mode & OpenModeFlag::WriteOnly) flags |= O_CREAT; - fd_ = UniqueFD(::open(name_.c_str(), flags, 0666)); + fd_ = UniqueFD(::open(name_.c_str(), flags | O_CLOEXEC, 0666)); if (!fd_.isValid()) { error_ = -errno; return false; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 52c8e66e9e99..2949816b4a64 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -477,7 +477,7 @@ int MediaDevice::open() return -EBUSY; } - fd_ = UniqueFD(::open(deviceNode_.c_str(), O_RDWR)); + fd_ = UniqueFD(::open(deviceNode_.c_str(), O_RDWR | O_CLOEXEC)); if (!fd_.isValid()) { int ret = -errno; LOG(MediaDevice, Error) diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp index 6b644406c90f..317b1fc17161 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp @@ -37,7 +37,7 @@ namespace RPi { DmaHeap::DmaHeap() { for (const char *name : heapNames) { - int ret = ::open(name, O_RDWR, 0); + int ret = ::open(name, O_RDWR | O_CLOEXEC, 0); if (ret < 0) { ret = errno; LOG(RPI, Debug) << "Failed to open " << name << ": "