Use STDOUT/STDERR_FILENO in most of syslogger.
authorAndres Freund <andres@anarazel.de>
Tue, 19 Jul 2022 00:06:34 +0000 (17:06 -0700)
committerAndres Freund <andres@anarazel.de>
Tue, 19 Jul 2022 00:22:11 +0000 (17:22 -0700)
This fixes problems on windows when logging collector is used in a service,
failing with:
FATAL:  could not redirect stderr: Bad file descriptor

This is triggered by 76e38b37a5. The problem is that STDOUT/STDERR_FILENO
aren't defined on windows, which lead us to use _fileno(stdout) etc, but that
doesn't work if stdout/stderr are closed.

Author: Andres Freund <andres@anarazel.de>
Reported-By: Sandeep Thakkar <sandeep.thakkar@enterprisedb.com>
Message-Id: 20220520164558.ozb7lm6unakqzezi@alap3.anarazel.de (on pgsql-packagers)
Backpatch: 15-, where 76e38b37a5 came in

src/backend/postmaster/syslogger.c

index 25e2131e31180b8e6a573b31399692a08e780103..d6d02e3c6363514209ac7f3151dc064080252e46 100644 (file)
@@ -205,12 +205,12 @@ SysLoggerMain(int argc, char *argv[])
         * if they fail then presumably the file descriptors are closed and
         * any writes will go into the bitbucket anyway.
         */
-       close(fileno(stdout));
-       close(fileno(stderr));
+       close(STDOUT_FILENO);
+       close(STDERR_FILENO);
        if (fd != -1)
        {
-           (void) dup2(fd, fileno(stdout));
-           (void) dup2(fd, fileno(stderr));
+           (void) dup2(fd, STDOUT_FILENO);
+           (void) dup2(fd, STDERR_FILENO);
            close(fd);
        }
    }
@@ -222,7 +222,7 @@ SysLoggerMain(int argc, char *argv[])
     */
 #ifdef WIN32
    else
-       _setmode(_fileno(stderr), _O_TEXT);
+       _setmode(STDERR_FILENO, _O_TEXT);
 #endif
 
    /*
@@ -716,12 +716,12 @@ SysLogger_Start(void)
 
 #ifndef WIN32
                fflush(stdout);
-               if (dup2(syslogPipe[1], fileno(stdout)) < 0)
+               if (dup2(syslogPipe[1], STDOUT_FILENO) < 0)
                    ereport(FATAL,
                            (errcode_for_file_access(),
                             errmsg("could not redirect stdout: %m")));
                fflush(stderr);
-               if (dup2(syslogPipe[1], fileno(stderr)) < 0)
+               if (dup2(syslogPipe[1], STDERR_FILENO) < 0)
                    ereport(FATAL,
                            (errcode_for_file_access(),
                             errmsg("could not redirect stderr: %m")));
@@ -738,12 +738,12 @@ SysLogger_Start(void)
                fflush(stderr);
                fd = _open_osfhandle((intptr_t) syslogPipe[1],
                                     _O_APPEND | _O_BINARY);
-               if (dup2(fd, _fileno(stderr)) < 0)
+               if (dup2(fd, STDERR_FILENO) < 0)
                    ereport(FATAL,
                            (errcode_for_file_access(),
                             errmsg("could not redirect stderr: %m")));
                close(fd);
-               _setmode(_fileno(stderr), _O_BINARY);
+               _setmode(STDERR_FILENO, _O_BINARY);
 
                /*
                 * Now we are done with the write end of the pipe.