]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Use exit_lock when accessing TID on pthread_getcpuclockid
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 23 Aug 2021 17:09:23 +0000 (14:09 -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.

Checked on x86_64-linux-gnu.

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

index 0cd9f77bea2bf1d05e84a3d09d279356096a5d59..f1141a6f9294af676217a4e7eb1113355eb94b0f 100644 (file)
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <pthreadP.h>
-#include <sys/time.h>
-#include <tls.h>
+#include <libc-lock.h>
 #include <kernel-posix-cpu-timers.h>
+#include <pthreadP.h>
 #include <shlib-compat.h>
 
 int
@@ -28,17 +26,21 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 {
   struct pthread *pd = (struct pthread *) threadid;
 
-  /* Make sure the descriptor is valid.  */
-  if (INVALID_TD_P (pd))
-    /* Not a valid thread handle.  */
-    return ESRCH;
+  /* 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);
 
-  /* The clockid_t value is a simple computation from the TID.  */
+  int res = 0;
+  if (pd->tid )
+    *clockid = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
+  else
+    res = EINVAL;
 
-  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
+  __libc_lock_unlock (pd->exit_lock);
+  internal_signal_restore_set (&old_mask);
 
-  *clockid = tidclock;
-  return 0;
+  return res;
 }
 versioned_symbol (libc, __pthread_getcpuclockid, pthread_getcpuclockid,
                   GLIBC_2_34);
index 8997752528b0257feacfc5d72e4efe6e043dc76a..c544ebc6be059e07af15a20ce9bd58b966695b39 100644 (file)
@@ -23,6 +23,7 @@
 #include <support/check.h>
 #include <support/support.h>
 #include <support/xthread.h>
+#include <time.h>
 
 static void *
 noop_thread (void *closure)
@@ -43,6 +44,12 @@ do_test (void)
     TEST_COMPARE (r, EINVAL);
   }
 
+  {
+    clockid_t clkid;
+    int r = pthread_getcpuclockid (thr, &clkid);
+    TEST_COMPARE (r, EINVAL);
+  }
+
   xpthread_join (thr);
 
   return 0;