Unix-domain socket support on Windows

Started by Peter Eisentrautover 6 years ago22 messages
#1Peter Eisentraut
peter.eisentraut@2ndquadrant.com
5 attachment(s)

It works!

(apparently as of Windows 10 version 1803)

Here are some patches to get a discussion rolling.

Basically, it just works, but you need to define your own struct
sockaddr_un. (This is what configure currently uses as a proxy for
HAVE_UNIX_SOCKETS, so (a) that needs a bit of tweaking, and (b) that is
the reason why builds haven't blown up already.)

But we'll now need to make things work so that binaries with Unix-domain
socket support work on systems without run-time support. We already did
that exercise with IPv6 support, so some of the framework is already in
place.

Depending on your Windows environment, there might not be a suitable
/tmp directory, so you'll need to specify a directory explicitly using
postgres -k or similar. This leads to the question what the default for
DEFAULT_PGSOCKET_DIR should be on Windows. I think it's probably best,
at least for now, to set it so that by default, neither server nor libpq
use Unix sockets unless explicitly selected. This can be done easily on
the server side by defining DEFAULT_PGSOCKET_DIR as "". But in libpq, I
don't think the code would handle that correctly everywhere, so it would
need some more analysis and restructuring possibly.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Enable-Unix-domain-sockets-support-on-Windows.patchtext/plain; charset=UTF-8; name=0001-Enable-Unix-domain-sockets-support-on-Windows.patch; x-mac-creator=0; x-mac-type=0Download
From 1f3575f38ca1321e0c5d71217cfb46f8efc35caa Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH 1/5] 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 7a6bfc2339..6e87537be3 100755
--- a/configure
+++ b/configure
@@ -14135,7 +14135,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 2a082afab1..434c403269 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1263,6 +1263,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 512213aa32..e75c090f85 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -594,6 +594,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
 
@@ -682,9 +685,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 2d903c82b8..b947a9fbde 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -463,6 +463,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 */
 
@@ -530,9 +533,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()
  */
-- 
2.22.0

0002-Sort-out-getpeereid-and-struct-passwd-handling-on-Wi.patchtext/plain; charset=UTF-8; name=0002-Sort-out-getpeereid-and-struct-passwd-handling-on-Wi.patch; x-mac-creator=0; x-mac-type=0Download
From 8bcbe1ab6786e746ac050f464c324c6e9aacdaa2 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH 2/5] 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 ++++
 5 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/configure b/configure
index 6e87537be3..8c48c6ce81 100755
--- a/configure
+++ b/configure
@@ -15837,6 +15837,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
@@ -16017,17 +16030,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 :
@@ -16040,19 +16047,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 dde3eec89f..03fd9b6a61 100644
--- a/configure.in
+++ b/configure.in
@@ -1696,6 +1696,7 @@ AC_REPLACE_FUNCS(m4_normalize([
 	dlopen
 	fls
 	getopt
+	getpeereid
 	getrusage
 	inet_aton
 	mkdtemp
@@ -1726,15 +1727,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 5fb1f1b7d7..8f49b457e4 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 b5c03d912b..a3e7b5c03c 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -357,7 +357,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 d262b57021..b4fa7a259f 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -2685,8 +2685,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;
@@ -2708,6 +2710,7 @@ PQconnectPoll(PGconn *conn)
 						goto error_return;
 					}
 
+#ifndef WIN32
 					passerr = pqGetpwuid(uid, &pass_buf, pwdbuf, sizeof(pwdbuf), &pass);
 					if (pass == NULL)
 					{
@@ -2730,6 +2733,7 @@ PQconnectPoll(PGconn *conn)
 										  conn->requirepeer, pass->pw_name);
 						goto error_return;
 					}
+#endif							/* WIN32 */
 				}
 #endif							/* HAVE_UNIX_SOCKETS */
 
-- 
2.22.0

0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patchtext/plain; charset=UTF-8; name=0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patch; x-mac-creator=0; x-mac-type=0Download
From 6ab2c7c3adffe4a9e86068e8d05bd1f332a44368 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH 3/5] 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 59afbc793a..0d6c1a5364 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"
@@ -138,7 +133,6 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
 							if (*p == 'm')
 								buf[strcspn(buf, ".")] = '\0';
 						}
-#ifdef HAVE_UNIX_SOCKETS
 						/* UNIX socket */
 						else
 						{
@@ -149,7 +143,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

0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKETS.patchtext/plain; charset=UTF-8; name=0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKETS.patch; x-mac-creator=0; x-mac-type=0Download
From 202758ed27915d0d4e1d430e24578d13f6a2412a Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH 4/5] 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 b4fa7a259f..a2c1c3002b 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1057,10 +1057,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
 		{
@@ -1568,7 +1566,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];
@@ -1585,7 +1582,6 @@ connectFailureMessage(PGconn *conn, int errorno)
 						  service);
 	}
 	else
-#endif							/* HAVE_UNIX_SOCKETS */
 	{
 		char		host_addr[NI_MAXHOST];
 		const char *displayed_host;
@@ -2675,8 +2671,6 @@ PQconnectPoll(PGconn *conn)
 				char	   *startpacket;
 				int			packetlen;
 
-#ifdef HAVE_UNIX_SOCKETS
-
 				/*
 				 * Implement requirepeer check, if requested and it's a
 				 * Unix-domain socket.
@@ -2735,7 +2729,6 @@ PQconnectPoll(PGconn *conn)
 					}
 #endif							/* WIN32 */
 				}
-#endif							/* HAVE_UNIX_SOCKETS */
 
 				if (IS_AF_UNIX(conn->raddr.addr.ss_family))
 				{
-- 
2.22.0

0005-initdb-Detect-Unix-domain-socket-support-dynamically.patchtext/plain; charset=UTF-8; name=0005-initdb-Detect-Unix-domain-socket-support-dynamically.patch; x-mac-creator=0; x-mac-type=0Download
From 409e472821e726712cf673d6cea223c7d25d0fd1 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH 5/5] 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 9c303e9cc4..4788b75247 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);
@@ -445,36 +442,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
  */
@@ -1084,10 +1051,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);
@@ -1104,8 +1131,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
@@ -1268,63 +1298,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

#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Peter Eisentraut (#1)
Re: Unix-domain socket support on Windows

On 07/08/2019 16:56, Peter Eisentraut wrote:

It works!

Cool!

Am I reading the patches correctly, that getpeereid() still doesn't work
on Windows? That means that peer authentication doesn't work, right?
That's a bit sad. One of the big advantages of unix domain sockets over
TCP is peer authentication.

- Heikki

#3Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Heikki Linnakangas (#2)
Re: Unix-domain socket support on Windows

On 2019-08-07 16:06, Heikki Linnakangas wrote:

