]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
main: add option to print configuration
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 10 Jun 2020 11:13:11 +0000 (13:13 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 10 Jun 2020 12:10:59 +0000 (14:10 +0200)
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
conf.h
doc/chronyd.adoc
main.c

diff --git a/conf.c b/conf.c
index c6a814013c361462dc17a4df5c4cff79245ed065..2a4473d557f7310bd59e99e122bdf21ea284db7d 100644 (file)
--- 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 3d18282f8f4b6a42b2b80a610c7b5ccc7b2ac926..0406d2db05c0a2c939f08c807e1a80b68081cd45 100644 (file)
--- 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);
index e2802134fac4c7aa42a8a65bb89a4ffbacd9a93f..0e5f9fbd002a37d30acf3ed3e8bd6f60f7bd966d 100644 (file)
@@ -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 48a19483ca8888a248e12d76197e20cabd20db70..26e115774974c21b1529ef559cc17a0b952d94fa 100644 (file)
--- 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();