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 1622 of file syslogger.c.

1623 {
1624  struct stat stat_buf;
1625 
1626  if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
1627  return true;
1628 
1629  return false;
1630 }
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:64
#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 1636 of file syslogger.c.

1637 {
1638  unlink(LOGROTATE_SIGNAL_FILE);
1639 }

References LOGROTATE_SIGNAL_FILE.

Referenced by PostmasterMain(), and process_pm_pmsignal().

◆ SysLogger_Start()

int SysLogger_Start ( void  )

Definition at line 567 of file syslogger.c.

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

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

◆ Log_file_mode

PGDLLIMPORT int Log_file_mode
extern

Definition at line 77 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

PGDLLIMPORT char* Log_filename
extern

Definition at line 75 of file syslogger.c.

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

◆ Log_RotationAge

PGDLLIMPORT int Log_RotationAge
extern

Definition at line 72 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

PGDLLIMPORT int Log_RotationSize
extern

Definition at line 73 of file syslogger.c.

Referenced by SysLoggerMain().

◆ Log_truncate_on_rotation

PGDLLIMPORT bool Log_truncate_on_rotation
extern

Definition at line 76 of file syslogger.c.

Referenced by logfile_rotate_dest().

◆ Logging_collector

PGDLLIMPORT bool Logging_collector
extern

Definition at line 71 of file syslogger.c.

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

◆ syslogPipe

PGDLLIMPORT int syslogPipe[2]
extern

Definition at line 117 of file syslogger.c.

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