1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "dbus-timer.h"
28 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, timer_result, TimerResult);
30 static int property_get_monotonic_timers(
33 const char *interface,
35 sd_bus_message *reply,
37 sd_bus_error *error) {
47 r = sd_bus_message_open_container(reply, 'a', "(stt)");
51 LIST_FOREACH(value, v, t->values) {
52 _cleanup_free_ char *buf = NULL;
56 if (v->base == TIMER_CALENDAR)
59 s = timer_base_to_string(v->base);
60 assert(endswith(s, "Sec"));
69 memcpy(buf+l-3, "USec", 5);
71 r = sd_bus_message_append(reply, "(stt)", buf, v->value, v->next_elapse);
76 return sd_bus_message_close_container(reply);
79 static int property_get_calendar_timers(
82 const char *interface,
84 sd_bus_message *reply,
86 sd_bus_error *error) {
96 r = sd_bus_message_open_container(reply, 'a', "(sst)");
100 LIST_FOREACH(value, v, t->values) {
101 _cleanup_free_ char *buf = NULL;
103 if (v->base != TIMER_CALENDAR)
106 r = calendar_spec_to_string(v->calendar_spec, &buf);
110 r = sd_bus_message_append(reply, "(sst)", timer_base_to_string(v->base), buf, v->next_elapse);
115 return sd_bus_message_close_container(reply);
118 static int property_get_unit(
121 const char *interface,
122 const char *property,
123 sd_bus_message *reply,
125 sd_bus_error *error) {
127 Unit *u = userdata, *trigger;
133 trigger = UNIT_TRIGGER(u);
135 return sd_bus_message_append(reply, "s", trigger ? trigger->id : "");
138 static int property_get_next_elapse_monotonic(
141 const char *interface,
142 const char *property,
143 sd_bus_message *reply,
145 sd_bus_error *error) {
154 if (t->next_elapse_monotonic_or_boottime <= 0)
156 else if (t->wake_system) {
159 a = now(CLOCK_MONOTONIC);
160 b = now(CLOCK_BOOTTIME);
162 if (t->next_elapse_monotonic_or_boottime + a > b)
163 x = t->next_elapse_monotonic_or_boottime + a - b;
167 x = t->next_elapse_monotonic_or_boottime;
169 return sd_bus_message_append(reply, "t", x);
172 const sd_bus_vtable bus_timer_vtable[] = {
173 SD_BUS_VTABLE_START(0),
174 SD_BUS_PROPERTY("Unit", "s", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
175 SD_BUS_PROPERTY("TimersMonotonic", "a(stt)", property_get_monotonic_timers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
176 SD_BUS_PROPERTY("TimersCalendar", "a(sst)", property_get_calendar_timers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
177 SD_BUS_PROPERTY("NextElapseUSecRealtime", "t", bus_property_get_usec, offsetof(Timer, next_elapse_realtime), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
178 SD_BUS_PROPERTY("NextElapseUSecMonotonic", "t", property_get_next_elapse_monotonic, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
179 BUS_PROPERTY_DUAL_TIMESTAMP("LastTriggerUSec", offsetof(Timer, last_trigger), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
180 SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Timer, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
181 SD_BUS_PROPERTY("AccuracyUSec", "t", bus_property_get_usec, offsetof(Timer, accuracy_usec), SD_BUS_VTABLE_PROPERTY_CONST),
182 SD_BUS_PROPERTY("Persistent", "b", bus_property_get_bool, offsetof(Timer, persistent), SD_BUS_VTABLE_PROPERTY_CONST),
183 SD_BUS_PROPERTY("WakeSystem", "b", bus_property_get_bool, offsetof(Timer, wake_system), SD_BUS_VTABLE_PROPERTY_CONST),
187 static int bus_timer_set_transient_property(
190 sd_bus_message *message,
191 UnitSetPropertiesMode mode,
192 sd_bus_error *error) {
205 "OnUnitInactiveSec")) {
208 TimerBase b = _TIMER_BASE_INVALID;
211 b = timer_base_from_string(name);
215 r = sd_bus_message_read(message, "t", &u);
219 if (mode != UNIT_CHECK) {
220 char time[FORMAT_TIMESPAN_MAX];
222 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, format_timespan(time, sizeof(time), u, USEC_PER_MSEC));
224 v = new0(TimerValue, 1);
231 LIST_PREPEND(value, t->values, v);
236 } else if (streq(name, "OnCalendar")) {
239 CalendarSpec *c = NULL;
242 r = sd_bus_message_read(message, "s", &str);
246 if (mode != UNIT_CHECK) {
247 r = calendar_spec_from_string(str, &c);
251 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, str);
253 v = new0(TimerValue, 1);
256 calendar_spec_free(c);
260 v->base = TIMER_CALENDAR;
261 v->calendar_spec = c;
263 LIST_PREPEND(value, t->values, v);
268 } else if (streq(name, "AccuracySec")) {
272 r = sd_bus_message_read(message, "t", &u);
276 if (mode != UNIT_CHECK) {
277 char time[FORMAT_TIMESPAN_MAX];
279 t->accuracy_usec = u;
280 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, format_timespan(time, sizeof(time), u, USEC_PER_MSEC));
285 } else if (streq(name, "WakeSystem")) {
289 r = sd_bus_message_read(message, "b", &b);
293 if (mode != UNIT_CHECK) {
295 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, yes_no(t->wake_system));
305 int bus_timer_set_property(
308 sd_bus_message *message,
309 UnitSetPropertiesMode mode,
310 sd_bus_error *error) {
319 if (u->transient && u->load_state == UNIT_STUB) {
320 r = bus_timer_set_transient_property(t, name, message, mode, error);