PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
syslogger.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include "lib/stringinfo.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "pgstat.h"
#include "pgtime.h"
#include "postmaster/fork_process.h"
#include "postmaster/postmaster.h"
#include "postmaster/syslogger.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pg_shmem.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
#include "utils/timestamp.h"
Include dependency graph for syslogger.c:

Go to the source code of this file.

Data Structures

struct  save_buffer
 

Macros

#define READ_BUF_SIZE   (2 * PIPE_CHUNK_SIZE)
 
#define NBUFFER_LISTS   256
 

Functions

NON_EXEC_STATIC void SysLoggerMain (int argc, char *argv[]) pg_attribute_noreturn()
 
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)
 
static void logfile_rotate (bool time_based_rotation, int size_rotation_for)
 
static char * logfile_getname (pg_time_t timestamp, const char *suffix)
 
static void set_next_rotation_time (void)
 
static void sigHupHandler (SIGNAL_ARGS)
 
static void sigUsr1Handler (SIGNAL_ARGS)
 
int SysLogger_Start (void)
 
void write_syslogger_file (const char *buffer, int count, int destination)
 

Variables

bool Logging_collector = false
 
int Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR
 
int Log_RotationSize = 10 * 1024
 
char * Log_directory = NULL
 
char * Log_filename = NULL
 
bool Log_truncate_on_rotation = false
 
int Log_file_mode = S_IRUSR | S_IWUSR
 
bool am_syslogger = false
 
bool redirection_done
 
static pg_time_t next_rotation_time
 
static bool pipe_eof_seen = false
 
static bool rotation_disabled = false
 
static FILE * syslogFile = NULL
 
static FILE * csvlogFile = NULL
 
NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0
 
static char * last_file_name = NULL
 
static char * last_csv_file_name = NULL
 
static Listbuffer_lists [NBUFFER_LISTS]
 
int syslogPipe [2] = {-1, -1}
 
static volatile sig_atomic_t got_SIGHUP = false
 
static volatile sig_atomic_t rotation_requested = false
 

Macro Definition Documentation

#define NBUFFER_LISTS   256

Definition at line 107 of file syslogger.c.

Referenced by flush_pipe_input(), and process_pipe_input().

#define READ_BUF_SIZE   (2 * PIPE_CHUNK_SIZE)

Definition at line 56 of file syslogger.c.

Referenced by SysLoggerMain().

Function Documentation

static void flush_pipe_input ( char *  logbuffer,
int *  bytes_in_logbuffer 
)
static

Definition at line 935 of file syslogger.c.

References buf, StringInfoData::data, save_buffer::data, i, StringInfoData::len, lfirst, sort-test::list, LOG_DESTINATION_STDERR, NBUFFER_LISTS, pfree(), save_buffer::pid, and write_syslogger_file().

Referenced by SysLoggerMain().

