Open a connection to libvirt only when it is required. Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx> --- src/connect.c | 41 +++++++++++++++++++++++++++++++++++------ src/connect.h | 1 + 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/connect.c b/src/connect.c index cb19c39..8d958c2 100644 --- a/src/connect.c +++ b/src/connect.c @@ -6,6 +6,23 @@ #include <errno.h> #include <stdlib.h> +static int +virtDBusConnectOpen(virtDBusConnect *connect, + sd_bus_error *error) +{ + if (connect->connection) + return 0; + + connect->connection = virConnectOpenAuth(connect->uri, + virConnectAuthPtrDefault, 0); + if (!connect->connection) + return virtDBusUtilSetLastVirtError(error); + + virtDBusEventsRegister(connect); + + return 0; +} + static int virtDBusConnectEnumarateDomains(sd_bus *bus VIR_ATTR_UNUSED, const char *path VIR_ATTR_UNUSED, @@ -17,6 +34,11 @@ virtDBusConnectEnumarateDomains(sd_bus *bus VIR_ATTR_UNUSED, _cleanup_(virtDBusUtilVirDomainListFreep) virDomainPtr *domains = NULL; _cleanup_(virtDBusUtilStrvFreep) char **paths = NULL; int n_domains; + int r; + + r = virtDBusConnectOpen(connect, error); + if (r < 0) + return r; n_domains = virConnectListAllDomains(connect->connection, &domains, 0); if (n_domains < 0) @@ -44,6 +66,10 @@ virtDBusConnectListDomains(sd_bus_message *message, uint32_t flags; int r; + r = virtDBusConnectOpen(connect, error); + if (r < 0) + return r; + r = sd_bus_message_read(message, "u", &flags); if (r < 0) return r; @@ -89,6 +115,10 @@ virtDBusConnectCreateXML(sd_bus_message *message, _cleanup_(virtDBusUtilFreep) char *path = NULL; int r; + r = virtDBusConnectOpen(connect, error); + if (r < 0) + return r; + r = sd_bus_message_read(message, "su", &xml, &flags); if (r < 0) return r; @@ -113,6 +143,10 @@ virtDBusConnectDefineXML(sd_bus_message *message, _cleanup_(virtDBusUtilFreep) char *path = NULL; int r; + r = virtDBusConnectOpen(connect, error); + if (r < 0) + return r; + r = sd_bus_message_read(message, "s", &xml); if (r < 0) return r; @@ -159,12 +193,7 @@ virtDBusConnectNew(virtDBusConnect **connectp, connect->callback_ids[i] = -1; connect->bus = sd_bus_ref(bus); - - connect->connection = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0); - if (!connect->connection) - return -EINVAL; - - virtDBusEventsRegister(connect); + connect->uri = uri; r = sd_bus_add_object_vtable(connect->bus, NULL, diff --git a/src/connect.h b/src/connect.h index 5d64a10..52e8279 100644 --- a/src/connect.h +++ b/src/connect.h @@ -7,6 +7,7 @@ struct virtDBusConnect { sd_bus *bus; + const char *uri; virConnectPtr connection; int callback_ids[VIR_DOMAIN_EVENT_ID_LAST]; -- 2.14.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list