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(a)redhat.com>
---
src/Makefile.am | 2 +
src/libvirt_private.syms | 14 +++
src/util/virsysfs.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++
src/util/virsysfs.h | 70 +++++++++++++++
src/util/virsysfspriv.h | 28 ++++++
5 files changed, 343 insertions(+)
create mode 100644 src/util/virsysfs.c
create mode 100644 src/util/virsysfs.h
create mode 100644 src/util/virsysfspriv.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 3b1bb1da35d0..2052fb507b70 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -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 \
@@ -2584,6 +2585,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 \
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 27fbca589faa..0a4659fe5e92 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2588,6 +2588,20 @@ virTrimSpaces;
virVasprintfInternal;
+# util/virsysfs.h
+virSysfsGetCpuValueBitmap;
+virSysfsGetCpuValueInt;
+virSysfsGetCpuValueString;
+virSysfsGetCpuValueUint;
+virSysfsGetNodeValueBitmap;
+virSysfsGetNodeValueString;
+virSysfsGetSystemPath;
+virSysfsGetValueBitmap;
+virSysfsGetValueInt;
+virSysfsGetValueString;
+virSysfsSetSystemPath;
+
+
# util/virsysinfo.h
virSysinfoBaseBoardDefClear;
virSysinfoBIOSDefFree;
diff --git a/src/util/virsysfs.c b/src/util/virsysfs.c
new file mode 100644
index 000000000000..7a98b488e0ff
--- /dev/null
+++ b/src/util/virsysfs.c
@@ -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(a)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;
+}
diff --git a/src/util/virsysfs.h b/src/util/virsysfs.h
new file mode 100644
index 000000000000..cd871ff11dd1
--- /dev/null
+++ b/src/util/virsysfs.h
@@ -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(a)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__*/
diff --git a/src/util/virsysfspriv.h b/src/util/virsysfspriv.h
new file mode 100644
index 000000000000..ae9f54a40c2a
--- /dev/null
+++ b/src/util/virsysfspriv.h
@@ -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(a)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__*/
--
2.12.0