tools lib api fs: Add helper to read string from procfs file
authorArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 26 Apr 2016 15:31:16 +0000 (12:31 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 26 Apr 2016 16:15:00 +0000 (13:15 -0300)
To read things like /proc/self/comm.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Milian Wolff <milian.wolff@kdab.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-ztpkbmseidt0hq2psr46o0h9@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/api/fs/fs.c
tools/lib/api/fs/fs.h

index ef78c22ff44d4142f01a1f5aea3bbe4e15fd0101..08556cf2c70d400666de1b4b2db6d7d2d05c0e04 100644 (file)
@@ -351,6 +351,19 @@ int filename__read_str(const char *filename, char **buf, size_t *sizep)
        return err;
 }
 
+int procfs__read_str(const char *entry, char **buf, size_t *sizep)
+{
+       char path[PATH_MAX];
+       const char *procfs = procfs__mountpoint();
+
+       if (!procfs)
+               return -1;
+
+       snprintf(path, sizeof(path), "%s/%s", procfs, entry);
+
+       return filename__read_str(path, buf, sizep);
+}
+
 int sysfs__read_ull(const char *entry, unsigned long long *value)
 {
        char path[PATH_MAX];
index 9f6598098dc5804a5bf660013ad5b07c499b6169..16c9c2ed7c5bfb85e270399f4d6a7253d9a367b3 100644 (file)
@@ -29,6 +29,8 @@ int filename__read_int(const char *filename, int *value);
 int filename__read_ull(const char *filename, unsigned long long *value);
 int filename__read_str(const char *filename, char **buf, size_t *sizep);
 
+int procfs__read_str(const char *entry, char **buf, size_t *sizep);
+
 int sysctl__read_int(const char *sysctl, int *value);
 int sysfs__read_int(const char *entry, int *value);
 int sysfs__read_ull(const char *entry, unsigned long long *value);