Commit bba1b81291731b71666dcbbb58752345c60d5d74

Authored by Richard Genoud
1 parent df7d633f

Add missing SOCK_CLOEXEC flag on socket creation

Signed-off-by: Richard Genoud <richard.genoud@gmail.com>
Showing 1 changed file with 14 additions and 3 deletions
src/modbus-tcp.c
@@ -481,6 +481,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) @@ -481,6 +481,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
481 { 481 {
482 int new_s; 482 int new_s;
483 int enable; 483 int enable;
  484 + int type;
484 struct sockaddr_in addr; 485 struct sockaddr_in addr;
485 modbus_tcp_t *ctx_tcp; 486 modbus_tcp_t *ctx_tcp;
486 487
@@ -497,7 +498,13 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) @@ -497,7 +498,13 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
497 } 498 }
498 #endif 499 #endif
499 500
500 - new_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 501 + type = SOCK_STREAM;
  502 +
  503 +#ifdef SOCK_CLOEXEC
  504 + type |= SOCK_CLOEXEC;
  505 +#endif
  506 +
  507 + new_s = socket(PF_INET, type, IPPROTO_TCP);
501 if (new_s == -1) { 508 if (new_s == -1) {
502 return -1; 509 return -1;
503 } 510 }
@@ -593,10 +600,14 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) @@ -593,10 +600,14 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
593 600
594 new_s = -1; 601 new_s = -1;
595 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { 602 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
  603 + int flags = ai_ptr->ai_socktype;
596 int s; 604 int s;
597 605
598 - s = socket(ai_ptr->ai_family, ai_ptr->ai_socktype,  
599 - ai_ptr->ai_protocol); 606 +#ifdef SOCK_CLOEXEC
  607 + flags |= SOCK_CLOEXEC;
  608 +#endif
  609 +
  610 + s = socket(ai_ptr->ai_family, flags, ai_ptr->ai_protocol);
600 if (s < 0) { 611 if (s < 0) {
601 if (ctx->debug) { 612 if (ctx->debug) {
602 perror("socket"); 613 perror("socket");