[{"id":3144,"web_url":"https://patchwork.libcamera.org/comment/3144/","msgid":"<20191127105204.b42fds7cfcqctqyk@uno.localdomain>","date":"2019-11-27T10:52:04","subject":"Re: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add\n\tFileDecriptor to help deal with file descriptors","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Niklas,\n\nOn Wed, Nov 27, 2019 at 12:35:54AM +0100, Niklas Söderlund wrote:\n> Add a helper to make it easier to pass file descriptors around. The\n> helper class duplicates the fd which decouples it from the original fd\n> which could be closed by its owner while the new FileDescriptor remains\n> valid.\n>\n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  include/libcamera/buffer.h | 12 ++++++++++++\n>  src/libcamera/buffer.cpp   | 40 ++++++++++++++++++++++++++++++++++++++\n>  2 files changed, 52 insertions(+)\n>\n> diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\n> index 7302b9f32ce8c50d..33793b4ccf881eda 100644\n> --- a/include/libcamera/buffer.h\n> +++ b/include/libcamera/buffer.h\n> @@ -46,6 +46,18 @@ private:\n>  \tstd::vector<Plane> planes_;\n>  };\n>\n> +class FileDescriptor final\n> +{\n> +public:\n> +\tFileDescriptor(int fd);\n> +\t~FileDescriptor();\n> +\n> +\tint fd() const { return fd_; }\n> +\n> +private:\n> +\tint fd_;\n> +};\n> +\n>  class Plane final\n>  {\n>  public:\n> diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\n> index 4acff216cafba484..0676586ae3be2a61 100644\n> --- a/src/libcamera/buffer.cpp\n> +++ b/src/libcamera/buffer.cpp\n> @@ -115,6 +115,46 @@ void BufferInfo::update(Status status, unsigned int sequence,\n>   * \\return A array holding all plane information for the buffer\n>   */\n>\n> +/**\n> + * \\class FileDescriptor\n> + * \\brief Life time management of a file descriptor\n> + *\n> + * The managed file descriptor (fd) is duplicated from the original one and\n> + * is opened for the life time of the FileDescriptor object disregarding\n\ns/is opened/stays opened/ ?\n\n> + * if the original one is closed.\n> + */\n> +\n> +/**\n> + * \\brief Create a life time managed file descriptor\n> + * \\param[in] fd File descriptor\n> + */\n> +FileDescriptor::FileDescriptor(int fd)\n> +\t: fd_(-1)\n> +{\n> +\tif (fd < 0)\n> +\t\treturn;\n\nIf one has gone so fare as providing an invalid fd to the constructor,\nprobably has missed some error check, I would LOG() an error here (or\neven Fatal ? )\n\n> +\n> +\t/* Failing to dup() a fd should not happen and is fatal. */\n> +\tfd_ = dup(fd);\n> +\tif (fd_ == -1) {\n> +\t\tint ret = -errno;\n> +\t\tLOG(Buffer, Fatal)\n> +\t\t\t<< \"Failed to dup() fd: \" << strerror(-ret);\n> +\t}\n> +}\n> +\n> +FileDescriptor::~FileDescriptor()\n> +{\n> +\tif (fd_ != -1)\n> +\t\tclose(fd_);\n> +}\n> +\n> +/**\n> + * \\fn FileDescriptor::fd()\n> + * \\brief Retrieve the file descriptor or\n\nor.. ?\n\n> + * \\return A valid file descriptor or a negative error code\n\nNot a negative error code, but -1 if the instance has been created\nwith an invalid file descriptor. How to put it nicely here, well...\nnot sure :)\n\nWording apart:\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nThanks\n   j\n\n> + */\n> +\n>  /**\n>   * \\class Plane\n>   * \\brief A memory region to store a single plane of a frame\n> --\n> 2.24.0\n>\n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net\n\t[217.70.183.197])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D790260BB8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Nov 2019 11:49:57 +0100 (CET)","from uno.localdomain (93-34-114-233.ip49.fastwebnet.it\n\t[93.34.114.233]) (Authenticated sender: jacopo@jmondi.org)\n\tby relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 603671C0005;\n\tWed, 27 Nov 2019 10:49:57 +0000 (UTC)"],"X-Originating-IP":"93.34.114.233","Date":"Wed, 27 Nov 2019 11:52:04 +0100","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191127105204.b42fds7cfcqctqyk@uno.localdomain>","References":"<20191126233620.1695316-1-niklas.soderlund@ragnatech.se>\n\t<20191126233620.1695316-5-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\"; boundary=\"klwekmb6k4kdlbpq\"","Content-Disposition":"inline","In-Reply-To":"<20191126233620.1695316-5-niklas.soderlund@ragnatech.se>","User-Agent":"NeoMutt/20180716","Subject":"Re: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add\n\tFileDecriptor to help deal with file descriptors","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>","X-List-Received-Date":"Wed, 27 Nov 2019 10:49:58 -0000"}},{"id":3229,"web_url":"https://patchwork.libcamera.org/comment/3229/","msgid":"<20191209180705.GG4853@pendragon.ideasonboard.com>","date":"2019-12-09T18:07:05","subject":"Re: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add\n\tFileDecriptor to help deal with file descriptors","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hello,\n\nOn Wed, Nov 27, 2019 at 11:52:04AM +0100, Jacopo Mondi wrote:\n> On Wed, Nov 27, 2019 at 12:35:54AM +0100, Niklas Söderlund wrote:\n> > Add a helper to make it easier to pass file descriptors around. The\n> > helper class duplicates the fd which decouples it from the original fd\n> > which could be closed by its owner while the new FileDescriptor remains\n> > valid.\n> >\n> > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> > ---\n> >  include/libcamera/buffer.h | 12 ++++++++++++\n> >  src/libcamera/buffer.cpp   | 40 ++++++++++++++++++++++++++++++++++++++\n> >  2 files changed, 52 insertions(+)\n> >\n> > diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\n> > index 7302b9f32ce8c50d..33793b4ccf881eda 100644\n> > --- a/include/libcamera/buffer.h\n> > +++ b/include/libcamera/buffer.h\n> > @@ -46,6 +46,18 @@ private:\n> >  \tstd::vector<Plane> planes_;\n> >  };\n> >\n> > +class FileDescriptor final\n> > +{\n> > +public:\n> > +\tFileDescriptor(int fd);\n> > +\t~FileDescriptor();\n\nYou're missing the copy constructor and assignment operator that should\ndup() the fd, and the move versions that should set them to -1 in the\noriginal.\n\nAll constructors should be marked as explicit to avoid unwanted copies,\nas those are a bit expensive.\n\n> > +\n> > +\tint fd() const { return fd_; }\n> > +\n> > +private:\n> > +\tint fd_;\n> > +};\n> > +\n> >  class Plane final\n> >  {\n> >  public:\n> > diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\n> > index 4acff216cafba484..0676586ae3be2a61 100644\n> > --- a/src/libcamera/buffer.cpp\n> > +++ b/src/libcamera/buffer.cpp\n> > @@ -115,6 +115,46 @@ void BufferInfo::update(Status status, unsigned int sequence,\n> >   * \\return A array holding all plane information for the buffer\n> >   */\n> >\n> > +/**\n> > + * \\class FileDescriptor\n> > + * \\brief Life time management of a file descriptor\n> > + *\n> > + * The managed file descriptor (fd) is duplicated from the original one and\n> > + * is opened for the life time of the FileDescriptor object disregarding\n> \n> s/is opened/stays opened/ ?\n> \n> > + * if the original one is closed.\n\n * \\brief RAII-style wrapper for file descriptors\n *\n * The FileDescriptor class wraps a file descriptor (expressed as a signed\n * integer) to provide an RAII-style mechanism for owning the file descriptor.\n * When constructed, the FileDescriptor instance duplicates a given file\n * descriptor and takes ownership of the duplicate as a resource. The copy\n * constructor and assignment operator duplicate the file descriptor, while the\n * move version of those methods move the resource and make the original\n * FileDescriptor invalid. When the FileDescriptor is deleted, it closes the\n * file descriptor it owns, if any.\n\n(See https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization\nfor a definition of RAII)\n\n> > + */\n> > +\n> > +/**\n> > + * \\brief Create a life time managed file descriptor\n> > + * \\param[in] fd File descriptor\n\n * \\brief Create a FileDescriptor wrapping a copy of a given \\a fd\n * \\param[in] fd File descriptor\n *\n * Constructing a FileDescriptor from a numerical file descriptor duplicates the\n * \\a fd and takes ownership of the copy. The original \\a fd is left untouched,\n * and the caller is responsible for closing it when appropriate. The duplicated\n * file descriptor will be closed automatically when the FileDescriptor instance\n * is destroyed.\n\nI wonder if we should also define a\n\nFileDescriptor::FileDescriptor(int &&fd)\n\nthat skips the duplication, as the caller may sometimes not have a need\nto keep the original around.\n\n> > + */\n> > +FileDescriptor::FileDescriptor(int fd)\n> > +\t: fd_(-1)\n> > +{\n> > +\tif (fd < 0)\n> > +\t\treturn;\n> \n> If one has gone so fare as providing an invalid fd to the constructor,\n> probably has missed some error check, I would LOG() an error here (or\n> even Fatal ? )\n\nThere could be use cases for creating FileDescriptor instances that\ndon't own any file descriptor, and then assigning a valid instance to\nthem with operator=(). I would this not add any check here, and set a\ndefault value of -1 for the fd parameter in the header file.\n\n> > +\n> > +\t/* Failing to dup() a fd should not happen and is fatal. */\n> > +\tfd_ = dup(fd);\n> > +\tif (fd_ == -1) {\n> > +\t\tint ret = -errno;\n> > +\t\tLOG(Buffer, Fatal)\n> > +\t\t\t<< \"Failed to dup() fd: \" << strerror(-ret);\n> > +\t}\n> > +}\n> > +\n> > +FileDescriptor::~FileDescriptor()\n> > +{\n> > +\tif (fd_ != -1)\n> > +\t\tclose(fd_);\n> > +}\n> > +\n> > +/**\n> > + * \\fn FileDescriptor::fd()\n> > + * \\brief Retrieve the file descriptor or\n> \n> or.. ?\n> \n> > + * \\return A valid file descriptor or a negative error code\n> \n> Not a negative error code, but -1 if the instance has been created\n> with an invalid file descriptor. How to put it nicely here, well...\n> not sure :)\n\n * \\brief Retrieve the numerical file descriptor\n * \\return The numerical file descriptor, which may be -1 if the FileDescriptor\n * instance doesn't own a file descriptor\n\n> \n> Wording apart:\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> \n> > + */\n> > +\n> >  /**\n> >   * \\class Plane\n> >   * \\brief A memory region to store a single plane of a frame","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["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 29B7560BDB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Dec 2019 19:07:13 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7C10011B7;\n\tMon,  9 Dec 2019 19:07:12 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1575914832;\n\tbh=SHRpEQdcKqvlJntZ0k29HpTtsJK4U/c4nqUQxDjEFOc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=SZy557hLWJlev7oDnHg0lZaXA4iK7nZKTNNhydEEwaKPKow7XuFMae7aeu/fxunJv\n\tArph4o0sTe1u011PZYuhxO+t/2UhbCzsg4c3OtuBMtNRGEJmLa2gK8RMcnEnKJ9+nO\n\tFm6JAeNldaNjj2kZrABeaN3ChPBzvgAKAqVmmr9U=","Date":"Mon, 9 Dec 2019 20:07:05 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>,\n\tlibcamera-devel@lists.libcamera.org","Message-ID":"<20191209180705.GG4853@pendragon.ideasonboard.com>","References":"<20191126233620.1695316-1-niklas.soderlund@ragnatech.se>\n\t<20191126233620.1695316-5-niklas.soderlund@ragnatech.se>\n\t<20191127105204.b42fds7cfcqctqyk@uno.localdomain>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20191127105204.b42fds7cfcqctqyk@uno.localdomain>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add\n\tFileDecriptor to help deal with file descriptors","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>","X-List-Received-Date":"Mon, 09 Dec 2019 18:07:13 -0000"}},{"id":3230,"web_url":"https://patchwork.libcamera.org/comment/3230/","msgid":"<20191209181241.GH4853@pendragon.ideasonboard.com>","date":"2019-12-09T18:12:41","subject":"Re: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add\n\tFileDecriptor to help deal with file descriptors","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hello Niklas,\n\nOn Wed, Nov 27, 2019 at 12:35:54AM +0100, Niklas Söderlund wrote:\n> Add a helper to make it easier to pass file descriptors around. The\n> helper class duplicates the fd which decouples it from the original fd\n> which could be closed by its owner while the new FileDescriptor remains\n> valid.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  include/libcamera/buffer.h | 12 ++++++++++++\n>  src/libcamera/buffer.cpp   | 40 ++++++++++++++++++++++++++++++++++++++\n\nI missed this, you should move this class to file_descriptor.{h,cpp} as\nit may be useful beyond buffers.\n\n>  2 files changed, 52 insertions(+)\n> \n> diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\n> index 7302b9f32ce8c50d..33793b4ccf881eda 100644\n> --- a/include/libcamera/buffer.h\n> +++ b/include/libcamera/buffer.h\n> @@ -46,6 +46,18 @@ private:\n>  \tstd::vector<Plane> planes_;\n>  };\n>  \n> +class FileDescriptor final\n> +{\n> +public:\n> +\tFileDescriptor(int fd);\n> +\t~FileDescriptor();\n> +\n> +\tint fd() const { return fd_; }\n> +\n> +private:\n> +\tint fd_;\n> +};\n> +\n>  class Plane final\n>  {\n>  public:\n> diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\n> index 4acff216cafba484..0676586ae3be2a61 100644\n> --- a/src/libcamera/buffer.cpp\n> +++ b/src/libcamera/buffer.cpp\n> @@ -115,6 +115,46 @@ void BufferInfo::update(Status status, unsigned int sequence,\n>   * \\return A array holding all plane information for the buffer\n>   */\n>  \n> +/**\n> + * \\class FileDescriptor\n> + * \\brief Life time management of a file descriptor\n> + *\n> + * The managed file descriptor (fd) is duplicated from the original one and\n> + * is opened for the life time of the FileDescriptor object disregarding\n> + * if the original one is closed.\n> + */\n> +\n> +/**\n> + * \\brief Create a life time managed file descriptor\n> + * \\param[in] fd File descriptor\n> + */\n> +FileDescriptor::FileDescriptor(int fd)\n> +\t: fd_(-1)\n> +{\n> +\tif (fd < 0)\n> +\t\treturn;\n> +\n> +\t/* Failing to dup() a fd should not happen and is fatal. */\n> +\tfd_ = dup(fd);\n> +\tif (fd_ == -1) {\n> +\t\tint ret = -errno;\n> +\t\tLOG(Buffer, Fatal)\n> +\t\t\t<< \"Failed to dup() fd: \" << strerror(-ret);\n> +\t}\n> +}\n> +\n> +FileDescriptor::~FileDescriptor()\n> +{\n> +\tif (fd_ != -1)\n> +\t\tclose(fd_);\n> +}\n> +\n> +/**\n> + * \\fn FileDescriptor::fd()\n> + * \\brief Retrieve the file descriptor or\n> + * \\return A valid file descriptor or a negative error code\n> + */\n> +\n>  /**\n>   * \\class Plane\n>   * \\brief A memory region to store a single plane of a frame","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A96A960BDB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Dec 2019 19:12:48 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 27D3111B7;\n\tMon,  9 Dec 2019 19:12:48 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1575915168;\n\tbh=g9mG+IkgSZblDv/0sZjRr61z9UA5ITpWpcNsSB1Bq7U=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=IwNG1iFgQSvXhaO2SyiLrCAF/QVjgkFUTzyK9TRuU+AFKJ61DG1cNAsxBwAUIl5xL\n\tgVWbfjqzLGEzknhzCqD/DGIqaeXsxV6mcQQfsHQFmXwVROLg3xSo5gowW1Vta0T9MI\n\tkV3pJpE+nk0Ko1x7ijfX0unhFlnLDHqHolFfX8D0=","Date":"Mon, 9 Dec 2019 20:12:41 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191209181241.GH4853@pendragon.ideasonboard.com>","References":"<20191126233620.1695316-1-niklas.soderlund@ragnatech.se>\n\t<20191126233620.1695316-5-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20191126233620.1695316-5-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add\n\tFileDecriptor to help deal with file descriptors","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>","X-List-Received-Date":"Mon, 09 Dec 2019 18:12:48 -0000"}}]