936 {
937  int i;
938 
939  /* Dump any incomplete protocol messages */
940  for (i = 0; i < NBUFFER_LISTS; i++)
941  {
942  List *list = buffer_lists[i];
943  ListCell *cell;
944 
945  foreach(cell, list)
946  {
947  save_buffer *buf = (save_buffer *) lfirst(cell);
948 
949  if (buf->pid != 0)
950  {
951  StringInfo str = &(buf->data);
952 
953  write_syslogger_file(str->data, str->len,
955  /* Mark the buffer unused, and reclaim string storage */
956  buf->pid = 0;
957  pfree(str->data);
958  }
959  }
960  }
961 
962  /*
963  * Force out any remaining pipe data as-is; we don't bother trying to
964  * remove any protocol headers that may exist in it.
965  */
966  if (*bytes_in_logbuffer > 0)
967  write_syslogger_file(logbuffer, *bytes_in_logbuffer,
969  *bytes_in_logbuffer = 0;
970 }
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:986
int32 pid
Definition: syslogger.c:103
void pfree(void *pointer)
Definition: mcxt.c:992
#define NBUFFER_LISTS
Definition: syslogger.c:107
static char * buf
Definition: pg_test_fsync.c:65
static List * buffer_lists[NBUFFER_LISTS]
Definition: syslogger.c:108
StringInfoData data
Definition: syslogger.c:104
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define lfirst(lc)
Definition: pg_list.h:106
tuple list
Definition: sort-test.py:11
int i
Definition: pg_list.h:45
static char * logfile_getname ( pg_time_t  timestamp,
const char *  suffix 
)
static

Definition at line 1284 of file syslogger.c.

References filename, Log_directory, Log_filename, log_timezone, MAXPGPATH, NULL, palloc(), pg_localtime(), pg_strftime(), snprintf(), and strlcpy().

Referenced by logfile_rotate(), open_csvlogfile(), SysLogger_Start(), and SysLoggerMain().

1285 {
1286  char *filename;
1287  int len;
1288 
1289  filename = palloc(MAXPGPATH);
1290 
1291  snprintf(filename, MAXPGPATH, "%s/", Log_directory);
1292 
1293  len = strlen(filename);
1294 
1295  /* treat Log_filename as a strftime pattern */
1296  pg_strftime(filename + len, MAXPGPATH - len, Log_filename,
1298 
1299  if (suffix != NULL)
1300  {
1301  len = strlen(filename);
1302  if (len > 4 && (strcmp(filename + (len - 4), ".log") == 0))
1303  len -= 4;
1304  strlcpy(filename + len, suffix, MAXPGPATH - len);
1305  }
1306 
1307  return filename;
1308 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:124
char * Log_directory
Definition: syslogger.c:66
int64 timestamp
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
pg_tz * log_timezone
Definition: pgtz.c:30
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:226
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1254
char * Log_filename
Definition: syslogger.c:67
static char * filename
Definition: pg_dumpall.c:84
void * palloc(Size size)
Definition: mcxt.c:891
static FILE * logfile_open ( const char *  filename,
const char *  mode,
bool  allow_errors 
)
static

Definition at line 1111 of file syslogger.c.

References ereport, errcode_for_file_access(), errmsg(), FATAL, LOG, Log_file_mode, NULL, PG_IOLBF, S_IRWXG, and S_IRWXO.

Referenced by logfile_rotate(), open_csvlogfile(), and SysLogger_Start().

1112 {
1113  FILE *fh;
1114  mode_t oumask;
1115 
1116  /*
1117  * Note we do not let Log_file_mode disable IWUSR, since we certainly want
1118  * to be able to write the files ourselves.
1119  */
1120  oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & (S_IRWXU | S_IRWXG | S_IRWXO)));
1121  fh = fopen(filename, mode);
1122  umask(oumask);
1123 
1124  if (fh)
1125  {
1126  setvbuf(fh, NULL, PG_IOLBF, 0);
1127 
1128 #ifdef WIN32
1129  /* use CRLF line endings on Windows */
1130  _setmode(_fileno(fh), _O_TEXT);
1131 #endif
1132  }
1133  else
1134  {
1135  int save_errno = errno;
1136 
1137  ereport(allow_errors ? LOG : FATAL,
1139  errmsg("could not open log file \"%s\": %m",
1140  filename)));
1141  errno = save_errno;
1142  }
1143 
1144  return fh;
1145 }
#define LOG
Definition: elog.h:26
int Log_file_mode
Definition: syslogger.c:69
#define FATAL
Definition: elog.h:52
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_IOLBF
Definition: port.h:340
#define S_IRWXO
Definition: win32.h:484
#define NULL
Definition: c.h:226
#define S_IRWXG
Definition: win32.h:480
static char * filename
Definition: pg_dumpall.c:84
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void logfile_rotate ( bool  time_based_rotation,
int  size_rotation_for 
)
static

Definition at line 1151 of file syslogger.c.

References csvlogFile, ereport, errmsg(), filename, last_csv_file_name, last_file_name, LOG, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, Log_truncate_on_rotation, logfile_getname(), logfile_open(), next_rotation_time, NULL, pfree(), rotation_disabled, rotation_requested, set_next_rotation_time(), and syslogFile.

Referenced by SysLoggerMain().

1152 {
1153  char *filename;
1154  char *csvfilename = NULL;
1155  pg_time_t fntime;
1156  FILE *fh;
1157 
1158  rotation_requested = false;
1159 
1160  /*
1161  * When doing a time-based rotation, invent the new logfile name based on
1162  * the planned rotation time, not current time, to avoid "slippage" in the
1163  * file name when we don't do the rotation immediately.
1164  */
1165  if (time_based_rotation)
1166  fntime = next_rotation_time;
1167  else
1168  fntime = time(NULL);
1169  filename = logfile_getname(fntime, NULL);
1170  if (csvlogFile != NULL)
1171  csvfilename = logfile_getname(fntime, ".csv");
1172 
1173  /*
1174  * Decide whether to overwrite or append. We can overwrite if (a)
1175  * Log_truncate_on_rotation is set, (b) the rotation was triggered by
1176  * elapsed time and not something else, and (c) the computed file name is
1177  * different from what we were previously logging into.
1178  *
1179  * Note: last_file_name should never be NULL here, but if it is, append.
1180  */
1181  if (time_based_rotation || (size_rotation_for & LOG_DESTINATION_STDERR))
1182  {
1183  if (Log_truncate_on_rotation && time_based_rotation &&
1184  last_file_name != NULL &&
1185  strcmp(filename, last_file_name) != 0)
1186  fh = logfile_open(filename, "w", true);
1187  else
1188  fh = logfile_open(filename, "a", true);
1189 
1190  if (!fh)
1191  {
1192  /*
1193  * ENFILE/EMFILE are not too surprising on a busy system; just
1194  * keep using the old file till we manage to get a new one.
1195  * Otherwise, assume something's wrong with Log_directory and stop
1196  * trying to create files.
1197  */
1198  if (errno != ENFILE && errno != EMFILE)
1199  {
1200  ereport(LOG,
1201  (errmsg("disabling automatic rotation (use SIGHUP to re-enable)")));
1202  rotation_disabled = true;
1203  }
1204 
1205  if (filename)
1206  pfree(filename);
1207  if (csvfilename)
1208  pfree(csvfilename);
1209  return;
1210  }
1211 
1212  fclose(syslogFile);
1213  syslogFile = fh;
1214 
1215  /* instead of pfree'ing filename, remember it for next time */
1216  if (last_file_name != NULL)
1219  filename = NULL;
1220  }
1221 
1222  /* Same as above, but for csv file. */
1223 
1224  if (csvlogFile != NULL &&
1225  (time_based_rotation || (size_rotation_for & LOG_DESTINATION_CSVLOG)))
1226  {
1227  if (Log_truncate_on_rotation && time_based_rotation &&
1228  last_csv_file_name != NULL &&
1229  strcmp(csvfilename, last_csv_file_name) != 0)
1230  fh = logfile_open(csvfilename, "w", true);
1231  else
1232  fh = logfile_open(csvfilename, "a", true);
1233 
1234  if (!fh)
1235  {
1236  /*
1237  * ENFILE/EMFILE are not too surprising on a busy system; just
1238  * keep using the old file till we manage to get a new one.
1239  * Otherwise, assume something's wrong with Log_directory and stop
1240  * trying to create files.
1241  */
1242  if (errno != ENFILE && errno != EMFILE)
1243  {
1244  ereport(LOG,
1245  (errmsg("disabling automatic rotation (use SIGHUP to re-enable)")));
1246  rotation_disabled = true;
1247  }
1248 
1249  if (filename)
1250  pfree(filename);
1251  if (csvfilename)
1252  pfree(csvfilename);
1253  return;
1254  }
1255 
1256  fclose(csvlogFile);
1257  csvlogFile = fh;
1258 
1259  /* instead of pfree'ing filename, remember it for next time */
1260  if (last_csv_file_name != NULL)
1262  last_csv_file_name = csvfilename;
1263  csvfilename = NULL;
1264  }
1265 
1266  if (filename)
1267  pfree(filename);
1268  if (csvfilename)
1269  pfree(csvfilename);
1270 
1272 }
static pg_time_t next_rotation_time
Definition: syslogger.c:81
int64 pg_time_t
Definition: pgtime.h:23
static FILE * csvlogFile
Definition: syslogger.c:85
static void set_next_rotation_time(void)
Definition: syslogger.c:1314
#define LOG
Definition: elog.h:26
static char * last_file_name
Definition: syslogger.c:87
void pfree(void *pointer)
Definition: mcxt.c:992
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1284
static bool rotation_disabled
Definition: syslogger.c:83
#define ereport(elevel, rest)
Definition: elog.h:122
static char * last_csv_file_name
Definition: syslogger.c:88
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define NULL
Definition: c.h:226
static FILE * syslogFile
Definition: syslogger.c:84
static char * filename
Definition: pg_dumpall.c:84
int errmsg(const char *fmt,...)
Definition: elog.c:797
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1111
bool Log_truncate_on_rotation
Definition: syslogger.c:68
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:126
static void open_csvlogfile ( void  )
static

Definition at line 1089 of file syslogger.c.

References csvlogFile, filename, last_csv_file_name, logfile_getname(), logfile_open(), NULL, and pfree().

Referenced by write_syslogger_file().

1090 {
1091  char *filename;
1092 
1093  filename = logfile_getname(time(NULL), ".csv");
1094 
1095  csvlogFile = logfile_open(filename, "a", false);
1096 
1097  if (last_csv_file_name != NULL) /* probably shouldn't happen */
1099 
1101 }
static FILE * csvlogFile
Definition: syslogger.c:85
void pfree(void *pointer)
Definition: mcxt.c:992
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1284
static char * last_csv_file_name
Definition: syslogger.c:88
#define NULL
Definition: c.h:226
static char * filename
Definition: pg_dumpall.c:84
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1111
static void process_pipe_input ( char *  logbuffer,
int *  bytes_in_logbuffer 
)
static

Definition at line 784 of file syslogger.c.

References appendBinaryStringInfo(), buf, StringInfoData::data, save_buffer::data, initStringInfo(), PipeProtoHeader::is_last, lappend(), StringInfoData::len, PipeProtoHeader::len, lfirst, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, memmove, NBUFFER_LISTS, NULL, PipeProtoHeader::nuls, offsetof, palloc(), pfree(), PipeProtoHeader::pid, save_buffer::pid, PIPE_HEADER_SIZE, PIPE_MAX_PAYLOAD, and write_syslogger_file().

Referenced by SysLoggerMain().

785 {
786  char *cursor = logbuffer;
787  int count = *bytes_in_logbuffer;
788  int dest = LOG_DESTINATION_STDERR;
789 
790  /* While we have enough for a header, process data... */
791  while (count >= (int) (offsetof(PipeProtoHeader, data) +1))
792  {
793  PipeProtoHeader p;
794  int chunklen;
795 
796  /* Do we have a valid header? */
797  memcpy(&p, cursor, offsetof(PipeProtoHeader, data));
798  if (p.nuls[0] == '\0' && p.nuls[1] == '\0' &&
799  p.len > 0 && p.len <= PIPE_MAX_PAYLOAD &&
800  p.pid != 0 &&
801  (p.is_last == 't' || p.is_last == 'f' ||
802  p.is_last == 'T' || p.is_last == 'F'))
803  {
804  List *buffer_list;
805  ListCell *cell;
806  save_buffer *existing_slot = NULL,
807  *free_slot = NULL;
808  StringInfo str;
809 
810  chunklen = PIPE_HEADER_SIZE + p.len;
811 
812  /* Fall out of loop if we don't have the whole chunk yet */
813  if (count < chunklen)
814  break;
815 
816  dest = (p.is_last == 'T' || p.is_last == 'F') ?
818 
819  /* Locate any existing buffer for this source pid */
820  buffer_list = buffer_lists[p.pid % NBUFFER_LISTS];
821  foreach(cell, buffer_list)
822  {
823  save_buffer *buf = (save_buffer *) lfirst(cell);
824 
825  if (buf->pid == p.pid)
826  {
827  existing_slot = buf;
828  break;
829  }
830  if (buf->pid == 0 && free_slot == NULL)
831  free_slot = buf;
832  }
833 
834  if (p.is_last == 'f' || p.is_last == 'F')
835  {
836  /*
837  * Save a complete non-final chunk in a per-pid buffer
838  */
839  if (existing_slot != NULL)
840  {
841  /* Add chunk to data from preceding chunks */
842  str = &(existing_slot->data);
844  cursor + PIPE_HEADER_SIZE,
845  p.len);
846  }
847  else
848  {
849  /* First chunk of message, save in a new buffer */
850  if (free_slot == NULL)
851  {
852  /*
853  * Need a free slot, but there isn't one in the list,
854  * so create a new one and extend the list with it.
855  */
856  free_slot = palloc(sizeof(save_buffer));
857  buffer_list = lappend(buffer_list, free_slot);
858  buffer_lists[p.pid % NBUFFER_LISTS] = buffer_list;
859  }
860  free_slot->pid = p.pid;
861  str = &(free_slot->data);
862  initStringInfo(str);
864  cursor + PIPE_HEADER_SIZE,
865  p.len);
866  }
867  }
868  else
869  {
870  /*
871  * Final chunk --- add it to anything saved for that pid, and
872  * either way write the whole thing out.
873  */
874  if (existing_slot != NULL)
875  {
876  str = &(existing_slot->data);
878  cursor + PIPE_HEADER_SIZE,
879  p.len);
880  write_syslogger_file(str->data, str->len, dest);
881  /* Mark the buffer unused, and reclaim string storage */
882  existing_slot->pid = 0;
883  pfree(str->data);
884  }
885  else
886  {
887  /* The whole message was one chunk, evidently. */
889  dest);
890  }
891  }
892 
893  /* Finished processing this chunk */
894  cursor += chunklen;
895  count -= chunklen;
896  }
897  else
898  {
899  /* Process non-protocol data */
900 
901  /*
902  * Look for the start of a protocol header. If found, dump data
903  * up to there and repeat the loop. Otherwise, dump it all and
904  * fall out of the loop. (Note: we want to dump it all if at all
905  * possible, so as to avoid dividing non-protocol messages across
906  * logfiles. We expect that in many scenarios, a non-protocol
907  * message will arrive all in one read(), and we want to respect
908  * the read() boundary if possible.)
909  */
910  for (chunklen = 1; chunklen < count; chunklen++)
911  {
912  if (cursor[chunklen] == '\0')
913  break;
914  }
915  /* fall back on the stderr log as the destination */
916  write_syslogger_file(cursor, chunklen, LOG_DESTINATION_STDERR);
917  cursor += chunklen;
918  count -= chunklen;
919  }
920  }
921 
922  /* We don't have a full chunk, so left-align what remains in the buffer */
923  if (count > 0 && cursor != logbuffer)
924  memmove(logbuffer, cursor, count);
925  *bytes_in_logbuffer = count;
926 }
#define PIPE_HEADER_SIZE
Definition: syslogger.h:60
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:986
int32 pid
Definition: syslogger.c:103
char nuls[2]
Definition: syslogger.h:46
#define PIPE_MAX_PAYLOAD
Definition: syslogger.h:61
void pfree(void *pointer)
Definition: mcxt.c:992
#define NBUFFER_LISTS
Definition: syslogger.c:107
static char * buf
Definition: pg_test_fsync.c:65
#define memmove(d, s, c)
Definition: c.h:1058
Definition: type.h:125
static List * buffer_lists[NBUFFER_LISTS]
Definition: syslogger.c:108
List * lappend(List *list, void *datum)
Definition: list.c:128
StringInfoData data
Definition: syslogger.c:104
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
void * palloc(Size size)
Definition: mcxt.c:891
Definition: pg_list.h:45
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
#define offsetof(type, field)
Definition: c.h:551
static void set_next_rotation_time ( void  )
static

