X Tutup
Skip to content

Commit b72ddf0

Browse files
committed
timedated: manage systemd-timesyncd directly instead of lists of alternatives
Alternative NTP implementations should add a: Conflicts=systemd-timesyncd.service to take over the built-in NTP functionality of systemd.
1 parent efab8d0 commit b72ddf0

File tree

3 files changed

+101
-169
lines changed

3 files changed

+101
-169
lines changed

Makefile.am

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ udevrulesdir=$(udevlibexecdir)/rules.d
106106
udevhwdbdir=$(udevlibexecdir)/hwdb.d
107107
catalogdir=$(prefix)/lib/systemd/catalog
108108
kernelinstalldir = $(prefix)/lib/kernel/install.d
109-
ntpunitsdir=$(prefix)/lib/systemd/ntp-units.d
110109

111110
# And these are the special ones for /
112111
rootprefix=@rootprefix@
@@ -4320,10 +4319,6 @@ dist_systemunit_DATA += \
43204319
polkitpolicy_files += \
43214320
src/timedate/org.freedesktop.timedate1.policy
43224321

4323-
INSTALL_DIRS += \
4324-
$(prefix)/lib/systemd/ntp-units.d \
4325-
$(sysconfdir)/systemd/ntp-units.d
4326-
43274322
SYSTEM_UNIT_ALIASES += \
43284323
systemd-timedated.service dbus-org.freedesktop.timedate1.service
43294324

@@ -4397,10 +4392,6 @@ EXTRA_DIST += \
43974392

43984393
CLEANFILES += \
43994394
src/timesync/timesyncd.conf
4400-
4401-
dist_ntpunits_DATA = \
4402-
src/timesync/90-systemd.list
4403-
44044395
endif
44054396

44064397
# ------------------------------------------------------------------------------

NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
systemd System and Service Manager
22

3+
CHANGES WITH 216:
4+
* timedated does no longer read NTP unit names from
5+
/usr/lib/systemd/ntp-units.d/*.list. Alternative NTP
6+
implementations should add a:
7+
Conflicts=systemd-timesyncd.service
8+
to take over and replace systemd's NTP functionality.
9+
310
CHANGES WITH 215:
411

512
* A new tool systemd-sysusers has been added. This tool

src/timedate/timedated.c

Lines changed: 94 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -180,211 +180,145 @@ static int context_write_data_local_rtc(Context *c) {
180180
return write_string_file_atomic_label("/etc/adjtime", w);
181181
}
182182

183-
static char** get_ntp_services(void) {
184-
_cleanup_strv_free_ char **r = NULL, **files = NULL;
185-
char **i;
186-
int k;
187-
188-
k = conf_files_list(&files, ".list", NULL,
189-
"/etc/systemd/ntp-units.d",
190-
"/run/systemd/ntp-units.d",
191-
"/usr/local/lib/systemd/ntp-units.d",
192-
"/usr/lib/systemd/ntp-units.d",
193-
NULL);
194-
if (k < 0)
195-
return NULL;
196-
197-
STRV_FOREACH(i, files) {
198-
_cleanup_fclose_ FILE *f;
199-
200-
f = fopen(*i, "re");
201-
if (!f)
202-
continue;
203-
204-
for (;;) {
205-
char line[PATH_MAX], *l;
206-
207-
if (!fgets(line, sizeof(line), f)) {
208-
if (ferror(f))
209-
log_error("Failed to read NTP unit file: %m");
210-
211-
break;
212-
}
213-
214-
l = strstrip(line);
215-
if (l[0] == 0 || l[0] == '#')
216-
continue;
217-
218-
if (strv_extend(&r, l) < 0) {
219-
log_oom();
220-
return NULL;
221-
}
222-
}
223-
}
224-
225-
i = r;
226-
r = NULL; /* avoid cleanup */
227-
228-
return strv_uniq(i);
229-
}
230-
231183
static int context_read_ntp(Context *c, sd_bus *bus) {
232-
_cleanup_strv_free_ char **l;
233-
char **i;
184+
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
185+
sd_bus_message *reply = NULL;
186+
const char *s;
234187
int r;
235188

236189
assert(c);
237190
assert(bus);
238191

239-
l = get_ntp_services();
240-
STRV_FOREACH(i, l) {
241-
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
242-
sd_bus_message *reply = NULL;
243-
const char *s;
244-
245-
r = sd_bus_call_method(
246-
bus,
247-
"org.freedesktop.systemd1",
248-
"/org/freedesktop/systemd1",
249-
"org.freedesktop.systemd1.Manager",
250-
"GetUnitFileState",
251-
&error,
252-
&reply,
253-
"s",
254-
*i);
255-
256-
if (r < 0) {
257-
/* This implementation does not exist. Try the next one. */
258-
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND))
259-
continue;
192+
r = sd_bus_call_method(
193+
bus,
194+
"org.freedesktop.systemd1",
195+
"/org/freedesktop/systemd1",
196+
"org.freedesktop.systemd1.Manager",
197+
"GetUnitFileState",
198+
&error,
199+
&reply,
200+
"s",
201+
"systemd-timesyncd.service");
260202

261-
return r;
262-
}
203+
if (r < 0) {
204+
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
205+
sd_bus_error_has_name(&error, "org.freedesktop.systemd1.LoadFailed") ||
206+
sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit"))
207+
return 0;
263208

264-
r = sd_bus_message_read(reply, "s", &s);
265-
if (r < 0)
266-
return r;
209+
return r;
210+
}
267211

