From bba1b81291731b71666dcbbb58752345c60d5d74 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Thu, 31 May 2018 10:35:35 +0200 Subject: [PATCH] Add missing SOCK_CLOEXEC flag on socket creation --- src/modbus-tcp.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index a0846f2..882c01a 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -481,6 +481,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) { int new_s; int enable; + int type; struct sockaddr_in addr; modbus_tcp_t *ctx_tcp; @@ -497,7 +498,13 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) } #endif - new_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + type = SOCK_STREAM; + +#ifdef SOCK_CLOEXEC + type |= SOCK_CLOEXEC; +#endif + + new_s = socket(PF_INET, type, IPPROTO_TCP); if (new_s == -1) { return -1; } @@ -593,10 +600,14 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) new_s = -1; for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { + int flags = ai_ptr->ai_socktype; int s; - s = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, - ai_ptr->ai_protocol); +#ifdef SOCK_CLOEXEC + flags |= SOCK_CLOEXEC; +#endif + + s = socket(ai_ptr->ai_family, flags, ai_ptr->ai_protocol); if (s < 0) { if (ctx->debug) { perror("socket"); -- libgit2 0.21.4