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/lib/dev.c | 7 +++---- src/lib/ipcp.c | 8 ++++++++ src/lib/sockets.c | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src/lib') 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