summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-07-16 20:02:26 +0200
committerGitHub <noreply@github.com>2019-07-16 20:02:26 +0200
commit3151b668c23b23d185cac5420b7abf8f5297d678 (patch)
treeb20152aa71c4060de8b3702a08e6fa8d0ba3832e /src/core
parenta483dc92c2ebce9fdc834b257c927df58d02a69b (diff)
parent4252171a94a0c7cb8f9ee180977465dc4fc0ec8e (diff)
downloadsystemd-3151b668c23b23d185cac5420b7abf8f5297d678.tar.gz
systemd-3151b668c23b23d185cac5420b7abf8f5297d678.tar.bz2
systemd-3151b668c23b23d185cac5420b7abf8f5297d678.zip
Merge pull request #13076 from keszybz/pr/13062
Timer formatting fixes
Diffstat (limited to 'src/core')
-rw-r--r--src/core/dbus-timer.c169
1 files changed, 81 insertions, 88 deletions
diff --git a/src/core/dbus-timer.c b/src/core/dbus-timer.c
index 807ca8022a..2f61a5842d 100644
--- a/src/core/dbus-timer.c
+++ b/src/core/dbus-timer.c
@@ -137,6 +137,74 @@ const sd_bus_vtable bus_timer_vtable[] = {
SD_BUS_VTABLE_END
};
+static int timer_add_one_monotonic_spec(
+ Timer *t,
+ const char *name,
+ TimerBase base,
+ UnitWriteFlags flags,
+ usec_t usec,
+ sd_bus_error *error) {
+
+ if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+ char ts[FORMAT_TIMESPAN_MAX];
+ TimerValue *v;
+
+ unit_write_settingf(UNIT(t), flags|UNIT_ESCAPE_SPECIFIERS, name,
+ "%s=%s",
+ timer_base_to_string(base),
+ format_timespan(ts, sizeof ts, usec, USEC_PER_MSEC));
+
+ v = new(TimerValue, 1);
+ if (!v)
+ return -ENOMEM;
+
+ *v = (TimerValue) {
+ .base = base,
+ .value = usec,
+ };
+
+ LIST_PREPEND(value, t->values, v);
+ }
+
+ return 1;
+}
+
+static int timer_add_one_calendar_spec(
+ Timer *t,
+ const char *name,
+ TimerBase base,
+ UnitWriteFlags flags,
+ const char *str,
+ sd_bus_error *error) {
+
+ _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
+ int r;
+
+ r = calendar_spec_from_string(str, &c);
+ if (r == -EINVAL)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec");
+ if (r < 0)
+ return r;
+
+ if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+ unit_write_settingf(UNIT(t), flags|UNIT_ESCAPE_SPECIFIERS, name,
+ "%s=%s", timer_base_to_string(base), str);
+
+ TimerValue *v = new(TimerValue, 1);
+ if (!v)
+ return -ENOMEM;
+
+ *v = (TimerValue) {
+ .base = base,
+ .calendar_spec = c,
+ };
+
+ LIST_PREPEND(value, t->values, v);
+ }
+
+ return 1;
+};
+
static int bus_timer_set_transient_property(
Timer *t,
const char *name,
@@ -181,7 +249,7 @@ static int bus_timer_set_transient_property(
if (streq(name, "TimersMonotonic")) {
const char *base_name;
- usec_t usec = 0;
+ usec_t usec;
bool empty = true;
r = sd_bus_message_enter_container(message, 'a', "(st)");
@@ -193,26 +261,12 @@ static int bus_timer_set_transient_property(
b = timer_base_from_string(base_name);
if (b < 0 || b == TIMER_CALENDAR)
- return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid timer base: %s", base_name);
-
- if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
- char ts[FORMAT_TIMESPAN_MAX];
- TimerValue *v;
-
- unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", base_name,
- format_timespan(ts, sizeof(ts), usec, USEC_PER_MSEC));
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
+ "Invalid timer base: %s", base_name);
- v = new(TimerValue, 1);
- if (!v)
- return -ENOMEM;
-
- *v = (TimerValue) {
- .base = b,
- .value = usec,
- };
-
- LIST_PREPEND(value, t->values, v);
- }
+ r = timer_add_one_monotonic_spec(t, name, b, flags, usec, error);
+ if (r < 0)
+ return r;
empty = false;
}
@@ -239,36 +293,17 @@ static int bus_timer_set_transient_property(
return r;
while ((r = sd_bus_message_read(message, "(ss)", &base_name, &str)) > 0) {
- _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
TimerBase b;
b = timer_base_from_string(base_name);
if (b != TIMER_CALENDAR)
- return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid timer base: %s", base_name);
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
+ "Invalid timer base: %s", base_name);
- r = calendar_spec_from_string(str, &c);
- if (r == -EINVAL)
- return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec: %s", str);
+ r = timer_add_one_calendar_spec(t, name, b, flags, str, error);
if (r < 0)
return r;
- if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
- TimerValue *v;
-
- unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", base_name, str);
-
- v = new(TimerValue, 1);
- if (!v)
- return -ENOMEM;
-
- *v = (TimerValue) {
- .base = b,
- .calendar_spec = TAKE_PTR(c),
- };
-
- LIST_PREPEND(value, t->values, v);
- }
-
empty = false;
}
if (r < 0)
@@ -292,9 +327,8 @@ static int bus_timer_set_transient_property(
"OnUnitActiveSec",
"OnUnitInactiveSec")) {
- TimerValue *v;
- TimerBase b = _TIMER_BASE_INVALID;
- usec_t usec = 0;
+ TimerBase b;
+ usec_t usec;
log_notice("Client is using obsolete %s= transient property, please use TimersMonotonic= instead.", name);
@@ -306,30 +340,10 @@ static int bus_timer_set_transient_property(
if (r < 0)
return r;
- if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
- char time[FORMAT_TIMESPAN_MAX];
-
- unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name,
- format_timespan(time, sizeof(time), usec, USEC_PER_MSEC));
-
- v = new(TimerValue, 1);
- if (!v)
- return -ENOMEM;
-
- *v = (TimerValue) {
- .base = b,
- .value = usec,
- };
-
- LIST_PREPEND(value, t->values, v);
- }
-
- return 1;
+ return timer_add_one_monotonic_spec(t, name, b, flags, usec, error);
} else if (streq(name, "OnCalendar")) {
- TimerValue *v;
- _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
const char *str;
log_notice("Client is using obsolete %s= transient property, please use TimersCalendar= instead.", name);
@@ -338,28 +352,7 @@ static int bus_timer_set_transient_property(
if (r < 0)
return r;
- if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
- r = calendar_spec_from_string(str, &c);
- if (r == -EINVAL)
- return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec");
- if (r < 0)
- return r;
-
- unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str);
-
- v = new(TimerValue, 1);
- if (!v)
- return -ENOMEM;
-
- *v = (TimerValue) {
- .base = TIMER_CALENDAR,
- .calendar_spec = TAKE_PTR(c),
- };
-
- LIST_PREPEND(value, t->values, v);
- }
-
- return 1;
+ return timer_add_one_calendar_spec(t, name, TIMER_CALENDAR, flags, str, error);
}
return 0;