Skip to content

Commit 950e64f

Browse files
committed
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 76e38b3. 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 76e38b3 came in
1 parent c290e79 commit 950e64f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/backend/postmaster/syslogger.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ SysLoggerMain(int argc, char *argv[])
205205
* if they fail then presumably the file descriptors are closed and
206206
* any writes will go into the bitbucket anyway.
207207
*/
208-
close(fileno(stdout));
209-
close(fileno(stderr));
208+
close(STDOUT_FILENO);
209+
close(STDERR_FILENO);
210210
if (fd != -1)
211211
{
212-
(void) dup2(fd, fileno(stdout));
213-
(void) dup2(fd, fileno(stderr));
212+
(void) dup2(fd, STDOUT_FILENO);
213+
(void) dup2(fd, STDERR_FILENO);
214214
close(fd);
215215
}
216216
}
@@ -222,7 +222,7 @@ SysLoggerMain(int argc, char *argv[])
222222
*/
223223
#ifdef WIN32
224224
else
225-
_setmode(_fileno(stderr), _O_TEXT);
225+
_setmode(STDERR_FILENO, _O_TEXT);
226226
#endif
227227

228228
/*
@@ -716,12 +716,12 @@ SysLogger_Start(void)
716716

717717
#ifndef WIN32
718718
fflush(stdout);
719-
if (dup2(syslogPipe[1], fileno(stdout)) < 0)
719+
if (dup2(syslogPipe[1], STDOUT_FILENO) < 0)
720720
ereport(FATAL,
721721
(errcode_for_file_access(),
722722
errmsg("could not redirect stdout: %m")));
723723
fflush(stderr);
724-
if (dup2(syslogPipe[1], fileno(stderr)) < 0)
724+
if (dup2(syslogPipe[1], STDERR_FILENO) < 0)
725725
ereport(FATAL,
726726
(errcode_for_file_access(),
727727
errmsg("could not redirect stderr: %m")));
@@ -738,12 +738,12 @@ SysLogger_Start(void)
738738
fflush(stderr);
739739
fd = _open_osfhandle((intptr_t) syslogPipe[1],
740740
_O_APPEND | _O_BINARY);
741-
if (dup2(fd, _fileno(stderr)) < 0)
741+
if (dup2(fd, STDERR_FILENO) < 0)
742742
ereport(FATAL,
743743
(errcode_for_file_access(),
744744
errmsg("could not redirect stderr: %m")));
745745
close(fd);
746-
_setmode(_fileno(stderr), _O_BINARY);
746+
_setmode(STDERR_FILENO, _O_BINARY);
747747

748748
/*
749749
* Now we are done with the write end of the pipe.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy