[vlc-commits] commit: vlc_socket: create socket with close-on-exec à la vlc_dup () ( Rémi Denis-Courmont )
git at videolan.org
git at videolan.org
Wed Mar 31 18:29:50 CEST 2010
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Mar 31 19:07:47 2010 +0300| [3eb3e72a3149bbbf328ce2baa382b65102d72f7b] | committer: Rémi Denis-Courmont
vlc_socket: create socket with close-on-exec à la vlc_dup()
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=3eb3e72a3149bbbf328ce2baa382b65102d72f7b
---
include/vlc_fs.h | 1 +
src/text/filesystem.c | 46 +++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/include/vlc_fs.h b/include/vlc_fs.h
index 3486f0a..9ff8200 100644
--- a/include/vlc_fs.h
+++ b/include/vlc_fs.h
@@ -54,5 +54,6 @@ VLC_EXPORT( int, vlc_lstat, ( const char *filename, struct stat *buf ) );
VLC_EXPORT( int, vlc_mkstemp, ( char * ) );
VLC_EXPORT( int, vlc_dup, ( int ) );
+int vlc_socket (int, int, int, bool nonblock);
#endif
diff --git a/src/text/filesystem.c b/src/text/filesystem.c
index b57a27d..a0d1050 100644
--- a/src/text/filesystem.c
+++ b/src/text/filesystem.c
@@ -42,9 +42,6 @@
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif
-#ifdef UNDER_CE
-# include <tchar.h>
-#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
@@ -53,11 +50,15 @@
#endif
#ifdef WIN32
# include <io.h>
+# include <winsock2.h>
# ifndef UNDER_CE
# include <direct.h>
+# else
+# include <tchar.h>
# endif
#else
# include <unistd.h>
+# include <sys/socket.h>
#endif
#ifndef HAVE_LSTAT
@@ -615,3 +616,42 @@ int vlc_dup (int oldfd)
#endif
return newfd;
}
+
+/**
+ * Creates a socket file descriptor. The new file descriptor has the
+ * close-on-exec flag set.
+ * @param pf protocol family
+ * @param type socket type
+ * @param proto network protocol
+ * @param nonblock true to create a non-blocking socket
+ * @return a new file descriptor or -1
+ */
+int vlc_socket (int pf, int type, int proto, bool nonblock)
+{
+ int fd;
+
+#ifdef SOCK_CLOEXEC
+ type |= SOCK_CLOEXEC;
+ if (nonblock)
+ type |= SOCK_NONBLOCK;
+ fd = socket (pf, type | SOCK_NONBLOCK | SOCK_CLOEXEC, proto);
+ if (fd != -1 || errno != EINVAL)
+ return fd;
+
+ type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
+#endif
+
+ fd = socket (pf, type, proto);
+ if (fd == -1)
+ return -1;
+
+#ifndef WIN32
+ fcntl (fd, F_SETFD, FD_CLOEXEC);
+ if (nonblock)
+ fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
+#else
+ if (nonblock)
+ ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
+#endif
+ return fd;
+}
More information about the vlc-commits
mailing list