]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
proc_fuse: improve /proc/meminfo
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 20 Mar 2020 09:46:40 +0000 (10:46 +0100)
committerStéphane Graber <stgraber@ubuntu.com>
Fri, 20 Mar 2020 15:18:33 +0000 (11:18 -0400)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/proc_fuse.c
src/utils.c
src/utils.h

index c3870fb27282119e11faaaa7d0af084f70c8ced0..36ca1f64f48309d569827b9fee4b94852f284cba 100644 (file)
@@ -1075,10 +1075,10 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 
        ret = cgroup_ops->get_memory_current(cgroup_ops, cgroup, &memusage_str);
        if (ret < 0)
-               return 0;
+               return read_file_fuse("/proc/meminfo", buf, size, d);
 
        if (!cgroup_parse_memory_stat(cgroup, &mstat))
-               return 0;
+               return read_file_fuse("/proc/meminfo", buf, size, d);
 
        /*
         * Following values are allowed to fail, because swapaccount might be
@@ -1089,18 +1089,20 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
                ret = cgroup_ops->get_memory_swap_current(cgroup_ops, cgroup, &memswusage_str);
        if (ret >= 0) {
                memswlimit = get_min_memlimit(cgroup, true);
-               memswusage = strtoull(memswusage_str, NULL, 10);
                memswlimit = memswlimit / 1024;
+               if (safe_uint64(memswusage_str, &memswusage, 10) < 0)
+                       memswusage = 0;
                memswusage = memswusage / 1024;
        }
 
-       memusage = strtoull(memusage_str, NULL, 10);
+       if (safe_uint64(memusage_str, &memusage, 10) < 0)
+               memusage = 0;
        memlimit /= 1024;
        memusage /= 1024;
 
        f = fopen_cached("/proc/meminfo", "re", &fopen_cache);
        if (!f)
-               return 0;
+               return read_file_fuse("/proc/meminfo", buf, size, d);
 
        while (getline(&line, &linelen, f) != -1) {
                ssize_t l;
@@ -1119,10 +1121,11 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
                } else if (startswith(line, "MemAvailable:")) {
                        snprintf(lbuf, 100, "MemAvailable:   %8" PRIu64 " kB\n", memlimit - memusage + mstat.total_cache / 1024);
                        printme = lbuf;
-               } else if (startswith(line, "SwapTotal:") && memswlimit > 0 &&
-                          opts && opts->swap_off == false) {
-                       memswlimit -= memlimit;
-                       snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " kB\n", memswlimit);
+               } else if (startswith(line, "SwapTotal:") && memswlimit > 0 && opts && opts->swap_off == false) {
+                       snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " kB\n",
+                                (memswlimit >= memlimit)
+                                    ? (memswlimit - memlimit)
+                                    : 0);
                        printme = lbuf;
                } else if (startswith(line, "SwapTotal:") && opts && opts->swap_off == true) {
                        snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " kB\n", (uint64_t)0);
index fe83aefec8e20c00b32d2b2bf33f22c1c6fae671..0be997c2f4d886bf18bdffb704e05a2b5d5f99f1 100644 (file)
 
 #define _FILE_OFFSET_BITS 64
 
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <fuse.h>
 #include <inttypes.h>
 #include <sched.h>
 #include <stdarg.h>
-#include <stdio.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -520,3 +520,26 @@ ssize_t write_nointr(int fd, const void *buf, size_t count)
 
        return ret;
 }
+
+int safe_uint64(const char *numstr, uint64_t *converted, int base)
+{
+       char *err = NULL;
+       uint64_t u;
+
+       while (isspace(*numstr))
+               numstr++;
+
+       if (*numstr == '-')
+               return -EINVAL;
+
+       errno = 0;
+       u = strtoull(numstr, &err, base);
+       if (errno == ERANGE && u == UINT64_MAX)
+               return -ERANGE;
+
+       if (err == numstr || *err != '\0')
+               return -EINVAL;
+
+       *converted = u;
+       return 0;
+}
index 39a29433949409dcce4a9bb5b482caff7e7fa1b9..29c87dc73f070b6cef1ab1b6fe5d334bc0772820 100644 (file)
@@ -75,5 +75,6 @@ extern FILE *fopen_cached(const char *path, const char *mode,
                          void **caller_freed_buffer);
 extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer);
 extern ssize_t write_nointr(int fd, const void *buf, size_t count);
+extern int safe_uint64(const char *numstr, uint64_t *converted, int base);
 
 #endif /* __LXCFS_UTILS_H */