Add a timeout to UST application socket
authorDavid Goulet <dgoulet@efficios.com>
Thu, 1 Nov 2012 15:24:22 +0000 (11:24 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Thu, 8 Nov 2012 16:22:39 +0000 (11:22 -0500)
By default, a 5 seconds timeout is set on every UST application socket
just after the accept() for both recv and send operation.

This value can be changed through a new environment variable (seconds)
of the session daemon.

This is the first step to fix bug386 where a stopped application can
stalls indefinitely the session daemon when a new command is sent.

The bug 386 is also fixed with the lttng-ust commit:
7bc53e94a229963972aa78880b361b1510fdd268

Fixes #386

Signed-off-by: David Goulet <dgoulet@efficios.com>
src/bin/lttng-sessiond/main.c
src/common/defaults.h
src/common/sessiond-comm/unix.c
src/common/sessiond-comm/unix.h

index 022bbc6d1dc5b293974166f0a92bd04818ba29e2..90f11d648e537fee78cbb88506dc6e026f675909 100644 (file)
@@ -228,6 +228,11 @@ struct health_state health_thread_app_manage;
 struct health_state health_thread_app_reg;
 struct health_state health_thread_kernel;
 
+/*
+ * Socket timeout for receiving and sending in seconds.
+ */
+static int app_socket_timeout;
+
 static
 void setup_consumerd_path(void)
 {
@@ -1205,6 +1210,12 @@ static void *thread_manage_apps(void *data)
                                                        goto error;
                                                }
 
+                                               /* Set socket timeout for both receiving and ending */
+                                               (void) lttcomm_setsockopt_rcv_timeout(ust_cmd.sock,
+                                                               app_socket_timeout);
+                                               (void) lttcomm_setsockopt_snd_timeout(ust_cmd.sock,
+                                                               app_socket_timeout);
+
                                                DBG("Apps with sock %d added to poll set",
                                                                ust_cmd.sock);
                                        }
@@ -3790,7 +3801,7 @@ int main(int argc, char **argv)
 {
        int ret = 0;
        void *status;
-       const char *home_path;
+       const char *home_path, *env_app_timeout;
 
        init_kernel_workarounds();
 
@@ -4070,6 +4081,14 @@ int main(int argc, char **argv)
        health_init(&ustconsumer64_data.health);
        health_poll_update(&ustconsumer64_data.health);
 
+       /* Check for the application socket timeout env variable. */
+       env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
+       if (env_app_timeout) {
+               app_socket_timeout = atoi(env_app_timeout);
+       } else {
+               app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
+       }
+
        /* Create thread to manage the client socket */
        ret = pthread_create(&health_thread, NULL,
                        thread_manage_health, (void *) NULL);
index 5495531f6318a5177b9d3e97fb05ff939121075f..0c0b6ac9c89628bb9fa7609bde99b9322d519c67 100644 (file)
  */
 #define DEFAULT_DATA_AVAILABILITY_WAIT_TIME 200000  /* usec */
 
+/*
+ * Default receiving and sending timeout for an application socket.
+ */
+#define DEFAULT_APP_SOCKET_RW_TIMEOUT       5  /* sec */
+#define DEFAULT_APP_SOCKET_TIMEOUT_ENV      "LTTNG_APP_SOCKET_TIMEOUT"
+
 #endif /* _DEFAULTS_H */
index 9ef04ee0df1943ce41b4ca886ee2913d27d21619..2f79f3a280a123bd85a861ff039f72e5e4c6064c 100644 (file)
@@ -523,3 +523,45 @@ int lttcomm_setsockopt_creds_unix_sock(int sock)
 #else
 #error "Please implement credential support for your OS."
 #endif /* __linux__ */
+
+/*
+ * Set socket reciving timeout.
+ */
+__attribute__((visibility("hidden")))
+int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int sec)
+{
+       int ret;
+       struct timeval tv;
+
+       tv.tv_sec = sec;
+       tv.tv_usec = 0;
+
+       ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+       if (ret < 0) {
+               PERROR("setsockopt SO_RCVTIMEO");
+               ret = -errno;
+       }
+
+       return ret;
+}
+
+/*
+ * Set socket sending timeout.
+ */
+__attribute__((visibility("hidden")))
+int lttcomm_setsockopt_snd_timeout(int sock, unsigned int sec)
+{
+       int ret;
+       struct timeval tv;
+
+       tv.tv_sec = sec;
+       tv.tv_usec = 0;
+
+       ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+       if (ret < 0) {
+               PERROR("setsockopt SO_SNDTIMEO");
+               ret = -errno;
+       }
+
+       return ret;
+}
index 19b91ce40e0dcb689529e886f6737ac9664b8fe8..34f156ffd46171298be768abafd1ebca899dcef0 100644 (file)
@@ -45,5 +45,7 @@ extern ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
                lttng_sock_cred *creds);
 
 extern int lttcomm_setsockopt_creds_unix_sock(int sock);
+extern int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int sec);
+extern int lttcomm_setsockopt_snd_timeout(int sock, unsigned int sec);
 
 #endif /* _LTTCOMM_UNIX_H */
This page took 0.034138 seconds and 4 git commands to generate.