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