[chrony-dev] [PATCH v4 2/3] sys: rework initialisation to return a value

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


This ensures any failure to initialize the clock driver
will eventually end in a unified clear statement that it failed to do
so instead of a random mix or EPerm or other issues that happened on
init.

The potential root causes (e.g. said Eperm) is still reported as warning.

Furthermore this allow further patches to plug-in a generic fallback
mechanism at the level os SYS_Initialise.

Messages will now look like:
 CAP_SYS_TIME not present
 adjtimex(0x8001) failed : Operation not permitted
 Could not initialise system clock driver

Signed-off-by: Christian Ehrhardt <christian.ehrhardt@xxxxxxxxxxxxx>
---
 sys.c         | 15 +++++++++++----
 sys_linux.c   | 19 ++++++++++---------
 sys_linux.h   |  2 +-
 sys_macosx.c  |  8 ++++----
 sys_macosx.h  |  2 +-
 sys_netbsd.c  |  8 ++++----
 sys_netbsd.h  |  2 +-
 sys_solaris.c |  6 +++---
 sys_solaris.h |  2 +-
 sys_timex.c   | 39 +++++++++++++++++++++++++++------------
 sys_timex.h   | 16 ++++++++--------
 11 files changed, 71 insertions(+), 48 deletions(-)

diff --git a/sys.c b/sys.c
index 4d68b37..391b259 100644
--- a/sys.c
+++ b/sys.c
@@ -52,22 +52,29 @@ static int null_driver;
 void
 SYS_Initialise(int clock_control)
 {
+  int initalised = 0;
+
   null_driver = !clock_control;
   if (null_driver) {
     SYS_Null_Initialise();
     return;
   }
 #if defined(LINUX)
-  SYS_Linux_Initialise();
+  initalised = SYS_Linux_Initialise();
 #elif defined(SOLARIS)
-  SYS_Solaris_Initialise();
+  initalised = SYS_Solaris_Initialise();
 #elif defined(NETBSD) || defined(FREEBSD)
-  SYS_NetBSD_Initialise();
+  initalised = SYS_NetBSD_Initialise();
 #elif defined(MACOSX)
-  SYS_MacOSX_Initialise();
+  initalised = SYS_MacOSX_Initialise();
 #else
 #error Unknown system
 #endif
+
+  if (!initalised) {
+    if (clock_control > 0)
+      LOG_FATAL("Could not initialise system clock driver");
+  }
 }
 
 /* ================================================== */
diff --git a/sys_linux.c b/sys_linux.c
index 7848816..e297a66 100644
--- a/sys_linux.c
+++ b/sys_linux.c
@@ -340,7 +340,7 @@ get_version_specific_details(void)
 
 /* ================================================== */
 
-static void
+static int
 reset_adjtime_offset(void)
 {
   struct timex txc;
@@ -349,7 +349,7 @@ reset_adjtime_offset(void)
   txc.modes = ADJ_OFFSET_SINGLESHOT;
   txc.offset = 0;
 
-  SYS_Timex_Adjust(&txc, 0);
+  return !(SYS_Timex_Adjust(&txc, 2) < 0);
 }
 
 /* ================================================== */
@@ -392,7 +392,7 @@ static void SYS_Linux_ReportTimeAdjustBlockers(void)
 /* ================================================== */
 /* Initialisation code for this module */
 
-void
+int
 SYS_Linux_Initialise(void)
 {
   get_version_specific_details();
@@ -401,18 +401,19 @@ SYS_Linux_Initialise(void)
   SYS_Linux_ReportTimeAdjustBlockers();
 #endif
 
-  reset_adjtime_offset();
+  if (!reset_adjtime_offset())
+    return 0;
 
   if (have_setoffset && !test_step_offset()) {
     LOG(LOGS_INFO, "adjtimex() doesn't support ADJ_SETOFFSET");
     have_setoffset = 0;
   }
 
-  SYS_Timex_InitialiseWithFunctions(1.0e6 * max_tick_bias / nominal_tick,
-                                    1.0 / tick_update_hz,
-                                    read_frequency, set_frequency,
-                                    have_setoffset ? apply_step_offset : NULL,
-                                    0.0, 0.0, NULL, NULL);
+  return SYS_Timex_InitialiseWithFunctions(1.0e6 * max_tick_bias / nominal_tick,
+                                           1.0 / tick_update_hz,
+                                           read_frequency, set_frequency,
+                                           have_setoffset ? apply_step_offset : NULL,
+                                           0.0, 0.0, NULL, NULL);
 }
 
 /* ================================================== */
diff --git a/sys_linux.h b/sys_linux.h
index 799ae9a..47af4bc 100644
--- a/sys_linux.h
+++ b/sys_linux.h
@@ -27,7 +27,7 @@
 #ifndef GOT_SYS_LINUX_H
 #define GOT_SYS_LINUX_H
 
