domain_conf: move boot timeouts check to domain_validate.c

This patch creates a new function, virDomainDefBootValidate(), to host
the validation of boot menu timeout and rebootTimeout outside of parse
time. The checks in virDomainDefParseBootXML() were changed to throw
VIR_ERR_XML_ERROR in case of parse error of those values.

In an attempt to alleviate the amount of code being stacked inside
domain_conf.c, let's put this new function in a new domain_validate.c
file that will be used to place these validations.

Suggested-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Daniel Henrique Barboza 2020-12-02 16:48:34 -03:00
parent 0ddebdb42e
commit 7ad9162961
6 changed files with 92 additions and 11 deletions

View File

@ -26,6 +26,7 @@
@SRCDIR@src/conf/domain_capabilities.c
@SRCDIR@src/conf/domain_conf.c
@SRCDIR@src/conf/domain_event.c
@SRCDIR@src/conf/domain_validate.c
@SRCDIR@src/conf/interface_conf.c
@SRCDIR@src/conf/netdev_bandwidth_conf.c
@SRCDIR@src/conf/netdev_vlan_conf.c

View File

@ -33,6 +33,7 @@
#include "datatypes.h"
#include "domain_addr.h"
#include "domain_conf.h"
#include "domain_validate.h"
#include "snapshot_conf.h"
#include "viralloc.h"
#include "virxml.h"
@ -7346,6 +7347,9 @@ virDomainDefValidateInternal(const virDomainDef *def,
if (virDomainDefCputuneValidate(def) < 0)
return -1;
if (virDomainDefBootValidate(def) < 0)
return -1;
if (virDomainNumaDefValidate(def->numa) < 0)
return -1;
@ -18872,11 +18876,9 @@ virDomainDefParseBootXML(xmlXPathContextPtr ctxt,
tmp = virXMLPropString(node, "timeout");
if (tmp && def->os.bootmenu == VIR_TRISTATE_BOOL_YES) {
if (virStrToLong_uip(tmp, NULL, 0, &def->os.bm_timeout) < 0 ||
def->os.bm_timeout > 65535) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("invalid value for boot menu timeout, "
"must be in range [0,65535]"));
if (virStrToLong_uip(tmp, NULL, 0, &def->os.bm_timeout) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("invalid value for boot menu timeout"));
return -1;
}
def->os.bm_timeout_set = true;
@ -18897,11 +18899,9 @@ virDomainDefParseBootXML(xmlXPathContextPtr ctxt,
if (tmp) {
/* that was really just for the check if it is there */
if (virStrToLong_i(tmp, NULL, 0, &def->os.bios.rt_delay) < 0 ||
def->os.bios.rt_delay < -1 || def->os.bios.rt_delay > 65535) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("invalid value for rebootTimeout, "
"must be in range [-1,65535]"));
if (virStrToLong_i(tmp, NULL, 0, &def->os.bios.rt_delay) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("invalid value for rebootTimeout"));
return -1;
}
def->os.bios.rt_set = true;

View File

@ -0,0 +1,51 @@
/*
* domain_validate.c: domain general validation functions
*
* Copyright IBM Corp, 2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "domain_validate.h"
#include "domain_conf.h"
#include "virlog.h"
#include "virutil.h"
#define VIR_FROM_THIS VIR_FROM_DOMAIN
VIR_LOG_INIT("conf.domain_validate");
int
virDomainDefBootValidate(const virDomainDef *def)
{
if (def->os.bm_timeout_set && def->os.bm_timeout > 65535) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("invalid value for boot menu timeout, "
"must be in range [0,65535]"));
return -1;
}
if (def->os.bios.rt_set &&
(def->os.bios.rt_delay < -1 || def->os.bios.rt_delay > 65535)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("invalid value for rebootTimeout, "
"must be in range [-1,65535]"));
return -1;
}
return 0;
}

View File

@ -0,0 +1,27 @@
/*
* domain_validate.h: domain general validation functions
*
* Copyright IBM Corp, 2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <glib-object.h>
#include "domain_conf.h"
int virDomainDefBootValidate(const virDomainDef *def);

View File

@ -14,6 +14,7 @@ domain_conf_sources = [
'domain_capabilities.c',
'domain_conf.c',
'domain_nwfilter.c',
'domain_validate.c',
'moment_conf.c',
'numa_conf.c',
'snapshot_conf.c',

View File

@ -1006,7 +1006,8 @@ mymain(void)
DO_TEST("boot-menu-enable-with-timeout",
QEMU_CAPS_SPLASH_TIMEOUT);
DO_TEST_PARSE_ERROR("boot-menu-enable-with-timeout", NONE);
DO_TEST_PARSE_ERROR("boot-menu-enable-with-timeout-invalid", NONE);
DO_TEST_PARSE_ERROR("boot-menu-enable-with-timeout-invalid",
QEMU_CAPS_SPLASH_TIMEOUT);
DO_TEST("boot-menu-disable", NONE);
DO_TEST("boot-menu-disable-drive", NONE);
DO_TEST_PARSE_ERROR("boot-dev+order",