PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgarch.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/fork_process.h"
#include "postmaster/pgarch.h"
#include "postmaster/postmaster.h"
#include "storage/dsm.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
Include dependency graph for pgarch.c:

Go to the source code of this file.

Macros

#define PGARCH_AUTOWAKE_INTERVAL
 
#define PGARCH_RESTART_INTERVAL
 
#define NUM_ARCHIVE_RETRIES   3
 

Functions

NON_EXEC_STATIC void PgArchiverMain (int argc, char *argv[]) pg_attribute_noreturn()
 
static void pgarch_exit (SIGNAL_ARGS)
 
static void ArchSigHupHandler (SIGNAL_ARGS)
 
static void ArchSigTermHandler (SIGNAL_ARGS)
 
static void pgarch_waken (SIGNAL_ARGS)
 
static void pgarch_waken_stop (SIGNAL_ARGS)
 
static void pgarch_MainLoop (void)
 
static void pgarch_ArchiverCopyLoop (void)
 
static bool pgarch_archiveXlog (char *xlog)
 
static bool pgarch_readyXlog (char *xlog)
 
static void pgarch_archiveDone (char *xlog)
 
int pgarch_start (void)
 

Variables

static time_t last_pgarch_start_time
 
static time_t last_sigterm_time = 0
 
static volatile sig_atomic_t got_SIGHUP = false
 
static volatile sig_atomic_t got_SIGTERM = false
 
static volatile sig_atomic_t wakened = false
 
static volatile sig_atomic_t ready_to_stop = false
 

Macro Definition Documentation

#define NUM_ARCHIVE_RETRIES   3

Definition at line 63 of file pgarch.c.

Referenced by pgarch_ArchiverCopyLoop().

#define PGARCH_AUTOWAKE_INTERVAL
Value:
60 /* How often to force a poll of the
* archive status directory; in
* seconds. */

Definition at line 57 of file pgarch.c.

Referenced by pgarch_MainLoop().

#define PGARCH_RESTART_INTERVAL
Value:
10 /* How often to attempt to restart a
* failed archiver; in seconds. */

Definition at line 60 of file pgarch.c.

Referenced by pgarch_start().

Function Documentation

static void ArchSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 257 of file pgarch.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by PgArchiverMain().

258 {
259  int save_errno = errno;
260 
261  /* set flag to re-read config file at next convenient time */
262  got_SIGHUP = true;
263  SetLatch(MyLatch);
264 
265  errno = save_errno;
266 }
static volatile sig_atomic_t got_SIGHUP
Definition: pgarch.c:76
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
struct Latch * MyLatch
Definition: globals.c:51
static void ArchSigTermHandler ( SIGNAL_ARGS  )
static

Definition at line 270 of file pgarch.c.

References got_SIGTERM, MyLatch, and SetLatch().

Referenced by PgArchiverMain().

271 {
272  int save_errno = errno;
273 
274  /*
275  * The postmaster never sends us SIGTERM, so we assume that this means
276  * that init is trying to shut down the whole system. If we hang around
277  * too long we'll get SIGKILL'd. Set flag to prevent starting any more
278  * archive commands.
279  */
280  got_SIGTERM = true;
281  SetLatch(MyLatch);
282 
283  errno = save_errno;
284 }
static volatile sig_atomic_t got_SIGTERM
Definition: pgarch.c:77
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
struct Latch * MyLatch
Definition: globals.c:51
static void pgarch_archiveDone ( char *  xlog)
static

Definition at line 725 of file pgarch.c.

References durable_rename(), MAXPGPATH, StatusFilePath, and WARNING.

Referenced by pgarch_ArchiverCopyLoop().

726 {
727  char rlogready[MAXPGPATH];
728  char rlogdone[MAXPGPATH];
729 
730  StatusFilePath(rlogready, xlog, ".ready");
731  StatusFilePath(rlogdone, xlog, ".done");
732  (void) durable_rename(rlogready, rlogdone, WARNING);
733 }
#define StatusFilePath(path, xlog, suffix)
#define MAXPGPATH
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:593
#define WARNING
Definition: elog.h:40
static void pgarch_ArchiverCopyLoop ( void  )
static

Definition at line 416 of file pgarch.c.

