timesyncd-conf.c revision 874ff7bf4d6fe693542209f127d23cd89adc499b
3802a3d3d7af51ddff31943d5514382f01265770Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen/***
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek This file is part of systemd.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek Copyright 2014 Kay Sievers, Lennart Poettering
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek (at your option) any later version.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek***/
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek#include "in-addr-util.h"
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
301af7e4853ad0281402f8d86f5a77c6cb7ce9f4Josh Triplett#include "timesyncd-manager.h"
301af7e4853ad0281402f8d86f5a77c6cb7ce9f4Josh Triplett#include "timesyncd-server.h"
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek#include "timesyncd-conf.h"
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekint manager_parse_server_string(Manager *m, ServerType type, const char *string) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek const char *word, *state;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek size_t length;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek ServerName *first;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek int r;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek assert(m);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek assert(string);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek first = type == SERVER_FALLBACK ? m->fallback_servers : m->system_servers;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(word, length, string, state) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek char buffer[length+1];
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek bool found = false;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek ServerName *n;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek memcpy(buffer, word, length);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek buffer[length] = 0;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
301af7e4853ad0281402f8d86f5a77c6cb7ce9f4Josh Triplett /* Filter out duplicates */
301af7e4853ad0281402f8d86f5a77c6cb7ce9f4Josh Triplett LIST_FOREACH(names, n, first)
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek if (streq_ptr(n->string, buffer)) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek found = true;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek break;
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen }
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen
301af7e4853ad0281402f8d86f5a77c6cb7ce9f4Josh Triplett if (found)
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen continue;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek r = server_name_new(m, NULL, type, buffer);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek if (r < 0)
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek return r;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek }
dd2b607b7d1ce355e93f9f71cd256ec20b8ae9c4Thomas Hindoe Paaboel Andersen
18ead1b00dff180a97507d014325cb46198e6657Chris Mayo return 0;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek}
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekint config_parse_servers(
e93549ef29c4123d9ee45acb5815048390201e49Zbigniew Jędrzejewski-Szmek const char *unit,
301af7e4853ad0281402f8d86f5a77c6cb7ce9f4Josh Triplett const char *filename,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek unsigned line,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek const char *section,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek unsigned section_line,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek const char *lvalue,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek int ltype,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek const char *rvalue,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek void *data,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek void *userdata) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek Manager *m = userdata;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek int r;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek assert(filename);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek assert(lvalue);
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardt assert(rvalue);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek if (isempty(rvalue))
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek manager_flush_server_names(m, ltype);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek else {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek r = manager_parse_server_string(m, ltype, rvalue);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek if (r < 0) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek log_syntax(unit, LOG_ERR, filename, line, -r, "Failed to parse NTP server string '%s'. Ignoring.", rvalue);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek return 0;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek }
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek }
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek return 0;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek}
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekint manager_parse_config_file(Manager *m) {
8c9571d0ae50656f730a5e37378d5c3dcf3b9789Lennart Poettering assert(m);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek
442f7f155358a7396a350d3c47ea42e86bee4024Chris Atkinson return config_parse(NULL, "/etc/systemd/timesyncd.conf", NULL,
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt "Time\0",
8c9571d0ae50656f730a5e37378d5c3dcf3b9789Lennart Poettering config_item_perf_lookup, timesyncd_gperf_lookup,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek false, false, true, m);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek}
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek