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)
 

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 94 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 95 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

◆ SysLogger_Start()

int SysLogger_Start ( void  )

Definition at line 521 of file syslogger.c.

References Assert, av, close, ClosePostmasterPorts(), 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_directory, logfile_getname(), logfile_open(), Logging_collector, MakePGDirectory(), pfree(), PG_IOLBF, PGSharedMemoryDetach(), redirection_done, snprintf(), syslogFile, SysLoggerMain(), syslogPipe, and TRUE.

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

522 {
523  pid_t sysloggerPid;
524  char *filename;
525 
526  if (!Logging_collector)
527  return 0;
528 
529  /*
530  * If first time through, create the pipe which will receive stderr
531  * output.
532  *
533  * If the syslogger crashes and needs to be restarted, we continue to use
534  * the same pipe (indeed must do so, since extant backends will be writing
535  * into that pipe).
536  *
537  * This means the postmaster must continue to hold the read end of the
538  * pipe open, so we can pass it down to the reincarnated syslogger. This
539  * is a bit klugy but we have little choice.
540  */
541 #ifndef WIN32
542  if (syslogPipe[0] < 0)
543  {
544  if (pipe(syslogPipe) < 0)
545  ereport(FATAL,
547  (errmsg("could not create pipe for syslog: %m"))));
548  }
549 #else
550  if (!syslogPipe[0])
551  {
552  SECURITY_ATTRIBUTES sa;
553 
554  memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
555  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
556  sa.bInheritHandle = TRUE;
557 
558  if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
559  ereport(FATAL,
561  (errmsg("could not create pipe for syslog: %m"))));
562  }
563 #endif
564 
565  /*
566  * Create log directory if not present; ignore errors
567  */
569 
570  /*
571  * The initial logfile is created right in the postmaster, to verify that
572  * the Log_directory is writable. We save the reference time so that the
573  * syslogger child process can recompute this file name.
574  *
575  * It might look a bit strange to re-do this during a syslogger restart,
576  * but we must do so since the postmaster closed syslogFile after the
577  * previous fork (and remembering that old file wouldn't be right anyway).
578  * Note we always append here, we won't overwrite any existing file. This
579  * is consistent with the normal rules, because by definition this is not
580  * a time-based rotation.
581  */
582  first_syslogger_file_time = time(NULL);
583  filename = logfile_getname(first_syslogger_file_time, NULL);
584 
585  syslogFile = logfile_open(filename, "a", false);
586 
587  pfree(filename);
588 
589 #ifdef EXEC_BACKEND
590  switch ((sysloggerPid = syslogger_forkexec()))
591 #else
592  switch ((sysloggerPid = fork_process()))
593 #endif
594  {
595  case -1:
596  ereport(LOG,
597  (errmsg("could not fork system logger: %m")));
598  return 0;
599 
600 #ifndef EXEC_BACKEND
601  case 0:
602  /* in postmaster child ... */
604 
605  /* Close the postmaster's sockets */
606  ClosePostmasterPorts(true);
607 
608  /* Drop our connection to postmaster's shared memory, as well */
609  dsm_detach_all();
611 
612  /* do the work */
613  SysLoggerMain(0, NULL);
614  break;
615 #endif
616 
617  default:
618  /* success, in postmaster */
619 
620  /* now we redirect stderr, if not done already */
621  if (!redirection_done)
622  {
623 #ifdef WIN32
624  int fd;
625 #endif
626 
627  /*
628  * Leave a breadcrumb trail when redirecting, in case the user
629  * forgets that redirection is active and looks only at the
630  * original stderr target file.
631  */
632  ereport(LOG,
633  (errmsg("redirecting log output to logging collector process"),
634  errhint("Future log output will appear in directory \"%s\".",
635  Log_directory)));
636 
637 #ifndef WIN32
638  fflush(stdout);
639  if (dup2(syslogPipe[1], fileno(stdout)) < 0)
640  ereport(FATAL,
642  errmsg("could not redirect stdout: %m")));
643  fflush(stderr);
644  if (dup2(syslogPipe[1], fileno(stderr)) < 0)
645  ereport(FATAL,
647  errmsg("could not redirect stderr: %m")));
648  /* Now we are done with the write end of the pipe. */
649  close(syslogPipe[1]);
650  syslogPipe[1] = -1;
651 #else
652 
653  /*
654  * open the pipe in binary mode and make sure stderr is binary
655  * after it's been dup'ed into, to avoid disturbing the pipe
656  * chunking protocol.
657  */
658  fflush(stderr);
659  fd = _open_osfhandle((intptr_t) syslogPipe[1],
660  _O_APPEND | _O_BINARY);
661  if (dup2(fd, _fileno(stderr)) < 0)
662  ereport(FATAL,
664  errmsg("could not redirect stderr: %m")));
665  close(fd);
666  _setmode(_fileno(stderr), _O_BINARY);
667 
668  /*
669  * Now we are done with the write end of the pipe.
670  * CloseHandle() must not be called because the preceding
671  * close() closes the underlying handle.
672  */
673  syslogPipe[1] = 0;
674 #endif
675  redirection_done = true;
676  }
677 
678  /* postmaster will never write the file; close it */
679  fclose(syslogFile);
680  syslogFile = NULL;
681  return (int) sysloggerPid;
682  }
683 
684  /* we should never reach here */
685  return 0;
686 }
NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: syslogger.c:158
#define TRUE
Definition: ecpglib.h:35
int errhint(const char *fmt,...)
Definition: elog.c:987
char * Log_directory
Definition: syslogger.c:67
void InitPostmasterChild(void)
Definition: miscinit.c:271
pid_t fork_process(void)
Definition: fork_process.c:31
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2448
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:790
bool Logging_collector
Definition: syslogger.c:64
bool redirection_done
Definition: postmaster.c:354
void pfree(void *pointer)
Definition: mcxt.c:1031
#define FATAL
Definition: elog.h:52
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
Definition: syslogger.c:1298
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
int errcode_for_socket_access(void)
Definition: elog.c:669
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3571
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition: syslogger.c:87
void dsm_detach_all(void)
Definition: dsm.c:653
static FILE * syslogFile
Definition: syslogger.c:85
static char * filename
Definition: pg_dumpall.c:87
int errmsg(const char *fmt,...)
Definition: elog.c:797
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
Definition: syslogger.c:1123
#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 996 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, open_csvlogfile(), 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().

