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 #include <stdio.h>
38 #include <signal.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdint.h>
43 
44 #include "usctest.h"
45 
46 #include "tst_common.h"
47 #include "old_safe_file_ops.h"
48 #include "old_checkpoint.h"
49 #include "tst_process_state.h"
50 #include "old_resource.h"
51 #include "tst_res_flags.h"
52 #include "tst_timer.h"
53 #include "tst_kvercmp.h"
54 #include "tst_fs.h"
55 #include "tst_pid.h"
56 #include "tst_cmd.h"
57 #include "tst_cpu.h"
58 #include "old_device.h"
59 #include "old_tmpdir.h"
60 
61 /*
62  * Ensure that NUMSIGS is defined.
63  * It should be defined in signal.h or sys/signal.h on
64  * UNICOS/mk and IRIX systems.   On UNICOS systems,
65  * it is not defined, thus it is being set to UNICOS's NSIG.
66  * Note:  IRIX's NSIG (signals are 1-(NSIG-1))
67  *      is not same meaning as UNICOS/UMK's NSIG  (signals 1-NSIG)
68  */
69 #ifndef NUMSIGS
70 #define NUMSIGS NSIG
71 #endif
72 
73 
74 /* defines for unexpected signal setup routine (set_usig.c) */
75 #define FORK    1		/* SIGCLD is to be ignored */
76 #define NOFORK  0		/* SIGCLD is to be caught */
77 #define DEF_HANDLER SIG_ERR	/* tells set_usig() to use default signal handler */
78 
79 /*
80  * The following defines are used to control tst_res and t_result reporting.
81  */
82 
83 #define TOUTPUT	   "TOUTPUT"		/* The name of the environment variable */
84 					/* that can be set to one of the following */
85 					/* strings to control tst_res output */
86 					/* If not set, TOUT_VERBOSE_S is assumed */
87 
88 /*
89  * fork() can't be used on uClinux systems, so use FORK_OR_VFORK instead,
90  * which will run vfork() on uClinux.
91  * mmap() doesn't support MAP_PRIVATE on uClinux systems, so use
92  * MAP_PRIVATE_EXCEPT_UCLINUX instead, which will skip the option on uClinux.
93  * If MAP_PRIVATE really is required, the test can not be run on uClinux.
94  */
95 #ifdef UCLINUX
96 # define FORK_OR_VFORK			tst_vfork
97 # define MAP_PRIVATE_EXCEPT_UCLINUX	0
98 /* tst_flush() + vfork() */
99 pid_t tst_vfork(void);
100 #else
101 # define FORK_OR_VFORK			tst_fork
102 # define MAP_PRIVATE_EXCEPT_UCLINUX	MAP_PRIVATE
103 #endif
104 
105 /*
106  * Macro to use for making functions called only once in
107  * multi-threaded tests such as init or cleanup function.
108  * The first call to @name_fn function by any thread shall
109  * call the @exec_fn. Subsequent calls shall not call @exec_fn.
110  * *_fn functions must not take any arguments.
111  */
112 #define TST_DECLARE_ONCE_FN(name_fn, exec_fn)				\
113 	void name_fn(void)						\
114 	{								\
115 		static pthread_once_t ltp_once = PTHREAD_ONCE_INIT;	\
116 		pthread_once(&ltp_once, exec_fn);			\
117 	}
118 
119 /*
120  * lib/forker.c
121  */
122 extern int Forker_pids[];
123 extern int Forker_npids;
124 
125 typedef struct {
126 	char *option;	/* Valid option string (one option only) like "a:"  */
127 	int  *flag;	/* Pointer to location to set true if option given  */
128 	char **arg;	/* Pointer to location to place argument, if needed */
129 } option_t;
130 
131 /* lib/tst_parse_opts.c */
132 void tst_parse_opts(int argc, char *argv[], const option_t *user_optarg,
133                     void (*user_help)(void));
134 
135 /* lib/tst_res.c */
136 const char *strttype(int ttype);
137 
138 void tst_resm_(const char *file, const int lineno, int ttype,
139 	const char *arg_fmt, ...)
140 	__attribute__ ((format (printf, 4, 5)));
141 #define tst_resm(ttype, arg_fmt, ...) \
142 	tst_resm_(__FILE__, __LINE__, (ttype), \
143 		  (arg_fmt), ##__VA_ARGS__)
144 
145 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
146 	const void *buf, size_t size, const char *arg_fmt, ...)
147 	__attribute__ ((format (printf, 6, 7)));
148 #define tst_resm_hexd(ttype, buf, size, arg_fmt, ...) \
149 	tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
150 		       (arg_fmt), ##__VA_ARGS__)
151 
152 void tst_brkm_(const char *file, const int lineno, int ttype,
153 	void (*func)(void), const char *arg_fmt, ...)
154 	__attribute__ ((format (printf, 5, 6))) LTP_ATTRIBUTE_NORETURN;
155 #define tst_brkm(ttype, func, arg_fmt, ...) \
156 	tst_brkm_(__FILE__, __LINE__, (ttype), (func), \
157 		  (arg_fmt), ##__VA_ARGS__)
158 
159 void tst_require_root(void);
160 void tst_exit(void) LTP_ATTRIBUTE_NORETURN;
161 void tst_flush(void);
162 
163 /*
164  * tst_flush() + fork
165  * NOTE: tst_fork() will reset T_exitval to 0 for child process.
166  */
167 pid_t tst_fork(void);
168 
169 /* lib/tst_res.c */
170 /*
171  * In case we need do real test work in child process parent process can use
172  * tst_record_childstatus() to make child process's test results propagated to
173  * parent process correctly.
174  *
175  * The child can use tst_resm(), tst_brkm() followed by the tst_exit() or
176  * plain old exit() (with TPASS, TFAIL and TBROK).
177  *
178  * WARNING: Be wary that the child cleanup function passed to tst_brkm()
179  *          must clean only resources the child has allocated. E.g. the
180  *          child cleanup is different function from the parent cleanup.
181  */
182 void tst_record_childstatus(void (*cleanup)(void), pid_t child);
183 
184 extern int tst_count;
185 
186 /* lib/tst_sig.c */
187 void tst_sig(int fork_flag, void (*handler)(), void (*cleanup)());
188 
189 /* lib/get_high_address.c */
190 char *get_high_address(void);
191 
192 /* lib/self_exec.c */
193 void maybe_run_child(void (*child)(), const char *fmt, ...);
194 int self_exec(const char *argv0, const char *fmt, ...);
195 
196 /* Functions from lib/cloner.c */
197 int ltp_clone(unsigned long flags, int (*fn)(void *arg), void *arg,
198 		size_t stack_size, void *stack);
199 int ltp_clone7(unsigned long flags, int (*fn)(void *arg), void *arg,
200 		size_t stack_size, void *stack, ...);
201 int ltp_clone_malloc(unsigned long clone_flags, int (*fn)(void *arg),
202 		void *arg, size_t stacksize);
203 int ltp_clone_quick(unsigned long clone_flags, int (*fn)(void *arg),
204 		void *arg);
205 #define clone(...) use_the_ltp_clone_functions,do_not_use_clone
206 
207 
208 /* lib/tst_mkfs.c
209  *
210  * @dev: path to a device
211  * @fs_type: filesystem type
212  * @fs_opts: NULL or NULL terminated array of mkfs options
213  * @extra_opt: extra mkfs option which is passed after the device name
214  */
215 void tst_mkfs(void (cleanup_fn)(void), const char *dev,
216               const char *fs_type, const char *const fs_opts[],
217               const char *extra_opt);
218 
219 /* lib/tst_net.c
220  *
221  * Return unused port
222  */
223 unsigned short tst_get_unused_port(void (cleanup_fn)(void),
224 	unsigned short family, int type);
225 
226 /* lib/tst_res.c
227  * tst_strsig converts signal's value to corresponding string.
228  * tst_strerrno converts errno to corresponding string.
229  */
230 const char *tst_strsig(int sig);
231 const char *tst_strerrno(int err);
232 
233 /* lib/tst_path_has_mnt_flags.c
234  *
235  * Check whether a path is on a filesystem that is mounted with
236  * specified flags
237  * @path: path to file, if path is NULL tst_tmpdir is used.
238  * @flags: NULL or NULL terminated array of mount flags
239  *
240  * Return: 0..n - number of flags matched
241  */
242 int tst_path_has_mnt_flags(void (cleanup_fn)(void),
243 		const char *path, const char *flags[]);
244 
245 #ifdef TST_USE_COMPAT16_SYSCALL
246 #define TCID_BIT_SUFFIX "_16"
247 #elif  TST_USE_NEWER64_SYSCALL
248 #define TCID_BIT_SUFFIX "_64"
249 #else
250 #define TCID_BIT_SUFFIX ""
251 #endif
252 #define TCID_DEFINE(ID) char *TCID = (#ID TCID_BIT_SUFFIX)
253 
254 #endif	/* __TEST_H__ */
255