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))
 

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 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 511 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, NULL, pfree(), PGSharedMemoryDetach(), redirection_done, syslogFile, SysLoggerMain(), syslogPipe, and TRUE.

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

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

Definition at line 986 of file syslogger.c.

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

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

987 {
988  int rc;
989  FILE *logfile;
990 
991  if (destination == LOG_DESTINATION_CSVLOG && csvlogFile == NULL)
992  open_csvlogfile();
993 
994  logfile = destination == LOG_DESTINATION_CSVLOG ? csvlogFile : syslogFile;
995  rc = fwrite(buffer, 1, count, logfile);
996 
997  /* can't use ereport here because of possible recursion */
998  if (rc != count)
999  write_stderr("could not write to log file: %s\n", strerror(errno));
1000 }
static FILE * csvlogFile
Definition: syslogger.c:85
#define write_stderr(str)
Definition: parallel.c:180
static void open_csvlogfile(void)
Definition: syslogger.c:1089
static FILE * logfile
Definition: pg_regress.c:97
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:398
#define NULL
Definition: c.h:226
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().

PGDLLIMPORT char* Log_directory
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().