References ereport, errmsg(), got_SIGHUP, got_SIGTERM, MAX_XFN_CHARS, NUM_ARCHIVE_RETRIES, pg_usleep(), pgarch_archiveDone(), pgarch_archiveXlog(), pgarch_readyXlog(), PGC_SIGHUP, pgstat_send_archiver(), PostmasterIsAlive(), ProcessConfigFile(), WARNING, and XLogArchiveCommandSet.

Referenced by pgarch_MainLoop().

417 {
418  char xlog[MAX_XFN_CHARS + 1];
419 
420  /*
421  * loop through all xlogs with archive_status of .ready and archive
422  * them...mostly we expect this to be a single file, though it is possible
423  * some backend will add files onto the list of those that need archiving
424  * while we are still copying earlier archives
425  */
426  while (pgarch_readyXlog(xlog))
427  {
428  int failures = 0;
429 
430  for (;;)
431  {
432  /*
433  * Do not initiate any more archive commands after receiving
434  * SIGTERM, nor after the postmaster has died unexpectedly. The
435  * first condition is to try to keep from having init SIGKILL the
436  * command, and the second is to avoid conflicts with another
437  * archiver spawned by a newer postmaster.
438  */
439  if (got_SIGTERM || !PostmasterIsAlive())
440  return;
441 
442  /*
443  * Check for config update. This is so that we'll adopt a new
444  * setting for archive_command as soon as possible, even if there
445  * is a backlog of files to be archived.
446  */
447  if (got_SIGHUP)
448  {
449  got_SIGHUP = false;
451  }
452 
453  /* can't do anything if no command ... */
454  if (!XLogArchiveCommandSet())
455  {
457  (errmsg("archive_mode enabled, yet archive_command is not set")));
458  return;
459  }
460 
461  if (pgarch_archiveXlog(xlog))
462  {
463  /* successful */
464  pgarch_archiveDone(xlog);
465 
466  /*
467  * Tell the collector about the WAL file that we successfully
468  * archived
469  */
470  pgstat_send_archiver(xlog, false);
471 
472  break; /* out of inner retry loop */
473  }
474  else
475  {
476  /*
477  * Tell the collector about the WAL file that we failed to
478  * archive
479  */
480  pgstat_send_archiver(xlog, true);
481 
482  if (++failures >= NUM_ARCHIVE_RETRIES)
483  {
485  (errmsg("archiving transaction log file \"%s\" failed too many times, will try again later",
486  xlog)));
487  return; /* give up archiving for now */
488  }
489  pg_usleep(1000000L); /* wait a bit before retrying */
490  }
491  }
492  }
493 }
void ProcessConfigFile(GucContext context)
#define XLogArchiveCommandSet()
Definition: xlog.h:139
static volatile sig_atomic_t got_SIGHUP
Definition: pgarch.c:76
void pg_usleep(long microsec)
Definition: signal.c:53
static bool pgarch_readyXlog(char *xlog)
Definition: pgarch.c:662
#define NUM_ARCHIVE_RETRIES
Definition: pgarch.c:63
static bool pgarch_archiveXlog(char *xlog)
Definition: pgarch.c:503
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
static void pgarch_archiveDone(char *xlog)
Definition: pgarch.c:725
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: guc.h:72
#define WARNING
Definition: elog.h:40
#define MAX_XFN_CHARS
Definition: pgarch.h:26
static volatile sig_atomic_t got_SIGTERM
Definition: pgarch.c:77
void pgstat_send_archiver(const char *xlog, bool failed)
Definition: pgstat.c:3889
int errmsg(const char *fmt,...)
Definition: elog.c:797
static bool pgarch_archiveXlog ( char *  xlog)
static

Definition at line 503 of file pgarch.c.

References DEBUG1, DEBUG3, ereport, errdetail(), errhint(), errmsg(), errmsg_internal(), FATAL, LOG, make_native_path(), MAXFNAMELEN, MAXPGPATH, set_ps_display(), snprintf(), strlcpy(), WEXITSTATUS, WIFEXITED, WIFSIGNALED, WTERMSIG, XLogArchiveCommand, and XLOGDIR.

Referenced by pgarch_ArchiverCopyLoop().

