]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
timer: Add two more helper functions
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 19 Oct 2023 14:42:38 +0000 (16:42 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 20 Oct 2023 12:09:32 +0000 (14:09 +0200)
src/core/dbus-timer.c
src/core/timer.c
src/core/timer.h

index 80dd40a16a46d3c02287e5f4525bf4c04b6cb336..4f78a521de2ab9c17f59dd7dbba76f73a4afe8d6 100644 (file)
@@ -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[] = {
index ba18691cec840dc94468a618614c6f1d8a7fd93c..97233a88f69e0155c770c79da0d7fe506fc10ebe 100644 (file)
@@ -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",
index 914e42580e350840d0ed712dbaf72c2ccd524f4a..76d45b2ae1d165c0306d3bb59ba4c799380c23ea 100644 (file)
@@ -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_;