Definition at line 1314 of file syslogger.c.

References Log_RotationAge, log_timezone, next_rotation_time, now(), NULL, pg_localtime(), SECS_PER_MINUTE, tm, and pg_tm::tm_gmtoff.

Referenced by logfile_rotate(), and SysLoggerMain().

1315 {
1316  pg_time_t now;
1317  struct pg_tm *tm;
1318  int rotinterval;
1319 
1320  /* nothing to do if time-based rotation is disabled */
1321  if (Log_RotationAge <= 0)
1322  return;
1323 
1324  /*
1325  * The requirements here are to choose the next time > now that is a
1326  * "multiple" of the log rotation interval. "Multiple" can be interpreted
1327  * fairly loosely. In this version we align to log_timezone rather than
1328  * GMT.
1329  */
1330  rotinterval = Log_RotationAge * SECS_PER_MINUTE; /* convert to seconds */
1331  now = (pg_time_t) time(NULL);
1332  tm = pg_localtime(&now, log_timezone);
1333  now += tm->tm_gmtoff;
1334  now -= now % rotinterval;
1335  now += rotinterval;
1336  now -= tm->tm_gmtoff;
1338 }
static pg_time_t next_rotation_time
Definition: syslogger.c:81
int64 pg_time_t
Definition: pgtime.h:23
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:103
pg_tz * log_timezone
Definition: pgtz.c:30
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int Log_RotationAge
Definition: syslogger.c:64
#define NULL
Definition: c.h:226
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1254
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1533
static void sigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 1347 of file syslogger.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by SysLoggerMain().

