On 2019-09-03 01:45, Thomas Munro wrote:
> fe-connect.obj : error LNK2019: unresolved external symbol getpeereid
> referenced in function PQconnectPoll
> [C:\projects\postgresql\libpq.vcxproj]

This should be fixed in the attached patches.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 5f7549edaaa33bf7fa0c1dd4e164d188ae4e5cd4 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v3 1/7] Enable Unix-domain sockets support on Windows

As of Windows 10 version 1803, Unix-domain sockets are supported on
Windows.  But it's not automatically detected by configure because it
looks for struct sockaddr_un and Windows doesn't define that.  So we
just make our own definition on Windows and override the configure
result.
---
 config/c-library.m4            |  5 +++--
 configure                      |  5 ++++-
 src/include/c.h                | 11 +++++++++++
 src/include/pg_config.h.in     |  6 +++---
 src/include/pg_config.h.win32  |  6 +++---
 src/include/pg_config_manual.h |  7 -------
 6 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/config/c-library.m4 b/config/c-library.m4
index 6f2b0fbb4e..1469b07d2f 100644
--- a/config/c-library.m4
+++ b/config/c-library.m4
@@ -121,10 +121,11 @@ AC_DEFUN([PGAC_UNION_SEMUN],
 
 # PGAC_STRUCT_SOCKADDR_UN
 # -----------------------
-# If `struct sockaddr_un' exists, define HAVE_UNIX_SOCKETS.
+# If `struct sockaddr_un' exists, define HAVE_STRUCT_SOCKADDR_UN.
+# If it is missing then one could define it.
 # (Requires test for <sys/un.h>!)
 AC_DEFUN([PGAC_STRUCT_SOCKADDR_UN],
-[AC_CHECK_TYPE([struct sockaddr_un], [AC_DEFINE(HAVE_UNIX_SOCKETS, 1, [Define 
to 1 if you have unix sockets.])], [],
+[AC_CHECK_TYPES([struct sockaddr_un], [], [],
 [#include <sys/types.h>
 #ifdef HAVE_SYS_UN_H
 #include <sys/un.h>
diff --git a/configure b/configure
index f14709ed1e..da1ef11991 100755
--- a/configure
+++ b/configure
@@ -14079,7 +14079,10 @@ ac_fn_c_check_type "$LINENO" "struct sockaddr_un" 
"ac_cv_type_struct_sockaddr_un
 "
 if test "x$ac_cv_type_struct_sockaddr_un" = xyes; then :
 
-$as_echo "#define HAVE_UNIX_SOCKETS 1" >>confdefs.h
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_SOCKADDR_UN 1
+_ACEOF
+
 
 fi
 
diff --git a/src/include/c.h b/src/include/c.h
index f461628a24..af690f0f5e 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1277,6 +1277,17 @@ extern unsigned long long strtoull(const char *str, char 
**endptr, int base);
 #define NON_EXEC_STATIC static
 #endif
 
+#ifdef HAVE_STRUCT_SOCKADDR_UN
+#define HAVE_UNIX_SOCKETS 1
+#elif defined(WIN32)
+struct sockaddr_un
+{
+       unsigned short sun_family;
+       char sun_path[108];
+};
+#define HAVE_UNIX_SOCKETS 1
+#endif
+
 /* /port compatibility functions */
 #include "port.h"
 
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index d876926c21..dfcd7da569 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -588,6 +588,9 @@
 /* Define to 1 if `__ss_len' is a member of `struct sockaddr_storage'. */
 #undef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN
 
+/* Define to 1 if the system has the type `struct sockaddr_un'. */
+#undef HAVE_STRUCT_SOCKADDR_UN
+
 /* Define to 1 if `tm_zone' is a member of `struct tm'. */
 #undef HAVE_STRUCT_TM_TM_ZONE
 
@@ -676,9 +679,6 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
-/* Define to 1 if you have unix sockets. */
-#undef HAVE_UNIX_SOCKETS
-
 /* Define to 1 if you have the `unsetenv' function. */
 #undef HAVE_UNSETENV
 
diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32
index fc50528590..08a3c0d916 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -457,6 +457,9 @@
 /* Define to 1 if `__ss_len' is member of `struct sockaddr_storage'. */
 /* #undef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN */
 
+/* Define to 1 if the system has the type `struct sockaddr_un'. */
+/* #undef HAVE_STRUCT_SOCKADDR_UN */
+
 /* Define to 1 if `tm_zone' is member of `struct tm'. */
 /* #undef HAVE_STRUCT_TM_TM_ZONE */
 
@@ -524,9 +527,6 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #define HAVE_UNISTD_H 1
 
-/* Define to 1 if you have unix sockets. */
-/* #undef HAVE_UNIX_SOCKETS */
-
 /* Define to 1 if you have the `unsetenv' function. */
 /* #undef HAVE_UNSETENV */
 
diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index 743401cb96..65d20d44b3 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -109,13 +109,6 @@
  */
 #define ALIGNOF_BUFFER 32
 
-/*
- * Disable UNIX sockets for certain operating systems.
- */
-#if defined(WIN32)
-#undef HAVE_UNIX_SOCKETS
-#endif
-
 /*
  * Define this if your operating system supports link()
  */

base-commit: 1d7a6e3eb45946db86d6d1776c55323740d955b0
-- 
2.22.0

From 16a4afffc28395d19b50c95dc79cd78cbf459a08 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v3 2/7] Sort out getpeereid() and struct passwd handling on
 Windows

The getpeereid() uses are protected by HAVE_UNIX_SOCKETS, so they
didn't ever care about Windows support.  But that is required now, and
so we need to provide a getpeereid() stub for Windows.  We can just
let configure do its usual thing of picking up the replacefrom from
libpgport instead of the custom overrides that it was doing before.

But then Windows doesn't have struct passwd, so all this code around
getpeereid() won't work anyway.  This patch just sprinkles some #ifdef
WIN32 around to make it work.  Perhaps a configure test for struct
passwd would be a better way to protect this code.
---
 configure                         | 34 +++++++++++++------------------
 configure.in                      |  9 +++-----
 src/backend/libpq/auth.c          |  6 ++++++
 src/include/port.h                |  2 +-
 src/interfaces/libpq/fe-connect.c |  4 ++++
 src/tools/msvc/Mkvcbuild.pm       |  2 +-
 6 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/configure b/configure
index da1ef11991..6726e6a5ec 100755
--- a/configure
+++ b/configure
@@ -15768,6 +15768,19 @@ esac
 
 fi
 
+ac_fn_c_check_func "$LINENO" "getpeereid" "ac_cv_func_getpeereid"
+if test "x$ac_cv_func_getpeereid" = xyes; then :
+  $as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" getpeereid.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getpeereid.$ac_objext"
+ ;;
+esac
+
+fi
+
 ac_fn_c_check_func "$LINENO" "getrusage" "ac_cv_func_getrusage"
 if test "x$ac_cv_func_getrusage" = xyes; then :
   $as_echo "#define HAVE_GETRUSAGE 1" >>confdefs.h
@@ -15948,17 +15961,11 @@ esac
 case $host_os in
 
         # Windows uses a specialised env handler
-        # and doesn't need a replacement getpeereid because it doesn't use
-        # Unix sockets.
         mingw*)
 
 $as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
 
-
-$as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
-
-                ac_cv_func_unsetenv=yes
-                ac_cv_func_getpeereid=yes;;
+                ac_cv_func_unsetenv=yes;;
         *)
                 ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
 if test "x$ac_cv_func_unsetenv" = xyes; then :
@@ -15971,19 +15978,6 @@ else
  ;;
 esac
 
-fi
-
-ac_fn_c_check_func "$LINENO" "getpeereid" "ac_cv_func_getpeereid"
-if test "x$ac_cv_func_getpeereid" = xyes; then :
-  $as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
-
-else
-  case " $LIBOBJS " in
-  *" getpeereid.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS getpeereid.$ac_objext"
- ;;
-esac
-
 fi
 
 
diff --git a/configure.in b/configure.in
index 805cf8617b..eb574b764f 100644
--- a/configure.in
+++ b/configure.in
@@ -1693,6 +1693,7 @@ AC_REPLACE_FUNCS(m4_normalize([
        dlopen
        fls
        getopt
+       getpeereid
        getrusage
        inet_aton
        mkdtemp
@@ -1723,15 +1724,11 @@ esac
 case $host_os in
 
         # Windows uses a specialised env handler
-        # and doesn't need a replacement getpeereid because it doesn't use
-        # Unix sockets.
         mingw*)
                 AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement 
version used.])
-                AC_DEFINE(HAVE_GETPEEREID, 1, [Define to 1 because function 
not required.])
-                ac_cv_func_unsetenv=yes
-                ac_cv_func_getpeereid=yes;;
+                ac_cv_func_unsetenv=yes;;
         *)
-                AC_REPLACE_FUNCS([unsetenv getpeereid])
+                AC_REPLACE_FUNCS([unsetenv])
                ;;
 esac
 
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index 0e0a6d8752..67bf82c898 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -1999,7 +1999,11 @@ auth_peer(hbaPort *port)
        }
 
        errno = 0;                                      /* clear errno before 
call */
+#ifndef WIN32
        pw = getpwuid(uid);
+#else
+       pw = NULL;
+#endif
        if (!pw)
        {
                int                     save_errno = errno;
@@ -2011,7 +2015,9 @@ auth_peer(hbaPort *port)
                return STATUS_ERROR;
        }
 
+#ifndef WIN32
        strlcpy(ident_user, pw->pw_name, IDENT_USERNAME_MAX + 1);
+#endif
 
        return check_usermap(port->hba->usermap, port->user_name, ident_user, 
false);
 }
diff --git a/src/include/port.h b/src/include/port.h
index 55619d893c..3f6b270d3c 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -354,7 +354,7 @@ extern int  fls(int mask);
 #define ftello(a)              ftell(a)
 #endif
 
-#if !defined(HAVE_GETPEEREID) && !defined(WIN32)
+#ifndef HAVE_GETPEEREID
 extern int     getpeereid(int sock, uid_t *uid, gid_t *gid);
 #endif
 
diff --git a/src/interfaces/libpq/fe-connect.c 
b/src/interfaces/libpq/fe-connect.c
index 7f1fd2f45e..c013098982 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -2686,8 +2686,10 @@ PQconnectPoll(PGconn *conn)
                                        IS_AF_UNIX(conn->raddr.addr.ss_family))
                                {
                                        char            pwdbuf[BUFSIZ];
+#ifndef WIN32
                                        struct passwd pass_buf;
                                        struct passwd *pass;
+#endif
                                        int                     passerr;
                                        uid_t           uid;
                                        gid_t           gid;
@@ -2709,6 +2711,7 @@ PQconnectPoll(PGconn *conn)
                                                goto error_return;
                                        }
 
