mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-26 00:26:30 +00:00
Improve configuration file handling
Put logic in config.c so it can be shared between repmgr and repmgrd.
This commit is contained in:
77
config.c
77
config.c
@@ -17,6 +17,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sys/stat.h> /* for stat() */
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "strutil.h"
|
#include "strutil.h"
|
||||||
@@ -37,14 +39,72 @@ static void tablespace_list_append(t_configuration_options *options, const char
|
|||||||
* reload_config()
|
* reload_config()
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
parse_config(const char *config_file, t_configuration_options * options)
|
parse_config(const char *config_file, t_configuration_options *options)
|
||||||
{
|
{
|
||||||
char *s,
|
char *s,
|
||||||
buff[MAXLINELENGTH];
|
buff[MAXLINELENGTH];
|
||||||
|
char config_file_buf[MAXLEN];
|
||||||
char name[MAXLEN];
|
char name[MAXLEN];
|
||||||
char value[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 */
|
/* Initialize */
|
||||||
memset(options->cluster_name, 0, sizeof(options->cluster_name));
|
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.head = NULL;
|
||||||
options->tablespace_mapping.tail = 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 */
|
/* Read next line */
|
||||||
while ((s = fgets(buff, sizeof buff, fp)) != NULL)
|
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
|
* Re-read the configuration file: repmgr.conf
|
||||||
*/
|
*/
|
||||||
log_info(_("Reloading configuration file and updating repmgr tables\n"));
|
log_info(_("Reloading configuration file and updating repmgr tables\n"));
|
||||||
|
|
||||||
parse_config(config_file, &new_options);
|
parse_config(config_file, &new_options);
|
||||||
if (new_options.node == -1)
|
if (new_options.node == -1)
|
||||||
{
|
{
|
||||||
|
|||||||
4
config.h
4
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} }
|
#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);
|
void parse_line(char *buff, char *name, char *value);
|
||||||
char *trim(char *s);
|
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
|
#endif
|
||||||
|
|||||||
26
repmgr.c
26
repmgr.c
@@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h> /* for stat() */
|
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -404,31 +403,6 @@ main(int argc, char **argv)
|
|||||||
strncpy(runtime_options.masterport, DEFAULT_MASTER_PORT, MAXLEN);
|
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)
|
if (runtime_options.verbose)
|
||||||
/* Logging is not yet set up, so using printf() */
|
/* Logging is not yet set up, so using printf() */
|
||||||
|
|||||||
Reference in New Issue
Block a user