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