-extern void SYS_Linux_Initialise(void);
+extern int SYS_Linux_Initialise(void);
 
 extern void SYS_Linux_Finalise(void);
 
diff --git a/sys_macosx.c b/sys_macosx.c
index 00ce302..27ea81a 100644
--- a/sys_macosx.c
+++ b/sys_macosx.c
@@ -479,7 +479,7 @@ test_adjtime()
 
 /* ================================================== */
 
-void
+int
 SYS_MacOSX_Initialise(void)
 {
 #ifdef HAVE_MACOS_SYS_TIMEX
@@ -488,14 +488,14 @@ SYS_MacOSX_Initialise(void)
     have_bad_adjtime = !test_adjtime();
     if (have_bad_adjtime) {
       LOG(LOGS_WARN, "adjtime() is buggy - using timex driver");
-      SYS_Timex_Initialise();
+      return SYS_Timex_Initialise();
     } else {
-      SYS_NetBSD_Initialise();
+      return SYS_NetBSD_Initialise();
     }
-    return;
   }
 #endif
   legacy_MacOSX_Initialise();
+  return 1;
 }
 
 /* ================================================== */
diff --git a/sys_macosx.h b/sys_macosx.h
index 5555616..748df8a 100644
--- a/sys_macosx.h
+++ b/sys_macosx.h
@@ -32,7 +32,7 @@
 
 void SYS_MacOSX_SetScheduler(int SchedPriority);
 void SYS_MacOSX_DropRoot(uid_t uid, gid_t gid);
-void SYS_MacOSX_Initialise(void);
+int SYS_MacOSX_Initialise(void);
 void SYS_MacOSX_Finalise(void);
 
 #endif
diff --git a/sys_netbsd.c b/sys_netbsd.c
index 840d6a5..35379e5 100644
--- a/sys_netbsd.c
+++ b/sys_netbsd.c
@@ -113,10 +113,10 @@ get_offset_correction(struct timespec *raw,
 void
 SYS_NetBSD_Initialise(void)
 {
-  SYS_Timex_InitialiseWithFunctions(MAX_FREQ, 1.0 / MIN_TICK_RATE,
-                                    NULL, NULL, NULL,
-                                    MIN_FASTSLEW_OFFSET, MAX_ADJTIME_SLEWRATE,
-                                    accrue_offset, get_offset_correction);
+  return SYS_Timex_InitialiseWithFunctions(MAX_FREQ, 1.0 / MIN_TICK_RATE,
+                                           NULL, NULL, NULL,
+                                           MIN_FASTSLEW_OFFSET, MAX_ADJTIME_SLEWRATE,
+                                           accrue_offset, get_offset_correction);
 }
 
 /* ================================================== */
diff --git a/sys_netbsd.h b/sys_netbsd.h
index 052f5b7..8b00cdf 100644
--- a/sys_netbsd.h
+++ b/sys_netbsd.h
@@ -28,7 +28,7 @@
 #ifndef GOT_SYS_NETBSD_H
 #define GOT_SYS_NETBSD_H
 
-void SYS_NetBSD_Initialise(void);
+int SYS_NetBSD_Initialise(void);
 
 void SYS_NetBSD_Finalise(void);
 
diff --git a/sys_solaris.c b/sys_solaris.c
index 21197b9..1047376 100644
--- a/sys_solaris.c
+++ b/sys_solaris.c
@@ -35,12 +35,12 @@
 
 /* ================================================== */
 
-void
+int
 SYS_Solaris_Initialise(void)
 {
   /* The kernel allows the frequency to be set in the full range off int32_t */
-  SYS_Timex_InitialiseWithFunctions(32500, 1.0 / 100, NULL, NULL, NULL,
-                                    0.0, 0.0, NULL, NULL);
+  return SYS_Timex_InitialiseWithFunctions(32500, 1.0 / 100, NULL, NULL, NULL,
+                                           0.0, 0.0, NULL, NULL);
 }
 
 /* ================================================== */
diff --git a/sys_solaris.h b/sys_solaris.h
index 46015ba..6d0532b 100644
--- a/sys_solaris.h
+++ b/sys_solaris.h
@@ -27,7 +27,7 @@
 #ifndef GOT_SYS_SOLARIS_H
 #define GOT_SYS_SOLARIS_H
 
-void SYS_Solaris_Initialise(void);
+int SYS_Solaris_Initialise(void);
 
 void SYS_Solaris_Finalise(void);
 
diff --git a/sys_timex.c b/sys_timex.c
index e54ad24..2754812 100644
--- a/sys_timex.c
+++ b/sys_timex.c
@@ -181,7 +181,7 @@ set_sync_status(int synchronised, double est_error, double max_error)
 
 /* ================================================== */
 
-static void
+static int
 initialise_timex(void)
 {
   struct timex txc;
@@ -193,26 +193,30 @@ initialise_timex(void)
   txc.modes = MOD_OFFSET | MOD_STATUS;
   txc.status = STA_PLL | sys_status;
   txc.offset = 0;
-  SYS_Timex_Adjust(&txc, 0);
+  if (SYS_Timex_Adjust(&txc, 2) < 0)
+    return 0;
 
   /* Turn PLL off */
   txc.modes = MOD_STATUS;
   txc.status = sys_status;
-  SYS_Timex_Adjust(&txc, 0);
+  if (SYS_Timex_Adjust(&txc, 2) < 0)
+    return 0;
+
+  return 1;
 }
 
 /* ================================================== */
 
-void
+int
 SYS_Timex_Initialise(void)
 {
-  SYS_Timex_InitialiseWithFunctions(MAX_FREQ, 1.0 / MIN_TICK_RATE, NULL, NULL, NULL,
-                                    0.0, 0.0, NULL, NULL);
+  return SYS_Timex_InitialiseWithFunctions(MAX_FREQ, 1.0 / MIN_TICK_RATE, NULL,
+                                           NULL, NULL, 0.0, 0.0, NULL, NULL);
 }
 
 /* ================================================== */
 
-void
+int
 SYS_Timex_InitialiseWithFunctions(double max_set_freq_ppm, double max_set_freq_delay,
                                   lcl_ReadFrequencyDriver sys_read_freq,
                                   lcl_SetFrequencyDriver sys_set_freq,
@@ -221,7 +225,8 @@ SYS_Timex_InitialiseWithFunctions(double max_set_freq_ppm, double max_set_freq_d
                                   lcl_AccrueOffsetDriver sys_accrue_offset,
                                   lcl_OffsetCorrectionDriver sys_get_offset_correction)
 {
-  initialise_timex();
+  if (!initialise_timex())
+    return 0;
 
   SYS_Generic_CompleteFreqDriver(max_set_freq_ppm, max_set_freq_delay,
                                  sys_read_freq ? sys_read_freq : read_frequency,
@@ -230,6 +235,7 @@ SYS_Timex_InitialiseWithFunctions(double max_set_freq_ppm, double max_set_freq_d
                                  min_fastslew_offset, max_fastslew_rate,
                                  sys_accrue_offset, sys_get_offset_correction,
                                  set_leap, set_sync_status);
+  return 1;
 }
 
 /* ================================================== */
@@ -256,10 +262,19 @@ SYS_Timex_Adjust(struct timex *txc, int ignore_error)
   state = NTP_ADJTIME(txc);
 
   if (state < 0) {
-    if (!ignore_error)
-      LOG_FATAL(NTP_ADJTIME_NAME"(0x%x) failed : %s", txc->modes, strerror(errno));
-    else
-      DEBUG_LOG(NTP_ADJTIME_NAME"(0x%x) failed : %s", txc->modes, strerror(errno));
+    switch (ignore_error) {
+      case 0:
+        LOG_FATAL(NTP_ADJTIME_NAME"(0x%x) failed : %s", txc->modes, strerror(errno));
+        break;
+      case 1:
+        DEBUG_LOG(NTP_ADJTIME_NAME"(0x%x) failed : %s", txc->modes, strerror(errno));
+        break;
+      case 2:
+        LOG(LOGS_WARN,NTP_ADJTIME_NAME"(0x%x) failed : %s", txc->modes, strerror(errno));
+        break;
+      default:
+        assert(0);
+    }
   }
 
   return state;
diff --git a/sys_timex.h b/sys_timex.h
index b8617a2..0921b43 100644
--- a/sys_timex.h
+++ b/sys_timex.h
@@ -29,16 +29,16 @@
 
 #include "localp.h"
 
-extern void SYS_Timex_Initialise(void);
+extern int SYS_Timex_Initialise(void);
 
 /* Initialise with some driver functions replaced with special versions */
-extern void SYS_Timex_InitialiseWithFunctions(double max_set_freq_ppm, double max_set_freq_delay,
-                                              lcl_ReadFrequencyDriver sys_read_freq,
-                                              lcl_SetFrequencyDriver sys_set_freq,
-                                              lcl_ApplyStepOffsetDriver sys_apply_step_offset,
-                                              double min_fastslew_offset, double max_fastslew_rate,
-                                              lcl_AccrueOffsetDriver sys_accrue_offset,
-                                              lcl_OffsetCorrectionDriver sys_get_offset_correction);
+extern int SYS_Timex_InitialiseWithFunctions(double max_set_freq_ppm, double max_set_freq_delay,
+                                             lcl_ReadFrequencyDriver sys_read_freq,
+                                             lcl_SetFrequencyDriver sys_set_freq,
+                                             lcl_ApplyStepOffsetDriver sys_apply_step_offset,
+                                             double min_fastslew_offset, double max_fastslew_rate,
+                                             lcl_AccrueOffsetDriver sys_accrue_offset,
+                                             lcl_OffsetCorrectionDriver sys_get_offset_correction);
 
 extern void SYS_Timex_Finalise(void);
 
-- 
2.7.4


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/