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
From 9ca09c6f6d59182cdc0c2a28912490471626038a Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@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
From 75f193e8aa6e5693517078516239503e17c515a2 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@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
From 5bbd177ff303308c2ed577ddc1c539c3027ae676 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@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
From a88d5a7c7a58161c6d414bd7600099f7d81f5b53 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@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
From eb71051830aa4019bb85acdb57372e23e54043ac Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@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