From 7db474baf6b386261688760c8f32f9bf1c4a549b Mon Sep 17 00:00:00 2001
From: Mike Palmiotto <mike.palmiotto@crunchydata.com>
Date: Wed, 11 Mar 2020 01:07:05 +0000
Subject: [PATCH v2 06/12] Add PgArchiverType to subprocess struct

---
 src/backend/postmaster/pgarch.c     | 87 ++++-------------------------
 src/backend/postmaster/postmaster.c | 14 ++---
 src/backend/postmaster/subprocess.c | 10 ++++
 src/include/postmaster/pgarch.h     |  9 +--
 src/include/postmaster/subprocess.h |  1 +
 5 files changed, 26 insertions(+), 95 deletions(-)

diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 01ffd6513c..9c1761ad94 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -38,10 +38,10 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
-#include "postmaster/fork_process.h"
 #include "postmaster/interrupt.h"
 #include "postmaster/pgarch.h"
 #include "postmaster/postmaster.h"
+#include "postmaster/subprocess.h"
 #include "storage/dsm.h"
 #include "storage/fd.h"
 #include "storage/ipc.h"
@@ -91,11 +91,6 @@ static volatile sig_atomic_t ready_to_stop = false;
  * Local function forward declarations
  * ----------
  */
-#ifdef EXEC_BACKEND
-static pid_t pgarch_forkexec(void);
-#endif
-
-NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]) pg_attribute_noreturn();
 static void pgarch_exit(SIGNAL_ARGS);
 static void pgarch_waken(SIGNAL_ARGS);
 static void pgarch_waken_stop(SIGNAL_ARGS);
@@ -122,10 +117,9 @@ static void pgarch_archiveDone(char *xlog);
  *	Note: if fail, we will be called again from the postmaster main loop.
  */
 int
-pgarch_start(void)
+PgArchiverPrep(int argc, char *argv[])
 {
 	time_t		curtime;
-	pid_t		pgArchPid;
 
 	/*
 	 * Do nothing if no archiver needed
@@ -140,86 +134,20 @@ pgarch_start(void)
 	 * the postmaster main loop, we will get another chance later.
 	 */
 	curtime = time(NULL);
-	if ((unsigned int) (curtime - last_pgarch_start_time) <
+	if ((unsigned int) (curtime - last_pgarch_start_time) >=
 		(unsigned int) PGARCH_RESTART_INTERVAL)
-		return 0;
-	last_pgarch_start_time = curtime;
-
-#ifdef EXEC_BACKEND
-	switch ((pgArchPid = pgarch_forkexec()))
-#else
-	switch ((pgArchPid = fork_process()))
-#endif
-	{
-		case -1:
-			ereport(LOG,
-					(errmsg("could not fork archiver: %m")));
-			return 0;
-
-#ifndef EXEC_BACKEND
-		case 0:
-			/* in postmaster child ... */
-			InitPostmasterChild();
-
-			/* Close the postmaster's sockets */
-			ClosePostmasterPorts(false);
-
-			/* Drop our connection to postmaster's shared memory, as well */
-			dsm_detach_all();
-			PGSharedMemoryDetach();
+		last_pgarch_start_time = curtime;
 
-			PgArchiverMain(0, NULL);
-			break;
-#endif
-
-		default:
-			return (int) pgArchPid;
-	}
-
-	/* shouldn't get here */
 	return 0;
 }
 
-/* ------------------------------------------------------------
- * Local functions called by archiver follow
- * ------------------------------------------------------------
- */
-
-
-#ifdef EXEC_BACKEND
-
-/*
- * pgarch_forkexec() -
- *
- * Format up the arglist for, then fork and exec, archive process
- */
-static pid_t
-pgarch_forkexec(void)
-{
-	char	   *av[10];
-	int			ac = 0;
-
-	av[ac++] = "postgres";
-
-	av[ac++] = "--forkarch";
-
-	av[ac++] = NULL;			/* filled in by postmaster_forkexec */
-
-	av[ac] = NULL;
-	Assert(ac < lengthof(av));
-
-	return postmaster_forkexec(ac, av);
-}
-#endif							/* EXEC_BACKEND */
-
-
 /*
  * PgArchiverMain
  *
  *	The argc/argv parameters are valid only in EXEC_BACKEND case.  However,
  *	since we don't use 'em, it hardly matters...
  */
-NON_EXEC_STATIC void
+void
 PgArchiverMain(int argc, char *argv[])
 {
 	/*
@@ -246,6 +174,11 @@ PgArchiverMain(int argc, char *argv[])
 	exit(0);
 }
 
+/* ------------------------------------------------------------
+ * Local functions called by archiver follow
+ * ------------------------------------------------------------
+ */
+
 /* SIGQUIT signal handler for archiver process */
 static void
 pgarch_exit(SIGNAL_ARGS)
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index e2ad879452..88d010ceb1 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -1785,7 +1785,7 @@ ServerLoop(void)
 
 		/* If we have lost the archiver, try to start a new one. */
 		if (PgArchPID == 0 && PgArchStartupAllowed())
-			PgArchPID = pgarch_start();
+			PgArchPID = StartSubprocess(PgArchiverType);
 
 		/* If we need to signal the autovacuum launcher, do so now */
 		if (avlauncher_needs_signal)
@@ -3055,7 +3055,7 @@ reaper(SIGNAL_ARGS)
 			if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
 				AutoVacPID = StartSubprocess(AutoVacuumLauncherType);
 			if (PgArchStartupAllowed() && PgArchPID == 0)
-				PgArchPID = pgarch_start();
+				PgArchPID = StartSubprocess(PgArchiverType);
 			if (PgStatPID == 0)
 				PgStatPID = StartSubprocess(PgstatCollectorType);
 
@@ -3203,7 +3203,7 @@ reaper(SIGNAL_ARGS)
 				LogChildExit(LOG, _("archiver process"),
 							 pid, exitstatus);
 			if (PgArchStartupAllowed())
-				PgArchPID = pgarch_start();
+				PgArchPID = StartSubprocess(PgArchiverType);
 			continue;
 		}
 
@@ -5063,12 +5063,6 @@ SubPostmasterMain(int argc, char *argv[])
 
 		StartBackgroundWorker();
 	}
