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

Go to the source code of this file.

Data Structures

struct  save_buffer
 

Macros

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

Functions

NON_EXEC_STATIC void SysLoggerMain (int argc, char *argv[]) pg_attribute_noreturn()
 
static void process_pipe_input (char *logbuffer, int *bytes_in_logbuffer)
 
static void flush_pipe_input (char *logbuffer, int *bytes_in_logbuffer)
 
static void open_csvlogfile (void)
 
static FILE * logfile_open (const char *filename, const char *mode, bool allow_errors)
 
static void logfile_rotate (bool time_based_rotation, int size_rotation_for)
 
static char * logfile_getname (pg_time_t timestamp, const char *suffix)
 
static void set_next_rotation_time (void)
 
static void sigHupHandler (SIGNAL_ARGS)
 
static void sigUsr1Handler (SIGNAL_ARGS)
 
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

#define NBUFFER_LISTS   256

Definition at line 107 of file syslogger.c.

Referenced by flush_pipe_input(), and process_pipe_input().

#define READ_BUF_SIZE   (2 * PIPE_CHUNK_SIZE)

Definition at line 56 of file syslogger.c.

Referenced by SysLoggerMain().

Function Documentation

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

Definition at line 944 of file syslogger.c.

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

Referenced by SysLoggerMain().

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

Definition at line 1297 of file syslogger.c.

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

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

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

Definition at line 1122 of file syslogger.c.

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

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

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

Definition at line 1162 of file syslogger.c.

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

Referenced by SysLoggerMain().

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

Definition at line 1098 of file syslogger.c.

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

Referenced by write_syslogger_file().

1099 {
1100  char *filename;
1101 
1102  filename = logfile_getname(time(NULL), ".csv");
1103 
1104  csvlogFile = logfile_open(filename, "a", false);
1105 
1106  if (last_csv_file_name != NULL) /* probably shouldn't happen */
1108 
1110 
1112 }
static FILE * csvlogFile
Definition: syslogger.c:85
void pfree(void *pointer)
Definition: mcxt.c:950
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1297
static char * last_csv_file_name
Definition: syslogger.c:88
#define NULL
Definition: c.h:229
static void update_metainfo_datafile(void)
Definition: syslogger.c:1361
static char * filename
Definition: pg_dumpall.c:87
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1122
static void process_pipe_input ( char *  logbuffer,
int *  bytes_in_logbuffer 
)
static

Definition at line 793 of file syslogger.c.

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

Referenced by SysLoggerMain().

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

Definition at line 1327 of file syslogger.c.

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

Referenced by logfile_rotate(), and SysLoggerMain().

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

Definition at line 1426 of file syslogger.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by SysLoggerMain().

1427 {
1428  int save_errno = errno;
1429 
1430  got_SIGHUP = true;
1431  SetLatch(MyLatch);
1432 
1433  errno = save_errno;
1434 }
static volatile sig_atomic_t got_SIGHUP
Definition: syslogger.c:125
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
struct Latch * MyLatch
Definition: globals.c:51
static void sigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 1438 of file syslogger.c.

References MyLatch, rotation_requested, and SetLatch().

Referenced by SysLoggerMain().

1439 {
1440  int save_errno = errno;
1441 
1442  rotation_requested = true;
1443  SetLatch(MyLatch);
1444 
1445  errno = save_errno;
1446 }
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
struct Latch * MyLatch
Definition: globals.c:51
static volatile sig_atomic_t rotation_requested
Definition: syslogger.c:126
int SysLogger_Start ( void  )

Definition at line 520 of file syslogger.c.

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

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

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

Definition at line 157 of file syslogger.c.

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

Referenced by SysLogger_Start().

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

Definition at line 1361 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, logfile_open(), NULL, and unlink().

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

