aboutsummaryrefslogtreecommitdiff
path: root/src/backend/postmaster/startup.c
diff options
context:
space:
mode:
authorNathan Bossart <nathan@postgresql.org>2023-10-17 10:42:12 -0500
committerNathan Bossart <nathan@postgresql.org>2023-10-17 10:42:12 -0500
commit54fc9dca5b10e82cb85c7cd44ebe1eb0f28f795b (patch)
tree4e5958941f7d8ca90e5945010076389e455be913 /src/backend/postmaster/startup.c
parentf6e1ee3cfa3eb6fbda0e798e103dbad7760f17f5 (diff)
downloadpostgresql-54fc9dca5b10e82cb85c7cd44ebe1eb0f28f795b.tar.gz
postgresql-54fc9dca5b10e82cb85c7cd44ebe1eb0f28f795b.zip
Avoid calling proc_exit() in processes forked by system().
The SIGTERM handler for the startup process immediately calls proc_exit() for the duration of the restore_command, i.e., a call to system(). This system() call forks a new process to execute the shell command, and this child process inherits the parent's signal handlers. If both the parent and child processes receive SIGTERM, both will attempt to call proc_exit(). This can end badly. For example, both processes will try to remove themselves from the PGPROC shared array. To fix this problem, this commit adds a check in StartupProcShutdownHandler() to see whether MyProcPid == getpid(). If they match, this is the parent process, and we can proc_exit() like before. If they do not match, this is a child process, and we just emit a message to STDERR (in a signal safe manner) and _exit(), thereby skipping any problematic exit callbacks. This commit also adds checks in proc_exit(), ProcKill(), and AuxiliaryProcKill() that verify they are not being called within such child processes. Suggested-by: Andres Freund Reviewed-by: Thomas Munro, Andres Freund Discussion: https://postgr.es/m/Y9nGDSgIm83FHcad%40paquier.xyz Discussion: https://postgr.es/m/20230223231503.GA743455%40nathanxps13 Backpatch-through: 11
Diffstat (limited to 'src/backend/postmaster/startup.c')
-rw-r--r--src/backend/postmaster/startup.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c
index 69077bd2075..c3f5e18b9c9 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -19,6 +19,8 @@
*/
#include "postgres.h"
+#include <unistd.h>
+
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -102,7 +104,20 @@ StartupProcShutdownHandler(SIGNAL_ARGS)
int save_errno = errno;
if (in_restore_command)
- proc_exit(1);
+ {
+ /*
+ * 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);
+ }
+ }
else
shutdown_requested = true;
WakeupRecovery();