]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - tools/lib/api/fs/fs.c
Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / tools / lib / api / fs / fs.c
index bc93baf33fffca625f0585e966595ae6ceac9b9d..459599d1b6c410b7b41333c13f248ee685c2dd4a 100644 (file)
@@ -1,5 +1,6 @@
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -11,7 +12,6 @@
 #include <unistd.h>
 #include <sys/mount.h>
 
-#include "debugfs.h"
 #include "fs.h"
 
 #define _STR(x) #x
@@ -69,7 +69,7 @@ static const char * const tracefs__known_mountpoints[] = {
 struct fs {
        const char              *name;
        const char * const      *mounts;
-       char                     path[PATH_MAX + 1];
+       char                     path[PATH_MAX];
        bool                     found;
        long                     magic;
 };
@@ -244,15 +244,20 @@ static const char *fs__mount(int idx)
        return fs__check_mounts(fs) ? fs->path : NULL;
 }
 
-#define FS(name, idx)                  \
-const char *name##__mountpoint(void)   \
-{                                      \
-       return fs__mountpoint(idx);     \
-}                                      \
-                                       \
-const char *name##__mount(void)                \
-{                                      \
-       return fs__mount(idx);          \
+#define FS(name, idx)                          \
+const char *name##__mountpoint(void)           \
+{                                              \
+       return fs__mountpoint(idx);             \
+}                                              \
+                                               \
+const char *name##__mount(void)                        \
+{                                              \
+       return fs__mount(idx);                  \
+}                                              \
+                                               \
+bool name##__configured(void)                  \
+{                                              \
+       return name##__mountpoint() != NULL;    \
 }
 
 FS(sysfs,   FS__SYSFS);
@@ -277,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];