PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
controldata_utils.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "port/pg_crc32c.h"
Include dependency graph for controldata_utils.c:

Go to the source code of this file.

Functions

ControlFileDataget_controlfile (const char *DataDir, const char *progname, bool *crc_ok_p)
 

Function Documentation

ControlFileData* get_controlfile ( const char *  DataDir,
const char *  progname,
bool crc_ok_p 
)

Definition at line 41 of file controldata_utils.c.

References _, AssertArg, close, COMP_CRC32C, ControlFile, ControlFileData::crc, elog, EQ_CRC32C, ereport, errcode_for_file_access(), errmsg(), ERROR, EXIT_FAILURE, fd(), FIN_CRC32C, INIT_CRC32C, MAXPGPATH, offsetof, palloc(), PG_BINARY, ControlFileData::pg_control_version, read, snprintf(), and strerror().

Referenced by get_control_dbstate(), main(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), and pg_control_system().

42 {
44  int fd;
45  char ControlFilePath[MAXPGPATH];
46  pg_crc32c crc;
47 
48  AssertArg(crc_ok_p);
49 
50  ControlFile = palloc(sizeof(ControlFileData));
51  snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
52 
53  if ((fd = open(ControlFilePath, O_RDONLY | PG_BINARY, 0)) == -1)
54 #ifndef FRONTEND
55  ereport(ERROR,
57  errmsg("could not open file \"%s\" for reading: %m",
58  ControlFilePath)));
59 #else
60  {
61  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
62  progname, ControlFilePath, strerror(errno));
63  exit(EXIT_FAILURE);
64  }
65 #endif
66 
67  if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
68 #ifndef FRONTEND
69  ereport(ERROR,
71  errmsg("could not read file \"%s\": %m", ControlFilePath)));
72 #else
73  {
74  fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
75  progname, ControlFilePath, strerror(errno));
76  exit(EXIT_FAILURE);
77  }
78 #endif
79 
80  close(fd);
81 
82  /* Check the CRC. */
83  INIT_CRC32C(crc);
84  COMP_CRC32C(crc,
85  (char *) ControlFile,
87  FIN_CRC32C(crc);
88 
89  *crc_ok_p = EQ_CRC32C(crc, ControlFile->crc);
90 
91  /* Make sure the control file is valid byte order. */
92  if (ControlFile->pg_control_version % 65536 == 0 &&
93  ControlFile->pg_control_version / 65536 != 0)
94 #ifndef FRONTEND
95  elog(ERROR, _("byte ordering mismatch"));
96 #else
97  printf(_("WARNING: possible byte ordering mismatch\n"
98  "The byte ordering used to store the pg_control file might not match the one\n"
99  "used by this program. In that case the results below would be incorrect, and\n"
100  "the PostgreSQL installation would be incompatible with this data directory.\n"));
101 #endif
102 
103  return ControlFile;
104 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
uint32 pg_crc32c
Definition: pg_crc32c.h:38
uint32 pg_control_version
Definition: pg_control.h:124
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
const char * progname
Definition: pg_standby.c:37
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1038
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
pg_crc32c crc
Definition: pg_control.h:235
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:677
static ControlFileData * ControlFile
Definition: xlog.c:714
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
const char * strerror(int errnum)
Definition: strerror.c:19
#define EXIT_FAILURE
Definition: settings.h:151
char * DataDir
Definition: globals.c:59
#define elog
Definition: elog.h:219
#define close(a)
Definition: win32.h:12
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:73
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78
#define _(x)
Definition: elog.c:84
#define read(a, b, c)
Definition: win32.h:13
#define offsetof(type, field)
Definition: c.h:555