util: Add virsysfs for handling sysfs files

By using this we are able to easily switch the sysfs path being
used (fake it).  This will not only help tests in the future but can
be also used from files where the code is duplicated currently.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2017-03-07 10:02:46 +01:00
parent 15b5e46364
commit a7b902c082
5 changed files with 343 additions and 0 deletions

View File

@ -173,6 +173,7 @@ UTIL_SOURCES = \
util/virstorageencryption.c util/virstorageencryption.h \
util/virstoragefile.c util/virstoragefile.h \
util/virstring.h util/virstring.c \
util/virsysfs.c util/virsysfs.h util/virsysfspriv.h \
util/virsysinfo.c util/virsysinfo.h util/virsysinfopriv.h \
util/virsystemd.c util/virsystemd.h util/virsystemdpriv.h \
util/virthread.c util/virthread.h \
@ -2585,6 +2586,7 @@ libvirt_setuid_rpc_client_la_SOURCES = \
util/virrandom.c \
util/virsocketaddr.c \
util/virstring.c \
util/virsysfs.c \
util/virsystemd.c \
util/virtime.c \
util/virthread.c \

View File

@ -2594,6 +2594,20 @@ virTrimSpaces;
virVasprintfInternal;
# util/virsysfs.h
virSysfsGetCpuValueBitmap;
virSysfsGetCpuValueInt;
virSysfsGetCpuValueString;
virSysfsGetCpuValueUint;
virSysfsGetNodeValueBitmap;
virSysfsGetNodeValueString;
virSysfsGetSystemPath;
virSysfsGetValueBitmap;
virSysfsGetValueInt;
virSysfsGetValueString;
virSysfsSetSystemPath;
# util/virsysinfo.h
virSysinfoBaseBoardDefClear;
virSysinfoBIOSDefFree;

229
src/util/virsysfs.c Normal file
View File

@ -0,0 +1,229 @@
/*
* virsysfs.c: Helper functions for manipulating sysfs files
*
* 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/>.
*
* Author: Martin Kletzander <mkletzan@redhat.com>
*/
#include <config.h>
#include "internal.h"
#include "virsysfspriv.h"
#include "viralloc.h"
#include "virfile.h"
#include "virlog.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("util.sysfs");
#define VIR_SYSFS_VALUE_MAXLEN 8192
#define SYSFS_SYSTEM_PATH "/sys/devices/system"
static const char *sysfs_system_path = SYSFS_SYSTEM_PATH;
void virSysfsSetSystemPath(const char *path)
{
if (path)
sysfs_system_path = path;
else
sysfs_system_path = SYSFS_SYSTEM_PATH;
}
const char *
virSysfsGetSystemPath(void)
{
return sysfs_system_path;
}
int
virSysfsGetValueInt(const char *file,
int *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/%s", sysfs_system_path, file) < 0)
return -1;
ret = virFileReadValueInt(path, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetValueString(const char *file,
char **value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/%s", sysfs_system_path, file) < 0)
return -1;
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
goto cleanup;
ret = 0;
cleanup:
VIR_FREE(path);
return ret;
}
int
virSysfsGetValueBitmap(const char *file,
virBitmapPtr *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/%s", sysfs_system_path, file) < 0)
return -1;
ret = virFileReadValueBitmap(path, VIR_SYSFS_VALUE_MAXLEN, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueInt(unsigned int cpu,
const char *file,
int *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
ret = virFileReadValueInt(path, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueUint(unsigned int cpu,
const char *file,
unsigned int *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
ret = virFileReadValueUint(path, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueString(unsigned int cpu,
const char *file,
char **value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
goto cleanup;
ret = 0;
cleanup:
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueBitmap(unsigned int cpu,
const char *file,
virBitmapPtr *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
ret = virFileReadValueBitmap(path, VIR_SYSFS_VALUE_MAXLEN, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetNodeValueString(unsigned int node,
const char *file,
char **value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/node/node%u/%s", sysfs_system_path, node, file) < 0)
return -1;
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
goto cleanup;
ret = 0;
cleanup:
VIR_FREE(path);
return ret;
}
int
virSysfsGetNodeValueBitmap(unsigned int node,
const char *file,
virBitmapPtr *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/node/node%u/%s", sysfs_system_path, node, file) < 0)
return -1;
ret = virFileReadValueBitmap(path, VIR_SYSFS_VALUE_MAXLEN, value);
VIR_FREE(path);
return ret;
}

70
src/util/virsysfs.h Normal file
View File

@ -0,0 +1,70 @@
/*
* virsysfs.h: Helper functions for manipulating sysfs files
*
* 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/>.
*
* Author: Martin Kletzander <mkletzan@redhat.com>
*/
#ifndef __VIR_SYSFS_H__
# define __VIR_SYSFS_H__
# include "internal.h"
# include "virbitmap.h"
const char * virSysfsGetSystemPath(void);
int
virSysfsGetValueInt(const char *file,
int *value);
int
virSysfsGetValueString(const char *file,
char **value);
int
virSysfsGetValueBitmap(const char *file,
virBitmapPtr *value);
int
virSysfsGetCpuValueInt(unsigned int cpu,
const char *file,
int *value);
int
virSysfsGetCpuValueUint(unsigned int cpu,
const char *file,
unsigned int *value);
int
virSysfsGetCpuValueString(unsigned int cpu,
const char *file,
char **value);
int
virSysfsGetCpuValueBitmap(unsigned int cpu,
const char *file,
virBitmapPtr *value);
int
virSysfsGetNodeValueString(unsigned int node,
const char *file,
char **value);
int
virSysfsGetNodeValueBitmap(unsigned int cpu,
const char *file,
virBitmapPtr *value);
#endif /* __VIR_SYSFS_H__*/

28
src/util/virsysfspriv.h Normal file
View File

@ -0,0 +1,28 @@
/*
* virsysfspriv.h: Helper functions for manipulating sysfs files
*
* 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/>.
*
* Author: Martin Kletzander <mkletzan@redhat.com>
*/
#ifndef __VIR_SYSFS_PRIV_H__
# define __VIR_SYSFS_PRIV_H__
# include "virsysfs.h"
void virSysfsSetSystemPath(const char *path);
#endif /* __VIR_SYSFS_PRIV_H__*/