Skip to content

Commit 4bd9de3

Browse files
committed
Fix elog(FATAL) before PostmasterMain() or just after fork().
Since commit 97550c0, these failed with "PANIC: proc_exit() called in child process" due to uninitialized or stale MyProcPid. That was reachable if close() failed in ClosePostmasterPorts() or setlocale(category, "C") failed, both unlikely. Back-patch to v13 (all supported versions). Discussion: https://postgr.es/m/20241208034614.45.nmisch@google.com
1 parent 67ef403 commit 4bd9de3

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

src/backend/main/main.c

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "bootstrap/bootstrap.h"
3434
#include "common/username.h"
35+
#include "miscadmin.h"
3536
#include "port/atomics.h"
3637
#include "postmaster/postmaster.h"
3738
#include "tcop/tcopprot.h"
@@ -96,6 +97,7 @@ main(int argc, char *argv[])
9697
* localization of messages may not work right away, and messages won't go
9798
* anywhere but stderr until GUC settings get loaded.
9899
*/
100+
MyProcPid = getpid();
99101
MemoryContextInit();
100102

101103
/*

src/backend/postmaster/fork_process.c

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <unistd.h>
2020

2121
#include "libpq/pqsignal.h"
22+
#include "miscadmin.h"
2223
#include "postmaster/fork_process.h"
2324

2425
#ifndef WIN32
@@ -66,6 +67,7 @@ fork_process(void)
6667
if (result == 0)
6768
{
6869
/* fork succeeded, in child */
70+
MyProcPid = getpid();
6971
#ifdef LINUX_PROFILE
7072
setitimer(ITIMER_PROF, &prof_itimer, NULL);
7173
#endif

src/backend/postmaster/postmaster.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2024,14 +2024,13 @@ ClosePostmasterPorts(bool am_syslogger)
20242024

20252025

20262026
/*
2027-
* InitProcessGlobals -- set MyProcPid, MyStartTime[stamp], random seeds
2027+
* InitProcessGlobals -- set MyStartTime[stamp], random seeds
20282028
*
20292029
* Called early in the postmaster and every backend.
20302030
*/
20312031
void
20322032
InitProcessGlobals(void)
20332033
{
2034-
MyProcPid = getpid();
20352034
MyStartTimestamp = GetCurrentTimestamp();
20362035
MyStartTime = timestamptz_to_time_t(MyStartTimestamp);
20372036

src/port/pqsignal.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ static volatile pqsigfunc pqsignal_handlers[PG_NSIG];
7474
/*
7575
* Except when called with SIG_IGN or SIG_DFL, pqsignal() sets up this function
7676
* as the handler for all signals. This wrapper handler function checks that
77-
* it is called within a process that the server knows about (i.e., any process
78-
* that has called InitProcessGlobals(), such as a client backend), and not a
77+
* it is called within a process that knew to maintain MyProcPid, and not a
7978
* child process forked by system(3), etc. This check ensures that such child
8079
* processes do not modify shared memory, which is often detrimental. If the
8180
* check succeeds, the function originally provided to pqsignal() is called.

0 commit comments

Comments
 (0)