[{"id":17416,"web_url":"https://patchwork.libcamera.org/comment/17416/","msgid":"<YL0aWWf8gSNMH4j5@pendragon.ideasonboard.com>","date":"2021-06-06T18:56:25","subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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:43PM +0900, Hirokazu Honda wrote:\n> V4L2Camera::getBufferFd() returns FileDescriptor. However, the\n> file descriptor is still owned by V4L2Camera. It should rather\n> return an integer to represent V4L2Camera doesn't have the\n> ownership of the file descriptor.\n\nThe FileDescriptor class doesn't imply ownership, as it's similar to a\nshared_ptr<>. I'm not against this patch, but why do you think\nFileDescriptor is bad here ?\n\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  src/v4l2/v4l2_camera.cpp       | 6 +++---\n>  src/v4l2/v4l2_camera.h         | 2 +-\n>  src/v4l2/v4l2_camera_proxy.cpp | 6 +++---\n>  3 files changed, 7 insertions(+), 7 deletions(-)\n> \n> diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\n> index 97825c71..cccc6749 100644\n> --- a/src/v4l2/v4l2_camera.cpp\n> +++ b/src/v4l2/v4l2_camera.cpp\n> @@ -186,16 +186,16 @@ void V4L2Camera::freeBuffers()\n>  \tbufferAllocator_->free(stream);\n>  }\n>  \n> -FileDescriptor V4L2Camera::getBufferFd(unsigned int index)\n> +int V4L2Camera::getBufferFd(unsigned int index)\n>  {\n>  \tStream *stream = config_->at(0).stream();\n>  \tconst std::vector<std::unique_ptr<FrameBuffer>> &buffers =\n>  \t\tbufferAllocator_->buffers(stream);\n>  \n>  \tif (buffers.size() <= index)\n> -\t\treturn FileDescriptor();\n> +\t\treturn -1;\n>  \n> -\treturn buffers[index]->planes()[0].fd;\n> +\treturn buffers[index]->planes()[0].fd.fd();\n>  }\n>  \n>  int V4L2Camera::streamOn()\n> diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\n> index d2380462..8efe1642 100644\n> --- a/src/v4l2/v4l2_camera.h\n> +++ b/src/v4l2/v4l2_camera.h\n> @@ -53,7 +53,7 @@ public:\n>  \n>  \tint allocBuffers(unsigned int count);\n>  \tvoid freeBuffers();\n> -\tFileDescriptor getBufferFd(unsigned int index);\n> +\tint getBufferFd(unsigned int index);\n>  \n>  \tint streamOn();\n>  \tint streamOff();\n> diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\n> index f8bfe595..8f417421 100644\n> --- a/src/v4l2/v4l2_camera_proxy.cpp\n> +++ b/src/v4l2/v4l2_camera_proxy.cpp\n> @@ -112,14 +112,14 @@ void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags,\n>  \t\treturn MAP_FAILED;\n>  \t}\n>  \n> -\tFileDescriptor fd = vcam_->getBufferFd(index);\n> -\tif (!fd.isValid()) {\n> +\tint fd = vcam_->getBufferFd(index);\n> +\tif (fd < 0) {\n>  \t\terrno = EINVAL;\n>  \t\treturn MAP_FAILED;\n>  \t}\n>  \n>  \tvoid *map = V4L2CompatManager::instance()->fops().mmap(addr, length, prot,\n> -\t\t\t\t\t\t\t       flags, fd.fd(), 0);\n> +\t\t\t\t\t\t\t       flags, fd, 0);\n>  \tif (map == MAP_FAILED)\n>  \t\treturn map;\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 32382C3206\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  6 Jun 2021 18:56:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9C6356892B;\n\tSun,  6 Jun 2021 20:56:40 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D0AE3602A7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Jun 2021 20:56:39 +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 1B311EF;\n\tSun,  6 Jun 2021 20:56:39 +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=\"uNYqJRVE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1623005799;\n\tbh=TLeEPz6CAuxZGT8S+O5N3HMFGEig97M+WUfEnknWFLQ=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=uNYqJRVEfhnSsFwXz12XyoClNanXN8LXEV0IIRiwJTgiVnm2HGwQQ+vNi7KDUdfJN\n\tBSazsiX8vvCcb+G/vwmFqB/q387oLSA41vMrNRXaYcosDeJzllUwF0iYyYI2SB0N2z\n\t5q/X7W9zI3J5rFZS0ZiUOFyCDCz0okhEtMShIgl0=","Date":"Sun, 6 Jun 2021 21:56:25 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YL0aWWf8gSNMH4j5@pendragon.ideasonboard.com>","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-10-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210415083843.3399502-10-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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":17435,"web_url":"https://patchwork.libcamera.org/comment/17435/","msgid":"<CAO5uPHNfnXTiV-Ysf7DMO=m3GX=ORaeLdVdpK9KU7RV40yPMfw@mail.gmail.com>","date":"2021-06-07T09:22:21","subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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:56 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:43PM +0900, Hirokazu Honda wrote:\n> > V4L2Camera::getBufferFd() returns FileDescriptor. However, the\n> > file descriptor is still owned by V4L2Camera. It should rather\n> > return an integer to represent V4L2Camera doesn't have the\n> > ownership of the file descriptor.\n>\n> The FileDescriptor class doesn't imply ownership, as it's similar to a\n> shared_ptr<>. I'm not against this patch, but why do you think\n> FileDescriptor is bad here ?\n>\n>\nIMO, returning FileDescriptor implies that a caller shares the ownership of\nit.\nIt enables a caller to keep it outlive a callee and even share its\nownership with others.\nWhat do you think?\n\n-Hiro\n\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > ---\n> >  src/v4l2/v4l2_camera.cpp       | 6 +++---\n> >  src/v4l2/v4l2_camera.h         | 2 +-\n> >  src/v4l2/v4l2_camera_proxy.cpp | 6 +++---\n> >  3 files changed, 7 insertions(+), 7 deletions(-)\n> >\n> > diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\n> > index 97825c71..cccc6749 100644\n> > --- a/src/v4l2/v4l2_camera.cpp\n> > +++ b/src/v4l2/v4l2_camera.cpp\n> > @@ -186,16 +186,16 @@ void V4L2Camera::freeBuffers()\n> >       bufferAllocator_->free(stream);\n> >  }\n> >\n> > -FileDescriptor V4L2Camera::getBufferFd(unsigned int index)\n> > +int V4L2Camera::getBufferFd(unsigned int index)\n> >  {\n> >       Stream *stream = config_->at(0).stream();\n> >       const std::vector<std::unique_ptr<FrameBuffer>> &buffers =\n> >               bufferAllocator_->buffers(stream);\n> >\n> >       if (buffers.size() <= index)\n> > -             return FileDescriptor();\n> > +             return -1;\n> >\n> > -     return buffers[index]->planes()[0].fd;\n> > +     return buffers[index]->planes()[0].fd.fd();\n> >  }\n> >\n> >  int V4L2Camera::streamOn()\n> > diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\n> > index d2380462..8efe1642 100644\n> > --- a/src/v4l2/v4l2_camera.h\n> > +++ b/src/v4l2/v4l2_camera.h\n> > @@ -53,7 +53,7 @@ public:\n> >\n> >       int allocBuffers(unsigned int count);\n> >       void freeBuffers();\n> > -     FileDescriptor getBufferFd(unsigned int index);\n> > +     int getBufferFd(unsigned int index);\n> >\n> >       int streamOn();\n> >       int streamOff();\n> > diff --git a/src/v4l2/v4l2_camera_proxy.cpp\n> b/src/v4l2/v4l2_camera_proxy.cpp\n> > index f8bfe595..8f417421 100644\n> > --- a/src/v4l2/v4l2_camera_proxy.cpp\n> > +++ b/src/v4l2/v4l2_camera_proxy.cpp\n> > @@ -112,14 +112,14 @@ void *V4L2CameraProxy::mmap(void *addr, size_t\n> length, int prot, int flags,\n> >               return MAP_FAILED;\n> >       }\n> >\n> > -     FileDescriptor fd = vcam_->getBufferFd(index);\n> > -     if (!fd.isValid()) {\n> > +     int fd = vcam_->getBufferFd(index);\n> > +     if (fd < 0) {\n> >               errno = EINVAL;\n> >               return MAP_FAILED;\n> >       }\n> >\n> >       void *map = V4L2CompatManager::instance()->fops().mmap(addr,\n> length, prot,\n> > -                                                            flags,\n> fd.fd(), 0);\n> > +                                                            flags, fd,\n> 0);\n> >       if (map == MAP_FAILED)\n> >               return map;\n> >\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 80F05C3206\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  7 Jun 2021 09:22:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0477368926;\n\tMon,  7 Jun 2021 11:22:34 +0200 (CEST)","from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com\n\t[IPv6:2a00:1450:4864:20::52e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1C14968925\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  7 Jun 2021 11:22:32 +0200 (CEST)","by mail-ed1-x52e.google.com with SMTP id r11so19420187edt.13\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 07 Jun 2021 02:22:32 -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=\"YQTjVQoW\"; 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=yxBoQjsCYLjNH8fQc0bbY2B+0zgu8kswAcugz7AvAjk=;\n\tb=YQTjVQoWNAIXQ3R+BG2JMr1fmQCIBKrwDlZ7Cuo2MfybrIlCnTyeKLfoLWLDacGioJ\n\tIBIF9nwjDrzNECMAdQ2UfB6V9Ioelmbu9cpPZDHW9oMfv7ki3zO4lEYzdT6fSHUSC/FK\n\tw+v11HpD6L9TqNZHI2dbQY/wTQl9kcPmWL0Tc=","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=yxBoQjsCYLjNH8fQc0bbY2B+0zgu8kswAcugz7AvAjk=;\n\tb=IF1ojLndPvEt+mu30gbGbEhLxtcf73tns9o0+u4JoKcbBIrP7NON6ULn5Ka2b0wReH\n\tw5Ek5uP/4Nm/Ajwm+6Pqj00fon7J6h1+Dpa82mbkD4hKdNY/Gt1eVTjDvtCNUNo/ENXB\n\tMtgkoNP1vuSaHH40S9ST5fDvK0IB4eni8QWGcfJl/ufZmK37mPBmbRLg8VTTKHg681uw\n\tute8jxfbn/3ymcuvH8PgTvt8/BJ99hdSJS6XwaOCmueiW/X884bZK0IlUMLeWk5h2QB/\n\tBuDmvKMdotuqs8oMm2j2OdMlEFTd6r7vuVqCB/Pt8xM/hT3168iK5yhfg4Zzwjim+Qhb\n\tdWRw==","X-Gm-Message-State":"AOAM530FB0/vcTGirW2YDdjh8tPqL+SkRJBxU9VIzjOh3/PlF/u0/khC\n\to8BxZw42lhOgu5ibYVYb8gVjNgYrp8AOqt4L72H2q0OgIxI=","X-Google-Smtp-Source":"ABdhPJyvjZqQmXqfIK+Wp3Dsij64AMHY3ppusNCu2SKj1tTv8Y7tgSqbzPJo0zDtZjSEaU3Nl9o1+9QvESpZxPFI9+A=","X-Received":"by 2002:aa7:ce05:: with SMTP id\n\td5mr18629062edv.325.1623057751703; \n\tMon, 07 Jun 2021 02:22:31 -0700 (PDT)","MIME-Version":"1.0","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-10-hiroh@chromium.org>\n\t<YL0aWWf8gSNMH4j5@pendragon.ideasonboard.com>","In-Reply-To":"<YL0aWWf8gSNMH4j5@pendragon.ideasonboard.com>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Mon, 7 Jun 2021 18:22:21 +0900","Message-ID":"<CAO5uPHNfnXTiV-Ysf7DMO=m3GX=ORaeLdVdpK9KU7RV40yPMfw@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Content-Type":"multipart/alternative; boundary=\"000000000000bd960705c429940d\"","Subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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>"}},{"id":17841,"web_url":"https://patchwork.libcamera.org/comment/17841/","msgid":"<YNk8FhZDa7Ttvg1c@pendragon.ideasonboard.com>","date":"2021-06-28T03:03:50","subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nOn Mon, Jun 07, 2021 at 06:22:21PM +0900, Hirokazu Honda wrote:\n> On Mon, Jun 7, 2021 at 3:56 AM Laurent Pinchart wrote:\n> > On Thu, Apr 15, 2021 at 05:38:43PM +0900, Hirokazu Honda wrote:\n> > > V4L2Camera::getBufferFd() returns FileDescriptor. However, the\n> > > file descriptor is still owned by V4L2Camera. It should rather\n> > > return an integer to represent V4L2Camera doesn't have the\n> > > ownership of the file descriptor.\n> >\n> > The FileDescriptor class doesn't imply ownership, as it's similar to a\n> > shared_ptr<>. I'm not against this patch, but why do you think\n> > FileDescriptor is bad here ?\n>\n> IMO, returning FileDescriptor implies that a caller shares the ownership of it.\n> It enables a caller to keep it outlive a callee and even share its\n> ownership with others.\n> What do you think?\n\nGenerally speaking, it may be dangerous to handle file descriptors as\nintegers, due not only to the risk of leakage, but also to the risk of\nuse-after-free (or rather use-after-close in this case). FileDescriptor\nprevents that.\n\nOn the other hand, I agree with you that in this case the caller isn't\nmeant to use the file descriptor in any way that would outlive the\ndescriptor stored in V4L2Camera. If it was a generic API, I'd prefer\nkeeping FileDescriptor, but as V4L2Camera and V4L2CameraProxy are\ntightly coupled, it really doesn't matter much. I'm OK with the patch if\nyou think it's better.\n\n> > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > > ---\n> > >  src/v4l2/v4l2_camera.cpp       | 6 +++---\n> > >  src/v4l2/v4l2_camera.h         | 2 +-\n> > >  src/v4l2/v4l2_camera_proxy.cpp | 6 +++---\n> > >  3 files changed, 7 insertions(+), 7 deletions(-)\n> > >\n> > > diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\n> > > index 97825c71..cccc6749 100644\n> > > --- a/src/v4l2/v4l2_camera.cpp\n> > > +++ b/src/v4l2/v4l2_camera.cpp\n> > > @@ -186,16 +186,16 @@ void V4L2Camera::freeBuffers()\n> > >       bufferAllocator_->free(stream);\n> > >  }\n> > >\n> > > -FileDescriptor V4L2Camera::getBufferFd(unsigned int index)\n> > > +int V4L2Camera::getBufferFd(unsigned int index)\n> > >  {\n> > >       Stream *stream = config_->at(0).stream();\n> > >       const std::vector<std::unique_ptr<FrameBuffer>> &buffers =\n> > >               bufferAllocator_->buffers(stream);\n> > >\n> > >       if (buffers.size() <= index)\n> > > -             return FileDescriptor();\n> > > +             return -1;\n> > >\n> > > -     return buffers[index]->planes()[0].fd;\n> > > +     return buffers[index]->planes()[0].fd.fd();\n> > >  }\n> > >\n> > >  int V4L2Camera::streamOn()\n> > > diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\n> > > index d2380462..8efe1642 100644\n> > > --- a/src/v4l2/v4l2_camera.h\n> > > +++ b/src/v4l2/v4l2_camera.h\n> > > @@ -53,7 +53,7 @@ public:\n> > >\n> > >       int allocBuffers(unsigned int count);\n> > >       void freeBuffers();\n> > > -     FileDescriptor getBufferFd(unsigned int index);\n> > > +     int getBufferFd(unsigned int index);\n> > >\n> > >       int streamOn();\n> > >       int streamOff();\n> > > diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\n> > > index f8bfe595..8f417421 100644\n> > > --- a/src/v4l2/v4l2_camera_proxy.cpp\n> > > +++ b/src/v4l2/v4l2_camera_proxy.cpp\n> > > @@ -112,14 +112,14 @@ void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags,\n> > >               return MAP_FAILED;\n> > >       }\n> > >\n> > > -     FileDescriptor fd = vcam_->getBufferFd(index);\n> > > -     if (!fd.isValid()) {\n> > > +     int fd = vcam_->getBufferFd(index);\n> > > +     if (fd < 0) {\n> > >               errno = EINVAL;\n> > >               return MAP_FAILED;\n> > >       }\n> > >\n> > >       void *map = V4L2CompatManager::instance()->fops().mmap(addr, length, prot,\n> > > -                                                            flags, fd.fd(), 0);\n> > > +                                                            flags, fd, 0);\n> > >       if (map == MAP_FAILED)\n> > >               return map;\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 2C27FC321D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 28 Jun 2021 03:03:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3F214684D7;\n\tMon, 28 Jun 2021 05:03:53 +0200 (CEST)","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 AD3116028C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Jun 2021 05:03:51 +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 25F4557E;\n\tMon, 28 Jun 2021 05:03:51 +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=\"X4xqNgZ2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1624849431;\n\tbh=pxswadTq1f+2IgyUTI/2mypO3CdQmwfbUtfwcPJ3g44=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=X4xqNgZ2mo3/EEH1FbiXxG/RJ4cbEeu4pRLCwLXOWV9nW+6n4b4v4xfZB28fvSk0Z\n\tTGQB1euTk8eKXho841Mhfupk2An/rjwyMeqfSYIEdghRRQN7aSIn7SVs8V96hjtWbH\n\tUcZam5gFg0IcFtsl+qCLAdSdfHy1v/gI0Eu5VATU=","Date":"Mon, 28 Jun 2021 06:03:50 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YNk8FhZDa7Ttvg1c@pendragon.ideasonboard.com>","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-10-hiroh@chromium.org>\n\t<YL0aWWf8gSNMH4j5@pendragon.ideasonboard.com>\n\t<CAO5uPHNfnXTiV-Ysf7DMO=m3GX=ORaeLdVdpK9KU7RV40yPMfw@mail.gmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<CAO5uPHNfnXTiV-Ysf7DMO=m3GX=ORaeLdVdpK9KU7RV40yPMfw@mail.gmail.com>","Subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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>"}},{"id":17905,"web_url":"https://patchwork.libcamera.org/comment/17905/","msgid":"<CAO5uPHO3QX-peDvfPCtx5w3e4YC8X-Mvr+jMSyRqH2BgGck2kw@mail.gmail.com>","date":"2021-06-28T21:11:14","subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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 28, 2021 at 12:03 PM Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n>\n> Hi Hiro,\n>\n> On Mon, Jun 07, 2021 at 06:22:21PM +0900, Hirokazu Honda wrote:\n> > On Mon, Jun 7, 2021 at 3:56 AM Laurent Pinchart wrote:\n> > > On Thu, Apr 15, 2021 at 05:38:43PM +0900, Hirokazu Honda wrote:\n> > > > V4L2Camera::getBufferFd() returns FileDescriptor. However, the\n> > > > file descriptor is still owned by V4L2Camera. It should rather\n> > > > return an integer to represent V4L2Camera doesn't have the\n> > > > ownership of the file descriptor.\n> > >\n> > > The FileDescriptor class doesn't imply ownership, as it's similar to a\n> > > shared_ptr<>. I'm not against this patch, but why do you think\n> > > FileDescriptor is bad here ?\n> >\n> > IMO, returning FileDescriptor implies that a caller shares the ownership of it.\n> > It enables a caller to keep it outlive a callee and even share its\n> > ownership with others.\n> > What do you think?\n>\n> Generally speaking, it may be dangerous to handle file descriptors as\n> integers, due not only to the risk of leakage, but also to the risk of\n> use-after-free (or rather use-after-close in this case). FileDescriptor\n> prevents that.\n>\n> On the other hand, I agree with you that in this case the caller isn't\n> meant to use the file descriptor in any way that would outlive the\n> descriptor stored in V4L2Camera. If it was a generic API, I'd prefer\n> keeping FileDescriptor, but as V4L2Camera and V4L2CameraProxy are\n> tightly coupled, it really doesn't matter much. I'm OK with the patch if\n> you think it's better.\n>\n\nI would like to keep my idea as the current getBufferFd usage intends\nto not share the ownership.\nIf the usage needs to be changed, I think it will be better to use\nFileDescriptor at that time.\n\nThanks,\n-Hiro\n> > > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > > > ---\n> > > >  src/v4l2/v4l2_camera.cpp       | 6 +++---\n> > > >  src/v4l2/v4l2_camera.h         | 2 +-\n> > > >  src/v4l2/v4l2_camera_proxy.cpp | 6 +++---\n> > > >  3 files changed, 7 insertions(+), 7 deletions(-)\n> > > >\n> > > > diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\n> > > > index 97825c71..cccc6749 100644\n> > > > --- a/src/v4l2/v4l2_camera.cpp\n> > > > +++ b/src/v4l2/v4l2_camera.cpp\n> > > > @@ -186,16 +186,16 @@ void V4L2Camera::freeBuffers()\n> > > >       bufferAllocator_->free(stream);\n> > > >  }\n> > > >\n> > > > -FileDescriptor V4L2Camera::getBufferFd(unsigned int index)\n> > > > +int V4L2Camera::getBufferFd(unsigned int index)\n> > > >  {\n> > > >       Stream *stream = config_->at(0).stream();\n> > > >       const std::vector<std::unique_ptr<FrameBuffer>> &buffers =\n> > > >               bufferAllocator_->buffers(stream);\n> > > >\n> > > >       if (buffers.size() <= index)\n> > > > -             return FileDescriptor();\n> > > > +             return -1;\n> > > >\n> > > > -     return buffers[index]->planes()[0].fd;\n> > > > +     return buffers[index]->planes()[0].fd.fd();\n> > > >  }\n> > > >\n> > > >  int V4L2Camera::streamOn()\n> > > > diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\n> > > > index d2380462..8efe1642 100644\n> > > > --- a/src/v4l2/v4l2_camera.h\n> > > > +++ b/src/v4l2/v4l2_camera.h\n> > > > @@ -53,7 +53,7 @@ public:\n> > > >\n> > > >       int allocBuffers(unsigned int count);\n> > > >       void freeBuffers();\n> > > > -     FileDescriptor getBufferFd(unsigned int index);\n> > > > +     int getBufferFd(unsigned int index);\n> > > >\n> > > >       int streamOn();\n> > > >       int streamOff();\n> > > > diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\n> > > > index f8bfe595..8f417421 100644\n> > > > --- a/src/v4l2/v4l2_camera_proxy.cpp\n> > > > +++ b/src/v4l2/v4l2_camera_proxy.cpp\n> > > > @@ -112,14 +112,14 @@ void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags,\n> > > >               return MAP_FAILED;\n> > > >       }\n> > > >\n> > > > -     FileDescriptor fd = vcam_->getBufferFd(index);\n> > > > -     if (!fd.isValid()) {\n> > > > +     int fd = vcam_->getBufferFd(index);\n> > > > +     if (fd < 0) {\n> > > >               errno = EINVAL;\n> > > >               return MAP_FAILED;\n> > > >       }\n> > > >\n> > > >       void *map = V4L2CompatManager::instance()->fops().mmap(addr, length, prot,\n> > > > -                                                            flags, fd.fd(), 0);\n> > > > +                                                            flags, fd, 0);\n> > > >       if (map == MAP_FAILED)\n> > > >               return map;\n> > > >\n>\n> --\n> Regards,\n>\n> Laurent Pinchart","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 2F290C321F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 28 Jun 2021 21:11:27 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 704A2684D8;\n\tMon, 28 Jun 2021 23:11:26 +0200 (CEST)","from mail-ed1-x532.google.com (mail-ed1-x532.google.com\n\t[IPv6:2a00:1450:4864:20::532])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2382C6028C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Jun 2021 23:11:25 +0200 (CEST)","by mail-ed1-x532.google.com with SMTP id df12so28083621edb.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Jun 2021 14:11:25 -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=\"iL4dh+/t\"; 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=MNhap2Ro5teOqCFdq3wFz4jstnR+YkNax/vAlwTFoOE=;\n\tb=iL4dh+/tIuDKwwkjcnbaZtupD8oKMgOGNUGcY63TvPeLCxTcMZPGJCrgi5/7QAOCi1\n\t7yhCuXpmAQiItOMgIajwUPyhGpdKPtdFT37oomLvQ0X8Y1rg1ObVzH7Emin2y6QXUltS\n\ti526MsvYRbPCcc7nVVSMNXlTl3TOj+lujL5UM=","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=MNhap2Ro5teOqCFdq3wFz4jstnR+YkNax/vAlwTFoOE=;\n\tb=RszWlcked7fCY5k4MXMtRYFQrsI+IRmLOWsSjb0G27OLaxcqCZL0YqrDlflvuWuNcA\n\tG9MzXghx2u3nSVYj73sI5YAvTP7G01ET4wLnqma5vWgpGMqd45zlWZu5RlPgagG1/ye7\n\trdj/N4+FiijZW5qWQeRqNssgSDtq2AqKewM1V8f+gKOsJchbdaMTCHKVnxmYJEtMo+gA\n\tfLozuxl5kSwZyaUE+G2I35lKpG6Mkj0IwEE6X29a3HRzE6AQJ3rmTIgesbsYlPKxj2pc\n\tG6aRcjl0TkjK9SKkGmrpBka/ycmYBXQ+xyI0lvaAUnFQiOIvA/RR806qDOeba/6b3Xt7\n\tvKbA==","X-Gm-Message-State":"AOAM532qD7yOdHXI7EfBFrKzoCXpgtttTgIhwl0Amki5Dr6njYjhE7zr\n\tbRW4R52XwldnBVtRCpT4E7NWh1w3C+w5nx814AIBOg==","X-Google-Smtp-Source":"ABdhPJzWbAYZIDQA3Q0OonHG6QSCWLizkXiKAPepexi96JvEj/sma00OwUCWgaax/SL+LORnOMf+AXEt2P9bV3xAeGY=","X-Received":"by 2002:a05:6402:151:: with SMTP id\n\ts17mr35179118edu.233.1624914684773; \n\tMon, 28 Jun 2021 14:11:24 -0700 (PDT)","MIME-Version":"1.0","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-10-hiroh@chromium.org>\n\t<YL0aWWf8gSNMH4j5@pendragon.ideasonboard.com>\n\t<CAO5uPHNfnXTiV-Ysf7DMO=m3GX=ORaeLdVdpK9KU7RV40yPMfw@mail.gmail.com>\n\t<YNk8FhZDa7Ttvg1c@pendragon.ideasonboard.com>","In-Reply-To":"<YNk8FhZDa7Ttvg1c@pendragon.ideasonboard.com>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Tue, 29 Jun 2021 06:11:14 +0900","Message-ID":"<CAO5uPHO3QX-peDvfPCtx5w3e4YC8X-Mvr+jMSyRqH2BgGck2kw@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [RFC PATCH 10/10] v4l2: V4L2Camera: Return\n\tint in getBufferFd()","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>"}}]