Skip to content

Commit 4bd112d

Browse files
committed
tools lib api fs: Add helper to read string from procfs file
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>
1 parent ccd9b2a commit 4bd112d

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

tools/lib/api/fs/fs.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ int filename__read_str(const char *filename, char **buf, size_t *sizep)
351351
return err;
352352
}
353353

354+
int procfs__read_str(const char *entry, char **buf, size_t *sizep)
355+
{
356+
char path[PATH_MAX];
357+
const char *procfs = procfs__mountpoint();
358+
359+
if (!procfs)
360+
return -1;
361+
362+
snprintf(path, sizeof(path), "%s/%s", procfs, entry);
363+
364+
return filename__read_str(path, buf, sizep);
365+
}
366+
354367
int sysfs__read_ull(const char *entry, unsigned long long *value)
355368
{
356369
char path[PATH_MAX];

tools/lib/api/fs/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int filename__read_int(const char *filename, int *value);
2929
int filename__read_ull(const char *filename, unsigned long long *value);
3030
int filename__read_str(const char *filename, char **buf, size_t *sizep);
3131

32+
int procfs__read_str(const char *entry, char **buf, size_t *sizep);
33+
3234
int sysctl__read_int(const char *sysctl, int *value);
3335
int sysfs__read_int(const char *entry, int *value);
3436
int sysfs__read_ull(const char *entry, unsigned long long *value);

0 commit comments

Comments
 (0)