From 54853d3ff39079a78e9681c28d5ec65119738b18 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 9 Aug 2016 17:20:36 +0200 Subject: irmd: Close filedescriptor on exit --- src/irmd/main.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/irmd/main.c b/src/irmd/main.c index c7c1566d..58535e27 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1431,6 +1431,11 @@ static void clean_msg(void * msg) irm_msg__free_unpacked(msg, NULL); } +static void close_ptr(void * o) +{ + close(*((int *) o)); +} + void * mainloop() { uint8_t buf[IRM_MSG_BUF_SIZE]; @@ -1465,6 +1470,7 @@ void * mainloop() continue; } + pthread_cleanup_push(close_ptr, &cli_sockfd); pthread_cleanup_push(clean_msg, (void *) msg); switch (msg->code) { @@ -1595,6 +1601,7 @@ void * mainloop() } pthread_cleanup_pop(true); + pthread_cleanup_pop(false); buffer.len = irm_msg__get_packed_size(&ret_msg); if (buffer.len == 0) { @@ -1615,17 +1622,12 @@ void * mainloop() irm_msg__pack(&ret_msg, buffer.data); - if (write(cli_sockfd, buffer.data, buffer.len) == -1) { - free(buffer.data); - if (apis != NULL) - free(apis); - close(cli_sockfd); - continue; - } - if (apis != NULL) free(apis); + if (write(cli_sockfd, buffer.data, buffer.len) == -1) + LOG_ERR("Failed to send reply message."); + free(buffer.data); close(cli_sockfd); } -- cgit v1.2.3 From e302f7c490e8ffc9a5fe74069115340230a80266 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 9 Aug 2016 18:13:33 +0200 Subject: ipcpd: Correct cleanup of filedescriptor --- src/ipcpd/ipcp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index 839a5a9b..a9f2b77b 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -256,7 +256,7 @@ void * ipcp_main_loop(void * o) } pthread_cleanup_pop(true); - + pthread_cleanup_pop(false); buffer.len = ipcp_msg__get_packed_size(&ret_msg); if (buffer.len == 0) { @@ -280,7 +280,7 @@ void * ipcp_main_loop(void * o) } free(buffer.data); - pthread_cleanup_pop(true); + close(lsockfd); } -- cgit v1.2.3 From b1d2e01f1c7c49bd2e1f926f4adb23b21ba36a4a Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 9 Aug 2016 18:39:03 +0200 Subject: lib: Fix cleanup for sending messages Cleanup handlers were not always popped. --- src/lib/ipcp.c | 22 ++++------------------ src/lib/sockets.c | 21 ++++----------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 76098691..141db43d 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -81,25 +81,11 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, ipcp_msg__pack(msg, buf.data); - if (write(sockfd, buf.data, buf.len) == -1) { - free(buf.data); - close(sockfd); - return NULL; - } - - count = read(sockfd, buf.data, IPCP_MSG_BUF_SIZE); - if (count <= 0) { - free(buf.data); - close(sockfd); - return NULL; - } + if (write(sockfd, buf.data, buf.len) != -1) + count = read(sockfd, buf.data, IPCP_MSG_BUF_SIZE); - recv_msg = ipcp_msg__unpack(NULL, count, buf.data); - if (recv_msg == NULL) { - free(buf.data); - close(sockfd); - return NULL; - } + if (count > 0) + recv_msg = ipcp_msg__unpack(NULL, count, buf.data); pthread_cleanup_pop(true); pthread_cleanup_pop(true); diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 5d861cdf..0d93c0a9 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -134,25 +134,12 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) irm_msg__pack(msg, buf.data); - if (write(sockfd, buf.data, buf.len) == -1) { - free(buf.data); - close(sockfd); - return NULL; - } + if (write(sockfd, buf.data, buf.len) != -1) + count = read(sockfd, buf.data, IRM_MSG_BUF_SIZE); - count = read(sockfd, buf.data, IRM_MSG_BUF_SIZE); - if (count <= 0) { - free(buf.data); - close(sockfd); - return NULL; - } - recv_msg = irm_msg__unpack(NULL, count, buf.data); - if (recv_msg == NULL) { - free(buf.data); - close(sockfd); - return NULL; - } + if (count > 0) + recv_msg = irm_msg__unpack(NULL, count, buf.data); pthread_cleanup_pop(true); pthread_cleanup_pop(true); -- cgit v1.2.3 From 1c67b4cd199f9efed96468a2a2ee69fd755bcf7c Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 9 Aug 2016 18:56:54 +0200 Subject: irmd: Fix some locking issues --- src/irmd/main.c | 7 ++++--- src/irmd/registry.c | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/irmd/main.c b/src/irmd/main.c index 58535e27..8503fcfa 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -787,10 +787,10 @@ static int flow_alloc_resp(pid_t n_api, return -1; } - registry_del_api(&irmd->registry, n_api); - pthread_mutex_unlock(&rne->state_lock); + registry_del_api(&irmd->registry, n_api); + pthread_rwlock_unlock(&irmd->reg_lock); if (!response) { @@ -1401,10 +1401,11 @@ void * irm_flow_cleaner() } pthread_rwlock_unlock(&irmd->flows_lock); - pthread_rwlock_wrlock(&irmd->reg_lock); registry_sanitize_apis(&irmd->registry); + pthread_rwlock_wrlock(&irmd->reg_lock); + list_for_each_safe(pos, n, &irmd->spawned_apis) { struct spawned_api * api = list_entry(pos, struct spawned_api, next); diff --git a/src/irmd/registry.c b/src/irmd/registry.c index ae8c3b0a..04f60009 100644 --- a/src/irmd/registry.c +++ b/src/irmd/registry.c @@ -544,6 +544,8 @@ void registry_del_api(struct list_head * registry, reg_api_destroy(i); + pthread_mutex_lock(&e->state_lock); + if (list_empty(&e->reg_apis)) { if (reg_entry_has_auto_binding(e)) e->state = REG_NAME_AUTO_ACCEPT; @@ -554,6 +556,7 @@ void registry_del_api(struct list_head * registry, } pthread_cond_signal(&e->state_cond); + pthread_mutex_unlock(&e->state_lock); return; } -- cgit v1.2.3