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