Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
Unify use of error codes across all exit statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Smith authored and Greg Smith committed Feb 10, 2011
1 parent 7e7c45c commit 78b925e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Gabriele Bartolini <gabriele@2ndQuadrant.com>
Bas van Oostveen <v.oostveen@gmail.com>
Hannu Krosing <hannu@2ndQuadrant.com>
Cédric Villemain <cedric@2ndquadrant.fr>
Charles Duffy <charles@dyfis.net>
6 changes: 4 additions & 2 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1.0.0 2010-12-05 First public release
1.0.0 2010-12-05 First public release (Greg Smith)

1.0.1 2011-02-XX Fix missing "--force" option in help
Correct warning message for wal_keep_segments (Bas van Oostveen)
Expand All @@ -13,4 +13,6 @@
Avoid buffer overruns by using snprintf etc. (Gabriele)
Fix use of database query after close (Gabriele)
Add information about progress during "standby clone" (Gabriele)

Fix double free error in repmgrd (Charles Duffy)
Make repmgr exit with an error code when encountering an error (Charles)
Standardize on error return codes, use in repmgrd too (Greg)
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,23 @@ and the same in the standby.
The repmgr daemon creates 2 connections: one to the master and another to the
standby.

Error codes
-----------

When the repmgr or repmgrd program exits, it will set one of the
following

* SUCCESS 0: Program ran successfully.

* ERR_BAD_CONFIG 1: One of the configuration checks the program makes failed.
* ERR_BAD_RSYNC 2: An rsync call made by the program returned an error.
* ERR_STOP_BACKUP 3: A ``pg_stop_backup()`` call made by the program didn't succeed.
* ERR_NO_RESTART 4: An attempt to restart a PostgreSQL instance failed.
* ERR_NEEDS_XLOG 5: Could note create the ``pg_xlog`` directory when cloning.
* ERR_DB_CON 6: Error when trying to connect to a database.
* ERR_DB_QUERY 7: Error executing a database query.
* ERR_PROMOTED 8: Exiting program because the node has been promoted to master.

Detailed walkthrough
====================

Expand Down
6 changes: 3 additions & 3 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ parse_config(const char* config_file, t_configuration_options* options)

if (fp == NULL) {
fprintf(stderr, _("Could not find configuration file '%s'\n"), config_file);
exit(1);
exit(ERR_BAD_CONFIG);
}

/* Initialize */
Expand Down Expand Up @@ -75,14 +75,14 @@ parse_config(const char* config_file, t_configuration_options* options)
{
fprintf(stderr, "Cluster name is missing. "
"Check the configuration file.\n");
exit(1);
exit(ERR_BAD_CONFIG);
}

if (config->node == -1)
{
fprintf(stderr, "Node information is missing. "
"Check the configuration file.\n");
exit(1);
exit(ERR_BAD_CONFIG);
}
}

Expand Down
12 changes: 6 additions & 6 deletions dbutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ establishDBConnection(const char *conninfo, const bool exit_on_error)
if (exit_on_error)
{
PQfinish(conn);
exit(1);
exit(ERR_DB_CON);
}
}

Expand All @@ -57,7 +57,7 @@ is_standby(PGconn *conn)
fprintf(stderr, "Can't query server mode: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
exit(ERR_NO_DB_CON);
}

if (strcmp(PQgetvalue(res, 0, 0), "f") == 0)
Expand Down Expand Up @@ -89,7 +89,7 @@ pg_version(PGconn *conn, char* major_version)
fprintf(stderr, "PQexec failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
exit(ERR_DB_QUERY);
}
major_version1 = atoi(PQgetvalue(res, 0, 0));
major_version2 = PQgetvalue(res, 0, 1);
Expand Down Expand Up @@ -123,7 +123,7 @@ guc_setted(PGconn *conn, const char *parameter, const char *op, const char *valu
fprintf(stderr, "PQexec failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
exit(ERR_DB_QUERY);
}
if (PQntuples(res) == 0)
{
Expand Down Expand Up @@ -152,7 +152,7 @@ get_cluster_size(PGconn *conn)
fprintf(stderr, "PQexec failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
exit(ERR_DB_QUERY);
}
size = PQgetvalue(res, 0, 0);
PQclear(res);
Expand Down Expand Up @@ -184,7 +184,7 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, int *master_id)
fprintf(stderr, "Can't get nodes info: %s\n", PQerrorMessage(standby_conn));
PQclear(res1);
PQfinish(standby_conn);
exit(1);
exit(ERR_DB_QUERY);
}

