]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Use exit_lock when accessing TID on pthread_setschedparam
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 23 Aug 2021 17:48:30 +0000 (14:48 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 9 Jul 2025 22:57:21 +0000 (19:57 -0300)
Also return EINVAL if the thread is already terminated at the time
of the call.

The sched_getparam call is also replaced with a INTERNAL_SYSCALL_CALL
to avoid clobbering errno.

Checked on x86_64-linux-gnu.

nptl/pthread_setschedparam.c
sysdeps/pthread/tst-pthread-exited.c

index 1b43eb1f714be4e44663c36997ea9cda9184fcdb..02933438de8848a93254190267f8ef84d182872e 100644 (file)
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <sched.h>
-#include <string.h>
-#include "pthreadP.h"
-#include <lowlevellock.h>
+#include <libc-lock.h>
+#include <kernel-posix-cpu-timers.h>
+#include <pthreadP.h>
 
 
-int
-__pthread_setschedparam (pthread_t threadid, int policy,
-                        const struct sched_param *param)
+static int
+setschedparam (struct pthread *pd, int policy,
+              const struct sched_param *param)
 {
-  struct pthread *pd = (struct pthread *) threadid;
-
-  /* Make sure the descriptor is valid.  */
-  if (INVALID_TD_P (pd))
-    /* Not a valid thread handle.  */
-    return ESRCH;
-
-  int result = 0;
-
   /* See CREATE THREAD NOTES in nptl/pthread_create.c.  */
   lll_lock (pd->lock, LLL_PRIVATE);
 
@@ -43,8 +32,7 @@ __pthread_setschedparam (pthread_t threadid, int policy,
 
   /* If the thread should have higher priority because of some
      PTHREAD_PRIO_PROTECT mutexes it holds, adjust the priority.  */
-  if (__builtin_expect (pd->tpp != NULL, 0)
-      && pd->tpp->priomax > param->sched_priority)
+  if (pd->tpp != NULL && pd->tpp->priomax > param->sched_priority)
     {
       p = *param;
       p.sched_priority = pd->tpp->priomax;
@@ -52,10 +40,8 @@ __pthread_setschedparam (pthread_t threadid, int policy,
     }
 
   /* Try to set the scheduler information.  */
-  if (__builtin_expect (__sched_setscheduler (pd->tid, policy,
-                                             param) == -1, 0))
-    result = errno;
-  else
+  int r = INTERNAL_SYSCALL_CALL (sched_setscheduler, pd->tid, policy, param);
+  if (r == 0)
     {
       /* We succeeded changing the kernel information.  Reflect this
         change in the thread descriptor.  */
@@ -66,6 +52,25 @@ __pthread_setschedparam (pthread_t threadid, int policy,
 
   lll_unlock (pd->lock, LLL_PRIVATE);
 
+  return -r;
+}
+
+int
+__pthread_setschedparam (pthread_t threadid, int policy,
+                        const struct sched_param *param)
+{
+  struct pthread *pd = (struct pthread *) threadid;
+
+  /* Block all signals, as required by pd->exit_lock.  */
+  internal_sigset_t old_mask;
+  internal_signal_block_all (&old_mask);
+  __libc_lock_lock (pd->exit_lock);
+
+  int result = pd->tid != 0 ? setschedparam (pd, policy, param) : EINVAL;
+
+  __libc_lock_unlock (pd->exit_lock);
+  internal_signal_restore_set (&old_mask);
+
   return result;
 }
 strong_alias (__pthread_setschedparam, pthread_setschedparam)
index c544ebc6be059e07af15a20ce9bd58b966695b39..b7686857711264bd84f07c5fbf8ddac6add1d183 100644 (file)
@@ -50,6 +50,12 @@ do_test (void)
     TEST_COMPARE (r, EINVAL);
   }
 
+  {
+    struct sched_param sch = { 0 };
+    int r = pthread_setschedparam (thr, SCHED_FIFO, &sch);
+    TEST_COMPARE (r, EINVAL);
+  }
+
   xpthread_join (thr);
 
   return 0;