X Tutup
Skip to content

Commit 36697dc

Browse files
committed
timer: implement calendar time events
1 parent 8a11751 commit 36697dc

File tree

16 files changed

+1288
-103
lines changed

16 files changed

+1288
-103
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/test-calendarspec
12
/test-catalog
23
/test-replace-var
34
/test-journal-enum

Makefile.am

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,9 @@ libsystemd_shared_la_SOURCES = \
833833
src/shared/hwclock.c \
834834
src/shared/hwclock.h \
835835
src/shared/time-dst.c \
836-
src/shared/time-dst.h
836+
src/shared/time-dst.h \
837+
src/shared/calendarspec.c \
838+
src/shared/calendarspec.h
837839

838840
#-------------------------------------------------------------------------------
839841
noinst_LTLIBRARIES += \
@@ -1218,7 +1220,8 @@ noinst_PROGRAMS += \
12181220
test-date \
12191221
test-sleep \
12201222
test-replace-var \
1221-
test-sched-prio
1223+
test-sched-prio \
1224+
test-calendarspec
12221225

12231226
TESTS += \
12241227
test-job-type \
@@ -1229,7 +1232,8 @@ TESTS += \
12291232
test-date \
12301233
test-sleep \
12311234
test-replace-var \
1232-
test-sched-prio
1235+
test-sched-prio \
1236+
test-calendarspec
12331237

12341238
EXTRA_DIST += \
12351239
test/sched_idle_bad.service \
@@ -1320,6 +1324,12 @@ test_replace_var_SOURCES = \
13201324
test_replace_var_LDADD = \
13211325
libsystemd-shared.la
13221326

1327+
test_calendarspec_SOURCES = \
1328+
src/test/test-calendarspec.c
1329+
1330+
test_calendarspec_LDADD = \
1331+
libsystemd-shared.la
1332+
13231333
test_daemon_SOURCES = \
13241334
src/test/test-daemon.c
13251335

