aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Hagander <magnus@hagander.net>2014-02-09 13:10:14 +0100
committerMagnus Hagander <magnus@hagander.net>2014-02-12 14:51:00 +0100
commit0ae288d2dd7c0b6725feebbcf799a4d65e057d05 (patch)
treed4fb4eb1426d63481e943558d9ded2bf28ad010c
parentdd560510400e4cf545ac92a5d0270dde2e45c5dd (diff)
downloadpostgresql-0ae288d2dd7c0b6725feebbcf799a4d65e057d05.tar.gz
postgresql-0ae288d2dd7c0b6725feebbcf799a4d65e057d05.zip
Kill pg_basebackup background process when exiting
If an error occurs in the foreground (backup) process of pg_basebackup, and we exit in a controlled way, the background process (streaming xlog process) would stay around and keep streaming.
-rw-r--r--src/bin/pg_basebackup/pg_basebackup.c21
-rw-r--r--src/bin/pg_basebackup/pg_receivexlog.c7
-rw-r--r--src/bin/pg_basebackup/streamutil.h7
3 files changed, 28 insertions, 7 deletions
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 32f17629332..4cf7c436e6e 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -72,6 +72,7 @@ static volatile LONG has_xlogendptr = 0;
/* Function headers */
static void usage(void);
+static void disconnect_and_exit(int code);
static void verify_dir_is_empty_or_create(char *dirname);
static void progress_report(int tablespacenum, const char *filename);
@@ -82,6 +83,26 @@ static void BaseBackup(void);
static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
bool segment_finished);
+
+static void disconnect_and_exit(int code)
+{
+ if (conn != NULL)
+ PQfinish(conn);
+
+#ifndef WIN32
+ /*
+ * On windows, our background thread dies along with the process.
+ * But on Unix, if we have started a subprocess, we want to kill
+ * it off so it doesn't remain running trying to stream data.
+ */
+ if (bgchild> 0)
+ kill(bgchild, SIGTERM);
+#endif
+
+ exit(code);
+}
+
+
#ifdef HAVE_LIBZ
static const char *
get_gz_error(gzFile gzf)
diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c
index b7df693f2dd..2d9f6c441b1 100644
--- a/src/bin/pg_basebackup/pg_receivexlog.c
+++ b/src/bin/pg_basebackup/pg_receivexlog.c
@@ -51,6 +51,13 @@ static void StreamLog();
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
usage(void)
{
diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h
index baba5eb04fb..5c9421d55b6 100644
--- a/src/bin/pg_basebackup/streamutil.h
+++ b/src/bin/pg_basebackup/streamutil.h
@@ -9,13 +9,6 @@ extern int dbgetpassword;
/* Connection kept global so we can disconnect easily */
extern PGconn *conn;
-#define disconnect_and_exit(code) \
- { \
- if (conn != NULL) PQfinish(conn); \
- exit(code); \
- }
-
-
char *xstrdup(const char *s);
void *xmalloc0(int size);