Add KEEP_ALIVE option to the socket connected with sdb server. 31/89831/1
authorKim Gunsoo <gunsoo83.kim@samsung.com>
Fri, 26 Feb 2016 05:41:50 +0000 (14:41 +0900)
committerKim Gunsoo <gunsoo83.kim@samsung.com>
Tue, 27 Sep 2016 06:49:38 +0000 (15:49 +0900)
- After connected with sdb server through TCP socket, add KEEP_ALIVE
  option to the socket.

Change-Id: I16ff01721dd8f919782c666871053a6e7599ec0e
Signed-off-by: Kim Gunsoo <gunsoo83.kim@samsung.com>
src/transport_local.c
src/utils.c
src/utils.h

index ff4aa36..16a595c 100644 (file)
@@ -36,6 +36,7 @@
 #if !SDB_HOST
 #include "commandline_sdbd.h"
 #endif
+#include "utils.h"
 
 #ifdef HAVE_BIG_ENDIAN
 #define H4(x)  (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
@@ -294,6 +295,15 @@ static void *server_socket_thread(void * arg)
                 && !request_plugin_verification(SDBD_CMD_VERIFY_PEERIP, inet_ntoa(addr.sin_addr))) {
                 sdb_close(fd);
             } else {
+                int ret = -1;
+                ret = keep_alive(fd, 1, SDB_KEEPALIVE_CNT, SDB_KEEPALIVE_IDLE, SDB_KEEPALIVE_INTVL);
+                if (ret < 0) {
+                    D("failed to set keep alive option. FD(%d), error=%s\n", fd, strerror(errno));
+                } else {
+                    D("Success to set keep alive option. FD(%d), cnt=%d, idle=%d(sec), interval=%d(sec)\n",
+                        fd, SDB_KEEPALIVE_CNT, SDB_KEEPALIVE_IDLE, SDB_KEEPALIVE_INTVL);
+                }
+
                 register_socket_transport(fd, "host", port, 1, NULL);
             }
         } else {
index acf8c5a..6f48ba2 100644 (file)
@@ -25,6 +25,9 @@
 #include <errno.h>
 #include <ctype.h>
 
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+
 #define STRING_MAXLEN 1024
 char*
 buff_addc (char*  buff, char*  buffEnd, int  c)
@@ -222,3 +225,33 @@ char** str_split(char* a_str, const char a_delim) {
        return result;
 }
 
+int keep_alive(int fd, int onoff, int cnt, int idle, int interval)
+{
+    int ret = -1;
+
+    ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &onoff, sizeof(onoff));
+    if (ret == -1) {
+        return ret;
+    }
+
+    /* override /proc/sys/net/ipv4/tcp_keepalive_probes */
+    ret = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
+    if (ret == -1) {
+        return ret;
+    }
+
+    /* override /proc/sys/net/ipv4/tcp_keepalive_time */
+    ret = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
+    if (ret == -1) {
+        return ret;
+    }
+
+    /* override /proc/sys/net/ipv4/tcp_keepalive_intvl */
+    ret = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
+    if (ret == -1) {
+        return ret;
+    }
+
+    return 0;
+}
+
index ce17442..7e78b6e 100644 (file)
@@ -76,4 +76,9 @@ int spawn(const char* program, char * const arg_list[]);
 
 char** str_split(char* a_str, const char a_delim);
 
+#define SDB_KEEPALIVE_CNT   (9)
+#define SDB_KEEPALIVE_IDLE  (1)
+#define SDB_KEEPALIVE_INTVL (1)
+int keep_alive(int fd, int onoff, int cnt, int idle, int interval);
+
 #endif /* _SDB_UTILS_H */