From 69c6dffd63c58f329999a8d3420c5d2875940b5f Mon Sep 17 00:00:00 2001 From: "Stefan R. Filipek" Date: Thu, 4 Apr 2019 19:12:39 -0400 Subject: [PATCH] sys_linux: use pthread_setschedparam instead of sched_setscheduler 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 | 16 +++++++++++----- sys.c | 2 +- sys_linux.c | 11 ++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/configure b/configure index 486b0bc..c434127 100755 --- 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 4d68b37..2c42db1 100644 --- 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); diff --git a/sys_linux.c b/sys_linux.c index 6ae7c0d..9e4ab3f 100644 --- a/sys_linux.c +++ b/sys_linux.c @@ -33,7 +33,8 @@ #include -#if defined(HAVE_SCHED_SETSCHEDULER) +#if defined(HAVE_PTHREAD_SETSCHEDPARAM) +# include # include #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 */