[libvirt] [PATCH 11/14] remote: refactor code for building UNIX socket paths

Daniel P. Berrangé posted 14 patches 7 years ago
[libvirt] [PATCH 11/14] remote: refactor code for building UNIX socket paths
Posted by Daniel P. Berrangé 7 years ago
The code for building UNIX socket paths will be getting more complex to
cope with accessing various different daemons. Refactor it to eliminate
the code duplication and isolation the logic for constructing paths.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 src/remote/remote_driver.c | 69 ++++++++++++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 27 deletions(-)

diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 436324d252..b4b034423a 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -710,6 +710,39 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr conn,
         var->ignore = 1; \
         continue; \
     }
+
+
+static char *remoteGetUNIXSocketNonRoot(void)
+{
+    char *sockname = NULL;
+    char *userdir = virGetUserRuntimeDirectory();
+
+    if (!userdir)
+        return NULL;
+
+    if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
+        VIR_FREE(userdir);
+        return NULL;
+    }
+    VIR_FREE(userdir);
+
+    VIR_DEBUG("Chosen UNIX sockname %s", sockname);
+    return sockname;
+}
+
+static char *remoteGetUNIXSocketRoot(unsigned int flags)
+{
+    char *sockname = NULL;
+
+    if (VIR_STRDUP(sockname,
+                   flags & VIR_DRV_OPEN_REMOTE_RO ?
+                   LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+        return NULL;
+
+    VIR_DEBUG("Chosen UNIX sockname %s", sockname);
+    return sockname;
+}
+
 /*
  * URIs that this driver needs to handle:
  *
@@ -971,9 +1004,7 @@ doRemoteOpen(virConnectPtr conn,
                 goto failed;
             }
 
-            if (VIR_STRDUP(sockname,
-                           flags & VIR_DRV_OPEN_REMOTE_RO ?
-                           LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+            if (!(sockname = remoteGetUNIXSocketRoot(flags)))
                 goto failed;
         }
 
@@ -1008,9 +1039,7 @@ doRemoteOpen(virConnectPtr conn,
                 goto failed;
             }
 
-            if (VIR_STRDUP(sockname,
-                           flags & VIR_DRV_OPEN_REMOTE_RO ?
-                           LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+            if (!(sockname = remoteGetUNIXSocketRoot(flags)))
                 goto failed;
         }
 
@@ -1037,24 +1066,12 @@ doRemoteOpen(virConnectPtr conn,
 #ifndef WIN32
     case trans_unix:
         if (!sockname) {
-            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
-                char *userdir = virGetUserRuntimeDirectory();
-
-                if (!userdir)
-                    goto failed;
-
-                if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
-                    VIR_FREE(userdir);
-                    goto failed;
-                }
-                VIR_FREE(userdir);
-            } else {
-                if (VIR_STRDUP(sockname,
-                               flags & VIR_DRV_OPEN_REMOTE_RO ?
-                               LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
-                    goto failed;
-            }
-            VIR_DEBUG("Proceeding with sockname %s", sockname);
+            if (flags & VIR_DRV_OPEN_REMOTE_USER)
+                sockname = remoteGetUNIXSocketNonRoot();
+            else
+                sockname = remoteGetUNIXSocketRoot(flags);
+            if (!sockname)
+                goto failed;
         }
 
         if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
@@ -1087,9 +1104,7 @@ doRemoteOpen(virConnectPtr conn,
                 goto failed;
             }
 
-            if (VIR_STRDUP(sockname,
-                           flags & VIR_DRV_OPEN_REMOTE_RO ?
-                           LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+            if (!(sockname = remoteGetUNIXSocketRoot(flags)))
                 goto failed;
         }
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 11/14] remote: refactor code for building UNIX socket paths
Posted by Michal Privoznik 7 years ago
On 04/19/2018 07:09 PM, Daniel P. Berrangé wrote:
> The code for building UNIX socket paths will be getting more complex to
> cope with accessing various different daemons. Refactor it to eliminate
> the code duplication and isolation the logic for constructing paths.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  src/remote/remote_driver.c | 69 ++++++++++++++++++++++++++++------------------
>  1 file changed, 42 insertions(+), 27 deletions(-)

ACK

Up until and including this patch, you can merge these as they fix real
problems / improve things.

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list