Skip to content

Commit 308bc2e

Browse files
t-8chKAGA-KOKO
authored andcommitted
selftests/timers/nanosleep: Add tests for return of remaining time
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
1 parent 05d89fe commit 308bc2e

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

tools/testing/selftests/timers/nanosleep.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,56 @@ int nanosleep_test(int clockid, long long ns)
116116
return 0;
117117
}
118118

119+
static void dummy_event_handler(int val)
120+
{
121+
/* No action needed */
122+
}
123+
124+
static int nanosleep_test_remaining(int clockid)
125+
{
126+
struct timespec rqtp = {}, rmtp = {};
127+
struct itimerspec itimer = {};
128+
struct sigaction sa = {};
129+
timer_t timer;
130+
int ret;
131+
132+
sa.sa_handler = dummy_event_handler;
133+
ret = sigaction(SIGALRM, &sa, NULL);
134+
if (ret)
135+
return -1;
136+
137+
ret = timer_create(clockid, NULL, &timer);
138+
if (ret)
139+
return -1;
140+
141+
itimer.it_value.tv_nsec = NSEC_PER_SEC / 4;
142+
ret = timer_settime(timer, 0, &itimer, NULL);
143+
if (ret)
144+
return -1;
145+
146+
rqtp.tv_nsec = NSEC_PER_SEC / 2;
147+
ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp);
148+
if (ret != EINTR)
149+
return -1;
150+
151+
ret = timer_delete(timer);
152+
if (ret)
153+
return -1;
154+
155+
sa.sa_handler = SIG_DFL;
156+
ret = sigaction(SIGALRM, &sa, NULL);
157+
if (ret)
158+
return -1;
159+
160+
if (!in_order((struct timespec) {}, rmtp))
161+
return -1;
162+
163+
if (!in_order(rmtp, rqtp))
164+
return -1;
165+
166+
return 0;
167+
}
168+
119169
int main(int argc, char **argv)
120170
{
121171
long long length;
@@ -150,6 +200,11 @@ int main(int argc, char **argv)
150200
}
151201
length *= 100;
152202
}
203+
ret = nanosleep_test_remaining(clockid);
204+
if (ret < 0) {
205+
ksft_test_result_fail("%-31s\n", clockstring(clockid));
206+
ksft_exit_fail();
207+
}
153208
ksft_test_result_pass("%-31s\n", clockstring(clockid));
154209
next:
155210
ret = 0;

0 commit comments

Comments
 (0)