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 "common/file_perm.h"
#include "lib/stringinfo.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "pgstat.h"
#include "pgtime.h"
#include "port/pg_bitutils.h"
#include "postmaster/fork_process.h"
#include "postmaster/interrupt.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 "tcop/tcopprot.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 LOGROTATE_SIGNAL_FILE   "logrotate"
 
#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 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 bool logfile_rotate_dest (bool time_based_rotation, int size_rotation_for, pg_time_t fntime, int target_dest, char **last_file_name, FILE **logFile)
 
static char * logfile_getname (pg_time_t timestamp, const char *suffix)
 
static void set_next_rotation_time (void)
 
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)
 
bool CheckLogrotateSignal (void)
 
void RemoveLogrotateSignalFiles (void)
 

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 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_sys_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 rotation_requested = false
 

Macro Definition Documentation

◆ LOGROTATE_SIGNAL_FILE

#define LOGROTATE_SIGNAL_FILE   "logrotate"

Definition at line 64 of file syslogger.c.

Referenced by CheckLogrotateSignal(), and RemoveLogrotateSignalFiles().

◆ NBUFFER_LISTS

#define NBUFFER_LISTS   256

Definition at line 110 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 61 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

Function Documentation

◆ CheckLogrotateSignal()

bool CheckLogrotateSignal ( void  )

Definition at line 1545 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE, and stat.

Referenced by sigusr1_handler().

1546 {
1547  struct stat stat_buf;
1548 
1549  if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
1550  return true;
1551 
1552  return false;
1553 }
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:64
#define stat
Definition: win32_port.h:283

◆ flush_pipe_input()

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

Definition at line 1043 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().

1044 {
1045  int i;
1046 
1047  /* Dump any incomplete protocol messages */
1048  for (i = 0; i < NBUFFER_LISTS; i++)
1049  {
1050  List *list = buffer_lists[i];
1051  ListCell *cell;
1052 
1053  foreach(cell, list)
1054  {
1055  save_buffer *buf = (save_buffer *) lfirst(cell);
1056 
1057  if (buf->pid != 0)
1058  {
1059  StringInfo str = &(buf->data);
1060 
1061  write_syslogger_file(str->data, str->len,
1063  /* Mark the buffer unused, and reclaim string storage */
1064  buf->pid = 0;
1065  pfree(str->data);
1066  }
1067  }
1068  }
1069 
1070  /*
1071  * Force out any remaining pipe data as-is; we don't bother trying to
1072  * remove any protocol headers that may exist in it.
1073  */
1074  if (*bytes_in_logbuffer > 0)
1075  write_syslogger_file(logbuffer, *bytes_in_logbuffer,
1077  *bytes_in_logbuffer = 0;
1078 }
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:1094
int32 pid
Definition: syslogger.c:106
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NBUFFER_LISTS
Definition: syslogger.c:110
static char * buf
Definition: pg_test_fsync.c:68
static List * buffer_lists[NBUFFER_LISTS]
Definition: syslogger.c:111
StringInfoData data
Definition: syslogger.c:107
#define LOG_DESTINATION_STDERR
Definition: elog.h:435
#define lfirst(lc)
Definition: pg_list.h:169
int i
Definition: pg_list.h:50

◆ logfile_getname()

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

Definition at line 1397 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_dest(), SysLogger_Start(), and SysLoggerMain().

1398 {
1399  char *filename;
1400  int len;
1401 
1402  filename = palloc(MAXPGPATH);
1403 
1404  snprintf(filename, MAXPGPATH, "%s/", Log_directory);
1405 
1406  len = strlen(filename);
1407 
1408  /* treat Log_filename as a strftime pattern */
1409  pg_strftime(filename + len, MAXPGPATH - len, Log_filename,
1411 
1412  if (suffix != NULL)
1413  {
1414  len = strlen(filename);
1415  if (len > 4 && (strcmp(filename + (len - 4), ".log") == 0))
1416  len -= 4;
1417  strlcpy(filename + len, suffix, MAXPGPATH - len);
1418  }
1419 
1420  return filename;
1421 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:128
char * Log_directory
Definition: syslogger.c:74
int64 timestamp
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:1342
char * Log_filename
Definition: syslogger.c:75
static char * filename
Definition: pg_dumpall.c:92
void * palloc(Size size)
Definition: mcxt.c:1062
#define snprintf
Definition: port.h:217

◆ logfile_open()

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

Definition at line 1212 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_dest(), and SysLogger_Start().

1213 {
1214  FILE *fh;
1215  mode_t oumask;
1216 
1217  /*
1218  * Note we do not let Log_file_mode disable IWUSR, since we certainly want
1219  * to be able to write the files ourselves.
1220  */
1221  oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & (S_IRWXU | S_IRWXG | S_IRWXO)));
1222  fh = fopen(filename, mode);
1223  umask(oumask);
1224 
1225  if (fh)
1226  {
1227  setvbuf(fh, NULL, PG_IOLBF, 0);
1228 
1229 #ifdef WIN32
1230  /* use CRLF line endings on Windows */
1231  _setmode(_fileno(fh), _O_TEXT);
1232 #endif
1233  }
1234  else
1235  {
1236  int save_errno = errno;
1237 
1238  ereport(allow_errors ? LOG : FATAL,
1240  errmsg("could not open log file \"%s\": %m",
1241  filename)));
1242  errno = save_errno;
1243  }
1244 
1245  return fh;
1246 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
#define LOG
Definition: elog.h:26
int Log_file_mode
Definition: syslogger.c:77
#define S_IWUSR
Definition: win32_port.h:291
#define FATAL
Definition: elog.h:49
#define S_IRWXG
Definition: win32_port.h:309
int errcode_for_file_access(void)
Definition: elog.c:721
#define PG_IOLBF
Definition: port.h:345
#define ereport(elevel,...)
Definition: elog.h:157
static char * filename
Definition: pg_dumpall.c:92
#define S_IRWXU
Definition: win32_port.h:297
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define S_IRWXO
Definition: win32_port.h:321

◆ logfile_rotate()

static void logfile_rotate ( bool  time_based_rotation,
int  size_rotation_for 
)
static

Definition at line 1354 of file syslogger.c.

References csvlogFile, last_csv_file_name, last_sys_file_name, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, logfile_rotate_dest(), next_rotation_time, rotation_requested, set_next_rotation_time(), syslogFile, and update_metainfo_datafile().

Referenced by SysLoggerMain().

1355 {
1356  pg_time_t fntime;
1357 
1358  rotation_requested = false;
1359 
1360  /*
1361  * When doing a time-based rotation, invent the new logfile name based on
1362  * the planned rotation time, not current time, to avoid "slippage" in the
1363  * file name when we don't do the rotation immediately.
1364  */
1365  if (time_based_rotation)
1366  fntime = next_rotation_time;
1367  else
1368  fntime = time(NULL);
1369 
1370  /* file rotation for stderr */
1371  if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
1373  &syslogFile))
1374  return;
1375 
1376  /* file rotation for csvlog */
1377  if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
1379  &csvlogFile))
1380  return;
1381 
1383 
1385 }
static pg_time_t next_rotation_time
Definition: syslogger.c:84
int64 pg_time_t
Definition: pgtime.h:23
static FILE * csvlogFile
Definition: syslogger.c:88
static void set_next_rotation_time(void)
Definition: syslogger.c:1427
static char * last_csv_file_name
Definition: syslogger.c:91
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define LOG_DESTINATION_STDERR
Definition: elog.h:435
static void update_metainfo_datafile(void)
Definition: syslogger.c:1462
static FILE * syslogFile
Definition: syslogger.c:87
static bool logfile_rotate_dest(bool time_based_rotation, int size_rotation_for, pg_time_t fntime, int target_dest, char **last_file_name, FILE **logFile)
Definition: syslogger.c:1257
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:128
static char * last_sys_file_name
Definition: syslogger.c:90

