Commit a4205fa0 authored by Peter Eisentraut's avatar Peter Eisentraut

pg_basebackup: Use atexit()

Instead of using our custom disconnect_and_exit(), just register the
desired cleanup using atexit() and use the standard exit() to leave
the program.
Reviewed-by: default avatarAlvaro Herrera <alvherre@2ndquadrant.com>
Reviewed-by: default avatarMichael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/ec4135ba-84e9-28bf-b584-0e78d47448d5@2ndquadrant.com/
parent afb0d071
This diff is collapsed.
......@@ -55,11 +55,12 @@ static void StreamLog(void);
static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline,
bool segment_finished);
#define disconnect_and_exit(code) \
{ \
if (conn != NULL) PQfinish(conn); \
exit(code); \
}
static void
disconnect_atexit(void)
{
if (conn != NULL)
PQfinish(conn);
}
/* Routines to evaluate segment file format */
#define IsCompressXLogFileName(fname) \
......@@ -168,7 +169,7 @@ get_destination_dir(char *dest_folder)
{
fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
progname, basedir, strerror(errno));
disconnect_and_exit(1);
exit(1);
}
return dir;
......@@ -186,7 +187,7 @@ close_destination_dir(DIR *dest_dir, char *dest_folder)
{
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, dest_folder, strerror(errno));
disconnect_and_exit(1);
exit(1);
}
}
......@@ -267,7 +268,7 @@ FindStreamingStart(uint32 *tli)
{
fprintf(stderr, _("%s: could not stat file \"%s\": %s\n"),
progname, fullpath, strerror(errno));
disconnect_and_exit(1);
exit(1);
}
if (statbuf.st_size != WalSegSz)
......@@ -293,13 +294,13 @@ FindStreamingStart(uint32 *tli)
{
fprintf(stderr, _("%s: could not open compressed file \"%s\": %s\n"),
progname, fullpath, strerror(errno));
disconnect_and_exit(1);
exit(1);
}
if (lseek(fd, (off_t) (-4), SEEK_END) < 0)
{
fprintf(stderr, _("%s: could not seek in compressed file \"%s\": %s\n"),
progname, fullpath, strerror(errno));
disconnect_and_exit(1);
exit(1);
}
r = read(fd, (char *) buf, sizeof(buf));
if (r != sizeof(buf))
......@@ -310,7 +311,7 @@ FindStreamingStart(uint32 *tli)
else
fprintf(stderr, _("%s: could not read compressed file \"%s\": read %d of %zu\n"),
progname, fullpath, r, sizeof(buf));
disconnect_and_exit(1);
exit(1);
}
close(fd);
......@@ -341,7 +342,7 @@ FindStreamingStart(uint32 *tli)
{
fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
progname, basedir, strerror(errno));
disconnect_and_exit(1);
exit(1);
}
close_destination_dir(dir, basedir);
......@@ -395,7 +396,7 @@ StreamLog(void)
* There's no hope of recovering from a version mismatch, so don't
* retry.
*/
disconnect_and_exit(1);
exit(1);
}
/*
......@@ -404,7 +405,7 @@ StreamLog(void)
* existing output directory.
*/
if (!RunIdentifySystem(conn, NULL, &servertli, &serverpos, NULL))
disconnect_and_exit(1);
exit(1);
/*
* Figure out where to start streaming.
......@@ -452,6 +453,7 @@ StreamLog(void)
}
PQfinish(conn);
conn = NULL;
FreeWalDirectoryMethod();
pg_free(stream.walmethod);
......@@ -701,6 +703,7 @@ main(int argc, char **argv)
if (!conn)
/* error message already written in GetConnection() */
exit(1);
atexit(disconnect_atexit);
/*
* Run IDENTIFY_SYSTEM to make sure we've successfully have established a
......@@ -708,7 +711,7 @@ main(int argc, char **argv)
* connection.
*/
if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
disconnect_and_exit(1);
exit(1);
/*
* Set umask so that directories/files are created with the same
......@@ -722,7 +725,7 @@ main(int argc, char **argv)
/* determine remote server's xlog segment size */
if (!RetrieveWalSegSize(conn))
disconnect_and_exit(1);
exit(1);
/*
* Check that there is a database associated with connection, none should
......@@ -733,7 +736,7 @@ main(int argc, char **argv)
fprintf(stderr,
_("%s: replication connection using slot \"%s\" is unexpectedly database specific\n"),
progname, replication_slot);
disconnect_and_exit(1);
exit(1);
}
/*
......@@ -747,8 +750,8 @@ main(int argc, char **argv)
progname, replication_slot);
if (!DropReplicationSlot(conn, replication_slot))
disconnect_and_exit(1);
disconnect_and_exit(0);
exit(1);
exit(0);
}
/* Create a replication slot */
......@@ -761,8 +764,8 @@ main(int argc, char **argv)
if (!CreateReplicationSlot(conn, replication_slot, NULL, false, true, false,
slot_exists_ok))
disconnect_and_exit(1);
disconnect_and_exit(0);
exit(1);
exit(0);
}
/*
......
......@@ -65,7 +65,6 @@ static XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr;
static void usage(void);
static void StreamLogicalLog(void);
static void disconnect_and_exit(int code) pg_attribute_noreturn();
static bool flushAndSendFeedback(PGconn *conn, TimestampTz *now);
static void prepareToTerminate(PGconn *conn, XLogRecPtr endpos,
bool keepalive, XLogRecPtr lsn);
......@@ -167,12 +166,10 @@ sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
}
static void
disconnect_and_exit(int code)
disconnect_atexit(void)
{
if (conn != NULL)
PQfinish(conn);
exit(code);
}
static bool
......@@ -944,20 +941,21 @@ main(int argc, char **argv)
if (!conn)
/* Error message already written in GetConnection() */
exit(1);
atexit(disconnect_atexit);
/*
* Run IDENTIFY_SYSTEM to make sure we connected using a database specific
* replication connection.
*/
if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
disconnect_and_exit(1);
exit(1);
if (db_name == NULL)
{
fprintf(stderr,
_("%s: could not establish database-specific replication connection\n"),
progname);
disconnect_and_exit(1);
exit(1);
}
/*
......@@ -979,7 +977,7 @@ main(int argc, char **argv)
progname, replication_slot);
if (!DropReplicationSlot(conn, replication_slot))
disconnect_and_exit(1);
exit(1);
}
/* Create a replication slot. */
......@@ -992,12 +990,12 @@ main(int argc, char **argv)
if (!CreateReplicationSlot(conn, replication_slot, plugin, false,
false, false, slot_exists_ok))
disconnect_and_exit(1);
exit(1);
startpos = InvalidXLogRecPtr;
}
if (!do_start_slot)
disconnect_and_exit(0);
exit(0);
/* Stream loop */
while (true)
......@@ -1009,7 +1007,7 @@ main(int argc, char **argv)
* We've been Ctrl-C'ed or reached an exit limit condition. That's
* not an error, so exit without an errorcode.
*/
disconnect_and_exit(0);
exit(0);
}
else if (noloop)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment