From 1b82604f615f5ac35992697c0bea8ba7092172d1 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 10 Jun 2020 13:13:11 +0200 Subject: [PATCH] main: add option to print configuration Add -p option to chronyd to print lines from the configuration as they are parsed and exit. It can be used to verify the syntax and get the whole configuration when it is split into multiple files. --- conf.c | 12 ++++++++++++ conf.h | 2 ++ doc/chronyd.adoc | 6 ++++++ main.c | 17 ++++++++++++++--- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/conf.c b/conf.c index c6a8140..2a4473d 100644 --- a/conf.c +++ b/conf.c @@ -85,6 +85,7 @@ static void parse_tempcomp(char *); /* ================================================== */ /* Configuration variables */ +static int print_config = 0; static int restarted = 0; static char *rtc_device; static int acquisition_port = -1; @@ -439,6 +440,14 @@ CNF_Finalise(void) /* ================================================== */ +void +CNF_EnablePrint(void) +{ + print_config = 1; +} + +/* ================================================== */ + /* Read the configuration file */ void CNF_ReadFile(const char *filename) @@ -489,6 +498,9 @@ CNF_ParseLine(const char *filename, int number, char *line) processed_command = command = line; p = CPS_SplitWord(line); + if (print_config && strcasecmp(command, "include") && strcasecmp(command, "confdirs")) + printf("%s%s%s\n", command, p[0] != '\0' ? " " : "", p); + if (!strcasecmp(command, "acquisitionport")) { parse_int(p, &acquisition_port); } else if (!strcasecmp(command, "allow")) { diff --git a/conf.h b/conf.h index 3d18282..0406d2d 100644 --- a/conf.h +++ b/conf.h @@ -35,6 +35,8 @@ extern void CNF_Initialise(int restarted, int client_only); extern void CNF_Finalise(void); +extern void CNF_EnablePrint(void); + extern char *CNF_GetRtcDevice(void); extern void CNF_ReadFile(const char *filename); diff --git a/doc/chronyd.adoc b/doc/chronyd.adoc index e280213..0e5f9fb 100644 --- a/doc/chronyd.adoc +++ b/doc/chronyd.adoc @@ -76,6 +76,12 @@ the log file, syslog, or terminal. The following levels can be specified: 0 (informational), 1 (warning), 2 (non-fatal error), and 3 (fatal error). The default value is 0. +*-p*:: +When run in this mode, *chronyd* will print the configuration and exit. It will +not detach from the terminal. This option can be used to verify the syntax of +the configuration and get the whole configuration, even if it is split into +multiple files and read by the *include* or *confdirs* directive. + *-q*:: When run in this mode, *chronyd* will set the system clock once and exit. It will not detach from the terminal. diff --git a/main.c b/main.c index 48a1948..26e1157 100644 --- a/main.c +++ b/main.c @@ -373,7 +373,7 @@ go_daemon(void) static void print_help(const char *progname) { - printf("Usage: %s [-4|-6] [-n|-d] [-q|-Q] [-r] [-R] [-s] [-t TIMEOUT] [-f FILE|COMMAND...]\n", + printf("Usage: %s [-4|-6] [-n|-d] [-p|-q|-Q] [-r] [-R] [-s] [-t TIMEOUT] [-f FILE|COMMAND...]\n", progname); } @@ -410,7 +410,7 @@ int main int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = -1; int scfilter_level = 0, lock_memory = 0, sched_priority = 0; int clock_control = 1, system_log = 1, log_severity = LOGS_INFO; - int config_args = 0; + int config_args = 0, print_config = 0; do_platform_checks(); @@ -430,7 +430,7 @@ int main optind = 1; /* Parse short command-line options */ - while ((opt = getopt(argc, argv, "46df:F:hl:L:mnP:qQrRst:u:vx")) != -1) { + while ((opt = getopt(argc, argv, "46df:F:hl:L:mnpP:qQrRst:u:vx")) != -1) { switch (opt) { case '4': case '6': @@ -459,6 +459,12 @@ int main case 'n': nofork = 1; break; + case 'p': + print_config = 1; + client_only = 1; + nofork = 1; + system_log = 0; + break; case 'P': sched_priority = parse_int_arg(optarg); break; @@ -523,6 +529,8 @@ int main DNS_SetAddressFamily(address_family); CNF_Initialise(restarted, client_only); + if (print_config) + CNF_EnablePrint(); /* Parse the config file or the remaining command line arguments */ config_args = argc - optind; @@ -533,6 +541,9 @@ int main CNF_ParseLine(NULL, config_args + optind - argc + 1, argv[optind]); } + if (print_config) + return 0; + /* Check whether another chronyd may already be running */ check_pidfile();