]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
tests: posix: use cpu clock for sleep
authorYury Khrustalev <yury.khrustalev@arm.com>
Mon, 2 Feb 2026 14:06:38 +0000 (14:06 +0000)
committerYury Khrustalev <yury.khrustalev@arm.com>
Tue, 3 Mar 2026 09:15:40 +0000 (09:15 +0000)
On some emulated targets sleep may result in inconsistent wait
times which will lead to the failure of the tst-chmod test.

To account for this we use the CLOCK_PROCESS_CPUTIME_ID clock ID
while also consuming CPU time by repeatedly calling clock_gettime.

Reviewed-by: DJ Delorie <dj@redhat.com>
posix/tst-chmod.c

index 80a69dddd441f771382bd9a7c19171c4ef33676f..738e9aedd2f36669d66a0dbfcf5cbe4b0ee21d38 100644 (file)
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <time.h>
 
 #include <support/xunistd.h>
 
     goto fail;                                                               \
   } while (0)
 
+/* On some emulated targets sleep may result in inconsistent wait
+   times which will lead to the failure of this test.  To account
+   for this we use the CLOCK_PROCESS_CPUTIME_ID clock ID while
+   also consuming CPU time by repeatedly calling clock_gettime. */
+static void
+test_sleep (signed long seconds)
+{
+  struct timespec ts;
+  clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
+  signed long end = ts.tv_sec + seconds;
+  signed long end_ns = ts.tv_nsec;
+  while (ts.tv_sec < end || (ts.tv_sec == end && ts.tv_nsec < end_ns))
+    clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
+}
+
 static int
 do_test (int argc, char *argv[])
 {
@@ -108,7 +124,7 @@ do_test (int argc, char *argv[])
     }
 
   /* We have to wait for a second to make sure the ctime changes.  */
-  sleep (1);
+  test_sleep (1);
 
   /* Remove all access rights from the directory.  */
   if (chmod (testdir, 0) != 0)
@@ -236,7 +252,7 @@ do_test (int argc, char *argv[])
   /* We are now in the directory above the one we create the test
      directory in.  */
 
-  sleep (1);
+  test_sleep (1);
   snprintf (buf, buflen, "./%s/../%s/file",
            basename (testdir), basename (testdir));
   if (chmod (buf, 0600) != 0)