1348 {
1349  int save_errno = errno;
1350 
1351  got_SIGHUP = true;
1352  SetLatch(MyLatch);
1353 
1354  errno = save_errno;
1355 }
static volatile sig_atomic_t got_SIGHUP
Definition: syslogger.c:125
void SetLatch(volatile Latch *latch)
Definition: latch.c:380
struct Latch * MyLatch
Definition: globals.c:51
static void sigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 1359 of file syslogger.c.

References MyLatch, rotation_requested, and SetLatch().

Referenced by SysLoggerMain().

1360 {
1361  int save_errno = errno;
1362 
1363  rotation_requested = true;
1364  SetLatch(MyLatch);
1365 
1366  errno = save_errno;
1367 }
void SetLatch(volatile Latch *latch)
Definition: latch.c:380
struct Latch * MyLatch
Definition: globals.c:51
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:126
int SysLogger_Start ( void  )

Definition at line 511 of file syslogger.c.

References close, ClosePostmasterPorts(), dsm_detach_all(), ereport, errcode_for_file_access(), errcode_for_socket_access(), errhint(), errmsg(), FATAL, fd(), filename, first_syslogger_file_time, fork_process(), InitPostmasterChild(), LOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, mkdir, NULL, pfree(), PGSharedMemoryDetach(), redirection_done, syslogFile, SysLoggerMain(), syslogPipe, and TRUE.

Referenced by PostmasterMain(), reaper(), and ServerLoop().

512 {
513  pid_t sysloggerPid;
514  char *filename;
515 
516  if (!Logging_collector)
517  return 0;
518 
519  /*
520  * If first time through, create the pipe which will receive stderr
521  * output.
522  *
523  * If the syslogger crashes and needs to be restarted, we continue to use
524  * the same pipe (indeed must do so, since extant backends will be writing
525  * into that pipe).
526  *
527  * This means the postmaster must continue to hold the read end of the
528  * pipe open, so we can pass it down to the reincarnated syslogger. This
529  * is a bit klugy but we have little choice.
530  */
531 #ifndef WIN32
532  if (syslogPipe[0] < 0)
533  {
534  if (pipe(syslogPipe) < 0)
535  ereport(FATAL,
537  (errmsg("could not create pipe for syslog: %m"))));
538  }
539 #else
540  if (!syslogPipe[0])
541  {
542  SECURITY_ATTRIBUTES sa;
543 
544  memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
545  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
546  sa.bInheritHandle = TRUE;
547 
548  if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
549  ereport(FATAL,
551  (errmsg("could not create pipe for syslog: %m"))));
552  }
553 #endif
554 
555  /*
556  * Create log directory if not present; ignore errors
557  */
558  mkdir(Log_directory, S_IRWXU);
559 
560  /*
561  * The initial logfile is created right in the postmaster, to verify that
562  * the Log_directory is writable. We save the reference time so that the
563  * syslogger child process can recompute this file name.
564  *
565  * It might look a bit strange to re-do this during a syslogger restart,
566  * but we must do so since the postmaster closed syslogFile after the
567  * previous fork (and remembering that old file wouldn't be right anyway).
568  * Note we always append here, we won't overwrite any existing file. This
569  * is consistent with the normal rules, because by definition this is not
570  * a time-based rotation.
571  */
574 
575  syslogFile = logfile_open(filename, "a", false);
576 
577  pfree(filename);
578 
579 #ifdef EXEC_BACKEND
580  switch ((sysloggerPid = syslogger_forkexec()))
581 #else
582  switch ((sysloggerPid = fork_process()))
583 #endif
584  {
585  case -1:
586  ereport(LOG,
587  (errmsg("could not fork system logger: %m")));
588  return 0;
589 
590 #ifndef EXEC_BACKEND
591  case 0:
592  /* in postmaster child ... */
594 
595  /* Close the postmaster's sockets */
596  ClosePostmasterPorts(true);
597 
598  /* Drop our connection to postmaster's shared memory, as well */
599  dsm_detach_all();
601 
602  /* do the work */
603  SysLoggerMain(0, NULL);
604  break;
605 #endif
606 
607  default:
608  /* success, in postmaster */
609 
610  /* now we redirect stderr, if not done already */
611  if (!redirection_done)
612  {
613 #ifdef WIN32
614  int fd;
615 #endif
616 
617  /*
618  * Leave a breadcrumb trail when redirecting, in case the user
619  * forgets that redirection is active and looks only at the
620  * original stderr target file.
621  */
622  ereport(LOG,
623  (errmsg("redirecting log output to logging collector process"),
624  errhint("Future log output will appear in directory \"%s\".",
625  Log_directory)));
626 
627 #ifndef WIN32
628  fflush(stdout);
629  if (dup2(syslogPipe[1], fileno(stdout)) < 0)
630  ereport(FATAL,
632  errmsg("could not redirect stdout: %m")));
633  fflush(stderr);
634  if (dup2(syslogPipe[1], fileno(stderr)) < 0)
635  ereport(FATAL,
637  errmsg("could not redirect stderr: %m")));
638  /* Now we are done with the write end of the pipe. */
639  close(syslogPipe[1]);
640  syslogPipe[1] = -1;
641 #else
642 
643  /*
644  * open the pipe in binary mode and make sure stderr is binary
645  * after it's been dup'ed into, to avoid disturbing the pipe
646  * chunking protocol.
647  */
648  fflush(stderr);
649  fd = _open_osfhandle((intptr_t) syslogPipe[1],
650  _O_APPEND | _O_BINARY);
651  if (dup2(fd, _fileno(stderr)) < 0)
652  ereport(FATAL,
654  errmsg("could not redirect stderr: %m")));
655  close(fd);
656  _setmode(_fileno(stderr), _O_BINARY);
657 
658  /*
659  * Now we are done with the write end of the pipe.
660  * CloseHandle() must not be called because the preceding
661  * close() closes the underlying handle.
662  */
663  syslogPipe[1] = 0;
664 #endif
665  redirection_done = true;
666  }
667 
668  /* postmaster will never write the file; close it */
669  fclose(syslogFile);
670  syslogFile = NULL;
671  return (int) sysloggerPid;
672  }
673 
674  /* we should never reach here */
675  return 0;
676 }
NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: syslogger.c:156
int errhint(const char *fmt,...)
Definition: elog.c:987
char * Log_directory
Definition: syslogger.c:66
void InitPostmasterChild(void)
Definition: miscinit.c:175
pid_t fork_process(void)
Definition: fork_process.c:31
#define mkdir(a, b)
Definition: win32.h:65
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2414
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:768
bool Logging_collector
Definition: syslogger.c:63
bool redirection_done
Definition: postmaster.c:351
void pfree(void *pointer)
Definition: mcxt.c:992
#define FATAL
Definition: elog.h:52
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1284
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
int errcode_for_socket_access(void)
Definition: elog.c:669
#define NULL
Definition: c.h:226
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition: syslogger.c:86
void dsm_detach_all(void)
Definition: dsm.c:655
static FILE * syslogFile
Definition: syslogger.c:84
static char * filename
Definition: pg_dumpall.c:84
int errmsg(const char *fmt,...)
Definition: elog.c:797
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1111
#define TRUE
Definition: c.h:214
#define close(a)
Definition: win32.h:17
int syslogPipe[2]
Definition: syslogger.c:112
NON_EXEC_STATIC void SysLoggerMain ( int  argc,
char *  argv[] 
)

Definition at line 156 of file syslogger.c.

References am_syslogger, close, csvlogFile, DEBUG1, DEVNULL, EINTR, elog, ereport, errcode_for_socket_access(), errmsg(), FATAL, fd(), first_syslogger_file_time, flush_pipe_input(), got_SIGHUP, init_ps_display(), last_file_name, LOG, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, Log_directory, Log_filename, Log_RotationAge, Log_RotationSize, logfile_getname(), logfile_rotate(), mkdir, MyLatch, MyStartTime, next_rotation_time, now(), NULL, pfree(), PG_SETMASK, PGC_SIGHUP, pipe_eof_seen, pqsignal(), proc_exit(), process_pipe_input(), ProcessConfigFile(), pstrdup(), read, READ_BUF_SIZE, redirection_done, ResetLatch(), rotation_disabled, rotation_requested, set_next_rotation_time(), SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGCONT, SIGHUP, sigHupHandler(), SIGPIPE, SIGQUIT, SIGTTIN, SIGTTOU, SIGUSR1, sigUsr1Handler(), SIGUSR2, SIGWINCH, syslogFile, syslogPipe, UnBlockSig, WAIT_EVENT_SYSLOGGER_MAIN, WaitLatch(), WaitLatchOrSocket(), WL_LATCH_SET, WL_SOCKET_READABLE, and WL_TIMEOUT.

Referenced by SysLogger_Start().

