From 9d5ec50ead97e088d77317e77b18cef06cb3d053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= Date: Thu, 8 Aug 2013 22:26:42 +0300 Subject: [PATCH] ff_socket: put out-of-line and fallback to fcntl() for close-on-exec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This supports non-Linux systems (SOCK_CLOEXEC is non-standard) and older Linux kernels to the extent possible. Signed-off-by: Martin Storsjö --- libavformat/network.c | 19 +++++++++++++++++++ libavformat/network.h | 9 +-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/libavformat/network.c b/libavformat/network.c index 810a907212..6d308ebd01 100644 --- a/libavformat/network.c +++ b/libavformat/network.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include "network.h" #include "url.h" #include "libavcodec/internal.h" @@ -210,6 +211,24 @@ static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout, return ret; } +int ff_socket(int af, int type, int proto) +{ + int fd; + +#ifdef SOCK_CLOEXEC + fd = socket(af, type | SOCK_CLOEXEC, proto); + if (fd == -1 && errno == EINVAL) +#endif + { + fd = socket(af, type, proto); +#if HAVE_FCNTL + if (fd != -1) + fcntl(fd, F_SETFD, FD_CLOEXEC); +#endif + } + return fd; +} + int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h) { diff --git a/libavformat/network.h b/libavformat/network.h index 85b8f6dfc1..1d3fb7b7e0 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -249,13 +249,6 @@ int ff_listen_connect(int fd, const struct sockaddr *addr, int ff_http_match_no_proxy(const char *no_proxy, const char *hostname); -#ifndef SOCK_CLOEXEC -#define SOCK_CLOEXEC 0 -#endif - -static inline int ff_socket(int domain, int type, int protocol) -{ - return socket(domain, type | SOCK_CLOEXEC, protocol); -} +int ff_socket(int domain, int type, int protocol); #endif /* AVFORMAT_NETWORK_H */