• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * Copyright (c) 2009-2013 Cyril Hrubis chrubis@suse.cz
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * Further, this software is distributed without any warranty that it is
14  * free of the rightful claim of any third person regarding infringement
15  * or the like.  Any license provided herein, whether implied or
16  * otherwise, applies only to this software file.  Patent licenses, if
17  * any, provided herein do not apply to combinations of this program with
18  * other software, or any other product whatsoever.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25  * Mountain View, CA  94043, or:
26  *
27  * http://www.sgi.com
28  *
29  * For further information regarding this notice, see:
30  *
31  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32  */
33 
34 #ifndef __TEST_H__
35 #define __TEST_H__
36 
37 #ifdef TST_TEST_H__
38 # error Newlib tst_test.h already included
39 #endif /* TST_TEST_H__ */
40 
41 #include <stdio.h>
42 #include <signal.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdint.h>
47 
48 #include "usctest.h"
49 
50 #include "tst_common.h"
51 #include "old_safe_file_ops.h"
52 #include "old_checkpoint.h"
53 #include "tst_process_state.h"
54 #include "old_resource.h"
55 #include "tst_res_flags.h"
56 #include "tst_timer.h"
57 #include "tst_kvercmp.h"
58 #include "tst_fs.h"
59 #include "tst_pid.h"
60 #include "tst_cmd.h"
61 #include "tst_cpu.h"
62 #include "tst_clone.h"
63 #include "old_device.h"
64 #include "old_tmpdir.h"
65 
66 /*
67  * Ensure that NUMSIGS is defined.
68  * It should be defined in signal.h or sys/signal.h on
69  * UNICOS/mk and IRIX systems.   On UNICOS systems,
70  * it is not defined, thus it is being set to UNICOS's NSIG.
71  * Note:  IRIX's NSIG (signals are 1-(NSIG-1))
72  *      is not same meaning as UNICOS/UMK's NSIG  (signals 1-NSIG)
73  */
74 #ifndef NUMSIGS
75 #define NUMSIGS NSIG
76 #endif
77 
78 
79 /* defines for unexpected signal setup routine (set_usig.c) */
80 #define FORK    1		/* SIGCHLD is to be ignored */
81 #define NOFORK  0		/* SIGCHLD is to be caught */
82 #define DEF_HANDLER SIG_ERR	/* tells set_usig() to use default signal handler */
83 
84 /*
85  * The following defines are used to control tst_res and t_result reporting.
86  */
87 
88 #define TOUTPUT	   "TOUTPUT"		/* The name of the environment variable */
89 					/* that can be set to one of the following */
90 					/* strings to control tst_res output */
91 					/* If not set, TOUT_VERBOSE_S is assumed */
92 
93 /*
94  * fork() can't be used on uClinux systems, so use FORK_OR_VFORK instead,
95  * which will run vfork() on uClinux.
96  * mmap() doesn't support MAP_PRIVATE on uClinux systems, so use
97  * MAP_PRIVATE_EXCEPT_UCLINUX instead, which will skip the option on uClinux.
98  * If MAP_PRIVATE really is required, the test can not be run on uClinux.
99  */
100 #ifdef UCLINUX
101 # define FORK_OR_VFORK			tst_vfork
102 # define MAP_PRIVATE_EXCEPT_UCLINUX	0
103 /* tst_flush() + vfork() */
104 pid_t tst_vfork(void);
105 #else
106 # define FORK_OR_VFORK			tst_fork
107 # define MAP_PRIVATE_EXCEPT_UCLINUX	MAP_PRIVATE
108 #endif
109 
110 /*
111  * Macro to use for making functions called only once in
112  * multi-threaded tests such as init or cleanup function.
113  * The first call to @name_fn function by any thread shall
114  * call the @exec_fn. Subsequent calls shall not call @exec_fn.
115  * *_fn functions must not take any arguments.
116  */
117 #define TST_DECLARE_ONCE_FN(name_fn, exec_fn)				\
118 	void name_fn(void)						\
119 	{								\
120 		static pthread_once_t ltp_once = PTHREAD_ONCE_INIT;	\
121 		pthread_once(&ltp_once, exec_fn);			\
122 	}
123 
124 /*
125  * lib/forker.c
126  */
127 extern int Forker_pids[];
128 extern int Forker_npids;
129 
130 typedef struct {
131 	char *option;	/* Valid option string (one option only) like "a:"  */
132 	int  *flag;	/* Pointer to location to set true if option given  */
133 	char **arg;	/* Pointer to location to place argument, if needed */
134 } option_t;
135 
136 /* lib/tst_parse_opts.c */
137 void tst_parse_opts(int argc, char *argv[], const option_t *user_optarg,
138                     void (*user_help)(void));
139 
140 /* lib/tst_res.c */
141 const char *strttype(int ttype);
142 
143 void tst_resm_(const char *file, const int lineno, int ttype,
144 	const char *arg_fmt, ...)
145 	__attribute__ ((format (printf, 4, 5)));
146 #define tst_resm(ttype, arg_fmt, ...) \
147 	tst_resm_(__FILE__, __LINE__, (ttype), \
148 		  (arg_fmt), ##__VA_ARGS__)
149 
150 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
151 	const void *buf, size_t size, const char *arg_fmt, ...)
152 	__attribute__ ((format (printf, 6, 7)));
153 #define tst_resm_hexd(ttype, buf, size, arg_fmt, ...) \
154 	tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
155 		       (arg_fmt), ##__VA_ARGS__)
156 
157 void tst_brkm_(const char *file, const int lineno, int ttype,
158 	void (*func)(void), const char *arg_fmt, ...)
159 	__attribute__ ((format (printf, 5, 6))) LTP_ATTRIBUTE_NORETURN;
160 
161 #ifdef LTPLIB
162 # include "ltp_priv.h"
163 # define tst_brkm(flags, cleanup, fmt, ...) do { \
164 	if (tst_test) \
165 		tst_brk_(__FILE__, __LINE__, flags, fmt, ##__VA_ARGS__); \
166 	else \
167 		tst_brkm_(__FILE__, __LINE__, flags, cleanup, fmt, ##__VA_ARGS__); \
168 	} while (0)
169 #else
170 # define tst_brkm(flags, cleanup, fmt, ...) do { \
171 		tst_brkm_(__FILE__, __LINE__, flags, cleanup, fmt, ##__VA_ARGS__); \
172 	} while (0)
173 #endif
174 
175 void tst_require_root(void);
176 void tst_exit(void) LTP_ATTRIBUTE_NORETURN;
177 void tst_flush(void);
178 
179 /*
180  * tst_flush() + fork
181  * NOTE: tst_fork() will reset T_exitval to 0 for child process.
182  */
183 pid_t tst_fork(void);
184 
185 /* lib/tst_res.c */
186 /*
187  * In case we need do real test work in child process parent process can use
188  * tst_record_childstatus() to make child process's test results propagated to
189  * parent process correctly.
190  *
191  * The child can use tst_resm(), tst_brkm() followed by the tst_exit() or
192  * plain old exit() (with TPASS, TFAIL and TBROK).
193  *
194  * WARNING: Be wary that the child cleanup function passed to tst_brkm()
195  *          must clean only resources the child has allocated. E.g. the
196  *          child cleanup is different function from the parent cleanup.
197  */
198 void tst_record_childstatus(void (*cleanup)(void), pid_t child);
199 
200 extern int tst_count;
201 
202 /* lib/tst_sig.c */
203 void tst_sig(int fork_flag, void (*handler)(), void (*cleanup)());
204 
205 /* lib/get_high_address.c */
206 char *get_high_address(void);
207 
208 /* lib/self_exec.c */
209 void maybe_run_child(void (*child)(), const char *fmt, ...);
210 int self_exec(const char *argv0, const char *fmt, ...);
211 
212 /* lib/tst_mkfs.c
213  *
214  * @dev: path to a device
215  * @fs_type: filesystem type
216  * @fs_opts: NULL or NULL terminated array of mkfs options
217  * @extra_opt: extra mkfs option which is passed after the device name
218  */
219 #define tst_mkfs(cleanup, dev, fs_type, fs_opts, extra_opt) \
220 	tst_mkfs_(__FILE__, __LINE__, cleanup, dev, fs_type, \
221 		  fs_opts, extra_opt)
222 void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
223 	       const char *dev, const char *fs_type,
224 	       const char *const fs_opts[], const char *extra_opt);
225 
226 /* lib/tst_net.c
227  *
228  * Return unused port
229  */
230 unsigned short tst_get_unused_port(void (cleanup_fn)(void),
231 	unsigned short family, int type);
232 
233 /* lib/tst_res.c
234  * tst_strsig converts signal's value to corresponding string.
235  * tst_strerrno converts errno to corresponding string.
236  */
237 const char *tst_strsig(int sig);
238 const char *tst_strerrno(int err);
239 
240 /* lib/tst_path_has_mnt_flags.c
241  *
242  * Check whether a path is on a filesystem that is mounted with
243  * specified flags
244  * @path: path to file, if path is NULL tst_tmpdir is used.
245  * @flags: NULL or NULL terminated array of mount flags
246  *
247  * Return: 0..n - number of flags matched
248  */
249 int tst_path_has_mnt_flags(void (cleanup_fn)(void),
250 		const char *path, const char *flags[]);
251 
252 #ifdef TST_USE_COMPAT16_SYSCALL
253 #define TCID_BIT_SUFFIX "_16"
254 #elif  TST_USE_NEWER64_SYSCALL
255 #define TCID_BIT_SUFFIX "_64"
256 #else
257 #define TCID_BIT_SUFFIX ""
258 #endif
259 #define TCID_DEFINE(ID) char *TCID = (#ID TCID_BIT_SUFFIX)
260 
261 #endif	/* __TEST_H__ */
262