Commit ace1b29b authored by Tom Lane's avatar Tom Lane

Fix two different copy-and-paste-os in CSV log rotation logic; one that led to

a double-pfree crash and another that effectively disabled size-based rotation
for CSV logs.  Also suppress a memory leak and make some trivial cosmetic
improvements.  Per bug #3901 from Chris Hoover and additional code-reading.
parent a794b99a
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.43 2008/01/01 19:45:51 momjian Exp $ * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.44 2008/01/25 20:42:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -89,7 +89,7 @@ static bool pipe_eof_seen = false; ...@@ -89,7 +89,7 @@ static bool pipe_eof_seen = false;
static FILE *syslogFile = NULL; static FILE *syslogFile = NULL;
static FILE *csvlogFile = NULL; static FILE *csvlogFile = NULL;
static char *last_file_name = NULL; static char *last_file_name = NULL;
static char *last_csvfile_name = NULL; static char *last_csv_file_name = NULL;
/* /*
* Buffers for saving partial messages from different backends. We don't expect * Buffers for saving partial messages from different backends. We don't expect
...@@ -345,12 +345,12 @@ SysLoggerMain(int argc, char *argv[]) ...@@ -345,12 +345,12 @@ SysLoggerMain(int argc, char *argv[])
rotation_requested = true; rotation_requested = true;
size_rotation_for |= LOG_DESTINATION_STDERR; size_rotation_for |= LOG_DESTINATION_STDERR;
} }
if (csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L) if (csvlogFile != NULL &&
ftell(csvlogFile) >= Log_RotationSize * 1024L)
{ {
rotation_requested = true; rotation_requested = true;
size_rotation_for |= LOG_DESTINATION_CSVLOG; size_rotation_for |= LOG_DESTINATION_CSVLOG;
} }
} }
if (rotation_requested) if (rotation_requested)
...@@ -1056,7 +1056,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) ...@@ -1056,7 +1056,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
if (time_based_rotation || (size_rotation_for & LOG_DESTINATION_STDERR)) if (time_based_rotation || (size_rotation_for & LOG_DESTINATION_STDERR))
{ {
if (Log_truncate_on_rotation && time_based_rotation && if (Log_truncate_on_rotation && time_based_rotation &&
last_file_name != NULL && strcmp(filename, last_file_name) != 0) last_file_name != NULL &&
strcmp(filename, last_file_name) != 0)
fh = fopen(filename, "w"); fh = fopen(filename, "w");
else else
fh = fopen(filename, "a"); fh = fopen(filename, "a");
...@@ -1084,6 +1085,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) ...@@ -1084,6 +1085,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
Log_RotationSize = 0; Log_RotationSize = 0;
} }
pfree(filename); pfree(filename);
if (csvfilename)
pfree(csvfilename);
return; return;
} }
...@@ -1107,20 +1110,16 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) ...@@ -1107,20 +1110,16 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
if (last_file_name != NULL) if (last_file_name != NULL)
pfree(last_file_name); pfree(last_file_name);
last_file_name = filename; last_file_name = filename;
} }
/* same as above, but for csv file. */ /* Same as above, but for csv file. */
if (csvlogFile != NULL && ( if (csvlogFile != NULL &&
time_based_rotation || (time_based_rotation || (size_rotation_for & LOG_DESTINATION_CSVLOG)))
(size_rotation_for & LOG_DESTINATION_STDERR)))
{ {
if (Log_truncate_on_rotation && time_based_rotation && if (Log_truncate_on_rotation && time_based_rotation &&
last_csvfile_name != NULL && last_csv_file_name != NULL &&
strcmp(csvfilename, last_csvfile_name) != 0) strcmp(csvfilename, last_csv_file_name) != 0)
fh = fopen(csvfilename, "w"); fh = fopen(csvfilename, "w");
else else
fh = fopen(csvfilename, "a"); fh = fopen(csvfilename, "a");
...@@ -1168,13 +1167,12 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) ...@@ -1168,13 +1167,12 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
#endif #endif
/* instead of pfree'ing filename, remember it for next time */ /* instead of pfree'ing filename, remember it for next time */
if (last_csvfile_name != NULL) if (last_csv_file_name != NULL)
pfree(last_csvfile_name); pfree(last_csv_file_name);
last_csvfile_name = filename; last_csv_file_name = csvfilename;
} }
set_next_rotation_time(); set_next_rotation_time();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment