1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) M. Koehrer <mathias_koehrer@arcor.de>, 2009
4 * Copyright (C) 2017 Cyril Hrubis <chrubis@suse.cz>
5 */
6
7#include <stdio.h>
8#include <time.h>
9#include <unistd.h>
10#include "time64_variants.h"
11#include "tst_safe_clocks.h"
12#include "tst_timer.h"
13
14static clockid_t tcase[] = {
15	CLOCK_MONOTONIC,
16	CLOCK_REALTIME,
17};
18
19static struct time64_variants variants[] = {
20	{ .clock_gettime = libc_clock_gettime, .clock_nanosleep = libc_clock_nanosleep, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
21
22#if (__NR_clock_nanosleep != __LTP__NR_INVALID_SYSCALL)
23	{ .clock_gettime = sys_clock_gettime, .clock_nanosleep = sys_clock_nanosleep, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
24#endif
25
26#if (__NR_clock_nanosleep_time64 != __LTP__NR_INVALID_SYSCALL)
27	{ .clock_gettime = sys_clock_gettime64, .clock_nanosleep = sys_clock_nanosleep64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
28#endif
29};
30
31void setup(void)
32{
33	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
34}
35
36static void do_test(unsigned int i)
37{
38	struct time64_variants *tv = &variants[tst_variant];
39	struct tst_ts ts = {.type = tv->ts_type};
40
41	TEST(tv->clock_gettime(tcase[i], tst_ts_get(&ts)));
42	if (TST_RET == -1) {
43		tst_res(TFAIL | TTERRNO, "clock_gettime(2) failed for clock %s",
44			tst_clock_name(tcase[i]));
45		return;
46	}
47
48	ts = tst_ts_add_us(ts, 10000);
49
50	TEST(tv->clock_nanosleep(tcase[i], TIMER_ABSTIME, tst_ts_get(&ts), NULL));
51
52	if (TST_RET) {
53		tst_res(TFAIL | TTERRNO, "clock_nanosleep(2) failed for clock %s",
54			tst_clock_name(tcase[i]));
55	}
56
57	tst_res(TPASS, "clock_nanosleep(2) passed for clock %s",
58		tst_clock_name(tcase[i]));
59}
60
61static struct tst_test test = {
62	.tcnt = ARRAY_SIZE(tcase),
63	.test = do_test,
64	.test_variants = ARRAY_SIZE(variants),
65	.setup = setup,
66};
67