aboutsummaryrefslogtreecommitdiff
path: root/src/common/wait_error.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2023-03-21 13:03:42 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2023-03-21 13:03:56 -0400
commitb0d8f2d983cb25d1035fae1cd7de214dd67809b4 (patch)
tree765a0ea3579a960f3574a38e229338011b804e3a /src/common/wait_error.c
parent0f85db92b9ea167d3b9e90f3fb5fb3b9a93babc2 (diff)
downloadpostgresql-b0d8f2d983cb25d1035fae1cd7de214dd67809b4.tar.gz
postgresql-b0d8f2d983cb25d1035fae1cd7de214dd67809b4.zip
Add SHELL_ERROR and SHELL_EXIT_CODE magic variables to psql.
These are set after a \! command or a backtick substitution. SHELL_ERROR is just "true" for error (nonzero exit status) or "false" for success, while SHELL_EXIT_CODE records the actual exit status following standard shell/system(3) conventions. Corey Huinker, reviewed by Maxim Orlov and myself Discussion: https://postgr.es/m/CADkLM=cWao2x2f+UDw15W1JkVFr_bsxfstw=NGea7r9m4j-7rQ@mail.gmail.com
Diffstat (limited to 'src/common/wait_error.c')
-rw-r--r--src/common/wait_error.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/common/wait_error.c b/src/common/wait_error.c
index 4a3c3c61af1..a90b745f077 100644
--- a/src/common/wait_error.c
+++ b/src/common/wait_error.c
@@ -127,3 +127,22 @@ wait_result_is_any_signal(int exit_status, bool include_command_not_found)
return true;
return false;
}
+
+/*
+ * Return the shell exit code (normally 0 to 255) that corresponds to the
+ * given wait status. The argument is a wait status as returned by wait(2)
+ * or waitpid(2), which also applies to pclose(3) and system(3). To support
+ * the latter two cases, we pass through "-1" unchanged.
+ */
+int
+wait_result_to_exit_code(int exit_status)
+{
+ if (exit_status == -1)
+ return -1; /* failure of pclose() or system() */
+ if (WIFEXITED(exit_status))
+ return WEXITSTATUS(exit_status);
+ if (WIFSIGNALED(exit_status))
+ return 128 + WTERMSIG(exit_status);
+ /* On many systems, this is unreachable */
+ return -1;
+}