[libvirt] [PATCH] tests: Lower stack usage below 4096 bytes
by Matthias Bolte
Make virtTestLoadFile allocate the buffer to read the file into.
Fix logic error in virtTestLoadFile, stop reading on the an empty line.
Use virFileReadLimFD in virtTestCaptureProgramOutput.
---
m4/virt-compile-warnings.m4 | 10 ++---
tests/commandhelper.c | 5 +-
tests/commandtest.c | 14 ++----
tests/conftest.c | 29 +++++++++----
tests/cputest.c | 66 ++++++++++++++++++-------------
tests/esxutilstest.c | 2 +
tests/interfacexml2xmltest.c | 33 ++++++++++------
tests/networkxml2xmltest.c | 51 ++++++++++++++---------
tests/nodedevxml2xmltest.c | 34 ++++++++++------
tests/nodeinfotest.c | 67 ++++++++++++++++++++-----------
tests/nwfilterxml2xmltest.c | 54 +++++++++++++++----------
tests/qemuargv2xmltest.c | 43 ++++++++++++--------
tests/qemuhelptest.c | 13 +++---
tests/qemuxml2argvtest.c | 56 ++++++++++++++++----------
tests/qemuxml2xmltest.c | 43 +++++++++++---------
tests/sexpr2xmltest.c | 51 ++++++++++++++---------
tests/storagepoolxml2xmltest.c | 51 ++++++++++++++---------
tests/storagevolxml2xmltest.c | 65 ++++++++++++++++++-------------
tests/testutils.c | 69 ++++++++++++++------------------
tests/testutils.h | 8 +---
tests/virshtest.c | 65 +++++++++++++-----------------
tests/vmx2xmltest.c | 45 ++++++++++++---------
tests/xencapstest.c | 38 ++++++++---------
tests/xmconfigtest.c | 85 +++++++++++++++++++++++----------------
tests/xml2sexprtest.c | 53 +++++++++++++++----------
tests/xml2vmxtest.c | 45 ++++++++++++---------
26 files changed, 624 insertions(+), 471 deletions(-)
diff --git a/m4/virt-compile-warnings.m4 b/m4/virt-compile-warnings.m4
index 295fd9b..305036f 100644
--- a/m4/virt-compile-warnings.m4
+++ b/m4/virt-compile-warnings.m4
@@ -88,12 +88,10 @@ AC_DEFUN([LIBVIRT_COMPILE_WARNINGS],[
# that one off, so we need to manually enable this again
gl_WARN_ADD([-Wjump-misses-init])
- # This should be < 256 really, but with PATH_MAX everywhere
- # we have doom, even with 4096. In fact we have some functions
- # with several PATH_MAX sized variables :-( We should kill off
- # all PATH_MAX usage and then lower this limit
- gl_WARN_ADD([-Wframe-larger-than=65700])
- dnl gl_WARN_ADD([-Wframe-larger-than=4096])
+ # This should be < 256 really. Currently we're down to 4096,
+ # but using 1024 bytes sized buffers (mostly for virStrerror)
+ # stops us from going down further
+ gl_WARN_ADD([-Wframe-larger-than=4096])
dnl gl_WARN_ADD([-Wframe-larger-than=256])
# Extra special flags
diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index 46c00f4..99ba22e 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -99,8 +99,8 @@ int main(int argc, char **argv) {
}
fprintf(log, "DAEMON:%s\n", getpgrp() == getsid(0) ? "yes" : "no");
- char cwd[1024];
- if (!getcwd(cwd, sizeof(cwd)))
+ char *cwd = NULL;
+ if (!(cwd = getcwd(NULL, 0)))
return EXIT_FAILURE;
if (strlen(cwd) > strlen("/commanddata") &&
STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata"))
@@ -137,5 +137,6 @@ int main(int argc, char **argv) {
return EXIT_SUCCESS;
error:
+ free(cwd);
return EXIT_FAILURE;
}
diff --git a/tests/commandtest.c b/tests/commandtest.c
index c313a2c..a728d10 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -47,21 +47,18 @@ mymain(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
#else
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static int checkoutput(const char *testname)
{
int ret = -1;
- char cwd[1024];
char *expectname = NULL;
char *expectlog = NULL;
char *actualname = NULL;
char *actuallog = NULL;
- if (!getcwd(cwd, sizeof(cwd)))
- return -1;
-
if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir,
testname) < 0)
goto cleanup;
@@ -545,12 +542,12 @@ cleanup:
static int test15(const void *unused ATTRIBUTE_UNUSED)
{
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
- char *cwd = NULL;
+ char *wd = NULL;
int ret = -1;
- if (virAsprintf(&cwd, "%s/commanddata", abs_srcdir) < 0)
+ if (virAsprintf(&wd, "%s/commanddata", abs_srcdir) < 0)
goto cleanup;
- virCommandSetWorkingDirectory(cmd, cwd);
+ virCommandSetWorkingDirectory(cmd, wd);
if (virCommandRun(cmd, NULL) < 0) {
virErrorPtr err = virGetLastError();
@@ -561,7 +558,7 @@ static int test15(const void *unused ATTRIBUTE_UNUSED)
ret = checkoutput("test15");
cleanup:
- VIR_FREE(cwd);
+ VIR_FREE(wd);
virCommandFree(cmd);
return ret;
@@ -757,7 +754,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
int fd;
abs_srcdir = getenv("abs_srcdir");
diff --git a/tests/conftest.c b/tests/conftest.c
index a7977bb..6514f4d 100644
--- a/tests/conftest.c
+++ b/tests/conftest.c
@@ -6,32 +6,43 @@
#include <string.h>
#include <errno.h>
#include "conf.h"
+#include "memory.h"
-int main(int argc, char **argv) {
- int ret;
+int main(int argc, char **argv)
+{
+ int ret, exit_code = EXIT_FAILURE;
virConfPtr conf;
int len = 10000;
- char buffer[10000];
+ char *buffer = NULL;
if (argc != 2) {
fprintf(stderr, "Usage: %s conf_file\n", argv[0]);
- exit(EXIT_FAILURE);
+ goto cleanup;
}
+ if (VIR_ALLOC_N(buffer, len) < 0) {
+ fprintf(stderr, "out of memory\n");
+ goto cleanup;
+ }
conf = virConfReadFile(argv[1], 0);
if (conf == NULL) {
fprintf(stderr, "Failed to process %s\n", argv[1]);
- exit(EXIT_FAILURE);
+ goto cleanup;
}
- ret = virConfWriteMem(&buffer[0], &len, conf);
+ ret = virConfWriteMem(buffer, &len, conf);
if (ret < 0) {
fprintf(stderr, "Failed to serialize %s back\n", argv[1]);
- exit(EXIT_FAILURE);
+ goto cleanup;
}
virConfFree(conf);
if (fwrite(buffer, 1, len, stdout) != len) {
fprintf(stderr, "Write failed: %s\n", strerror (errno));
- exit(EXIT_FAILURE);
+ goto cleanup;
}
- exit(EXIT_SUCCESS);
+
+ exit_code = EXIT_SUCCESS;
+
+cleanup:
+ VIR_FREE(buffer);
+ return exit_code;
}
diff --git a/tests/cputest.c b/tests/cputest.c
index bd06958..831fa81 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -40,11 +40,11 @@
#include "cpu/cpu_map.h"
static const char *progname;
+static char cwd[PATH_MAX];
static const char *abs_srcdir;
static const char *abs_top_srcdir;
#define VIR_FROM_THIS VIR_FROM_CPU
-#define MAX_FILE 4096
enum compResultShadow {
ERROR = VIR_CPU_COMPARE_ERROR,
@@ -91,14 +91,13 @@ struct data {
static virCPUDefPtr
cpuTestLoadXML(const char *arch, const char *name)
{
- char xml[PATH_MAX];
+ char *xml = NULL;
xmlDocPtr doc = NULL;
xmlXPathContextPtr ctxt = NULL;
virCPUDefPtr cpu = NULL;
- snprintf(xml, PATH_MAX,
- "%s/cputestdata/%s-%s.xml",
- abs_srcdir, arch, name);
+ if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml", abs_srcdir, arch, name) < 0)
+ goto cleanup;
if (!(doc = virXMLParseFile(xml)) ||
!(ctxt = xmlXPathNewContext(doc)))
@@ -110,6 +109,7 @@ cpuTestLoadXML(const char *arch, const char *name)
cleanup:
xmlXPathFreeContext(ctxt);
xmlFreeDoc(doc);
+ free(xml);
return cpu;
}
@@ -119,7 +119,7 @@ cpuTestLoadMultiXML(const char *arch,
const char *name,
unsigned int *count)
{
- char xml[PATH_MAX];
+ char *xml = NULL;
xmlDocPtr doc = NULL;
xmlXPathContextPtr ctxt = NULL;
xmlNodePtr *nodes = NULL;
@@ -127,9 +127,8 @@ cpuTestLoadMultiXML(const char *arch,
int n;
int i;
- snprintf(xml, PATH_MAX,
- "%s/cputestdata/%s-%s.xml",
- abs_srcdir, arch, name);
+ if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml", abs_srcdir, arch, name) < 0)
+ goto cleanup;
if (!(doc = virXMLParseFile(xml)) ||
!(ctxt = xmlXPathNewContext(doc)))
@@ -151,6 +150,7 @@ cpuTestLoadMultiXML(const char *arch,
*count = n;
cleanup:
+ free(xml);
free(nodes);
xmlXPathFreeContext(ctxt);
xmlFreeDoc(doc);
@@ -172,17 +172,16 @@ cpuTestCompareXML(const char *arch,
const virCPUDefPtr cpu,
const char *name)
{
- char xml[PATH_MAX];
- char expected[MAX_FILE];
- char *expectedPtr = &(expected[0]);
+ char *xml = NULL;
+ char *expected = NULL;
char *actual = NULL;
int ret = -1;
- snprintf(xml, PATH_MAX,
- "%s/cputestdata/%s-%s.xml",
- abs_srcdir, arch, name);
+ if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml",
+ abs_srcdir, arch, name) < 0)
+ goto cleanup;
- if (virtTestLoadFile(xml, &expectedPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &expected) < 0)
goto cleanup;
if (!(actual = virCPUDefFormat(cpu, NULL, 0)))
@@ -196,6 +195,8 @@ cpuTestCompareXML(const char *arch,
ret = 0;
cleanup:
+ free(xml);
+ free(expected);
free(actual);
return ret;
}
@@ -335,7 +336,7 @@ cpuTestBaseline(const void *arg)
virCPUDefPtr *cpus = NULL;
virCPUDefPtr baseline = NULL;
unsigned int ncpus = 0;
- char result[PATH_MAX];
+ char *result = NULL;
unsigned int i;
if (!(cpus = cpuTestLoadMultiXML(data->arch, data->name, &ncpus)))
@@ -355,7 +356,9 @@ cpuTestBaseline(const void *arg)
if (!baseline)
goto cleanup;
- snprintf(result, PATH_MAX, "%s-result", data->name);
+ if (virAsprintf(&result, "%s-result", data->name) < 0)
+ goto cleanup;
+
if (cpuTestCompareXML(data->arch, baseline, result) < 0)
goto cleanup;
@@ -384,6 +387,7 @@ cleanup:
free(cpus);
}
virCPUDefFree(baseline);
+ free(result);
return ret;
}
@@ -395,7 +399,7 @@ cpuTestUpdate(const void *arg)
int ret = -1;
virCPUDefPtr host = NULL;
virCPUDefPtr cpu = NULL;
- char result[PATH_MAX];
+ char *result = NULL;
if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
!(cpu = cpuTestLoadXML(data->arch, data->name)))
@@ -404,12 +408,15 @@ cpuTestUpdate(const void *arg)
if (cpuUpdate(cpu, host) < 0)
goto cleanup;
- snprintf(result, PATH_MAX, "%s+%s", data->host, data->name);
+ if (virAsprintf(&result, "%s+%s", data->host, data->name) < 0)
+ goto cleanup;
+
ret = cpuTestCompareXML(data->arch, cpu, result);
cleanup:
virCPUDefFree(host);
virCPUDefFree(cpu);
+ free(result);
return ret;
}
@@ -467,10 +474,10 @@ static int (*cpuTest[])(const void *) = {
static int
cpuTestRun(const char *name, const struct data *data)
{
- char label[PATH_MAX];
+ char *label = NULL;
- snprintf(label, PATH_MAX, "CPU %s(%s): %s",
- apis[data->api], data->arch, name);
+ if (virAsprintf(&label, "CPU %s(%s): %s", apis[data->api], data->arch, name) < 0)
+ return -1;
free(virtTestLogContentAndReset());
@@ -482,9 +489,12 @@ cpuTestRun(const char *name, const struct data *data)
fprintf(stderr, "\n%s\n", log);
free(log);
}
+
+ free(label);
return -1;
}
+ free(label);
return 0;
}
@@ -497,8 +507,7 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
- char map[PATH_MAX];
+ char *map = NULL;
progname = argv[0];
@@ -515,9 +524,11 @@ mymain(int argc, char **argv)
if (!abs_top_srcdir)
abs_top_srcdir = "..";
- snprintf(map, PATH_MAX, "%s/src/cpu/cpu_map.xml", abs_top_srcdir);
- if (cpuMapOverride(map) < 0)
+ if (virAsprintf(&map, "%s/src/cpu/cpu_map.xml", abs_top_srcdir) < 0 ||
+ cpuMapOverride(map) < 0) {
+ free(map);
return EXIT_FAILURE;
+ }
#define DO_TEST(arch, api, name, host, cpu, \
models, nmodels, preferred, result) \
@@ -627,6 +638,7 @@ mymain(int argc, char **argv)
DO_TEST_GUESTDATA("x86", "host", "guest", models, "qemu64", 0);
DO_TEST_GUESTDATA("x86", "host", "guest", nomodel, NULL, -1);
+ free(map);
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
diff --git a/tests/esxutilstest.c b/tests/esxutilstest.c
index b6bf87b..78761a1 100644
--- a/tests/esxutilstest.c
+++ b/tests/esxutilstest.c
@@ -276,6 +276,7 @@ testEscapeDatastoreItem(const void *data ATTRIBUTE_UNUSED)
}
}
+ VIR_FREE(escaped);
return 0;
}
@@ -317,6 +318,7 @@ testConvertWindows1252ToUTF8(const void *data ATTRIBUTE_UNUSED)
}
}
+ VIR_FREE(utf8);
return 0;
}
diff --git a/tests/interfacexml2xmltest.c b/tests/interfacexml2xmltest.c
index 094ccf5..26a19ab 100644
--- a/tests/interfacexml2xmltest.c
+++ b/tests/interfacexml2xmltest.c
@@ -14,19 +14,19 @@
#include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-static int testCompareXMLToXMLFiles(const char *xml) {
- char xmlData[MAX_FILE];
- char *xmlPtr = &(xmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *xml)
+{
+ char *xmlData = NULL;
char *actual = NULL;
int ret = -1;
virInterfaceDefPtr dev = NULL;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &xmlData) < 0)
goto fail;
if (!(dev = virInterfaceDefParseString(xmlData)))
@@ -43,16 +43,26 @@ static int testCompareXMLToXMLFiles(const char *xml) {
ret = 0;
fail:
+ free(xmlData);
free(actual);
virInterfaceDefFree(dev);
return ret;
}
-static int testCompareXMLToXMLHelper(const void *data) {
- char xml[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/interfaceschemadata/%s.xml",
- abs_srcdir, (const char*)data);
- return testCompareXMLToXMLFiles(xml);
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
+ int result = -1;
+ char *xml = NULL;
+
+ if (virAsprintf(&xml, "%s/interfaceschemadata/%s.xml",
+ abs_srcdir, (const char*)data) < 0)
+ return -1;
+
+ result = testCompareXMLToXMLFiles(xml);
+
+ free (xml);
+ return result;
}
@@ -60,7 +70,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c
index 7805548..6faeab2 100644
--- a/tests/networkxml2xmltest.c
+++ b/tests/networkxml2xmltest.c
@@ -14,23 +14,21 @@
#include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-
-static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) {
- char inXmlData[MAX_FILE];
- char *inXmlPtr = &(inXmlData[0]);
- char outXmlData[MAX_FILE];
- char *outXmlPtr = &(outXmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *inxml, const char *outxml)
+{
+ char *inXmlData = NULL;
+ char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
virNetworkDefPtr dev = NULL;
- if (virtTestLoadFile(inxml, &inXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(inxml, &inXmlData) < 0)
goto fail;
- if (virtTestLoadFile(outxml, &outXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(outxml, &outXmlData) < 0)
goto fail;
if (!(dev = virNetworkDefParseString(inXmlData)))
@@ -47,27 +45,40 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) {
ret = 0;
fail:
+ free(inXmlData);
+ free(outXmlData);
free(actual);
virNetworkDefFree(dev);
return ret;
}
-static int testCompareXMLToXMLHelper(const void *data) {
- char inxml[PATH_MAX];
- char outxml[PATH_MAX];
- snprintf(inxml, PATH_MAX, "%s/networkxml2xmlin/%s.xml",
- abs_srcdir, (const char*)data);
- snprintf(outxml, PATH_MAX, "%s/networkxml2xmlout/%s.xml",
- abs_srcdir, (const char*)data);
- return testCompareXMLToXMLFiles(inxml, outxml);
-}
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
+ int result = -1;
+ char *inxml = NULL;
+ char *outxml = NULL;
+
+ if (virAsprintf(&inxml, "%s/networkxml2xmlin/%s.xml",
+ abs_srcdir, (const char*)data) < 0 ||
+ virAsprintf(&outxml, "%s/networkxml2xmlout/%s.xml",
+ abs_srcdir, (const char*)data) < 0) {
+ goto cleanup;
+ }
+
+ result = testCompareXMLToXMLFiles(inxml, outxml);
+cleanup:
+ free(inxml);
+ free(outxml);
+
+ return result;
+}
static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/nodedevxml2xmltest.c b/tests/nodedevxml2xmltest.c
index 2bc6743..e58883b 100644
--- a/tests/nodedevxml2xmltest.c
+++ b/tests/nodedevxml2xmltest.c
@@ -14,19 +14,18 @@
#include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-
-static int testCompareXMLToXMLFiles(const char *xml) {
- char xmlData[MAX_FILE];
- char *xmlPtr = &(xmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *xml)
+{
+ char *xmlData = NULL;
char *actual = NULL;
int ret = -1;
virNodeDeviceDefPtr dev = NULL;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &xmlData) < 0)
goto fail;
if (!(dev = virNodeDeviceDefParseString(xmlData, EXISTING_DEVICE)))
@@ -43,16 +42,26 @@ static int testCompareXMLToXMLFiles(const char *xml) {
ret = 0;
fail:
+ free(xmlData);
free(actual);
virNodeDeviceDefFree(dev);
return ret;
}
-static int testCompareXMLToXMLHelper(const void *data) {
- char xml[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/nodedevschemadata/%s.xml",
- abs_srcdir, (const char*)data);
- return testCompareXMLToXMLFiles(xml);
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
+ int result = -1;
+ char *xml = NULL;
+
+ if (virAsprintf(&xml, "%s/nodedevschemadata/%s.xml",
+ abs_srcdir, (const char*)data) < 0)
+ return -1;
+
+ result = testCompareXMLToXMLFiles(xml);
+
+ free(xml);
+ return result;
}
@@ -60,7 +69,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/nodeinfotest.c b/tests/nodeinfotest.c
index c690403..0d8c13b 100644
--- a/tests/nodeinfotest.c
+++ b/tests/nodeinfotest.c
@@ -22,26 +22,27 @@ mymain(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
#else
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-# define MAX_FILE 4096
-
extern int linuxNodeInfoCPUPopulate(FILE *cpuinfo, virNodeInfoPtr nodeinfo,
bool need_hyperthreads);
-static int linuxTestCompareFiles(const char *cpuinfofile, const char *outputfile) {
- char actualData[MAX_FILE];
- char expectData[MAX_FILE];
- char *expect = &expectData[0];
+static int
+linuxTestCompareFiles(const char *cpuinfofile, const char *outputfile)
+{
+ int ret = -1;
+ char *actualData = NULL;
+ char *expectData = NULL;
virNodeInfo nodeinfo;
FILE *cpuinfo;
- if (virtTestLoadFile(outputfile, &expect, MAX_FILE) < 0)
- return -1;
+ if (virtTestLoadFile(outputfile, &expectData) < 0)
+ goto fail;
cpuinfo = fopen(cpuinfofile, "r");
if (!cpuinfo)
- return -1;
+ goto fail;
memset(&nodeinfo, 0, sizeof(nodeinfo));
if (linuxNodeInfoCPUPopulate(cpuinfo, &nodeinfo, false) < 0) {
@@ -52,7 +53,7 @@ static int linuxTestCompareFiles(const char *cpuinfofile, const char *outputfile
virFreeError(error);
}
VIR_FORCE_FCLOSE(cpuinfo);
- return -1;
+ goto fail;
}
VIR_FORCE_FCLOSE(cpuinfo);
@@ -61,30 +62,49 @@ static int linuxTestCompareFiles(const char *cpuinfofile, const char *outputfile
* so blank it to a predictable value */
nodeinfo.nodes = 1;
- snprintf(actualData, MAX_FILE,
- "CPUs: %u, MHz: %u, Nodes: %u, Cores: %u\n",
- nodeinfo.cpus, nodeinfo.mhz, nodeinfo.nodes, nodeinfo.cores);
+ if (virAsprintf(&actualData, "CPUs: %u, MHz: %u, Nodes: %u, Cores: %u\n",
+ nodeinfo.cpus, nodeinfo.mhz, nodeinfo.nodes,
+ nodeinfo.cores) < 0)
+ goto fail;
if (STRNEQ(actualData, expectData)) {
if (getenv("DEBUG_TESTS")) {
printf("Expect %d '%s'\n", (int)strlen(expectData), expectData);
printf("Actual %d '%s'\n", (int)strlen(actualData), actualData);
}
- return -1;
+ goto fail;
}
- return 0;
+ ret = 0;
+
+fail:
+ free(expectData);
+ free(actualData);
+ return ret;
}
-static int linuxTestNodeInfo(const void *data) {
- char cpuinfo[PATH_MAX];
- char output[PATH_MAX];
- snprintf(cpuinfo, PATH_MAX, "%s/nodeinfodata/linux-%s.cpuinfo",
- abs_srcdir, (const char*)data);
- snprintf(output, PATH_MAX, "%s/nodeinfodata/linux-%s.txt",
- abs_srcdir, (const char*)data);
- return linuxTestCompareFiles(cpuinfo, output);
+static int
+linuxTestNodeInfo(const void *data)
+{
+ int result = -1;
+ char *cpuinfo = NULL;
+ char *output = NULL;
+
+ if (virAsprintf(&cpuinfo, "%s/nodeinfodata/linux-%s.cpuinfo",
+ abs_srcdir, (const char*)data) < 0 ||
+ virAsprintf(&output, "%s/nodeinfodata/linux-%s.txt",
+ abs_srcdir, (const char*)data) < 0) {
+ goto cleanup;
+ }
+
+ result = linuxTestCompareFiles(cpuinfo, output);
+
+cleanup:
+ free(cpuinfo);
+ free(output);
+
+ return result;
}
@@ -101,7 +121,6 @@ mymain(int argc, char **argv)
"nodeinfo-5",
"nodeinfo-6",
};
- char cwd[PATH_MAX];
abs_srcdir = getenv("abs_srcdir");
if (!abs_srcdir)
diff --git a/tests/nwfilterxml2xmltest.c b/tests/nwfilterxml2xmltest.c
index 575177a..0463a79 100644
--- a/tests/nwfilterxml2xmltest.c
+++ b/tests/nwfilterxml2xmltest.c
@@ -17,25 +17,22 @@
#include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-
-static int testCompareXMLToXMLFiles(const char *inxml,
- const char *outxml,
- bool expect_error) {
- char inXmlData[MAX_FILE];
- char *inXmlPtr = &(inXmlData[0]);
- char outXmlData[MAX_FILE];
- char *outXmlPtr = &(outXmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
+ bool expect_error)
+{
+ char *inXmlData = NULL;
+ char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
virNWFilterDefPtr dev = NULL;
- if (virtTestLoadFile(inxml, &inXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(inxml, &inXmlData) < 0)
goto fail;
- if (virtTestLoadFile(outxml, &outXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(outxml, &outXmlData) < 0)
goto fail;
virResetLastError();
@@ -62,6 +59,8 @@ static int testCompareXMLToXMLFiles(const char *inxml,
ret = 0;
fail:
+ free(inXmlData);
+ free(outXmlData);
free(actual);
virNWFilterDefFree(dev);
return ret;
@@ -72,23 +71,34 @@ typedef struct test_parms {
bool expect_warning;
} test_parms;
-static int testCompareXMLToXMLHelper(const void *data) {
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
+ int result = -1;
const test_parms *tp = data;
- char inxml[PATH_MAX];
- char outxml[PATH_MAX];
- snprintf(inxml, PATH_MAX, "%s/nwfilterxml2xmlin/%s.xml",
- abs_srcdir, tp->name);
- snprintf(outxml, PATH_MAX, "%s/nwfilterxml2xmlout/%s.xml",
- abs_srcdir, tp->name);
- return testCompareXMLToXMLFiles(inxml, outxml, tp->expect_warning);
-}
+ char *inxml = NULL;
+ char *outxml = NULL;
+
+ if (virAsprintf(&inxml, "%s/nwfilterxml2xmlin/%s.xml",
+ abs_srcdir, tp->name) < 0 ||
+ virAsprintf(&outxml, "%s/nwfilterxml2xmlout/%s.xml",
+ abs_srcdir, tp->name) < 0) {
+ goto cleanup;
+ }
+
+ result = testCompareXMLToXMLFiles(inxml, outxml, tp->expect_warning);
+cleanup:
+ free(inxml);
+ free(outxml);
+
+ return result;
+}
static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c
index 0214db0..47e452e 100644
--- a/tests/qemuargv2xmltest.c
+++ b/tests/qemuargv2xmltest.c
@@ -17,11 +17,10 @@
# include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static struct qemud_driver driver;
-# define MAX_FILE 4096
-
static int blankProblemElements(char *data)
{
if (virtTestClearLineRegex("<name>[[:alnum:]]+</name>", data) < 0 ||
@@ -37,18 +36,16 @@ static int blankProblemElements(char *data)
static int testCompareXMLToArgvFiles(const char *xml,
const char *cmdfile,
bool expect_warning) {
- char xmlData[MAX_FILE];
- char cmdData[MAX_FILE];
- char *expectxml = &(xmlData[0]);
+ char *expectxml = NULL;
char *actualxml = NULL;
- char *cmd = &(cmdData[0]);
+ char *cmd = NULL;
int ret = -1;
virDomainDefPtr vmdef = NULL;
char *log;
- if (virtTestLoadFile(cmdfile, &cmd, MAX_FILE) < 0)
+ if (virtTestLoadFile(cmdfile, &cmd) < 0)
goto fail;
- if (virtTestLoadFile(xml, &expectxml, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &expectxml) < 0)
goto fail;
if (!(vmdef = qemuParseCommandLineString(driver.caps, cmd)))
@@ -77,7 +74,9 @@ static int testCompareXMLToArgvFiles(const char *xml,
ret = 0;
fail:
+ free(expectxml);
free(actualxml);
+ free(cmd);
virDomainDefFree(vmdef);
return ret;
}
@@ -89,15 +88,26 @@ struct testInfo {
const char *migrateFrom;
};
-static int testCompareXMLToArgvHelper(const void *data) {
+static int
+testCompareXMLToArgvHelper(const void *data)
+{
+ int result = -1;
const struct testInfo *info = data;
- char xml[PATH_MAX];
- char args[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
- abs_srcdir, info->name);
- snprintf(args, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.args",
- abs_srcdir, info->name);
- return testCompareXMLToArgvFiles(xml, args, !!info->extraFlags);
+ char *xml = NULL;
+ char *args = NULL;
+
+ if (virAsprintf(&xml, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
+ abs_srcdir, info->name) < 0 ||
+ virAsprintf(&args, "%s/qemuxml2argvdata/qemuxml2argv-%s.args",
+ abs_srcdir, info->name) < 0)
+ goto cleanup;
+
+ result = testCompareXMLToArgvFiles(xml, args, !!info->extraFlags);
+
+cleanup:
+ free(xml);
+ free(args);
+ return result;
}
@@ -106,7 +116,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/qemuhelptest.c b/tests/qemuhelptest.c
index c86c578..4838e9a 100644
--- a/tests/qemuhelptest.c
+++ b/tests/qemuhelptest.c
@@ -9,8 +9,6 @@
# include "qemu/qemu_capabilities.h"
# include "memory.h"
-# define MAX_HELP_OUTPUT_SIZE 1024*64
-
struct testInfo {
const char *name;
virBitmapPtr flags;
@@ -20,6 +18,7 @@ struct testInfo {
};
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static void printMismatchedFlags(virBitmapPtr got,
@@ -41,8 +40,7 @@ static int testHelpStrParsing(const void *data)
{
const struct testInfo *info = data;
char *path = NULL;
- char helpStr[MAX_HELP_OUTPUT_SIZE];
- char *help = &(helpStr[0]);
+ char *help = NULL;
unsigned int version, is_kvm, kvm_version;
virBitmapPtr flags = NULL;
int ret = -1;
@@ -52,7 +50,7 @@ static int testHelpStrParsing(const void *data)
if (virAsprintf(&path, "%s/qemuhelpdata/%s", abs_srcdir, info->name) < 0)
return -1;
- if (virtTestLoadFile(path, &help, MAX_HELP_OUTPUT_SIZE) < 0)
+ if (virtTestLoadFile(path, &help) < 0)
goto cleanup;
if (!(flags = qemuCapsNew()))
@@ -64,11 +62,12 @@ static int testHelpStrParsing(const void *data)
if (qemuCapsGet(info->flags, QEMU_CAPS_DEVICE)) {
VIR_FREE(path);
+ VIR_FREE(help);
if (virAsprintf(&path, "%s/qemuhelpdata/%s-device", abs_srcdir,
info->name) < 0)
goto cleanup;
- if (virtTestLoadFile(path, &help, MAX_HELP_OUTPUT_SIZE) < 0)
+ if (virtTestLoadFile(path, &help) < 0)
goto cleanup;
if (qemuCapsParseDeviceStr(help, flags) < 0)
@@ -114,6 +113,7 @@ static int testHelpStrParsing(const void *data)
ret = 0;
cleanup:
VIR_FREE(path);
+ VIR_FREE(help);
qemuCapsFree(flags);
VIR_FREE(got);
VIR_FREE(expected);
@@ -124,7 +124,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index c06bdf6..023f5df 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -20,20 +20,19 @@
# include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static const char *abs_top_srcdir;
static struct qemud_driver driver;
-# define MAX_FILE 4096
-
static int testCompareXMLToArgvFiles(const char *xml,
const char *cmdline,
virBitmapPtr extraFlags,
const char *migrateFrom,
int migrateFd,
- bool expectError) {
- char argvData[MAX_FILE];
- char *expectargv = &(argvData[0]);
+ bool expectError)
+{
+ char *expectargv = NULL;
int len;
char *actualargv = NULL;
int ret = -1;
@@ -47,7 +46,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
if (!(conn = virGetConnect()))
goto fail;
- len = virtTestLoadFile(cmdline, &expectargv, MAX_FILE);
+ len = virtTestLoadFile(cmdline, &expectargv);
if (len < 0)
goto fail;
if (len && expectargv[len - 1] == '\n')
@@ -158,6 +157,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
fail:
free(log);
free(emulator);
+ free(expectargv);
free(actualargv);
virCommandFree(cmd);
virDomainDefFree(vmdef);
@@ -174,17 +174,28 @@ struct testInfo {
bool expectError;
};
-static int testCompareXMLToArgvHelper(const void *data) {
+static int
+testCompareXMLToArgvHelper(const void *data)
+{
+ int result = -1;
const struct testInfo *info = data;
- char xml[PATH_MAX];
- char args[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
- abs_srcdir, info->name);
- snprintf(args, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.args",
- abs_srcdir, info->name);
- return testCompareXMLToArgvFiles(xml, args, info->extraFlags,
- info->migrateFrom, info->migrateFd,
- info->expectError);
+ char *xml = NULL;
+ char *args = NULL;
+
+ if (virAsprintf(&xml, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
+ abs_srcdir, info->name) < 0 ||
+ virAsprintf(&args, "%s/qemuxml2argvdata/qemuxml2argv-%s.args",
+ abs_srcdir, info->name) < 0)
+ goto cleanup;
+
+ result = testCompareXMLToArgvFiles(xml, args, info->extraFlags,
+ info->migrateFrom, info->migrateFd,
+ info->expectError);
+
+cleanup:
+ free(xml);
+ free(args);
+ return result;
}
@@ -193,14 +204,13 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
- char map[PATH_MAX];
+ char *map = NULL;
progname = argv[0];
if (argc > 1) {
fprintf(stderr, "Usage: %s\n", progname);
- return (EXIT_FAILURE);
+ return EXIT_FAILURE;
}
abs_srcdir = getenv("abs_srcdir");
@@ -224,10 +234,11 @@ mymain(int argc, char **argv)
return EXIT_FAILURE;
if (!(driver.spicePassword = strdup("123456")))
return EXIT_FAILURE;
-
- snprintf(map, PATH_MAX, "%s/src/cpu/cpu_map.xml", abs_top_srcdir);
- if (cpuMapOverride(map) < 0)
+ if (virAsprintf(&map, "%s/src/cpu/cpu_map.xml", abs_top_srcdir) < 0 ||
+ cpuMapOverride(map) < 0) {
+ free(map);
return EXIT_FAILURE;
+ }
# define DO_TEST_FULL(name, migrateFrom, migrateFd, expectError, ...) \
do { \
@@ -498,6 +509,7 @@ mymain(int argc, char **argv)
free(driver.stateDir);
virCapabilitiesFree(driver.caps);
+ free(map);
return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
index 27330a9..9d8beeb 100644
--- a/tests/qemuxml2xmltest.c
+++ b/tests/qemuxml2xmltest.c
@@ -16,24 +16,22 @@
# include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static struct qemud_driver driver;
-# define MAX_FILE 4096
-
-
-static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) {
- char inXmlData[MAX_FILE];
- char *inXmlPtr = &(inXmlData[0]);
- char outXmlData[MAX_FILE];
- char *outXmlPtr = &(outXmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *inxml, const char *outxml)
+{
+ char *inXmlData = NULL;
+ char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
virDomainDefPtr def = NULL;
- if (virtTestLoadFile(inxml, &inXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(inxml, &inXmlData) < 0)
goto fail;
- if (virtTestLoadFile(outxml, &outXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(outxml, &outXmlData) < 0)
goto fail;
if (!(def = virDomainDefParseString(driver.caps, inXmlData,
@@ -51,6 +49,8 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) {
ret = 0;
fail:
+ free(inXmlData);
+ free(outXmlData);
free(actual);
virDomainDefFree(def);
return ret;
@@ -61,16 +61,19 @@ struct testInfo {
int different;
};
-static int testCompareXMLToXMLHelper(const void *data) {
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
const struct testInfo *info = data;
- char xml_in[PATH_MAX];
- char xml_out[PATH_MAX];
- int ret;
+ char *xml_in = NULL;
+ char *xml_out = NULL;
+ int ret = -1;
- snprintf(xml_in, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
- abs_srcdir, info->name);
- snprintf(xml_out, PATH_MAX, "%s/qemuxml2xmloutdata/qemuxml2xmlout-%s.xml",
- abs_srcdir, info->name);
+ if (virAsprintf(&xml_in, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
+ abs_srcdir, info->name) < 0 ||
+ virAsprintf(&xml_out, "%s/qemuxml2xmloutdata/qemuxml2xmlout-%s.xml",
+ abs_srcdir, info->name) < 0)
+ goto cleanup;
if (info->different) {
ret = testCompareXMLToXMLFiles(xml_in, xml_out);
@@ -78,6 +81,9 @@ static int testCompareXMLToXMLHelper(const void *data) {
ret = testCompareXMLToXMLFiles(xml_in, xml_in);
}
+cleanup:
+ free(xml_in);
+ free(xml_out);
return ret;
}
@@ -86,7 +92,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/sexpr2xmltest.c b/tests/sexpr2xmltest.c
index 9f74ece..4df0fd5 100644
--- a/tests/sexpr2xmltest.c
+++ b/tests/sexpr2xmltest.c
@@ -14,18 +14,16 @@
#include "testutilsxen.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static virCapsPtr caps;
-#define MAX_FILE 4096
-
-static int testCompareFiles(const char *xml, const char *sexpr,
- int xendConfigVersion) {
- char xmlData[MAX_FILE];
- char sexprData[MAX_FILE];
+static int
+testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion)
+{
+ char *xmlData = NULL;
+ char *sexprData = NULL;
char *gotxml = NULL;
- char *xmlPtr = &(xmlData[0]);
- char *sexprPtr = &(sexprData[0]);
int id;
char * tty;
int vncport;
@@ -38,10 +36,10 @@ static int testCompareFiles(const char *xml, const char *sexpr,
conn = virGetConnect();
if (!conn) goto fail;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &xmlData) < 0)
goto fail;
- if (virtTestLoadFile(sexpr, &sexprPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(sexpr, &sexprData) < 0)
goto fail;
memset(&priv, 0, sizeof priv);
@@ -72,6 +70,8 @@ static int testCompareFiles(const char *xml, const char *sexpr,
ret = 0;
fail:
+ free(xmlData);
+ free(sexprData);
free(gotxml);
virDomainDefFree(def);
if (conn)
@@ -86,23 +86,34 @@ struct testInfo {
int version;
};
-static int testCompareHelper(const void *data) {
+static int
+testCompareHelper(const void *data)
+{
+ int result = -1;
const struct testInfo *info = data;
- char xml[PATH_MAX];
- char args[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/sexpr2xmldata/sexpr2xml-%s.xml",
- abs_srcdir, info->input);
- snprintf(args, PATH_MAX, "%s/sexpr2xmldata/sexpr2xml-%s.sexpr",
- abs_srcdir, info->output);
- return testCompareFiles(xml, args, info->version);
-}
+ char *xml = NULL;
+ char *args = NULL;
+
+ if (virAsprintf(&xml, "%s/sexpr2xmldata/sexpr2xml-%s.xml",
+ abs_srcdir, info->input) < 0 ||
+ virAsprintf(&args, "%s/sexpr2xmldata/sexpr2xml-%s.sexpr",
+ abs_srcdir, info->output) < 0) {
+ goto cleanup;
+ }
+
+ result = testCompareFiles(xml, args, info->version);
+cleanup:
+ free(xml);
+ free(args);
+
+ return result;
+}
static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/storagepoolxml2xmltest.c b/tests/storagepoolxml2xmltest.c
index 2ec29e3..e947100 100644
--- a/tests/storagepoolxml2xmltest.c
+++ b/tests/storagepoolxml2xmltest.c
@@ -14,23 +14,21 @@
#include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-
-static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) {
- char inXmlData[MAX_FILE];
- char *inXmlPtr = &(inXmlData[0]);
- char outXmlData[MAX_FILE];
- char *outXmlPtr = &(outXmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *inxml, const char *outxml)
+{
+ char *inXmlData = NULL;
+ char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
virStoragePoolDefPtr dev = NULL;
- if (virtTestLoadFile(inxml, &inXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(inxml, &inXmlData) < 0)
goto fail;
- if (virtTestLoadFile(outxml, &outXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(outxml, &outXmlData) < 0)
goto fail;
if (!(dev = virStoragePoolDefParseString(inXmlData)))
@@ -47,27 +45,40 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) {
ret = 0;
fail:
+ free(inXmlData);
+ free(outXmlData);
free(actual);
virStoragePoolDefFree(dev);
return ret;
}
-static int testCompareXMLToXMLHelper(const void *data) {
- char inxml[PATH_MAX];
- char outxml[PATH_MAX];
- snprintf(inxml, PATH_MAX, "%s/storagepoolxml2xmlin/%s.xml",
- abs_srcdir, (const char*)data);
- snprintf(outxml, PATH_MAX, "%s/storagepoolxml2xmlout/%s.xml",
- abs_srcdir, (const char*)data);
- return testCompareXMLToXMLFiles(inxml, outxml);
-}
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
+ int result = -1;
+ char *inxml = NULL;
+ char *outxml = NULL;
+
+ if (virAsprintf(&inxml, "%s/storagepoolxml2xmlin/%s.xml",
+ abs_srcdir, (const char*)data) < 0 ||
+ virAsprintf(&outxml, "%s/storagepoolxml2xmlout/%s.xml",
+ abs_srcdir, (const char*)data) < 0) {
+ goto cleanup;
+ }
+
+ result = testCompareXMLToXMLFiles(inxml, outxml);
+cleanup:
+ free(inxml);
+ free(outxml);
+
+ return result;
+}
static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/storagevolxml2xmltest.c b/tests/storagevolxml2xmltest.c
index ffa5dfe..c57b7a8 100644
--- a/tests/storagevolxml2xmltest.c
+++ b/tests/storagevolxml2xmltest.c
@@ -14,30 +14,26 @@
#include "testutilsqemu.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-
-static int testCompareXMLToXMLFiles(const char *poolxml,
- const char *inxml,
- const char *outxml) {
- char poolXmlData[MAX_FILE];
- char *poolXmlPtr = &(poolXmlData[0]);
- char inXmlData[MAX_FILE];
- char *inXmlPtr = &(inXmlData[0]);
- char outXmlData[MAX_FILE];
- char *outXmlPtr = &(outXmlData[0]);
+static int
+testCompareXMLToXMLFiles(const char *poolxml, const char *inxml,
+ const char *outxml)
+{
+ char *poolXmlData = NULL;
+ char *inXmlData = NULL;
+ char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
virStoragePoolDefPtr pool = NULL;
virStorageVolDefPtr dev = NULL;
- if (virtTestLoadFile(poolxml, &poolXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(poolxml, &poolXmlData) < 0)
goto fail;
- if (virtTestLoadFile(inxml, &inXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(inxml, &inXmlData) < 0)
goto fail;
- if (virtTestLoadFile(outxml, &outXmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(outxml, &outXmlData) < 0)
goto fail;
if (!(pool = virStoragePoolDefParseString(poolXmlData)))
@@ -57,6 +53,9 @@ static int testCompareXMLToXMLFiles(const char *poolxml,
ret = 0;
fail:
+ free(poolXmlData);
+ free(inXmlData);
+ free(outXmlData);
free(actual);
virStoragePoolDefFree(pool);
virStorageVolDefFree(dev);
@@ -68,19 +67,32 @@ struct testInfo {
const char *name;
};
-static int testCompareXMLToXMLHelper(const void *data) {
- char poolxml[PATH_MAX];
- char inxml[PATH_MAX];
- char outxml[PATH_MAX];
+static int
+testCompareXMLToXMLHelper(const void *data)
+{
+ int result = -1;
const struct testInfo *info = data;
+ char *poolxml = NULL;
+ char *inxml = NULL;
+ char *outxml = NULL;
+
+ if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
+ abs_srcdir, info->pool) < 0 ||
+ virAsprintf(&inxml, "%s/storagevolxml2xmlin/%s.xml",
+ abs_srcdir, info->name) < 0 ||
+ virAsprintf(&outxml, "%s/storagevolxml2xmlout/%s.xml",
+ abs_srcdir, info->name) < 0) {
+ goto cleanup;
+ }
+
+ result = testCompareXMLToXMLFiles(poolxml, inxml, outxml);
+
+cleanup:
+ free(poolxml);
+ free(inxml);
+ free(outxml);
- snprintf(poolxml, PATH_MAX, "%s/storagepoolxml2xmlin/%s.xml",
- abs_srcdir, (const char*)info->pool);
- snprintf(inxml, PATH_MAX, "%s/storagevolxml2xmlin/%s.xml",
- abs_srcdir, (const char*)info->name);
- snprintf(outxml, PATH_MAX, "%s/storagevolxml2xmlout/%s.xml",
- abs_srcdir, (const char*)info->name);
- return testCompareXMLToXMLFiles(poolxml, inxml, outxml);
+ return result;
}
@@ -88,7 +100,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/testutils.c b/tests/testutils.c
index 1f3b569..aaee337 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -170,17 +170,16 @@ virtTestRun(const char *title, int nloops, int (*body)(const void *data), const
return ret;
}
-/* Read FILE into buffer BUF of length BUFLEN.
- Upon any failure, or if FILE appears to contain more than BUFLEN bytes,
- diagnose it and return -1, but don't bother trying to preserve errno.
- Otherwise, return the number of bytes copied into BUF. */
-int virtTestLoadFile(const char *file,
- char **buf,
- int buflen) {
+/* Allocate BUF to the size of FILE. Read FILE into buffer BUF.
+ Upon any failure, diagnose it and return -1, but don't bother trying
+ to preserve errno. Otherwise, return the number of bytes copied into BUF. */
+int
+virtTestLoadFile(const char *file, char **buf)
+{
FILE *fp = fopen(file, "r");
struct stat st;
- char *tmp = *buf;
- int len, tmplen = buflen;
+ char *tmp;
+ int len, tmplen, buflen;
if (!fp) {
fprintf (stderr, "%s: failed to open: %s\n", file, strerror(errno));
@@ -193,17 +192,23 @@ int virtTestLoadFile(const char *file,
return -1;
}
- if (st.st_size > (buflen-1)) {
- fprintf (stderr, "%s: larger than buffer (> %d)\n", file, buflen-1);
+ tmplen = buflen = st.st_size + 1;
+
+ if (VIR_ALLOC_N(*buf, buflen) < 0) {
+ fprintf (stderr, "%s: larger than available memory (> %d)\n", file, buflen);
VIR_FORCE_FCLOSE(fp);
return -1;
}
+ tmp = *buf;
(*buf)[0] = '\0';
if (st.st_size) {
/* read the file line by line */
while (fgets(tmp, tmplen, fp) != NULL) {
len = strlen(tmp);
+ /* stop on an empty line */
+ if (len == 0)
+ break;
/* remove trailing backslash-newline pair */
if (len >= 2 && tmp[len-2] == '\\' && tmp[len-1] == '\n') {
len -= 2;
@@ -216,6 +221,7 @@ int virtTestLoadFile(const char *file,
if (ferror(fp)) {
fprintf (stderr, "%s: read failed: %s\n", file, strerror(errno));
VIR_FORCE_FCLOSE(fp);
+ free(*buf);
return -1;
}
}
@@ -265,10 +271,11 @@ void virtTestCaptureProgramExecChild(const char *const argv[],
VIR_FORCE_CLOSE(stdinfd);
}
-int virtTestCaptureProgramOutput(const char *const argv[],
- char **buf,
- int buflen) {
+int
+virtTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen)
+{
int pipefd[2];
+ int len;
if (pipe(pipefd) < 0)
return -1;
@@ -286,34 +293,20 @@ int virtTestCaptureProgramOutput(const char *const argv[],
return -1;
default:
- {
- int got = 0;
- int ret = -1;
- int want = buflen-1;
-
- VIR_FORCE_CLOSE(pipefd[1]);
-
- while (want) {
- if ((ret = read(pipefd[0], (*buf)+got, want)) <= 0)
- break;
- got += ret;
- want -= ret;
- }
- VIR_FORCE_CLOSE(pipefd[0]);
-
- if (!ret)
- (*buf)[got] = '\0';
-
- waitpid(pid, NULL, 0);
+ VIR_FORCE_CLOSE(pipefd[1]);
+ len = virFileReadLimFD(pipefd[0], maxlen, buf);
+ VIR_FORCE_CLOSE(pipefd[0]);
+ waitpid(pid, NULL, 0);
- return ret;
- }
+ return len;
}
}
#else /* !WIN32 */
-int virtTestCaptureProgramOutput(const char *const argv[] ATTRIBUTE_UNUSED,
- char **buf ATTRIBUTE_UNUSED,
- int buflen ATTRIBUTE_UNUSED) {
+int
+virtTestCaptureProgramOutput(const char *const argv[] ATTRIBUTE_UNUSED,
+ char **buf ATTRIBUTE_UNUSED,
+ int maxlen ATTRIBUTE_UNUSED)
+{
return -1;
}
#endif /* !WIN32 */
diff --git a/tests/testutils.h b/tests/testutils.h
index 88603a1..6adc0a0 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -23,12 +23,8 @@ int virtTestRun(const char *title,
int nloops,
int (*body)(const void *data),
const void *data);
-int virtTestLoadFile(const char *name,
- char **buf,
- int buflen);
-int virtTestCaptureProgramOutput(const char *const argv[],
- char **buf,
- int buflen);
+int virtTestLoadFile(const char *file, char **buf);
+int virtTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
int virtTestClearLineRegex(const char *pattern,
char *string);
diff --git a/tests/virshtest.c b/tests/virshtest.c
index 8ec97a8..e6743d7 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -6,11 +6,12 @@
#include "internal.h"
#include "xml.h"
+#include "util.h"
#include "testutils.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
#define DOM_UUID "ef861801-45b9-11cb-88e3-afbfe5370493"
@@ -47,41 +48,31 @@ static int testFilterLine(char *buffer,
return 0;
}
-static int testCompareOutputLit(const char *expectData,
- const char *filter, const char *const argv[]) {
- char actualData[MAX_FILE];
- char *actualPtr = &(actualData[0]);
-
- if (virtTestCaptureProgramOutput(argv, &actualPtr, MAX_FILE) < 0)
- return -1;
-
- if (filter)
- if (testFilterLine(actualData, filter) < 0)
- return -1;
+static int
+testCompareOutputLit(const char *expectData,
+ const char *filter, const char *const argv[])
+{
+ int result = -1;
+ char *actualData = NULL;
- if (STRNEQ(expectData, actualData)) {
- virtTestDifference(stderr, expectData, actualData);
- return -1;
- }
+ if (virtTestCaptureProgramOutput(argv, &actualData, 4096) < 0)
+ goto cleanup;
- return 0;
-}
+ if (filter && testFilterLine(actualData, filter) < 0)
+ goto cleanup;
-#if unused
-static int testCompareOutput(const char *expect_rel, const char *filter,
- const char *const argv[]) {
- char expectData[MAX_FILE];
- char *expectPtr = &(expectData[0]);
- char expect[PATH_MAX];
+ if (STRNEQ(expectData, actualData)) {
+ virtTestDifference(stderr, expectData, actualData);
+ goto cleanup;
+ }
- snprintf(expect, sizeof expect - 1, "%s/%s", abs_srcdir, expect_rel);
+ result = 0;
- if (virtTestLoadFile(expect, &expectPtr, MAX_FILE) < 0)
- return -1;
+cleanup:
+ free(actualData);
- return testCompareOutputLit(expectData, filter, argv);
+ return result;
}
-#endif
#define VIRSH_DEFAULT "../tools/virsh", \
"--connect", \
@@ -235,8 +226,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char buffer[PATH_MAX];
- char cwd[PATH_MAX];
abs_srcdir = getenv("abs_srcdir");
if (!abs_srcdir)
@@ -246,16 +235,17 @@ mymain(int argc, char **argv)
exit (EXIT_AM_SKIP);
#endif
- snprintf(buffer, PATH_MAX-1, "test://%s/../examples/xml/test/testnode.xml", abs_srcdir);
- buffer[PATH_MAX-1] = '\0';
- progname = argv[0];
- custom_uri = buffer;
-
if (argc > 1) {
fprintf(stderr, "Usage: %s\n", progname);
- return(EXIT_FAILURE);
+ return EXIT_FAILURE;
}
+ if (virAsprintf(&custom_uri, "test://%s/../examples/xml/test/testnode.xml",
+ abs_srcdir) < 0)
+ return EXIT_FAILURE;
+
+ progname = argv[0];
+
if (virtTestRun("virsh list (default)",
1, testCompareListDefault, NULL) != 0)
ret = -1;
@@ -406,6 +396,7 @@ mymain(int argc, char **argv)
#undef DO_TEST
+ free(custom_uri);
return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c
index 5fed1c4..53d66c5 100644
--- a/tests/vmx2xmltest.c
+++ b/tests/vmx2xmltest.c
@@ -11,13 +11,12 @@
# include "testutils.h"
# include "vmx/vmx.h"
-static char *progname = NULL;
-static char *abs_srcdir = NULL;
-static virCapsPtr caps = NULL;
+static char *progname;
+static char cwd[PATH_MAX];
+static char *abs_srcdir;
+static virCapsPtr caps;
static virVMXContext ctx;
-# define MAX_FILE 4096
-
static void
testCapsInit(void)
{
@@ -71,19 +70,17 @@ static int
testCompareFiles(const char *vmx, const char *xml)
{
int result = -1;
- char vmxData[MAX_FILE];
- char xmlData[MAX_FILE];
+ char *vmxData = NULL;
+ char *xmlData = NULL;
char *formatted = NULL;
- char *vmxPtr = &(vmxData[0]);
- char *xmlPtr = &(xmlData[0]);
virDomainDefPtr def = NULL;
virErrorPtr err = NULL;
- if (virtTestLoadFile(vmx, &vmxPtr, MAX_FILE) < 0) {
+ if (virtTestLoadFile(vmx, &vmxData) < 0) {
goto failure;
}
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0) {
+ if (virtTestLoadFile(xml, &xmlData) < 0) {
goto failure;
}
@@ -111,6 +108,8 @@ testCompareFiles(const char *vmx, const char *xml)
result = 0;
failure:
+ VIR_FREE(vmxData);
+ VIR_FREE(xmlData);
VIR_FREE(formatted);
virDomainDefFree(def);
@@ -125,16 +124,25 @@ struct testInfo {
static int
testCompareHelper(const void *data)
{
+ int result = -1;
const struct testInfo *info = data;
- char vmx[PATH_MAX];
- char xml[PATH_MAX];
+ char *vmx = NULL;
+ char *xml = NULL;
+
+ if (virAsprintf(&vmx, "%s/vmx2xmldata/vmx2xml-%s.vmx", abs_srcdir,
+ info->input) < 0 ||
+ virAsprintf(&xml, "%s/vmx2xmldata/vmx2xml-%s.xml", abs_srcdir,
+ info->output) < 0) {
+ goto cleanup;
+ }
- snprintf(vmx, PATH_MAX, "%s/vmx2xmldata/vmx2xml-%s.vmx", abs_srcdir,
- info->input);
- snprintf(xml, PATH_MAX, "%s/vmx2xmldata/vmx2xml-%s.xml", abs_srcdir,
- info->output);
+ result = testCompareFiles(vmx, xml);
- return testCompareFiles(vmx, xml);
+ cleanup:
+ VIR_FREE(vmx);
+ VIR_FREE(xml);
+
+ return result;
}
static char *
@@ -184,7 +192,6 @@ static int
mymain(int argc, char **argv)
{
int result = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/xencapstest.c b/tests/xencapstest.c
index 7b48812..a00b30b 100644
--- a/tests/xencapstest.c
+++ b/tests/xencapstest.c
@@ -12,34 +12,30 @@
#include "files.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
-#define MAX_FILE 4096
-
-static int testCompareFiles(const char *hostmachine,
- const char *xml_rel,
- const char *cpuinfo_rel,
- const char *capabilities_rel) {
- char xmlData[MAX_FILE];
- char *expectxml = &(xmlData[0]);
+static int
+testCompareFiles(const char *hostmachine, const char *xml_rel,
+ const char *cpuinfo_rel, const char *capabilities_rel)
+{
+ char *expectxml = NULL;
char *actualxml = NULL;
FILE *fp1 = NULL, *fp2 = NULL;
virCapsPtr caps = NULL;
int ret = -1;
- char xml[PATH_MAX];
- char cpuinfo[PATH_MAX];
- char capabilities[PATH_MAX];
+ char *xml = NULL;
+ char *cpuinfo = NULL;
+ char *capabilities = NULL;
- snprintf(xml, sizeof xml - 1, "%s/%s",
- abs_srcdir, xml_rel);
- snprintf(cpuinfo, sizeof cpuinfo - 1, "%s/%s",
- abs_srcdir, cpuinfo_rel);
- snprintf(capabilities, sizeof capabilities - 1, "%s/%s",
- abs_srcdir, capabilities_rel);
+ if (virAsprintf(&xml, "%s/%s", abs_srcdir, xml_rel) < 0 ||
+ virAsprintf(&cpuinfo, "%s/%s", abs_srcdir, cpuinfo_rel) < 0 ||
+ virAsprintf(&capabilities, "%s/%s", abs_srcdir, capabilities_rel) < 0)
+ goto fail;
- if (virtTestLoadFile(xml, &expectxml, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &expectxml) < 0)
goto fail;
if (!(fp1 = fopen(cpuinfo, "r")))
@@ -62,8 +58,11 @@ static int testCompareFiles(const char *hostmachine,
ret = 0;
fail:
-
+ free(expectxml);
free(actualxml);
+ free(xml);
+ free(cpuinfo);
+ free(capabilities);
VIR_FORCE_FCLOSE(fp1);
VIR_FORCE_FCLOSE(fp2);
@@ -154,7 +153,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/xmconfigtest.c b/tests/xmconfigtest.c
index 7d418a5..41ea0c8 100644
--- a/tests/xmconfigtest.c
+++ b/tests/xmconfigtest.c
@@ -37,33 +37,33 @@
#include "memory.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static virCapsPtr caps;
-#define MAX_FILE 4096
-
-static int testCompareParseXML(const char *xmcfg, const char *xml,
- int xendConfigVersion) {
- char xmlData[MAX_FILE];
- char xmcfgData[MAX_FILE];
- char gotxmcfgData[MAX_FILE];
- char *xmlPtr = &(xmlData[0]);
- char *xmcfgPtr = &(xmcfgData[0]);
- char *gotxmcfgPtr = &(gotxmcfgData[0]);
+static int
+testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion)
+{
+ char *xmlData = NULL;
+ char *xmcfgData = NULL;
+ char *gotxmcfgData = NULL;
virConfPtr conf = NULL;
int ret = -1;
virConnectPtr conn;
- int wrote = MAX_FILE;
+ int wrote = 4096;
struct _xenUnifiedPrivate priv;
virDomainDefPtr def = NULL;
+ if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0)
+ goto fail;
+
conn = virGetConnect();
if (!conn) goto fail;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &xmlData) < 0)
goto fail;
- if (virtTestLoadFile(xmcfg, &xmcfgPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
goto fail;
/* Many puppies died to bring you this code. */
@@ -71,16 +71,16 @@ static int testCompareParseXML(const char *xmcfg, const char *xml,
priv.caps = caps;
conn->privateData = &priv;
- if (!(def = virDomainDefParseString(caps, xmlPtr,
+ if (!(def = virDomainDefParseString(caps, xmlData,
VIR_DOMAIN_XML_INACTIVE)))
goto fail;
if (!(conf = xenFormatXM(conn, def, xendConfigVersion)))
goto fail;
- if (virConfWriteMem(gotxmcfgPtr, &wrote, conf) < 0)
+ if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0)
goto fail;
- gotxmcfgPtr[wrote] = '\0';
+ gotxmcfgData[wrote] = '\0';
if (STRNEQ(xmcfgData, gotxmcfgData)) {
virtTestDifference(stderr, xmcfgData, gotxmcfgData);
@@ -90,6 +90,9 @@ static int testCompareParseXML(const char *xmcfg, const char *xml,
ret = 0;
fail:
+ free(xmlData);
+ free(xmcfgData);
+ free(gotxmcfgData);
if (conf)
virConfFree(conf);
virDomainDefFree(def);
@@ -98,12 +101,11 @@ static int testCompareParseXML(const char *xmcfg, const char *xml,
return ret;
}
-static int testCompareFormatXML(const char *xmcfg, const char *xml,
- int xendConfigVersion) {
- char xmlData[MAX_FILE];
- char xmcfgData[MAX_FILE];
- char *xmlPtr = &(xmlData[0]);
- char *xmcfgPtr = &(xmcfgData[0]);
+static int
+testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion)
+{
+ char *xmlData = NULL;
+ char *xmcfgData = NULL;
char *gotxml = NULL;
virConfPtr conf = NULL;
int ret = -1;
@@ -114,10 +116,10 @@ static int testCompareFormatXML(const char *xmcfg, const char *xml,
conn = virGetConnect();
if (!conn) goto fail;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &xmlData) < 0)
goto fail;
- if (virtTestLoadFile(xmcfg, &xmcfgPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
goto fail;
/* Many puppies died to bring you this code. */
@@ -125,7 +127,7 @@ static int testCompareFormatXML(const char *xmcfg, const char *xml,
priv.caps = caps;
conn->privateData = &priv;
- if (!(conf = virConfReadMem(xmcfgPtr, strlen(xmcfgPtr), 0)))
+ if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
goto fail;
if (!(def = xenParseXM(conf, priv.xendConfigVersion, priv.caps)))
@@ -144,6 +146,8 @@ static int testCompareFormatXML(const char *xmcfg, const char *xml,
fail:
if (conf)
virConfFree(conf);
+ VIR_FREE(xmlData);
+ VIR_FREE(xmcfgData);
VIR_FREE(gotxml);
virDomainDefFree(def);
virUnrefConnect(conn);
@@ -158,18 +162,30 @@ struct testInfo {
int mode;
};
-static int testCompareHelper(const void *data) {
+static int
+testCompareHelper(const void *data)
+{
+ int result = -1;
const struct testInfo *info = data;
- char xml[PATH_MAX];
- char cfg[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/xmconfigdata/test-%s.xml",
- abs_srcdir, info->name);
- snprintf(cfg, PATH_MAX, "%s/xmconfigdata/test-%s.cfg",
- abs_srcdir, info->name);
+ char *xml = NULL;
+ char *cfg = NULL;
+
+ if (virAsprintf(&xml, "%s/xmconfigdata/test-%s.xml",
+ abs_srcdir, info->name) < 0 ||
+ virAsprintf(&cfg, "%s/xmconfigdata/test-%s.cfg",
+ abs_srcdir, info->name) < 0)
+ goto cleanup;
+
if (info->mode == 0)
- return testCompareParseXML(cfg, xml, info->version);
+ result = testCompareParseXML(cfg, xml, info->version);
else
- return testCompareFormatXML(cfg, xml, info->version);
+ result = testCompareFormatXML(cfg, xml, info->version);
+
+cleanup:
+ free(xml);
+ free(cfg);
+
+ return result;
}
@@ -177,7 +193,6 @@ static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/xml2sexprtest.c b/tests/xml2sexprtest.c
index cf530b6..b6d8c00 100644
--- a/tests/xml2sexprtest.c
+++ b/tests/xml2sexprtest.c
@@ -15,25 +15,23 @@
#include "testutilsxen.h"
static char *progname;
+static char cwd[PATH_MAX];
static char *abs_srcdir;
static virCapsPtr caps;
-#define MAX_FILE 4096
-
-static int testCompareFiles(const char *xml, const char *sexpr,
- int xendConfigVersion) {
- char xmlData[MAX_FILE];
- char sexprData[MAX_FILE];
+static int
+testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion)
+{
+ char *xmlData = NULL;
+ char *sexprData = NULL;
char *gotsexpr = NULL;
- char *xmlPtr = &(xmlData[0]);
- char *sexprPtr = &(sexprData[0]);
int ret = -1;
virDomainDefPtr def = NULL;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(xml, &xmlData) < 0)
goto fail;
- if (virtTestLoadFile(sexpr, &sexprPtr, MAX_FILE) < 0)
+ if (virtTestLoadFile(sexpr, &sexprData) < 0)
goto fail;
if (!(def = virDomainDefParseString(caps, xmlData,
@@ -51,8 +49,10 @@ static int testCompareFiles(const char *xml, const char *sexpr,
ret = 0;
fail:
- virDomainDefFree(def);
+ free(xmlData);
+ free(sexprData);
free(gotsexpr);
+ virDomainDefFree(def);
return ret;
}
@@ -64,23 +64,34 @@ struct testInfo {
int version;
};
-static int testCompareHelper(const void *data) {
+static int
+testCompareHelper(const void *data)
+{
+ int result = -1;
const struct testInfo *info = data;
- char xml[PATH_MAX];
- char args[PATH_MAX];
- snprintf(xml, PATH_MAX, "%s/xml2sexprdata/xml2sexpr-%s.xml",
- abs_srcdir, info->input);
- snprintf(args, PATH_MAX, "%s/xml2sexprdata/xml2sexpr-%s.sexpr",
- abs_srcdir, info->output);
- return testCompareFiles(xml, args, info->version);
-}
+ char *xml = NULL;
+ char *args = NULL;
+
+ if (virAsprintf(&xml, "%s/xml2sexprdata/xml2sexpr-%s.xml",
+ abs_srcdir, info->input) < 0 ||
+ virAsprintf(&args, "%s/xml2sexprdata/xml2sexpr-%s.sexpr",
+ abs_srcdir, info->output) < 0) {
+ goto cleanup;
+ }
+ result = testCompareFiles(xml, args, info->version);
+
+cleanup:
+ free(xml);
+ free(args);
+
+ return result;
+}
static int
mymain(int argc, char **argv)
{
int ret = 0;
- char cwd[PATH_MAX];
progname = argv[0];
diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c
index a5fb4f7..1b58db8 100644
--- a/tests/xml2vmxtest.c
+++ b/tests/xml2vmxtest.c
@@ -11,13 +11,12 @@
# include "testutils.h"
# include "vmx/vmx.h"
-static char *progname = NULL;
-static char *abs_srcdir = NULL;
-static virCapsPtr caps = NULL;
+static char *progname;
+static char cwd[PATH_MAX];
+static char *abs_srcdir;
+static virCapsPtr caps;
static virVMXContext ctx;
-# define MAX_FILE 4096
-
static void
testCapsInit(void)
{
@@ -71,18 +70,16 @@ static int
testCompareFiles(const char *xml, const char *vmx, int virtualHW_version)
{
int result = -1;
- char xmlData[MAX_FILE];
- char vmxData[MAX_FILE];
+ char *xmlData = NULL;
+ char *vmxData = NULL;
char *formatted = NULL;
- char *xmlPtr = &(xmlData[0]);
- char *vmxPtr = &(vmxData[0]);
virDomainDefPtr def = NULL;
- if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0) {
+ if (virtTestLoadFile(xml, &xmlData) < 0) {
goto failure;
}
- if (virtTestLoadFile(vmx, &vmxPtr, MAX_FILE) < 0) {
+ if (virtTestLoadFile(vmx, &vmxData) < 0) {
goto failure;
}
@@ -106,6 +103,8 @@ testCompareFiles(const char *xml, const char *vmx, int virtualHW_version)
result = 0;
failure:
+ VIR_FREE(xmlData);
+ VIR_FREE(vmxData);
VIR_FREE(formatted);
virDomainDefFree(def);
@@ -121,16 +120,25 @@ struct testInfo {
static int
testCompareHelper(const void *data)
{
+ int result = -1;
const struct testInfo *info = data;
- char xml[PATH_MAX];
- char vmx[PATH_MAX];
+ char *xml = NULL;
+ char *vmx = NULL;
+
+ if (virAsprintf(&xml, "%s/xml2vmxdata/xml2vmx-%s.xml", abs_srcdir,
+ info->input) < 0 ||
+ virAsprintf(&vmx, "%s/xml2vmxdata/xml2vmx-%s.vmx", abs_srcdir,
+ info->output) < 0) {
+ goto cleanup;
+ }
+
+ result = testCompareFiles(xml, vmx, info->virtualHW_version);
- snprintf(xml, PATH_MAX, "%s/xml2vmxdata/xml2vmx-%s.xml", abs_srcdir,
- info->input);
- snprintf(vmx, PATH_MAX, "%s/xml2vmxdata/xml2vmx-%s.vmx", abs_srcdir,
- info->output);
+ cleanup:
+ VIR_FREE(xml);
+ VIR_FREE(vmx);
- return testCompareFiles(xml, vmx, info->virtualHW_version);
+ return result;
}
static int
@@ -201,7 +209,6 @@ static int
mymain(int argc, char **argv)
{
int result = 0;
- char cwd[PATH_MAX];
progname = argv[0];
--
1.7.0.4
13 years, 11 months
[libvirt] [PATCH] virsh: avoid compiler warning on mingw
by Eric Blake
We don't use gnulib's sanitizations for vfprintf, but vshDebug
was used with %zu, which means that it would fail on mingw.
Thank goodness the compiler indirectly caught this for us :)
virsh.c: In function 'vshDebug':
virsh.c:12105:5: warning: function might be possible candidate for
'ms_printf' format attribute [-Wmissing-format-attribute]
since mingw <stdio.h> hasn't yet added gcc attributes to vfprintf.
* tools/virsh.c (vshDebug): Avoid vfprintf.
(vshPrintExtra): Use lighter-weight fputs.
Reported by Matthias Bolte.
---
tools/virsh.c | 11 +++++++++--
1 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/tools/virsh.c b/tools/virsh.c
index 27140f3..0212b99 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -12096,6 +12096,7 @@ static void
vshDebug(vshControl *ctl, int level, const char *format, ...)
{
va_list ap;
+ char *str;
va_start(ap, format);
vshOutputLogFile(ctl, VSH_ERR_DEBUG, format, ap);
@@ -12105,8 +12106,14 @@ vshDebug(vshControl *ctl, int level, const char *format, ...)
return;
va_start(ap, format);
- vfprintf(stdout, format, ap);
+ if (virVasprintf(&str, format, ap) < 0) {
+ /* Skip debug messages on low memory */
+ va_end(ap);
+ return;
+ }
va_end(ap);
+ fputs(str, stdout);
+ VIR_FREE(str);
}
static void
@@ -12125,7 +12132,7 @@ vshPrintExtra(vshControl *ctl, const char *format, ...)
return;
}
va_end(ap);
- fprintf(stdout, "%s", str);
+ fputs(str, stdout);
VIR_FREE(str);
}
--
1.7.4.4
13 years, 11 months
[libvirt] [PATCH 0/4] Unify error messages
by Michal Privoznik
As libvirt error codes evolved over time, it was more unclear to know, which
code should be use in what cases. This patch series tries to correct those
places, so we don't say it is internal error, when in fact incorrect XML was
given. Also some error messages were changed to give more hints.
Michal Privoznik (4):
Fix error messages codes when TypeFromString fails
New error code VIR_ERR_XML_CONFIG
Improve incorrect root element error messages
Change XML parsing error codes when details are presented.
include/libvirt/virterror.h | 1 +
src/conf/cpu_conf.c | 13 ++--
src/conf/domain_conf.c | 165 +++++++++++++++++++-----------------
src/conf/interface_conf.c | 56 ++++++------
src/conf/network_conf.c | 20 +++--
src/conf/node_device_conf.c | 10 ++-
src/conf/nwfilter_conf.c | 16 ++--
src/conf/secret_conf.c | 14 ++--
src/conf/storage_conf.c | 66 ++++++++-------
src/conf/storage_encryption_conf.c | 14 ++--
src/security/virt-aa-helper.c | 3 +-
src/test/test_driver.c | 13 ++-
src/util/virterror.c | 6 ++
src/util/xml.c | 2 +-
14 files changed, 218 insertions(+), 181 deletions(-)
--
1.7.4.4
13 years, 11 months
[libvirt] [PATCH V3] xen: check if device is assigned to guest before reattaching
by Yufang Zhang
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=664059
This is the version3 patch for BZ#664059. Reattaching pci device back to
host without destroying guest or detaching device from guest would cause
host to crash. This patch adds a check before doing device reattach. If
the device is being assigned to guest, libvirt refuses to reattach device
to host. The patch only works for Xen, for it just checks xenstore to get
pci device information. This version fixes some formatting problem and moves
lock-unlock out of loop so that the check is atomic.
Signed-off-by: Yufang Zhang <yuzhang(a)redhat.com>
---
src/xen/xen_driver.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 67 insertions(+), 0 deletions(-)
diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
index 2a07b7b..dd94fbc 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -1927,11 +1927,70 @@ out:
}
static int
+xenUnifiedNodeDeviceAssignedDomainId (virNodeDevicePtr dev)
+{
+ int numdomains;
+ int ret = -1, i;
+ int *ids = NULL;
+ char *bdf = NULL;
+ char *xref = NULL;
+ unsigned int domain, bus, slot, function;
+ virConnectPtr conn = dev->conn;
+ xenUnifiedPrivatePtr priv = conn->privateData;
+
+ /* Get active domains */
+ numdomains = xenUnifiedNumOfDomains(conn);
+ if (numdomains < 0) {
+ return ret;
+ }
+ if (numdomains > 0){
+ if (VIR_ALLOC_N(ids, numdomains) < 0) {
+ virReportOOMError();
+ goto out;
+ }
+ if ((numdomains = xenUnifiedListDomains(conn, &ids[0], numdomains)) < 0) {
+ goto out;
+ }
+ }
+
+ /* Get pci bdf */
+ if (xenUnifiedNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
+ goto out;
+
+ if (virAsprintf(&bdf, "%04x:%02x:%02x.%0x",
+ domain, bus, slot, function) < 0) {
+ virReportOOMError();
+ goto out;
+ }
+
+ xenUnifiedLock(priv);
+ /* Check if bdf is assigned to one of active domains */
+ for (i = 0; i < numdomains; i++ ) {
+ xref = xenStoreDomainGetPCIID(conn, ids[i], bdf);
+ if (xref == NULL) {
+ continue;
+ } else {
+ ret = ids[i];
+ break;
+ }
+ }
+ xenUnifiedUnlock(priv);
+
+ VIR_FREE(xref);
+ VIR_FREE(bdf);
+out:
+ VIR_FREE(ids);
+
+ return ret;
+}
+
+static int
xenUnifiedNodeDeviceReAttach (virNodeDevicePtr dev)
{
pciDevice *pci;
unsigned domain, bus, slot, function;
int ret = -1;
+ int domid;
if (xenUnifiedNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
return -1;
@@ -1940,6 +1999,14 @@ xenUnifiedNodeDeviceReAttach (virNodeDevicePtr dev)
if (!pci)
return -1;
+ /* Check if device is assigned to an active guest */
+ if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
+ xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
+ _("Device %s has been assigned to guest %d"),
+ dev->name, domid);
+ goto out;
+ }
+
if (pciReAttachDevice(pci, NULL) < 0)
goto out;
--
1.7.4
13 years, 11 months
[libvirt] [PATCHv12 1/3] libvirt/qemu - support persistent modification of devices
by KAMEZAWA Hiroyuki
Rebased ont the latest git tree, which makes this work easier.
This series adds support for attach/detach/update disks of domain config.
==
This patch adds functions for modify domain's persistent definition.
To do error recovery in easy way, we use a copy of vmdef and update it.
The whole sequence will be:
make a copy of domain definition.
if (flags & MODIFY_CONFIG)
update copied domain definition
if (flags & MODIF_LIVE)
do hotplug.
if (no error)
save copied one to the file and update cached definition.
else
discard copied definition.
This patch is mixuture of Eric Blake's work and mine.
From: Eric Blake <eblake(a)redhat.com>
Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu(a)jp.fujitsu.com>
Changelog: v11 -> v12
- rebased and fixed hunks.
- renamed qemudDomain....to qemuDomain...
(virDomainObjCopyPersistentDef): make a copy of persistent vm definition
(qemuDomainAttach/Detach/UpdateDeviceConfig) : callbacks. now empty
(qemuDomainModifyDeviceFlags): add support for MODIFY_CONFIG and MODIFY_CURRENT
---
src/conf/domain_conf.c | 18 ++++++
src/conf/domain_conf.h | 3 +
src/libvirt_private.syms | 1 +
src/qemu/qemu_driver.c | 147 ++++++++++++++++++++++++++++++++++++----------
4 files changed, 137 insertions(+), 32 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 381e692..6c1098a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -9509,3 +9509,21 @@ cleanup:
return ret;
}
+
+
+virDomainDefPtr
+virDomainObjCopyPersistentDef(virCapsPtr caps, virDomainObjPtr dom)
+{
+ char *xml;
+ virDomainDefPtr cur, ret;
+
+ cur = virDomainObjGetPersistentDef(caps, dom);
+
+ xml = virDomainDefFormat(cur, VIR_DOMAIN_XML_WRITE_FLAGS);
+ if (!xml)
+ return NULL;
+
+ ret = virDomainDefParseString(caps, xml, VIR_DOMAIN_XML_READ_FLAGS);
+
+ return ret;
+}
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 6ea30b9..ddf111a 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1288,6 +1288,9 @@ int virDomainObjSetDefTransient(virCapsPtr caps,
virDomainDefPtr
virDomainObjGetPersistentDef(virCapsPtr caps,
virDomainObjPtr domain);
+virDomainDefPtr
+virDomainObjCopyPersistentDef(virCapsPtr caps, virDomainObjPtr dom);
+
void virDomainRemoveInactive(virDomainObjListPtr doms,
virDomainObjPtr dom);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ba7739d..f732431 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -287,6 +287,7 @@ virDomainMemballoonModelTypeToString;
virDomainNetDefFree;
virDomainNetTypeToString;
virDomainObjAssignDef;
+virDomainObjCopyPersistentDef;
virDomainObjSetDefTransient;
virDomainObjGetPersistentDef;
virDomainObjIsDuplicate;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 771678e..fd85c8a 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4073,6 +4073,46 @@ qemuDomainUpdateDeviceLive(virDomainObjPtr vm,
return ret;
}
+static int
+qemuDomainAttachDeviceConfig(virDomainDefPtr vmdef ATTRIBUTE_UNUSED,
+ virDomainDeviceDefPtr dev)
+{
+ switch (dev->type) {
+ default:
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("persistent attach of device is not supported"));
+ return -1;
+ }
+ return 0;
+}
+
+
+static int
+qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef ATTRIBUTE_UNUSED,
+ virDomainDeviceDefPtr dev)
+{
+ switch (dev->type) {
+ default:
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("persistent detach of device is not supported"));
+ return -1;
+ }
+ return 0;
+}
+
+static int
+qemuDomainUpdateDeviceConfig(virDomainDefPtr vmdef ATTRIBUTE_UNUSED,
+ virDomainDeviceDefPtr dev)
+{
+ switch (dev->type) {
+ default:
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("persistent update of device is not supported"));
+ return -1;
+ }
+ return 0;
+}
+
/* Actions for qemuDomainModifyDeviceFlags */
enum {
QEMU_DEVICE_ATTACH,
@@ -4088,6 +4128,7 @@ qemuDomainModifyDeviceFlags(virDomainPtr dom, const char *xml,
struct qemud_driver *driver = dom->conn->privateData;
virBitmapPtr qemuCaps = NULL;
virDomainObjPtr vm = NULL;
+ virDomainDefPtr vmdef = NULL;
virDomainDeviceDefPtr dev = NULL;
bool force = (flags & VIR_DOMAIN_DEVICE_MODIFY_FORCE) != 0;
int ret = -1;
@@ -4097,12 +4138,6 @@ qemuDomainModifyDeviceFlags(virDomainPtr dom, const char *xml,
(action == QEMU_DEVICE_UPDATE ?
VIR_DOMAIN_DEVICE_MODIFY_FORCE : 0), -1);
- if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
- qemuReportError(VIR_ERR_OPERATION_INVALID,
- "%s", _("cannot modify the persistent configuration of a domain"));
- return -1;
- }
-
qemuDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid);
if (!vm) {
@@ -4116,12 +4151,27 @@ qemuDomainModifyDeviceFlags(virDomainPtr dom, const char *xml,
if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0)
goto cleanup;
- if (!virDomainObjIsActive(vm)) {
- qemuReportError(VIR_ERR_OPERATION_INVALID,
- "%s", _("cannot attach device on inactive domain"));
- goto endjob;
+ if (virDomainObjIsActive(vm)) {
+ if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
+ flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;
+ } else {
+ if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
+ flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
+ /* check consistency between flags and the vm state */
+ if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
+ qemuReportError(VIR_ERR_OPERATION_INVALID,
+ "%s",
+ _("cannot do live update a device on "
+ "inactive domain"));
+ goto endjob;
+ }
}
+ if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
+ qemuReportError(VIR_ERR_OPERATION_INVALID,
+ "%s", _("cannot modify device on transient domain"));
+ goto endjob;
+ }
dev = virDomainDeviceDefParse(driver->caps, vm->def, xml,
VIR_DOMAIN_XML_INACTIVE);
if (dev == NULL)
@@ -4132,35 +4182,68 @@ qemuDomainModifyDeviceFlags(virDomainPtr dom, const char *xml,
&qemuCaps) < 0)
goto endjob;
- switch (action) {
- case QEMU_DEVICE_ATTACH:
- ret = qemuDomainAttachDeviceLive(vm, dev, dom, qemuCaps);
- break;
- case QEMU_DEVICE_DETACH:
- ret = qemuDomainDetachDeviceLive(vm, dev, dom, qemuCaps);
- break;
- case QEMU_DEVICE_UPDATE:
- ret = qemuDomainUpdateDeviceLive(vm, dev, dom, qemuCaps, force);
- break;
- default:
- qemuReportError(VIR_ERR_INTERNAL_ERROR,
- _("unknown domain modify action %d"), action);
- break;
- }
+ if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
+ /* Make a copy for updated domain. */
+ vmdef = virDomainObjCopyPersistentDef(driver->caps, vm);
+ if (!vmdef)
+ goto endjob;
+ switch (action) {
+ case QEMU_DEVICE_ATTACH:
+ ret = qemuDomainAttachDeviceConfig(vmdef, dev);
+ break;
+ case QEMU_DEVICE_DETACH:
+ ret = qemuDomainDetachDeviceConfig(vmdef, dev);
+ break;
+ case QEMU_DEVICE_UPDATE:
+ ret = qemuDomainUpdateDeviceConfig(vmdef, dev);
+ break;
+ default:
+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
+ _("unknown domain modify action %d"), action);
+ break;
+ }
+ } else
+ ret = 0;
- /*
- * update domain status forcibly because the domain status may be changed
- * even if we attach the device failed. For example, a new controller may
- * be created.
- */
- if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
- ret = -1;
+ if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE)) {
+ switch (action) {
+ case QEMU_DEVICE_ATTACH:
+ ret = qemuDomainAttachDeviceLive(vm, dev, dom, qemuCaps);
+ break;
+ case QEMU_DEVICE_DETACH:
+ ret = qemuDomainDetachDeviceLive(vm, dev, dom, qemuCaps);
+ break;
+ case QEMU_DEVICE_UPDATE:
+ ret = qemuDomainUpdateDeviceLive(vm, dev, dom, qemuCaps, force);
+ break;
+ default:
+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
+ _("unknown domain modify action %d"), action);
+ break;
+ }
+ /*
+ * update domain status forcibly because the domain status may be
+ * changed even if we attach the device failed. For example, a
+ * For example, a new controller may be created.
+ */
+ if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
+ ret = -1;
+ }
+ /* Finally, if no error until here, we can save config. */
+ if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
+ ret = virDomainSaveConfig(driver->configDir, vmdef);
+ if (!ret) {
+ virDomainObjAssignDef(vm, vmdef, false);
+ vmdef = NULL;
+ }
+ }
endjob:
if (qemuDomainObjEndJob(vm) == 0)
vm = NULL;
cleanup:
+ virDomainDefFree(vmdef);
virDomainDeviceDefFree(dev);
if (vm)
virDomainObjUnlock(vm);
--
1.7.4.1
13 years, 11 months
[libvirt] [PATCH] Xen: Do not generate net ifname if domain is inactive
by Jim Fehlig
The xend driver will generate a virDomainNetDef ifname if one is not
specified in xend sexpr, even if domain is inactive. The result is
network interface XML containing 'vif-1.Y' on dev attribute of target
element, e.g.
<interface type='bridge'>
<target dev='vif-1.0'/>
...
This patch changes the behavior to only generate the ifname if not
specified in xend sexpr *and* domain is not inactive (id != -1).
---
src/xenxs/xen_sxpr.c | 13 +++++++++----
1 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/src/xenxs/xen_sxpr.c b/src/xenxs/xen_sxpr.c
index d2ec370..66df786 100644
--- a/src/xenxs/xen_sxpr.c
+++ b/src/xenxs/xen_sxpr.c
@@ -547,12 +547,17 @@ xenParseSxprNets(virDomainDefPtr def,
}
tmp = sexpr_node(node, "device/vif/vifname");
- if (!tmp) {
+ /* If vifname is specified in xend config, include it in net
+ * definition regardless of domain state. If vifname is not
+ * specified, only generate one if domain is active (id != -1). */
+ if (tmp) {
+ if (!(net->ifname = strdup(tmp)))
+ goto no_memory;
+ } else if (def->id != -1) {
snprintf(buf, sizeof(buf), "vif%d.%d", def->id, vif_index);
- tmp = buf;
+ if (!(net->ifname = strdup(buf)))
+ goto no_memory;
}
- if (!(net->ifname = strdup(tmp)))
- goto no_memory;
tmp = sexpr_node(node, "device/vif/mac");
if (tmp) {
--
1.7.3.1
13 years, 11 months
[libvirt] [PATCH] Add warning message to XML definition files stored on disk
by Michal Privoznik
Users often edit XML domain file stored in configuration directory
thinking of modifying a domain/network/pool/etc. Thus it is wise
to let them know they are using the wrong way and give them hint.
---
src/conf/domain_conf.c | 2 ++
src/conf/network_conf.c | 1 +
src/conf/nwfilter_conf.c | 3 +++
src/conf/storage_conf.c | 2 ++
src/libvirt_private.syms | 1 +
src/util/util.c | 22 ++++++++++++++++++++++
src/util/util.h | 2 ++
7 files changed, 33 insertions(+), 0 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 2a681d9..4cd1228 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8543,6 +8543,7 @@ int virDomainSaveConfig(const char *configDir,
VIR_DOMAIN_XML_WRITE_FLAGS)))
goto cleanup;
+ virSavePrependWarning(&xml);
if (virDomainSaveXML(configDir, def, xml))
goto cleanup;
@@ -8563,6 +8564,7 @@ int virDomainSaveStatus(virCapsPtr caps,
if (!(xml = virDomainObjFormat(caps, obj, flags)))
goto cleanup;
+ virSavePrependWarning(&xml);
if (virDomainSaveXML(statusDir, obj->def, xml))
goto cleanup;
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 5738757..82f10e6 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -959,6 +959,7 @@ int virNetworkSaveConfig(const char *configDir,
if (!(xml = virNetworkDefFormat(def)))
goto cleanup;
+ virSavePrependWarning(&xml);
if (virNetworkSaveXML(configDir, def, xml))
goto cleanup;
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 09dc32b..fcd0b24 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2282,6 +2282,7 @@ int virNWFilterSaveConfig(const char *configDir,
if (!(xml = virNWFilterDefFormat(def)))
goto cleanup;
+ virSavePrependWarning(&xml);
if (virNWFilterSaveXML(configDir, def, xml))
goto cleanup;
@@ -2635,6 +2636,8 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
return -1;
}
+ virSavePrependWarning(&xml);
+
if ((fd = open(nwfilter->configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 116898d..96aa567 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1542,6 +1542,8 @@ virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
return -1;
}
+ virSavePrependWarning(&xml);
+
if ((fd = open(pool->configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1b22be6..f2c005e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -946,6 +946,7 @@ virRandom;
virRandomInitialize;
virRun;
virRunWithHook;
+virSavePrependWarning;
virSetBlocking;
virSetCloseExec;
virSetInherit;
diff --git a/src/util/util.c b/src/util/util.c
index 1bb0328..c6a4634 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -3204,3 +3204,25 @@ bool virIsDevMapperDevice(const char *devname ATTRIBUTE_UNUSED)
return false;
}
#endif
+
+int virSavePrependWarning(char **xml) {
+ char *retPtr = NULL;
+ int ret;
+
+ ret = virAsprintf(&retPtr, "%s\n%s",
+ _("<!--\n\
+WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE\n\
+OVERWRITTEN AND LOST. Changes to xml configurations should be made using virsh\n\
+edit or other application using the libvirt API. See:\n\
+\n\
+http://wiki.libvirt.org/wiki/index.php?title=FAQ#Where_are_VM_config_files_stored.3F_How_do_I_edit_a_VM.27s_XML_config.3F\n\
+-->\n"),
+ *xml);
+
+ if (ret < 0)
+ return ret;
+
+ VIR_FREE(*xml);
+ *xml = retPtr;
+ return ret;
+}
diff --git a/src/util/util.h b/src/util/util.h
index d320c40..a6a91de 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -299,4 +299,6 @@ int virBuildPathInternal(char **path, ...) ATTRIBUTE_SENTINEL;
char *virTimestamp(void);
bool virIsDevMapperDevice(const char *devname) ATTRIBUTE_NONNULL(1);
+
+int virSavePrependWarning(char **xml);
#endif /* __VIR_UTIL_H__ */
--
1.7.4.4
13 years, 11 months
[libvirt] [PATCH v2 0/5] Add TXT record and hosts support for DNS service and dnsmasq command-line regression testing
by Michal Novotny
Hi,
this is the patch to introduce the TXT record and DNS hosts support
for the DNS service on the virtual network. This can be defined using
the txt-record subelement in the dns element of the network XML
description. The definition of TXT record names containing spaces
is rejected with the error message that TXT record names in DNS
doesn't support spaces.
Also, regression testing for the dnsmasq command line has been
added to test whether the dnsmasq command-line is correct or not.
The new XML definition syntax is:
<dns>
<txt-record name="example" value="example value" />
<host ip='192.168.122.1'>
<hostname>gateway</hostname>
<hostname>host</hostname>
</host>
</dns>
Where multiple host elements can be defined to put the aliases
for specified IP addresses.
The patch series has been tested for the configuration and it
was working fine and also RelaxNG schema with the tests have
been both altered to add test cases to test those patches.
All of the patches passed the make, make check and make
syntax-check commands and it has been tested for the definition
described above and it was working fine (tested using dig for
DNS TXT record and nslookup in the guest for the DNS hosts).
Michal
Signed-off-by: Michal Novotny <minovotn(a)redhat.com>
Michal Novotny (5):
Network: Add TXT record support for virtual DNS service
Network: Add regression tests for the command line arguments
Network: Move dnsmasqContext creation to
networkSaveDnsmasqHostsfile() and pass to dnsmasq only if
applicable
Network: Add additional hosts internal infrastructure
Network: Add support for DNS hosts definition in the network XML
docs/formatnetwork.html.in | 31 +++-
docs/schemas/network.rng | 20 ++
src/conf/network_conf.c | 183 ++++++++++++++
src/conf/network_conf.h | 25 ++
src/libvirt_private.syms | 1 +
src/network/bridge_driver.c | 103 ++++++--
src/network/bridge_driver.h | 3 +
src/util/dnsmasq.c | 266 +++++++++++++++++++-
src/util/dnsmasq.h | 22 ++-
tests/Makefile.am | 11 +
tests/networkxml2argvdata/isolated-network.argv | 1 +
tests/networkxml2argvdata/isolated-network.xml | 11 +
.../networkxml2argvdata/nat-network-dns-hosts.argv | 1 +
.../networkxml2argvdata/nat-network-dns-hosts.xml | 19 ++
.../nat-network-dns-txt-record.argv | 1 +
.../nat-network-dns-txt-record.xml | 24 ++
tests/networkxml2argvdata/nat-network.argv | 1 +
tests/networkxml2argvdata/nat-network.xml | 21 ++
tests/networkxml2argvdata/netboot-network.argv | 1 +
tests/networkxml2argvdata/netboot-network.xml | 14 +
.../networkxml2argvdata/netboot-proxy-network.argv | 1 +
.../networkxml2argvdata/netboot-proxy-network.xml | 13 +
tests/networkxml2argvdata/routed-network.argv | 1 +
tests/networkxml2argvdata/routed-network.xml | 9 +
tests/networkxml2argvtest.c | 120 +++++++++
tests/networkxml2xmlin/nat-network-dns-hosts.xml | 27 ++
.../nat-network-dns-txt-record.xml | 24 ++
tests/networkxml2xmlout/nat-network-dns-hosts.xml | 27 ++
.../nat-network-dns-txt-record.xml | 24 ++
tests/networkxml2xmltest.c | 2 +
30 files changed, 974 insertions(+), 33 deletions(-)
create mode 100644 tests/networkxml2argvdata/isolated-network.argv
create mode 100644 tests/networkxml2argvdata/isolated-network.xml
create mode 100644 tests/networkxml2argvdata/nat-network-dns-hosts.argv
create mode 100644 tests/networkxml2argvdata/nat-network-dns-hosts.xml
create mode 100644 tests/networkxml2argvdata/nat-network-dns-txt-record.argv
create mode 100644 tests/networkxml2argvdata/nat-network-dns-txt-record.xml
create mode 100644 tests/networkxml2argvdata/nat-network.argv
create mode 100644 tests/networkxml2argvdata/nat-network.xml
create mode 100644 tests/networkxml2argvdata/netboot-network.argv
create mode 100644 tests/networkxml2argvdata/netboot-network.xml
create mode 100644 tests/networkxml2argvdata/netboot-proxy-network.argv
create mode 100644 tests/networkxml2argvdata/netboot-proxy-network.xml
create mode 100644 tests/networkxml2argvdata/routed-network.argv
create mode 100644 tests/networkxml2argvdata/routed-network.xml
create mode 100644 tests/networkxml2argvtest.c
create mode 100644 tests/networkxml2xmlin/nat-network-dns-hosts.xml
create mode 100644 tests/networkxml2xmlin/nat-network-dns-txt-record.xml
create mode 100644 tests/networkxml2xmlout/nat-network-dns-hosts.xml
create mode 100644 tests/networkxml2xmlout/nat-network-dns-txt-record.xml
--
1.7.3.2
13 years, 11 months
[libvirt] libvirt-0.8.8 libvir: Security Labeling error : unable to set user and group to '0:0' on '...': Permission denied
by Nikola Ciprich
Hello,
I just noticed strange problem. While trying to start vm with cdrom image on a samba mount, I get:
libvir: Security Labeling error : unable to set user and group to '0:0' on '/home/cluster/images/interinformatics/CentOS-5.4-i386-bin-DVD.iso': Permission denied
I googled this up, and found some solutions, but none seems to be suitable for me. The biggest problem is I'm not using selinux at all, its completely disabled, and I don't have a chance to change mount parameters as well...
Is there any way to workaround this?
thanks a lot in advance!
BR
nik
--
-------------------------------------
Ing. Nikola CIPRICH
LinuxBox.cz, s.r.o.
28. rijna 168, 709 01 Ostrava
tel.: +420 596 603 142
fax: +420 596 621 273
mobil: +420 777 093 799
www.linuxbox.cz
mobil servis: +420 737 238 656
email servis: servis(a)linuxbox.cz
-------------------------------------
13 years, 11 months
[libvirt] [PATCH] network: fix return value of hostsFileWrite
by Laine Stump
The lone caller to hostsFileWrite (and the callers for at least 3
levels up the return stack) assume that the return value will be < 0
on failure. However, hostsFileWrite returns 0 on success, and a
positive errno on failure. This patch changes hostsFileWrite to return
-errno on failure.
---
src/util/dnsmasq.c | 12 ++++++------
1 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/util/dnsmasq.c b/src/util/dnsmasq.c
index be230e1..2ba9355 100644
--- a/src/util/dnsmasq.c
+++ b/src/util/dnsmasq.c
@@ -159,19 +159,19 @@ hostsfileWrite(const char *path,
return rc;
if (virAsprintf(&tmp, "%s.new", path) < 0)
- return ENOMEM;
+ return -ENOMEM;
if (!(f = fopen(tmp, "w"))) {
istmp = false;
if (!(f = fopen(path, "w"))) {
- rc = errno;
+ rc = -errno;
goto cleanup;
}
}
for (i = 0; i < nhosts; i++) {
if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
- rc = errno;
+ rc = -errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
@@ -182,19 +182,19 @@ hostsfileWrite(const char *path,
}
if (VIR_FCLOSE(f) == EOF) {
- rc = errno;
+ rc = -errno;
goto cleanup;
}
if (istmp) {
if (rename(tmp, path) < 0) {
- rc = errno;
+ rc = -errno;
unlink(tmp);
goto cleanup;
}
if (unlink(tmp) < 0) {
- rc = errno;
+ rc = -errno;
goto cleanup;
}
}
--
1.7.3.4
13 years, 11 months