summaryrefslogtreecommitdiff
path: root/src/lib/sockets.c
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@intec.ugent.be>2016-08-04 21:20:07 +0200
committerdimitri staessens <dimitri.staessens@intec.ugent.be>2016-08-04 21:22:24 +0200
commit4ca72a0c670082f087529257f620614c7d94c1c9 (patch)
tree80a5bcbf18693220a0bc0d81882dae2b557901fe /src/lib/sockets.c
parent7e78d3f2fb1d3203aff64e72589cf98649e4fada (diff)
downloadouroboros-4ca72a0c670082f087529257f620614c7d94c1c9.tar.gz
ouroboros-4ca72a0c670082f087529257f620614c7d94c1c9.zip
lib: sockets: Fix cancellation cleanup
When cancelled in read() the fd should be closed and the allocated memory freed.
Diffstat (limited to 'src/lib/sockets.c')
-rw-r--r--src/lib/sockets.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/lib/sockets.c b/src/lib/sockets.c
index 9e13b687..5d861cdf 100644
--- a/src/lib/sockets.c
+++ b/src/lib/sockets.c
@@ -33,6 +33,8 @@
#include <sys/un.h>
#include <string.h>
#include <stdlib.h>
+#include <pthread.h>
+#include <stdbool.h>
int client_socket_open(char * file_name)
{
@@ -99,6 +101,11 @@ int server_socket_open(char * file_name)
return sockfd;
}
+void close_ptr(void * o)
+{
+ close(*(int *) o);
+}
+
irm_msg_t * send_recv_irm_msg(irm_msg_t * msg)
{
int sockfd;
@@ -122,6 +129,9 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg)
return NULL;
}
+ pthread_cleanup_push(close_ptr, &sockfd);
+ pthread_cleanup_push((void (*)(void *)) free, (void *) buf.data);
+
irm_msg__pack(msg, buf.data);
if (write(sockfd, buf.data, buf.len) == -1) {
@@ -144,8 +154,9 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg)
return NULL;
}
- free(buf.data);
- close(sockfd);
+ pthread_cleanup_pop(true);
+ pthread_cleanup_pop(true);
+
return recv_msg;
}