1362 {
1363  FILE *fh;
1364 
1367  {
1368  if (unlink(LOG_METAINFO_DATAFILE) < 0 && errno != ENOENT)
1369  ereport(LOG,
1371  errmsg("could not remove file \"%s\": %m",
1373  return;
1374  }
1375 
1376  if ((fh = logfile_open(LOG_METAINFO_DATAFILE_TMP, "w", true)) == NULL)
1377  {
1378  ereport(LOG,
1380  errmsg("could not open file \"%s\": %m",
1382  return;
1383  }
1384 
1385  if (last_file_name && (Log_destination & LOG_DESTINATION_STDERR))
1386  {
1387  if (fprintf(fh, "stderr %s\n", last_file_name) < 0)
1388  {
1389  ereport(LOG,
1391  errmsg("could not write file \"%s\": %m",
1393  fclose(fh);
1394  return;
1395  }
1396  }
1397 
1398  if (last_csv_file_name && (Log_destination & LOG_DESTINATION_CSVLOG))
1399  {
1400  if (fprintf(fh, "csvlog %s\n", last_csv_file_name) < 0)
1401  {
1402  ereport(LOG,
1404  errmsg("could not write file \"%s\": %m",
1406  fclose(fh);
1407  return;
1408  }
1409  }
1410  fclose(fh);
1411 
1413  ereport(LOG,
1415  errmsg("could not rename file \"%s\" to \"%s\": %m",
1417 }
#define LOG_METAINFO_DATAFILE
Definition: syslogger.h:94
#define LOG
Definition: elog.h:26
static char * last_file_name
Definition: syslogger.c:87
int errcode_for_file_access(void)
Definition: elog.c:598
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
static char * last_csv_file_name
Definition: syslogger.c:88
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define LOG_DESTINATION_STDERR
Definition: elog.h:395
#define NULL
Definition: c.h:229
#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:1122
int Log_destination
Definition: elog.c:106
void write_syslogger_file ( const char *  buffer,
int  count,
int  destination 
)

Definition at line 995 of file syslogger.c.

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

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

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

Variable Documentation

bool am_syslogger = false

Definition at line 74 of file syslogger.c.

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

List* buffer_lists[NBUFFER_LISTS]
static

Definition at line 108 of file syslogger.c.

FILE* csvlogFile = NULL
static

Definition at line 85 of file syslogger.c.

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

NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0

Definition at line 86 of file syslogger.c.

Referenced by SysLogger_Start(), and SysLoggerMain().

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 125 of file syslogger.c.

Referenced by sigHupHandler(), and SysLoggerMain().

char* last_csv_file_name = NULL
static

Definition at line 88 of file syslogger.c.

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

char* last_file_name = NULL
static

Definition at line 87 of file syslogger.c.

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

char* Log_directory = NULL
int Log_file_mode = S_IRUSR | S_IWUSR

Definition at line 69 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

char* Log_filename = NULL

Definition at line 67 of file syslogger.c.

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

int Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR

Definition at line 64 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

int Log_RotationSize = 10 * 1024

Definition at line 65 of file syslogger.c.

Referenced by SysLoggerMain().

bool Log_truncate_on_rotation = false

Definition at line 68 of file syslogger.c.

Referenced by logfile_rotate().

bool Logging_collector = false

Definition at line 63 of file syslogger.c.

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

pg_time_t next_rotation_time
static

Definition at line 81 of file syslogger.c.

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

bool pipe_eof_seen = false
static

Definition at line 82 of file syslogger.c.

Referenced by SysLoggerMain().

bool redirection_done

Definition at line 351 of file postmaster.c.

Referenced by SysLogger_Start(), and SysLoggerMain().

bool rotation_disabled = false
static

Definition at line 83 of file syslogger.c.

Referenced by logfile_rotate(), and SysLoggerMain().

volatile sig_atomic_t rotation_requested = false
static

Definition at line 126 of file syslogger.c.

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

FILE* syslogFile = NULL
static

Definition at line 84 of file syslogger.c.

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

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

Definition at line 112 of file syslogger.c.

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