Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 610f60a

Browse files
committed
Print meaningfull error text for abonormal process exit on Win32, rather
than hex codes, using FormatMessage().
1 parent b97b866 commit 610f60a

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.510 2007/01/22 19:38:05 momjian Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.511 2007/01/23 01:45:11 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -2430,14 +2430,30 @@ LogChildExit(int lev, const char *procname, int pid, int exitstatus)
24302430
(errmsg("%s (PID %d) was terminated by signal %d",
24312431
procname, pid, WTERMSIG(exitstatus))));
24322432
#else
2433-
ereport(lev,
2433+
{
2434+
static char last_system_error[512];
2435+
2436+
if (WERRORCODE(exitstatus) == 0 ||
2437+
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
2438+
FORMAT_MESSAGE_FROM_SYSTEM,
2439+
NULL,
2440+
WERRORCODE(exitstatus),
2441+
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
2442+
last_system_error,
2443+
sizeof(last_system_error) - 1,
2444+
NULL) == 0)
2445+
snprintf(last_system_error, sizeof(last_system_error) - 1,
2446+
"Unknown error %X.", WEXITSTATUS(exitstatus));
24342447

2448+
ereport(lev,
2449+
24352450
/*------
24362451
translator: %s is a noun phrase describing a child process, such as
24372452
"server process" */
2438-
(errmsg("%s (PID %d) was terminated by exception %X",
2439-
procname, pid, WTERMSIG(exitstatus)),
2440-
errhint("See http://source.winehq.org/source/include/ntstatus.h for a description of the hex value.")));
2453+
(errmsg("%s (PID %d) was terminated by the operating system",
2454+
procname, pid),
2455+
errdetail("%s", last_system_error)));
2456+
}
24412457
#endif
24422458
else
24432459
ereport(lev,

src/include/port/win32.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.67 2007/01/22 18:32:57 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.68 2007/01/23 01:45:11 momjian Exp $ */
22

33
#if defined(_MSC_VER) || defined(__BORLANDC__)
44
#define WIN32_ONLY_COMPILER
@@ -140,13 +140,26 @@ int semop(int semId, struct sembuf * sops, int flag);
140140
* Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
141141
* MS SDK - http://www.nologs.com/ntstatus.html
142142
*
143-
* Some day we might want to print descriptions for the most common
144-
* exceptions, rather than printing a URL.
143+
* Because FormatMessage only handles NT_ERROR strings, and assumes they
144+
* do not have the 0xC prefix, we strip it to match this list:
145+
* http://msdn2.microsoft.com/en-us/library/ms681381.aspx
146+
*
147+
* When using FormatMessage():
148+
*
149+
* On MinGW, system() returns STATUS_* values. MSVC might be
150+
* different. To test, create a binary that does *(NULL), and
151+
* then create a second binary that calls it via system(),
152+
* and check the return value of system(). On MinGW, it is
153+
* 0xC0000005 == STATUS_ACCESS_VIOLATION, and 0x5 is a value
154+
* FormatMessage() can look up. GetLastError() does not work;
155+
* always zero.
145156
*/
146-
#define WIFEXITED(w) (((w) & 0XFFFFFF00) == 0)
147-
#define WIFSIGNALED(w) (!WIFEXITED(w))
148-
#define WEXITSTATUS(w) (w)
149-
#define WTERMSIG(w) (w)
157+
#define STATUS_ERROR_MASK 0xC0000000
158+
#define WIFEXITED(w) (((w) & 0XFFFFFF00) == 0)
159+
#define WIFSIGNALED(w) (!WIFEXITED(w))
160+
#define WEXITSTATUS(w) (w)
161+
#define WERRORCODE(w) ((((w) & STATUS_ERROR_MASK) == STATUS_ERROR_MASK) ? \
162+
((w) & ~STATUS_ERROR_MASK) : 0)
150163

151164
#define sigmask(sig) ( 1 << ((sig)-1) )
152165

src/port/exec.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/port/exec.c,v 1.45 2007/01/22 18:31:51 momjian Exp $
12+
* $PostgreSQL: pgsql/src/port/exec.c,v 1.46 2007/01/23 01:45:11 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -586,8 +586,24 @@ pclose_check(FILE *stream)
586586
log_error(_("child process was terminated by signal %d"),
587587
WTERMSIG(exitstatus));
588588
#else
589-
log_error(_("child process was terminated by exception %X\nSee http://source.winehq.org/source/include/ntstatus.h for a description\nof the hex value."),
590-
WTERMSIG(exitstatus));
589+
{
590+
static char last_system_error[512];
591+
592+
if (WERRORCODE(exitstatus) == 0 ||
593+
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
594+
FORMAT_MESSAGE_FROM_SYSTEM,
595+
NULL,
596+
WERRORCODE(exitstatus),
597+
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
598+
last_system_error,
599+
sizeof(last_system_error) - 1,
600+
NULL) == 0)
601+
snprintf(last_system_error, sizeof(last_system_error) - 1,
602+
"Unknown error %X.", WEXITSTATUS(exitstatus));
603+
604+
log_error(_("child process was terminated by the operating system\n%s"),
605+
last_system_error);
606+
}
591607
#endif
592608
else
593609
log_error(_("child process exited with unrecognized status %d"),

0 commit comments

Comments
 (0)