Skip to content

Commit 9d5ec50

Browse files
Rémi Denis-Courmontmstorsjo
authored andcommitted
ff_socket: put out-of-line and fallback to fcntl() for close-on-exec
This supports non-Linux systems (SOCK_CLOEXEC is non-standard) and older Linux kernels to the extent possible. Signed-off-by: Martin Storsjö <[email protected]>
1 parent fa09e76 commit 9d5ec50

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

libavformat/network.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919
*/
2020

21+
#include <fcntl.h>
2122
#include "network.h"
2223
#include "url.h"
2324
#include "libavcodec/internal.h"
@@ -210,6 +211,24 @@ static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
210211
return ret;
211212
}
212213

214+
int ff_socket(int af, int type, int proto)
215+
{
216+
int fd;
217+
218+
#ifdef SOCK_CLOEXEC
219+
fd = socket(af, type | SOCK_CLOEXEC, proto);
220+
if (fd == -1 && errno == EINVAL)
221+
#endif
222+
{
223+
fd = socket(af, type, proto);
224+
#if HAVE_FCNTL
225+
if (fd != -1)
226+
fcntl(fd, F_SETFD, FD_CLOEXEC);
227+
#endif
228+
}
229+
return fd;
230+
}
231+
213232
int ff_listen_bind(int fd, const struct sockaddr *addr,
214233
socklen_t addrlen, int timeout, URLContext *h)
215234
{

libavformat/network.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,6 @@ int ff_listen_connect(int fd, const struct sockaddr *addr,
249249

250250
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
251251

252-
#ifndef SOCK_CLOEXEC
253-
#define SOCK_CLOEXEC 0
254-
#endif
255-
256-
static inline int ff_socket(int domain, int type, int protocol)
257-
{
258-
return socket(domain, type | SOCK_CLOEXEC, protocol);
259-
}
252+
int ff_socket(int domain, int type, int protocol);
260253

261254
#endif /* AVFORMAT_NETWORK_H */

0 commit comments

Comments
 (0)