]> granicus.if.org Git - postgresql/blob - src/common/wait_error.c
Update copyright for 2018
[postgresql] / src / common / wait_error.c
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-2018, 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 #if defined(WIN32)
60                 snprintf(str, sizeof(str),
61                                  _("child process was terminated by exception 0x%X"),
62                                  WTERMSIG(exitstatus));
63 #elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
64         {
65                 char            str2[256];
66
67                 snprintf(str2, sizeof(str2), "%d: %s", WTERMSIG(exitstatus),
68                                  WTERMSIG(exitstatus) < NSIG ?
69                                  sys_siglist[WTERMSIG(exitstatus)] : "(unknown)");
70                 snprintf(str, sizeof(str),
71                                  _("child process was terminated by signal %s"), str2);
72         }
73 #else
74                 snprintf(str, sizeof(str),
75                                  _("child process was terminated by signal %d"),
76                                  WTERMSIG(exitstatus));
77 #endif
78         else
79                 snprintf(str, sizeof(str),
80                                  _("child process exited with unrecognized status %d"),
81                                  exitstatus);
82
83         return pstrdup(str);
84 }