Debian Codegraph @

src/timesync/timesyncd-conf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "conf-parser.h"
5 #include "dns-domain.h"
6 #include "extract-word.h"
7 #include "log.h"
8 #include "string-util.h"
9 #include "timesyncd-conf.h"
10 #include "timesyncd-manager.h"
11 #include "timesyncd-server.h"
12
13 int manager_parse_server_string(Manager *m, ServerType type, const char *string) {
14 ServerName *first;
15 int r;
16
17 assert(m);
18 assert(string);
19
20 first = type == SERVER_FALLBACK ? m->fallback_servers : m->system_servers;
21
22 if (type == SERVER_FALLBACK)
23 m->fallback_set = true;
24
25 for (;;) {
26 _cleanup_free_ char *word = NULL;
27 bool found = false;
28
29 r = extract_first_word(&string, &word, NULL, 0);
30 if (r < 0)
31 return log_error_errno(r, "Failed to parse timesyncd server syntax \"%s\": %m", string);
32 if (r == 0)
33 break;
34
35 r = dns_name_is_valid_or_address(word);
36 if (r < 0)
37 return log_error_errno(r, "Failed to check validity of NTP server name or address '%s': %m", word);
38 if (r == 0) {
39 log_error("Invalid NTP server name or address, ignoring: %s", word);
40 continue;
41 }
42
43 /* Filter out duplicates */
44 LIST_FOREACH(names, n, first)
45 if (streq_ptr(n->string, word)) {
46 found = true;
47 break;
48 }
49
50 if (found)
51 continue;
52
53 r = server_name_new(m, NULL, type, word);
54 if (r < 0)
55 return r;
56 }
57
58 return 0;
59 }
60
61 int manager_parse_fallback_string(Manager *m, const char *string) {
62 if (m->fallback_set)
63 return 0;
64
65 return manager_parse_server_string(m, SERVER_FALLBACK, string);
66 }
67
68 int config_parse_servers(
69 const char *unit,
70 const char *filename,
71 unsigned line,
72 const char *section,
73 unsigned section_line,
74 const char *lvalue,
75 int ltype,
76 const char *rvalue,
77 void *data,
78 void *userdata) {
79
80 Manager *m = userdata;
81 int r;
82
83 assert(filename);
84 assert(lvalue);
85 assert(rvalue);
86
87 if (isempty(rvalue)) {
88 manager_flush_server_names(m, ltype);
89
90 /* FallbackNTP= with an empty string disables the built-in fallback servers. */
91 if (ltype == SERVER_FALLBACK)
92 m->fallback_set = true;
93
94 return 0;
95 }
96
97 r = manager_parse_server_string(m, ltype, rvalue);
98 if (r < 0) {
99 log_syntax(unit, LOG_WARNING, filename, line, r,
100 "Failed to parse NTP server string '%s', ignoring: %m", rvalue);
101 return 0;
102 }
103
104 return 0;
105 }
106
107 int manager_parse_config_file(Manager *m) {
108 int r;
109
110 assert(m);
111
112 r = config_parse_standard_file_with_dropins(
113 "systemd/timesyncd.conf",
114 "Time\0",
115 config_item_perf_lookup, timesyncd_gperf_lookup,
116 CONFIG_PARSE_WARN,
117 /* userdata= */ m);
118 if (r < 0)
119 return r;
120
121 if (m->poll_interval_min_usec < 16 * USEC_PER_SEC) {
122 log_warning("Invalid PollIntervalMinSec=. Using default value.");
123 m->poll_interval_min_usec = NTP_POLL_INTERVAL_MIN_USEC;
124 }
125
126 if (m->poll_interval_max_usec < m->poll_interval_min_usec) {
127 log_warning("PollIntervalMaxSec= is smaller than PollIntervalMinSec=. Using default value.");
128 m->poll_interval_max_usec = MAX(NTP_POLL_INTERVAL_MAX_USEC, m->poll_interval_min_usec * 32);
129 }
130
131 if (m->connection_retry_usec < 1 * USEC_PER_SEC) {
132 log_warning("Invalid ConnectionRetrySec=. Using default value.");
133 m->connection_retry_usec = DEFAULT_CONNECTION_RETRY_USEC;
134 }
135
136 return r;
137 }