xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/settimeofday/settimeofday01.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  * Copyright (c) Linux Test Project, 2001-2024
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Check the basic functionality of settimeofday().
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define	VAL_SEC		100
20*49cdfc7eSAndroid Build Coastguard Worker #define	VAL_MSEC	100
21*49cdfc7eSAndroid Build Coastguard Worker #define ACCEPTABLE_DELTA 500
22*49cdfc7eSAndroid Build Coastguard Worker #define USEC_PER_SEC    1000000L
23*49cdfc7eSAndroid Build Coastguard Worker 
verify_settimeofday(void)24*49cdfc7eSAndroid Build Coastguard Worker static void verify_settimeofday(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	suseconds_t delta;
27*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval tv1, tv2;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	if (gettimeofday(&tv1, NULL) == -1)
30*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "gettimeofday(&tv1, NULL) failed");
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	tv1.tv_sec += VAL_SEC;
33*49cdfc7eSAndroid Build Coastguard Worker 	tv1.tv_usec += VAL_MSEC;
34*49cdfc7eSAndroid Build Coastguard Worker 	if (tv1.tv_usec >= USEC_PER_SEC)
35*49cdfc7eSAndroid Build Coastguard Worker 		tv1.tv_usec = VAL_MSEC;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	TEST(settimeofday(&tv1, NULL));
38*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "settimeofday(&tv1, NULL) failed");
40*49cdfc7eSAndroid Build Coastguard Worker 		return;
41*49cdfc7eSAndroid Build Coastguard Worker 	}
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	if (gettimeofday(&tv2, NULL) == -1)
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "gettimeofday(&tv2, NULL) failed");
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (tv2.tv_sec > tv1.tv_sec) {
47*49cdfc7eSAndroid Build Coastguard Worker 		delta =
48*49cdfc7eSAndroid Build Coastguard Worker 			(suseconds_t) (tv2.tv_sec - tv1.tv_sec) * 1000 +
49*49cdfc7eSAndroid Build Coastguard Worker 			(tv2.tv_usec - tv1.tv_usec) / 1000;
50*49cdfc7eSAndroid Build Coastguard Worker 	} else {
51*49cdfc7eSAndroid Build Coastguard Worker 		delta =
52*49cdfc7eSAndroid Build Coastguard Worker 			(suseconds_t) (tv1.tv_sec - tv2.tv_sec) * 1000 +
53*49cdfc7eSAndroid Build Coastguard Worker 			(tv1.tv_usec - tv2.tv_usec) / 1000;
54*49cdfc7eSAndroid Build Coastguard Worker 	}
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	if (delta > -ACCEPTABLE_DELTA && delta < ACCEPTABLE_DELTA)
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "settimeofday() pass");
58*49cdfc7eSAndroid Build Coastguard Worker 	else
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "settimeofday() fail");
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
63*49cdfc7eSAndroid Build Coastguard Worker 	.restore_wallclock = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_settimeofday,
65*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
66*49cdfc7eSAndroid Build Coastguard Worker };
67