]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd-conf.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / timesync / timesyncd-conf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Kay Sievers, Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "string-util.h"
23 #include "timesyncd-manager.h"
24 #include "timesyncd-server.h"
25 #include "timesyncd-conf.h"
26
27 int manager_parse_server_string(Manager *m, ServerType type, const char *string) {
28 const char *word, *state;
29 size_t length;
30 ServerName *first;
31 int r;
32
33 assert(m);
34 assert(string);
35
36 first = type == SERVER_FALLBACK ? m->fallback_servers : m->system_servers;
37
38 FOREACH_WORD_QUOTED(word, length, string, state) {
39 char buffer[length+1];
40 bool found = false;
41 ServerName *n;
42
43 memcpy(buffer, word, length);
44 buffer[length] = 0;
45
46 /* Filter out duplicates */
47 LIST_FOREACH(names, n, first)
48 if (streq_ptr(n->string, buffer)) {
49 found = true;
50 break;
51 }
52
53 if (found)
54 continue;
55
56 r = server_name_new(m, NULL, type, buffer);
57 if (r < 0)
58 return r;
59 }
60
61 return 0;
62 }
63
64 int config_parse_servers(
65 const char *unit,
66 const char *filename,
67 unsigned line,
68 const char *section,
69 unsigned section_line,
70 const char *lvalue,
71 int ltype,
72 const char *rvalue,
73 void *data,
74 void *userdata) {
75
76 Manager *m = userdata;
77 int r;
78
79 assert(filename);
80 assert(lvalue);
81 assert(rvalue);
82
83 if (isempty(rvalue))
84 manager_flush_server_names(m, ltype);
85 else {
86 r = manager_parse_server_string(m, ltype, rvalue);
87 if (r < 0) {
88 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse NTP server string '%s'. Ignoring.", rvalue);
89 return 0;
90 }
91 }
92
93 return 0;
94 }
95
96 int manager_parse_config_file(Manager *m) {
97 assert(m);
98
99 return config_parse_many("/etc/systemd/timesyncd.conf",
100 CONF_DIRS_NULSTR("systemd/timesyncd.conf"),
101 "Time\0",
102 config_item_perf_lookup, timesyncd_gperf_lookup,
103 false, m);
104 }