[Listener] Fix config parsing memory leaks

The configuration parsing function could call exit() (e.g. with -h, -V,
or in case of error), implying a memory leak. This is fixed.
Additionally, minor cleaning in the configuration functions.
This commit is contained in:
Matteo Cypriani 2011-08-23 21:48:59 +02:00
parent a301c87c1d
commit 8f48a22482
2 changed files with 73 additions and 32 deletions

View File

@ -164,14 +164,14 @@ enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ;
/* Function headers */ /* Function headers */
void initialise_configuration(int argc, char **argv) ; int initialise_configuration(int argc, char **argv) ;
void parse_config_file(int argc, char **argv) ; int parse_config_file(int argc, char **argv) ;
void parse_command_line(int argc, char **argv) ; int parse_command_line(int argc, char **argv) ;
void parse_main_options(int argc, char **argv) ; int parse_main_options(int argc, char **argv) ;
#ifdef USE_PTHREAD #ifdef USE_PTHREAD
void parse_calibration_data(int argc, char **argv) ; int parse_calibration_data(int argc, char **argv) ;
#endif // USE_PTHREAD #endif // USE_PTHREAD
void check_configuration(void) ; int check_configuration(void) ;
#ifdef DEBUG #ifdef DEBUG
void print_configuration(void) ; void print_configuration(void) ;
#endif // DEBUG #endif // DEBUG

View File