157 {
158 #ifndef WIN32
159  char logbuffer[READ_BUF_SIZE];
160  int bytes_in_logbuffer = 0;
161 #endif
162  char *currentLogDir;
163  char *currentLogFilename;
164  int currentLogRotationAge;
165  pg_time_t now;
166 
167  now = MyStartTime;
168 
169 #ifdef EXEC_BACKEND
170  syslogger_parseArgs(argc, argv);
171 #endif /* EXEC_BACKEND */
172 
173  am_syslogger = true;
174 
175  init_ps_display("logger process", "", "", "");
176 
177  /*
178  * If we restarted, our stderr is already redirected into our own input
179  * pipe. This is of course pretty useless, not to mention that it
180  * interferes with detecting pipe EOF. Point stderr to /dev/null. This
181  * assumes that all interesting messages generated in the syslogger will
182  * come through elog.c and will be sent to write_syslogger_file.
183  */
184  if (redirection_done)
185  {
186  int fd = open(DEVNULL, O_WRONLY, 0);
187 
188  /*
189  * The closes might look redundant, but they are not: we want to be
190  * darn sure the pipe gets closed even if the open failed. We can
191  * survive running with stderr pointing nowhere, but we can't afford
192  * to have extra pipe input descriptors hanging around.
193  *
194  * As we're just trying to reset these to go to DEVNULL, there's not
195  * much point in checking for failure from the close/dup2 calls here,
196  * if they fail then presumably the file descriptors are closed and
197  * any writes will go into the bitbucket anyway.
198  */
199  close(fileno(stdout));
200  close(fileno(stderr));
201  if (fd != -1)
202  {
203  (void) dup2(fd, fileno(stdout));
204  (void) dup2(fd, fileno(stderr));
205  close(fd);
206  }
207  }
208 
209  /*
210  * Syslogger's own stderr can't be the syslogPipe, so set it back to text
211  * mode if we didn't just close it. (It was set to binary in
212  * SubPostmasterMain).
213  */
214 #ifdef WIN32
215  else
216  _setmode(_fileno(stderr), _O_TEXT);
217 #endif
218 
219  /*
220  * Also close our copy of the write end of the pipe. This is needed to
221  * ensure we can detect pipe EOF correctly. (But note that in the restart
222  * case, the postmaster already did this.)
223  */
224 #ifndef WIN32
225  if (syslogPipe[1] >= 0)
226  close(syslogPipe[1]);
227  syslogPipe[1] = -1;
228 #else
229  if (syslogPipe[1])
230  CloseHandle(syslogPipe[1]);
231  syslogPipe[1] = 0;
232 #endif
233 
234  /*
235  * Properly accept or ignore signals the postmaster might send us
236  *
237  * Note: we ignore all termination signals, and instead exit only when all
238  * upstream processes are gone, to ensure we don't miss any dying gasps of
239  * broken backends...
240  */
241 
242  pqsignal(SIGHUP, sigHupHandler); /* set flag to read config file */
243  pqsignal(SIGINT, SIG_IGN);
244  pqsignal(SIGTERM, SIG_IGN);
248  pqsignal(SIGUSR1, sigUsr1Handler); /* request log rotation */
250 
251  /*
252  * Reset some signals that are accepted by postmaster but not here
253  */
259 
261 
262 #ifdef WIN32
263  /* Fire up separate data transfer thread */
264  InitializeCriticalSection(&sysloggerSection);
265  EnterCriticalSection(&sysloggerSection);
266 
267  threadHandle = (HANDLE) _beginthreadex(NULL, 0, pipeThread, NULL, 0, NULL);
268  if (threadHandle == 0)
269  elog(FATAL, "could not create syslogger data transfer thread: %m");
270 #endif /* WIN32 */
271 
272  /*
273  * Remember active logfile's name. We recompute this from the reference
274  * time because passing down just the pg_time_t is a lot cheaper than
275  * passing a whole file path in the EXEC_BACKEND case.
276  */
278 
279  /* remember active logfile parameters */
280  currentLogDir = pstrdup(Log_directory);
281  currentLogFilename = pstrdup(Log_filename);
282  currentLogRotationAge = Log_RotationAge;
283  /* set next planned rotation time */
285 
286  /* main worker loop */
287  for (;;)
288  {
289  bool time_based_rotation = false;
290  int size_rotation_for = 0;
291  long cur_timeout;
292  int cur_flags;
293 
294 #ifndef WIN32
295  int rc;
296 #endif
297 
298  /* Clear any already-pending wakeups */
300 
301  /*
302  * Process any requests or signals received recently.
303  */
304  if (got_SIGHUP)
305  {
306  got_SIGHUP = false;
308 
309  /*
310  * Check if the log directory or filename pattern changed in
311  * postgresql.conf. If so, force rotation to make sure we're
312  * writing the logfiles in the right place.
313  */
314  if (strcmp(Log_directory, currentLogDir) != 0)
315  {
316  pfree(currentLogDir);
317  currentLogDir = pstrdup(Log_directory);
318  rotation_requested = true;
319 
320  /*
321  * Also, create new directory if not present; ignore errors
322  */
323  mkdir(Log_directory, S_IRWXU);
324  }
325  if (strcmp(Log_filename, currentLogFilename) != 0)
326  {
327  pfree(currentLogFilename);
328  currentLogFilename = pstrdup(Log_filename);
329  rotation_requested = true;
330  }
331 
332  /*
333  * If rotation time parameter changed, reset next rotation time,
334  * but don't immediately force a rotation.
335  */
336  if (currentLogRotationAge != Log_RotationAge)
337  {
338  currentLogRotationAge = Log_RotationAge;
340  }
341 
342  /*
343  * If we had a rotation-disabling failure, re-enable rotation
344  * attempts after SIGHUP, and force one immediately.
345  */
346  if (rotation_disabled)
347  {
348  rotation_disabled = false;
349  rotation_requested = true;
350  }
351  }
352 
354  {
355  /* Do a logfile rotation if it's time */
356  now = (pg_time_t) time(NULL);
357  if (now >= next_rotation_time)
358  rotation_requested = time_based_rotation = true;
359  }
360 
362  {
363  /* Do a rotation if file is too big */
364  if (ftell(syslogFile) >= Log_RotationSize * 1024L)
365  {
366  rotation_requested = true;
367  size_rotation_for |= LOG_DESTINATION_STDERR;
368  }
369  if (csvlogFile != NULL &&
370  ftell(csvlogFile) >= Log_RotationSize * 1024L)
371  {
372  rotation_requested = true;
373  size_rotation_for |= LOG_DESTINATION_CSVLOG;
374  }
375  }
376 
377  if (rotation_requested)
378  {
379  /*
380  * Force rotation when both values are zero. It means the request
381  * was sent by pg_rotate_logfile.
382  */
383  if (!time_based_rotation && size_rotation_for == 0)
384  size_rotation_for = LOG_DESTINATION_STDERR | LOG_DESTINATION_CSVLOG;
385  logfile_rotate(time_based_rotation, size_rotation_for);
386  }
387 
388  /*
389  * Calculate time till next time-based rotation, so that we don't
390  * sleep longer than that. We assume the value of "now" obtained
391  * above is still close enough. Note we can't make this calculation
392  * until after calling logfile_rotate(), since it will advance
393  * next_rotation_time.
394  *
395  * Also note that we need to beware of overflow in calculation of the
396  * timeout: with large settings of Log_RotationAge, next_rotation_time
397  * could be more than INT_MAX msec in the future. In that case we'll
398  * wait no more than INT_MAX msec, and try again.
399  */
401  {
402  pg_time_t delay;
403 
404  delay = next_rotation_time - now;
405  if (delay > 0)
406  {
407  if (delay > INT_MAX / 1000)
408  delay = INT_MAX / 1000;
409  cur_timeout = delay * 1000L; /* msec */
410  }
411  else
412  cur_timeout = 0;
413  cur_flags = WL_TIMEOUT;
414  }
415  else
416  {
417  cur_timeout = -1L;
418  cur_flags = 0;
419  }
420 
421  /*
422  * Sleep until there's something to do
423  */
424 #ifndef WIN32
426  WL_LATCH_SET | WL_SOCKET_READABLE | cur_flags,
427  syslogPipe[0],
428  cur_timeout,
430 
431  if (rc & WL_SOCKET_READABLE)
432  {
433  int bytesRead;
434 
435  bytesRead = read(syslogPipe[0],
436  logbuffer + bytes_in_logbuffer,
437  sizeof(logbuffer) - bytes_in_logbuffer);
438  if (bytesRead < 0)
439  {
440  if (errno != EINTR)
441  ereport(LOG,
443  errmsg("could not read from logger pipe: %m")));
444  }
445  else if (bytesRead > 0)
446  {
447  bytes_in_logbuffer += bytesRead;
448  process_pipe_input(logbuffer, &bytes_in_logbuffer);
449  continue;
450  }
451  else
452  {
453  /*
454  * Zero bytes read when select() is saying read-ready means
455  * EOF on the pipe: that is, there are no longer any processes
456  * with the pipe write end open. Therefore, the postmaster
457  * and all backends are shut down, and we are done.
458  */
459  pipe_eof_seen = true;
460 
461  /* if there's any data left then force it out now */
462  flush_pipe_input(logbuffer, &bytes_in_logbuffer);
463  }
464  }
465 #else /* WIN32 */
466 
467  /*
468  * On Windows we leave it to a separate thread to transfer data and
469  * detect pipe EOF. The main thread just wakes up to handle SIGHUP
470  * and rotation conditions.
471  *
472  * Server code isn't generally thread-safe, so we ensure that only one
473  * of the threads is active at a time by entering the critical section
474  * whenever we're not sleeping.
475  */
476  LeaveCriticalSection(&sysloggerSection);
477 
478  (void) WaitLatch(MyLatch,
479  WL_LATCH_SET | cur_flags,
480  cur_timeout,
482 
483  EnterCriticalSection(&sysloggerSection);
484 #endif /* WIN32 */
485 
486  if (pipe_eof_seen)
487  {
488  /*
489  * seeing this message on the real stderr is annoying - so we make
490  * it DEBUG1 to suppress in normal use.
491  */
492  ereport(DEBUG1,
493  (errmsg("logger shutting down")));
494 
495  /*
496  * Normal exit from the syslogger is here. Note that we
497  * deliberately do not close syslogFile before exiting; this is to
498  * allow for the possibility of elog messages being generated
499  * inside proc_exit. Regular exit() will take care of flushing
500  * and closing stdio channels.
501  */
502  proc_exit(0);
503  }
504  }
505 }
static void logfile_rotate(bool time_based_rotation, int size_rotation_for)
Definition: syslogger.c:1151
static pg_time_t next_rotation_time
Definition: syslogger.c:81
#define SIGUSR1
Definition: win32.h:211
#define DEBUG1
Definition: elog.h:25
#define SIGCONT
Definition: win32.h:205
int64 pg_time_t
Definition: pgtime.h:23
char * Log_directory
Definition: syslogger.c:66
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
#define READ_BUF_SIZE
Definition: syslogger.c:56
#define SIGWINCH
Definition: win32.h:209
#define mkdir(a, b)
Definition: win32.h:65
char * pstrdup(const char *in)
Definition: mcxt.c:1165
static FILE * csvlogFile
Definition: syslogger.c:85
pg_time_t MyStartTime
Definition: globals.c:39
#define SIGTTIN
Definition: win32.h:207
static void sigHupHandler(SIGNAL_ARGS)
Definition: syslogger.c:1347
void proc_exit(int code)
Definition: ipc.c:99
#define WL_SOCKET_READABLE
Definition: latch.h:125
void ResetLatch(volatile Latch *latch)
Definition: latch.c:462
static void set_next_rotation_time(void)
Definition: syslogger.c:1314
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define SIGQUIT
Definition: win32.h:197
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
int Log_RotationSize
Definition: syslogger.c:65
bool redirection_done
Definition: postmaster.c:351
static char * last_file_name
Definition: syslogger.c:87
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:301
void pfree(void *pointer)
Definition: mcxt.c:992
#define SIG_IGN
Definition: win32.h:193
#define FATAL
Definition: elog.h:52
static void sigUsr1Handler(SIGNAL_ARGS)
Definition: syslogger.c:1359
bool am_syslogger
Definition: syslogger.c:74
static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
Definition: syslogger.c:784
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1284
static bool rotation_disabled
Definition: syslogger.c:83
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define DEVNULL
Definition: port.h:116
#define ereport(elevel, rest)
Definition: elog.h:122
int errcode_for_socket_access(void)
Definition: elog.c:669
Definition: guc.h:72
#define EINTR
Definition: win32.h:295
#define SIGPIPE
Definition: win32.h:201
#define SIGHUP
Definition: win32.h:196
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define SIG_DFL
Definition: win32.h:191
int Log_RotationAge
Definition: syslogger.c:64
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static volatile sig_atomic_t got_SIGHUP
Definition: syslogger.c:125
#define NULL
Definition: c.h:226
int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:321
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition: syslogger.c:86
static FILE * syslogFile
Definition: syslogger.c:84
char * Log_filename
Definition: syslogger.c:67
#define SIGTTOU
Definition: win32.h:208
static bool pipe_eof_seen
Definition: syslogger.c:82
static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
Definition: syslogger.c:935
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct Latch * MyLatch
Definition: globals.c:51
#define elog
Definition: elog.h:219
#define close(a)
Definition: win32.h:17
#define SIGCHLD
Definition: win32.h:206
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:126
#define WL_LATCH_SET
Definition: latch.h:124
#define SIGALRM
Definition: win32.h:202
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1533
#define read(a, b, c)
Definition: win32.h:18
#define SIGUSR2
Definition: win32.h:212
void init_ps_display(const char *username, const char *dbname, const char *host_info, const char *initial_str)
Definition: ps_status.c:244
int syslogPipe[2]
Definition: syslogger.c:112
void write_syslogger_file ( const char *  buffer,
int  count,
int  destination 
)