◆ logfile_rotate_dest()

static bool logfile_rotate_dest ( bool  time_based_rotation,
int  size_rotation_for,
pg_time_t  fntime,
int  target_dest,
char **  last_file_name,
FILE **  logFile 
)
static

Definition at line 1257 of file syslogger.c.

References Assert, ereport, errmsg(), filename, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, Log_truncate_on_rotation, logfile_getname(), logfile_open(), pfree(), and rotation_disabled.

Referenced by logfile_rotate().

1260 {
1261  char *logFileExt = NULL;
1262  char *filename;
1263  FILE *fh;
1264 
1265  /*
1266  * If the target destination was just turned off, close the previous file
1267  * and unregister its data. This cannot happen for stderr as syslogFile
1268  * is assumed to be always opened even if stderr is disabled in
1269  * log_destination.
1270  */
1271  if ((Log_destination & target_dest) == 0 &&
1272  target_dest != LOG_DESTINATION_STDERR)
1273  {
1274  if (*logFile != NULL)
1275  fclose(*logFile);
1276  *logFile = NULL;
1277  if (*last_file_name != NULL)
1278  pfree(*last_file_name);
1279  *last_file_name = NULL;
1280  return true;
1281  }
1282 
1283  /*
1284  * Leave if it is not time for a rotation or if the target destination has
1285  * no need to do a rotation based on the size of its file.
1286  */
1287  if (!time_based_rotation && (size_rotation_for & target_dest) == 0)
1288  return true;
1289 
1290  /* file extension depends on the destination type */
1291  if (target_dest == LOG_DESTINATION_STDERR)
1292  logFileExt = NULL;
1293  else if (target_dest == LOG_DESTINATION_CSVLOG)
1294  logFileExt = ".csv";
1295  else
1296  {
1297  /* cannot happen */
1298  Assert(false);
1299  }
1300 
1301  /* build the new file name */
1302  filename = logfile_getname(fntime, logFileExt);
1303 
1304  /*
1305  * Decide whether to overwrite or append. We can overwrite if (a)
1306  * Log_truncate_on_rotation is set, (b) the rotation was triggered by
1307  * elapsed time and not something else, and (c) the computed file name is
1308  * different from what we were previously logging into.
1309  */
1310  if (Log_truncate_on_rotation && time_based_rotation &&
1311  *last_file_name != NULL &&
1312  strcmp(filename, *last_file_name) != 0)
1313  fh = logfile_open(filename, "w", true);
1314  else
1315  fh = logfile_open(filename, "a", true);
1316 
1317  if (!fh)
1318  {
1319  /*
1320  * ENFILE/EMFILE are not too surprising on a busy system; just keep
1321  * using the old file till we manage to get a new one. Otherwise,
1322  * assume something's wrong with Log_directory and stop trying to
1323  * create files.
1324  */
1325  if (errno != ENFILE && errno != EMFILE)
1326  {
1327  ereport(LOG,
1328  (errmsg("disabling automatic rotation (use SIGHUP to re-enable)")));
1329  rotation_disabled = true;
1330  }
1331 
1332  if (filename)
1333  pfree(filename);
1334  return false;
1335  }
1336 
1337  /* fill in the new information */
1338  if (*logFile != NULL)
1339  fclose(*logFile);
1340  *logFile = fh;
1341 
1342  /* instead of pfree'ing filename, remember it for next time */
1343  if (*last_file_name != NULL)
1344  pfree(*last_file_name);
1345  *last_file_name = filename;
1346 
1347  return true;
1348 }
#define LOG
Definition: elog.h:26
void pfree(void *pointer)
Definition: mcxt.c:1169
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1397
static bool rotation_disabled
Definition: syslogger.c:86
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define LOG_DESTINATION_STDERR
Definition: elog.h:435
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
static char * filename
Definition: pg_dumpall.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:909
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1212
bool Log_truncate_on_rotation
Definition: syslogger.c:76
int Log_destination
Definition: elog.c:111

◆ process_pipe_input()

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

Definition at line 884 of file syslogger.c.

References appendBinaryStringInfo(), Assert, buf, StringInfoData::data, save_buffer::data, generate_unaccent_rules::dest, PipeProtoHeader::flags, initStringInfo(), lappend(), StringInfoData::len, PipeProtoHeader::len, lfirst, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, NBUFFER_LISTS, PipeProtoHeader::nuls, offsetof, palloc(), pfree(), pg_popcount(), PipeProtoHeader::pid, save_buffer::pid, PIPE_HEADER_SIZE, PIPE_MAX_PAYLOAD, PIPE_PROTO_DEST_CSVLOG, PIPE_PROTO_DEST_STDERR, PIPE_PROTO_IS_LAST, generate_unaccent_rules::str, and write_syslogger_file().

Referenced by SysLoggerMain(), and write_syslogger_file().

