From 87c1b8dff6dea44f6ef920b0b02386e5e2b68be4 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 11 Aug 2022 15:30:38 +1200
Subject: [PATCH 1/4] Remove HAVE_UNIX_SOCKETS.

Since HAVE_UNIX_SOCKETS is now defined unconditionally, remove the macro
and drop a small amount of dead code.

The last known systems not to have them (as far as I know at least) were
QNX, which we de-supported years ago, and Windows, which now has them.

If a hypothetical new OS shows up with the POSIX sockets API but without
working AF_UNIX, it'll presumably still be able to compile the code, and
fail at runtime with unsupported address family.  We might want to
consider adding a HINT that you should turn it off if your OS doesn't
support it at that point, but it doesn't seem worth making the relevant
code compile-time conditional.

Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CA%2BhUKG%2BL_3brvh%3D8e0BW_VfX9h7MtwgN%3DnFHP5o7X2oZucY9dg%40mail.gmail.com
---
 src/backend/libpq/hba.c             | 10 -------
 src/backend/libpq/pqcomm.c          | 18 -------------
 src/backend/postmaster/postmaster.c |  2 --
 src/backend/utils/misc/guc.c        |  4 ---
 src/bin/initdb/initdb.c             | 41 -----------------------------
 src/bin/pg_upgrade/option.c         |  4 +--
 src/bin/pg_upgrade/server.c         |  2 +-
 src/common/ip.c                     | 11 --------
 src/include/port.h                  |  3 ---
 src/interfaces/libpq/fe-connect.c   | 12 ---------
 src/test/regress/pg_regress.c       | 19 ++-----------
 11 files changed, 5 insertions(+), 121 deletions(-)

diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c
index deee05c197..66f2156cde 100644
--- a/src/backend/libpq/hba.c
+++ b/src/backend/libpq/hba.c
@@ -973,17 +973,7 @@ parse_hba_line(TokenizedAuthLine *tok_line, int elevel)
 	token = linitial(tokens);
 	if (strcmp(token->string, "local") == 0)
 	{
-#ifdef HAVE_UNIX_SOCKETS
 		parsedline->conntype = ctLocal;
-#else
-		ereport(elevel,
-				(errcode(ERRCODE_CONFIG_FILE_ERROR),
-				 errmsg("local connections are not supported by this build"),
-				 errcontext("line %d of configuration file \"%s\"",
-							line_num, HbaFileName)));
-		*err_msg = "local connections are not supported by this build";
-		return NULL;
-#endif
 	}
 	else if (strcmp(token->string, "host") == 0 ||
 			 strcmp(token->string, "hostssl") == 0 ||
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 75392a8bb7..3ec4328613 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -149,10 +149,8 @@ static void socket_putmessage_noblock(char msgtype, const char *s, size_t len);
 static int	internal_putbytes(const char *s, size_t len);
 static int	internal_flush(void);
 
-#ifdef HAVE_UNIX_SOCKETS
 static int	Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath);
 static int	Setup_AF_UNIX(const char *sock_path);
