Skip to content

Commit bcf3733

Browse files
vireshkmetan-ucw
authored andcommitted
syscalls/clock_settime: Add support for time64 tests
This adds support for time64 tests to the existing clock_settime() syscall tests. Signed-off-by: Viresh Kumar <[email protected]> Signed-off-by: Cyril Hrubis <[email protected]>
1 parent a143f48 commit bcf3733

File tree

3 files changed

+142
-53
lines changed

3 files changed

+142
-53
lines changed

include/tst_timer.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ static inline int sys_clock_gettime64(clockid_t clk_id, void *ts)
159159
return tst_syscall(__NR_clock_gettime64, clk_id, ts);
160160
}
161161

162+
static inline int libc_clock_settime(clockid_t clk_id, void *ts)
163+
{
164+
return clock_settime(clk_id, ts);
165+
}
166+
167+
static inline int sys_clock_settime(clockid_t clk_id, void *ts)
168+
{
169+
return tst_syscall(__NR_clock_settime, clk_id, ts);
170+
}
171+
172+
static inline int sys_clock_settime64(clockid_t clk_id, void *ts)
173+
{
174+
return tst_syscall(__NR_clock_settime64, clk_id, ts);
175+
}
176+
162177
/*
163178
* Returns tst_ts seconds.
164179
*/

testcases/kernel/syscalls/clock_settime/clock_settime01.c

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,91 @@
1616
#include "config.h"
1717
#include "tst_timer.h"
1818
#include "tst_safe_clocks.h"
19-
#include "tst_test.h"
20-
#include "lapi/syscalls.h"
19+
#include "lapi/abisize.h"
2120

2221
#define DELTA_SEC 10
2322
#define DELTA_US (long long) (DELTA_SEC * 1000000)
2423
#define DELTA_EPS (long long) (DELTA_US * 0.1)
2524

26-
static struct timespec *begin, *change, *end;
25+
static struct tst_ts *begin, *change, *end;
26+
27+
static struct test_variants {
28+
int (*gettime)(clockid_t clk_id, void *ts);
29+
int (*settime)(clockid_t clk_id, void *ts);
30+
enum tst_ts_type type;
31+
char *desc;
32+
} variants[] = {
33+
#if defined(TST_ABI32)
34+
{ .gettime = libc_clock_gettime, .settime = libc_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
35+
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "syscall with libc spec"},
36+
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with kernel spec32"},
37+
#endif
38+
39+
#if defined(TST_ABI64)
40+
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_TIMESPEC, .desc = "syscall with kernel spec64"},
41+
#endif
42+
43+
#if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
44+
{ .gettime = sys_clock_gettime64, .settime = sys_clock_settime64, .type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec64"},
45+
#endif
46+
};
47+
48+
static void setup(void)
49+
{
50+
begin->type = change->type = end->type = variants[tst_variant].type;
51+
tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
52+
}
53+
54+
static void do_clock_gettime(struct test_variants *tv, struct tst_ts *ts)
55+
{
56+
int ret;
57+
58+
ret = tv->gettime(CLOCK_REALTIME, tst_ts_get(ts));
59+
if (ret == -1)
60+
tst_brk(TBROK | TERRNO, "clock_settime(CLOCK_REALTIME) failed");
61+
}
2762

2863
static void verify_clock_settime(void)
2964
{
65+
struct test_variants *tv = &variants[tst_variant];
3066
long long elapsed;
3167

3268
/* test 01: move forward */
69+
do_clock_gettime(tv, begin);
3370

34-
SAFE_CLOCK_GETTIME(CLOCK_REALTIME, begin);
35-
36-
*change = tst_timespec_add_us(*begin, DELTA_US);
71+
*change = tst_ts_add_us(*begin, DELTA_US);
3772

38-
if (clock_settime(CLOCK_REALTIME, change) != 0)
39-
tst_brk(TBROK | TTERRNO, "could not set realtime change");
73+
TEST(tv->settime(CLOCK_REALTIME, tst_ts_get(change)));
74+
if (TST_RET == -1) {
75+
tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
76+
tst_clock_name(CLOCK_REALTIME));
77+
return;
78+
}
4079

