From: Alberto Leiva Popper Date: Thu, 2 May 2019 17:45:27 +0000 (-0500) Subject: Bits of random cleanup X-Git-Tag: v0.0.2~41^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc6abb632571a56f951ffb3359d47632321ab846;p=thirdparty%2FFORT-validator.git Bits of random cleanup --- diff --git a/src/main.c b/src/main.c index f3813e35..2051f16d 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,6 @@ start_rtr_server(void) goto revert_clients; error = rtr_listen(); - rtr_cleanup(); /* TODO shouldn't this only happen on !error? */ slurm_cleanup(); revert_clients: @@ -60,12 +59,14 @@ main(int argc, char **argv) goto revert_rsync; error = extension_init(); if (error) - goto revert_rsync; + goto revert_nid; error = (config_get_server_address() != NULL) ? start_rtr_server() : validate_into_console(); +revert_nid: + nid_destroy(); revert_rsync: rsync_destroy(); revert_config: diff --git a/src/nid.c b/src/nid.c index 408f48a1..7bbb148c 100644 --- a/src/nid.c +++ b/src/nid.c @@ -78,6 +78,12 @@ nid_init(void) return 0; } +void +nid_destroy(void) +{ + OBJ_cleanup(); +} + int nid_rpkiManifest(void) { return NID_rpkiManifest; diff --git a/src/nid.h b/src/nid.h index 64cdac86..2b34afc1 100644 --- a/src/nid.h +++ b/src/nid.h @@ -2,6 +2,7 @@ #define SRC_NID_H_ int nid_init(void); +void nid_destroy(void); int nid_rpkiManifest(void); int nid_signedObject(void); diff --git a/src/rtr/db/vrps.c b/src/rtr/db/vrps.c index b5b753ed..1b262684 100644 --- a/src/rtr/db/vrps.c +++ b/src/rtr/db/vrps.c @@ -44,7 +44,9 @@ vrps_init(void) { int error; - state.base = NULL; + state.base = roa_table_create(); + if (state.base == NULL) + return pr_enomem(); deltas_db_init(&state.deltas); @@ -65,6 +67,7 @@ vrps_init(void) error = pthread_rwlock_init(&lock, NULL); if (error) { deltas_db_cleanup(&state.deltas, delta_destroy); + roa_table_put(state.base); return pr_errno(error, "pthread_rwlock_init() errored"); } diff --git a/src/rtr/rtr.c b/src/rtr/rtr.c index e8077c93..4662da9a 100644 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@ -22,6 +22,7 @@ /* TODO (next iteration) Support both RTR v0 and v1 */ #define RTR_VERSION_SUPPORTED RTR_V0 +/* TODO (urgent) this needs to be atomic */ volatile bool loop; struct thread_node { @@ -315,6 +316,7 @@ init_signal_handler(void) act.sa_flags = SA_SIGINFO; act.sa_sigaction = signal_handler; + /* TODO is this really legal? @act might need to be global. */ error = sigaction(SIGINT, &act, NULL); if (error) { pr_errno(errno, "Error initializing signal handler"); @@ -323,6 +325,23 @@ init_signal_handler(void) return error; } +/* Wait for threads to end gracefully */ +static void +wait_threads(void) +{ + struct thread_node *ptr; + + loop = false; + while (!SLIST_EMPTY(&threads)) { + ptr = SLIST_FIRST(&threads); + SLIST_REMOVE_HEAD(&threads, next); + /* TODO interrupt then join? Is this legal? */ + pthread_kill(ptr->tid, SIGINT); + pthread_join(ptr->tid, NULL); + free(ptr); + } +} + /* * Starts the server, using the current thread to listen for RTR client * requests. @@ -342,7 +361,7 @@ rtr_listen(void) /* Server ready, start updates thread */ error = updates_daemon_start(); if (error) - return error; + goto revert_server_socket; /* Init global vars */ loop = true; @@ -350,25 +369,14 @@ rtr_listen(void) error = init_signal_handler(); if (error) - return error; - - return handle_client_connections(server_fd); -} + goto revert_updates_daemon; -void -rtr_cleanup(void) -{ - struct thread_node *ptr; + error = handle_client_connections(server_fd); + wait_threads(); +revert_updates_daemon: updates_daemon_destroy(); - - /* Wait for threads to end gracefully */ - loop = false; - while (!SLIST_EMPTY(&threads)) { - ptr = SLIST_FIRST(&threads); - SLIST_REMOVE_HEAD(&threads, next); - pthread_kill(ptr->tid, SIGINT); - pthread_join(ptr->tid, NULL); - free(ptr); - } +revert_server_socket: + close(server_fd); + return error; } diff --git a/src/rtr/rtr.h b/src/rtr/rtr.h index 2d445c8d..7353be96 100644 --- a/src/rtr/rtr.h +++ b/src/rtr/rtr.h @@ -1,9 +1,6 @@ #ifndef RTR_RTR_H_ #define RTR_RTR_H_ -#include "common.h" - int rtr_listen(void); -void rtr_cleanup(void); #endif /* RTR_RTR_H_ */