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