From: Mathieu Desnoyers Date: Thu, 8 Aug 2013 09:02:30 +0000 (-0400) Subject: inet/inet6 sockets: apply timeout X-Git-Tag: v2.3.0-rc3~52 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=783a3b9aa609d6805e1463655d25c7ae176c4859;p=lttng-tools.git inet/inet6 sockets: apply timeout Close #546 Signed-off-by: Mathieu Desnoyers Signed-off-by: David Goulet --- diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index e05d72bac..d22e2a63d 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -1298,11 +1298,17 @@ static void *thread_manage_apps(void *data) goto error; } - /* Set socket timeout for both receiving and ending */ + /* + * Set socket timeout for both receiving and ending. + * app_socket_timeout is in seconds, whereas + * lttcomm_setsockopt_rcv_timeout and + * lttcomm_setsockopt_snd_timeout expect msec as + * parameter. + */ (void) lttcomm_setsockopt_rcv_timeout(sock, - app_socket_timeout); + app_socket_timeout * 1000); (void) lttcomm_setsockopt_snd_timeout(sock, - app_socket_timeout); + app_socket_timeout * 1000); DBG("Apps with sock %d added to poll set", sock); diff --git a/src/common/sessiond-comm/inet.c b/src/common/sessiond-comm/inet.c index 6d729238a..eb0c4fde3 100644 --- a/src/common/sessiond-comm/inet.c +++ b/src/common/sessiond-comm/inet.c @@ -57,6 +57,7 @@ LTTNG_HIDDEN int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto) { int val = 1, ret; + unsigned long timeout; /* Create server socket */ if ((sock->fd = socket(PF_INET, type, proto)) < 0) { @@ -74,6 +75,17 @@ int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto) PERROR("setsockopt inet"); goto error; } + timeout = lttcomm_get_network_timeout(); + if (timeout) { + ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout); + if (ret) { + goto error; + } + ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout); + if (ret) { + goto error; + } + } return 0; diff --git a/src/common/sessiond-comm/inet6.c b/src/common/sessiond-comm/inet6.c index 52e2f8e58..5b137c4eb 100644 --- a/src/common/sessiond-comm/inet6.c +++ b/src/common/sessiond-comm/inet6.c @@ -57,6 +57,7 @@ LTTNG_HIDDEN int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto) { int val = 1, ret; + unsigned long timeout; /* Create server socket */ if ((sock->fd = socket(PF_INET6, type, proto)) < 0) { @@ -74,6 +75,17 @@ int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto) PERROR("setsockopt inet6"); goto error; } + timeout = lttcomm_get_network_timeout(); + if (timeout) { + ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout); + if (ret) { + goto error; + } + ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout); + if (ret) { + goto error; + } + } return 0; diff --git a/src/common/sessiond-comm/sessiond-comm.c b/src/common/sessiond-comm/sessiond-comm.c index 58a7ffb50..65952b273 100644 --- a/src/common/sessiond-comm/sessiond-comm.c +++ b/src/common/sessiond-comm/sessiond-comm.c @@ -373,6 +373,46 @@ error: return NULL; } +/* + * Set socket receiving timeout. + */ +LTTNG_HIDDEN +int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) +{ + int ret; + struct timeval tv; + + tv.tv_sec = msec / 1000; + tv.tv_usec = (msec % 1000) * 1000; + + ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + if (ret < 0) { + PERROR("setsockopt SO_RCVTIMEO"); + } + + return ret; +} + +/* + * Set socket sending timeout. + */ +LTTNG_HIDDEN +int lttcomm_setsockopt_snd_timeout(int sock, unsigned int msec) +{ + int ret; + struct timeval tv; + + tv.tv_sec = msec / 1000; + tv.tv_usec = (msec % 1000) * 1000; + + ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + if (ret < 0) { + PERROR("setsockopt SO_SNDTIMEO"); + } + + return ret; +} + LTTNG_HIDDEN void lttcomm_init(void) { diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index 2450e79ec..c52a6caa0 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -474,6 +474,9 @@ extern void lttcomm_copy_sock(struct lttcomm_sock *dst, extern struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock( struct lttng_uri *uri, uint32_t major, uint32_t minor); +extern int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int msec); +extern int lttcomm_setsockopt_snd_timeout(int sock, unsigned int msec); + extern void lttcomm_init(void); /* Get network timeout, in milliseconds */ extern unsigned long lttcomm_get_network_timeout(void); diff --git a/src/common/sessiond-comm/unix.c b/src/common/sessiond-comm/unix.c index 48fa1049a..0d7c95a76 100644 --- a/src/common/sessiond-comm/unix.c +++ b/src/common/sessiond-comm/unix.c @@ -539,45 +539,3 @@ int lttcomm_setsockopt_creds_unix_sock(int sock) #else #error "Please implement credential support for your OS." #endif /* __linux__ */ - -/* - * Set socket reciving timeout. - */ -LTTNG_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. - */ -LTTNG_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 34f156ffd..19b91ce40 100644 --- a/src/common/sessiond-comm/unix.h +++ b/src/common/sessiond-comm/unix.h @@ -45,7 +45,5 @@ 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 */