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