Skip to content

Commit

Permalink
Enable FD_CLOEXEC.
Browse files Browse the repository at this point in the history
PostgreSQL enables this option when available which seems like a good idea since we also do not share connections between processes.

Note that as in PostgreSQL there is no way to disable this option.
  • Loading branch information
dwsteele committed Apr 1, 2020
1 parent 967f2c0 commit 3aedcd1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/io/socket/common.c
Expand Up @@ -3,6 +3,8 @@ Socket Common Functions
***********************************************************************************************************************************/
#include "build.auto.h"

#include <fcntl.h>

#ifdef __FreeBSD__
#include <netinet/in.h>
#endif
Expand Down Expand Up @@ -72,6 +74,12 @@ sckOptionSet(int fd)
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &socketValue, sizeof(int)) == -1, ProtocolError, "unable set TCP_NODELAY");
#endif

// Automatically close the socket (in the child process) on a successful execve() call. Connections are never shared between
// processes so there is no reason to leave them open.
#ifdef F_SETFD
THROW_ON_SYS_ERROR(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1, ProtocolError, "unable set FD_CLOEXEC");
#endif

// Enable TCP keepalives
if (socketLocal.keepAlive)
{
Expand Down
3 changes: 3 additions & 0 deletions test/src/module/common/ioTlsTest.c
@@ -1,6 +1,7 @@
/***********************************************************************************************************************************
Test Tls Client
***********************************************************************************************************************************/
#include <fcntl.h>
#include <unistd.h>

#include "common/time.h"
Expand Down Expand Up @@ -130,6 +131,8 @@ testRun(void)
sckInit(true, 32, 3113, 818);
sckOptionSet(fd);

TEST_RESULT_INT(fcntl(fd, F_GETFD), FD_CLOEXEC, "check FD_CLOEXEC");

socklen_t socketValueSize = sizeof(int);
int noDelayValue = 0;

Expand Down

0 comments on commit 3aedcd1

Please sign in to comment.