Header And Logo

PostgreSQL
| The world's most advanced open source database.

wait_error.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * wait_error.c
00004  *      Convert a wait/waitpid(2) result code to a human-readable string
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  *
00011  * IDENTIFICATION
00012  *    src/port/wait_error.c
00013  *
00014  *-------------------------------------------------------------------------
00015  */
00016 
00017 #ifndef FRONTEND
00018 #include "postgres.h"
00019 #else
00020 #include "postgres_fe.h"
00021 #endif
00022 
00023 #include <signal.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <sys/wait.h>
00027 
00028 /*
00029  * Return a human-readable string explaining the reason a child process
00030  * terminated. The argument is a return code returned by wait(2) or
00031  * waitpid(2). The result is a translated, palloc'd or malloc'd string.
00032  */
00033 char *
00034 wait_result_to_str(int exitstatus)
00035 {
00036     char        str[512];
00037 
00038     if (WIFEXITED(exitstatus))
00039     {
00040         /*
00041          * Give more specific error message for some common exit codes that
00042          * have a special meaning in shells.
00043          */
00044         switch (WEXITSTATUS(exitstatus))
00045         {
00046             case 126:
00047                 snprintf(str, sizeof(str), _("command not executable"));
00048                 break;
00049 
00050             case 127:
00051                 snprintf(str, sizeof(str), _("command not found"));
00052                 break;
00053 
00054             default:
00055                 snprintf(str, sizeof(str),
00056                          _("child process exited with exit code %d"),
00057                          WEXITSTATUS(exitstatus));
00058         }
00059     }
00060     else if (WIFSIGNALED(exitstatus))
00061 #if defined(WIN32)
00062         snprintf(str, sizeof(str),
00063                  _("child process was terminated by exception 0x%X"),
00064                  WTERMSIG(exitstatus));
00065 #elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
00066     {
00067         char        str2[256];
00068 
00069         snprintf(str2, sizeof(str2), "%d: %s", WTERMSIG(exitstatus),
00070                  WTERMSIG(exitstatus) < NSIG ?
00071                  sys_siglist[WTERMSIG(exitstatus)] : "(unknown)");
00072         snprintf(str, sizeof(str),
00073                  _("child process was terminated by signal %s"), str2);
00074     }
00075 #else
00076         snprintf(str, sizeof(str),
00077                  _("child process was terminated by signal %d"),
00078                  WTERMSIG(exitstatus));
00079 #endif
00080     else
00081         snprintf(str, sizeof(str),
00082                  _("child process exited with unrecognized status %d"),
00083                   exitstatus);
00084 
00085     return pstrdup(str);
00086 }