]> git.sesse.net Git - vlc/blobdiff - src/text/filesystem.c
net: fix socket blocking if accept4 not available
[vlc] / src / text / filesystem.c
index 6ebfc39a138c4cedfbaef7b250d0f3da34d86938..e230f058c10178b8a45f107e50bf716c1d71d4dd 100644 (file)
@@ -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
 #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
@@ -193,6 +194,53 @@ FILE *vlc_fopen (const char *filename, const char *mode)
     return stream;
 }
 
+/**
+ * Opens a system file handle relative to an existing directory handle.
+ *
+ * @param dir directory file descriptor
+ * @param filename file path to open (with UTF-8 encoding)
+ * @param flags open() flags, see the C library open() documentation
+ * @return a file handle on success, -1 on error (see errno).
+ * @note Contrary to standard open(), this function returns file handles
+ * with the close-on-exec flag enabled.
+ */
+int vlc_openat (int dir, const char *filename, int flags, ...)
+{
+    unsigned int mode = 0;
+    va_list ap;
+
+    va_start (ap, flags);
+    if (flags & O_CREAT)
+        mode = va_arg (ap, unsigned int);
+    va_end (ap);
+
+#ifdef O_CLOEXEC
+    flags |= O_CLOEXEC;
+#endif
+
+    const char *local_name = ToLocale (filename);
+    if (local_name == NULL)
+    {
+        errno = ENOENT;
+        return -1;
+    }
+
+#ifdef HAVE_FDOPENDIR
+    int fd = openat (dir, local_name, flags, mode);
+# ifdef HAVE_FCNTL
+    if (fd != -1)
+        fcntl (fd, F_SETFD, FD_CLOEXEC);
+# endif
+#else
+    int fd = -1;
+    errno = ENOSYS;
+#endif
+
+    LocaleFree (local_name);
+    return fd;
+}
+
+
 /**
  * Creates a directory using UTF-8 paths.
  *
@@ -417,7 +465,7 @@ int vlc_stat( const char *filename, struct stat *buf)
  *
  * @param filename UTF-8 file path
  */
-int utf8_lstat( const char *filename, struct stat *buf)
+int vlc_lstat( const char *filename, struct stat *buf)
 {
     return vlc_statEx( filename, buf, false );
 }
@@ -535,6 +583,10 @@ int vlc_mkstemp( char *template )
     return -1;
 }
 
+#ifdef UNDER_CE
+# define dup(fd) (fd, -1)
+#endif
+
 /**
  * Duplicates a file descriptor. The new file descriptor has the close-on-exec
  * descriptor flag set.
@@ -564,3 +616,96 @@ int vlc_dup (int oldfd)
 #endif
     return newfd;
 }
+
+#include <vlc_network.h>
+
+/**
+ * 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, 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;
+}
+
+/**
+ * Accepts an inbound connection request on a listening socket.
+ * The new file descriptor has the close-on-exec flag set.
+ * @param lfd listening socket file descriptor
+ * @param addr pointer to the peer address or NULL [OUT]
+ * @param alen pointer to the length of the peer address or NULL [OUT]
+ * @param nonblock whether to put the new socket in non-blocking mode
+ * @return a new file descriptor, or -1 on error.
+ */
+int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
+{
+#ifdef HAVE_ACCEPT4
+    int flags = SOCK_CLOEXEC;
+    if (nonblock)
+        flags |= SOCK_NONBLOCK;
+
+    do
+    {
+        int fd = accept4 (lfd, addr, alen, flags);
+        if (fd != -1)
+            return fd;
+    }
+    while (errno == EINTR);
+
+    if (errno != ENOSYS)
+        return -1;
+#endif
+#ifdef WIN32
+    errno = 0;
+#endif
+
+    do
+    {
+        int fd = accept (lfd, addr, alen);
+        if (fd != -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;
+        }
+    }
+    while (errno == EINTR);
+
+    return -1;
+}