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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2014-09-29 07:31:14 -05:00
parent ee26bcc038
commit b1d6dcf5a5
5 changed files with 28 additions and 16 deletions

View File

@@ -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,

View File

@@ -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));
}
}
}

View File

@@ -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:

View File

@@ -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;

View File

@@ -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) {