LCOV - code coverage report
Current view: top level - src/backend/postmaster - syslogger.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 207 373 55.5 %
Date: 2019-08-24 15:07:19 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * syslogger.c
       4             :  *
       5             :  * The system logger (syslogger) appeared in Postgres 8.0. It catches all
       6             :  * stderr output from the postmaster, backends, and other subprocesses
       7             :  * by redirecting to a pipe, and writes it to a set of logfiles.
       8             :  * It's possible to have size and age limits for the logfile configured
       9             :  * in postgresql.conf. If these limits are reached or passed, the
      10             :  * current logfile is closed and a new one is created (rotated).
      11             :  * The logfiles are stored in a subdirectory (configurable in
      12             :  * postgresql.conf), using a user-selectable naming scheme.
      13             :  *
      14             :  * Author: Andreas Pflug <pgadmin@pse-consulting.de>
      15             :  *
      16             :  * Copyright (c) 2004-2019, PostgreSQL Global Development Group
      17             :  *
      18             :  *
      19             :  * IDENTIFICATION
      20             :  *    src/backend/postmaster/syslogger.c
      21             :  *
      22             :  *-------------------------------------------------------------------------
      23             :  */
      24             : #include "postgres.h"
      25             : 
      26             : #include <fcntl.h>
      27             : #include <limits.h>
      28             : #include <signal.h>
      29             : #include <time.h>
      30             : #include <unistd.h>
      31             : #include <sys/stat.h>
      32             : #include <sys/time.h>
      33             : 
      34             : #include "common/file_perm.h"
      35             : #include "lib/stringinfo.h"
      36             : #include "libpq/pqsignal.h"
      37             : #include "miscadmin.h"
      38             : #include "nodes/pg_list.h"
      39             : #include "pgstat.h"
      40             : #include "pgtime.h"
      41             : #include "postmaster/fork_process.h"
      42             : #include "postmaster/postmaster.h"
      43             : #include "postmaster/syslogger.h"
      44             : #include "storage/dsm.h"
      45             : #include "storage/fd.h"
      46             : #include "storage/ipc.h"
      47             : #include "storage/latch.h"
      48             : #include "storage/pg_shmem.h"
      49             : #include "tcop/tcopprot.h"
      50             : #include "utils/guc.h"
      51             : #include "utils/ps_status.h"
      52             : #include "utils/timestamp.h"
      53             : 
      54             : /*
      55             :  * We read() into a temp buffer twice as big as a chunk, so that any fragment
      56             :  * left after processing can be moved down to the front and we'll still have
      57             :  * room to read a full chunk.
      58             :  */
      59             : #define READ_BUF_SIZE (2 * PIPE_CHUNK_SIZE)
      60             : 
      61             : /* Log rotation signal file path, relative to $PGDATA */
      62             : #define LOGROTATE_SIGNAL_FILE   "logrotate"
      63             : 
      64             : 
      65             : /*
      66             :  * GUC parameters.  Logging_collector cannot be changed after postmaster
      67             :  * start, but the rest can change at SIGHUP.
      68             :  */
      69             : bool        Logging_collector = false;
      70             : int         Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR;
      71             : int         Log_RotationSize = 10 * 1024;
      72             : char       *Log_directory = NULL;
      73             : char       *Log_filename = NULL;
      74             : bool        Log_truncate_on_rotation = false;
      75             : int         Log_file_mode = S_IRUSR | S_IWUSR;
      76             : 
      77             : /*
      78             :  * Globally visible state (used by elog.c)
      79             :  */
      80             : bool        am_syslogger = false;
      81             : 
      82             : extern bool redirection_done;
      83             : 
      84             : /*
      85             :  * Private state
      86             :  */
      87             : static pg_time_t next_rotation_time;
      88             : static bool pipe_eof_seen = false;
      89             : static bool rotation_disabled = false;
      90             : static FILE *syslogFile = NULL;
      91             : static FILE *csvlogFile = NULL;
      92             : NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0;
      93             : static char *last_file_name = NULL;
      94             : static char *last_csv_file_name = NULL;
      95             : 
      96             : /*
      97             :  * Buffers for saving partial messages from different backends.
      98             :  *
      99             :  * Keep NBUFFER_LISTS lists of these, with the entry for a given source pid
     100             :  * being in the list numbered (pid % NBUFFER_LISTS), so as to cut down on
     101             :  * the number of entries we have to examine for any one incoming message.
     102             :  * There must never be more than one entry for the same source pid.
     103             :  *
     104             :  * An inactive buffer is not removed from its list, just held for re-use.
     105             :  * An inactive buffer has pid == 0 and undefined contents of data.
     106             :  */
     107             : typedef struct
     108             : {
     109             :     int32       pid;            /* PID of source process */
     110             :     StringInfoData data;        /* accumulated data, as a StringInfo */
     111             : } save_buffer;
     112             : 
     113             : #define NBUFFER_LISTS 256
     114             : static List *buffer_lists[NBUFFER_LISTS];
     115             : 
     116             : /* These must be exported for EXEC_BACKEND case ... annoying */
     117             : #ifndef WIN32
     118             : int         syslogPipe[2] = {-1, -1};
     119             : #else
     120             : HANDLE      syslogPipe[2] = {0, 0};
     121             : #endif
     122             : 
     123             : #ifdef WIN32
     124             : static HANDLE threadHandle = 0;
     125             : static CRITICAL_SECTION sysloggerSection;
     126             : #endif
     127             : 
     128             : /*
     129             :  * Flags set by interrupt handlers for later service in the main loop.
     130             :  */
     131             : static volatile sig_atomic_t got_SIGHUP = false;
     132             : static volatile sig_atomic_t rotation_requested = false;
     133             : 
     134             : 
     135             : /* Local subroutines */
     136             : #ifdef EXEC_BACKEND
     137             : static pid_t syslogger_forkexec(void);
     138             : static void syslogger_parseArgs(int argc, char *argv[]);
     139             : #endif
     140             : NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn();
     141             : static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer);
     142             : static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer);
     143             : static FILE *logfile_open(const char *filename, const char *mode,
     144             :                           bool allow_errors);
     145             : 
     146             : #ifdef WIN32
     147             : static unsigned int __stdcall pipeThread(void *arg);
     148             : #endif
     149             : static void logfile_rotate(bool time_based_rotation, int size_rotation_for);
     150             : static char *logfile_getname(pg_time_t timestamp, const char *suffix);
     151             : static void set_next_rotation_time(void);
     152             : static void sigHupHandler(SIGNAL_ARGS);
     153             : static void sigUsr1Handler(SIGNAL_ARGS);
     154             : static void update_metainfo_datafile(void);
     155             : 
     156             : 
     157             : /*
     158             :  * Main entry point for syslogger process
     159             :  * argc/argv parameters are valid only in EXEC_BACKEND case.
     160             :  */
     161             : NON_EXEC_STATIC void
     162           2 : SysLoggerMain(int argc, char *argv[])
     163             : {
     164             : #ifndef WIN32
     165             :     char        logbuffer[READ_BUF_SIZE];
     166           2 :     int         bytes_in_logbuffer = 0;
     167             : #endif
     168             :     char       *currentLogDir;
     169             :     char       *currentLogFilename;
     170             :     int         currentLogRotationAge;
     171             :     pg_time_t   now;
     172             :     WaitEventSet *wes;
     173             : 
     174           2 :     now = MyStartTime;
     175             : 
     176             : #ifdef EXEC_BACKEND
     177             :     syslogger_parseArgs(argc, argv);
     178             : #endif                          /* EXEC_BACKEND */
     179             : 
     180           2 :     am_syslogger = true;
     181             : 
     182           2 :     init_ps_display("logger", "", "", "");
     183             : 
     184             :     /*
     185             :      * If we restarted, our stderr is already redirected into our own input
     186             :      * pipe.  This is of course pretty useless, not to mention that it
     187             :      * interferes with detecting pipe EOF.  Point stderr to /dev/null. This
     188             :      * assumes that all interesting messages generated in the syslogger will
     189             :      * come through elog.c and will be sent to write_syslogger_file.
     190             :      */
     191           2 :     if (redirection_done)
     192             :     {
     193           0 :         int         fd = open(DEVNULL, O_WRONLY, 0);
     194             : 
     195             :         /*
     196             :          * The closes might look redundant, but they are not: we want to be
     197             :          * darn sure the pipe gets closed even if the open failed.  We can
     198             :          * survive running with stderr pointing nowhere, but we can't afford
     199             :          * to have extra pipe input descriptors hanging around.
     200             :          *
     201             :          * As we're just trying to reset these to go to DEVNULL, there's not
     202             :          * much point in checking for failure from the close/dup2 calls here,
     203             :          * if they fail then presumably the file descriptors are closed and
     204             :          * any writes will go into the bitbucket anyway.
     205             :          */
     206           0 :         close(fileno(stdout));
     207           0 :         close(fileno(stderr));
     208           0 :         if (fd != -1)
     209             :         {
     210           0 :             (void) dup2(fd, fileno(stdout));
     211           0 :             (void) dup2(fd, fileno(stderr));
     212           0 :             close(fd);
     213             :         }
     214             :     }
     215             : 
     216             :     /*
     217             :      * Syslogger's own stderr can't be the syslogPipe, so set it back to text
     218             :      * mode if we didn't just close it. (It was set to binary in
     219             :      * SubPostmasterMain).
     220             :      */
     221             : #ifdef WIN32
     222             :     else
     223             :         _setmode(_fileno(stderr), _O_TEXT);
     224             : #endif
     225             : 
     226             :     /*
     227             :      * Also close our copy of the write end of the pipe.  This is needed to
     228             :      * ensure we can detect pipe EOF correctly.  (But note that in the restart
     229             :      * case, the postmaster already did this.)
     230             :      */
     231             : #ifndef WIN32
     232           2 :     if (syslogPipe[1] >= 0)
     233           2 :         close(syslogPipe[1]);
     234           2 :     syslogPipe[1] = -1;
     235             : #else
     236             :     if (syslogPipe[1])
     237             :         CloseHandle(syslogPipe[1]);
     238             :     syslogPipe[1] = 0;
     239             : #endif
     240             : 
     241             :     /*
     242             :      * Properly accept or ignore signals the postmaster might send us
     243             :      *
     244             :      * Note: we ignore all termination signals, and instead exit only when all
     245             :      * upstream processes are gone, to ensure we don't miss any dying gasps of
     246             :      * broken backends...
     247             :      */
     248             : 
     249           2 :     pqsignal(SIGHUP, sigHupHandler);    /* set flag to read config file */
     250           2 :     pqsignal(SIGINT, SIG_IGN);
     251           2 :     pqsignal(SIGTERM, SIG_IGN);
     252           2 :     pqsignal(SIGQUIT, SIG_IGN);
     253           2 :     pqsignal(SIGALRM, SIG_IGN);
     254           2 :     pqsignal(SIGPIPE, SIG_IGN);
     255           2 :     pqsignal(SIGUSR1, sigUsr1Handler);  /* request log rotation */
     256           2 :     pqsignal(SIGUSR2, SIG_IGN);
     257             : 
     258             :     /*
     259             :      * Reset some signals that are accepted by postmaster but not here
     260             :      */
     261           2 :     pqsignal(SIGCHLD, SIG_DFL);
     262             : 
     263           2 :     PG_SETMASK(&UnBlockSig);
     264             : 
     265             : #ifdef WIN32
     266             :     /* Fire up separate data transfer thread */
     267             :     InitializeCriticalSection(&sysloggerSection);
     268             :     EnterCriticalSection(&sysloggerSection);
     269             : 
     270             :     threadHandle = (HANDLE) _beginthreadex(NULL, 0, pipeThread, NULL, 0, NULL);
     271             :     if (threadHandle == 0)
     272             :         elog(FATAL, "could not create syslogger data transfer thread: %m");
     273             : #endif                          /* WIN32 */
     274             : 
     275             :     /*
     276             :      * Remember active logfiles' name(s).  We recompute 'em from the reference
     277             :      * time because passing down just the pg_time_t is a lot cheaper than
     278             :      * passing a whole file path in the EXEC_BACKEND case.
     279             :      */
     280           2 :     last_file_name = logfile_getname(first_syslogger_file_time, NULL);
     281           2 :     if (csvlogFile != NULL)
     282           0 :         last_csv_file_name = logfile_getname(first_syslogger_file_time, ".csv");
     283             : 
     284             :     /* remember active logfile parameters */
     285           2 :     currentLogDir = pstrdup(Log_directory);
     286           2 :     currentLogFilename = pstrdup(Log_filename);
     287           2 :     currentLogRotationAge = Log_RotationAge;
     288             :     /* set next planned rotation time */
     289           2 :     set_next_rotation_time();
     290           2 :     update_metainfo_datafile();
     291             : 
     292             :     /*
     293             :      * Reset whereToSendOutput, as the postmaster will do (but hasn't yet, at
     294             :      * the point where we forked).  This prevents duplicate output of messages
     295             :      * from syslogger itself.
     296             :      */
     297           2 :     whereToSendOutput = DestNone;
     298             : 
     299             :     /*
     300             :      * Set up a reusable WaitEventSet object we'll use to wait for our latch,
     301             :      * and (except on Windows) our socket.
     302             :      *
     303             :      * Unlike all other postmaster child processes, we'll ignore postmaster
     304             :      * death because we want to collect final log output from all backends and
     305             :      * then exit last.  We'll do that by running until we see EOF on the
     306             :      * syslog pipe, which implies that all other backends have exited
     307             :      * (including the postmaster).
     308             :      */
     309           2 :     wes = CreateWaitEventSet(CurrentMemoryContext, 2);
     310           2 :     AddWaitEventToSet(wes, WL_LATCH_SET, PGINVALID_SOCKET, MyLatch, NULL);
     311             : #ifndef WIN32
     312           2 :     AddWaitEventToSet(wes, WL_SOCKET_READABLE, syslogPipe[0], NULL, NULL);
     313             : #endif
     314             : 
     315             :     /* main worker loop */
     316             :     for (;;)
     317          22 :     {
     318          24 :         bool        time_based_rotation = false;
     319          24 :         int         size_rotation_for = 0;
     320             :         long        cur_timeout;
     321             :         WaitEvent   event;
     322             : 
     323             : #ifndef WIN32
     324             :         int         rc;
     325             : #endif
     326             : 
     327             :         /* Clear any already-pending wakeups */
     328          24 :         ResetLatch(MyLatch);
     329             : 
     330             :         /*
     331             :          * Process any requests or signals received recently.
     332             :          */
     333          24 :         if (got_SIGHUP)
     334             :         {
     335           0 :             got_SIGHUP = false;
     336           0 :             ProcessConfigFile(PGC_SIGHUP);
     337             : 
     338             :             /*
     339             :              * Check if the log directory or filename pattern changed in
     340             :              * postgresql.conf. If so, force rotation to make sure we're
     341             :              * writing the logfiles in the right place.
     342             :              */
     343           0 :             if (strcmp(Log_directory, currentLogDir) != 0)
     344             :             {
     345           0 :                 pfree(currentLogDir);
     346           0 :                 currentLogDir = pstrdup(Log_directory);
     347           0 :                 rotation_requested = true;
     348             : 
     349             :                 /*
     350             :                  * Also, create new directory if not present; ignore errors
     351             :                  */
     352           0 :                 (void) MakePGDirectory(Log_directory);
     353             :             }
     354           0 :             if (strcmp(Log_filename, currentLogFilename) != 0)
     355             :             {
     356           0 :                 pfree(currentLogFilename);
     357           0 :                 currentLogFilename = pstrdup(Log_filename);
     358           0 :                 rotation_requested = true;
     359             :             }
     360             : 
     361             :             /*
     362             :              * Force a rotation if CSVLOG output was just turned on or off and
     363             :              * we need to open or close csvlogFile accordingly.
     364             :              */
     365           0 :             if (((Log_destination & LOG_DESTINATION_CSVLOG) != 0) !=
     366             :                 (csvlogFile != NULL))
     367           0 :                 rotation_requested = true;
     368             : 
     369             :             /*
     370             :              * If rotation time parameter changed, reset next rotation time,
     371             :              * but don't immediately force a rotation.
     372             :              */
     373           0 :             if (currentLogRotationAge != Log_RotationAge)
     374             :             {
     375           0 :                 currentLogRotationAge = Log_RotationAge;
     376           0 :                 set_next_rotation_time();
     377             :             }
     378             : 
     379             :             /*
     380             :              * If we had a rotation-disabling failure, re-enable rotation
     381             :              * attempts after SIGHUP, and force one immediately.
     382             :              */
     383           0 :             if (rotation_disabled)
     384             :             {
     385           0 :                 rotation_disabled = false;
     386           0 :                 rotation_requested = true;
     387             :             }
     388             : 
     389             :             /*
     390             :              * Force rewriting last log filename when reloading configuration.
     391             :              * Even if rotation_requested is false, log_destination may have
     392             :              * been changed and we don't want to wait the next file rotation.
     393             :              */
     394           0 :             update_metainfo_datafile();
     395             :         }
     396             : 
     397          24 :         if (Log_RotationAge > 0 && !rotation_disabled)
     398             :         {
     399             :             /* Do a logfile rotation if it's time */
     400          24 :             now = (pg_time_t) time(NULL);
     401          24 :             if (now >= next_rotation_time)
     402           0 :                 rotation_requested = time_based_rotation = true;
     403             :         }
     404             : 
     405          24 :         if (!rotation_requested && Log_RotationSize > 0 && !rotation_disabled)
     406             :         {
     407             :             /* Do a rotation if file is too big */
     408          22 :             if (ftell(syslogFile) >= Log_RotationSize * 1024L)
     409             :             {
     410           0 :                 rotation_requested = true;
     411           0 :                 size_rotation_for |= LOG_DESTINATION_STDERR;
     412             :             }
     413          22 :             if (csvlogFile != NULL &&
     414           0 :                 ftell(csvlogFile) >= Log_RotationSize * 1024L)
     415             :             {
     416           0 :                 rotation_requested = true;
     417           0 :                 size_rotation_for |= LOG_DESTINATION_CSVLOG;
     418             :             }
     419             :         }
     420             : 
     421          24 :         if (rotation_requested)
     422             :         {
     423             :             /*
     424             :              * Force rotation when both values are zero. It means the request
     425             :              * was sent by pg_rotate_logfile() or "pg_ctl logrotate".
     426             :              */
     427           2 :             if (!time_based_rotation && size_rotation_for == 0)
     428           2 :                 size_rotation_for = LOG_DESTINATION_STDERR | LOG_DESTINATION_CSVLOG;
     429           2 :             logfile_rotate(time_based_rotation, size_rotation_for);
     430             :         }
     431             : 
     432             :         /*
     433             :          * Calculate time till next time-based rotation, so that we don't
     434             :          * sleep longer than that.  We assume the value of "now" obtained
     435             :          * above is still close enough.  Note we can't make this calculation
     436             :          * until after calling logfile_rotate(), since it will advance
     437             :          * next_rotation_time.
     438             :          *
     439             :          * Also note that we need to beware of overflow in calculation of the
     440             :          * timeout: with large settings of Log_RotationAge, next_rotation_time
     441             :          * could be more than INT_MAX msec in the future.  In that case we'll
     442             :          * wait no more than INT_MAX msec, and try again.
     443             :          */
     444          24 :         if (Log_RotationAge > 0 && !rotation_disabled)
     445          24 :         {
     446             :             pg_time_t   delay;
     447             : 
     448          24 :             delay = next_rotation_time - now;
     449          24 :             if (delay > 0)
     450             :             {
     451          24 :                 if (delay > INT_MAX / 1000)
     452           0 :                     delay = INT_MAX / 1000;
     453          24 :                 cur_timeout = delay * 1000L;    /* msec */
     454             :             }
     455             :             else
     456           0 :                 cur_timeout = 0;
     457             :         }
     458             :         else
     459           0 :             cur_timeout = -1L;
     460             : 
     461             :         /*
     462             :          * Sleep until there's something to do
     463             :          */
     464             : #ifndef WIN32
     465          24 :         rc = WaitEventSetWait(wes, cur_timeout, &event, 1,
     466             :                               WAIT_EVENT_SYSLOGGER_MAIN);
     467             : 
     468          24 :         if (rc == 1 && event.events == WL_SOCKET_READABLE)
     469             :         {
     470             :             int         bytesRead;
     471             : 
     472          22 :             bytesRead = read(syslogPipe[0],
     473             :                              logbuffer + bytes_in_logbuffer,
     474             :                              sizeof(logbuffer) - bytes_in_logbuffer);
     475          22 :             if (bytesRead < 0)
     476             :             {
     477           0 :                 if (errno != EINTR)
     478           0 :                     ereport(LOG,
     479             :                             (errcode_for_socket_access(),
     480             :                              errmsg("could not read from logger pipe: %m")));
     481             :             }
     482          22 :             else if (bytesRead > 0)
     483             :             {
     484          20 :                 bytes_in_logbuffer += bytesRead;
     485          20 :                 process_pipe_input(logbuffer, &bytes_in_logbuffer);
     486          20 :                 continue;
     487             :             }
     488             :             else
     489             :             {
     490             :                 /*
     491             :                  * Zero bytes read when select() is saying read-ready means
     492             :                  * EOF on the pipe: that is, there are no longer any processes
     493             :                  * with the pipe write end open.  Therefore, the postmaster
     494             :                  * and all backends are shut down, and we are done.
     495             :                  */
     496           2 :                 pipe_eof_seen = true;
     497             : 
     498             :                 /* if there's any data left then force it out now */
     499           2 :                 flush_pipe_input(logbuffer, &bytes_in_logbuffer);
     500             :             }
     501             :         }
     502             : #else                           /* WIN32 */
     503             : 
     504             :         /*
     505             :          * On Windows we leave it to a separate thread to transfer data and
     506             :          * detect pipe EOF.  The main thread just wakes up to handle SIGHUP
     507             :          * and rotation conditions.
     508             :          *
     509             :          * Server code isn't generally thread-safe, so we ensure that only one
     510             :          * of the threads is active at a time by entering the critical section
     511             :          * whenever we're not sleeping.
     512             :          */
     513             :         LeaveCriticalSection(&sysloggerSection);
     514             : 
     515             :         (void) WaitEventSetWait(wes, cur_timeout, &event, 1,
     516             :                                 WAIT_EVENT_SYSLOGGER_MAIN);
     517             : 
     518             :         EnterCriticalSection(&sysloggerSection);
     519             : #endif                          /* WIN32 */
     520             : 
     521           4 :         if (pipe_eof_seen)
     522             :         {
     523             :             /*
     524             :              * seeing this message on the real stderr is annoying - so we make
     525             :              * it DEBUG1 to suppress in normal use.
     526             :              */
     527           2 :             ereport(DEBUG1,
     528             :                     (errmsg("logger shutting down")));
     529             : 
     530             :             /*
     531             :              * Normal exit from the syslogger is here.  Note that we
     532             :              * deliberately do not close syslogFile before exiting; this is to
     533             :              * allow for the possibility of elog messages being generated
     534             :              * inside proc_exit.  Regular exit() will take care of flushing
     535             :              * and closing stdio channels.
     536             :              */
     537           2 :             proc_exit(0);
     538             :         }
     539             :     }
     540             : }
     541             : 
     542             : /*
     543             :  * Postmaster subroutine to start a syslogger subprocess.
     544             :  */
     545             : int
     546         574 : SysLogger_Start(void)
     547             : {
     548             :     pid_t       sysloggerPid;
     549             :     char       *filename;
     550             : 
     551         574 :     if (!Logging_collector)
     552         572 :         return 0;
     553             : 
     554             :     /*
     555             :      * If first time through, create the pipe which will receive stderr
     556             :      * output.
     557             :      *
     558             :      * If the syslogger crashes and needs to be restarted, we continue to use
     559             :      * the same pipe (indeed must do so, since extant backends will be writing
     560             :      * into that pipe).
     561             :      *
     562             :      * This means the postmaster must continue to hold the read end of the
     563             :      * pipe open, so we can pass it down to the reincarnated syslogger. This
     564             :      * is a bit klugy but we have little choice.
     565             :      */
     566             : #ifndef WIN32
     567           2 :     if (syslogPipe[0] < 0)
     568             :     {
     569           2 :         if (pipe(syslogPipe) < 0)
     570           0 :             ereport(FATAL,
     571             :                     (errcode_for_socket_access(),
     572             :                      (errmsg("could not create pipe for syslog: %m"))));
     573             :     }
     574             : #else
     575             :     if (!syslogPipe[0])
     576             :     {
     577             :         SECURITY_ATTRIBUTES sa;
     578             : 
     579             :         memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
     580             :         sa.nLength = sizeof(SECURITY_ATTRIBUTES);
     581             :         sa.bInheritHandle = TRUE;
     582             : 
     583             :         if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
     584             :             ereport(FATAL,
     585             :                     (errcode_for_file_access(),
     586             :                      (errmsg("could not create pipe for syslog: %m"))));
     587             :     }
     588             : #endif
     589             : 
     590             :     /*
     591             :      * Create log directory if not present; ignore errors
     592             :      */
     593           2 :     (void) MakePGDirectory(Log_directory);
     594             : 
     595             :     /*
     596             :      * The initial logfile is created right in the postmaster, to verify that
     597             :      * the Log_directory is writable.  We save the reference time so that the
     598             :      * syslogger child process can recompute this file name.
     599             :      *
     600             :      * It might look a bit strange to re-do this during a syslogger restart,
     601             :      * but we must do so since the postmaster closed syslogFile after the
     602             :      * previous fork (and remembering that old file wouldn't be right anyway).
     603             :      * Note we always append here, we won't overwrite any existing file.  This
     604             :      * is consistent with the normal rules, because by definition this is not
     605             :      * a time-based rotation.
     606             :      */
     607           2 :     first_syslogger_file_time = time(NULL);
     608             : 
     609           2 :     filename = logfile_getname(first_syslogger_file_time, NULL);
     610             : 
     611           2 :     syslogFile = logfile_open(filename, "a", false);
     612             : 
     613           2 :     pfree(filename);
     614             : 
     615             :     /*
     616             :      * Likewise for the initial CSV log file, if that's enabled.  (Note that
     617             :      * we open syslogFile even when only CSV output is nominally enabled,
     618             :      * since some code paths will write to syslogFile anyway.)
     619             :      */
     620           2 :     if (Log_destination & LOG_DESTINATION_CSVLOG)
     621             :     {
     622           0 :         filename = logfile_getname(first_syslogger_file_time, ".csv");
     623             : 
     624           0 :         csvlogFile = logfile_open(filename, "a", false);
     625             : 
     626           0 :         pfree(filename);
     627             :     }
     628             : 
     629             : #ifdef EXEC_BACKEND
     630             :     switch ((sysloggerPid = syslogger_forkexec()))
     631             : #else
     632           2 :     switch ((sysloggerPid = fork_process()))
     633             : #endif
     634             :     {
     635             :         case -1:
     636           0 :             ereport(LOG,
     637             :                     (errmsg("could not fork system logger: %m")));
     638           0 :             return 0;
     639             : 
     640             : #ifndef EXEC_BACKEND
     641             :         case 0:
     642             :             /* in postmaster child ... */
     643           2 :             InitPostmasterChild();
     644             : 
     645             :             /* Close the postmaster's sockets */
     646           2 :             ClosePostmasterPorts(true);
     647             : 
     648             :             /* Drop our connection to postmaster's shared memory, as well */
     649           2 :             dsm_detach_all();
     650           2 :             PGSharedMemoryDetach();
     651             : 
     652             :             /* do the work */
     653           2 :             SysLoggerMain(0, NULL);
     654             :             break;
     655             : #endif
     656             : 
     657             :         default:
     658             :             /* success, in postmaster */
     659             : 
     660             :             /* now we redirect stderr, if not done already */
     661           2 :             if (!redirection_done)
     662             :             {
     663             : #ifdef WIN32
     664             :                 int         fd;
     665             : #endif
     666             : 
     667             :                 /*
     668             :                  * Leave a breadcrumb trail when redirecting, in case the user
     669             :                  * forgets that redirection is active and looks only at the
     670             :                  * original stderr target file.
     671             :                  */
     672           2 :                 ereport(LOG,
     673             :                         (errmsg("redirecting log output to logging collector process"),
     674             :                          errhint("Future log output will appear in directory \"%s\".",
     675             :                                  Log_directory)));
     676             : 
     677             : #ifndef WIN32
     678           2 :                 fflush(stdout);
     679           2 :                 if (dup2(syslogPipe[1], fileno(stdout)) < 0)
     680           0 :                     ereport(FATAL,
     681             :                             (errcode_for_file_access(),
     682             :                              errmsg("could not redirect stdout: %m")));
     683           2 :                 fflush(stderr);
     684           2 :                 if (dup2(syslogPipe[1], fileno(stderr)) < 0)
     685           0 :                     ereport(FATAL,
     686             :                             (errcode_for_file_access(),
     687             :                              errmsg("could not redirect stderr: %m")));
     688             :                 /* Now we are done with the write end of the pipe. */
     689           2 :                 close(syslogPipe[1]);
     690           2 :                 syslogPipe[1] = -1;
     691             : #else
     692             : 
     693             :                 /*
     694             :                  * open the pipe in binary mode and make sure stderr is binary
     695             :                  * after it's been dup'ed into, to avoid disturbing the pipe
     696             :                  * chunking protocol.
     697             :                  */
     698             :                 fflush(stderr);
     699             :                 fd = _open_osfhandle((intptr_t) syslogPipe[1],
     700             :                                      _O_APPEND | _O_BINARY);
     701             :                 if (dup2(fd, _fileno(stderr)) < 0)
     702             :                     ereport(FATAL,
     703             :                             (errcode_for_file_access(),
     704             :                              errmsg("could not redirect stderr: %m")));
     705             :                 close(fd);
     706             :                 _setmode(_fileno(stderr), _O_BINARY);
     707             : 
     708             :                 /*
     709             :                  * Now we are done with the write end of the pipe.
     710             :                  * CloseHandle() must not be called because the preceding
     711             :                  * close() closes the underlying handle.
     712             :                  */
     713             :                 syslogPipe[1] = 0;
     714             : #endif
     715           2 :                 redirection_done = true;
     716             :             }
     717             : 
     718             :             /* postmaster will never write the file(s); close 'em */
     719           2 :             fclose(syslogFile);
     720           2 :             syslogFile = NULL;
     721           2 :             if (csvlogFile != NULL)
     722             :             {
     723           0 :                 fclose(csvlogFile);
     724           0 :                 csvlogFile = NULL;
     725             :             }
     726           2 :             return (int) sysloggerPid;
     727             :     }
     728             : 
     729             :     /* we should never reach here */
     730             :     return 0;
     731             : }
     732             : 
     733             : 
     734             : #ifdef EXEC_BACKEND
     735             : 
     736             : /*
     737             :  * syslogger_forkexec() -
     738             :  *
     739             :  * Format up the arglist for, then fork and exec, a syslogger process
     740             :  */
     741             : static pid_t
     742             : syslogger_forkexec(void)
     743             : {
     744             :     char       *av[10];
     745             :     int         ac = 0;
     746             :     char        filenobuf[32];
     747             :     char        csvfilenobuf[32];
     748             : 
     749             :     av[ac++] = "postgres";
     750             :     av[ac++] = "--forklog";
     751             :     av[ac++] = NULL;            /* filled in by postmaster_forkexec */
     752             : 
     753             :     /* static variables (those not passed by write_backend_variables) */
     754             : #ifndef WIN32
     755             :     if (syslogFile != NULL)
     756             :         snprintf(filenobuf, sizeof(filenobuf), "%d",
     757             :                  fileno(syslogFile));
     758             :     else
     759             :         strcpy(filenobuf, "-1");
     760             : #else                           /* WIN32 */
     761             :     if (syslogFile != NULL)
     762             :         snprintf(filenobuf, sizeof(filenobuf), "%ld",
     763             :                  (long) _get_osfhandle(_fileno(syslogFile)));
     764             :     else
     765             :         strcpy(filenobuf, "0");
     766             : #endif                          /* WIN32 */
     767             :     av[ac++] = filenobuf;
     768             : 
     769             : #ifndef WIN32
     770             :     if (csvlogFile != NULL)
     771             :         snprintf(csvfilenobuf, sizeof(csvfilenobuf), "%d",
     772             :                  fileno(csvlogFile));
     773             :     else
     774             :         strcpy(csvfilenobuf, "-1");
     775             : #else                           /* WIN32 */
     776             :     if (csvlogFile != NULL)
     777             :         snprintf(csvfilenobuf, sizeof(csvfilenobuf), "%ld",
     778             :                  (long) _get_osfhandle(_fileno(csvlogFile)));
     779             :     else
     780             :         strcpy(csvfilenobuf, "0");
     781             : #endif                          /* WIN32 */
     782             :     av[ac++] = csvfilenobuf;
     783             : 
     784             :     av[ac] = NULL;
     785             :     Assert(ac < lengthof(av));
     786             : 
     787             :     return postmaster_forkexec(ac, av);
     788             : }
     789             : 
     790             : /*
     791             :  * syslogger_parseArgs() -
     792             :  *
     793             :  * Extract data from the arglist for exec'ed syslogger process
     794             :  */
     795             : static void
     796             : syslogger_parseArgs(int argc, char *argv[])
     797             : {
     798             :     int         fd;
     799             : 
     800             :     Assert(argc == 5);
     801             :     argv += 3;
     802             : 
     803             :     /*
     804             :      * Re-open the error output files that were opened by SysLogger_Start().
     805             :      *
     806             :      * We expect this will always succeed, which is too optimistic, but if it
     807             :      * fails there's not a lot we can do to report the problem anyway.  As
     808             :      * coded, we'll just crash on a null pointer dereference after failure...
     809             :      */
     810             : #ifndef WIN32
     811             :     fd = atoi(*argv++);
     812             :     if (fd != -1)
     813             :     {
     814             :         syslogFile = fdopen(fd, "a");
     815             :         setvbuf(syslogFile, NULL, PG_IOLBF, 0);
     816             :     }
     817             :     fd = atoi(*argv++);
     818             :     if (fd != -1)
     819             :     {
     820             :         csvlogFile = fdopen(fd, "a");
     821             :         setvbuf(csvlogFile, NULL, PG_IOLBF, 0);
     822             :     }
     823             : #else                           /* WIN32 */
     824             :     fd = atoi(*argv++);
     825             :     if (fd != 0)
     826             :     {
     827             :         fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
     828             :         if (fd > 0)
     829             :         {
     830             :             syslogFile = fdopen(fd, "a");
     831             :             setvbuf(syslogFile, NULL, PG_IOLBF, 0);
     832             :         }
     833             :     }
     834             :     fd = atoi(*argv++);
     835             :     if (fd != 0)
     836             :     {
     837             :         fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
     838             :         if (fd > 0)
     839             :         {
     840             :             csvlogFile = fdopen(fd, "a");
     841             :             setvbuf(csvlogFile, NULL, PG_IOLBF, 0);
     842             :         }
     843             :     }
     844             : #endif                          /* WIN32 */
     845             : }
     846             : #endif                          /* EXEC_BACKEND */
     847             : 
     848             : 
     849             : /* --------------------------------
     850             :  *      pipe protocol handling
     851             :  * --------------------------------
     852             :  */
     853             : 
     854             : /*
     855             :  * Process data received through the syslogger pipe.
     856             :  *
     857             :  * This routine interprets the log pipe protocol which sends log messages as
     858             :  * (hopefully atomic) chunks - such chunks are detected and reassembled here.
     859             :  *
     860             :  * The protocol has a header that starts with two nul bytes, then has a 16 bit
     861             :  * length, the pid of the sending process, and a flag to indicate if it is
     862             :  * the last chunk in a message. Incomplete chunks are saved until we read some
     863             :  * more, and non-final chunks are accumulated until we get the final chunk.
     864             :  *
     865             :  * All of this is to avoid 2 problems:
     866             :  * . partial messages being written to logfiles (messes rotation), and
     867             :  * . messages from different backends being interleaved (messages garbled).
     868             :  *
     869             :  * Any non-protocol messages are written out directly. These should only come
     870             :  * from non-PostgreSQL sources, however (e.g. third party libraries writing to
     871             :  * stderr).
     872             :  *
     873             :  * logbuffer is the data input buffer, and *bytes_in_logbuffer is the number
     874             :  * of bytes present.  On exit, any not-yet-eaten data is left-justified in
     875             :  * logbuffer, and *bytes_in_logbuffer is updated.
     876             :  */
     877             : static void
     878          20 : process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
     879             : {
     880          20 :     char       *cursor = logbuffer;
     881          20 :     int         count = *bytes_in_logbuffer;
     882          20 :     int         dest = LOG_DESTINATION_STDERR;
     883             : 
     884             :     /* While we have enough for a header, process data... */
     885          60 :     while (count >= (int) (offsetof(PipeProtoHeader, data) + 1))
     886             :     {
     887             :         PipeProtoHeader p;
     888             :         int         chunklen;
     889             : 
     890             :         /* Do we have a valid header? */
     891          20 :         memcpy(&p, cursor, offsetof(PipeProtoHeader, data));
     892          40 :         if (p.nuls[0] == '\0' && p.nuls[1] == '\0' &&
     893          60 :             p.len > 0 && p.len <= PIPE_MAX_PAYLOAD &&
     894          40 :             p.pid != 0 &&
     895          20 :             (p.is_last == 't' || p.is_last == 'f' ||
     896           0 :              p.is_last == 'T' || p.is_last == 'F'))
     897          20 :         {
     898             :             List       *buffer_list;
     899             :             ListCell   *cell;
     900          20 :             save_buffer *existing_slot = NULL,
     901          20 :                        *free_slot = NULL;
     902             :             StringInfo  str;
     903             : 
     904          20 :             chunklen = PIPE_HEADER_SIZE + p.len;
     905             : 
     906             :             /* Fall out of loop if we don't have the whole chunk yet */
     907          20 :             if (count < chunklen)
     908           0 :                 break;
     909             : 
     910          60 :             dest = (p.is_last == 'T' || p.is_last == 'F') ?
     911          20 :                 LOG_DESTINATION_CSVLOG : LOG_DESTINATION_STDERR;
     912             : 
     913             :             /* Locate any existing buffer for this source pid */
     914          20 :             buffer_list = buffer_lists[p.pid % NBUFFER_LISTS];
     915          20 :             foreach(cell, buffer_list)
     916             :             {
     917           0 :                 save_buffer *buf = (save_buffer *) lfirst(cell);
     918             : 
     919           0 :                 if (buf->pid == p.pid)
     920             :                 {
     921           0 :                     existing_slot = buf;
     922           0 :                     break;
     923             :                 }
     924           0 :                 if (buf->pid == 0 && free_slot == NULL)
     925           0 :                     free_slot = buf;
     926             :             }
     927             : 
     928          20 :             if (p.is_last == 'f' || p.is_last == 'F')
     929             :             {
     930             :                 /*
     931             :                  * Save a complete non-final chunk in a per-pid buffer
     932             :                  */
     933           0 :                 if (existing_slot != NULL)
     934             :                 {
     935             :                     /* Add chunk to data from preceding chunks */
     936           0 :                     str = &(existing_slot->data);
     937           0 :                     appendBinaryStringInfo(str,
     938           0 :                                            cursor + PIPE_HEADER_SIZE,
     939           0 :                                            p.len);
     940             :                 }
     941             :                 else
     942             :                 {
     943             :                     /* First chunk of message, save in a new buffer */
     944           0 :                     if (free_slot == NULL)
     945             :                     {
     946             :                         /*
     947             :                          * Need a free slot, but there isn't one in the list,
     948             :                          * so create a new one and extend the list with it.
     949             :                          */
     950           0 :                         free_slot = palloc(sizeof(save_buffer));
     951           0 :                         buffer_list = lappend(buffer_list, free_slot);
     952           0 :                         buffer_lists[p.pid % NBUFFER_LISTS] = buffer_list;
     953             :                     }
     954           0 :                     free_slot->pid = p.pid;
     955           0 :                     str = &(free_slot->data);
     956           0 :                     initStringInfo(str);
     957           0 :                     appendBinaryStringInfo(str,
     958           0 :                                            cursor + PIPE_HEADER_SIZE,
     959           0 :                                            p.len);
     960             :                 }
     961             :             }
     962             :             else
     963             :             {
     964             :                 /*
     965             :                  * Final chunk --- add it to anything saved for that pid, and
     966             :                  * either way write the whole thing out.
     967             :                  */
     968          20 :                 if (existing_slot != NULL)
     969             :                 {
     970           0 :                     str = &(existing_slot->data);
     971           0 :                     appendBinaryStringInfo(str,
     972           0 :                                            cursor + PIPE_HEADER_SIZE,
     973           0 :                                            p.len);
     974           0 :                     write_syslogger_file(str->data, str->len, dest);
     975             :                     /* Mark the buffer unused, and reclaim string storage */
     976           0 :                     existing_slot->pid = 0;
     977           0 :                     pfree(str->data);
     978             :                 }
     979             :                 else
     980             :                 {
     981             :                     /* The whole message was one chunk, evidently. */
     982          20 :                     write_syslogger_file(cursor + PIPE_HEADER_SIZE, p.len,
     983             :                                          dest);
     984             :                 }
     985             :             }
     986             : 
     987             :             /* Finished processing this chunk */
     988          20 :             cursor += chunklen;
     989          20 :             count -= chunklen;
     990             :         }
     991             :         else
     992             :         {
     993             :             /* Process non-protocol data */
     994             : 
     995             :             /*
     996             :              * Look for the start of a protocol header.  If found, dump data
     997             :              * up to there and repeat the loop.  Otherwise, dump it all and
     998             :              * fall out of the loop.  (Note: we want to dump it all if at all
     999             :              * possible, so as to avoid dividing non-protocol messages across
    1000             :              * logfiles.  We expect that in many scenarios, a non-protocol
    1001             :              * message will arrive all in one read(), and we want to respect
    1002             :              * the read() boundary if possible.)
    1003             :              */
    1004           0 :             for (chunklen = 1; chunklen < count; chunklen++)
    1005             :             {
    1006           0 :                 if (cursor[chunklen] == '\0')
    1007           0 :                     break;
    1008             :             }
    1009             :             /* fall back on the stderr log as the destination */
    1010           0 :             write_syslogger_file(cursor, chunklen, LOG_DESTINATION_STDERR);
    1011           0 :             cursor += chunklen;
    1012           0 :             count -= chunklen;
    1013             :         }
    1014             :     }
    1015             : 
    1016             :     /* We don't have a full chunk, so left-align what remains in the buffer */
    1017          20 :     if (count > 0 && cursor != logbuffer)
    1018           0 :         memmove(logbuffer, cursor, count);
    1019          20 :     *bytes_in_logbuffer = count;
    1020          20 : }
    1021             : 
    1022             : /*
    1023             :  * Force out any buffered data
    1024             :  *
    1025             :  * This is currently used only at syslogger shutdown, but could perhaps be
    1026             :  * useful at other times, so it is careful to leave things in a clean state.
    1027             :  */
    1028             : static void
    1029           2 : flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
    1030             : {
    1031             :     int         i;
    1032             : 
    1033             :     /* Dump any incomplete protocol messages */
    1034         514 :     for (i = 0; i < NBUFFER_LISTS; i++)
    1035             :     {
    1036         512 :         List       *list = buffer_lists[i];
    1037             :         ListCell   *cell;
    1038             : 
    1039         512 :         foreach(cell, list)
    1040             :         {
    1041           0 :             save_buffer *buf = (save_buffer *) lfirst(cell);
    1042             : 
    1043           0 :             if (buf->pid != 0)
    1044             :             {
    1045           0 :                 StringInfo  str = &(buf->data);
    1046             : 
    1047           0 :                 write_syslogger_file(str->data, str->len,
    1048             :                                      LOG_DESTINATION_STDERR);
    1049             :                 /* Mark the buffer unused, and reclaim string storage */
    1050           0 :                 buf->pid = 0;
    1051           0 :                 pfree(str->data);
    1052             :             }
    1053             :         }
    1054             :     }
    1055             : 
    1056             :     /*
    1057             :      * Force out any remaining pipe data as-is; we don't bother trying to
    1058             :      * remove any protocol headers that may exist in it.
    1059             :      */
    1060           2 :     if (*bytes_in_logbuffer > 0)
    1061           0 :         write_syslogger_file(logbuffer, *bytes_in_logbuffer,
    1062             :                              LOG_DESTINATION_STDERR);
    1063           2 :     *bytes_in_logbuffer = 0;
    1064           2 : }
    1065             : 
    1066             : 
    1067             : /* --------------------------------
    1068             :  *      logfile routines
    1069             :  * --------------------------------
    1070             :  */
    1071             : 
    1072             : /*
    1073             :  * Write text to the currently open logfile
    1074             :  *
    1075             :  * This is exported so that elog.c can call it when am_syslogger is true.
    1076             :  * This allows the syslogger process to record elog messages of its own,
    1077             :  * even though its stderr does not point at the syslog pipe.
    1078             :  */
    1079             : void
    1080          20 : write_syslogger_file(const char *buffer, int count, int destination)
    1081             : {
    1082             :     int         rc;
    1083             :     FILE       *logfile;
    1084             : 
    1085             :     /*
    1086             :      * If we're told to write to csvlogFile, but it's not open, dump the data
    1087             :      * to syslogFile (which is always open) instead.  This can happen if CSV
    1088             :      * output is enabled after postmaster start and we've been unable to open
    1089             :      * csvlogFile.  There are also race conditions during a parameter change
    1090             :      * whereby backends might send us CSV output before we open csvlogFile or
    1091             :      * after we close it.  Writing CSV-formatted output to the regular log
    1092             :      * file isn't great, but it beats dropping log output on the floor.
    1093             :      *
    1094             :      * Think not to improve this by trying to open csvlogFile on-the-fly.  Any
    1095             :      * failure in that would lead to recursion.
    1096             :      */
    1097          20 :     logfile = (destination == LOG_DESTINATION_CSVLOG &&
    1098          20 :                csvlogFile != NULL) ? csvlogFile : syslogFile;
    1099             : 
    1100          20 :     rc = fwrite(buffer, 1, count, logfile);
    1101             : 
    1102             :     /*
    1103             :      * Try to report any failure.  We mustn't use ereport because it would
    1104             :      * just recurse right back here, but write_stderr is OK: it will write
    1105             :      * either to the postmaster's original stderr, or to /dev/null, but never
    1106             :      * to our input pipe which would result in a different sort of looping.
    1107             :      */
    1108          20 :     if (rc != count)
    1109           0 :         write_stderr("could not write to log file: %s\n", strerror(errno));
    1110          20 : }
    1111             : 
    1112             : #ifdef WIN32
    1113             : 
    1114             : /*
    1115             :  * Worker thread to transfer data from the pipe to the current logfile.
    1116             :  *
    1117             :  * We need this because on Windows, WaitForMultipleObjects does not work on
    1118             :  * unnamed pipes: it always reports "signaled", so the blocking ReadFile won't
    1119             :  * allow for SIGHUP; and select is for sockets only.
    1120             :  */
    1121             : static unsigned int __stdcall
    1122             : pipeThread(void *arg)
    1123             : {
    1124             :     char        logbuffer[READ_BUF_SIZE];
    1125             :     int         bytes_in_logbuffer = 0;
    1126             : 
    1127             :     for (;;)
    1128             :     {
    1129             :         DWORD       bytesRead;
    1130             :         BOOL        result;
    1131             : 
    1132             :         result = ReadFile(syslogPipe[0],
    1133             :                           logbuffer + bytes_in_logbuffer,
    1134             :                           sizeof(logbuffer) - bytes_in_logbuffer,
    1135             :                           &bytesRead, 0);
    1136             : 
    1137             :         /*
    1138             :          * Enter critical section before doing anything that might touch
    1139             :          * global state shared by the main thread. Anything that uses
    1140             :          * palloc()/pfree() in particular are not safe outside the critical
    1141             :          * section.
    1142             :          */
    1143             :         EnterCriticalSection(&sysloggerSection);
    1144             :         if (!result)
    1145             :         {
    1146             :             DWORD       error = GetLastError();
    1147             : 
    1148             :             if (error == ERROR_HANDLE_EOF ||
    1149             :                 error == ERROR_BROKEN_PIPE)
    1150             :                 break;
    1151             :             _dosmaperr(error);
    1152             :             ereport(LOG,
    1153             :                     (errcode_for_file_access(),
    1154             :                      errmsg("could not read from logger pipe: %m")));
    1155             :         }
    1156             :         else if (bytesRead > 0)
    1157             :         {
    1158             :             bytes_in_logbuffer += bytesRead;
    1159             :             process_pipe_input(logbuffer, &bytes_in_logbuffer);
    1160             :         }
    1161             : 
    1162             :         /*
    1163             :          * If we've filled the current logfile, nudge the main thread to do a
    1164             :          * log rotation.
    1165             :          */
    1166             :         if (Log_RotationSize > 0)
    1167             :         {
    1168             :             if (ftell(syslogFile) >= Log_RotationSize * 1024L ||
    1169             :                 (csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L))
    1170             :                 SetLatch(MyLatch);
    1171             :         }
    1172             :         LeaveCriticalSection(&sysloggerSection);
    1173             :     }
    1174             : 
    1175             :     /* We exit the above loop only upon detecting pipe EOF */
    1176             :     pipe_eof_seen = true;
    1177             : 
    1178             :     /* if there's any data left then force it out now */
    1179             :     flush_pipe_input(logbuffer, &bytes_in_logbuffer);
    1180             : 
    1181             :     /* set the latch to waken the main thread, which will quit */
    1182             :     SetLatch(MyLatch);
    1183             : 
    1184             :     LeaveCriticalSection(&sysloggerSection);
    1185             :     _endthread();
    1186             :     return 0;
    1187             : }
    1188             : #endif                          /* WIN32 */
    1189             : 
    1190             : /*
    1191             :  * Open a new logfile with proper permissions and buffering options.
    1192             :  *
    1193             :  * If allow_errors is true, we just log any open failure and return NULL
    1194             :  * (with errno still correct for the fopen failure).
    1195             :  * Otherwise, errors are treated as fatal.
    1196             :  */
    1197             : static FILE *
    1198           4 : logfile_open(const char *filename, const char *mode, bool allow_errors)
    1199             : {
    1200             :     FILE       *fh;
    1201             :     mode_t      oumask;
    1202             : 
    1203             :     /*
    1204             :      * Note we do not let Log_file_mode disable IWUSR, since we certainly want
    1205             :      * to be able to write the files ourselves.
    1206             :      */
    1207           4 :     oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & (S_IRWXU | S_IRWXG | S_IRWXO)));
    1208           4 :     fh = fopen(filename, mode);
    1209           4 :     umask(oumask);
    1210             : 
    1211           4 :     if (fh)
    1212             :     {
    1213           4 :         setvbuf(fh, NULL, PG_IOLBF, 0);
    1214             : 
    1215             : #ifdef WIN32
    1216             :         /* use CRLF line endings on Windows */
    1217             :         _setmode(_fileno(fh), _O_TEXT);
    1218             : #endif
    1219             :     }
    1220             :     else
    1221             :     {
    1222           0 :         int         save_errno = errno;
    1223             : 
    1224           0 :         ereport(allow_errors ? LOG : FATAL,
    1225             :                 (errcode_for_file_access(),
    1226             :                  errmsg("could not open log file \"%s\": %m",
    1227             :                         filename)));
    1228           0 :         errno = save_errno;
    1229             :     }
    1230             : 
    1231           4 :     return fh;
    1232             : }
    1233             : 
    1234             : /*
    1235             :  * perform logfile rotation
    1236             :  */
    1237             : static void
    1238           2 : logfile_rotate(bool time_based_rotation, int size_rotation_for)
    1239             : {
    1240             :     char       *filename;
    1241           2 :     char       *csvfilename = NULL;
    1242             :     pg_time_t   fntime;
    1243             :     FILE       *fh;
    1244             : 
    1245           2 :     rotation_requested = false;
    1246             : 
    1247             :     /*
    1248             :      * When doing a time-based rotation, invent the new logfile name based on
    1249             :      * the planned rotation time, not current time, to avoid "slippage" in the
    1250             :      * file name when we don't do the rotation immediately.
    1251             :      */
    1252           2 :     if (time_based_rotation)
    1253           0 :         fntime = next_rotation_time;
    1254             :     else
    1255           2 :         fntime = time(NULL);
    1256           2 :     filename = logfile_getname(fntime, NULL);
    1257           2 :     if (Log_destination & LOG_DESTINATION_CSVLOG)
    1258           0 :         csvfilename = logfile_getname(fntime, ".csv");
    1259             : 
    1260             :     /*
    1261             :      * Decide whether to overwrite or append.  We can overwrite if (a)
    1262             :      * Log_truncate_on_rotation is set, (b) the rotation was triggered by
    1263             :      * elapsed time and not something else, and (c) the computed file name is
    1264             :      * different from what we were previously logging into.
    1265             :      *
    1266             :      * Note: last_file_name should never be NULL here, but if it is, append.
    1267             :      */
    1268           2 :     if (time_based_rotation || (size_rotation_for & LOG_DESTINATION_STDERR))
    1269             :     {
    1270           2 :         if (Log_truncate_on_rotation && time_based_rotation &&
    1271           0 :             last_file_name != NULL &&
    1272           0 :             strcmp(filename, last_file_name) != 0)
    1273           0 :             fh = logfile_open(filename, "w", true);
    1274             :         else
    1275           2 :             fh = logfile_open(filename, "a", true);
    1276             : 
    1277           2 :         if (!fh)
    1278             :         {
    1279             :             /*
    1280             :              * ENFILE/EMFILE are not too surprising on a busy system; just
    1281             :              * keep using the old file till we manage to get a new one.
    1282             :              * Otherwise, assume something's wrong with Log_directory and stop
    1283             :              * trying to create files.
    1284             :              */
    1285           0 :             if (errno != ENFILE && errno != EMFILE)
    1286             :             {
    1287           0 :                 ereport(LOG,
    1288             :                         (errmsg("disabling automatic rotation (use SIGHUP to re-enable)")));
    1289           0 :                 rotation_disabled = true;
    1290             :             }
    1291             : 
    1292           0 :             if (filename)
    1293           0 :                 pfree(filename);
    1294           0 :             if (csvfilename)
    1295           0 :                 pfree(csvfilename);
    1296           0 :             return;
    1297             :         }
    1298             : 
    1299           2 :         fclose(syslogFile);
    1300           2 :         syslogFile = fh;
    1301             : 
    1302             :         /* instead of pfree'ing filename, remember it for next time */
    1303           2 :         if (last_file_name != NULL)
    1304           2 :             pfree(last_file_name);
    1305           2 :         last_file_name = filename;
    1306           2 :         filename = NULL;
    1307             :     }
    1308             : 
    1309             :     /*
    1310             :      * Same as above, but for csv file.  Note that if LOG_DESTINATION_CSVLOG
    1311             :      * was just turned on, we might have to open csvlogFile here though it was
    1312             :      * not open before.  In such a case we'll append not overwrite (since
    1313             :      * last_csv_file_name will be NULL); that is consistent with the normal
    1314             :      * rules since it's not a time-based rotation.
    1315             :      */
    1316           2 :     if ((Log_destination & LOG_DESTINATION_CSVLOG) &&
    1317           0 :         (csvlogFile == NULL ||
    1318           0 :          time_based_rotation || (size_rotation_for & LOG_DESTINATION_CSVLOG)))
    1319             :     {
    1320           0 :         if (Log_truncate_on_rotation && time_based_rotation &&
    1321           0 :             last_csv_file_name != NULL &&
    1322           0 :             strcmp(csvfilename, last_csv_file_name) != 0)
    1323           0 :             fh = logfile_open(csvfilename, "w", true);
    1324             :         else
    1325           0 :             fh = logfile_open(csvfilename, "a", true);
    1326             : 
    1327           0 :         if (!fh)
    1328             :         {
    1329             :             /*
    1330             :              * ENFILE/EMFILE are not too surprising on a busy system; just
    1331             :              * keep using the old file till we manage to get a new one.
    1332             :              * Otherwise, assume something's wrong with Log_directory and stop
    1333             :              * trying to create files.
    1334             :              */
    1335           0 :             if (errno != ENFILE && errno != EMFILE)
    1336             :             {
    1337           0 :                 ereport(LOG,
    1338             :                         (errmsg("disabling automatic rotation (use SIGHUP to re-enable)")));
    1339           0 :                 rotation_disabled = true;
    1340             :             }
    1341             : 
    1342           0 :             if (filename)
    1343           0 :                 pfree(filename);
    1344           0 :             if (csvfilename)
    1345           0 :                 pfree(csvfilename);
    1346           0 :             return;
    1347             :         }
    1348             : 
    1349           0 :         if (csvlogFile != NULL)
    1350           0 :             fclose(csvlogFile);
    1351           0 :         csvlogFile = fh;
    1352             : 
    1353             :         /* instead of pfree'ing filename, remember it for next time */
    1354           0 :         if (last_csv_file_name != NULL)
    1355           0 :             pfree(last_csv_file_name);
    1356           0 :         last_csv_file_name = csvfilename;
    1357           0 :         csvfilename = NULL;
    1358             :     }
    1359           4 :     else if (!(Log_destination & LOG_DESTINATION_CSVLOG) &&
    1360           2 :              csvlogFile != NULL)
    1361             :     {
    1362             :         /* CSVLOG was just turned off, so close the old file */
    1363           0 :         fclose(csvlogFile);
    1364           0 :         csvlogFile = NULL;
    1365           0 :         if (last_csv_file_name != NULL)
    1366           0 :             pfree(last_csv_file_name);
    1367           0 :         last_csv_file_name = NULL;
    1368             :     }
    1369             : 
    1370           2 :     if (filename)
    1371           0 :         pfree(filename);
    1372           2 :     if (csvfilename)
    1373           0 :         pfree(csvfilename);
    1374             : 
    1375           2 :     update_metainfo_datafile();
    1376             : 
    1377           2 :     set_next_rotation_time();
    1378             : }
    1379             : 
    1380             : 
    1381             : /*
    1382             :  * construct logfile name using timestamp information
    1383             :  *
    1384             :  * If suffix isn't NULL, append it to the name, replacing any ".log"
    1385             :  * that may be in the pattern.
    1386             :  *
    1387             :  * Result is palloc'd.
    1388             :  */
    1389             : static char *
    1390           6 : logfile_getname(pg_time_t timestamp, const char *suffix)
    1391             : {
    1392             :     char       *filename;
    1393             :     int         len;
    1394             : 
    1395           6 :     filename = palloc(MAXPGPATH);
    1396             : 
    1397           6 :     snprintf(filename, MAXPGPATH, "%s/", Log_directory);
    1398             : 
    1399           6 :     len = strlen(filename);
    1400             : 
    1401             :     /* treat Log_filename as a strftime pattern */
    1402           6 :     pg_strftime(filename + len, MAXPGPATH - len, Log_filename,
    1403           6 :                 pg_localtime(&timestamp, log_timezone));
    1404             : 
    1405           6 :     if (suffix != NULL)
    1406             :     {
    1407           0 :         len = strlen(filename);
    1408           0 :         if (len > 4 && (strcmp(filename + (len - 4), ".log") == 0))
    1409           0 :             len -= 4;
    1410           0 :         strlcpy(filename + len, suffix, MAXPGPATH - len);
    1411             :     }
    1412             : 
    1413           6 :     return filename;
    1414             : }
    1415             : 
    1416             : /*
    1417             :  * Determine the next planned rotation time, and store in next_rotation_time.
    1418             :  */
    1419             : static void
    1420           4 : set_next_rotation_time(void)
    1421             : {
    1422             :     pg_time_t   now;
    1423             :     struct pg_tm *tm;
    1424             :     int         rotinterval;
    1425             : 
    1426             :     /* nothing to do if time-based rotation is disabled */
    1427           4 :     if (Log_RotationAge <= 0)
    1428           0 :         return;
    1429             : 
    1430             :     /*
    1431             :      * The requirements here are to choose the next time > now that is a
    1432             :      * "multiple" of the log rotation interval.  "Multiple" can be interpreted
    1433             :      * fairly loosely.  In this version we align to log_timezone rather than
    1434             :      * GMT.
    1435             :      */
    1436           4 :     rotinterval = Log_RotationAge * SECS_PER_MINUTE;    /* convert to seconds */
    1437           4 :     now = (pg_time_t) time(NULL);
    1438           4 :     tm = pg_localtime(&now, log_timezone);
    1439           4 :     now += tm->tm_gmtoff;
    1440           4 :     now -= now % rotinterval;
    1441           4 :     now += rotinterval;
    1442           4 :     now -= tm->tm_gmtoff;
    1443           4 :     next_rotation_time = now;
    1444             : }
    1445             : 
    1446             : /*
    1447             :  * Store the name of the file(s) where the log collector, when enabled, writes
    1448             :  * log messages.  Useful for finding the name(s) of the current log file(s)
    1449             :  * when there is time-based logfile rotation.  Filenames are stored in a
    1450             :  * temporary file and which is renamed into the final destination for
    1451             :  * atomicity.  The file is opened with the same permissions as what gets
    1452             :  * created in the data directory and has proper buffering options.
    1453             :  */
    1454             : static void
    1455           4 : update_metainfo_datafile(void)
    1456             : {
    1457             :     FILE       *fh;
    1458             :     mode_t      oumask;
    1459             : 
    1460           4 :     if (!(Log_destination & LOG_DESTINATION_STDERR) &&
    1461           0 :         !(Log_destination & LOG_DESTINATION_CSVLOG))
    1462             :     {
    1463           0 :         if (unlink(LOG_METAINFO_DATAFILE) < 0 && errno != ENOENT)
    1464           0 :             ereport(LOG,
    1465             :                     (errcode_for_file_access(),
    1466             :                      errmsg("could not remove file \"%s\": %m",
    1467             :                             LOG_METAINFO_DATAFILE)));
    1468           0 :         return;
    1469             :     }
    1470             : 
    1471             :     /* use the same permissions as the data directory for the new file */
    1472           4 :     oumask = umask(pg_mode_mask);
    1473           4 :     fh = fopen(LOG_METAINFO_DATAFILE_TMP, "w");
    1474           4 :     umask(oumask);
    1475             : 
    1476           4 :     if (fh)
    1477             :     {
    1478           4 :         setvbuf(fh, NULL, PG_IOLBF, 0);
    1479             : 
    1480             : #ifdef WIN32
    1481             :         /* use CRLF line endings on Windows */
    1482             :         _setmode(_fileno(fh), _O_TEXT);
    1483             : #endif
    1484             :     }
    1485             :     else
    1486             :     {
    1487           0 :         ereport(LOG,
    1488             :                 (errcode_for_file_access(),
    1489             :                  errmsg("could not open file \"%s\": %m",
    1490             :                         LOG_METAINFO_DATAFILE_TMP)));
    1491           0 :         return;
    1492             :     }
    1493             : 
    1494           4 :     if (last_file_name && (Log_destination & LOG_DESTINATION_STDERR))
    1495             :     {
    1496           4 :         if (fprintf(fh, "stderr %s\n", last_file_name) < 0)
    1497             :         {
    1498           0 :             ereport(LOG,
    1499             :                     (errcode_for_file_access(),
    1500             :                      errmsg("could not write file \"%s\": %m",
    1501             :                             LOG_METAINFO_DATAFILE_TMP)));
    1502           0 :             fclose(fh);
    1503           0 :             return;
    1504             :         }
    1505             :     }
    1506             : 
    1507           4 :     if (last_csv_file_name && (Log_destination & LOG_DESTINATION_CSVLOG))
    1508             :     {
    1509           0 :         if (fprintf(fh, "csvlog %s\n", last_csv_file_name) < 0)
    1510             :         {
    1511           0 :             ereport(LOG,
    1512             :                     (errcode_for_file_access(),
    1513             :                      errmsg("could not write file \"%s\": %m",
    1514             :                             LOG_METAINFO_DATAFILE_TMP)));
    1515           0 :             fclose(fh);
    1516           0 :             return;
    1517             :         }
    1518             :     }
    1519           4 :     fclose(fh);
    1520             : 
    1521           4 :     if (rename(LOG_METAINFO_DATAFILE_TMP, LOG_METAINFO_DATAFILE) != 0)
    1522           0 :         ereport(LOG,
    1523             :                 (errcode_for_file_access(),
    1524             :                  errmsg("could not rename file \"%s\" to \"%s\": %m",
    1525             :                         LOG_METAINFO_DATAFILE_TMP, LOG_METAINFO_DATAFILE)));
    1526             : }
    1527             : 
    1528             : /* --------------------------------
    1529             :  *      signal handler routines
    1530             :  * --------------------------------
    1531             :  */
    1532             : 
    1533             : /*
    1534             :  * Check to see if a log rotation request has arrived.  Should be
    1535             :  * called by postmaster after receiving SIGUSR1.
    1536             :  */
    1537             : bool
    1538           2 : CheckLogrotateSignal(void)
    1539             : {
    1540             :     struct stat stat_buf;
    1541             : 
    1542           2 :     if (stat(LOGROTATE_SIGNAL_FILE, &stat_buf) == 0)
    1543           2 :         return true;
    1544             : 
    1545           0 :     return false;
    1546             : }
    1547             : 
    1548             : /*
    1549             :  * Remove the file signaling a log rotation request.
    1550             :  */
    1551             : void
    1552         576 : RemoveLogrotateSignalFiles(void)
    1553             : {
    1554         576 :     unlink(LOGROTATE_SIGNAL_FILE);
    1555         576 : }
    1556             : 
    1557             : /* SIGHUP: set flag to reload config file */
    1558             : static void
    1559           0 : sigHupHandler(SIGNAL_ARGS)
    1560             : {
    1561           0 :     int         save_errno = errno;
    1562             : 
    1563           0 :     got_SIGHUP = true;
    1564           0 :     SetLatch(MyLatch);
    1565             : 
    1566           0 :     errno = save_errno;
    1567           0 : }
    1568             : 
    1569             : /* SIGUSR1: set flag to rotate logfile */
    1570             : static void
    1571           2 : sigUsr1Handler(SIGNAL_ARGS)
    1572             : {
    1573           2 :     int         save_errno = errno;
    1574             : 
    1575           2 :     rotation_requested = true;
    1576           2 :     SetLatch(MyLatch);
    1577             : 
    1578           2 :     errno = save_errno;
    1579           2 : }

Generated by: LCOV version 1.13