Enable Unix-domain sockets support on Windows
authorPeter Eisentraut <peter@eisentraut.org>
Sat, 28 Mar 2020 12:01:42 +0000 (13:01 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Sat, 28 Mar 2020 14:01:01 +0000 (15:01 +0100)
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.

Set DEFAULT_PGSOCKET_DIR to empty on Windows so by default no
Unix-domain socket is used, because there is no good standard
location.

In pg_upgrade, we have to do some extra tweaking to preserve the
existing behavior of not using Unix-domain sockets on Windows.  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.

The pg_upgrade test script needs a fix.  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.

The test suites will continue to not use Unix-domain sockets on
Windows.  This requires a small tweak in pg_regress.c.  The TAP tests
don't need to be changed because they decide by the operating system
rather than HAVE_UNIX_SOCKETS.

Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/54bde68c-d134-4eb8-5bd3-8af33b72a010@2ndquadrant.com

config/c-library.m4
configure
src/bin/pg_upgrade/option.c
src/bin/pg_upgrade/server.c
src/bin/pg_upgrade/test.sh
src/include/c.h
src/include/pg_config.h.in
src/include/pg_config_manual.h
src/include/port/win32.h
src/test/regress/pg_regress.c
src/tools/msvc/Solution.pm

index d9a31d7664ae29577e57b40d0673527456e19904..163ad5742d803d43bb3b2bfe4a8830dc6278e7bf 100644 (file)
@@ -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>
index 899116517cf46d5eeac65911b590a89aff1bd96e..83abe872aa693cbafa81ca1513ab2a247e5ccbcf 100755 (executable)
--- a/configure
+++ b/configure
@@ -14096,7 +14096,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
 
index 4ef2036ecd670c4d826aed596b517c89e2598ada..aca1ee8b48d810223baed2e3b512462a97489993 100644 (file)
@@ -468,7 +468,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
@@ -530,7 +530,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
 }
index f669bb4e8a9e199741db7be5bdbf45078ae71314..7d17280ecb4386b0fc5aad4c12688c0f287fb02c 100644 (file)
@@ -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");
index 6430610d488d3f3b98183380252a64cad5f3aefe..10a28d8133c7904a27729601df2a4b5acd60a2c9 100644 (file)
@@ -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
index 831c89f473dd55e3ca35f7acbc993926c851a192..d72b23afe4ce6cb287396dd71838ee533d66bb5f 100644 (file)
@@ -1076,6 +1076,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
index 41ad20938064ab1aaaba15aacd9abc74fb133e86..c199cd46d2da3da235577a6a966e7f3a42fb6f49 100644 (file)
 /* 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
 
 /* 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
 
index b7410ff51e5f049d97a404a1ea82e90af77ba143..32f739706d5e04e87ee1e4dbf5687d3d2f9ff591 100644 (file)
 #define EXEC_BACKEND
 #endif
 
-/*
- * Disable UNIX sockets for certain operating systems.
- */
-#if defined(WIN32)
-#undef HAVE_UNIX_SOCKETS
-#endif
-
 /*
  * USE_POSIX_FADVISE controls whether Postgres will attempt to use the
  * posix_fadvise() kernel call.  Usually the automatic configure tests are
  * 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.
index bb2f7540b3d9b3878d91bd3968e55c8df7e68f92..c280c131c037ccec12cfa32774334502db04dd7f 100644 (file)
 #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
index f6a5e1b9c7661e0e033524d89d6c2f2c5bc27674..6bc19b9668dcc4dd53650a1c8c0d49c1b65dd1bd 100644 (file)
@@ -292,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.
  */
 static void
 remove_temp(void)
@@ -2120,8 +2120,12 @@ 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 */
+#if !defined(HAVE_UNIX_SOCKETS) || defined(WIN32)
+       /*
+        * No Unix-domain sockets available, so change default.  For now, we also
+        * don't use them on Windows, even if the build supports them.  (See
+        * comment at remove_temp() for a reason.)
+        */
        hostname = "localhost";
 #endif
 
index 34d1f61dbaabbca1e4fa5262f5703f63f0908ec4..545bdcef7be49c7d23e48051dbde361dce70d3f6 100644 (file)
@@ -370,6 +370,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,
@@ -397,7 +398,6 @@ sub GenerateFiles
                HAVE_UINT8                               => undef,
                HAVE_UNION_SEMUN                         => undef,
                HAVE_UNISTD_H                            => 1,
-               HAVE_UNIX_SOCKETS                        => undef,
                HAVE_UNSETENV                            => undef,
                HAVE_USELOCALE                           => undef,
                HAVE_UUID_BSD                            => undef,