Definition at line 986 of file syslogger.c.

References csvlogFile, LOG_DESTINATION_CSVLOG, logfile, NULL, open_csvlogfile(), strerror(), syslogFile, and write_stderr.

Referenced by flush_pipe_input(), process_pipe_input(), send_message_to_server_log(), and write_csvlog().

987 {
988  int rc;
989  FILE *logfile;
990 
991  if (destination == LOG_DESTINATION_CSVLOG && csvlogFile == NULL)
992  open_csvlogfile();
993 
994  logfile = destination == LOG_DESTINATION_CSVLOG ? csvlogFile : syslogFile;
995  rc = fwrite(buffer, 1, count, logfile);
996 
997  /* can't use ereport here because of possible recursion */
998  if (rc != count)
999  write_stderr("could not write to log file: %s\n", strerror(errno));
1000 }
static FILE * csvlogFile
Definition: syslogger.c:85
#define write_stderr(str)
Definition: parallel.c:182
static void open_csvlogfile(void)
Definition: syslogger.c:1089
static FILE * logfile
Definition: pg_regress.c:97
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define NULL
Definition: c.h:226
static FILE * syslogFile
Definition: syslogger.c:84
const char * strerror(int errnum)
Definition: strerror.c:19

Variable Documentation

bool am_syslogger = false

