8000 Fix unportable setvbuf() usage in initdb. · larkly/postgres-docker@1dd0b3e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dd0b3e

Browse files
committed
Fix unportable setvbuf() usage in initdb.
In yesterday's commit 2dc4f01, 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.
1 parent 56693cf commit 1dd0b3e

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/backend/postmaster/syslogger.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@
4545
#include "utils/ps_status.h"
4646
#include "utils/timestamp.h"
4747

48-
/*
49-
* We really want line-buffered mode for logfile output, but Windows does
50-
* not have it, and interprets _IOLBF as _IOFBF (bozos). So use _IONBF
51-
* instead on Windows.
52-
*/
53-
#ifdef WIN32
54-
#define LBF_MODE _IONBF
55-
#else
56-
#define LBF_MODE _IOLBF
57-
#endif
58-
5948
/*
6049
* We read() into a temp buffer twice as big as a chunk, so that any fragment
6150
* left after processing can be moved down to the front and we'll still have
@@ -561,7 +550,7 @@ SysLogger_Start(void)
561550
(errmsg("could not create log file \"%s\": %m",
562551
filename))));
563552

564-
setvbuf(syslogFile, NULL, LBF_MODE, 0);
553+
setvbuf(syslogFile, NULL, PG_IOLBF, 0);
565554

566555
pfree(filename);
567556

@@ -709,7 +698,7 @@ syslogger_parseArgs(int argc, char *argv[])
709698
if (fd != -1)
710699
{
711700
syslogFile = fdopen(fd, "a");
712-
setvbuf(syslogFile, NULL, LBF_MODE, 0);
701+
setvbuf(syslogFile, NULL, PG_IOLBF, 0);
713702
}
714703
#else /* WIN32 */
715704
fd = atoi(*argv++);
@@ -719,7 +708,7 @@ syslogger_parseArgs(int argc, char *argv[])
719708
if (fd > 0)
720709
{
721710
syslogFile = fdopen(fd, "a");
722-
setvbuf(syslogFile, NULL, LBF_MODE, 0);
711+
setvbuf(syslogFile, NULL, PG_IOLBF, 0);
723712
}
724713
}
725714
#endif /* WIN32 */
@@ -1062,7 +1051,7 @@ open_csvlogfile(void)
10621051
(errmsg("could not create log file \"%s\": %m",
10631052
filename))));
10641053

1065-
setvbuf(fh, NULL, LBF_MODE, 0);
1054+
setvbuf(fh, NULL, PG_IOLBF, 0);
10661055

10671056
#ifdef WIN32
10681057
_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)
11481137
return;
11491138
}
11501139

1151-
setvbuf(fh, NULL, LBF_MODE, 0);
1140+
setvbuf(fh, NULL, PG_IOLBF, 0);
11521141

11531142
#ifdef WIN32
11541143
_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)
12051194
return;
12061195
}
12071196

1208-
setvbuf(fh, NULL, LBF_MODE, 0);
1197+
setvbuf(fh, NULL, PG_IOLBF, 0);
12091198

12101199
#ifdef WIN32
12111200
_setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */

src/bin/initdb/initdb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ main(int argc, char *argv[])
25162516
* unexpected output ordering when, eg, output is redirected to a file.
25172517
* POSIX says we must do this before any other usage of these files.
25182518
*/
2519-
setvbuf(stdout, NULL, _IOLBF, 0);
2519+
setvbuf(stdout, NULL, PG_IOLBF, 0);
25202520
setvbuf(stderr, NULL, _IONBF, 0);
25212521

25222522
progname = get_progname(argv[0]);

src/include/port.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
341341
#define closesocket close
342342
#endif /* WIN32 */
343343

344+
/*
345+
* On Windows, setvbuf() does not support _IOLBF mode, and interprets that
346+
* as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
347+
* crashes outright if "parameter validation" is enabled. Therefore, in
348+
* places where we'd like to select line-buffered mode, we fall back to
349+
* unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
350+
* directly in order to implement this behavior.
351+
*/
352+
#ifndef WIN32
353+
#define PG_IOLBF _IOLBF
354+
#else
355+
#define PG_IOLBF _IONBF
356+
#endif
357+
344358
/*
345359
* Default "extern" declarations or macro substitutes for library routines.
346360
* When necessary, these routines are provided by files in src/port/.

0 commit comments

Comments
 (0)
0