1 | /*------------------------------------------------------------------------- |
2 | * |
3 | * wait_error.c |
4 | * Convert a wait/waitpid(2) result code to a human-readable string |
5 | * |
6 | * |
7 | * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
8 | * Portions Copyright (c) 1994, Regents of the University of California |
9 | * |
10 | * |
11 | * IDENTIFICATION |
12 | * src/common/wait_error.c |
13 | * |
14 | *------------------------------------------------------------------------- |
15 | */ |
16 | |
17 | #ifndef FRONTEND |
18 | #include "postgres.h" |
19 | #else |
20 | #include "postgres_fe.h" |
21 | #endif |
22 | |
23 | #include <signal.h> |
24 | #include <sys/wait.h> |
25 | |
26 | /* |
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). The result is a translated, palloc'd or malloc'd string. |
30 | */ |
31 | char * |
32 | wait_result_to_str(int exitstatus) |
33 | { |
34 | char str[512]; |
35 | |
36 | if (WIFEXITED(exitstatus)) |
37 | { |
38 | /* |
39 | * Give more specific error message for some common exit codes that |
40 | * have a special meaning in shells. |
41 | */ |
42 | switch (WEXITSTATUS(exitstatus)) |
43 | { |
44 | case 126: |
45 | snprintf(str, sizeof(str), _("command not executable" )); |
46 | break; |
47 | |
48 | case 127: |
49 | snprintf(str, sizeof(str), _("command not found" )); |
50 | break; |
51 | |
52 | default: |
53 | snprintf(str, sizeof(str), |
54 | _("child process exited with exit code %d" ), |
55 | WEXITSTATUS(exitstatus)); |
56 | } |
57 | } |
58 | else if (WIFSIGNALED(exitstatus)) |
59 | { |
60 | #if defined(WIN32) |
61 | snprintf(str, sizeof(str), |
62 | _("child process was terminated by exception 0x%X" ), |
63 | WTERMSIG(exitstatus)); |
64 | #else |
65 | snprintf(str, sizeof(str), |
66 | _("child process was terminated by signal %d: %s" ), |
67 | WTERMSIG(exitstatus), pg_strsignal(WTERMSIG(exitstatus))); |
68 | #endif |
69 | } |
70 | else |
71 | snprintf(str, sizeof(str), |
72 | _("child process exited with unrecognized status %d" ), |
73 | exitstatus); |
74 | |
75 | return pstrdup(str); |
76 | } |
77 | |
78 | /* |
79 | * Return true if a wait(2) result indicates that the child process |
80 | * died due to the specified signal. |
81 | * |
82 | * The reason this is worth having a wrapper function for is that |
83 | * there are two cases: the signal might have been received by our |
84 | * immediate child process, or there might've been a shell process |
85 | * between us and the child that died. The shell will, per POSIX, |
86 | * report the child death using exit code 128 + signal number. |
87 | * |
88 | * If there is no possibility of an intermediate shell, this function |
89 | * need not (and probably should not) be used. |
90 | */ |
91 | bool |
92 | wait_result_is_signal(int exit_status, int signum) |
93 | { |
94 | if (WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum) |
95 | return true; |
96 | if (WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == 128 + signum) |
97 | return true; |
98 | return false; |
99 | } |
100 | |
101 | /* |
102 | * Return true if a wait(2) result indicates that the child process |
103 | * died due to any signal. We consider either direct child death |
104 | * or a shell report of child process death as matching the condition. |
105 | * |
106 | * If include_command_not_found is true, also return true for shell |
107 | * exit codes indicating "command not found" and the like |
108 | * (specifically, exit codes 126 and 127; see above). |
109 | */ |
110 | bool |
111 | wait_result_is_any_signal(int exit_status, bool include_command_not_found) |
112 | { |
113 | if (WIFSIGNALED(exit_status)) |
114 | return true; |
115 | if (WIFEXITED(exit_status) && |
116 | WEXITSTATUS(exit_status) > (include_command_not_found ? 125 : 128)) |
117 | return true; |
118 | return false; |
119 | } |
120 | |