+#ifndef WIN32
                                        passerr = pqGetpwuid(uid, &pass_buf, 
pwdbuf, sizeof(pwdbuf), &pass);
                                        if (pass == NULL)
                                        {
@@ -2731,6 +2734,7 @@ PQconnectPoll(PGconn *conn)
                                                                                
  conn->requirepeer, pass->pw_name);
                                                goto error_return;
                                        }
+#endif                                                 /* WIN32 */
                                }
 #endif                                                 /* HAVE_UNIX_SOCKETS */
 
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 2eab635898..dfd1d01ac3 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -93,7 +93,7 @@ sub mkvcbuild
        $solution = CreateSolution($vsVersion, $config);
 
        our @pgportfiles = qw(
-         chklocale.c fls.c fseeko.c getrusage.c inet_aton.c random.c
+         chklocale.c fls.c fseeko.c getpeereid.c getrusage.c inet_aton.c 
random.c
          srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
          erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
          dirent.c dlopen.c getopt.c getopt_long.c
-- 
2.22.0

From 03c4c306f71d5d75b678558edb4dc9a6f47f1a2f Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v3 3/7] psql: Remove one use of HAVE_UNIX_SOCKETS

This use was not protecting any unportable code, it was just omitting
the code because it wouldn't be used.  Remove the use to reduce code
complexity a bit.
---
 src/bin/psql/prompt.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index 0fcb8c7783..f8edc942da 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -12,11 +12,6 @@
 #include <win32.h>
 #endif
 
