18
18
*
19
19
*
20
20
* 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 $
22
22
*
23
23
*-------------------------------------------------------------------------
24
24
*/
41
41
#include "utils/ps_status.h"
42
42
43
43
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
+
44
56
/*
45
57
* GUC parameters. Redirect_stderr cannot be changed after postmaster
46
58
* start, but the rest can change at SIGHUP.
@@ -132,11 +144,19 @@ SysLoggerMain(int argc, char *argv[])
132
144
*/
133
145
if (redirection_done )
134
146
{
135
- int i = open (NULL_DEV ,O_WRONLY );
147
+ int fd = open (NULL_DEV ,O_WRONLY );
136
148
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 );
140
160
}
141
161
142
162
/*
@@ -317,9 +337,13 @@ SysLoggerMain(int argc, char *argv[])
317
337
{
318
338
ereport (LOG ,
319
339
(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
+ */
323
347
proc_exit (0 );
324
348
}
325
349
}
@@ -401,7 +425,7 @@ SysLogger_Start(void)
401
425
(errmsg ("could not create logfile \"%s\": %m" ,
402
426
filename ))));
403
427
404
- setvbuf (syslogFile ,NULL ,_IOLBF ,0 );
428
+ setvbuf (syslogFile ,NULL ,LBF_MODE ,0 );
405
429
406
430
pfree (filename );
407
431
@@ -557,7 +581,7 @@ syslogger_parseArgs(int argc, char *argv[])
557
581
if (fd != -1 )
558
582
{
559
583
syslogFile = fdopen (fd ,"a" );
560
- setvbuf (syslogFile ,NULL ,_IOLBF ,0 );
584
+ setvbuf (syslogFile ,NULL ,LBF_MODE ,0 );
561
585
}
562
586
redirection_done = (bool )atoi (* argv ++ );
563
587
#else /* WIN32 */
@@ -568,7 +592,7 @@ syslogger_parseArgs(int argc, char *argv[])
568
592
if (fd != 0 )
569
593
{
570
594
syslogFile = fdopen (fd ,"a" );
571
- setvbuf (syslogFile ,NULL ,_IOLBF ,0 );
595
+ setvbuf (syslogFile ,NULL ,LBF_MODE ,0 );
572
596
}
573
597
}
574
598
redirection_done = (bool )atoi (* argv ++ );
@@ -631,7 +655,8 @@ pipeThread(void *arg)
631
655
{
632
656
DWORD error = GetLastError ();
633
657
634
- if (error == ERROR_HANDLE_EOF )
658
+ if (error == ERROR_HANDLE_EOF ||
659
+ error == ERROR_BROKEN_PIPE )
635
660
break ;
636
661
ereport (LOG ,
637
662
(errcode_for_file_access (),
@@ -689,7 +714,7 @@ logfile_rotate(void)
689
714
return ;
690
715
}
691
716
692
- setvbuf (fh ,NULL ,_IOLBF ,0 );
717
+ setvbuf (fh ,NULL ,LBF_MODE ,0 );
693
718
694
719
/* On Windows, need to interlock against data-transfer thread */
695
720
#ifdef WIN32