]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Clean up system options code
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 25 Nov 2009 13:22:11 +0000 (14:22 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 25 Nov 2009 13:37:41 +0000 (14:37 +0100)
Abort with error message when trying to use unsupported/disabled
system specific option.

conf.c
conf.h
logging.h
main.c
sys.c
sys.h
sys_linux.c

diff --git a/conf.c b/conf.c
index d7e42dbd6800f6a447da2083dc2216f711b378b8..2d8a2c9aa4d05a0d70826763b9dc68041f0597b4 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -97,14 +97,8 @@ static void parse_pidfile(const char *);
 static void parse_broadcast(const char *);
 static void parse_linux_hz(const char *);
 static void parse_linux_freq_scale(const char *);
-
-#if defined(HAVE_SCHED_SETSCHEDULER)
 static void parse_sched_priority(const char *);
-#endif
-
-#if defined(HAVE_MLOCKALL)
 static void parse_lockall(const char *);
-#endif
 
 /* ================================================== */
 /* Configuration variables */
@@ -184,6 +178,9 @@ static int linux_hz;
 static int set_linux_freq_scale = 0;
 static double linux_freq_scale;
 
+static int sched_priority = 0;
+static int lock_memory = 0;
+
 /* ================================================== */
 
 typedef struct {
@@ -226,15 +223,9 @@ static const Command commands[] = {
   {"pidfile", 7, parse_pidfile},
   {"broadcast", 9, parse_broadcast},
   {"linux_hz", 8, parse_linux_hz},
-  {"linux_freq_scale", 16, parse_linux_freq_scale}
-#if defined(HAVE_SCHED_SETSCHEDULER)
-  ,{"sched_priority", 14, parse_sched_priority}
-#endif
-
-#if defined(HAVE_MLOCKALL)
-  ,{"lock_all", 8, parse_lockall}
-#endif
-
+  {"linux_freq_scale", 16, parse_linux_freq_scale},
+  {"sched_priority", 14, parse_sched_priority},
+  {"lock_all", 8, parse_lockall}
 };
 
 static int n_commands = (sizeof(commands) / sizeof(commands[0]));
@@ -396,23 +387,21 @@ parse_source(const char *line, NTP_Source_Type type)
 
 /* ================================================== */
 
-#if defined(HAVE_SCHED_SETSCHEDULER)
 static void
 parse_sched_priority(const char *line)
 {
-  if (SchedPriority == 0) { /* Command-line switch must have priority */
-    sscanf(line, "%d", &SchedPriority);
+  if (sscanf(line, "%d", &sched_priority) != 1) {
+    LOG(LOGS_WARN, LOGF_Configure, "Could not read scheduling priority at line %d", line_number);
   }
 }
-#endif
 
-#if defined(HAVE_MLOCKALL)
+/* ================================================== */
+
 static void
 parse_lockall(const char *line)
 {
-  LockAll = 1;
+  lock_memory = 1;
 }
-#endif
 
 /* ================================================== */
 
@@ -1431,3 +1420,18 @@ CNF_GetLinuxFreqScale(int *set, double *freq_scale)
   *freq_scale = linux_freq_scale ;
 }
 
+/* ================================================== */
+
+int
+CNF_GetSchedPriority(void)
+{
+  return sched_priority;
+}
+
+/* ================================================== */
+
+int
+CNF_GetLockMemory(void)
+{
+  return lock_memory;
+}
diff --git a/conf.h b/conf.h
index 9c90bb8be5a90598e1ca842fc3e44947dcf01083..845502fdf486daa02e4b51d380274ded78e695f7 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -75,12 +75,7 @@ extern int CNF_AllowLocalReference(int *stratum);
 
 extern void CNF_SetupAccessRestrictions(void);
 
-#if defined(HAVE_SCHED_SETSCHEDULER)
-extern int SchedPriority;
-#endif
-
-#if defined(HAVE_MLOCKALL)
-extern int LockAll;
-#endif
+extern int CNF_GetSchedPriority(void);
+extern int CNF_GetLockMemory(void);
 
 #endif /* GOT_CONF_H */
index 81505e129f290f95e97285e5ab868861414ea7e9..92fd397fa802c17155043805057fd58df23e0a05 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -61,6 +61,7 @@ typedef enum {
   LOGF_Logging,
   LOGF_Rtc,
   LOGF_Regress,
+  LOGF_Sys,
   LOGF_SysLinux,
   LOGF_SysSolaris,
   LOGF_SysSunOS,
diff --git a/main.c b/main.c
index 27bf2c3de90819d8ad905bc7a9bb577b74abfe73..e6b433d4cd8a5b8ba82f2b5516636255d03bcee5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -216,10 +216,7 @@ int main
   int debug = 0;
   int do_init_rtc = 0;
   int other_pid;
-
-#if defined(HAVE_SCHED_SETSCHEDULER)
-  int return_value = 0;
-#endif
+  int lock_memory = 0, sched_priority = 0;
 
   LOG_Initialise();
 
@@ -229,29 +226,22 @@ int main
     if (!strcmp("-f", *argv)) {
       ++argv, --argc;
       conf_file = *argv;
-
-#if defined(HAVE_SCHED_SETSCHEDULER)
-      /* Get real-time scheduler priority */
     } else if (!strcmp("-P", *argv)) {
       ++argv, --argc;
-      return_value = sscanf(*argv, "%d", &SchedPriority);
-      if (return_value != 1 || SchedPriority < 1 || SchedPriority > 99) { 
-       SchedPriority = 0;
-       LOG(LOGS_WARN, LOGF_Main, "Bad scheduler priority: [%s]", *argv);
+      if (argc == 0 || sscanf(*argv, "%d", &sched_priority) != 1) {
+        LOG_FATAL(LOGF_Main, "Bad scheduler priority");
       }
-#endif /* HAVE_SCHED_SETCHEDULER */
-
-#if defined(HAVE_MLOCKALL)
-      /* Detect lockall switch */
     } else if (!strcmp("-m", *argv)) {
-      LockAll = 1;
-#endif /* HAVE_MLOCKALL */
-
+      lock_memory = 1;
     } else if (!strcmp("-r", *argv)) {
       reload = 1;
     } else if (!strcmp("-u", *argv)) {
       ++argv, --argc;
-      user = *argv;
+      if (argc == 0) {
+        LOG_FATAL(LOGF_Main, "Missing user name");
+      } else {
+        user = *argv;
+      }
     } else if (!strcmp("-s", *argv)) {
       do_init_rtc = 1;
     } else if (!strcmp("-v", *argv) || !strcmp("--version",*argv)) {
@@ -310,16 +300,21 @@ int main
   RTC_Initialise();
   RCL_Initialise();
 
-  if (SchedPriority > 0) {
-    SYS_SetScheduler(SchedPriority);
+  /* Command-line switch must have priority */
+  if (!sched_priority) {
+    sched_priority = CNF_GetSchedPriority();
+  }
+  if (sched_priority) {
+    SYS_SetScheduler(sched_priority);
   }
 
-  if (LockAll == 1 ) {
-    SYS_MemLockAll(LockAll);
+  if (lock_memory || CNF_GetLockMemory()) {
+    SYS_LockMemory();
   }
 
-  if (user)
+  if (user) {
     SYS_DropRoot(user);
+  }
 
   REF_Initialise();
   SST_Initialise();
diff --git a/sys.c b/sys.c
index 1596c6352cdac343a21a3055ffba14ee2e766301..c119c8c8d06dfe472e0f51bb1992d931ec43b2ce 100644 (file)
--- a/sys.c
+++ b/sys.c
@@ -30,6 +30,7 @@
   */
 
 #include "sys.h"
+#include "logging.h"
 
 #if defined (LINUX)
 #include "sys_linux.h"
@@ -102,7 +103,11 @@ void SYS_DropRoot(char *user)
 {
 #if defined(LINUX) && defined (FEAT_LINUXCAPS)
   SYS_Linux_DropRoot(user);
+#else
+  LOG_FATAL(LOGF_Sys, "dropping root privileges not supported");
 #endif
+
+  return;
 }
 
 /* ================================================== */
@@ -111,19 +116,24 @@ void SYS_SetScheduler(int SchedPriority)
 {
 #if defined(LINUX) && defined(HAVE_SCHED_SETSCHEDULER)
   SYS_Linux_SetScheduler(SchedPriority);
+#else
+  LOG_FATAL(LOGF_Sys, "scheduler priority setting not supported");
 #endif
-  ;;
+
+  return;
 }
 
-void SYS_MemLockAll(int LockAll)
+/* ================================================== */
+
+void SYS_LockMemory(void)
 {
 #if defined(LINUX) && defined(HAVE_MLOCKALL)
-  SYS_Linux_MemLockAll(LockAll);
+  SYS_Linux_MemLockAll(1);
+#else
+  LOG_FATAL(LOGF_Sys, "memory locking not supported");
 #endif
-  ;;
+
+  return;
 }
 
 /* ================================================== */
-
-
-
diff --git a/sys.h b/sys.h
index 8379b8d8277ffa599f9fe7eb8b2b34748006d7a3..139e85376c7896ff2e44d8cbf87affdea9046a7d 100644 (file)
--- a/sys.h
+++ b/sys.h
@@ -43,6 +43,6 @@ extern void SYS_Finalise(void);
 extern void SYS_DropRoot(char *user);
 
 extern void SYS_SetScheduler(int SchedPriority);
-extern void SYS_MemLockAll(int LockAll);
+extern void SYS_LockMemory(void);
 
 #endif /* GOT_SYS_H */
index 4c68a82768827c65434b628f51fcd3583c332af1..4821121e1fc02515aa20d9cac7b135b0e1b24505 100644 (file)
@@ -963,7 +963,9 @@ void SYS_Linux_SetScheduler(int SchedPriority)
   int pmax, pmin;
   struct sched_param sched;
 
-  if (SchedPriority > 0) {
+  if (SchedPriority < 1 || SchedPriority > 99) {
+    LOG_FATAL(LOGF_SysLinux, "Bad scheduler priority: %d", SchedPriority);
+  } else {
     sched.sched_priority = SchedPriority;
     pmax = sched_get_priority_max(SCHED_FIFO);
     pmin = sched_get_priority_min(SCHED_FIFO);