6ae9ed
From c70fe441c47030833541ec9401c9b6729bdae2fe Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <c70fe441c47030833541ec9401c9b6729bdae2fe@dist-git>
6ae9ed
From: "Daniel P. Berrange" <berrange@redhat.com>
6ae9ed
Date: Tue, 16 Aug 2016 13:06:02 +0200
6ae9ed
Subject: [PATCH] libvirt: convert to typesafe virConf accessors
6ae9ed
6ae9ed
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
6ae9ed
(cherry picked from commit f5da0d18053376cc2a80e4648c68840b801cba81)
6ae9ed
6ae9ed
Although the upstream commit applied to downstream cleanly, it had to be
6ae9ed
further modified in the way how it retrieves data from our config structure.
6ae9ed
The original commit relied on some new accessors (wrappers) added by 6381c89f
6ae9ed
which however was not backported. Therefore the old way of retrieving data from
6ae9ed
the config structure by virConfGetValue had to be used.
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1367269
6ae9ed
Signed-off-by: Erik Skultety <eskultet@redhat.com>
6ae9ed
---
6ae9ed
 src/libvirt-admin.c | 74 +++++++++++++++++++++++++++++++-------------------
6ae9ed
 src/libvirt.c       | 78 +++++++++++++++++++++++++++++++++--------------------
6ae9ed
 2 files changed, 96 insertions(+), 56 deletions(-)
6ae9ed
6ae9ed
diff --git a/src/libvirt-admin.c b/src/libvirt-admin.c
6ae9ed
index f07cb10..c485752 100644
6ae9ed
--- a/src/libvirt-admin.c
6ae9ed
+++ b/src/libvirt-admin.c
6ae9ed
@@ -158,35 +158,44 @@ getSocketPath(virURIPtr uri)
6ae9ed
     goto cleanup;
6ae9ed
 }
6ae9ed
 
6ae9ed
-static const char *
6ae9ed
-virAdmGetDefaultURI(virConfPtr conf)
6ae9ed
+static int
6ae9ed
+virAdmGetDefaultURI(virConfPtr conf, char **uristr)
6ae9ed
 {
6ae9ed
     virConfValuePtr value = NULL;
6ae9ed
-    const char *uristr = NULL;
6ae9ed
+    const char *defname = virGetEnvAllowSUID("LIBVIRT_ADMIN_DEFAULT_URI");
6ae9ed
 
6ae9ed
-    uristr = virGetEnvAllowSUID("LIBVIRT_ADMIN_DEFAULT_URI");
6ae9ed
-    if (uristr && *uristr) {
6ae9ed
-        VIR_DEBUG("Using LIBVIRT_ADMIN_DEFAULT_URI '%s'", uristr);
6ae9ed
-    } else if ((value = virConfGetValue(conf, "admin_uri_default"))) {
6ae9ed
-        if (value->type != VIR_CONF_STRING) {
6ae9ed
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
6ae9ed
-                           _("Expected a string for 'admin_uri_default' config "
6ae9ed
-                             "parameter"));
6ae9ed
-            return NULL;
6ae9ed
+    if (defname && *defname) {
6ae9ed
+        if (VIR_STRDUP(*uristr, defname) < 0)
6ae9ed
+            return -1;
6ae9ed
+        VIR_DEBUG("Using LIBVIRT_ADMIN_DEFAULT_URI '%s'", *uristr);
6ae9ed
+    } else {
6ae9ed
+        if ((value = virConfGetValue(conf, "admin_uri_default"))) {
6ae9ed
+            if (value->type != VIR_CONF_STRING) {
6ae9ed
+                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
6ae9ed
+                               _("Expected a string for 'admin_uri_default' "
6ae9ed
+                                 "config parameter"));
6ae9ed
+                return -1;
6ae9ed
+            }
6ae9ed
+
6ae9ed
+            VIR_FREE(*uristr);
6ae9ed
+            if (VIR_STRDUP(*uristr, value->str) < 0)
6ae9ed
+                return -1;
6ae9ed
         }
6ae9ed
 
6ae9ed
-        VIR_DEBUG("Using config file uri '%s'", value->str);
6ae9ed
-        uristr = value->str;
6ae9ed
-    } else {
6ae9ed
-        /* Since we can't probe connecting via any hypervisor driver as libvirt
6ae9ed
-         * does, if no explicit URI was given and neither the environment
6ae9ed
-         * variable, nor the configuration parameter had previously been set,
6ae9ed
-         * we set the default admin server URI to 'libvirtd://system'.
6ae9ed
-         */
6ae9ed
-        uristr = "libvirtd:///system";
6ae9ed
+        if (*uristr) {
6ae9ed
+            VIR_DEBUG("Using config file uri '%s'", *uristr);
6ae9ed
+        } else {
6ae9ed
+            /* Since we can't probe connecting via any hypervisor driver as libvirt
6ae9ed
+             * does, if no explicit URI was given and neither the environment
6ae9ed
+             * variable, nor the configuration parameter had previously been set,
6ae9ed
+             * we set the default admin server URI to 'libvirtd://system'.
6ae9ed
+             */
6ae9ed
+            if (VIR_STRDUP(*uristr, "libvirtd:///system") < 0)
6ae9ed
+                return -1;
6ae9ed
+        }
6ae9ed
     }
6ae9ed
 
6ae9ed
-    return uristr;
6ae9ed
+    return 0;
6ae9ed
 }
