diff --git a/configfile.c b/configfile.c index 3cf9135a..e51ba461 100644 --- a/configfile.c +++ b/configfile.c @@ -90,8 +90,8 @@ load_config(const char *config_file, bool verbose, bool terse, t_configuration_o */ if (config_file_provided == false) { - char my_exec_path[MAXPGPATH]; - char sysconf_etc_path[MAXPGPATH]; + char my_exec_path[MAXPGPATH] = ""; + char sysconf_etc_path[MAXPGPATH] = ""; /* 1. "./repmgr.conf" */ if (verbose == true) @@ -195,10 +195,10 @@ static void _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *warning_list) { FILE *fp; - char *s, - buf[MAXLINELENGTH]; - char name[MAXLEN]; - char value[MAXLEN]; + char *s = NULL, + buf[MAXLINELENGTH] = ""; + char name[MAXLEN] = ""; + char value[MAXLEN] = ""; bool node_id_found = false; @@ -585,7 +585,7 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList * * but does not sanity check values */ - PQconninfoOption *conninfo_options; + PQconninfoOption *conninfo_options = NULL; char *conninfo_errmsg = NULL; conninfo_options = PQconninfoParse(options->conninfo, &conninfo_errmsg); @@ -638,10 +638,10 @@ parse_recovery_conf(const char *data_dir, t_recovery_conf *conf) { char recovery_conf_path[MAXPGPATH] = ""; FILE *fp; - char *s, - buf[MAXLINELENGTH]; - char name[MAXLEN]; - char value[MAXLEN]; + char *s = NULL, + buf[MAXLINELENGTH] = ""; + char name[MAXLEN] = ""; + char value[MAXLEN] = ""; snprintf(recovery_conf_path, MAXPGPATH, "%s/%s", @@ -795,13 +795,13 @@ reload_config(t_configuration_options *orig_options) } -/* TODO: don't emit warnings if --terse and no errors */ static void exit_with_config_file_errors(ItemList *config_errors, ItemList *config_warnings, bool terse) { log_error(_("following errors were found in the configuration file:")); print_item_list(config_errors); + item_list_free(config_errors); if (terse == false && config_warnings->head != NULL) { @@ -809,6 +809,7 @@ exit_with_config_file_errors(ItemList *config_errors, ItemList *config_warnings, log_warning(_("the following problems were also found in the configuration file:")); print_item_list(config_warnings); + item_list_free(config_warnings); } exit(ERR_BAD_CONFIG); @@ -830,7 +831,7 @@ exit_with_cli_errors(ItemList *error_list) void print_item_list(ItemList *item_list) { - ItemListCell *cell; + ItemListCell *cell = NULL; for (cell = item_list->head; cell; cell = cell->next) { @@ -849,7 +850,7 @@ print_item_list(ItemList *item_list) int repmgr_atoi(const char *value, const char *config_item, ItemList *error_list, int minval) { - char *endptr; + char *endptr = NULL; long longval = 0; PQExpBufferData errors; @@ -978,10 +979,10 @@ parse_bool(const char *s, const char *config_item, ItemList *error_list) static void tablespace_list_append(t_configuration_options *options, const char *arg) { - TablespaceListCell *cell; - char *dst; - char *dst_ptr; - const char *arg_ptr; + TablespaceListCell *cell = NULL; + char *dst = NULL; + char *dst_ptr = NULL; + const char *arg_ptr = NULL; cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell)); if (cell == NULL) @@ -1046,7 +1047,7 @@ tablespace_list_append(t_configuration_options *options, const char *arg) static void parse_event_notifications_list(t_configuration_options *options, const char *arg) { - const char *arg_ptr; + const char *arg_ptr = NULL; char event_type_buf[MAXLEN] = ""; char *dst_ptr = event_type_buf; @@ -1117,15 +1118,15 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti */ static ItemList option_argv = { NULL, NULL }; - char *argv_item; + char *argv_item = NULL; int c, argc_item = 1; - char **argv_array; - ItemListCell *cell; + char **argv_array = NULL; + ItemListCell *cell = NULL; int optindex = 0; - struct option *long_options; + struct option *long_options = NULL; bool backup_options_ok = true; diff --git a/controldata.c b/controldata.c index 7b33ac7b..312b0c9a 100644 --- a/controldata.c +++ b/controldata.c @@ -20,7 +20,7 @@ static ControlFileInfo *get_controlfile(const char *DataDir); uint64 get_system_identifier(const char *data_directory) { - ControlFileInfo *control_file_info; + ControlFileInfo *control_file_info = T_CONTROLFILEINFO_INITIALIZER; uint64 system_identifier = UNKNOWN_SYSTEM_IDENTIFIER; control_file_info = get_controlfile(data_directory); @@ -39,7 +39,7 @@ get_system_identifier(const char *data_directory) DBState get_db_state(const char *data_directory) { - ControlFileInfo *control_file_info; + ControlFileInfo *control_file_info = T_CONTROLFILEINFO_INITIALIZER; DBState state; control_file_info = get_controlfile(data_directory); @@ -60,8 +60,8 @@ get_db_state(const char *data_directory) extern XLogRecPtr get_latest_checkpoint_location(const char *data_directory) { - ControlFileInfo *control_file_info; - XLogRecPtr checkPoint; + ControlFileInfo *control_file_info = T_CONTROLFILEINFO_INITIALIZER; + XLogRecPtr checkPoint = InvalidXLogRecPtr; control_file_info = get_controlfile(data_directory); @@ -80,8 +80,8 @@ get_latest_checkpoint_location(const char *data_directory) int get_data_checksum_version(const char *data_directory) { - ControlFileInfo *control_file_info; - int data_checksum_version; + ControlFileInfo *control_file_info = T_CONTROLFILEINFO_INITIALIZER; + int data_checksum_version = -1; control_file_info = get_controlfile(data_directory); @@ -132,9 +132,9 @@ describe_db_state(DBState state) static ControlFileInfo * get_controlfile(const char *DataDir) { - ControlFileInfo *control_file_info; + ControlFileInfo *control_file_info = T_CONTROLFILEINFO_INITIALIZER; int fd; - char ControlFilePath[MAXPGPATH]; + char ControlFilePath[MAXPGPATH] = ""; control_file_info = palloc0(sizeof(ControlFileInfo)); control_file_info->control_file_processed = false; diff --git a/controldata.h b/controldata.h index e3ca9e21..8585029f 100644 --- a/controldata.h +++ b/controldata.h @@ -18,6 +18,8 @@ typedef struct ControlFileData *control_file; } ControlFileInfo; +#define T_CONTROLFILEINFO_INITIALIZER { false, NULL } + extern DBState get_db_state(const char *data_directory); extern const char * describe_db_state(DBState state); extern int get_data_checksum_version(const char *data_directory); diff --git a/dbutils.c b/dbutils.c index fc4f75b7..10837a3f 100644 --- a/dbutils.c +++ b/dbutils.c @@ -105,8 +105,8 @@ _establish_db_connection(const char *conninfo, const bool exit_on_error, const b char *connection_string = NULL; char *errmsg = NULL; - t_conninfo_param_list conninfo_params; - bool parse_success; + t_conninfo_param_list conninfo_params = T_CONNINFO_PARAM_LIST_INITIALIZER; + bool parse_success = false; initialize_conninfo_params(&conninfo_params, false); @@ -209,9 +209,7 @@ PGconn const bool exit_on_error) { t_node_info primary_node_info = T_NODE_INFO_INITIALIZER; - bool primary_record_found; - - primary_record_found = get_primary_node_record(conn, &primary_node_info); + bool primary_record_found = get_primary_node_record(conn, &primary_node_info); if (primary_record_found == false) { @@ -229,8 +227,8 @@ establish_db_connection_as_user(const char *conninfo, const bool exit_on_error) { PGconn *conn = NULL; - t_conninfo_param_list conninfo_params; - bool parse_success; + t_conninfo_param_list conninfo_params = T_CONNINFO_PARAM_LIST_INITIALIZER; + bool parse_success = false; char *errmsg = NULL; initialize_conninfo_params(&conninfo_params, false); @@ -257,7 +255,7 @@ PGconn * establish_db_connection_by_params(t_conninfo_param_list *param_list, const bool exit_on_error) { - PGconn *conn; + PGconn *conn = NULL; /* set some default values if not explicitly provided */ param_set_ine(param_list, "connect_timeout", "2"); @@ -310,9 +308,9 @@ establish_db_connection_by_params(t_conninfo_param_list *param_list, bool is_superuser_connection(PGconn *conn, t_connection_user *userinfo) { - char *current_user; - const char *superuser_status; - bool is_superuser; + char *current_user = NULL; + const char *superuser_status = NULL; + bool is_superuser = false; current_user = PQuser(conn); superuser_status = PQparameterStatus(conn, "is_superuser"); @@ -346,8 +344,8 @@ is_superuser_connection(PGconn *conn, t_connection_user *userinfo) bool get_conninfo_value(const char *conninfo, const char *keyword, char *output) { - PQconninfoOption *conninfo_options; - PQconninfoOption *conninfo_option; + PQconninfoOption *conninfo_options = NULL; + PQconninfoOption *conninfo_option = NULL; conninfo_options = PQconninfoParse(conninfo, NULL); @@ -379,7 +377,7 @@ void initialize_conninfo_params(t_conninfo_param_list *param_list, bool set_defaults) { PQconninfoOption *defs = NULL; - PQconninfoOption *def; + PQconninfoOption *def = NULL; int c; defs = PQconndefaults(); @@ -562,8 +560,8 @@ param_get(t_conninfo_param_list *param_list, const char *param) bool parse_conninfo_string(const char *conninfo_str, t_conninfo_param_list *param_list, char *errmsg, bool ignore_local_params) { - PQconninfoOption *connOptions; - PQconninfoOption *option; + PQconninfoOption *connOptions = NULL; + PQconninfoOption *option = NULL; connOptions = PQconninfoParse(conninfo_str, &errmsg); @@ -604,8 +602,8 @@ parse_conninfo_string(const char *conninfo_str, t_conninfo_param_list *param_lis void conn_to_param_list(PGconn *conn, t_conninfo_param_list *param_list) { - PQconninfoOption *connOptions; - PQconninfoOption *option; + PQconninfoOption *connOptions = NULL; + PQconninfoOption *option = NULL; connOptions = PQconninfo(conn); for (option = connOptions; option && option->keyword; option++) @@ -630,8 +628,8 @@ param_list_to_string(t_conninfo_param_list *param_list) { int c; PQExpBufferData conninfo_buf; - char *conninfo_str; - int len; + char *conninfo_str = NULL; + int len = 0; initPQExpBuffer(&conninfo_buf); @@ -668,7 +666,7 @@ param_list_to_string(t_conninfo_param_list *param_list) bool begin_transaction(PGconn *conn) { - PGresult *res; + PGresult *res = NULL; log_verbose(LOG_DEBUG, "begin_transaction()"); @@ -692,7 +690,7 @@ begin_transaction(PGconn *conn) bool commit_transaction(PGconn *conn) { - PGresult *res; + PGresult *res = NULL; log_verbose(LOG_DEBUG, "commit_transaction()"); @@ -716,7 +714,7 @@ commit_transaction(PGconn *conn) bool rollback_transaction(PGconn *conn) { - PGresult *res; + PGresult *res = NULL; log_verbose(LOG_DEBUG, "rollback_transaction()"); @@ -744,7 +742,7 @@ rollback_transaction(PGconn *conn) static bool _set_config(PGconn *conn, const char *config_param, const char *sqlquery) { - PGresult *res; + PGresult *res = NULL; res = PQexec(conn, sqlquery); @@ -765,7 +763,7 @@ bool set_config(PGconn *conn, const char *config_param, const char *config_value) { PQExpBufferData query; - bool result; + bool result = false; initPQExpBuffer(&query); appendPQExpBuffer(&query, @@ -786,7 +784,7 @@ bool set_config_bool(PGconn *conn, const char *config_param, bool state) { PQExpBufferData query; - bool result; + bool result = false; initPQExpBuffer(&query); appendPQExpBuffer(&query, @@ -810,7 +808,7 @@ guc_set(PGconn *conn, const char *parameter, const char *op, const char *value) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; int retval = 1; char *escaped_parameter = escape_string(conn, parameter); @@ -855,7 +853,7 @@ guc_set_typed(PGconn *conn, const char *parameter, const char *op, const char *value, const char *datatype) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; int retval = 1; char *escaped_parameter = escape_string(conn, parameter); @@ -896,7 +894,7 @@ bool get_pg_setting(PGconn *conn, const char *setting, char *output) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; int i; bool success = false; @@ -964,7 +962,7 @@ bool get_cluster_size(PGconn *conn, char *size) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); appendPQExpBuffer(&query, @@ -997,7 +995,7 @@ get_cluster_size(PGconn *conn, char *size) int get_server_version(PGconn *conn, char *server_version) { - PGresult *res; + PGresult *res = NULL; int server_version_num; res = PQexec(conn, @@ -1025,8 +1023,8 @@ get_server_version(PGconn *conn, char *server_version) RecoveryType get_recovery_type(PGconn *conn) { - PGresult *res; - RecoveryType recovery_type = RECTYPE_PRIMARY; + PGresult *res = NULL; + RecoveryType recovery_type = RECTYPE_UNKNOWN; char *sqlquery = "SELECT pg_catalog.pg_is_in_recovery()"; @@ -1068,7 +1066,7 @@ _get_primary_connection(PGconn *conn, PQExpBufferData query; PGconn *remote_conn = NULL; - PGresult *res; + PGresult *res = NULL; char remote_conninfo_stack[MAXCONNINFO]; char *remote_conninfo = &*remote_conninfo_stack; @@ -1193,8 +1191,8 @@ int get_primary_node_id(PGconn *conn) { PQExpBufferData query; - PGresult *res; - int retval; + PGresult *res = NULL; + int retval = NODE_NOT_FOUND; initPQExpBuffer(&query); appendPQExpBuffer(&query, @@ -1233,7 +1231,7 @@ bool get_replication_info(PGconn *conn, ReplInfo *replication_info) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; if (server_version_num == UNKNOWN_SERVER_VERSION_NUM) server_version_num = get_server_version(conn, NULL); @@ -1245,9 +1243,9 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info) " last_wal_replay_lsn, " " last_xact_replay_timestamp, " " CASE WHEN (last_wal_receive_lsn = last_wal_replay_lsn) " - " THEN '0 seconds'::INTERVAL " + " THEN 0::INT " " ELSE " - " clock_timestamp() - last_xact_replay_timestamp " + " EXTRACT(epoch FROM (clock_timestamp() - last_xact_replay_timestamp))::INT " " END AS replication_lag_time " " FROM ( "); @@ -1272,6 +1270,8 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info) &query, " ) q "); + log_verbose(LOG_DEBUG, "get_replication_info():\n%s", query.data); + res = PQexec(conn, query.data); termPQExpBuffer(&query); @@ -1286,7 +1286,7 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info) replication_info->last_wal_receive_lsn = parse_lsn(PQgetvalue(res, 0, 0)); replication_info->last_wal_replay_lsn = parse_lsn(PQgetvalue(res, 0, 1)); - strncpy(replication_info->replication_lag_time, PQgetvalue(res, 0, 3), MAXLEN); + replication_info->replication_lag_time = atoi(PQgetvalue(res, 0, 3)); PQclear(res); @@ -1356,7 +1356,6 @@ get_ready_archive_files(PGconn *conn, const char *data_directory) struct dirent *arcdir_ent; DIR *arcdir; - int ready_count = 0; if (server_version_num == UNKNOWN_SERVER_VERSION_NUM) @@ -1400,7 +1399,7 @@ get_ready_archive_files(PGconn *conn, const char *data_directory) { struct stat statbuf; char file_path[MAXPGPATH] = ""; - int basenamelen; + int basenamelen = 0; snprintf(file_path, MAXPGPATH, "%s/%s", @@ -1433,7 +1432,7 @@ int get_replication_lag_seconds(PGconn *conn) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; int lag_seconds = 0; if (server_version_num == UNKNOWN_SERVER_VERSION_NUM) @@ -1486,7 +1485,7 @@ get_replication_lag_seconds(PGconn *conn) bool identify_system(PGconn *repl_conn, t_system_identification *identification) { - PGresult *res; + PGresult *res = NULL; res = PQexec(repl_conn, "IDENTIFY_SYSTEM;"); @@ -1525,7 +1524,7 @@ ExtensionStatus get_repmgr_extension_status(PGconn *conn) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; ExtensionStatus status = REPMGR_UNKNOWN; /* TODO: check version */ @@ -1579,7 +1578,7 @@ get_repmgr_extension_status(PGconn *conn) void checkpoint(PGconn *conn) { - PGresult *res; + PGresult *res = NULL; res = PQexec(conn, "CHECKPOINT"); @@ -1602,8 +1601,8 @@ checkpoint(PGconn *conn) static RecordStatus _get_node_record(PGconn *conn, char *sqlquery, t_node_info *node_info) { - int ntuples; - PGresult *res; + int ntuples = 0; + PGresult *res = NULL; res = PQexec(conn, sqlquery); @@ -1736,7 +1735,7 @@ RecordStatus get_node_record_by_name(PGconn *conn, const char *node_name, t_node_info *node_info) { PQExpBufferData query; - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; initPQExpBuffer(&query); @@ -1766,7 +1765,7 @@ t_node_info * get_node_record_pointer(PGconn *conn, int node_id) { t_node_info *node_info = pg_malloc0(sizeof(t_node_info)); - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; record_status = get_node_record(conn, node_id, node_info); @@ -1783,7 +1782,7 @@ get_node_record_pointer(PGconn *conn, int node_id) bool get_primary_node_record(PGconn *conn, t_node_info *node_info) { - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; int primary_node_id = get_primary_node_id(conn); @@ -1806,9 +1805,7 @@ get_primary_node_record(PGconn *conn, t_node_info *node_info) bool get_local_node_record(PGconn *conn, int node_id, t_node_info *node_info) { - RecordStatus record_status; - - record_status = get_node_record(conn, node_id, node_info); + RecordStatus record_status = get_node_record(conn, node_id, node_info); if (record_status != RECORD_FOUND) { @@ -1862,7 +1859,7 @@ void get_all_node_records(PGconn *conn, NodeInfoList *node_list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -1889,7 +1886,7 @@ void get_downstream_node_records(PGconn *conn, int node_id, NodeInfoList *node_list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -1919,7 +1916,7 @@ void get_active_sibling_node_records(PGconn *conn, int node_id, int upstream_node_id, NodeInfoList *node_list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -1952,7 +1949,7 @@ void get_node_records_by_priority(PGconn *conn, NodeInfoList *node_list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -1979,7 +1976,7 @@ void get_all_node_records_with_upstream(PGconn *conn, NodeInfoList *node_list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -2029,10 +2026,10 @@ static bool _create_update_node_record(PGconn *conn, char *action, t_node_info *node_info) { PQExpBufferData query; - char node_id[MAXLEN]; - char priority[MAXLEN]; + char node_id[MAXLEN] = ""; + char priority[MAXLEN] = ""; - char upstream_node_id[MAXLEN]; + char upstream_node_id[MAXLEN] = ""; char *upstream_node_id_ptr = NULL; char *slot_name_ptr = NULL; @@ -2138,7 +2135,7 @@ bool update_node_record_set_active(PGconn *conn, int this_node_id, bool active) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -2172,7 +2169,7 @@ bool update_node_record_set_primary(PGconn *conn, int this_node_id) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; log_debug(_("setting node %i as primary and marking existing primary as failed"), this_node_id); @@ -2234,7 +2231,7 @@ bool update_node_record_set_upstream(PGconn *conn, int this_node_id, int new_upstream_node_id) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; log_debug(_("update_node_record_set_upstream(): Updating node %i's upstream node to %i"), this_node_id, new_upstream_node_id); @@ -2275,7 +2272,7 @@ bool update_node_record_status(PGconn *conn, int this_node_id, char *type, int upstream_node_id, bool active) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -2318,7 +2315,7 @@ bool update_node_record_conn_priority(PGconn *conn, t_configuration_options *options) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -2351,7 +2348,7 @@ bool delete_node_record(PGconn *conn, int node) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -2383,7 +2380,7 @@ void get_node_replication_stats(PGconn *conn, t_node_info *node_info) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -2422,8 +2419,8 @@ get_node_replication_stats(PGconn *conn, t_node_info *node_info) void clear_node_info_list(NodeInfoList *nodes) { - NodeInfoListCell *cell; - NodeInfoListCell *next_cell; + NodeInfoListCell *cell = NULL; + NodeInfoListCell *next_cell = NULL; log_verbose(LOG_DEBUG, "clear_node_info_list() - closing open connections"); @@ -2464,7 +2461,7 @@ bool get_datadir_configuration_files(PGconn *conn, KeyValueList *list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; int i; initPQExpBuffer(&query); @@ -2519,7 +2516,7 @@ bool get_configuration_file_locations(PGconn *conn, t_configfile_list *list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; int i; initPQExpBuffer(&query); @@ -2729,7 +2726,7 @@ static bool _create_event(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info, bool send_notification) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; char event_timestamp[MAXLEN] = ""; bool success = true; @@ -2820,10 +2817,10 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char if (send_notification == true && strlen(options->event_notification_command)) { char parsed_command[MAXPGPATH]; - const char *src_ptr; - char *dst_ptr; - char *end_ptr; - int r; + const char *src_ptr = NULL; + char *dst_ptr = NULL; + char *end_ptr = NULL; + int r = 0; /* * If configuration option 'event_notifications' was provided, @@ -2962,9 +2959,9 @@ bool create_replication_slot(PGconn *conn, char *slot_name, int server_version_num, PQExpBufferData *error_msg) { PQExpBufferData query; - RecordStatus record_status; - PGresult *res; - t_replication_slot slot_info; + RecordStatus record_status = RECORD_NOT_FOUND; + PGresult *res = NULL; + t_replication_slot slot_info = T_REPLICATION_SLOT_INITIALIZER; /* * Check whether slot exists already; if it exists and is active, that @@ -3040,7 +3037,7 @@ bool drop_replication_slot(PGconn *conn, char *slot_name) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3074,7 +3071,7 @@ RecordStatus get_slot_record(PGconn *conn, char *slot_name, t_replication_slot *record) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3123,7 +3120,7 @@ bool get_tablespace_name_by_location(PGconn *conn, const char *location, char *name) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3166,8 +3163,8 @@ get_tablespace_name_by_location(PGconn *conn, const char *location, char *name) bool cancel_query(PGconn *conn, int timeout) { - char errbuf[ERRBUFF_SIZE]; - PGcancel *pgcancel; + char errbuf[ERRBUFF_SIZE] = ""; + PGcancel *pgcancel = NULL; if (wait_connection_availability(conn, timeout) != 1) return false; @@ -3203,7 +3200,7 @@ cancel_query(PGconn *conn, int timeout) int wait_connection_availability(PGconn *conn, long long timeout) { - PGresult *res; + PGresult *res = NULL; fd_set read_set; int sock = PQsocket(conn); struct timeval tmout, @@ -3291,8 +3288,8 @@ is_server_available(const char *conninfo) NodeVotingStatus get_voting_status(PGconn *conn) { - PGresult *res; - NodeVotingStatus voting_status; + PGresult *res = NULL; + NodeVotingStatus voting_status = VS_UNKNOWN; res = PQexec(conn, "SELECT repmgr.get_voting_status()"); @@ -3315,8 +3312,8 @@ VoteRequestResult request_vote(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int electoral_term) { PQExpBufferData query; - PGresult *res; - int lsn_diff; + PGresult *res = NULL; + int lsn_diff = 0; other_node->last_wal_receive_lsn = InvalidXLogRecPtr; @@ -3416,8 +3413,8 @@ request_vote(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int int set_voting_status_initiated(PGconn *conn) { - PGresult *res; - int electoral_term; + PGresult *res = NULL; + int electoral_term = 0; res = PQexec(conn, "SELECT repmgr.set_voting_status_initiated()"); @@ -3433,9 +3430,9 @@ bool announce_candidature(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int electoral_term) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; - bool retval; + bool retval = false; initPQExpBuffer(&query); @@ -3460,7 +3457,7 @@ void notify_follow_primary(PGconn *conn, int primary_node_id) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3489,9 +3486,9 @@ bool get_new_primary(PGconn *conn, int *primary_node_id) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; - int new_primary_node_id; + int new_primary_node_id = UNKNOWN_NODE_ID; initPQExpBuffer(&query); @@ -3522,7 +3519,7 @@ void reset_voting_status(PGconn *conn) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3551,7 +3548,7 @@ reset_voting_status(PGconn *conn) XLogRecPtr get_last_wal_receive_location(PGconn *conn) { - PGresult *res; + PGresult *res = NULL; XLogRecPtr ptr = InvalidXLogRecPtr; @@ -3582,8 +3579,8 @@ bool is_bdr_db(PGconn *conn) { PQExpBufferData query; - PGresult *res; - bool is_bdr_db; + PGresult *res = NULL; + bool is_bdr_db = false; initPQExpBuffer(&query); @@ -3637,8 +3634,8 @@ bool is_active_bdr_node(PGconn *conn, const char *node_name) { PQExpBufferData query; - PGresult *res; - bool is_active_bdr_node; + PGresult *res = NULL; + bool is_active_bdr_node = false; initPQExpBuffer(&query); appendPQExpBuffer( @@ -3672,8 +3669,8 @@ bool is_bdr_repmgr(PGconn *conn) { PQExpBufferData query; - PGresult *res; - int non_bdr_nodes; + PGresult *res = NULL; + int non_bdr_nodes = 0; initPQExpBuffer(&query); @@ -3704,8 +3701,8 @@ bool is_table_in_bdr_replication_set(PGconn *conn, const char *tablename, const char *set) { PQExpBufferData query; - PGresult *res; - bool in_replication_set; + PGresult *res = NULL; + bool in_replication_set = false; initPQExpBuffer(&query); @@ -3740,7 +3737,7 @@ bool add_table_to_bdr_replication_set(PGconn *conn, const char *tablename, const char *set) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3775,8 +3772,8 @@ bool bdr_node_exists(PGconn *conn, const char *node_name) { PQExpBufferData query; - PGresult *res; - bool node_exists; + PGresult *res = NULL; + bool node_exists = false; initPQExpBuffer(&query); @@ -3809,8 +3806,8 @@ ReplSlotStatus get_bdr_node_replication_slot_status(PGconn *conn, const char *node_name) { PQExpBufferData query; - PGresult *res; - ReplSlotStatus status; + PGresult *res = NULL; + ReplSlotStatus status = SLOT_UNKNOWN; initPQExpBuffer(&query); @@ -3850,7 +3847,7 @@ void get_bdr_other_node_name(PGconn *conn, int node_id, char *node_name) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3885,7 +3882,7 @@ void add_extension_tables_to_bdr_replication_set(PGconn *conn) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); @@ -3927,21 +3924,13 @@ void get_all_bdr_node_records(PGconn *conn, BdrNodeInfoList *node_list) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); appendPQExpBuffer( &query, - " SELECT node_sysid, " - " node_timeline, " - " node_dboid, " - " node_status, " - " node_name, " - " node_local_dsn, " - " node_init_from_dsn, " - " node_read_only, " - " node_seq_id " + " SELECT " BDR_NODES_COLUMNS " FROM bdr.bdr_nodes " "ORDER BY node_seq_id "); @@ -3960,21 +3949,13 @@ RecordStatus get_bdr_node_record_by_name(PGconn *conn, const char *node_name, t_bdr_node_info *node_info) { PQExpBufferData query; - PGresult *res; + PGresult *res = NULL; initPQExpBuffer(&query); appendPQExpBuffer( &query, - " SELECT node_sysid, " - " node_timeline, " - " node_dboid, " - " node_status, " - " node_name, " - " node_local_dsn, " - " node_init_from_dsn, " - " node_read_only, " - " node_seq_id " + " SELECT " BDR_NODES_COLUMNS " FROM bdr.bdr_nodes " " WHERE node_name = '%s'", node_name); @@ -4045,7 +4026,7 @@ void _populate_bdr_node_records(PGresult *res, BdrNodeInfoList *node_list) static void _populate_bdr_node_record(PGresult *res, t_bdr_node_info *node_info, int row) { - char buf[MAXLEN]; + char buf[MAXLEN] = ""; strncpy(node_info->node_sysid, PQgetvalue(res, row, 0), MAXLEN); node_info->node_timeline = atoi(PQgetvalue(res, row, 1)); @@ -4062,8 +4043,8 @@ bool am_bdr_failover_handler(PGconn *conn, int node_id) { PQExpBufferData query; - PGresult *res; - bool am_handler; + PGresult *res = NULL; + bool am_handler = false; initPQExpBuffer(&query); @@ -4094,7 +4075,7 @@ am_bdr_failover_handler(PGconn *conn, int node_id) void unset_bdr_failover_handler(PGconn *conn) { - PGresult *res; + PGresult *res = NULL; res = PQexec(conn, "SELECT repmgr.unset_bdr_failover_handler()"); PQclear(res); diff --git a/dbutils.h b/dbutils.h index 903c52ad..38139d91 100644 --- a/dbutils.h +++ b/dbutils.h @@ -16,6 +16,7 @@ #include "voting.h" #define REPMGR_NODES_COLUMNS "node_id, type, upstream_node_id, node_name, conninfo, repluser, slot_name, location, priority, active, config_file, '' AS upstream_node_name " +#define BDR_NODES_COLUMNS "node_sysid, node_timeline, node_dboid, node_status, node_name, node_local_dsn, node_init_from_dsn, node_read_only, node_seq_id" #define ERRBUFF_SIZE 512 @@ -159,8 +160,8 @@ typedef struct s_event_info } t_event_info; #define T_EVENT_INFO_INITIALIZER { \ - NULL, \ - NULL \ + NULL, \ + NULL \ } @@ -174,6 +175,11 @@ typedef struct char **values; } t_conninfo_param_list; +#define T_CONNINFO_PARAM_LIST_INITIALIZER { \ + 0, \ + NULL, \ + NULL, \ +} /* * Struct to store replication slot information @@ -185,6 +191,8 @@ typedef struct s_replication_slot bool active; } t_replication_slot; +#define T_REPLICATION_SLOT_INITIALIZER { "", "", false } + typedef struct s_connection_user { @@ -192,6 +200,8 @@ typedef struct s_connection_user bool is_superuser; } t_connection_user; +#define T_CONNECTION_USER_INITIALIZER { "", false } + /* represents an entry in bdr.bdr_nodes */ typedef struct s_bdr_node_info @@ -237,13 +247,13 @@ typedef struct BdrNodeInfoList typedef struct { uint64 last_wal_receive_lsn; uint64 last_wal_replay_lsn; - char replication_lag_time[MAXLEN]; + int replication_lag_time; } ReplInfo; #define T_REPLINFO_INTIALIZER { \ InvalidXLogRecPtr, \ InvalidXLogRecPtr, \ - "" \ + 0 \ } @@ -254,6 +264,7 @@ typedef struct bool in_data_directory; } t_configfile_info; +#define T_CONFIGFILE_INFO_INITIALIZER { "", "", false } typedef struct { @@ -385,8 +396,6 @@ bool update_node_record_conn_priority(PGconn *conn, t_configuration_options *op void clear_node_info_list(NodeInfoList *nodes); -void get_node_replication_stats(PGconn *conn, t_node_info *node_info); - /* PostgreSQL configuration file location functions */ bool get_datadir_configuration_files(PGconn *conn, KeyValueList *list); bool get_configuration_file_locations(PGconn *conn, t_configfile_list *list); @@ -427,6 +436,7 @@ void reset_voting_status(PGconn *conn); XLogRecPtr get_last_wal_receive_location(PGconn *conn); bool get_replication_info(PGconn *conn, ReplInfo *replication_info); int get_replication_lag_seconds(PGconn *conn); +void get_node_replication_stats(PGconn *conn, t_node_info *node_info); /* BDR functions */ void get_all_bdr_node_records(PGconn *conn, BdrNodeInfoList *node_list); diff --git a/repmgr-action-bdr.c b/repmgr-action-bdr.c index 13853f63..f564b799 100644 --- a/repmgr-action-bdr.c +++ b/repmgr-action-bdr.c @@ -23,9 +23,9 @@ do_bdr_register(void) { PGconn *conn = NULL; BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; - ExtensionStatus extension_status; + ExtensionStatus extension_status = REPMGR_UNKNOWN; t_node_info node_info = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; PQExpBufferData event_details; bool success = true; char dbname[MAXLEN]; @@ -137,7 +137,7 @@ do_bdr_register(void) if (local_node_records.node_count == 0) { BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; - BdrNodeInfoListCell *bdr_cell; + BdrNodeInfoListCell *bdr_cell = NULL; get_all_bdr_node_records(conn, &bdr_nodes); @@ -152,8 +152,8 @@ do_bdr_register(void) { PGconn *bdr_node_conn = NULL; NodeInfoList existing_nodes = T_NODE_INFO_LIST_INITIALIZER; - NodeInfoListCell *cell; - ExtensionStatus other_node_extension_status; + NodeInfoListCell *cell = NULL; + ExtensionStatus other_node_extension_status = REPMGR_UNKNOWN; /* skip the local node */ if (strncmp(node_info.node_name, bdr_cell->node_info->node_name, MAXLEN) == 0) @@ -222,7 +222,7 @@ do_bdr_register(void) if (record_status == RECORD_FOUND) { - bool node_updated; + bool node_updated = false; /* * At this point we will have established there are no non-BDR records, * so no need to verify the node type @@ -314,12 +314,12 @@ do_bdr_register(void) void do_bdr_unregister(void) { - PGconn *conn; - ExtensionStatus extension_status; - int target_node_id; + PGconn *conn = NULL; + ExtensionStatus extension_status = REPMGR_UNKNOWN; + int target_node_id = UNKNOWN_NODE_ID; t_node_info node_info = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; - bool node_record_deleted; + RecordStatus record_status = RECORD_NOT_FOUND; + bool node_record_deleted = false; PQExpBufferData event_details; char dbname[MAXLEN]; diff --git a/repmgr-action-cluster.c b/repmgr-action-cluster.c index d4802179..dbe6691a 100644 --- a/repmgr-action-cluster.c +++ b/repmgr-action-cluster.c @@ -54,10 +54,10 @@ static void cube_set_node_status(t_node_status_cube **cube, int n, int node_id, void do_cluster_show(void) { - PGconn *conn; + PGconn *conn = NULL; NodeInfoList nodes = T_NODE_INFO_LIST_INITIALIZER; - NodeInfoListCell *cell; - int i; + NodeInfoListCell *cell = NULL; + int i = 0; /* Connect to local database to obtain cluster connection data */ log_verbose(LOG_INFO, _("connecting to database\n")); @@ -338,11 +338,11 @@ do_cluster_show(void) void do_cluster_event(void) { - PGconn *conn; + PGconn *conn = NULL; PQExpBufferData query; PQExpBufferData where_clause; PGresult *res; - int i; + int i = 0; conn = establish_db_connection(config_file_options.conninfo, true); @@ -490,7 +490,7 @@ do_cluster_event(void) void do_cluster_crosscheck(void) { - int i, n; + int i = 0, n = 0; char c; const char *node_header = "Name"; int name_length = strlen(node_header); @@ -522,7 +522,7 @@ do_cluster_crosscheck(void) for (column_node_ix = 0; column_node_ix < n; column_node_ix++) { int max_node_status = -2; - int node_ix; + int node_ix = 0; /* * The value of entry (i,j) is equal to the @@ -571,8 +571,8 @@ do_cluster_crosscheck(void) void do_cluster_matrix() { - int i, j; - int n; + int i = 0, j = 0, n = 0; + const char *node_header = "Name"; int name_length = strlen(node_header); @@ -661,11 +661,11 @@ matrix_set_node_status(t_node_matrix_rec **matrix_rec_list, int n, int node_id, static int build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length) { - PGconn *conn; - int i, j; - int local_node_id; + PGconn *conn = NULL; + int i = 0, j = 0; + int local_node_id = UNKNOWN_NODE_ID; NodeInfoList nodes = T_NODE_INFO_LIST_INITIALIZER; - NodeInfoListCell *cell; + NodeInfoListCell *cell = NULL; PQExpBufferData command; PQExpBufferData command_output; @@ -747,9 +747,9 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length) for (cell = nodes.head; cell; cell = cell->next) { - int connection_status; + int connection_status = 0; t_conninfo_param_list remote_conninfo; - char *host, *p; + char *host = NULL, *p = NULL; int connection_node_id = cell->node_info->node_id; int x, y; @@ -854,10 +854,10 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length) static int build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length) { - PGconn *conn; + PGconn *conn = NULL; int h, i, j; NodeInfoList nodes = T_NODE_INFO_LIST_INITIALIZER; - NodeInfoListCell *cell; + NodeInfoListCell *cell = NULL; t_node_status_cube **cube; @@ -893,8 +893,8 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length) for (cell = nodes.head; cell; cell = cell->next) { - int name_length_cur; - NodeInfoListCell *cell_i; + int name_length_cur = 0; + NodeInfoListCell *cell_i = NULL; cube[h] = (t_node_status_cube *) pg_malloc(sizeof(t_node_status_cube)); cube[h]->node_id = cell->node_info->node_id; @@ -947,11 +947,11 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length) for (cell = nodes.head; cell; cell = cell->next) { - int remote_node_id; + int remote_node_id = UNKNOWN_NODE_ID; PQExpBufferData command; PQExpBufferData command_output; - char *p; + char *p = NULL; remote_node_id = cell->node_info->node_id; @@ -988,7 +988,7 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length) else { t_conninfo_param_list remote_conninfo; - char *host; + char *host = NULL; PQExpBufferData quoted_command; initPQExpBuffer("ed_command); diff --git a/repmgr-action-node.c b/repmgr-action-node.c index e2f4dd6c..e86d825e 100644 --- a/repmgr-action-node.c +++ b/repmgr-action-node.c @@ -32,7 +32,7 @@ static void _do_node_restore_config(void); void do_node_status(void) { - PGconn *conn; + PGconn *conn = NULL; int target_node_id = UNKNOWN_NODE_ID; t_node_info node_info = T_NODE_INFO_INITIALIZER; @@ -41,14 +41,15 @@ do_node_status(void) PQExpBufferData output; KeyValueList node_status = { NULL, NULL }; - KeyValueListCell *cell; + KeyValueListCell *cell = NULL; ItemList warnings = { NULL, NULL }; - RecoveryType recovery_type; + RecoveryType recovery_type = RECTYPE_UNKNOWN; ReplInfo replication_info = T_REPLINFO_INTIALIZER; t_recovery_conf recovery_conf = T_RECOVERY_CONF_INITIALIZER; - char data_dir[MAXPGPATH] = ""; + char data_dir[MAXPGPATH] = ""; + if (runtime_options.is_shutdown == true) @@ -111,6 +112,18 @@ do_node_status(void) "Conninfo", node_info.conninfo); + if (runtime_options.verbose == true) + { + uint64 local_system_identifier = UNKNOWN_SYSTEM_IDENTIFIER; + + local_system_identifier = get_system_identifier(config_file_options.data_directory); + + key_value_list_set_format( + &node_status, + "System identifier", + "%lu", local_system_identifier); + } + key_value_list_set( &node_status, "Role", @@ -289,6 +302,12 @@ do_node_status(void) get_replication_info(conn, &replication_info); + key_value_list_set_format( + &node_status, + "Replication lag", + "%i seconds", + replication_info.replication_lag_time); + key_value_list_set_format( &node_status, "Last received LSN", @@ -307,6 +326,11 @@ do_node_status(void) "(none)"); key_value_list_set_output_mode(&node_status, "Upstream node", OM_CSV); + key_value_list_set( + &node_status, + "Replication lag", + "n/a"); + key_value_list_set( &node_status, "Last received LSN", @@ -397,6 +421,7 @@ do_node_status(void) } key_value_list_free(&node_status); + item_list_free(&warnings); PQfinish(conn); } @@ -413,7 +438,7 @@ void _do_node_status_is_shutdown(void) bool is_shutdown = true; DBState db_state; - XLogRecPtr checkPoint; + XLogRecPtr checkPoint = InvalidXLogRecPtr; initPQExpBuffer(&output); @@ -490,7 +515,7 @@ void _do_node_status_is_shutdown(void) void do_node_check(void) { - PGconn *conn; + PGconn *conn = NULL; if (strlen(config_file_options.conninfo)) conn = establish_db_connection(config_file_options.conninfo, true); @@ -513,6 +538,8 @@ do_node_check(void) return; } + + PQfinish(conn); } CheckStatus @@ -675,7 +702,7 @@ do_node_check_replication_lag(PGconn *conn, OutputMode mode, PQExpBufferData *ou CheckStatus status = CHECK_STATUS_UNKNOWN; bool own_buffer = false; PQExpBufferData buf; - int lag_seconds; + int lag_seconds = 0; if (mode == OM_CSV) { @@ -884,7 +911,7 @@ do_node_service(void) } else { - PGconn *conn; + PGconn *conn = NULL; if (strlen(config_file_options.conninfo)) conn = establish_db_connection(config_file_options.conninfo, true); @@ -1038,7 +1065,7 @@ do_node_rejoin(void) char filebuf[MAXPGPATH] = ""; t_node_info primary_node_record = T_NODE_INFO_INITIALIZER; - bool success; + bool success = true; /* check node is not actually running */ @@ -1087,7 +1114,7 @@ do_node_rejoin(void) /* check provided upstream connection */ upstream_conn = establish_db_connection_by_params(&source_conninfo, true); -/* establish_db_connection(runtime_options.upstream_conninfo, true); */ + /* establish_db_connection(runtime_options.upstream_conninfo, true); */ if (get_primary_node_record(upstream_conn, &primary_node_record) == false) { @@ -1230,7 +1257,7 @@ _do_node_archive_config(void) KeyValueList config_files = { NULL, NULL }; - KeyValueListCell *cell; + KeyValueListCell *cell = NULL; int copied_count = 0; format_archive_dir(archive_dir); @@ -1281,20 +1308,18 @@ _do_node_archive_config(void) */ while ((arcdir_ent = readdir(arcdir)) != NULL) { - char arcdir_ent_path[MAXPGPATH]; + char arcdir_ent_path[MAXPGPATH] = ""; snprintf(arcdir_ent_path, MAXPGPATH, "%s/%s", archive_dir, arcdir_ent->d_name); - if (stat(arcdir_ent_path, &statbuf) == 0 && !S_ISREG(statbuf.st_mode)) { continue; } - if (unlink(arcdir_ent_path) == -1) { log_error(_("unable to delete file in temporary archive directory")); @@ -1311,7 +1336,8 @@ _do_node_archive_config(void) * extract list of config files from --config-files */ { - int i = 0, j; + int i = 0; + int j = 0; int config_file_len = strlen(runtime_options.config_files); char filenamebuf[MAXLEN] = ""; @@ -1404,7 +1430,7 @@ _do_node_archive_config(void) static void _do_node_restore_config(void) { - char archive_dir[MAXPGPATH]; + char archive_dir[MAXPGPATH] = ""; DIR *arcdir; struct dirent *arcdir_ent; @@ -1511,7 +1537,7 @@ static bool copy_file(const char *src_file, const char *dest_file) { FILE *ptr_old, *ptr_new; - int a; + int a = 0; ptr_old = fopen(src_file, "r"); ptr_new = fopen(dest_file, "w"); diff --git a/repmgr-action-primary.c b/repmgr-action-primary.c index d5695214..e5482380 100644 --- a/repmgr-action-primary.c +++ b/repmgr-action-primary.c @@ -24,11 +24,11 @@ do_primary_register(void) PGconn *conn = NULL; PGconn *primary_conn = NULL; int current_primary_id = UNKNOWN_NODE_ID; - RecoveryType recovery_type; + RecoveryType recovery_type = RECTYPE_UNKNOWN; t_node_info node_info = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; - bool record_created; + bool record_created = false; PQExpBufferData event_description; @@ -230,7 +230,7 @@ do_primary_unregister(void) PGconn *local_conn = NULL; t_node_info local_node_info = T_NODE_INFO_INITIALIZER; - t_node_info *target_node_info_ptr; + t_node_info *target_node_info_ptr = NULL; PGconn *target_node_conn = NULL; NodeInfoList downstream_nodes = T_NODE_INFO_LIST_INITIALIZER; @@ -249,7 +249,7 @@ do_primary_unregister(void) if (PQstatus(primary_conn) != CONNECTION_OK) { - t_node_info primary_node_info; + t_node_info primary_node_info = T_NODE_INFO_INITIALIZER; log_error(_("unable to connect to primary server")); @@ -290,7 +290,7 @@ do_primary_unregister(void) if (downstream_nodes.node_count > 0) { - NodeInfoListCell *cell; + NodeInfoListCell *cell = NULL; PQExpBufferData detail; if (downstream_nodes.node_count == 1) @@ -387,7 +387,7 @@ do_primary_unregister(void) else if (recovery_type == RECTYPE_PRIMARY) { t_node_info primary_node_info = T_NODE_INFO_INITIALIZER; - bool primary_record_found; + bool primary_record_found = false; primary_record_found = get_primary_node_record(primary_conn, &primary_node_info); diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index 7b558aee..dcaadaa2 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -97,7 +97,7 @@ void do_standby_clone(void) { PQExpBufferData event_details; - int r; + int r = 0; /* dummy node record */ t_node_info node_record = T_NODE_INFO_INITIALIZER; @@ -252,7 +252,7 @@ do_standby_clone(void) { /* parse returned upstream conninfo string to recovery primary_conninfo params*/ char *errmsg = NULL; - bool parse_success; + bool parse_success = false; log_verbose(LOG_DEBUG, "parsing upstream conninfo string \"%s\"", recovery_conninfo_str); @@ -414,7 +414,7 @@ do_standby_clone(void) */ { t_node_info node_record = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; record_status = get_node_record(primary_conn, config_file_options.node_id, @@ -480,7 +480,7 @@ void check_barman_config(void) { char command[MAXLEN]; - bool command_ok; + bool command_ok = false; /* * Check that there is at least one valid backup @@ -563,12 +563,12 @@ check_barman_config(void) void do_standby_register(void) { - PGconn *conn; - PGconn *primary_conn; + PGconn *conn = NULL; + PGconn *primary_conn = NULL; - bool record_created; + bool record_created = false; t_node_info node_record = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; PQExpBufferData details; @@ -688,7 +688,7 @@ do_standby_register(void) */ if (runtime_options.upstream_node_id != NO_UPSTREAM_NODE) { - RecordStatus upstream_record_status; + RecordStatus upstream_record_status = RECORD_NOT_FOUND; upstream_record_status = get_node_record(primary_conn, runtime_options.upstream_node_id, @@ -861,7 +861,7 @@ do_standby_register(void) { bool sync_ok = false; int timer = 0; - RecordStatus node_record_status; + RecordStatus node_record_status = RECORD_NOT_FOUND; t_node_info node_record_on_primary = T_NODE_INFO_INITIALIZER; t_node_info node_record_on_standby = T_NODE_INFO_INITIALIZER; @@ -962,13 +962,13 @@ do_standby_register(void) void do_standby_unregister(void) { - PGconn *conn; - PGconn *primary_conn; + PGconn *conn = NULL; + PGconn *primary_conn = NULL; - int target_node_id; + int target_node_id = UNKNOWN_NODE_ID; t_node_info node_info = T_NODE_INFO_INITIALIZER; - bool node_record_deleted; + bool node_record_deleted = false; log_info(_("connecting to local standby")); conn = establish_db_connection(config_file_options.conninfo, true); @@ -1050,10 +1050,10 @@ do_standby_unregister(void) void do_standby_promote(void) { - PGconn *conn; - PGconn *current_primary_conn; + PGconn *conn = NULL; + PGconn *current_primary_conn = NULL; - RecoveryType recovery_type; + RecoveryType recovery_type = RECTYPE_UNKNOWN; int existing_primary_id = UNKNOWN_NODE_ID; @@ -1124,8 +1124,8 @@ _do_standby_promote_internal(const char *data_dir) promote_check_interval = 2; bool promote_success = false; PQExpBufferData details; - PGconn *conn; - RecoveryType recovery_type; + PGconn *conn = NULL; + RecoveryType recovery_type = RECTYPE_UNKNOWN; log_notice(_("promoting standby")); @@ -1242,10 +1242,10 @@ do_standby_follow(void) int primary_id = UNKNOWN_NODE_ID; t_node_info primary_node_record = T_NODE_INFO_INITIALIZER; - int timer; + int timer = 0; PQExpBufferData follow_output; - bool success; + bool success = false; uint64 local_system_identifier = UNKNOWN_SYSTEM_IDENTIFIER; t_conninfo_param_list repl_conninfo; @@ -1377,10 +1377,10 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor { t_node_info local_node_record = T_NODE_INFO_INITIALIZER; int original_upstream_node_id = UNKNOWN_NODE_ID; - char restart_command[MAXLEN]; + char restart_command[MAXLEN] = ""; int r; - RecordStatus record_status; + RecordStatus record_status = RECORD_NOT_FOUND; char *errmsg = NULL; @@ -1511,8 +1511,8 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor if (config_file_options.use_replication_slots && runtime_options.host_param_provided == false && original_upstream_node_id != UNKNOWN_NODE_ID) { t_node_info upstream_node_record = T_NODE_INFO_INITIALIZER; - RecordStatus upstream_record_status; - PGconn *local_conn; + RecordStatus upstream_record_status = RECORD_NOT_FOUND; + PGconn *local_conn = NULL; log_verbose(LOG_INFO, "attempting to remove replication slot from old upstream node %i", original_upstream_node_id); @@ -1599,19 +1599,19 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor void do_standby_switchover(void) { - PGconn *local_conn; - PGconn *remote_conn; + PGconn *local_conn = NULL; + PGconn *remote_conn = NULL; t_node_info local_node_record = T_NODE_INFO_INITIALIZER; /* the remote server is the primary to be demoted */ char remote_conninfo[MAXCONNINFO] = ""; char remote_host[MAXLEN] = ""; - int remote_node_id; + int remote_node_id = UNKNOWN_NODE_ID; t_node_info remote_node_record = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; - RecoveryType recovery_type; + RecordStatus record_status = RECORD_NOT_FOUND; + RecoveryType recovery_type = RECTYPE_UNKNOWN; PQExpBufferData remote_command_str; PQExpBufferData command_output; PQExpBufferData node_rejoin_options; @@ -1636,7 +1636,6 @@ do_standby_switchover(void) * to be demoted) - careful checks needed before proceding. */ - local_conn = establish_db_connection(config_file_options.conninfo, true); record_status = get_node_record(local_conn, config_file_options.node_id, &local_node_record); @@ -1786,7 +1785,7 @@ do_standby_switchover(void) { int files = 0; int threshold = 0; - bool command_success; + bool command_success = false; initPQExpBuffer(&remote_command_str); make_remote_repmgr_path(&remote_command_str, &remote_node_record); @@ -2166,7 +2165,7 @@ do_standby_switchover(void) initPQExpBuffer(&node_rejoin_options); if (replication_info.last_wal_receive_lsn < remote_last_checkpoint_lsn) { - KeyValueListCell *cell; + KeyValueListCell *cell = NULL; bool first_entry = true; if (runtime_options.force_rewind == false) @@ -2213,8 +2212,6 @@ do_standby_switchover(void) termPQExpBuffer(&remote_command_str); termPQExpBuffer(&node_rejoin_options); - - /* TODO: verify this node's record was updated correctly */ create_event_record(local_conn, @@ -2260,14 +2257,15 @@ do_standby_switchover(void) { int failed_follow_count = 0; char host[MAXLEN] = ""; - NodeInfoListCell *cell; + NodeInfoListCell *cell = NULL; + log_notice(_("executing STANDBY FOLLOW on %i of %i siblings"), sibling_nodes.node_count - unreachable_sibling_node_count, sibling_nodes.node_count); for (cell = sibling_nodes.head; cell; cell = cell->next) { - bool success; + bool success = false; t_node_info sibling_node_record = T_NODE_INFO_INITIALIZER; /* skip nodes previously determined as unreachable */ @@ -2335,8 +2333,8 @@ check_source_server() char cluster_size[MAXLEN]; t_node_info node_record = T_NODE_INFO_INITIALIZER; - RecordStatus record_status; - ExtensionStatus extension_status; + RecordStatus record_status = RECORD_NOT_FOUND; + ExtensionStatus extension_status = REPMGR_UNKNOWN; /* Attempt to connect to the upstream server to verify its configuration */ log_info(_("connecting to upstream node")); @@ -2512,17 +2510,17 @@ check_source_server() static void check_source_server_via_barman() { - char buf[MAXLEN]; - char barman_conninfo_str[MAXLEN]; - t_conninfo_param_list barman_conninfo; + char buf[MAXLEN] = ""; + char barman_conninfo_str[MAXLEN] = ""; + t_conninfo_param_list barman_conninfo = T_CONNINFO_PARAM_LIST_INITIALIZER; char *errmsg = NULL; - bool parse_success, - command_success; + bool parse_success = false, + command_success = false; char where_condition[MAXLEN]; PQExpBufferData command_output; PQExpBufferData repmgr_conninfo_buf; - int c; + int c = 0; get_barman_property(barman_conninfo_str, "conninfo", local_repmgr_tmp_directory); @@ -2612,7 +2610,7 @@ initialise_direct_clone(t_node_info *node_record) { PGconn *superuser_conn = NULL; PGconn *privileged_conn = NULL; - bool success; + bool success = false; /* * Check the destination data directory can be used @@ -2638,7 +2636,7 @@ initialise_direct_clone(t_node_info *node_record) if (config_file_options.tablespace_mapping.head != NULL) { - TablespaceListCell *cell; + TablespaceListCell *cell = false; KeyValueList not_found = { NULL, NULL }; int total = 0, matched = 0; @@ -2773,10 +2771,10 @@ initialise_direct_clone(t_node_info *node_record) static int run_basebackup(t_node_info *node_record) { - char script[MAXLEN]; + char script[MAXLEN] = ""; int r = SUCCESS; PQExpBufferData params; - TablespaceListCell *cell; + TablespaceListCell *cell = NULL; t_basebackup_options backup_options = T_BASEBACKUP_OPTIONS_INITIALIZER; /* @@ -2802,8 +2800,8 @@ run_basebackup(t_node_info *node_record) */ if (runtime_options.conninfo_provided == true) { - t_conninfo_param_list conninfo; - char *conninfo_str; + t_conninfo_param_list conninfo = T_CONNINFO_PARAM_LIST_INITIALIZER; + char *conninfo_str = NULL; initialize_conninfo_params(&conninfo, false); @@ -2933,14 +2931,14 @@ run_file_backup(t_node_info *node_record) { int r = SUCCESS, i; - char command[MAXLEN]; - char filename[MAXLEN]; - char buf[MAXLEN]; - char basebackups_directory[MAXLEN]; + char command[MAXLEN] = ""; + char filename[MAXLEN] = ""; + char buf[MAXLEN] = ""; + char basebackups_directory[MAXLEN] = ""; char backup_id[MAXLEN] = ""; - char *p, *q; + char *p = NULL, *q = NULL; TablespaceDataList tablespace_list = { NULL, NULL }; - TablespaceDataListCell *cell_t; + TablespaceDataListCell *cell_t = NULL; PQExpBufferData tablespace_map; bool tablespace_map_rewrite = false; @@ -2966,9 +2964,9 @@ run_file_backup(t_node_info *node_record) { FILE *fi; /* input stream */ FILE *fd; /* output for data.txt */ - char prefix[MAXLEN]; - char output[MAXLEN]; - int n; + char prefix[MAXLEN] = ""; + char output[MAXLEN] = ""; + int n = 0; maxlen_snprintf(command, "%s list-files --target=data %s latest", make_barman_ssh_command(barman_command_buf), @@ -3196,8 +3194,8 @@ run_file_backup(t_node_info *node_record) for (cell_t = tablespace_list.head; cell_t; cell_t = cell_t->next) { bool mapping_found = false; - TablespaceListCell *cell; - char *tblspc_dir_dest; + TablespaceListCell *cell = NULL; + char *tblspc_dir_dest = NULL; /* Check if tablespace path matches one of the provided tablespace mappings */ if (config_file_options.tablespace_mapping.head != NULL) @@ -3349,8 +3347,6 @@ run_file_backup(t_node_info *node_record) fclose(tablespace_map_file); } - - stop_backup: if (mode == barman) @@ -3392,11 +3388,11 @@ get_tablespace_data_barman * [('main', 24674, '/var/lib/postgresql/tablespaces/9.5/main'), ('alt', 24678, '/var/lib/postgresql/tablespaces/9.5/alt')] */ - char name[MAXLEN]; - char oid[MAXLEN]; - char location[MAXPGPATH]; + char name[MAXLEN] = ""; + char oid[MAXLEN] = ""; + char location[MAXPGPATH] = ""; char *p = tablespace_data_barman; - int i; + int i = 0; tablespace_list->head = NULL; tablespace_list->tail = NULL; @@ -3447,9 +3443,9 @@ void get_barman_property(char *dst, char *name, char *local_repmgr_directory) { PQExpBufferData command_output; - char buf[MAXLEN]; - char command[MAXLEN]; - char *p; + char buf[MAXLEN] = ""; + char command[MAXLEN] = ""; + char *p = NULL; initPQExpBuffer(&command_output); @@ -3478,7 +3474,7 @@ static void copy_configuration_files(void) { int i, r; - t_configfile_info *file; + t_configfile_info *file = NULL; char *host = NULL; /* get host from upstream record */ @@ -3500,7 +3496,7 @@ copy_configuration_files(void) for (i = 0; i < config_files.entries; i++) { - char dest_path[MAXPGPATH]; + char dest_path[MAXPGPATH] = ""; file = config_files.files[i]; /* @@ -3538,7 +3534,7 @@ copy_configuration_files(void) static void tablespace_data_append(TablespaceDataList *list, const char *name, const char *oid, const char *location) { - TablespaceDataListCell *cell; + TablespaceDataListCell *cell = NULL; cell = (TablespaceDataListCell *) pg_malloc0(sizeof(TablespaceDataListCell)); @@ -3577,11 +3573,11 @@ tablespace_data_append(TablespaceDataList *list, const char *name, const char *o static void check_primary_standby_version_match(PGconn *conn, PGconn *primary_conn) { - char standby_version[MAXVERSIONSTR]; - int standby_version_num = 0; + char standby_version[MAXVERSIONSTR] = ""; + int standby_version_num = UNKNOWN_SERVER_VERSION_NUM; - char primary_version[MAXVERSIONSTR]; - int primary_version_num = 0; + char primary_version[MAXVERSIONSTR] = ""; + int primary_version_num = UNKNOWN_SERVER_VERSION_NUM; standby_version_num = check_server_version(conn, "standby", true, standby_version); @@ -3634,13 +3630,12 @@ check_recovery_type(PGconn *conn) static void drop_replication_slot_if_exists(PGconn *conn, int node_id, char *slot_name) { - t_replication_slot slot_info; - int record_status; - - record_status = get_slot_record(conn, slot_name, &slot_info); + t_replication_slot slot_info = T_REPLICATION_SLOT_INITIALIZER; + RecordStatus record_status = get_slot_record(conn, slot_name, &slot_info); log_verbose(LOG_DEBUG, "attempting to delete slot \"%s\" on node %i", slot_name, node_id); + if (record_status != RECORD_FOUND) { log_info(_("no slot record found for slot \"%s\" on node %i"), diff --git a/repmgr-client.c b/repmgr-client.c index aa723924..27c9aaec 100644 --- a/repmgr-client.c +++ b/repmgr-client.c @@ -578,7 +578,7 @@ main(int argc, char **argv) item_list_append(&cli_errors, conninfo_error.data); termPQExpBuffer(&conninfo_error); - free(errmsg); + pfree(errmsg); } else { @@ -619,6 +619,8 @@ main(int argc, char **argv) strncpy(runtime_options.username, opt->val, MAXLEN); } } + + PQconninfoFree(opts); } } else @@ -661,7 +663,7 @@ main(int argc, char **argv) * NODE { STATUS | CHECK | REJOIN | ARCHIVE-CONFIG | RESTORE-CONFIG | SERVICE } | * CLUSTER { CROSSCHECK | MATRIX | SHOW | CLEANUP | EVENT } * - * [node] is an optional hostname, provided instead of the -h/--host optipn + * [node] is an optional hostname, provided instead of the -h/--host option */ if (optind < argc) { @@ -953,8 +955,8 @@ main(int argc, char **argv) if (runtime_options.node_id != UNKNOWN_NODE_ID || runtime_options.node_name[0] != '\0') { - PGconn *conn; - RecordStatus record_status; + PGconn *conn = NULL; + RecordStatus record_status = RECORD_NOT_FOUND; log_verbose(LOG_DEBUG, "connecting to local node to retrieve record for node specified with --node-id or --node-name"); @@ -1533,7 +1535,7 @@ action_name(const int action) void print_error_list(ItemList *error_list, int log_level) { - ItemListCell *cell; + ItemListCell *cell = NULL; for (cell = error_list->head; cell; cell = cell->next) { @@ -1635,9 +1637,9 @@ create_repmgr_extension(PGconn *conn) PQExpBufferData query; PGresult *res; - ExtensionStatus extension_status; + ExtensionStatus extension_status = REPMGR_UNKNOWN; - t_connection_user userinfo; + t_connection_user userinfo = T_CONNECTION_USER_INITIALIZER; bool is_superuser = false; PGconn *superuser_conn = NULL; PGconn *schema_create_conn = NULL; @@ -1791,7 +1793,7 @@ create_repmgr_extension(PGconn *conn) int check_server_version(PGconn *conn, char *server_type, bool exit_on_error, char *server_version_string) { - int conn_server_version_num = 0; + int conn_server_version_num = UNKNOWN_SERVER_VERSION_NUM; conn_server_version_num = get_server_version(conn, server_version_string); if (conn_server_version_num < MIN_SUPPORTED_VERSION_NUM) @@ -1819,7 +1821,7 @@ check_server_version(PGconn *conn, char *server_type, bool exit_on_error, char * int test_ssh_connection(char *host, char *remote_user) { - char script[MAXLEN]; + char script[MAXLEN] = ""; int r = 1, i; /* On some OS, true is located in a different place than in Linux @@ -1863,7 +1865,7 @@ local_command(const char *command, PQExpBufferData *outputbuf) { FILE *fp; char output[MAXLEN]; - int retval; + int retval = 0; if (outputbuf == NULL) { @@ -1899,8 +1901,8 @@ local_command(const char *command, PQExpBufferData *outputbuf) void get_superuser_connection(PGconn **conn, PGconn **superuser_conn, PGconn **privileged_conn) { - t_connection_user userinfo; - bool is_superuser; + t_connection_user userinfo = T_CONNECTION_USER_INITIALIZER; + bool is_superuser = false; /* this should never happen */ if (PQstatus(*conn) != CONNECTION_OK) @@ -2123,18 +2125,18 @@ check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error) { if (i == 0) { - log_error(_("parameter 'wal_keep_segments' on the upstream server must be be set to %s or greater"), + log_error(_("parameter \"wal_keep_segments\" on the upstream server must be be set to %s or greater"), min_wal_keep_segments); log_hint(_("Choose a value sufficiently high enough to retain enough WAL " "until the standby has been cloned and started.\n " "Alternatively set up WAL archiving using e.g. PgBarman and configure " - "'restore_command' in repmgr.conf to fetch WALs from there." - )); + "'restore_command' in repmgr.conf to fetch WALs from there.")); + if (server_version_num >= 90400) { log_hint(_("In PostgreSQL 9.4 and later, replication slots can be used, which " - "do not require 'wal_keep_segments' to be set " - "(set parameter 'use_replication_slots' in repmgr.conf to enable)\n" + "do not require \"wal_keep_segments\" to be set " + "(set parameter \"use_replication_slots\" in repmgr.conf to enable)\n" )); } } @@ -2168,7 +2170,7 @@ check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error) if (i == 0 || i == -1) { if (i == 0) - log_error(_("parameter 'archive_command' must be set to a valid command")); + log_error(_("parameter \"archive_command\" must be set to a valid command")); if (exit_on_error == true) { @@ -2210,8 +2212,8 @@ check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error) { if (i == 0) { - log_error(_("parameter 'max_wal_senders' must be set to be at least 1")); - log_hint(_("'max_wal_senders' should be set to at least the number of expected standbys")); + log_error(_("parameter \"max_wal_senders\" must be set to be at least 1")); + log_hint(_("\"max_wal_senders\" should be set to at least the number of expected standbys")); } if (exit_on_error == true) @@ -2295,7 +2297,8 @@ check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error) * Alternatively call PQconnectStart() and poll for presence/absence of CONNECTION_AUTH_OK ? */ log_error(_("unable to establish necessary replication connections")); - log_hint(_("increase 'max_wal_senders' by at least %i"), min_replication_connections - possible_replication_connections); + log_hint(_("increase \"max_wal_senders\" by at least %i"), + min_replication_connections - possible_replication_connections); if (exit_on_error == true) { @@ -2339,9 +2342,9 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_path, bool is_directory, int server_version_num) { PQExpBufferData rsync_flags; - char script[MAXLEN]; - char host_string[MAXLEN]; - int r; + char script[MAXLEN] = ""; + char host_string[MAXLEN] = ""; + int r = 0; initPQExpBuffer(&rsync_flags); @@ -2456,8 +2459,8 @@ bool create_recovery_file(t_node_info *node_record, t_conninfo_param_list *recovery_conninfo, const char *data_dir) { FILE *recovery_file; - char recovery_file_path[MAXPGPATH]; - char line[MAXLEN]; + char recovery_file_path[MAXPGPATH] = ""; + char line[MAXLEN] = ""; mode_t um; maxpath_snprintf(recovery_file_path, "%s/%s", data_dir, RECOVERY_COMMAND_FILE); @@ -2577,7 +2580,7 @@ write_primary_conninfo(char *line, t_conninfo_param_list *param_list) PQExpBufferData conninfo_buf; bool application_name_provided = false; int c; - char *escaped; + char *escaped = NULL; initPQExpBuffer(&conninfo_buf); @@ -2640,10 +2643,10 @@ bool remote_command(const char *host, const char *user, const char *command, PQExpBufferData *outputbuf) { FILE *fp; - char ssh_command[MAXLEN]; + char ssh_command[MAXLEN] = ""; PQExpBufferData ssh_host; - char output[MAXLEN]; + char output[MAXLEN] = ""; initPQExpBuffer(&ssh_host); diff --git a/strutil.c b/strutil.c index a3b734b3..837ef6d4 100644 --- a/strutil.c +++ b/strutil.c @@ -127,6 +127,24 @@ item_list_append_format(ItemList *item_list, const char *format, ...) } +void +item_list_free(ItemList *item_list) +{ + ItemListCell *cell; + ItemListCell *next_cell; + + cell = item_list->head; + + while (cell != NULL) + { + next_cell = cell->next; + pfree(cell->string); + pfree(cell); + cell = next_cell; + } +} + + void key_value_list_set(KeyValueList *item_list, const char *key, const char *value) { @@ -208,7 +226,6 @@ key_value_list_free(KeyValueList *item_list) pfree(cell); cell = next_cell; } - } diff --git a/strutil.h b/strutil.h index f64b32eb..159c8e00 100644 --- a/strutil.h +++ b/strutil.h @@ -69,6 +69,9 @@ extern void item_list_append_format(ItemList *item_list, const char *format, ...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); +extern void +item_list_free(ItemList *item_list); + extern void key_value_list_set(KeyValueList *item_list, const char *key, const char *value);