diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-09-29 07:31:14 -0500 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-09-29 11:09:39 -0400 |
commit | b1d6dcf5a5c5aa02843c026dede0638f77798cb4 (patch) | |
tree | 6065b8eedce06acb651c6ad2d378749049951fcb /src/shared/time-util.c | |
parent | ee26bcc0387f6eda83878eb85a08c01ee0d82c44 (diff) |
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.
Diffstat (limited to 'src/shared/time-util.c')
-rw-r--r-- | src/shared/time-util.c | 21 |
1 files changed, 15 insertions, 6 deletions
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; |