Improve configuration file handling

Put logic in config.c so it can be shared between repmgr and repmgrd.
This commit is contained in:
Ian Barwick
2015-03-03 15:39:56 +09:00
parent 6b1f55ff1b
commit 46888de77f
4 changed files with 67 additions and 41 deletions

View File

@@ -17,6 +17,8 @@
*
*/
#include <sys/stat.h> /* 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)
{

View File

@@ -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

View File

@@ -30,7 +30,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h> /* for stat() */
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
@@ -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() */

View File

@@ -154,6 +154,7 @@ main(int argc, char **argv)
int optindex;
int c;
bool daemonize = false;
FILE *fd;
int server_version_num = 0;