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 PIPE_PROTO_IS_LAST   0x01 /* last chunk of message? */
 
#define PIPE_PROTO_DEST_STDERR   0x10
 
#define PIPE_PROTO_DEST_CSVLOG   0x20
 
#define PIPE_PROTO_DEST_JSONLOG   0x40
 
#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 destination)
 
bool CheckLogrotateSignal (void)
 
void RemoveLogrotateSignalFiles (void)
 

Variables

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

Macro Definition Documentation

◆ LOG_METAINFO_DATAFILE

#define LOG_METAINFO_DATAFILE   "current_logfiles"

Definition at line 100 of file syslogger.h.

◆ LOG_METAINFO_DATAFILE_TMP

#define LOG_METAINFO_DATAFILE_TMP   LOG_METAINFO_DATAFILE ".tmp"

Definition at line 101 of file syslogger.h.

◆ 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 59 of file syslogger.h.

◆ PIPE_MAX_PAYLOAD

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

Definition at line 60 of file syslogger.h.

◆ PIPE_PROTO_DEST_CSVLOG

#define PIPE_PROTO_DEST_CSVLOG   0x20

Definition at line 66 of file syslogger.h.

◆ PIPE_PROTO_DEST_JSONLOG

#define PIPE_PROTO_DEST_JSONLOG   0x40

Definition at line 67 of file syslogger.h.

◆ PIPE_PROTO_DEST_STDERR

#define PIPE_PROTO_DEST_STDERR   0x10

Definition at line 65 of file syslogger.h.

◆ PIPE_PROTO_IS_LAST

#define PIPE_PROTO_IS_LAST   0x01 /* last chunk of message? */

Definition at line 63 of file syslogger.h.

Function Documentation

◆ CheckLogrotateSignal()

bool CheckLogrotateSignal ( void  )

Definition at line 1621 of file syslogger.c.

1622 {
1623  struct stat stat_buf;
1624 
1625  if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
1626  return true;
1627 
1628  return false;
1629 }
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:63
#define stat
Definition: win32_port.h:284

References LOGROTATE_SIGNAL_FILE, and stat.

Referenced by process_pm_pmsignal().

◆ RemoveLogrotateSignalFiles()

void RemoveLogrotateSignalFiles ( void  )

Definition at line 1635 of file syslogger.c.

1636 {
1637  unlink(LOGROTATE_SIGNAL_FILE);
1638 }

References LOGROTATE_SIGNAL_FILE.

Referenced by PostmasterMain(), and process_pm_pmsignal().

◆ SysLogger_Start()

int SysLogger_Start ( void  )

Definition at line 566 of file syslogger.c.

