From 4add03efa23a5b1a0b52362442b897a1805b7821 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 5 Mar 2019 16:13:08 +0100 Subject: [PATCH] Improve free and error handling --- Dockerfile | 2 +- README.md | 15 ++++++++++++++- scripts/xp1-server | 1 + src/evt_core.c | 41 ++++++++++++++++++++++++++++++++--------- src/evt_core.h | 3 +++ src/meas_lat.c | 31 +++++++++++++++++-------------- 6 files changed, 68 insertions(+), 25 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0121436..faf4e44 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ RUN mkdir out && \ FROM fedora:29 RUN dnf install -y glib2 tor WORKDIR /home/donar -RUN chown -R 1000 /home/donar +RUN mkdir /home/donar/shared && mkdir /home/donar/res && chown -R 1000 /home/donar USER 1000 ENV HOME /home/donar COPY --from=builder /home/donar-build/out/donar /usr/local/bin diff --git a/README.md b/README.md index 253abf5..03cac15 100644 --- a/README.md +++ b/README.md @@ -21,5 +21,18 @@ sudo docker push registry.gitlab.inria.fr/qdufour/donar ``` ``` -sudo docker run -t -i -v `pwd`/xp1:/home/donar/res registry.gitlab.inria.fr/qdufour/donar xp1-server +mkdir -p ./{xp1-shared,xp1-res} +sudo chown -R 1000 ./{xp1-shared,xp1-res} + +sudo docker run -t -i \ + -v `pwd`/xp1-shared:/home/donar/shared \ + registry.gitlab.inria.fr/qdufour/donar \ + xp1-server + +sudo docker run -t -i \ + -v `pwd`/xp1-res:/home/donar/res \ + -v `pwd`/xp1-shared:/home/donar/shared \ + registry.gitlab.inria.fr/qdufour/donar \ + xp1-client 1000 100 100 + ``` diff --git a/scripts/xp1-server b/scripts/xp1-server index 30b3b26..1cfc4d9 100755 --- a/scripts/xp1-server +++ b/scripts/xp1-server @@ -3,5 +3,6 @@ tor -f /etc/torrc & sleep 2 mkdir -p ./shared cd ./shared +pwd donar -a naive -s -e 9000 & udpecho -p 9000 diff --git a/src/evt_core.c b/src/evt_core.c index 1de3786..01a7d7f 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -109,18 +109,23 @@ struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd) { if (fdinfo == NULL) return NULL; cat = fdinfo->cat; - // 2. Remove structure from urltofd and socklist - g_hash_table_remove(ctx->urltofd, fdinfo->url); - g_hash_table_remove(ctx->socklist, &fd); - - // 3. Update category + // 2. Update category for (int i = 0; i < cat->socklist->len; i++) { - if (g_array_index(cat->socklist, int, i) == fd) { + if (g_array_index(cat->socklist, struct evt_core_fdinfo*, i) == fdinfo) { + printf("Remove fd\n"); g_array_remove_index(cat->socklist, i); } } - // 4. Return file descriptor's category + // 3. Remove structure from urltofd and socklist + g_hash_table_remove(ctx->urltofd, fdinfo->url); + g_hash_table_remove(ctx->socklist, &fd); // Will be free here + + // 4. Close and remove file descriptor + epoll_ctl(ctx->epollfd, EPOLL_CTL_DEL, fd, NULL); + close(fd); + + // 5. Return file descriptor's category return cat; } @@ -155,7 +160,13 @@ void evt_core_loop(struct evt_core_ctx* ctx) { int error = 0; socklen_t errlen = sizeof(error); if (getsockopt(err_fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0) { - printf("Socket %d error = %s\n", err_fd, strerror(error)); + fprintf(stderr, "Socket %d error = %s\n", err_fd, strerror(error)); + } else if (errno == EBADF) { + fprintf(stderr, "fd=%d is invalid. Probably closed\n", err_fd); + continue; + } else { + fprintf(stderr, "Using fd=%d as socket produced error: ", err_fd); + perror("getsockopt"); } fprintf(stderr, "Epoll Err Event. "); } @@ -168,7 +179,7 @@ void evt_core_loop(struct evt_core_ctx* ctx) { } evt_core_rm_fd (ctx, err_fd); } else { - fprintf(stderr, "The file descriptor is not registered in a category, this is probably a logic error\n"); + fprintf(stderr, "The file descriptor %d is not registered in a category, this is probably a logic error\n", err_fd); close (err_fd); } continue; @@ -191,10 +202,22 @@ void evt_core_loop(struct evt_core_ctx* ctx) { struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd) { return g_hash_table_lookup (ctx->socklist, &fd); } + struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url) { return g_hash_table_lookup (ctx->urltofd, url); } +struct evt_core_cat* evt_core_get_from_cat(struct evt_core_ctx* ctx, char* name) { + return g_hash_table_lookup (ctx->catlist, name); +} + +struct evt_core_fdinfo* evt_core_get_first_from_cat(struct evt_core_ctx* ctx, char* name) { + struct evt_core_cat* cat = g_hash_table_lookup (ctx->catlist, name); + if (cat == NULL) return NULL; + if (cat->socklist == NULL || cat->socklist->len <= 0) return NULL; + return g_array_index(cat->socklist, struct evt_core_fdinfo*, 0); +} + void evt_core_free_app_ctx_simple(void* v) { free(v); } diff --git a/src/evt_core.h b/src/evt_core.h index aadadff..2f6d96a 100644 --- a/src/evt_core.h +++ b/src/evt_core.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -51,3 +52,5 @@ void evt_core_loop(struct evt_core_ctx* ctx); struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd); struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url); void evt_core_free_app_ctx_simple(void* v); +struct evt_core_cat* evt_core_get_from_cat(struct evt_core_ctx* ctx, char* name); +struct evt_core_fdinfo* evt_core_get_first_from_cat(struct evt_core_ctx* ctx, char* name); diff --git a/src/meas_lat.c b/src/meas_lat.c index 0f72e65..f23c7e5 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -8,7 +8,6 @@ #include "net_tools.h" struct measure_conf { - int udp_fd; uint64_t max_measure; uint64_t payload_size; char* payload; @@ -20,20 +19,13 @@ struct packet_header { struct timespec emit_time; }; -struct measure_conf* create_measure_conf(int udp_sock, int max_mes, int plsize) { +struct measure_conf* create_measure_conf(int max_mes, int plsize) { struct measure_conf* mc = malloc(sizeof(struct measure_conf)); if (mc == NULL) { perror("Malloc failed"); exit(EXIT_FAILURE); } - if (udp_sock >= 0) { - mc->udp_fd = dup(udp_sock); - if (mc->udp_fd == -1) { - perror("Dup failed"); - exit(EXIT_FAILURE); - } - } mc->counter = 0; mc->max_measure = max_mes; mc->payload_size = plsize; @@ -45,6 +37,12 @@ struct measure_conf* create_measure_conf(int udp_sock, int max_mes, int plsize) return mc; } +void free_mesure_conf(void* v) { + struct measure_conf* mc = (struct measure_conf*)v; + free(mc->payload); + free(mc); +} + int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { ssize_t res; int secs, nsecs; @@ -99,17 +97,21 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { perror("clock_gettime error"); exit(EXIT_FAILURE); } - s = send(mc->udp_fd, mc->payload, mc->payload_size, 0); + struct evt_core_fdinfo* udpinfo = evt_core_get_first_from_cat (ctx, "udp-read"); + if (udpinfo == NULL) { + printf("No connection yet\n"); + return 1; + } + s = send(udpinfo->fd, mc->payload, mc->payload_size, 0); if (s < 0) { perror("Send error"); - exit(EXIT_FAILURE); + //exit(EXIT_FAILURE); } return 1; } void free_timer_conf(void* v) { struct measure_conf* mc = v; - close(mc->udp_fd); free(mc->payload); free(mc); } @@ -151,7 +153,8 @@ int register_udp_socket(struct evt_core_ctx* evts, char* host, char* port, int c fdinfo.fd = udp_sock; fdinfo.cat->name = "udp-read"; - fdinfo.other = create_measure_conf (-1, count, size); + fdinfo.other = create_measure_conf (count, size); + fdinfo.free_other = free_mesure_conf; sprintf(fdinfo.url, "udp:read:%s:%s", host, port); evt_core_add_fd (evts, &fdinfo); printf("--- UDP socket registered\n"); @@ -187,7 +190,7 @@ void register_timer(struct evt_core_ctx* evts, int udp, int interval, int count, exit(EXIT_FAILURE); } fdinfo.cat->name = "timer"; - fdinfo.other = create_measure_conf(udp, count, size); + fdinfo.other = create_measure_conf(count, size); fdinfo.free_other = free_timer_conf; sprintf(fdinfo.url, "timer:%d:%d", interval, count); evt_core_add_fd (evts, &fdinfo);