qemu: domain: Store passthrough environment variables in a struct

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

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-06-21 14:43:01 +02:00
parent 8c85d51926
commit b7b8ea965b
3 changed files with 35 additions and 24 deletions

View File

@ -10638,8 +10638,8 @@ qemuBuildCommandLine(virQEMUDriver *driver,
for (i = 0; i < qemuxmlns->num_args; i++) for (i = 0; i < qemuxmlns->num_args; i++)
virCommandAddArg(cmd, qemuxmlns->args[i]); virCommandAddArg(cmd, qemuxmlns->args[i]);
for (i = 0; i < qemuxmlns->num_env; i++) for (i = 0; i < qemuxmlns->num_env; i++)
virCommandAddEnvPair(cmd, qemuxmlns->env_name[i], virCommandAddEnvPair(cmd, qemuxmlns->env[i].name,
NULLSTR_EMPTY(qemuxmlns->env_value[i])); NULLSTR_EMPTY(qemuxmlns->env[i].value));
} }
if (qemuBuildSeccompSandboxCommandLine(cmd, cfg, qemuCaps) < 0) if (qemuBuildSeccompSandboxCommandLine(cmd, cfg, qemuCaps) < 0)

View File

@ -3319,12 +3319,18 @@ virDomainXMLPrivateDataCallbacks virQEMUDriverPrivateDataCallbacks = {
static void static void
qemuDomainXmlNsDefFree(qemuDomainXmlNsDef *def) qemuDomainXmlNsDefFree(qemuDomainXmlNsDef *def)
{ {
size_t i;
if (!def) if (!def)
return; return;
for (i = 0; i < def->num_env; i++) {
g_free(def->env[i].name);
g_free(def->env[i].value);
}
g_free(def->env);
virStringListFreeCount(def->args, def->num_args); 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->capsadd, def->ncapsadd);
virStringListFreeCount(def->capsdel, def->ncapsdel); virStringListFreeCount(def->capsdel, def->ncapsdel);
@ -3372,15 +3378,21 @@ qemuDomainDefNamespaceParseCommandlineArgs(qemuDomainXmlNsDef *nsdef,
static int 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", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid environment name, it must begin with a letter or underscore")); _("Invalid environment name, it must begin with a letter or underscore"));
return -1; return -1;
} }
if (strspn(envname, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(envname)) { if (strspn(env->name, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(env->name)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid environment name, it must contain only alphanumerics and underscore")); _("Invalid environment name, it must contain only alphanumerics and underscore"));
return -1; return -1;
@ -3404,22 +3416,17 @@ qemuDomainDefNamespaceParseCommandlineEnv(qemuDomainXmlNsDef *nsdef,
if (nnodes == 0) if (nnodes == 0)
return 0; return 0;
nsdef->env_name = g_new0(char *, nnodes); nsdef->env = g_new0(qemuDomainXmlNsEnvTuple, nnodes);
nsdef->env_value = g_new0(char *, nnodes);
for (i = 0; i < nnodes; i++) { for (i = 0; i < nnodes; i++) {
if (!(nsdef->env_name[nsdef->num_env] = virXMLPropString(nodes[i], "name"))) { qemuDomainXmlNsEnvTuple *env = nsdef->env + i;
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("No qemu environment name specified"));
return -1;
}
if (qemuDomainDefNamespaceParseCommandlineEnvNameValidate(nsdef->env_name[nsdef->num_env]) < 0) env->name = virXMLPropString(nodes[i], "name");
return -1; env->value = virXMLPropString(nodes[i], "value");
nsdef->env_value[nsdef->num_env] = virXMLPropString(nodes[i], "value");
/* a NULL value for command is allowed, since it might be empty */
nsdef->num_env++; nsdef->num_env++;
if (qemuDomainDefNamespaceParseCommandlineEnvValidate(env) < 0)
return -1;
} }
return 0; return 0;
@ -3513,9 +3520,8 @@ qemuDomainDefNamespaceFormatXMLCommandline(virBuffer *buf,
virBufferEscapeString(buf, "<qemu:arg value='%s'/>\n", virBufferEscapeString(buf, "<qemu:arg value='%s'/>\n",
cmd->args[i]); cmd->args[i]);
for (i = 0; i < cmd->num_env; i++) { for (i = 0; i < cmd->num_env; i++) {
virBufferAsprintf(buf, "<qemu:env name='%s'", cmd->env_name[i]); virBufferAsprintf(buf, "<qemu:env name='%s'", cmd->env[i].name);
if (cmd->env_value[i]) virBufferEscapeString(buf, " value='%s'", cmd->env[i].value);
virBufferEscapeString(buf, " value='%s'", cmd->env_value[i]);
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
} }

View File

@ -457,14 +457,19 @@ struct _qemuDomainSaveCookie {
G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuDomainSaveCookie, virObjectUnref); G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuDomainSaveCookie, virObjectUnref);
typedef struct _qemuDomainXmlNsEnvTuple qemuDomainXmlNsEnvTuple;
struct _qemuDomainXmlNsEnvTuple {
char *name;
char *value;
};
typedef struct _qemuDomainXmlNsDef qemuDomainXmlNsDef; typedef struct _qemuDomainXmlNsDef qemuDomainXmlNsDef;
struct _qemuDomainXmlNsDef { struct _qemuDomainXmlNsDef {
size_t num_args; size_t num_args;
char **args; char **args;
unsigned int num_env; unsigned int num_env;
char **env_name; qemuDomainXmlNsEnvTuple *env;
char **env_value;
size_t ncapsadd; size_t ncapsadd;
char **capsadd; char **capsadd;