-#endif							/* HAVE_UNIX_SOCKETS */
 
 static const PQcommMethods PqCommSocketMethods = {
 	socket_comm_reset,
@@ -334,10 +332,7 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 	struct addrinfo hint;
 	int			listen_index = 0;
 	int			added = 0;
-
-#ifdef HAVE_UNIX_SOCKETS
 	char		unixSocketPath[MAXPGPATH];
-#endif
 #if !defined(WIN32) || defined(IPV6_V6ONLY)
 	int			one = 1;
 #endif
@@ -348,7 +343,6 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 	hint.ai_flags = AI_PASSIVE;
 	hint.ai_socktype = SOCK_STREAM;
 
-#ifdef HAVE_UNIX_SOCKETS
 	if (family == AF_UNIX)
 	{
 		/*
@@ -369,7 +363,6 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 		service = unixSocketPath;
 	}
 	else
-#endif							/* HAVE_UNIX_SOCKETS */
 	{
 		snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
 		service = portNumberStr;
@@ -427,11 +420,9 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 				familyDesc = _("IPv6");
 				break;
 #endif
-#ifdef HAVE_UNIX_SOCKETS
 			case AF_UNIX:
 				familyDesc = _("Unix");
 				break;
-#endif
 			default:
 				snprintf(familyDescBuf, sizeof(familyDescBuf),
 						 _("unrecognized address family %d"),
@@ -441,11 +432,9 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 		}
 
 		/* set up text form of address for log messages */
-#ifdef HAVE_UNIX_SOCKETS
 		if (addr->ai_family == AF_UNIX)
 			addrDesc = unixSocketPath;
 		else
-#endif
 		{
 			pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
 							   addr->ai_addrlen,
@@ -540,7 +529,6 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 			continue;
 		}
 
-#ifdef HAVE_UNIX_SOCKETS
 		if (addr->ai_family == AF_UNIX)
 		{
 			if (Setup_AF_UNIX(service) != STATUS_OK)
@@ -549,7 +537,6 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 				break;
 			}
 		}
-#endif
 
 		/*
 		 * Select appropriate accept-queue length limit.  PG_SOMAXCONN is only
@@ -572,13 +559,11 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 			continue;
 		}
 
-#ifdef HAVE_UNIX_SOCKETS
 		if (addr->ai_family == AF_UNIX)
 			ereport(LOG,
 					(errmsg("listening on Unix socket \"%s\"",
 							addrDesc)));
 		else
-#endif
 			ereport(LOG,
 			/* translator: first %s is IPv4 or IPv6 */
 					(errmsg("listening on %s address \"%s\", port %d",
@@ -597,8 +582,6 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 }
 
 
-#ifdef HAVE_UNIX_SOCKETS
-
 /*
  * Lock_AF_UNIX -- configure unix socket file path
  */
@@ -699,7 +682,6 @@ Setup_AF_UNIX(const char *sock_path)
 	}
 	return STATUS_OK;
 }
-#endif							/* HAVE_UNIX_SOCKETS */
 
 
 /*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 227bd9891e..8a038d1b2a 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -1297,7 +1297,6 @@ PostmasterMain(int argc, char *argv[])
 	}
 #endif
 
-#ifdef HAVE_UNIX_SOCKETS
 	if (Unix_socket_directories)
 	{
 		char	   *rawstring;
@@ -1347,7 +1346,6 @@ PostmasterMain(int argc, char *argv[])
 		list_free_deep(elemlist);
 		pfree(rawstring);
 	}
-#endif
 
 	/*
 	 * check that we have some socket to listen on
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 5db5df6285..9fbbfb1be5 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -4452,11 +4452,7 @@ static struct config_string ConfigureNamesString[] =
 			GUC_LIST_INPUT | GUC_LIST_QUOTE | GUC_SUPERUSER_ONLY
 		},
 		&Unix_socket_directories,
-#ifdef HAVE_UNIX_SOCKETS
 		DEFAULT_PGSOCKET_DIR,
-#else
-		"",
-#endif
 		NULL, NULL, NULL
 	},
 
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 2c93ffe811..4feb5db3fa 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -242,9 +242,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);
@@ -418,36 +415,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
  */
@@ -1045,12 +1012,8 @@ setup_config(void)
 				 n_buffers * (BLCKSZ / 1024));
 	conflines = replace_token(conflines, "#shared_buffers = 128MB", repltok);
 
-#ifdef HAVE_UNIX_SOCKETS
 	snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'",
 			 DEFAULT_PGSOCKET_DIR);
-#else
-	snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''");
-#endif
 	conflines = replace_token(conflines, "#unix_socket_directories = '/tmp'",
 							  repltok);
 
@@ -1210,11 +1173,7 @@ 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
 
diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c
index 0954b75ec0..fbab1c4fb7 100644
--- a/src/bin/pg_upgrade/option.c
+++ b/src/bin/pg_upgrade/option.c
@@ -444,7 +444,7 @@ adjust_data_dir(ClusterInfo *cluster)
 void
 get_sock_dir(ClusterInfo *cluster, bool live_check)
 {
-#if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
+#if !defined(WIN32)
 	if (!live_check)
 		cluster->sockdir = user_opts.socketdir;
 	else
@@ -490,7 +490,7 @@ get_sock_dir(ClusterInfo *cluster, bool live_check)
 			pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu",
 				   orig_port, cluster->port);
 	}
-#else							/* !HAVE_UNIX_SOCKETS || WIN32 */
+#else							/* WIN32 */
 	cluster->sockdir = NULL;
 #endif
 }
diff --git a/src/bin/pg_upgrade/server.c b/src/bin/pg_upgrade/server.c
index 79479edc0e..9a1a20fb27 100644
--- a/src/bin/pg_upgrade/server.c
+++ b/src/bin/pg_upgrade/server.c
@@ -212,7 +212,7 @@ start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
 
 	socket_string[0] = '\0';
 
-#if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
+#if !defined(WIN32)
 	/* prevent TCP/IP connections, restrict socket access */
 	strcat(socket_string,
 		   " -c listen_addresses='' -c unix_socket_permissions=0700");
