]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Add virsysfs for handling sysfs files
authorMartin Kletzander <mkletzan@redhat.com>
Tue, 7 Mar 2017 09:02:46 +0000 (10:02 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Mon, 27 Mar 2017 11:13:29 +0000 (13:13 +0200)
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>
src/Makefile.am
src/libvirt_private.syms
src/util/virsysfs.c [new file with mode: 0644]
src/util/virsysfs.h [new file with mode: 0644]
src/util/virsysfspriv.h [new file with mode: 0644]

index 26e2297d8b3a1427db7e3f035471490942c7dd5b..86fb721fc3af1f10ea41d92b07f47e26a7db2295 100644 (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                \
index 0f5a96d12376fdc3d74b8775917a44657ecb0af5..d0978f7d65b8692c606b0385389a42bf9e24a663 100644 (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;
diff --git a/src/util/virsysfs.c b/src/util/virsysfs.c
new file mode 100644 (file)
index 0000000..7a98b48
--- /dev/null
@@ -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;
+}
diff --git a/src/util/virsysfs.h b/src/util/virsysfs.h
new file mode 100644 (file)
index 0000000..cd871ff
--- /dev/null
@@ -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__*/
diff --git a/src/util/virsysfspriv.h b/src/util/virsysfspriv.h
new file mode 100644 (file)
index 0000000..ae9f54a
--- /dev/null
@@ -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__*/