From a6456b24ed2d2ac30c34443991e8fe5ee6025106 Mon Sep 17 00:00:00 2001 From: Zbigniew Bodek Date: Tue, 8 Dec 2020 02:35:01 +0800 Subject: [PATCH] Enable normal user to create a thread for the service Default scheduling policy for a service thread is a real time SCHED_RR which requires superuser privilege to be set. To allow for creating task when invoked by normal user on Linux, apply policy depending on root or not root. Signed-off-by: Zbigniew Bodek Change-Id: Ia0bdf9c3225e118b662f817c5d53c93092a33348 --- samgr/adapter/posix/thread_adapter.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/samgr/adapter/posix/thread_adapter.c b/samgr/adapter/posix/thread_adapter.c index efe6bf2..3a3f977 100755 --- a/samgr/adapter/posix/thread_adapter.c +++ b/samgr/adapter/posix/thread_adapter.c @@ -73,10 +73,23 @@ ThreadId THREAD_Create(Runnable run, void *argv, const ThreadAttr *attr) pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED); #ifdef SAMGR_LINUX_ADAPTER struct sched_param sched = {attr->priority}; + int policy = SCHED_OTHER; + const int ROOT_UID = 0; + if (geteuid() == ROOT_UID) { + /* + * Real-time scheduling policy requires superuser privileges. + * Note: additionally, real-time thread can be scheduled before + * normal thread even if it yields CPU using sched_yield(). + * To actually yield real-time thread one could + * sleep() rather than yield(). + */ + policy = SCHED_RR; + } #else struct sched_param sched = {PRI_BUTT - attr->priority}; + int policy = SCHED_RR; #endif - pthread_attr_setschedpolicy(&threadAttr, SCHED_RR); + pthread_attr_setschedpolicy(&threadAttr, policy); pthread_attr_setschedparam(&threadAttr, &sched); (void) pthread_once(&g_localKeyOnce, KeyCreate); pthread_t threadId = 0;