summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorSander Vrijders <[email protected]>2016-08-08 10:29:06 +0200
committerSander Vrijders <[email protected]>2016-08-08 10:29:06 +0200
commitc1e1831ded0330771fd175f1c8138ed82c90f9f8 (patch)
tree617e3002ffa5addb364c01d5cd925d8c609b12b5 /src/lib
parentd5b71449bc18116444720257ba640e3c597ff6e9 (diff)
parent8450af0e2950e5a1415e38702d6169cc8a732294 (diff)
downloadouroboros-c1e1831ded0330771fd175f1c8138ed82c90f9f8.tar.gz
ouroboros-c1e1831ded0330771fd175f1c8138ed82c90f9f8.zip
Merged in dstaesse/ouroboros/be-bugfixing (pull request #187)
bugfixes
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ipcp.c22
-rw-r--r--src/lib/shm_ap_rbuff.c16
-rw-r--r--src/lib/shm_du_map.c24
-rw-r--r--src/lib/sockets.c15
4 files changed, 40 insertions, 37 deletions
diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c
index 373baafd..76098691 100644
--- a/src/lib/ipcp.c
+++ b/src/lib/ipcp.c
@@ -34,9 +34,15 @@
#include <string.h>
#include <signal.h>
#include <stdbool.h>
+#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
+static void close_ptr(void * o)
+{
+ close(*((int *) o));
+}
+
static ipcp_msg_t * send_recv_ipcp_msg(pid_t api,
ipcp_msg_t * msg)
{
@@ -56,24 +62,26 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api,
return NULL;
}
+ free(sock_path);
+
buf.len = ipcp_msg__get_packed_size(msg);
if (buf.len == 0) {
close(sockfd);
- free(sock_path);
return NULL;
}
buf.data = malloc(IPCP_MSG_BUF_SIZE);
if (buf.data == NULL) {
close(sockfd);
- free(sock_path);
return NULL;
}
+ pthread_cleanup_push(close_ptr, (void *) &sockfd);
+ pthread_cleanup_push((void (*)(void *)) free, (void *) buf.data);
+
ipcp_msg__pack(msg, buf.data);
if (write(sockfd, buf.data, buf.len) == -1) {
- free(sock_path);
free(buf.data);
close(sockfd);
return NULL;
@@ -81,7 +89,6 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api,
count = read(sockfd, buf.data, IPCP_MSG_BUF_SIZE);
if (count <= 0) {
- free(sock_path);
free(buf.data);
close(sockfd);
return NULL;
@@ -89,15 +96,14 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api,
recv_msg = ipcp_msg__unpack(NULL, count, buf.data);
if (recv_msg == NULL) {
- free(sock_path);
free(buf.data);
close(sockfd);
return NULL;
}
- free(buf.data);
- free(sock_path);
- close(sockfd);
+ pthread_cleanup_pop(true);
+ pthread_cleanup_pop(true);
+
return recv_msg;
}
diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c
index 56555533..e9c51533 100644
--- a/src/lib/shm_ap_rbuff.c
+++ b/src/lib/shm_ap_rbuff.c
@@ -52,9 +52,6 @@
#define shm_rbuff_empty(rb) (*rb->ptr_head == *rb->ptr_tail)
#define head_el_ptr(rb) (rb->shm_base + *rb->ptr_head)
#define tail_el_ptr(rb) (rb->shm_base + *rb->ptr_tail)
-#define clean_sdus(rb) \
- while (!shm_rbuff_empty(rb) && tail_el_ptr(rb)->port_id < 0) \
- *rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_BUFFER_SIZE -1); \
struct shm_ap_rbuff {
struct rb_entry * shm_base; /* start of entry */
@@ -173,6 +170,7 @@ struct shm_ap_rbuff * shm_ap_rbuff_open(pid_t api)
shm_fd = shm_open(fn, O_RDWR, 0666);
if (shm_fd == -1) {
LOG_DBG("%d failed opening shared memory %s.", getpid(), fn);
+ free(rb);
return NULL;
}
@@ -315,8 +313,6 @@ int shm_ap_rbuff_peek(struct shm_ap_rbuff * rb,
pthread_mutex_consistent(rb->lock);
}
- clean_sdus(rb);
-
while (shm_rbuff_empty(rb)) {
if (timeout != NULL)
ret = pthread_cond_timedwait(rb->add,
@@ -325,8 +321,6 @@ int shm_ap_rbuff_peek(struct shm_ap_rbuff * rb,
else
ret = pthread_cond_wait(rb->add, rb->lock);
- clean_sdus(rb);
-
if (ret == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
@@ -360,8 +354,6 @@ struct rb_entry * shm_ap_rbuff_read(struct shm_ap_rbuff * rb)
pthread_mutex_consistent(rb->lock);
}
- clean_sdus(rb);
-
while (shm_rbuff_empty(rb))
if (pthread_cond_wait(rb->add, rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
@@ -392,8 +384,6 @@ ssize_t shm_ap_rbuff_read_port(struct shm_ap_rbuff * rb, int port_id)
pthread_mutex_consistent(rb->lock);
}
- clean_sdus(rb);
-
if (shm_rbuff_empty(rb) || tail_el_ptr(rb)->port_id != port_id) {
pthread_mutex_unlock(rb->lock);
return -1;
@@ -428,8 +418,6 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
ts_add(&abstime, timeout, &abstime);
}
- clean_sdus(rb);
-
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) rb->lock);
@@ -441,8 +429,6 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
else
ret = pthread_cond_wait(rb->del, rb->lock);
- clean_sdus(rb);
-
if (ret == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c
index a12ef223..3e2c99d4 100644
--- a/src/lib/shm_du_map.c
+++ b/src/lib/shm_du_map.c
@@ -91,13 +91,14 @@ static void garbage_collect(struct shm_du_map * dum)
{
#ifdef SHM_DU_MAP_MULTI_BLOCK
struct shm_du_buff * sdb;
- while ((sdb = get_tail_ptr(dum))->dst_api == -1 &&
- !shm_map_empty(dum))
+ while (!shm_map_empty(dum) &&
+ (sdb = get_tail_ptr(dum))->dst_api == -1)
+
*dum->ptr_tail = (*dum->ptr_tail + sdb->blocks)
& (SHM_BUFFER_SIZE - 1);
#else
- while (get_tail_ptr(dum)->dst_api == -1 &&
- !shm_map_empty(dum))
+ while (!shm_map_empty(dum) &&
+ get_tail_ptr(dum)->dst_api == -1)
*dum->ptr_tail =
(*dum->ptr_tail + 1) & (SHM_BUFFER_SIZE - 1);
@@ -182,7 +183,6 @@ struct shm_du_map * shm_du_map_create()
if (shm_base == MAP_FAILED) {
LOG_DBGF("Failed to map shared memory.");
-
if (shm_unlink(SHM_DU_MAP_FILENAME) == -1)
LOG_DBGF("Failed to remove invalid shm.");
@@ -237,6 +237,7 @@ struct shm_du_map * shm_du_map_open()
shm_fd = shm_open(SHM_DU_MAP_FILENAME, O_RDWR, 0666);
if (shm_fd < 0) {
LOG_DBGF("Failed opening shared memory.");
+ free(dum);
return NULL;
}
@@ -250,10 +251,9 @@ struct shm_du_map * shm_du_map_open()
LOG_DBGF("Failed to map shared memory.");
if (close(shm_fd) == -1)
LOG_DBGF("Failed to close invalid shm.");
-
if (shm_unlink(SHM_DU_MAP_FILENAME) == -1)
LOG_DBGF("Failed to unlink invalid shm.");
-
+ free(dum);
return NULL;
}
@@ -332,7 +332,7 @@ void * shm_du_map_sanitize(void * o)
}
if (ret == ETIMEDOUT) {
- LOG_DBGF("SDU timed out.");
+ LOG_DBGF("SDU timed out (dst: %d).", api);
clean_sdus(dum, api, false);
}
}
@@ -413,8 +413,8 @@ ssize_t shm_du_map_write(struct shm_du_map * dum,
#ifdef SHM_DU_MAP_MULTI_BLOCK
long blocks = 0;
long padblocks = 0;
- int sz = size + sizeof *sdb;
#endif
+ int sz = size + sizeof *sdb;
uint8_t * write_pos;
ssize_t idx = -1;
@@ -439,7 +439,7 @@ ssize_t shm_du_map_write(struct shm_du_map * dum,
++blocks;
}
- if (blocks + *dum->ptr_head > SHM_BUFFER_SIZE - 1)
+ if (blocks + *dum->ptr_head > SHM_BUFFER_SIZE)
padblocks = SHM_BUFFER_SIZE - *dum->ptr_head;
if (!shm_map_free(dum, (blocks + padblocks))) {
@@ -498,8 +498,8 @@ ssize_t shm_du_map_write_b(struct shm_du_map * dum,
#ifdef SHM_DU_MAP_MULTI_BLOCK
long blocks = 0;
long padblocks = 0;
- int sz = size + sizeof *sdb;
#endif
+ int sz = size + sizeof *sdb;
uint8_t * write_pos;
ssize_t idx = -1;
@@ -528,7 +528,7 @@ ssize_t shm_du_map_write_b(struct shm_du_map * dum,
++blocks;
}
- if (blocks + *dum->ptr_head > SHM_BUFFER_SIZE - 1)
+ if (blocks + *dum->ptr_head > SHM_BUFFER_SIZE)
padblocks = SHM_BUFFER_SIZE - *dum->ptr_head;
while (!shm_map_free(dum, (blocks + padblocks))) {
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;
}