885 {
886  char *cursor = logbuffer;
887  int count = *bytes_in_logbuffer;
889 
890  /* While we have enough for a header, process data... */
891  while (count >= (int) (offsetof(PipeProtoHeader, data) + 1))
892  {
893  PipeProtoHeader p;
894  int chunklen;
895  bits8 dest_flags;
896 
897  /* Do we have a valid header? */
898  memcpy(&p, cursor, offsetof(PipeProtoHeader, data));
900  if (p.nuls[0] == '\0' && p.nuls[1] == '\0' &&
901  p.len > 0 && p.len <= PIPE_MAX_PAYLOAD &&
902  p.pid != 0 &&
903  pg_popcount((char *) &dest_flags, 1) == 1)
904  {
905  List *buffer_list;
906  ListCell *cell;
907  save_buffer *existing_slot = NULL,
908  *free_slot = NULL;
909  StringInfo str;
910 
911  chunklen = PIPE_HEADER_SIZE + p.len;
912 
913  /* Fall out of loop if we don't have the whole chunk yet */
914  if (count < chunklen)
915  break;
916 
917  if ((p.flags & PIPE_PROTO_DEST_STDERR) != 0)
918  dest = LOG_DESTINATION_STDERR;
919  else if ((p.flags & PIPE_PROTO_DEST_CSVLOG) != 0)
920  dest = LOG_DESTINATION_CSVLOG;
921  else
922  {
923  /* this should never happen as of the header validation */
924  Assert(false);
925  }
926 
927  /* Locate any existing buffer for this source pid */
928  buffer_list = buffer_lists[p.pid % NBUFFER_LISTS];
929  foreach(cell, buffer_list)
930  {
931  save_buffer *buf = (save_buffer *) lfirst(cell);
932 
933  if (buf->pid == p.pid)
934  {
935  existing_slot = buf;
936  break;
937  }
938  if (buf->pid == 0 && free_slot == NULL)
939  free_slot = buf;
940  }
941 
942  if ((p.flags & PIPE_PROTO_IS_LAST) == 0)
943  {
944  /*
945  * Save a complete non-final chunk in a per-pid buffer
946  */
947  if (existing_slot != NULL)
948  {
949  /* Add chunk to data from preceding chunks */
950  str = &(existing_slot->data);
952  cursor + PIPE_HEADER_SIZE,
953  p.len);
954  }
955  else
956  {
957  /* First chunk of message, save in a new buffer */
958  if (free_slot == NULL)
959  {
960  /*
961  * Need a free slot, but there isn't one in the list,
962  * so create a new one and extend the list with it.
963  */
964  free_slot = palloc(sizeof(save_buffer));
965  buffer_list = lappend(buffer_list, free_slot);
966  buffer_lists[p.pid % NBUFFER_LISTS] = buffer_list;
967  }
968  free_slot->pid = p.pid;
969  str = &(free_slot->data);
970  initStringInfo(str);
972  cursor + PIPE_HEADER_SIZE,
973  p.len);
974  }
975  }
976  else
977  {
978  /*
979  * Final chunk --- add it to anything saved for that pid, and
980  * either way write the whole thing out.
981  */
982  if (existing_slot != NULL)
983  {
984  str = &(existing_slot->data);
986  cursor + PIPE_HEADER_SIZE,
987  p.len);
988  write_syslogger_file(str->data, str->len, dest);
989  /* Mark the buffer unused, and reclaim string storage */
990  existing_slot->pid = 0;
991  pfree(str->data);
992  }
993  else
994  {
995  /* The whole message was one chunk, evidently. */
997  dest);
998  }
999  }
1000 
1001  /* Finished processing this chunk */
1002  cursor += chunklen;
1003  count -= chunklen;
1004  }
1005  else
1006  {
1007  /* Process non-protocol data */
1008 
1009  /*
1010  * Look for the start of a protocol header. If found, dump data
1011  * up to there and repeat the loop. Otherwise, dump it all and
1012  * fall out of the loop. (Note: we want to dump it all if at all
1013  * possible, so as to avoid dividing non-protocol messages across
1014  * logfiles. We expect that in many scenarios, a non-protocol
1015  * message will arrive all in one read(), and we want to respect
1016  * the read() boundary if possible.)
1017  */
1018  for (chunklen = 1; chunklen < count; chunklen++)
1019  {
1020  if (cursor[chunklen] == '\0')
1021  break;
1022  }
1023  /* fall back on the stderr log as the destination */
1024  write_syslogger_file(cursor, chunklen, LOG_DESTINATION_STDERR);
1025  cursor += chunklen;
1026  count -= chunklen;
1027  }
1028  }
1029 
1030  /* We don't have a full chunk, so left-align what remains in the buffer */
1031  if (count > 0 && cursor != logbuffer)
1032  memmove(logbuffer, cursor, count);
1033  *bytes_in_logbuffer = count;
1034 }
#define PIPE_HEADER_SIZE
Definition: syslogger.h:59
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:1094
int32 pid
Definition: syslogger.c:106
char nuls[2]
Definition: syslogger.h:46
#define PIPE_MAX_PAYLOAD
Definition: syslogger.h:60
#define PIPE_PROTO_DEST_STDERR
Definition: syslogger.h:65
uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.c:296
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NBUFFER_LISTS
Definition: syslogger.c:110
static char * buf
Definition: pg_test_fsync.c:68
Definition: type.h:130
static List * buffer_lists[NBUFFER_LISTS]
Definition: syslogger.c:111
List * lappend(List *list, void *datum)
Definition: list.c:336
StringInfoData data
Definition: syslogger.c:107
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uint8 bits8
Definition: c.h:448
#define PIPE_PROTO_IS_LAST
Definition: syslogger.h:63
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define LOG_DESTINATION_STDERR
Definition: elog.h:435
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
#define PIPE_PROTO_DEST_CSVLOG
Definition: syslogger.h:66
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: pg_list.h:50
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
#define offsetof(type, field)
Definition: c.h:727

◆ RemoveLogrotateSignalFiles()

void RemoveLogrotateSignalFiles ( void  )

Definition at line 1559 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE.

Referenced by PostmasterMain(), and sigusr1_handler().

1560 {
1561  unlink(LOGROTATE_SIGNAL_FILE);
1562 }
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:64

◆ set_next_rotation_time()

static void set_next_rotation_time ( void  )
static

Definition at line 1427 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().

