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 "lapi/syscalls.h"
15 #include "lapi/posix_clocks.h"
16 
safe_clock_getres(const char * file,const int lineno,clockid_t clk_id,struct timespec * res)17 static inline void safe_clock_getres(const char *file, const int lineno,
18 	clockid_t clk_id, struct timespec *res)
19 {
20 	int rval;
21 
22 	rval = clock_getres(clk_id, res);
23 	if (rval != 0)
24 		tst_brk(TBROK | TERRNO,
25 			"%s:%d clock_getres() failed", file, lineno);
26 
27 }
28 
safe_clock_gettime(const char * file,const int lineno,clockid_t clk_id,struct timespec * tp)29 static inline void safe_clock_gettime(const char *file, const int lineno,
30 	clockid_t clk_id, struct timespec *tp)
31 {
32 	int rval;
33 
34 	rval = clock_gettime(clk_id, tp);
35 	if (rval != 0)
36 		tst_brk(TBROK | TERRNO,
37 			"%s:%d clock_gettime() failed", file, lineno);
38 }
39 
40 
safe_clock_settime(const char * file,const int lineno,clockid_t clk_id,struct timespec * tp)41 static inline void safe_clock_settime(const char *file, const int lineno,
42 	clockid_t clk_id, struct timespec *tp)
43 {
44 	int rval;
45 
46 	rval = clock_settime(clk_id, tp);
47 	if (rval != 0)
48 		tst_brk(TBROK | TERRNO,
49 			"%s:%d clock_gettime() failed", file, lineno);
50 }
51 
safe_clock_adjtime(const char * file,const int lineno,clockid_t clk_id,struct timex * txc)52 static inline int safe_clock_adjtime(const char *file, const int lineno,
53 	clockid_t clk_id, struct timex *txc)
54 {
55 	int rval;
56 
57 	rval = tst_syscall(__NR_clock_adjtime, clk_id, txc);
58 	if (rval < 0)
59 		tst_brk(TBROK | TERRNO,
60 			"%s:%d clock_adjtime() failed %i", file, lineno, rval);
61 
62 	return rval;
63 }
64 
65 #define SAFE_CLOCK_GETRES(clk_id, res)\
66 	safe_clock_getres(__FILE__, __LINE__, (clk_id), (res))
67 
68 #define SAFE_CLOCK_GETTIME(clk_id, tp)\
69 	safe_clock_gettime(__FILE__, __LINE__, (clk_id), (tp))
70 
71 #define SAFE_CLOCK_SETTIME(clk_id, tp)\
72 	safe_clock_settime(__FILE__, __LINE__, (clk_id), (tp))
73 
74 #define SAFE_CLOCK_ADJTIME(clk_id, txc)\
75 	safe_clock_adjtime(__FILE__, __LINE__, (clk_id), (txc))
76 
77 #endif /* SAFE_CLOCKS_H__ */
78