From f5044465cbf28b2213a8e53644b890b95b96da3c Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 14 Aug 2019 16:57:42 +0900 Subject: [PATCH] Add function to safely modify postgresql.auto.conf This is required for PostgreSQL 12 and later. --- configfile-scan.l | 34 ++++++++++++++--- configfile.c | 89 ++++++++++++++++++++++++++++++++++++++++++-- configfile.h | 5 ++- repmgr-action-node.c | 5 +-- repmgr-client.c | 5 +-- strutil.c | 76 ++++++++++++++++++++++++++++++++----- strutil.h | 3 ++ 7 files changed, 190 insertions(+), 27 deletions(-) diff --git a/configfile-scan.l b/configfile-scan.l index 003c8149..a705d2c0 100644 --- a/configfile-scan.l +++ b/configfile-scan.l @@ -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) diff --git a/configfile.c b/configfile.c index 812ba908..d7a5224c 100644 --- a/configfile.c +++ b/configfile.c @@ -23,6 +23,9 @@ #include "configfile.h" #include "log.h" +#include +#include /* 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) { diff --git a/configfile.h b/configfile.h index 33f1382a..1f7bf9c1 100644 --- a/configfile.h +++ b/configfile.h @@ -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_ */ diff --git a/repmgr-action-node.c b/repmgr-action-node.c index 59f78dba..7c521e5a 100644 --- a/repmgr-action-node.c +++ b/repmgr-action-node.c @@ -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); diff --git a/repmgr-client.c b/repmgr-client.c index 42a5c444..c1361795 100644 --- a/repmgr-client.c +++ b/repmgr-client.c @@ -57,6 +57,7 @@ #include #include + #include "repmgr.h" #include "compat.h" #include "controldata.h" @@ -72,7 +73,6 @@ #include /* 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: * diff --git a/strutil.c b/strutil.c index df339ee7..43d436be 100644 --- a/strutil.c +++ b/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; diff --git a/strutil.h b/strutil.h index 77b5efb2..99a02824 100644 --- a/strutil.h +++ b/strutil.h @@ -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)));