X Tutup
Skip to content

Commit afaae43

Browse files
committed
timedated: add back support for ntp-units.d/
We removed support for foreign services (and ntp-units.d/) in b72ddf0. Support for foreign services was added back in 5d28074, but through an environment variable. The problem with the env var approach is that it only works as a mechanism to select one item, and doesn't work nicely as a mechinism to create a list of items through drop-ins (because the env var can be easily overridden, but not extended). Having a list of "ntp providers" is important to be able to reliably disable all of them when that is requested. Another problem is that nobody ever bothered to care about our new "standard". ntp-units.d/ is a nice simple format that works and is already supported by chrony and ntpd and timedatex. If we were to introduce and ask people to follow a new standard, there should be some good reason for this. The idea with env vars has lower functionality, requires systemd-specific syntax. We should just re-adopt the format that we originally introduced and that seems to work for everyone, and more on to more interesting problems.
1 parent 670fb0b commit afaae43

File tree

4 files changed

+114
-43
lines changed

4 files changed

+114
-43
lines changed

man/systemd-timedated.service.xml

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<para><filename>systemd-timedated</filename> is a system service
3333
that may be used as a mechanism to change the system clock and
34-
timezone, as well as to enable/disable NTP time synchronization.
34+
timezone, as well as to enable/disable network time synchronization.
3535
<filename>systemd-timedated</filename> is automatically activated
3636
on request and terminates itself when it is unused.</para>
3737

@@ -46,25 +46,36 @@
4646
</refsect1>
4747

