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 481 {
482 482 int new_s;
483 483 int enable;
  484 + int type;
484 485 struct sockaddr_in addr;
485 486 modbus_tcp_t *ctx_tcp;
486 487  
... ... @@ -497,7 +498,13 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
497 498 }
498 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 508 if (new_s == -1) {
502 509 return -1;
503 510 }
... ... @@ -593,10 +600,14 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
593 600  
594 601 new_s = -1;
595 602 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
  603 + int flags = ai_ptr->ai_socktype;
596 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 611 if (s < 0) {
601 612 if (ctx->debug) {
602 613 perror("socket");
... ...