Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+49
+45
tools/lib/api/fs/fs.c
··· 1 1 #include <ctype.h> 2 2 #include <errno.h> 3 + #include <limits.h> 3 4 #include <stdbool.h> 4 5 #include <stdio.h> 5 6 #include <stdlib.h> ··· 280 279 281 280 close(fd); 282 281 return err; 282 + } 283 + 284 + int filename__read_ull(const char *filename, unsigned long long *value) 285 + { 286 + char line[64]; 287 + int fd = open(filename, O_RDONLY), err = -1; 288 + 289 + if (fd < 0) 290 + return -1; 291 + 292 + if (read(fd, line, sizeof(line)) > 0) { 293 + *value = strtoull(line, NULL, 10); 294 + if (*value != ULLONG_MAX) 295 + err = 0; 296 + } 297 + 298 + close(fd); 299 + return err; 300 + } 301 + 302 + int sysfs__read_ull(const char *entry, unsigned long long *value) 303 + { 304 + char path[PATH_MAX]; 305 + const char *sysfs = sysfs__mountpoint(); 306 + 307 + if (!sysfs) 308 + return -1; 309 + 310 + snprintf(path, sizeof(path), "%s/%s", sysfs, entry); 311 + 312 + return filename__read_ull(path, value); 313 + } 314 + 315 + int sysfs__read_int(const char *entry, int *value) 316 + { 317 + char path[PATH_MAX]; 318 + const char *sysfs = sysfs__mountpoint(); 319 + 320 + if (!sysfs) 321 + return -1; 322 + 323 + snprintf(path, sizeof(path), "%s/%s", sysfs, entry); 324 + 325 + return filename__read_int(path, value); 283 326 } 284 327 285 328 int sysctl__read_int(const char *sysctl, int *value)
+4
tools/lib/api/fs/fs.h
··· 25 25 26 26 27 27 int filename__read_int(const char *filename, int *value); 28 + int filename__read_ull(const char *filename, unsigned long long *value); 29 + 28 30 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); 29 33 #endif /* __API_FS__ */