From: Daan De Meyer Date: Thu, 19 Oct 2023 14:42:38 +0000 (+0200) Subject: timer: Add two more helper functions X-Git-Tag: v255-rc1~182^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f8a990a0a14f714c689e24c8d34df74f2f73c3ca;p=thirdparty%2Fsystemd.git timer: Add two more helper functions --- diff --git a/src/core/dbus-timer.c b/src/core/dbus-timer.c index 80dd40a16a4..4f78a521de2 100644 --- a/src/core/dbus-timer.c +++ b/src/core/dbus-timer.c @@ -30,26 +30,16 @@ static int property_get_monotonic_timers( return r; LIST_FOREACH(value, v, t->values) { - _cleanup_free_ char *buf = NULL; - const char *s; - size_t l; + _cleanup_free_ char *usec = NULL; if (v->base == TIMER_CALENDAR) continue; - s = timer_base_to_string(v->base); - assert(endswith(s, "Sec")); - - /* s/Sec/USec/ */ - l = strlen(s); - buf = new(char, l+2); - if (!buf) + usec = timer_base_to_usec_string(v->base); + if (!usec) return -ENOMEM; - memcpy(buf, s, l-3); - memcpy(buf+l-3, "USec", 5); - - r = sd_bus_message_append(reply, "(stt)", buf, v->value, v->next_elapse); + r = sd_bus_message_append(reply, "(stt)", usec, v->value, v->next_elapse); if (r < 0) return r; } @@ -108,9 +98,7 @@ static int property_get_next_elapse_monotonic( assert(bus); assert(reply); - return sd_bus_message_append(reply, "t", - (uint64_t) usec_shift_clock(t->next_elapse_monotonic_or_boottime, - TIMER_MONOTONIC_CLOCK(t), CLOCK_MONOTONIC)); + return sd_bus_message_append(reply, "t", timer_next_elapse_monotonic(t)); } const sd_bus_vtable bus_timer_vtable[] = { diff --git a/src/core/timer.c b/src/core/timer.c index ba18691cec8..97233a88f69 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -1001,6 +1001,13 @@ static int activation_details_timer_append_pair(ActivationDetails *details, char return 2; /* Return the number of pairs added to the env block */ } +uint64_t timer_next_elapse_monotonic(const Timer *t) { + assert(t); + + return (uint64_t) usec_shift_clock(t->next_elapse_monotonic_or_boottime, + TIMER_MONOTONIC_CLOCK(t), CLOCK_MONOTONIC); +} + static const char* const timer_base_table[_TIMER_BASE_MAX] = { [TIMER_ACTIVE] = "OnActiveSec", [TIMER_BOOT] = "OnBootSec", @@ -1012,6 +1019,31 @@ static const char* const timer_base_table[_TIMER_BASE_MAX] = { DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase); +char* timer_base_to_usec_string(TimerBase i) { + _cleanup_free_ char *buf = NULL; + const char *s; + size_t l; + + s = timer_base_to_string(i); + + if (endswith(s, "Sec")) { + /* s/Sec/USec/ */ + l = strlen(s); + buf = new(char, l+2); + if (!buf) + return NULL; + + memcpy(buf, s, l-3); + memcpy(buf+l-3, "USec", 5); + } else { + buf = strdup(s); + if (!buf) + return NULL; + } + + return TAKE_PTR(buf); +} + static const char* const timer_result_table[_TIMER_RESULT_MAX] = { [TIMER_SUCCESS] = "success", [TIMER_FAILURE_RESOURCES] = "resources", diff --git a/src/core/timer.h b/src/core/timer.h index 914e42580e3..76d45b2ae1d 100644 --- a/src/core/timer.h +++ b/src/core/timer.h @@ -72,6 +72,8 @@ struct ActivationDetailsTimer { #define TIMER_MONOTONIC_CLOCK(t) ((t)->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC) +uint64_t timer_next_elapse_monotonic(const Timer *t); + void timer_free_values(Timer *t); extern const UnitVTable timer_vtable; @@ -80,6 +82,8 @@ extern const ActivationDetailsVTable activation_details_timer_vtable; const char *timer_base_to_string(TimerBase i) _const_; TimerBase timer_base_from_string(const char *s) _pure_; +char* timer_base_to_usec_string(TimerBase i); + const char* timer_result_to_string(TimerResult i) _const_; TimerResult timer_result_from_string(const char *s) _pure_;