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