]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix unportable setvbuf() usage in initdb.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 15 May 2014 19:58:11 +0000 (15:58 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 15 May 2014 19:58:11 +0000 (15:58 -0400)
In yesterday's commit 2dc4f011fd61501cce507be78c39a2677690d44b, I tried
to force buffering of stdout/stderr in initdb to be what it is by
default when the program is run interactively on Unix (since that's how
most manual testing is done).  This tripped over the fact that Windows
doesn't support _IOLBF mode.  We dealt with that a long time ago in
syslogger.c by falling back to unbuffered mode on Windows.  Export that
solution in port.h and use it in initdb.

Back-patch to 8.4, like the previous commit.

src/backend/postmaster/syslogger.c
src/bin/initdb/initdb.c
src/include/port.h

index a731005f96a7aab62203519282236db5b4d4dc8f..1765ca7df9a4e223b074cf02c91247c6007022dd 100644 (file)
 #include "utils/ps_status.h"
 #include "utils/timestamp.h"
 
-/*
- * We really want line-buffered mode for logfile output, but Windows does
- * not have it, and interprets _IOLBF as _IOFBF (bozos).  So use _IONBF
- * instead on Windows.
- */
-#ifdef WIN32
-#define LBF_MODE       _IONBF
-#else
-#define LBF_MODE       _IOLBF
-#endif
-
 /*
  * We read() into a temp buffer twice as big as a chunk, so that any fragment
  * left after processing can be moved down to the front and we'll still have
@@ -561,7 +550,7 @@ SysLogger_Start(void)
                                 (errmsg("could not create log file \"%s\": %m",
                                                 filename))));
 
-       setvbuf(syslogFile, NULL, LBF_MODE, 0);
+       setvbuf(syslogFile, NULL, PG_IOLBF, 0);
 
        pfree(filename);
 
@@ -709,7 +698,7 @@ syslogger_parseArgs(int argc, char *argv[])
        if (fd != -1)
        {
                syslogFile = fdopen(fd, "a");
-               setvbuf(syslogFile, NULL, LBF_MODE, 0);
+               setvbuf(syslogFile, NULL, PG_IOLBF, 0);
        }
 #else                                                  /* WIN32 */
        fd = atoi(*argv++);
@@ -719,7 +708,7 @@ syslogger_parseArgs(int argc, char *argv[])
                if (fd > 0)
                {
                        syslogFile = fdopen(fd, "a");
-                       setvbuf(syslogFile, NULL, LBF_MODE, 0);
+                       setvbuf(syslogFile, NULL, PG_IOLBF, 0);
                }
        }
 #endif   /* WIN32 */
@@ -1062,7 +1051,7 @@ open_csvlogfile(void)
                                 (errmsg("could not create log file \"%s\": %m",
                                                 filename))));
 
-       setvbuf(fh, NULL, LBF_MODE, 0);
+       setvbuf(fh, NULL, PG_IOLBF, 0);
 
 #ifdef WIN32
        _setmode(_fileno(fh), _O_TEXT);         /* use CRLF line endings on Windows */
@@ -1148,7 +1137,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
                        return;
                }
 
-               setvbuf(fh, NULL, LBF_MODE, 0);
+               setvbuf(fh, NULL, PG_IOLBF, 0);
 
 #ifdef WIN32
                _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
@@ -1205,7 +1194,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
                        return;
                }
 
-               setvbuf(fh, NULL, LBF_MODE, 0);
+               setvbuf(fh, NULL, PG_IOLBF, 0);
 
 #ifdef WIN32
                _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
index 47d31373dd16add127e6d02d4047f543e2114aaa..c6af854785e76837025e8f115efb25ca9a75a666 100644 (file)
@@ -2516,7 +2516,7 @@ main(int argc, char *argv[])
         * unexpected output ordering when, eg, output is redirected to a file.
         * POSIX says we must do this before any other usage of these files.
         */
-       setvbuf(stdout, NULL, _IOLBF, 0);
+       setvbuf(stdout, NULL, PG_IOLBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);
 
        progname = get_progname(argv[0]);
index 76231961f161ec9827a19f54d7222876fbd1ddf7..8e57b58fe26855e919813deb8e74345811a31ae6 100644 (file)
@@ -341,6 +341,20 @@ extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
 #define closesocket close
 #endif   /* WIN32 */
 
+/*
+ * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
+ * as _IOFBF.  To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
+ * crashes outright if "parameter validation" is enabled.  Therefore, in
+ * places where we'd like to select line-buffered mode, we fall back to
+ * unbuffered mode instead on Windows.  Always use PG_IOLBF not _IOLBF
+ * directly in order to implement this behavior.
+ */
+#ifndef WIN32
+#define PG_IOLBF       _IOLBF
+#else
+#define PG_IOLBF       _IONBF
+#endif
+
 /*
  * Default "extern" declarations or macro substitutes for library routines.
  * When necessary, these routines are provided by files in src/port/.