postgres.git / summary / log / commit / refs

commit    c91a216ef7385e78e7eb80355c4d02953711bdba
Author:   Andres Freund <andres@anarazel.de>
Date:     Tue Jul 19 00:06:34 2022 +0000
First released in: 16.0, 15.0

    Use STDOUT/STDERR_FILENO in most of syslogger.
    
    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 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c index 25e2131e311..d6d02e3c636 100644 --- a/src/backend/postmaster/syslogger.c +++ b/src/backend/postmaster/syslogger.c @@ -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. [parent: 92f478657c55]