1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2014 Fujitsu Ltd.
4 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
5 *
6 * DESCRIPTION
7 * Verify that,
8 * 1. fd is not a valid file descriptor, EBADF would return.
9 * 2. curr_value is not valid a pointer, EFAULT would return.
10 * 3. fd is not a valid timerfd file descriptor, EINVAL would return.
11 */
12
13 #define _GNU_SOURCE
14
15 #include "time64_variants.h"
16 #include "tst_timer.h"
17 #include "tst_safe_timerfd.h"
18
19 char *TCID = "timerfd_gettime01";
20
21 static int bad_clockfd = -1;
22 static int clockfd;
23 static int fd;
24 static void *bad_addr;
25
26 static struct test_case_t {
27 int *fd;
28 struct tst_its *curr_value;
29 int exp_errno;
30 } test_cases[] = {
31 {&bad_clockfd, NULL, EBADF},
32 {&clockfd, NULL, EFAULT},
33 {&fd, NULL, EINVAL},
34 };
35
36 static struct time64_variants variants[] = {
37 #if (__NR_timerfd_gettime != __LTP__NR_INVALID_SYSCALL)
38 { .tfd_gettime = sys_timerfd_gettime, .desc = "syscall with old kernel spec"},
39 #endif
40
41 #if (__NR_timerfd_gettime64 != __LTP__NR_INVALID_SYSCALL)
42 { .tfd_gettime = sys_timerfd_gettime64, .desc = "syscall time64 with kernel spec"},
43 #endif
44 };
45
setup(void)46 static void setup(void)
47 {
48 tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
49 bad_addr = tst_get_bad_addr(NULL);
50
51 clockfd = timerfd_create(CLOCK_REALTIME, 0);
52 if (clockfd == -1) {
53 tst_brk(TFAIL | TERRNO, "timerfd_create() fail");
54 return;
55 }
56
57 fd = SAFE_OPEN("test_file", O_RDWR | O_CREAT, 0644);
58 }
59
cleanup(void)60 static void cleanup(void)
61 {
62 if (clockfd > 0)
63 close(clockfd);
64
65 if (fd > 0)
66 close(fd);
67 }
68
run(unsigned int n)69 static void run(unsigned int n)
70 {
71 struct time64_variants *tv = &variants[tst_variant];
72 struct test_case_t *test = &test_cases[n];
73 void *its;
74
75 if (test->exp_errno == EFAULT)
76 its = bad_addr;
77 else
78 its = tst_its_get(test->curr_value);
79
80 TEST(tv->tfd_gettime(*test->fd, its));
81
82 if (TST_RET != -1) {
83 tst_res(TFAIL, "timerfd_gettime() succeeded unexpectedly");
84 return;
85 }
86
87 if (TST_ERR == test->exp_errno) {
88 tst_res(TPASS | TTERRNO,
89 "timerfd_gettime() failed as expected");
90 } else {
91 tst_res(TFAIL | TTERRNO,
92 "timerfd_gettime() failed unexpectedly; expected: "
93 "%d - %s", test->exp_errno, strerror(test->exp_errno));
94 }
95 }
96
97 static struct tst_test test = {
98 .test = run,
99 .tcnt = ARRAY_SIZE(test_cases),
100 .test_variants = ARRAY_SIZE(variants),
101 .setup = setup,
102 .cleanup = cleanup,
103 .needs_tmpdir = 1,
104 .min_kver = "2.6.25",
105 };
106