]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
manual: check offset sanity
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 7 Apr 2015 13:01:30 +0000 (15:01 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 7 Apr 2015 13:23:47 +0000 (15:23 +0200)
cmdmon.c
manual.c
manual.h

index 6a8737655a4d5ceffee38355d629f21b9bdde112..e36331798d47109dc00e41206f46a6f508ebf12f 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -926,14 +926,16 @@ handle_settime(CMD_Request *rx_message, CMD_Reply *tx_message)
   long offset_cs;
   double dfreq_ppm, new_afreq_ppm;
   UTI_TimevalNetworkToHost(&rx_message->data.settime.ts, &ts);
-  if (MNL_AcceptTimestamp(&ts, &offset_cs, &dfreq_ppm, &new_afreq_ppm)) {
+  if (!MNL_IsEnabled()) {
+    tx_message->status = htons(STT_NOTENABLED);
+  } else if (MNL_AcceptTimestamp(&ts, &offset_cs, &dfreq_ppm, &new_afreq_ppm)) {
     tx_message->status = htons(STT_SUCCESS);
     tx_message->reply = htons(RPY_MANUAL_TIMESTAMP);
     tx_message->data.manual_timestamp.centiseconds = htonl((int32_t)offset_cs);
     tx_message->data.manual_timestamp.dfreq_ppm = UTI_FloatHostToNetwork(dfreq_ppm);
     tx_message->data.manual_timestamp.new_afreq_ppm = UTI_FloatHostToNetwork(new_afreq_ppm);
   } else {
-    tx_message->status = htons(STT_NOTENABLED);
+    tx_message->status = htons(STT_FAILED);
   }
 }
 
index c286ce875e8c62de93b77493f1e0ad267ef6f657..da2ed9252327ed4ea9fe1dbfad4a683652730335 100644 (file)
--- a/manual.c
+++ b/manual.c
@@ -54,6 +54,8 @@ typedef struct {
                      (measured-predicted)) */
 } Sample;
 
+#define MIN_SAMPLE_SEPARATION 1.0
+
 #define MAX_SAMPLES 16
 
 static Sample samples[16];
@@ -174,14 +176,24 @@ int
 MNL_AcceptTimestamp(struct timeval *ts, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm)
 {
   struct timeval now;
-  double offset;
+  double offset, diff;
   int i;
 
   if (enabled) {
-
-    /* Check whether timestamp is within margin of old one */
     LCL_ReadCookedTime(&now, NULL);
 
+    /* Make sure the provided timestamp is sane and the sample
+       is not too close to the last one */
+
+    if (!UTI_IsTimeOffsetSane(ts, 0.0))
+     return 0;
+
+    if (n_samples) {
+      UTI_DiffTimevalsToDouble(&diff, &now, &samples[n_samples - 1].when);
+      if (diff < MIN_SAMPLE_SEPARATION)
+        return 0;
+    }
+
     UTI_DiffTimevalsToDouble(&offset, &now, ts);
 
     /* Check if buffer full up */
@@ -258,6 +270,14 @@ MNL_Reset(void)
   n_samples = 0;
 }
 
+/* ================================================== */
+
+int
+MNL_IsEnabled(void)
+{
+  return enabled;
+}
+
 /* ================================================== */
 /* Generate report data for the REQ_MANUAL_LIST command/monitoring
    protocol */
index bd49b98bd1d376429a6b135380994a0ed8f89618..bf0c72adffd95b192066a7e5e38badffc9d0c6f3 100644 (file)
--- a/manual.h
+++ b/manual.h
@@ -38,6 +38,7 @@ extern int MNL_AcceptTimestamp(struct timeval *ts, long *offset_cs, double *dfre
 extern void MNL_Enable(void);
 extern void MNL_Disable(void);
 extern void MNL_Reset(void);
+extern int MNL_IsEnabled(void);
 
 extern void MNL_ReportSamples(RPT_ManualSamplesReport *report, int max, int *n);
 extern int MNL_DeleteSample(int index);