@@ -4618,10 +4618,17 @@ SubPostmasterMain(int argc, char *argv[])
4618
4618
/* Setup essential subsystems (to ensure elog() behaves sanely) */
4619
4619
InitializeGUCOptions ();
4620
4620
4621
+ /* Check we got appropriate args */
4622
+ if (argc < 3 )
4623
+ elog (FATAL ,"invalid subpostmaster invocation" );
4624
+
4621
4625
/* Read in the variables file */
4622
4626
memset (& port ,0 ,sizeof (Port ));
4623
4627
read_backend_variables (argv [2 ],& port );
4624
4628
4629
+ /* Close the postmaster's sockets (as soon as we know them) */
4630
+ ClosePostmasterPorts (strcmp (argv [1 ],"--forklog" )== 0 );
4631
+
4625
4632
/*
4626
4633
* Set reference point for stack-depth checking
4627
4634
*/
@@ -4639,15 +4646,21 @@ SubPostmasterMain(int argc, char *argv[])
4639
4646
errmsg ("out of memory" )));
4640
4647
#endif
4641
4648
4642
- /* Check we got appropriate args */
4643
- if (argc < 3 )
4644
- elog (FATAL ,"invalid subpostmaster invocation" );
4645
-
4646
4649
/*
4647
4650
* If appropriate, physically re-attach to shared memory segment. We want
4648
4651
* to do this before going any further to ensure that we can attach at the
4649
4652
* same address the postmaster used. On the other hand, if we choose not
4650
4653
* to re-attach, we may have other cleanup to do.
4654
+ *
4655
+ * If testing EXEC_BACKEND on Linux, you should run this as root before
4656
+ * starting the postmaster:
4657
+ *
4658
+ * echo 0 >/proc/sys/kernel/randomize_va_space
4659
+ *
4660
+ * This prevents using randomized stack and code addresses that cause the
4661
+ * child process's memory map to be different from the parent's, making it
4662
+ * sometimes impossible to attach to shared memory at the desired address.
4663
+ * Return the setting to its old value (usually '1' or '2') when finished.
4651
4664
*/
4652
4665
if (strcmp (argv [1 ],"--forkbackend" )== 0 ||
4653
4666
strcmp (argv [1 ],"--forkavlauncher" )== 0 ||
@@ -4693,9 +4706,6 @@ SubPostmasterMain(int argc, char *argv[])
4693
4706
{
4694
4707
Assert (argc == 3 );/* shouldn't be any more args */
4695
4708
4696
- /* Close the postmaster's sockets */
4697
- ClosePostmasterPorts (false);
4698
-
4699
4709
/*
4700
4710
* Need to reinitialize the SSL library in the backend, since the
4701
4711
* context structures contain function pointers and cannot be passed
@@ -4726,27 +4736,14 @@ SubPostmasterMain(int argc, char *argv[])
4726
4736
/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4727
4737
InitProcess ();
4728
4738
4729
- /*
4730
- * Attach process to shared data structures. If testing EXEC_BACKEND
4731
- * on Linux, you must run this as root before starting the postmaster:
4732
- *
4733
- * echo 0 >/proc/sys/kernel/randomize_va_space
4734
- *
4735
- * This prevents a randomized stack base address that causes child
4736
- * shared memory to be at a different address than the parent, making
4737
- * it impossible to attached to shared memory. Return the value to
4738
- * '1' when finished.
4739
- */
4739
+ /* Attach process to shared data structures */
4740
4740
CreateSharedMemoryAndSemaphores (false,0 );
4741
4741
4742
4742
/* And run the backend */
4743
4743
BackendRun (& port );/* does not return */
4744
4744
}
4745
4745
if (strcmp (argv [1 ],"--forkboot" )== 0 )
4746
4746
{
4747
- /* Close the postmaster's sockets */
4748
- ClosePostmasterPorts (false);
4749
-
4750
4747
/* Restore basic shared memory pointers */
4751
4748
InitShmemAccess (UsedShmemSegAddr );
4752
4749
@@ -4760,9 +4757,6 @@ SubPostmasterMain(int argc, char *argv[])
4760
4757
}
4761
4758
if (strcmp (argv [1 ],"--forkavlauncher" )== 0 )
4762
4759
{
4763
- /* Close the postmaster's sockets */
4764
- ClosePostmasterPorts (false);
4765
-
4766
4760
/* Restore basic shared memory pointers */
4767
4761
InitShmemAccess (UsedShmemSegAddr );
4768
4762
@@ -4776,9 +4770,6 @@ SubPostmasterMain(int argc, char *argv[])
4776
4770
}
4777
4771
if (strcmp (argv [1 ],"--forkavworker" )== 0 )
4778
4772
{
4779
- /* Close the postmaster's sockets */
4780
- ClosePostmasterPorts (false);
4781
-
4782
4773
/* Restore basic shared memory pointers */
4783
4774
InitShmemAccess (UsedShmemSegAddr );
4784
4775
@@ -4797,9 +4788,6 @@ SubPostmasterMain(int argc, char *argv[])
4797
4788
/* do this as early as possible; in particular, before InitProcess() */
4798
4789
IsBackgroundWorker = true;
4799
4790
4800
- /* Close the postmaster's sockets */
4801
- ClosePostmasterPorts (false);
4802
-
4803
4791
/* Restore basic shared memory pointers */
4804
4792
InitShmemAccess (UsedShmemSegAddr );
4805
4793
@@ -4817,27 +4805,18 @@ SubPostmasterMain(int argc, char *argv[])
4817
4805
}
4818
4806
if (strcmp (argv [1 ],"--forkarch" )== 0 )
4819
4807
{
4820
- /* Close the postmaster's sockets */
4821
- ClosePostmasterPorts (false);
4822
-
4823
4808
/* Do not want to attach to shared memory */
4824
4809
4825
4810
PgArchiverMain (argc ,argv );/* does not return */
4826
4811
}
4827
4812
if (strcmp (argv [1 ],"--forkcol" )== 0 )
4828
4813
{
4829
- /* Close the postmaster's sockets */
4830
- ClosePostmasterPorts (false);
4831
-
4832
4814
/* Do not want to attach to shared memory */
4833
4815
4834
4816
PgstatCollectorMain (argc ,argv );/* does not return */
4835
4817
}
4836
4818
if (strcmp (argv [1 ],"--forklog" )== 0 )
4837
4819
{
4838
- /* Close the postmaster's sockets */
4839
- ClosePostmasterPorts (true);
4840
-
4841
4820
/* Do not want to attach to shared memory */
4842
4821
4843
4822
SysLoggerMain (argc ,argv );/* does not return */