504 {
505  char xlogarchcmd[MAXPGPATH];
506  char pathname[MAXPGPATH];
507  char activitymsg[MAXFNAMELEN + 16];
508  char *dp;
509  char *endp;
510  const char *sp;
511  int rc;
512 
513  snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
514 
515  /*
516  * construct the command to be executed
517  */
518  dp = xlogarchcmd;
519  endp = xlogarchcmd + MAXPGPATH - 1;
520  *endp = '\0';
521 
522  for (sp = XLogArchiveCommand; *sp; sp++)
523  {
524  if (*sp == '%')
525  {
526  switch (sp[1])
527  {
528  case 'p':
529  /* %p: relative path of source file */
530  sp++;
531  strlcpy(dp, pathname, endp - dp);
532  make_native_path(dp);
533  dp += strlen(dp);
534  break;
535  case 'f':
536  /* %f: filename of source file */
537  sp++;
538  strlcpy(dp, xlog, endp - dp);
539  dp += strlen(dp);
540  break;
541  case '%':
542  /* convert %% to a single % */
543  sp++;
544  if (dp < endp)
545  *dp++ = *sp;
546  break;
547  default:
548  /* otherwise treat the % as not special */
549  if (dp < endp)
550  *dp++ = *sp;
551  break;
552  }
553  }
554  else
555  {
556  if (dp < endp)
557  *dp++ = *sp;
558  }
559  }
560  *dp = '\0';
561 
562  ereport(DEBUG3,
563  (errmsg_internal("executing archive command \"%s\"",
564  xlogarchcmd)));
565 
566  /* Report archive activity in PS display */
567  snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
568  set_ps_display(activitymsg, false);
569 
570  rc = system(xlogarchcmd);
571  if (rc != 0)
572  {
573  /*
574  * If either the shell itself, or a called command, died on a signal,
575  * abort the archiver. We do this because system() ignores SIGINT and
576  * SIGQUIT while waiting; so a signal is very likely something that
577  * should have interrupted us too. If we overreact it's no big deal,
578  * the postmaster will just start the archiver again.
579  *
580  * Per the Single Unix Spec, shells report exit status > 128 when a
581  * called command died on a signal.
582  */
583  int lev = (WIFSIGNALED(rc) || WEXITSTATUS(rc) > 128) ? FATAL : LOG;
584 
585  if (WIFEXITED(rc))
586  {
587  ereport(lev,
588  (errmsg("archive command failed with exit code %d",
589  WEXITSTATUS(rc)),
590  errdetail("The failed archive command was: %s",
591  xlogarchcmd)));
592  }
593  else if (WIFSIGNALED(rc))
594  {
595 #if defined(WIN32)
596  ereport(lev,
597  (errmsg("archive command was terminated by exception 0x%X",
598  WTERMSIG(rc)),
599  errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
600  errdetail("The failed archive command was: %s",
601  xlogarchcmd)));
602 #elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
603  ereport(lev,
604  (errmsg("archive command was terminated by signal %d: %s",
605  WTERMSIG(rc),
606  WTERMSIG(rc) < NSIG ? sys_siglist[WTERMSIG(rc)] : "(unknown)"),
607  errdetail("The failed archive command was: %s",
608  xlogarchcmd)));
609 #else
610  ereport(lev,
611  (errmsg("archive command was terminated by signal %d",
612  WTERMSIG(rc)),
613  errdetail("The failed archive command was: %s",
614  xlogarchcmd)));
615 #endif
616  }
617  else
618  {
619  ereport(lev,
620  (errmsg("archive command exited with unrecognized status %d",
621  rc),
622  errdetail("The failed archive command was: %s",
623  xlogarchcmd)));
624  }
625 
626  snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
627  set_ps_display(activitymsg, false);
628 
629  return false;
630  }
631  ereport(DEBUG1,
632  (errmsg("archived transaction log file \"%s\"", xlog)));
633 
634  snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
635  set_ps_display(activitymsg, false);
636 
637  return true;
638 }
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:987
#define DEBUG3
Definition: elog.h:23
void make_native_path(char *path)
Definition: path.c:166
void set_ps_display(const char *activity, bool force)
Definition: ps_status.c:326
#define WIFEXITED(w)
Definition: win32.h:180
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOG
Definition: elog.h:26
#define WIFSIGNALED(w)
Definition: win32.h:181
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
char * XLogArchiveCommand
Definition: xlog.c:95
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
#define MAXFNAMELEN
#define XLOGDIR
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define WEXITSTATUS(w)
Definition: win32.h:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define WTERMSIG(w)
Definition: win32.h:183
static void pgarch_exit ( SIGNAL_ARGS  )
static

Definition at line 249 of file pgarch.c.

Referenced by PgArchiverMain().

250 {
251  /* SIGQUIT means curl up and die ... */
252  exit(1);
253 }
static void pgarch_MainLoop ( void  )
static

