{"id":2515,"url":"https://patchwork.libcamera.org/api/patches/2515/?format=json","web_url":"https://patchwork.libcamera.org/patch/2515/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200106161417.19150-2-laurent.pinchart@ideasonboard.com>","date":"2020-01-06T16:14:14","name":"[libcamera-devel,1/4] v4l2: compat_manager: Move file operations to new struct FileOperations","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"dfdacc79fbf37c2aa2d9a9a2a39908ae0d814fd0","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2515/mbox/","series":[{"id":604,"url":"https://patchwork.libcamera.org/api/series/604/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=604","date":"2020-01-06T16:14:13","name":"v4l2: Miscellaneous improvements","version":1,"mbox":"https://patchwork.libcamera.org/series/604/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2515/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2515/checks/","tags":{},"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 61B3460463\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Jan 2020 17:14:31 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E584D123D;\n\tMon,  6 Jan 2020 17:14:30 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1578327271;\n\tbh=h/KRYq2jcOQc5RNLGBGWmui5ap4uEpheS/JEK0VTal0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=DUMTRgkb6aiPHtZTbEBWrzC+Q2cVy4nuGZRRqjc0SVOe/AwigNwSiOyHVJuZVg9FP\n\t9XuvfmfXCYx2LpII3wYJavMlmYm8hlDOKdJ/Vk6F3KIOIKRDGHN6opxNY6nUbbkoi2\n\tXEItfMEabynucGl1Gr3XzOjTKqI82AWOjUPJMbg8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon,  6 Jan 2020 18:14:14 +0200","Message-Id":"<20200106161417.19150-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200106161417.19150-1-laurent.pinchart@ideasonboard.com>","References":"<20200106161417.19150-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 1/4] v4l2: compat_manager: Move file\n\toperations to new struct FileOperations","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, 06 Jan 2020 16:14:31 -0000"},"content":"Create a new FileOperations structure to hold all the dynamically-loaded\nC library file operations. The file operations are now exposed publicly,\nto prepare for usage of mmap in the V4L2CameraProxy.\n\nA new template helper function is added to retrieve a symbol with\ndlsym() with proper casting to simplify the V4L2CompatManager\nconstructor.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/v4l2/v4l2_compat_manager.cpp | 34 ++++++++++++++++++++------------\n src/v4l2/v4l2_compat_manager.h   | 34 +++++++++++++++++++-------------\n 2 files changed, 41 insertions(+), 27 deletions(-)","diff":"diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp\nindex a3d693745dd6..fe453445a9fb 100644\n--- a/src/v4l2/v4l2_compat_manager.cpp\n+++ b/src/v4l2/v4l2_compat_manager.cpp\n@@ -28,15 +28,23 @@ using namespace libcamera;\n \n LOG_DEFINE_CATEGORY(V4L2Compat)\n \n+namespace {\n+template<typename T>\n+void get_symbol(T &func, const char *name)\n+{\n+\tfunc = reinterpret_cast<T>(dlsym(RTLD_NEXT, name));\n+}\n+} /* namespace */\n+\n V4L2CompatManager::V4L2CompatManager()\n \t: cm_(nullptr), initialized_(false)\n {\n-\topenat_func_ = (openat_func_t)dlsym(RTLD_NEXT, \"openat\");\n-\tdup_func_    = (dup_func_t   )dlsym(RTLD_NEXT, \"dup\");\n-\tclose_func_  = (close_func_t )dlsym(RTLD_NEXT, \"close\");\n-\tioctl_func_  = (ioctl_func_t )dlsym(RTLD_NEXT, \"ioctl\");\n-\tmmap_func_   = (mmap_func_t  )dlsym(RTLD_NEXT, \"mmap\");\n-\tmunmap_func_ = (munmap_func_t)dlsym(RTLD_NEXT, \"munmap\");\n+\tget_symbol(fops_.openat, \"openat\");\n+\tget_symbol(fops_.dup, \"dup\");\n+\tget_symbol(fops_.close, \"close\");\n+\tget_symbol(fops_.ioctl, \"ioctl\");\n+\tget_symbol(fops_.mmap, \"mmap\");\n+\tget_symbol(fops_.munmap, \"munmap\");\n }\n \n V4L2CompatManager::~V4L2CompatManager()\n@@ -141,7 +149,7 @@ int V4L2CompatManager::getCameraIndex(int fd)\n \n int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mode)\n {\n-\tint fd = openat_func_(dirfd, path, oflag, mode);\n+\tint fd = fops_.openat(dirfd, path, oflag, mode);\n \tif (fd < 0)\n \t\treturn fd;\n \n@@ -160,7 +168,7 @@ int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mod\n \t\treturn fd;\n \t}\n \n-\tclose_func_(fd);\n+\tfops_.close(fd);\n \n \tunsigned int camera_index = static_cast<unsigned int>(ret);\n \n@@ -182,7 +190,7 @@ int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mod\n \n int V4L2CompatManager::dup(int oldfd)\n {\n-\tint newfd = dup_func_(oldfd);\n+\tint newfd = fops_.dup(oldfd);\n \tif (newfd < 0)\n \t\treturn newfd;\n \n@@ -205,7 +213,7 @@ int V4L2CompatManager::close(int fd)\n \t\treturn 0;\n \t}\n \n-\treturn close_func_(fd);\n+\treturn fops_.close(fd);\n }\n \n void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags,\n@@ -213,7 +221,7 @@ void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags,\n {\n \tV4L2CameraProxy *proxy = getProxy(fd);\n \tif (!proxy)\n-\t\treturn mmap_func_(addr, length, prot, flags, fd, offset);\n+\t\treturn fops_.mmap(addr, length, prot, flags, fd, offset);\n \n \tvoid *map = proxy->mmap(length, prot, flags, offset);\n \tif (map == MAP_FAILED)\n@@ -227,7 +235,7 @@ int V4L2CompatManager::munmap(void *addr, size_t length)\n {\n \tauto device = mmaps_.find(addr);\n \tif (device == mmaps_.end())\n-\t\treturn munmap_func_(addr, length);\n+\t\treturn fops_.munmap(addr, length);\n \n \tV4L2CameraProxy *proxy = device->second;\n \n@@ -244,7 +252,7 @@ int V4L2CompatManager::ioctl(int fd, unsigned long request, void *arg)\n {\n \tV4L2CameraProxy *proxy = getProxy(fd);\n \tif (!proxy)\n-\t\treturn ioctl_func_(fd, request, arg);\n+\t\treturn fops_.ioctl(fd, request, arg);\n \n \treturn proxy->ioctl(request, arg);\n }\ndiff --git a/src/v4l2/v4l2_compat_manager.h b/src/v4l2/v4l2_compat_manager.h\nindex dd9d321078e4..d51b5953d930 100644\n--- a/src/v4l2/v4l2_compat_manager.h\n+++ b/src/v4l2/v4l2_compat_manager.h\n@@ -26,11 +26,30 @@ using namespace libcamera;\n class V4L2CompatManager : public Thread\n {\n public:\n+\tstruct FileOperations {\n+\t\tusing openat_func_t = int (*)(int dirfd, const char *path,\n+\t\t\t\t\t      int oflag, ...);\n+\t\tusing dup_func_t = int (*)(int oldfd);\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\tusing munmap_func_t = int (*)(void *addr, size_t length);\n+\n+\t\topenat_func_t openat;\n+\t\tdup_func_t dup;\n+\t\tclose_func_t close;\n+\t\tioctl_func_t ioctl;\n+\t\tmmap_func_t mmap;\n+\t\tmunmap_func_t munmap;\n+\t};\n+\n \tstatic V4L2CompatManager *instance();\n \n \tint init();\n \n \tV4L2CameraProxy *getProxy(int fd);\n+\tconst FileOperations &fops() const { return fops_; }\n \n \tint openat(int dirfd, const char *path, int oflag, mode_t mode);\n \n@@ -48,20 +67,7 @@ private:\n \tvoid run() override;\n \tint getCameraIndex(int fd);\n \n-\ttypedef int (*openat_func_t)(int dirfd, const char *path, int oflag, ...);\n-\ttypedef int (*dup_func_t)(int oldfd);\n-\ttypedef int (*close_func_t)(int fd);\n-\ttypedef int (*ioctl_func_t)(int fd, unsigned long request, ...);\n-\ttypedef void *(*mmap_func_t)(void *addr, size_t length, int prot,\n-\t\t\t\t     int flags, int fd, off_t offset);\n-\ttypedef int (*munmap_func_t)(void *addr, size_t length);\n-\n-\topenat_func_t openat_func_;\n-\tdup_func_t    dup_func_;\n-\tclose_func_t  close_func_;\n-\tioctl_func_t  ioctl_func_;\n-\tmmap_func_t   mmap_func_;\n-\tmunmap_func_t munmap_func_;\n+\tFileOperations fops_;\n \n \tCameraManager *cm_;\n \n","prefixes":["libcamera-devel","1/4"]}