997 {
998  int rc;
999  FILE *logfile;
1000 
1001  if (destination == LOG_DESTINATION_CSVLOG && csvlogFile == NULL)
1002  open_csvlogfile();
1003 
1004  logfile = destination == LOG_DESTINATION_CSVLOG ? csvlogFile : syslogFile;
1005  rc = fwrite(buffer, 1, count, logfile);
1006 
1007  /* can't use ereport here because of possible recursion */
1008  if (rc != count)
1009  write_stderr("could not write to log file: %s\n", strerror(errno));
1010 }
static FILE * csvlogFile
Definition: syslogger.c:86
#define write_stderr(str)
Definition: parallel.c:182
static void open_csvlogfile(void)
Definition: syslogger.c:1099
static FILE * logfile
Definition: pg_regress.c:100
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
static FILE * syslogFile
Definition: syslogger.c:85
const char * strerror(int errnum)
Definition: strerror.c:19

Variable Documentation

◆ am_syslogger

bool am_syslogger

Definition at line 75 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 70 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

◆ Log_filename

PGDLLIMPORT char* Log_filename

Definition at line 68 of file syslogger.c.

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

◆ Log_RotationAge

int Log_RotationAge

Definition at line 65 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

◆ Log_RotationSize

int Log_RotationSize

Definition at line 66 of file syslogger.c.

Referenced by SysLoggerMain(), and write_syslogger_file().

◆ Log_truncate_on_rotation

bool Log_truncate_on_rotation

Definition at line 69 of file syslogger.c.

Referenced by logfile_rotate().

◆ Logging_collector

bool Logging_collector

Definition at line 64 of file syslogger.c.

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

◆ syslogPipe