~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/bin/pg_controldata/pg_controldata.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pg_controldata
 
3
 *
 
4
 * reads the data from $PGDATA/global/pg_control
 
5
 *
 
6
 * copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
 
7
 * licence: BSD
 
8
 *
 
9
 * $PostgreSQL: pgsql/src/bin/pg_controldata/pg_controldata.c,v 1.20 2004-09-23 00:47:44 neilc Exp $
 
10
 */
 
11
#include "postgres.h"
 
12
 
 
13
#include <unistd.h>
 
14
#include <time.h>
 
15
#include <sys/stat.h>
 
16
#include <fcntl.h>
 
17
 
 
18
#include "catalog/pg_control.h"
 
19
 
 
20
#define _(x) gettext((x))
 
21
 
 
22
 
 
23
static void
 
24
usage(const char *progname)
 
25
{
 
26
        printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
 
27
        printf
 
28
                (
 
29
                 _(
 
30
                   "Usage:\n"
 
31
                   "  %s [OPTION] [DATADIR]\n\n"
 
32
                   "Options:\n"
 
33
                   "  --help         show this help, then exit\n"
 
34
                   "  --version      output version information, then exit\n"
 
35
                   ),
 
36
                 progname
 
37
                );
 
38
        printf(_("\nIf no data directory (DATADIR) is specified, "
 
39
                         "the environment variable PGDATA\nis used.\n\n"));
 
40
        printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
 
41
}
 
42
 
 
43
 
 
44
static const char *
 
45
dbState(DBState state)
 
46
{
 
47
        switch (state)
 
48
        {
 
49
                case DB_STARTUP:
 
50
                        return _("starting up");
 
51
                case DB_SHUTDOWNED:
 
52
                        return _("shut down");
 
53
                case DB_SHUTDOWNING:
 
54
                        return _("shutting down");
 
55
                case DB_IN_RECOVERY:
 
56
                        return _("in recovery");
 
57
                case DB_IN_PRODUCTION:
 
58
                        return _("in production");
 
59
        }
 
60
        return _("unrecognized status code");
 
61
}
 
62
 
 
63
 
 
64
int
 
65
main(int argc, char *argv[])
 
66
{
 
67
        ControlFileData ControlFile;
 
68
        int                     fd;
 
69
        char            ControlFilePath[MAXPGPATH];
 
70
        char       *DataDir;
 
71
        crc64           crc;
 
72
        char            pgctime_str[128];
 
73
        char            ckpttime_str[128];
 
74
        char            sysident_str[32];
 
75
        char       *strftime_fmt = "%c";
 
76
        const char *progname;
 
77
 
 
78
        set_pglocale_pgservice(argv[0], "pg_controldata");
 
79
 
 
80
        progname = get_progname(argv[0]);
 
81
 
 
82
        if (argc > 1)
 
83
        {
 
84
                if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
 
85
                {
 
86
                        usage(progname);
 
87
                        exit(0);
 
88
                }
 
89
                if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
 
90
                {
 
91
                        puts("pg_controldata (PostgreSQL) " PG_VERSION);
 
92
                        exit(0);
 
93
                }
 
94
        }
 
95
 
 
96
        if (argc > 1)
 
97
                DataDir = argv[1];
 
98
        else
 
99
                DataDir = getenv("PGDATA");
 
100
        if (DataDir == NULL)
 
101
        {
 
102
                fprintf(stderr, _("%s: no data directory specified\n"), progname);
 
103
                fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
104
                exit(1);
 
105
        }
 
106
 
 
107
        snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
 
108
 
 
109
        if ((fd = open(ControlFilePath, O_RDONLY)) == -1)
 
110
        {
 
111
                fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
 
112
                                progname, ControlFilePath, strerror(errno));
 
113
                exit(2);
 
114
        }
 
