]> git.sesse.net Git - vlc/commitdiff
vlc_accept: accept() with close-on-exec
authorRémi Denis-Courmont <remi@remlab.net>
Wed, 31 Mar 2010 16:23:47 +0000 (19:23 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Wed, 31 Mar 2010 16:23:47 +0000 (19:23 +0300)
include/vlc_fs.h
src/libvlccore.sym
src/text/filesystem.c

index 9ff820096555d9e5d790d0c8089cd7c54e35d803..5bdd10ce0d32153c05530a5d6e5e4648b50def47 100644 (file)
@@ -54,6 +54,9 @@ 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);
+int vlc_socket (int, int, int, bool nonblock) LIBVLC_USED;
+
+struct sockaddr;
+VLC_EXPORT( int, vlc_accept, ( int, struct sockaddr *, socklen_t *, bool ) LIBVLC_USED );
 
 #endif
index 3ab5bf44a8fa4b9c34c8cfe844f63a50869d1946..e618f92d6073999825604065f4769a5a1baa807e 100644 (file)
@@ -445,6 +445,7 @@ vlc_stat
 vlc_unlink
 vlc_rename
 vlc_dup
+vlc_accept
 utf8_vfprintf
 var_AddCallback
 var_Change
index a0d10502337327e4b396cffafda6e9e1d24fa57d..1025238bc0ee06eb6842f8a694e6124d255eaea2 100644 (file)
@@ -655,3 +655,45 @@ int vlc_socket (int pf, int type, int proto, bool nonblock)
 #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)
+            return fd;
+    }
+    while (errno == EINTR);
+
+    return -1;
+}