6ae9ed
 
6ae9ed
 /**
6ae9ed
@@ -206,6 +215,7 @@ virAdmConnectOpen(const char *name, unsigned int flags)
6ae9ed
     char *alias = NULL;
6ae9ed
     virAdmConnectPtr conn = NULL;
6ae9ed
     virConfPtr conf = NULL;
6ae9ed
+    char *uristr = NULL;
6ae9ed
 
6ae9ed
     if (virAdmInitialize() < 0)
6ae9ed
         goto error;
6ae9ed
@@ -219,14 +229,24 @@ virAdmConnectOpen(const char *name, unsigned int flags)
6ae9ed
     if (virConfLoadConfig(&conf, "libvirt-admin.conf") < 0)
6ae9ed
         goto error;
6ae9ed
 
6ae9ed
-    if (!name && !(name = virAdmGetDefaultURI(conf)))
6ae9ed
-        goto error;
6ae9ed
+    if (name) {
6ae9ed
+        if (VIR_STRDUP(uristr, name) < 0)
6ae9ed
+            goto error;
6ae9ed
+    } else {
6ae9ed
+        if (virAdmGetDefaultURI(conf, &uristr) < 0)
6ae9ed
+            goto error;
6ae9ed
+    }
6ae9ed
 
6ae9ed
     if ((!(flags & VIR_CONNECT_NO_ALIASES) &&
6ae9ed
-         virURIResolveAlias(conf, name, &alias) < 0))
6ae9ed
+         virURIResolveAlias(conf, uristr, &alias) < 0))
6ae9ed
         goto error;
6ae9ed
 
6ae9ed
-    if (!(conn->uri = virURIParse(alias ? alias : name)))
6ae9ed
+    if (alias) {
6ae9ed
+        VIR_FREE(uristr);
6ae9ed
+        uristr = alias;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    if (!(conn->uri = virURIParse(uristr)))
6ae9ed
         goto error;
6ae9ed
 
6ae9ed
     if (!(sock_path = getSocketPath(conn->uri)))
6ae9ed
@@ -242,7 +262,7 @@ virAdmConnectOpen(const char *name, unsigned int flags)
6ae9ed
 
6ae9ed
  cleanup:
6ae9ed
     VIR_FREE(sock_path);
6ae9ed
-    VIR_FREE(alias);
6ae9ed
+    VIR_FREE(uristr);
6ae9ed
     virConfFree(conf);
6ae9ed
     return conn;
6ae9ed
 
6ae9ed
diff --git a/src/libvirt.c b/src/libvirt.c
6ae9ed
index f26eec4..215701f 100644
6ae9ed
--- a/src/libvirt.c
6ae9ed
+++ b/src/libvirt.c
6ae9ed
@@ -903,22 +903,32 @@ virGetVersion(unsigned long *libVer, const char *type ATTRIBUTE_UNUSED,
6ae9ed
 
6ae9ed
 static int
6ae9ed
 virConnectGetDefaultURI(virConfPtr conf,
6ae9ed
-                        const char **name)
6ae9ed
+                        char **name)
6ae9ed
 {
6ae9ed
     int ret = -1;
6ae9ed
     virConfValuePtr value = NULL;
6ae9ed
     const char *defname = virGetEnvBlockSUID("LIBVIRT_DEFAULT_URI");
6ae9ed
+
6ae9ed
     if (defname && *defname) {
6ae9ed
         VIR_DEBUG("Using LIBVIRT_DEFAULT_URI '%s'", defname);
6ae9ed
-        *name = defname;
6ae9ed
-    } else if ((value = virConfGetValue(conf, "uri_default"))) {
6ae9ed
-        if (value->type != VIR_CONF_STRING) {
6ae9ed
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
6ae9ed
-                           _("Expected a string for 'uri_default' config parameter"));
6ae9ed
+        if (VIR_STRDUP(*name, defname) < 0)
6ae9ed
             goto cleanup;
6ae9ed
+    } else {
6ae9ed
+        if ((value = virConfGetValue(conf, "uri_default"))) {
6ae9ed
+            if (value->type != VIR_CONF_STRING) {
6ae9ed
+                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
6ae9ed
+                               _("Expected a string for 'uri_default' config "
6ae9ed
+                                 "parameter"));
6ae9ed
+                goto cleanup;
6ae9ed
+            }
6ae9ed
+
6ae9ed
+            VIR_FREE(*name);
6ae9ed
+            if (VIR_STRDUP(*name, value->str) < 0)
6ae9ed
+                goto cleanup;
6ae9ed
         }
6ae9ed
-        VIR_DEBUG("Using config file uri '%s'", value->str);
6ae9ed
-        *name = value->str;
6ae9ed
+
6ae9ed
+        if (*name)
6ae9ed
+            VIR_DEBUG("Using config file uri '%s'", *name);
6ae9ed
     }
6ae9ed
 
6ae9ed
     ret = 0;
6ae9ed
@@ -965,6 +975,7 @@ virConnectOpenInternal(const char *name,
6ae9ed
     int res;
6ae9ed
     virConnectPtr ret;
6ae9ed
     virConfPtr conf = NULL;
6ae9ed
+    char *uristr = NULL;
6ae9ed
 
6ae9ed
     ret = virGetConnect();
6ae9ed
     if (ret == NULL)
6ae9ed
@@ -982,54 +993,61 @@ virConnectOpenInternal(const char *name,
6ae9ed
         goto failed;
6ae9ed
     }
6ae9ed
 
6ae9ed
+    /* Convert xen -> xen:/// for back compat */
6ae9ed
+    if (name && STRCASEEQ(name, "xen"))
6ae9ed
+        name = "xen:///";
6ae9ed
+
6ae9ed
+    /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
6ae9ed
+     * former.  This allows URIs such as xen://localhost to work.
6ae9ed
+     */
6ae9ed
+    if (name && STREQ(name, "xen://"))
6ae9ed
+        name = "xen:///";
6ae9ed
+
6ae9ed
     /*
6ae9ed
      * If no URI is passed, then check for an environment string if not
6ae9ed
      * available probe the compiled in drivers to find a default hypervisor
6ae9ed
      * if detectable.
6ae9ed
      */