Am I reading the patches correctly, that getpeereid() still doesn't work
on Windows? That means that peer authentication doesn't work, right?
That's a bit sad. One of the big advantages of unix domain sockets over
TCP is peer authentication.

Correct, it's not supported. I think it's plausible that they will add
this in the future.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#4Magnus Hagander
magnus@hagander.net
In reply to: Peter Eisentraut (#3)
Re: Unix-domain socket support on Windows

On Wed, Aug 7, 2019 at 4:59 PM Peter Eisentraut <
peter.eisentraut@2ndquadrant.com> wrote:

On 2019-08-07 16:06, Heikki Linnakangas wrote:

Am I reading the patches correctly, that getpeereid() still doesn't work
on Windows? That means that peer authentication doesn't work, right?
That's a bit sad. One of the big advantages of unix domain sockets over
TCP is peer authentication.

Correct, it's not supported. I think it's plausible that they will add
this in the future.

Does it work well enough that SSPI auth can run over it? SSPI auth with the
local provider gives you more or less the same results as peer, doesn't it?

--
Magnus Hagander
Me: https://www.hagander.net/ <http://www.hagander.net/&gt;
Work: https://www.redpill-linpro.com/ <http://www.redpill-linpro.com/&gt;

#5Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#1)
7 attachment(s)
Re: Unix-domain socket support on Windows

On 2019-08-07 15:56, Peter Eisentraut wrote:

Depending on your Windows environment, there might not be a suitable
/tmp directory, so you'll need to specify a directory explicitly using
postgres -k or similar. This leads to the question what the default for
DEFAULT_PGSOCKET_DIR should be on Windows. I think it's probably best,
at least for now, to set it so that by default, neither server nor libpq
use Unix sockets unless explicitly selected. This can be done easily on
the server side by defining DEFAULT_PGSOCKET_DIR as "". But in libpq, I
don't think the code would handle that correctly everywhere, so it would
need some more analysis and restructuring possibly.

Updated patches, which now also address that issue: There is no default
socket dir on Windows and it's disabled by default on both client and
server.

Some comments on the patches:

v2-0001-Enable-Unix-domain-sockets-support-on-Windows.patch

This is pretty straightforward, apart from maybe some comments, but it
would need to be committed last, because it would enable all the Unix
socket related code on Windows, which needs to be fixed up by the
subsequent patches first.

v2-0002-Sort-out-getpeereid-and-struct-passwd-handling-on.patch

Maybe a more elegant way with fewer #ifdef WIN32 can be found?

v2-0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patch

This could be committed independently.

v2-0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKET.patch

This one as well.

v2-0005-initdb-Detect-Unix-domain-socket-support-dynamica.patch

I think this patch contains some nice improvements in general. How much
of that ends up being useful depends on how the subsequent patches (esp.
0007) end up, since with Unix-domain sockets disabled by default on
Windows, we won't need initdb doing any detection.

v2-0006-Fix-handling-of-Unix-domain-sockets-on-Windows-in.patch

This is a fairly independent and isolated change.

v2-0007-Disable-Unix-sockets-by-default-on-Windows.patch

This one is a bit complicated. Since there is no good default location
for Unix sockets on Windows, and many systems won't support them for
some time, the default implemented here is to not use them by default on
the server or client. This needs a fair amount of restructuring in the
to support the case of "supports Unix sockets but don't use them by
default", while maintaining the existing cases of "doesn't support Unix
sockets" and "use Unix sockets by default". There is some room for
discussion here.

This patch set needs testers with various Windows versions to test
different configurations, combinations, and versions.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v2-0001-Enable-Unix-domain-sockets-support-on-Windows.patchtext/plain; charset=UTF-8; name=v2-0001-Enable-Unix-domain-sockets-support-on-Windows.patch; x-mac-creator=0; x-mac-type=0Download
From c817dfc0ec2aead25e6fcd33f9e89bd55cb2c0b1 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v2 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 7a6bfc2339..6e87537be3 100755
--- a/configure
+++ b/configure
@@ -14135,7 +14135,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 2a082afab1..434c403269 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1263,6 +1263,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 512213aa32..e75c090f85 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -594,6 +594,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
 
@@ -682,9 +685,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 2d903c82b8..b947a9fbde 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -463,6 +463,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 */
 
@@ -530,9 +533,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: 66bde49d96a9ddacc49dcbdf1b47b5bd6e31ead5
-- 
2.22.0

v2-0002-Sort-out-getpeereid-and-struct-passwd-handling-on.patchtext/plain; charset=UTF-8; name=v2-0002-Sort-out-getpeereid-and-struct-passwd-handling-on.patch; x-mac-creator=0; x-mac-type=0Download
From 66a8cba64515da97b6614fde23d52bc1f0a6ecd2 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v2 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 ++++
 5 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/configure b/configure
index 6e87537be3..8c48c6ce81 100755
--- a/configure
+++ b/configure
@@ -15837,6 +15837,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
@@ -16017,17 +16030,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 :
@@ -16040,19 +16047,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 dde3eec89f..03fd9b6a61 100644
--- a/configure.in
+++ b/configure.in
@@ -1696,6 +1696,7 @@ AC_REPLACE_FUNCS(m4_normalize([
 	dlopen
 	fls
 	getopt
+	getpeereid
 	getrusage
 	inet_aton
 	mkdtemp
@@ -1726,15 +1727,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 b5c03d912b..a3e7b5c03c 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -357,7 +357,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 fa5af18ffa..ec456725dd 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 */
 
-- 
2.22.0

v2-0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patchtext/plain; charset=UTF-8; name=v2-0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patch; x-mac-creator=0; x-mac-type=0Download
From 6c6a7f4a0fdf165b5fee5bdd1dab88388cc2f623 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v2 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

v2-0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKET.patchtext/plain; charset=UTF-8; name=v2-0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKET.patch; x-mac-creator=0; x-mac-type=0Download
From a5fb2dbd8a9bc17541250a7e6093a5d115d8b745 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v2 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 ec456725dd..ac4fc8a6fc 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

v2-0005-initdb-Detect-Unix-domain-socket-support-dynamica.patchtext/plain; charset=UTF-8; name=v2-0005-initdb-Detect-Unix-domain-socket-support-dynamica.patch; x-mac-creator=0; x-mac-type=0Download
From fc787f67222d32d144529ed8f9e0242c30632e85 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v2 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 551d379d85..db7054fbf8 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

v2-0006-Fix-handling-of-Unix-domain-sockets-on-Windows-in.patchtext/plain; charset=UTF-8; name=v2-0006-Fix-handling-of-Unix-domain-sockets-on-Windows-in.patch; x-mac-creator=0; x-mac-type=0Download
From 00aa3b1f9c621d12c47f8e2e5f40a0a1ef0b0ce3 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 8 Aug 2019 11:19:04 +0200
Subject: [PATCH v2 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 6195c21c59..a20c3709eb 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -50,9 +50,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
 {
@@ -79,6 +80,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 '');
 }
 
 INIT
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 117a9544ea..22b40304ce 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;
 
@@ -2117,10 +2116,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

v2-0007-Disable-Unix-sockets-by-default-on-Windows.patchtext/plain; charset=UTF-8; name=v2-0007-Disable-Unix-sockets-by-default-on-Windows.patch; x-mac-creator=0; x-mac-type=0Download
From bc754ebf8304c03ed4ddb9e71901a639ae9a323c Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 13 Aug 2019 00:47:59 +0200
Subject: [PATCH v2 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 eb78522053..c8fe30ce53 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 db7054fbf8..da563a9bf6 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 ac4fc8a6fc..abfc1c409f 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

#6Thomas Munro
thomas.munro@gmail.com
In reply to: Peter Eisentraut (#5)
Re: Unix-domain socket support on Windows

On Wed, Aug 14, 2019 at 6:27 AM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

This patch set needs testers with various Windows versions to test
different configurations, combinations, and versions.

It's failing to build on cfbot's AppVeyor setup[1]https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.55034?fullLog=true. That's currently
using Windows SDK 7.1, so too old for the new AF_UNIX sockets, but
presumably something is wrong because it shouldn't fail to compile and
link.

src/interfaces/libpq/fe-connect.c(2682): warning C4101: 'pwdbuf' :
unreferenced local variable [C:\projects\postgresql\libpq.vcxproj]
src/interfaces/libpq/fe-connect.c(2687): warning C4101: 'passerr' :
unreferenced local variable [C:\projects\postgresql\libpq.vcxproj]

fe-connect.obj : error LNK2019: unresolved external symbol getpeereid
referenced in function PQconnectPoll
[C:\projects\postgresql\libpq.vcxproj]

[1]: https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.55034?fullLog=true

--
Thomas Munro
https://enterprisedb.com

#7Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Thomas Munro (#6)
7 attachment(s)
Re: Unix-domain socket support on Windows

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

Attachments:

v3-0001-Enable-Unix-domain-sockets-support-on-Windows.patchtext/plain; charset=UTF-8; name=v3-0001-Enable-Unix-domain-sockets-support-on-Windows.patch; x-mac-creator=0; x-mac-type=0Download
From 5f7549edaaa33bf7fa0c1dd4e164d188ae4e5cd4 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

v3-0002-Sort-out-getpeereid-and-struct-passwd-handling-on.patchtext/plain; charset=UTF-8; name=v3-0002-Sort-out-getpeereid-and-struct-passwd-handling-on.patch; x-mac-creator=0; x-mac-type=0Download
From 16a4afffc28395d19b50c95dc79cd78cbf459a08 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

v3-0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patchtext/plain; charset=UTF-8; name=v3-0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patch; x-mac-creator=0; x-mac-type=0Download
From 03c4c306f71d5d75b678558edb4dc9a6f47f1a2f Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

v3-0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKET.patchtext/plain; charset=UTF-8; name=v3-0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKET.patch; x-mac-creator=0; x-mac-type=0Download
From b7bfde70e748ae67bba183e874cd98ea7097fc83 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

v3-0005-initdb-Detect-Unix-domain-socket-support-dynamica.patchtext/plain; charset=UTF-8; name=v3-0005-initdb-Detect-Unix-domain-socket-support-dynamica.patch; x-mac-creator=0; x-mac-type=0Download
From b5f496c2ee05266909bd9952bcac819377850083 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

v3-0006-Fix-handling-of-Unix-domain-sockets-on-Windows-in.patchtext/plain; charset=UTF-8; name=v3-0006-Fix-handling-of-Unix-domain-sockets-on-Windows-in.patch; x-mac-creator=0; x-mac-type=0Download
From cb66ae54a4df19d2dbfcf74e9073d36435b6abc5 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

v3-0007-Disable-Unix-sockets-by-default-on-Windows.patchtext/plain; charset=UTF-8; name=v3-0007-Disable-Unix-sockets-by-default-on-Windows.patch; x-mac-creator=0; x-mac-type=0Download
From 9752500fa24f305ca075ae839bd8890002db2136 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@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

#8Alvaro Herrera from 2ndQuadrant
alvherre@alvh.no-ip.org
In reply to: Peter Eisentraut (#7)
Re: Unix-domain socket support on Windows

On 2019-Sep-03, Peter Eisentraut wrote:

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.

Minor bitrot in MSVC script; probably trivial to resolve.

I think you should get 0001 (+0002?) pushed and see what the buildfarm
thinks; move forward from there. 0003+0004 sound like they should just
be pushed shortly afterwards, while the three remaining ones might need
some more careful review.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#9Alvaro Herrera from 2ndQuadrant
alvherre@alvh.no-ip.org
In reply to: Alvaro Herrera from 2ndQuadrant (#8)
Re: Unix-domain socket support on Windows

On 2019-Sep-06, Alvaro Herrera from 2ndQuadrant wrote:

I think you should get 0001 (+0002?) pushed and see what the buildfarm
thinks; move forward from there.

... but of course this goes counter to what you said earlier about 0001
needing to be pushed last.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#10Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#7)
1 attachment(s)
Re: Unix-domain socket support on Windows

To move this topic a long, I'll submit some preparatory patches in a
committable order.

First is the patch to deal with getpeereid() that was already included
in the previous patch series. This is just some refactoring that
reduces the difference between Windows and other platforms and prepares
the Unix-domain socket specific code to compile cleanly on Windows.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v4-0001-Sort-out-getpeereid-and-peer-auth-handling-on-Win.patchtext/plain; charset=UTF-8; name=v4-0001-Sort-out-getpeereid-and-peer-auth-handling-on-Win.patch; x-mac-creator=0; x-mac-type=0Download
From adcad11992f4fde0a1eda4f96fef4abeb8570cdc Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 30 Oct 2019 12:58:32 +0100
Subject: [PATCH v4] Sort out getpeereid() and peer auth handling on Windows

The getpeereid() uses have so far been protected by HAVE_UNIX_SOCKETS,
so they didn't ever care about Windows support.  But in anticipation
of Unix-domain socket support on Windows, that needs to be handled
differently.

Windows doesn't support getpeereid() at this time, so we use the
existing not-supported code path.  We let configure do its usual thing
of picking up the replacement from libpgport, instead of the custom
overrides that it was doing before.

But then Windows doesn't have struct passwd, so this patch sprinkles
some additional #ifdef WIN32 around to make it work.  This is similar
to existing code that deals with this issue.
---
 configure                         | 36 +++++++++++++------------------
 configure.in                      | 11 ++++------
 src/backend/libpq/auth.c          | 17 +++++++--------
 src/include/port.h                |  2 +-
 src/interfaces/libpq/fe-connect.c | 10 ++++++---
 src/tools/msvc/Mkvcbuild.pm       |  2 +-
 6 files changed, 36 insertions(+), 42 deletions(-)

diff --git a/configure b/configure
index 6b1c779ee3..5d4998d9e1 100755
--- a/configure
+++ b/configure
@@ -15743,6 +15743,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
@@ -15921,19 +15934,13 @@ $as_echo "$as_me: On $host_os we will use our strtof wrapper." >&6;}
 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_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
 if test "x$ac_cv_func_unsetenv" = xyes; then :
@@ -15948,21 +15955,8 @@ 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
-
-
-		;;
+                ;;
 esac
 
 # System's version of getaddrinfo(), if any, may be used only if we found
diff --git a/configure.in b/configure.in
index 2b9025cac3..ebbeea8b2b 100644
--- a/configure.in
+++ b/configure.in
@@ -1718,6 +1718,7 @@ AC_REPLACE_FUNCS(m4_normalize([
 	explicit_bzero
 	fls
 	getopt
+	getpeereid
 	getrusage
 	inet_aton
 	mkdtemp
@@ -1746,18 +1747,14 @@ case $host_os in
 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_REPLACE_FUNCS([unsetenv getpeereid])
-		;;
+                AC_REPLACE_FUNCS([unsetenv])
+                ;;
 esac
 
 # System's version of getaddrinfo(), if any, may be used only if we found
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index d28271c1d8..0bafb83557 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -78,9 +78,7 @@ static int	ident_inet(hbaPort *port);
  * Peer authentication
  *----------------------------------------------------------------
  */
-#ifdef HAVE_UNIX_SOCKETS
 static int	auth_peer(hbaPort *port);
-#endif
 
 
 /*----------------------------------------------------------------
@@ -559,11 +557,7 @@ ClientAuthentication(Port *port)
 			break;
 
 		case uaPeer:
-#ifdef HAVE_UNIX_SOCKETS
 			status = auth_peer(port);
-#else
-			Assert(false);
-#endif
 			break;
 
 		case uaIdent:
@@ -1985,16 +1979,16 @@ ident_inet(hbaPort *port)
  *
  *	Iff authorized, return STATUS_OK, otherwise return STATUS_ERROR.
  */
-#ifdef HAVE_UNIX_SOCKETS
-
 static int
 auth_peer(hbaPort *port)
 {
 	uid_t		uid;
 	gid_t		gid;
+#ifndef WIN32
 	struct passwd *pw;
 	char	   *peer_user;
 	int			ret;
+#endif
 
 	if (getpeereid(port->sock, &uid, &gid) != 0)
 	{
@@ -2010,6 +2004,7 @@ auth_peer(hbaPort *port)
 		return STATUS_ERROR;
 	}
 
+#ifndef WIN32
 	errno = 0;					/* clear errno before call */
 	pw = getpwuid(uid);
 	if (!pw)
@@ -2031,8 +2026,12 @@ auth_peer(hbaPort *port)
 	pfree(peer_user);
 
 	return ret;
+#else
+	/* should have failed with ENOSYS above */
+	Assert(false);
+	return STATUS_ERROR;
+#endif
 }
-#endif							/* HAVE_UNIX_SOCKETS */
 
 
 /*----------------------------------------------------------------
diff --git a/src/include/port.h b/src/include/port.h
index 10dcb5f0a6..e3bbd4d517 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 dcd86ee804..58b18cb417 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -2729,8 +2729,6 @@ PQconnectPoll(PGconn *conn)
 				char	   *startpacket;
 				int			packetlen;
 
-#ifdef HAVE_UNIX_SOCKETS
-
 				/*
 				 * Implement requirepeer check, if requested and it's a
 				 * Unix-domain socket.
@@ -2738,10 +2736,12 @@ PQconnectPoll(PGconn *conn)
 				if (conn->requirepeer && conn->requirepeer[0] &&
 					IS_AF_UNIX(conn->raddr.addr.ss_family))
 				{
+#ifndef WIN32
 					char		pwdbuf[BUFSIZ];
 					struct passwd pass_buf;
 					struct passwd *pass;
 					int			passerr;
+#endif
 					uid_t		uid;
 					gid_t		gid;
 
@@ -2762,6 +2762,7 @@ PQconnectPoll(PGconn *conn)
 						goto error_return;
 					}
 
+#ifndef WIN32
 					passerr = pqGetpwuid(uid, &pass_buf, pwdbuf, sizeof(pwdbuf), &pass);
 					if (pass == NULL)
 					{
@@ -2784,8 +2785,11 @@ PQconnectPoll(PGconn *conn)
 										  conn->requirepeer, pass->pw_name);
 						goto error_return;
 					}
+#else							/* WIN32 */
+					/* should have failed with ENOSYS above */
+					Assert(false);
+#endif							/* WIN32 */
 				}
-#endif							/* HAVE_UNIX_SOCKETS */
 
 				if (IS_AF_UNIX(conn->raddr.addr.ss_family))
 				{
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 7a103e6140..cda1adaeea 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -94,7 +94,7 @@ sub mkvcbuild
 	$solution = CreateSolution($vsVersion, $config);
 
 	our @pgportfiles = qw(
-	  chklocale.c explicit_bzero.c fls.c fseeko.c getrusage.c inet_aton.c random.c
+	  chklocale.c explicit_bzero.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

base-commit: c5e1df951d9d70ab7d53ce47caaf73f3b2d6b1e1
-- 
2.23.0

#11Andrew Dunstan
andrew.dunstan@2ndquadrant.com
In reply to: Peter Eisentraut (#10)
Re: Unix-domain socket support on Windows

On Wed, Oct 30, 2019 at 10:32 PM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

To move this topic a long, I'll submit some preparatory patches in a
committable order.

First is the patch to deal with getpeereid() that was already included
in the previous patch series. This is just some refactoring that
reduces the difference between Windows and other platforms and prepares
the Unix-domain socket specific code to compile cleanly on Windows.

This looks fairly sane and straightforward. Let's give it an outing on
the buildfarm ASAP so we can keep moving forward on this.

cheers

andrew

--
Andrew Dunstan https://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#12Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Andrew Dunstan (#11)
Re: Unix-domain socket support on Windows

On 2019-12-16 05:39, Andrew Dunstan wrote:

On Wed, Oct 30, 2019 at 10:32 PM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

To move this topic a long, I'll submit some preparatory patches in a
committable order.

First is the patch to deal with getpeereid() that was already included
in the previous patch series. This is just some refactoring that
reduces the difference between Windows and other platforms and prepares
the Unix-domain socket specific code to compile cleanly on Windows.

This looks fairly sane and straightforward. Let's give it an outing on
the buildfarm ASAP so we can keep moving forward on this.

pushed

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#13Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#12)
1 attachment(s)
Re: Unix-domain socket support on Windows

Next patch: This allows building *with* Unix-domain socket support but
*without* a default Unix socket path. This is needed because on Windows
we don't have a good default location like "/tmp" and we probably don't
want Unix sockets by default at run time so that older Windows versions
continue to work out of the box with the same binaries.

We have code paths for Unix socket support and no Unix socket support.
Now add a third variant: Unix socket support but do not use a Unix
socket by default in the client or the server, only if you explicitly
specify one.

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect. This mostly already worked like that;
only a few places needed to be adjusted. Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v5-0001-Allow-building-without-default-socket-directory.patchtext/plain; charset=UTF-8; name=v5-0001-Allow-building-without-default-socket-directory.patch; x-mac-creator=0; x-mac-type=0Download
From 0b63a8a758d507095bf5070cec8119436c267519 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 18 Dec 2019 14:47:04 +0100
Subject: [PATCH v5] Allow building without default socket directory

We have code paths for Unix socket support and no Unix socket support.
Now add a third variant: Unix socket support but do not use a Unix
socket by default in the client or the server, only if you explicitly
specify one.  This will be useful when we enable Unix socket support
on Windows.

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect.  This mostly already worked like that;
only a few places needed to be adjusted.  Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.
---
 src/include/libpq/pqcomm.h        |  6 +++---
 src/include/pg_config_manual.h    |  5 +++++
 src/interfaces/libpq/fe-connect.c | 15 ++++++++++-----
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h
index baf6a4b6c0..a24bb796f4 100644
--- a/src/include/libpq/pqcomm.h
+++ b/src/include/libpq/pqcomm.h
@@ -68,10 +68,10 @@ typedef struct
 /* Configure the UNIX socket location for the well known port. */
 
 #define UNIXSOCK_PATH(path, port, sockdir) \
+	   (AssertMacro(sockdir), \
+		AssertMacro(*(sockdir) != '\0'), \
 		snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
-				((sockdir) && *(sockdir) != '\0') ? (sockdir) : \
-				DEFAULT_PGSOCKET_DIR, \
-				(port))
+				 (sockdir), (port)))
 
 /*
  * The maximum workable length of a socket path is what will fit into
diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index 61b667d166..3b7cf8d0b5 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -191,6 +191,11 @@
  * directory.  But if you just hate the idea of sockets in /tmp,
  * here's where to twiddle it.  You can also override this at runtime
  * with the postmaster's -k switch.
+ *
+ * If set to an empty string, then AF_UNIX sockets are not used by default.  A
+ * server will not create an AF_UNIX socket unless the run-time configuration
+ * is changed; a client will connect via TCP/IP by default and will only use
+ * an AF_UNIX socket if one is explicitly specified.
  */
 #define DEFAULT_PGSOCKET_DIR  "/tmp"
 
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 3ca7e0560c..198ecbc040 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1079,12 +1079,17 @@ connectOptions2(PGconn *conn)
 			if (ch->host)
 				free(ch->host);
 #ifdef HAVE_UNIX_SOCKETS
-			ch->host = strdup(DEFAULT_PGSOCKET_DIR);
-			ch->type = CHT_UNIX_SOCKET;
-#else
-			ch->host = strdup(DefaultHost);
-			ch->type = CHT_HOST_NAME;
+			if (DEFAULT_PGSOCKET_DIR[0])
+			{
+				ch->host = strdup(DEFAULT_PGSOCKET_DIR);
+				ch->type = CHT_UNIX_SOCKET;
+			}
+			else
 #endif
+			{
+				ch->host = strdup(DefaultHost);
+				ch->type = CHT_HOST_NAME;
+			}
 			if (ch->host == NULL)
 				goto oom_error;
 		}
-- 
2.24.1

#14Hamlin, Garick L
ghamlin@isc.upenn.edu
In reply to: Peter Eisentraut (#13)
Re: Unix-domain socket support on Windows

On Wed, Dec 18, 2019 at 02:52:15PM +0100, Peter Eisentraut wrote:

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect. This mostly already worked like that;
only a few places needed to be adjusted. Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.

Would it make sense to support abstract sockets in PostgreSQL?

I know it's bit unrelated. I haven't read all the code here I just was
thinking about it because of the code checking the leading \0 byte of the dir.

Garick

#15Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Hamlin, Garick L (#14)
Re: Unix-domain socket support on Windows

On 2019-12-18 15:24, Hamlin, Garick L wrote:

On Wed, Dec 18, 2019 at 02:52:15PM +0100, Peter Eisentraut wrote:

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect. This mostly already worked like that;
only a few places needed to be adjusted. Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.

Would it make sense to support abstract sockets in PostgreSQL?

Maybe, I'm not sure.

I know it's bit unrelated. I haven't read all the code here I just was
thinking about it because of the code checking the leading \0 byte of the dir.

We would probably represent abstract sockets with a leading '@' in the
user-facing components and only translate it to the internal format at
the last moment, probably in that very same UNIXSOCK_PATH() function.
So I think that wouldn't be a problem.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#16Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#13)
Re: Unix-domain socket support on Windows

On 2019-12-18 14:52, Peter Eisentraut wrote:

Next patch: This allows building *with* Unix-domain socket support but
*without* a default Unix socket path. This is needed because on Windows
we don't have a good default location like "/tmp" and we probably don't
want Unix sockets by default at run time so that older Windows versions
continue to work out of the box with the same binaries.

We have code paths for Unix socket support and no Unix socket support.
Now add a third variant: Unix socket support but do not use a Unix
socket by default in the client or the server, only if you explicitly
specify one.

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect. This mostly already worked like that;
only a few places needed to be adjusted. Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.

Perhaps this patch is too boring to be reviewed. If there are no
objections, I'll commit it soon and then submit the final patches with
the real functionality for the next commit fest.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#17Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#16)
Re: Unix-domain socket support on Windows

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

On 2019-12-18 14:52, Peter Eisentraut wrote:

We have code paths for Unix socket support and no Unix socket support.
Now add a third variant: Unix socket support but do not use a Unix
socket by default in the client or the server, only if you explicitly
specify one.

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect. This mostly already worked like that;
only a few places needed to be adjusted. Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.

Perhaps this patch is too boring to be reviewed. If there are no
objections, I'll commit it soon and then submit the final patches with
the real functionality for the next commit fest.

Sorry, I'd paid no particular attention to this thread because
I figured it'd take a Windows-competent person to review. But
the patch as it stands isn't that.

The code looks fine (and a big +1 for not having knowledge of
DEFAULT_PGSOCKET_DIR wired into UNIXSOCK_PATH). I wonder though
whether any user-facing documentation needs to be adjusted.

regards, tom lane

#18Craig Ringer
craig@2ndquadrant.com
In reply to: Tom Lane (#17)
Re: Unix-domain socket support on Windows

On Fri, 31 Jan 2020 at 02:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

On 2019-12-18 14:52, Peter Eisentraut wrote:

We have code paths for Unix socket support and no Unix socket support.
Now add a third variant: Unix socket support but do not use a Unix
socket by default in the client or the server, only if you explicitly
specify one.

To implement this, tweak things so that setting DEFAULT_PGSOCKET_DIR
to "" has the desired effect. This mostly already worked like that;
only a few places needed to be adjusted. Notably, the reference to
DEFAULT_PGSOCKET_DIR in UNIXSOCK_PATH() could be removed because all
callers already resolve an empty socket directory setting with a
default if appropriate.

Perhaps this patch is too boring to be reviewed. If there are no
objections, I'll commit it soon and then submit the final patches with
the real functionality for the next commit fest.

Sorry, I'd paid no particular attention to this thread because
I figured it'd take a Windows-competent person to review. But
the patch as it stands isn't that.

The code looks fine (and a big +1 for not having knowledge of
DEFAULT_PGSOCKET_DIR wired into UNIXSOCK_PATH). I wonder though
whether any user-facing documentation needs to be adjusted.

Probably, since it won't work with 'peer' auth from what was said upthread.

--
Craig Ringer http://www.2ndQuadrant.com/
2ndQuadrant - PostgreSQL Solutions for the Enterprise

#19Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Tom Lane (#17)
Re: Unix-domain socket support on Windows

On 2020-01-30 19:41, Tom Lane wrote:

The code looks fine (and a big +1 for not having knowledge of
DEFAULT_PGSOCKET_DIR wired into UNIXSOCK_PATH). I wonder though
whether any user-facing documentation needs to be adjusted.

There are no user-facing changes in this patch yet. That will come with
subsequent patches.

This patch has now been committed.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#20Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#13)
5 attachment(s)
Re: Unix-domain socket support on Windows

Here is another patch set to enable this functionality.

0001 enables Unix-domain sockets on Windows, but leaves them turned off
by default at run time, using the mechanism introduced by a9cff89f7e.
This is relatively straightforward, except perhaps some aesthetic
questions about how these different configuration bits are distributed
around the various files.

0002 deals with pg_upgrade. It preserves the existing behavior of not
using Unix-domain sockets on Windows. This could perhaps be enhanced
later by either adding a command-line option or a run-time test. It's
too complicated right now.

0003 deals with how initdb should initialize postgresql.conf and
pg_hba.conf. It introduces a run-time test similar to how we detect
presence of IPv6. After I wrote this patch, I have come to think that
this is overkill and we should just always leave the "local" line in
pg_hba.conf even if there is no run-time support in the OS. (I think
the reason we do the run-time test for IPv6 is that we need it to parse
the IPv6 addresses in pg_hba.conf, but there is no analogous requirement
for Unix-domain sockets.) This patch is optional in any case.

0004 fixes a bug in the pg_upgrade test.sh script that was exposed by
these changes.

0005 fixes up some issues in the test suites. Right now, the TAP tests
are hardcoded to not use Unix-domain sockets on Windows, where as
pg_regress keys off HAVE_UNIX_SOCKETS, which is no longer a useful
distinguisher. The change is to not use Unix-domain sockets for all the
tests by default on Windows (the previous behavior) but give an option
to use them. At the moment, I would consider running the test suites
with Unix-domain sockets enabled as experimental, but that's only
because of various issues in the test setups. For instance, there is an
issue in the comment of pg_regress.c remove_temp() that I'm not sure how
to address. Also, the TAP tests don't seem to work because of some path
issues. I figured I'd call time on fiddling with this for now and ship
the patches.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v6-0001-Enable-Unix-domain-sockets-support-on-Windows.patchtext/plain; charset=UTF-8; name=v6-0001-Enable-Unix-domain-sockets-support-on-Windows.patch; x-mac-creator=0; x-mac-type=0Download
From 9ca09c6f6d59182cdc0c2a28912490471626038a Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v6 1/5] 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.

Also set DEFAULT_PGSOCKET_DIR to empty on Windows so by default no
Unix-domain socket is used, because there is no good standard
location.
---
 config/c-library.m4            |  5 +++--
 configure                      |  5 ++++-
 src/include/c.h                |  4 ++++
 src/include/pg_config.h.in     |  6 +++---
 src/include/pg_config_manual.h | 15 ++++++++-------
 src/include/port/win32.h       | 11 +++++++++++
 src/tools/msvc/Solution.pm     |  2 +-
 7 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/config/c-library.m4 b/config/c-library.m4
index d9a31d7664..163ad5742d 100644
--- a/config/c-library.m4
+++ b/config/c-library.m4
@@ -102,10 +102,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 37aa82dcd4..8b770cb67f 100755
--- a/configure
+++ b/configure
@@ -14061,7 +14061,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 d10b9812fb..9562569d2a 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1077,6 +1077,10 @@ extern void ExceptionalCondition(const char *conditionName,
  * ----------------------------------------------------------------
  */
 
+#ifdef HAVE_STRUCT_SOCKADDR_UN
+#define HAVE_UNIX_SOCKETS 1
+#endif
+
 /*
  * Invert the sign of a qsort-style comparison result, ie, exchange negative
  * and positive integer values, being careful not to get the wrong answer
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 60dcf42974..8449e02123 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -605,6 +605,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
 
@@ -689,9 +692,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_manual.h b/src/include/pg_config_manual.h
index b4ce53300b..4ee0efaed8 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -122,13 +122,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()
  */
@@ -196,8 +189,16 @@
  * server will not create an AF_UNIX socket unless the run-time configuration
  * is changed, a client will connect via TCP/IP by default and will only use
  * an AF_UNIX socket if one is explicitly specified.
+ *
+ * This is done by default on Windows because there is no good standard
+ * location for AF_UNIX sockets and many installations on Windows don't
+ * support them yet.
  */
+#ifndef WIN32
 #define DEFAULT_PGSOCKET_DIR  "/tmp"
+#else
+#define DEFAULT_PGSOCKET_DIR ""
+#endif
 
 /*
  * This is the default event source for Windows event log.
diff --git a/src/include/port/win32.h b/src/include/port/win32.h
index bb2f7540b3..c280c131c0 100644
--- a/src/include/port/win32.h
+++ b/src/include/port/win32.h
@@ -56,3 +56,14 @@
 #else
 #define PGDLLEXPORT
 #endif
+
+/*
+ * Windows headers don't define this structure, but you can define it yourself
+ * to use the functionality.
+ */
+struct sockaddr_un
+{
+	unsigned short sun_family;
+	char sun_path[108];
+};
+#define HAVE_STRUCT_SOCKADDR_UN 1
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 8412ef298e..f90cdfe887 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -371,6 +371,7 @@ sub GenerateFiles
 		HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN      => undef,
 		HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY => undef,
 		HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN    => undef,
+		HAVE_STRUCT_SOCKADDR_UN                  => undef,
 		HAVE_STRUCT_TM_TM_ZONE                   => undef,
 		HAVE_SYNC_FILE_RANGE                     => undef,
 		HAVE_SYMLINK                             => 1,
@@ -399,7 +400,6 @@ sub GenerateFiles
 		HAVE_UINTPTR_T                           => undef,
 		HAVE_UNION_SEMUN                         => undef,
 		HAVE_UNISTD_H                            => 1,
-		HAVE_UNIX_SOCKETS                        => undef,
 		HAVE_UNSETENV                            => undef,
 		HAVE_USELOCALE                           => undef,
 		HAVE_UTIME                               => 1,

base-commit: 997563dfcb2501a7a199589cd6f15f2bb8af3d04
-- 
2.25.0

v6-0002-pg_upgrade-Disable-Unix-domain-socket-support-on-.patchtext/plain; charset=UTF-8; name=v6-0002-pg_upgrade-Disable-Unix-domain-socket-support-on-.patch; x-mac-creator=0; x-mac-type=0Download
From 75f193e8aa6e5693517078516239503e17c515a2 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 11 Feb 2020 23:53:56 +0100
Subject: [PATCH v6 2/5] pg_upgrade: Disable Unix-domain socket support on
 Windows

This preserves the existing behavior of not using Unix-domain sockets
on Windows in pg_upgrade.  Adding support would be desirable, but it
needs further work, in particular a way to select whether to use
Unix-domain sockets from the command-line or with a run-time test.
---
 src/bin/pg_upgrade/option.c | 4 ++--
 src/bin/pg_upgrade/server.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c
index eed70fff4f..7b957ee365 100644
--- a/src/bin/pg_upgrade/option.c
+++ b/src/bin/pg_upgrade/option.c
@@ -467,7 +467,7 @@ adjust_data_dir(ClusterInfo *cluster)
 void
 get_sock_dir(ClusterInfo *cluster, bool live_check)
 {
-#ifdef HAVE_UNIX_SOCKETS
+#if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
 
 	/*
 	 * sockdir and port were added to postmaster.pid in PG 9.1. Pre-9.1 cannot
@@ -529,7 +529,7 @@ get_sock_dir(ClusterInfo *cluster, bool live_check)
 		 * default
 		 */
 		cluster->sockdir = NULL;
-#else							/* !HAVE_UNIX_SOCKETS */
+#else							/* !HAVE_UNIX_SOCKETS || WIN32 */
 	cluster->sockdir = NULL;
 #endif
 }
diff --git a/src/bin/pg_upgrade/server.c b/src/bin/pg_upgrade/server.c
index e244256501..f8d57663e5 100644
--- a/src/bin/pg_upgrade/server.c
+++ b/src/bin/pg_upgrade/server.c
@@ -210,7 +210,7 @@ start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
 
 	socket_string[0] = '\0';
 
-#ifdef HAVE_UNIX_SOCKETS
+#if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
 	/* prevent TCP/IP connections, restrict socket access */
 	strcat(socket_string,
 		   " -c listen_addresses='' -c unix_socket_permissions=0700");
-- 
2.25.0

v6-0003-initdb-Detect-Unix-domain-socket-support-dynamica.patchtext/plain; charset=UTF-8; name=v6-0003-initdb-Detect-Unix-domain-socket-support-dynamica.patch; x-mac-creator=0; x-mac-type=0Download
From 5bbd177ff303308c2ed577ddc1c539c3027ae676 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 7 Aug 2019 15:44:19 +0200
Subject: [PATCH v6 3/5] 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 7f1534aebb..ad37748afe 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -231,9 +231,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);
@@ -431,36 +428,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
  */
@@ -1070,10 +1037,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);
@@ -1090,8 +1117,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
@@ -1254,63 +1284,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.25.0

v6-0004-Fix-pg_upgrade-test-for-Unix-domain-sockets-on-Wi.patchtext/plain; charset=UTF-8; name=v6-0004-Fix-pg_upgrade-test-for-Unix-domain-sockets-on-Wi.patch; x-mac-creator=0; x-mac-type=0Download
From a88d5a7c7a58161c6d414bd7600099f7d81f5b53 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 11 Feb 2020 19:53:27 +0100
Subject: [PATCH v6 4/5] Fix pg_upgrade test for Unix-domain sockets on Windows

The previous code passed "localhost" to postgres -k, which only
happened to work because Windows used to ignore the -k argument value
altogether.  We instead need to pass an empty string to get the
desired effect.
---
 src/bin/pg_upgrade/test.sh | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/bin/pg_upgrade/test.sh b/src/bin/pg_upgrade/test.sh
index 6430610d48..10a28d8133 100644
--- a/src/bin/pg_upgrade/test.sh
+++ b/src/bin/pg_upgrade/test.sh
@@ -39,14 +39,14 @@ testhost=`uname -s | sed 's/^MSYS/MINGW/'`
 case $testhost in
 	MINGW*)
 		LISTEN_ADDRESSES="localhost"
+		PG_REGRESS_SOCKET_DIR=""
 		PGHOST=localhost
 		;;
 	*)
 		LISTEN_ADDRESSES=""
 		# Select a socket directory.  The algorithm is from the "configure"
 		# script; the outcome mimics pg_regress.c:make_temp_sockdir().
-		PGHOST=$PG_REGRESS_SOCK_DIR
-		if [ "x$PGHOST" = x ]; then
+		if [ x"$PG_REGRESS_SOCKET_DIR" = x ]; then
 			set +e
 			dir=`(umask 077 &&
 				  mktemp -d /tmp/pg_upgrade_check-XXXXXX) 2>/dev/null`
@@ -59,14 +59,15 @@ case $testhost in
 				fi
 			fi
 			set -e
-			PGHOST=$dir
-			trap 'rm -rf "$PGHOST"' 0
+			PG_REGRESS_SOCKET_DIR=$dir
+			trap 'rm -rf "$PG_REGRESS_SOCKET_DIR"' 0
 			trap 'exit 3' 1 2 13 15
 		fi
+		PGHOST=$PG_REGRESS_SOCKET_DIR
 		;;
 esac
 
-POSTMASTER_OPTS="-F -c listen_addresses=\"$LISTEN_ADDRESSES\" -k \"$PGHOST\""
+POSTMASTER_OPTS="-F -c listen_addresses=\"$LISTEN_ADDRESSES\" -k \"$PG_REGRESS_SOCKET_DIR\""
 export PGHOST
 
 # don't rely on $PWD here, as old shells don't set it
-- 
2.25.0

v6-0005-Allow-using-Unix-domain-sockets-on-Windows-in-tes.patchtext/plain; charset=UTF-8; name=v6-0005-Allow-using-Unix-domain-sockets-on-Windows-in-tes.patch; x-mac-creator=0; x-mac-type=0Download
From eb71051830aa4019bb85acdb57372e23e54043ac Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 8 Aug 2019 11:19:04 +0200
Subject: [PATCH v6 5/5] Allow using Unix-domain sockets on Windows in tests

As before, 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.

Previously, the TAP tests were hardcoded to not use Unix-domain
sockets on Windows, where as pg_regress would key off
HAVE_UNIX_SOCKETS, which is no longer a useful distinguisher.

For pg_regress, allow overriding using /tmp for the temporary socket
location by using the TMPDIR environment variable if present, because
/tmp might not exist on Windows.
---
 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             | 21 +++++++++++++++------
 6 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/src/bin/pg_ctl/t/001_start_stop.pl b/src/bin/pg_ctl/t/001_start_stop.pl
index 6a1619e171..b1e419f02e 100644
--- a/src/bin/pg_ctl/t/001_start_stop.pl
+++ b/src/bin/pg_ctl/t/001_start_stop.pl
@@ -29,7 +29,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 5985130e3d..b8d6cc52e9 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 9575268bd7..1d5450758e 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 65ee0425b0..0e6c4819e4 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
 {
@@ -117,6 +118,11 @@ BEGIN
 		require Win32API::File;
 		Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
 	}
+
+	# 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 || defined $ENV{PG_TEST_USE_UNIX_SOCKETS});
 }
 
 =pod
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 92bd28dc5a..b58bd7e49c 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -101,11 +101,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;
 
@@ -293,7 +292,7 @@ stop_postmaster(void)
  * remove the directory.  Ignore errors; leaking a temporary directory is
  * unimportant.  This can run from a signal handler.  The code is not
  * acceptable in a Windows signal handler (see initdb.c:trapsig()), but
- * Windows is not a HAVE_UNIX_SOCKETS platform.
+ * on Windows, pg_regress does not use Unix sockets by default.
  */
 static void
 remove_temp(void)
@@ -331,7 +330,7 @@ signal_remove_temp(int signum)
 static const char *
 make_temp_sockdir(void)
 {
-	char	   *template = pg_strdup("/tmp/pg_regress-XXXXXX");
+	char	   *template = psprintf("%s/pg_regress-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
 
 	temp_sockdir = mkdtemp(template);
 	if (temp_sockdir == NULL)
@@ -993,6 +992,9 @@ config_sspi_auth(const char *pgdata, const char *superuser_name)
 			   *ident;
 	_stringlist *sl;
 
+	if (use_unix_sockets)
+		return;
+
 	/* Find out the name of the current OS user */
 	current_windows_user(&accountname, &domainname);
 
@@ -2134,10 +2136,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.25.0

#21Andrew Dunstan
andrew.dunstan@2ndquadrant.com
In reply to: Peter Eisentraut (#20)
Re: Unix-domain socket support on Windows

On 2/12/20 3:32 AM, Peter Eisentraut wrote:

Here is another patch set to enable this functionality.

0001 enables Unix-domain sockets on Windows, but leaves them turned
off by default at run time, using the mechanism introduced by
a9cff89f7e. This is relatively straightforward, except perhaps some
aesthetic questions about how these different configuration bits are
distributed around the various files.

0002 deals with pg_upgrade.  It preserves the existing behavior of not
using Unix-domain sockets on Windows.  This could perhaps be enhanced
later by either adding a command-line option or a run-time test.  It's
too complicated right now.

0003 deals with how initdb should initialize postgresql.conf and
pg_hba.conf.  It introduces a run-time test similar to how we detect
presence of IPv6.  After I wrote this patch, I have come to think that
this is overkill and we should just always leave the "local" line in
pg_hba.conf even if there is no run-time support in the OS.  (I think
the reason we do the run-time test for IPv6 is that we need it to
parse the IPv6 addresses in pg_hba.conf, but there is no analogous
requirement for Unix-domain sockets.)  This patch is optional in any
case.

0004 fixes a bug in the pg_upgrade test.sh script that was exposed by
these changes.

0005 fixes up some issues in the test suites.  Right now, the TAP
tests are hardcoded to not use Unix-domain sockets on Windows, where
as pg_regress keys off HAVE_UNIX_SOCKETS, which is no longer a useful
distinguisher.  The change is to not use Unix-domain sockets for all
the tests by default on Windows (the previous behavior) but give an
option to use them.  At the moment, I would consider running the test
suites with Unix-domain sockets enabled as experimental, but that's
only because of various issues in the test setups.  For instance,
there is an issue in the comment of pg_regress.c remove_temp() that
I'm not sure how to address.  Also, the TAP tests don't seem to work
because of some path issues.  I figured I'd call time on fiddling with
this for now and ship the patches.

I have tested this on drongo/fairywren and it works fine. The patches
apply cleanly (with a bit of fuzz) and a full buildfarm run is happy in
both cases.

Unfortunately I don't have a Windows machine that's young enough to
support git master and old enough not to support Unix Domain sockets, so
i can't test that with socket-enabled binaries.

On inspection the patches seem fine.

Let's commit this and keep working on the pg_upgrade and test issues.

cheers

andrew

--
Andrew Dunstan https://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#22Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Andrew Dunstan (#21)
Re: Unix-domain socket support on Windows

On 2020-03-27 18:52, Andrew Dunstan wrote:

I have tested this on drongo/fairywren and it works fine. The patches
apply cleanly (with a bit of fuzz) and a full buildfarm run is happy in
both cases.

Unfortunately I don't have a Windows machine that's young enough to
support git master and old enough not to support Unix Domain sockets, so
i can't test that with socket-enabled binaries.

On inspection the patches seem fine.

Let's commit this and keep working on the pg_upgrade and test issues.

I have committed this in chunks over the last couple of days. It's done
now.

I didn't commit the initdb auto-detection patch. As I mentioned
earlier, that one is probably not necessary.

Btw., the default AppVeyor images are too old to support this. You need
something like 'image: Visual Studio 2019' to get a new enough image.
So that's one way to test what happens when it's not supported at run
time. (I did test it and you get a sensible error message.)

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services