]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
tools lib api fs: Introduce sysfs__read_{int,ull}()
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 10 Sep 2015 14:58:50 +0000 (11:58 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 14 Sep 2015 15:50:25 +0000 (12:50 -0300)
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>
tools/lib/api/fs/fs.c
tools/lib/api/fs/fs.h

index 791509346178ebfdf7ad2dd92d81e8b523859ea8..732dbef588b09d54da58939f76cc78234b20a4f0 100644 (file)
@@ -1,5 +1,6 @@
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -281,6 +282,50 @@ int filename__read_int(const char *filename, int *value)
        return err;
 }
 
+int filename__read_ull(const char *filename, unsigned long long *value)
+{
+       char line[64];
+       int fd = open(filename, O_RDONLY), err = -1;
+
+       if (fd < 0)
+               return -1;
+
+       if (read(fd, line, sizeof(line)) > 0) {
+               *value = strtoull(line, NULL, 10);
+               if (*value != ULLONG_MAX)
+                       err = 0;
+       }
+
+       close(fd);
+       return err;
+}
+
+int sysfs__read_ull(const char *entry, unsigned long long *value)
+{
+       char path[PATH_MAX];
+       const char *sysfs = sysfs__mountpoint();
+
+       if (!sysfs)
+               return -1;
+
+       snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
+
+       return filename__read_ull(path, value);
+}
+
+int sysfs__read_int(const char *entry, int *value)
+{
+       char path[PATH_MAX];
+       const char *sysfs = sysfs__mountpoint();
+
+       if (!sysfs)
+               return -1;
+
+       snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
+
+       return filename__read_int(path, value);
+}
+
 int sysctl__read_int(const char *sysctl, int *value)
 {
        char path[PATH_MAX];
index f654bcb99d1e9b7a7823279135d97420b45b7861..d024a7f682f69b27a4bba1aed7b8f1894d07b50f 100644 (file)
@@ -25,5 +25,9 @@ FS(tracefs)
 
 
 int filename__read_int(const char *filename, int *value);
+int filename__read_ull(const char *filename, unsigned long long *value);
+
 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);
 #endif /* __API_FS__ */