PostgreSQL Source Code  git master
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/fd.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)
 
static void update_metainfo_datafile (void)
 
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

◆ NBUFFER_LISTS

#define NBUFFER_LISTS   256

Definition at line 108 of file syslogger.c.

Referenced by flush_pipe_input(), and process_pipe_input().

◆ READ_BUF_SIZE

#define READ_BUF_SIZE   (2 * PIPE_CHUNK_SIZE)

Definition at line 57 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

Function Documentation

◆ flush_pipe_input()

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

Definition at line 945 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, generate_unaccent_rules::str, and write_syslogger_file().

Referenced by SysLoggerMain(), and write_syslogger_file().

946 {
947  int i;
948 
949  /* Dump any incomplete protocol messages */
950  for (i = 0; i < NBUFFER_LISTS; i++)
951  {
952  List *list = buffer_lists[i];
953  ListCell *cell;
954 
955  foreach(cell, list)
956  {
957  save_buffer *buf = (save_buffer *) lfirst(cell);
958 
959  if (buf->pid != 0)
960  {
961  StringInfo str = &(buf->data);
962 
963  write_syslogger_file(str->data, str->len,
965  /* Mark the buffer unused, and reclaim string storage */
966  buf->pid = 0;
967  pfree(str->data);
968  }
969  }
970  }
971 
972  /*
973  * Force out any remaining pipe data as-is; we don't bother trying to
974  * remove any protocol headers that may exist in it.
975  */
976  if (*bytes_in_logbuffer > 0)
977  write_syslogger_file(logbuffer, *bytes_in_logbuffer,
979  *bytes_in_logbuffer = 0;
980 }
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:996
int32 pid
Definition: syslogger.c:104
void pfree(void *pointer)
Definition: mcxt.c:1031
#define NBUFFER_LISTS
Definition: syslogger.c:108
static char * buf
Definition: pg_test_fsync.c:67
static List * buffer_lists[NBUFFER_LISTS]
Definition: syslogger.c:109
StringInfoData data
Definition: syslogger.c:105
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define lfirst(lc)
Definition: pg_list.h:106
int i
Definition: pg_list.h:45

◆ logfile_getname()

static char * logfile_getname ( pg_time_t  timestamp,
const char *  suffix 
)
static

Definition at line 1298 of file syslogger.c.

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

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

1299 {
1300  char *filename;
1301  int len;
1302 
1303  filename = palloc(MAXPGPATH);
1304 
1305  snprintf(filename, MAXPGPATH, "%s/", Log_directory);
1306 
1307  len = strlen(filename);
1308 
1309  /* treat Log_filename as a strftime pattern */
1310  pg_strftime(filename + len, MAXPGPATH - len, Log_filename,
1312 
1313  if (suffix != NULL)
1314  {
1315  len = strlen(filename);
1316  if (len > 4 && (strcmp(filename + (len - 4), ".log") == 0))
1317  len -= 4;
1318  strlcpy(filename + len, suffix, MAXPGPATH - len);
1319  }
1320 
1321  return filename;
1322 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:122
char * Log_directory
Definition: syslogger.c:67
int64 timestamp
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
pg_tz * log_timezone
Definition: pgtz.c:31
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1314
char * Log_filename
Definition: syslogger.c:68
static char * filename
Definition: pg_dumpall.c:87
void * palloc(Size size)
Definition: mcxt.c:924

◆ logfile_open()

static FILE * logfile_open ( const char *  filename,
const char *  mode,
bool  allow_errors 
)
static

Definition at line 1123 of file syslogger.c.

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

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

1124 {
1125  FILE *fh;
1126  mode_t oumask;
1127 
1128  /*
1129  * Note we do not let Log_file_mode disable IWUSR, since we certainly want
1130  * to be able to write the files ourselves.
1131  */
1132  oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & (S_IRWXU | S_IRWXG | S_IRWXO)));
1133  fh = fopen(filename, mode);
1134  umask(oumask);
1135 
1136  if (fh)
1137  {
1138  setvbuf(fh, NULL, PG_IOLBF, 0);
1139 
1140 #ifdef WIN32
1141  /* use CRLF line endings on Windows */
1142  _setmode(_fileno(fh), _O_TEXT);
1143 #endif
1144  }
1145  else
1146  {
1147  int save_errno = errno;
1148 
1149  ereport(allow_errors ? LOG : FATAL,
1151  errmsg("could not open log file \"%s\": %m",
1152  filename)));
1153  errno = save_errno;
1154  }
1155 
1156  return fh;
1157 }
#define LOG
Definition: elog.h:26
int Log_file_mode
Definition: syslogger.c:70
#define S_IWUSR
Definition: win32_port.h:274
#define FATAL
Definition: elog.h:52
#define S_IRWXG
Definition: win32_port.h:292
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_IOLBF
Definition: port.h:304
static char * filename
Definition: pg_dumpall.c:87
#define S_IRWXU
Definition: win32_port.h:280
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define S_IRWXO
Definition: win32_port.h:304

◆ logfile_rotate()

static void logfile_rotate ( bool  time_based_rotation,
int  size_rotation_for 
)
static

Definition at line 1163 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, pfree(), rotation_disabled, rotation_requested, set_next_rotation_time(), syslogFile, and update_metainfo_datafile().

Referenced by SysLoggerMain().

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

◆ open_csvlogfile()

static void open_csvlogfile ( void  )
static

Definition at line 1099 of file syslogger.c.

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

Referenced by write_syslogger_file().

1100 {
1101  char *filename;
1102 
1103  filename = logfile_getname(time(NULL), ".csv");
1104 
1105  csvlogFile = logfile_open(filename, "a", false);
1106 
1107  if (last_csv_file_name != NULL) /* probably shouldn't happen */
1109 
1111 
1113 }
static FILE * csvlogFile
Definition: syslogger.c:86
void pfree(void *pointer)
Definition: mcxt.c:1031
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1298
static char * last_csv_file_name
Definition: syslogger.c:89
static void update_metainfo_datafile(void)
Definition: syslogger.c:1362
static char * filename
Definition: pg_dumpall.c:87
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1123

◆ process_pipe_input()

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

Definition at line 794 of file syslogger.c.

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

Referenced by SysLoggerMain(), and write_syslogger_file().

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

◆ set_next_rotation_time()

static void set_next_rotation_time ( void  )
static

Definition at line 1328 of file syslogger.c.

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

Referenced by logfile_rotate(), and SysLoggerMain().

1329 {
1330  pg_time_t now;
1331  struct pg_tm *tm;
1332  int rotinterval;
1333 
1334  /* nothing to do if time-based rotation is disabled */
1335  if (Log_RotationAge <= 0)
1336  return;
1337 
1338  /*
1339  * The requirements here are to choose the next time > now that is a
1340  * "multiple" of the log rotation interval. "Multiple" can be interpreted
1341  * fairly loosely. In this version we align to log_timezone rather than
1342  * GMT.
1343  */
1344  rotinterval = Log_RotationAge * SECS_PER_MINUTE; /* convert to seconds */
1345  now = (pg_time_t) time(NULL);
1346  tm = pg_localtime(&now, log_timezone);
1347  now += tm->tm_gmtoff;
1348  now -= now % rotinterval;
1349  now += rotinterval;
1350  now -= tm->tm_gmtoff;
1352 }
static pg_time_t next_rotation_time
Definition: syslogger.c:82
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:106
pg_tz * log_timezone
Definition: pgtz.c:31
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int Log_RotationAge
Definition: syslogger.c:65
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1314
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534

◆ sigHupHandler()

static void sigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 1427 of file syslogger.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by SysLoggerMain().

1428 {
1429  int save_errno = errno;
1430 
1431  got_SIGHUP = true;
1432  SetLatch(MyLatch);
1433 
1434  errno = save_errno;
1435 }
static volatile sig_atomic_t got_SIGHUP
Definition: syslogger.c:126
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
struct Latch * MyLatch
Definition: globals.c:53

◆ sigUsr1Handler()

static void sigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 1439 of file syslogger.c.

References MyLatch, rotation_requested, and SetLatch().

Referenced by SysLoggerMain().

1440 {
1441  int save_errno = errno;
1442 
1443  rotation_requested = true;
1444  SetLatch(MyLatch);
1445 
1446  errno = save_errno;
1447 }
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
struct Latch * MyLatch
Definition: globals.c:53
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:127

◆ SysLogger_Start()

int SysLogger_Start ( void  )

Definition at line 521 of file syslogger.c.

References Assert, av, 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(), lengthof, LOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, MakePGDirectory(), pfree(), PG_IOLBF, PGSharedMemoryDetach(), redirection_done, snprintf(), syslogFile, SysLoggerMain(), syslogPipe, and TRUE.

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

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

◆ SysLoggerMain()

NON_EXEC_STATIC void SysLoggerMain ( int  argc,
char *  argv[] 
)

Definition at line 158 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(), MakePGDirectory(), MyLatch, MyStartTime, next_rotation_time, now(), 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, update_metainfo_datafile(), WAIT_EVENT_SYSLOGGER_MAIN, WaitLatch(), WaitLatchOrSocket(), WL_LATCH_SET, WL_SOCKET_READABLE, and WL_TIMEOUT.

Referenced by BackendRun(), and SysLogger_Start().

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

◆ update_metainfo_datafile()

static void update_metainfo_datafile ( void  )
static

Definition at line 1362 of file syslogger.c.

References ereport, errcode_for_file_access(), errmsg(), last_csv_file_name, last_file_name, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, LOG_METAINFO_DATAFILE, LOG_METAINFO_DATAFILE_TMP, and logfile_open().

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

1363 {
1364  FILE *fh;
1365 
1368  {
1369  if (unlink(LOG_METAINFO_DATAFILE) < 0 && errno != ENOENT)
1370  ereport(LOG,
1372  errmsg("could not remove file \"%s\": %m",
1374  return;
1375  }
1376 
1377  if ((fh = logfile_open(LOG_METAINFO_DATAFILE_TMP, "w", true)) == NULL)
1378  {
1379  ereport(LOG,
1381  errmsg("could not open file \"%s\": %m",
1383  return;
1384  }
1385 
1386  if (last_file_name && (Log_destination & LOG_DESTINATION_STDERR))
1387  {
1388  if (fprintf(fh, "stderr %s\n", last_file_name) < 0)
1389  {
1390  ereport(LOG,
1392  errmsg("could not write file \"%s\": %m",
1394  fclose(fh);
1395  return;
1396  }
1397  }
1398 
1399  if (last_csv_file_name && (Log_destination & LOG_DESTINATION_CSVLOG))
1400  {
1401  if (fprintf(fh, "csvlog %s\n", last_csv_file_name) < 0)
1402  {
1403  ereport(LOG,
1405  errmsg("could not write file \"%s\": %m",
1407  fclose(fh);
1408  return;
1409  }
1410  }
1411  fclose(fh);
1412 
1414  ereport(LOG,
1416  errmsg("could not rename file \"%s\" to \"%s\": %m",
1418 }
#define LOG_METAINFO_DATAFILE
Definition: syslogger.h:94
#define LOG
Definition: elog.h:26
static char * last_file_name
Definition: syslogger.c:88
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
static char * last_csv_file_name
Definition: syslogger.c:89
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define LOG_METAINFO_DATAFILE_TMP
Definition: syslogger.h:95
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:1123
int Log_destination
Definition: elog.c:106

◆ write_syslogger_file()

void write_syslogger_file ( const char *  buffer,
int  count,
int  destination 
)

Definition at line 996 of file syslogger.c.

References _dosmaperr(), arg, csvlogFile, ereport, errcode_for_file_access(), errmsg(), error(), flush_pipe_input(), LOG, LOG_DESTINATION_CSVLOG, Log_RotationSize, logfile, MyLatch, open_csvlogfile(), pipe_eof_seen, process_pipe_input(), READ_BUF_SIZE, SetLatch(), strerror(), syslogFile, syslogPipe, and write_stderr.

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

997 {
998  int rc;
999  FILE *logfile;
1000 
1001  if (destination == LOG_DESTINATION_CSVLOG && csvlogFile == NULL)
1002  open_csvlogfile();
1003 
1004  logfile = destination == LOG_DESTINATION_CSVLOG ? csvlogFile : syslogFile;
1005  rc = fwrite(buffer, 1, count, logfile);
1006 
1007  /* can't use ereport here because of possible recursion */
1008  if (rc != count)
1009  write_stderr("could not write to log file: %s\n", strerror(errno));
1010 }
static FILE * csvlogFile
Definition: syslogger.c:86
#define write_stderr(str)
Definition: parallel.c:182
static void open_csvlogfile(void)
Definition: syslogger.c:1099
static FILE * logfile
Definition: pg_regress.c:100
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
static FILE * syslogFile
Definition: syslogger.c:85
const char * strerror(int errnum)
Definition: strerror.c:19

Variable Documentation

◆ am_syslogger

bool am_syslogger = false

Definition at line 75 of file syslogger.c.

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

◆ buffer_lists

List* buffer_lists[NBUFFER_LISTS]
static

Definition at line 109 of file syslogger.c.

◆ csvlogFile

FILE* csvlogFile = NULL
static

Definition at line 86 of file syslogger.c.

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

◆ first_syslogger_file_time

NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0

Definition at line 87 of file syslogger.c.

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

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 126 of file syslogger.c.

Referenced by sigHupHandler(), and SysLoggerMain().

◆ last_csv_file_name

char* last_csv_file_name = NULL
static

Definition at line 89 of file syslogger.c.

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

◆ last_file_name

char* last_file_name = NULL
static

Definition at line 88 of file syslogger.c.

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

◆ Log_directory

char* Log_directory = NULL

◆ Log_file_mode

int Log_file_mode = S_IRUSR | S_IWUSR

Definition at line 70 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

char* Log_filename = NULL

Definition at line 68 of file syslogger.c.

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

◆ Log_RotationAge

int Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR

Definition at line 65 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

int Log_RotationSize = 10 * 1024

Definition at line 66 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ Log_truncate_on_rotation

bool Log_truncate_on_rotation = false

Definition at line 69 of file syslogger.c.

Referenced by logfile_rotate().

◆ Logging_collector

bool Logging_collector = false

Definition at line 64 of file syslogger.c.

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

◆ next_rotation_time

pg_time_t next_rotation_time
static

Definition at line 82 of file syslogger.c.

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

◆ pipe_eof_seen

bool pipe_eof_seen = false
static

Definition at line 83 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ redirection_done

bool redirection_done

Definition at line 354 of file postmaster.c.

Referenced by SysLogger_Start(), and SysLoggerMain().

◆ rotation_disabled

bool rotation_disabled = false
static

Definition at line 84 of file syslogger.c.

Referenced by logfile_rotate(), and SysLoggerMain().

◆ rotation_requested

volatile sig_atomic_t rotation_requested = false
static

Definition at line 127 of file syslogger.c.

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

◆ syslogFile

FILE* syslogFile = NULL
static

Definition at line 85 of file syslogger.c.

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

◆ syslogPipe

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