summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorSander Vrijders <sander.vrijders@intec.ugent.be>2016-08-10 14:41:49 +0200
committerSander Vrijders <sander.vrijders@intec.ugent.be>2016-08-10 15:59:27 +0200
commit25e76def21829edef7ef9bca0a028cccfabb944a (patch)
treedb9bca81c40407328d9c42d95ac893647b11c3af /src/lib
parent26b20744a441705accbe550aa0b996f8601a9404 (diff)
downloadouroboros-25e76def21829edef7ef9bca0a028cccfabb944a.tar.gz
ouroboros-25e76def21829edef7ef9bca0a028cccfabb944a.zip
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.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/dev.c7
-rw-r--r--src/lib/ipcp.c8
-rw-r--r--src/lib/sockets.c16
3 files changed, 26 insertions, 5 deletions
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 <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/time.h>
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 <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
+#include <sys/time.h>
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)
{