[vlc-commits] v4l2: avoid using a constructor function

Rémi Denis-Courmont git at videolan.org
Sat Feb 3 12:12:54 CET 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Feb  3 13:10:16 2018 +0200| [a81d6c7c292754c0e4aff701e5e059432ff5606b] | committer: Rémi Denis-Courmont

v4l2: avoid using a constructor function

We do not really want to load libv4l2 or libmediaclient when the cache
generator describes the V4L2 plugin. We really want to load them when
we actually use the V4L2 plugin.

This incidentally avoids nested dynamic library loading (and works
around a leak in glibc).

This partially reverts ef46dc6ccc43002063d823eff7095b68f3476003.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=a81d6c7c292754c0e4aff701e5e059432ff5606b
---

 modules/access/v4l2/lib.c  | 18 ++++++++++++++----
 modules/access/v4l2/v4l2.h |  2 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/modules/access/v4l2/lib.c b/modules/access/v4l2/lib.c
index b212e1bc5f..dc46b1acfe 100644
--- a/modules/access/v4l2/lib.c
+++ b/modules/access/v4l2/lib.c
@@ -39,7 +39,7 @@ static int fd_open (int fd, int flags)
 
 static void *v4l2_handle = NULL;
 
-int (*v4l2_fd_open) (int, int) = fd_open;
+static int (*v4l2_fd_open_cb)(int, int) = fd_open;
 //int (*v4l2_open) (const char *, int, ...) = open;
 //int (*v4l2_dup) (const char *, int, ...) = dup;
 int (*v4l2_close) (int) = close;
@@ -49,7 +49,6 @@ ssize_t (*v4l2_read) (int, void *, size_t) = read;
 void * (*v4l2_mmap) (void *, size_t, int, int, int, int64_t) = mmap;
 int (*v4l2_munmap) (void *, size_t) = munmap;
 
-__attribute__((constructor))
 static void v4l2_lib_load (void)
 {
     void *h;
@@ -60,12 +59,15 @@ static void v4l2_lib_load (void)
     if (h == NULL)
         return;
 
-    void *sym;
+    void *sym = dlsym(h, "v4l2_fd_open");
+    if (sym != NULL)
+        v4l2_fd_open_cb = sym;
+
 #define SYM(name) \
     sym = dlsym (h, "v4l2_"#name); \
     if (sym != NULL) v4l2_##name = sym
 
-    SYM(fd_open); /*SYM(open); SYM(dup);*/ SYM(close); SYM(ioctl);
+    /*SYM(open); SYM(dup);*/ SYM(close); SYM(ioctl);
     SYM(read); /*SYM(write);*/ SYM(mmap); SYM(munmap);
 
     v4l2_handle = h;
@@ -77,3 +79,11 @@ static void v4l2_lib_unload (void)
     if (v4l2_handle != NULL)
         dlclose (v4l2_handle);
 }
+
+int v4l2_fd_open(int fd, int flags)
+{
+    static pthread_once_t once = PTHREAD_ONCE_INIT;
+
+    pthread_once(&once, v4l2_lib_load);
+    return v4l2_fd_open_cb(fd, flags);
+}
diff --git a/modules/access/v4l2/v4l2.h b/modules/access/v4l2/v4l2.h
index ac4562e269..d1458b8666 100644
--- a/modules/access/v4l2/v4l2.h
+++ b/modules/access/v4l2/v4l2.h
@@ -21,7 +21,7 @@
 #include <linux/videodev2.h>
 
 /* libv4l2 functions */
-extern int (*v4l2_fd_open) (int, int);
+extern int v4l2_fd_open(int, int);
 extern int (*v4l2_close) (int);
 extern int (*v4l2_ioctl) (int, unsigned long int, ...);
 extern ssize_t (*v4l2_read) (int, void *, size_t);



More information about the vlc-commits mailing list