Messages in this thread Patch in this message |  | | | From | Jiri Olsa <> | | Subject | [PATCH 08/10] perf, tool: Add sysfs read file interface | | Date | Wed, 4 Jul 2012 00:00:46 +0200 |
| |
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@redhat.com> --- 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
|  |