diff options
Diffstat (limited to 'src/bin/pg_resetwal/pg_resetwal.c')
-rw-r--r-- | src/bin/pg_resetwal/pg_resetwal.c | 175 |
1 files changed, 72 insertions, 103 deletions
diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index 67fc646befb..82a8ec993de 100644 --- a/src/bin/pg_resetwal/pg_resetwal.c +++ b/src/bin/pg_resetwal/pg_resetwal.c @@ -53,6 +53,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/restricted_token.h" +#include "fe_utils/logging.h" #include "storage/large_object.h" #include "pg_getopt.h" #include "getopt_long.h" @@ -115,8 +116,8 @@ main(int argc, char *argv[]) char *log_fname = NULL; int fd; + pg_logging_init(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetwal")); - progname = get_progname(argv[0]); if (argc > 1) @@ -156,13 +157,13 @@ main(int argc, char *argv[]) { /*------ translator: the second %s is a command line argument (-e, etc) */ - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-e"); + pg_log_error("invalid argument for option %s", "-e"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (set_xid_epoch == -1) { - fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname); + pg_log_error("transaction ID epoch (-e) must not be -1"); exit(1); } break; @@ -171,13 +172,13 @@ main(int argc, char *argv[]) set_xid = strtoul(optarg, &endptr, 0); if (endptr == optarg || *endptr != '\0') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-x"); + pg_log_error("invalid argument for option %s", "-x"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (set_xid == 0) { - fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname); + pg_log_error("transaction ID (-x) must not be 0"); exit(1); } break; @@ -186,14 +187,14 @@ main(int argc, char *argv[]) set_oldest_commit_ts_xid = strtoul(optarg, &endptr, 0); if (endptr == optarg || *endptr != ',') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-c"); + pg_log_error("invalid argument for option %s", "-c"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } set_newest_commit_ts_xid = strtoul(endptr + 1, &endptr2, 0); if (endptr2 == endptr + 1 || *endptr2 != '\0') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-c"); + pg_log_error("invalid argument for option %s", "-c"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -201,14 +202,14 @@ main(int argc, char *argv[]) if (set_oldest_commit_ts_xid < 2 && set_oldest_commit_ts_xid != 0) { - fprintf(stderr, _("%s: transaction ID (-c) must be either 0 or greater than or equal to 2\n"), progname); + pg_log_error("transaction ID (-c) must be either 0 or greater than or equal to 2"); exit(1); } if (set_newest_commit_ts_xid < 2 && set_newest_commit_ts_xid != 0) { - fprintf(stderr, _("%s: transaction ID (-c) must be either 0 or greater than or equal to 2\n"), progname); + pg_log_error("transaction ID (-c) must be either 0 or greater than or equal to 2"); exit(1); } break; @@ -217,13 +218,13 @@ main(int argc, char *argv[]) set_oid = strtoul(optarg, &endptr, 0); if (endptr == optarg || *endptr != '\0') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-o"); + pg_log_error("invalid argument for option %s", "-o"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (set_oid == 0) { - fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname); + pg_log_error("OID (-o) must not be 0"); exit(1); } break; @@ -232,7 +233,7 @@ main(int argc, char *argv[]) set_mxid = strtoul(optarg, &endptr, 0); if (endptr == optarg || *endptr != ',') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-m"); + pg_log_error("invalid argument for option %s", "-m"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -240,13 +241,13 @@ main(int argc, char *argv[]) set_oldestmxid = strtoul(endptr + 1, &endptr2, 0); if (endptr2 == endptr + 1 || *endptr2 != '\0') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-m"); + pg_log_error("invalid argument for option %s", "-m"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (set_mxid == 0) { - fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname); + pg_log_error("multitransaction ID (-m) must not be 0"); exit(1); } @@ -256,8 +257,7 @@ main(int argc, char *argv[]) */ if (set_oldestmxid == 0) { - fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"), - progname); + pg_log_error("oldest multitransaction ID (-m) must not be 0"); exit(1); } break; @@ -266,13 +266,13 @@ main(int argc, char *argv[]) set_mxoff = strtoul(optarg, &endptr, 0); if (endptr == optarg || *endptr != '\0') { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-O"); + pg_log_error("invalid argument for option %s", "-O"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (set_mxoff == -1) { - fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname); + pg_log_error("multitransaction offset (-O) must not be -1"); exit(1); } break; @@ -280,7 +280,7 @@ main(int argc, char *argv[]) case 'l': if (strspn(optarg, "01234567890ABCDEFabcdef") != XLOG_FNAME_LEN) { - fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-l"); + pg_log_error("invalid argument for option %s", "-l"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -296,16 +296,12 @@ main(int argc, char *argv[]) set_wal_segsize = strtol(optarg, &endptr, 10) * 1024 * 1024; if (endptr == optarg || *endptr != '\0') { - fprintf(stderr, - _("%s: argument of --wal-segsize must be a number\n"), - progname); + pg_log_error("argument of --wal-segsize must be a number"); exit(1); } if (!IsValidWalSegSize(set_wal_segsize)) { - fprintf(stderr, - _("%s: argument of --wal-segsize must be a power of 2 between 1 and 1024\n"), - progname); + pg_log_error("argument of --wal-segsize must be a power of 2 between 1 and 1024"); exit(1); } break; @@ -322,8 +318,8 @@ main(int argc, char *argv[]) /* Complain if any arguments remain */ if (optind < argc) { - fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), - progname, argv[optind]); + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -331,7 +327,7 @@ main(int argc, char *argv[]) if (DataDir == NULL) { - fprintf(stderr, _("%s: no data directory specified\n"), progname); + pg_log_error("no data directory specified"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -345,21 +341,20 @@ main(int argc, char *argv[]) #ifndef WIN32 if (geteuid() == 0) { - fprintf(stderr, _("%s: cannot be executed by \"root\"\n"), - progname); - fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"), - progname); + pg_log_error("cannot be executed by \"root\""); + pg_log_info("You must run %s as the PostgreSQL superuser.", + progname); exit(1); } #endif - get_restricted_token(progname); + get_restricted_token(); /* Set mask based on PGDATA permissions */ if (!GetDataDirectoryCreatePerm(DataDir)) { - fprintf(stderr, _("%s: could not read permissions of directory \"%s\": %s\n"), - progname, DataDir, strerror(errno)); + pg_log_error("could not read permissions of directory \"%s\": %m", + DataDir); exit(1); } @@ -367,8 +362,8 @@ main(int argc, char *argv[]) if (chdir(DataDir) < 0) { - fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"), - progname, DataDir, strerror(errno)); + pg_log_error("could not change directory to \"%s\": %m", + DataDir); exit(1); } @@ -383,16 +378,15 @@ main(int argc, char *argv[]) { if (errno != ENOENT) { - fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), - progname, "postmaster.pid", strerror(errno)); + pg_log_error("could not open file \"%s\" for reading: %m", + "postmaster.pid"); exit(1); } } else { - fprintf(stderr, _("%s: lock file \"%s\" exists\n" - "Is a server running? If not, delete the lock file and try again.\n"), - progname, "postmaster.pid"); + pg_log_error("lock file \"%s\" exists", "postmaster.pid"); + pg_log_info("Is a server running? If not, delete the lock file and try again."); exit(1); } @@ -548,8 +542,8 @@ CheckDataVersion(void) if ((ver_fd = fopen(ver_file, "r")) == NULL) { - fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), - progname, ver_file, strerror(errno)); + pg_log_error("could not open file \"%s\" for reading: %m", + ver_file); exit(1); } @@ -557,15 +551,9 @@ CheckDataVersion(void) if (!fgets(rawline, sizeof(rawline), ver_fd)) { if (!ferror(ver_fd)) - { - fprintf(stderr, _("%s: unexpected empty file \"%s\"\n"), - progname, ver_file); - } + pg_log_error("unexpected empty file \"%s\"", ver_file); else - { - fprintf(stderr, _("%s: could not read file \"%s\": %s\n"), - progname, ver_file, strerror(errno)); - } + pg_log_error("could not read file \"%s\": %m", ver_file); exit(1); } @@ -580,9 +568,9 @@ CheckDataVersion(void) if (strcmp(rawline, PG_MAJORVERSION) != 0) { - fprintf(stderr, _("%s: data directory is of wrong version\n" - "File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".\n"), - progname, ver_file, rawline, PG_MAJORVERSION); + pg_log_error("data directory is of wrong version"); + pg_log_info("File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".", + ver_file, rawline, PG_MAJORVERSION); exit(1); } @@ -611,13 +599,13 @@ ReadControlFile(void) * are we've been handed a bad DataDir path, so give up. User can do * "touch pg_control" to force us to proceed. */ - fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), - progname, XLOG_CONTROL_FILE, strerror(errno)); + pg_log_error("could not open file \"%s\" for reading: %m", + XLOG_CONTROL_FILE); if (errno == ENOENT) - fprintf(stderr, _("If you are sure the data directory path is correct, execute\n" - " touch %s\n" - "and try again.\n"), - XLOG_CONTROL_FILE); + pg_log_info("If you are sure the data directory path is correct, execute\n" + " touch %s\n" + "and try again.", + XLOG_CONTROL_FILE); exit(1); } @@ -627,8 +615,7 @@ ReadControlFile(void) len = read(fd, buffer, PG_CONTROL_FILE_SIZE); if (len < 0) { - fprintf(stderr, _("%s: could not read file \"%s\": %s\n"), - progname, XLOG_CONTROL_FILE, strerror(errno)); + pg_log_error("could not read file \"%s\": %m", XLOG_CONTROL_FILE); exit(1); } close(fd); @@ -646,9 +633,7 @@ ReadControlFile(void) if (!EQ_CRC32C(crc, ((ControlFileData *) buffer)->crc)) { /* We will use the data but treat it as guessed. */ - fprintf(stderr, - _("%s: pg_control exists but has invalid CRC; proceed with caution\n"), - progname); + pg_log_warning("pg_control exists but has invalid CRC; proceed with caution"); guessed = true; } @@ -657,11 +642,10 @@ ReadControlFile(void) /* return false if WAL segment size is not valid */ if (!IsValidWalSegSize(ControlFile.xlog_seg_size)) { - fprintf(stderr, - ngettext("%s: pg_control specifies invalid WAL segment size (%d byte); proceed with caution\n", - "%s: pg_control specifies invalid WAL segment size (%d bytes); proceed with caution\n", - ControlFile.xlog_seg_size), - progname, ControlFile.xlog_seg_size); + pg_log_warning(ngettext("pg_control specifies invalid WAL segment size (%d byte); proceed with caution", + "pg_control specifies invalid WAL segment size (%d bytes); proceed with caution", + ControlFile.xlog_seg_size), + ControlFile.xlog_seg_size); return false; } @@ -669,8 +653,7 @@ ReadControlFile(void) } /* Looks like it's a mess. */ - fprintf(stderr, _("%s: pg_control exists but is broken or wrong version; ignoring it\n"), - progname); + pg_log_warning("pg_control exists but is broken or wrong version; ignoring it"); return false; } @@ -953,7 +936,7 @@ RewriteControlFile(void) ControlFile.max_locks_per_xact = 64; /* The control file gets flushed here. */ - update_controlfile(".", progname, &ControlFile, true); + update_controlfile(".", &ControlFile, true); } @@ -989,8 +972,7 @@ FindEndOfXLOG(void) xldir = opendir(XLOGDIR); if (xldir == NULL) { - fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"), - progname, XLOGDIR, strerror(errno)); + pg_log_error("could not open directory \"%s\": %m", XLOGDIR); exit(1); } @@ -1025,15 +1007,13 @@ FindEndOfXLOG(void) if (errno) { - fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"), - progname, XLOGDIR, strerror(errno)); + pg_log_error("could not read directory \"%s\": %m", XLOGDIR); exit(1); } if (closedir(xldir)) { - fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"), - progname, XLOGDIR, strerror(errno)); + pg_log_error("could not close directory \"%s\": %m", XLOGDIR); exit(1); } @@ -1060,8 +1040,7 @@ KillExistingXLOG(void) xldir = opendir(XLOGDIR); if (xldir == NULL) { - fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"), - progname, XLOGDIR, strerror(errno)); + pg_log_error("could not open directory \"%s\": %m", XLOGDIR); exit(1); } @@ -1073,8 +1052,7 @@ KillExistingXLOG(void) snprintf(path, sizeof(path), "%s/%s", XLOGDIR, xlde->d_name); if (unlink(path) < 0) { - fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"), - progname, path, strerror(errno)); + pg_log_error("could not delete file \"%s\": %m", path); exit(1); } } @@ -1082,15 +1060,13 @@ KillExistingXLOG(void) if (errno) { - fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"), - progname, XLOGDIR, strerror(errno)); + pg_log_error("could not read directory \"%s\": %m", XLOGDIR); exit(1); } if (closedir(xldir)) { - fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"), - progname, XLOGDIR, strerror(errno)); + pg_log_error("could not close directory \"%s\": %m", XLOGDIR); exit(1); } } @@ -1111,8 +1087,7 @@ KillExistingArchiveStatus(void) xldir = opendir(ARCHSTATDIR); if (xldir == NULL) { - fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"), - progname, ARCHSTATDIR, strerror(errno)); + pg_log_error("could not open directory \"%s\": %m", ARCHSTATDIR); exit(1); } @@ -1127,8 +1102,7 @@ KillExistingArchiveStatus(void) snprintf(path, sizeof(path), "%s/%s", ARCHSTATDIR, xlde->d_name); if (unlink(path) < 0) { - fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"), - progname, path, strerror(errno)); + pg_log_error("could not delete file \"%s\": %m", path); exit(1); } } @@ -1136,15 +1110,13 @@ KillExistingArchiveStatus(void) if (errno) { - fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"), - progname, ARCHSTATDIR, strerror(errno)); + pg_log_error("could not read directory \"%s\": %m", ARCHSTATDIR); exit(1); } if (closedir(xldir)) { - fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"), - progname, ARCHSTATDIR, strerror(errno)); + pg_log_error("could not close directory \"%s\": %m", ARCHSTATDIR); exit(1); } } @@ -1211,8 +1183,7 @@ WriteEmptyXLOG(void) pg_file_create_mode); if (fd < 0) { - fprintf(stderr, _("%s: could not open file \"%s\": %s\n"), - progname, path, strerror(errno)); + pg_log_error("could not open file \"%s\": %m", path); exit(1); } @@ -1222,8 +1193,7 @@ WriteEmptyXLOG(void) /* if write didn't set errno, assume problem is no disk space */ if (errno == 0) errno = ENOSPC; - fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), - progname, path, strerror(errno)); + pg_log_error("could not write file \"%s\": %m", path); exit(1); } @@ -1236,15 +1206,14 @@ WriteEmptyXLOG(void) { if (errno == 0) errno = ENOSPC; - fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), - progname, path, strerror(errno)); + pg_log_error("could not write file \"%s\": %m", path); exit(1); } } if (fsync(fd) != 0) { - fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno)); + pg_log_error("fsync error: %m"); exit(1); } |