From 0858c2eed348bf4c2d1ae0c7131cafc438afb461 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <nathan@postgresql.org>
Date: Fri, 17 Nov 2023 22:09:24 -0600
Subject: [PATCH v1 3/3] Revert "Avoid calling proc_exit() in processes forked
 by system()."

Thanks to commit XXXXXXXXXX, this check in the SIGTERM handler for
the startup process is now obsolete and can be removed.  Instead of
leaving around the elog(PANIC, ...) calls that are now unlikely to
be triggered and the dead function write_stderr_signal_safe(), I've
opted to just remove them for now.  Thanks to modern version
control software, it will be trivial to dig those up if they are
ever needed in the future.

This reverts commit 97550c0711972a9856b5db751539bbaf2f88884c.

Reviewed-by: ???
Discussion: ???
---
 src/backend/postmaster/startup.c | 17 +----------------
 src/backend/storage/ipc/ipc.c    |  4 ----
 src/backend/storage/lmgr/proc.c  |  8 --------
 src/backend/utils/error/elog.c   | 28 ----------------------------
 src/include/utils/elog.h         |  6 ------
 5 files changed, 1 insertion(+), 62 deletions(-)

diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c
index 83dbed86b9..f40acd20ff 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -19,8 +19,6 @@
  */
 #include "postgres.h"
 
-#include <unistd.h>
-
 #include "access/xlog.h"
 #include "access/xlogrecovery.h"
 #include "access/xlogutils.h"
@@ -113,20 +111,7 @@ static void
 StartupProcShutdownHandler(SIGNAL_ARGS)
 {
 	if (in_restore_command)
-	{
-		/*
-		 * If we are in a child process (e.g., forked by system() in
-		 * RestoreArchivedFile()), we don't want to call any exit callbacks.
-		 * The parent will take care of that.
-		 */
-		if (MyProcPid == (int) getpid())
-			proc_exit(1);
-		else
-		{
-			write_stderr_signal_safe("StartupProcShutdownHandler() called in child process\n");
-			_exit(1);
-		}
-	}
+		proc_exit(1);
 	else
 		shutdown_requested = true;
 	WakeupRecovery();
diff --git a/src/backend/storage/ipc/ipc.c b/src/backend/storage/ipc/ipc.c
index 6591b5d6a8..1904d21795 100644
--- a/src/backend/storage/ipc/ipc.c
+++ b/src/backend/storage/ipc/ipc.c
@@ -103,10 +103,6 @@ static int	on_proc_exit_index,
 void
 proc_exit(int code)
 {
-	/* not safe if forked by system(), etc. */
-	if (MyProcPid != (int) getpid())
-		elog(PANIC, "proc_exit() called in child process");
-
 	/* Clean up everything that must be cleaned up */
 	proc_exit_prepare(code);
 
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index e9e445bb21..5b663a2997 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -806,10 +806,6 @@ ProcKill(int code, Datum arg)
 
 	Assert(MyProc != NULL);
 
-	/* not safe if forked by system(), etc. */
-	if (MyProc->pid != (int) getpid())
-		elog(PANIC, "ProcKill() called in child process");
-
 	/* Make sure we're out of the sync rep lists */
 	SyncRepCleanupAtProcExit();
 
@@ -930,10 +926,6 @@ AuxiliaryProcKill(int code, Datum arg)
 
 	Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
 
-	/* not safe if forked by system(), etc. */
-	if (MyProc->pid != (int) getpid())
-		elog(PANIC, "AuxiliaryProcKill() called in child process");
-
 	auxproc = &AuxiliaryProcs[proctype];
 
 	Assert(MyProc == auxproc);
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 6aeb855e49..ba9179da85 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -3733,34 +3733,6 @@ write_stderr(const char *fmt,...)
 }
 
 
-/*
- * Write a message to STDERR using only async-signal-safe functions.  This can
- * be used to safely emit a message from a signal handler.
- *
- * TODO: It is likely possible to safely do a limited amount of string
- * interpolation (e.g., %s and %d), but that is not presently supported.
- */
-void
-write_stderr_signal_safe(const char *str)
-{
-	int			nwritten = 0;
-	int			ntotal = strlen(str);
-
-	while (nwritten < ntotal)
-	{
-		int			rc;
-
-		rc = write(STDERR_FILENO, str + nwritten, ntotal - nwritten);
-
-		/* Just give up on error.  There isn't much else we can do. */
-		if (rc == -1)
-			return;
-
-		nwritten += rc;
-	}
-}
-
-
 /*
  * Adjust the level of a recovery-related message per trace_recovery_messages.
  *
diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h
index 0971d5ce33..b25ea65dff 100644
--- a/src/include/utils/elog.h
+++ b/src/include/utils/elog.h
@@ -536,10 +536,4 @@ extern void write_jsonlog(ErrorData *edata);
  */
 extern void write_stderr(const char *fmt,...) pg_attribute_printf(1, 2);
 
-/*
- * Write a message to STDERR using only async-signal-safe functions.  This can
- * be used to safely emit a message from a signal handler.
- */
-extern void write_stderr_signal_safe(const char *fmt);
-
 #endif							/* ELOG_H */
-- 
2.25.1

