From ae9e45b342636e7b42eafc15cd105bccfbbbe373 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Thu, 1 Nov 2012 11:24:22 -0400 Subject: [PATCH] Add a timeout to UST application socket 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 --- src/bin/lttng-sessiond/main.c | 21 ++++++++++++++++- src/common/defaults.h | 6 +++++ src/common/sessiond-comm/unix.c | 42 +++++++++++++++++++++++++++++++++ src/common/sessiond-comm/unix.h | 2 ++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 022bbc6d1..90f11d648 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -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); diff --git a/src/common/defaults.h b/src/common/defaults.h index 5495531f6..0c0b6ac9c 100644 --- a/src/common/defaults.h +++ b/src/common/defaults.h @@ -154,4 +154,10 @@ */ #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 */ diff --git a/src/common/sessiond-comm/unix.c b/src/common/sessiond-comm/unix.c index 9ef04ee0d..2f79f3a28 100644 --- a/src/common/sessiond-comm/unix.c +++ b/src/common/sessiond-comm/unix.c @@ -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; +} diff --git a/src/common/sessiond-comm/unix.h b/src/common/sessiond-comm/unix.h index 19b91ce40..34f156ffd 100644 --- a/src/common/sessiond-comm/unix.h +++ b/src/common/sessiond-comm/unix.h @@ -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 */ -- 2.34.1