567 {
568  pid_t sysloggerPid;
569  char *filename;
570 
571  if (!Logging_collector)
572  return 0;
573 
574  /*
575  * If first time through, create the pipe which will receive stderr
576  * output.
577  *
578  * If the syslogger crashes and needs to be restarted, we continue to use
579  * the same pipe (indeed must do so, since extant backends will be writing
580  * into that pipe).
581  *
582  * This means the postmaster must continue to hold the read end of the
583  * pipe open, so we can pass it down to the reincarnated syslogger. This
584  * is a bit klugy but we have little choice.
585  *
586  * Also note that we don't bother counting the pipe FDs by calling
587  * Reserve/ReleaseExternalFD. There's no real need to account for them
588  * accurately in the postmaster or syslogger process, and both ends of the
589  * pipe will wind up closed in all other postmaster children.
590  */
591 #ifndef WIN32
592  if (syslogPipe[0] < 0)
593  {
594  if (pipe(syslogPipe) < 0)
595  ereport(FATAL,
597  errmsg("could not create pipe for syslog: %m")));
598  }
599 #else
600  if (!syslogPipe[0])
601  {
602  SECURITY_ATTRIBUTES sa;
603 
604  memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
605  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
606  sa.bInheritHandle = TRUE;
607 
608  if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
609  ereport(FATAL,
611  errmsg("could not create pipe for syslog: %m")));
612  }
613 #endif
614 
615  /*
616  * Create log directory if not present; ignore errors
617  */
619 
620  /*
621  * The initial logfile is created right in the postmaster, to verify that
622  * the Log_directory is writable. We save the reference time so that the
623  * syslogger child process can recompute this file name.
624  *
625  * It might look a bit strange to re-do this during a syslogger restart,
626  * but we must do so since the postmaster closed syslogFile after the
627  * previous fork (and remembering that old file wouldn't be right anyway).
628  * Note we always append here, we won't overwrite any existing file. This
629  * is consistent with the normal rules, because by definition this is not
630  * a time-based rotation.
631  */
632  first_syslogger_file_time = time(NULL);
633 
635 
636  syslogFile = logfile_open(filename, "a", false);
637 
638  pfree(filename);
639 
640  /*
641  * Likewise for the initial CSV log file, if that's enabled. (Note that
642  * we open syslogFile even when only CSV output is nominally enabled,
643  * since some code paths will write to syslogFile anyway.)
644  */
646  {
648 
649  csvlogFile = logfile_open(filename, "a", false);
650 
651  pfree(filename);
652  }
653 
654  /*
655  * Likewise for the initial JSON log file, if that's enabled. (Note that
656  * we open syslogFile even when only JSON output is nominally enabled,
657  * since some code paths will write to syslogFile anyway.)
658  */
660  {
662 
663  jsonlogFile = logfile_open(filename, "a", false);
664 
665  pfree(filename);
666  }
667 
668 #ifdef EXEC_BACKEND
669  switch ((sysloggerPid = syslogger_forkexec()))
670 #else
671  switch ((sysloggerPid = fork_process()))
672 #endif
673  {
674  case -1:
675  ereport(LOG,
676  (errmsg("could not fork system logger: %m")));
677  return 0;
678 
679 #ifndef EXEC_BACKEND
680  case 0:
681  /* in postmaster child ... */
683 
684  /* Close the postmaster's sockets */
685  ClosePostmasterPorts(true);
686 
687  /* Drop our connection to postmaster's shared memory, as well */
688  dsm_detach_all();
690 
691  /* do the work */
692  SysLoggerMain(0, NULL);
693  break;
694 #endif
695 
696  default:
697  /* success, in postmaster */
698 
699  /* now we redirect stderr, if not done already */
700  if (!redirection_done)
701  {
702 #ifdef WIN32
703  int fd;
704 #endif
705 
706  /*
707  * Leave a breadcrumb trail when redirecting, in case the user
708  * forgets that redirection is active and looks only at the
709  * original stderr target file.
710  */
711  ereport(LOG,
712  (errmsg("redirecting log output to logging collector process"),
713  errhint("Future log output will appear in directory \"%s\".",
714  Log_directory)));
715 
716 #ifndef WIN32
717  fflush(stdout);
718  if (dup2(syslogPipe[1], STDOUT_FILENO) < 0)
719  ereport(FATAL,
721  errmsg("could not redirect stdout: %m")));
722  fflush(stderr);
723  if (dup2(syslogPipe[1], STDERR_FILENO) < 0)
724  ereport(FATAL,
726  errmsg("could not redirect stderr: %m")));
727  /* Now we are done with the write end of the pipe. */
728  close(syslogPipe[1]);
729  syslogPipe[1] = -1;
730 #else
731 
732  /*
733  * open the pipe in binary mode and make sure stderr is binary
734  * after it's been dup'ed into, to avoid disturbing the pipe
735  * chunking protocol.
736  */
737  fflush(stderr);
738  fd = _open_osfhandle((intptr_t) syslogPipe[1],
739  _O_APPEND | _O_BINARY);
740  if (dup2(fd, STDERR_FILENO) < 0)
741  ereport(FATAL,
743  errmsg("could not redirect stderr: %m")));
744  close(fd);
745  _setmode(STDERR_FILENO, _O_BINARY);
746 
747  /*
748  * Now we are done with the write end of the pipe.
749  * CloseHandle() must not be called because the preceding
750  * close() closes the underlying handle.
751  */
752  syslogPipe[1] = 0;
753 #endif
754  redirection_done = true;
755  }
756 
757  /* postmaster will never write the file(s); close 'em */
758  fclose(syslogFile);
759  syslogFile = NULL;
760  if (csvlogFile != NULL)
761  {
762  fclose(csvlogFile);
763  csvlogFile = NULL;
764  }
765  if (jsonlogFile != NULL)
766  {
767  fclose(jsonlogFile);
768  jsonlogFile = NULL;
769  }
770  return (int) sysloggerPid;
771  }
772 
773  /* we should never reach here */
774  return 0;
775 }
void dsm_detach_all(void)
Definition: dsm.c:775
int errcode_for_socket_access(void)
Definition: elog.c:955
int errcode_for_file_access(void)
Definition: elog.c:882
int Log_destination
Definition: elog.c:112
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define LOG
Definition: elog.h:31
#define FATAL
Definition: elog.h:41
#define LOG_DESTINATION_JSONLOG
Definition: elog.h:496
#define ereport(elevel,...)
Definition: elog.h:149
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:495
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3913
pid_t fork_process(void)
Definition: fork_process.c:32
#define close(a)
Definition: win32.h:12
static void const char fflush(stdout)
void pfree(void *pointer)
Definition: mcxt.c:1401
void InitPostmasterChild(void)
Definition: miscinit.c:95
static char * filename
Definition: pg_dumpall.c:121
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2500
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char * Log_directory
Definition: syslogger.c:73
bool redirection_done
Definition: postmaster.c:360
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1459
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1266
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition: syslogger.c:89
int syslogPipe[2]
Definition: syslogger.c:116
static FILE * syslogFile
Definition: syslogger.c:86
bool Logging_collector
Definition: syslogger.c:70
static FILE * csvlogFile
Definition: syslogger.c:87
static FILE * jsonlogFile
Definition: syslogger.c:88
NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: syslogger.c:164
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:970
#define STDOUT_FILENO
Definition: unistd.h:8
#define STDERR_FILENO
Definition: unistd.h:9

