]> granicus.if.org Git - postgresql/blobdiff - src/backend/postmaster/postmaster.c
Do ClosePostmasterPorts() earlier in SubPostmasterMain().
[postgresql] / src / backend / postmaster / postmaster.c
index 0c0a609735e74fb1356c30539ca3dd96171713e6..2d43506cd0e362fee067fd1c3126fe8a29f6acbc 100644 (file)
@@ -4635,10 +4635,17 @@ SubPostmasterMain(int argc, char *argv[])
        /* Setup essential subsystems (to ensure elog() behaves sanely) */
        InitializeGUCOptions();
 
+       /* Check we got appropriate args */
+       if (argc < 3)
+               elog(FATAL, "invalid subpostmaster invocation");
+
        /* Read in the variables file */
        memset(&port, 0, sizeof(Port));
        read_backend_variables(argv[2], &port);
 
+       /* Close the postmaster's sockets (as soon as we know them) */
+       ClosePostmasterPorts(strcmp(argv[1], "--forklog") == 0);
+
        /*
         * Set reference point for stack-depth checking
         */
@@ -4656,15 +4663,21 @@ SubPostmasterMain(int argc, char *argv[])
                                 errmsg("out of memory")));
 #endif
 
-       /* Check we got appropriate args */
-       if (argc < 3)
-               elog(FATAL, "invalid subpostmaster invocation");
-
        /*
         * If appropriate, physically re-attach to shared memory segment. We want
         * to do this before going any further to ensure that we can attach at the
         * same address the postmaster used.  On the other hand, if we choose not
         * to re-attach, we may have other cleanup to do.
+        *
+        * If testing EXEC_BACKEND on Linux, you should run this as root before
+        * starting the postmaster:
+        *
+        * echo 0 >/proc/sys/kernel/randomize_va_space
+        *
+        * This prevents using randomized stack and code addresses that cause the
+        * child process's memory map to be different from the parent's, making it
+        * sometimes impossible to attach to shared memory at the desired address.
+        * Return the setting to its old value (usually '1' or '2') when finished.
         */
        if (strcmp(argv[1], "--forkbackend") == 0 ||
                strcmp(argv[1], "--forkavlauncher") == 0 ||
@@ -4710,9 +4723,6 @@ SubPostmasterMain(int argc, char *argv[])
        {
                Assert(argc == 3);              /* shouldn't be any more args */
 
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /*
                 * Need to reinitialize the SSL library in the backend, since the
                 * context structures contain function pointers and cannot be passed
@@ -4743,17 +4753,7 @@ SubPostmasterMain(int argc, char *argv[])
                /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
                InitProcess();
 
-               /*
-                * Attach process to shared data structures.  If testing EXEC_BACKEND
-                * on Linux, you must run this as root before starting the postmaster:
-                *
-                * echo 0 >/proc/sys/kernel/randomize_va_space
-                *
-                * This prevents a randomized stack base address that causes child
-                * shared memory to be at a different address than the parent, making
-                * it impossible to attached to shared memory.  Return the value to
-                * '1' when finished.
-                */
+               /* Attach process to shared data structures */
                CreateSharedMemoryAndSemaphores(false, 0);
 
                /* And run the backend */
@@ -4761,9 +4761,6 @@ SubPostmasterMain(int argc, char *argv[])
        }
        if (strcmp(argv[1], "--forkboot") == 0)
        {
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /* Restore basic shared memory pointers */
                InitShmemAccess(UsedShmemSegAddr);
 
@@ -4777,9 +4774,6 @@ SubPostmasterMain(int argc, char *argv[])
        }
        if (strcmp(argv[1], "--forkavlauncher") == 0)
        {
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /* Restore basic shared memory pointers */
                InitShmemAccess(UsedShmemSegAddr);
 
@@ -4793,9 +4787,6 @@ SubPostmasterMain(int argc, char *argv[])
        }
        if (strcmp(argv[1], "--forkavworker") == 0)
        {
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /* Restore basic shared memory pointers */
                InitShmemAccess(UsedShmemSegAddr);
 
@@ -4814,9 +4805,6 @@ SubPostmasterMain(int argc, char *argv[])
                /* do this as early as possible; in particular, before InitProcess() */
                IsBackgroundWorker = true;
 
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /* Restore basic shared memory pointers */
                InitShmemAccess(UsedShmemSegAddr);
 
@@ -4834,27 +4822,18 @@ SubPostmasterMain(int argc, char *argv[])
        }
        if (strcmp(argv[1], "--forkarch") == 0)
        {
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /* Do not want to attach to shared memory */
 
                PgArchiverMain(argc, argv);             /* does not return */
        }
        if (strcmp(argv[1], "--forkcol") == 0)
        {
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(false);
-
                /* Do not want to attach to shared memory */
 
                PgstatCollectorMain(argc, argv);                /* does not return */
        }
        if (strcmp(argv[1], "--forklog") == 0)
        {
-               /* Close the postmaster's sockets */
-               ClosePostmasterPorts(true);
-
                /* Do not want to attach to shared memory */
 
                SysLoggerMain(argc, argv);              /* does not return */