166{
167#ifndef WIN32
169 int bytes_in_logbuffer = 0;
170#endif
171 char *currentLogDir;
172 char *currentLogFilename;
173 int currentLogRotationAge;
176
177 /*
178 * Re-open the error output files that were opened by SysLogger_Start().
179 *
180 * We expect this will always succeed, which is too optimistic, but if it
181 * fails there's not a lot we can do to report the problem anyway. As
182 * coded, we'll just crash on a null pointer dereference after failure...
183 */
184#ifdef EXEC_BACKEND
185 {
187
188 Assert(startup_data_len ==
sizeof(*slsdata));
192 }
193#else
194 Assert(startup_data_len == 0);
195#endif
196
197 /*
198 * Now that we're done reading the startup data, release postmaster's
199 * working memory context.
200 */
202 {
205 }
206
208
211
212 /*
213 * If we restarted, our stderr is already redirected into our own input
214 * pipe. This is of course pretty useless, not to mention that it
215 * interferes with detecting pipe EOF. Point stderr to /dev/null. This
216 * assumes that all interesting messages generated in the syslogger will
217 * come through elog.c and will be sent to write_syslogger_file.
218 */
220 {
222
223 /*
224 * The closes might look redundant, but they are not: we want to be
225 * darn sure the pipe gets closed even if the open failed. We can
226 * survive running with stderr pointing nowhere, but we can't afford
227 * to have extra pipe input descriptors hanging around.
228 *
229 * As we're just trying to reset these to go to DEVNULL, there's not
230 * much point in checking for failure from the close/dup2 calls here,
231 * if they fail then presumably the file descriptors are closed and
232 * any writes will go into the bitbucket anyway.
233 */
237 {
241 }
242 }
243
244 /*
245 * Syslogger's own stderr can't be the syslogPipe, so set it back to text
246 * mode if we didn't just close it. (It was set to binary in
247 * SubPostmasterMain).
248 */
249#ifdef WIN32
250 else
252#endif
253
254 /*
255 * Also close our copy of the write end of the pipe. This is needed to
256 * ensure we can detect pipe EOF correctly. (But note that in the restart
257 * case, the postmaster already did this.)
258 */
259#ifndef WIN32
263#else
267#endif
268
269 /*
270 * Properly accept or ignore signals the postmaster might send us
271 *
272 * Note: we ignore all termination signals, and instead exit only when all
273 * upstream processes are gone, to ensure we don't miss any dying gasps of
274 * broken backends...
275 */
276
278 * file */
286
287 /*
288 * Reset some signals that are accepted by postmaster but not here
289 */
291
293
294#ifdef WIN32
295 /* Fire up separate data transfer thread */
296 InitializeCriticalSection(&sysloggerSection);
297 EnterCriticalSection(&sysloggerSection);
298
299 threadHandle = (HANDLE) _beginthreadex(NULL, 0, pipeThread, NULL, 0, NULL);
300 if (threadHandle == 0)
301 elog(
FATAL,
"could not create syslogger data transfer thread: %m");
302#endif /* WIN32 */
303
304 /*
305 * Remember active logfiles' name(s). We recompute 'em from the reference
306 * time because passing down just the pg_time_t is a lot cheaper than
307 * passing a whole file path in the EXEC_BACKEND case.
308 */
314
315 /* remember active logfile parameters */
319 /* set next planned rotation time */
322
323 /*
324 * Reset whereToSendOutput, as the postmaster will do (but hasn't yet, at
325 * the point where we forked). This prevents duplicate output of messages
326 * from syslogger itself.
327 */
329
330 /*
331 * Set up a reusable WaitEventSet object we'll use to wait for our latch,
332 * and (except on Windows) our socket.
333 *
334 * Unlike all other postmaster child processes, we'll ignore postmaster
335 * death because we want to collect final log output from all backends and
336 * then exit last. We'll do that by running until we see EOF on the
337 * syslog pipe, which implies that all other backends have exited
338 * (including the postmaster).
339 */
342#ifndef WIN32
344#endif
345
346 /* main worker loop */
347 for (;;)
348 {
349 bool time_based_rotation = false;
350 int size_rotation_for = 0;
351 long cur_timeout;
353
354#ifndef WIN32
355 int rc;
356#endif
357
358 /* Clear any already-pending wakeups */
360
361 /*
362 * Process any requests or signals received recently.
363 */
365 {
368
369 /*
370 * Check if the log directory or filename pattern changed in
371 * postgresql.conf. If so, force rotation to make sure we're
372 * writing the logfiles in the right place.
373 */
375 {
376 pfree(currentLogDir);
379
380 /*
381 * Also, create new directory if not present; ignore errors
382 */
384 }
386 {
387 pfree(currentLogFilename);
390 }
391
392 /*
393 * Force a rotation if CSVLOG output was just turned on or off and
394 * we need to open or close csvlogFile accordingly.
395 */
399
400 /*
401 * Force a rotation if JSONLOG output was just turned on or off
402 * and we need to open or close jsonlogFile accordingly.
403 */
407
408 /*
409 * If rotation time parameter changed, reset next rotation time,
410 * but don't immediately force a rotation.
411 */
413 {
416 }
417
418 /*
419 * If we had a rotation-disabling failure, re-enable rotation
420 * attempts after SIGHUP, and force one immediately.
421 */
423 {
426 }
427
428 /*
429 * Force rewriting last log filename when reloading configuration.
430 * Even if rotation_requested is false, log_destination may have
431 * been changed and we don't want to wait the next file rotation.
432 */
434 }
435
437 {
438 /* Do a logfile rotation if it's time */
442 }
443
445 {
446 /* Do a rotation if file is too big */
448 {
451 }
454 {
457 }
460 {
463 }
464 }
465
467 {
468 /*
469 * Force rotation when both values are zero. It means the request
470 * was sent by pg_rotate_logfile() or "pg_ctl logrotate".
471 */
472 if (!time_based_rotation && size_rotation_for == 0)
477 }
478
479 /*
480 * Calculate time till next time-based rotation, so that we don't
481 * sleep longer than that. We assume the value of "now" obtained
482 * above is still close enough. Note we can't make this calculation
483 * until after calling logfile_rotate(), since it will advance
484 * next_rotation_time.
485 *
486 * Also note that we need to beware of overflow in calculation of the
487 * timeout: with large settings of Log_RotationAge, next_rotation_time
488 * could be more than INT_MAX msec in the future. In that case we'll
489 * wait no more than INT_MAX msec, and try again.
490 */
492 {
494
496 if (delay > 0)
497 {
498 if (delay > INT_MAX / 1000)
499 delay = INT_MAX / 1000;
500 cur_timeout = delay * 1000L; /* msec */
501 }
502 else
503 cur_timeout = 0;
504 }
505 else
506 cur_timeout = -1L;
507
508 /*
509 * Sleep until there's something to do
510 */
511#ifndef WIN32
513 WAIT_EVENT_SYSLOGGER_MAIN);
514
516 {
517 int bytesRead;
518
520 logbuffer + bytes_in_logbuffer,
521 sizeof(logbuffer) - bytes_in_logbuffer);
522 if (bytesRead < 0)
523 {
527 errmsg(
"could not read from logger pipe: %m")));
528 }
529 else if (bytesRead > 0)
530 {
531 bytes_in_logbuffer += bytesRead;
533 continue;
534 }
535 else
536 {
537 /*
538 * Zero bytes read when select() is saying read-ready means
539 * EOF on the pipe: that is, there are no longer any processes
540 * with the pipe write end open. Therefore, the postmaster
541 * and all backends are shut down, and we are done.
542 */
544
545 /* if there's any data left then force it out now */
547 }
548 }
549#else /* WIN32 */
550
551 /*
552 * On Windows we leave it to a separate thread to transfer data and
553 * detect pipe EOF. The main thread just wakes up to handle SIGHUP
554 * and rotation conditions.
555 *
556 * Server code isn't generally thread-safe, so we ensure that only one
557 * of the threads is active at a time by entering the critical section
558 * whenever we're not sleeping.
559 */
560 LeaveCriticalSection(&sysloggerSection);
561
563 WAIT_EVENT_SYSLOGGER_MAIN);
564
565 EnterCriticalSection(&sysloggerSection);
566#endif /* WIN32 */
567
569 {
570 /*
571 * seeing this message on the real stderr is annoying - so we make
572 * it DEBUG1 to suppress in normal use.
573 */
576
577 /*
578 * Normal exit from the syslogger is here. Note that we
579 * deliberately do not close syslogFile before exiting; this is to
580 * allow for the possibility of elog messages being generated
581 * inside proc_exit. Regular exit() will take care of flushing
582 * and closing stdio channels.
583 */
585 }
586 }
587}
Datum now(PG_FUNCTION_ARGS)
int errmsg_internal(const char *fmt,...)
#define LOG_DESTINATION_STDERR
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ConfigReloadPending
void SignalHandlerForConfigReload(SIGNAL_ARGS)
void ResetLatch(Latch *latch)
char * pstrdup(const char *in)
MemoryContext PostmasterContext
void MemoryContextDelete(MemoryContext context)
BackendType MyBackendType
CommandDest whereToSendOutput
void init_ps_display(const char *fixed_part)
static bool rotation_disabled
static void logfile_rotate(bool time_based_rotation, int size_rotation_for)
static bool pipe_eof_seen
static void update_metainfo_datafile(void)
static char * last_csv_file_name
static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
static pg_time_t next_rotation_time
static volatile sig_atomic_t rotation_requested
static void sigUsr1Handler(SIGNAL_ARGS)
static void set_next_rotation_time(void)
static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
static char * last_json_file_name
static char * last_sys_file_name
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
#define WL_SOCKET_READABLE