diff options
author | Sander Vrijders <[email protected]> | 2016-08-08 10:29:06 +0200 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2016-08-08 10:29:06 +0200 |
commit | c1e1831ded0330771fd175f1c8138ed82c90f9f8 (patch) | |
tree | 617e3002ffa5addb364c01d5cd925d8c609b12b5 | |
parent | d5b71449bc18116444720257ba640e3c597ff6e9 (diff) | |
parent | 8450af0e2950e5a1415e38702d6169cc8a732294 (diff) | |
download | ouroboros-c1e1831ded0330771fd175f1c8138ed82c90f9f8.tar.gz ouroboros-c1e1831ded0330771fd175f1c8138ed82c90f9f8.zip |
Merged in dstaesse/ouroboros/be-bugfixing (pull request #187)
bugfixes
-rw-r--r-- | src/ipcpd/local/main.c | 2 | ||||
-rw-r--r-- | src/ipcpd/shim-eth-llc/main.c | 2 | ||||
-rw-r--r-- | src/ipcpd/shim-udp/main.c | 2 | ||||
-rw-r--r-- | src/irmd/registry.c | 3 | ||||
-rw-r--r-- | src/lib/ipcp.c | 22 | ||||
-rw-r--r-- | src/lib/shm_ap_rbuff.c | 16 | ||||
-rw-r--r-- | src/lib/shm_du_map.c | 24 | ||||
-rw-r--r-- | src/lib/sockets.c | 15 | ||||
-rw-r--r-- | src/tools/cbr/cbr.c | 6 | ||||
-rw-r--r-- | src/tools/cbr/cbr_server.c | 22 | ||||
-rw-r--r-- | src/tools/oping/oping_client.c | 8 | ||||
-rw-r--r-- | src/tools/oping/oping_server.c | 8 |
12 files changed, 69 insertions, 61 deletions
diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index e77a0403..99580a45 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -210,6 +210,8 @@ static void * ipcp_local_sdu_loop(void * o) pthread_rwlock_unlock(&_ap_instance->flows_lock); pthread_rwlock_unlock(&_ipcp->state_lock); + + free(e); } return (void *) 1; diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index 3b70b955..9f6573c9 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -846,6 +846,8 @@ static void * eth_llc_ipcp_sdu_writer(void * o) shm_du_map_remove(shim_data(_ipcp)->dum, e->index); pthread_rwlock_unlock(&_ipcp->state_lock); + + free(e); } return (void *) 1; diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index 49fd7772..9d6d15a7 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -802,6 +802,8 @@ static void * ipcp_udp_sdu_loop(void * o) shm_du_map_remove(_ap_instance->dum, e->index); pthread_rwlock_unlock(&_ipcp->state_lock); + + free(e); } return (void *) 1; diff --git a/src/irmd/registry.c b/src/irmd/registry.c index fc7213ab..7745efaa 100644 --- a/src/irmd/registry.c +++ b/src/irmd/registry.c @@ -253,9 +253,6 @@ static void reg_entry_destroy(struct reg_entry * e) if (e->name != NULL) free(e->name); - if (e->req_ae_name != NULL) - free(e->req_ae_name); - list_for_each_safe(pos, n, &e->reg_apis) { struct reg_api * i = list_entry(pos, struct reg_api, next); reg_api_destroy(i); 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; } diff --git a/src/tools/cbr/cbr.c b/src/tools/cbr/cbr.c index 0cce50db..65783119 100644 --- a/src/tools/cbr/cbr.c +++ b/src/tools/cbr/cbr.c @@ -75,8 +75,10 @@ int main(int argc, char ** argv) bool server = false; - /* FIXME: should be argv[0] */ - ap_init(argv[0]); + if (ap_init(argv[0]) < 0) { + printf("Failed to init.\n"); + exit(EXIT_FAILURE); + } server_settings.interval = 1; /* One second reporting interval */ server_settings.timeout = 1; diff --git a/src/tools/cbr/cbr_server.c b/src/tools/cbr/cbr_server.c index 1890c842..fc23363f 100644 --- a/src/tools/cbr/cbr_server.c +++ b/src/tools/cbr/cbr_server.c @@ -47,18 +47,11 @@ pthread_cond_t fds_signal; void shutdown_server(int signo, siginfo_t * info, void * c) { - int i; - switch(signo) { case SIGINT: case SIGTERM: case SIGHUP: pthread_cancel(listen_thread); - - for (i = 0; i < THREADS_SIZE; i++) { - pthread_cancel(threads[i]); - } - default: return; } @@ -226,19 +219,18 @@ int server_main() exit(EXIT_FAILURE); } - for (i = 0; i < THREADS_SIZE; i++) { - pthread_create(&threads[i], NULL, - worker, NULL); - } + for (i = 0; i < THREADS_SIZE; i++) + pthread_create(&threads[i], NULL, worker, NULL); - pthread_create(&listen_thread, NULL, - listener, NULL); + pthread_create(&listen_thread, NULL, listener, NULL); pthread_join(listen_thread, NULL); - for (i = 0; i < THREADS_SIZE; i++) { + for (i = 0; i < THREADS_SIZE; i++) + pthread_cancel(threads[i]); + + for (i = 0; i < THREADS_SIZE; i++) pthread_join(threads[i], NULL); - } return 0; } diff --git a/src/tools/oping/oping_client.c b/src/tools/oping/oping_client.c index 7693ce41..0d4a10af 100644 --- a/src/tools/oping/oping_client.c +++ b/src/tools/oping/oping_client.c @@ -115,7 +115,8 @@ void * writer(void * o) { int * fdp = (int *) o; struct timespec now; - struct timespec wait = {client.interval / 1000, client.interval % 1000}; + struct timespec wait = {client.interval / 1000, + (client.interval % 1000) * MILLION}; struct oping_msg * msg; char * buf = malloc(client.size); @@ -132,12 +133,15 @@ void * writer(void * o) printf("Pinging %s with %d bytes of data:\n\n", client.s_apn, client.size); + pthread_cleanup_push((void (*) (void *)) free, buf); + while (client.sent < client.count) { nanosleep(&wait, NULL); msg->id = htonl(client.sent); if (flow_write(*fdp, buf, client.size) == -1) { printf("Failed to send SDU.\n"); flow_dealloc(*fdp); + free(buf); return (void *) -1; } @@ -148,6 +152,8 @@ void * writer(void * o) pthread_mutex_unlock(&client.lock); } + pthread_cleanup_pop(true); + return (void *) 0; } diff --git a/src/tools/oping/oping_server.c b/src/tools/oping/oping_server.c index eb0b511b..a5021cba 100644 --- a/src/tools/oping/oping_server.c +++ b/src/tools/oping/oping_server.c @@ -37,9 +37,7 @@ void shutdown_server(int signo, siginfo_t * info, void * c) case SIGINT: case SIGTERM: case SIGHUP: - pthread_cancel(server.server_pt); pthread_cancel(server.accept_pt); - pthread_cancel(server.cleaner_pt); default: return; } @@ -154,8 +152,12 @@ int server_main() pthread_create(&server.accept_pt, NULL, accept_thread, NULL); pthread_create(&server.server_pt, NULL, server_thread, NULL); - pthread_join(server.server_pt, NULL); pthread_join(server.accept_pt, NULL); + + pthread_cancel(server.server_pt); + pthread_cancel(server.cleaner_pt); + + pthread_join(server.server_pt, NULL); pthread_join(server.cleaner_pt, NULL); return 0; |