aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dunstan <andrew@dunslane.net>2007-08-02 23:17:20 +0000
committerAndrew Dunstan <andrew@dunslane.net>2007-08-02 23:17:20 +0000
commit1032186f0c1080a1630b6c4321990b49d86b2b4a (patch)
tree34035063418e409a7c89a900d758393152b48740
parent2bd26c516666b5538a67627d65fc590a6476b044 (diff)
downloadpostgresql-1032186f0c1080a1630b6c4321990b49d86b2b4a.tar.gz
postgresql-1032186f0c1080a1630b6c4321990b49d86b2b4a.zip
Make sure syslogPipe runs in binary mode on Windows to avoid corrupting the pipe chunking protocol. Backport to 8.0
-rw-r--r--src/backend/postmaster/postmaster.c11
-rw-r--r--src/backend/postmaster/syslogger.c25
2 files changed, 32 insertions, 4 deletions
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 42cb240260f..876b1063802 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.505.2.4 2007/07/19 19:14:25 adunstan Exp $
+ * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.505.2.5 2007/08/02 23:17:20 adunstan Exp $
*
* NOTES
*
@@ -3272,6 +3272,15 @@ SubPostmasterMain(int argc, char *argv[])
MyProcPid = getpid(); /* reset MyProcPid */
+ /* make sure stderr is in binary mode before anything can
+ * possibly be written to it, in case it's actually the syslogger pipe,
+ * so the pipe chunking protocol isn't disturbed. Non-logpipe data
+ * gets translated on redirection (e.g. via pg_ctl -l) anyway.
+ */
+#ifdef WIN32
+ _setmode(fileno(stderr),_O_BINARY);
+#endif
+
/* Lose the postmaster's on-exit routines (really a no-op) */
on_exit_reset();
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 110bec9f78e..8c0c6b787ef 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -18,7 +18,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.29.2.2 2007/07/19 19:14:25 adunstan Exp $
+ * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.29.2.3 2007/08/02 23:17:20 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
@@ -195,6 +195,15 @@ SysLoggerMain(int argc, char *argv[])
close(fd);
}
+ /* Syslogger's own stderr can't be the syslogPipe, so set it back to
+ * text mode if we didn't just close it.
+ * (It was set to binary in SubPostmasterMain).
+ */
+#ifdef WIN32
+ else
+ _setmode(_fileno(stderr),_O_TEXT);
+#endif
+
/*
* Also close our copy of the write end of the pipe. This is needed to
* ensure we can detect pipe EOF correctly. (But note that in the restart
@@ -532,14 +541,20 @@ SysLogger_Start(void)
#else
int fd;
+ /*
+ * open the pipe in binary mode and make sure
+ * stderr is binary after it's been dup'ed into, to avoid
+ * disturbing the pipe chunking protocol.
+ */
fflush(stderr);
fd = _open_osfhandle((long) syslogPipe[1],
- _O_APPEND | _O_TEXT);
+ _O_APPEND | _O_BINARY);
if (dup2(fd, _fileno(stderr)) < 0)
ereport(FATAL,
(errcode_for_file_access(),
errmsg("could not redirect stderr: %m")));
close(fd);
+ _setmode(_fileno(stderr),_O_BINARY);
/* Now we are done with the write end of the pipe. */
CloseHandle(syslogPipe[1]);
syslogPipe[1] = 0;
@@ -627,7 +642,7 @@ syslogger_parseArgs(int argc, char *argv[])
fd = atoi(*argv++);
if (fd != 0)
{
- fd = _open_osfhandle(fd, _O_APPEND);
+ fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
if (fd > 0)
{
syslogFile = fdopen(fd, "a");
@@ -1032,6 +1047,10 @@ logfile_rotate(bool time_based_rotation)
setvbuf(fh, NULL, LBF_MODE, 0);
+#ifdef WIN32
+ _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
+#endif
+
/* On Windows, need to interlock against data-transfer thread */
#ifdef WIN32
EnterCriticalSection(&sysfileSection);