teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
Blob Blame History Raw
From b1d6dcf5a5c5aa02843c026dede0638f77798cb4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Mon, 29 Sep 2014 07:31:14 -0500
Subject: [PATCH] Do not format USEC_INFINITY as NULL

systemctl would print 'CPUQuotaPerSecUSec=(null)' for no limit. This
does not look right.

Since USEC_INFINITY is one of the valid values, format_timespan()
could return NULL, and we should wrap every use of it in strna() or
similar. But most callers didn't do that, and it seems more robust to
return a string ("infinity") that makes sense most of the time, even
if in some places the result will not be grammatically correct.
---
 src/core/cgroup.c           |  2 +-
 src/core/timer.c            |  2 +-
 src/network/networkd-link.c | 12 ++++--------
 src/shared/time-util.c      | 21 +++++++++++++++------
 src/test/test-time.c        |  7 +++++++
 5 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 6c6e4f5e7b..e604c3cbc6 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -111,7 +111,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
                 prefix, yes_no(c->memory_accounting),
                 prefix, c->cpu_shares,
                 prefix, c->startup_cpu_shares,
-                prefix, strna(format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1)),
+                prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1),
                 prefix, c->blockio_weight,
                 prefix, c->startup_blockio_weight,
                 prefix, c->memory_limit,
diff --git a/src/core/timer.c b/src/core/timer.c
index dc0f289c7a..a3713e2140 100644
--- a/src/core/timer.c
+++ b/src/core/timer.c
@@ -242,7 +242,7 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) {
                                 "%s%s: %s\n",
                                 prefix,
                                 timer_base_to_string(v->base),
-                                strna(format_timespan(timespan1, sizeof(timespan1), v->value, 0)));
+                                format_timespan(timespan1, sizeof(timespan1), v->value, 0));
                 }
         }
 }
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 427f6953c5..dcbe38a90a 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -1475,12 +1475,10 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         case RTM_NEWADDR:
                 if (!address_dropped)
                         log_debug_link(link, "added address: %s/%u (valid for %s)",
-                                       buf, address->prefixlen,
-                                       strna(valid_str));
+                                       buf, address->prefixlen, valid_str);
                 else
                         log_debug_link(link, "updated address: %s/%u (valid for %s)",
-                                       buf, address->prefixlen,
-                                       strna(valid_str));
+                                       buf, address->prefixlen, valid_str);
 
                 LIST_PREPEND(addresses, link->addresses, address);
                 address = NULL;
@@ -1491,15 +1489,13 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         case RTM_DELADDR:
                 if (address_dropped) {
                         log_debug_link(link, "removed address: %s/%u (valid for %s)",
-                                       buf, address->prefixlen,
-                                       strna(valid_str));
+                                       buf, address->prefixlen, valid_str);
 
                         link_save(link);
                 } else
                         log_warning_link(link,
                                          "removing non-existent address: %s/%u (valid for %s)",
-                                         buf, address->prefixlen,
-                                         strna(valid_str));
+                                         buf, address->prefixlen, valid_str);
 
                 break;
         default:
diff --git a/src/shared/time-util.c b/src/shared/time-util.c
index 2dc01e6ed3..066ef973ac 100644
--- a/src/shared/time-util.c
+++ b/src/shared/time-util.c
@@ -279,11 +279,8 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
         assert(buf);
         assert(l > 0);
 
-        if (t == USEC_INFINITY)
-                return NULL;
-
-        if (t <= 0) {
-                snprintf(p, l, "0");
+        if (t == USEC_INFINITY || t <= 0) {
+                strncpy(p, t == USEC_INFINITY ? "infinity" : "0", l);
                 p[l-1] = 0;
                 return p;
         }
@@ -628,7 +625,7 @@ int parse_sec(const char *t, usec_t *usec) {
                 { "", USEC_PER_SEC }, /* default is sec */
         };
 
-        const char *p;
+        const char *p, *s;
         usec_t r = 0;
         bool something = false;
 
@@ -636,6 +633,18 @@ int parse_sec(const char *t, usec_t *usec) {
         assert(usec);
 
         p = t;
+
+        p += strspn(p, WHITESPACE);
+        s = startswith(p, "infinity");
+        if (s) {
+                s += strspn(s, WHITESPACE);
+                if (*s != 0)
+                        return -EINVAL;
+
+                *usec = USEC_INFINITY;
+                return 0;
+        }
+
         for (;;) {
                 long long l, z = 0;
                 char *e;
diff --git a/src/test/test-time.c b/src/test/test-time.c
index 87e7ae742a..8cfc4cc4fe 100644
--- a/src/test/test-time.c
+++ b/src/test/test-time.c
@@ -43,12 +43,18 @@ static void test_parse_sec(void) {
         assert_se(u == 2500 * USEC_PER_MSEC);
         assert_se(parse_sec(".7", &u) >= 0);
         assert_se(u == 700 * USEC_PER_MSEC);
+        assert_se(parse_sec("infinity", &u) >= 0);
+        assert_se(u == USEC_INFINITY);
+        assert_se(parse_sec(" infinity ", &u) >= 0);
+        assert_se(u == USEC_INFINITY);
 
         assert_se(parse_sec(" xyz ", &u) < 0);
         assert_se(parse_sec("", &u) < 0);
         assert_se(parse_sec(" . ", &u) < 0);
         assert_se(parse_sec(" 5. ", &u) < 0);
         assert_se(parse_sec(".s ", &u) < 0);
+        assert_se(parse_sec(" infinity .7", &u) < 0);
+        assert_se(parse_sec(".3 infinity", &u) < 0);
 }
 
 static void test_parse_nsec(void) {
@@ -125,6 +131,7 @@ static void test_format_timespan(usec_t accuracy) {
         test_format_timespan_one(986087, accuracy);
         test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
         test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
+        test_format_timespan_one(USEC_INFINITY, accuracy);
 }
 
 static void test_timezone_is_valid(void) {