]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
sources: add function to modify selection options
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 14 Dec 2022 14:15:41 +0000 (15:15 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 14 Dec 2022 16:04:49 +0000 (17:04 +0100)
Add a function to add new selection options or remove existing options
specified in the configuration for both NTP sources and reference
clocks.

Provide a pair of IP address and reference ID to identify the source
depending on the type. Find the source directly in the array of sources
instead of going through the NSR hashtable for NTP sources to not
complicate it unnecessarily.

sources.c
sources.h

index 9ab5d104097b908a0b9db6a1ff0a3727b6d45ed5..b768b7b65fc96b9061e647e73f66c518b0486b83 100644 (file)
--- a/sources.c
+++ b/sources.c
@@ -1598,6 +1598,46 @@ SRC_ActiveSources(void)
 
 /* ================================================== */
 
+static SRC_Instance
+find_source(IPAddr *ip, uint32_t ref_id)
+{
+  int i;
+
+  for (i = 0; i < n_sources; i++) {
+    if ((ip->family != IPADDR_UNSPEC && sources[i]->type == SRC_NTP &&
+         UTI_CompareIPs(ip, sources[i]->ip_addr, NULL) == 0) ||
+        (ip->family == IPADDR_UNSPEC && sources[i]->type == SRC_REFCLOCK &&
+         ref_id == sources[i]->ref_id))
+      return sources[i];
+  }
+
+  return NULL;
+}
+
+/* ================================================== */
+
+int
+SRC_ModifySelectOptions(IPAddr *ip, uint32_t ref_id, int options, int mask)
+{
+  SRC_Instance inst;
+
+  inst = find_source(ip, ref_id);
+  if (!inst)
+    return 0;
+
+  if ((inst->conf_sel_options & mask) == options)
+    return 1;
+
+  inst->conf_sel_options = (inst->conf_sel_options & ~mask) | options;
+  LOG(LOGS_INFO, "Source %s selection options modified", source_to_string(inst));
+
+  update_sel_options();
+
+  return 1;
+}
+
+/* ================================================== */
+
 int
 SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now)
 {
index 6c799bb4bcd72059700bdfbbaff6a08c7c2a4b8b..da3a5335570ebe9299bdcf52e3872cb94ea77149 100644 (file)
--- a/sources.h
+++ b/sources.h
@@ -131,6 +131,10 @@ extern int SRC_IsReachable(SRC_Instance inst);
 extern int SRC_ReadNumberOfSources(void);
 extern int SRC_ActiveSources(void);
 
+/* Modify selection options of an NTP source specified by address, or
+   refclock specified by its reference ID */
+extern int SRC_ModifySelectOptions(IPAddr *ip, uint32_t ref_id, int options, int mask);
+
 extern int SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now);
 extern int SRC_ReportSourcestats(int index, RPT_SourcestatsReport *report, struct timespec *now);
 extern int SRC_GetSelectReport(int index, RPT_SelectReport *report);