Skip to content

Commit 2d729f6

Browse files
committed
tools lib api fs: Introduce sysfs__read_{int,ull}()
To read either an int or an unsigned long long value from the given file. E.g.: $ cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 3200000 $ ./sysfs__read_ull devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq=3200000 $ Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Borislav Petkov <bp@suse.de> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Kan Liang <kan.liang@intel.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Stephane Eranian <eranian@google.com> Cc: Wang Nan <wangnan0@huawei.com> Link: http://lkml.kernel.org/n/tip-4a12m4d5k8m4qgc1vguocvei@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e0838e0 commit 2d729f6

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

tools/lib/api/fs/fs.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <ctype.h>
22
#include <errno.h>
3+
#include <limits.h>
34
#include <stdbool.h>
45
#include <stdio.h>
56
#include <stdlib.h>
@@ -281,6 +282,50 @@ int filename__read_int(const char *filename, int *value)
281282
return err;
282283
}
283284

285+
int filename__read_ull(const char *filename, unsigned long long *value)
286+
{
287+
char line[64];
288+
int fd = open(filename, O_RDONLY), err = -1;
289+
290+
if (fd < 0)
291+
return -1;
292+
293+
if (read(fd, line, sizeof(line)) > 0) {
294+
*value = strtoull(line, NULL, 10);
295+
if (*value != ULLONG_MAX)
296+
err = 0;
297+
}
298+
299+
close(fd);
300+
return err;
301+
}
302+
303+
int sysfs__read_ull(const char *entry, unsigned long long *value)
304+
{
305+
char path[PATH_MAX];
306+
const char *sysfs = sysfs__mountpoint();
307+
308+
if (!sysfs)
309+
return -1;
310+
311+
snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
312+
313+
return filename__read_ull(path, value);
314+
}
315+
316+
int sysfs__read_int(const char *entry, int *value)
317+
{
318+
char path[PATH_MAX];
319+
const char *sysfs = sysfs__mountpoint();
320+
321+
if (!sysfs)
322+
return -1;
323+
324+
snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
325+
326+
return filename__read_int(path, value);
327+
}
328+
284329
int sysctl__read_int(const char *sysctl, int *value)
285330
{
286331
char path[PATH_MAX];

tools/lib/api/fs/fs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@ FS(tracefs)
2525

2626

2727
int filename__read_int(const char *filename, int *value);
28+
int filename__read_ull(const char *filename, unsigned long long *value);
29+
2830
int sysctl__read_int(const char *sysctl, int *value);
31+
int sysfs__read_int(const char *entry, int *value);
32+
int sysfs__read_ull(const char *entry, unsigned long long *value);
2933
#endif /* __API_FS__ */

0 commit comments

Comments
 (0)