41-
SAFE_CLOCK_GETTIME(CLOCK_REALTIME, end);
80+
do_clock_gettime(tv, end);
4281

43-
elapsed = tst_timespec_diff_us(*end, *begin);
82+
elapsed = tst_ts_diff_us(*end, *begin);
4483

4584
if (elapsed >= DELTA_US && elapsed < (DELTA_US + DELTA_EPS))
4685
tst_res(TPASS, "clock_settime(2): was able to advance time");
4786
else
4887
tst_res(TFAIL, "clock_settime(2): could not advance time");
4988

5089
/* test 02: move backward */
90+
do_clock_gettime(tv, begin);
5191

52-
SAFE_CLOCK_GETTIME(CLOCK_REALTIME, begin);
53-
54-
*change = tst_timespec_sub_us(*begin, DELTA_US);
92+
*change = tst_ts_sub_us(*begin, DELTA_US);
5593

56-
if (clock_settime(CLOCK_REALTIME, change) != 0)
57-
tst_brk(TBROK | TTERRNO, "could not set realtime change");
94+
TEST(tv->settime(CLOCK_REALTIME, tst_ts_get(change)));
95+
if (TST_RET == -1) {
96+
tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
97+
tst_clock_name(CLOCK_REALTIME));
98+
return;
99+
}
58100

59-
SAFE_CLOCK_GETTIME(CLOCK_REALTIME, end);
101+
do_clock_gettime(tv, end);
60102

61-
elapsed = tst_timespec_diff_us(*end, *begin);
103+
elapsed = tst_ts_diff_us(*end, *begin);
62104

63105
if (~(elapsed) <= DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS))
64106
tst_res(TPASS, "clock_settime(2): was able to recede time");
@@ -68,6 +110,8 @@ static void verify_clock_settime(void)
68110

