[PATCH] Standardize socket family checks with proper macro

Julio Faracco posted 1 patch 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20260120033659.58202-1-jcfaracco@gmail.com
src/rpc/virnetsocket.c   | 20 ++++++++++----------
src/util/virsocketaddr.c |  4 ++--
2 files changed, 12 insertions(+), 12 deletions(-)
[PATCH] Standardize socket family checks with proper macro
Posted by Julio Faracco 1 week, 6 days ago
There is a macro called VIR_SOCKET_ADDR_IS_FAMILY that checks the socket
family. There are several occurrences of raw checks in libvirt code. This
commit just standardizes the socket family checks by using its macro.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
---
 src/rpc/virnetsocket.c   | 20 ++++++++++----------
 src/util/virsocketaddr.c |  4 ++--
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index e8fc2d5f7d..22b0aa6ead 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -276,8 +276,8 @@ virNetSocketNew(virSocketAddr *localAddr,
     sock->unlinkUNIX = unlinkUNIX;
 
     /* Disable nagle for TCP sockets */
-    if (sock->localAddr.data.sa.sa_family == AF_INET ||
-        sock->localAddr.data.sa.sa_family == AF_INET6) {
+    if (VIR_SOCKET_ADDR_IS_FAMILY(&sock->localAddr, AF_INET) ||
+        VIR_SOCKET_ADDR_IS_FAMILY(&sock->localAddr, AF_INET6)) {
         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
                        &no_slow_start,
                        sizeof(no_slow_start)) < 0) {
@@ -405,9 +405,9 @@ int virNetSocketNewListenTCP(const char *nodename,
          * other IP protocol
          */
         if (port != 0 && service == NULL) {
-            if (addr.data.sa.sa_family == AF_INET) {
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&addr, AF_INET)) {
                 addr.data.inet4.sin_port = port;
-            } else if (addr.data.sa.sa_family == AF_INET6) {
+            } else if (VIR_SOCKET_ADDR_IS_FAMILY(&addr, AF_INET6)) {
                 addr.data.inet6.sin6_port = port;
             }
             VIR_DEBUG("Used saved port %d", port);
@@ -432,9 +432,9 @@ int virNetSocketNewListenTCP(const char *nodename,
         }
 
         if (port == 0 && service == NULL) {
-            if (addr.data.sa.sa_family == AF_INET)
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&addr, AF_INET))
                 port = addr.data.inet4.sin_port;
-            else if (addr.data.sa.sa_family == AF_INET6)
+            else if (VIR_SOCKET_ADDR_IS_FAMILY(&addr, AF_INET6))
                 port = addr.data.inet6.sin6_port;
             VIR_DEBUG("Saved port %d", port);
         }
@@ -1293,7 +1293,7 @@ void virNetSocketDispose(void *obj)
 #ifndef WIN32
     /* If a server socket, then unlink UNIX path */
     if (sock->unlinkUNIX &&
-        sock->localAddr.data.sa.sa_family == AF_UNIX &&
+        VIR_SOCKET_ADDR_IS_FAMILY(&sock->localAddr, AF_UNIX) &&
         sock->localAddr.data.un.sun_path[0] != '\0')
         unlink(sock->localAddr.data.un.sun_path);
 #endif
@@ -1370,7 +1370,7 @@ bool virNetSocketIsLocal(virNetSocket *sock)
 {
     bool isLocal = false;
     virObjectLock(sock);
-    if (sock->localAddr.data.sa.sa_family == AF_UNIX)
+    if (VIR_SOCKET_ADDR_IS_FAMILY(&sock->localAddr, AF_UNIX))
         isLocal = true;
     virObjectUnlock(sock);
     return isLocal;
@@ -1381,7 +1381,7 @@ bool virNetSocketHasPassFD(virNetSocket *sock)
 {
     bool hasPassFD = false;
     virObjectLock(sock);
-    if (sock->localAddr.data.sa.sa_family == AF_UNIX)
+    if (VIR_SOCKET_ADDR_IS_FAMILY(&sock->localAddr, AF_UNIX))
         hasPassFD = true;
     virObjectUnlock(sock);
     return hasPassFD;
@@ -2223,7 +2223,7 @@ void virNetSocketClose(virNetSocket *sock)
 #ifndef WIN32
     /* If a server socket, then unlink UNIX path */
     if (sock->unlinkUNIX &&
-        sock->localAddr.data.sa.sa_family == AF_UNIX &&
+        VIR_SOCKET_ADDR_IS_FAMILY(&sock->localAddr, AF_UNIX) &&
         sock->localAddr.data.un.sun_path[0] != '\0') {
         if (unlink(sock->localAddr.data.un.sun_path) == 0)
             sock->localAddr.data.un.sun_path[0] = '\0';
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 4d4a6b2a0f..e8e691ce8f 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -481,7 +481,7 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
 
     /* Short-circuit since getnameinfo doesn't work
      * nicely for UNIX sockets */
-    if (addr->data.sa.sa_family == AF_UNIX) {
+    if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_UNIX)) {
         if (withService) {
             addrstr = g_strdup_printf(VIR_LOOPBACK_IPV4_ADDR "%s0",
                                       separator ? separator : ":");
@@ -636,7 +636,7 @@ virSocketAddrGetPath(virSocketAddr *addr G_GNUC_UNUSED)
         return NULL;
     }
 
-    if (addr->data.sa.sa_family != AF_UNIX) {
+    if (!VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_UNIX)) {
         virReportError(VIR_ERR_INVALID_ARG, "%s",
                        _("UNIX socket address is required"));
         return NULL;
-- 
2.52.0
Re: [PATCH] Standardize socket family checks with proper macro
Posted by Ján Tomko via Devel 1 week, 3 days ago
It would be sufficient to summarize this as:
     Standardize socket family checks

On a Tuesday in 2026, Julio Faracco wrote:
>There is a macro

"The macro"

>called

Unsure what purpose this word has in this sentence. What else would you
refer to a macro by if not by its name.

>VIR_SOCKET_ADDR_IS_FAMILY that checks the socket
>family.

I think it's named propely enough to not have to spell out its function.

>There are several occurrences of raw checks in libvirt code.
>This commit

>just

I have no idea what this word is supposed to mean in this sentence.
It feels completely out of place.

Should this commit do something else? Is it not enough?
I don't think so, a commit should be some logically separate set of
changes. If there are more changes to do, they belong in a separate
commit.

>standardizes the socket family checks by using its macro.

Whose macro?

Jano

>
>Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
>---
Re: [PATCH] Standardize socket family checks with proper macro
Posted by Julio Faracco 1 week, 2 days ago
Em qui., 22 de jan. de 2026 às 07:26, Ján Tomko <jtomko@redhat.com> escreveu:
>
> It would be sufficient to summarize this as:
>      Standardize socket family checks
>
> On a Tuesday in 2026, Julio Faracco wrote:
> >There is a macro
>
> "The macro"
>
> >called
>
> Unsure what purpose this word has in this sentence. What else would you
> refer to a macro by if not by its name.
>
> >VIR_SOCKET_ADDR_IS_FAMILY that checks the socket
> >family.
>
> I think it's named propely enough to not have to spell out its function.
>
> >There are several occurrences of raw checks in libvirt code.
> >This commit
>
> >just
>
> I have no idea what this word is supposed to mean in this sentence.
> It feels completely out of place.

In V2, I will rephrase the whole commit message properly.

>
> Should this commit do something else? Is it not enough?
> I don't think so, a commit should be some logically separate set of
> changes. If there are more changes to do, they belong in a separate
> commit.

Ok, let me split up this commit in separate changes.

>
> >standardizes the socket family checks by using its macro.
>
> Whose macro?
>
> Jano
>
> >
> >Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
> >---

--
Julio