Skip to content

Commit 4aa0d70

Browse files
committed
Pass a strdup'd ident string to openlog(), to ensure that reallocation
of GUC memory doesn't cause us to start emitting a bogus ident string. Per report from Han Holl. Also some trivial code cleanup in write_syslog.
1 parent a93bf45 commit 4aa0d70

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

src/backend/utils/error/elog.c

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
*
4444
* IDENTIFICATION
45-
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.162 2005/08/12 21:36:59 momjian Exp $
45+
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.163 2005/10/14 16:41:02 tgl Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -85,6 +85,7 @@ char *Syslog_ident;
8585

8686
static void write_syslog(int level, const char *line);
8787
#endif
88+
8889
#ifdef WIN32
8990
static void write_eventlog(int level, const char *line);
9091
#endif
@@ -1152,41 +1153,51 @@ DebugFileOpen(void)
11521153
#endif
11531154

11541155
/*
1155-
* Write a message line to syslog if the syslog option is set.
1156-
*
1157-
* Our problem here is that many syslog implementations don't handle
1158-
* long messages in an acceptable manner. While this function doesn't
1159-
* help that fact, it does work around by splitting up messages into
1160-
* smaller pieces.
1156+
* Write a message line to syslog
11611157
*/
11621158
static void
11631159
write_syslog(int level, const char *line)
11641160
{
11651161
static bool openlog_done = false;
11661162
static unsigned long seq = 0;
1167-
static int syslog_fac = LOG_LOCAL0;
11681163

1169-
int len = strlen(line);
1164+
int len;
11701165

11711166
if (!openlog_done)
11721167
{
1168+
int syslog_fac;
1169+
char *syslog_ident;
1170+
11731171
if (pg_strcasecmp(Syslog_facility, "LOCAL0") == 0)
11741172
syslog_fac = LOG_LOCAL0;
1175-
if (pg_strcasecmp(Syslog_facility, "LOCAL1") == 0)
1173+
else if (pg_strcasecmp(Syslog_facility, "LOCAL1") == 0)
11761174
syslog_fac = LOG_LOCAL1;
1177-
if (pg_strcasecmp(Syslog_facility, "LOCAL2") == 0)
1175+
else if (pg_strcasecmp(Syslog_facility, "LOCAL2") == 0)
11781176
syslog_fac = LOG_LOCAL2;
1179-
if (pg_strcasecmp(Syslog_facility, "LOCAL3") == 0)
1177+
else if (pg_strcasecmp(Syslog_facility, "LOCAL3") == 0)
11801178
syslog_fac = LOG_LOCAL3;
1181-
if (pg_strcasecmp(Syslog_facility, "LOCAL4") == 0)
1179+
else if (pg_strcasecmp(Syslog_facility, "LOCAL4") == 0)
11821180
syslog_fac = LOG_LOCAL4;
1183-
if (pg_strcasecmp(Syslog_facility, "LOCAL5") == 0)
1181+
else if (pg_strcasecmp(Syslog_facility, "LOCAL5") == 0)
11841182
syslog_fac = LOG_LOCAL5;
1185-
if (pg_strcasecmp(Syslog_facility, "LOCAL6") == 0)
1183+
else if (pg_strcasecmp(Syslog_facility, "LOCAL6") == 0)
11861184
syslog_fac = LOG_LOCAL6;
1187-
if (pg_strcasecmp(Syslog_facility, "LOCAL7") == 0)
1185+
else if (pg_strcasecmp(Syslog_facility, "LOCAL7") == 0)
11881186
syslog_fac = LOG_LOCAL7;
1189-
openlog(Syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT, syslog_fac);
1187+
else
1188+
syslog_fac = LOG_LOCAL0;
1189+
/*
1190+
* openlog() usually just stores the passed char pointer as-is,
1191+
* so we must give it a string that will be unchanged for the life of
1192+
* the process. The Syslog_ident GUC variable does not meet this
1193+
* requirement, so strdup() it. This isn't a memory leak because
1194+
* this code is executed at most once per process.
1195+
*/
1196+
syslog_ident = strdup(Syslog_ident);
1197+
if (syslog_ident == NULL) /* out of memory already!? */
1198+
syslog_ident = "postgres";
1199+
1200+
openlog(syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT, syslog_fac);
11901201
openlog_done = true;
11911202
}
11921203

@@ -1196,8 +1207,16 @@ write_syslog(int level, const char *line)
11961207
*/
11971208
seq++;
11981209

1199-
/* divide into multiple syslog() calls if message is too long */
1200-
/* or if the message contains embedded NewLine(s) '\n' */
1210+
/*
1211+
* Our problem here is that many syslog implementations don't handle
1212+
* long messages in an acceptable manner. While this function doesn't
1213+
* help that fact, it does work around by splitting up messages into
1214+
* smaller pieces.
1215+
*
1216+
* We divide into multiple syslog() calls if message is too long
1217+
* or if the message contains embedded NewLine(s) '\n'.
1218+
*/
1219+
len = strlen(line);
12011220
if (len > PG_SYSLOG_LIMIT || strchr(line, '\n') != NULL)
12021221
{
12031222
int chunk_nr = 0;
@@ -1230,8 +1249,8 @@ write_syslog(int level, const char *line)
12301249
buf[buflen] = '\0';
12311250

12321251
/* already word boundary? */
1233-
if (!isspace((unsigned char) line[buflen]) &&
1234-
line[buflen] != '\0')
1252+
if (line[buflen] != '\0' &&
1253+
!isspace((unsigned char) line[buflen]))
12351254
{
12361255
/* try to divide at word boundary */
12371256
i = buflen - 1;
@@ -1259,6 +1278,7 @@ write_syslog(int level, const char *line)
12591278
}
12601279
}
12611280
#endif /* HAVE_SYSLOG */
1281+
12621282
#ifdef WIN32
12631283
/*
12641284
* Write a message line to the windows event log

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