803fb7
From 8afe4259a8add0d042950015d34afc95a221ad96 Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Wed, 6 Jul 2016 13:47:07 +0200
803fb7
Subject: [PATCH] time-util: add parse_time(), which is like parse_sec() but
803fb7
 allows specification of default time unit if none is specified
803fb7
MIME-Version: 1.0
803fb7
Content-Type: text/plain; charset=UTF-8
803fb7
Content-Transfer-Encoding: 8bit
803fb7
803fb7
This is useful if we want to parse RLIMIT_RTTIME values where the common
803fb7
UNIX syntax is without any units but refers to a non-second unit (µs in
803fb7
this case), but where we want to allow specification of units.
803fb7
803fb7
Cherry-picked from: 519cffec890510f817740d07355e911b10c203b7
803fb7
Related: #1351415
803fb7
---
803fb7
 src/shared/calendarspec.c |  4 ++--
803fb7
 src/shared/time-util.c    | 34 ++++++++++++++++++++++------------
803fb7
 src/shared/time-util.h    |  1 +
803fb7
 src/test/test-time.c      | 23 +++++++++++++++++++++++
803fb7
 src/test/test-unit-file.c |  6 +++---
803fb7
 5 files changed, 51 insertions(+), 17 deletions(-)
803fb7
803fb7
diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c
803fb7
index 2fde3e107..abbf0261e 100644
803fb7
--- a/src/shared/calendarspec.c
803fb7
+++ b/src/shared/calendarspec.c
803fb7
@@ -556,7 +556,7 @@ static int parse_date(const char **p, CalendarSpec *c) {
803fb7
         return -EINVAL;
803fb7
 }
803fb7
 
