mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-22 22:56:29 +00:00
Add function to safely modify postgresql.auto.conf
This is required for PostgreSQL 12 and later.
This commit is contained in:
@@ -38,6 +38,8 @@ static sigjmp_buf *CONF_flex_fatal_jmp;
|
||||
static char *CONF_scanstr(const char *s);
|
||||
static int CONF_flex_fatal(const char *msg);
|
||||
|
||||
static bool ProcessConfigFile(FILE *fp, const char *config_file, KeyValueList *contents, t_configuration_options *options, ItemList *error_list, ItemList *warning_list);
|
||||
|
||||
%}
|
||||
|
||||
%option 8bit
|
||||
@@ -88,9 +90,20 @@ STRING \'([^'\\\n]|\\.|\'\')*\'
|
||||
|
||||
%%
|
||||
|
||||
extern bool
|
||||
ProcessRepmgrConfigFile(FILE *fp, const char *config_file, t_configuration_options *options, ItemList *error_list, ItemList *warning_list)
|
||||
{
|
||||
return ProcessConfigFile(fp, config_file, NULL, options, error_list, warning_list);
|
||||
}
|
||||
|
||||
extern bool
|
||||
ProcessConfigFile(FILE *fp, const char *config_file, t_configuration_options *options, ItemList *error_list, ItemList *warning_list)
|
||||
ProcessPostgresConfigFile(FILE *fp, const char *config_file, KeyValueList *contents, ItemList *error_list, ItemList *warning_list)
|
||||
{
|
||||
return ProcessConfigFile(fp, config_file, contents, NULL, error_list, warning_list);
|
||||
}
|
||||
|
||||
static bool
|
||||
ProcessConfigFile(FILE *fp, const char *config_file, KeyValueList *contents, t_configuration_options *options, ItemList *error_list, ItemList *warning_list)
|
||||
{
|
||||
volatile bool OK = true;
|
||||
volatile YY_BUFFER_STATE lex_buffer = NULL;
|
||||
@@ -167,12 +180,21 @@ ProcessConfigFile(FILE *fp, const char *config_file, t_configuration_options *op
|
||||
}
|
||||
|
||||
/* OK, process the option name and value */
|
||||
if (contents != NULL)
|
||||
{
|
||||
key_value_list_replace_or_set(contents,
|
||||
opt_name,
|
||||
opt_value);
|
||||
}
|
||||
|
||||
parse_configuration_item(options,
|
||||
error_list,
|
||||
warning_list,
|
||||
opt_name,
|
||||
opt_value);
|
||||
if (options != NULL)
|
||||
{
|
||||
parse_configuration_item(options,
|
||||
error_list,
|
||||
warning_list,
|
||||
opt_name,
|
||||
opt_value);
|
||||
}
|
||||
|
||||
/* break out of loop if read EOF, else loop for next line */
|
||||
if (token == 0)
|
||||
|
||||
89
configfile.c
89
configfile.c
@@ -23,6 +23,9 @@
|
||||
#include "configfile.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <utils/elog.h>
|
||||
#include <storage/fd.h> /* for durable_rename() */
|
||||
|
||||
const static char *_progname = NULL;
|
||||
char config_file_path[MAXPGPATH] = "";
|
||||
static bool config_file_provided = false;
|
||||
@@ -462,7 +465,7 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
(void) ProcessConfigFile(fp, config_file_path, options, error_list, warning_list);
|
||||
(void) ProcessRepmgrConfigFile(fp, config_file_path, options, error_list, warning_list);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
@@ -1800,6 +1803,7 @@ parse_bool(const char *s, const char *config_item, ItemList *error_list)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Split argument into old_dir and new_dir and append to tablespace mapping
|
||||
* list.
|
||||
@@ -1867,6 +1871,86 @@ tablespace_list_append(t_configuration_options *options, const char *arg)
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
modify_auto_conf(const char *data_dir)
|
||||
{
|
||||
PQExpBufferData auto_conf;
|
||||
PQExpBufferData auto_conf_tmp;
|
||||
PQExpBufferData auto_conf_contents;
|
||||
|
||||
FILE *fp;
|
||||
KeyValueList config = {NULL, NULL};
|
||||
KeyValueListCell *cell = NULL;
|
||||
|
||||
initPQExpBuffer(&auto_conf);
|
||||
appendPQExpBuffer(&auto_conf, "%s/%s",
|
||||
data_dir, PG_AUTOCONF_FILENAME);
|
||||
|
||||
fp = fopen(auto_conf.data, "r");
|
||||
|
||||
if (fp == NULL)
|
||||
{
|
||||
fprintf(stderr, "unable to open \"%s\": %s\n",
|
||||
auto_conf.data,
|
||||
strerror(errno));
|
||||
termPQExpBuffer(&auto_conf);
|
||||
return false;
|
||||
}
|
||||
|
||||
(void) ProcessPostgresConfigFile(fp, auto_conf.data, &config, NULL, NULL);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
initPQExpBuffer(&auto_conf_tmp);
|
||||
appendPQExpBuffer(&auto_conf_tmp, "%s.tmp",
|
||||
auto_conf.data);
|
||||
|
||||
initPQExpBuffer(&auto_conf_contents);
|
||||
|
||||
/*
|
||||
* Keep this in sync with src/backend/utils/misc/guc.c:write_auto_conf_file()
|
||||
*/
|
||||
appendPQExpBufferStr(&auto_conf_contents,
|
||||
"# Do not edit this file manually!\n"
|
||||
"# It will be overwritten by the ALTER SYSTEM command.\n");
|
||||
|
||||
for (cell = config.head; cell; cell = cell->next)
|
||||
{
|
||||
appendPQExpBuffer(&auto_conf_contents,
|
||||
"%s = '%s'\n",
|
||||
cell->key, cell->value);
|
||||
}
|
||||
|
||||
fp = fopen(auto_conf_tmp.data, "w");
|
||||
if (fp == NULL)
|
||||
{
|
||||
fprintf(stderr, "unable to open \"%s\": %s\n",
|
||||
auto_conf_tmp.data,
|
||||
strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fwrite(auto_conf_contents.data, strlen(auto_conf_contents.data) + 1, 1, fp) != 1)
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
fclose(fp);
|
||||
|
||||
(void) durable_rename(auto_conf_tmp.data, auto_conf.data, LOG);
|
||||
}
|
||||
}
|
||||
|
||||
termPQExpBuffer(&auto_conf);
|
||||
termPQExpBuffer(&auto_conf_tmp);
|
||||
termPQExpBuffer(&auto_conf_contents);
|
||||
|
||||
key_value_list_free(&config);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* parse_event_notifications_list()
|
||||
@@ -2043,9 +2127,6 @@ free_parsed_argv(char ***argv_array)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool
|
||||
parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_options *backup_options, int server_version_num, ItemList *error_list)
|
||||
{
|
||||
|
||||
@@ -350,7 +350,10 @@ void exit_with_cli_errors(ItemList *error_list, const char *repmgr_command);
|
||||
void print_item_list(ItemList *item_list);
|
||||
const char *print_connection_check_type(ConnectionCheckType type);
|
||||
|
||||
extern bool modify_auto_conf(const char *data_dir);
|
||||
|
||||
extern bool ProcessConfigFile(FILE *fp, const char *config_file, t_configuration_options *options, ItemList *error_list, ItemList *warning_list);
|
||||
extern bool ProcessRepmgrConfigFile(FILE *fp, const char *config_file, t_configuration_options *options, ItemList *error_list, ItemList *warning_list);
|
||||
|
||||
extern bool ProcessPostgresConfigFile(FILE *fp, const char *config_file, KeyValueList *contents, ItemList *error_list, ItemList *warning_list);
|
||||
|
||||
#endif /* _REPMGR_CONFIGFILE_H_ */
|
||||
|
||||
@@ -71,6 +71,7 @@ do_node_status(void)
|
||||
PQExpBufferData output;
|
||||
|
||||
KeyValueList node_status = {NULL, NULL};
|
||||
KeyValueListCell *cell = NULL;
|
||||
NodeInfoList missing_slots = T_NODE_INFO_LIST_INITIALIZER;
|
||||
|
||||
ItemList warnings = {NULL, NULL};
|
||||
@@ -465,8 +466,6 @@ do_node_status(void)
|
||||
|
||||
if (runtime_options.output_mode == OM_CSV)
|
||||
{
|
||||
KeyValueListCell *cell = NULL;
|
||||
|
||||
appendPQExpBuffer(&output,
|
||||
"\"Node name\",\"%s\"\n",
|
||||
node_info.node_name);
|
||||
@@ -540,8 +539,6 @@ do_node_status(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
KeyValueListCell *cell = NULL;
|
||||
|
||||
appendPQExpBuffer(&output,
|
||||
"Node \"%s\":\n",
|
||||
node_info.node_name);
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
#include "repmgr.h"
|
||||
#include "compat.h"
|
||||
#include "controldata.h"
|
||||
@@ -72,7 +73,6 @@
|
||||
|
||||
#include <storage/fd.h> /* for PG_TEMP_FILE_PREFIX */
|
||||
|
||||
|
||||
/* globally available variables *
|
||||
* ============================ */
|
||||
|
||||
@@ -348,7 +348,7 @@ main(int argc, char **argv)
|
||||
*-------------
|
||||
*/
|
||||
|
||||
/* -D/--pgdata */
|
||||
/* -D/--pgdata/--data-dir */
|
||||
case 'D':
|
||||
strncpy(runtime_options.data_dir, optarg, MAXPGPATH);
|
||||
break;
|
||||
@@ -804,7 +804,6 @@ main(int argc, char **argv)
|
||||
exit_with_cli_errors(&cli_errors, NULL);
|
||||
}
|
||||
|
||||
|
||||
/*----------
|
||||
* Determine the node type and action; following are valid:
|
||||
*
|
||||
|
||||
76
strutil.c
76
strutil.c
@@ -29,6 +29,9 @@ static int
|
||||
xvsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
|
||||
|
||||
static void
|
||||
_key_value_list_set(KeyValueList *item_list, bool replace, const char *key, const char *value);
|
||||
|
||||
static int
|
||||
xvsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||
{
|
||||
@@ -164,16 +167,74 @@ item_list_free(ItemList *item_list)
|
||||
void
|
||||
key_value_list_set(KeyValueList *item_list, const char *key, const char *value)
|
||||
{
|
||||
key_value_list_set_format(item_list, key, "%s", value);
|
||||
_key_value_list_set(item_list, false, key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
key_value_list_set_format(KeyValueList *item_list, const char *key, const char *value,...)
|
||||
key_value_list_replace_or_set(KeyValueList *item_list, const char *key, const char *value)
|
||||
{
|
||||
_key_value_list_set(item_list, true, key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
key_value_list_set_format(KeyValueList *item_list, const char *key, const char *value, ...)
|
||||
{
|
||||
va_list arglist;
|
||||
char formatted_value[MAXLEN];
|
||||
|
||||
va_start(arglist, value);
|
||||
(void) xvsnprintf(formatted_value, MAXLEN, value, arglist);
|
||||
va_end(arglist);
|
||||
|
||||
return _key_value_list_set(item_list, false, key, formatted_value);
|
||||
}
|
||||
|
||||
static void
|
||||
_key_value_list_set(KeyValueList *item_list, bool replace, const char *key, const char *value)
|
||||
{
|
||||
KeyValueListCell *cell = NULL;
|
||||
va_list arglist;
|
||||
int keylen = 0;
|
||||
int vallen = 0;
|
||||
|
||||
if (replace == true)
|
||||
{
|
||||
KeyValueListCell *prev_cell = NULL;
|
||||
KeyValueListCell *next_cell = NULL;
|
||||
|
||||
|
||||
for (cell = item_list->head; cell; cell = next_cell)
|
||||
{
|
||||
next_cell = cell->next;
|
||||
|
||||
if (strcmp(cell->key, key) == 0)
|
||||
{
|
||||
if (item_list->head == cell)
|
||||
item_list->head = cell->next;
|
||||
|
||||
if (prev_cell)
|
||||
{
|
||||
prev_cell->next = cell->next;
|
||||
|
||||
if (item_list->tail == cell)
|
||||
item_list->tail = prev_cell;
|
||||
}
|
||||
else if (item_list->tail == cell)
|
||||
{
|
||||
item_list->tail = NULL;
|
||||
}
|
||||
|
||||
pfree(cell->key);
|
||||
pfree(cell->value);
|
||||
pfree(cell);
|
||||
}
|
||||
else
|
||||
{
|
||||
prev_cell = cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cell = (KeyValueListCell *) pg_malloc0(sizeof(KeyValueListCell));
|
||||
|
||||
@@ -184,17 +245,14 @@ key_value_list_set_format(KeyValueList *item_list, const char *key, const char *
|
||||
}
|
||||
|
||||
keylen = strlen(key);
|
||||
vallen = strlen(value);
|
||||
|
||||
cell->key = pg_malloc0(keylen + 1);
|
||||
cell->value = pg_malloc0(MAXLEN);
|
||||
cell->value = pg_malloc0(vallen + 1);
|
||||
cell->output_mode = OM_NOT_SET;
|
||||
|
||||
strncpy(cell->key, key, keylen);
|
||||
|
||||
va_start(arglist, value);
|
||||
(void) xvsnprintf(cell->value, MAXLEN, value, arglist);
|
||||
va_end(arglist);
|
||||
|
||||
strncpy(cell->value, value, vallen);
|
||||
|
||||
if (item_list->tail)
|
||||
item_list->tail->next = cell;
|
||||
|
||||
@@ -119,6 +119,9 @@ extern void
|
||||
extern void
|
||||
key_value_list_set(KeyValueList *item_list, const char *key, const char *value);
|
||||
|
||||
extern void
|
||||
key_value_list_replace_or_set(KeyValueList *item_list, const char *key, const char *value);
|
||||
|
||||
extern void
|
||||
key_value_list_set_format(KeyValueList *item_list, const char *key, const char *value,...)
|
||||
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
|
||||
|
||||
Reference in New Issue
Block a user