115
 
 
116
        if (read(fd, &ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
 
117
        {
 
118
                fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
 
119
                                progname, ControlFilePath, strerror(errno));
 
120
                exit(2);
 
121
        }
 
122
        close(fd);
 
123
 
 
124
        /* Check the CRC. */
 
125
        INIT_CRC64(crc);
 
126
        COMP_CRC64(crc,
 
127
                           (char *) &ControlFile + sizeof(crc64),
 
128
                           sizeof(ControlFileData) - sizeof(crc64));
 
129
        FIN_CRC64(crc);
 
130
 
 
131
        if (!EQ_CRC64(crc, ControlFile.crc))
 
132
                printf(_("WARNING: Calculated CRC checksum does not match value stored in file.\n"
 
133
                                 "Either the file is corrupt, or it has a different layout than this program\n"
 
134
                         "is expecting.  The results below are untrustworthy.\n\n"));
 
135
 
 
136
        /*
 
137
         * Use variable for format to suppress overly-anal-retentive gcc
 
138
         * warning about %c
 
139
         */
 
140
        strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt,
 
141
                         localtime(&(ControlFile.time)));
 
142
        strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt,
 
143
                         localtime(&(ControlFile.checkPointCopy.time)));
 
144
 
 
145
        /*
 
146
         * Format system_identifier separately to keep platform-dependent
 
147
         * format code out of the translatable message string.
 
148
         */
 
149
        snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
 
150
                         ControlFile.system_identifier);
 
151
 
 
152
        printf(_("pg_control version number:            %u\n"), ControlFile.pg_control_version);
 
153
        printf(_("Catalog version number:               %u\n"), ControlFile.catalog_version_no);
 
154
        printf(_("Database system identifier:           %s\n"), sysident_str);
 
155
        printf(_("Database cluster state:               %s\n"), dbState(ControlFile.state));
 
156
        printf(_("pg_control last modified:             %s\n"), pgctime_str);
 
157
        printf(_("Current log file ID:                  %u\n"), ControlFile.logId);
 
158
        printf(_("Next log file segment:                %u\n"), ControlFile.logSeg);
 
159
        printf(_("Latest checkpoint location:           %X/%X\n"),
 
160
                   ControlFile.checkPoint.xlogid, ControlFile.checkPoint.xrecoff);
 
161
        printf(_("Prior checkpoint location:            %X/%X\n"),
 
162
                   ControlFile.prevCheckPoint.xlogid, ControlFile.prevCheckPoint.xrecoff);
 
163
        printf(_("Latest checkpoint's REDO location:    %X/%X\n"),
 
164
                   ControlFile.checkPointCopy.redo.xlogid, ControlFile.checkPointCopy.redo.xrecoff);
 
165
        printf(_("Latest checkpoint's UNDO location:    %X/%X\n"),
 
166
                   ControlFile.checkPointCopy.undo.xlogid, ControlFile.checkPointCopy.undo.xrecoff);
 
167
        printf(_("Latest checkpoint's TimeLineID:       %u\n"), ControlFile.checkPointCopy.ThisTimeLineID);
 
168
        printf(_("Latest checkpoint's NextXID:          %u\n"), ControlFile.checkPointCopy.nextXid);
 
169
        printf(_("Latest checkpoint's NextOID:          %u\n"), ControlFile.checkPointCopy.nextOid);
 
170
        printf(_("Time of latest checkpoint:            %s\n"), ckpttime_str);
 
171
        printf(_("Database block size:                  %u\n"), ControlFile.blcksz);
 
172
        printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
 
173
        printf(_("Bytes per WAL segment:                %u\n"), ControlFile.xlog_seg_size);
 
174
        printf(_("Maximum length of identifiers:        %u\n"), ControlFile.nameDataLen);
 
175
        printf(_("Maximum number of function arguments: %u\n"), ControlFile.funcMaxArgs);
 
176
        printf(_("Date/time type storage:               %s\n"),
 
177
                   (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
 
178
        printf(_("Maximum length of locale name:        %u\n"), ControlFile.localeBuflen);
 
179
        printf(_("LC_COLLATE:                           %s\n"), ControlFile.lc_collate);
 
180
        printf(_("LC_CTYPE:                             %s\n"), ControlFile.lc_ctype);
 
181
 
 
182
        return 0;
 
183
}