]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-timer.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / core / dbus-timer.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "bus-util.h"
22 #include "dbus-timer.h"
23 #include "strv.h"
24 #include "timer.h"
25 #include "unit.h"
26
27 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, timer_result, TimerResult);
28
29 static int property_get_monotonic_timers(
30 sd_bus *bus,
31 const char *path,
32 const char *interface,
33 const char *property,
34 sd_bus_message *reply,
35 void *userdata,
36 sd_bus_error *error) {
37
38 Timer *t = userdata;
39 TimerValue *v;
40 int r;
41
42 assert(bus);
43 assert(reply);
44 assert(t);
45
46 r = sd_bus_message_open_container(reply, 'a', "(stt)");
47 if (r < 0)
48 return r;
49
50 LIST_FOREACH(value, v, t->values) {
51 _cleanup_free_ char *buf = NULL;
52 const char *s;
53 size_t l;
54
55 if (v->base == TIMER_CALENDAR)
56 continue;
57
58 s = timer_base_to_string(v->base);
59 assert(endswith(s, "Sec"));
60
61 /* s/Sec/USec/ */
62 l = strlen(s);
63 buf = new(char, l+2);
64 if (!buf)
65 return -ENOMEM;
66
67 memcpy(buf, s, l-3);
68 memcpy(buf+l-3, "USec", 5);
69
70 r = sd_bus_message_append(reply, "(stt)", buf, v->value, v->next_elapse);
71 if (r < 0)
72 return r;
73 }
74
75 return sd_bus_message_close_container(reply);
76 }
77
78 static int property_get_calendar_timers(
79 sd_bus *bus,
80 const char *path,
81 const char *interface,
82 const char *property,
83 sd_bus_message *reply,
84 void *userdata,
85 sd_bus_error *error) {
86
87 Timer *t = userdata;
88 TimerValue *v;
89 int r;
90
91 assert(bus);
92 assert(reply);
93 assert(t);
94
95 r = sd_bus_message_open_container(reply, 'a', "(sst)");
96 if (r < 0)
97 return r;
98
99 LIST_FOREACH(value, v, t->values) {
100 _cleanup_free_ char *buf = NULL;
101
102 if (v->base != TIMER_CALENDAR)
103 continue;
104
105 r = calendar_spec_to_string(v->calendar_spec, &buf);
106 if (r < 0)
107 return r;
108
109 r = sd_bus_message_append(reply, "(sst)", timer_base_to_string(v->base), buf, v->next_elapse);
110 if (r < 0)
111 return r;
112 }
113
114 return sd_bus_message_close_container(reply);
115 }
116
117 static int property_get_unit(
118 sd_bus *bus,
119 const char *path,
120 const char *interface,
121 const char *property,
122 sd_bus_message *reply,
123 void *userdata,
124 sd_bus_error *error) {
125
126 Unit *u = userdata, *trigger;
127
128 assert(bus);
129 assert(reply);
130 assert(u);
131
132 trigger = UNIT_TRIGGER(u);
133
134 return sd_bus_message_append(reply, "s", trigger ? trigger->id : "");
135 }
136
137 static int property_get_next_elapse_monotonic(
138 sd_bus *bus,
139 const char *path,
140 const char *interface,
141 const char *property,
142 sd_bus_message *reply,
143 void *userdata,
144 sd_bus_error *error) {
145
146 Timer *t = userdata;
147 usec_t x;
148
149 assert(bus);
150 assert(reply);
151 assert(t);
152
153 if (t->next_elapse_monotonic_or_boottime <= 0)
154 x = 0;
155 else if (t->wake_system) {
156 usec_t a, b;
157
158 a = now(CLOCK_MONOTONIC);
159 b = now(CLOCK_BOOTTIME);
160
161 if (t->next_elapse_monotonic_or_boottime + a > b)
162 x = t->next_elapse_monotonic_or_boottime + a - b;
163 else
164 x = 0;
165 } else
166 x = t->next_elapse_monotonic_or_boottime;
167
168 return sd_bus_message_append(reply, "t", x);
169 }
170
171 const sd_bus_vtable bus_timer_vtable[] = {
172 SD_BUS_VTABLE_START(0),
173 SD_BUS_PROPERTY("Unit", "s", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
174 SD_BUS_PROPERTY("TimersMonotonic", "a(stt)", property_get_monotonic_timers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
175 SD_BUS_PROPERTY("TimersCalendar", "a(sst)", property_get_calendar_timers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
176 SD_BUS_PROPERTY("NextElapseUSecRealtime", "t", bus_property_get_usec, offsetof(Timer, next_elapse_realtime), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
177 SD_BUS_PROPERTY("NextElapseUSecMonotonic", "t", property_get_next_elapse_monotonic, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
178 BUS_PROPERTY_DUAL_TIMESTAMP("LastTriggerUSec", offsetof(Timer, last_trigger), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
179 SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Timer, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
180 SD_BUS_PROPERTY("AccuracyUSec", "t", bus_property_get_usec, offsetof(Timer, accuracy_usec), SD_BUS_VTABLE_PROPERTY_CONST),
181 SD_BUS_PROPERTY("RandomizedDelayUSec", "t", bus_property_get_usec, offsetof(Timer, random_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),
184 SD_BUS_PROPERTY("RemainAfterElapse", "b", bus_property_get_bool, offsetof(Timer, remain_after_elapse), SD_BUS_VTABLE_PROPERTY_CONST),
185 SD_BUS_VTABLE_END
186 };
187
188 static int bus_timer_set_transient_property(
189 Timer *t,
190 const char *name,
191 sd_bus_message *message,
192 UnitSetPropertiesMode mode,
193 sd_bus_error *error) {
194
195 int r;
196
197 assert(t);
198 assert(name);
199 assert(message);
200
201 if (STR_IN_SET(name,
202 "OnActiveSec",
203 "OnBootSec",
204 "OnStartupSec",
205 "OnUnitActiveSec",
206 "OnUnitInactiveSec")) {
207
208 TimerValue *v;
209 TimerBase b = _TIMER_BASE_INVALID;
210 usec_t u = 0;
211
212 b = timer_base_from_string(name);
213 if (b < 0)
214 return -EINVAL;
215
216 r = sd_bus_message_read(message, "t", &u);
217 if (r < 0)
218 return r;
219
220 if (mode != UNIT_CHECK) {
221 char time[FORMAT_TIMESPAN_MAX];
222
223 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, format_timespan(time, sizeof(time), u, USEC_PER_MSEC));
224
225 v = new0(TimerValue, 1);
226 if (!v)
227 return -ENOMEM;
228
229 v->base = b;
230 v->value = u;
231
232 LIST_PREPEND(value, t->values, v);
233 }
234
235 return 1;
236
237 } else if (streq(name, "OnCalendar")) {
238
239 TimerValue *v;
240 CalendarSpec *c = NULL;
241 const char *str;
242
243 r = sd_bus_message_read(message, "s", &str);
244 if (r < 0)
245 return r;
246
247 if (mode != UNIT_CHECK) {
248 r = calendar_spec_from_string(str, &c);
249 if (r < 0)
250 return r;
251
252 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, str);
253
254 v = new0(TimerValue, 1);
255 if (!v) {
256 calendar_spec_free(c);
257 return -ENOMEM;
258 }
259
260 v->base = TIMER_CALENDAR;
261 v->calendar_spec = c;
262
263 LIST_PREPEND(value, t->values, v);
264 }
265
266 return 1;
267
268 } else if (STR_IN_SET(name, "AccuracyUSec", "AccuracySec")) {
269 usec_t u = 0;
270
271 if (streq(name, "AccuracySec"))
272 log_notice("Client is using obsolete AccuracySec= transient property, please use AccuracyUSec= instead.");
273
274 r = sd_bus_message_read(message, "t", &u);
275 if (r < 0)
276 return r;
277
278 if (mode != UNIT_CHECK) {
279 t->accuracy_usec = u;
280 unit_write_drop_in_private_format(UNIT(t), mode, name, "AccuracySec=" USEC_FMT "us\n", u);
281 }
282
283 return 1;
284
285 } else if (streq(name, "RandomizedDelayUSec")) {
286 usec_t u = 0;
287
288 r = sd_bus_message_read(message, "t", &u);
289 if (r < 0)
290 return r;
291
292 if (mode != UNIT_CHECK) {
293 t->random_usec = u;
294 unit_write_drop_in_private_format(UNIT(t), mode, name, "RandomizedDelaySec=" USEC_FMT "us\n", u);
295 }
296
297 return 1;
298
299 } else if (streq(name, "WakeSystem")) {
300 int b;
301
302 r = sd_bus_message_read(message, "b", &b);
303 if (r < 0)
304 return r;
305
306 if (mode != UNIT_CHECK) {
307 t->wake_system = b;
308 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, yes_no(b));
309 }
310
311 return 1;
312
313 } else if (streq(name, "RemainAfterElapse")) {
314 int b;
315
316 r = sd_bus_message_read(message, "b", &b);
317 if (r < 0)
318 return r;
319
320 if (mode != UNIT_CHECK) {
321 t->remain_after_elapse = b;
322 unit_write_drop_in_private_format(UNIT(t), mode, name, "%s=%s\n", name, yes_no(b));
323 }
324
325 return 1;
326 }
327
328 return 0;
329 }
330
331 int bus_timer_set_property(
332 Unit *u,
333 const char *name,
334 sd_bus_message *message,
335 UnitSetPropertiesMode mode,
336 sd_bus_error *error) {
337
338 Timer *t = TIMER(u);
339 int r;
340
341 assert(t);
342 assert(name);
343 assert(message);
344
345 if (u->transient && u->load_state == UNIT_STUB) {
346 r = bus_timer_set_transient_property(t, name, message, mode, error);
347 if (r != 0)
348 return r;
349 }
350
351 return 0;
352 }