xref: /aosp_15_r20/external/ltp/include/old/test.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * Copyright (c) 2009-2013 Cyril Hrubis [email protected]
5  */
6 
7 #ifndef __TEST_H__
8 #define __TEST_H__
9 
10 #ifdef TST_TEST_H__
11 # error Newlib tst_test.h already included
12 #endif /* TST_TEST_H__ */
13 
14 #include <stdio.h>
15 #include <signal.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 
21 #include "usctest.h"
22 
23 #include "tst_common.h"
24 #include "old_safe_file_ops.h"
25 #include "old_checkpoint.h"
26 #include "tst_process_state.h"
27 #include "old_resource.h"
28 #include "tst_res_flags.h"
29 #include "tst_kvercmp.h"
30 #include "tst_fs.h"
31 #include "tst_pid.h"
32 #include "tst_cmd.h"
33 #include "tst_cpu.h"
34 #include "tst_clone.h"
35 #include "old_device.h"
36 #include "old_tmpdir.h"
37 #include "tst_minmax.h"
38 #include "tst_get_bad_addr.h"
39 #include "tst_path_has_mnt_flags.h"
40 
41 /*
42  * Ensure that NUMSIGS is defined.
43  * It should be defined in signal.h or sys/signal.h on
44  * UNICOS/mk and IRIX systems.   On UNICOS systems,
45  * it is not defined, thus it is being set to UNICOS's NSIG.
46  * Note:  IRIX's NSIG (signals are 1-(NSIG-1))
47  *      is not same meaning as UNICOS/UMK's NSIG  (signals 1-NSIG)
48  */
49 #ifndef NUMSIGS
50 #define NUMSIGS NSIG
51 #endif
52 
53 
54 /* defines for unexpected signal setup routine (set_usig.c) */
55 #define FORK    1		/* SIGCHLD is to be ignored */
56 #define NOFORK  0		/* SIGCHLD is to be caught */
57 #define DEF_HANDLER SIG_ERR	/* tells set_usig() to use default signal handler */
58 
59 /*
60  * The following defines are used to control tst_res and t_result reporting.
61  */
62 
63 #define TOUTPUT	   "TOUTPUT"		/* The name of the environment variable */
64 					/* that can be set to one of the following */
65 					/* strings to control tst_res output */
66 					/* If not set, TOUT_VERBOSE_S is assumed */
67 
68 /*
69  * Macro to use for making functions called only once in
70  * multi-threaded tests such as init or cleanup function.
71  * The first call to @name_fn function by any thread shall
72  * call the @exec_fn. Subsequent calls shall not call @exec_fn.
73  * *_fn functions must not take any arguments.
74  */
75 #define TST_DECLARE_ONCE_FN(name_fn, exec_fn)				\
76 	void name_fn(void)						\
77 	{								\
78 		static pthread_once_t ltp_once = PTHREAD_ONCE_INIT;	\
79 		pthread_once(&ltp_once, exec_fn);			\
80 	}
81 
82 /*
83  * lib/forker.c
84  */
85 extern int Forker_pids[];
86 extern int Forker_npids;
87 
88 typedef struct {
89 	char *option;	/* Valid option string (one option only) like "a:"  */
90 	int  *flag;	/* Pointer to location to set true if option given  */
91 	char **arg;	/* Pointer to location to place argument, if needed */
92 } option_t;
93 
94 /* lib/tst_parse_opts.c */
95 void tst_parse_opts(int argc, char *argv[], const option_t *user_optarg,
96                     void (*user_help)(void));
97 
98 /* lib/tst_res.c */
99 const char *strttype(int ttype);
100 
101 void tst_resm_(const char *file, const int lineno, int ttype,
102 	const char *arg_fmt, ...)
103 	__attribute__ ((format (printf, 4, 5)));
104 #define tst_resm(ttype, arg_fmt, ...) \
105 	tst_resm_(__FILE__, __LINE__, (ttype), \
106 		  (arg_fmt), ##__VA_ARGS__)
107 
108 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
109 	const void *buf, size_t size, const char *arg_fmt, ...)
110 	__attribute__ ((format (printf, 6, 7)));
111 #define tst_resm_hexd(ttype, buf, size, arg_fmt, ...) \
112 	tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
113 		       (arg_fmt), ##__VA_ARGS__)
114 
115 void tst_brkm__(const char *file, const int lineno, int ttype,
116 	void (*func)(void), const char *arg_fmt, ...)
117 	__attribute__ ((format (printf, 5, 6))) LTP_ATTRIBUTE_NORETURN;
118 
119 #ifdef LTPLIB
120 # include "ltp_priv.h"
121 # define tst_brkm(flags, cleanup, fmt, ...) do { \
122 	if (tst_test) \
123 		tst_brk_(__FILE__, __LINE__, flags, fmt, ##__VA_ARGS__); \
124 	else \
125 		tst_brkm__(__FILE__, __LINE__, flags, cleanup, fmt, ##__VA_ARGS__); \
126 	} while (0)
127 
128 #define tst_brkm_(file, lineno, flags, cleanup, fmt, ...) do { \
129 	if (tst_test) \
130 		tst_brk_(file, lineno, flags, fmt, ##__VA_ARGS__); \
131 	else \
132 		tst_brkm__(file, lineno, flags, cleanup, fmt, ##__VA_ARGS__); \
133 	} while (0)
134 #else
135 # define tst_brkm(flags, cleanup, fmt, ...) do { \
136 		tst_brkm__(__FILE__, __LINE__, flags, cleanup, fmt, ##__VA_ARGS__); \
137 	} while (0)
138 #endif
139 
140 void tst_require_root(void);
141 void tst_exit(void) LTP_ATTRIBUTE_NORETURN;
142 void tst_old_flush(void);
143 
144 /*
145  * tst_old_flush() + fork
146  * NOTE: tst_fork() will reset T_exitval to 0 for child process.
147  */
148 pid_t tst_fork(void);
149 
150 /* lib/tst_res.c */
151 /*
152  * In case we need do real test work in child process parent process can use
153  * tst_record_childstatus() to make child process's test results propagated to
154  * parent process correctly.
155  *
156  * The child can use tst_resm(), tst_brkm() followed by the tst_exit() or
157  * plain old exit() (with TPASS, TFAIL and TBROK).
158  *
159  * WARNING: Be wary that the child cleanup function passed to tst_brkm()
160  *          must clean only resources the child has allocated. E.g. the
161  *          child cleanup is different function from the parent cleanup.
162  */
163 void tst_record_childstatus(void (*cleanup)(void), pid_t child);
164 
165 extern int tst_count;
166 
167 /* lib/tst_sig.c */
168 void tst_sig(int fork_flag, void (*handler)(), void (*cleanup)());
169 
170 /* lib/tst_mkfs.c
171  *
172  * @dev: path to a device
173  * @fs_type: filesystem type
174  * @fs_opts: NULL or NULL terminated array of mkfs options
175  * @extra_opts: NULL or NULL terminated array of extra mkfs options which are
176  * passed after the device name.
177  */
178 #define tst_mkfs(cleanup, dev, fs_type, fs_opts, extra_opts) \
179 	tst_mkfs_(__FILE__, __LINE__, cleanup, dev, fs_type, \
180 		  fs_opts, extra_opts)
181 void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
182 	       const char *dev, const char *fs_type,
183 	       const char *const fs_opts[], const char *const extra_opts[]);
184 
185 /* lib/tst_res.c
186  * tst_strsig converts signal's value to corresponding string.
187  * tst_strerrno converts errno to corresponding string.
188  */
189 const char *tst_strsig(int sig);
190 const char *tst_strerrno(int err);
191 
192 #ifdef TST_USE_COMPAT16_SYSCALL
193 #define TCID_BIT_SUFFIX "_16"
194 #elif  TST_USE_NEWER64_SYSCALL
195 #define TCID_BIT_SUFFIX "_64"
196 #else
197 #define TCID_BIT_SUFFIX ""
198 #endif
199 #define TCID_DEFINE(ID) char *TCID = (#ID TCID_BIT_SUFFIX)
200 
201 #endif	/* __TEST_H__ */
202