Merge remote branch 'gbartolini/gabriele-2010-12' into heroku

Conflicts:
	config.c
	dbutils.c
	dbutils.h
	repmgrd.c

Signed-off-by: Dan Farina <drfarina@acm.org>
This commit is contained in:
Dan Farina
2010-12-21 16:06:42 -08:00
6 changed files with 48 additions and 52 deletions

View File

@@ -11,11 +11,15 @@
#include "strutil.h" #include "strutil.h"
void void
parse_config(const char *config_file, char *cluster_name, int *node, parse_config(const char *config_file, char *cluster_name, int *node,
char *conninfo) char *conninfo)
{ {
char *s, buff[1024]; char *s, buff[MAXLINELENGTH];
char name[MAXLEN];
char value[MAXLEN];
FILE *fp = fopen (config_file, "r"); FILE *fp = fopen (config_file, "r");
if (fp == NULL) if (fp == NULL)
@@ -24,9 +28,6 @@ parse_config(const char *config_file, char *cluster_name, int *node,
/* Read next line */ /* Read next line */
while ((s = fgets (buff, sizeof buff, fp)) != NULL) while ((s = fgets (buff, sizeof buff, fp)) != NULL)
{ {
char name[MAXLEN];
char value[MAXLEN];
/* Skip blank lines and comments */ /* Skip blank lines and comments */
if (buff[0] == '\n' || buff[0] == '#') if (buff[0] == '\n' || buff[0] == '#')
continue; continue;
@@ -36,16 +37,16 @@ parse_config(const char *config_file, char *cluster_name, int *node,
/* Copy into correct entry in parameters struct */ /* Copy into correct entry in parameters struct */
if (strcmp(name, "cluster") == 0) if (strcmp(name, "cluster") == 0)
strncpy(cluster_name, value, MAXLEN); strncpy (cluster_name, value, MAXLEN);
else if (strcmp(name, "node") == 0) else if (strcmp(name, "node") == 0)
*node = atoi(value); *node = atoi(value);
else if (strcmp(name, "conninfo") == 0) else if (strcmp(name, "conninfo") == 0)
strncpy(conninfo, value, MAXLEN); strncpy (conninfo, value, MAXLEN);
else else
printf("WARNING: %s/%s: Unknown name/value pair!\n", printf ("WARNING: %s/%s: Unknown name/value pair!\n", name, value);
name, value);
} }
/* Close file */ /* Close file */
fclose (fp); fclose (fp);
} }
@@ -58,12 +59,12 @@ trim (char *s)
/* Trim and delimit right side */ /* Trim and delimit right side */
while ( (isspace (*s2)) && (s2 >= s1) ) while ( (isspace (*s2)) && (s2 >= s1) )
s2--; --s2;
*(s2+1) = '\0'; *(s2+1) = '\0';
/* Trim left side */ /* Trim left side */
while ( (isspace (*s1)) && (s1 < s2) ) while ( (isspace (*s1)) && (s1 < s2) )
s1++; ++s1;
/* Copy finished string */ /* Copy finished string */
strcpy (s, s1); strcpy (s, s1);
@@ -73,14 +74,13 @@ trim (char *s)
void void
parse_line(char *buff, char *name, char *value) parse_line(char *buff, char *name, char *value)
{ {
int i; int i = 0;
int j; int j = 0;
/* /*
* first we find the name of the parameter * first we find the name of the parameter
*/ */
j = 0; for ( ; i < MAXLEN; ++i)
for (i = 0; i < MAXLEN; i++)
{ {
if (buff[i] != '=') if (buff[i] != '=')
name[j++] = buff[i]; name[j++] = buff[i];
@@ -89,12 +89,11 @@ parse_line(char *buff, char *name, char *value)
} }
name[j] = '\0'; name[j] = '\0';
i++;
/* /*
* Now the value * Now the value
*/ */
j = 0; j = 0;
for ( ; i < MAXLEN; i++) for ( ++i ; i < MAXLEN; ++i)
if (buff[i] == '\'') if (buff[i] == '\'')
continue; continue;
else if (buff[i] != '\n') else if (buff[i] != '\n')

View File

@@ -9,21 +9,21 @@
*/ */
#include "repmgr.h" #include "repmgr.h"
#include "strutil.h" #include "strutil.h"
PGconn * PGconn *
establishDBConnection(const char *conninfo, const bool exit_on_error) establishDBConnection(const char *conninfo, const bool exit_on_error)
{ {
PGconn *conn;
/* Make a connection to the database */ /* Make a connection to the database */
conn = PQconnectdb(conninfo); PGconn *conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if ((PQstatus(conn) != CONNECTION_OK)) if ((PQstatus(conn) != CONNECTION_OK))
{ {
fprintf(stderr, "Connection to database failed: %s", fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn)); PQerrorMessage(conn));
if (exit_on_error) if (exit_on_error)
{ {
PQfinish(conn); PQfinish(conn);
@@ -35,7 +35,6 @@ establishDBConnection(const char *conninfo, const bool exit_on_error)
} }
bool bool
is_standby(PGconn *conn) is_standby(PGconn *conn)
{ {
@@ -67,12 +66,9 @@ is_standby(PGconn *conn)
* if 8 or inferior returns an empty string * if 8 or inferior returns an empty string
*/ */
char * char *
pg_version(PGconn *conn) pg_version(PGconn *conn, char* major_version)
{ {
PGresult *res; PGresult *res;
const size_t major_version_sz = 10;
char *major_version;
int major_version1; int major_version1;
char *major_version2; char *major_version2;
@@ -94,13 +90,11 @@ pg_version(PGconn *conn)
major_version1 = atoi(PQgetvalue(res, 0, 0)); major_version1 = atoi(PQgetvalue(res, 0, 0));
major_version2 = PQgetvalue(res, 0, 1); major_version2 = PQgetvalue(res, 0, 1);
major_version = malloc(major_version_sz);
if (major_version1 >= 9) if (major_version1 >= 9)
{ {
/* form a major version string */ /* form a major version string */
xsnprintf(major_version, major_version_sz, "%d.%s", xsnprintf(major_version, MAXVERSIONSTR, "%d.%s", major_version1,
major_version1, major_version2); major_version2);
} }
else else
strcpy(major_version, ""); strcpy(major_version, "");
@@ -145,8 +139,8 @@ const char *
get_cluster_size(PGconn *conn) get_cluster_size(PGconn *conn)
{ {
PGresult *res; PGresult *res;
const char *size; const char *size;
char sqlquery[QUERY_STR_LEN]; char sqlquery[QUERY_STR_LEN];
sqlquery_snprintf( sqlquery_snprintf(
sqlquery, sqlquery,
@@ -178,7 +172,7 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster,
PGresult *res1; PGresult *res1;
PGresult *res2; PGresult *res2;
char sqlquery[QUERY_STR_LEN]; char sqlquery[QUERY_STR_LEN];
char master_conninfo[8192]; char master_conninfo[MAXCONNINFO];
int i; int i;
/* find all nodes belonging to this cluster */ /* find all nodes belonging to this cluster */
@@ -200,7 +194,7 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster,
{ {
/* initialize with the values of the current node being processed */ /* initialize with the values of the current node being processed */
*master_id = atoi(PQgetvalue(res1, i, 0)); *master_id = atoi(PQgetvalue(res1, i, 0));
strcpy(master_conninfo, PQgetvalue(res1, i, 2)); strncpy(master_conninfo, PQgetvalue(res1, i, 2), MAXCONNINFO);
master_conn = establishDBConnection(master_conninfo, false); master_conn = establishDBConnection(master_conninfo, false);
if (PQstatus(master_conn) != CONNECTION_OK) if (PQstatus(master_conn) != CONNECTION_OK)

View File

@@ -6,7 +6,7 @@
PGconn *establishDBConnection(const char *conninfo, const bool exit_on_error); PGconn *establishDBConnection(const char *conninfo, const bool exit_on_error);
bool is_standby(PGconn *conn); bool is_standby(PGconn *conn);
char *pg_version(PGconn *conn); char *pg_version(PGconn *conn, char* major_version);
bool guc_setted(PGconn *conn, const char *parameter, const char *op, bool guc_setted(PGconn *conn, const char *parameter, const char *op,
const char *value); const char *value);
const char *get_cluster_size(PGconn *conn); const char *get_cluster_size(PGconn *conn);

View File

@@ -299,7 +299,7 @@ do_master_register(void)
char conninfo[MAXLEN]; char conninfo[MAXLEN];
bool schema_exists = false; bool schema_exists = false;
const char *master_version = NULL; char master_version[MAXVERSIONSTR];
/* /*
* Read the configuration file: repmgr.conf * Read the configuration file: repmgr.conf
@@ -315,7 +315,7 @@ do_master_register(void)
conn = establishDBConnection(conninfo, true); conn = establishDBConnection(conninfo, true);
/* master should be v9 or better */ /* master should be v9 or better */
master_version = pg_version(conn); pg_version(conn, master_version);
if (strcmp(master_version, "") == 0) if (strcmp(master_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
@@ -484,8 +484,8 @@ do_standby_register(void)
int myLocalId = -1; int myLocalId = -1;
char conninfo[MAXLEN]; char conninfo[MAXLEN];
const char *master_version = NULL; char master_version[MAXVERSIONSTR];
const char *standby_version = NULL; char standby_version[MAXVERSIONSTR];
/* /*
* Read the configuration file: repmgr.conf * Read the configuration file: repmgr.conf
@@ -501,7 +501,7 @@ do_standby_register(void)
conn = establishDBConnection(conninfo, true); conn = establishDBConnection(conninfo, true);
/* should be v9 or better */ /* should be v9 or better */
standby_version = pg_version(conn); pg_version(conn, standby_version);
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
@@ -548,7 +548,7 @@ do_standby_register(void)
return; return;
/* master should be v9 or better */ /* master should be v9 or better */
master_version = pg_version(master_conn); pg_version(master_conn, master_version);
if (strcmp(master_version, "") == 0) if (strcmp(master_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
@@ -626,7 +626,7 @@ do_standby_clone(void)
const char *first_wal_segment = NULL; const char *first_wal_segment = NULL;
const char *last_wal_segment = NULL; const char *last_wal_segment = NULL;
const char *master_version = NULL; char master_version[MAXVERSIONSTR];
/* if dest_dir hasn't been provided, initialize to current directory */ /* if dest_dir hasn't been provided, initialize to current directory */
if (dest_dir == NULL) if (dest_dir == NULL)
@@ -709,7 +709,7 @@ do_standby_clone(void)
} }
/* primary should be v9 or better */ /* primary should be v9 or better */
master_version = pg_version(conn); pg_version(conn, master_version);
if (strcmp(master_version, "") == 0) if (strcmp(master_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
@@ -1028,7 +1028,7 @@ do_standby_promote(void)
char recovery_file_path[MAXLEN]; char recovery_file_path[MAXLEN];
char recovery_done_path[MAXLEN]; char recovery_done_path[MAXLEN];
const char *standby_version = NULL; char standby_version[MAXVERSIONSTR];
/* /*
* Read the configuration file: repmgr.conf * Read the configuration file: repmgr.conf
@@ -1045,7 +1045,7 @@ do_standby_promote(void)
conn = establishDBConnection(conninfo, true); conn = establishDBConnection(conninfo, true);
/* we need v9 or better */ /* we need v9 or better */
standby_version = pg_version(conn); pg_version(conn, standby_version);
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
@@ -1140,8 +1140,8 @@ do_standby_follow(void)
int r; int r;
char data_dir[MAXLEN]; char data_dir[MAXLEN];
const char *master_version = NULL; char master_version[MAXVERSIONSTR];
const char *standby_version = NULL; char standby_version[MAXVERSIONSTR];
/* Read the configuration file: repmgr.conf */ /* Read the configuration file: repmgr.conf */
parse_config(config_file, myClusterName, &myLocalId, conninfo); parse_config(config_file, myClusterName, &myLocalId, conninfo);
@@ -1163,7 +1163,7 @@ do_standby_follow(void)
} }
/* should be v9 or better */ /* should be v9 or better */
standby_version = pg_version(conn); pg_version(conn, standby_version);
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
@@ -1191,7 +1191,7 @@ do_standby_follow(void)
} }
/* should be v9 or better */ /* should be v9 or better */
master_version = pg_version(master_conn); pg_version(master_conn, master_version);
if (strcmp(master_version, "") == 0) if (strcmp(master_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);

View File

@@ -84,7 +84,7 @@ main(int argc, char **argv)
int c; int c;
char conninfo[MAXLEN]; char conninfo[MAXLEN];
const char *standby_version = NULL; char standby_version[MAXVERSIONSTR];
progname = get_progname(argv[0]); progname = get_progname(argv[0]);
@@ -144,7 +144,7 @@ main(int argc, char **argv)
myLocalConn = establishDBConnection(conninfo, true); myLocalConn = establishDBConnection(conninfo, true);
/* should be v9 or better */ /* should be v9 or better */
standby_version = pg_version(myLocalConn); pg_version(myLocalConn, standby_version);
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(myLocalConn); PQfinish(myLocalConn);

View File

@@ -8,8 +8,11 @@
#ifndef _STRUTIL_H_ #ifndef _STRUTIL_H_
#define _STRUTIL_H_ #define _STRUTIL_H_
#define QUERY_STR_LEN 8192 #define QUERY_STR_LEN 8192
#define MAXLEN 1024 #define MAXLEN 1024
#define MAXLINELENGTH 4096
#define MAXVERSIONSTR 16
#define MAXCONNINFO 1024
extern int xsnprintf(char *str, size_t size, const char *format, ...); extern int xsnprintf(char *str, size_t size, const char *format, ...);