PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

#define LOG_METAINFO_DATAFILE   "current_logfiles"

Definition at line 94 of file syslogger.h.

Referenced by pg_current_logfile(), PostmasterMain(), and update_metainfo_datafile().

#define LOG_METAINFO_DATAFILE_TMP   LOG_METAINFO_DATAFILE ".tmp"

Definition at line 95 of file syslogger.h.

Referenced by update_metainfo_datafile().

#define PIPE_CHUNK_SIZE   512

Definition at line 41 of file syslogger.h.

#define PIPE_HEADER_SIZE   offsetof(PipeProtoHeader, data)

Definition at line 60 of file syslogger.h.

Referenced by process_pipe_input(), and write_pipe_chunks().

#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

int SysLogger_Start ( void  )

Definition at line 520 of file syslogger.c.

References 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(), LOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, mkdir, pfree(), PGSharedMemoryDetach(), redirection_done, syslogFile, SysLoggerMain(), syslogPipe, and TRUE.

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

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

Definition at line 995 of file syslogger.c.

References csvlogFile, LOG_DESTINATION_CSVLOG, logfile, open_csvlogfile(), strerror(), syslogFile, and write_stderr.

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

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

Variable Documentation

bool am_syslogger

Definition at line 74 of file syslogger.c.

Referenced by send_message_to_server_log(), SysLoggerMain(), and write_csvlog().

int Log_file_mode

Definition at line 69 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

PGDLLIMPORT char* Log_filename

Definition at line 67 of file syslogger.c.

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

int Log_RotationAge

Definition at line 64 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

int Log_RotationSize

Definition at line 65 of file syslogger.c.

Referenced by SysLoggerMain().

bool Log_truncate_on_rotation

Definition at line 68 of file syslogger.c.

Referenced by logfile_rotate().

bool Logging_collector

Definition at line 63 of file syslogger.c.

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

int syslogPipe[2]

Definition at line 112 of file syslogger.c.

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