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
This commit is contained in:
Andres Freund 2022-07-18 17:06:34 -07:00
parent c290e79cf0
commit 950e64fa46
1 changed files with 9 additions and 9 deletions

View 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.