util: sysinfo: Reduce amount of conditionally compiled code

Whole implementations along with helper totalling screens of code were
conditionally compiled. That made the code totally unreadable and
untestable. Rename functions to have the architecture in the name so
that all can be compiled at the same time and introduce header to allow
testing them all.
This commit is contained in:
Peter Krempa 2017-03-06 15:14:45 +01:00
parent 2149d405a0
commit b38c6b6ad3
4 changed files with 132 additions and 79 deletions

View File

@ -173,7 +173,7 @@ UTIL_SOURCES = \
util/virstorageencryption.c util/virstorageencryption.h \ util/virstorageencryption.c util/virstorageencryption.h \
util/virstoragefile.c util/virstoragefile.h \ util/virstoragefile.c util/virstoragefile.h \
util/virstring.h util/virstring.c \ util/virstring.h util/virstring.c \
util/virsysinfo.c util/virsysinfo.h \ util/virsysinfo.c util/virsysinfo.h util/virsysinfopriv.h \
util/virsystemd.c util/virsystemd.h util/virsystemdpriv.h \ util/virsystemd.c util/virsystemd.h util/virsystemdpriv.h \
util/virthread.c util/virthread.h \ util/virthread.c util/virthread.h \
util/virthreadjob.c util/virthreadjob.h \ util/virthreadjob.c util/virthreadjob.h \

View File

@ -2561,6 +2561,13 @@ virSysinfoSetup;
virSysinfoSystemDefFree; virSysinfoSystemDefFree;
# util/virsysinfopriv.h
virSysinfoReadARM;
virSysinfoReadPPC;
virSysinfoReadS390;
virSysinfoReadX86;
# util/virsystemd.h # util/virsystemd.h
virSystemdCanHibernate; virSystemdCanHibernate;
virSystemdCanHybridSleep; virSystemdCanHybridSleep;

View File

