Minor improvements in backup and recovery:
[PostgreSQL.git] / src / backend / postmaster / syslogger.c
blob2dfda7e8c717362a115e9dcd8563b14e3718eab1
1 /*-------------------------------------------------------------------------
3 * syslogger.c
5 * The system logger (syslogger) is new 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 an internal naming scheme that mangles
13 * creation time and current postmaster pid.
15 * Author: Andreas Pflug <pgadmin@pse-consulting.de>
17 * Copyright (c) 2004-2007, PostgreSQL Global Development Group
20 * IDENTIFICATION
21 * $PostgreSQL$
23 *-------------------------------------------------------------------------
25 #include "postgres.h"
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
34 #include "lib/stringinfo.h"
35 #include "libpq/pqsignal.h"
36 #include "miscadmin.h"
37 #include "pgtime.h"
38 #include "postmaster/fork_process.h"
39 #include "postmaster/postmaster.h"
40 #include "postmaster/syslogger.h"
41 #include "storage/ipc.h"
42 #include "storage/pg_shmem.h"
43 #include "utils/guc.h"
44 #include "utils/ps_status.h"
45 #include "utils/timestamp.h"
48 * We really want line-buffered mode for logfile output, but Windows does
49 * not have it, and interprets _IOLBF as _IOFBF (bozos). So use _IONBF
50 * instead on Windows.
52 #ifdef WIN32
53 #define LBF_MODE _IONBF
54 #else
55 #define LBF_MODE _IOLBF
56 #endif
58 /*
59 * We read() into a temp buffer twice as big as a chunk, so that any fragment
60 * left after processing can be moved down to the front and we'll still have
61 * room to read a full chunk.
63 #define READ_BUF_SIZE (2 * PIPE_CHUNK_SIZE)
67 * GUC parameters. Logging_collector cannot be changed after postmaster
68 * start, but the rest can change at SIGHUP.
70 bool Logging_collector = false;
71 int Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR;
72 int Log_RotationSize = 10 * 1024;
73 char *Log_directory = NULL;
74 char *Log_filename = NULL;
75 bool Log_truncate_on_rotation = false;
78 * Globally visible state (used by elog.c)
80 bool am_syslogger = false;
82 extern bool redirection_done;
85 * Private state
87 static pg_time_t next_rotation_time;
88 static bool pipe_eof_seen = false;
89 static FILE *syslogFile = NULL;
90 static FILE *csvlogFile = NULL;
91 static char *last_file_name = NULL;
92 static char *last_csvfile_name = NULL;
94 /*
95 * Buffers for saving partial messages from different backends. We don't expect
96 * that there will be very many outstanding at one time, so 20 seems plenty of
97 * leeway. If this array gets full we won't lose messages, but we will lose
98 * the protocol protection against them being partially written or interleaved.
100 * An inactive buffer has pid == 0 and undefined contents of data.
102 typedef struct
104 int32 pid; /* PID of source process */
105 StringInfoData data; /* accumulated data, as a StringInfo */
106 } save_buffer;
108 #define CHUNK_SLOTS 20
109 static save_buffer saved_chunks[CHUNK_SLOTS];
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
118 #ifdef WIN32
119 static HANDLE threadHandle = 0;
120 static CRITICAL_SECTION sysfileSection;
121 #endif
124 * Flags set by interrupt handlers for later service in the main loop.
126 static volatile sig_atomic_t got_SIGHUP = false;
127 static volatile sig_atomic_t rotation_requested = false;
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 static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer);
136 static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer);
137 static void open_csvlogfile(void);
139 #ifdef WIN32
140 static unsigned int __stdcall pipeThread(void *arg);
141 #endif
142 static void logfile_rotate(bool time_based_rotation, int size_rotation_for);
143 static char *logfile_getname(pg_time_t timestamp, char * suffix);
144 static void set_next_rotation_time(void);
145 static void sigHupHandler(SIGNAL_ARGS);
146 static void sigUsr1Handler(SIGNAL_ARGS);
150 * Main entry point for syslogger process
151 * argc/argv parameters are valid only in EXEC_BACKEND case.
153 NON_EXEC_STATIC void
154 SysLoggerMain(int argc, char *argv[])
156 #ifndef WIN32
157 char logbuffer[READ_BUF_SIZE];
158 int bytes_in_logbuffer = 0;
159 #endif
160 char *currentLogDir;
161 char *currentLogFilename;
162 int currentLogRotationAge;
164 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
166 MyProcPid = getpid(); /* reset MyProcPid */
168 MyStartTime = time(NULL); /* set our start time in case we call elog */
170 #ifdef EXEC_BACKEND
171 syslogger_parseArgs(argc, argv);
172 #endif /* EXEC_BACKEND */
174 am_syslogger = true;
176 init_ps_display("logger process", "", "", "");
179 * If we restarted, our stderr is already redirected into our own input
180 * pipe. This is of course pretty useless, not to mention that it
181 * interferes with detecting pipe EOF. Point stderr to /dev/null. This
182 * assumes that all interesting messages generated in the syslogger will
183 * come through elog.c and will be sent to write_syslogger_file.
185 if (redirection_done)
187 int fd = open(NULL_DEV, O_WRONLY, 0);
190 * The closes might look redundant, but they are not: we want to be
191 * darn sure the pipe gets closed even if the open failed. We can
192 * survive running with stderr pointing nowhere, but we can't afford
193 * to have extra pipe input descriptors hanging around.
195 close(fileno(stdout));
196 close(fileno(stderr));
197 dup2(fd, fileno(stdout));
198 dup2(fd, fileno(stderr));
199 close(fd);
202 /* Syslogger's own stderr can't be the syslogPipe, so set it back to
203 * text mode if we didn't just close it.
204 * (It was set to binary in SubPostmasterMain).
206 #ifdef WIN32
207 else
208 _setmode(_fileno(stderr),_O_TEXT);
209 #endif
212 * Also close our copy of the write end of the pipe. This is needed to
213 * ensure we can detect pipe EOF correctly. (But note that in the restart
214 * case, the postmaster already did this.)
216 #ifndef WIN32
217 if (syslogPipe[1] >= 0)
218 close(syslogPipe[1]);
219 syslogPipe[1] = -1;
220 #else
221 if (syslogPipe[1])
222 CloseHandle(syslogPipe[1]);
223 syslogPipe[1] = 0;
224 #endif
227 * If possible, make this process a group leader, so that the postmaster
228 * can signal any child processes too. (syslogger probably never has
229 * any child processes, but for consistency we make all postmaster
230 * child processes do this.)
232 #ifdef HAVE_SETSID
233 if (setsid() < 0)
234 elog(FATAL, "setsid() failed: %m");
235 #endif
238 * Properly accept or ignore signals the postmaster might send us
240 * Note: we ignore all termination signals, and instead exit only when all
241 * upstream processes are gone, to ensure we don't miss any dying gasps of
242 * broken backends...
245 pqsignal(SIGHUP, sigHupHandler); /* set flag to read config file */
246 pqsignal(SIGINT, SIG_IGN);
247 pqsignal(SIGTERM, SIG_IGN);
248 pqsignal(SIGQUIT, SIG_IGN);
249 pqsignal(SIGALRM, SIG_IGN);
250 pqsignal(SIGPIPE, SIG_IGN);
251 pqsignal(SIGUSR1, sigUsr1Handler); /* request log rotation */
252 pqsignal(SIGUSR2, SIG_IGN);
255 * Reset some signals that are accepted by postmaster but not here
257 pqsignal(SIGCHLD, SIG_DFL);
258 pqsignal(SIGTTIN, SIG_DFL);
259 pqsignal(SIGTTOU, SIG_DFL);
260 pqsignal(SIGCONT, SIG_DFL);
261 pqsignal(SIGWINCH, SIG_DFL);
263 PG_SETMASK(&UnBlockSig);
265 #ifdef WIN32
266 /* Fire up separate data transfer thread */
267 InitializeCriticalSection(&sysfileSection);
270 unsigned int tid;
272 threadHandle = (HANDLE) _beginthreadex(0, 0, pipeThread, 0, 0, &tid);
274 #endif /* WIN32 */
276 /* remember active logfile parameters */
277 currentLogDir = pstrdup(Log_directory);
278 currentLogFilename = pstrdup(Log_filename);
279 currentLogRotationAge = Log_RotationAge;
280 /* set next planned rotation time */
281 set_next_rotation_time();
283 /* main worker loop */
284 for (;;)
286 bool time_based_rotation = false;
287 int size_rotation_for = 0;
288 #ifndef WIN32
289 int bytesRead;
290 int rc;
291 fd_set rfds;
292 struct timeval timeout;
293 #endif
295 if (got_SIGHUP)
297 got_SIGHUP = false;
298 ProcessConfigFile(PGC_SIGHUP);
301 * Check if the log directory or filename pattern changed in
302 * postgresql.conf. If so, force rotation to make sure we're
303 * writing the logfiles in the right place.
305 if (strcmp(Log_directory, currentLogDir) != 0)
307 pfree(currentLogDir);
308 currentLogDir = pstrdup(Log_directory);
309 rotation_requested = true;
311 if (strcmp(Log_filename, currentLogFilename) != 0)
313 pfree(currentLogFilename);
314 currentLogFilename = pstrdup(Log_filename);
315 rotation_requested = true;
319 * If rotation time parameter changed, reset next rotation time,
320 * but don't immediately force a rotation.
322 if (currentLogRotationAge != Log_RotationAge)
324 currentLogRotationAge = Log_RotationAge;
325 set_next_rotation_time();
329 if (!rotation_requested && Log_RotationAge > 0)
331 /* Do a logfile rotation if it's time */
332 pg_time_t now = time(NULL);
334 if (now >= next_rotation_time)
335 rotation_requested = time_based_rotation = true;
338 if (!rotation_requested && Log_RotationSize > 0)
340 /* Do a rotation if file is too big */
341 if (ftell(syslogFile) >= Log_RotationSize * 1024L)
343 rotation_requested = true;
344 size_rotation_for |= LOG_DESTINATION_STDERR;
346 if (csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L)
348 rotation_requested = true;
349 size_rotation_for |= LOG_DESTINATION_CSVLOG;
354 if (rotation_requested)
357 * Force rotation when both values are zero.
358 * It means the request was sent by pg_rotate_logfile.
360 if (!time_based_rotation && size_rotation_for == 0)
361 size_rotation_for = LOG_DESTINATION_STDERR | LOG_DESTINATION_CSVLOG;
362 logfile_rotate(time_based_rotation, size_rotation_for);
365 #ifndef WIN32
368 * Wait for some data, timing out after 1 second
370 FD_ZERO(&rfds);
371 FD_SET(syslogPipe[0], &rfds);
372 timeout.tv_sec = 1;
373 timeout.tv_usec = 0;
375 rc = select(syslogPipe[0] + 1, &rfds, NULL, NULL, &timeout);
377 if (rc < 0)
379 if (errno != EINTR)
380 ereport(LOG,
381 (errcode_for_socket_access(),
382 errmsg("select() failed in logger process: %m")));
384 else if (rc > 0 && FD_ISSET(syslogPipe[0], &rfds))
386 bytesRead = piperead(syslogPipe[0],
387 logbuffer + bytes_in_logbuffer,
388 sizeof(logbuffer) - bytes_in_logbuffer);
389 if (bytesRead < 0)
391 if (errno != EINTR)
392 ereport(LOG,
393 (errcode_for_socket_access(),
394 errmsg("could not read from logger pipe: %m")));
396 else if (bytesRead > 0)
398 bytes_in_logbuffer += bytesRead;
399 process_pipe_input(logbuffer, &bytes_in_logbuffer);
400 continue;
402 else
405 * Zero bytes read when select() is saying read-ready means
406 * EOF on the pipe: that is, there are no longer any processes
407 * with the pipe write end open. Therefore, the postmaster
408 * and all backends are shut down, and we are done.
410 pipe_eof_seen = true;
412 /* if there's any data left then force it out now */
413 flush_pipe_input(logbuffer, &bytes_in_logbuffer);
416 #else /* WIN32 */
419 * On Windows we leave it to a separate thread to transfer data and
420 * detect pipe EOF. The main thread just wakes up once a second to
421 * check for SIGHUP and rotation conditions.
423 pg_usleep(1000000L);
424 #endif /* WIN32 */
426 if (pipe_eof_seen)
428 /* seeing this message on the real stderr is annoying - so we
429 * make it DEBUG1 to suppress in normal use.
431 ereport(DEBUG1,
432 (errmsg("logger shutting down")));
435 * Normal exit from the syslogger is here. Note that we
436 * deliberately do not close syslogFile before exiting; this is to
437 * allow for the possibility of elog messages being generated
438 * inside proc_exit. Regular exit() will take care of flushing
439 * and closing stdio channels.
441 proc_exit(0);
447 * Postmaster subroutine to start a syslogger subprocess.
450 SysLogger_Start(void)
452 pid_t sysloggerPid;
453 char *filename;
455 if (!Logging_collector)
456 return 0;
459 * If first time through, create the pipe which will receive stderr
460 * output.
462 * If the syslogger crashes and needs to be restarted, we continue to use
463 * the same pipe (indeed must do so, since extant backends will be writing
464 * into that pipe).
466 * This means the postmaster must continue to hold the read end of the
467 * pipe open, so we can pass it down to the reincarnated syslogger. This
468 * is a bit klugy but we have little choice.
470 #ifndef WIN32
471 if (syslogPipe[0] < 0)
473 if (pgpipe(syslogPipe) < 0)
474 ereport(FATAL,
475 (errcode_for_socket_access(),
476 (errmsg("could not create pipe for syslog: %m"))));
478 #else
479 if (!syslogPipe[0])
481 SECURITY_ATTRIBUTES sa;
483 memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
484 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
485 sa.bInheritHandle = TRUE;
487 if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
488 ereport(FATAL,
489 (errcode_for_file_access(),
490 (errmsg("could not create pipe for syslog: %m"))));
492 #endif
495 * Create log directory if not present; ignore errors
497 mkdir(Log_directory, 0700);
500 * The initial logfile is created right in the postmaster, to verify that
501 * the Log_directory is writable.
503 filename = logfile_getname(time(NULL), NULL);
505 syslogFile = fopen(filename, "a");
507 if (!syslogFile)
508 ereport(FATAL,
509 (errcode_for_file_access(),
510 (errmsg("could not create log file \"%s\": %m",
511 filename))));
513 setvbuf(syslogFile, NULL, LBF_MODE, 0);
515 pfree(filename);
517 #ifdef EXEC_BACKEND
518 switch ((sysloggerPid = syslogger_forkexec()))
519 #else
520 switch ((sysloggerPid = fork_process()))
521 #endif
523 case -1:
524 ereport(LOG,
525 (errmsg("could not fork system logger: %m")));
526 return 0;
528 #ifndef EXEC_BACKEND
529 case 0:
530 /* in postmaster child ... */
531 /* Close the postmaster's sockets */
532 ClosePostmasterPorts(true);
534 /* Lose the postmaster's on-exit routines */
535 on_exit_reset();
537 /* Drop our connection to postmaster's shared memory, as well */
538 PGSharedMemoryDetach();
540 /* do the work */
541 SysLoggerMain(0, NULL);
542 break;
543 #endif
545 default:
546 /* success, in postmaster */
548 /* now we redirect stderr, if not done already */
549 if (!redirection_done)
551 #ifndef WIN32
552 fflush(stdout);
553 if (dup2(syslogPipe[1], fileno(stdout)) < 0)
554 ereport(FATAL,
555 (errcode_for_file_access(),
556 errmsg("could not redirect stdout: %m")));
557 fflush(stderr);
558 if (dup2(syslogPipe[1], fileno(stderr)) < 0)
559 ereport(FATAL,
560 (errcode_for_file_access(),
561 errmsg("could not redirect stderr: %m")));
562 /* Now we are done with the write end of the pipe. */
563 close(syslogPipe[1]);
564 syslogPipe[1] = -1;
565 #else
566 int fd;
569 * open the pipe in binary mode and make sure
570 * stderr is binary after it's been dup'ed into, to avoid
571 * disturbing the pipe chunking protocol.
573 fflush(stderr);
574 fd = _open_osfhandle((long) syslogPipe[1],
575 _O_APPEND | _O_BINARY);
576 if (dup2(fd, _fileno(stderr)) < 0)
577 ereport(FATAL,
578 (errcode_for_file_access(),
579 errmsg("could not redirect stderr: %m")));
580 close(fd);
581 _setmode(_fileno(stderr),_O_BINARY);
582 /* Now we are done with the write end of the pipe. */
583 CloseHandle(syslogPipe[1]);
584 syslogPipe[1] = 0;
585 #endif
586 redirection_done = true;
589 /* postmaster will never write the file; close it */
590 fclose(syslogFile);
591 syslogFile = NULL;
592 return (int) sysloggerPid;
595 /* we should never reach here */
596 return 0;
600 #ifdef EXEC_BACKEND
603 * syslogger_forkexec() -
605 * Format up the arglist for, then fork and exec, a syslogger process
607 static pid_t
608 syslogger_forkexec(void)
610 char *av[10];
611 int ac = 0;
612 char filenobuf[32];
614 av[ac++] = "postgres";
615 av[ac++] = "--forklog";
616 av[ac++] = NULL; /* filled in by postmaster_forkexec */
618 /* static variables (those not passed by write_backend_variables) */
619 #ifndef WIN32
620 if (syslogFile != NULL)
621 snprintf(filenobuf, sizeof(filenobuf), "%d",
622 fileno(syslogFile));
623 else
624 strcpy(filenobuf, "-1");
625 #else /* WIN32 */
626 if (syslogFile != NULL)
627 snprintf(filenobuf, sizeof(filenobuf), "%ld",
628 _get_osfhandle(_fileno(syslogFile)));
629 else
630 strcpy(filenobuf, "0");
631 #endif /* WIN32 */
632 av[ac++] = filenobuf;
634 av[ac] = NULL;
635 Assert(ac < lengthof(av));
637 return postmaster_forkexec(ac, av);
641 * syslogger_parseArgs() -
643 * Extract data from the arglist for exec'ed syslogger process
645 static void
646 syslogger_parseArgs(int argc, char *argv[])
648 int fd;
650 Assert(argc == 4);
651 argv += 3;
653 #ifndef WIN32
654 fd = atoi(*argv++);
655 if (fd != -1)
657 syslogFile = fdopen(fd, "a");
658 setvbuf(syslogFile, NULL, LBF_MODE, 0);
660 #else /* WIN32 */
661 fd = atoi(*argv++);
662 if (fd != 0)
664 fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
665 if (fd > 0)
667 syslogFile = fdopen(fd, "a");
668 setvbuf(syslogFile, NULL, LBF_MODE, 0);
671 #endif /* WIN32 */
673 #endif /* EXEC_BACKEND */
676 /* --------------------------------
677 * pipe protocol handling
678 * --------------------------------
682 * Process data received through the syslogger pipe.
684 * This routine interprets the log pipe protocol which sends log messages as
685 * (hopefully atomic) chunks - such chunks are detected and reassembled here.
687 * The protocol has a header that starts with two nul bytes, then has a 16 bit
688 * length, the pid of the sending process, and a flag to indicate if it is
689 * the last chunk in a message. Incomplete chunks are saved until we read some
690 * more, and non-final chunks are accumulated until we get the final chunk.
692 * All of this is to avoid 2 problems:
693 * . partial messages being written to logfiles (messes rotation), and
694 * . messages from different backends being interleaved (messages garbled).
696 * Any non-protocol messages are written out directly. These should only come
697 * from non-PostgreSQL sources, however (e.g. third party libraries writing to
698 * stderr).
700 * logbuffer is the data input buffer, and *bytes_in_logbuffer is the number
701 * of bytes present. On exit, any not-yet-eaten data is left-justified in
702 * logbuffer, and *bytes_in_logbuffer is updated.
704 static void
705 process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
707 char *cursor = logbuffer;
708 int count = *bytes_in_logbuffer;
709 int dest = LOG_DESTINATION_STDERR;
711 /* While we have enough for a header, process data... */
712 while (count >= (int) sizeof(PipeProtoHeader))
714 PipeProtoHeader p;
715 int chunklen;
717 /* Do we have a valid header? */
718 memcpy(&p, cursor, sizeof(PipeProtoHeader));
719 if (p.nuls[0] == '\0' && p.nuls[1] == '\0' &&
720 p.len > 0 && p.len <= PIPE_MAX_PAYLOAD &&
721 p.pid != 0 &&
722 (p.is_last == 't' || p.is_last == 'f' ||
723 p.is_last == 'T' || p.is_last == 'F' ))
725 chunklen = PIPE_HEADER_SIZE + p.len;
727 /* Fall out of loop if we don't have the whole chunk yet */
728 if (count < chunklen)
729 break;
731 dest = (p.is_last == 'T' || p.is_last == 'F' ) ?
732 LOG_DESTINATION_CSVLOG : LOG_DESTINATION_STDERR;
734 if (p.is_last == 'f' || p.is_last == 'F')
737 * Save a complete non-final chunk in the per-pid buffer
738 * if possible - if not just write it out.
740 int free_slot = -1, existing_slot = -1;
741 int i;
742 StringInfo str;
744 for (i = 0; i < CHUNK_SLOTS; i++)
746 if (saved_chunks[i].pid == p.pid)
748 existing_slot = i;
749 break;
751 if (free_slot < 0 && saved_chunks[i].pid == 0)
752 free_slot = i;
754 if (existing_slot >= 0)
756 str = &(saved_chunks[existing_slot].data);
757 appendBinaryStringInfo(str,
758 cursor + PIPE_HEADER_SIZE,
759 p.len);
761 else if (free_slot >= 0)
763 saved_chunks[free_slot].pid = p.pid;
764 str = &(saved_chunks[free_slot].data);
765 initStringInfo(str);
766 appendBinaryStringInfo(str,
767 cursor + PIPE_HEADER_SIZE,
768 p.len);
770 else
773 * If there is no free slot we'll just have to take our
774 * chances and write out a partial message and hope that
775 * it's not followed by something from another pid.
777 write_syslogger_file(cursor + PIPE_HEADER_SIZE, p.len,
778 dest);
781 else
784 * Final chunk --- add it to anything saved for that pid, and
785 * either way write the whole thing out.
787 int existing_slot = -1;
788 int i;
789 StringInfo str;
791 for (i = 0; i < CHUNK_SLOTS; i++)
793 if (saved_chunks[i].pid == p.pid)
795 existing_slot = i;
796 break;
799 if (existing_slot >= 0)
801 str = &(saved_chunks[existing_slot].data);
802 appendBinaryStringInfo(str,
803 cursor + PIPE_HEADER_SIZE,
804 p.len);
805 write_syslogger_file(str->data, str->len, dest);
806 saved_chunks[existing_slot].pid = 0;
807 pfree(str->data);
809 else
811 /* The whole message was one chunk, evidently. */
812 write_syslogger_file(cursor + PIPE_HEADER_SIZE, p.len,
813 dest);
817 /* Finished processing this chunk */
818 cursor += chunklen;
819 count -= chunklen;
821 else
823 /* Process non-protocol data */
826 * Look for the start of a protocol header. If found, dump data
827 * up to there and repeat the loop. Otherwise, dump it all and
828 * fall out of the loop. (Note: we want to dump it all if
829 * at all possible, so as to avoid dividing non-protocol messages
830 * across logfiles. We expect that in many scenarios, a
831 * non-protocol message will arrive all in one read(), and we
832 * want to respect the read() boundary if possible.)
834 for (chunklen = 1; chunklen < count; chunklen++)
836 if (cursor[chunklen] == '\0')
837 break;
839 /* fall back on the stderr log as the destination */
840 write_syslogger_file(cursor, chunklen, LOG_DESTINATION_STDERR);
841 cursor += chunklen;
842 count -= chunklen;
846 /* We don't have a full chunk, so left-align what remains in the buffer */
847 if (count > 0 && cursor != logbuffer)
848 memmove(logbuffer, cursor, count);
849 *bytes_in_logbuffer = count;
853 * Force out any buffered data
855 * This is currently used only at syslogger shutdown, but could perhaps be
856 * useful at other times, so it is careful to leave things in a clean state.
858 static void
859 flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
861 int i;
862 StringInfo str;
864 /* Dump any incomplete protocol messages */
865 for (i = 0; i < CHUNK_SLOTS; i++)
867 if (saved_chunks[i].pid != 0)
869 str = &(saved_chunks[i].data);
870 write_syslogger_file(str->data, str->len, LOG_DESTINATION_STDERR);
871 saved_chunks[i].pid = 0;
872 pfree(str->data);
876 * Force out any remaining pipe data as-is; we don't bother trying to
877 * remove any protocol headers that may exist in it.
879 if (*bytes_in_logbuffer > 0)
880 write_syslogger_file(logbuffer, *bytes_in_logbuffer,
881 LOG_DESTINATION_STDERR);
882 *bytes_in_logbuffer = 0;
886 /* --------------------------------
887 * logfile routines
888 * --------------------------------
892 * Write text to the currently open logfile
894 * This is exported so that elog.c can call it when am_syslogger is true.
895 * This allows the syslogger process to record elog messages of its own,
896 * even though its stderr does not point at the syslog pipe.
898 void
899 write_syslogger_file(const char *buffer, int count, int destination)
901 int rc;
902 FILE * logfile;
904 if (destination == LOG_DESTINATION_CSVLOG && csvlogFile == NULL)
905 open_csvlogfile();
907 logfile = destination == LOG_DESTINATION_CSVLOG ? csvlogFile : syslogFile ;
909 #ifndef WIN32
910 rc = fwrite(buffer, 1, count, logfile);
911 #else
912 EnterCriticalSection(&sysfileSection);
913 rc = fwrite(buffer, 1, count, logfile);
914 LeaveCriticalSection(&sysfileSection);
915 #endif
917 /* can't use ereport here because of possible recursion */
918 if (rc != count)
919 write_stderr("could not write to log file: %s\n", strerror(errno));
922 #ifdef WIN32
925 * Worker thread to transfer data from the pipe to the current logfile.
927 * We need this because on Windows, WaitForSingleObject does not work on
928 * unnamed pipes: it always reports "signaled", so the blocking ReadFile won't
929 * allow for SIGHUP; and select is for sockets only.
931 static unsigned int __stdcall
932 pipeThread(void *arg)
934 char logbuffer[READ_BUF_SIZE];
935 int bytes_in_logbuffer = 0;
937 for (;;)
939 DWORD bytesRead;
941 if (!ReadFile(syslogPipe[0],
942 logbuffer + bytes_in_logbuffer,
943 sizeof(logbuffer) - bytes_in_logbuffer,
944 &bytesRead, 0))
946 DWORD error = GetLastError();
948 if (error == ERROR_HANDLE_EOF ||
949 error == ERROR_BROKEN_PIPE)
950 break;
951 _dosmaperr(error);
952 ereport(LOG,
953 (errcode_for_file_access(),
954 errmsg("could not read from logger pipe: %m")));
956 else if (bytesRead > 0)
958 bytes_in_logbuffer += bytesRead;
959 process_pipe_input(logbuffer, &bytes_in_logbuffer);
963 /* We exit the above loop only upon detecting pipe EOF */
964 pipe_eof_seen = true;
966 /* if there's any data left then force it out now */
967 flush_pipe_input(logbuffer, &bytes_in_logbuffer);
969 _endthread();
970 return 0;
972 #endif /* WIN32 */
975 * open the csv log file - we do this opportunistically, because
976 * we don't know if CSV logging will be wanted.
978 static void
979 open_csvlogfile(void)
981 char *filename;
982 FILE *fh;
984 filename = logfile_getname(time(NULL),".csv");
986 fh = fopen(filename, "a");
988 if (!fh)
989 ereport(FATAL,
990 (errcode_for_file_access(),
991 (errmsg("could not create log file \"%s\": %m",
992 filename))));
994 setvbuf(fh, NULL, LBF_MODE, 0);
996 #ifdef WIN32
997 _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
998 #endif
1000 csvlogFile = fh;
1002 pfree(filename);
1007 * perform logfile rotation
1009 static void
1010 logfile_rotate(bool time_based_rotation, int size_rotation_for)
1012 char *filename;
1013 char *csvfilename = NULL;
1014 FILE *fh;
1016 rotation_requested = false;
1019 * When doing a time-based rotation, invent the new logfile name based on
1020 * the planned rotation time, not current time, to avoid "slippage" in the
1021 * file name when we don't do the rotation immediately.
1023 if (time_based_rotation)
1025 filename = logfile_getname(next_rotation_time, NULL);
1026 if (csvlogFile != NULL)
1027 csvfilename = logfile_getname(next_rotation_time, ".csv");
1029 else
1031 filename = logfile_getname(time(NULL), NULL);
1032 if (csvlogFile != NULL)
1033 csvfilename = logfile_getname(time(NULL), ".csv");
1037 * Decide whether to overwrite or append. We can overwrite if (a)
1038 * Log_truncate_on_rotation is set, (b) the rotation was triggered by
1039 * elapsed time and not something else, and (c) the computed file name is
1040 * different from what we were previously logging into.
1042 * Note: during the first rotation after forking off from the postmaster,
1043 * last_file_name will be NULL. (We don't bother to set it in the
1044 * postmaster because it ain't gonna work in the EXEC_BACKEND case.) So we
1045 * will always append in that situation, even though truncating would
1046 * usually be safe.
1048 * For consistency, we treat CSV logs the same even though they aren't
1049 * opened in the postmaster.
1051 if (time_based_rotation || (size_rotation_for & LOG_DESTINATION_STDERR))
1053 if (Log_truncate_on_rotation && time_based_rotation &&
1054 last_file_name != NULL && strcmp(filename, last_file_name) != 0)
1055 fh = fopen(filename, "w");
1056 else
1057 fh = fopen(filename, "a");
1059 if (!fh)
1061 int saveerrno = errno;
1063 ereport(LOG,
1064 (errcode_for_file_access(),
1065 errmsg("could not open new log file \"%s\": %m",
1066 filename)));
1069 * ENFILE/EMFILE are not too surprising on a busy system; just keep
1070 * using the old file till we manage to get a new one. Otherwise,
1071 * assume something's wrong with Log_directory and stop trying to
1072 * create files.
1074 if (saveerrno != ENFILE && saveerrno != EMFILE)
1076 ereport(LOG,
1077 (errmsg("disabling automatic rotation (use SIGHUP to reenable)")));
1078 Log_RotationAge = 0;
1079 Log_RotationSize = 0;
1081 pfree(filename);
1082 return;
1085 setvbuf(fh, NULL, LBF_MODE, 0);
1087 #ifdef WIN32
1088 _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
1089 #endif
1091 /* On Windows, need to interlock against data-transfer thread */
1092 #ifdef WIN32
1093 EnterCriticalSection(&sysfileSection);
1094 #endif
1095 fclose(syslogFile);
1096 syslogFile = fh;
1097 #ifdef WIN32
1098 LeaveCriticalSection(&sysfileSection);
1099 #endif
1101 /* instead of pfree'ing filename, remember it for next time */
1102 if (last_file_name != NULL)
1103 pfree(last_file_name);
1104 last_file_name = filename;
1109 /* same as above, but for csv file. */
1111 if (csvlogFile != NULL && (
1112 time_based_rotation ||
1113 (size_rotation_for & LOG_DESTINATION_STDERR)))
1115 if (Log_truncate_on_rotation && time_based_rotation &&
1116 last_csvfile_name != NULL &&
1117 strcmp(csvfilename, last_csvfile_name) != 0)
1119 fh = fopen(csvfilename, "w");
1120 else
1121 fh = fopen(csvfilename, "a");
1123 if (!fh)
1125 int saveerrno = errno;
1127 ereport(LOG,
1128 (errcode_for_file_access(),
1129 errmsg("could not open new log file \"%s\": %m",
1130 csvfilename)));
1133 * ENFILE/EMFILE are not too surprising on a busy system; just keep
1134 * using the old file till we manage to get a new one. Otherwise,
1135 * assume something's wrong with Log_directory and stop trying to
1136 * create files.
1138 if (saveerrno != ENFILE && saveerrno != EMFILE)
1140 ereport(LOG,
1141 (errmsg("disabling automatic rotation (use SIGHUP to reenable)")));
1142 Log_RotationAge = 0;
1143 Log_RotationSize = 0;
1145 pfree(csvfilename);
1146 return;
1149 setvbuf(fh, NULL, LBF_MODE, 0);
1151 #ifdef WIN32
1152 _setmode(_fileno(fh), _O_TEXT); /* use CRLF line endings on Windows */
1153 #endif
1155 /* On Windows, need to interlock against data-transfer thread */
1156 #ifdef WIN32
1157 EnterCriticalSection(&sysfileSection);
1158 #endif
1159 fclose(csvlogFile);
1160 csvlogFile = fh;
1161 #ifdef WIN32
1162 LeaveCriticalSection(&sysfileSection);
1163 #endif
1165 /* instead of pfree'ing filename, remember it for next time */
1166 if (last_csvfile_name != NULL)
1167 pfree(last_csvfile_name);
1168 last_csvfile_name = filename;
1171 set_next_rotation_time();
1177 * construct logfile name using timestamp information
1179 * Result is palloc'd.
1181 static char *
1182 logfile_getname(pg_time_t timestamp, char * suffix)
1184 char *filename;
1185 int len;
1187 filename = palloc(MAXPGPATH);
1189 snprintf(filename, MAXPGPATH, "%s/", Log_directory);
1191 len = strlen(filename);
1193 if (strchr(Log_filename, '%'))
1195 /* treat it as a strftime pattern */
1196 pg_strftime(filename + len, MAXPGPATH - len, Log_filename,
1197 pg_localtime(&timestamp, log_timezone));
1199 else
1201 /* no strftime escapes, so append timestamp to new filename */
1202 snprintf(filename + len, MAXPGPATH - len, "%s.%lu",
1203 Log_filename, (unsigned long) timestamp);
1206 if (suffix != NULL)
1208 len = strlen(filename);
1209 if (len > 4 && (strcmp(filename+(len-4),".log") == 0))
1210 len -= 4;
1211 strncpy(filename + len, suffix, MAXPGPATH - len);
1214 return filename;
1218 * Determine the next planned rotation time, and store in next_rotation_time.
1220 static void
1221 set_next_rotation_time(void)
1223 pg_time_t now;
1224 struct pg_tm *tm;
1225 int rotinterval;
1227 /* nothing to do if time-based rotation is disabled */
1228 if (Log_RotationAge <= 0)
1229 return;
1232 * The requirements here are to choose the next time > now that is a
1233 * "multiple" of the log rotation interval. "Multiple" can be interpreted
1234 * fairly loosely. In this version we align to log_timezone rather than
1235 * GMT.
1237 rotinterval = Log_RotationAge * SECS_PER_MINUTE; /* convert to seconds */
1238 now = (pg_time_t) time(NULL);
1239 tm = pg_localtime(&now, log_timezone);
1240 now += tm->tm_gmtoff;
1241 now -= now % rotinterval;
1242 now += rotinterval;
1243 now -= tm->tm_gmtoff;
1244 next_rotation_time = now;
1247 /* --------------------------------
1248 * signal handler routines
1249 * --------------------------------
1252 /* SIGHUP: set flag to reload config file */
1253 static void
1254 sigHupHandler(SIGNAL_ARGS)
1256 got_SIGHUP = true;
1259 /* SIGUSR1: set flag to rotate logfile */
1260 static void
1261 sigUsr1Handler(SIGNAL_ARGS)
1263 rotation_requested = true;