• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2024
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 #include <sys/resource.h>
19 
20 #include "tst_common.h"
21 #include "tst_res_flags.h"
22 #include "tst_parse.h"
23 #include "tst_test_macros.h"
24 #include "tst_checkpoint.h"
25 #include "tst_device.h"
26 #include "tst_mkfs.h"
27 #include "tst_fs.h"
28 #include "tst_pid.h"
29 #include "tst_cmd.h"
30 #include "tst_cpu.h"
31 #include "tst_process_state.h"
32 #include "tst_atomic.h"
33 #include "tst_kvercmp.h"
34 #include "tst_kernel.h"
35 #include "tst_minmax.h"
36 #include "tst_get_bad_addr.h"
37 #include "tst_path_has_mnt_flags.h"
38 #include "tst_sys_conf.h"
39 #include "tst_coredump.h"
40 #include "tst_buffers.h"
41 #include "tst_capability.h"
42 #include "tst_hugepage.h"
43 #include "tst_assert.h"
44 #include "tst_security.h"
45 #include "tst_taint.h"
46 #include "tst_memutils.h"
47 #include "tst_arch.h"
48 #include "tst_fd.h"
49 #include "tst_tmpdir.h"
50 
51 void tst_res_(const char *file, const int lineno, int ttype,
52               const char *fmt, ...)
53               __attribute__ ((format (printf, 4, 5)));
54 
55 /**
56  * tst_res() - Reports a test result.
57  *
58  * @ttype: An enum tst_res_type.
59  * @arg_fmt: A printf-like format.
60  * @...: A printf-like parameters.
61  *
62  * This is the main test reporting function. Each time this function is called
63  * with one of TPASS, TFAIL, TCONF, TBROK or TWARN a counter in page of shared
64  * memory is incremented. This means that there is no need to propagate test
65  * results from children and that results are accounted for once this function
66  * returns. The counters are incremented atomically which makes this function
67  * thread-safe.
68  */
69 #define tst_res(ttype, arg_fmt, ...) \
70 	({									\
71 		TST_RES_SUPPORTS_TCONF_TDEBUG_TFAIL_TINFO_TPASS_TWARN(\
72 			!((TTYPE_RESULT(ttype) ?: TCONF) & \
73 			(TCONF | TDEBUG | TFAIL | TINFO | TPASS | TWARN)));				\
74 		tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\
75 	})
76 
77 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
78 	const void *buf, size_t size, const char *arg_fmt, ...)
79 	__attribute__ ((format (printf, 6, 7)));
80 /**
81  * tst_res_hexd() - Reports a test result along with hex dump of a buffer.
82  *
83  * This call is the same as tst_res() but includes a pointer and size of the
84  * buffer that is going to be printed in the output in a hexadecimal format.
85  *
86  * @ttype: An enum tst_res_type.
87  * @buf: A pointer to a buffer to print in hexadecimal format.
88  * @size: A size of the buffer.
89  * @arg_fmt: A printf-like format.
90  * @...: A printf-like parameters.
91  */
92 #define tst_res_hexd(ttype, buf, size, arg_fmt, ...) \
93 	tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
94 			(arg_fmt), ##__VA_ARGS__)
95 
96 void tst_brk_(const char *file, const int lineno, int ttype,
97               const char *fmt, ...)
98               __attribute__ ((format (printf, 4, 5)));
99 
100 /**
101  * tst_brk() - Reports a breakage and exits the test.
102  *
103  * @ttype: An enum tst_res_type.
104  * @arg_fmt: A printf-like format.
105  * @...: A printf-like parameters.
106  *
107  * Reports either TBROK or TCONF and exits the test immediately. When called
108  * all children in the same process group as the main test library process are
109  * killed. This function, unless in a test cleanup, calls _exit() and does not
110  * return.
111  *
112  * When test is in cleanup() function TBROK is converted into TWARN by the test
113  * library and we attempt to carry on with a cleanup even when tst_brk() was
114  * called. This makes it possible to use SAFE_FOO() macros in the test cleanup
115  * without interrupting the cleanup process on a failure.
116  */
117 #define tst_brk(ttype, arg_fmt, ...)						\
118 	({									\
119 		TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(!((ttype) &			\
120 			(TBROK | TCONF | TFAIL)));				\
121 		tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\
122 	})
123 
124 void tst_printf(const char *const fmt, ...)
125 		__attribute__((nonnull(1), format (printf, 1, 2)));
126 
127 /**
128  * tst_flush() - Flushes the output file streams.
129  *
130  * There are rare cases when we want to flush the output file streams
131  * explicitly, e.g. before we do an action that may crash the test to ensure
132  * that the messages have been written out.
133  *
134  * This is also called by the SAFE_FORK() because otherwise each child would
135  * end up with the same copy of the file in it's memory and any messages in
136  * buffers would be multiplied.
137  */
138 void tst_flush(void);
139 
140 pid_t safe_fork(const char *filename, unsigned int lineno);
141 /**
142  * SAFE_FORK() - Forks a test child.
143  *
144  * This call makes sure that output file streams are flushed and also handles
145  * errors from fork(). Use this instead of fork() whenever possible!
146  */
147 #define SAFE_FORK() \
148 	safe_fork(__FILE__, __LINE__)
149 
150 #define TST_TRACE(expr)	                                            \
151 	({int ret = expr;                                           \
152 	  ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \
153 
154 /**
155  * tst_strerrno() - Converts an errno number into a name.
156  *
157  * @err: An errno number.
158  * return: An errno name e.g. "EINVAL".
159  */
160 const char *tst_strerrno(int err);
161 
162 /**
163  * tst_strsig() - Converts a signal number into a name.
164  *
165  * @sig: A signal number.
166  * return: A signal name e.g. "SIGINT".
167  */
168 const char *tst_strsig(int sig);
169 
170 
171 /**
172  * tst_strstatus() - Returns string describing status as returned by wait().
173  *
174  * WARNING: Not thread safe.
175  *
176  * @status: A status as returned by wait()
177  * return: A string description for the status e.g. "killed by SIGKILL".
178  */
179 const char *tst_strstatus(int status);
180 
181 #include "tst_safe_macros.h"
182 #include "tst_safe_file_ops.h"
183 #include "tst_safe_net.h"
184 #include "tst_clone.h"
185 #include "tst_cgroup.h"
186 
187 /**
188  * tst_reap_children() - Waits for all child processes to exit.
189  *
190  * Wait for all children and exit with TBROK if any of them returned a non-zero
191  * exit status.
192  */
193 void tst_reap_children(void);
194 
195 /**
196  * struct tst_option - Test command line option.
197  *
198  * @optstr: A short command line option, e.g. "a" or "a:".
199  * @arg: A pointer to store the option value to.
200  * @help: A help string for the option displayed when test is passed '-h' on
201  *        the command-line.
202  */
203 struct tst_option {
204 	char *optstr;
205 	char **arg;
206 	char *help;
207 };
208 
209 /**
210  * struct tst_tag - A test tag.
211  *
212  * @name: A tag name.
213  * @value: A tag value.
214  *
215  * This structure is used to encode pointers to upstream commits in regression
216  * tests as well as CVE numbers or any additional useful hints.
217  *
218  * The content of these tags is printed by the test on a failure to help the
219  * testers with debugging.
220  *
221  * The supported tags are:
222  *
223  * - "linux-git" with first 12 numbers from an upstream kernel git hash.
224  * - "CVE" with a CVE number e.g. "2000-1234".
225  * - "glibc-git" with first 12 numbers from an upstream glibc git hash.
226  * - "musl-git" with first 12 numbers from an upstream musl git hash.
227  * - "known-fail" a message describing something that is supposed to work but
228  *   rather than that produces a longstanding failures.
229  */
230 struct tst_tag {
231 	const char *name;
232 	const char *value;
233 };
234 
235 extern unsigned int tst_variant;
236 
237 #define TST_UNLIMITED_TIMEOUT (-1)
238 
239 /**
240  * struct tst_ulimit_val - An ulimit resource and value.
241  *
242  * @resource: Which resource limits should be adjusted. See setrlimit(2) for
243  *            the list of the RLIMIT_* constants.
244  * @rlim_cur: A limit value.
245  */
246 struct tst_ulimit_val {
247 	int resource;
248 	rlim_t rlim_cur;
249 };
250 
251 /**
252  * struct tst_fs - A file system type, mkfs and mount options
253  *
254  * @type: A filesystem type to use.
255  *
256  * @mkfs_opts: A NULL terminated array of options passed to mkfs in the case
257  *             of 'tst_test.format_device'. These options are passed to mkfs
258  *             before the device path.
259  *
260  * @mkfs_size_opt: An option passed to mkfs in the case of
261  *                 'tst_test.format_device'. The device size in blocks is
262  *                 passed to mkfs after the device path and can be used to
263  *                 limit the file system not to use the whole block device.
264  *
265  * @mkfs_ver: mkfs tool version. The string format supports relational
266  *            operators such as < > <= >= ==.
267  *
268  * @mnt_flags: MS_* flags passed to mount(2) when the test library mounts a
269  *             device in the case of 'tst_test.mount_device'.
270  *
271  * @mnt_data: The data passed to mount(2) when the test library mounts a device
272  *            in the case of 'tst_test.mount_device'.
273  *
274  * @min_kver: A minimum kernel version supporting the filesystem which has been
275  *            created with mkfs.
276  */
277 struct tst_fs {
278 	const char *type;
279 
280 	const char *const *mkfs_opts;
281 	const char *mkfs_size_opt;
282 	const char *mkfs_ver;
283 
284 	unsigned int mnt_flags;
285 	const void *mnt_data;
286 
287 	const char *min_kver;
288 };
289 
290 /**
291  * struct tst_test - A test description.
292  *
293  * @tcnt: A number of tests. If set the test() callback is called tcnt times
294  *        and each time passed an increasing counter value.
295  * @options: An NULL optstr terminated array of struct tst_option.
296  *
297  * @min_kver: A minimal kernel version the test can run on. e.g. "3.10".
298  *
299  * @supported_archs: A NULL terminated array of architectures the test runs on
300  *                   e.g. {"x86_64, "x86", NULL}. Calls tst_is_on_arch() to
301  *                   check if current CPU architecture is supported and exits
302  *                   the test with TCONF if it's not.
303  *
304  * @tconf_msg: If set the test exits with TCONF right after entering the test
305  *             library. This is used by the TST_TEST_TCONF() macro to disable
306  *             tests at compile time.
307  *
308  * @needs_tmpdir: If set a temporary directory is prepared for the test inside
309  *                $TMPDIR and the test $CWD is set to point to it. The content
310  *                of the temporary directory is removed automatically after
311  *                the test is finished.
312  *
313  * @needs_root: If set the test exit with TCONF unless it's executed under root
314  *              user.
315  *
316  * @forks_child: Has to be set if the test intends to fork children.
317  *
318  * @needs_device: If set a block device is prepared for the test, the device
319  *                path and size are set in the struct tst_device variable
320  *                called tst_device. If $LTP_DEV variable exists in the test
321  *                environment the test attempts to use that device first and
322  *                only if that fails the test falls back to use loop devices.
323  *                This flag implies needs_tmpdir flag because loop device
324  *                backing files are created in the test temporary directory.
325  *
326  * @needs_checkpoints: Has to be set if the test wants to use checkpoint
327  *                     synchronization primitives.
328  *
329  * @needs_overlay: If set overlay file system is mounted on the top of the
330  *                 file system at tst_test.mntpoint.
331  *
332  * @format_device: Does all tst_test.needs_device would do and also formats
333  *                 the device with a file system as well.
334  *
335  * @mount_device: Does all tst_test.format_device would do and also mounts the
336  *                device at tst_test.mntpoint.
337  *
338  * @needs_rofs: If set a read-only file system is mounted at tst_test.mntpoint.
339  *
340  * @child_needs_reinit: Has to be set if the test needs to call tst_reinit()
341  *                      from a process started by exec().
342  *
343  * @runs_script: Implies child_needs_reinit and forks_child at the moment.
344  *
345  * @needs_devfs: If set the devfs is mounted at tst_test.mntpoint. This is
346  *               needed for tests that need to create device files since tmpfs
347  *               at /tmp is usually mounted with 'nodev' option.
348  *
349  * @restore_wallclock: Saves wall clock at the start of the test and restores
350  *                     it at the end with the help of monotonic timers.
351  *                     Testcases that modify system wallclock use this to
352  *                     restore the system to the previous state.
353  *
354  * @all_filesystems: If set the test is executed for all supported filesytems,
355  *                   i.e. file system that is supported by the kernel and has
356  *                   mkfs installed on the system.The file system is mounted at
357  *                   tst_test.mntpoint and file system details, e.g. type are set
358  *                   in the struct tst_device. Each execution is independent,
359  *                   that means that for each iteration tst_test.setup() is
360  *                   called at the test start and tst_test.cleanup() is called
361  *                   at the end and tst_brk() only exits test for a single
362  *                   file system. That especially means that calling
363  *                   tst_brk(TCONF, ...) in the test setup will skip the
364  *                   current file system.
365  *
366  * @skip_in_lockdown: Skip the test if kernel lockdown is enabled.
367  *
368  * @skip_in_secureboot: Skip the test if secureboot is enabled.
369  *
370  * @skip_in_compat: Skip the test if we are executing 32bit binary on a 64bit
371  *                  kernel, i.e. we are testing the kernel compat layer.
372  *
373  * @needs_abi_bits: Skip the test if runs on a different kernel ABI than
374  *                  requested (on 32bit instead of 64bit or vice versa).
375  *                  Possible values: 32, 64.
376  *
377  * @needs_hugetlbfs: If set hugetlbfs is mounted at tst_test.mntpoint.
378  *
379  * @skip_filesystems: A NULL terminated array of unsupported file systems. The
380  *                    test reports TCONF if the file system to be tested is
381  *                    present in the array. This is especially useful to filter
382  *                    out unsupported file system when tst_test.all_filesystems
383  *                    is enabled.
384  *
385  * @min_cpus: Minimal number of online CPUs the test needs to run.
386  *
387  * @min_mem_avail: Minimal amount of available RAM memory in megabytes required
388  *                 for the test to run.
389  *
390  * @min_swap_avail: Minimal amount of available swap memory in megabytes
391  *                  required for the test to run.
392  *
393  * @hugepages: An interface to reserve hugepages prior running the test.
394  *             Request how many hugepages should be reserved in the global
395  *             pool and also if having hugepages is required for the test run
396  *             or not, i.e. if test should exit with TCONF if the requested
397  *             amount of hugepages cannot be reserved. If TST_REQUEST is set
398  *             the library will try it's best to reserve the hugepages and
399  *             return the number of available hugepages in tst_hugepages, which
400  *             may be 0 if there is no free memory or hugepages are not
401  *             supported at all. If TST_NEEDS the requested hugepages are
402  *             required for the test and the test exits if it couldn't be
403  *             required. It can also be used to disable hugepages by setting
404  *             .hugepages = {TST_NO_HUGEPAGES}. The test library restores the
405  *             original poll allocations after the test has finished.
406  *
407  * @taint_check: If set the test fails if kernel is tainted during the test run.
408  *               That means tst_taint_init() is called during the test setup
409  *               and tst_taint_check() at the end of the test. If all_filesystems
410  *               is set taint check will be performed after each iteration and
411  *               testing will be terminated by TBROK if taint is detected.
412  *
413  * @test_variants: If set denotes number of test variant, the test is executed
414  *                 variants times each time with tst_variant set to different
415  *                 number. This allows us to run the same test for different
416  *                 settings. The intended use is to test different syscall
417  *                 wrappers/variants but the API is generic and does not limit
418  *                 usage in any way.
419  *
420  * @dev_min_size: A minimal device size in megabytes.
421  *
422  * @filesystems: A NULL type terminated array of per file system type
423  *               parameters for mkfs and mount. If the first entry type is NULL
424  *               it describes a default parameters for all file system tests.
425  *               The rest of the entries the describes per file system type
426  *               parameters. If tst_test.all_filesystems is set, the test runs
427  *               for all filesystems and uses the array to lookup the mkfs
428  *               and mount options. If tst_test.all_filesystems is not set
429  *               the test iterates over file system types defined in the array.
430  *               If there is only a single entry in the array with a NULL type,
431  *               the test runs just once for the default file sytem i.e.
432  *               $TST_FS_TYPE.
433  *
434  * @mntpoint: A mount point where the test library mounts requested file system.
435  *            The directory is created by the library, the test must not create
436  *            it itself.
437  *
438  * @timeout: Maximum allowable time in seconds for the entire duration of a test.
439  *           By default, the timeout limits the total time for setup, single test
440  *           function invocation, and cleanup phases. However, if .runtime is
441  *           explicitly set and tst_remaining_runtime() is used in the test's
442  *           main loop, the timeout then applies only to the setup and cleanup
443  *           phases, as the runtime separately limits the main test execution.
444  *           This ensures the test does not hang indefinitely, in the rare case
445  *           that the test timeout cannot be accurately determined, it can be
446  *           set to a sufficiently large value or TST_UNLIMITED_TIMEOUT to remove
447  *           the limit.
448  *
449  * @runtime: Maximum runtime in seconds for the test's main execution loop.
450  *           This should be set for tests that are expected to run longer
451  *           than a few seconds and call tst_remaining_runtime() in their
452  *           main loop to exit gracefully when the runtime is exceeded.
453  *           Tests may finish sooner if their task completes (e.g., reaching
454  *           a requested number of iterations) before the runtime runs out.
455  *           The runtime is fixed and does not scale with timeout multipliers
456  *           (e.g., TIMEOUT_MUL), ensuring consistent test duration across
457  *           different environments (e.g., debug vs. stock kernels).
458  *
459  * @setup: Setup callback is called once at the start of the test in order to
460  *         prepare the test environment.
461  *
462  * @cleanup: Cleanup callback is either called at the end of the test, or in a
463  *           case that tst_brk() was called. That means that cleanup must be
464  *           able to handle all possible states the test can be in. This
465  *           usually means that we have to check if file descriptor was opened
466  *           before we attempt to close it, etc.
467  *
468  *
469  * @test: A main test function, only one of the tst_test.test and test_all can
470  *        be set. When this function is set the tst_test.tcnt must be set to a
471  *        positive integer and this function will be executed tcnt times
472  *        during a single test iteration. May be executed several times if test
473  *        was passed '-i' or '-d' command line parameters.
474  *
475  * @test_all: A main test function, only one of the tst_test.test and test_all
476  *            can be set. May be executed several times if test was passed '-i'
477  *            or '-d' command line parameters.
478  *
479  * @scall: Internal only (timer measurement library).
480  *
481  * @sample: Internal only (timer measurement library).
482  *
483  * @resource_files: A NULL terminated array of filenames that will be copied
484  *                  to the test temporary directory from the LTP datafiles
485  *                  directory.
486  *
487  * @needs_drivers: A NULL terminated array of kernel modules required to run
488  *                 the test. The module has to be build in or present in order
489  *                 for the test to run.
490  *
491  * @save_restore: A {} terminated array of /proc or /sys files that should
492  *                saved at the start of the test and restored at the end. See
493  *                tst_sys_conf_save() and struct tst_path_val for details.
494  *
495  * @ulimit: A {} terminated array of process limits RLIMIT_* to be adjusted for
496  *          the test.
497  *
498  * @needs_kconfigs: A NULL terminated array of kernel config options that are
499  *                  required for the test. All strings in the array have to be
500  *                  evaluated to true for the test to run. Boolean operators
501  *                  and parenthesis are supported, e.g.
502  *                  "CONFIG_X86_INTEL_UMIP=y | CONFIG_X86_UIMP=y" is evaluated
503  *                  to true if at least one of the options is present.
504  *
505  * @bufs: A description of guarded buffers to be allocated for the test. Guarded
506  *        buffers are buffers with poisoned page allocated right before the start
507  *        of the buffer and canary right after the end of the buffer. See
508  *        struct tst_buffers and tst_buffer_alloc() for details.
509  *
510  * @caps: A {} terminated array of capabilities to change before the start of
511  *        the test. See struct tst_cap and tst_cap_setup() for details.
512  *
513  * @tags: A {} terminated array of test tags. See struct tst_tag for details.
514  *
515  * @needs_cmds: A NULL terminated array of commands required for the test to run.
516  *
517  * @needs_cgroup_ver: If set the test will run only if the specified cgroup
518  *                    version is present on the system.
519  *
520  * @needs_cgroup_ctrls: A {} terminated array of cgroup controllers the test
521  *                      needs to run.
522  *
523  * @needs_cgroup_nsdelegate: If set test the will run only if cgroup2 is mounted
524  *                           with nsdelegate option.
525  */
526 
527  struct tst_test {
528 	unsigned int tcnt;
529 
530 	struct tst_option *options;
531 
532 	const char *min_kver;
533 
534 	const char *const *supported_archs;
535 
536 	const char *tconf_msg;
537 
538 	unsigned int needs_tmpdir:1;
539 	unsigned int needs_root:1;
540 	unsigned int forks_child:1;
541 	unsigned int needs_device:1;
542 	unsigned int needs_checkpoints:1;
543 	unsigned int needs_overlay:1;
544 	unsigned int format_device:1;
545 	unsigned int mount_device:1;
546 	unsigned int needs_rofs:1;
547 	unsigned int child_needs_reinit:1;
548 	unsigned int runs_script:1;
549 	unsigned int needs_devfs:1;
550 	unsigned int restore_wallclock:1;
551 
552 	unsigned int all_filesystems:1;
553 
554 	unsigned int skip_in_lockdown:1;
555 	unsigned int skip_in_secureboot:1;
556 	unsigned int skip_in_compat:1;
557 
558 
559 	int needs_abi_bits;
560 
561 	unsigned int needs_hugetlbfs:1;
562 
563 	const char *const *skip_filesystems;
564 
565 	unsigned long min_cpus;
566 	unsigned long min_mem_avail;
567 	unsigned long min_swap_avail;
568 
569 	struct tst_hugepage hugepages;
570 
571 	unsigned int taint_check;
572 
573 	unsigned int test_variants;
574 
575 	unsigned int dev_min_size;
576 
577 	struct tst_fs *filesystems;
578 
579 	const char *mntpoint;
580 
581 	int timeout;
582 	int runtime;
583 
584 	void (*setup)(void);
585 	void (*cleanup)(void);
586 	void (*test)(unsigned int test_nr);
587 	void (*test_all)(void);
588 
589 	const char *scall;
590 	int (*sample)(int clk_id, long long usec);
591 
592 	const char *const *resource_files;
593 	const char * const *needs_drivers;
594 
595 	const struct tst_path_val *save_restore;
596 
597 	const struct tst_ulimit_val *ulimit;
598 
599 	const char *const *needs_kconfigs;
600 
601 	struct tst_buffers *bufs;
602 
603 	struct tst_cap *caps;
604 
605 	const struct tst_tag *tags;
606 
607 	const char *const *needs_cmds;
608 
609 	const enum tst_cg_ver needs_cgroup_ver;
610 
611 	const char *const *needs_cgroup_ctrls;
612 
613 	unsigned int needs_cgroup_nsdelegate:1;
614 };
615 
616 /**
617  * tst_run_tcases() - Entry point to the test library.
618  *
619  * @argc: An argc that was passed to the main().
620  * @argv: An argv that was passed to the main().
621  * @self: The test description and callbacks packed in the struct tst_test
622  *        structure.
623  *
624  * A main() function which calls this function is added to each test
625  * automatically by including the tst_test.h header. The test has to define the
626  * struct tst_test called test.
627  *
628  * This function does not return, i.e. calls exit() after the test has finished.
629  */
630 void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
631                     __attribute__ ((noreturn));
632 
633 #define IPC_ENV_VAR "LTP_IPC_PATH"
634 
635 /**
636  * tst_reinit() - Reinitialize the test library.
637  *
638  * In a cases where a test child process calls exec() it no longer can access
639  * the test library shared memory and therefore use the test reporting
640  * functions, checkpoint library, etc. This function re-initializes the test
641  * library so that it can be used again.
642  *
643  * @important The LTP_IPC_PATH variable must be passed to the program environment.
644  */
645 void tst_reinit(void);
646 
647 /**
648  * tst_run_script() - Prepare the environment and execute a (shell) script.
649  *
650  * @script_name: A filename of the script.
651  * @params: A NULL terminated array of (shell) script parameters, pass NULL if
652  *          none are needed. This what is passed starting from argv[1].
653  *
654  * The (shell) script is executed with LTP_IPC_PATH in environment so that the
655  * binary helpers such as tst_res_ or tst_checkpoint work properly when executed
656  * from the script. This also means that the tst_test.runs_script flag needs to
657  * be set.
658  *
659  * A shell script has to source the tst_env.sh shell script at the start and
660  * after that it's free to use tst_res in the same way C code would use.
661  *
662  * Example shell script that reports success::
663  *
664  *   #!/bin/sh
665  *   . tst_env.sh
666  *
667  *   tst_res TPASS "Example test works"
668  *
669  * The call returns a pid in a case that you want to examine the return value
670  * of the script yourself. If you do not need to check the return value
671  * yourself you can use tst_reap_children() to wait for the completion. Or let
672  * the test library collect the child automatically, just be wary that the
673  * script and the test both runs concurently at the same time in this case.
674  *
675  * Return: A pid of the (shell) script process.
676  */
677 int tst_run_script(const char *script_name, char *const params[]);
678 
679 /*
680  * Sets entire timeout in seconds.
681  */
682 void tst_set_timeout(int timeout);
683 
684 unsigned int tst_multiply_timeout(unsigned int timeout);
685 
686 /*
687  * Returns remaining test runtime. Test that runs for more than a few seconds
688  * should check if they should exit by calling this function regularly.
689  *
690  * The function returns remaining runtime in seconds. If runtime was used up
691  * zero is returned.
692  */
693 unsigned int tst_remaining_runtime(void);
694 
695 /*
696  * Sets maximal test runtime in seconds.
697  */
698 void tst_set_runtime(int runtime);
699 
700 /*
701  * Create and open a random file inside the given dir path.
702  * It unlinks the file after opening and return file descriptor.
703  */
704 int tst_creat_unlinked(const char *path, int flags, mode_t mode);
705 
706 /*
707  * Returns path to the test temporary directory root (TMPDIR).
708  */
709 const char *tst_get_tmpdir_root(void);
710 
711 /*
712  * Validates exit status of child processes
713  */
714 int tst_validate_children_(const char *file, const int lineno,
715 	unsigned int count);
716 #define tst_validate_children(child_count) \
717 	tst_validate_children_(__FILE__, __LINE__, (child_count))
718 
719 #ifndef TST_NO_DEFAULT_MAIN
720 
721 static struct tst_test test;
722 
main(int argc,char * argv[])723 int main(int argc, char *argv[])
724 {
725 	tst_run_tcases(argc, argv, &test);
726 }
727 
728 #endif /* TST_NO_DEFAULT_MAIN */
729 
730 /**
731  * TST_TEST_TCONF() - Exit tests with a TCONF message.
732  *
733  * @message: Error message (the reason to skip test).
734  *
735  * This macro is used in test that couldn't be compiled either because current
736  * CPU architecture is unsupported or because of missing development libraries.
737  */
738 #define TST_TEST_TCONF(message)                                 \
739         static struct tst_test test = { .tconf_msg = message  } \
740 
741 #endif	/* TST_TEST_H__ */
742