1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
4 * Copyright (c) Linux Test Project, 2016-2019
5 */
6
7 #ifndef TST_TEST_H__
8 #define TST_TEST_H__
9
10 #ifdef __TEST_H__
11 # error Oldlib test.h already included
12 #endif /* __TEST_H__ */
13
14 #include <unistd.h>
15 #include <limits.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include "tst_common.h"
20 #include "tst_res_flags.h"
21 #include "tst_checkpoint.h"
22 #include "tst_device.h"
23 #include "tst_mkfs.h"
24 #include "tst_fs.h"
25 #include "tst_pid.h"
26 #include "tst_cmd.h"
27 #include "tst_cpu.h"
28 #include "tst_process_state.h"
29 #include "tst_atomic.h"
30 #include "tst_kvercmp.h"
31 #include "tst_clone.h"
32 #include "tst_kernel.h"
33 #include "tst_minmax.h"
34 #include "tst_get_bad_addr.h"
35 #include "tst_path_has_mnt_flags.h"
36 #include "tst_sys_conf.h"
37 #include "tst_coredump.h"
38 #include "tst_buffers.h"
39 #include "tst_capability.h"
40
41 /*
42 * Reports testcase result.
43 */
44 void tst_res_(const char *file, const int lineno, int ttype,
45 const char *fmt, ...)
46 __attribute__ ((format (printf, 4, 5)));
47
48 #define tst_res(ttype, arg_fmt, ...) \
49 tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
50
51 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
52 const void *buf, size_t size, const char *arg_fmt, ...)
53 __attribute__ ((format (printf, 6, 7)));
54
55 #define tst_res_hexd(ttype, buf, size, arg_fmt, ...) \
56 tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
57 (arg_fmt), ##__VA_ARGS__)
58
59 /*
60 * Reports result and exits a test.
61 */
62 void tst_brk_(const char *file, const int lineno, int ttype,
63 const char *fmt, ...)
64 __attribute__ ((format (printf, 4, 5)));
65
66 #define tst_brk(ttype, arg_fmt, ...) \
67 ({ \
68 TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(!((ttype) & \
69 (TBROK | TCONF | TFAIL))); \
70 tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\
71 })
72
73 /* flush stderr and stdout */
74 void tst_flush(void);
75
76 pid_t safe_fork(const char *filename, unsigned int lineno);
77 #define SAFE_FORK() \
78 safe_fork(__FILE__, __LINE__)
79
80 #define TST_TRACE(expr) \
81 ({int ret = expr; \
82 ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \
83
84 #include "tst_safe_macros.h"
85 #include "tst_safe_file_ops.h"
86 #include "tst_safe_net.h"
87
88 /*
89 * Wait for all children and exit with TBROK if
90 * any of them returned a non-zero exit status.
91 */
92 void tst_reap_children(void);
93
94 struct tst_option {
95 char *optstr;
96 char **arg;
97 char *help;
98 };
99
100 /*
101 * Options parsing helpers.
102 *
103 * If str is NULL these are No-op.
104 *
105 * On failure non-zero (errno) is returned.
106 */
107 int tst_parse_int(const char *str, int *val, int min, int max);
108 int tst_parse_long(const char *str, long *val, long min, long max);
109 int tst_parse_float(const char *str, float *val, float min, float max);
110
111 struct tst_tag {
112 const char *name;
113 const char *value;
114 };
115
116 extern unsigned int tst_variant;
117
118 struct tst_test {
119 /* number of tests available in test() function */
120 unsigned int tcnt;
121
122 struct tst_option *options;
123
124 const char *min_kver;
125
126 /* If set the test is compiled out */
127 const char *tconf_msg;
128
129 int needs_tmpdir:1;
130 int needs_root:1;
131 int forks_child:1;
132 int needs_device:1;
133 int needs_checkpoints:1;
134 int needs_overlay:1;
135 int format_device:1;
136 int mount_device:1;
137 int needs_rofs:1;
138 int child_needs_reinit:1;
139 int needs_devfs:1;
140 int restore_wallclock:1;
141 /*
142 * If set the test function will be executed for all available
143 * filesystems and the current filesytem type would be set in the
144 * tst_device->fs_type.
145 *
146 * The test setup and cleanup are executed before/after __EACH__ call
147 * to the test function.
148 */
149 int all_filesystems:1;
150
151 /*
152 * If set non-zero denotes number of test variant, the test is executed
153 * variants times each time with tst_variant set to different number.
154 *
155 * This allows us to run the same test for different settings. The
156 * intended use is to test different syscall wrappers/variants but the
157 * API is generic and does not limit the usage in any way.
158 */
159 unsigned int test_variants;
160
161 /* Minimal device size in megabytes */
162 unsigned int dev_min_size;
163
164 /* Device filesystem type override NULL == default */
165 const char *dev_fs_type;
166 /* Flags to be passed to tst_get_supported_fs_types() */
167 int dev_fs_flags;
168
169 /* Options passed to SAFE_MKFS() when format_device is set */
170 const char *const *dev_fs_opts;
171 const char *const *dev_extra_opts;
172
173 /* Device mount options, used if mount_device is set */
174 const char *mntpoint;
175 unsigned int mnt_flags;
176 void *mnt_data;
177
178 /* override default timeout per test run, disabled == -1 */
179 int timeout;
180
181 void (*setup)(void);
182 void (*cleanup)(void);
183
184 void (*test)(unsigned int test_nr);
185 void (*test_all)(void);
186
187 /* Syscall name used by the timer measurement library */
188 const char *scall;
189
190 /* Sampling function for timer measurement testcases */
191 int (*sample)(int clk_id, long long usec);
192
193 /* NULL terminated array of resource file names */
194 const char *const *resource_files;
195
196 /* NULL terminated array of needed kernel drivers */
197 const char * const *needs_drivers;
198
199 /*
200 * NULL terminated array of (/proc, /sys) files to save
201 * before setup and restore after cleanup
202 */
203 const char * const *save_restore;
204
205 /*
206 * NULL terminated array of kernel config options required for the
207 * test.
208 */
209 const char *const *needs_kconfigs;
210
211 /*
212 * NULL-terminated array to be allocated buffers.
213 */
214 struct tst_buffers *bufs;
215
216 /*
217 * NULL-terminated array of capability settings
218 */
219 struct tst_cap *caps;
220
221 /*
222 * {NULL, NULL} terminated array of tags.
223 */
224 const struct tst_tag *tags;
225 };
226
227 /*
228 * Runs tests.
229 */
230 void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
231 __attribute__ ((noreturn));
232
233 /*
234 * Does library initialization for child processes started by exec()
235 *
236 * The LTP_IPC_PATH variable must be passed to the program environment.
237 */
238 void tst_reinit(void);
239
240 //TODO Clean?
241 #define TEST(SCALL) \
242 do { \
243 errno = 0; \
244 TST_RET = SCALL; \
245 TST_ERR = errno; \
246 } while (0)
247
248 #define TEST_VOID(SCALL) \
249 do { \
250 errno = 0; \
251 SCALL; \
252 TST_ERR = errno; \
253 } while (0)
254
255 extern long TST_RET;
256 extern int TST_ERR;
257
258 extern void *TST_RET_PTR;
259
260 #define TESTPTR(SCALL) \
261 do { \
262 errno = 0; \
263 TST_RET_PTR = (void*)SCALL; \
264 TST_ERR = errno; \
265 } while (0)
266
267 /*
268 * Functions to convert ERRNO to its name and SIGNAL to its name.
269 */
270 const char *tst_strerrno(int err);
271 const char *tst_strsig(int sig);
272 /*
273 * Returns string describing status as returned by wait().
274 *
275 * BEWARE: Not thread safe.
276 */
277 const char *tst_strstatus(int status);
278
279 unsigned int tst_timeout_remaining(void);
280 unsigned int tst_multiply_timeout(unsigned int timeout);
281 void tst_set_timeout(int timeout);
282
283
284 /*
285 * Returns path to the test temporary directory in a newly allocated buffer.
286 */
287 char *tst_get_tmpdir(void);
288
289 #ifndef TST_NO_DEFAULT_MAIN
290
291 static struct tst_test test;
292
main(int argc,char * argv[])293 int main(int argc, char *argv[])
294 {
295 tst_run_tcases(argc, argv, &test);
296 }
297
298 #endif /* TST_NO_DEFAULT_MAIN */
299
300 #define TST_TEST_TCONF(message) \
301 static struct tst_test test = { .tconf_msg = message } \
302 /*
303 * This is a hack to make the testcases link without defining TCID
304 */
305 const char *TCID;
306
307 #endif /* TST_TEST_H__ */
308