[libvirt PATCH v2 07/10] remote: extract logic for determining daemon to connect to

Daniel P. Berrangé berrange at redhat.com
Fri Jul 24 15:14:11 UTC 2020


We'll shortly want to reuse code for determining whether to connect to
the system or session daemon from places outside the remote driver
client. Pulling it out into a self contained function facilitates reuse.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/remote/remote_driver.c  | 51 ++++----------------------------
 src/remote/remote_sockets.c | 59 +++++++++++++++++++++++++++++++++++++
 src/remote/remote_sockets.h |  6 ++++
 3 files changed, 71 insertions(+), 45 deletions(-)

diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 572326d0ce..b59352aca0 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -1198,7 +1198,8 @@ remoteConnectOpen(virConnectPtr conn,
     struct private_data *priv;
     int ret = VIR_DRV_OPEN_ERROR;
     int rflags = 0;
-    const char *autostart = getenv("LIBVIRT_AUTOSTART");
+    bool user;
+    bool autostart;
     char *driver = NULL;
     remoteDriverTransport transport;
 
@@ -1233,51 +1234,11 @@ remoteConnectOpen(virConnectPtr conn,
     if (flags & VIR_CONNECT_RO)
         rflags |= VIR_DRV_OPEN_REMOTE_RO;
 
-    /*
-     * User session daemon is used for
-     *
-     *  - Any URI with /session suffix
-     *  - Test driver, if a protocol is given
-     *
-     * provided we are running non-root
-     */
-    if (conn->uri &&
-        conn->uri->path &&
-        conn->uri->scheme &&
-        (STREQ(conn->uri->path, "/session") ||
-         STRPREFIX(conn->uri->scheme, "test+")) &&
-        geteuid() > 0) {
-        VIR_DEBUG("User session daemon required");
+    remoteGetURIDaemonInfo(conn->uri, transport, &user, &autostart);
+    if (user)
         rflags |= VIR_DRV_OPEN_REMOTE_USER;
-
-        /*
-         * Furthermore if no servername is given,
-         * and the transport is unix,
-         * and uid is unprivileged then auto-spawn a daemon.
-         */
-        if (!conn->uri->server &&
-            (transport == REMOTE_DRIVER_TRANSPORT_UNIX) &&
-            (!autostart ||
-             STRNEQ(autostart, "0"))) {
-            VIR_DEBUG("Try daemon autostart");
-            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
-        }
-    }
-
-    /*
-     * If URI is NULL, then do a UNIX connection possibly auto-spawning
-     * unprivileged server and probe remote server for URI.
-     */
-    if (!conn->uri) {
-        VIR_DEBUG("Auto-probe remote URI");
-        if (geteuid() > 0) {
-            VIR_DEBUG("Auto-spawn user daemon instance");
-            rflags |= VIR_DRV_OPEN_REMOTE_USER;
-            if (!autostart ||
-                STRNEQ(autostart, "0"))
-                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
-        }
-    }
+    if (autostart)
+        rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
 
     ret = doRemoteOpen(conn, priv, driver, transport, auth, conf, rflags);
     if (ret != VIR_DRV_OPEN_SUCCESS) {
diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c
index 28e02e24d5..854775f401 100644
--- a/src/remote/remote_sockets.c
+++ b/src/remote/remote_sockets.c
@@ -224,3 +224,62 @@ remoteGetUNIXSocket(remoteDriverTransport transport,
               ro, session);
     return sock_name;
 }
+
+
+void
+remoteGetURIDaemonInfo(virURIPtr uri,
+                       remoteDriverTransport transport,
+                       bool *session,
+                       bool *autostart)
+{
+    const char *autostart_str = getenv("LIBVIRT_AUTOSTART");
+
+    *session = false;
+    *autostart = false;
+
+    /*
+     * User session daemon is used for
+     *
+     *  - Any URI with /session suffix
+     *  - Test driver, if a protocol is given
+     *
+     * provided we are running non-root
+     */
+    if (uri &&
+        uri->path &&
+        uri->scheme &&
+        (STREQ(uri->path, "/session") ||
+         STRPREFIX(uri->scheme, "test+")) &&
+        geteuid() > 0) {
+        VIR_DEBUG("User session daemon required");
+        *session = true;
+
+        /*
+         * Furthermore if no servername is given,
+         * and the transport is unix,
+         * and uid is unprivileged then auto-spawn a daemon.
+         */
+        if (!uri->server &&
+            (transport == REMOTE_DRIVER_TRANSPORT_UNIX) &&
+            (!autostart_str ||
+             STRNEQ(autostart_str, "0"))) {
+            VIR_DEBUG("Try daemon autostart");
+            *autostart = true;
+        }
+    }
+
+    /*
+     * If URI is NULL, then do a UNIX connection possibly auto-spawning
+     * unprivileged server and probe remote server for URI.
+     */
+    if (!uri) {
+        VIR_DEBUG("Auto-probe remote URI");
+        if (geteuid() > 0) {
+            VIR_DEBUG("Auto-spawn user daemon instance");
+            *session = true;
+            if (!autostart_str ||
+                STRNEQ(autostart_str, "0"))
+                *autostart = true;
+        }
+    }
+}
diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h
index 64055f3d44..7526752835 100644
--- a/src/remote/remote_sockets.h
+++ b/src/remote/remote_sockets.h
@@ -62,3 +62,9 @@ remoteGetUNIXSocket(remoteDriverTransport transport,
                     bool ro,
                     bool session,
                     char **daemon);
+
+void
+remoteGetURIDaemonInfo(virURIPtr uri,
+                       remoteDriverTransport transport,
+                       bool *session,
+                       bool *autostart);
-- 
2.26.2




More information about the libvir-list mailing list