1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Crackerjack Project., 2007
4f08c3bdfSopenharmony_ci * Porting from Crackerjack to LTP is done by:
5f08c3bdfSopenharmony_ci *	Manas Kumar Nayak <maknayak@in.ibm.com>
6f08c3bdfSopenharmony_ci * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#include <time.h>
10f08c3bdfSopenharmony_ci#include <signal.h>
11f08c3bdfSopenharmony_ci#include <sys/syscall.h>
12f08c3bdfSopenharmony_ci#include <stdio.h>
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include "time64_variants.h"
16f08c3bdfSopenharmony_ci#include "tst_timer.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic struct time64_variants variants[] = {
19f08c3bdfSopenharmony_ci#if (__NR_timer_gettime != __LTP__NR_INVALID_SYSCALL)
20f08c3bdfSopenharmony_ci	{ .timer_gettime = sys_timer_gettime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
21f08c3bdfSopenharmony_ci#endif
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#if (__NR_timer_gettime64 != __LTP__NR_INVALID_SYSCALL)
24f08c3bdfSopenharmony_ci	{ .timer_gettime = sys_timer_gettime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
25f08c3bdfSopenharmony_ci#endif
26f08c3bdfSopenharmony_ci};
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic kernel_timer_t timer;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void setup(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	struct sigevent ev;
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	ev.sigev_value = (union sigval) 0;
37f08c3bdfSopenharmony_ci	ev.sigev_signo = SIGALRM;
38f08c3bdfSopenharmony_ci	ev.sigev_notify = SIGEV_SIGNAL;
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	TEST(tst_syscall(__NR_timer_create, CLOCK_REALTIME, &ev, &timer));
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if (TST_RET) {
43f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "timer_create() failed");
44f08c3bdfSopenharmony_ci		return;
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void verify(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	struct time64_variants *tv = &variants[tst_variant];
51f08c3bdfSopenharmony_ci	struct tst_its spec = {.type = tv->ts_type, };
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	TEST(tv->timer_gettime(timer, tst_its_get(&spec)));
54f08c3bdfSopenharmony_ci	if (TST_RET == 0) {
55f08c3bdfSopenharmony_ci		if (tst_its_get_interval_sec(spec) ||
56f08c3bdfSopenharmony_ci		    tst_its_get_interval_nsec(spec) ||
57f08c3bdfSopenharmony_ci		    tst_its_get_value_sec(spec) ||
58f08c3bdfSopenharmony_ci		    tst_its_get_value_nsec(spec))
59f08c3bdfSopenharmony_ci			tst_res(TFAIL, "timespec should have been zeroed");
60f08c3bdfSopenharmony_ci		else
61f08c3bdfSopenharmony_ci			tst_res(TPASS, "timer_gettime() Passed");
62f08c3bdfSopenharmony_ci	} else {
63f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "timer_gettime() Failed");
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	TEST(tv->timer_gettime((kernel_timer_t)-1, tst_its_get(&spec)));
67f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR == EINVAL)
68f08c3bdfSopenharmony_ci		tst_res(TPASS, "timer_gettime(-1) Failed: EINVAL");
69f08c3bdfSopenharmony_ci	else
70f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "timer_gettime(-1) = %li", TST_RET);
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	TEST(tv->timer_gettime(timer, NULL));
73f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR == EFAULT)
74f08c3bdfSopenharmony_ci		tst_res(TPASS, "timer_gettime(NULL) Failed: EFAULT");
75f08c3bdfSopenharmony_ci	else
76f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "timer_gettime(-1) = %li", TST_RET);
77f08c3bdfSopenharmony_ci}
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_cistatic struct tst_test test = {
80f08c3bdfSopenharmony_ci	.test_all = verify,
81f08c3bdfSopenharmony_ci	.test_variants = ARRAY_SIZE(variants),
82f08c3bdfSopenharmony_ci	.setup = setup,
83f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
84f08c3bdfSopenharmony_ci};
85