summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFilipe Brandenburger <filbranden@google.com>2019-01-24 04:48:54 +0100
committerFilipe Brandenburger <filbranden@google.com>2019-02-14 20:04:42 +0100
commit7b61ce3c44ef5908e817009ce4f9d2a7a37722be (patch)
treec0e8cdc688bd5904c5743b25364ef2514ad763ae /src
parentRevert "README: remove Coverity Scan badge" (diff)
downloadsystemd-7b61ce3c44ef5908e817009ce4f9d2a7a37722be.tar.xz
systemd-7b61ce3c44ef5908e817009ce4f9d2a7a37722be.zip
time-util: Introduce parse_sec_def_infinity
This works like parse_sec() but defaults to USEC_INFINITY when passed an empty string or only whitespace. Also introduce config_parse_sec_def_infinity, which can be used to parse config options using this function. This is useful for time options that use "infinity" for default and that can be reset by unsetting them. Introduce a test case to ensure it works as expected.
Diffstat (limited to 'src')
-rw-r--r--src/basic/time-util.c9
-rw-r--r--src/basic/time-util.h1
-rw-r--r--src/shared/conf-parser.c1
-rw-r--r--src/shared/conf-parser.h1
-rw-r--r--src/test/test-time-util.c21
5 files changed, 33 insertions, 0 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 62cdc305f9..25a5c116e8 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -1031,6 +1031,15 @@ int parse_sec_fix_0(const char *t, usec_t *ret) {
return r;
}
+int parse_sec_def_infinity(const char *t, usec_t *ret) {
+ t += strspn(t, WHITESPACE);
+ if (isempty(t)) {
+ *ret = USEC_INFINITY;
+ return 0;
+ }
+ return parse_sec(t, ret);
+}
+
static const char* extract_nsec_multiplier(const char *p, nsec_t *multiplier) {
static const struct {
const char *suffix;
diff --git a/src/basic/time-util.h b/src/basic/time-util.h
index 5316305062..a238f6914d 100644
--- a/src/basic/time-util.h
+++ b/src/basic/time-util.h
@@ -112,6 +112,7 @@ int parse_timestamp(const char *t, usec_t *usec);
int parse_sec(const char *t, usec_t *usec);
int parse_sec_fix_0(const char *t, usec_t *usec);
+int parse_sec_def_infinity(const char *t, usec_t *usec);
int parse_time(const char *t, usec_t *usec, usec_t default_unit);
int parse_nsec(const char *t, nsec_t *nsec);
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index b80c147807..aa8283313f 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -506,6 +506,7 @@ DEFINE_PARSER(unsigned, unsigned, safe_atou);
DEFINE_PARSER(double, double, safe_atod);
DEFINE_PARSER(nsec, nsec_t, parse_nsec);
DEFINE_PARSER(sec, usec_t, parse_sec);
+DEFINE_PARSER(sec_def_infinity, usec_t, parse_sec_def_infinity);
DEFINE_PARSER(mode, mode_t, parse_mode);
int config_parse_iec_size(const char* unit,
diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
index 865db4278b..17b4bdf1a2 100644
--- a/src/shared/conf-parser.h
+++ b/src/shared/conf-parser.h
@@ -127,6 +127,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_string);
CONFIG_PARSER_PROTOTYPE(config_parse_path);
CONFIG_PARSER_PROTOTYPE(config_parse_strv);
CONFIG_PARSER_PROTOTYPE(config_parse_sec);
+CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
CONFIG_PARSER_PROTOTYPE(config_parse_mode);
CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c
index eb6041c152..633544ff2e 100644
--- a/src/test/test-time-util.c
+++ b/src/test/test-time-util.c
@@ -85,6 +85,26 @@ static void test_parse_sec_fix_0(void) {
assert_se(u == USEC_INFINITY);
}
+static void test_parse_sec_def_infinity(void) {
+ usec_t u;
+
+ log_info("/* %s */", __func__);
+
+ assert_se(parse_sec_def_infinity("5s", &u) >= 0);
+ assert_se(u == 5 * USEC_PER_SEC);
+ assert_se(parse_sec_def_infinity("", &u) >= 0);
+ assert_se(u == USEC_INFINITY);
+ assert_se(parse_sec_def_infinity(" ", &u) >= 0);
+ assert_se(u == USEC_INFINITY);
+ assert_se(parse_sec_def_infinity("0s", &u) >= 0);
+ assert_se(u == 0);
+ assert_se(parse_sec_def_infinity("0", &u) >= 0);
+ assert_se(u == 0);
+ assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
+ assert_se(u == 0);
+ assert_se(parse_sec_def_infinity("-5s", &u) < 0);
+}
+
static void test_parse_time(void) {
usec_t u;
@@ -472,6 +492,7 @@ int main(int argc, char *argv[]) {
test_parse_sec();
test_parse_sec_fix_0();
+ test_parse_sec_def_infinity();
test_parse_time();
test_parse_nsec();
test_format_timespan(1);