[PATCH 04/13] qemu: domain: Store passthrough environment variables in a struct

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Previously they were stored in two separate arrays. This way it's
obvious when referencing the same one.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/qemu/qemu_command.c |  4 ++--
 src/qemu/qemu_domain.c  | 45 +++++++++++++++++++++++------------------
 src/qemu/qemu_domain.h  |  9 +++++++--
 3 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 4381ea7d8b..4bcb5a3146 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -10638,8 +10638,8 @@ qemuBuildCommandLine(virQEMUDriver *driver,
         for (i = 0; i < qemuxmlns->num_args; i++)
             virCommandAddArg(cmd, qemuxmlns->args[i]);
         for (i = 0; i < qemuxmlns->num_env; i++)
-            virCommandAddEnvPair(cmd, qemuxmlns->env_name[i],
-                                 NULLSTR_EMPTY(qemuxmlns->env_value[i]));
+            virCommandAddEnvPair(cmd, qemuxmlns->env[i].name,
+                                 NULLSTR_EMPTY(qemuxmlns->env[i].value));
     }

     if (qemuBuildSeccompSandboxCommandLine(cmd, cfg, qemuCaps) < 0)
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 6f8c93ea0c..21668cae4c 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -3319,12 +3319,17 @@ virDomainXMLPrivateDataCallbacks virQEMUDriverPrivateDataCallbacks = {
 static void
 qemuDomainXmlNsDefFree(qemuDomainXmlNsDef *def)
 {
+    size_t i;
+
     if (!def)
         return;

+    for (i = 0; i < def->num_env; i++) {
+        g_free(def->env[i].name);
+        g_free(def->env[i].value);
+    }
+
     virStringListFreeCount(def->args, def->num_args);
-    virStringListFreeCount(def->env_name, def->num_env);
-    virStringListFreeCount(def->env_value, def->num_env);
     virStringListFreeCount(def->capsadd, def->ncapsadd);
     virStringListFreeCount(def->capsdel, def->ncapsdel);

@@ -3372,15 +3377,21 @@ qemuDomainDefNamespaceParseCommandlineArgs(qemuDomainXmlNsDef *nsdef,


 static int
-qemuDomainDefNamespaceParseCommandlineEnvNameValidate(const char *envname)
+qemuDomainDefNamespaceParseCommandlineEnvValidate(qemuDomainXmlNsEnvTuple *env)
 {
-    if (!g_ascii_isalpha(envname[0]) && envname[0] != '_') {
+    if (!env->name) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("No qemu environment name specified"));
+        return -1;
+    }
+
+    if (!g_ascii_isalpha(env->name[0]) && env->name[0] != '_') {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Invalid environment name, it must begin with a letter or underscore"));
         return -1;
     }

-    if (strspn(envname, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(envname)) {
+    if (strspn(env->name, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(env->name)) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Invalid environment name, it must contain only alphanumerics and underscore"));
         return -1;
@@ -3404,22 +3415,17 @@ qemuDomainDefNamespaceParseCommandlineEnv(qemuDomainXmlNsDef *nsdef,
     if (nnodes == 0)
         return 0;

-    nsdef->env_name = g_new0(char *, nnodes);
-    nsdef->env_value = g_new0(char *, nnodes);
+    nsdef->env = g_new0(qemuDomainXmlNsEnvTuple, nnodes);

     for (i = 0; i < nnodes; i++) {
-        if (!(nsdef->env_name[nsdef->num_env] = virXMLPropString(nodes[i], "name"))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("No qemu environment name specified"));
-            return -1;
-        }
+        qemuDomainXmlNsEnvTuple *env = nsdef->env + i;

-        if (qemuDomainDefNamespaceParseCommandlineEnvNameValidate(nsdef->env_name[nsdef->num_env]) < 0)
-            return -1;
-
-        nsdef->env_value[nsdef->num_env] = virXMLPropString(nodes[i], "value");
-        /* a NULL value for command is allowed, since it might be empty */
+        env->name = virXMLPropString(nodes[i], "name");
+        env->value = virXMLPropString(nodes[i], "value");
         nsdef->num_env++;
+
+        if (qemuDomainDefNamespaceParseCommandlineEnvValidate(env) < 0)
+            return -1;
     }

     return 0;
@@ -3513,9 +3519,8 @@ qemuDomainDefNamespaceFormatXMLCommandline(virBuffer *buf,
         virBufferEscapeString(buf, "<qemu:arg value='%s'/>\n",
                               cmd->args[i]);
     for (i = 0; i < cmd->num_env; i++) {
-        virBufferAsprintf(buf, "<qemu:env name='%s'", cmd->env_name[i]);
-        if (cmd->env_value[i])
-            virBufferEscapeString(buf, " value='%s'", cmd->env_value[i]);
+        virBufferAsprintf(buf, "<qemu:env name='%s'", cmd->env[i].name);
+        virBufferEscapeString(buf, " value='%s'", cmd->env[i].value);
         virBufferAddLit(buf, "/>\n");
     }

diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index acf6ca5ab6..5f2814271d 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -457,14 +457,19 @@ struct _qemuDomainSaveCookie {

 G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuDomainSaveCookie, virObjectUnref);

+typedef struct _qemuDomainXmlNsEnvTuple qemuDomainXmlNsEnvTuple;
+struct _qemuDomainXmlNsEnvTuple {
+    char *name;
+    char *value;
+};
+
 typedef struct _qemuDomainXmlNsDef qemuDomainXmlNsDef;
 struct _qemuDomainXmlNsDef {
     size_t num_args;
     char **args;

     unsigned int num_env;
-    char **env_name;
-    char **env_value;
+    qemuDomainXmlNsEnvTuple *env;

     size_t ncapsadd;
     char **capsadd;
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux