]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/timers/nanosleep: Add tests for return of remaining time
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Thu, 6 Nov 2025 15:15:24 +0000 (16:15 +0100)
committerThomas Gleixner <tglx@linutronix.de>
Fri, 14 Nov 2025 19:34:50 +0000 (20:34 +0100)
If interrupted by a signal clock_nanosleep() returns the remaining time
into the structure pointed to by the rmtp parameter. So far this
functionality was not tested by the timer selftests.

Extend the nanosleep selftest to cover this feature.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://patch.msgid.link/20251106-nanosleep-rtmp-selftest-v1-1-f9212fb295fe@linutronix.de
tools/testing/selftests/timers/nanosleep.c

index 252c6308c5698f9094b8bdc39c284077b5d55531..10badae13ebeb8d596839d5aab1a5161526eeaa9 100644 (file)
@@ -116,6 +116,56 @@ int nanosleep_test(int clockid, long long ns)
        return 0;
 }
 
+static void dummy_event_handler(int val)
+{
+       /* No action needed */
+}
+
+static int nanosleep_test_remaining(int clockid)
+{
+       struct timespec rqtp = {}, rmtp = {};
+       struct itimerspec itimer = {};
+       struct sigaction sa = {};
+       timer_t timer;
+       int ret;
+
+       sa.sa_handler = dummy_event_handler;
+       ret = sigaction(SIGALRM, &sa, NULL);
+       if (ret)
+               return -1;
+
+       ret = timer_create(clockid, NULL, &timer);
+       if (ret)
+               return -1;
+
+       itimer.it_value.tv_nsec = NSEC_PER_SEC / 4;
+       ret = timer_settime(timer, 0, &itimer, NULL);
+       if (ret)
+               return -1;
+
+       rqtp.tv_nsec = NSEC_PER_SEC / 2;
+       ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp);
+       if (ret != EINTR)
+               return -1;
+
+       ret = timer_delete(timer);
+       if (ret)
+               return -1;
+
+       sa.sa_handler = SIG_DFL;
+       ret = sigaction(SIGALRM, &sa, NULL);
+       if (ret)
+               return -1;
+
+       if (!in_order((struct timespec) {}, rmtp))
+               return -1;
+
+       if (!in_order(rmtp, rqtp))
+               return -1;
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        long long length;
@@ -150,6 +200,11 @@ int main(int argc, char **argv)
                        }
                        length *= 100;
                }
+               ret = nanosleep_test_remaining(clockid);
+               if (ret < 0) {
+                       ksft_test_result_fail("%-31s\n", clockstring(clockid));
+                       ksft_exit_fail();
+               }
                ksft_test_result_pass("%-31s\n", clockstring(clockid));
 next:
                ret = 0;