1 /*-------------------------------------------------------------------------
4 * Convert a wait/waitpid(2) result code to a human-readable string
7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/common/wait_error.c
14 *-------------------------------------------------------------------------
20 #include "postgres_fe.h"
27 * Return a human-readable string explaining the reason a child process
28 * terminated. The argument is a return code returned by wait(2) or
29 * waitpid(2), which also applies to pclose(3) and system(3). The result is a
30 * translated, palloc'd or malloc'd string.
33 wait_result_to_str(int exitstatus
)
38 * To simplify using this after pclose() and system(), handle status -1
39 * first. In that case, there is no wait result but some error indicated
44 snprintf(str
, sizeof(str
), "%m");
46 else if (WIFEXITED(exitstatus
))
49 * Give more specific error message for some common exit codes that
50 * have a special meaning in shells.
52 switch (WEXITSTATUS(exitstatus
))
55 snprintf(str
, sizeof(str
), _("command not executable"));
59 snprintf(str
, sizeof(str
), _("command not found"));
63 snprintf(str
, sizeof(str
),
64 _("child process exited with exit code %d"),
65 WEXITSTATUS(exitstatus
));
68 else if (WIFSIGNALED(exitstatus
))
71 snprintf(str
, sizeof(str
),
72 _("child process was terminated by exception 0x%X"),
73 WTERMSIG(exitstatus
));
75 snprintf(str
, sizeof(str
),
76 _("child process was terminated by signal %d: %s"),
77 WTERMSIG(exitstatus
), pg_strsignal(WTERMSIG(exitstatus
)));
81 snprintf(str
, sizeof(str
),
82 _("child process exited with unrecognized status %d"),
89 * Return true if a wait(2) result indicates that the child process
90 * died due to the specified signal.
92 * The reason this is worth having a wrapper function for is that
93 * there are two cases: the signal might have been received by our
94 * immediate child process, or there might've been a shell process
95 * between us and the child that died. The shell will, per POSIX,
96 * report the child death using exit code 128 + signal number.
98 * If there is no possibility of an intermediate shell, this function
99 * need not (and probably should not) be used.
102 wait_result_is_signal(int exit_status
, int signum
)
104 if (WIFSIGNALED(exit_status
) && WTERMSIG(exit_status
) == signum
)
106 if (WIFEXITED(exit_status
) && WEXITSTATUS(exit_status
) == 128 + signum
)
112 * Return true if a wait(2) result indicates that the child process
113 * died due to any signal. We consider either direct child death
114 * or a shell report of child process death as matching the condition.
116 * If include_command_not_found is true, also return true for shell
117 * exit codes indicating "command not found" and the like
118 * (specifically, exit codes 126 and 127; see above).
121 wait_result_is_any_signal(int exit_status
, bool include_command_not_found
)
123 if (WIFSIGNALED(exit_status
))
125 if (WIFEXITED(exit_status
) &&
126 WEXITSTATUS(exit_status
) > (include_command_not_found
? 125 : 128))