6ae9ed
-    if (!name &&
6ae9ed
-        virConnectGetDefaultURI(conf, &name) < 0)
6ae9ed
-        goto failed;
6ae9ed
-
6ae9ed
     if (name) {
6ae9ed
-        char *alias = NULL;
6ae9ed
-        /* Convert xen -> xen:/// for back compat */
6ae9ed
-        if (STRCASEEQ(name, "xen"))
6ae9ed
-            name = "xen:///";
6ae9ed
+        if (VIR_STRDUP(uristr, name) < 0)
6ae9ed
+            goto failed;
6ae9ed
+    } else {
6ae9ed
+        if (virConnectGetDefaultURI(conf, &uristr) < 0)
6ae9ed
+            goto failed;
6ae9ed
+    }
6ae9ed
 
6ae9ed
-        /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
6ae9ed
-         * former.  This allows URIs such as xen://localhost to work.
6ae9ed
-         */
6ae9ed
-        if (STREQ(name, "xen://"))
6ae9ed
-            name = "xen:///";
6ae9ed
+    if (uristr) {
6ae9ed
+        char *alias = NULL;
6ae9ed
 
6ae9ed
         if (!(flags & VIR_CONNECT_NO_ALIASES) &&
6ae9ed
-            virURIResolveAlias(conf, name, &alias) < 0)
6ae9ed
+            virURIResolveAlias(conf, uristr, &alias) < 0)
6ae9ed
             goto failed;
