1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *	03/2001 - Written by Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Check that a correct call to getitimer() succeeds.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "tst_safe_clocks.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define SEC  100
18f08c3bdfSopenharmony_ci#define USEC 10000
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic struct timeval tv;
21f08c3bdfSopenharmony_cistatic struct itimerval *value;
22f08c3bdfSopenharmony_cistatic long jiffy;
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic struct tcase {
25f08c3bdfSopenharmony_ci	int which;
26f08c3bdfSopenharmony_ci	char *des;
27f08c3bdfSopenharmony_ci} tcases[] = {
28f08c3bdfSopenharmony_ci	{ITIMER_REAL,    "ITIMER_REAL"},
29f08c3bdfSopenharmony_ci	{ITIMER_VIRTUAL, "ITIMER_VIRTUAL"},
30f08c3bdfSopenharmony_ci	{ITIMER_PROF,    "ITIMER_PROF"},
31f08c3bdfSopenharmony_ci};
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void set_setitimer_value(int sec, int usec)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	value->it_value.tv_sec = sec;
36f08c3bdfSopenharmony_ci	value->it_value.tv_usec = usec;
37f08c3bdfSopenharmony_ci	value->it_interval.tv_sec = sec;
38f08c3bdfSopenharmony_ci	value->it_interval.tv_usec = usec;
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void verify_getitimer(unsigned int i)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[i];
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	tst_res(TINFO, "tc->which = %s", tc->des);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (tc->which == ITIMER_REAL) {
48f08c3bdfSopenharmony_ci		if (gettimeofday(&tv, NULL) == -1)
49f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "gettimeofday(&tv1, NULL) failed");
50f08c3bdfSopenharmony_ci		else
51f08c3bdfSopenharmony_ci			tst_res(TINFO, "Test begin time: %ld.%lds", tv.tv_sec, tv.tv_usec);
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	TST_EXP_PASS(getitimer(tc->which, value));
55f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(value->it_value.tv_sec, 0);
56f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(value->it_value.tv_usec, 0);
57f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(value->it_interval.tv_sec, 0);
58f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(value->it_interval.tv_usec, 0);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	set_setitimer_value(SEC, USEC);
61f08c3bdfSopenharmony_ci	TST_EXP_PASS(setitimer(tc->which, value, NULL));
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	set_setitimer_value(0, 0);
64f08c3bdfSopenharmony_ci	TST_EXP_PASS(getitimer(tc->which, value));
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(value->it_interval.tv_sec, SEC);
67f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(value->it_interval.tv_usec, USEC);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	tst_res(TINFO, "value->it_value.tv_sec=%ld, value->it_value.tv_usec=%ld",
70f08c3bdfSopenharmony_ci			value->it_value.tv_sec, value->it_value.tv_usec);
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	/*
73f08c3bdfSopenharmony_ci	 * ITIMER_VIRTUAL and ITIMER_PROF timers always expire a
74f08c3bdfSopenharmony_ci	 * TICK_NSEC (jiffy) afterward the elapsed time to make
75f08c3bdfSopenharmony_ci	 * sure that at least time counters take effect.
76f08c3bdfSopenharmony_ci	 */
77f08c3bdfSopenharmony_ci	long margin = (tc->which == ITIMER_REAL) ? 0 : jiffy;
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	if (value->it_value.tv_sec == SEC) {
80f08c3bdfSopenharmony_ci		if (value->it_value.tv_usec < 0 ||
81f08c3bdfSopenharmony_ci				value->it_value.tv_usec > USEC + margin)
82f08c3bdfSopenharmony_ci			tst_brk(TFAIL, "value->it_value.tv_usec is out of range: %ld",
83f08c3bdfSopenharmony_ci				value->it_value.tv_usec);
84f08c3bdfSopenharmony_ci	} else {
85f08c3bdfSopenharmony_ci		if (value->it_value.tv_sec < 0 ||
86f08c3bdfSopenharmony_ci				value->it_value.tv_sec > SEC)
87f08c3bdfSopenharmony_ci			tst_brk(TFAIL, "value->it_value.tv_sec is out of range: %ld",
88f08c3bdfSopenharmony_ci				value->it_value.tv_sec);
89f08c3bdfSopenharmony_ci	}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	tst_res(TPASS, "timer value is within the expected range");
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	if (tc->which == ITIMER_REAL) {
94f08c3bdfSopenharmony_ci		if (gettimeofday(&tv, NULL) == -1)
95f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "gettimeofday(&tv1, NULL) failed");
96f08c3bdfSopenharmony_ci		else
97f08c3bdfSopenharmony_ci			tst_res(TINFO, "Test end time: %ld.%lds", tv.tv_sec, tv.tv_usec);
98f08c3bdfSopenharmony_ci	}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	set_setitimer_value(0, 0);
101f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(setitimer(tc->which, value, NULL));
102f08c3bdfSopenharmony_ci}
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_cistatic void setup(void)
105f08c3bdfSopenharmony_ci{
106f08c3bdfSopenharmony_ci	struct timespec time_res;
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	SAFE_CLOCK_GETRES(CLOCK_MONOTONIC_COARSE, &time_res);
109f08c3bdfSopenharmony_ci	jiffy = (time_res.tv_nsec + 999) / 1000;
110f08c3bdfSopenharmony_ci}
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_cistatic struct tst_test test = {
113f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
114f08c3bdfSopenharmony_ci	.setup = setup,
115f08c3bdfSopenharmony_ci	.test = verify_getitimer,
116f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers[]) {
117f08c3bdfSopenharmony_ci		{&value,  .size = sizeof(struct itimerval)},
118f08c3bdfSopenharmony_ci		{}
119f08c3bdfSopenharmony_ci	}
120f08c3bdfSopenharmony_ci};
121