PostgreSQL Source Code  git master
wait_error.c File Reference
#include "postgres.h"
#include <signal.h>
#include <sys/wait.h>
Include dependency graph for wait_error.c:

Go to the source code of this file.

Functions

char * wait_result_to_str (int exitstatus)
 
bool wait_result_is_signal (int exit_status, int signum)
 
bool wait_result_is_any_signal (int exit_status, bool include_command_not_found)
 
int wait_result_to_exit_code (int exit_status)
 

Function Documentation

◆ wait_result_is_any_signal()

bool wait_result_is_any_signal ( int  exit_status,
bool  include_command_not_found 
)

Definition at line 121 of file wait_error.c.

122 {
123  if (WIFSIGNALED(exit_status))
124  return true;
125  if (WIFEXITED(exit_status) &&
126  WEXITSTATUS(exit_status) > (include_command_not_found ? 125 : 128))
127  return true;
128  return false;
129 }
#define WIFEXITED(w)
Definition: win32_port.h:152
#define WIFSIGNALED(w)
Definition: win32_port.h:153
#define WEXITSTATUS(w)
Definition: win32_port.h:154

References WEXITSTATUS, WIFEXITED, and WIFSIGNALED.

Referenced by ExecuteRecoveryCommand(), RestoreArchivedFile(), and shell_archive_file().

◆ wait_result_is_signal()

bool wait_result_is_signal ( int  exit_status,
int  signum 
)

Definition at line 102 of file wait_error.c.

103 {
104  if (WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum)
105  return true;
106  if (WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == 128 + signum)
107  return true;
108  return false;
109 }
#define WTERMSIG(w)
Definition: win32_port.h:155

References WEXITSTATUS, WIFEXITED, WIFSIGNALED, and WTERMSIG.

Referenced by ClosePipeFromProgram(), and RestoreArchivedFile().

◆ wait_result_to_exit_code()

int wait_result_to_exit_code ( int  exit_status)

Definition at line 138 of file wait_error.c.

139 {
140  if (exit_status == -1)
141  return -1; /* failure of pclose() or system() */
142  if (WIFEXITED(exit_status))
143  return WEXITSTATUS(exit_status);
144  if (WIFSIGNALED(exit_status))
145  return 128 + WTERMSIG(exit_status);
146  /* On many systems, this is unreachable */
147  return -1;
148 }

References WEXITSTATUS, WIFEXITED, WIFSIGNALED, and WTERMSIG.

Referenced by SetShellResultVariables().

◆ wait_result_to_str()

char* wait_result_to_str ( int  exitstatus)

Definition at line 33 of file wait_error.c.

34 {
35  char str[512];
36 
37  /*
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
40  * by errno.
41  */
42  if (exitstatus == -1)
43  {
44  snprintf(str, sizeof(str), "%m");
45  }
46  else if (WIFEXITED(exitstatus))
47  {
48  /*
49  * Give more specific error message for some common exit codes that
50  * have a special meaning in shells.
51  */
52  switch (WEXITSTATUS(exitstatus))
53  {
54  case 126:
55  snprintf(str, sizeof(str), _("command not executable"));
56  break;
57 
58  case 127:
59  snprintf(str, sizeof(str), _("command not found"));
60  break;
61 
62  default:
63  snprintf(str, sizeof(str),
64  _("child process exited with exit code %d"),
65  WEXITSTATUS(exitstatus));
66  }
67  }
68  else if (WIFSIGNALED(exitstatus))
69  {
70 #if defined(WIN32)
71  snprintf(str, sizeof(str),
72  _("child process was terminated by exception 0x%X"),
73  WTERMSIG(exitstatus));
74 #else
75  snprintf(str, sizeof(str),
76  _("child process was terminated by signal %d: %s"),
77  WTERMSIG(exitstatus), pg_strsignal(WTERMSIG(exitstatus)));
78 #endif
79  }
80  else
81  snprintf(str, sizeof(str),
82  _("child process exited with unrecognized status %d"),
83  exitstatus);
84 
85  return pstrdup(str);
86 }
#define _(x)
Definition: elog.c:90
const char * str
char * pstrdup(const char *in)
Definition: mcxt.c:1695
const char * pg_strsignal(int signum)
Definition: pgstrsignal.c:39
#define snprintf
Definition: port.h:238

References _, pg_strsignal(), pstrdup(), snprintf, str, WEXITSTATUS, WIFEXITED, WIFSIGNALED, and WTERMSIG.

Referenced by adjust_data_dir(), BaseBackup(), ClosePipeFromProgram(), ClosePipeToProgram(), do_copy(), exec_command_write(), ExecuteRecoveryCommand(), get_bin_version(), get_control_data(), pclose_check(), RestoreArchivedFile(), run_ssl_passphrase_command(), and shell_finish_command().