@ -50,7 +50,7 @@ owl_bool coordinates_provided = owl_false ;
#endif // USE_PTHREAD #endif // USE_PTHREAD
#ifdef USE_CONFIG_FILE #ifdef USE_CONFIG_FILE
cfg_t *cfg ; // Configuration structure cfg_t *cfg = NULL ; // Configuration structure
#else // USE_CONFIG_FILE #else // USE_CONFIG_FILE
/* If we do not use libconfuse, we declare a structure to store getopt /* If we do not use libconfuse, we declare a structure to store getopt
@ -119,11 +119,13 @@ int main(int argc, char *argv[])
autocalibration_hello_thread ; autocalibration_hello_thread ;
#endif // USE_PTHREAD #endif // USE_PTHREAD
program_name = argv[0] ;
initialise_configuration(argc, argv) ;
owl_run = owl_true ; owl_run = owl_true ;
program_name = argv[0] ;
ret = initialise_configuration(argc, argv) ;
if (! owl_run)
goto exit ;
/* Set up signal handlers */ /* Set up signal handlers */
action.sa_flags = 0 ; action.sa_flags = 0 ;
sigemptyset(&action.sa_mask) ; sigemptyset(&action.sa_mask) ;
@ -245,19 +247,38 @@ 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) ; int ret ;
parse_command_line(argc, argv) ;
check_configuration() ; 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 ;
if (VERBOSE_INFO) if (VERBOSE_INFO)
print_configuration() ; print_configuration() ;
return 0 ;
} }
void parse_config_file(int argc, char **argv) int parse_config_file(int argc, char **argv)
{ {
#ifdef USE_CONFIG_FILE #ifdef USE_CONFIG_FILE
// If we use libconfuse, we declare options: // If we use libconfuse, we declare options:
@ -343,10 +364,12 @@ void parse_config_file(int argc, char **argv)
break ; break ;
case 'h' : case 'h' :
print_usage() ; print_usage() ;
exit(0) ; owl_run = owl_false ;
return EXIT_SUCCESS ;
case 'V' : case 'V' :
print_version() ; print_version() ;
exit(0) ; owl_run = owl_false ;
return EXIT_SUCCESS ;
} }
} }
@ -373,25 +396,37 @@ void parse_config_file(int argc, char **argv)
"Error! Parsing of configuration file « %s » failed!\n", "Error! Parsing of configuration file « %s » failed!\n",
config_file) ; config_file) ;
free(config_file) ; free(config_file) ;
exit(ERR_PARSING_CONFIG_FILE) ; owl_run = owl_false ;
return ERR_PARSING_CONFIG_FILE ;
} }
free(config_file) ; free(config_file) ;
#endif // USE_CONFIG_FILE #endif // USE_CONFIG_FILE
return 0 ;
} }
void parse_command_line(int argc, char **argv) int parse_command_line(int argc, char **argv)
{ {
parse_main_options(argc, argv) ; int ret ;
ret = parse_main_options(argc, argv) ;
if (! owl_run)
return ret ;
#ifdef USE_PTHREAD #ifdef USE_PTHREAD
parse_calibration_data(argc, argv) ; ret = parse_calibration_data(argc, argv) ;
if (! owl_run)
return ret ;
#endif // USE_PTHREAD #endif // USE_PTHREAD
return 0 ;
} }
void parse_main_options(int argc, char **argv) int parse_main_options(int argc, char **argv)
{ {
int opt ; int opt ;
@ -430,8 +465,6 @@ void parse_main_options(int argc, char **argv)
break ; break ;
case 'f' : // Config file case 'f' : // Config file
break ; // (already parsed) break ; // (already parsed)
case 'h' : // Usage
break ; // (already parsed)
case 'H' : case 'H' :
#ifdef USE_PTHREAD #ifdef USE_PTHREAD
SET_AUTOCALIBRATION_HELLO_DELAY(strtol(optarg, NULL, 0)) ; SET_AUTOCALIBRATION_HELLO_DELAY(strtol(optarg, NULL, 0)) ;
@ -483,22 +516,23 @@ void parse_main_options(int argc, char **argv)
case 'v' : case 'v' :
INCREMENT_VERBOSE() ; INCREMENT_VERBOSE() ;
break ; break ;
case 'V' : // Version
break ; // (already parsed)
case 'w' : case 'w' :
SET_WIFI_IFACE(optarg) ; SET_WIFI_IFACE(optarg) ;
break ; break ;
default : default :
print_usage() ; print_usage() ;
exit(ERR_BAD_USAGE) ; owl_run = owl_false ;
return ERR_BAD_USAGE ;
} }
} }
return 0 ;
} }
#ifdef USE_PTHREAD #ifdef USE_PTHREAD
void parse_calibration_data(int argc, char **argv) int parse_calibration_data(int argc, char **argv)
{ {
/* Parse remaining arguments (possible calibration data) */ /* Parse remaining arguments (possible calibration data) */
if (argc - optind != 0) if (argc - optind != 0)
@ -514,15 +548,18 @@ void parse_calibration_data(int argc, char **argv)
else // Bad number of arguments else // Bad number of arguments
{ {
print_usage() ; print_usage() ;
exit(ERR_BAD_USAGE) ; owl_run = owl_false ;
return ERR_BAD_USAGE ;
} }
} }
return 0 ;
} }
#endif // USE_PTHREAD #endif // USE_PTHREAD
void check_configuration() int check_configuration()
{ {
switch (GET_MODE()) switch (GET_MODE())
{ {
@ -533,7 +570,8 @@ void check_configuration()
default : default :
fprintf(stderr, "Error! Unknown mode « %c ».\n", (char) GET_MODE()) ; fprintf(stderr, "Error! Unknown mode « %c ».\n", (char) GET_MODE()) ;
print_usage() ; print_usage() ;
exit(ERR_BAD_USAGE) ; owl_run = owl_false ;
return ERR_BAD_USAGE ;
} }
if (GET_RTAP_IFACE()[0] == '\0') if (GET_RTAP_IFACE()[0] == '\0')
@ -541,7 +579,8 @@ void check_configuration()
fprintf(stderr, "Error! You must specify a radiotap interface" fprintf(stderr, "Error! You must specify a radiotap interface"
" for the capture.\n") ; " for the capture.\n") ;
print_usage() ; print_usage() ;
exit(ERR_BAD_USAGE) ; owl_run = owl_false ;
return ERR_BAD_USAGE ;
} }
if (GET_WIFI_IFACE()[0] == '\0') if (GET_WIFI_IFACE()[0] == '\0')
@ -611,6 +650,8 @@ void check_configuration()
} }
} }
#endif // USE_PTHREAD #endif // USE_PTHREAD
return 0 ;
} }