Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8ae7278

Browse files
committed
Fix several small Windows compatibility issues, per Andreas.
1 parent 7887726 commit 8ae7278

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/backend/postmaster/syslogger.c

Lines changed: 38 additions & 13 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.1 2004/08/05 23:32:10 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.2 2004/08/06 16:00:51 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -41,6 +41,18 @@
4141
#include "utils/ps_status.h"
4242

4343

44+
/*
45+
* We really want line-buffered mode for logfile output, but Windows does
46+
* not have it, and interprets _IOLBF as _IOFBF (bozos). So use _IONBF
47+
* instead on Windows.
48+
*/
49+
#ifdef WIN32
50+
#define LBF_MODE _IONBF
51+
#else
52+
#define LBF_MODE _IOLBF
53+
#endif
54+
55+
4456
/*
4557
* GUC parameters. Redirect_stderr cannot be changed after postmaster
4658
* start, but the rest can change at SIGHUP.
@@ -132,11 +144,19 @@ SysLoggerMain(int argc, char *argv[])
132144
*/
133145
if (redirection_done)
134146
{
135-
int i = open(NULL_DEV, O_WRONLY);
147+
int fd = open(NULL_DEV, O_WRONLY);
136148

137-
dup2(i, fileno(stdout));
138-
dup2(i, fileno(stderr));
139-
close(i);
149+
/*
150+
* The closes might look redundant, but they are not: we want to be
151+
* darn sure the pipe gets closed even if the open failed. We can
152+
* survive running with stderr pointing nowhere, but we can't afford
153+
* to have extra pipe input descriptors hanging around.
154+
*/
155+
close(fileno(stdout));
156+
close(fileno(stderr));
157+
dup2(fd, fileno(stdout));
158+
dup2(fd, fileno(stderr));
159+
close(fd);
140160
}
141161

142162
/*
@@ -317,9 +337,13 @@ SysLoggerMain(int argc, char *argv[])
317337
{
318338
ereport(LOG,
319339
(errmsg("logger shutting down")));
320-
if (syslogFile)
321-
fclose(syslogFile);
322-
/* normal exit from the syslogger is here */
340+
/*
341+
* Normal exit from the syslogger is here. Note that we
342+
* deliberately do not close syslogFile before exiting;
343+
* this is to allow for the possibility of elog messages
344+
* being generated inside proc_exit. Regular exit() will
345+
* take care of flushing and closing stdio channels.
346+
*/
323347
proc_exit(0);
324348
}
325349
}
@@ -401,7 +425,7 @@ SysLogger_Start(void)
401425
(errmsg("could not create logfile \"%s\": %m",
402426
filename))));
403427

404-
setvbuf(syslogFile, NULL, _IOLBF, 0);
428+
setvbuf(syslogFile, NULL, LBF_MODE, 0);
405429

406430
pfree(filename);
407431

@@ -557,7 +581,7 @@ syslogger_parseArgs(int argc, char *argv[])
557581
if (fd != -1)
558582
{
559583
syslogFile = fdopen(fd, "a");
560-
setvbuf(syslogFile, NULL, _IOLBF, 0);
584+
setvbuf(syslogFile, NULL, LBF_MODE, 0);
561585
}
562586
redirection_done = (bool) atoi(*argv++);
563587
#else /* WIN32 */
@@ -568,7 +592,7 @@ syslogger_parseArgs(int argc, char *argv[])
568592
if (fd != 0)
569593
{
570594
syslogFile = fdopen(fd, "a");
571-
setvbuf(syslogFile, NULL, _IOLBF, 0);
595+
setvbuf(syslogFile, NULL, LBF_MODE, 0);
572596
}
573597
}
574598
redirection_done = (bool) atoi(*argv++);
@@ -631,7 +655,8 @@ pipeThread(void *arg)
631655
{
632656
DWORD error = GetLastError();
633657

634-
if (error == ERROR_HANDLE_EOF)
658+
if (error == ERROR_HANDLE_EOF ||
659+
error == ERROR_BROKEN_PIPE)
635660
break;
636661
ereport(LOG,
637662
(errcode_for_file_access(),
@@ -689,7 +714,7 @@ logfile_rotate(void)
689714
return;
690715
}
691716

692-
setvbuf(fh, NULL, _IOLBF, 0);
717+
setvbuf(fh, NULL, LBF_MODE, 0);
693718

694719
/* On Windows, need to interlock against data-transfer thread */
695720
#ifdef WIN32

0 commit comments

Comments
 (0)