1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
***/
#include "alloc-util.h"
#include "bus-util.h"
#include "dbus-timer.h"
#include "dbus-util.h"
#include "strv.h"
#include "timer.h"
#include "unit.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, timer_result, TimerResult);
static int property_get_monotonic_timers(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Timer *t = userdata;
TimerValue *v;
int r;
assert(bus);
assert(reply);
assert(t);
r = sd_bus_message_open_container(reply, 'a', "(stt)");
if (r < 0)
return r;
LIST_FOREACH(value, v, t->values) {
_cleanup_free_ char *buf = NULL;
const char *s;
size_t l;
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)
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);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_calendar_timers(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Timer *t = userdata;
TimerValue *v;
int r;
assert(bus);
assert(reply);
assert(t);
r = sd_bus_message_open_container(reply, 'a', "(sst)");
if (r < 0)
return r;
LIST_FOREACH(value, v, t->values) {
_cleanup_free_ char *buf = NULL;
if (v->base != TIMER_CALENDAR)
continue;
r = calendar_spec_to_string(v->calendar_spec, &buf);
if (r < 0)
return r;
r = sd_bus_message_append(reply, "(sst)", timer_base_to_string(v->base), buf, v->next_elapse);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_unit(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Unit *u = userdata, *trigger;
assert(bus);
assert(reply);
assert(u);
trigger = UNIT_TRIGGER(u);
return sd_bus_message_append(reply, "s", trigger ? trigger->id : "");
}
static int property_get_next_elapse_monotonic(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Timer *t = userdata;
assert(bus);
assert(reply);
assert(t);
return sd_bus_message_append(reply, "t",
(uint64_t) usec_shift_clock(t->next_elapse_monotonic_or_boottime,
TIMER_MONOTONIC_CLOCK(t), CLOCK_MONOTONIC));
}
const sd_bus_vtable bus_timer_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Unit", "s", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("TimersMonotonic", "a(stt)", property_get_monotonic_timers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
SD_BUS_PROPERTY("TimersCalendar", "a(sst)", property_get_calendar_timers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
SD_BUS_PROPERTY("NextElapseUSecRealtime", "t", bus_property_get_usec, offsetof(Timer, next_elapse_realtime), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("NextElapseUSecMonotonic", "t", property_get_next_elapse_monotonic, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("LastTriggerUSec", offsetof(Timer, last_trigger), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Timer, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("AccuracyUSec", "t", bus_property_get_usec, offsetof(Timer, accuracy_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RandomizedDelayUSec", "t", bus_property_get_usec, offsetof(Timer, random_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Persistent", "b", bus_property_get_bool, offsetof(Timer, persistent), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("WakeSystem", "b", bus_property_get_bool, offsetof(Timer, wake_system), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RemainAfterElapse", "b", bus_property_get_bool, offsetof(Timer, remain_after_elapse), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_VTABLE_END
};
static int bus_timer_set_transient_property(
Timer *t,
const char *name,
sd_bus_message *message,
UnitWriteFlags flags,
sd_bus_error *error) {
Unit *u = UNIT(t);
int r;
assert(t);
assert(name);
assert(message);
flags |= UNIT_PRIVATE;
if (streq(name, "AccuracyUSec"))
return bus_set_transient_usec(u, name, &t->accuracy_usec, message, flags, error);
if (streq(name, "AccuracySec")) {
log_notice("Client is using obsolete AccuracySec= transient property, please use AccuracyUSec= instead.");
return bus_set_transient_usec(u, "AccuracyUSec", &t->accuracy_usec, message, flags, error);
}
if (streq(name, "RandomizedDelayUSec"))
return bus_set_transient_usec(u, name, &t->random_usec, message, flags, error);
if (streq(name, "WakeSystem"))
return bus_set_transient_bool(u, name, &t->wake_system, message, flags, error);
if (streq(name, "Persistent"))
return bus_set_transient_bool(u, name, &t->persistent, message, flags, error);
if (streq(name, "RemainAfterElapse"))
return bus_set_transient_bool(u, name, &t->remain_after_elapse, message, flags, error);
if (streq(name, "TimersMonotonic")) {
const char *base_name;
usec_t usec = 0;
bool empty = true;
r = sd_bus_message_enter_container(message, 'a', "(st)");
if (r < 0)
return r;
while ((r = sd_bus_message_read(message, "(st)", &base_name, &usec)) > 0) {
TimerBase b;
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);
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 = new0(TimerValue, 1);
if (!v)
return -ENOMEM;
v->base = b;
v->value = usec;
LIST_PREPEND(value, t->values, v);
}
empty = false;
}
if (r < 0)
return r;
r = sd_bus_message_exit_container(message);
if (r < 0)
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
timer_free_values(t);
unit_write_setting(u, flags, name, "OnActiveSec=");
}
return 1;
} else if (streq(name, "TimersCalendar")) {
const char *base_name, *str;
bool empty = true;
r = sd_bus_message_enter_container(message, 'a', "(ss)");
if (r < 0)
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);
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);
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 = new0(TimerValue, 1);
if (!v)
return -ENOMEM;
v->base = b;
v->calendar_spec = TAKE_PTR(c);
LIST_PREPEND(value, t->values, v);
}
empty = false;
}
if (r < 0)
return r;
r = sd_bus_message_exit_container(message);
if (r < 0)
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
timer_free_values(t);
unit_write_setting(u, flags, name, "OnCalendar=");
}
return 1;
} else if (STR_IN_SET(name,
"OnActiveSec",
"OnBootSec",
"OnStartupSec",
"OnUnitActiveSec",
"OnUnitInactiveSec")) {
TimerValue *v;
TimerBase b = _TIMER_BASE_INVALID;
usec_t usec = 0;
log_notice("Client is using obsolete %s= transient property, please use TimersMonotonic= instead.", name);
b = timer_base_from_string(name);
if (b < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown timer base");
r = sd_bus_message_read(message, "t", &usec);
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 = new0(TimerValue, 1);
if (!v)
return -ENOMEM;
v->base = b;
v->value = usec;
LIST_PREPEND(value, t->values, v);
}
return 1;
} 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);
r = sd_bus_message_read(message, "s", &str);
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 = new0(TimerValue, 1);
if (!v)
return -ENOMEM;
v->base = TIMER_CALENDAR;
v->calendar_spec = TAKE_PTR(c);
LIST_PREPEND(value, t->values, v);
}
return 1;
}
return 0;
}
int bus_timer_set_property(
Unit *u,
const char *name,
sd_bus_message *message,
UnitWriteFlags mode,
sd_bus_error *error) {
Timer *t = TIMER(u);
assert(t);
assert(name);
assert(message);
if (u->transient && u->load_state == UNIT_STUB)
return bus_timer_set_transient_property(t, name, message, mode, error);
return 0;
}
|