[PATCH 08/10] perf, tool: Add sysfs read file interface

From: Jiri Olsa
Date: Tue Jul 03 2012 - 18:01:59 EST


Adding read file interface to access sysfs files data.

int sysfs_read_file(const char *file, char *buf, size_t size)

The 'file' argument is file path relative to the sysfs mount.

Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/util/sysfs.c | 19 +++++++++++++++++++
tools/perf/util/sysfs.h | 1 +
2 files changed, 20 insertions(+)

diff --git a/tools/perf/util/sysfs.c b/tools/perf/util/sysfs.c
index 48c6902..4ee886a 100644
--- a/tools/perf/util/sysfs.c
+++ b/tools/perf/util/sysfs.c
@@ -58,3 +58,22 @@ const char *sysfs_find_mountpoint(void)

return sysfs_found ? sysfs_mountpoint : NULL;
}
+
+int sysfs_read_file(const char *file, char *buf, size_t size)
+{
+ char path[PATH_MAX];
+ FILE *fp;
+ int ret = 0;
+
+ snprintf(path, PATH_MAX, "%s/%s", sysfs_mountpoint, file);
+
+ fp = fopen(path, "r");
+ if (!fp)
+ return -errno;
+
+ if (fread(buf, 1, size, fp) == 0)
+ ret = -errno;
+
+ fclose(fp);
+ return ret;
+}
diff --git a/tools/perf/util/sysfs.h b/tools/perf/util/sysfs.h
index a813b72..5ce99e7 100644
--- a/tools/perf/util/sysfs.h
+++ b/tools/perf/util/sysfs.h
@@ -2,5 +2,6 @@
#define __SYSFS_H__

const char *sysfs_find_mountpoint(void);
+int sysfs_read_file(const char *file, char *buf, size_t size);

#endif /* __DEBUGFS_H__ */
--
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/