]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
sys_linux: use pthread_setschedparam instead of sched_setscheduler
authorStefan R. Filipek <srfilipek@gmail.com>
Thu, 4 Apr 2019 23:12:39 +0000 (19:12 -0400)
committerMiroslav Lichvar <mlichvar@redhat.com>
Mon, 8 Apr 2019 14:38:38 +0000 (16:38 +0200)
Fix an issue with Linux and musl libc where sched_setscheduler is not
implemented. It seems that pthread_setschedparam is more widely
supported across different C libraries and OSs. For our use case, it
should make no difference which call is used.

configure
sys.c
sys_linux.c

index 486b0bcd08744021a5dc89587b84fd36a15e2495..c434127e3915b53f6d7eb7306b05dc20485c4113 100755 (executable)
--- a/configure
+++ b/configure
@@ -227,6 +227,7 @@ feat_timestamping=1
 try_timestamping=0
 feat_ntp_signd=0
 ntp_era_split=""
+use_pthread=0
 default_user="root"
 default_hwclockfile=""
 default_pidfile="/var/run/chrony/chronyd.pid"
@@ -652,7 +653,7 @@ then
   add_def FEAT_ASYNCDNS
   add_def USE_PTHREAD_ASYNCDNS
   EXTRA_OBJECTS="$EXTRA_OBJECTS nameserv_async.o"
-  MYCFLAGS="$MYCFLAGS -pthread"
+  use_pthread=1
 fi
 
 if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then
@@ -786,13 +787,14 @@ fi
 
 if [ $try_setsched = "1" ] && \
   test_code \
-    'sched_setscheduler()' \
-    'sched.h' '' '' '
+    'pthread_setschedparam()' \
+    'pthread.h sched.h' '-pthread' '' '
      struct sched_param sched;
      sched_get_priority_max(SCHED_FIFO);
-     sched_setscheduler(0, SCHED_FIFO, &sched);'
+     pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);'
 then
-  add_def HAVE_SCHED_SETSCHEDULER
+  add_def HAVE_PTHREAD_SETSCHEDPARAM
+  use_pthread=1
 fi
 
 if [ $try_lockmem = "1" ] && \
@@ -896,6 +898,10 @@ if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_tomcrypt = "1" ]
   fi
 fi
 
+if [ $use_pthread = "1" ]; then
+  MYCFLAGS="$MYCFLAGS -pthread"
+fi
+
 SYSCONFDIR=/etc
 if [ "x$SETSYSCONFDIR" != "x" ]; then
   SYSCONFDIR=$SETSYSCONFDIR
diff --git a/sys.c b/sys.c
index 4d68b37147c4e4985e6177e2f22e70863f533eda..2c42db1d2fdd21ace409710430477157ca9d3873 100644 (file)
--- a/sys.c
+++ b/sys.c
@@ -124,7 +124,7 @@ void SYS_EnableSystemCallFilter(int level)
 
 void SYS_SetScheduler(int SchedPriority)
 {
-#if defined(LINUX) && defined(HAVE_SCHED_SETSCHEDULER)
+#if defined(LINUX) && defined(HAVE_PTHREAD_SETSCHEDPARAM)
   SYS_Linux_SetScheduler(SchedPriority);
 #elif defined(MACOSX)
   SYS_MacOSX_SetScheduler(SchedPriority);
index 6ae7c0d1438532dfaf2349e811d56e4d405e5136..9e4ab3f49273de2c11482f231c3706fff86f4f3a 100644 (file)
@@ -33,7 +33,8 @@
 
 #include <sys/utsname.h>
 
-#if defined(HAVE_SCHED_SETSCHEDULER)
+#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
+#  include <pthread.h>
 #  include <sched.h>
 #endif
 
@@ -632,7 +633,7 @@ add_failed:
 
 /* ================================================== */
 
-#if defined(HAVE_SCHED_SETSCHEDULER)
+#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
   /* Install SCHED_FIFO real-time scheduler with specified priority */
 void SYS_Linux_SetScheduler(int SchedPriority)
 {
@@ -651,8 +652,8 @@ void SYS_Linux_SetScheduler(int SchedPriority)
     else if ( SchedPriority < pmin ) {
       sched.sched_priority = pmin;
     }
-    if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 ) {
-      LOG(LOGS_ERR, "sched_setscheduler() failed");
+    if ( pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched) == -1 ) {
+      LOG(LOGS_ERR, "pthread_setschedparam() failed");
     }
     else {
       DEBUG_LOG("Enabled SCHED_FIFO with priority %d",
@@ -660,7 +661,7 @@ void SYS_Linux_SetScheduler(int SchedPriority)
     }
   }
 }
-#endif /* HAVE_SCHED_SETSCHEDULER */
+#endif /* HAVE_PTHREAD_SETSCHEDPARAM  */
 
 #if defined(HAVE_MLOCKALL)
 /* Lock the process into RAM so that it will never be swapped out */