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