Definition at line 318 of file pgarch.c.

References got_SIGHUP, got_SIGTERM, last_sigterm_time, MyLatch, NULL, pgarch_ArchiverCopyLoop(), PGARCH_AUTOWAKE_INTERVAL, PGC_SIGHUP, PostmasterIsAlive(), ProcessConfigFile(), ready_to_stop, ResetLatch(), WAIT_EVENT_ARCHIVER_MAIN, WaitLatch(), wakened, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

Referenced by PgArchiverMain().

319 {
320  pg_time_t last_copy_time = 0;
321  bool time_to_stop;
322 
323  /*
324  * We run the copy loop immediately upon entry, in case there are
325  * unarchived files left over from a previous database run (or maybe the
326  * archiver died unexpectedly). After that we wait for a signal or
327  * timeout before doing more.
328  */
329  wakened = true;
330 
331  /*
332  * There shouldn't be anything for the archiver to do except to wait for a
333  * signal ... however, the archiver exists to protect our data, so she
334  * wakes up occasionally to allow herself to be proactive.
335  */
336  do
337  {
339 
340  /* When we get SIGUSR2, we do one more archive cycle, then exit */
341  time_to_stop = ready_to_stop;
342 
343  /* Check for config update */
344  if (got_SIGHUP)
345  {
346  got_SIGHUP = false;
348  }
349 
350  /*
351  * If we've gotten SIGTERM, we normally just sit and do nothing until
352  * SIGUSR2 arrives. However, that means a random SIGTERM would
353  * disable archiving indefinitely, which doesn't seem like a good
354  * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
355  * that the postmaster can start a new archiver if needed.
356  */
357  if (got_SIGTERM)
358  {
359  time_t curtime = time(NULL);
360 
361  if (last_sigterm_time == 0)
362  last_sigterm_time = curtime;
363  else if ((unsigned int) (curtime - last_sigterm_time) >=
364  (unsigned int) 60)
365  break;
366  }
367 
368  /* Do what we're here for */
369  if (wakened || time_to_stop)
370  {
371  wakened = false;
373  last_copy_time = time(NULL);
374  }
375 
376  /*
377  * Sleep until a signal is received, or until a poll is forced by
378  * PGARCH_AUTOWAKE_INTERVAL having passed since last_copy_time, or
379  * until postmaster dies.
380  */
381  if (!time_to_stop) /* Don't wait during last iteration */
382  {
383  pg_time_t curtime = (pg_time_t) time(NULL);
384  int timeout;
385 
386  timeout = PGARCH_AUTOWAKE_INTERVAL - (curtime - last_copy_time);
387  if (timeout > 0)
388  {
389  int rc;
390 
391  rc = WaitLatch(MyLatch,
393  timeout * 1000L,
395  if (rc & WL_TIMEOUT)
396  wakened = true;
397  }
398  else
399  wakened = true;
400  }
401 
402  /*
403  * The archiver quits either when the postmaster dies (not expected)
404  * or after completing one more archiving cycle after receiving
405  * SIGUSR2.
406  */
407  } while (PostmasterIsAlive() && !time_to_stop);
408 }
int64 pg_time_t
Definition: pgtime.h:23
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
static time_t last_sigterm_time
Definition: pgarch.c:71
void ResetLatch(volatile Latch *latch)
Definition: latch.c:461
static volatile sig_atomic_t got_SIGHUP
Definition: pgarch.c:76
#define PGARCH_AUTOWAKE_INTERVAL
Definition: pgarch.c:57
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:300
static volatile sig_atomic_t wakened
Definition: pgarch.c:78
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
static void pgarch_ArchiverCopyLoop(void)
Definition: pgarch.c:416
Definition: guc.h:72
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
static volatile sig_atomic_t got_SIGTERM
Definition: pgarch.c:77
#define NULL
Definition: c.h:229
struct Latch * MyLatch
Definition: globals.c:51
static volatile sig_atomic_t ready_to_stop
Definition: pgarch.c:79
#define WL_LATCH_SET
Definition: latch.h:124
static bool pgarch_readyXlog ( char *  xlog)
static

Definition at line 662 of file pgarch.c.

References AllocateDir(), dirent::d_name, ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), MAX_XFN_CHARS, MAXPGPATH, MIN_XFN_CHARS, NULL, ReadDir(), snprintf(), VALID_XFN_CHARS, and XLOGDIR.