1428 {
1429  pg_time_t now;
1430  struct pg_tm *tm;
1431  int rotinterval;
1432 
1433  /* nothing to do if time-based rotation is disabled */
1434  if (Log_RotationAge <= 0)
1435  return;
1436 
1437  /*
1438  * The requirements here are to choose the next time > now that is a
1439  * "multiple" of the log rotation interval. "Multiple" can be interpreted
1440  * fairly loosely. In this version we align to log_timezone rather than
1441  * GMT.
1442  */
1443  rotinterval = Log_RotationAge * SECS_PER_MINUTE; /* convert to seconds */
1444  now = (pg_time_t) time(NULL);
1445  tm = pg_localtime(&now, log_timezone);
1446  now += tm->tm_gmtoff;
1447  now -= now % rotinterval;
1448  now += rotinterval;
1449  now -= tm->tm_gmtoff;
1451 }
static pg_time_t next_rotation_time
Definition: syslogger.c:84
int64 pg_time_t
Definition: pgtime.h:23
long int tm_gmtoff
Definition: pgtime.h:43
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
pg_tz * log_timezone
Definition: pgtz.c:31
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int Log_RotationAge
Definition: syslogger.c:72
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ sigUsr1Handler()

static void sigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 1566 of file syslogger.c.

References MyLatch, rotation_requested, and SetLatch().

Referenced by SysLoggerMain().

1567 {
1568  int save_errno = errno;
1569 
1570  rotation_requested = true;
1571  SetLatch(MyLatch);
1572 
1573  errno = save_errno;
1574 }
void SetLatch(Latch *latch)
Definition: latch.c:567
struct Latch * MyLatch
Definition: globals.c:57
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:128

◆ SysLogger_Start()

int SysLogger_Start ( void  )

Definition at line 547 of file syslogger.c.

References Assert, av, close, ClosePostmasterPorts(), csvlogFile, 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_destination, LOG_DESTINATION_CSVLOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, MakePGDirectory(), pfree(), PG_IOLBF, PGSharedMemoryDetach(), redirection_done, snprintf, generate_unaccent_rules::stdout, syslogFile, SysLoggerMain(), and syslogPipe.

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

548 {
549  pid_t sysloggerPid;
550  char *filename;
551 
552  if (!Logging_collector)
553  return 0;
554 
555  /*
556  * If first time through, create the pipe which will receive stderr
557  * output.
558  *
559  * If the syslogger crashes and needs to be restarted, we continue to use
560  * the same pipe (indeed must do so, since extant backends will be writing
561  * into that pipe).
562  *
563  * This means the postmaster must continue to hold the read end of the
564  * pipe open, so we can pass it down to the reincarnated syslogger. This
565  * is a bit klugy but we have little choice.
566  *
567  * Also note that we don't bother counting the pipe FDs by calling
568  * Reserve/ReleaseExternalFD. There's no real need to account for them
569  * accurately in the postmaster or syslogger process, and both ends of the
570  * pipe will wind up closed in all other postmaster children.
571  */
572 #ifndef WIN32
573  if (syslogPipe[0] < 0)
574  {
575  if (pipe(syslogPipe) < 0)
576  ereport(FATAL,
578  errmsg("could not create pipe for syslog: %m")));
579  }
580 #else
581  if (!syslogPipe[0])
582  {
583  SECURITY_ATTRIBUTES sa;
584 
585  memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
586  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
587  sa.bInheritHandle = TRUE;
588 
589  if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
590  ereport(FATAL,
592  errmsg("could not create pipe for syslog: %m")));
593  }
594 #endif
595 
596  /*
597  * Create log directory if not present; ignore errors
598  */
600 
601  /*
602  * The initial logfile is created right in the postmaster, to verify that
603  * the Log_directory is writable. We save the reference time so that the
604  * syslogger child process can recompute this file name.
605  *
606  * It might look a bit strange to re-do this during a syslogger restart,
607  * but we must do so since the postmaster closed syslogFile after the
608  * previous fork (and remembering that old file wouldn't be right anyway).
609  * Note we always append here, we won't overwrite any existing file. This
610  * is consistent with the normal rules, because by definition this is not
611  * a time-based rotation.
612  */
613  first_syslogger_file_time = time(NULL);
614 
615  filename = logfile_getname(first_syslogger_file_time, NULL);
616 
617  syslogFile = logfile_open(filename, "a", false);
618 
619  pfree(filename);
620 
621  /*
622  * Likewise for the initial CSV log file, if that's enabled. (Note that
623  * we open syslogFile even when only CSV output is nominally enabled,
624  * since some code paths will write to syslogFile anyway.)
625  */
627  {
628  filename = logfile_getname(first_syslogger_file_time, ".csv");
629 
630  csvlogFile = logfile_open(filename, "a", false);
631 
632  pfree(filename);
633  }
634 
635 #ifdef EXEC_BACKEND
636  switch ((sysloggerPid = syslogger_forkexec()))
637 #else
638  switch ((sysloggerPid = fork_process()))
639 #endif
640  {
641  case -1:
642  ereport(LOG,
643  (errmsg("could not fork system logger: %m")));
644  return 0;
645 
646 #ifndef EXEC_BACKEND
647  case 0:
648  /* in postmaster child ... */
650 
651  /* Close the postmaster's sockets */
652  ClosePostmasterPorts(true);
653 
654  /* Drop our connection to postmaster's shared memory, as well */
655  dsm_detach_all();
657 
658  /* do the work */
659  SysLoggerMain(0, NULL);
660  break;
661 #endif
662 
663  default:
664  /* success, in postmaster */
665 
666  /* now we redirect stderr, if not done already */
667  if (!redirection_done)
668  {
669 #ifdef WIN32
670  int fd;
671 #endif
672 
673  /*
674  * Leave a breadcrumb trail when redirecting, in case the user
675  * forgets that redirection is active and looks only at the
676  * original stderr target file.
677  */
678  ereport(LOG,
679  (errmsg("redirecting log output to logging collector process"),
680  errhint("Future log output will appear in directory \"%s\".",
681  Log_directory)));
682 
683 #ifndef WIN32
684  fflush(stdout);
685  if (dup2(syslogPipe[1], fileno(stdout)) < 0)
686  ereport(FATAL,
688  errmsg("could not redirect stdout: %m")));
689  fflush(stderr);
690  if (dup2(syslogPipe[1], fileno(stderr)) < 0)
691  ereport(FATAL,
693  errmsg("could not redirect stderr: %m")));
694  /* Now we are done with the write end of the pipe. */
695  close(syslogPipe[1]);
696  syslogPipe[1] = -1;
697 #else
698 
699  /*
700  * open the pipe in binary mode and make sure stderr is binary
701  * after it's been dup'ed into, to avoid disturbing the pipe
702  * chunking protocol.
703  */
704  fflush(stderr);
705  fd = _open_osfhandle((intptr_t) syslogPipe[1],
706  _O_APPEND | _O_BINARY);
707  if (dup2(fd, _fileno(stderr)) < 0)
708  ereport(FATAL,
710  errmsg("could not redirect stderr: %m")));
711  close(fd);
712  _setmode(_fileno(stderr), _O_BINARY);
713 
714  /*
715  * Now we are done with the write end of the pipe.
716  * CloseHandle() must not be called because the preceding
717  * close() closes the underlying handle.
718  */
719  syslogPipe[1] = 0;
720 #endif
721  redirection_done = true;
722  }
723 
724  /* postmaster will never write the file(s); close 'em */
725  fclose(syslogFile);
726  syslogFile = NULL;
727  if (csvlogFile != NULL)
728  {
729  fclose(csvlogFile);
730  csvlogFile = NULL;
731  }
732  return (int) sysloggerPid;
733  }
734 
735  /* we should never reach here */
736  return 0;
737 }
NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: syslogger.c:163
int errhint(const char *fmt,...)
Definition: elog.c:1156
char * Log_directory
Definition: syslogger.c:74
void InitPostmasterChild(void)
Definition: miscinit.c:94
pid_t fork_process(void)
Definition: fork_process.c:29
static FILE * csvlogFile
Definition: syslogger.c:88
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2612
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:942
bool Logging_collector
Definition: syslogger.c:71
bool redirection_done
Definition: postmaster.c:367
void pfree(void *pointer)
Definition: mcxt.c:1169
#define FATAL
Definition: elog.h:49
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1397
int errcode_for_file_access(void)
Definition: elog.c:721
int errcode_for_socket_access(void)
Definition: elog.c:792
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define ereport(elevel,...)
Definition: elog.h:157
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3801
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition: syslogger.c:89
void dsm_detach_all(void)
Definition: dsm.c:741
static FILE * syslogFile
Definition: syslogger.c:87
static char * filename
Definition: pg_dumpall.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:909
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1212
int Log_destination
Definition: elog.c:111
#define close(a)
Definition: win32.h:12
int syslogPipe[2]
Definition: syslogger.c:115

