]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/timesync/timesyncd-conf.c
conf-parser: turn three bool function params into a flags fields
[thirdparty/systemd.git] / src / timesync / timesyncd-conf.c
CommitLineData
84e51726
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Kay Sievers, Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
b5efdb8a 20#include "alloc-util.h"
a0f29c76
LP
21#include "def.h"
22#include "extract-word.h"
07630cea 23#include "string-util.h"
a0f29c76 24#include "timesyncd-conf.h"
84e51726 25#include "timesyncd-manager.h"
874ff7bf 26#include "timesyncd-server.h"
84e51726 27
874ff7bf 28int manager_parse_server_string(Manager *m, ServerType type, const char *string) {
874ff7bf
LP
29 ServerName *first;
30 int r;
31
32 assert(m);
33 assert(string);
34
35 first = type == SERVER_FALLBACK ? m->fallback_servers : m->system_servers;
36
3745770a
MB
37 if (type == SERVER_FALLBACK)
38 m->have_fallbacks = true;
39
2e3c5854 40 for (;;) {
b5efdb8a 41 _cleanup_free_ char *word = NULL;
874ff7bf
LP
42 bool found = false;
43 ServerName *n;
44
2e3c5854
SS
45 r = extract_first_word(&string, &word, NULL, 0);
46 if (r < 0)
47 return log_error_errno(r, "Failed to parse timesyncd server syntax \"%s\": %m", string);
2e3c5854
SS
48 if (r == 0)
49 break;
b5efdb8a 50
874ff7bf
LP
51 /* Filter out duplicates */
52 LIST_FOREACH(names, n, first)
2e3c5854 53 if (streq_ptr(n->string, word)) {
874ff7bf
LP
54 found = true;
55 break;
56 }
57
58 if (found)
59 continue;
60
2e3c5854 61 r = server_name_new(m, NULL, type, word);
874ff7bf
LP
62 if (r < 0)
63 return r;
64 }
65
66 return 0;
67}
68
3745770a
MB
69int manager_parse_fallback_string(Manager *m, const char *string) {
70 if (m->have_fallbacks)
71 return 0;
72
73 return manager_parse_server_string(m, SERVER_FALLBACK, string);
74}
75
84e51726
LP
76int config_parse_servers(
77 const char *unit,
78 const char *filename,
79 unsigned line,
80 const char *section,
81 unsigned section_line,
82 const char *lvalue,
83 int ltype,
84 const char *rvalue,
85 void *data,
86 void *userdata) {
87
88 Manager *m = userdata;
874ff7bf 89 int r;
84e51726
LP
90
91 assert(filename);
92 assert(lvalue);
93 assert(rvalue);
94
95 if (isempty(rvalue))
874ff7bf
LP
96 manager_flush_server_names(m, ltype);
97 else {
98 r = manager_parse_server_string(m, ltype, rvalue);
99 if (r < 0) {
12ca818f 100 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse NTP server string '%s'. Ignoring.", rvalue);
874ff7bf
LP
101 return 0;
102 }
103 }
84e51726
LP
104
105 return 0;
106}
874ff7bf
LP
107
108int manager_parse_config_file(Manager *m) {
a4465d0d
YW
109 int r;
110
874ff7bf
LP
111 assert(m);
112
a4465d0d
YW
113 r = config_parse_many_nulstr(PKGSYSCONFDIR "/timesyncd.conf",
114 CONF_PATHS_NULSTR("systemd/timesyncd.conf.d"),
115 "Time\0",
116 config_item_perf_lookup, timesyncd_gperf_lookup,
bcde742e 117 CONFIG_PARSE_WARN, m);
a4465d0d
YW
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 return r;
874ff7bf 132}