Referenced by pgarch_ArchiverCopyLoop().

663 {
664  /*
665  * open xlog status directory and read through list of xlogs that have the
666  * .ready suffix, looking for earliest file. It is possible to optimise
667  * this code, though only a single file is expected on the vast majority
668  * of calls, so....
669  */
670  char XLogArchiveStatusDir[MAXPGPATH];
671  char newxlog[MAX_XFN_CHARS + 6 + 1];
672  DIR *rldir;
673  struct dirent *rlde;
674  bool found = false;
675 
676  snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
677  rldir = AllocateDir(XLogArchiveStatusDir);
678  if (rldir == NULL)
679  ereport(ERROR,
681  errmsg("could not open archive status directory \"%s\": %m",
682  XLogArchiveStatusDir)));
683 
684  while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
685  {
686  int basenamelen = (int) strlen(rlde->d_name) - 6;
687 
688  if (basenamelen >= MIN_XFN_CHARS &&
689  basenamelen <= MAX_XFN_CHARS &&
690  strspn(rlde->d_name, VALID_XFN_CHARS) >= basenamelen &&
691  strcmp(rlde->d_name + basenamelen, ".ready") == 0)
692  {
693  if (!found)
694  {
695  strcpy(newxlog, rlde->d_name);
696  found = true;
697  }
698  else
699  {
700  if (strcmp(rlde->d_name, newxlog) < 0)
701  strcpy(newxlog, rlde->d_name);
702  }
703  }
704  }
705  FreeDir(rldir);
706 
707  if (found)
708  {
709  /* truncate off the .ready */
710  newxlog[strlen(newxlog) - 6] = '\0';
711  strcpy(xlog, newxlog);
712  }
713  return found;
714 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Definition: dirent.h:9
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
#define MIN_XFN_CHARS
Definition: pgarch.h:25
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2298
#define ereport(elevel, rest)
Definition: elog.h:122
#define XLOGDIR
#define VALID_XFN_CHARS
Definition: pgarch.h:27
#define MAX_XFN_CHARS
Definition: pgarch.h:26
#define NULL
Definition: c.h:229
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2364
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
int FreeDir(DIR *dir)
Definition: fd.c:2407
int pgarch_start ( void  )

Definition at line 118 of file pgarch.c.

References ClosePostmasterPorts(), dsm_detach_all(), ereport, errmsg(), fork_process(), InitPostmasterChild(), last_pgarch_start_time, LOG, NULL, PGARCH_RESTART_INTERVAL, PgArchiverMain(), PGSharedMemoryDetach(), and XLogArchivingActive.

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

119 {
120  time_t curtime;
121  pid_t pgArchPid;
122 
123  /*
124  * Do nothing if no archiver needed
125  */
126  if (!XLogArchivingActive())
127  return 0;
128 
129  /*
130  * Do nothing if too soon since last archiver start. This is a safety
131  * valve to protect against continuous respawn attempts if the archiver is
132  * dying immediately at launch. Note that since we will be re-called from
133  * the postmaster main loop, we will get another chance later.
134  */
135  curtime = time(NULL);
136  if ((unsigned int) (curtime - last_pgarch_start_time) <
137  (unsigned int) PGARCH_RESTART_INTERVAL)
138  return 0;
139  last_pgarch_start_time = curtime;
140 
141 #ifdef EXEC_BACKEND
142  switch ((pgArchPid = pgarch_forkexec()))
143 #else
144  switch ((pgArchPid = fork_process()))
145 #endif
146  {
147  case -1:
148  ereport(LOG,
149  (errmsg("could not fork archiver: %m")));
150  return 0;
151 
152 #ifndef EXEC_BACKEND
153  case 0:
154  /* in postmaster child ... */
156 
157  /* Close the postmaster's sockets */
158  ClosePostmasterPorts(false);
159 
160  /* Drop our connection to postmaster's shared memory, as well */
161  dsm_detach_all();
163 
164  PgArchiverMain(0, NULL);
165  break;
166 #endif
167 
168  default:
169  return (int) pgArchPid;
170  }
171 
172  /* shouldn't get here */
173  return 0;
174 }
void InitPostmasterChild(void)
Definition: miscinit.c:175
#define PGARCH_RESTART_INTERVAL
Definition: pgarch.c:60
pid_t fork_process(void)
Definition: fork_process.c:31
static time_t last_pgarch_start_time
Definition: pgarch.c:70
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2421
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:768
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define XLogArchivingActive()
Definition: xlog.h:134
void dsm_detach_all(void)
Definition: dsm.c:654
int errmsg(const char *fmt,...)
Definition: elog.c:797
NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: pgarch.c:216
static void pgarch_waken ( SIGNAL_ARGS  )
static