268-
c->can_ntp = true;
269-
c->use_ntp = STR_IN_SET(s, "enabled", "enabled-runtime");
212+
r = sd_bus_message_read(reply, "s", &s);
213+
if (r < 0)
214+
return r;
270215

271-
return 0;
272-
}
216+
c->can_ntp = true;
217+
c->use_ntp = STR_IN_SET(s, "enabled", "enabled-runtime");
273218

274219
return 0;
275220
}
276221

277222
static int context_start_ntp(Context *c, sd_bus *bus, sd_bus_error *error) {
278-
_cleanup_strv_free_ char **l = NULL;
279-
char **i;
280223
int r;
281224

282225
assert(c);
283226
assert(bus);
284227
assert(error);
285228

286-
l = get_ntp_services();
287-
STRV_FOREACH(i, l) {
288-
289-
if (c->use_ntp)
290-
r = sd_bus_call_method(
291-
bus,
292-
"org.freedesktop.systemd1",
293-
"/org/freedesktop/systemd1",
294-
"org.freedesktop.systemd1.Manager",
295-
"StartUnit",
296-
error,
297-
NULL,
298-
"ss", *i, "replace");
299-
else
300-
r = sd_bus_call_method(
301-
bus,
302-
"org.freedesktop.systemd1",
303-
"/org/freedesktop/systemd1",
304-
"org.freedesktop.systemd1.Manager",
305-
"StopUnit",
306-
error,
307-
NULL,
308-
"ss", *i, "replace");
309-
310-
if (r < 0) {
311-
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
312-
sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
313-
sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit")) {
314-
/* This implementation does not exist. Try the next one. */
315-
sd_bus_error_free(error);
316-
continue;
317-
}
229+
if (c->use_ntp)
230+
r = sd_bus_call_method(
231+
bus,
232+
"org.freedesktop.systemd1",
233+
"/org/freedesktop/systemd1",
234+
"org.freedesktop.systemd1.Manager",
235+
"StartUnit",
236+
error,
237+
NULL,
238+
"ss",
239+
"systemd-timesyncd.service",
240+
"replace");
241+
else
242+
r = sd_bus_call_method(
243+
bus,
244+
"org.freedesktop.systemd1",
245+
"/org/freedesktop/systemd1",
246+
"org.freedesktop.systemd1.Manager",
247+
"StopUnit",
248+
error,
249+
NULL,
250+
"ss",
251+
"systemd-timesyncd.service",
252+
"replace");
318253

319-
return r;
254+
if (r < 0) {
255+
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
256+
sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
257+
sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit")) {
258+
sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
259+
return -ENOTSUP;
320260
}
321261

322-
return 1;
262+
return r;
323263
}
324264

325-
sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
326-
return -ENOTSUP;
265+
return 0;
327266
}
328267

329268
static int context_enable_ntp(Context*c, sd_bus *bus, sd_bus_error *error) {
330-
_cleanup_strv_free_ char **l = NULL;
331-
char **i;
332269
int r;
333270

334271
assert(c);
335272
assert(bus);
336273
assert(error);
337274

338-
l = get_ntp_services();
339-
STRV_FOREACH(i, l) {
340-
if (c->use_ntp)
341-
r = sd_bus_call_method(
342-
bus,
343-
"org.freedesktop.systemd1",
344-
"/org/freedesktop/systemd1",
345-
"org.freedesktop.systemd1.Manager",
346-
"EnableUnitFiles",
347-
error,
348-
NULL,
349-
"asbb", 1, *i, false, true);
350-
else
351-
r = sd_bus_call_method(
352-
bus,
353-
"org.freedesktop.systemd1",
354-
"/org/freedesktop/systemd1",
355-
"org.freedesktop.systemd1.Manager",
356-
"DisableUnitFiles",
357-
error,
358-
NULL,
359-
"asb", 1, *i, false);
360-
361-
if (r < 0) {
362-
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND)) {
363-
/* This implementation does not exist. Try the next one. */
364-
sd_bus_error_free(error);
365-
continue;
366-
}
367-
368-
return r;
369-
}
370-
275+
if (c->use_ntp)
371276
r = sd_bus_call_method(
372277
bus,
373278
"org.freedesktop.systemd1",
374279
"/org/freedesktop/systemd1",
375280
"org.freedesktop.systemd1.Manager",
376-
"Reload",
281+
"EnableUnitFiles",
377282
error,
378283
NULL,
379-
NULL);
380-
if (r < 0)
381-
return r;
284+
"asbb", 1,
285+
"systemd-timesyncd.service",
286+
false, true);
287+
else
288+
r = sd_bus_call_method(
289+
bus,
290+
"org.freedesktop.systemd1",
291+
"/org/freedesktop/systemd1",
292+
"org.freedesktop.systemd1.Manager",
293+
"DisableUnitFiles",
294+
error,
295+
NULL,
296+
"asb", 1,
297+
"systemd-timesyncd.service",
298+
false);
382299

383-
return 1;
300+
if (r < 0) {
301+
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND)) {
302+
sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
303+
return -ENOTSUP;
304+
}
305+
306+
return r;
384307
}
385308

386-
sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
387-
return -ENOTSUP;
309+
r = sd_bus_call_method(
310+
bus,
311+
"org.freedesktop.systemd1",
312+
"/org/freedesktop/systemd1",
313+
"org.freedesktop.systemd1.Manager",
314+
"Reload",
315+
error,
316+
NULL,
317+
NULL);
318+
if (r < 0)
319+
return r;
320+
321+
return 0;
388322
}
389323

390324
static int property_get_rtc_time(

0 commit comments

Comments
 (0)
X Tutup