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
 
int syslogPipe [2]
 

Macro Definition Documentation

◆ LOG_METAINFO_DATAFILE

#define LOG_METAINFO_DATAFILE   "current_logfiles"

Definition at line 95 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 96 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 1537 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE, and stat.

Referenced by sigusr1_handler().

1538 {
1539  struct stat stat_buf;
1540 
1541  if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
1542  return true;
1543 
1544  return false;
1545 }
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 1551 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE.

Referenced by PostmasterMain(), and sigusr1_handler().

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

◆ SysLogger_Start()

int SysLogger_Start ( void  )

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

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

◆ write_syslogger_file()

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

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

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

Variable Documentation

◆ 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