[{"id":11824,"web_url":"https://patchwork.libcamera.org/comment/11824/","msgid":"<20200804114424.GC6075@pendragon.ideasonboard.com>","date":"2020-08-04T11:44:24","subject":"Re: [libcamera-devel] [PATCH v6 3/9] libcamera: v4l2_device: Add\n\tmethod to lookup device path","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Mon, Aug 03, 2020 at 11:17:27PM +0200, Niklas Söderlund wrote:\n> Add a method to lookup a V4L2 devices path in sysfs.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n> * Changes since v5\n> - Expand documentation\n> - Use DeviceEnumerator interface to lookup device path\n> - Include stdlib.h\n> - Use realpath(..., nullptr); ...; free();\n> \n> * Changes since v3\n> - s/the device path/the device path in sysfs/\n> ---\n>  include/libcamera/internal/v4l2_device.h |  1 +\n>  src/libcamera/v4l2_device.cpp            | 27 ++++++++++++++++++++++++\n>  2 files changed, 28 insertions(+)\n> \n> diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\n> index bf643f2ec966bb33..3b605aab343b3b94 100644\n> --- a/include/libcamera/internal/v4l2_device.h\n> +++ b/include/libcamera/internal/v4l2_device.h\n> @@ -30,6 +30,7 @@ public:\n>  \tint setControls(ControlList *ctrls);\n>  \n>  \tconst std::string &deviceNode() const { return deviceNode_; }\n> +\tstd::string devicePath() const;\n>  \n>  protected:\n>  \tV4L2Device(const std::string &deviceNode);\n> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> index 56ea1ddda2c1425f..ef601d8e7e04b32a 100644\n> --- a/src/libcamera/v4l2_device.cpp\n> +++ b/src/libcamera/v4l2_device.cpp\n> @@ -9,11 +9,14 @@\n>  \n>  #include <fcntl.h>\n>  #include <iomanip>\n> +#include <limits.h>\n> +#include <stdlib.h>\n>  #include <string.h>\n>  #include <sys/ioctl.h>\n>  #include <sys/syscall.h>\n>  #include <unistd.h>\n>  \n> +#include \"libcamera/internal/device_enumerator.h\"\n>  #include \"libcamera/internal/log.h\"\n>  #include \"libcamera/internal/utils.h\"\n>  #include \"libcamera/internal/v4l2_controls.h\"\n> @@ -350,6 +353,30 @@ int V4L2Device::setControls(ControlList *ctrls)\n>  \treturn ret;\n>  }\n>  \n> +/**\n> + * \\brief Retrieve the device path in sysfs\n> + *\n> + * This function returns the sysfs path to the physical device backing the V4L2\n> + * device. The path is guaranteed to be an absolute path, without any symbolic\n> + * link.\n> + *\n> + * \\return The device path in sysfs\n> + */\n> +std::string V4L2Device::devicePath() const\n> +{\n> +\tstd::unique_ptr<DeviceEnumerator> de = DeviceEnumerator::create();\n\nCreating a new device enumerator just for this is a bit expensive :-( If\nyou agree with my suggestion in 1/9, I think we can ditch the device\nenumerator. Otherwise it would be best to retrieve the instance created\nby the camera manager.\n\n> +\tstd::string devicePath = de->lookupSysfsPath(deviceNode_) + \"/device\";\n> +\n> +\tchar *realPath = realpath(devicePath.c_str(), nullptr);\n> +\tif (!realPath)\n> +\t\tLOG(V4L2, Fatal) << \"Can not resolve path for \" << devicePath;\n> +\n> +\tstd::string path{ realPath };\n\nstd::string::string(nullptr) has undefined behaviour. You should return\n{} in the error case above.\n\n> +\tfree(realPath);\n> +\n> +\treturn path;\n> +}\n> +\n>  /**\n>   * \\brief Perform an IOCTL system call on the device node\n>   * \\param[in] request The IOCTL request code","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 6DC7BBD87A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  4 Aug 2020 11:44:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3CC166048E;\n\tTue,  4 Aug 2020 13:44:45 +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 03DD9603D9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  4 Aug 2020 13:44:44 +0200 (CEST)","from pendragon.ideasonboard.com (85-76-79-50-nat.elisa-mobile.fi\n\t[85.76.79.50])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5D8DF277;\n\tTue,  4 Aug 2020 13:44:40 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"mHnnU+TR\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1596541483;\n\tbh=IRUrg6bm24ySXgYaP3oPB+47RgpmBUs1Jt2TkOoe374=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=mHnnU+TRCgZNe+npRsQKFogIrQwBmQ3RJ0HJYTNw1Cz2Q7n1PYN7rtjGE1BwDgMWF\n\tL+wgscGuEq2goOjbKYhW/SSjoihSZwl4T4fM9O/13sw8Va501zC0uiVBfzfSLTjHHw\n\tYSVfnPSc6Im7MqX2XzwXVBFgtZl1f5cjK9t+efwM=","Date":"Tue, 4 Aug 2020 14:44:24 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Message-ID":"<20200804114424.GC6075@pendragon.ideasonboard.com>","References":"<20200803211733.1037019-1-niklas.soderlund@ragnatech.se>\n\t<20200803211733.1037019-4-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200803211733.1037019-4-niklas.soderlund@ragnatech.se>","Subject":"Re: [libcamera-devel] [PATCH v6 3/9] libcamera: v4l2_device: Add\n\tmethod to lookup device path","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","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]