[libvirt] [dbus RFC 10/11] main: implement multiple connection within one daemon

Pavel Hrdina phrdina at redhat.com
Mon Jan 22 17:16:08 UTC 2018


This effectively removes --connect parameter from daemon, for now we
will support only local connection.  Instead of specifying URI for
different drivers we will use connect object which allows using
multiple drivers from one daemon.  In the future if that feature will
be requested we can extend libvirt-dbus to have some configuration
file where we will have an option to specify remote host instead of
local connection.

This change also defines the difference between session and system
bus, and limits the list of drivers available for that specific bus
because not all drivers support session mode.  Currently only QEMU
driver has session mode.  For test purposes we can allow TEST driver
to be available for session bus.

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/connect.c       | 17 +++++++++++++++--
 src/connect.h       |  4 +++-
 src/main.c          | 55 ++++++++++++++++++++++++++++++++++-------------------
 test/libvirttest.py |  4 ++--
 4 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/src/connect.c b/src/connect.c
index 5249eba..878cf91 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -238,7 +238,8 @@ static const sd_bus_vtable virt_connect_vtable[] = {
 int
 virtDBusConnectNew(virtDBusConnect **connectp,
                    sd_bus *bus,
-                   const char *uri)
+                   const char *uri,
+                   const char *connectPath)
 {
     _cleanup_(virtDBusConnectFreep) virtDBusConnect *connect = NULL;
     int r;
@@ -249,7 +250,7 @@ virtDBusConnectNew(virtDBusConnect **connectp,
 
     connect->bus = sd_bus_ref(bus);
     connect->uri = uri;
-    connect->connectPath = "/org/libvirt/Connect";
+    connect->connectPath = connectPath;
 
     connect->enumerateDomains = virtDBusConnectEnumarateDomains;
 
@@ -293,3 +294,15 @@ virtDBusConnectFreep(virtDBusConnect **connectp)
     if (*connectp)
         virtDBusConnectFree(*connectp);
 }
+
+void
+virtDBusConnectListFree(virtDBusConnect ***connectList)
+{
+    if (!*connectList)
+        return;
+
+    for (int i = 0; (*connectList)[i]; i += 1)
+        virtDBusConnectFree((*connectList)[i]);
+
+    free(*connectList);
+}
diff --git a/src/connect.h b/src/connect.h
index bed2c7e..46e8c9a 100644
--- a/src/connect.h
+++ b/src/connect.h
@@ -20,6 +20,8 @@ typedef struct virtDBusConnect virtDBusConnect;
 
 int virtDBusConnectNew(virtDBusConnect **connectp,
                        sd_bus *bus,
-                       const char *uri);
+                       const char *uri,
+                       const char *connectPath);
 virtDBusConnect *virtDBusConnectFree(virtDBusConnect *connect);
 void virtDBusConnectFreep(virtDBusConnect **connectp);
+void virtDBusConnectListFree(virtDBusConnect ***connectList);
diff --git a/src/main.c b/src/main.c
index 95522f5..14c7c18 100644
--- a/src/main.c
+++ b/src/main.c
@@ -79,6 +79,21 @@ virtDBusHandleBusEvent(int watch,
     virEventUpdateHandle(watch, virtDBusGetLibvirtEvents(bus));
 }
 
+struct virtDBusDriver {
+    const char *uri;
+    const char *object;
+};
+
+static const struct virtDBusDriver sessionDrivers[] = {
+    { "qemu:///session",    "/org/libvirt/qemu" },
+    { "test:///default",    "/org/libvirt/test" },
+};
+
+static const struct virtDBusDriver systemDrivers[] = {
+    { "qemu:///system",     "/org/libvirt/qemu" },
+    { "test:///default",    "/org/libvirt/test" },
+};
+
 int
 main(int argc, char *argv[])
 {
@@ -89,16 +104,16 @@ main(int argc, char *argv[])
 
     static const struct option options[] = {
         { "help",    no_argument,       NULL, 'h' },
-        { "connect", required_argument, NULL, 'c' },
         { "system",  no_argument,       NULL, ARG_SYSTEM },
         { "session", no_argument,       NULL, ARG_SESSION },
         {}
     };
 
     bool system_bus;
-    const char *uri = NULL;
+    const struct virtDBusDriver *drivers = NULL;
+    int ndrivers = 0;
 
-    _cleanup_(virtDBusConnectFreep) virtDBusConnect *connect = NULL;
+    _cleanup_(virtDBusConnectListFree) virtDBusConnect **connect = NULL;
     _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
     _cleanup_(virtDBusUtilClosep) int signal_fd = -1;
     _cleanup_(virtDBusVirEventRemoveHandlep) int bus_watch = -1;
@@ -121,15 +136,10 @@ main(int argc, char *argv[])
                 printf("Provide a D-Bus interface to a libvirtd.\n");
                 printf("\n");
                 printf("  -h, --help        Display this help text and exit\n");
-                printf("  -c, --connect URI Connect to the specified libvirt URI\n");
                 printf("  --session         Connect to the session bus\n");
                 printf("  --system          Connect to the system bus\n");
                 return 0;
 
-            case 'c':
-                uri = optarg;
-                break;
-
             case ARG_SYSTEM:
                 system_bus = true;
                 break;
@@ -143,14 +153,6 @@ main(int argc, char *argv[])
         }
     }
 
-    if (uri == NULL) {
-        if (system_bus) {
-            uri = "qemu:///system";
-        } else {
-            uri = "qemu:///session";
-        }
-    }
-
     sigemptyset(&mask);
     sigaddset(&mask, SIGTERM);
     sigaddset(&mask, SIGINT);
@@ -170,10 +172,23 @@ main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
-    r = virtDBusConnectNew(&connect, bus, uri);
-    if (r < 0) {
-        fprintf(stderr, "Failed to connect to libvirt.");
-        return EXIT_FAILURE;
+    if (system_bus) {
+        drivers = systemDrivers;
+        ndrivers = VIRT_ARRAY_CARDINALITY(systemDrivers);
+    } else {
+        drivers = sessionDrivers;
+        ndrivers = VIRT_ARRAY_CARDINALITY(sessionDrivers);
+    }
+
+    connect = calloc(ndrivers + 1, sizeof(virtDBusConnect *));
+
+    for (int i = 0; i < ndrivers; i += 1) {
+        r = virtDBusConnectNew(&connect[i], bus,
+                               drivers[i].uri, drivers[i].object);
+        if (r < 0) {
+            fprintf(stderr, "Failed to register libvirt connection.");
+            return EXIT_FAILURE;
+        }
     }
 
     r = virtDBusProcessEvents(bus);
diff --git a/test/libvirttest.py b/test/libvirttest.py
index 2c735e9..579485f 100644
--- a/test/libvirttest.py
+++ b/test/libvirttest.py
@@ -28,7 +28,7 @@ class TestCase(unittest.TestCase):
     def setUp(self):
         os.environ['LIBVIRT_DEBUG'] = '3'
 
-        self.daemon = subprocess.Popen([exe, '--connect', 'test:///default'])
+        self.daemon = subprocess.Popen([exe])
         self.bus = dbus.SessionBus()
 
         for i in range(10):
@@ -38,7 +38,7 @@ class TestCase(unittest.TestCase):
         else:
             raise TimeoutError('error starting libvirt-dbus')
 
-        obj = self.bus.get_object('org.libvirt', '/org/libvirt/Connect')
+        obj = self.bus.get_object('org.libvirt', '/org/libvirt/test')
         self.connect = dbus.Interface(obj, 'org.libvirt.Connect')
 
     def tearDown(self):
-- 
2.14.3




More information about the libvir-list mailing list