PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
controldata_utils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * controldata_utils.c
4  * Common code for control data file output.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  * src/common/controldata_utils.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 #ifndef FRONTEND
18 #include "postgres.h"
19 #else
20 #include "postgres_fe.h"
21 #endif
22 
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 
27 #include "catalog/pg_control.h"
29 #include "port/pg_crc32c.h"
30 
31 /*
32  * get_controlfile(char *DataDir, const char *progname, bool *crc_ok_p)
33  *
34  * Get controlfile values. The result is returned as a palloc'd copy of the
35  * control file data.
36  *
37  * crc_ok_p can be used by the caller to see whether the CRC of the control
38  * file data is correct.
39  */
41 get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
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
ControlFileData * get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
uint32 pg_control_version
Definition: pg_control.h:121
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:1044
#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:232
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:683
static ControlFileData * ControlFile
Definition: xlog.c:715
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
const char * strerror(int errnum)
Definition: strerror.c:19
#define EXIT_FAILURE
Definition: settings.h:152
char * DataDir
Definition: globals.c:60
#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:549