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.
This commit is contained in:
Stefan R. Filipek 2019-04-04 19:12:39 -04:00 committed by Miroslav Lichvar
parent 2ddd0ae231
commit 69c6dffd63
3 changed files with 18 additions and 11 deletions

16
configure vendored
View file

@ -227,6 +227,7 @@ feat_timestamping=1
try_timestamping=0 try_timestamping=0
feat_ntp_signd=0 feat_ntp_signd=0
ntp_era_split="" ntp_era_split=""
use_pthread=0
default_user="root" default_user="root"
default_hwclockfile="" default_hwclockfile=""
default_pidfile="/var/run/chrony/chronyd.pid" default_pidfile="/var/run/chrony/chronyd.pid"
@ -652,7 +653,7 @@ then
add_def FEAT_ASYNCDNS add_def FEAT_ASYNCDNS
add_def USE_PTHREAD_ASYNCDNS add_def USE_PTHREAD_ASYNCDNS
EXTRA_OBJECTS="$EXTRA_OBJECTS nameserv_async.o" EXTRA_OBJECTS="$EXTRA_OBJECTS nameserv_async.o"
MYCFLAGS="$MYCFLAGS -pthread" use_pthread=1
fi fi
if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then
@ -786,13 +787,14 @@ fi
if [ $try_setsched = "1" ] && \ if [ $try_setsched = "1" ] && \
test_code \ test_code \
'sched_setscheduler()' \ 'pthread_setschedparam()' \
'sched.h' '' '' ' 'pthread.h sched.h' '-pthread' '' '
struct sched_param sched; struct sched_param sched;
sched_get_priority_max(SCHED_FIFO); sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, &sched);' pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);'
then then
add_def HAVE_SCHED_SETSCHEDULER add_def HAVE_PTHREAD_SETSCHEDPARAM
use_pthread=1
fi fi
if [ $try_lockmem = "1" ] && \ if [ $try_lockmem = "1" ] && \
@ -896,6 +898,10 @@ if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_tomcrypt = "1" ]
fi fi
fi fi
if [ $use_pthread = "1" ]; then
MYCFLAGS="$MYCFLAGS -pthread"
fi
SYSCONFDIR=/etc SYSCONFDIR=/etc
if [ "x$SETSYSCONFDIR" != "x" ]; then if [ "x$SETSYSCONFDIR" != "x" ]; then
SYSCONFDIR=$SETSYSCONFDIR SYSCONFDIR=$SETSYSCONFDIR

2
sys.c
View file

@ -124,7 +124,7 @@ void SYS_EnableSystemCallFilter(int level)
void SYS_SetScheduler(int SchedPriority) void SYS_SetScheduler(int SchedPriority)
{ {
#if defined(LINUX) && defined(HAVE_SCHED_SETSCHEDULER) #if defined(LINUX) && defined(HAVE_PTHREAD_SETSCHEDPARAM)
SYS_Linux_SetScheduler(SchedPriority); SYS_Linux_SetScheduler(SchedPriority);
#elif defined(MACOSX) #elif defined(MACOSX)
SYS_MacOSX_SetScheduler(SchedPriority); SYS_MacOSX_SetScheduler(SchedPriority);

View file

@ -33,7 +33,8 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#if defined(HAVE_SCHED_SETSCHEDULER) #if defined(HAVE_PTHREAD_SETSCHEDPARAM)
# include <pthread.h>
# include <sched.h> # include <sched.h>
#endif #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 */ /* Install SCHED_FIFO real-time scheduler with specified priority */
void SYS_Linux_SetScheduler(int SchedPriority) void SYS_Linux_SetScheduler(int SchedPriority)
{ {
@ -651,8 +652,8 @@ void SYS_Linux_SetScheduler(int SchedPriority)
else if ( SchedPriority < pmin ) { else if ( SchedPriority < pmin ) {
sched.sched_priority = pmin; sched.sched_priority = pmin;
} }
if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 ) { if ( pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched) == -1 ) {
LOG(LOGS_ERR, "sched_setscheduler() failed"); LOG(LOGS_ERR, "pthread_setschedparam() failed");
} }
else { else {
DEBUG_LOG("Enabled SCHED_FIFO with priority %d", 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) #if defined(HAVE_MLOCKALL)
/* Lock the process into RAM so that it will never be swapped out */ /* Lock the process into RAM so that it will never be swapped out */