]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
remote: extract logic for determining daemon to connect to
authorDaniel P. Berrangé <berrange@redhat.com>
Mon, 8 Jul 2019 14:30:59 +0000 (15:30 +0100)
committerDaniel P. Berrangé <berrange@redhat.com>
Wed, 9 Sep 2020 15:46:22 +0000 (16:46 +0100)
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@redhat.com>
src/remote/remote_driver.c
src/remote/remote_sockets.c
src/remote/remote_sockets.h

index 56c3650448aa1afdcc281cdaddfed8163df95dd4..4a5bd5b6798c9a66f2ad80fbfe7cd87e9e2d75cd 100644 (file)
@@ -1197,7 +1197,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;
 
@@ -1236,51 +1237,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) {
index 28e02e24d5c0620c38828af2ddb8ed5143432bd5..854775f4014f5e35448f1da012e749ff73578e3a 100644 (file)
@@ -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;
+        }
+    }
+}
index 64055f3d44cf77dead61a4e47bdc78510af79c10..752675283501983e0853ad32c8daef91c73b2800 100644 (file)
@@ -62,3 +62,9 @@ remoteGetUNIXSocket(remoteDriverTransport transport,
                     bool ro,
                     bool session,
                     char **daemon);
+
+void
+remoteGetURIDaemonInfo(virURIPtr uri,
+                       remoteDriverTransport transport,
+                       bool *session,
+                       bool *autostart);