*** pgsql/src/backend/postmaster/syslogger.c 2010/07/06 19:18:57 1.58 --- pgsql/src/backend/postmaster/syslogger.c 2010/07/16 22:25:50 1.59 *************** *** 18,24 **** * * * IDENTIFICATION ! * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.57 2010/04/16 09:51:49 heikki Exp $ * *------------------------------------------------------------------------- */ --- 18,24 ---- * * * IDENTIFICATION ! * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.58 2010/07/06 19:18:57 momjian Exp $ * *------------------------------------------------------------------------- */ *************** int Log_RotationSize = 10 * 1024; *** 73,78 **** --- 73,79 ---- char *Log_directory = NULL; char *Log_filename = NULL; bool Log_truncate_on_rotation = false; + int Log_file_mode = 0600; /* * Globally visible state (used by elog.c) *************** static void syslogger_parseArgs(int argc *** 135,140 **** --- 136,143 ---- static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer); static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer); static void open_csvlogfile(void); + static FILE *logfile_open(const char *filename, const char *mode, + bool allow_errors); #ifdef WIN32 static unsigned int __stdcall pipeThread(void *arg); *************** SysLogger_Start(void) *** 516,530 **** */ filename = logfile_getname(time(NULL), NULL); ! syslogFile = fopen(filename, "a"); ! ! if (!syslogFile) ! ereport(FATAL, ! (errcode_for_file_access(), ! (errmsg("could not create log file \"%s\": %m", ! filename)))); ! ! setvbuf(syslogFile, NULL, LBF_MODE, 0); pfree(filename); --- 519,525 ---- */ filename = logfile_getname(time(NULL), NULL); ! syslogFile = logfile_open(filename, "a", false); pfree(filename); *************** static void *** 1000,1027 **** open_csvlogfile(void) { char *filename; - FILE *fh; filename = logfile_getname(time(NULL), ".csv"); ! fh = fopen(filename, "a"); ! if (!fh) ! ereport(FATAL, ! (errcode_for_file_access(), ! (errmsg("could not create log file \"%s\": %m", ! filename)))); ! setvbuf(fh, NULL, LBF_MODE, 0); #ifdef WIN32 ! _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */ #endif ! csvlogFile = fh; ! ! pfree(filename); } /* --- 995,1050 ---- open_csvlogfile(void) { char *filename; filename = logfile_getname(time(NULL), ".csv"); ! csvlogFile = logfile_open(filename, "a", false); ! pfree(filename); ! } ! ! /* ! * Open a new logfile with proper permissions and buffering options. ! * ! * If allow_errors is true, we just log any open failure and return NULL ! * (with errno still correct for the fopen failure). ! * Otherwise, errors are treated as fatal. ! */ ! static FILE * ! logfile_open(const char *filename, const char *mode, bool allow_errors) ! { ! FILE *fh; ! mode_t oumask; ! /* ! * Note we do not let Log_file_mode disable IWUSR, since we certainly ! * want to be able to write the files ourselves. ! */ ! oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & 0777)); ! fh = fopen(filename, mode); ! umask(oumask); ! ! if (fh) ! { ! setvbuf(fh, NULL, LBF_MODE, 0); #ifdef WIN32 ! /* use CRLF line endings on Windows */ ! _setmode(_fileno(fh), _O_TEXT); #endif + } + else + { + int save_errno = errno; ! ereport(allow_errors ? LOG : FATAL, ! (errcode_for_file_access(), ! errmsg("could not open log file \"%s\": %m", ! filename))); ! errno = save_errno; ! } + return fh; } /* *************** logfile_rotate(bool time_based_rotation, *** 1070,1095 **** if (Log_truncate_on_rotation && time_based_rotation && last_file_name != NULL && strcmp(filename, last_file_name) != 0) ! fh = fopen(filename, "w"); else ! fh = fopen(filename, "a"); if (!fh) { - int saveerrno = errno; - - ereport(LOG, - (errcode_for_file_access(), - errmsg("could not open new log file \"%s\": %m", - filename))); - /* * ENFILE/EMFILE are not too surprising on a busy system; just * keep using the old file till we manage to get a new one. * Otherwise, assume something's wrong with Log_directory and stop * trying to create files. */ ! if (saveerrno != ENFILE && saveerrno != EMFILE) { ereport(LOG, (errmsg("disabling automatic rotation (use SIGHUP to re-enable)"))); --- 1093,1111 ---- if (Log_truncate_on_rotation && time_based_rotation && last_file_name != NULL && strcmp(filename, last_file_name) != 0) ! fh = logfile_open(filename, "w", true); else ! fh = logfile_open(filename, "a", true); if (!fh) { /* * ENFILE/EMFILE are not too surprising on a busy system; just * keep using the old file till we manage to get a new one. * Otherwise, assume something's wrong with Log_directory and stop * trying to create files. */ ! if (errno != ENFILE && errno != EMFILE) { ereport(LOG, (errmsg("disabling automatic rotation (use SIGHUP to re-enable)"))); *************** logfile_rotate(bool time_based_rotation, *** 1104,1115 **** return; } - setvbuf(fh, NULL, LBF_MODE, 0); - - #ifdef WIN32 - _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */ - #endif - fclose(syslogFile); syslogFile = fh; --- 1120,1125 ---- *************** logfile_rotate(bool time_based_rotation, *** 1128,1153 **** if (Log_truncate_on_rotation && time_based_rotation && last_csv_file_name != NULL && strcmp(csvfilename, last_csv_file_name) != 0) ! fh = fopen(csvfilename, "w"); else ! fh = fopen(csvfilename, "a"); if (!fh) { - int saveerrno = errno; - - ereport(LOG, - (errcode_for_file_access(), - errmsg("could not open new log file \"%s\": %m", - csvfilename))); - /* * ENFILE/EMFILE are not too surprising on a busy system; just * keep using the old file till we manage to get a new one. * Otherwise, assume something's wrong with Log_directory and stop * trying to create files. */ ! if (saveerrno != ENFILE && saveerrno != EMFILE) { ereport(LOG, (errmsg("disabling automatic rotation (use SIGHUP to re-enable)"))); --- 1138,1156 ---- if (Log_truncate_on_rotation && time_based_rotation && last_csv_file_name != NULL && strcmp(csvfilename, last_csv_file_name) != 0) ! fh = logfile_open(csvfilename, "w", true); else ! fh = logfile_open(csvfilename, "a", true); if (!fh) { /* * ENFILE/EMFILE are not too surprising on a busy system; just * keep using the old file till we manage to get a new one. * Otherwise, assume something's wrong with Log_directory and stop * trying to create files. */ ! if (errno != ENFILE && errno != EMFILE) { ereport(LOG, (errmsg("disabling automatic rotation (use SIGHUP to re-enable)"))); *************** logfile_rotate(bool time_based_rotation, *** 1162,1173 **** return; } - setvbuf(fh, NULL, LBF_MODE, 0); - - #ifdef WIN32 - _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */ - #endif - fclose(csvlogFile); csvlogFile = fh; --- 1165,1170 ----