diff --git a/src/common/ip.c b/src/common/ip.c
index 267103efb9..dd9193feb1 100644
--- a/src/common/ip.c
+++ b/src/common/ip.c
@@ -38,7 +38,6 @@
 
 
 
-#ifdef	HAVE_UNIX_SOCKETS
 static int	getaddrinfo_unix(const char *path,
 							 const struct addrinfo *hintsp,
 							 struct addrinfo **result);
@@ -47,7 +46,6 @@ static int	getnameinfo_unix(const struct sockaddr_un *sa, int salen,
 							 char *node, int nodelen,
 							 char *service, int servicelen,
 							 int flags);
-#endif
 
 
 /*
@@ -62,10 +60,8 @@ pg_getaddrinfo_all(const char *hostname, const char *servname,
 	/* not all versions of getaddrinfo() zero *result on failure */
 	*result = NULL;
 
-#ifdef HAVE_UNIX_SOCKETS
 	if (hintp->ai_family == AF_UNIX)
 		return getaddrinfo_unix(servname, hintp, result);
-#endif
 
 	/* NULL has special meaning to getaddrinfo(). */
 	rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
@@ -87,7 +83,6 @@ pg_getaddrinfo_all(const char *hostname, const char *servname,
 void
 pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
 {
-#ifdef HAVE_UNIX_SOCKETS
 	if (hint_ai_family == AF_UNIX)
 	{
 		/* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */
@@ -101,7 +96,6 @@ pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
 		}
 	}
 	else
-#endif							/* HAVE_UNIX_SOCKETS */
 	{
 		/* struct was built by getaddrinfo() */
 		if (ai != NULL)
@@ -126,14 +120,12 @@ pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen,
 {
 	int			rc;
 
-#ifdef HAVE_UNIX_SOCKETS
 	if (addr && addr->ss_family == AF_UNIX)
 		rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen,
 							  node, nodelen,
 							  service, servicelen,
 							  flags);
 	else
-#endif
 		rc = getnameinfo((const struct sockaddr *) addr, salen,
 						 node, nodelen,
 						 service, servicelen,
@@ -151,8 +143,6 @@ pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen,
 }
 
 
-#if defined(HAVE_UNIX_SOCKETS)
-
 /* -------
  *	getaddrinfo_unix - get unix socket info using IPv6-compatible API
  *
@@ -286,4 +276,3 @@ getnameinfo_unix(const struct sockaddr_un *sa, int salen,
 
 	return 0;
 }
-#endif							/* HAVE_UNIX_SOCKETS */
diff --git a/src/include/port.h b/src/include/port.h
index 80dcfb7dfe..cec41eae71 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -503,7 +503,4 @@ extern bool wait_result_is_any_signal(int exit_status, bool include_command_not_
 #define HAVE_SYMLINK 1
 #endif
 
-/* Interfaces that we assume that all systems have. */
-#define HAVE_UNIX_SOCKETS 1
-
 #endif							/* PG_PORT_H */
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index dc49387d6c..730e79a31d 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1102,10 +1102,8 @@ connectOptions2(PGconn *conn)
 		else if (ch->host != NULL && ch->host[0] != '\0')
 		{
 			ch->type = CHT_HOST_NAME;
-#ifdef HAVE_UNIX_SOCKETS
 			if (is_unixsock_path(ch->host))
 				ch->type = CHT_UNIX_SOCKET;
-#endif
 		}
 		else
 		{
@@ -1115,14 +1113,12 @@ connectOptions2(PGconn *conn)
 			 * This bit selects the default host location.  If you change
 			 * this, see also pg_regress.
 			 */
-#ifdef HAVE_UNIX_SOCKETS
 			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;
@@ -1684,7 +1680,6 @@ getHostaddr(PGconn *conn, char *host_addr, int host_addr_len)
 static void
 emitHostIdentityInfo(PGconn *conn, const char *host_addr)
 {
-#ifdef HAVE_UNIX_SOCKETS
 	if (conn->raddr.addr.ss_family == AF_UNIX)
 	{
 		char		service[NI_MAXHOST];
@@ -1698,7 +1693,6 @@ emitHostIdentityInfo(PGconn *conn, const char *host_addr)
 						  service);
 	}
 	else
-#endif							/* HAVE_UNIX_SOCKETS */
 	{
 		const char *displayed_host;
 		const char *displayed_port;
@@ -1748,12 +1742,10 @@ connectFailureMessage(PGconn *conn, int errorno)
 					  "%s\n",
 					  SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)));
 
-#ifdef HAVE_UNIX_SOCKETS
 	if (conn->raddr.addr.ss_family == AF_UNIX)
 		appendPQExpBufferStr(&conn->errorMessage,
 							 libpq_gettext("\tIs the server running locally and accepting connections on that socket?\n"));
 	else
-#endif
 		appendPQExpBufferStr(&conn->errorMessage,
 							 libpq_gettext("\tIs the server running on that host and accepting TCP/IP connections?\n"));
 }
@@ -2415,7 +2407,6 @@ keep_going:						/* We will come back to here until there is
 				break;
 
 			case CHT_UNIX_SOCKET:
-#ifdef HAVE_UNIX_SOCKETS
 				conn->addrlist_family = hint.ai_family = AF_UNIX;
 				UNIXSOCK_PATH(portstr, thisport, ch->host);
 				if (strlen(portstr) >= UNIXSOCK_PATH_BUFLEN)
@@ -2440,9 +2431,6 @@ keep_going:						/* We will come back to here until there is
 									  portstr, gai_strerror(ret));
 					goto keep_going;
 				}
-#else
-				Assert(false);
-#endif
 				break;
 		}
 
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index e015a11c21..cd5a5fd5dd 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -99,11 +99,9 @@ static char *logfilename;
 static FILE *logfile;
 static char *difffilename;
 static const char *sockdir;
-#ifdef HAVE_UNIX_SOCKETS
 static const char *temp_sockdir;
 static char sockself[MAXPGPATH];
 static char socklock[MAXPGPATH];
-#endif
 
 static _resultmap *resultmap = NULL;
 
@@ -285,7 +283,6 @@ stop_postmaster(void)
 	}
 }
 
-#ifdef HAVE_UNIX_SOCKETS
 /*
  * Remove the socket temporary directory.  pg_regress never waits for a
  * postmaster exit, so it is indeterminate whether the postmaster has yet to
@@ -360,7 +357,6 @@ make_temp_sockdir(void)
 
 	return temp_sockdir;
 }
-#endif							/* HAVE_UNIX_SOCKETS */
 
 /*
  * Check whether string matches pattern
@@ -683,7 +679,6 @@ initialize_environment(void)
 		/* PGPORT, see below */
 		/* PGHOST, see below */
 
-#ifdef HAVE_UNIX_SOCKETS
 		if (hostname != NULL)
 			setenv("PGHOST", hostname, 1);
 		else
@@ -693,10 +688,6 @@ initialize_environment(void)
 				sockdir = make_temp_sockdir();
 			setenv("PGHOST", sockdir, 1);
 		}
