[vlc-devel] commit: vlc_dup: dup with close-on-exec ( Rémi Denis-Courmont )

git version control git at videolan.org
Sun Feb 7 14:54:45 CET 2010


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Feb  7 15:51:35 2010 +0200| [71a1c8a68f31206cf725563e07c225fed8e5727a] | committer: Rémi Denis-Courmont 

vlc_dup: dup with close-on-exec

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

 configure.ac          |    2 +-
 include/vlc_fs.h      |    2 ++
 src/libvlccore.sym    |    1 +
 src/text/filesystem.c |   29 +++++++++++++++++++++++++++++
 4 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/configure.ac b/configure.ac
index f3221bc..9c9d9a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -530,7 +530,7 @@ AC_CHECK_FUNCS(fdatasync,,
 ])
 
 dnl Check for non-standard system calls
-AC_CHECK_FUNCS([accept4 eventfd fstatfs vmsplice])
+AC_CHECK_FUNCS([accept4 dup3 eventfd fstatfs vmsplice])
 
 AH_BOTTOM([#include <vlc_fixups.h>])
 
diff --git a/include/vlc_fs.h b/include/vlc_fs.h
index 9f03b1b..51bfd60 100644
--- a/include/vlc_fs.h
+++ b/include/vlc_fs.h
@@ -53,4 +53,6 @@ VLC_EXPORT( int, utf8_lstat, ( const char *filename, struct stat *buf ) );
 
 VLC_EXPORT( int, vlc_mkstemp, ( char * ) );
 
+VLC_EXPORT( int, vlc_dup, ( int ) );
+
 #endif
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 6446679..d608ab3 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -440,6 +440,7 @@ vlc_readdir
 vlc_scandir
 vlc_stat
 vlc_unlink
+vlc_dup
 utf8_vfprintf
 var_AddCallback
 var_Change
diff --git a/src/text/filesystem.c b/src/text/filesystem.c
index fdb25fb..6ebfc39 100644
--- a/src/text/filesystem.c
+++ b/src/text/filesystem.c
@@ -535,3 +535,32 @@ int vlc_mkstemp( char *template )
     return -1;
 }
 
+/**
+ * Duplicates a file descriptor. The new file descriptor has the close-on-exec
+ * descriptor flag set.
+ * @return a new file descriptor or -1
+ */
+int vlc_dup (int oldfd)
+{
+    int newfd;
+
+#ifdef HAVE_DUP3
+    /* Unfortunately, dup3() works like dup2(), not like plain dup(). So we
+     * need such contortion to find the new file descriptor while preserving
+     * thread safety of the file descriptor table. */
+    newfd = vlc_open ("/dev/null", O_RDONLY);
+    if (likely(newfd != -1))
+    {
+        if (likely(dup3 (oldfd, newfd, O_CLOEXEC) == newfd))
+            return newfd;
+        close (newfd);
+    }
+#endif
+
+    newfd = dup (oldfd);
+#ifdef HAVE_FCNTL
+    if (likely(newfd != -1))
+        fcntl (newfd, F_SETFD, FD_CLOEXEC);
+#endif
+    return newfd;
+}




More information about the vlc-devel mailing list