@ -37,6 +37,9 @@
#include "virfile.h" #include "virfile.h"
#include "virstring.h" #include "virstring.h"
#define __VIR_SYSINFO_PRIV_H_ALLOW__
#include "virsysinfopriv.h"
#define VIR_FROM_THIS VIR_FROM_SYSINFO #define VIR_FROM_THIS VIR_FROM_SYSINFO
@ -156,17 +159,9 @@ void virSysinfoDefFree(virSysinfoDefPtr def)
VIR_FREE(def); VIR_FREE(def);
} }
/**
* virSysinfoRead:
*
* Tries to read the SMBIOS information from the current host
*
* Returns: a filled up sysinfo structure or NULL in case of error
*/
#if defined(__powerpc__)
static int static int
virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef) virSysinfoParsePPCSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
{ {
int ret = -1; int ret = -1;
char *eol = NULL; char *eol = NULL;
@ -218,7 +213,7 @@ virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
} }
static int static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret) virSysinfoParsePPCProcessor(const char *base, virSysinfoDefPtr ret)
{ {
const char *cur; const char *cur;
char *eol, *tmp_base; char *eol, *tmp_base;
@ -265,7 +260,7 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
/* virSysinfoRead for PowerPC /* virSysinfoRead for PowerPC
* Gathers sysinfo data from /proc/cpuinfo */ * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr virSysinfoDefPtr
virSysinfoRead(void) virSysinfoReadPPC(void)
{ {
virSysinfoDefPtr ret = NULL; virSysinfoDefPtr ret = NULL;
char *outbuf = NULL; char *outbuf = NULL;
@ -281,10 +276,10 @@ virSysinfoRead(void)
ret->nprocessor = 0; ret->nprocessor = 0;
ret->processor = NULL; ret->processor = NULL;
if (virSysinfoParseProcessor(outbuf, ret) < 0) if (virSysinfoParsePPCProcessor(outbuf, ret) < 0)
goto no_memory; goto no_memory;
if (virSysinfoParseSystem(outbuf, &ret->system) < 0) if (virSysinfoParsePPCSystem(outbuf, &ret->system) < 0)
goto no_memory; goto no_memory;
return ret; return ret;
@ -294,9 +289,9 @@ virSysinfoRead(void)
return NULL; return NULL;
} }
#elif defined(__arm__) || defined(__aarch64__)
static int static int
virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef) virSysinfoParseARMSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
{ {
int ret = -1; int ret = -1;
char *eol = NULL; char *eol = NULL;
@ -348,7 +343,7 @@ virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
} }
static int static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret) virSysinfoParseARMProcessor(const char *base, virSysinfoDefPtr ret)
{ {
const char *cur; const char *cur;
char *eol, *tmp_base; char *eol, *tmp_base;
@ -397,7 +392,7 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
/* virSysinfoRead for ARMv7 /* virSysinfoRead for ARMv7
* Gathers sysinfo data from /proc/cpuinfo */ * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr virSysinfoDefPtr
virSysinfoRead(void) virSysinfoReadARM(void)
{ {
virSysinfoDefPtr ret = NULL; virSysinfoDefPtr ret = NULL;
char *outbuf = NULL; char *outbuf = NULL;
@ -413,10 +408,10 @@ virSysinfoRead(void)
ret->nprocessor = 0; ret->nprocessor = 0;
ret->processor = NULL; ret->processor = NULL;
if (virSysinfoParseProcessor(outbuf, ret) < 0) if (virSysinfoParseARMProcessor(outbuf, ret) < 0)
goto no_memory; goto no_memory;
if (virSysinfoParseSystem(outbuf, &ret->system) < 0) if (virSysinfoParseARMSystem(outbuf, &ret->system) < 0)
goto no_memory; goto no_memory;
return ret; return ret;
@ -427,13 +422,12 @@ virSysinfoRead(void)
} }
#elif defined(__s390__) || defined(__s390x__)
VIR_WARNINGS_NO_WLOGICALOP_STRCHR VIR_WARNINGS_NO_WLOGICALOP_STRCHR
static char * static char *
virSysinfoParseDelimited(const char *base, const char *name, char **value, virSysinfoParseS390Delimited(const char *base, const char *name, char **value,
char delim1, char delim2) char delim1, char delim2)
{ {
const char *start; const char *start;
char *end; char *end;
@ -453,13 +447,13 @@ virSysinfoParseDelimited(const char *base, const char *name, char **value,
} }
static char * static char *
virSysinfoParseLine(const char *base, const char *name, char **value) virSysinfoParseS390Line(const char *base, const char *name, char **value)
{ {
return virSysinfoParseDelimited(base, name, value, ':', '\n'); return virSysinfoParseS390Delimited(base, name, value, ':', '\n');
} }
static int static int
virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef) virSysinfoParseS390System(const char *base, virSysinfoSystemDefPtr *sysdef)
{ {
int ret = -1; int ret = -1;
virSysinfoSystemDefPtr def; virSysinfoSystemDefPtr def;
@ -467,16 +461,13 @@ virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
if (VIR_ALLOC(def) < 0) if (VIR_ALLOC(def) < 0)
return ret; return ret;
if (!virSysinfoParseLine(base, "Manufacturer", if (!virSysinfoParseS390Line(base, "Manufacturer", &def->manufacturer))
&def->manufacturer))
goto cleanup; goto cleanup;
if (!virSysinfoParseLine(base, "Type", if (!virSysinfoParseS390Line(base, "Type", &def->family))
&def->family))
goto cleanup; goto cleanup;
if (!virSysinfoParseLine(base, "Sequence Code", if (!virSysinfoParseS390Line(base, "Sequence Code", &def->serial))
&def->serial))
goto cleanup; goto cleanup;
if (!def->manufacturer && !def->product && !def->version && if (!def->manufacturer && !def->product && !def->version &&
@ -494,7 +485,7 @@ virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
} }
static int static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret) virSysinfoParseS390Processor(const char *base, virSysinfoDefPtr ret)
{ {
char *tmp_base; char *tmp_base;
char *manufacturer = NULL; char *manufacturer = NULL;
@ -502,28 +493,28 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
int result = -1; int result = -1;
virSysinfoProcessorDefPtr processor; virSysinfoProcessorDefPtr processor;
if (!(tmp_base = virSysinfoParseLine(base, "vendor_id", &manufacturer))) if (!(tmp_base = virSysinfoParseS390Line(base, "vendor_id", &manufacturer)))
goto cleanup; goto cleanup;
/* Find processor N: line and gather the processor manufacturer, /* Find processor N: line and gather the processor manufacturer,
version, serial number, and family */ version, serial number, and family */
while ((tmp_base = strstr(tmp_base, "processor ")) while ((tmp_base = strstr(tmp_base, "processor "))
&& (tmp_base = virSysinfoParseLine(tmp_base, "processor ", && (tmp_base = virSysinfoParseS390Line(tmp_base, "processor ",
&procline))) { &procline))) {
if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0) if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
goto cleanup; goto cleanup;
processor = &ret->processor[ret->nprocessor - 1]; processor = &ret->processor[ret->nprocessor - 1];
if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0) if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0)
goto cleanup; goto cleanup;
if (!virSysinfoParseDelimited(procline, "version", if (!virSysinfoParseS390Delimited(procline, "version",
&processor->processor_version, &processor->processor_version,
'=', ',') || '=', ',') ||
!virSysinfoParseDelimited(procline, "identification", !virSysinfoParseS390Delimited(procline, "identification",
&processor->processor_serial_number, &processor->processor_serial_number,
'=', ',') || '=', ',') ||
!virSysinfoParseDelimited(procline, "machine", !virSysinfoParseS390Delimited(procline, "machine",
&processor->processor_family, &processor->processor_family,
'=', '\n')) '=', '\n'))
goto cleanup; goto cleanup;
} }
result = 0; result = 0;
@ -537,7 +528,7 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
/* virSysinfoRead for s390x /* virSysinfoRead for s390x
* Gathers sysinfo data from /proc/sysinfo and /proc/cpuinfo */ * Gathers sysinfo data from /proc/sysinfo and /proc/cpuinfo */
virSysinfoDefPtr virSysinfoDefPtr
virSysinfoRead(void) virSysinfoReadS390(void)
{ {
virSysinfoDefPtr ret = NULL; virSysinfoDefPtr ret = NULL;
char *outbuf = NULL; char *outbuf = NULL;
@ -554,7 +545,7 @@ virSysinfoRead(void)
ret->nprocessor = 0; ret->nprocessor = 0;
ret->processor = NULL; ret->processor = NULL;
if (virSysinfoParseProcessor(outbuf, ret) < 0) if (virSysinfoParseS390Processor(outbuf, ret) < 0)
goto no_memory; goto no_memory;
/* Free buffer before reading next file */ /* Free buffer before reading next file */
@ -567,7 +558,7 @@ virSysinfoRead(void)
return NULL; return NULL;
} }
if (virSysinfoParseSystem(outbuf, &ret->system) < 0) if (virSysinfoParseS390System(outbuf, &ret->system) < 0)
goto no_memory; goto no_memory;
return ret; return ret;
@ -578,26 +569,6 @@ virSysinfoRead(void)
return NULL; return NULL;
} }
#elif defined(WIN32) || \
!(defined(__x86_64__) || \
defined(__i386__) || \
defined(__amd64__) || \
defined(__arm__) || \
defined(__aarch64__) || \
defined(__powerpc__))
virSysinfoDefPtr
virSysinfoRead(void)
{
/*
* this can probably be extracted from Windows using API or registry
* http://www.microsoft.com/whdc/system/platform/firmware/SMBIOS.mspx
*/
virReportSystemError(ENOSYS, "%s",
_("Host sysinfo extraction not supported on this platform"));
return NULL;
}
#else /* !WIN32 && x86 */
static int static int
virSysinfoParseBIOS(const char *base, virSysinfoBIOSDefPtr *bios) virSysinfoParseBIOS(const char *base, virSysinfoBIOSDefPtr *bios)
@ -653,7 +624,7 @@ virSysinfoParseBIOS(const char *base, virSysinfoBIOSDefPtr *bios)
} }
static int static int
virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef) virSysinfoParseX86System(const char *base, virSysinfoSystemDefPtr *sysdef)
{ {
int ret = -1; int ret = -1;
const char *cur, *eol = NULL; const char *cur, *eol = NULL;
@ -724,9 +695,9 @@ virSysinfoParseSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
} }
static int static int
virSysinfoParseBaseBoard(const char *base, virSysinfoParseX86BaseBoard(const char *base,
virSysinfoBaseBoardDefPtr *baseBoard, virSysinfoBaseBoardDefPtr *baseBoard,
size_t *nbaseBoard) size_t *nbaseBoard)
{ {
int ret = -1; int ret = -1;
const char *cur, *eol = NULL; const char *cur, *eol = NULL;
@ -802,7 +773,7 @@ virSysinfoParseBaseBoard(const char *base,
} }
static int static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret) virSysinfoParseX86Processor(const char *base, virSysinfoDefPtr ret)
{ {
const char *cur, *tmp_base; const char *cur, *tmp_base;
char *eol; char *eol;
@ -909,7 +880,7 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
} }
static int static int
virSysinfoParseMemory(const char *base, virSysinfoDefPtr ret) virSysinfoParseX86Memory(const char *base, virSysinfoDefPtr ret)
{ {
const char *cur, *tmp_base; const char *cur, *tmp_base;
char *eol; char *eol;
@ -1008,7 +979,7 @@ virSysinfoParseMemory(const char *base, virSysinfoDefPtr ret)
} }
virSysinfoDefPtr virSysinfoDefPtr
virSysinfoRead(void) virSysinfoReadX86(void)
{ {
char *path; char *path;
virSysinfoDefPtr ret = NULL; virSysinfoDefPtr ret = NULL;
@ -1037,20 +1008,20 @@ virSysinfoRead(void)
if (virSysinfoParseBIOS(outbuf, &ret->bios) < 0) if (virSysinfoParseBIOS(outbuf, &ret->bios) < 0)
goto error; goto error;
if (virSysinfoParseSystem(outbuf, &ret->system) < 0) if (virSysinfoParseX86System(outbuf, &ret->system) < 0)
goto error; goto error;
if (virSysinfoParseBaseBoard(outbuf, &ret->baseBoard, &ret->nbaseBoard) < 0) if (virSysinfoParseX86BaseBoard(outbuf, &ret->baseBoard, &ret->nbaseBoard) < 0)
goto error; goto error;
ret->nprocessor = 0; ret->nprocessor = 0;
ret->processor = NULL; ret->processor = NULL;
if (virSysinfoParseProcessor(outbuf, ret) < 0) if (virSysinfoParseX86Processor(outbuf, ret) < 0)
goto error; goto error;
ret->nmemory = 0; ret->nmemory = 0;
ret->memory = NULL; ret->memory = NULL;
if (virSysinfoParseMemory(outbuf, ret) < 0) if (virSysinfoParseX86Memory(outbuf, ret) < 0)
goto error; goto error;
cleanup: cleanup:
@ -1064,7 +1035,43 @@ virSysinfoRead(void)
ret = NULL; ret = NULL;
goto cleanup; goto cleanup;
} }
/**
* virSysinfoRead:
*
* Tries to read the SMBIOS information from the current host
*
* Returns: a filled up sysinfo structure or NULL in case of error
*/
virSysinfoDefPtr
virSysinfoRead(void)
{
#if defined(__powerpc__)
return virSysinfoReadPPC();
#elif defined(__arm__) || defined(__aarch64__)
return virSysinfoReadARM();
#elif defined(__s390__) || defined(__s390x__)
return virSysinfoReadS390();
#elif defined(WIN32) || \
!(defined(__x86_64__) || \
defined(__i386__) || \
defined(__amd64__) || \
defined(__arm__) || \
defined(__aarch64__) || \
defined(__powerpc__))
/*
* this can probably be extracted from Windows using API or registry
* http://www.microsoft.com/whdc/system/platform/firmware/SMBIOS.mspx
*/
virReportSystemError(ENOSYS, "%s",
_("Host sysinfo extraction not supported on this platform"));
return NULL;
#else /* !WIN32 && x86 */
return virSysinfoReadX86();
#endif /* !WIN32 && x86 */ #endif /* !WIN32 && x86 */
}
static void static void
virSysinfoBIOSFormat(virBufferPtr buf, virSysinfoBIOSDefPtr def) virSysinfoBIOSFormat(virBufferPtr buf, virSysinfoBIOSDefPtr def)

39
src/util/virsysinfopriv.h Normal file
View File

@ -0,0 +1,39 @@
/*
* virsysinfopriv.h: Header for functions tested in the test suite
*
* 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/>.
*
*/
#ifndef __VIR_SYSINFO_PRIV_H_ALLOW__
# error "virsysinfopriv.h may only be included by virsysinfo.c or test suites"
#endif
#ifndef __VIR_SYSINFO_PRIV_H__
# define __VIR_SYSINFO_PRIV_H__
virSysinfoDefPtr
virSysinfoReadPPC(void);
virSysinfoDefPtr
virSysinfoReadARM(void);
virSysinfoDefPtr
virSysinfoReadS390(void);
virSysinfoDefPtr
virSysinfoReadX86(void);
#endif /* __VIR_SYSINFO_PRIV_H__ */