4848
<refsect1>
49-
<title>Environment</title>
50-
51-
<variablelist class='environment-variables'>
52-
<varlistentry>
53-
<term><varname>$SYSTEMD_TIMEDATED_NTP_SERVICES</varname></term>
54-
55-
<listitem><para>Colon-separated list of unit names of NTP client services.
56-
If not set, then
57-
<citerefentry><refentrytitle>systemd-timesyncd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
58-
is used. See the entries of NTP related commands of
59-
<citerefentry><refentrytitle>timedatectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
60-
for details about this.</para>
61-
62-
<para>Example:
63-
<programlisting>SYSTEMD_TIMEDATED_NTP_SERVICES=ntpd.service:chronyd.service:systemd-timesyncd.service</programlisting>
64-
</para></listitem>
65-
</varlistentry>
66-
</variablelist>
49+
<title>List of network time synchronization services</title>
50+
51+
<para><command>systemd-timesyncd</command> will look for files with a <literal>.list</literal> extension
52+
in <filename>ntp-units.d/</filename> directories. Each file is parsed as a list of unit names, one per
53+
line. Empty lines and lines with comments (<literal>#</literal>) are ignored. Files are read from
54+
<filename>/usr/lib/systemd/ntp-units.d/</filename> and the corresponding directories under
55+
<filename>/etc/</filename>, <filename>/run/</filename>, <filename>/usr/local/lib/</filename>. Files in
56+
<filename>/etc/</filename> override files with the same name in <filename>/run/</filename>,
57+
<filename>/usr/local/lib/</filename>, and <filename>/usr/lib/</filename>. Files in
58+
<filename>/run/</filename> override files with the same name under <filename>/usr/</filename>. Packages
59+
should install their configuration files in <filename>/usr/lib/</filename> (distribution packages) or
60+
<filename>/usr/local/lib/</filename> (local installs).</para>
61+
62+
<example>
63+
<title><filename>ntp-units.d/</filename> entry for <command>systemd-timesyncd</command></title>
64+
<programlisting># /usr/lib/systemd/ntp-units.d/80-systemd-timesync.list
65+
systemd-timesyncd.service
66+
</programlisting>
67+
</example>
68+
69+
<para>If the environment variable <varname>$SYSTEMD_TIMEDATED_NTP_SERVICES</varname> is set,
70+
<command>systemd-timesyncd</command> will parse the contents of that variable as a colon-separated list
71+
of unit names. When set, this variable overrides the file-based list described above.</para>
72+
73+
<example>
74+
<title>An override that specifies that <command>chronyd</command> should be used if available</title>
75+
<programlisting>SYSTEMD_TIMEDATED_NTP_SERVICES=chronyd.service:systemd-timesyncd.service</programlisting>
76+
</example>
6777
</refsect1>
78+
6879
<refsect1>
6980
<title>See Also</title>
7081
<para>

man/timedatectl.xml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323

2424
<refsynopsisdiv>
2525
<cmdsynopsis>
26-
<command>timedatectl <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="req">COMMAND</arg></command>
26+
<command>timedatectl</command>
27+
<arg choice="opt" rep="repeat">OPTIONS</arg>
28+
<arg choice="req">COMMAND</arg>
2729
</cmdsynopsis>
2830
</refsynopsisdiv>
2931

3032
<refsect1>
3133
<title>Description</title>
3234

33-
<para><command>timedatectl</command> may be used to query and
34-
change the system clock and its settings.</para>
35+
<para><command>timedatectl</command> may be used to query and change the system clock and its settings,
36+
and enable or disable time synchronization services.</para>
3537

3638
<para>Use
3739
<citerefentry><refentrytitle>systemd-firstboot</refentrytitle><manvolnum>1</manvolnum></citerefentry>
3840
to initialize the system time zone for mounted (but not booted)
3941
system images.</para>
4042

41-
<para><command>timedatectl</command> may be used to show the current status of
43+
<para><command>timedatectl</command> may be used to show the current status of time synchronization
44+
services, for example
4245
<citerefentry><refentrytitle>systemd-timesyncd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
4346
</para>
4447

@@ -123,11 +126,8 @@
123126
<varlistentry>
124127
<term><command>status</command></term>
125128

126-
<listitem><para>Show current settings of the system clock and RTC,
127-
including whether network time synchronization through
128-
<filename>systemd-timesyncd.service</filename> is active. Even if it is
129-
inactive, a different service might still synchronize the clock.
130-
If no command is specified, this is the implied default.
129+
<listitem><para>Show current settings of the system clock and RTC, including whether network time
130+
synchronization is active. If no command is specified, this is the implied default.
131131
</para></listitem>
132132
</varlistentry>
133133

@@ -193,11 +193,11 @@
193193
<varlistentry>
194194
<term><command>set-ntp [BOOL]</command></term>
195195

196-
<listitem><para>Takes a boolean argument. Controls whether network time synchronization is active
197-
and enabled (if available). If the argument is true, this enables and starts the first existed
198-
service listed in the environment variable <varname>$SYSTEMD_TIMEDATED_NTP_SERVICES</varname>
199-
of <filename>systemd-timedated.service</filename>. If the argument is false, then this disables and
200-
stops the all services listed in <varname>$SYSTEMD_TIMEDATED_NTP_SERVICES</varname>.</para></listitem>
196+
<listitem><para>Takes a boolean argument. Controls whether network time synchronization is active and
197+
enabled (if available). If the argument is true, this enables and starts the first existing network
198+
synchronization service. If the argument is false, then this disables and stops the known network
199+
synchronization services. The way that the list of services is built is described below.</para>
200+
</listitem>
201201
</varlistentry>
202202

203203
</variablelist>
@@ -250,8 +250,7 @@
250250
<refsect1>
251251
<title>Exit status</title>
252252

253-
<para>On success, 0 is returned, a non-zero failure
254-
code otherwise.</para>
253+
<para>On success, 0 is returned, a non-zero failure code otherwise.</para>
255254
</refsect1>
256255

257256
<xi:include href="less-variables.xml" />

src/shared/pretty-print.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ static int guess_type(const char **name, bool *is_usr, bool *is_collection, cons
247247
if (path_equal(n, "kernel/install.d"))
248248
ext = ".install";
249249

250+
if (path_equal(n, "systemd/ntp-units.d")) {
251+
coll = true;
252+
ext = ".list";
253+
}
254+
250255
if (PATH_IN_SET(n, "systemd/system-preset", "systemd/user-preset")) {
251256
coll = true;
252257
ext = ".preset";

src/timedate/timedated.c

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include "bus-error.h"
1616
#include "bus-util.h"
1717
#include "clock-util.h"
18+
#include "conf-files.h"
1819
#include "def.h"
20+
#include "fd-util.h"
1921
#include "fileio-label.h"
2022
#include "fileio.h"
2123
#include "fs-util.h"
@@ -36,6 +38,8 @@
3638
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
3739
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
3840

41+
#define UNIT_LIST_DIRS (const char* const*) CONF_PATHS_STRV("systemd/ntp-units.d")
42+
3943
typedef struct UnitStatusInfo {
4044
char *name;
4145
char *load_state;
@@ -117,20 +121,17 @@ static int context_add_ntp_service(Context *c, const char *s) {
117121
return 0;
118122
}
119123

120-
static int context_parse_ntp_services(Context *c) {
124+
static int context_parse_ntp_services_from_environment(Context *c) {
121125
const char *env, *p;
122126
int r;
123127

124128
assert(c);
125129

126130
env = getenv("SYSTEMD_TIMEDATED_NTP_SERVICES");
127-
if (!env) {
128-
r = context_add_ntp_service(c, "systemd-timesyncd.service");
129-
if (r < 0)
130-
log_warning_errno(r, "Failed to add NTP service \"systemd-timesyncd.service\", ignoring: %m");
131-
131+
if (!env)
132132
return 0;
133-
}
133+
134+
log_debug("Using list of ntp services from environment variable $SYSTEMD_TIMEDATED_NTP_SERVICES.");
134135

135136
for (p = env;;) {
136137
_cleanup_free_ char *word = NULL;
@@ -150,7 +151,62 @@ static int context_parse_ntp_services(Context *c) {
150151
log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", word);
151152
}
152153

153-
return 0;
154+
return 1;
155+
}
156+
157+
static int context_parse_ntp_services_from_disk(Context *c) {
158+
_cleanup_strv_free_ char **files = NULL;
159+
char **f;
160+
int r;
161+
162+
r = conf_files_list_strv(&files, ".list", NULL, CONF_FILES_FILTER_MASKED, UNIT_LIST_DIRS);
163+
if (r < 0)
164+
return log_error_errno(r, "Failed to enumerate .list files: %m");
165+
166+
STRV_FOREACH(f, files) {
167+
_cleanup_fclose_ FILE *file = NULL;
168+
169+
log_debug("Reading file '%s'", *f);
170+
171+
r = fopen_unlocked(*f, "re", &file);
172+
if (r < 0) {
173+
log_error_errno(r, "Failed to open %s, ignoring: %m", *f);
174+
continue;
175+
}
176+
177+
for (;;) {
178+
_cleanup_free_ char *line = NULL;
179+
const char *word;
180+
181+
r = read_line(file, LINE_MAX, &line);
182+
if (r < 0) {
183+
log_error_errno(r, "Failed to read %s, ignoring: %m", *f);
184+
continue;
185+
}
186+
if (r == 0)
187+
break;
188+
189+
word = strstrip(line);
190+
if (isempty(word) || startswith("#", word))
191+
continue;
192+
193+
r = context_add_ntp_service(c, word);
194+
if (r < 0)
195+
log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", word);
196+
}
197+
}
198+
199+
return 1;
200+
}
201+
202+
static int context_parse_ntp_services(Context *c) {
203+
int r;
204+
205+
r = context_parse_ntp_services_from_environment(c);
206+
if (r != 0)
207+
return r;
208+
209+
return context_parse_ntp_services_from_disk(c);
154210
}
155211

156212
static int context_ntp_service_is_active(Context *c) {

0 commit comments

Comments
 (0)
X Tutup