src/core/dbus-timer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ static int bus_timer_append_timers(DBusMessageIter *i, const char *property, voi
7979

8080
/* s/Sec/USec/ */
8181
l = strlen(t);
82-
if (!(buf = new(char, l+2)))
82+
buf = new(char, l+2);
83+
if (!buf)
8384
return -ENOMEM;
8485

8586
memcpy(buf, t, l-3);
@@ -121,7 +122,8 @@ static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_timer_append_timer_result, timer_resu
121122
static const BusProperty bus_timer_properties[] = {
122123
{ "Unit", bus_timer_append_unit, "s", 0 },
123124
{ "Timers", bus_timer_append_timers, "a(stt)", 0 },
124-
{ "NextElapseUSec", bus_property_append_usec, "t", offsetof(Timer, next_elapse) },
125+
{ "NextElapseUSec", bus_property_append_usec, "t", offsetof(Timer, next_elapse_monotonic) },
126+
{ "NextElapseUSecRealtime", bus_property_append_usec, "t", offsetof(Timer, next_elapse_realtime) },
125127
{ "Result", bus_timer_append_timer_result,"s", offsetof(Timer, result) },
126128
{ NULL, }
127129
};

src/core/load-fragment-gperf.gperf.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ Swap.TimeoutSec, config_parse_usec, 0,
234234
EXEC_CONTEXT_CONFIG_ITEMS(Swap)m4_dnl
235235
KILL_CONTEXT_CONFIG_ITEMS(Swap)m4_dnl
236236
m4_dnl
237+
Timer.OnCalendar, config_parse_timer, 0, 0
237238
Timer.OnActiveSec, config_parse_timer, 0, 0
238239
Timer.OnBootSec, config_parse_timer, 0, 0
239240
Timer.OnStartupSec, config_parse_timer, 0, 0

src/core/load-fragment.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,30 +1136,47 @@ int config_parse_timer(
11361136
void *userdata) {
11371137

11381138
Timer *t = data;
1139-
usec_t u;
1139+
usec_t u = 0;
11401140
TimerValue *v;
11411141
TimerBase b;
1142+
CalendarSpec *c = NULL;
1143+
clockid_t id;
11421144

11431145
assert(filename);
11441146
assert(lvalue);
11451147
assert(rvalue);
11461148
assert(data);
11471149

1148-
if ((b = timer_base_from_string(lvalue)) < 0) {
1150+
b = timer_base_from_string(lvalue);
1151+
if (b < 0) {
11491152
log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
11501153
return 0;
11511154
}
11521155

1153-
if (parse_usec(rvalue, &u) < 0) {
1154-
log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1155-
return 0;
1156+
if (b == TIMER_CALENDAR) {
1157+
if (calendar_spec_from_string(rvalue, &c) < 0) {
1158+
log_error("[%s:%u] Failed to parse calendar specification, ignoring: %s", filename, line, rvalue);
1159+
return 0;
1160+
}
1161+
1162+
id = CLOCK_REALTIME;
1163+
} else {
1164+
if (parse_usec(rvalue, &u) < 0) {
1165+
log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1166+
return 0;
1167+
}
1168+
1169+
id = CLOCK_MONOTONIC;
11561170
}
11571171

1158-
if (!(v = new0(TimerValue, 1)))
1172+
v = new0(TimerValue, 1);
1173+
if (!v)
11591174
return -ENOMEM;
11601175

11611176
v->base = b;
1177+
v->clock_id = id;
11621178
v->value = u;
1179+
v->calendar_spec = c;
11631180

11641181
LIST_PREPEND(TimerValue, value, t->values, v);
11651182

src/core/mount.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,12 @@ static int mount_coldplug(Unit *u) {
741741
if (m->control_pid <= 0)
742742
return -EBADMSG;
743743

744-
if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0)
744+
r = unit_watch_pid(UNIT(m), m->control_pid);
745+
if (r < 0)
745746
return r;
746747

747-
if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
748+
r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
749+
if (r < 0)
748750
return r;
749751
}
750752

@@ -800,7 +802,8 @@ static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
800802
assert(c);
801803
assert(_pid);
802804

803-
if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
805+
r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
806+
if (r < 0)
804807
goto fail;
805808

806809
if ((r = exec_spawn(c,
@@ -900,7 +903,8 @@ static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
900903
}
901904

902905
if (wait_for_exit) {
903-
if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
906+
r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
907+
if (r < 0)
904908
goto fail;
905909

906910
mount_set_state(m, state);

src/core/service.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static void service_handle_watchdog(Service *s) {
249249
return;
250250
}
251251

252-
r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
252+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
253253
if (r < 0)
254254
log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
255255
}
@@ -1599,7 +1599,8 @@ static int service_coldplug(Unit *u) {
15991599

16001600
k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
16011601

1602-
if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1602+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1603+
if (r < 0)
16031604
return r;
16041605
}
16051606
}
@@ -1744,7 +1745,7 @@ static int service_spawn(
17441745
}
17451746

17461747
if (timeout && s->timeout_start_usec) {
1747-
r = unit_watch_timer(UNIT(s), s->timeout_start_usec, &s->timer_watch);
1748+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
17481749
if (r < 0)
17491750
goto fail;
17501751
} else
@@ -1899,7 +1900,7 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
18991900
!set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
19001901
) {
19011902

1902-
r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
1903+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
19031904
if (r < 0)
19041905
goto fail;
19051906

@@ -2012,7 +2013,7 @@ static void service_enter_signal(Service *s, ServiceState state, ServiceResult f
20122013

20132014
if (wait_for_exit) {
20142015
if (s->timeout_stop_usec > 0) {
2015-
r = unit_watch_timer(UNIT(s), s->timeout_stop_usec, &s->timer_watch);
2016+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
20162017
if (r < 0)
20172018
goto fail;
20182019
}
@@ -2262,7 +2263,7 @@ static void service_enter_restart(Service *s) {
22622263
/* Don't restart things if we are going down anyway */
22632264
log_info("Stop job pending for unit, delaying automatic restart.");
22642265

2265-
r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
2266+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
22662267
if (r < 0)
22672268
goto fail;
22682269

src/core/socket.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,10 +1146,12 @@ static int socket_coldplug(Unit *u) {
11461146
if (s->control_pid <= 0)
11471147
return -EBADMSG;
11481148

1149-
if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1149+
r = unit_watch_pid(UNIT(s), s->control_pid);
1150+
if (r < 0)
11501151
return r;
11511152

1152-
if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1153+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
1154+
if (r < 0)
11531155
return r;
11541156
}
11551157

@@ -1181,10 +1183,12 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
11811183
assert(c);
11821184
assert(_pid);
11831185

1184-
if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1186+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
1187+
if (r < 0)
11851188
goto fail;
11861189

1187-
if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1190+
argv = unit_full_printf_strv(UNIT(s), c->argv);
1191+
if (!argv) {
11881192
r = -ENOMEM;
11891193
goto fail;
11901194
}
@@ -1306,7 +1310,8 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
13061310
}
13071311

13081312
if (wait_for_exit) {
1309-
if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1313+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
1314+
if (r < 0)
13101315
goto fail;
13111316

13121317
socket_set_state(s, state);

src/core/swap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ static int swap_coldplug(Unit *u) {
521521
if (r < 0)
522522
return r;
523523

524-
r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch);
524+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
525525
if (r < 0)
526526
return r;
527527
}
@@ -584,7 +584,7 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
584584
assert(c);
585585
assert(_pid);
586586

587-
r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch);
587+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
588588
if (r < 0)
589589
goto fail;
590590

@@ -689,7 +689,7 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
689689
}
690690

691691
if (wait_for_exit) {
692-
r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch);
692+
r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
693693
if (r < 0)
694694
goto fail;
695695

0 commit comments

Comments
 (0)
X Tutup