conf: allocate sources dynamically

This removes the limits on maximum number of sources specified by the
initstepslew, server and refclock directives in the config file.
This commit is contained in:
Miroslav Lichvar
2014-09-24 14:59:20 +02:00
parent 9e71932c2e
commit 75fd327222
2 changed files with 64 additions and 62 deletions

View File

@@ -1722,7 +1722,7 @@ used instead. This makes it easier to restart @code{chronyd} whilst the
system is in normal operation. system is in normal operation.
The @code{initstepslew} directive takes a threshold and a list of NTP The @code{initstepslew} directive takes a threshold and a list of NTP
servers as arguments. A maximum of 8 will be used. Each of the servers servers as arguments. Each of the servers
is rapidly polled several times, and a majority voting mechanism used to is rapidly polled several times, and a majority voting mechanism used to
find the most likely range of system clock error that is present. A find the most likely range of system clock error that is present. A
step (or slew) is applied to the system clock to correct this error. step (or slew) is applied to the system clock to correct this error.

124
conf.c
View File

@@ -29,6 +29,7 @@
#include "sysincl.h" #include "sysincl.h"
#include "array.h"
#include "conf.h" #include "conf.h"
#include "ntp_sources.h" #include "ntp_sources.h"
#include "ntp_core.h" #include "ntp_core.h"
@@ -110,13 +111,11 @@ static char *dumpdir;
static int enable_local=0; static int enable_local=0;
static int local_stratum; static int local_stratum;
static int n_init_srcs;
/* Threshold (in seconds) - if absolute value of initial error is less /* Threshold (in seconds) - if absolute value of initial error is less
than this, slew instead of stepping */ than this, slew instead of stepping */
static double init_slew_threshold; static double init_slew_threshold;
#define MAX_INIT_SRCS 8 /* Array of IPAddr */
static IPAddr init_srcs_ip[MAX_INIT_SRCS]; static ARR_Instance init_sources;
static int enable_manual=0; static int enable_manual=0;
@@ -201,15 +200,11 @@ typedef struct {
CPS_NTP_Source params; CPS_NTP_Source params;
} NTP_Source; } NTP_Source;
#define MAX_NTP_SOURCES 64 /* Array of NTP_Source */
static ARR_Instance ntp_sources;
static NTP_Source ntp_sources[MAX_NTP_SOURCES]; /* Array of RefclockParameters */
static int n_ntp_sources = 0; static ARR_Instance refclock_sources;
#define MAX_RCL_SOURCES 8
static RefclockParameters refclock_sources[MAX_RCL_SOURCES];
static int n_refclock_sources = 0;
typedef struct _AllowDeny { typedef struct _AllowDeny {
struct _AllowDeny *next; struct _AllowDeny *next;
@@ -290,6 +285,10 @@ CNF_Initialise(int r)
{ {
restarted = r; restarted = r;
init_sources = ARR_CreateInstance(sizeof (IPAddr));
ntp_sources = ARR_CreateInstance(sizeof (NTP_Source));
refclock_sources = ARR_CreateInstance(sizeof (RefclockParameters));
dumpdir = Strdup("."); dumpdir = Strdup(".");
logdir = Strdup("."); logdir = Strdup(".");
pidfile = Strdup("/var/run/chronyd.pid"); pidfile = Strdup("/var/run/chronyd.pid");
@@ -304,8 +303,12 @@ CNF_Finalise(void)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < n_ntp_sources; i++) for (i = 0; i < ARR_GetSize(ntp_sources); i++)
Free(ntp_sources[i].params.name); Free(((NTP_Source *)ARR_GetElement(ntp_sources, i))->params.name);
ARR_DestroyInstance(init_sources);
ARR_DestroyInstance(ntp_sources);
ARR_DestroyInstance(refclock_sources);
Free(drift_file); Free(drift_file);
Free(dumpdir); Free(dumpdir);
@@ -551,17 +554,15 @@ static void
parse_source(char *line, NTP_Source_Type type) parse_source(char *line, NTP_Source_Type type)
{ {
CPS_Status status; CPS_Status status;
NTP_Source source;
if (n_ntp_sources >= MAX_NTP_SOURCES) source.type = type;
return; status = CPS_ParseNTPSourceAdd(line, &source.params);
ntp_sources[n_ntp_sources].type = type;
status = CPS_ParseNTPSourceAdd(line, &ntp_sources[n_ntp_sources].params);
switch (status) { switch (status) {
case CPS_Success: case CPS_Success:
ntp_sources[n_ntp_sources].params.name = Strdup(ntp_sources[n_ntp_sources].params.name); source.params.name = Strdup(source.params.name);
n_ntp_sources++; ARR_AppendElement(ntp_sources, &source);
break; break;
case CPS_BadOption: case CPS_BadOption:
other_parse_error("Invalid server/peer parameter"); other_parse_error("Invalid server/peer parameter");
@@ -623,16 +624,13 @@ parse_peer(char *line)
static void static void
parse_refclock(char *line) parse_refclock(char *line)
{ {
int i, n, poll, dpoll, filter_length, pps_rate; int n, poll, dpoll, filter_length, pps_rate;
uint32_t ref_id, lock_ref_id; uint32_t ref_id, lock_ref_id;
double offset, delay, precision, max_dispersion; double offset, delay, precision, max_dispersion;
char *p, *cmd, *name, *param; char *p, *cmd, *name, *param;
unsigned char ref[5]; unsigned char ref[5];
SRC_SelectOption sel_option; SRC_SelectOption sel_option;
RefclockParameters *refclock;
i = n_refclock_sources;
if (i >= MAX_RCL_SOURCES)
return;
poll = 4; poll = 4;
dpoll = 0; dpoll = 0;
@@ -720,21 +718,20 @@ parse_refclock(char *line)
return; return;
} }
refclock_sources[i].driver_name = name; refclock = (RefclockParameters *)ARR_GetNewElement(refclock_sources);
refclock_sources[i].driver_parameter = param; refclock->driver_name = name;
refclock_sources[i].driver_poll = dpoll; refclock->driver_parameter = param;
refclock_sources[i].poll = poll; refclock->driver_poll = dpoll;
refclock_sources[i].filter_length = filter_length; refclock->poll = poll;
refclock_sources[i].pps_rate = pps_rate; refclock->filter_length = filter_length;
refclock_sources[i].offset = offset; refclock->pps_rate = pps_rate;
refclock_sources[i].delay = delay; refclock->offset = offset;
refclock_sources[i].precision = precision; refclock->delay = delay;
refclock_sources[i].max_dispersion = max_dispersion; refclock->precision = precision;
refclock_sources[i].sel_option = sel_option; refclock->max_dispersion = max_dispersion;
refclock_sources[i].ref_id = ref_id; refclock->sel_option = sel_option;
refclock_sources[i].lock_ref_id = lock_ref_id; refclock->ref_id = ref_id;
refclock->lock_ref_id = lock_ref_id;
n_refclock_sources++;
} }
/* ================================================== */ /* ================================================== */
@@ -796,7 +793,7 @@ parse_initstepslew(char *line)
return; return;
} }
n_init_srcs = 0; ARR_SetSize(init_sources, 0);
p = CPS_SplitWord(line); p = CPS_SplitWord(line);
if (sscanf(line, "%lf", &init_slew_threshold) != 1) { if (sscanf(line, "%lf", &init_slew_threshold) != 1) {
@@ -809,15 +806,10 @@ parse_initstepslew(char *line)
p = CPS_SplitWord(p); p = CPS_SplitWord(p);
if (*hostname) { if (*hostname) {
if (DNS_Name2IPAddress(hostname, &ip_addr) == DNS_Success) { if (DNS_Name2IPAddress(hostname, &ip_addr) == DNS_Success) {
init_srcs_ip[n_init_srcs] = ip_addr; ARR_AppendElement(init_sources, &ip_addr);
++n_init_srcs;
} else { } else {
LOG(LOGS_WARN, LOGF_Configure, "Could not resolve address of initstepslew server %s", hostname); LOG(LOGS_WARN, LOGF_Configure, "Could not resolve address of initstepslew server %s", hostname);
} }
if (n_init_srcs >= MAX_INIT_SRCS) {
other_parse_error("Too many initstepslew servers");
}
} }
} }
} }
@@ -1192,43 +1184,53 @@ CNF_AddInitSources(void)
CPS_NTP_Source cps_source; CPS_NTP_Source cps_source;
NTP_Remote_Address ntp_addr; NTP_Remote_Address ntp_addr;
char dummy_hostname[2] = "H"; char dummy_hostname[2] = "H";
int i; unsigned int i;
for (i = 0; i < n_init_srcs; i++) { for (i = 0; i < ARR_GetSize(init_sources); i++) {
/* Get the default NTP params */ /* Get the default NTP params */
CPS_ParseNTPSourceAdd(dummy_hostname, &cps_source); CPS_ParseNTPSourceAdd(dummy_hostname, &cps_source);
/* Add the address as an offline iburst server */ /* Add the address as an offline iburst server */
ntp_addr.ip_addr = init_srcs_ip[i]; ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i);
ntp_addr.port = cps_source.port; ntp_addr.port = cps_source.port;
cps_source.params.iburst = 1; cps_source.params.iburst = 1;
cps_source.params.online = 0; cps_source.params.online = 0;
NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params); NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params);
} }
ARR_SetSize(init_sources, 0);
} }
/* ================================================== */ /* ================================================== */
void void
CNF_AddSources(void) { CNF_AddSources(void)
int i; {
NTP_Source *source;
unsigned int i;
for (i=0; i<n_ntp_sources; i++) { for (i = 0; i < ARR_GetSize(ntp_sources); i++) {
NSR_AddUnresolvedSource(ntp_sources[i].params.name, ntp_sources[i].params.port, source = (NTP_Source *)ARR_GetElement(ntp_sources, i);
ntp_sources[i].type, &ntp_sources[i].params.params); NSR_AddUnresolvedSource(source->params.name, source->params.port,
source->type, &source->params.params);
} }
ARR_SetSize(ntp_sources, 0);
} }
/* ================================================== */ /* ================================================== */
void void
CNF_AddRefclocks(void) { CNF_AddRefclocks(void)
int i; {
unsigned int i;
for (i=0; i<n_refclock_sources; i++) { for (i = 0; i < ARR_GetSize(refclock_sources); i++) {
RCL_AddRefclock(&refclock_sources[i]); RCL_AddRefclock((RefclockParameters *)ARR_GetElement(refclock_sources, i));
} }
ARR_SetSize(refclock_sources, 0);
} }
/* ================================================== */ /* ================================================== */
@@ -1709,7 +1711,7 @@ CNF_GetHwclockFile(void)
int int
CNF_GetInitSources(void) CNF_GetInitSources(void)
{ {
return n_init_srcs; return ARR_GetSize(init_sources);
} }
/* ================================================== */ /* ================================================== */