Skip to content

Commit b349034

Browse files
committed
Make sure syslogPipe runs in binary mode on Windows to avoid corrupting the pipe chunking protocol. Backport to 8.0
1 parent 494d6f8 commit b349034

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.535 2007/07/24 04:54:09 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.536 2007/08/02 23:15:26 adunstan Exp $
4141
*
4242
* NOTES
4343
*
@@ -3385,6 +3385,15 @@ SubPostmasterMain(int argc, char *argv[])
33853385

33863386
MyProcPid = getpid(); /* reset MyProcPid */
33873387

3388+
/* make sure stderr is in binary mode before anything can
3389+
* possibly be written to it, in case it's actually the syslogger pipe,
3390+
* so the pipe chunking protocol isn't disturbed. Non-logpipe data
3391+
* gets translated on redirection (e.g. via pg_ctl -l) anyway.
3392+
*/
3393+
#ifdef WIN32
3394+
_setmode(fileno(stderr),_O_BINARY);
3395+
#endif
3396+
33883397
/* Lose the postmaster's on-exit routines (really a no-op) */
33893398
on_exit_reset();
33903399

src/backend/postmaster/syslogger.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.33 2007/07/19 19:13:43 adunstan Exp $
21+
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.34 2007/08/02 23:15:27 adunstan Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -194,6 +194,15 @@ SysLoggerMain(int argc, char *argv[])
194194
close(fd);
195195
}
196196

197+
/* Syslogger's own stderr can't be the syslogPipe, so set it back to
198+
* text mode if we didn't just close it.
199+
* (It was set to binary in SubPostmasterMain).
200+
*/
201+
#ifdef WIN32
202+
else
203+
_setmode(_fileno(stderr),_O_TEXT);
204+
#endif
205+
197206
/*
198207
* Also close our copy of the write end of the pipe. This is needed to
199208
* ensure we can detect pipe EOF correctly. (But note that in the restart
@@ -531,14 +540,20 @@ SysLogger_Start(void)
531540
#else
532541
int fd;
533542

543+
/*
544+
* open the pipe in binary mode and make sure
545+
* stderr is binary after it's been dup'ed into, to avoid
546+
* disturbing the pipe chunking protocol.
547+
*/
534548
fflush(stderr);
535549
fd = _open_osfhandle((long) syslogPipe[1],
536-
_O_APPEND | _O_TEXT);
550+
_O_APPEND | _O_BINARY);
537551
if (dup2(fd, _fileno(stderr)) < 0)
538552
ereport(FATAL,
539553
(errcode_for_file_access(),
540554
errmsg("could not redirect stderr: %m")));
541555
close(fd);
556+
_setmode(_fileno(stderr),_O_BINARY);
542557
/* Now we are done with the write end of the pipe. */
543558
CloseHandle(syslogPipe[1]);
544559
syslogPipe[1] = 0;
@@ -626,7 +641,7 @@ syslogger_parseArgs(int argc, char *argv[])
626641
fd = atoi(*argv++);
627642
if (fd != 0)
628643
{
629-
fd = _open_osfhandle(fd, _O_APPEND);
644+
fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
630645
if (fd > 0)
631646
{
632647
syslogFile = fdopen(fd, "a");
@@ -988,6 +1003,10 @@ logfile_rotate(bool time_based_rotation)
9881003

9891004
setvbuf(fh, NULL, LBF_MODE, 0);
9901005

1006+
#ifdef WIN32
1007+
_setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
1008+
#endif
1009+
9911010
/* On Windows, need to interlock against data-transfer thread */
9921011
#ifdef WIN32
9931012
EnterCriticalSection(&sysfileSection);

0 commit comments

Comments
 (0)