diff --git a/configure b/configure index cbc1ebfc613..375a8b7dbc4 100755 --- a/configure +++ b/configure @@ -16542,6 +16542,12 @@ esac ;; esac + case " $LIBOBJS " in + *" win32gai_strerror.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS win32gai_strerror.$ac_objext" + ;; +esac + case " $LIBOBJS " in *" win32getrusage.$ac_objext "* ) ;; *) LIBOBJS="$LIBOBJS win32getrusage.$ac_objext" diff --git a/configure.ac b/configure.ac index 69b2bbb5763..aed940410c3 100644 --- a/configure.ac +++ b/configure.ac @@ -1907,6 +1907,7 @@ if test "$PORTNAME" = "win32"; then AC_LIBOBJ(win32env) AC_LIBOBJ(win32error) AC_LIBOBJ(win32fdatasync) + AC_LIBOBJ(win32gai_strerror) AC_LIBOBJ(win32getrusage) AC_LIBOBJ(win32link) AC_LIBOBJ(win32ntdll) diff --git a/src/include/port/win32/sys/socket.h b/src/include/port/win32/sys/socket.h index 0c32c0f7b2e..f2b475df5e5 100644 --- a/src/include/port/win32/sys/socket.h +++ b/src/include/port/win32/sys/socket.h @@ -23,4 +23,12 @@ #define ERROR PGERROR #endif +/* + * We don't use the Windows gai_strerror[A] function because it is not + * thread-safe. We define our own in src/port/win32gai_strerror.c. + */ +#undef gai_strerror + +extern const char *gai_strerror(int ecode); + #endif /* WIN32_SYS_SOCKET_H */ diff --git a/src/port/meson.build b/src/port/meson.build index 0a16f1c7f5f..900076df571 100644 --- a/src/port/meson.build +++ b/src/port/meson.build @@ -35,6 +35,7 @@ if host_system == 'windows' 'win32error.c', 'win32fdatasync.c', 'win32fseek.c', + 'win32gai_strerror.c', 'win32getrusage.c', 'win32link.c', 'win32ntdll.c', diff --git a/src/port/win32gai_strerror.c b/src/port/win32gai_strerror.c new file mode 100644 index 00000000000..5b47d1722df --- /dev/null +++ b/src/port/win32gai_strerror.c @@ -0,0 +1,45 @@ +/*------------------------------------------------------------------------- + * + * win32gai_strerror.c + * Thread-safe gai_strerror() for Windows. + * + * Portions Copyright (c) 2024, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/port/win32gai_strerror.c + * + *------------------------------------------------------------------------- + */ + +#include + +/* + * Windows has gai_strerrorA(), but it is not thread-safe so we avoid it. + * + * https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-gai_strerrora + */ +const char * +gai_strerror(int errcode) +{ + switch (errcode) + { + case EAI_AGAIN: + return "Temporary failure in name resolution"; + case EAI_BADFLAGS: + return "Bad value for ai_flags"; + case EAI_FAIL: + return "Non-recoverable failure in name resolution"; + case EAI_FAMILY: + return "ai_family not supported"; + case EAI_MEMORY: + return "Memory allocation failure"; + case EAI_NONAME: + return "Name or service not known"; + case EAI_SERVICE: + return "Servname not supported for ai_socktype"; + case EAI_SOCKTYPE: + return "ai_socktype not supported"; + default: + return "Unknown server error"; + } +} diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 9e05eb91b1a..6a79a0e037d 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -113,6 +113,7 @@ sub mkvcbuild win32env.c win32error.c win32fdatasync.c win32fseek.c + win32gai_strerror.c win32getrusage.c win32gettimeofday.c win32link.c