Skip to content

Commit f1adb81

Browse files
committed
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.
1 parent 80e0bb0 commit f1adb81

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

contrib/pg_tde/src/bin/pg_tde_archive_decrypt.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ main(int argc, char *argv[])
146146
char tmpdir[MAXPGPATH] = TMPFS_DIRECTORY "/pg_tde_archiveXXXXXX";
147147
char tmppath[MAXPGPATH];
148148
bool issegment;
149+
int rc;
149150

150151
pg_logging_init(argv[0]);
151152
progname = get_progname(argv[0]);
@@ -208,9 +209,22 @@ main(int argc, char *argv[])
208209
command = replace_percent_placeholders(command,
209210
"ARCHIVE-COMMAND", "fp",
210211
targetname, sourcepath);
212+
rc = system(command);
211213

212-
if (system(command) != 0)
213-
pg_fatal("ARCHIVE-COMMAND \"%s\" failed: %m", command);
214+
if (rc != 0)
215+
{
216+
if (rc == -1)
217+
pg_fatal("ARCHIVE-COMMAND \"%s\" failed: %m", command);
218+
else if (WIFEXITED(rc))
219+
pg_fatal("ARCHIVE-COMMAND \"%s\" failed with exit code %d",
220+
command, WEXITSTATUS(rc));
221+
else if (WIFSIGNALED(rc))
222+
pg_fatal("ARCHIVE-COMMAND \"%s\" was terminated by signal %d: %s",
223+
command, WTERMSIG(rc), pg_strsignal(WTERMSIG(rc)));
224+
else
225+
pg_fatal("ARCHIVE-COMMAND \"%s\" exited with unrecognized status %d",
226+
command, rc);
227+
}
214228

215229
free(command);
216230

contrib/pg_tde/src/bin/pg_tde_restore_encrypt.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ main(int argc, char *argv[])
141141
char tmpdir[MAXPGPATH] = TMPFS_DIRECTORY "/pg_tde_restoreXXXXXX";
142142
char tmppath[MAXPGPATH];
143143
bool issegment;
144+
int rc;
144145

145146
pg_logging_init(argv[0]);
146147
progname = get_progname(argv[0]);
@@ -201,9 +202,22 @@ main(int argc, char *argv[])
201202
command = replace_percent_placeholders(command,
202203
"RESTORE-COMMAND", "fp",
203204
sourcename, targetpath);
205+
rc = system(command);
204206

205-
if (system(command) != 0)
206-
pg_fatal("RESTORE-COMMAND \"%s\" failed: %m", command);
207+
if (rc != 0)
208+
{
209+
if (rc == -1)
210+
pg_fatal("RESTORE-COMMAND \"%s\" failed: %m", command);
211+
else if (WIFEXITED(rc))
212+
pg_fatal("RESTORE-COMMAND \"%s\" failed with exit code %d",
213+
command, WEXITSTATUS(rc));
214+
else if (WIFSIGNALED(rc))
215+
pg_fatal("RESTORE-COMMAND \"%s\" was terminated by signal %d: %s",
216+
command, WTERMSIG(rc), pg_strsignal(WTERMSIG(rc)));
217+
else
218+
pg_fatal("RESTORE-COMMAND \"%s\" exited with unrecognized status %d",
219+
command, rc);
220+
}
207221

208222
free(command);
209223

0 commit comments

Comments
 (0)