803fb7
-static int parse_time(const char **p, CalendarSpec *c) {
803fb7
+static int parse_calendar_time(const char **p, CalendarSpec *c) {
803fb7
         CalendarComponent *h = NULL, *m = NULL, *s = NULL;
803fb7
         const char *t;
803fb7
         int r;
803fb7
@@ -789,7 +789,7 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
803fb7
                 if (r < 0)
803fb7
                         goto fail;
803fb7
 
803fb7
-                r = parse_time(&p, c);
803fb7
+                r = parse_calendar_time(&p, c);
803fb7
                 if (r < 0)
803fb7
                         goto fail;
803fb7
 
803fb7
diff --git a/src/shared/time-util.c b/src/shared/time-util.c
803fb7
index 1c36c577c..c001f52de 100644
803fb7
--- a/src/shared/time-util.c
803fb7
+++ b/src/shared/time-util.c
803fb7
@@ -613,7 +613,8 @@ finish:
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
-int parse_sec(const char *t, usec_t *usec) {
803fb7
+int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
803fb7
+
803fb7
         static const struct {
803fb7
                 const char *suffix;
803fb7
                 usec_t usec;
803fb7
@@ -645,7 +646,6 @@ int parse_sec(const char *t, usec_t *usec) {
803fb7
                 { "y", USEC_PER_YEAR },
803fb7
                 { "usec", 1ULL },
803fb7
                 { "us", 1ULL },
803fb7
-                { "", USEC_PER_SEC }, /* default is sec */
803fb7
         };
803fb7
 
803fb7
         const char *p, *s;
803fb7
@@ -654,6 +654,7 @@ int parse_sec(const char *t, usec_t *usec) {
803fb7
 
803fb7
         assert(t);
803fb7
         assert(usec);
803fb7
+        assert(default_unit > 0);
803fb7
 
803fb7
         p = t;
803fb7
 
803fb7
@@ -672,6 +673,7 @@ int parse_sec(const char *t, usec_t *usec) {
803fb7
                 long long l, z = 0;
803fb7
                 char *e;
803fb7
                 unsigned i, n = 0;
803fb7
+                usec_t multiplier, k;
803fb7
 
803fb7
                 p += strspn(p, WHITESPACE);
803fb7
 
803fb7
@@ -714,21 +716,24 @@ int parse_sec(const char *t, usec_t *usec) {
803fb7
 
803fb7
                 for (i = 0; i < ELEMENTSOF(table); i++)
803fb7
                         if (startswith(e, table[i].suffix)) {
803fb7
-                                usec_t k = (usec_t) z * table[i].usec;
803fb7
-
803fb7
-                                for (; n > 0; n--)
803fb7
-                                        k /= 10;
803fb7
-
803fb7
-                                r += (usec_t) l * table[i].usec + k;
803fb7
+                                multiplier = table[i].usec;
803fb7
                                 p = e + strlen(table[i].suffix);
803fb7
-
803fb7
-                                something = true;
803fb7
                                 break;
803fb7
                         }
803fb7
 
803fb7
-                if (i >= ELEMENTSOF(table))
803fb7
-                        return -EINVAL;
803fb7
+                if (i >= ELEMENTSOF(table)) {
803fb7
+                        multiplier = default_unit;
803fb7
+                        p = e;
803fb7
+                }
803fb7
+
803fb7
+                something = true;
803fb7
+
803fb7
+                k = (usec_t) z * multiplier;
803fb7
+
803fb7
+                for (; n > 0; n--)
803fb7
+                        k /= 10;
803fb7
 
803fb7
+                r += (usec_t) l * multiplier + k;
803fb7
         }
803fb7
 
803fb7
         *usec = r;
803fb7
@@ -736,6 +741,11 @@ int parse_sec(const char *t, usec_t *usec) {
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
+
803fb7
+int parse_sec(const char *t, usec_t *usec) {
803fb7
+        return parse_time(t, usec, USEC_PER_SEC);
803fb7
+}
803fb7
+
803fb7
 int parse_nsec(const char *t, nsec_t *nsec) {
803fb7
         static const struct {
803fb7
                 const char *suffix;
803fb7
diff --git a/src/shared/time-util.h b/src/shared/time-util.h
803fb7
index fca8a4db9..f2789142f 100644
803fb7
--- a/src/shared/time-util.h
803fb7
+++ b/src/shared/time-util.h
803fb7
@@ -99,6 +99,7 @@ void dual_timestamp_deserialize(const char *value, dual_timestamp *t);
803fb7
 int parse_timestamp(const char *t, usec_t *usec);
803fb7
 
803fb7
 int parse_sec(const char *t, usec_t *usec);
803fb7
+int parse_time(const char *t, usec_t *usec, usec_t default_unit);
803fb7
 int parse_nsec(const char *t, nsec_t *nsec);
803fb7
 
803fb7
 bool ntp_synced(void);
803fb7
diff --git a/src/test/test-time.c b/src/test/test-time.c
803fb7
index 3840fff06..820e4aaee 100644
803fb7
--- a/src/test/test-time.c
803fb7
+++ b/src/test/test-time.c
803fb7
@@ -57,6 +57,28 @@ static void test_parse_sec(void) {
803fb7
         assert_se(parse_sec(".3 infinity", &u) < 0);
803fb7
 }
803fb7
 
803fb7
+static void test_parse_time(void) {
803fb7
+        usec_t u;
803fb7
+
803fb7
+        assert_se(parse_time("5", &u, 1) >= 0);
803fb7
+        assert_se(u == 5);
803fb7
+
803fb7
+        assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
803fb7
+        assert_se(u == 5 * USEC_PER_MSEC);
803fb7
+
803fb7
+        assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
803fb7
+        assert_se(u == 5 * USEC_PER_SEC);
803fb7
+
803fb7
+        assert_se(parse_time("5s", &u, 1) >= 0);
803fb7
+        assert_se(u == 5 * USEC_PER_SEC);
803fb7
+
803fb7
+        assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
803fb7
+        assert_se(u == 5 * USEC_PER_SEC);
803fb7
+
803fb7
+        assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
803fb7
+        assert_se(u == 5 * USEC_PER_SEC);
803fb7
+}
803fb7
+
803fb7
 static void test_parse_nsec(void) {
803fb7
         nsec_t u;
803fb7
 
803fb7
@@ -161,6 +183,7 @@ static void test_get_timezones(void) {
803fb7
 
803fb7
 int main(int argc, char *argv[]) {
803fb7
         test_parse_sec();
803fb7
+        test_parse_time();
803fb7
         test_parse_nsec();
803fb7
         test_format_timespan(1);
803fb7
         test_format_timespan(USEC_PER_MSEC);
803fb7
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
803fb7
index d15173796..87c81ccd7 100644
803fb7
--- a/src/test/test-unit-file.c
803fb7
+++ b/src/test/test-unit-file.c
803fb7
@@ -559,7 +559,7 @@ static void test_config_parse_rlimit(void) {
803fb7
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
803fb7
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
803fb7
 
803fb7
-        rl[RLIMIT_NOFILE] = free(rl[RLIMIT_NOFILE]);
803fb7
+        free(rl[RLIMIT_NOFILE]);
803fb7
         assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0);
803fb7
         assert_se(rl[RLIMIT_CPU]);
803fb7
         assert_se(rl[RLIMIT_CPU]->rlim_cur == 56);
803fb7
@@ -580,7 +580,7 @@ static void test_config_parse_rlimit(void) {
803fb7
         assert_se(rl[RLIMIT_CPU]->rlim_cur == 2);
803fb7
         assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
803fb7
 
803fb7
-        rl[RLIMIT_CPU] = free(rl[RLIMIT_CPU]);
803fb7
+        free(rl[RLIMIT_CPU]);
803fb7
 
803fb7
         assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL) >= 0);
803fb7
         assert_se(rl[RLIMIT_RTTIME]);
803fb7
@@ -602,7 +602,7 @@ static void test_config_parse_rlimit(void) {
803fb7
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 2345 * USEC_PER_MSEC);
803fb7
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
803fb7
 
803fb7
-        rl[RLIMIT_RTTIME] = free(rl[RLIMIT_RTTIME]);
803fb7
+        free(rl[RLIMIT_RTTIME]);
803fb7
 }
803fb7
 
803fb7
 int main(int argc, char *argv[]) {