summaryrefslogtreecommitdiffstats
path: root/kernel/tests/include/tst_safe_clocks.h
blob: 5909f40836fa410195a9283ccd29c650d0e0ed9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2019, Linux Test Project
 * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
 * Email : code@zilogic.com
 */

#ifndef TST_SAFE_CLOCKS_H__
#define TST_SAFE_CLOCKS_H__

#include <time.h>
#include <sys/timex.h>
#include "tst_test.h"
#include "tst_clocks.h"
#include "lapi/syscalls.h"
#include "lapi/posix_clocks.h"

static inline void safe_clock_getres(const char *file, const int lineno,
	clockid_t clk_id, struct timespec *res)
{
	int rval;

	rval = clock_getres(clk_id, res);
	if (rval != 0) {
		tst_brk(TBROK | TERRNO,
			"%s:%d clock_getres(%s) failed",
			file, lineno, tst_clock_name(clk_id));
	}
}

static inline void safe_clock_gettime(const char *file, const int lineno,
	clockid_t clk_id, struct timespec *tp)
{
	int rval;

	rval = clock_gettime(clk_id, tp);
	if (rval != 0) {
		tst_brk(TBROK | TERRNO,
			"%s:%d clock_gettime(%s) failed",
			file, lineno, tst_clock_name(clk_id));
	}
}


static inline void safe_clock_settime(const char *file, const int lineno,
	clockid_t clk_id, struct timespec *tp)
{
	int rval;

	rval = clock_settime(clk_id, tp);
	if (rval != 0) {
		tst_brk(TBROK | TERRNO,
			"%s:%d clock_gettime(%s) failed",
			file, lineno, tst_clock_name(clk_id));
	}
}

static inline int safe_timer_create(const char *file, const int lineno,
	clockid_t clockid, struct sigevent *sevp, timer_t *timerid)
{
	int ret;

	errno = 0;
	ret = timer_create(clockid, sevp, timerid);

	if (ret == -1) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"timer_create(%s) failed", tst_clock_name(clockid));
	} else if (ret) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"Invalid timer_create(%s) return value %d",
			tst_clock_name(clockid), ret);
	}

	return ret;
}

static inline int safe_timer_settime(const char *file, const int lineno,
	timer_t timerid, int flags, const struct itimerspec *new_value,
	struct itimerspec *old_value)
{
	int ret;

	errno = 0;
	ret = timer_settime(timerid, flags, new_value, old_value);

	if (ret == -1) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"timer_settime() failed");
	} else if (ret) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"Invalid timer_settime() return value %d", ret);
	}

	return ret;
}

static inline int safe_timer_gettime(const char *file, const int lineno,
	timer_t timerid, struct itimerspec *curr_value)
{
	int ret;

	errno = 0;
	ret = timer_gettime(timerid, curr_value);

	if (ret == -1) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"timer_gettime() failed");
	} else if (ret) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"Invalid timer_gettime() return value %d", ret);
	}

	return ret;
}

static inline int safe_timer_delete(const char *file, const int lineno,
	timer_t timerid)
{
	int ret;

	errno = 0;
	ret = timer_delete(timerid);

	if (ret == -1) {
		tst_brk_(file, lineno, TBROK | TERRNO, "timer_delete() failed");
	} else if (ret) {
		tst_brk_(file, lineno, TBROK | TERRNO,
			"Invalid timer_delete() return value %d", ret);
	}

	return ret;
}

#define SAFE_CLOCK_GETRES(clk_id, res)\
	safe_clock_getres(__FILE__, __LINE__, (clk_id), (res))

#define SAFE_CLOCK_GETTIME(clk_id, tp)\
	safe_clock_gettime(__FILE__, __LINE__, (clk_id), (tp))

#define SAFE_CLOCK_SETTIME(clk_id, tp)\
	safe_clock_settime(__FILE__, __LINE__, (clk_id), (tp))

#define SAFE_TIMER_CREATE(clockid, sevp, timerid)\
	safe_timer_create(__FILE__, __LINE__, (clockid), (sevp), (timerid))

#define SAFE_TIMER_SETTIME(timerid, flags, new_value, old_value)\
	safe_timer_settime(__FILE__, __LINE__, (timerid), (flags),\
		(new_value), (old_value))

#define SAFE_TIMER_GETTIME(timerid, curr_value)\
	safe_timer_gettime(__FILE__, __LINE__, (timerid), (curr_value))

#define SAFE_TIMER_DELETE(timerid)\
	safe_timer_delete(__FILE__, __LINE__, timerid)

#endif /* SAFE_CLOCKS_H__ */