sockets O_CLOEXEC

This commit is contained in:
Michael Davidsaver
2021-01-29 15:17:29 -08:00
parent b7640016a6
commit fb7abfc132
2 changed files with 11 additions and 3 deletions
+10 -2
View File
@@ -320,15 +320,23 @@ evsocket::evsocket(evutil_socket_t sock)
if(sock==evutil_socket_t(-1))
throw std::bad_alloc();
evutil_make_socket_closeonexec(sock);
if(evutil_make_socket_nonblocking(sock)) {
evutil_closesocket(sock);
throw std::runtime_error("Unable to make non-blocking socket");
}
}
// Linux specific way to atomically create a socket with O_CLOEXEC
#ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0
#endif
evsocket::evsocket(int af, int type, int proto)
:evsocket(socket(af, type, proto))
{}
:evsocket(socket(af, type | SOCK_CLOEXEC, proto))
{
}
evsocket::evsocket(evsocket&& o) noexcept
:sock(o.sock)
+1 -1
View File
@@ -366,7 +366,7 @@ ServIface::ServIface(const std::string& addr, unsigned short port, server::Serve
#endif
const int backlog = 4;
listener = evlisten(evconnlistener_new(server->acceptor_loop.base, onConnS, this, LEV_OPT_DISABLED, backlog, sock.sock));
listener = evlisten(evconnlistener_new(server->acceptor_loop.base, onConnS, this, LEV_OPT_DISABLED|LEV_OPT_CLOSE_ON_EXEC, backlog, sock.sock));
if(!LEV_OPT_DISABLED)
evconnlistener_disable(listener.get());