References close, ClosePostmasterPorts(), csvlogFile, dsm_detach_all(), ereport, errcode_for_file_access(), errcode_for_socket_access(), errhint(), errmsg(), FATAL, fd(), fflush(), filename, first_syslogger_file_time, fork_process(), InitPostmasterChild(), jsonlogFile, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_JSONLOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, MakePGDirectory(), pfree(), PGSharedMemoryDetach(), redirection_done, STDERR_FILENO, generate_unaccent_rules::stdout, STDOUT_FILENO, syslogFile, SysLoggerMain(), and syslogPipe.

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

◆ write_syslogger_file()

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

Definition at line 1142 of file syslogger.c.

1143 {
1144  int rc;
1145  FILE *logfile;
1146 
1147  /*
1148  * If we're told to write to a structured log file, but it's not open,
1149  * dump the data to syslogFile (which is always open) instead. This can
1150  * happen if structured output is enabled after postmaster start and we've
1151  * been unable to open logFile. There are also race conditions during a
1152  * parameter change whereby backends might send us structured output
1153  * before we open the logFile or after we close it. Writing formatted
1154  * output to the regular log file isn't great, but it beats dropping log
1155  * output on the floor.
1156  *
1157  * Think not to improve this by trying to open logFile on-the-fly. Any
1158  * failure in that would lead to recursion.
1159  */
1160  if ((destination & LOG_DESTINATION_CSVLOG) && csvlogFile != NULL)
1161  logfile = csvlogFile;
1162  else if ((destination & LOG_DESTINATION_JSONLOG) && jsonlogFile != NULL)
1163  logfile = jsonlogFile;
1164  else
1165  logfile = syslogFile;
1166 
1167  rc = fwrite(buffer, 1, count, logfile);
1168 
1169  /*
1170  * Try to report any failure. We mustn't use ereport because it would
1171  * just recurse right back here, but write_stderr is OK: it will write
1172  * either to the postmaster's original stderr, or to /dev/null, but never
1173  * to our input pipe which would result in a different sort of looping.
1174  */
1175  if (rc != count)
1176  write_stderr("could not write to log file: %s\n", strerror(errno));
1177 }
#define write_stderr(str)
Definition: parallel.c:184
static FILE * logfile
Definition: pg_regress.c:127
#define strerror
Definition: port.h:251

References csvlogFile, jsonlogFile, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_JSONLOG, logfile, strerror, syslogFile, and write_stderr.

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

Variable Documentation

◆ Log_directory

PGDLLIMPORT char* Log_directory
extern

◆ Log_file_mode

PGDLLIMPORT int Log_file_mode
extern

Definition at line 76 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

PGDLLIMPORT char* Log_filename
extern

Definition at line 74 of file syslogger.c.

Referenced by logfile_getname(), and SysLoggerMain().

◆ Log_RotationAge

PGDLLIMPORT int Log_RotationAge
extern

Definition at line 71 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

PGDLLIMPORT int Log_RotationSize
extern

Definition at line 72 of file syslogger.c.

Referenced by SysLoggerMain().

◆ Log_truncate_on_rotation

PGDLLIMPORT bool Log_truncate_on_rotation
extern

Definition at line 75 of file syslogger.c.

Referenced by logfile_rotate_dest().

◆ Logging_collector

PGDLLIMPORT bool Logging_collector
extern

Definition at line 70 of file syslogger.c.

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

◆ syslogPipe

PGDLLIMPORT int syslogPipe[2]
extern

Definition at line 116 of file syslogger.c.

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