6ae9ed
 
6ae9ed
-        if (!(ret->uri = virURIParse(alias ? alias : name))) {
6ae9ed
+        if (alias) {
6ae9ed
+            VIR_FREE(uristr);
6ae9ed
+            uristr = alias;
6ae9ed
+        }
6ae9ed
+
6ae9ed
+        if (!(ret->uri = virURIParse(uristr))) {
6ae9ed
             VIR_FREE(alias);
6ae9ed
             goto failed;
6ae9ed
         }
6ae9ed
 
6ae9ed
-        VIR_DEBUG("name \"%s\" to URI components:\n"
6ae9ed
+        VIR_DEBUG("Split \"%s\" to URI components:\n"
6ae9ed
                   "  scheme %s\n"
6ae9ed
                   "  server %s\n"
6ae9ed
                   "  user %s\n"
6ae9ed
                   "  port %d\n"
6ae9ed
                   "  path %s",
6ae9ed
-                  alias ? alias : name,
6ae9ed
+                  uristr,
6ae9ed
                   NULLSTR(ret->uri->scheme), NULLSTR(ret->uri->server),
6ae9ed
                   NULLSTR(ret->uri->user), ret->uri->port,
6ae9ed
                   NULLSTR(ret->uri->path));
6ae9ed
 
6ae9ed
-        if (virConnectCheckURIMissingSlash(alias ? alias : name,
6ae9ed
+        if (virConnectCheckURIMissingSlash(uristr,
6ae9ed
                                            ret->uri) < 0) {
6ae9ed
-            VIR_FREE(alias);
6ae9ed
             goto failed;
6ae9ed
         }
6ae9ed
-
6ae9ed
-        VIR_FREE(alias);
6ae9ed
     } else {
6ae9ed
         VIR_DEBUG("no name, allowing driver auto-select");
6ae9ed
     }
6ae9ed
@@ -1114,10 +1132,12 @@ virConnectOpenInternal(const char *name,
6ae9ed
     }
6ae9ed
 
6ae9ed
     virConfFree(conf);
6ae9ed
+    VIR_FREE(uristr);
6ae9ed
 
6ae9ed
     return ret;
6ae9ed
 
6ae9ed
  failed:
6ae9ed
+    VIR_FREE(uristr);
6ae9ed
     virConfFree(conf);
6ae9ed
     virObjectUnref(ret);
6ae9ed
 
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed