Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ixwebsocket/IXSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ namespace ix
#endif
}

bool Socket::setCloseOnExec(socket_t fd)
{
#ifdef _WIN32
// Not implemented on Windows.
return false;
#else
return ::fcntl(fd, F_SETFD, FD_CLOEXEC) == 0;
#endif
}

bool Socket::init(std::string& errorMsg)
{
return _selectInterrupt->init(errorMsg);
Expand Down
1 change: 1 addition & 0 deletions ixwebsocket/IXSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace ix
static void setErrno(int err);
static bool isWaitNeeded();
static void closeSocket(socket_t fd);
static bool setCloseOnExec(socket_t fd);

static PollResultType poll(bool readyToRead,
int timeoutMs,
Expand Down
30 changes: 30 additions & 0 deletions ixwebsocket/IXSocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ namespace ix
return std::make_pair(false, ss.str());
}

if (_closeOnExec)
{
if (!Socket::setCloseOnExec(_serverFd))
{
std::stringstream ss;
ss << "SocketServer::listen() error setting close on exec: "
<< strerror(Socket::getErrno());

Socket::closeSocket(_serverFd);
_serverFd = -1;
return std::make_pair(false, ss.str());
}
}

// Make that socket reusable. (allow restarting this server at will)
int enable = 1;
if (setsockopt(_serverFd, SOL_SOCKET, SO_REUSEADDR, (char*) &enable, sizeof(enable)) < 0)
Expand Down Expand Up @@ -373,6 +387,22 @@ namespace ix
continue;
}

if (_closeOnExec)
{
if (!Socket::setCloseOnExec(clientFd))
{
int err = Socket::getErrno();
std::stringstream ss;
ss << "SocketServer::run() error setting close on exec: " << err << ", "
<< strerror(err);
logError(ss.str());

Socket::closeSocket(clientFd);

continue;
}
}

// Retrieve connection info, the ip address of the remote peer/client)
std::string remoteIp;
int remotePort;
Expand Down
9 changes: 8 additions & 1 deletion ixwebsocket/IXSocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ namespace ix

void setTLSOptions(const SocketTLSOptions& socketTLSOptions);

int getPort();
// Set FD_CLOEXEC on server and client file descriptors.
void setCloseOnExec()
{
_closeOnExec = true;
}

int getPort();
std::string getHost();
int getBacklog();
std::size_t getMaxConnections();
Expand All @@ -93,6 +99,7 @@ namespace ix
int _backlog;
size_t _maxConnections;
int _addressFamily;
bool _closeOnExec = false;

// socket for accepting connections
socket_t _serverFd;
Expand Down
Loading