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/ipcpd/ipcp.c | 7 +++++++ src/irmd/main.c | 17 ++++++++++------- src/lib/dev.c | 7 +++---- src/lib/ipcp.c | 8 ++++++++ src/lib/sockets.c | 16 +++++++++++++++- 5 files changed, 43 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index a9f2b77b..784d845a 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -113,6 +113,9 @@ void * ipcp_main_loop(void * o) char * sock_path; + struct timeval tv = {(SOCKET_TIMEOUT / 1000), + (SOCKET_TIMEOUT % 1000) * 1000}; + if (_ipcp == NULL) { LOG_ERR("Invalid ipcp struct."); return (void *) 1; @@ -141,6 +144,10 @@ void * ipcp_main_loop(void * o) break; } + if (setsockopt(lsockfd, SOL_SOCKET, SO_RCVTIMEO, + (void *) &tv, sizeof(tv))) + LOG_WARN("Failed to set timeout on socket."); + pthread_cleanup_push(close_ptr, (void *) &lsockfd); count = read(lsockfd, buf, IPCP_MSG_BUF_SIZE); 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; diff --git a/src/lib/dev.c b/src/lib/dev.c index cc332233..c8577feb 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -159,10 +159,9 @@ int flow_accept(char ** ae_name) pthread_rwlock_unlock(&_ap_instance->data_lock); - recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { + recv_msg = send_recv_irm_msg_b(&msg); + if (recv_msg == NULL) return -1; - } if (!recv_msg->has_api || !recv_msg->has_port_id) { irm_msg__free_unpacked(recv_msg, NULL); @@ -358,7 +357,7 @@ int flow_alloc_res(int fd) pthread_rwlock_unlock(&_ap_instance->flows_lock); pthread_rwlock_unlock(&_ap_instance->data_lock); - recv_msg = send_recv_irm_msg(&msg); + recv_msg = send_recv_irm_msg_b(&msg); if (recv_msg == NULL) { return -1; } diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 9ff05e7b..bdc980f9 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -37,6 +37,8 @@ #include #include #include +#include +#include static void close_ptr(void * o) { @@ -51,6 +53,8 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, char * sock_path = NULL; ssize_t count = 0; ipcp_msg_t * recv_msg = NULL; + struct timeval tv = {(SOCKET_TIMEOUT / 1000), + (SOCKET_TIMEOUT % 1000) * 1000}; sock_path = ipcp_sock_path(api); if (sock_path == NULL) @@ -62,6 +66,10 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, return NULL; } + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, + (void *) &tv, sizeof(tv))) + LOG_WARN("Failed to set timeout on socket."); + free(sock_path); buf.len = ipcp_msg__get_packed_size(msg); diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 0d93c0a9..751c61b2 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -35,6 +35,7 @@ #include #include #include +#include int client_socket_open(char * file_name) { @@ -106,17 +107,25 @@ void close_ptr(void * o) close(*(int *) o); } -irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) +static irm_msg_t * send_recv_irm_msg_timed(irm_msg_t * msg, + bool timed) { int sockfd; buffer_t buf; ssize_t count = 0; irm_msg_t * recv_msg = NULL; + struct timeval tv = {(SOCKET_TIMEOUT / 1000), + (SOCKET_TIMEOUT % 1000) * 1000}; sockfd = client_socket_open(IRM_SOCK_PATH); if (sockfd < 0) return NULL; + if (timed) + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, + (void *) &tv, sizeof(tv))) + LOG_WARN("Failed to set timeout on socket."); + buf.len = irm_msg__get_packed_size(msg); if (buf.len == 0) { close(sockfd); @@ -147,6 +156,11 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) return recv_msg; } +irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) +{ return send_recv_irm_msg_timed(msg, true); } + +irm_msg_t * send_recv_irm_msg_b(irm_msg_t * msg) +{ return send_recv_irm_msg_timed(msg, false); } char * ipcp_sock_path(pid_t api) { -- cgit v1.2.3