1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019, Linux Test Project
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
5*49cdfc7eSAndroid Build Coastguard Worker * Email : [email protected]
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_SAFE_CLOCKS_H__
9*49cdfc7eSAndroid Build Coastguard Worker #define TST_SAFE_CLOCKS_H__
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/timex.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/posix_clocks.h"
17*49cdfc7eSAndroid Build Coastguard Worker
safe_clock_getres(const char * file,const int lineno,clockid_t clk_id,struct timespec * res)18*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_clock_getres(const char *file, const int lineno,
19*49cdfc7eSAndroid Build Coastguard Worker clockid_t clk_id, struct timespec *res)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker int rval;
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker rval = clock_getres(clk_id, res);
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
26*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
27*49cdfc7eSAndroid Build Coastguard Worker "clock_getres(%s) failed", tst_clock_name(clk_id));
28*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
29*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
30*49cdfc7eSAndroid Build Coastguard Worker "Invalid clock_getres(%s) return value %d",
31*49cdfc7eSAndroid Build Coastguard Worker tst_clock_name(clk_id), rval);
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker return rval;
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker
safe_clock_gettime(const char * file,const int lineno,clockid_t clk_id,struct timespec * tp)37*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_clock_gettime(const char *file, const int lineno,
38*49cdfc7eSAndroid Build Coastguard Worker clockid_t clk_id, struct timespec *tp)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker int rval;
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker rval = clock_gettime(clk_id, tp);
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
45*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
46*49cdfc7eSAndroid Build Coastguard Worker "clock_gettime(%s) failed", tst_clock_name(clk_id));
47*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
48*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
49*49cdfc7eSAndroid Build Coastguard Worker "Invalid clock_gettime(%s) return value %d",
50*49cdfc7eSAndroid Build Coastguard Worker tst_clock_name(clk_id), rval);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker return rval;
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker
safe_clock_settime(const char * file,const int lineno,clockid_t clk_id,struct timespec * tp)57*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_clock_settime(const char *file, const int lineno,
58*49cdfc7eSAndroid Build Coastguard Worker clockid_t clk_id, struct timespec *tp)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker int rval;
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker rval = clock_settime(clk_id, tp);
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
65*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
66*49cdfc7eSAndroid Build Coastguard Worker "clock_gettime(%s) failed", tst_clock_name(clk_id));
67*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
69*49cdfc7eSAndroid Build Coastguard Worker "Invalid clock_gettime(%s) return value %d",
70*49cdfc7eSAndroid Build Coastguard Worker tst_clock_name(clk_id), rval);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker return rval;
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
safe_timer_create(const char * file,const int lineno,clockid_t clockid,struct sigevent * sevp,timer_t * timerid)76*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_timer_create(const char *file, const int lineno,
77*49cdfc7eSAndroid Build Coastguard Worker clockid_t clockid, struct sigevent *sevp, timer_t *timerid)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker int ret;
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
82*49cdfc7eSAndroid Build Coastguard Worker ret = timer_create(clockid, sevp, timerid);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1) {
85*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
86*49cdfc7eSAndroid Build Coastguard Worker "timer_create(%s) failed", tst_clock_name(clockid));
87*49cdfc7eSAndroid Build Coastguard Worker } else if (ret) {
88*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
89*49cdfc7eSAndroid Build Coastguard Worker "Invalid timer_create(%s) return value %d",
90*49cdfc7eSAndroid Build Coastguard Worker tst_clock_name(clockid), ret);
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker return ret;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
safe_timer_settime(const char * file,const int lineno,timer_t timerid,int flags,const struct itimerspec * new_value,struct itimerspec * old_value)96*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_timer_settime(const char *file, const int lineno,
97*49cdfc7eSAndroid Build Coastguard Worker timer_t timerid, int flags, const struct itimerspec *new_value,
98*49cdfc7eSAndroid Build Coastguard Worker struct itimerspec *old_value)
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker int ret;
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
103*49cdfc7eSAndroid Build Coastguard Worker ret = timer_settime(timerid, flags, new_value, old_value);
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1) {
106*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
107*49cdfc7eSAndroid Build Coastguard Worker "timer_settime() failed");
108*49cdfc7eSAndroid Build Coastguard Worker } else if (ret) {
109*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
110*49cdfc7eSAndroid Build Coastguard Worker "Invalid timer_settime() return value %d", ret);
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker return ret;
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker
safe_timer_gettime(const char * file,const int lineno,timer_t timerid,struct itimerspec * curr_value)116*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_timer_gettime(const char *file, const int lineno,
117*49cdfc7eSAndroid Build Coastguard Worker timer_t timerid, struct itimerspec *curr_value)
118*49cdfc7eSAndroid Build Coastguard Worker {
119*49cdfc7eSAndroid Build Coastguard Worker int ret;
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
122*49cdfc7eSAndroid Build Coastguard Worker ret = timer_gettime(timerid, curr_value);
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1) {
125*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
126*49cdfc7eSAndroid Build Coastguard Worker "timer_gettime() failed");
127*49cdfc7eSAndroid Build Coastguard Worker } else if (ret) {
128*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
129*49cdfc7eSAndroid Build Coastguard Worker "Invalid timer_gettime() return value %d", ret);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker return ret;
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker
safe_timer_delete(const char * file,const int lineno,timer_t timerid)135*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_timer_delete(const char *file, const int lineno,
136*49cdfc7eSAndroid Build Coastguard Worker timer_t timerid)
137*49cdfc7eSAndroid Build Coastguard Worker {
138*49cdfc7eSAndroid Build Coastguard Worker int ret;
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
141*49cdfc7eSAndroid Build Coastguard Worker ret = timer_delete(timerid);
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1) {
144*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO, "timer_delete() failed");
145*49cdfc7eSAndroid Build Coastguard Worker } else if (ret) {
146*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK | TERRNO,
147*49cdfc7eSAndroid Build Coastguard Worker "Invalid timer_delete() return value %d", ret);
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker return ret;
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CLOCK_GETRES(clk_id, res)\
154*49cdfc7eSAndroid Build Coastguard Worker safe_clock_getres(__FILE__, __LINE__, (clk_id), (res))
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CLOCK_GETTIME(clk_id, tp)\
157*49cdfc7eSAndroid Build Coastguard Worker safe_clock_gettime(__FILE__, __LINE__, (clk_id), (tp))
158*49cdfc7eSAndroid Build Coastguard Worker
159*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CLOCK_SETTIME(clk_id, tp)\
160*49cdfc7eSAndroid Build Coastguard Worker safe_clock_settime(__FILE__, __LINE__, (clk_id), (tp))
161*49cdfc7eSAndroid Build Coastguard Worker
162*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_TIMER_CREATE(clockid, sevp, timerid)\
163*49cdfc7eSAndroid Build Coastguard Worker safe_timer_create(__FILE__, __LINE__, (clockid), (sevp), (timerid))
164*49cdfc7eSAndroid Build Coastguard Worker
165*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_TIMER_SETTIME(timerid, flags, new_value, old_value)\
166*49cdfc7eSAndroid Build Coastguard Worker safe_timer_settime(__FILE__, __LINE__, (timerid), (flags),\
167*49cdfc7eSAndroid Build Coastguard Worker (new_value), (old_value))
168*49cdfc7eSAndroid Build Coastguard Worker
169*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_TIMER_GETTIME(timerid, curr_value)\
170*49cdfc7eSAndroid Build Coastguard Worker safe_timer_gettime(__FILE__, __LINE__, (timerid), (curr_value))
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_TIMER_DELETE(timerid)\
173*49cdfc7eSAndroid Build Coastguard Worker safe_timer_delete(__FILE__, __LINE__, timerid)
174*49cdfc7eSAndroid Build Coastguard Worker
175*49cdfc7eSAndroid Build Coastguard Worker #endif /* SAFE_CLOCKS_H__ */
176