◆ SysLoggerMain()

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

Definition at line 163 of file syslogger.c.

References AddWaitEventToSet(), B_LOGGER, close, ConfigReloadPending, CreateWaitEventSet(), csvlogFile, CurrentMemoryContext, DEBUG1, DestNone, DEVNULL, EINTR, elog, ereport, errcode_for_socket_access(), errmsg(), errmsg_internal(), WaitEvent::events, FATAL, fd(), first_syslogger_file_time, flush_pipe_input(), init_ps_display(), last_csv_file_name, last_sys_file_name, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, Log_directory, Log_filename, Log_RotationAge, Log_RotationSize, logfile_getname(), logfile_rotate(), MakePGDirectory(), MyBackendType, MyLatch, MyStartTime, next_rotation_time, now(), pfree(), PG_SETMASK, PGC_SIGHUP, PGINVALID_SOCKET, 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, SIGHUP, SignalHandlerForConfigReload(), SIGPIPE, SIGQUIT, SIGUSR1, sigUsr1Handler(), SIGUSR2, generate_unaccent_rules::stdout, syslogFile, syslogPipe, UnBlockSig, update_metainfo_datafile(), WAIT_EVENT_SYSLOGGER_MAIN, WaitEventSetWait(), whereToSendOutput, WL_LATCH_SET, and WL_SOCKET_READABLE.

Referenced by BackendRun(), and SysLogger_Start().

