diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index d67743f..0a3d6f1 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -25,6 +25,8 @@ version (linux) { extern (C) int accept4(int sockfd, sockaddr *addr, socklen_t *addrlen, int flags) nothrow @nogc; static if (!is(typeof(SOCK_NONBLOCK))) enum SOCK_NONBLOCK = 0x800; + static if (!is(typeof(SOCK_CLOEXEC))) + enum SOCK_CLOEXEC = 0x80000; static if (__VERSION__ < 2077) { @@ -230,12 +232,13 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets sockaddr_storage addr; socklen_t addr_len = addr.sizeof; version (linux) { - () @trusted { sockfd = accept4(cast(sock_t)listenfd, () @trusted { return cast(sockaddr*)&addr; } (), &addr_len, SOCK_NONBLOCK); } (); + () @trusted { sockfd = accept4(cast(sock_t)listenfd, () @trusted { return cast(sockaddr*)&addr; } (), &addr_len, SOCK_NONBLOCK | SOCK_CLOEXEC); } (); if (sockfd == -1) return; } else { () @trusted { sockfd = accept(cast(sock_t)listenfd, () @trusted { return cast(sockaddr*)&addr; } (), &addr_len); } (); if (sockfd == -1) return; setSocketNonBlocking(cast(SocketFD)sockfd); + setSocketCloseOnExec(cast(SocketFD)sockfd); } auto fd = cast(StreamSocketFD)sockfd; m_loop.initFD(fd, FDFlags.none); @@ -927,6 +930,13 @@ private void setSocketNonBlocking(SocketFD sockfd) } } +private void setSocketCloseOnExec(SocketFD sockfd) +@nogc nothrow { + version (Windows) {} else { + () @trusted { fcntl(cast(int)sockfd, F_SETFL, O_CLOEXEC, 1); } (); + } +} + private int getSocketError() @nogc nothrow { version (Windows) return WSAGetLastError();