commit 2efc597abcaa72aa84fef08882beffc090ee03c6
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Date:   Thu Jul 11 09:51:18 2013 -0400

    inet/inet6 sockets: apply timeout
    
    Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c
index b9404eb..023106f 100644
--- a/src/bin/lttng-sessiond/main.c
+++ b/src/bin/lttng-sessiond/main.c
@@ -1293,11 +1293,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 7212001..10cf1ad 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 3c1c970..5cdb262 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 58a7ffb..65952b2 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 8377e9a..1f9131d 100644
--- a/src/common/sessiond-comm/sessiond-comm.h
+++ b/src/common/sessiond-comm/sessiond-comm.h
@@ -473,6 +473,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 48fa104..0d7c95a 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 34f156f..19b91ce 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 */