69111
static struct tst_test test = {
70112
.test_all = verify_clock_settime,
113+
.test_variants = ARRAY_SIZE(variants),
114+
.setup = setup,
71115
.needs_root = 1,
72116
.restore_wallclock = 1,
73117
.bufs = (struct tst_buffers []) {

testcases/kernel/syscalls/clock_settime/clock_settime02.c

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,51 @@
99
*/
1010

1111
#include "config.h"
12-
#include "tst_test.h"
13-
#include "lapi/syscalls.h"
1412
#include "tst_timer.h"
1513
#include "tst_safe_clocks.h"
14+
#include "lapi/abisize.h"
1615

1716
#define DELTA_SEC 10
1817
#define NSEC_PER_SEC (1000000000L)
1918

19+
static void *bad_addr;
20+
2021
struct test_case {
2122
clockid_t type;
22-
struct timespec newtime;
2323
int exp_err;
2424
int replace;
25+
long tv_sec;
26+
long tv_nsec;
2527
};
2628

2729
struct test_case tc[] = {
2830
{ /* case 01: REALTIME: timespec NULL */
2931
.type = CLOCK_REALTIME,
3032
.exp_err = EFAULT,
3133
.replace = 1,
34+
.tv_sec = 0,
35+
.tv_nsec = 0,
3236
},
3337
{ /* case 02: REALTIME: tv_sec = -1 */
3438
.type = CLOCK_REALTIME,
35-
.newtime.tv_sec = -1,
3639
.exp_err = EINVAL,
3740
.replace = 1,
41+
.tv_sec = -1,
42+
.tv_nsec = 0,
3843
},
3944
{ /* case 03: REALTIME: tv_nsec = -1 */
4045
.type = CLOCK_REALTIME,
41-
.newtime.tv_nsec = -1,
4246
.exp_err = EINVAL,
4347
.replace = 1,
48+
.tv_sec = 0,
49+
.tv_nsec = -1,
4450
},
4551
{ /* case 04: REALTIME: tv_nsec = 1s+1 */
4652
.type = CLOCK_REALTIME,
47-
.newtime.tv_nsec = NSEC_PER_SEC + 1,
4853
.exp_err = EINVAL,
4954
.replace = 1,
55+
.tv_sec = 0,
56+
.tv_nsec = NSEC_PER_SEC + 1,
5057
},
5158
{ /* case 05: MONOTONIC */
5259
.type = CLOCK_MONOTONIC,
@@ -83,64 +90,87 @@ struct test_case tc[] = {
8390
},
8491
};
8592

86-
/*
87-
* Some tests may cause libc to segfault when passing bad arguments.
88-
*/
89-
static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
93+
static struct tst_ts spec;
94+
95+
static struct test_variants {
96+
int (*gettime)(clockid_t clk_id, void *ts);
97+
int (*settime)(clockid_t clk_id, void *ts);
98+
enum tst_ts_type type;
99+
char *desc;
100+
} variants[] = {
101+
#if defined(TST_ABI32)
102+
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "syscall with libc spec"},
103+
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with kernel spec32"},
104+
#endif
105+
106+
#if defined(TST_ABI64)
107+
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_TIMESPEC, .desc = "syscall with kernel spec64"},
108+
#endif
109+
110+
#if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
111+
{ .gettime = sys_clock_gettime64, .settime = sys_clock_settime64, .type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec64"},
112+
#endif
113+
};
114+
115+
static void setup(void)
90116
{
91-
return tst_syscall(__NR_clock_settime, clk_id, tp);
117+
tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
118+
119+
bad_addr = tst_get_bad_addr(NULL);
92120
}
93121

94122
static void verify_clock_settime(unsigned int i)
95123
{
96-
struct timespec spec, *specptr;
124+
struct test_variants *tv = &variants[tst_variant];
125+
void *ts;
97126

98-
specptr = &spec;
127+
spec.type = tv->type;
99128

100129
if (tc[i].replace == 0) {
101-
102-
SAFE_CLOCK_GETTIME(CLOCK_REALTIME, specptr);
130+
TEST(tv->gettime(CLOCK_REALTIME, tst_ts_get(&spec)));
131+
if (TST_RET == -1) {
132+
tst_res(TFAIL | TTERRNO, "clock_gettime(2) failed for clock %s",
133+
tst_clock_name(CLOCK_REALTIME));
134+
return;
135+
}
103136

104137
/* add 1 sec to wall clock */
105-
specptr->tv_sec += 1;
106-
138+
spec = tst_ts_add_us(spec, 1000000);
107139
} else {
108-
109140
/* use given time spec */
110-
*specptr = tc[i].newtime;
141+
tst_ts_set_sec(&spec, tc[i].tv_sec);
142+
tst_ts_set_nsec(&spec, tc[i].tv_nsec);
111143
}
112144

113145
/* bad pointer case */
114146
if (tc[i].exp_err == EFAULT)
115-
specptr = tst_get_bad_addr(NULL);
116-
117-
TEST(sys_clock_settime(tc[i].type, specptr));
118-
119-
if (TST_RET == -1) {
147+
ts = bad_addr;
148+
else
149+
ts = tst_ts_get(&spec);
120150

121-
if (tc[i].exp_err == TST_ERR) {
122-
tst_res(TPASS | TTERRNO,
123-
"clock_settime(%s): failed as expected",
124-
tst_clock_name(tc[i].type));
125-
return;
126-
}
151+
TEST(tv->settime(tc[i].type, ts));
127152

128-
tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s "
129-
"expected to fail with %s",
153+
if (TST_RET != -1) {
154+
tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s passed unexpectedly, expected %s",
130155
tst_clock_name(tc[i].type),
131156
tst_strerrno(tc[i].exp_err));
157+
return;
158+
}
132159

160+
if (tc[i].exp_err == TST_ERR) {
161+
tst_res(TPASS | TTERRNO, "clock_settime(%s): failed as expected",
162+
tst_clock_name(tc[i].type));
133163
return;
134164
}
135165

136-
tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s passed "
137-
"unexpectedly, expected %s",
138-
tst_clock_name(tc[i].type),
139-
tst_strerrno(tc[i].exp_err));
166+
tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s " "expected to fail with %s",
167+
tst_clock_name(tc[i].type), tst_strerrno(tc[i].exp_err));
140168
}
141169

142170
static struct tst_test test = {
143171
.test = verify_clock_settime,
172+
.test_variants = ARRAY_SIZE(variants),
173+
.setup = setup,
144174
.tcnt = ARRAY_SIZE(tc),
145175
.needs_root = 1,
146176
.restore_wallclock = 1,

0 commit comments

Comments
 (0)