[{"id":5225,"web_url":"https://patchwork.libcamera.org/comment/5225/","msgid":"<20200617002822.GM913@pendragon.ideasonboard.com>","date":"2020-06-17T00:28:22","subject":"Re: [libcamera-devel] [PATCH 1/2] v4l2: v4l2_compat: Intercept\n\topen64, openat64, and mmap64","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Tue, Jun 16, 2020 at 07:56:32PM +0900, Paul Elder wrote:\n> Some applications (eg. Firefox, Google Chrome, Skype) use import open64,\n\nuse or import ?\n\n> openat64, and mmap64 instead of their non-64 versions that we currently\n> intercept. Intercept these calls as well.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> ---\n>  src/v4l2/meson.build             |  1 +\n>  src/v4l2/v4l2_camera_proxy.cpp   |  2 +-\n>  src/v4l2/v4l2_camera_proxy.h     |  3 ++-\n>  src/v4l2/v4l2_compat.cpp         | 29 ++++++++++++++++++++++++++++-\n>  src/v4l2/v4l2_compat_manager.cpp |  2 +-\n>  src/v4l2/v4l2_compat_manager.h   |  5 +++--\n>  6 files changed, 36 insertions(+), 6 deletions(-)\n> \n> diff --git a/src/v4l2/meson.build b/src/v4l2/meson.build\n> index 0fb941e..f2e4aaf 100644\n> --- a/src/v4l2/meson.build\n> +++ b/src/v4l2/meson.build\n> @@ -15,6 +15,7 @@ v4l2_compat_cpp_args = [\n>      # file operations, disable transparent large file support.\n>      '-U_FILE_OFFSET_BITS',\n>      '-D_FILE_OFFSET_BITS=32',\n> +    '-D_LARGEFILE64_SOURCE',\n\nThis deserves an explanation in the commit message.\n\n>      '-fvisibility=hidden',\n>  ]\n>  \n> diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\n> index 17477ab..59af209 100644\n> --- a/src/v4l2/v4l2_camera_proxy.cpp\n> +++ b/src/v4l2/v4l2_camera_proxy.cpp\n> @@ -75,7 +75,7 @@ void V4L2CameraProxy::close()\n>  }\n>  \n>  void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags,\n> -\t\t\t    off_t offset)\n> +\t\t\t    __off64_t offset)\n>  {\n>  \tLOG(V4L2Compat, Debug) << \"Servicing mmap\";\n>  \n> diff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h\n> index 7c65c88..b5a9801 100644\n> --- a/src/v4l2/v4l2_camera_proxy.h\n> +++ b/src/v4l2/v4l2_camera_proxy.h\n> @@ -11,6 +11,7 @@\n>  #include <linux/videodev2.h>\n>  #include <map>\n>  #include <memory>\n> +#include <sys/mman.h>\n>  #include <sys/types.h>\n>  #include <vector>\n>  \n> @@ -28,7 +29,7 @@ public:\n>  \tint open(bool nonBlocking);\n>  \tvoid dup();\n>  \tvoid close();\n> -\tvoid *mmap(void *addr, size_t length, int prot, int flags, off_t offset);\n> +\tvoid *mmap(void *addr, size_t length, int prot, int flags, __off64_t offset);\n\nCan't you use off64_t ? Types starting with a double underscore are\ninternal to the libc implementation. While this will work with glibc and\nuclibc, musl doesn't provide __off64_t, but provides off64_t.\n\n>  \tint munmap(void *addr, size_t length);\n>  \n>  \tint ioctl(unsigned long request, void *arg);\n> diff --git a/src/v4l2/v4l2_compat.cpp b/src/v4l2/v4l2_compat.cpp\n> index 2a9a176..ead36e6 100644\n> --- a/src/v4l2/v4l2_compat.cpp\n> +++ b/src/v4l2/v4l2_compat.cpp\n> @@ -38,6 +38,16 @@ LIBCAMERA_PUBLIC int open(const char *path, int oflag, ...)\n>  \t\t\t\t\t\t     oflag, mode);\n>  }\n>  \n> +LIBCAMERA_PUBLIC int open64(const char *path, int oflag, ...)\n> +{\n> +\tmode_t mode = 0;\n> +\tif (oflag & O_CREAT || oflag & O_TMPFILE)\n> +\t\textract_va_arg(mode_t, mode, oflag);\n> +\n> +\treturn V4L2CompatManager::instance()->openat(AT_FDCWD, path,\n> +\t\t\t\t\t\t     oflag | O_LARGEFILE, mode);\n> +}\n> +\n>  /* _FORTIFY_SOURCE redirects open to __open_2 */\n>  LIBCAMERA_PUBLIC int __open_2(const char *path, int oflag)\n>  {\n> @@ -53,6 +63,16 @@ LIBCAMERA_PUBLIC int openat(int dirfd, const char *path, int oflag, ...)\n>  \treturn V4L2CompatManager::instance()->openat(dirfd, path, oflag, mode);\n>  }\n>  \n> +LIBCAMERA_PUBLIC int openat64(int dirfd, const char *path, int oflag, ...)\n> +{\n> +\tmode_t mode = 0;\n> +\tif (oflag & O_CREAT || oflag & O_TMPFILE)\n> +\t\textract_va_arg(mode_t, mode, oflag);\n> +\n> +\treturn V4L2CompatManager::instance()->openat(dirfd, path,\n> +\t\t\t\t\t\t     oflag | O_LARGEFILE, mode);\n> +}\n> +\n>  LIBCAMERA_PUBLIC int __openat_2(int dirfd, const char *path, int oflag)\n>  {\n>  \treturn openat(dirfd, path, oflag);\n> @@ -69,7 +89,14 @@ LIBCAMERA_PUBLIC int close(int fd)\n>  }\n>  \n>  LIBCAMERA_PUBLIC void *mmap(void *addr, size_t length, int prot, int flags,\n> -\t\t\t    int fd, off_t offset)\n> +\t\t\t    int fd, __off_t offset)\n> +{\n> +\treturn V4L2CompatManager::instance()->mmap(addr, length, prot, flags,\n> +\t\t\t\t\t\t   fd, offset);\n> +}\n> +\n> +LIBCAMERA_PUBLIC void *mmap64(void *addr, size_t length, int prot, int flags,\n> +\t\t\t      int fd, __off64_t offset)\n\nWith musl, when _LARGEFILE64_SOURCE is define, mmap64 is handled with\n\n#define mmap64 mmap\n#define off64_t off_t\n\nOther functions are handled in a similar way. You will thus have an\nissue here.\n\nThere doesn't seem to be an easy way to detect musl (as in #ifdef\n__MUSL__ for instance), but I think we can address this with\n\n#ifndef mmap64\nLIBCAMERA_PUBLIC void *mmap64(void *addr, size_t length, int prot, int flags,\n\t\t\t      int fd, __off64_t offset)\n{\n...\n}\n#endif\n\nas with glibc and uclibc mmap64 is a function, not a macro. Same for the\nother functions.\n\n>  {\n>  \treturn V4L2CompatManager::instance()->mmap(addr, length, prot, flags,\n>  \t\t\t\t\t\t   fd, offset);\n> diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp\n> index 56533c4..f928760 100644\n> --- a/src/v4l2/v4l2_compat_manager.cpp\n> +++ b/src/v4l2/v4l2_compat_manager.cpp\n> @@ -200,7 +200,7 @@ int V4L2CompatManager::close(int fd)\n>  }\n>  \n>  void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags,\n> -\t\t\t      int fd, off_t offset)\n> +\t\t\t      int fd, __off64_t offset)\n>  {\n>  \tV4L2CameraProxy *proxy = getProxy(fd);\n>  \tif (!proxy)\n> diff --git a/src/v4l2/v4l2_compat_manager.h b/src/v4l2/v4l2_compat_manager.h\n> index 14338a5..baf23ae 100644\n> --- a/src/v4l2/v4l2_compat_manager.h\n> +++ b/src/v4l2/v4l2_compat_manager.h\n> @@ -11,6 +11,7 @@\n>  #include <fcntl.h>\n>  #include <map>\n>  #include <memory>\n> +#include <sys/mman.h>\n>  #include <sys/types.h>\n>  #include <vector>\n>  \n> @@ -30,7 +31,7 @@ public:\n>  \t\tusing close_func_t = int (*)(int fd);\n>  \t\tusing ioctl_func_t = int (*)(int fd, unsigned long request, ...);\n>  \t\tusing mmap_func_t = void *(*)(void *addr, size_t length, int prot,\n> -\t\t\t\t\t      int flags, int fd, off_t offset);\n> +\t\t\t\t\t      int flags, int fd, __off64_t offset);\n>  \t\tusing munmap_func_t = int (*)(void *addr, size_t length);\n\nThe redirection is performed with\n\n        get_symbol(fops_.openat, \"openat\");\n        get_symbol(fops_.dup, \"dup\");\n        get_symbol(fops_.close, \"close\");\n        get_symbol(fops_.ioctl, \"ioctl\");\n        get_symbol(fops_.mmap, \"mmap\");\n        get_symbol(fops_.munmap, \"munmap\");\n\nDon't you thus redirect to 32-bit versions ? Shouldn't this become\nopenat64 and mmap64 ?\n\nThe uclibc implementation is very similar to glibc, but musl is quite\ndifferent. I would thus recommend at least compile-testing the series\nagainst musl. You can build a minimal musl-based rootfs with buildroot\nand use that to \"cross-compile\" libcamera (not sure if compiling for x86\non x86 technically counts as cross-compilation :-)) for musl.\n\n>  \n>  \t\topenat_func_t openat;\n> @@ -51,7 +52,7 @@ public:\n>  \tint dup(int oldfd);\n>  \tint close(int fd);\n>  \tvoid *mmap(void *addr, size_t length, int prot, int flags,\n> -\t\t   int fd, off_t offset);\n> +\t\t   int fd, __off64_t offset);\n>  \tint munmap(void *addr, size_t length);\n>  \tint ioctl(int fd, unsigned long request, void *arg);\n>","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 672E9603D8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 17 Jun 2020 02:28:45 +0200 (CEST)","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 C2D38F9;\n\tWed, 17 Jun 2020 02:28:44 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"hVLdZTkW\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1592353724;\n\tbh=UJmRcvjXmw7WINBGDoubn66rnyNZEkTlD1K0k2LAC5w=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=hVLdZTkWJtzfD/yzo1iKQOKhlBbM9pyQR9fbGTvycb6R9sJDwW9uPQ18pvuZOc/eH\n\ta5GteaYSQ0Y4rtP08cXg9QniXnWO9s2K5zLgJcKmPZutu+a+aYUNnfLo7Z7EllOzBy\n\ttWMIPdB9zgabzf+OrcEiBenopYdnCw2vzRoZrlnY=","Date":"Wed, 17 Jun 2020 03:28:22 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200617002822.GM913@pendragon.ideasonboard.com>","References":"<20200616105633.57717-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200616105633.57717-1-paul.elder@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 1/2] v4l2: v4l2_compat: Intercept\n\topen64, openat64, and mmap64","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, 17 Jun 2020 00:28:45 -0000"}}]