I noticed that in pgstatfuncs.c, the AF_UNIX macro is being used unprotected by HAVE_UNIX_SOCKETS, apparently since 2008. So I think the redirection through IS_AF_UNIX() is no longer necessary. (More generally, all supported platforms are now HAVE_UNIX_SOCKETS, but even if there were a new platform in the future, it seems plausible that it would define the AF_UNIX symbol even without kernel support.) So maybe we should remove the IS_AF_UNIX() macro and make the code a bit more consistent?
From 388be5ebd137fc409878ac74061526e6dfcecf57 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Mon, 7 Feb 2022 08:37:00 +0100
Subject: [PATCH] Remove IS_AF_UNIX macro

---
 src/backend/libpq/hba.c             |  4 ++--
 src/backend/libpq/pqcomm.c          | 24 ++++++++++++------------
 src/backend/postmaster/postmaster.c |  4 ++--
 src/include/common/ip.h             |  6 ------
 src/interfaces/libpq/fe-connect.c   | 14 +++++++-------
 5 files changed, 23 insertions(+), 29 deletions(-)

diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c
index a7f3def184..d84a40b726 100644
--- a/src/backend/libpq/hba.c
+++ b/src/backend/libpq/hba.c
@@ -2138,12 +2138,12 @@ check_hba(hbaPort *port)
                /* Check connection type */
                if (hba->conntype == ctLocal)
                {
-                       if (!IS_AF_UNIX(port->raddr.addr.ss_family))
+                       if (port->raddr.addr.ss_family != AF_UNIX)
                                continue;
                }
                else
                {
-                       if (IS_AF_UNIX(port->raddr.addr.ss_family))
+                       if (port->raddr.addr.ss_family == AF_UNIX)
                                continue;
 
                        /* Check SSL state */
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index f05723dc92..b96c2811a4 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -408,7 +408,7 @@ StreamServerPort(int family, const char *hostName, unsigned 
short portNumber,
 
        for (addr = addrs; addr; addr = addr->ai_next)
        {
-               if (!IS_AF_UNIX(family) && IS_AF_UNIX(addr->ai_family))
+               if (family != AF_UNIX && addr->ai_family == AF_UNIX)
                {
                        /*
                         * Only set up a unix domain socket when they really 
asked for it.
@@ -493,7 +493,7 @@ StreamServerPort(int family, const char *hostName, unsigned 
short portNumber,
                 * unpredictable behavior. With no flags at all, win32 behaves 
as Unix
                 * with SO_REUSEADDR.
                 */
-               if (!IS_AF_UNIX(addr->ai_family))
+               if (addr->ai_family != AF_UNIX)
                {
                        if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
                                                        (char *) &one, 
sizeof(one))) == -1)
@@ -545,7 +545,7 @@ StreamServerPort(int family, const char *hostName, unsigned 
short portNumber,
                                         errmsg("could not bind %s address 
\"%s\": %m",
                                                        familyDesc, addrDesc),
                                         saved_errno == EADDRINUSE ?
-                                        (IS_AF_UNIX(addr->ai_family) ?
+                                        (addr->ai_family == AF_UNIX ?
                                          errhint("Is another postmaster 
already running on port %d?",
                                                          (int) portNumber) :
                                          errhint("Is another postmaster 
already running on port %d?"
@@ -763,7 +763,7 @@ StreamConnection(pgsocket server_fd, Port *port)
        }
 
        /* select NODELAY and KEEPALIVE options if it's a TCP connection */
-       if (!IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port->laddr.addr.ss_family != AF_UNIX)
        {
                int                     on;
 #ifdef WIN32
@@ -1638,7 +1638,7 @@ int
 pq_getkeepalivesidle(Port *port)
 {
 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return 0;
 
        if (port->keepalives_idle != 0)
@@ -1672,7 +1672,7 @@ pq_getkeepalivesidle(Port *port)
 int
 pq_setkeepalivesidle(int idle, Port *port)
 {
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return STATUS_OK;
 
 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
@@ -1723,7 +1723,7 @@ int
 pq_getkeepalivesinterval(Port *port)
 {
 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return 0;
 
        if (port->keepalives_interval != 0)
@@ -1757,7 +1757,7 @@ pq_getkeepalivesinterval(Port *port)
 int
 pq_setkeepalivesinterval(int interval, Port *port)
 {
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return STATUS_OK;
 
 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
@@ -1807,7 +1807,7 @@ int
 pq_getkeepalivescount(Port *port)
 {
 #ifdef TCP_KEEPCNT
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return 0;
 
        if (port->keepalives_count != 0)
@@ -1836,7 +1836,7 @@ pq_getkeepalivescount(Port *port)
 int
 pq_setkeepalivescount(int count, Port *port)
 {
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return STATUS_OK;
 
 #ifdef TCP_KEEPCNT
@@ -1882,7 +1882,7 @@ int
 pq_gettcpusertimeout(Port *port)
 {
 #ifdef TCP_USER_TIMEOUT
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return 0;
 
        if (port->tcp_user_timeout != 0)
@@ -1911,7 +1911,7 @@ pq_gettcpusertimeout(Port *port)
 int
 pq_settcpusertimeout(int timeout, Port *port)
 {
-       if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
+       if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
                return STATUS_OK;
 
 #ifdef TCP_USER_TIMEOUT
diff --git a/src/backend/postmaster/postmaster.c 
b/src/backend/postmaster/postmaster.c
index ac0ec0986a..9478e28257 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2085,7 +2085,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool 
gss_done)
 
 #ifdef USE_SSL
                /* No SSL when disabled or on Unix sockets */
-               if (!LoadedSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
+               if (!LoadedSSL || port->laddr.addr.ss_family == AF_UNIX)
                        SSLok = 'N';
                else
                        SSLok = 'S';            /* Support for SSL */
@@ -2134,7 +2134,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool 
gss_done)
 
 #ifdef ENABLE_GSS
                /* No GSSAPI encryption when on Unix socket */
-               if (!IS_AF_UNIX(port->laddr.addr.ss_family))
+               if (port->laddr.addr.ss_family != AF_UNIX)
                        GSSok = 'G';
 #endif
 
diff --git a/src/include/common/ip.h b/src/include/common/ip.h
index 85c8857435..8414520989 100644
--- a/src/include/common/ip.h
+++ b/src/include/common/ip.h
@@ -18,12 +18,6 @@
 #include "libpq/pqcomm.h"              /* pgrminclude ignore */
 
 
-#ifdef HAVE_UNIX_SOCKETS
-#define IS_AF_UNIX(fam) ((fam) == AF_UNIX)
-#else
-#define IS_AF_UNIX(fam) (0)
-#endif
-
 extern int     pg_getaddrinfo_all(const char *hostname, const char *servname,
                                                           const struct 
addrinfo *hintp,
                                                           struct addrinfo 
**result);
diff --git a/src/interfaces/libpq/fe-connect.c 
b/src/interfaces/libpq/fe-connect.c
index a6a1db3356..30d6b7b377 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1694,7 +1694,7 @@ static void
 emitHostIdentityInfo(PGconn *conn, const char *host_addr)
 {
 #ifdef HAVE_UNIX_SOCKETS
-       if (IS_AF_UNIX(conn->raddr.addr.ss_family))
+       if (conn->raddr.addr.ss_family == AF_UNIX)
        {
                char            service[NI_MAXHOST];
 
@@ -1758,7 +1758,7 @@ connectFailureMessage(PGconn *conn, int errorno)
                                          SOCK_STRERROR(errorno, sebuf, 
sizeof(sebuf)));
 
 #ifdef HAVE_UNIX_SOCKETS
-       if (IS_AF_UNIX(conn->raddr.addr.ss_family))
+       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
@@ -2590,7 +2590,7 @@ PQconnectPoll(PGconn *conn)
                                         * TCP sockets, nonblock mode, 
close-on-exec.  Try the
                                         * next address if any of this fails.
                                         */
-                                       if (!IS_AF_UNIX(addr_cur->ai_family))
+                                       if (addr_cur->ai_family != AF_UNIX)
                                        {
                                                if (!connectNoDelay(conn))
                                                {
@@ -2619,7 +2619,7 @@ PQconnectPoll(PGconn *conn)
                                        }
 #endif                                                 /* F_SETFD */
 
-                                       if (!IS_AF_UNIX(addr_cur->ai_family))
+                                       if (addr_cur->ai_family != AF_UNIX)
                                        {
 #ifndef WIN32
                                                int                     on = 1;
@@ -2821,7 +2821,7 @@ PQconnectPoll(PGconn *conn)
                                 * Unix-domain socket.
                                 */
                                if (conn->requirepeer && conn->requirepeer[0] &&
-                                       IS_AF_UNIX(conn->raddr.addr.ss_family))
+                                       conn->raddr.addr.ss_family == AF_UNIX)
                                {
 #ifndef WIN32
                                        char       *remote_username;
@@ -2867,7 +2867,7 @@ PQconnectPoll(PGconn *conn)
 #endif                                                 /* WIN32 */
                                }
 
-                               if (IS_AF_UNIX(conn->raddr.addr.ss_family))
+                               if (conn->raddr.addr.ss_family == AF_UNIX)
                                {
                                        /* Don't request SSL or GSSAPI over 
Unix sockets */
 #ifdef USE_SSL
@@ -4528,7 +4528,7 @@ PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
         * This ensures that this function does not block indefinitely when
         * reasonable keepalive and timeout settings have been provided.
         */
-       if (!IS_AF_UNIX(cancel->raddr.addr.ss_family) &&
+       if (cancel->raddr.addr.ss_family != AF_UNIX &&
                cancel->keepalives != 0)
        {
 #ifndef WIN32
-- 
2.35.1

Reply via email to