From 46888de77fcf6cab1ed6039ecffa47bb7327148e Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 3 Mar 2015 15:39:56 +0900 Subject: [PATCH] Improve configuration file handling Put logic in config.c so it can be shared between repmgr and repmgrd. --- config.c | 77 +++++++++++++++++++++++++++++++++++++++++++++---------- config.h | 4 +-- repmgr.c | 26 ------------------- repmgrd.c | 1 + 4 files changed, 67 insertions(+), 41 deletions(-) diff --git a/config.c b/config.c index 90c527ab..96783c0a 100644 --- a/config.c +++ b/config.c @@ -17,6 +17,8 @@ * */ +#include /* for stat() */ + #include "config.h" #include "log.h" #include "strutil.h" @@ -37,14 +39,72 @@ static void tablespace_list_append(t_configuration_options *options, const char * reload_config() */ bool -parse_config(const char *config_file, t_configuration_options * options) +parse_config(const char *config_file, t_configuration_options *options) { char *s, buff[MAXLINELENGTH]; + char config_file_buf[MAXLEN]; char name[MAXLEN]; char value[MAXLEN]; + bool config_file_provided = true; + FILE *fp; - FILE *fp = fopen(config_file, "r"); + /* Sanity checks */ + + /* + * If a configuration file was provided, check it exists, otherwise + * emit an error + */ + if (config_file[0]) + { + struct stat config; + if(stat(config_file, &config) != 0) + { + log_err(_("Provided configuration file '%s' not found: %s\n"), + config_file, + strerror(errno) + ); + exit(ERR_BAD_CONFIG); + } + strncpy(config_file_buf, config_file, MAXLEN); + config_file_provided = true; + } + + /* + * If no configuration file was provided, set to a default file + * which `parse_config()` will attempt to read if it exists + */ + else + { + strncpy(config_file_buf, DEFAULT_CONFIG_FILE, MAXLEN); + } + + + fp = fopen(config_file_buf, "r"); + + /* + * Since some commands don't require a config file at all, not having one + * isn't necessarily a problem. + * + * If the user explictly provided a configuration file and we can't + * read it we'll raise an error. + * + * If no configuration file was provided, we'll try and read the default\ + * file if it exists and is readable, but won't worry if it's not. + */ + if (fp == NULL) + { + if(config_file_provided) + { + log_err(_("Unable to open provided configuration file '%s' - terminating\n"), config_file_buf); + exit(ERR_BAD_CONFIG); + } + + log_notice(_("No configuration file provided and default file '%s' not found - " + "continuing with default values\n"), + DEFAULT_CONFIG_FILE); + return false; + } /* Initialize */ memset(options->cluster_name, 0, sizeof(options->cluster_name)); @@ -74,17 +134,7 @@ parse_config(const char *config_file, t_configuration_options * options) options->tablespace_mapping.head = NULL; options->tablespace_mapping.tail = NULL; - /* - * Since some commands don't require a config file at all, not having one - * isn't necessarily a problem. - */ - if (fp == NULL) - { - log_notice(_("No configuration file provided and default file '%s' not found - " - "continuing with default values\n"), - config_file); - return false; - } + /* Read next line */ while ((s = fgets(buff, sizeof buff, fp)) != NULL) @@ -268,6 +318,7 @@ reload_config(char *config_file, t_configuration_options * orig_options) * Re-read the configuration file: repmgr.conf */ log_info(_("Reloading configuration file and updating repmgr tables\n")); + parse_config(config_file, &new_options); if (new_options.node == -1) { diff --git a/config.h b/config.h index 8a434229..ccec3004 100644 --- a/config.h +++ b/config.h @@ -67,9 +67,9 @@ typedef struct #define T_CONFIGURATION_OPTIONS_INITIALIZER { "", -1, NO_UPSTREAM_NODE, "", MANUAL_FAILOVER, -1, "", "", "", "", "", "", "", -1, -1, -1, "", "", "", "", 0, 0, 0, {NULL, NULL} } -bool parse_config(const char *config_file, t_configuration_options * options); +bool parse_config(const char *config_file, t_configuration_options *options); void parse_line(char *buff, char *name, char *value); char *trim(char *s); -bool reload_config(char *config_file, t_configuration_options * orig_options); +bool reload_config(char *config_file, t_configuration_options *orig_options); #endif diff --git a/repmgr.c b/repmgr.c index 47dcb73e..2317093b 100644 --- a/repmgr.c +++ b/repmgr.c @@ -30,7 +30,6 @@ #include #include -#include /* for stat() */ #include #include #include @@ -404,31 +403,6 @@ main(int argc, char **argv) strncpy(runtime_options.masterport, DEFAULT_MASTER_PORT, MAXLEN); } - /* - * If a configuration file was provided, check it exists, otherwise - * emit an error - */ - if (runtime_options.config_file[0]) - { - struct stat config; - if(stat(runtime_options.config_file, &config) != 0) - { - log_err(_("Provided configuration file '%s' not found: %s\n"), - runtime_options.config_file, - strerror(errno) - ); - exit(ERR_BAD_CONFIG); - } - } - - /* - * If no configuration file was provided, set to a default file - * which `parse_config()` will attempt to read if it exists - */ - else - { - strncpy(runtime_options.config_file, DEFAULT_CONFIG_FILE, MAXLEN); - } if (runtime_options.verbose) /* Logging is not yet set up, so using printf() */ diff --git a/repmgrd.c b/repmgrd.c index 5de80494..fcbf839c 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -154,6 +154,7 @@ main(int argc, char **argv) int optindex; int c; bool daemonize = false; + FILE *fd; int server_version_num = 0;