1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. 4 * Copyright (c) 2009-2013 Cyril Hrubis chrubis@suse.cz 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 * fork() can't be used on uClinux systems, so use FORK_OR_VFORK instead, 70 * which will run vfork() on uClinux. 71 * mmap() doesn't support MAP_PRIVATE on uClinux systems, so use 72 * MAP_PRIVATE_EXCEPT_UCLINUX instead, which will skip the option on uClinux. 73 * If MAP_PRIVATE really is required, the test can not be run on uClinux. 74 */ 75 #ifdef UCLINUX 76 # define FORK_OR_VFORK tst_vfork 77 # define MAP_PRIVATE_EXCEPT_UCLINUX 0 78 /* tst_old_flush() + vfork() */ 79 pid_t tst_vfork(void); 80 #else 81 # define FORK_OR_VFORK tst_fork 82 # define MAP_PRIVATE_EXCEPT_UCLINUX MAP_PRIVATE 83 #endif 84 85 /* 86 * Macro to use for making functions called only once in 87 * multi-threaded tests such as init or cleanup function. 88 * The first call to @name_fn function by any thread shall 89 * call the @exec_fn. Subsequent calls shall not call @exec_fn. 90 * *_fn functions must not take any arguments. 91 */ 92 #define TST_DECLARE_ONCE_FN(name_fn, exec_fn) \ 93 void name_fn(void) \ 94 { \ 95 static pthread_once_t ltp_once = PTHREAD_ONCE_INIT; \ 96 pthread_once(<p_once, exec_fn); \ 97 } 98 99 /* 100 * lib/forker.c 101 */ 102 extern int Forker_pids[]; 103 extern int Forker_npids; 104 105 typedef struct { 106 char *option; /* Valid option string (one option only) like "a:" */ 107 int *flag; /* Pointer to location to set true if option given */ 108 char **arg; /* Pointer to location to place argument, if needed */ 109 } option_t; 110 111 /* lib/tst_parse_opts.c */ 112 void tst_parse_opts(int argc, char *argv[], const option_t *user_optarg, 113 void (*user_help)(void)); 114 115 /* lib/tst_res.c */ 116 const char *strttype(int ttype); 117 118 void tst_resm_(const char *file, const int lineno, int ttype, 119 const char *arg_fmt, ...) 120 __attribute__ ((format (printf, 4, 5))); 121 #define tst_resm(ttype, arg_fmt, ...) \ 122 tst_resm_(__FILE__, __LINE__, (ttype), \ 123 (arg_fmt), ##__VA_ARGS__) 124 125 void tst_resm_hexd_(const char *file, const int lineno, int ttype, 126 const void *buf, size_t size, const char *arg_fmt, ...) 127 __attribute__ ((format (printf, 6, 7))); 128 #define tst_resm_hexd(ttype, buf, size, arg_fmt, ...) \ 129 tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \ 130 (arg_fmt), ##__VA_ARGS__) 131 132 void tst_brkm__(const char *file, const int lineno, int ttype, 133 void (*func)(void), const char *arg_fmt, ...) 134 __attribute__ ((format (printf, 5, 6))) LTP_ATTRIBUTE_NORETURN; 135 136 #ifdef LTPLIB 137 # include "ltp_priv.h" 138 # define tst_brkm(flags, cleanup, fmt, ...) do { \ 139 if (tst_test) \ 140 tst_brk_(__FILE__, __LINE__, flags, fmt, ##__VA_ARGS__); \ 141 else \ 142 tst_brkm__(__FILE__, __LINE__, flags, cleanup, fmt, ##__VA_ARGS__); \ 143 } while (0) 144 145 #define tst_brkm_(file, lineno, flags, cleanup, fmt, ...) do { \ 146 if (tst_test) \ 147 tst_brk_(file, lineno, flags, fmt, ##__VA_ARGS__); \ 148 else \ 149 tst_brkm__(file, lineno, flags, cleanup, fmt, ##__VA_ARGS__); \ 150 } while (0) 151 #else 152 # define tst_brkm(flags, cleanup, fmt, ...) do { \ 153 tst_brkm__(__FILE__, __LINE__, flags, cleanup, fmt, ##__VA_ARGS__); \ 154 } while (0) 155 #endif 156 157 void tst_require_root(void); 158 void tst_exit(void) LTP_ATTRIBUTE_NORETURN; 159 void tst_old_flush(void); 160 161 /* 162 * tst_old_flush() + fork 163 * NOTE: tst_fork() will reset T_exitval to 0 for child process. 164 */ 165 pid_t tst_fork(void); 166 167 /* lib/tst_res.c */ 168 /* 169 * In case we need do real test work in child process parent process can use 170 * tst_record_childstatus() to make child process's test results propagated to 171 * parent process correctly. 172 * 173 * The child can use tst_resm(), tst_brkm() followed by the tst_exit() or 174 * plain old exit() (with TPASS, TFAIL and TBROK). 175 * 176 * WARNING: Be wary that the child cleanup function passed to tst_brkm() 177 * must clean only resources the child has allocated. E.g. the 178 * child cleanup is different function from the parent cleanup. 179 */ 180 void tst_record_childstatus(void (*cleanup)(void), pid_t child); 181 182 extern int tst_count; 183 184 /* lib/tst_sig.c */ 185 void tst_sig(int fork_flag, void (*handler)(), void (*cleanup)()); 186 187 /* lib/self_exec.c */ 188 void maybe_run_child(void (*child)(), const char *fmt, ...); 189 int self_exec(const char *argv0, const char *fmt, ...); 190 191 /* lib/tst_mkfs.c 192 * 193 * @dev: path to a device 194 * @fs_type: filesystem type 195 * @fs_opts: NULL or NULL terminated array of mkfs options 196 * @extra_opt: extra mkfs option which is passed after the device name 197 */ 198 #define tst_mkfs(cleanup, dev, fs_type, fs_opts, extra_opts) \ 199 tst_mkfs_(__FILE__, __LINE__, cleanup, dev, fs_type, \ 200 fs_opts, extra_opts) 201 void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void), 202 const char *dev, const char *fs_type, 203 const char *const fs_opts[], const char *const extra_opts[]); 204 205 /* lib/tst_res.c 206 * tst_strsig converts signal's value to corresponding string. 207 * tst_strerrno converts errno to corresponding string. 208 */ 209 const char *tst_strsig(int sig); 210 const char *tst_strerrno(int err); 211 212 #ifdef TST_USE_COMPAT16_SYSCALL 213 #define TCID_BIT_SUFFIX "_16" 214 #elif TST_USE_NEWER64_SYSCALL 215 #define TCID_BIT_SUFFIX "_64" 216 #else 217 #define TCID_BIT_SUFFIX "" 218 #endif 219 #define TCID_DEFINE(ID) char *TCID = (#ID TCID_BIT_SUFFIX) 220 221 #endif /* __TEST_H__ */ 222