Skip to content

Commit 8f3ec75

Browse files
committed
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. 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
1 parent 87779aa commit 8f3ec75

File tree

11 files changed

+50
-25
lines changed

11 files changed

+50
-25
lines changed

config/c-library.m4

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ AC_DEFUN([PGAC_UNION_SEMUN],
102102

103103
# PGAC_STRUCT_SOCKADDR_UN
104104
# -----------------------
105-
# If `struct sockaddr_un' exists, define HAVE_UNIX_SOCKETS.
105+
# If `struct sockaddr_un' exists, define HAVE_STRUCT_SOCKADDR_UN.
106+
# If it is missing then one could define it.
106107
# (Requires test for <sys/un.h>!)
107108
AC_DEFUN([PGAC_STRUCT_SOCKADDR_UN],
108-
[AC_CHECK_TYPE([struct sockaddr_un], [AC_DEFINE(HAVE_UNIX_SOCKETS, 1, [Define to 1 if you have unix sockets.])], [],
109+
[AC_CHECK_TYPES([struct sockaddr_un], [], [],
109110
[#include <sys/types.h>
110111
#ifdef HAVE_SYS_UN_H
111112
#include <sys/un.h>

configure

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14096,7 +14096,10 @@ ac_fn_c_check_type "$LINENO" "struct sockaddr_un" "ac_cv_type_struct_sockaddr_un
1409614096
"
1409714097
if test "x$ac_cv_type_struct_sockaddr_un" = xyes; then :
1409814098

14099-
$as_echo "#define HAVE_UNIX_SOCKETS 1" >>confdefs.h
14099+
cat >>confdefs.h <<_ACEOF
14100+
#define HAVE_STRUCT_SOCKADDR_UN 1
14101+
_ACEOF
14102+
1410014103

1410114104
fi
1410214105

src/bin/pg_upgrade/option.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ adjust_data_dir(ClusterInfo *cluster)
468468
void
469469
get_sock_dir(ClusterInfo *cluster, bool live_check)
470470
{
471-
#ifdef HAVE_UNIX_SOCKETS
471+
#if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
472472

473473
/*
474474
* 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)
530530
* default
531531
*/
532532
cluster->sockdir = NULL;
533-
#else /* !HAVE_UNIX_SOCKETS */
533+
#else /* !HAVE_UNIX_SOCKETS || WIN32 */
534534
cluster->sockdir = NULL;
535535
#endif
536536
}

src/bin/pg_upgrade/server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
210210

211211
socket_string[0] = '\0';
212212

213-
#ifdef HAVE_UNIX_SOCKETS
213+
#if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
214214
/* prevent TCP/IP connections, restrict socket access */
215215
strcat(socket_string,
216216
" -c listen_addresses='' -c unix_socket_permissions=0700");

src/bin/pg_upgrade/test.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ testhost=`uname -s | sed 's/^MSYS/MINGW/'`
3939
case $testhost in
4040
MINGW*)
4141
LISTEN_ADDRESSES="localhost"
42+
PG_REGRESS_SOCKET_DIR=""
4243
PGHOST=localhost
4344
;;
4445
*)
4546
LISTEN_ADDRESSES=""
4647
# Select a socket directory. The algorithm is from the "configure"
4748
# script; the outcome mimics pg_regress.c:make_temp_sockdir().
48-
PGHOST=$PG_REGRESS_SOCK_DIR
49-
if [ "x$PGHOST" = x ]; then
49+
if [ x"$PG_REGRESS_SOCKET_DIR" = x ]; then
5050
set +e
5151
dir=`(umask 077 &&
5252
mktemp -d /tmp/pg_upgrade_check-XXXXXX) 2>/dev/null`
@@ -59,14 +59,15 @@ case $testhost in
5959
fi
6060
fi
6161
set -e
62-
PGHOST=$dir
63-
trap 'rm -rf "$PGHOST"' 0
62+
PG_REGRESS_SOCKET_DIR=$dir
63+
trap 'rm -rf "$PG_REGRESS_SOCKET_DIR"' 0
6464
trap 'exit 3' 1 2 13 15
6565
fi
66+
PGHOST=$PG_REGRESS_SOCKET_DIR
6667
;;
6768
esac
6869

69-
POSTMASTER_OPTS="-F -c listen_addresses=\"$LISTEN_ADDRESSES\" -k \"$PGHOST\""
70+
POSTMASTER_OPTS="-F -c listen_addresses=\"$LISTEN_ADDRESSES\" -k \"$PG_REGRESS_SOCKET_DIR\""
7071
export PGHOST
7172

7273
# don't rely on $PWD here, as old shells don't set it

src/include/c.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,10 @@ extern void ExceptionalCondition(const char *conditionName,
10761076
* ----------------------------------------------------------------
10771077
*/
10781078

1079+
#ifdef HAVE_STRUCT_SOCKADDR_UN
1080+
#define HAVE_UNIX_SOCKETS 1
1081+
#endif
1082+
10791083
/*
10801084
* Invert the sign of a qsort-style comparison result, ie, exchange negative
10811085
* and positive integer values, being careful not to get the wrong answer

src/include/pg_config.h.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@
566566
/* Define to 1 if `__ss_len' is a member of `struct sockaddr_storage'. */
567567
#undef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN
568568

569+
/* Define to 1 if the system has the type `struct sockaddr_un'. */
570+
#undef HAVE_STRUCT_SOCKADDR_UN
571+
569572
/* Define to 1 if `tm_zone' is a member of `struct tm'. */
570573
#undef HAVE_STRUCT_TM_TM_ZONE
571574

@@ -647,9 +650,6 @@
647650
/* Define to 1 if you have the <unistd.h> header file. */
648651
#undef HAVE_UNISTD_H
649652

650-
/* Define to 1 if you have unix sockets. */
651-
#undef HAVE_UNIX_SOCKETS
652-
653653
/* Define to 1 if you have the `unsetenv' function. */
654654
#undef HAVE_UNSETENV
655655

src/include/pg_config_manual.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,6 @@
135135
#define EXEC_BACKEND
136136
#endif
137137

138-
/*
139-
* Disable UNIX sockets for certain operating systems.
140-
*/
141-
#if defined(WIN32)
142-
#undef HAVE_UNIX_SOCKETS
143-
#endif
144-
145138
/*
146139
* USE_POSIX_FADVISE controls whether Postgres will attempt to use the
147140
* posix_fadvise() kernel call. Usually the automatic configure tests are
@@ -202,8 +195,16 @@
202195
* server will not create an AF_UNIX socket unless the run-time configuration
203196
* is changed, a client will connect via TCP/IP by default and will only use
204197
* an AF_UNIX socket if one is explicitly specified.
198+
*
199+
* This is done by default on Windows because there is no good standard
200+
* location for AF_UNIX sockets and many installations on Windows don't
201+
* support them yet.
205202
*/
203+
#ifndef WIN32
206204
#define DEFAULT_PGSOCKET_DIR "/tmp"
205+
#else
206+
#define DEFAULT_PGSOCKET_DIR ""
207+
#endif
207208

208209
/*
209210
* This is the default event source for Windows event log.

src/include/port/win32.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@
5656
#else
5757
#define PGDLLEXPORT
5858
#endif
59+
60+
/*
61+
* Windows headers don't define this structure, but you can define it yourself
62+
* to use the functionality.
63+
*/
64+
struct sockaddr_un
65+
{
66+
unsigned short sun_family;
67+
char sun_path[108];
68+
};
69+
#define HAVE_STRUCT_SOCKADDR_UN 1

src/test/regress/pg_regress.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ stop_postmaster(void)
292292
* remove the directory. Ignore errors; leaking a temporary directory is
293293
* unimportant. This can run from a signal handler. The code is not
294294
* acceptable in a Windows signal handler (see initdb.c:trapsig()), but
295-
* Windows is not a HAVE_UNIX_SOCKETS platform.
295+
* on Windows, pg_regress does not use Unix sockets.
296296
*/
297297
static void
298298
remove_temp(void)
@@ -2120,8 +2120,12 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
21202120

21212121
atexit(stop_postmaster);
21222122

2123-
#ifndef HAVE_UNIX_SOCKETS
2124-
/* no unix domain sockets available, so change default */
2123+
#if !defined(HAVE_UNIX_SOCKETS) || defined(WIN32)
2124+
/*
2125+
* No Unix-domain sockets available, so change default. For now, we also
2126+
* don't use them on Windows, even if the build supports them. (See
2127+
* comment at remove_temp() for a reason.)
2128+
*/
21252129
hostname = "localhost";
21262130
#endif
21272131

src/tools/msvc/Solution.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ sub GenerateFiles
370370
HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN => undef,
371371
HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY => undef,
372372
HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN => undef,
373+
HAVE_STRUCT_SOCKADDR_UN => undef,
373374
HAVE_STRUCT_TM_TM_ZONE => undef,
374375
HAVE_SYNC_FILE_RANGE => undef,
375376
HAVE_SYMLINK => 1,
@@ -397,7 +398,6 @@ sub GenerateFiles
397398
HAVE_UINT8 => undef,
398399
HAVE_UNION_SEMUN => undef,
399400
HAVE_UNISTD_H => 1,
400-
HAVE_UNIX_SOCKETS => undef,
401401
HAVE_UNSETENV => undef,
402402
HAVE_USELOCALE => undef,
403403
HAVE_UUID_BSD => undef,

0 commit comments

Comments
 (0)