1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-11 05:17:44 +03:00

Merge pull request #13076 from keszybz/pr/13062

Timer formatting fixes
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-07-16 20:02:26 +02:00 committed by GitHub
commit 3151b668c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 115 deletions

View File

@ -1080,7 +1080,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
if (n <= 0)
return n;
table = table_new("TIME", "UNIT");
table = table_new("time", "unit");
if (!table)
return log_oom();
@ -1733,7 +1733,7 @@ static int dump_timespan(int argc, char *argv[], void *userdata) {
return r;
}
table = table_new("NAME", "VALUE");
table = table_new("name", "value");
if (!table)
return log_oom();
@ -1805,7 +1805,7 @@ static int test_timestamp_one(const char *p) {
return r;
}
table = table_new("NAME", "VALUE");
table = table_new("name", "value");
if (!table)
return log_oom();
@ -1859,11 +1859,13 @@ static int test_timestamp_one(const char *p) {
if (r < 0)
return r;
r = table_add_cell_stringf(table, &cell, "@%"PRI_USEC"%s%0*"PRI_USEC"",
usec / USEC_PER_SEC,
usec % USEC_PER_SEC ? "." : "",
usec % USEC_PER_SEC ? 6 : 0,
usec % USEC_PER_SEC);
if (usec % USEC_PER_SEC == 0)
r = table_add_cell_stringf(table, &cell, "@%"PRI_USEC,
usec / USEC_PER_SEC);
else
r = table_add_cell_stringf(table, &cell, "@%"PRI_USEC".%06"PRI_USEC"",
usec / USEC_PER_SEC,
usec % USEC_PER_SEC);
if (r < 0)
return r;
@ -1912,7 +1914,7 @@ static int test_calendar_one(usec_t n, const char *p) {
if (r < 0)
return log_error_errno(r, "Failed to format calendar specification '%s': %m", p);
table = table_new("NAME", "VALUE");
table = table_new("name", "value");
if (!table)
return log_oom();

View File

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

View File

@ -324,7 +324,7 @@ static int list_links(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_pager_flags);
table = table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
table = table_new("idx", "link", "type", "operational", "setup");
if (!table)
return log_oom();
@ -658,7 +658,7 @@ static int dump_address_labels(sd_netlink *rtnl) {
if (r < 0)
return r;
table = table_new("Label", "Prefix/Prefixlen");
table = table_new("label", "prefix/prefixlen");
if (!table)
return -ENOMEM;
@ -972,7 +972,7 @@ static int link_status_one(
(void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
(void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
table = table_new("DOT", "KEY", "VALUE");
table = table_new("dot", "key", "value");
if (!table)
return -ENOMEM;
@ -1274,7 +1274,7 @@ static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
(void) sd_network_get_operational_state(&operational_state);
operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
table = table_new("DOT", "KEY", "VALUE");
table = table_new("dot", "key", "value");
if (!table)
return -ENOMEM;
@ -1439,12 +1439,12 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_pager_flags);
table = table_new("LINK",
"CHASSIS ID",
"SYSTEM NAME",
"CAPS",
"PORT ID",
"PORT DESCRIPTION");
table = table_new("link",
"chassis id",
"system name",
"caps",
"port id",
"port description");
if (!table)
return -ENOMEM;

View File

@ -383,11 +383,11 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_ON_CALENDAR: {
_cleanup_(calendar_spec_freep) CalendarSpec *cs = NULL;
/* Let's make sure the given calendar event is not in the past */
r = calendar_spec_from_string(optarg, &cs);
if (r < 0)
return log_error_errno(r, "Failed to parse calendar event specification: %m");
/* Let's make sure the given calendar event is not in the past */
r = calendar_spec_next_usec(cs, now(CLOCK_REALTIME), NULL);
if (r == -ENOENT)
/* The calendar event is in the past — let's warn about this, but install it

View File

@ -5033,11 +5033,13 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) {
char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
char timespan1[FORMAT_TIMESPAN_MAX] = "n/a", timespan2[FORMAT_TIMESPAN_MAX] = "n/a";
bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base,
format_timespan(timespan1, sizeof(timespan1), v, 0),
format_timespan(timespan2, sizeof(timespan2), next_elapse, 0));
(void) format_timespan(timespan1, sizeof timespan1, v, 0);
(void) format_timespan(timespan2, sizeof timespan2, next_elapse, 0);
bus_print_property_valuef(name, expected_value, value,
"{ %s=%s ; next_elapse=%s }", base, timespan1, timespan2);
}
if (r < 0)
return bus_log_parse_error(r);
@ -5057,10 +5059,11 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) {
char timestamp[FORMAT_TIMESTAMP_MAX];
char timestamp[FORMAT_TIMESTAMP_MAX] = "n/a";
bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec,
format_timestamp(timestamp, sizeof(timestamp), next_elapse));
(void) format_timestamp(timestamp, sizeof(timestamp), next_elapse);
bus_print_property_valuef(name, expected_value, value,
"{ %s=%s ; next_elapse=%s }", base, spec, timestamp);
}
if (r < 0)
return bus_log_parse_error(r);