1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd
4 * Authors: Takahiro Yasui <takahiro.yasui.mp@hitachi.com>,
5 * Yumiko Sugita <yumiko.sugita.yf@hitachi.com>,
6 * Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp>
7 * LTP authors:
8 * Manas Kumar Nayak maknayak@in.ibm.com>
9 * Zeng Linggang <zenglg.jy@cn.fujitsu.com>
10 * Cyril Hrubis <chrubis@suse.cz>
11 */
12
13 #include <errno.h>
14
15 #include "tst_test.h"
16 #include "lapi/syscalls.h"
17 #include "lapi/posix_clocks.h"
18
19 static struct timespec *res;
20
21 static struct test_case {
22 char *name;
23 clockid_t clk_id;
24 int ret;
25 int err;
26 } tcase[] = {
27 {"REALTIME", CLOCK_REALTIME, 0, 0},
28 {"MONOTONIC", CLOCK_MONOTONIC, 0, 0},
29 {"PROCESS_CPUTIME_ID", CLOCK_PROCESS_CPUTIME_ID, 0, 0},
30 {"THREAD_CPUTIME_ID", CLOCK_THREAD_CPUTIME_ID, 0, 0},
31 {"CLOCK_MONOTONIC_RAW", CLOCK_MONOTONIC_RAW, 0, 0,},
32 {"CLOCK_REALTIME_COARSE", CLOCK_REALTIME_COARSE, 0, 0,},
33 {"CLOCK_MONOTONIC_COARSE", CLOCK_MONOTONIC_COARSE, 0, 0,},
34 {"CLOCK_BOOTTIME", CLOCK_BOOTTIME, 0, 0,},
35 {"CLOCK_REALTIME_ALARM", CLOCK_REALTIME_ALARM, 0, 0,},
36 {"CLOCK_BOOTTIME_ALARM", CLOCK_BOOTTIME_ALARM, 0, 0,},
37 {"-1", -1, -1, EINVAL},
38 };
39
40 static const char *variant_desc[] = {
41 "default (vdso or syscall)",
42 "syscall",
43 "syscall with NULL res parameter" };
44
setup(void)45 static void setup(void)
46 {
47 tst_res(TINFO, "Testing variant: %s", variant_desc[tst_variant]);
48 }
49
do_test(unsigned int i)50 static void do_test(unsigned int i)
51 {
52 switch (tst_variant) {
53 case 0:
54 TEST(clock_getres(tcase[i].clk_id, res));
55 break;
56 case 1:
57 TEST(tst_syscall(__NR_clock_getres, tcase[i].clk_id, res));
58 break;
59 case 2:
60 TEST(tst_syscall(__NR_clock_getres, tcase[i].clk_id, NULL));
61 break;
62 }
63
64 if (TST_RET != tcase[i].ret) {
65 if (TST_ERR == EINVAL) {
66 tst_res(TCONF, "clock_getres(%s, ...) NO SUPPORTED", tcase[i].name);
67 return;
68 }
69
70 tst_res(TFAIL | TTERRNO, "clock_getres(%s, ...) failed", tcase[i].name);
71 return;
72 }
73
74 if (TST_ERR != tcase[i].err) {
75 tst_res(TFAIL,
76 "clock_getres(%s, ...) failed unexpectedly: %s, expected: %s",
77 tcase[i].name, tst_strerrno(TST_ERR), tst_strerrno(tcase[i].err));
78 return;
79 }
80
81 tst_res(TPASS, "clock_getres(%s, ...) succeeded", tcase[i].name);
82 }
83
84 static struct tst_test test = {
85 .test = do_test,
86 .tcnt = ARRAY_SIZE(tcase),
87 .test_variants = 3,
88 .setup = setup,
89 .bufs = (struct tst_buffers []) {
90 {&res, .size = sizeof(*res)},
91 {},
92 }
93 };
94