From 25e76def21829edef7ef9bca0a028cccfabb944a Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Wed, 10 Aug 2016 14:41:49 +0200 Subject: lib, irmd, ipcp: Add socket timeout This will add a timeout to the socket so that a process won't be blocked by the actions of the process with which it is communicating over the socket. --- src/irmd/main.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/irmd') diff --git a/src/irmd/main.c b/src/irmd/main.c index e36fb98e..aae8a41e 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1477,6 +1477,8 @@ void * mainloop() irm_msg_t ret_msg = IRM_MSG__INIT; struct irm_flow * e = NULL; pid_t * apis = NULL; + struct timeval tv = {(SOCKET_TIMEOUT / 1000), + (SOCKET_TIMEOUT % 1000) * 1000}; pthread_rwlock_rdlock(&irmd->state_lock); if (irmd->state != IRMD_RUNNING) { @@ -1491,6 +1493,10 @@ void * mainloop() if (cli_sockfd < 0) continue; + if (setsockopt(cli_sockfd, SOL_SOCKET, SO_RCVTIMEO, + (void *) &tv, sizeof(tv))) + LOG_WARN("Failed to set timeout on socket."); + count = read(cli_sockfd, buf, IRM_MSG_BUF_SIZE); if (count <= 0) { LOG_ERR("Failed to read from socket."); @@ -1639,7 +1645,7 @@ void * mainloop() buffer.len = irm_msg__get_packed_size(&ret_msg); if (buffer.len == 0) { - LOG_ERR("Failed to send reply message."); + LOG_ERR("Failed to calculate length of reply message."); if (apis != NULL) free(apis); close(cli_sockfd); @@ -1660,7 +1666,7 @@ void * mainloop() free(apis); if (write(cli_sockfd, buffer.data, buffer.len) == -1) - LOG_ERR("Failed to send reply message."); + LOG_WARN("Failed to send reply message."); free(buffer.data); close(cli_sockfd); @@ -1730,11 +1736,8 @@ static struct irm * irm_create() return NULL; } - if (setsockopt(irmd->sockfd, - SOL_SOCKET, - SO_RCVTIMEO, - (char *) &timeout, - sizeof(timeout)) < 0) { + if (setsockopt(irmd->sockfd, SOL_SOCKET, SO_RCVTIMEO, + (char *) &timeout, sizeof(timeout)) < 0) { LOG_ERR("Failed setting socket option."); irm_destroy(); return NULL; -- cgit v1.2.3