-#ifdef HAVE_UNIX_SOCKETS
-#include <unistd.h>
-#include <netdb.h>
-#endif
-
 #include "common.h"
 #include "input.h"
 #include "prompt.h"
@@ -139,7 +134,6 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
                                                        if (*p == 'm')
                                                                
buf[strcspn(buf, ".")] = '\0';
                                                }
-#ifdef HAVE_UNIX_SOCKETS
                                                /* UNIX socket */
                                                else
                                                {
@@ -150,7 +144,6 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
                                                        else
                                                                snprintf(buf, 
sizeof(buf), "[local:%s]", host);
                                                }
-#endif
                                        }
                                        break;
                                        /* DB server port number */
-- 
2.22.0

From b7bfde70e748ae67bba183e874cd98ea7097fc83 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v3 4/7] libpq: Remove unnecessary uses of HAVE_UNIX_SOCKETS

These are not protecting any unportable code and they were not used
consistently anyway (other code uses IS_AF_UNIX without
HAVE_UNIX_SOCKETS).
---
 src/interfaces/libpq/fe-connect.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c 
b/src/interfaces/libpq/fe-connect.c
index c013098982..79323114e4 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1058,10 +1058,8 @@ connectOptions2(PGconn *conn)
                else if (ch->host != NULL && ch->host[0] != '\0')
                {
                        ch->type = CHT_HOST_NAME;
-#ifdef HAVE_UNIX_SOCKETS
                        if (is_absolute_path(ch->host))
                                ch->type = CHT_UNIX_SOCKET;
-#endif
                }
                else
                {
@@ -1569,7 +1567,6 @@ connectFailureMessage(PGconn *conn, int errorno)
 {
        char            sebuf[PG_STRERROR_R_BUFLEN];
 
-#ifdef HAVE_UNIX_SOCKETS
        if (IS_AF_UNIX(conn->raddr.addr.ss_family))
        {
                char            service[NI_MAXHOST];
@@ -1586,7 +1583,6 @@ connectFailureMessage(PGconn *conn, int errorno)
                                                  service);
        }
        else
-#endif                                                 /* HAVE_UNIX_SOCKETS */
        {
                char            host_addr[NI_MAXHOST];
                const char *displayed_host;
@@ -2676,8 +2672,6 @@ PQconnectPoll(PGconn *conn)
                                char       *startpacket;
                                int                     packetlen;
 
-#ifdef HAVE_UNIX_SOCKETS
-
                                /*
                                 * Implement requirepeer check, if requested 
and it's a
                                 * Unix-domain socket.
@@ -2736,7 +2730,6 @@ PQconnectPoll(PGconn *conn)
                                        }
 #endif                                                 /* WIN32 */
                                }
-#endif                                                 /* HAVE_UNIX_SOCKETS */
 
                                if (IS_AF_UNIX(conn->raddr.addr.ss_family))
                                {
-- 
2.22.0

From b5f496c2ee05266909bd9952bcac819377850083 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v3 5/7] initdb: Detect Unix-domain socket support dynamically

On Windows we now have to support the situation where a binary built
with Unix-domain socket support could be used on a system that doesn't
support it at run time.  So in initdb, for setting up the default
pg_hba.conf and postgresql.conf, do a run-time check for support
instead of relying on compile-time decisions.  This is very similar to
what we already do for IPv6, so similar code can be used.

A change is that now if Unix-domain socket support is not found, the
"local" lines in pg_hba.conf are commented out instead of removed,
again similar to IPv6 support.
---
 src/backend/libpq/pg_hba.conf.sample |   6 +-
 src/bin/initdb/initdb.c              | 170 +++++++++++++--------------
 2 files changed, 84 insertions(+), 92 deletions(-)

diff --git a/src/backend/libpq/pg_hba.conf.sample 
b/src/backend/libpq/pg_hba.conf.sample
index c853e36232..0c163d2c6d 100644
--- a/src/backend/libpq/pg_hba.conf.sample
+++ b/src/backend/libpq/pg_hba.conf.sample
@@ -76,14 +76,14 @@
 
 # TYPE  DATABASE        USER            ADDRESS                 METHOD
 
-@remove-line-for-nolocal@# "local" is for Unix domain socket connections only
-@remove-line-for-nolocal@local   all             all                           
          @authmethodlocal@
+# "local" is for Unix domain socket connections only
+local   all             all                                     
@authmethodlocal@
 # IPv4 local connections:
 host    all             all             127.0.0.1/32            
@authmethodhost@
 # IPv6 local connections:
 host    all             all             ::1/128                 
@authmethodhost@
 # Allow replication connections from localhost, by a user with the
 # replication privilege.
-@remove-line-for-nolocal@local   replication     all                           
          @authmethodlocal@
+local   replication     all                                     
@authmethodlocal@
 host    replication     all             127.0.0.1/32            
@authmethodhost@
 host    replication     all             ::1/128                 
@authmethodhost@
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 88a261d9bd..ffbcf685d5 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -233,9 +233,6 @@ static char backend_exec[MAXPGPATH];
 static char **replace_token(char **lines,
                                                        const char *token, 
const char *replacement);
 
-#ifndef HAVE_UNIX_SOCKETS
-static char **filter_lines_with_token(char **lines, const char *token);
-#endif
 static char **readfile(const char *path);
 static void writefile(char *path, char **lines);
 static FILE *popen_check(const char *command, const char *mode);
@@ -433,36 +430,6 @@ replace_token(char **lines, const char *token, const char 
*replacement)
        return result;
 }
 
