Skip to content

Commit 220ec93

Browse files
committed
Arrange for proper newline termination of syslogger's own messages,
per Andreas.
1 parent 18661f2 commit 220ec93

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

src/backend/postmaster/syslogger.c

Lines changed: 54 additions & 8 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.3 2004/08/06 16:06:59 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.4 2004/08/06 19:17:31 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -102,6 +102,7 @@ static volatile sig_atomic_t got_SIGHUP = false;
102102
static pid_t syslogger_forkexec(void);
103103
static void syslogger_parseArgs(int argc, char *argv[]);
104104
#endif
105+
static void write_syslogger_file_binary(const char *buffer, int count);
105106
#ifdef WIN32
106107
static unsigned int __stdcall pipeThread(void *arg);
107108
#endif
@@ -309,7 +310,7 @@ SysLoggerMain(int argc, char *argv[])
309310
}
310311
else if (bytesRead > 0)
311312
{
312-
write_syslogger_file(logbuffer, bytesRead);
313+
write_syslogger_file_binary(logbuffer, bytesRead);
313314
continue;
314315
}
315316
else
@@ -494,13 +495,16 @@ SysLogger_Start(void)
494495
close(syslogPipe[1]);
495496
syslogPipe[1] = -1;
496497
#else
498+
int fd;
499+
497500
fflush(stderr);
498-
if (dup2(_open_osfhandle((long)syslogPipe[1],
499-
_O_APPEND | _O_TEXT),
500-
_fileno(stderr)) < 0)
501+
fd = _open_osfhandle((long) syslogPipe[1],
502+
_O_APPEND | _O_TEXT);
503+
if (dup2(fd, _fileno(stderr)) < 0)
501504
ereport(FATAL,
502505
(errcode_for_file_access(),
503506
errmsg("could not redirect stderr: %m")));
507+
close(fd);
504508
/* Now we are done with the write end of the pipe. */
505509
CloseHandle(syslogPipe[1]);
506510
syslogPipe[1] = 0;
@@ -590,7 +594,7 @@ syslogger_parseArgs(int argc, char *argv[])
590594
if (fd != 0)
591595
{
592596
fd = _open_osfhandle(fd, _O_APPEND);
593-
if (fd != 0)
597+
if (fd > 0)
594598
{
595599
syslogFile = fdopen(fd, "a");
596600
setvbuf(syslogFile, NULL, LBF_MODE, 0);
@@ -609,14 +613,56 @@ syslogger_parseArgs(int argc, char *argv[])
609613
*/
610614

611615
/*
612-
* Write to the currently open logfile
616+
* Write text to the currently open logfile
613617
*
614618
* This is exported so that elog.c can call it when am_syslogger is true.
615619
* This allows the syslogger process to record elog messages of its own,
616620
* even though its stderr does not point at the syslog pipe.
617621
*/
618622
void
619623
write_syslogger_file(const char *buffer, int count)
624+
{
625+
#ifdef WIN32
626+
/*
627+
* On Windows we need to do our own newline-to-CRLF translation.
628+
*/
629+
char convbuf[256];
630+
char *p;
631+
int n;
632+
633+
p = convbuf;
634+
n = 0;
635+
while (count-- > 0)
636+
{
637+
if (*buffer == '\n')
638+
{
639+
*p++ = '\r';
640+
n++;
641+
}
642+
*p++ = *buffer++;
643+
n++;
644+
if (n >= sizeof(convbuf) - 1)
645+
{
646+
write_syslogger_file_binary(convbuf, n);
647+
p = convbuf;
648+
n = 0;
649+
}
650+
}
651+
if (n > 0)
652+
write_syslogger_file_binary(convbuf, n);
653+
#else /* !WIN32 */
654+
write_syslogger_file_binary(buffer, count);
655+
#endif
656+
}
657+
658+
/*
659+
* Write binary data to the currently open logfile
660+
*
661+
* On Windows the data arriving in the pipe already has CR/LF newlines,
662+
* so we must send it to the file without further translation.
663+
*/
664+
static void
665+
write_syslogger_file_binary(const char *buffer, int count)
620666
{
621667
int rc;
622668

@@ -664,7 +710,7 @@ pipeThread(void *arg)
664710
errmsg("could not read from logger pipe: %m")));
665711
}
666712
else if (bytesRead > 0)
667-
write_syslogger_file(logbuffer, bytesRead);
713+
write_syslogger_file_binary(logbuffer, bytesRead);
668714
}
669715

670716
/* We exit the above loop only upon detecting pipe EOF */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy