X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fnetwork%2Fio.c;h=ef07124f740e2ec781a404c72b0b2f80b1550d03;hb=7faec405d0b22f43e729e4fecac1302cbbe03015;hp=313e0e8a9494ce83aa46d4ca5ba638d060e0832c;hpb=30ceb37fe8d1cacb6f8fac51f9c1a2a58d324ca0;p=vlc diff --git a/src/network/io.c b/src/network/io.c index 313e0e8a94..ef07124f74 100644 --- a/src/network/io.c +++ b/src/network/io.c @@ -96,15 +96,29 @@ int net_SetupSocket (int fd) int net_Socket (vlc_object_t *p_this, int family, int socktype, int protocol) { - int fd = socket (family, socktype, protocol); - if (fd == -1) + int fd; + +#ifdef SOCK_CLOEXEC + fd = socket (family, socktype | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol); + if (fd == -1 && errno == EINVAL) +#endif { - if (net_errno != EAFNOSUPPORT) - msg_Err (p_this, "cannot create socket: %m"); - return -1; + fd = socket (family, socktype, protocol); + if (fd == -1) + { + if (net_errno != EAFNOSUPPORT) + msg_Err (p_this, "cannot create socket: %m"); + return -1; + } +#ifndef WIN32 + fcntl (fd, F_SETFD, FD_CLOEXEC); + fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK); +#else + ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 }); +#endif } - net_SetupSocket (fd); + setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int)); #ifdef IPV6_V6ONLY /* @@ -508,7 +522,7 @@ char *__net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs ) if( i_line == i_max ) { i_max += 1024; - psz_line = realloc( psz_line, i_max ); + psz_line = xrealloc( psz_line, i_max ); ptr = psz_line + i_line; }