Skip to content

Commit 31c4531

Browse files
committed
Commonalize process startup code.
Move common code, that was duplicated in every postmaster child/every standalone process, into two functions in miscinit.c. Not only does that already result in a fair amount of net code reduction but it also makes it much easier to remove more duplication in the future. The prime motivation wasn't code deduplication though, but easier addition of new common code.
1 parent 2be82dc commit 31c4531

File tree

15 files changed

+96
-274
lines changed

15 files changed

+96
-274
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
#include "postgres.h"
1616

17-
#include <time.h>
1817
#include <unistd.h>
1918
#include <signal.h>
2019

@@ -191,19 +190,11 @@ AuxiliaryProcessMain(int argc, char *argv[])
191190
char *userDoption = NULL;
192191

193192
/*
194-
* initialize globals
193+
* Initialize process environment (already done if under postmaster, but
194+
* not if standalone).
195195
*/
196-
MyProcPid = getpid();
197-
198-
MyStartTime = time(NULL);
199-
200-
/* Compute paths, if we didn't inherit them from postmaster */
201-
if (my_exec_path[0] == '\0')
202-
{
203-
if (find_my_exec(progname, my_exec_path) < 0)
204-
elog(FATAL, "%s: could not locate my own executable path",
205-
progname);
206-
}
196+
if (!IsUnderPostmaster)
197+
InitStandaloneProcess(argv[0]);
207198

208199
/*
209200
* process command arguments
@@ -515,15 +506,6 @@ bootstrap_signals(void)
515506
{
516507
if (IsUnderPostmaster)
517508
{
518-
/*
519-
* If possible, make this process a group leader, so that the
520-
* postmaster can signal any child processes too.
521-
*/
522-
#ifdef HAVE_SETSID
523-
if (setsid() < 0)
524-
elog(FATAL, "setsid() failed: %m");
525-
#endif
526-
527509
/*
528510
* Properly accept or ignore signals the postmaster might send us
529511
*/

src/backend/postmaster/autovacuum.c

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
#include <signal.h>
6565
#include <sys/types.h>
6666
#include <sys/time.h>
67-
#include <time.h>
6867
#include <unistd.h>
6968

7069
#include "access/heapam.h"
@@ -383,12 +382,11 @@ StartAutoVacLauncher(void)
383382
#ifndef EXEC_BACKEND
384383
case 0:
385384
/* in postmaster child ... */
385+
InitPostmasterChild();
386+
386387
/* Close the postmaster's sockets */
387388
ClosePostmasterPorts(false);
388389