-/*
- * make a copy of lines without any that contain the token
- *
- * a sort of poor man's grep -v
- */
-#ifndef HAVE_UNIX_SOCKETS
-static char **
-filter_lines_with_token(char **lines, const char *token)
-{
-       int                     numlines = 1;
-       int                     i,
-                               src,
-                               dst;
-       char      **result;
-
-       for (i = 0; lines[i]; i++)
-               numlines++;
-
-       result = (char **) pg_malloc(numlines * sizeof(char *));
-
-       for (src = 0, dst = 0; src < numlines; src++)
-       {
-               if (lines[src] == NULL || strstr(lines[src], token) == NULL)
-                       result[dst++] = lines[src];
-       }
-
-       return result;
-}
-#endif
-
 /*
  * get the lines from a text file
  */
@@ -1072,10 +1039,70 @@ setup_config(void)
        char            repltok[MAXPGPATH];
        char            path[MAXPGPATH];
        char       *autoconflines[3];
+       bool            unix_sockets_work = false;
+       bool            ipv6_works = false;
 
        fputs(_("creating configuration files ... "), stdout);
        fflush(stdout);
 
+#ifdef WIN32
+       {
+               /* need to call WSAStartup before calling socket or getaddrinfo 
*/
+               int                     err = 0;
+               WSADATA         wsaData;
+
+               err = WSAStartup(MAKEWORD(2, 2), &wsaData);
+               if (err != 0)
+               {
+                       pg_log_error("WSAStartup failed: %d", err);
+                       exit(1);
+               }
+       }
+#endif
+
+#ifdef HAVE_UNIX_SOCKETS
+       /*
+        * Probe to see whether Unix-domain sockets are working.
+        */
+       {
+               pgsocket    tmpsock;
+
+               tmpsock = socket(AF_UNIX, SOCK_STREAM, 0);
+               if (tmpsock != PGINVALID_SOCKET)
+               {
+                       unix_sockets_work = true;
+                       closesocket(tmpsock);
+               }
+       }
+#endif
+
+#ifdef HAVE_IPV6
+       /*
+        * Probe to see if there is really any platform support for IPv6, and
+        * comment out the relevant pg_hba line if not.  This avoids runtime
+        * warnings if getaddrinfo doesn't actually cope with IPv6.  
Particularly
+        * useful on Windows, where executables built on a machine with IPv6 may
+        * have to run on a machine without.
+        */
+       {
+               struct addrinfo *gai_result;
+               struct addrinfo hints;
+
+               /* for best results, this code should match parse_hba_line() */
+               hints.ai_flags = AI_NUMERICHOST;
+               hints.ai_family = AF_UNSPEC;
+               hints.ai_socktype = 0;
+               hints.ai_protocol = 0;
+               hints.ai_addrlen = 0;
+               hints.ai_canonname = NULL;
+               hints.ai_addr = NULL;
+               hints.ai_next = NULL;
+
+               if (getaddrinfo("::1", NULL, &hints, &gai_result) == 0)
+                       ipv6_works = true;
+       }
+#endif                                                 /* !HAVE_IPV6 */
+
        /* postgresql.conf */
 
        conflines = readfile(conf_file);
