PostgreSQL Source Code  git master
syslogger.h File Reference
#include <limits.h>
Include dependency graph for syslogger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PipeProtoHeader
 
union  PipeProtoChunk
 

Macros

#define PIPE_CHUNK_SIZE   512
 
#define PIPE_HEADER_SIZE   offsetof(PipeProtoHeader, data)
 
#define PIPE_MAX_PAYLOAD   ((int) (PIPE_CHUNK_SIZE - PIPE_HEADER_SIZE))
 
#define LOG_METAINFO_DATAFILE   "current_logfiles"
 
#define LOG_METAINFO_DATAFILE_TMP   LOG_METAINFO_DATAFILE ".tmp"
 

Functions

int SysLogger_Start (void)
 
void write_syslogger_file (const char *buffer, int count, int dest)
 
bool CheckLogrotateSignal (void)
 
void RemoveLogrotateSignalFiles (void)
 

Variables

bool Logging_collector
 
int Log_RotationAge
 
int Log_RotationSize
 
PGDLLIMPORT char * Log_directory
 
PGDLLIMPORT char * Log_filename
 
bool Log_truncate_on_rotation
 
int Log_file_mode
 
bool am_syslogger
 
int syslogPipe [2]
 

Macro Definition Documentation

◆ LOG_METAINFO_DATAFILE

#define LOG_METAINFO_DATAFILE   "current_logfiles"

Definition at line 97 of file syslogger.h.

Referenced by pg_current_logfile(), PostmasterMain(), and update_metainfo_datafile().

◆ LOG_METAINFO_DATAFILE_TMP

#define LOG_METAINFO_DATAFILE_TMP   LOG_METAINFO_DATAFILE ".tmp"

Definition at line 98 of file syslogger.h.

Referenced by update_metainfo_datafile().

◆ PIPE_CHUNK_SIZE

#define PIPE_CHUNK_SIZE   512

Definition at line 41 of file syslogger.h.

◆ PIPE_HEADER_SIZE

#define PIPE_HEADER_SIZE   offsetof(PipeProtoHeader, data)

Definition at line 60 of file syslogger.h.

Referenced by process_pipe_input(), and write_pipe_chunks().

◆ PIPE_MAX_PAYLOAD

#define PIPE_MAX_PAYLOAD   ((int) (PIPE_CHUNK_SIZE - PIPE_HEADER_SIZE))

Definition at line 61 of file syslogger.h.

Referenced by process_pipe_input(), and write_pipe_chunks().

Function Documentation

◆ CheckLogrotateSignal()

bool CheckLogrotateSignal ( void  )

Definition at line 1538 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE, and stat.

Referenced by sigusr1_handler().

1539 {
1540  struct stat stat_buf;
1541 
1542  if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
1543  return true;
1544 
1545  return false;
1546 }
struct stat stat_buf
Definition: pg_standby.c:100
#define stat(a, b)
Definition: win32_port.h:255
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:62

◆ RemoveLogrotateSignalFiles()

void RemoveLogrotateSignalFiles ( void  )

Definition at line 1552 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE.

Referenced by PostmasterMain(), and sigusr1_handler().

1553 {
1554  unlink(LOGROTATE_SIGNAL_FILE);
1555 }
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:62

◆ SysLogger_Start()

int SysLogger_Start ( void  )

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

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

◆ write_syslogger_file()

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

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

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

Variable Documentation

◆ am_syslogger

bool am_syslogger

Definition at line 80 of file syslogger.c.

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

◆ Log_directory

◆ Log_file_mode

int Log_file_mode

Definition at line 75 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

PGDLLIMPORT char* Log_filename

Definition at line 73 of file syslogger.c.

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

◆ Log_RotationAge

int Log_RotationAge

Definition at line 70 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

int Log_RotationSize

Definition at line 71 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ Log_truncate_on_rotation

bool Log_truncate_on_rotation

Definition at line 74 of file syslogger.c.

Referenced by logfile_rotate().

◆ Logging_collector

bool Logging_collector

Definition at line 69 of file syslogger.c.

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

◆ syslogPipe