mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-23 06:05:27 +00:00
qemu: Extract parsing of qemu namespace env vars into separate function
Simplify the main function by splitting out how we parse the extra passthrough environment variables. Note that the validation function checks that the first letter must be a character or underscore which makes the check whether the name is redundant. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
3f76419a05
commit
a858b77754
@ -3139,6 +3139,62 @@ qemuDomainDefNamespaceParseCommandlineArgs(qemuDomainXmlNsDefPtr nsdef,
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
qemuDomainDefNamespaceParseCommandlineEnvNameValidate(const char *envname)
|
||||
{
|
||||
if (!c_isalpha(envname[0]) && envname[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)) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("Invalid environment name, it must contain only alphanumerics and underscore"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
qemuDomainDefNamespaceParseCommandlineEnv(qemuDomainXmlNsDefPtr nsdef,
|
||||
xmlXPathContextPtr ctxt)
|
||||
{
|
||||
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
|
||||
ssize_t nnodes;
|
||||
size_t i;
|
||||
|
||||
if ((nnodes = virXPathNodeSet("./qemu:commandline/qemu:env", ctxt, &nodes)) < 0)
|
||||
return -1;
|
||||
|
||||
if (nnodes == 0)
|
||||
return 0;
|
||||
|
||||
if (VIR_ALLOC_N(nsdef->env_name, nnodes) < 0 ||
|
||||
VIR_ALLOC_N(nsdef->env_value, nnodes) < 0)
|
||||
return -1;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 */
|
||||
nsdef->num_env++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
|
||||
xmlNodePtr root ATTRIBUTE_UNUSED,
|
||||
@ -3147,9 +3203,6 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
|
||||
{
|
||||
qemuDomainXmlNsDefPtr cmd = NULL;
|
||||
bool uses_qemu_ns = false;
|
||||
xmlNodePtr *nodes = NULL;
|
||||
int n;
|
||||
size_t i;
|
||||
|
||||
if (xmlXPathRegisterNs(ctxt, BAD_CAST "qemu", BAD_CAST QEMU_NAMESPACE_HREF) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
@ -3161,58 +3214,13 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
|
||||
if (VIR_ALLOC(cmd) < 0)
|
||||
return -1;
|
||||
|
||||
if (qemuDomainDefNamespaceParseCommandlineArgs(cmd, ctxt) < 0)
|
||||
if (qemuDomainDefNamespaceParseCommandlineArgs(cmd, ctxt) < 0 ||
|
||||
qemuDomainDefNamespaceParseCommandlineEnv(cmd, ctxt) < 0)
|
||||
goto error;
|
||||
|
||||
if (cmd->num_args > 0)
|
||||
if (cmd->num_args > 0 || cmd->num_env > 0)
|
||||
uses_qemu_ns = true;
|
||||
|
||||
/* now handle the extra environment variables */
|
||||
n = virXPathNodeSet("./qemu:commandline/qemu:env", ctxt, &nodes);
|
||||
if (n < 0)
|
||||
goto error;
|
||||
uses_qemu_ns |= n > 0;
|
||||
|
||||
if (n && VIR_ALLOC_N(cmd->env_name, n) < 0)
|
||||
goto error;
|
||||
|
||||
if (n && VIR_ALLOC_N(cmd->env_value, n) < 0)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
char *tmp;
|
||||
|
||||
tmp = virXMLPropString(nodes[i], "name");
|
||||
if (tmp == NULL) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
"%s", _("No qemu environment name specified"));
|
||||
goto error;
|
||||
}
|
||||
if (tmp[0] == '\0') {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
"%s", _("Empty qemu environment name specified"));
|
||||
goto error;
|
||||
}
|
||||
if (!c_isalpha(tmp[0]) && tmp[0] != '_') {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
"%s", _("Invalid environment name, it must begin with a letter or underscore"));
|
||||
goto error;
|
||||
}
|
||||
if (strspn(tmp, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(tmp)) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
"%s", _("Invalid environment name, it must contain only alphanumerics and underscore"));
|
||||
goto error;
|
||||
}
|
||||
|
||||
cmd->env_name[cmd->num_env] = tmp;
|
||||
|
||||
cmd->env_value[cmd->num_env] = virXMLPropString(nodes[i], "value");
|
||||
/* a NULL value for command is allowed, since it might be empty */
|
||||
cmd->num_env++;
|
||||
}
|
||||
|
||||
VIR_FREE(nodes);
|
||||
|
||||
if (uses_qemu_ns)
|
||||
*data = cmd;
|
||||
else
|
||||
@ -3221,7 +3229,6 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
|
||||
return 0;
|
||||
|
||||
error:
|
||||
VIR_FREE(nodes);
|
||||
qemuDomainDefNamespaceFree(cmd);
|
||||
return -1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user