Properly print errors from system() in archive and restore commands

We used to assume that the only errors which could happen were ones
which set the errno, but that is not the case. We also want to give nice
output on non-zero return values and if the process was killed by a
signal.
pull/238/head
Andreas Karlsson 3 weeks ago committed by Andreas Karlsson
parent 481030de9a
commit 415cb8dd5b
  1. 18
      contrib/pg_tde/src/bin/pg_tde_archive_decrypt.c
  2. 18
      contrib/pg_tde/src/bin/pg_tde_restore_encrypt.c

@ -146,6 +146,7 @@ main(int argc, char *argv[])
char tmpdir[MAXPGPATH] = TMPFS_DIRECTORY "/pg_tde_archiveXXXXXX";
char tmppath[MAXPGPATH];
bool issegment;
int rc;
pg_logging_init(argv[0]);
progname = get_progname(argv[0]);
@ -208,9 +209,22 @@ main(int argc, char *argv[])
command = replace_percent_placeholders(command,
"ARCHIVE-COMMAND", "fp",
targetname, sourcepath);
rc = system(command);
if (system(command) != 0)
pg_fatal("ARCHIVE-COMMAND \"%s\" failed: %m", command);
if (rc != 0)
{
if (rc == -1)
pg_fatal("ARCHIVE-COMMAND \"%s\" failed: %m", command);
else if (WIFEXITED(rc))
pg_fatal("ARCHIVE-COMMAND \"%s\" failed with exit code %d",
command, WEXITSTATUS(rc));
else if (WIFSIGNALED(rc))
pg_fatal("ARCHIVE-COMMAND \"%s\" was terminated by signal %d: %s",
command, WTERMSIG(rc), pg_strsignal(WTERMSIG(rc)));
else
pg_fatal("ARCHIVE-COMMAND \"%s\" exited with unrecognized status %d",
command, rc);
}
free(command);

@ -141,6 +141,7 @@ main(int argc, char *argv[])
char tmpdir[MAXPGPATH] = TMPFS_DIRECTORY "/pg_tde_restoreXXXXXX";
char tmppath[MAXPGPATH];
bool issegment;
int rc;
pg_logging_init(argv[0]);
progname = get_progname(argv[0]);
@ -201,9 +202,22 @@ main(int argc, char *argv[])
command = replace_percent_placeholders(command,
"RESTORE-COMMAND", "fp",
sourcename, targetpath);
rc = system(command);
if (system(command) != 0)
pg_fatal("RESTORE-COMMAND \"%s\" failed: %m", command);
if (rc != 0)
{
if (rc == -1)
pg_fatal("RESTORE-COMMAND \"%s\" failed: %m", command);
else if (WIFEXITED(rc))
pg_fatal("RESTORE-COMMAND \"%s\" failed with exit code %d",
command, WEXITSTATUS(rc));
else if (WIFSIGNALED(rc))
pg_fatal("RESTORE-COMMAND \"%s\" was terminated by signal %d: %s",
command, WTERMSIG(rc), pg_strsignal(WTERMSIG(rc)));
else
pg_fatal("RESTORE-COMMAND \"%s\" exited with unrecognized status %d",
command, rc);
}
free(command);

Loading…
Cancel
Save