164 {
165 #ifndef WIN32
166  char logbuffer[READ_BUF_SIZE];
167  int bytes_in_logbuffer = 0;
168 #endif
169  char *currentLogDir;
170  char *currentLogFilename;
171  int currentLogRotationAge;
172  pg_time_t now;
173  WaitEventSet *wes;
174 
175  now = MyStartTime;
176 
177 #ifdef EXEC_BACKEND
178  syslogger_parseArgs(argc, argv);
179 #endif /* EXEC_BACKEND */
180 
182  init_ps_display(NULL);
183 
184  /*
185  * If we restarted, our stderr is already redirected into our own input
186  * pipe. This is of course pretty useless, not to mention that it
187  * interferes with detecting pipe EOF. Point stderr to /dev/null. This
188  * assumes that all interesting messages generated in the syslogger will
189  * come through elog.c and will be sent to write_syslogger_file.
190  */
191  if (redirection_done)
192  {
193  int fd = open(DEVNULL, O_WRONLY, 0);
194 
195  /*
196  * The closes might look redundant, but they are not: we want to be
197  * darn sure the pipe gets closed even if the open failed. We can
198  * survive running with stderr pointing nowhere, but we can't afford
199  * to have extra pipe input descriptors hanging around.
200  *
201  * As we're just trying to reset these to go to DEVNULL, there's not
202  * much point in checking for failure from the close/dup2 calls here,
203  * if they fail then presumably the file descriptors are closed and
204  * any writes will go into the bitbucket anyway.
205  */
206  close(fileno(stdout));
207  close(fileno(stderr));
208  if (fd != -1)
209  {
210  (void) dup2(fd, fileno(stdout));
211  (void) dup2(fd, fileno(stderr));
212  close(fd);
213  }
214  }
215 
216  /*
217  * Syslogger's own stderr can't be the syslogPipe, so set it back to text
218  * mode if we didn't just close it. (It was set to binary in
219  * SubPostmasterMain).
220  */
221 #ifdef WIN32
222  else
223  _setmode(_fileno(stderr), _O_TEXT);
224 #endif
225 
226  /*
227  * Also close our copy of the write end of the pipe. This is needed to
228  * ensure we can detect pipe EOF correctly. (But note that in the restart
229  * case, the postmaster already did this.)
230  */
231 #ifndef WIN32
232  if (syslogPipe[1] >= 0)
233  close(syslogPipe[1]);
234  syslogPipe[1] = -1;
235 #else
236  if (syslogPipe[1])
237  CloseHandle(syslogPipe[1]);
238  syslogPipe[1] = 0;
239 #endif
240 
241  /*
242  * Properly accept or ignore signals the postmaster might send us
243  *
244  * Note: we ignore all termination signals, and instead exit only when all
245  * upstream processes are gone, to ensure we don't miss any dying gasps of
246  * broken backends...
247  */
248 
249  pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config
250  * file */
251  pqsignal(SIGINT, SIG_IGN);
252  pqsignal(SIGTERM, SIG_IGN);
256  pqsignal(SIGUSR1, sigUsr1Handler); /* request log rotation */
258 
259  /*
260  * Reset some signals that are accepted by postmaster but not here
261  */
263 
265 
266 #ifdef WIN32
267  /* Fire up separate data transfer thread */
268  InitializeCriticalSection(&sysloggerSection);
269  EnterCriticalSection(&sysloggerSection);
270 
271  threadHandle = (HANDLE) _beginthreadex(NULL, 0, pipeThread, NULL, 0, NULL);
272  if (threadHandle == 0)
273  elog(FATAL, "could not create syslogger data transfer thread: %m");
274 #endif /* WIN32 */
275 
276  /*
277  * Remember active logfiles' name(s). We recompute 'em from the reference
278  * time because passing down just the pg_time_t is a lot cheaper than
279  * passing a whole file path in the EXEC_BACKEND case.
280  */
282  if (csvlogFile != NULL)
284 
285  /* remember active logfile parameters */
286  currentLogDir = pstrdup(Log_directory);
287  currentLogFilename = pstrdup(Log_filename);
288  currentLogRotationAge = Log_RotationAge;
289  /* set next planned rotation time */
292 
293  /*
294  * Reset whereToSendOutput, as the postmaster will do (but hasn't yet, at
295  * the point where we forked). This prevents duplicate output of messages
296  * from syslogger itself.
297  */
299 
300  /*
301  * Set up a reusable WaitEventSet object we'll use to wait for our latch,
302  * and (except on Windows) our socket.
303  *
304  * Unlike all other postmaster child processes, we'll ignore postmaster
305  * death because we want to collect final log output from all backends and
306  * then exit last. We'll do that by running until we see EOF on the
307  * syslog pipe, which implies that all other backends have exited
308  * (including the postmaster).
309  */
312 #ifndef WIN32
313  AddWaitEventToSet(wes, WL_SOCKET_READABLE, syslogPipe[0], NULL, NULL);
314 #endif
315 
316  /* main worker loop */
317  for (;;)
318  {
319  bool time_based_rotation = false;
320  int size_rotation_for = 0;
321  long cur_timeout;
322  WaitEvent event;
323 
324 #ifndef WIN32
325  int rc;
326 #endif
327 
328  /* Clear any already-pending wakeups */
330 
331  /*
332  * Process any requests or signals received recently.
333  */
335  {
336  ConfigReloadPending = false;
338 
339  /*
340  * Check if the log directory or filename pattern changed in
341  * postgresql.conf. If so, force rotation to make sure we're
342  * writing the logfiles in the right place.
343  */
344  if (strcmp(Log_directory, currentLogDir) != 0)
345  {
346  pfree(currentLogDir);
347  currentLogDir = pstrdup(Log_directory);
348  rotation_requested = true;
349 
350  /*
351  * Also, create new directory if not present; ignore errors
352  */
354  }
355  if (strcmp(Log_filename, currentLogFilename) != 0)
356  {
357  pfree(currentLogFilename);
358  currentLogFilename = pstrdup(Log_filename);
359  rotation_requested = true;
360  }
361 
362  /*
363  * Force a rotation if CSVLOG output was just turned on or off and
364  * we need to open or close csvlogFile accordingly.
365  */
366  if (((Log_destination & LOG_DESTINATION_CSVLOG) != 0) !=
367  (csvlogFile != NULL))
368  rotation_requested = true;
369 
370  /*
371  * If rotation time parameter changed, reset next rotation time,
372  * but don't immediately force a rotation.
373  */
374  if (currentLogRotationAge != Log_RotationAge)
375  {
376  currentLogRotationAge = Log_RotationAge;
378  }
379 
380  /*
381  * If we had a rotation-disabling failure, re-enable rotation
382  * attempts after SIGHUP, and force one immediately.
383  */
384  if (rotation_disabled)
385  {
386  rotation_disabled = false;
387  rotation_requested = true;
388  }
389 
390  /*
391  * Force rewriting last log filename when reloading configuration.
392  * Even if rotation_requested is false, log_destination may have
393  * been changed and we don't want to wait the next file rotation.
394  */
396  }
397 
399  {
400  /* Do a logfile rotation if it's time */
401  now = (pg_time_t) time(NULL);
402  if (now >= next_rotation_time)
403  rotation_requested = time_based_rotation = true;
404  }
405 
407  {
408  /* Do a rotation if file is too big */
409  if (ftell(syslogFile) >= Log_RotationSize * 1024L)
410  {
411  rotation_requested = true;
412  size_rotation_for |= LOG_DESTINATION_STDERR;
413  }
414  if (csvlogFile != NULL &&
415  ftell(csvlogFile) >= Log_RotationSize * 1024L)
416  {
417  rotation_requested = true;
418  size_rotation_for |= LOG_DESTINATION_CSVLOG;
419  }
420  }
421 
422  if (rotation_requested)
423  {
424  /*
425  * Force rotation when both values are zero. It means the request
426  * was sent by pg_rotate_logfile() or "pg_ctl logrotate".
427  */
428  if (!time_based_rotation && size_rotation_for == 0)
429  size_rotation_for = LOG_DESTINATION_STDERR | LOG_DESTINATION_CSVLOG;
430  logfile_rotate(time_based_rotation, size_rotation_for);
431  }
432 
433  /*
434  * Calculate time till next time-based rotation, so that we don't
435  * sleep longer than that. We assume the value of "now" obtained
436  * above is still close enough. Note we can't make this calculation
437  * until after calling logfile_rotate(), since it will advance
438  * next_rotation_time.
439  *
440  * Also note that we need to beware of overflow in calculation of the
441  * timeout: with large settings of Log_RotationAge, next_rotation_time
442  * could be more than INT_MAX msec in the future. In that case we'll
443  * wait no more than INT_MAX msec, and try again.
444  */
446  {
447  pg_time_t delay;
448 
449  delay = next_rotation_time - now;
450  if (delay > 0)
451  {
452  if (delay > INT_MAX / 1000)
453  delay = INT_MAX / 1000;
454  cur_timeout = delay * 1000L; /* msec */
455  }
456  else
457  cur_timeout = 0;
458  }
459  else
460  cur_timeout = -1L;
461 
462  /*
463  * Sleep until there's something to do
464  */
465 #ifndef WIN32
466  rc = WaitEventSetWait(wes, cur_timeout, &event, 1,
468 
469  if (rc == 1 && event.events == WL_SOCKET_READABLE)
470  {
471  int bytesRead;
472 
473  bytesRead = read(syslogPipe[0],
474  logbuffer + bytes_in_logbuffer,
475  sizeof(logbuffer) - bytes_in_logbuffer);
476  if (bytesRead < 0)
477  {
478  if (errno != EINTR)
479  ereport(LOG,
481  errmsg("could not read from logger pipe: %m")));
482  }
483  else if (bytesRead > 0)
484  {
485  bytes_in_logbuffer += bytesRead;
486  process_pipe_input(logbuffer, &bytes_in_logbuffer);
487  continue;
488  }
489  else
490  {
491  /*
492  * Zero bytes read when select() is saying read-ready means
493  * EOF on the pipe: that is, there are no longer any processes
494  * with the pipe write end open. Therefore, the postmaster
495  * and all backends are shut down, and we are done.
496  */
497  pipe_eof_seen = true;
498 
499  /* if there's any data left then force it out now */
500  flush_pipe_input(logbuffer, &bytes_in_logbuffer);
501  }
502  }
503 #else /* WIN32 */
504 
505  /*
506  * On Windows we leave it to a separate thread to transfer data and
507  * detect pipe EOF. The main thread just wakes up to handle SIGHUP
508  * and rotation conditions.
509  *
510  * Server code isn't generally thread-safe, so we ensure that only one
511  * of the threads is active at a time by entering the critical section
512  * whenever we're not sleeping.
513  */
514  LeaveCriticalSection(&sysloggerSection);
515 
516  (void) WaitEventSetWait(wes, cur_timeout, &event, 1,
518 
519  EnterCriticalSection(&sysloggerSection);
520 #endif /* WIN32 */
521 
522  if (pipe_eof_seen)
523  {
524  /*
525  * seeing this message on the real stderr is annoying - so we make
526  * it DEBUG1 to suppress in normal use.
527  */
528  ereport(DEBUG1,
529  (errmsg_internal("logger shutting down")));
530 
531  /*
532  * Normal exit from the syslogger is here. Note that we
533  * deliberately do not close syslogFile before exiting; this is to
534  * allow for the possibility of elog messages being generated
535  * inside proc_exit. Regular exit() will take care of flushing
536  * and closing stdio channels.
537  */
538  proc_exit(0);
539  }
540  }
541 }
static void logfile_rotate(bool time_based_rotation, int size_rotation_for)
Definition: syslogger.c:1354
static pg_time_t next_rotation_time
Definition: syslogger.c:84
#define SIGQUIT
Definition: win32_port.h:168
#define DEBUG1
Definition: elog.h:25
int64 pg_time_t
Definition: pgtime.h:23
char * Log_directory
Definition: syslogger.c:74
void ProcessConfigFile(GucContext context)
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:862
#define SIGUSR1
Definition: win32_port.h:179
BackendType MyBackendType
Definition: miscinit.c:62
#define SIGCHLD
Definition: win32_port.h:177
#define READ_BUF_SIZE
Definition: syslogger.c:61
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
char * pstrdup(const char *in)
Definition: mcxt.c:1299
static FILE * csvlogFile
Definition: syslogger.c:88
pg_time_t MyStartTime
Definition: globals.c:44
void proc_exit(int code)
Definition: ipc.c:104
#define WL_SOCKET_READABLE
Definition: latch.h:126
#define SIGPIPE
Definition: win32_port.h:172
#define SIGUSR2
Definition: win32_port.h:180
static void set_next_rotation_time(void)
Definition: syslogger.c:1427
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void ResetLatch(Latch *latch)
Definition: latch.c:660
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
int Log_RotationSize
Definition: syslogger.c:73
bool redirection_done
Definition: postmaster.c:367
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:684
void pfree(void *pointer)
Definition: mcxt.c:1169
#define FATAL
Definition: elog.h:49
uint32 events
Definition: latch.h:145
static void sigUsr1Handler(SIGNAL_ARGS)
Definition: syslogger.c:1566
static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
Definition: syslogger.c:884
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1397
Definition: dest.h:89
static bool rotation_disabled
Definition: syslogger.c:86
#define SIGHUP
Definition: win32_port.h:167
sigset_t UnBlockSig
Definition: pqsignal.c:22
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define DEVNULL
Definition: port.h:148
static char * last_csv_file_name
Definition: syslogger.c:91
int errcode_for_socket_access(void)
Definition: elog.c:792
Definition: guc.h:72
#define SIG_IGN
Definition: win32_port.h:164
#define PGINVALID_SOCKET
Definition: port.h:33
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define LOG_DESTINATION_STDERR
Definition: elog.h:435
#define ereport(elevel,...)
Definition: elog.h:157
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3801
int Log_RotationAge
Definition: syslogger.c:72
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
#define SIG_DFL
Definition: win32_port.h:162
#define SIGALRM
Definition: win32_port.h:173
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition: syslogger.c:89
static void update_metainfo_datafile(void)
Definition: syslogger.c:1462
static FILE * syslogFile
Definition: syslogger.c:87
char * Log_filename
Definition: syslogger.c:75
static bool pipe_eof_seen
Definition: syslogger.c:85
static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
Definition: syslogger.c:1043
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
struct Latch * MyLatch
Definition: globals.c:57
int Log_destination
Definition: elog.c:111
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:258
#define close(a)
Definition: win32.h:12
#define EINTR
Definition: win32_port.h:351
CommandDest whereToSendOutput
Definition: postgres.c:92
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:128
static char * last_sys_file_name
Definition: syslogger.c:90
#define WL_LATCH_SET
Definition: latch.h:125
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544
#define read(a, b, c)
Definition: win32.h:13
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1306
int syslogPipe[2]
Definition: syslogger.c:115

◆ update_metainfo_datafile()

static void update_metainfo_datafile ( void  )
static

Definition at line 1462 of file syslogger.c.

References ereport, errcode_for_file_access(), errmsg(), fprintf, last_csv_file_name, last_sys_file_name, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_STDERR, LOG_METAINFO_DATAFILE, LOG_METAINFO_DATAFILE_TMP, PG_IOLBF, and pg_mode_mask.

Referenced by logfile_rotate(), and SysLoggerMain().

1463 {
1464  FILE *fh;
1465  mode_t oumask;
1466 
1469  {
1470  if (unlink(LOG_METAINFO_DATAFILE) < 0 && errno != ENOENT)
1471  ereport(LOG,
1473  errmsg("could not remove file \"%s\": %m",
1475  return;
1476  }
1477 
1478  /* use the same permissions as the data directory for the new file */
1479  oumask = umask(pg_mode_mask);
1480  fh = fopen(LOG_METAINFO_DATAFILE_TMP, "w");
1481  umask(oumask);
1482 
1483  if (fh)
1484  {
1485  setvbuf(fh, NULL, PG_IOLBF, 0);
1486 
1487 #ifdef WIN32
1488  /* use CRLF line endings on Windows */
1489  _setmode(_fileno(fh), _O_TEXT);
1490 #endif
1491  }
1492  else
1493  {
1494  ereport(LOG,
1496  errmsg("could not open file \"%s\": %m",
1498  return;
1499  }
1500 
1501  if (last_sys_file_name && (Log_destination & LOG_DESTINATION_STDERR))
1502  {
1503  if (fprintf(fh, "stderr %s\n", last_sys_file_name) < 0)
1504  {
1505  ereport(LOG,
1507  errmsg("could not write file \"%s\": %m",
1509  fclose(fh);
1510  return;
1511  }
1512  }
1513 
1514  if (last_csv_file_name && (Log_destination & LOG_DESTINATION_CSVLOG))
1515  {
1516  if (fprintf(fh, "csvlog %s\n", last_csv_file_name) < 0)
1517  {
1518  ereport(LOG,
1520  errmsg("could not write file \"%s\": %m",
1522  fclose(fh);
1523  return;
1524  }
1525  }
1526  fclose(fh);
1527 
1529  ereport(LOG,
1531  errmsg("could not rename file \"%s\" to \"%s\": %m",
1533 }
#define LOG_METAINFO_DATAFILE
Definition: syslogger.h:99
#define LOG
Definition: elog.h:26
#define fprintf
Definition: port.h:221
int errcode_for_file_access(void)
Definition: elog.c:721
static char * last_csv_file_name
Definition: syslogger.c:91
#define PG_IOLBF
Definition: port.h:345
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define LOG_DESTINATION_STDERR
Definition: elog.h:435
#define ereport(elevel,...)
Definition: elog.h:157
#define LOG_METAINFO_DATAFILE_TMP
Definition: syslogger.h:100
int errmsg(const char *fmt,...)
Definition: elog.c:909
int Log_destination
Definition: elog.c:111
static char * last_sys_file_name
Definition: syslogger.c:90
int pg_mode_mask
Definition: file_perm.c:25

◆ write_syslogger_file()

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

Definition at line 1094 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, 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().

1095 {
1096  int rc;
1097  FILE *logfile;
1098 
1099  /*
1100  * If we're told to write to csvlogFile, but it's not open, dump the data
1101  * to syslogFile (which is always open) instead. This can happen if CSV
1102  * output is enabled after postmaster start and we've been unable to open
1103  * csvlogFile. There are also race conditions during a parameter change
1104  * whereby backends might send us CSV output before we open csvlogFile or
1105  * after we close it. Writing CSV-formatted output to the regular log
1106  * file isn't great, but it beats dropping log output on the floor.
1107  *
1108  * Think not to improve this by trying to open csvlogFile on-the-fly. Any
1109  * failure in that would lead to recursion.
1110  */
1111  logfile = (destination == LOG_DESTINATION_CSVLOG &&
1112  csvlogFile != NULL) ? csvlogFile : syslogFile;
1113 
1114  rc = fwrite(buffer, 1, count, logfile);
1115 
1116  /*
1117  * Try to report any failure. We mustn't use ereport because it would
1118  * just recurse right back here, but write_stderr is OK: it will write
1119  * either to the postmaster's original stderr, or to /dev/null, but never
1120  * to our input pipe which would result in a different sort of looping.
1121  */
1122  if (rc != count)
1123  write_stderr("could not write to log file: %s\n", strerror(errno));
1124 }
static FILE * csvlogFile
Definition: syslogger.c:88
#define write_stderr(str)
Definition: parallel.c:186
static FILE * logfile
Definition: pg_regress.c:102
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:438
#define strerror
Definition: port.h:230
static FILE * syslogFile
Definition: syslogger.c:87

Variable Documentation

◆ buffer_lists

List* buffer_lists[NBUFFER_LISTS]
static

Definition at line 111 of file syslogger.c.

◆ csvlogFile

FILE* csvlogFile = NULL
static

Definition at line 88 of file syslogger.c.

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

◆ first_syslogger_file_time

NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0

Definition at line 89 of file syslogger.c.

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

◆ last_csv_file_name

char* last_csv_file_name = NULL
static

Definition at line 91 of file syslogger.c.

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

◆ last_sys_file_name

char* last_sys_file_name = NULL
static

Definition at line 90 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 77 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

char* Log_filename = NULL

Definition at line 75 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 72 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

int Log_RotationSize = 10 * 1024

Definition at line 73 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ Log_truncate_on_rotation

bool Log_truncate_on_rotation = false

Definition at line 76 of file syslogger.c.

Referenced by logfile_rotate_dest().

◆ Logging_collector

bool Logging_collector = false

Definition at line 71 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 84 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 85 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ redirection_done

bool redirection_done

Definition at line 367 of file postmaster.c.

Referenced by SysLogger_Start(), and SysLoggerMain().

◆ rotation_disabled

bool rotation_disabled = false
static

Definition at line 86 of file syslogger.c.

Referenced by logfile_rotate_dest(), and SysLoggerMain().

◆ rotation_requested

volatile sig_atomic_t rotation_requested = false
static

Definition at line 128 of file syslogger.c.

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

◆ syslogFile

FILE* syslogFile = NULL
static

Definition at line 87 of file syslogger.c.

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

◆ syslogPipe

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