}
 
 #ifndef WIN32
+       /* Don't give the listen socket to any subprograms we execute. */
+       if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
+           elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
 
        /*
         * Without the SO_REUSEADDR flag, a new postmaster can't be started
 void
 StreamClose(pgsocket sock)
 {
-   closesocket(sock);
+   if (closesocket(sock) != 0)
+       elog(LOG, "could not close client or listen socket: %m");
 }
 
 /*
 
    Port        port;
    InheritableSocket portsocket;
    char        DataDir[MAXPGPATH];
-   pgsocket    ListenSocket[MAXLISTEN];
    int32       MyCancelKey;
    int         MyPMChildSlot;
 #ifndef WIN32
 void
 ClosePostmasterPorts(bool am_syslogger)
 {
-   int         i;
-
    /* Release resources held by the postmaster's WaitEventSet. */
    if (pm_wait_set)
    {
    /*
     * Close the postmaster's listen sockets.  These aren't tracked by fd.c,
     * so we don't call ReleaseExternalFD() here.
+    *
+    * The listen sockets are marked as FD_CLOEXEC, so this isn't needed in
+    * EXEC_BACKEND mode.
     */
-   for (i = 0; i < MAXLISTEN; i++)
+#ifndef EXEC_BACKEND
+   for (int i = 0; i < MAXLISTEN; i++)
    {
        if (ListenSocket[i] != PGINVALID_SOCKET)
        {
            ListenSocket[i] = PGINVALID_SOCKET;
        }
    }
+#endif
 
    /*
     * If using syslogger, close the read side of the pipe.  We don't bother
 
    strlcpy(param->DataDir, DataDir, MAXPGPATH);
 
-   memcpy(¶m->ListenSocket, &ListenSocket, sizeof(ListenSocket));
-
    param->MyCancelKey = MyCancelKey;
    param->MyPMChildSlot = MyPMChildSlot;
 
 
    SetDataDir(param->DataDir);
 
-   memcpy(&ListenSocket, ¶m->ListenSocket, sizeof(ListenSocket));
-
    MyCancelKey = param->MyCancelKey;
    MyPMChildSlot = param->MyPMChildSlot;