linux: Add SOCK_CLOEXEC to socket calls

Günther Brammer 2012-03-11 00:50:51 +01:00
parent b4fd8fcd15
commit 3755a36dd1
3 changed files with 8 additions and 4 deletions

View File

@ -625,7 +625,7 @@ bool C4NetIOTCP::Execute(int iMaxTime, pollfd *fds) // (mt-safe)
bool C4NetIOTCP::Connect(const C4NetIO::addr_t &addr) // (mt-safe)
{
// create new socket
SOCKET nsock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKET nsock = ::socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
if (nsock == INVALID_SOCKET)
{
SetError("socket creation failed", true);
@ -954,7 +954,7 @@ bool C4NetIOTCP::Listen(uint16_t inListenPort)
iListenPort = P_NONE;
// create socket
if ((lsock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
if ((lsock = ::socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == INVALID_SOCKET)
{
SetError("socket creation failed", true);
return false;
@ -1326,7 +1326,7 @@ bool C4NetIOSimpleUDP::Init(uint16_t inPort)
#endif
// create socket
if ((sock = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
if ((sock = ::socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == INVALID_SOCKET)
{
SetError("could not create socket", true);
return false;

View File

@ -48,6 +48,10 @@
#include <sys/socket.h>
#endif
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC 0
#endif
#ifndef HAVE_CONFIG_H
// #define C4NETIO_DEBUG
#endif

View File

@ -40,7 +40,7 @@ namespace
operator int() const { return sock; }
socket_wrapper(int domain, int type, int protocol):
sock(socket(domain, type, protocol))
sock(socket(domain, type | SOCK_CLOEXEC, protocol))
{
if(sock == -1)
throw std::runtime_error(std::string("Failed to create a socket: ") + strerror(errno));