@@ -1092,8 +1119,11 @@ setup_config(void)
        conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
 
 #ifdef HAVE_UNIX_SOCKETS
-       snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'",
-                        DEFAULT_PGSOCKET_DIR);
+       if (unix_sockets_work)
+               snprintf(repltok, sizeof(repltok), "#unix_socket_directories = 
'%s'",
+                                DEFAULT_PGSOCKET_DIR);
+       else
+               snprintf(repltok, sizeof(repltok), "unix_socket_directories = 
''");
 #else
        snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''");
 #endif
@@ -1256,63 +1286,25 @@ setup_config(void)
 
        conflines = readfile(hba_file);
 
-#ifndef HAVE_UNIX_SOCKETS
-       conflines = filter_lines_with_token(conflines, 
"@remove-line-for-nolocal@");
-#else
-       conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
-#endif
-
-#ifdef HAVE_IPV6
-
-       /*
-        * Probe to see if there is really any platform support for IPv6, and
-        * comment out the relevant pg_hba line if not.  This avoids runtime
-        * warnings if getaddrinfo doesn't actually cope with IPv6.  
Particularly
-        * useful on Windows, where executables built on a machine with IPv6 may
-        * have to run on a machine without.
-        */
+       if (!unix_sockets_work)
        {
-               struct addrinfo *gai_result;
-               struct addrinfo hints;
-               int                     err = 0;
-
-#ifdef WIN32
-               /* need to call WSAStartup before calling getaddrinfo */
-               WSADATA         wsaData;
-
-               err = WSAStartup(MAKEWORD(2, 2), &wsaData);
-#endif
-
-               /* for best results, this code should match parse_hba_line() */
-               hints.ai_flags = AI_NUMERICHOST;
-               hints.ai_family = AF_UNSPEC;
-               hints.ai_socktype = 0;
-               hints.ai_protocol = 0;
-               hints.ai_addrlen = 0;
-               hints.ai_canonname = NULL;
-               hints.ai_addr = NULL;
-               hints.ai_next = NULL;
+               conflines = replace_token(conflines,
+                                                                 "local   all",
+                                                                 "#local   
all");
+               conflines = replace_token(conflines,
+                                                                 "local   
replication",
+                                                                 "#local   
replication");
+       }
 
-               if (err != 0 ||
-                       getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
-               {
-                       conflines = replace_token(conflines,
-                                                                         "host 
   all             all             ::1",
-                                                                         
"#host    all             all             ::1");
-                       conflines = replace_token(conflines,
-                                                                         "host 
   replication     all             ::1",
-                                                                         
"#host    replication     all             ::1");
-               }
+       if (!ipv6_works)
+       {
+               conflines = replace_token(conflines,
+                                                                 "host    all  
           all             ::1",
+                                                                 "#host    all 
            all             ::1");
+               conflines = replace_token(conflines,
+                                                                 "host    
replication     all             ::1",
+                                                                 "#host    
replication     all             ::1");
        }
-#else                                                  /* !HAVE_IPV6 */
-       /* If we didn't compile IPV6 support at all, always comment it out */
-       conflines = replace_token(conflines,
-                                                         "host    all          
   all             ::1",
-                                                         "#host    all         
    all             ::1");
-       conflines = replace_token(conflines,
-                                                         "host    replication  
   all             ::1",
-                                                         "#host    replication 
    all             ::1");
-#endif                                                 /* HAVE_IPV6 */
 
        /* Replace default authentication methods */
        conflines = replace_token(conflines,
-- 
2.22.0

From cb66ae54a4df19d2dbfcf74e9073d36435b6abc5 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Thu, 8 Aug 2019 11:19:04 +0200
Subject: [PATCH v3 6/7] Fix handling of Unix-domain sockets on Windows in
 tests

Don't run the tests using Unix-domain sockets by default on Windows,
but allow enabling it explicitly by setting the environment variable
PG_TEST_USE_UNIX_SOCKETS.
---
 src/bin/pg_ctl/t/001_start_stop.pl        |  2 +-
 src/test/authentication/t/001_password.pl |  7 +++----
 src/test/authentication/t/002_saslprep.pl |  7 +++----
 src/test/perl/PostgresNode.pm             |  4 ++--
 src/test/perl/TestLib.pm                  |  8 +++++++-
 src/test/regress/pg_regress.c             | 14 ++++++++++----
 6 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/src/bin/pg_ctl/t/001_start_stop.pl 
b/src/bin/pg_ctl/t/001_start_stop.pl
index e5d46a6f25..70748354a6 100644
--- a/src/bin/pg_ctl/t/001_start_stop.pl
+++ b/src/bin/pg_ctl/t/001_start_stop.pl
@@ -27,7 +27,7 @@
 print $conf TestLib::slurp_file($ENV{TEMP_CONFIG})
   if defined $ENV{TEMP_CONFIG};
 
-if (!$windows_os)
+if ($use_unix_sockets)
 {
        print $conf "listen_addresses = ''\n";
        print $conf "unix_socket_directories = '$tempdir_short'\n";
diff --git a/src/test/authentication/t/001_password.pl 
b/src/test/authentication/t/001_password.pl
index 3a3b0eb7e8..f69d6dcf3f 100644
--- a/src/test/authentication/t/001_password.pl
+++ b/src/test/authentication/t/001_password.pl
@@ -3,17 +3,16 @@
 # - Plain
 # - MD5-encrypted
 # - SCRAM-encrypted
-# This test cannot run on Windows as Postgres cannot be set up with Unix
-# sockets and needs to go through SSPI.
+# This test can only run with Unix-domain sockets.
 
 use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
 use Test::More;
-if ($windows_os)
+if (!$use_unix_sockets)
 {
-       plan skip_all => "authentication tests cannot run on Windows";
+       plan skip_all => "authentication tests cannot run without Unix-domain 
sockets";
 }
 else
 {
diff --git a/src/test/authentication/t/002_saslprep.pl 
b/src/test/authentication/t/002_saslprep.pl
index c4b335c45f..bf57933d94 100644
--- a/src/test/authentication/t/002_saslprep.pl
+++ b/src/test/authentication/t/002_saslprep.pl
@@ -1,16 +1,15 @@
 # Test password normalization in SCRAM.
 #
-# This test cannot run on Windows as Postgres cannot be set up with Unix
-# sockets and needs to go through SSPI.
+# This test can only run with Unix-domain sockets.
 
 use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
 use Test::More;
-if ($windows_os)
+if (!$use_unix_sockets)
 {
-       plan skip_all => "authentication tests cannot run on Windows";
+       plan skip_all => "authentication tests cannot run without Unix-domain 
sockets";
 }
 else
 {
diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index 270bd6c856..04d9e6bde1 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -116,7 +116,7 @@ INIT
 
        # Set PGHOST for backward compatibility.  This doesn't work for own_host
        # nodes, so prefer to not rely on this when writing new tests.
-       $use_tcp            = $TestLib::windows_os;
+       $use_tcp            = !$TestLib::use_unix_sockets;
        $test_localhost     = "127.0.0.1";
        $last_host_assigned = 1;
        $test_pghost        = $use_tcp ? $test_localhost : 
TestLib::tempdir_short;
@@ -387,7 +387,7 @@ sub set_replication_conf
 
        open my $hba, '>>', "$pgdata/pg_hba.conf";
        print $hba "\n# Allow replication (set up by PostgresNode.pm)\n";
-       if ($TestLib::windows_os)
+       if ($TestLib::windows_os && !$TestLib::use_unix_sockets)
        {
                print $hba
                  "host replication all $test_localhost/32 sspi include_realm=1 
map=regress\n";
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index 92199792eb..c8b8613d9a 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -83,9 +83,10 @@ our @EXPORT = qw(
   command_checks_all
 
   $windows_os
+  $use_unix_sockets
 );
 
-our ($windows_os, $tmp_check, $log_path, $test_logfile);
+our ($windows_os, $use_unix_sockets, $tmp_check, $log_path, $test_logfile);
 
 BEGIN
 {
@@ -112,6 +113,11 @@ BEGIN
 
        # Must be set early
        $windows_os = $Config{osname} eq 'MSWin32' || $Config{osname} eq 'msys';
+
+       # Specifies whether to use Unix sockets for test setups.  On
+       # Windows we don't use them by default since it's not universally
+       # supported, but it can be overridden if desired.
+       $use_unix_sockets = (!$windows_os || $ENV{PG_TEST_USE_UNIX_SOCKETS} ne 
'');
 }
 
 =pod
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index b4045abf21..0ac05a3ea3 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -102,11 +102,10 @@ static char *logfilename;
 static FILE *logfile;
 static char *difffilename;
 static const char *sockdir;
-#ifdef HAVE_UNIX_SOCKETS
+static bool use_unix_sockets;
 static const char *temp_sockdir;
 static char sockself[MAXPGPATH];
 static char socklock[MAXPGPATH];
-#endif
 
 static _resultmap *resultmap = NULL;
 
@@ -2121,10 +2120,17 @@ regression_main(int argc, char *argv[], init_function 
ifunc, test_function tfunc
        atexit(stop_postmaster);
 
 #ifndef HAVE_UNIX_SOCKETS
-       /* no unix domain sockets available, so change default */
-       hostname = "localhost";
+       use_unix_sockets = false;
+#elif defined(WIN32)
+       use_unix_sockets = getenv("PG_TEST_USE_UNIX_SOCKETS") ? true : false;
+#else
+       use_unix_sockets = true;
 #endif
 
+       if (!use_unix_sockets)
+               /* no unix domain sockets available, so change default */
+               hostname = "localhost";
+
        /*
         * We call the initialization function here because that way we can set
         * default parameters and let them be overwritten by the commandline.
-- 
2.22.0

From 9752500fa24f305ca075ae839bd8890002db2136 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Tue, 13 Aug 2019 00:47:59 +0200
Subject: [PATCH v3 7/7] Disable Unix sockets by default on Windows

---
 src/backend/utils/misc/guc.c      | 2 +-
 src/bin/initdb/initdb.c           | 2 +-
 src/bin/psql/prompt.c             | 2 ++
 src/bin/scripts/pg_isready.c      | 4 ++++
 src/include/libpq/pqcomm.h        | 8 ++++++++
 src/include/port/win32.h          | 8 ++++++++
 src/interfaces/libpq/fe-connect.c | 4 +++-
 7 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 90ffd89339..e4a82c7f07 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -3939,7 +3939,7 @@ static struct config_string ConfigureNamesString[] =
                        GUC_SUPERUSER_ONLY
                },
                &Unix_socket_directories,
-#ifdef HAVE_UNIX_SOCKETS
+#if defined(HAVE_UNIX_SOCKETS) && defined(DEFAULT_PGSOCKET_DIR)
                DEFAULT_PGSOCKET_DIR,
 #else
                "",
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index ffbcf685d5..5931e6baad 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1118,7 +1118,7 @@ setup_config(void)
                                 n_buffers * (BLCKSZ / 1024));
        conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
 
-#ifdef HAVE_UNIX_SOCKETS
+#if defined(HAVE_UNIX_SOCKETS) && defined(DEFAULT_PGSOCKET_DIR)
        if (unix_sockets_work)
                snprintf(repltok, sizeof(repltok), "#unix_socket_directories = 
'%s'",
                                 DEFAULT_PGSOCKET_DIR);
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index f8edc942da..7764bae064 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -138,7 +138,9 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
                                                else
                                                {
                                                        if (!host
+#ifdef DEFAULT_PGSOCKET_DIR
                                                                || strcmp(host, 
DEFAULT_PGSOCKET_DIR) == 0
+#endif
                                                                || *p == 'm')
                                                                strlcpy(buf, 
"[local]", sizeof(buf));
                                                        else
diff --git a/src/bin/scripts/pg_isready.c b/src/bin/scripts/pg_isready.c
index 079447f951..4dfc8712bf 100644
--- a/src/bin/scripts/pg_isready.c
+++ b/src/bin/scripts/pg_isready.c
@@ -164,7 +164,11 @@ main(int argc, char **argv)
                        else if (def->val)
                                pghost_str = def->val;
                        else
+#ifdef DEFAULT_PGSOCKET_DIR
                                pghost_str = DEFAULT_PGSOCKET_DIR;
+#else
+                               pghost_str = "";
+#endif
                }
                else if (strcmp(def->keyword, "hostaddr") == 0)
                {
diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h
index baf6a4b6c0..8fe332c1e4 100644
--- a/src/include/libpq/pqcomm.h
+++ b/src/include/libpq/pqcomm.h
@@ -67,11 +67,19 @@ typedef struct
 
 /* Configure the UNIX socket location for the well known port. */
 
+#ifdef DEFAULT_PGSOCKET_DIR
 #define UNIXSOCK_PATH(path, port, sockdir) \
                snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
                                ((sockdir) && *(sockdir) != '\0') ? (sockdir) : 
\
                                DEFAULT_PGSOCKET_DIR, \
                                (port))
+#else
+#define UNIXSOCK_PATH(path, port, sockdir) \
+               (AssertMacro((sockdir) && *(sockdir) != '\0'), \
+                snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
+                                 (sockdir), \
+                                 (port)))
+#endif
 
 /*
  * The maximum workable length of a socket path is what will fit into
diff --git a/src/include/port/win32.h b/src/include/port/win32.h
index 9f48a58aed..47c1868149 100644
--- a/src/include/port/win32.h
+++ b/src/include/port/win32.h
@@ -56,3 +56,11 @@
 #else
 #define PGDLLEXPORT
 #endif
+
+
+/*
+ * On Windows, there is no good standard location for AF_UNIX sockets, and
+ * many builds of Windows don't support them yet, so don't create one by
+ * default.
+ */
+#undef DEFAULT_PGSOCKET_DIR
diff --git a/src/interfaces/libpq/fe-connect.c 
b/src/interfaces/libpq/fe-connect.c
index 79323114e4..1b5ddc92ff 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1065,7 +1065,7 @@ connectOptions2(PGconn *conn)
                {
                        if (ch->host)
                                free(ch->host);
-#ifdef HAVE_UNIX_SOCKETS
+#if defined(HAVE_UNIX_SOCKETS) && defined(DEFAULT_PGSOCKET_DIR)
                        ch->host = strdup(DEFAULT_PGSOCKET_DIR);
                        ch->type = CHT_UNIX_SOCKET;
 #else
@@ -6853,6 +6853,7 @@ passwordFromFile(const char *hostname, const char *port, 
const char *dbname,
        /* 'localhost' matches pghost of '' or the default socket directory */
        if (hostname == NULL || hostname[0] == '\0')
                hostname = DefaultHost;
+#ifdef DEFAULT_PGSOCKET_DIR
        else if (is_absolute_path(hostname))
 
                /*
@@ -6861,6 +6862,7 @@ passwordFromFile(const char *hostname, const char *port, 
const char *dbname,
                 */
                if (strcmp(hostname, DEFAULT_PGSOCKET_DIR) == 0)
                        hostname = DefaultHost;
+#endif
 
        if (port == NULL || port[0] == '\0')
                port = DEF_PGPORT_STR;
-- 
2.22.0

Reply via email to