389-
/* Lose the postmaster's on-exit routines */
390-
on_exit_reset();
391-
392390
AutoVacLauncherMain(0, NULL);
393391
break;
394392
#endif
@@ -408,16 +406,8 @@ AutoVacLauncherMain(int argc, char *argv[])
408406
{
409407
sigjmp_buf local_sigjmp_buf;
410408

411-
/* we are a postmaster subprocess now */
412-
IsUnderPostmaster = true;
413409
am_autovacuum_launcher = true;
414410

415-
/* reset MyProcPid */
416-
MyProcPid = getpid();
417-
418-
/* record Start Time for logging */
419-
MyStartTime = time(NULL);
420-
421411
/* Identify myself via ps */
422412
init_ps_display("autovacuum launcher process", "", "", "");
423413

@@ -429,17 +419,6 @@ AutoVacLauncherMain(int argc, char *argv[])
429419

430420
SetProcessingMode(InitProcessing);
431421

432-
/*
433-
* If possible, make this process a group leader, so that the postmaster
434-
* can signal any child processes too. (autovacuum probably never has any
435-
* child processes, but for consistency we make all postmaster child
436-
* processes do this.)
437-
*/
438-
#ifdef HAVE_SETSID
439-
if (setsid() < 0)
440-
elog(FATAL, "setsid() failed: %m");
441-
#endif
442-
443422
/*
444423
* Set up signal handlers. We operate on databases much like a regular
445424
* backend, so we use the same signal handling. See equivalent code in
@@ -1455,12 +1434,11 @@ StartAutoVacWorker(void)
14551434
#ifndef EXEC_BACKEND
14561435
case 0:
14571436
/* in postmaster child ... */
1437+
InitPostmasterChild();
1438+
14581439
/* Close the postmaster's sockets */
14591440
ClosePostmasterPorts(false);
14601441

1461-
/* Lose the postmaster's on-exit routines */
1462-
on_exit_reset();
1463-
14641442
AutoVacWorkerMain(0, NULL);
14651443
break;
14661444
#endif
@@ -1481,32 +1459,13 @@ AutoVacWorkerMain(int argc, char *argv[])
14811459
sigjmp_buf local_sigjmp_buf;
14821460
Oid dbid;
14831461

1484-
/* we are a postmaster subprocess now */
1485-
IsUnderPostmaster = true;
14861462
am_autovacuum_worker = true;
14871463

1488-
/* reset MyProcPid */
1489-
MyProcPid = getpid();
1490-
1491-
/* record Start Time for logging */
1492-
MyStartTime = time(NULL);
1493-
14941464
/* Identify myself via ps */
14951465
init_ps_display("autovacuum worker process", "", "", "");
14961466

14971467
SetProcessingMode(InitProcessing);
14981468

1499-
/*
1500-
* If possible, make this process a group leader, so that the postmaster
1501-
* can signal any child processes too. (autovacuum probably never has any
1502-
* child processes, but for consistency we make all postmaster child
1503-
* processes do this.)
1504-
*/
1505-
#ifdef HAVE_SETSID
1506-
if (setsid() < 0)
1507-
elog(FATAL, "setsid() failed: %m");
1508-
#endif
1509-
15101469
/*
15111470
* Set up signal handlers. We operate on databases much like a regular
15121471
* backend, so we use the same signal handling. See equivalent code in

src/backend/postmaster/bgworker.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "postgres.h"
1414

1515
#include <unistd.h>
16-
#include <time.h>
1716

1817
#include "miscadmin.h"
1918
#include "libpq/pqsignal.h"
@@ -556,16 +555,8 @@ StartBackgroundWorker(void)
556555
if (worker == NULL)
557556
elog(FATAL, "unable to find bgworker entry");
558557

559-
/* we are a postmaster subprocess now */
560-
IsUnderPostmaster = true;
561558
IsBackgroundWorker = true;
562559

563-
/* reset MyProcPid */
564-
MyProcPid = getpid();
565-
566-
/* record Start Time for logging */
567-
MyStartTime = time(NULL);
568-
569560
/* Identify myself via ps */
570561
snprintf(buf, MAXPGPATH, "bgworker: %s", worker->bgw_name);
571562
init_ps_display(buf, "", "", "");
@@ -590,15 +581,6 @@ StartBackgroundWorker(void)
590581
if (PostAuthDelay > 0)
591582
pg_usleep(PostAuthDelay * 1000000L);
592583

593-
/*
594-
* If possible, make this process a group leader, so that the postmaster
595-
* can signal any child processes too.
596-
*/
597-
#ifdef HAVE_SETSID
598-
if (setsid() < 0)
599-
elog(FATAL, "setsid() failed: %m");
600-
#endif
601-
602584
/*
603585
* Set up signal handlers.
604586
*/

src/backend/postmaster/bgwriter.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
#include <signal.h>
3838
#include <sys/time.h>
39-
#include <time.h>
4039
#include <unistd.h>
4140

4241
#include "access/xlog.h"
@@ -113,17 +112,6 @@ BackgroundWriterMain(void)
113112
MemoryContext bgwriter_context;
114113
bool prev_hibernate;
115114

116-
/*
117-
* If possible, make this process a group leader, so that the postmaster
118-
* can signal any child processes too. (bgwriter probably never has any
119-
* child processes, but for consistency we make all postmaster child
120-
* processes do this.)
121-
*/
122-
#ifdef HAVE_SETSID
123-
if (setsid() < 0)
124-
elog(FATAL, "setsid() failed: %m");
125-
#endif
126-
127115
/*
128116
* Properly accept or ignore signals the postmaster might send us.
129117
*

src/backend/postmaster/checkpointer.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,6 @@ CheckpointerMain(void)
196196

197197
CheckpointerShmem->checkpointer_pid = MyProcPid;
198198

199-
/*
200-
* If possible, make this process a group leader, so that the postmaster
201-
* can signal any child processes too. (checkpointer probably never has
202-
* any child processes, but for consistency we make all postmaster child
203-
* processes do this.)
204-
*/
205-
#ifdef HAVE_SETSID
206-
if (setsid() < 0)
207-
elog(FATAL, "setsid() failed: %m");
208-
#endif
209-
210199
/*
211200
* Properly accept or ignore signals the postmaster might send us
212201
*

src/backend/postmaster/pgarch.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,11 @@ pgarch_start(void)
157157
#ifndef EXEC_BACKEND
158158
case 0:
159159
/* in postmaster child ... */
160+
InitPostmasterChild();
161+
160162
/* Close the postmaster's sockets */
161163
ClosePostmasterPorts(false);
162164

163-
/* Lose the postmaster's on-exit routines */
164-
on_exit_reset();
165-
166165
/* Drop our connection to postmaster's shared memory, as well */
167166
dsm_detach_all();
168167
PGSharedMemoryDetach();
@@ -221,21 +220,6 @@ pgarch_forkexec(void)
221220
NON_EXEC_STATIC void
222221
PgArchiverMain(int argc, char *argv[])
223222
{
224-
IsUnderPostmaster = true; /* we are a postmaster subprocess now */
225-
226-
MyProcPid = getpid(); /* reset MyProcPid */
227-
228-
MyStartTime = time(NULL); /* record Start Time for logging */
229-
230-
/*
231-
* If possible, make this process a group leader, so that the postmaster
232-
* can signal any child processes too.
233-
*/
234-
#ifdef HAVE_SETSID
235-
if (setsid() < 0)
236-
elog(FATAL, "setsid() failed: %m");
237-
#endif
238-
239223
InitializeLatchSupport(); /* needed for latch waits */
240224

241225
InitLatch(&mainloop_latch); /* initialize latch used in main loop */

src/backend/postmaster/pgstat.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,11 @@ pgstat_start(void)
695695
#ifndef EXEC_BACKEND
696696
case 0:
697697
/* in postmaster child ... */
698+
InitPostmasterChild();
699+
698700
/* Close the postmaster's sockets */
699701
ClosePostmasterPorts(false);
700702

701-
/* Lose the postmaster's on-exit routines */
702-
on_exit_reset();
703-
704703
/* Drop our connection to postmaster's shared memory, as well */
705704
dsm_detach_all();
706705
PGSharedMemoryDetach();
@@ -3152,23 +3151,6 @@ PgstatCollectorMain(int argc, char *argv[])
31523151
PgStat_Msg msg;
31533152
int wr;
31543153

3155-
IsUnderPostmaster = true; /* we are a postmaster subprocess now */
3156-
3157-
MyProcPid = getpid(); /* reset MyProcPid */
3158-
3159-
MyStartTime = time(NULL); /* record Start Time for logging */
3160-
3161-
/*
3162-
* If possible, make this process a group leader, so that the postmaster
3163-
* can signal any child processes too. (pgstat probably never has any
3164-
* child processes, but for consistency we make all postmaster child
3165-
* processes do this.)
3166-
*/
3167-
#ifdef HAVE_SETSID
3168-
if (setsid() < 0)
3169-
elog(FATAL, "setsid() failed: %m");
3170-
#endif
3171-
31723154
InitializeLatchSupport(); /* needed for latch waits */
31733155

31743156
/* Initialize private latch for use by signal handlers */

0 commit comments

Comments
 (0)