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

References LOGROTATE_SIGNAL_FILE, and stat.

Referenced by sigusr1_handler().

1537 {
1538  struct stat stat_buf;
1539 
1540  if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
1541  return true;
1542 
1543  return false;
1544 }
struct stat stat_buf
Definition: pg_standby.c:100
#define LOGROTATE_SIGNAL_FILE
Definition: syslogger.c:63
#define stat
Definition: win32_port.h:275

◆ RemoveLogrotateSignalFiles()

void RemoveLogrotateSignalFiles ( void  )

Definition at line 1550 of file syslogger.c.

References LOGROTATE_SIGNAL_FILE.

Referenced by PostmasterMain(), and sigusr1_handler().

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

◆ SysLogger_Start()

int SysLogger_Start ( void  )

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

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

◆ write_syslogger_file()

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

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

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

Variable Documentation

◆ Log_directory

◆ Log_file_mode

int Log_file_mode

Definition at line 76 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

PGDLLIMPORT char* Log_filename

Definition at line 74 of file syslogger.c.

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

◆ Log_RotationAge

int Log_RotationAge

Definition at line 71 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

int Log_RotationSize

Definition at line 72 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ Log_truncate_on_rotation

bool Log_truncate_on_rotation

Definition at line 75 of file syslogger.c.

Referenced by logfile_rotate().

◆ Logging_collector

bool Logging_collector

Definition at line 70 of file syslogger.c.

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

◆ syslogPipe