-#else
-		Assert(hostname != NULL);
-		setenv("PGHOST", hostname, 1);
-#endif
 		unsetenv("PGHOSTADDR");
 		if (port != -1)
 		{
@@ -746,11 +737,9 @@ initialize_environment(void)
 		if (!pghost)
 		{
 			/* Keep this bit in sync with libpq's default host location: */
-#ifdef HAVE_UNIX_SOCKETS
 			if (DEFAULT_PGSOCKET_DIR[0])
 				 /* do nothing, we'll print "Unix socket" below */ ;
 			else
-#endif
 				pghost = "localhost";	/* DefaultHost in fe-connect.c */
 		}
 
@@ -2068,9 +2057,7 @@ regression_main(int argc, char *argv[],
 
 	atexit(stop_postmaster);
 
-#if !defined(HAVE_UNIX_SOCKETS)
-	use_unix_sockets = false;
-#elif defined(WIN32)
+#if defined(WIN32)
 
 	/*
 	 * We don't use Unix-domain sockets on Windows by default, even if the
@@ -2209,7 +2196,7 @@ regression_main(int argc, char *argv[],
 		/*
 		 * To reduce chances of interference with parallel installations, use
 		 * a port number starting in the private range (49152-65535)
-		 * calculated from the version number.  This aids !HAVE_UNIX_SOCKETS
+		 * calculated from the version number.  This aids non-Unix socket mode
 		 * systems; elsewhere, the use of a private socket directory already
 		 * prevents interference.
 		 */
@@ -2329,8 +2316,6 @@ regression_main(int argc, char *argv[],
 			snprintf(buf, sizeof(buf), "%s/data", temp_instance);
 			config_sspi_auth(buf, NULL);
 		}
-#elif !defined(HAVE_UNIX_SOCKETS)
-#error Platform has no means to secure the test installation.
 #endif
 
 		/*
-- 
2.30.2

