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.
This commit is contained in:
Miroslav Lichvar 2020-06-10 13:13:11 +02:00
parent d69ac07183
commit 1b82604f61
4 changed files with 34 additions and 3 deletions

12
conf.c
View file

@ -85,6 +85,7 @@ static void parse_tempcomp(char *);
/* ================================================== */ /* ================================================== */
/* Configuration variables */ /* Configuration variables */
static int print_config = 0;
static int restarted = 0; static int restarted = 0;
static char *rtc_device; static char *rtc_device;
static int acquisition_port = -1; static int acquisition_port = -1;
@ -439,6 +440,14 @@ CNF_Finalise(void)
/* ================================================== */ /* ================================================== */
void
CNF_EnablePrint(void)
{
print_config = 1;
}
/* ================================================== */
/* Read the configuration file */ /* Read the configuration file */
void void
CNF_ReadFile(const char *filename) CNF_ReadFile(const char *filename)
@ -489,6 +498,9 @@ CNF_ParseLine(const char *filename, int number, char *line)
processed_command = command = line; processed_command = command = line;
p = CPS_SplitWord(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")) { if (!strcasecmp(command, "acquisitionport")) {
parse_int(p, &acquisition_port); parse_int(p, &acquisition_port);
} else if (!strcasecmp(command, "allow")) { } else if (!strcasecmp(command, "allow")) {

2
conf.h
View file

@ -35,6 +35,8 @@
extern void CNF_Initialise(int restarted, int client_only); extern void CNF_Initialise(int restarted, int client_only);
extern void CNF_Finalise(void); extern void CNF_Finalise(void);
extern void CNF_EnablePrint(void);
extern char *CNF_GetRtcDevice(void); extern char *CNF_GetRtcDevice(void);
extern void CNF_ReadFile(const char *filename); extern void CNF_ReadFile(const char *filename);

View 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 0 (informational), 1 (warning), 2 (non-fatal error), and 3 (fatal error). The
default value is 0. 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*:: *-q*::
When run in this mode, *chronyd* will set the system clock once and exit. It When run in this mode, *chronyd* will set the system clock once and exit. It
will not detach from the terminal. will not detach from the terminal.

17
main.c
View file

@ -373,7 +373,7 @@ go_daemon(void)
static void static void
print_help(const char *progname) 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); progname);
} }
@ -410,7 +410,7 @@ int main
int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = -1; int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = -1;
int scfilter_level = 0, lock_memory = 0, sched_priority = 0; int scfilter_level = 0, lock_memory = 0, sched_priority = 0;
int clock_control = 1, system_log = 1, log_severity = LOGS_INFO; 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(); do_platform_checks();
@ -430,7 +430,7 @@ int main
optind = 1; optind = 1;
/* Parse short command-line options */ /* 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) { switch (opt) {
case '4': case '4':
case '6': case '6':
@ -459,6 +459,12 @@ int main
case 'n': case 'n':
nofork = 1; nofork = 1;
break; break;
case 'p':
print_config = 1;
client_only = 1;
nofork = 1;
system_log = 0;
break;
case 'P': case 'P':
sched_priority = parse_int_arg(optarg); sched_priority = parse_int_arg(optarg);
break; break;
@ -523,6 +529,8 @@ int main
DNS_SetAddressFamily(address_family); DNS_SetAddressFamily(address_family);
CNF_Initialise(restarted, client_only); CNF_Initialise(restarted, client_only);
if (print_config)
CNF_EnablePrint();
/* Parse the config file or the remaining command line arguments */ /* Parse the config file or the remaining command line arguments */
config_args = argc - optind; config_args = argc - optind;
@ -533,6 +541,9 @@ int main
CNF_ParseLine(NULL, config_args + optind - argc + 1, argv[optind]); CNF_ParseLine(NULL, config_args + optind - argc + 1, argv[optind]);
} }
if (print_config)
return 0;
/* Check whether another chronyd may already be running */ /* Check whether another chronyd may already be running */
check_pidfile(); check_pidfile();