[systemd-devel] [PATCH] WIP: conf-parser: allow config_parse_iec_off to parse percentages

jsynacek at redhat.com jsynacek at redhat.com
Wed May 20 01:37:42 PDT 2015


From: Jan Synacek <jsynacek at redhat.com>

---
 src/shared/conf-parser.c | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index 2c85515..d7d9aa4 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <sys/statvfs.h>
 
 #include "conf-parser.h"
 #include "conf-files.h"
@@ -517,6 +518,37 @@ int config_parse_si_size(const char* unit,
         return 0;
 }
 
+static int parse_percent(const char *lvalue, const char *rvalue, off_t *bytes) {
+        char *p, *e;
+        off_t percent;
+        struct statvfs ss;
+
+        p = endswith(rvalue, "%");
+        if (!p)
+                return -ERANGE;
+
+        percent = strtoll(rvalue, &e, 10);
+        if (*e != '%' || e != p)
+                return -ERANGE;
+
+        if (percent > 100)
+                return -ERANGE;
+
+        if (!startswith(lvalue, "System") && !startswith(lvalue, "Runtime")) {
+                if (statvfs("/var/lib/systemd/coredump", &ss) < 0)
+                        return -errno;
+        } else if (startswith(lvalue, "System")) {
+                if (statvfs("/var/log/journal", &ss) < 0)
+                        return -errno;
+        } else
+                if (statvfs("/run/log", &ss) < 0)
+                        return -errno;
+
+        *bytes = percent * 0.01 * ss.f_bsize * ss.f_bavail;
+
+        return 0;
+}
+
 int config_parse_iec_off(const char* unit,
                            const char *filename,
                            unsigned line,
@@ -538,10 +570,12 @@ int config_parse_iec_off(const char* unit,
 
         assert_cc(sizeof(off_t) == sizeof(uint64_t));
 
-        r = parse_size(rvalue, 1024, bytes);
-        if (r < 0)
-                log_syntax(unit, LOG_ERR, filename, line, -r, "Failed to parse size value, ignoring: %s", rvalue);
-
+        r = parse_percent(lvalue, rvalue, bytes);
+        if (r < 0) {
+                r = parse_size(rvalue, 1024, bytes);
+                if (r < 0)
+                        log_syntax(unit, LOG_ERR, filename, line, -r, "Failed to parse size value, ignoring: %s", rvalue);
+        }
         return 0;
 }
 
-- 
2.1.0



More information about the systemd-devel mailing list