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