for (i = 0; i < PQntuples(res1); i++)
Expand Down
4 changes: 2 additions & 2 deletions repmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ main(int argc, char **argv)
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
help(progname);
exit(0);
exit(SUCCESS);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
printf("%s (PostgreSQL) " PG_VERSION "\n", progname);
exit(0);
exit(SUCCESS);
}
}

Expand Down
4 changes: 4 additions & 0 deletions repmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@

/* Exit return code */

#define SUCCESS 0
#define ERR_BAD_CONFIG 1
#define ERR_BAD_RSYNC 2
#define ERR_STOP_BACKUP 3
#define ERR_NO_RESTART 4
#define ERR_NEEDS_XLOG 5
#define ERR_DB_CON 6
#define ERR_DB_QUERY 7
#define ERR_PROMOTED 8

/* Run time options type */
typedef struct {
Expand Down
24 changes: 12 additions & 12 deletions repmgrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ main(int argc, char **argv)
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
help(progname);
exit(0);
exit(SUCCESS);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
printf("%s (PostgreSQL) " PG_VERSION "\n", progname);
exit(0);
exit(SUCCESS);
}
}

Expand All @@ -129,7 +129,7 @@ main(int argc, char **argv)
break;
default:
usage();
exit(1);
exit(ERR_BAD_CONFIG);
}
}

Expand All @@ -143,7 +143,7 @@ main(int argc, char **argv)
{
log_err("Node information is missing. "
"Check the configuration file.\n");
exit(1);
exit(ERR_BAD_CONFIG);
}
logger_init(progname, local_options.loglevel, local_options.logfacility);
snprintf(repmgr_schema, MAXLEN, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX, local_options.cluster_name);
Expand All @@ -156,7 +156,7 @@ main(int argc, char **argv)
{
PQfinish(myLocalConn);
log_err(_("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
exit(1);
exit(ERR_BAD_CONFIG);
}

/*
Expand All @@ -175,7 +175,7 @@ main(int argc, char **argv)
/* I need the id of the primary as well as a connection to it */
primaryConn = getMasterConnection(myLocalConn, local_options.node, local_options.cluster_name, &primary_options.node);
if (primaryConn == NULL)
exit(1);
exit(ERR_BAD_CONFIG);
}

checkClusterConfiguration();
Expand Down Expand Up @@ -258,15 +258,15 @@ MonitorExecute(void)
if (PQstatus(primaryConn) != CONNECTION_OK)
{
log_err(_("We couldn't reconnect for long enough, exiting..."));
exit(1);
exit(ERR_DB_CON);
}

/* Check if we still are a standby, we could have been promoted */
if (!is_standby(myLocalConn))
{
log_err(_("It seems like we have been promoted, so exit from monitoring..."));
CloseConnections();
exit(1);
exit(ERR_PROMOTED);
}

/*
Expand Down Expand Up @@ -354,7 +354,7 @@ checkClusterConfiguration(void)
PQclear(res);
PQfinish(myLocalConn);
PQfinish(primaryConn);
exit(1);
exit(ERR_DB_QUERY);
}

/*
Expand All @@ -368,7 +368,7 @@ checkClusterConfiguration(void)
PQclear(res);
PQfinish(myLocalConn);
PQfinish(primaryConn);
exit(1);
exit(ERR_BAD_CONFIG);
}
PQclear(res);
}
Expand All @@ -393,7 +393,7 @@ checkNodeConfiguration(char *conninfo)
PQclear(res);
PQfinish(myLocalConn);
PQfinish(primaryConn);
exit(1);
exit(ERR_BAD_CONFIG);
}

/*
Expand All @@ -414,7 +414,7 @@ checkNodeConfiguration(char *conninfo)
PQerrorMessage(primaryConn));
PQfinish(myLocalConn);
PQfinish(primaryConn);
exit(1);
exit(ERR_BAD_CONFIG);
}
}
PQclear(res);
Expand Down

0 comments on commit 78b925e

Please sign in to comment.