From 1a98cf44b2f1a31be6552edc8dd9253a9790c9c7 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 24 Aug 2011 10:47:13 +0200 Subject: [PATCH] [Aggregator] Fix config parsing memory leaks Same thing as for the Listener: avoid using exit(), prefer return an int value instead. --- owlps-aggregator/owlps-aggregator.h | 8 ++-- owlps-aggregator/owlps-aggregatord.c | 72 ++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/owlps-aggregator/owlps-aggregator.h b/owlps-aggregator/owlps-aggregator.h index 930ce88..bf07eaf 100644 --- a/owlps-aggregator/owlps-aggregator.h +++ b/owlps-aggregator/owlps-aggregator.h @@ -86,10 +86,10 @@ typedef struct _ap_list /* Function headers */ -void initialise_configuration(int argc, char **argv) ; -void parse_config_file(int argc, char **argv) ; -void parse_command_line(int argc, char **argv) ; -void check_configuration(void) ; +int initialise_configuration(int argc, char **argv) ; +int parse_config_file(int argc, char **argv) ; +int parse_command_line(int argc, char **argv) ; +int check_configuration(void) ; int read_loop(int sockfd) ; void got_request(owl_captured_request request) ; diff --git a/owlps-aggregator/owlps-aggregatord.c b/owlps-aggregator/owlps-aggregatord.c index d357514..557a931 100644 --- a/owlps-aggregator/owlps-aggregatord.c +++ b/owlps-aggregator/owlps-aggregatord.c @@ -42,10 +42,14 @@ int main(int argc, char **argv) monitor_aps_thread, // APs monitoring thread autocalibration_hello_thread ; // Hello messages reception thread uint_fast16_t listening_port ; - int sockfd ; // UDP listening socket + int sockfd = -1 ; // UDP listening socket + + owl_run = owl_true ; program_name = argv[0] ; - initialise_configuration(argc, argv) ; + ret = initialise_configuration(argc, argv) ; + if (! owl_run) + goto exit ; /* Set up signal handlers */ action.sa_flags = 0 ; @@ -98,7 +102,6 @@ int main(int argc, char **argv) } } - owl_run = owl_true ; ret = read_loop(sockfd) ; /* Wait for the threads to terminate */ @@ -140,7 +143,8 @@ int main(int argc, char **argv) /* Last cleaning tasks */ exit: - close(sockfd) ; // Close socket + if (sockfd >= 0) + close(sockfd) ; // Close socket free_request_list() ; free_ap_list() ; cfg_free(cfg) ; // Clean configuration @@ -154,11 +158,28 @@ int main(int argc, char **argv) -void initialise_configuration(int argc, char **argv) +/* + * Read the configuration from both the command line and the + * configuration file. + * Returns an error code, or 0 in case of success. If the program should + * stop (because of a special option or a configuration error), owl_run + * is set to false. + */ +int initialise_configuration(int argc, char **argv) { - parse_config_file(argc, argv) ; - parse_command_line(argc, argv) ; - check_configuration() ; + int ret ; + + ret = parse_config_file(argc, argv) ; + if (! owl_run) + return ret ; + + ret = parse_command_line(argc, argv) ; + if (! owl_run) + return ret ; + + ret = check_configuration() ; + if (! owl_run) + return ret ; /* Configuration printing */ if (VERBOSE_INFO) @@ -166,10 +187,12 @@ void initialise_configuration(int argc, char **argv) fprintf(stderr, "Configuration:\n") ; cfg_print(cfg, stderr) ; } + + return 0 ; } -void parse_config_file(int argc, char **argv) +int parse_config_file(int argc, char **argv) { // Config file options for confuse cfg_opt_t opts[] = @@ -221,10 +244,12 @@ void parse_config_file(int argc, char **argv) break ; case 'h' : print_usage() ; - exit(0) ; + owl_run = owl_false ; + return EXIT_SUCCESS ; case 'V' : print_version() ; - exit(0) ; + owl_run = owl_false ; + return EXIT_SUCCESS ; } } @@ -250,13 +275,16 @@ void parse_config_file(int argc, char **argv) "Error! Parsing of configuration file « %s » failed!\n", config_file) ; free(config_file) ; - exit(ERR_PARSING_CONFIG_FILE) ; + owl_run = owl_false ; + return ERR_PARSING_CONFIG_FILE ; } free(config_file) ; + + return 0 ; } -void parse_command_line(int argc, char **argv) +int parse_command_line(int argc, char **argv) { int opt ; @@ -281,9 +309,6 @@ void parse_command_line(int argc, char **argv) break ; case 'f' : // Config file break ; // (already parsed) - case 'h' : - print_usage() ; - exit(0) ; case 'i' : cfg_setstr(cfg, "positioner_ip", optarg) ; break ; @@ -316,20 +341,24 @@ void parse_command_line(int argc, char **argv) break ; default : print_usage() ; - exit(ERR_BAD_USAGE) ; + owl_run = owl_false ; + return ERR_BAD_USAGE ; } } + + return 0 ; } -void check_configuration() +int check_configuration() { // output_file // if (cfg_getstr(cfg, "output_file")[0] == '\0') { fprintf(stderr, "Error! You must specify an output file.\n") ; print_usage() ; - exit(ERR_BAD_USAGE) ; + owl_run = owl_false ; + return ERR_BAD_USAGE ; } // listening_port // @@ -358,7 +387,8 @@ void check_configuration() fprintf(stderr, "Error! You must specify the IP address of the" " localisation server.\n") ; print_usage() ; - exit(ERR_BAD_USAGE) ; + owl_run = owl_false ; + return ERR_BAD_USAGE ; } // aggregate_timeout // @@ -405,6 +435,8 @@ void check_configuration() " failing back to the default value.\n") ; cfg_setint(cfg, "ap_check_interval", DEFAULT_AP_CHECK_INTERVAL) ; } + + return 0 ; }