From: Bruce Momjian Date: Thu, 22 Apr 2004 03:51:24 +0000 (+0000) Subject: Per discussion earlier today, here is a fix that lets ereport() on win32 X-Git-Tag: REL8_0_0BETA1~783 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b00d50d3657bcf089d5f4e358d8618d9898cb827;p=postgresql Per discussion earlier today, here is a fix that lets ereport() on win32 report socket errors. Magnus Hagander --- diff --git a/src/backend/port/win32/socket.c b/src/backend/port/win32/socket.c index 878e7a276f..5eb3082363 100644 --- a/src/backend/port/win32/socket.c +++ b/src/backend/port/win32/socket.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.1 2004/04/12 16:19:18 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.2 2004/04/22 03:50:54 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -432,3 +432,35 @@ int pgwin32_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfd 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("Failed to load netmsg.dll: %i",(int)GetLastError()))); + } + + ZeroMemory(&wserrbuf, sizeof(wserrbuf)); + if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, + handleDLL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + wserrbuf, + sizeof(wserrbuf)-1, + NULL) == 0) { + /* Failed to get id */ + sprintf(wserrbuf,"Unknown winsock error %i",err); + } + return wserrbuf; +} + diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 42650a1f7e..234bc4af39 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.134 2004/04/16 12:59:05 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.135 2004/04/22 03:51:09 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -1661,6 +1661,11 @@ useful_strerror(int errnum) static char errorstr_buf[48]; const char *str; +#ifdef WIN32 + /* Winsock error code range, per WinError.h */ + if (errnum >= 10000 && errnum <= 11999) + return pgwin32_socket_strerror(errnum); +#endif str = strerror(errnum); /* diff --git a/src/include/port/win32.h b/src/include/port/win32.h index fb91e66468..03bc7a75b9 100644 --- a/src/include/port/win32.h +++ b/src/include/port/win32.h @@ -1,4 +1,4 @@ -/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.22 2004/04/19 17:42:59 momjian Exp $ */ +/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.23 2004/04/22 03:51:24 momjian Exp $ */ /* undefine and redefine after #include */ #undef mkdir @@ -139,6 +139,8 @@ int pgwin32_connect(SOCKET s, const struct sockaddr* name, int namelen); int pgwin32_select(int nfds, fd_set* readfs, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout); int pgwin32_recv(SOCKET s, char* buf, int len, int flags); int pgwin32_send(SOCKET s, char* buf, int len, int flags); + +const char *pgwin32_socket_strerror(int err); #endif