Definition at line 74 of file syslogger.c.

Referenced by send_message_to_server_log(), SysLoggerMain(), and write_csvlog().

List* buffer_lists[NBUFFER_LISTS]
static

Definition at line 108 of file syslogger.c.

FILE* csvlogFile = NULL
static

Definition at line 85 of file syslogger.c.

Referenced by logfile_rotate(), open_csvlogfile(), SysLoggerMain(), and write_syslogger_file().

NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0

Definition at line 86 of file syslogger.c.

Referenced by SysLogger_Start(), and SysLoggerMain().

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 125 of file syslogger.c.

Referenced by sigHupHandler(), and SysLoggerMain().

char* last_csv_file_name = NULL
static

Definition at line 88 of file syslogger.c.

Referenced by logfile_rotate(), and open_csvlogfile().

char* last_file_name = NULL
static

Definition at line 87 of file syslogger.c.

Referenced by logfile_rotate(), and SysLoggerMain().

char* Log_directory = NULL
int Log_file_mode = S_IRUSR | S_IWUSR

Definition at line 69 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

char* Log_filename = NULL

Definition at line 67 of file syslogger.c.

Referenced by logfile_getname(), pg_logdir_ls(), and SysLoggerMain().

int Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR

Definition at line 64 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

int Log_RotationSize = 10 * 1024

Definition at line 65 of file syslogger.c.

Referenced by SysLoggerMain().

bool Log_truncate_on_rotation = false

Definition at line 68 of file syslogger.c.

Referenced by logfile_rotate().

bool Logging_collector = false

Definition at line 63 of file syslogger.c.

Referenced by pg_rotate_logfile(), ServerLoop(), and SysLogger_Start().

pg_time_t next_rotation_time
static

Definition at line 81 of file syslogger.c.

Referenced by logfile_rotate(), set_next_rotation_time(), and SysLoggerMain().

bool pipe_eof_seen = false
static

Definition at line 82 of file syslogger.c.

Referenced by SysLoggerMain().

bool redirection_done

Definition at line 351 of file postmaster.c.

Referenced by SysLogger_Start(), and SysLoggerMain().

bool rotation_disabled = false
static

Definition at line 83 of file syslogger.c.

Referenced by logfile_rotate(), and SysLoggerMain().

volatile sig_atomic_t rotation_requested = false
static

Definition at line 126 of file syslogger.c.

Referenced by logfile_rotate(), sigUsr1Handler(), and SysLoggerMain().

FILE* syslogFile = NULL
static

Definition at line 84 of file syslogger.c.

Referenced by logfile_rotate(), SysLogger_Start(), SysLoggerMain(), and write_syslogger_file().

int syslogPipe[2] = {-1, -1}

Definition at line 112 of file syslogger.c.

Referenced by ClosePostmasterPorts(), SysLogger_Start(), and SysLoggerMain().