mirror of
https://github.com/postgres/postgres.git
synced 2025-07-12 21:01:52 +03:00
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted possible failures or deficiencies of the platform's strerror. On Windows, it also knows how to translate Winsock error codes, which the native strerror does not. Move all this code into src/port/strerror.c and define strerror() as a macro that invokes it, so that both our frontend and backend code will have all of this behavior. I believe this constitutes an actual bug fix on Windows, since AFAICS our frontend code did not report Winsock error codes properly before this. However, the main point is to lay the groundwork for implementing %m in src/port/snprintf.c: the behavior we want %m to have is this one, not the native strerror's. Note that this throws away the prior use of src/port/strerror.c, which was to implement strerror() on platforms lacking it. That's been dead code for nigh twenty years now, since strerror() was already required by C89. We should likewise cause strerror_r to use this behavior, but I'll tackle that separately. Patch by me, reviewed by Michael Paquier Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
This commit is contained in:
@ -690,39 +690,3 @@ pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, c
|
||||
memcpy(writefds, &outwritefds, sizeof(fd_set));
|
||||
return nummatches;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Return win32 error string, since strerror can't
|
||||
* handle winsock codes
|
||||
*/
|
||||
static char wserrbuf[256];
|
||||
const char *
|
||||
pgwin32_socket_strerror(int err)
|
||||
{
|
||||
static HANDLE handleDLL = INVALID_HANDLE_VALUE;
|
||||
|
||||
if (handleDLL == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
handleDLL = LoadLibraryEx("netmsg.dll", NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
|
||||
if (handleDLL == NULL)
|
||||
ereport(FATAL,
|
||||
(errmsg_internal("could not load netmsg.dll: error code %lu", GetLastError())));
|
||||
}
|
||||
|
||||
ZeroMemory(&wserrbuf, sizeof(wserrbuf));
|
||||
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_FROM_HMODULE,
|
||||
handleDLL,
|
||||
err,
|
||||
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
|
||||
wserrbuf,
|
||||
sizeof(wserrbuf) - 1,
|
||||
NULL) == 0)
|
||||
{
|
||||
/* Failed to get id */
|
||||
sprintf(wserrbuf, "unrecognized winsock error %d", err);
|
||||
}
|
||||
return wserrbuf;
|
||||
}
|
||||
|
Reference in New Issue
Block a user