[{"id":17409,"web_url":"https://patchwork.libcamera.org/comment/17409/","msgid":"<YL0OQcuHeabRoBW8@pendragon.ideasonboard.com>","date":"2021-06-06T18:04:49","subject":"Re: [libcamera-devel] [RFC PATCH 03/10] libcamera: File: Manage fd\n\tby ScopedFD","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Thu, Apr 15, 2021 at 05:38:36PM +0900, Hirokazu Honda wrote:\n> File owns the file descriptor for a file. It should be managed\n> by ScopedFD to avoid the leakage.\n\nIs there any leak in the current implementation ? If not, I'd reword the\ncommit message.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  include/libcamera/internal/file.h |  5 +++--\n>  src/libcamera/file.cpp            | 26 +++++++++++++-------------\n>  2 files changed, 16 insertions(+), 15 deletions(-)\n> \n> diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h\n> index f0b313a5..e6eb7f04 100644\n> --- a/include/libcamera/internal/file.h\n> +++ b/include/libcamera/internal/file.h\n> @@ -12,6 +12,7 @@\n>  #include <sys/types.h>\n>  \n>  #include <libcamera/class.h>\n> +#include <libcamera/scoped_file_descriptor.h>\n>  #include <libcamera/span.h>\n>  \n>  namespace libcamera {\n> @@ -40,7 +41,7 @@ public:\n>  \tbool exists() const;\n>  \n>  \tbool open(OpenMode mode);\n> -\tbool isOpen() const { return fd_ != -1; }\n> +\tbool isOpen() const { return fd_.isValid(); }\n>  \tOpenMode openMode() const { return mode_; }\n>  \tvoid close();\n>  \n> @@ -65,7 +66,7 @@ private:\n>  \tvoid unmapAll();\n>  \n>  \tstd::string name_;\n> -\tint fd_;\n> +\tScopedFD fd_;\n>  \tOpenMode mode_;\n>  \n>  \tint error_;\n> diff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp\n> index bce2b613..4db9a8d3 100644\n> --- a/src/libcamera/file.cpp\n> +++ b/src/libcamera/file.cpp\n> @@ -73,7 +73,7 @@ LOG_DEFINE_CATEGORY(File)\n>   * before performing I/O operations.\n>   */\n>  File::File(const std::string &name)\n> -\t: name_(name), fd_(-1), mode_(NotOpen), error_(0)\n> +\t: name_(name), mode_(NotOpen), error_(0)\n>  {\n>  }\n>  \n> @@ -84,7 +84,7 @@ File::File(const std::string &name)\n>   * setFileName().\n>   */\n>  File::File()\n> -\t: fd_(-1), mode_(NotOpen), error_(0)\n> +\t: mode_(NotOpen), error_(0)\n>  {\n>  }\n>  \n> @@ -167,12 +167,13 @@ bool File::open(File::OpenMode mode)\n>  \tif (mode & WriteOnly)\n>  \t\tflags |= O_CREAT;\n>  \n> -\tfd_ = ::open(name_.c_str(), flags, 0666);\n> -\tif (fd_ < 0) {\n> +\tint fd = ::open(name_.c_str(), flags, 0666);\n> +\tif (fd < 0) {\n>  \t\terror_ = -errno;\n>  \t\treturn false;\n>  \t}\n>  \n> +\tfd_ = ScopedFD(fd);\n>  \tmode_ = mode;\n>  \terror_ = 0;\n>  \treturn true;\n> @@ -199,11 +200,10 @@ bool File::open(File::OpenMode mode)\n>   */\n>  void File::close()\n>  {\n> -\tif (fd_ == -1)\n> +\tif (!fd_.isValid())\n>  \t\treturn;\n>  \n> -\t::close(fd_);\n> -\tfd_ = -1;\n> +\tfd_.reset();\n>  \tmode_ = NotOpen;\n>  }\n>  \n> @@ -233,7 +233,7 @@ ssize_t File::size() const\n>  \t\treturn -EINVAL;\n>  \n>  \tstruct stat st;\n> -\tint ret = fstat(fd_, &st);\n> +\tint ret = fstat(fd_.get(), &st);\n>  \tif (ret < 0)\n>  \t\treturn -errno;\n>  \n> @@ -252,7 +252,7 @@ off_t File::pos() const\n>  \tif (!isOpen())\n>  \t\treturn 0;\n>  \n> -\treturn lseek(fd_, 0, SEEK_CUR);\n> +\treturn lseek(fd_.get(), 0, SEEK_CUR);\n>  }\n>  \n>  /**\n> @@ -266,7 +266,7 @@ off_t File::seek(off_t pos)\n>  \tif (!isOpen())\n>  \t\treturn -EINVAL;\n>  \n> -\toff_t ret = lseek(fd_, pos, SEEK_SET);\n> +\toff_t ret = lseek(fd_.get(), pos, SEEK_SET);\n>  \tif (ret < 0)\n>  \t\treturn -errno;\n>  \n> @@ -298,7 +298,7 @@ ssize_t File::read(const Span<uint8_t> &data)\n>  \n>  \t/* Retry in case of interrupted system calls. */\n>  \twhile (readBytes < data.size()) {\n> -\t\tret = ::read(fd_, data.data() + readBytes,\n> +\t\tret = ::read(fd_.get(), data.data() + readBytes,\n>  \t\t\t     data.size() - readBytes);\n>  \t\tif (ret <= 0)\n>  \t\t\tbreak;\n> @@ -335,7 +335,7 @@ ssize_t File::write(const Span<const uint8_t> &data)\n>  \n>  \t/* Retry in case of interrupted system calls. */\n>  \twhile (writtenBytes < data.size()) {\n> -\t\tssize_t ret = ::write(fd_, data.data() + writtenBytes,\n> +\t\tssize_t ret = ::write(fd_.get(), data.data() + writtenBytes,\n>  \t\t\t\t      data.size() - writtenBytes);\n>  \t\tif (ret <= 0)\n>  \t\t\tbreak;\n> @@ -398,7 +398,7 @@ Span<uint8_t> File::map(off_t offset, ssize_t size, enum File::MapFlag flags)\n>  \tif (flags & MapPrivate)\n>  \t\tprot |= PROT_WRITE;\n>  \n> -\tvoid *map = mmap(NULL, size, prot, mmapFlags, fd_, offset);\n> +\tvoid *map = mmap(NULL, size, prot, mmapFlags, fd_.get(), offset);\n>  \tif (map == MAP_FAILED) {\n>  \t\terror_ = -errno;\n>  \t\treturn {};","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 4B582C3206\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  6 Jun 2021 18:05:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8E95068926;\n\tSun,  6 Jun 2021 20:05:07 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A0F99602A7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Jun 2021 20:05:03 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CECC2EF;\n\tSun,  6 Jun 2021 20:05:02 +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=\"n5jyWOL2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1623002703;\n\tbh=Oa18EZ+JKA/8lYI7TVgvq2y5laCbDgu2Khyo4zd6FsA=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=n5jyWOL231+j5vV19pmPTSAd7yqbwhUfm9zBLrB5bw9bN7m/8xf24WeFyWhLM716T\n\tb41FaMRQKxUm+McOhdC6Wo5mJp0lXwgVcfIx+aL0TUpXiBJYhrJBNnge2+rAh1SpB/\n\tun2YR9/LK4H2YlvD1I9Pf6lAlETp6yamal8bDFCE=","Date":"Sun, 6 Jun 2021 21:04:49 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YL0OQcuHeabRoBW8@pendragon.ideasonboard.com>","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-3-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210415083843.3399502-3-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [RFC PATCH 03/10] libcamera: File: Manage fd\n\tby ScopedFD","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":17431,"web_url":"https://patchwork.libcamera.org/comment/17431/","msgid":"<CAO5uPHND_3mnXAnf6WK4OvEyqrOXsFm=0hdu2vTW=H0LQJRbyQ@mail.gmail.com>","date":"2021-06-07T03:22:27","subject":"Re: [libcamera-devel] [RFC PATCH 03/10] libcamera: File: Manage fd\n\tby ScopedFD","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"content":"Hi Laurent,\n\nOn Mon, Jun 7, 2021 at 3:05 AM Laurent Pinchart <\nlaurent.pinchart@ideasonboard.com> wrote:\n\n> Hi Hiro,\n>\n> Thank you for the patch.\n>\n> On Thu, Apr 15, 2021 at 05:38:36PM +0900, Hirokazu Honda wrote:\n> > File owns the file descriptor for a file. It should be managed\n> > by ScopedFD to avoid the leakage.\n>\n> Is there any leak in the current implementation ? If not, I'd reword the\n> commit message.\n>\n\nNo leakage is there today. I fixed the commit message.\n\nRegards,\n-Hiro\n\n>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n>\n> > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > ---\n> >  include/libcamera/internal/file.h |  5 +++--\n> >  src/libcamera/file.cpp            | 26 +++++++++++++-------------\n> >  2 files changed, 16 insertions(+), 15 deletions(-)\n> >\n> > diff --git a/include/libcamera/internal/file.h\n> b/include/libcamera/internal/file.h\n> > index f0b313a5..e6eb7f04 100644\n> > --- a/include/libcamera/internal/file.h\n> > +++ b/include/libcamera/internal/file.h\n> > @@ -12,6 +12,7 @@\n> >  #include <sys/types.h>\n> >\n> >  #include <libcamera/class.h>\n> > +#include <libcamera/scoped_file_descriptor.h>\n> >  #include <libcamera/span.h>\n> >\n> >  namespace libcamera {\n> > @@ -40,7 +41,7 @@ public:\n> >       bool exists() const;\n> >\n> >       bool open(OpenMode mode);\n> > -     bool isOpen() const { return fd_ != -1; }\n> > +     bool isOpen() const { return fd_.isValid(); }\n> >       OpenMode openMode() const { return mode_; }\n> >       void close();\n> >\n> > @@ -65,7 +66,7 @@ private:\n> >       void unmapAll();\n> >\n> >       std::string name_;\n> > -     int fd_;\n> > +     ScopedFD fd_;\n> >       OpenMode mode_;\n> >\n> >       int error_;\n> > diff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp\n> > index bce2b613..4db9a8d3 100644\n> > --- a/src/libcamera/file.cpp\n> > +++ b/src/libcamera/file.cpp\n> > @@ -73,7 +73,7 @@ LOG_DEFINE_CATEGORY(File)\n> >   * before performing I/O operations.\n> >   */\n> >  File::File(const std::string &name)\n> > -     : name_(name), fd_(-1), mode_(NotOpen), error_(0)\n> > +     : name_(name), mode_(NotOpen), error_(0)\n> >  {\n> >  }\n> >\n> > @@ -84,7 +84,7 @@ File::File(const std::string &name)\n> >   * setFileName().\n> >   */\n> >  File::File()\n> > -     : fd_(-1), mode_(NotOpen), error_(0)\n> > +     : mode_(NotOpen), error_(0)\n> >  {\n> >  }\n> >\n> > @@ -167,12 +167,13 @@ bool File::open(File::OpenMode mode)\n> >       if (mode & WriteOnly)\n> >               flags |= O_CREAT;\n> >\n> > -     fd_ = ::open(name_.c_str(), flags, 0666);\n> > -     if (fd_ < 0) {\n> > +     int fd = ::open(name_.c_str(), flags, 0666);\n> > +     if (fd < 0) {\n> >               error_ = -errno;\n> >               return false;\n> >       }\n> >\n> > +     fd_ = ScopedFD(fd);\n> >       mode_ = mode;\n> >       error_ = 0;\n> >       return true;\n> > @@ -199,11 +200,10 @@ bool File::open(File::OpenMode mode)\n> >   */\n> >  void File::close()\n> >  {\n> > -     if (fd_ == -1)\n> > +     if (!fd_.isValid())\n> >               return;\n> >\n> > -     ::close(fd_);\n> > -     fd_ = -1;\n> > +     fd_.reset();\n> >       mode_ = NotOpen;\n> >  }\n> >\n> > @@ -233,7 +233,7 @@ ssize_t File::size() const\n> >               return -EINVAL;\n> >\n> >       struct stat st;\n> > -     int ret = fstat(fd_, &st);\n> > +     int ret = fstat(fd_.get(), &st);\n> >       if (ret < 0)\n> >               return -errno;\n> >\n> > @@ -252,7 +252,7 @@ off_t File::pos() const\n> >       if (!isOpen())\n> >               return 0;\n> >\n> > -     return lseek(fd_, 0, SEEK_CUR);\n> > +     return lseek(fd_.get(), 0, SEEK_CUR);\n> >  }\n> >\n> >  /**\n> > @@ -266,7 +266,7 @@ off_t File::seek(off_t pos)\n> >       if (!isOpen())\n> >               return -EINVAL;\n> >\n> > -     off_t ret = lseek(fd_, pos, SEEK_SET);\n> > +     off_t ret = lseek(fd_.get(), pos, SEEK_SET);\n> >       if (ret < 0)\n> >               return -errno;\n> >\n> > @@ -298,7 +298,7 @@ ssize_t File::read(const Span<uint8_t> &data)\n> >\n> >       /* Retry in case of interrupted system calls. */\n> >       while (readBytes < data.size()) {\n> > -             ret = ::read(fd_, data.data() + readBytes,\n> > +             ret = ::read(fd_.get(), data.data() + readBytes,\n> >                            data.size() - readBytes);\n> >               if (ret <= 0)\n> >                       break;\n> > @@ -335,7 +335,7 @@ ssize_t File::write(const Span<const uint8_t> &data)\n> >\n> >       /* Retry in case of interrupted system calls. */\n> >       while (writtenBytes < data.size()) {\n> > -             ssize_t ret = ::write(fd_, data.data() + writtenBytes,\n> > +             ssize_t ret = ::write(fd_.get(), data.data() +\n> writtenBytes,\n> >                                     data.size() - writtenBytes);\n> >               if (ret <= 0)\n> >                       break;\n> > @@ -398,7 +398,7 @@ Span<uint8_t> File::map(off_t offset, ssize_t size,\n> enum File::MapFlag flags)\n> >       if (flags & MapPrivate)\n> >               prot |= PROT_WRITE;\n> >\n> > -     void *map = mmap(NULL, size, prot, mmapFlags, fd_, offset);\n> > +     void *map = mmap(NULL, size, prot, mmapFlags, fd_.get(), offset);\n> >       if (map == MAP_FAILED) {\n> >               error_ = -errno;\n> >               return {};\n>\n> --\n> Regards,\n>\n> Laurent Pinchart\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 DF1C2C320B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  7 Jun 2021 03:22:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 980CE68925;\n\tMon,  7 Jun 2021 05:22:38 +0200 (CEST)","from mail-ed1-x533.google.com (mail-ed1-x533.google.com\n\t[IPv6:2a00:1450:4864:20::533])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AA8D06029E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  7 Jun 2021 05:22:37 +0200 (CEST)","by mail-ed1-x533.google.com with SMTP id g18so16488589edq.8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 06 Jun 2021 20:22:37 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"Pbm4kdC3\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc; bh=JaNi2NWb6gBRi6ywcCqZ8L4M+pBNQFRzF8lfMP2ZfHU=;\n\tb=Pbm4kdC3rASaj2zikMA2t9jVV3r8W+G7aOEZvrbt0FvRT4Fi8SgzJDrTy1uitAbBee\n\tLQLbw61QuzfutiNUA2sdjT3jFRvxByGP2DcozSnyJFuixsts8B7hBBntCXe0Nbeq5IDy\n\tMpnp/z/XQSboFQTX+15h+yuzyYyCES1EKdPM8=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=JaNi2NWb6gBRi6ywcCqZ8L4M+pBNQFRzF8lfMP2ZfHU=;\n\tb=BRRgGnuionqMYdoxZymryLgdn5IYQMVcKXdHDNK8Vg06z8KRaTpY5Ipvjwa/t8lVsC\n\tz+674vz6FWsPT34orjxInGFBhv0xZZkDMZh3ObloGI+8qF0cVpVGIHrDaj//UUup/f+W\n\teqbRP3l9TYLHBDetxyUfwfwv38z27BY0/e02fxeEeu/QyHpGZ0JqWIKEeUlSdeSMu4DW\n\tG+SHhSUUtYMpepigeOw+vqGZz75jX15+RmxiKy4fvA/RVgNKAsyOI3S4tyYz4yu+zMkD\n\tOOz+2jZH/eQOs42DCIv/0jR37JH0EvQsotFsxdpRkwz4ox2+S7nPvapqwClurYp4lV2k\n\tw/Lg==","X-Gm-Message-State":"AOAM533hfEhQIPqC/zu1r2WvANB2tqsDpgCH1L3IMWrrOLqWz5YfKCjz\n\tK1JT7mUi3ECisM7sCPh36UJBu5el1e5jRumRTrxcxjkxEPY=","X-Google-Smtp-Source":"ABdhPJy2J9zxZRqQ8mBtGiq+iyDiOQYvMb8Cw+Jy+RMGdmQETKY2Q2HEcMyiZzWlhoSwIuWxGDpLXw8Ut3gMY7Zzo7c=","X-Received":"by 2002:a05:6402:3198:: with SMTP id\n\tdi24mr17252144edb.244.1623036157358; \n\tSun, 06 Jun 2021 20:22:37 -0700 (PDT)","MIME-Version":"1.0","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-3-hiroh@chromium.org>\n\t<YL0OQcuHeabRoBW8@pendragon.ideasonboard.com>","In-Reply-To":"<YL0OQcuHeabRoBW8@pendragon.ideasonboard.com>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Mon, 7 Jun 2021 12:22:27 +0900","Message-ID":"<CAO5uPHND_3mnXAnf6WK4OvEyqrOXsFm=0hdu2vTW=H0LQJRbyQ@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Content-Type":"multipart/alternative; boundary=\"0000000000009e044e05c4248d53\"","Subject":"Re: [libcamera-devel] [RFC PATCH 03/10] libcamera: File: Manage fd\n\tby ScopedFD","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>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]