Definition at line 288 of file pgarch.c.

References MyLatch, SetLatch(), and wakened.

Referenced by PgArchiverMain().

289 {
290  int save_errno = errno;
291 
292  /* set flag that there is work to be done */
293  wakened = true;
294  SetLatch(MyLatch);
295 
296  errno = save_errno;
297 }
static volatile sig_atomic_t wakened
Definition: pgarch.c:78
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
struct Latch * MyLatch
Definition: globals.c:51
static void pgarch_waken_stop ( SIGNAL_ARGS  )
static

Definition at line 301 of file pgarch.c.

References MyLatch, ready_to_stop, and SetLatch().

Referenced by PgArchiverMain().

302 {
303  int save_errno = errno;
304 
305  /* set flag to do a final cycle and shut down afterwards */
306  ready_to_stop = true;
307  SetLatch(MyLatch);
308 
309  errno = save_errno;
310 }
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
struct Latch * MyLatch
Definition: globals.c:51
static volatile sig_atomic_t ready_to_stop
Definition: pgarch.c:79
NON_EXEC_STATIC void PgArchiverMain ( int  argc,
char *  argv[] 
)

Definition at line 216 of file pgarch.c.

References ArchSigHupHandler(), ArchSigTermHandler(), init_ps_display(), PG_SETMASK, pgarch_exit(), pgarch_MainLoop(), pgarch_waken(), pgarch_waken_stop(), pqsignal(), SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGCONT, SIGHUP, SIGPIPE, SIGQUIT, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGWINCH, and UnBlockSig.

Referenced by pgarch_start().

217 {
218  /*
219  * Ignore all signals usually bound to some action in the postmaster,
220  * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
221  */
223  pqsignal(SIGINT, SIG_IGN);
224  pqsignal(SIGTERM, ArchSigTermHandler);
236 
237  /*
238  * Identify myself via ps
239  */
240  init_ps_display("archiver process", "", "", "");
241 
242  pgarch_MainLoop();
243 
244  exit(0);
245 }
#define SIGUSR1
Definition: win32.h:211
#define SIGCONT
Definition: win32.h:205
static void ArchSigTermHandler(SIGNAL_ARGS)
Definition: pgarch.c:270
static void pgarch_exit(SIGNAL_ARGS)
Definition: pgarch.c:249
#define SIGWINCH
Definition: win32.h:209
#define SIGTTIN
Definition: win32.h:207
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:301
#define SIGQUIT
Definition: win32.h:197
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
#define SIG_IGN
Definition: win32.h:193
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define SIGPIPE
Definition: win32.h:201
#define SIGHUP
Definition: win32.h:196
#define SIG_DFL
Definition: win32.h:191
static void pgarch_MainLoop(void)
Definition: pgarch.c:318
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void ArchSigHupHandler(SIGNAL_ARGS)
Definition: pgarch.c:257
#define SIGTTOU
Definition: win32.h:208
#define SIGCHLD
Definition: win32.h:206
#define SIGALRM
Definition: win32.h:202
#define SIGUSR2
Definition: win32.h:212
static void pgarch_waken(SIGNAL_ARGS)
Definition: pgarch.c:288
void init_ps_display(const char *username, const char *dbname, const char *host_info, const char *initial_str)
Definition: ps_status.c:244

Variable Documentation

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 76 of file pgarch.c.

Referenced by ArchSigHupHandler(), pgarch_ArchiverCopyLoop(), and pgarch_MainLoop().

volatile sig_atomic_t got_SIGTERM = false
static

Definition at line 77 of file pgarch.c.

Referenced by ArchSigTermHandler(), pgarch_ArchiverCopyLoop(), and pgarch_MainLoop().

time_t last_pgarch_start_time
static

Definition at line 70 of file pgarch.c.

Referenced by pgarch_start().

time_t last_sigterm_time = 0
static

Definition at line 71 of file pgarch.c.

Referenced by pgarch_MainLoop().

volatile sig_atomic_t ready_to_stop = false
static

Definition at line 79 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken_stop().

volatile sig_atomic_t wakened = false
static

Definition at line 78 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken().