]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
refclock: check all driver options
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 18 Apr 2019 13:56:15 +0000 (15:56 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 18 Apr 2019 14:27:47 +0000 (16:27 +0200)
In each driver provide a list of supported options and abort when an
unknown option is specified in the refclock directive.

refclock.c
refclock.h
refclock_phc.c
refclock_pps.c
refclock_shm.c
refclock_sock.c

index 8f234f604db62240e4b38d55da2d3aadd902be1d..42fee4cdf5044b2890566d0a867a36c0994cf649 100644 (file)
@@ -325,25 +325,57 @@ RCL_GetDriverParameter(RCL_Instance instance)
   return instance->driver_parameter;
 }
 
-char *
-RCL_GetDriverOption(RCL_Instance instance, char *name)
+static char *
+get_next_driver_option(RCL_Instance instance, char *option)
 {
-  char *s, *e;
-  int n;
+  if (option == NULL)
+    option = instance->driver_parameter;
 
-  s = instance->driver_parameter;
-  e = s + instance->driver_parameter_length;
-  n = strlen(name);
+  option += strlen(option) + 1;
 
-  while (1) {
-    s += strlen(s) + 1;
-    if (s >= e)
-      break;
-    if (!strncmp(name, s, n)) {
-      if (s[n] == '=')
-        return s + n + 1;
-      if (s[n] == '\0')
-        return s + n;
+  if (option >= instance->driver_parameter + instance->driver_parameter_length)
+    return NULL;
+
+  return option;
+}
+
+void
+RCL_CheckDriverOptions(RCL_Instance instance, const char **options)
+{
+  char *option;
+  int i, len;
+
+  for (option = get_next_driver_option(instance, NULL);
+       option;
+       option = get_next_driver_option(instance, option)) {
+    for (i = 0; options && options[i]; i++) {
+      len = strlen(options[i]);
+      if (!strncmp(options[i], option, strlen(options[i])) &&
+          (option[len] == '=' || option[len] == '\0'))
+        break;
+    }
+
+    if (!options || !options[i])
+      LOG_FATAL("Invalid refclock driver option %s", option);
+  }
+}
+
+char *
+RCL_GetDriverOption(RCL_Instance instance, char *name)
+{
+  char *option;
+  int len;
+
+  len = strlen(name);
+
+  for (option = get_next_driver_option(instance, NULL);
+       option;
+       option = get_next_driver_option(instance, option)) {
+    if (!strncmp(name, option, len)) {
+      if (option[len] == '=')
+        return option + len + 1;
+      if (option[len] == '\0')
+        return option + len;
     }
   }
 
index 724f6209f62dccb53672e191b130581f90308cef..69a015238f48eaacb93a5f35307c20415ef9acf2 100644 (file)
@@ -72,6 +72,7 @@ extern void RCL_ReportSource(RPT_SourceReport *report, struct timespec *now);
 extern void RCL_SetDriverData(RCL_Instance instance, void *data);
 extern void *RCL_GetDriverData(RCL_Instance instance);
 extern char *RCL_GetDriverParameter(RCL_Instance instance);
+extern void RCL_CheckDriverOptions(RCL_Instance instance, const char **options);
 extern char *RCL_GetDriverOption(RCL_Instance instance, char *name);
 extern int RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap);
 extern int RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second);
index 03450dbd35faa7e2e51eafbf2dce36b62b1a7b3b..a000fe430acbf8bbf40306d94dc40e3a1bb26f56 100644 (file)
@@ -56,10 +56,13 @@ static void read_ext_pulse(int sockfd, int event, void *anything);
 
 static int phc_initialise(RCL_Instance instance)
 {
+  const char *options[] = {"nocrossts", "extpps", "pin", "channel", "clear", NULL};
   struct phc_instance *phc;
   int phc_fd, rising_edge;
   char *path, *s;
 
+  RCL_CheckDriverOptions(instance, options);
+
   path = RCL_GetDriverParameter(instance);
  
   phc_fd = SYS_Linux_OpenPHC(path, 0);
index 85ff9e93660be2a0a1cb02f5e511a9593bbffd3d..b9e8009c7327555e93de8c8b0c1843f7e6a15050 100644 (file)
@@ -48,12 +48,15 @@ struct pps_instance {
 };
 
 static int pps_initialise(RCL_Instance instance) {
+  const char *options[] = {"clear", NULL};
   pps_handle_t handle;
   pps_params_t params;
   struct pps_instance *pps;
   int fd, edge_clear, mode;
   char *path;
 
+  RCL_CheckDriverOptions(instance, options);
+
   path = RCL_GetDriverParameter(instance);
   edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0;
 
index e8f62560b48e66e8dfa8a50898de573045ecefab..ed68095f64ba05f45bf54e064e974352d83b46c5 100644 (file)
@@ -59,10 +59,13 @@ struct shmTime {
 };
 
 static int shm_initialise(RCL_Instance instance) {
+  const char *options[] = {"perm", NULL};
   int id, param, perm;
   char *s;
   struct shmTime *shm;
 
+  RCL_CheckDriverOptions(instance, options);
+
   param = atoi(RCL_GetDriverParameter(instance));
   s = RCL_GetDriverOption(instance, "perm");
   perm = s ? strtol(s, NULL, 8) & 0777 : 0600;
index 176310ca179de74a8cd3f8fa8c03bb8da6643c7c..492ee32393699e76620280780423c3c4f744a8dd 100644 (file)
@@ -101,6 +101,8 @@ static int sock_initialise(RCL_Instance instance)
   int sockfd;
   char *path;
 
+  RCL_CheckDriverOptions(instance, NULL);
+
   path = RCL_GetDriverParameter(instance);
  
   s.sun_family = AF_UNIX;