Эх сурвалжийг харах

Add missing SOCK_CLOEXEC flag on socket creation

Signed-off-by: Richard Genoud <richard.genoud@gmail.com>
Richard Genoud 6 жил өмнө
parent
commit
bba1b81291
1 өөрчлөгдсөн 14 нэмэгдсэн , 3 устгасан
  1. 14 3
      src/modbus-tcp.c

+ 14 - 3
src/modbus-tcp.c

@@ -481,6 +481,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
 {
 {
     int new_s;
     int new_s;
     int enable;
     int enable;
+    int type;
     struct sockaddr_in addr;
     struct sockaddr_in addr;
     modbus_tcp_t *ctx_tcp;
     modbus_tcp_t *ctx_tcp;
 
 
@@ -497,7 +498,13 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
     }
     }
 #endif
 #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) {
     if (new_s == -1) {
         return -1;
         return -1;
     }
     }
@@ -593,10 +600,14 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
 
 
     new_s = -1;
     new_s = -1;
     for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
     for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
+        int flags = ai_ptr->ai_socktype;
         int s;
         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 (s < 0) {
             if (ctx->debug) {
             if (ctx->debug) {
                 perror("socket");
                 perror("socket");