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 62 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 59 of file pgarch.c.

Referenced by pgarch_start().

Function Documentation

static void ArchSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 256 of file pgarch.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by PgArchiverMain().

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

Definition at line 269 of file pgarch.c.

References got_SIGTERM, MyLatch, and SetLatch().

Referenced by PgArchiverMain().

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

Definition at line 723 of file pgarch.c.

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

Referenced by pgarch_ArchiverCopyLoop().

724 {
725  char rlogready[MAXPGPATH];
726  char rlogdone[MAXPGPATH];
727 
728  StatusFilePath(rlogready, xlog, ".ready");
729  StatusFilePath(rlogdone, xlog, ".done");
730  (void) durable_rename(rlogready, rlogdone, WARNING);
731 }
#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 415 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().

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

Definition at line 502 of file pgarch.c.

References DEBUG1, DEBUG3, elog, 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().

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

Definition at line 248 of file pgarch.c.

Referenced by PgArchiverMain().

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

Definition at line 317 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().

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

Definition at line 660 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().

661 {
662  /*
663  * open xlog status directory and read through list of xlogs that have the
664  * .ready suffix, looking for earliest file. It is possible to optimise
665  * this code, though only a single file is expected on the vast majority
666  * of calls, so....
667  */
668  char XLogArchiveStatusDir[MAXPGPATH];
669  char newxlog[MAX_XFN_CHARS + 6 + 1];
670  DIR *rldir;
671  struct dirent *rlde;
672  bool found = false;
673 
674  snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
675  rldir = AllocateDir(XLogArchiveStatusDir);
676  if (rldir == NULL)
677  ereport(ERROR,
679  errmsg("could not open archive status directory \"%s\": %m",
680  XLogArchiveStatusDir)));
681 
682  while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
683  {
684  int basenamelen = (int) strlen(rlde->d_name) - 6;
685 
686  if (basenamelen >= MIN_XFN_CHARS &&
687  basenamelen <= MAX_XFN_CHARS &&
688  strspn(rlde->d_name, VALID_XFN_CHARS) >= basenamelen &&
689  strcmp(rlde->d_name + basenamelen, ".ready") == 0)
690  {
691  if (!found)
692  {
693  strcpy(newxlog, rlde->d_name);
694  found = true;
695  }
696  else
697  {
698  if (strcmp(rlde->d_name, newxlog) < 0)
699  strcpy(newxlog, rlde->d_name);
700  }
701  }
702  }
703  FreeDir(rldir);
704 
705  if (found)
706  {
707  /* truncate off the .ready */
708  newxlog[strlen(newxlog) - 6] = '\0';
709  strcpy(xlog, newxlog);
710  }
711  return found;
712 }
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:2335
#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:2401
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:2444
int pgarch_start ( void  )

Definition at line 117 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().

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

Definition at line 287 of file pgarch.c.

References MyLatch, SetLatch(), and wakened.

Referenced by PgArchiverMain().

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

Definition at line 300 of file pgarch.c.

References MyLatch, ready_to_stop, and SetLatch().

Referenced by PgArchiverMain().

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

Definition at line 215 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().

216 {
217  /*
218  * Ignore all signals usually bound to some action in the postmaster,
219  * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
220  */
222  pqsignal(SIGINT, SIG_IGN);
223  pqsignal(SIGTERM, ArchSigTermHandler);
235 
236  /*
237  * Identify myself via ps
238  */
239  init_ps_display("archiver process", "", "", "");
240 
241  pgarch_MainLoop();
242 
243  exit(0);
244 }
#define SIGUSR1
Definition: win32.h:202
#define SIGCONT
Definition: win32.h:197
static void ArchSigTermHandler(SIGNAL_ARGS)
Definition: pgarch.c:269
static void pgarch_exit(SIGNAL_ARGS)
Definition: pgarch.c:248
#define SIGWINCH
Definition: win32.h:201
#define SIGTTIN
Definition: win32.h:199
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:300
#define SIGQUIT
Definition: win32.h:189
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
#define SIG_IGN
Definition: win32.h:185
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define SIGPIPE
Definition: win32.h:193
#define SIGHUP
Definition: win32.h:188
#define SIG_DFL
Definition: win32.h:183
static void pgarch_MainLoop(void)
Definition: pgarch.c:317
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void ArchSigHupHandler(SIGNAL_ARGS)
Definition: pgarch.c:256
#define SIGTTOU
Definition: win32.h:200
#define SIGCHLD
Definition: win32.h:198
#define SIGALRM
Definition: win32.h:194
#define SIGUSR2
Definition: win32.h:203
static void pgarch_waken(SIGNAL_ARGS)
Definition: pgarch.c:287
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 75 of file pgarch.c.

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

volatile sig_atomic_t got_SIGTERM = false
static

Definition at line 76 of file pgarch.c.

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

time_t last_pgarch_start_time
static

Definition at line 69 of file pgarch.c.

Referenced by pgarch_start().

time_t last_sigterm_time = 0
static

Definition at line 70 of file pgarch.c.

Referenced by pgarch_MainLoop().

volatile sig_atomic_t ready_to_stop = false
static

Definition at line 78 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken_stop().

volatile sig_atomic_t wakened = false
static

Definition at line 77 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken().