From: Yury Khrustalev Date: Mon, 2 Feb 2026 14:06:38 +0000 (+0000) Subject: tests: posix: use cpu clock for sleep X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d28f1f1a07e3a4473fb87cfade9528e54fbffe0;p=thirdparty%2Fglibc.git tests: posix: use cpu clock for sleep 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 --- diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c index 80a69dddd4..738e9aedd2 100644 --- a/posix/tst-chmod.c +++ b/posix/tst-chmod.c @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -37,6 +38,21 @@ 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)