>From 0c87800038cc907cd67c92ce5101596712e3d56f Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 15 Oct 2009 12:30:34 +0100 Subject: [PATCH 4/8] Always connect to libvirt read-only. We should never be modifying domains, so there should never be a need to make a writable connection to libvirt. This also tidies up the connection code into a single function. --- vhostmd/virt-util.c | 43 +++++++++++++++++-------------------------- 1 files changed, 17 insertions(+), 26 deletions(-) diff --git a/vhostmd/virt-util.c b/vhostmd/virt-util.c index 79d1a14..8d59aea 100644 --- a/vhostmd/virt-util.c +++ b/vhostmd/virt-util.c @@ -28,31 +28,28 @@ static virConnectPtr conn = NULL; +static int +do_connect (void) +{ + if (conn == NULL) { + conn = virConnectOpenReadOnly (NULL); + if (conn == NULL) { + vu_log (VHOSTMD_ERR, "Unable to open libvirt connection"); + return -1; + } + } + return 0; +} + int vu_num_vms(void) { - if (conn == NULL) - conn = virConnectOpen(NULL); - - if (conn == NULL) { - conn = virConnectOpenReadOnly(NULL); - if (conn == NULL) { - vu_log(VHOSTMD_ERR, "Unable to open libvirt connection"); - return -1; - } - } - + if (do_connect () == -1) return -1; return virConnectNumOfDomains(conn); } int vu_get_vms(int *ids, int max_ids) { - if (conn == NULL) - conn = virConnectOpen(NULL); - - if (conn == NULL) { - vu_log(VHOSTMD_ERR, "Unable to open libvirt connection"); - return -1; - } + if (do_connect () == -1) return -1; return (virConnectListDomains(conn, ids, max_ids)); } @@ -64,20 +61,14 @@ vu_vm *vu_get_vm(int id) virDomainPtr dom = NULL; char uuid[VIR_UUID_STRING_BUFLEN]; + if (do_connect () == -1) return NULL; + vm = calloc(1, sizeof(vu_vm)); if (vm == NULL) return NULL; vm->id = id; - if (conn == NULL) - conn = virConnectOpen(NULL); - - if (conn == NULL) { - vu_log(VHOSTMD_ERR, "Unable to open libvirt connection"); - goto error; - } - dom = virDomainLookupByID(conn, id); if (dom == NULL) { vu_log(VHOSTMD_ERR, "Failed to lookup domain for id %d", id); -- 1.6.5.rc2