Set sockets on posix to be FD_CLOEXEC

* This ensures target control sockets aren't inherited into child processes
  inadvertently and give incorrect idents for processes.
This commit is contained in:
baldurk
2020-05-22 22:33:18 +01:00
parent 7539533cb9
commit df9b7a36c3
+12
View File
@@ -113,6 +113,9 @@ Socket *Socket::AcceptClient(uint32_t timeoutMilliseconds)
int flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
flags = fcntl(s, F_GETFD, 0);
fcntl(s, F_SETFD, flags | FD_CLOEXEC);
int nodelay = 1;
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
@@ -383,6 +386,9 @@ Socket *CreateTCPServerSocket(const char *bindaddr, uint16_t port, int queuesize
int flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
flags = fcntl(s, F_GETFD, 0);
fcntl(s, F_SETFD, flags | FD_CLOEXEC);
return new Socket((ptrdiff_t)s);
}
@@ -426,6 +432,9 @@ Socket *CreateAbstractServerSocket(uint16_t port, int queuesize)
int flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
flags = fcntl(s, F_GETFD, 0);
fcntl(s, F_SETFD, flags | FD_CLOEXEC);
return new Socket((ptrdiff_t)s);
}
@@ -457,6 +466,9 @@ Socket *CreateClientSocket(const char *host, uint16_t port, int timeoutMS)
int flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
flags = fcntl(s, F_GETFD, 0);
fcntl(s, F_SETFD, flags | FD_CLOEXEC);
int result = connect(s, ptr->ai_addr, (int)ptr->ai_addrlen);
if(result == -1)
{