-	if (strcmp(argv[1], "--forkarch") == 0)
-	{
-		/* Do not want to attach to shared memory */
-
-		PgArchiverMain(argc, argv); /* does not return */
-	}
 	if (strcmp(argv[1], "--forklog") == 0)
 	{
 		/* Do not want to attach to shared memory */
@@ -5197,7 +5191,7 @@ sigusr1_handler(SIGNAL_ARGS)
 		 */
 		Assert(PgArchPID == 0);
 		if (XLogArchivingAlways())
-			PgArchPID = pgarch_start();
+			PgArchPID = StartSubprocess(PgArchiverType);
 
 		/*
 		 * If we aren't planning to enter hot standby mode later, treat
diff --git a/src/backend/postmaster/subprocess.c b/src/backend/postmaster/subprocess.c
index ee44ef0789..f9d71a627c 100644
--- a/src/backend/postmaster/subprocess.c
+++ b/src/backend/postmaster/subprocess.c
@@ -15,6 +15,7 @@
 #include "pgstat.h"
 #include "postmaster/autovacuum.h"
 #include "postmaster/bgwriter.h"
+#include "postmaster/pgarch.h"
 #include "postmaster/postmaster.h"
 #include "postmaster/startup.h"
 #include "postmaster/subprocess.h"
@@ -114,6 +115,15 @@ static PgSubprocess process_types[] = {
 		.fork_prep = PgstatCollectorPrep,
 		.entrypoint = PgstatCollectorMain,
 		.fork_failure = NULL
+	},
+	{
+		.name = "arch",
+		.desc = "archiver",
+		.needs_aux_proc = false,
+		.needs_shmem = false,
+		.fork_prep = PgArchiverPrep,
+		.entrypoint = PgArchiverMain,
+		.fork_failure = NULL
 	}
 };
 
diff --git a/src/include/postmaster/pgarch.h b/src/include/postmaster/pgarch.h
index b3200874ca..ac527212b1 100644
--- a/src/include/postmaster/pgarch.h
+++ b/src/include/postmaster/pgarch.h
@@ -26,14 +26,7 @@
 #define MAX_XFN_CHARS	40
 #define VALID_XFN_CHARS "0123456789ABCDEF.history.backup.partial"
 
-/* ----------
- * Functions called from postmaster
- * ----------
- */
-extern int	pgarch_start(void);
-
-#ifdef EXEC_BACKEND
+extern int PgArchiverPrep(int argc, char *argv[]);
 extern void PgArchiverMain(int argc, char *argv[]) pg_attribute_noreturn();
-#endif
 
 #endif							/* _PGARCH_H */
diff --git a/src/include/postmaster/subprocess.h b/src/include/postmaster/subprocess.h
index a7ab036b7b..3f00f904dc 100644
--- a/src/include/postmaster/subprocess.h
+++ b/src/include/postmaster/subprocess.h
@@ -26,6 +26,7 @@ typedef enum
 	AutoVacuumLauncherType,
 	AutoVacuumWorkerType,
 	PgstatCollectorType,
+	PgArchiverType,
 
 	NUMSUBPROCESSTYPES			/* Must be last! */
 } SubprocessType;
-- 
2.17.0

