diff --git a/src/Makefile.am b/src/Makefile.am index 26e2297d8b..86fb721fc3 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 \ @@ -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 \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0f5a96d123..d0978f7d65 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -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 index 0000000000..7a98b488e0 --- /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@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 0000000000..cd871ff11d --- /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@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 0000000000..ae9f54a40c --- /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@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__*/