blob: 81fca3eb06e5f7fa6d8e93a86fe29d3a666d9ff7 [file] [log] [blame]
alaffincc2e5552000-07-27 17:13:18 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubis06f9fe42013-06-26 14:55:48 +02003 * Copyright (c) 2009-2013 Cyril Hrubis chrubis@suse.cz
subrata_modak88c166c2009-06-09 16:01:20 +00004 *
alaffincc2e5552000-07-27 17:13:18 +00005 * 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.
vapier45a8ba02009-07-20 10:59:32 +00008 *
alaffincc2e5552000-07-27 17:13:18 +00009 * 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.
vapier45a8ba02009-07-20 10:59:32 +000012 *
alaffincc2e5552000-07-27 17:13:18 +000013 * 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.
vapier45a8ba02009-07-20 10:59:32 +000019 *
alaffincc2e5552000-07-27 17:13:18 +000020 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080021 * with this program; if not, write the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000023 *
alaffincc2e5552000-07-27 17:13:18 +000024 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000026 *
27 * http://www.sgi.com
28 *
29 * For further information regarding this notice, see:
30 *
alaffincc2e5552000-07-27 17:13:18 +000031 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32 */
33
alaffincc2e5552000-07-27 17:13:18 +000034#ifndef __TEST_H__
35#define __TEST_H__
36
37#include <stdio.h>
38#include <signal.h>
39#include <unistd.h>
plars28884512002-05-21 13:01:00 +000040#include <string.h>
41#include <stdlib.h>
alaffincc2e5552000-07-27 17:13:18 +000042
vapier2751f262008-02-11 16:39:54 +000043#include "compiler.h"
44
Cyril Hrubis42748952012-11-27 19:26:45 +010045#include "safe_file_ops.h"
Cyril Hrubisab8388c2012-11-28 15:42:17 +010046#include "tst_checkpoint.h"
Cyril Hrubis19240612012-12-11 18:51:54 +010047#include "tst_process_state.h"
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010048#include "tst_resource.h"
Cyril Hrubis42748952012-11-27 19:26:45 +010049
subrata_modak7f71cfb2009-08-04 11:47:10 +000050/* Use low 6 bits to encode test type */
51#define TTYPE_MASK 0x3f
vapier9799ea12009-07-20 02:42:32 +000052#define TPASS 0 /* Test passed flag */
53#define TFAIL 1 /* Test failed flag */
54#define TBROK 2 /* Test broken flag */
subrata_modak7f71cfb2009-08-04 11:47:10 +000055#define TWARN 4 /* Test warning flag */
56#define TRETR 8 /* Test retire flag */
57#define TINFO 16 /* Test information flag */
58#define TCONF 32 /* Test not appropriate for configuration flag */
vapier9799ea12009-07-20 02:42:32 +000059#define TTYPE_RESULT(ttype) ((ttype) & TTYPE_MASK)
60
61#define TERRNO 0x100 /* Append errno information to output */
62#define TTERRNO 0x200 /* Append TEST_ERRNO information to output */
Garrett Coopera9e49f12010-12-16 10:54:03 -080063#define TRERRNO 0x300 /* Capture errno information from TEST_RETURN to
64 output; useful for pthread-like APIs :). */
alaffincc2e5552000-07-27 17:13:18 +000065
Jan Stancek11a84a62013-10-17 12:44:49 +020066/* virt types for tst_is_virt() */
67#define VIRT_XEN 1 /* xen dom0/domU */
68
alaffincc2e5552000-07-27 17:13:18 +000069/*
alaffincc2e5552000-07-27 17:13:18 +000070 * Ensure that NUMSIGS is defined.
71 * It should be defined in signal.h or sys/signal.h on
72 * UNICOS/mk and IRIX systems. On UNICOS systems,
73 * it is not defined, thus it is being set to UNICOS's NSIG.
vapier45a8ba02009-07-20 10:59:32 +000074 * Note: IRIX's NSIG (signals are 1-(NSIG-1))
alaffincc2e5552000-07-27 17:13:18 +000075 * is not same meaning as UNICOS/UMK's NSIG (signals 1-NSIG)
76 */
77#ifndef NUMSIGS
78#define NUMSIGS NSIG
79#endif
80
81
82/* defines for unexpected signal setup routine (set_usig.c) */
83#define FORK 1 /* SIGCLD is to be ignored */
84#define NOFORK 0 /* SIGCLD is to be caught */
subrata_modak40bc6aa2009-09-27 17:46:05 +000085#define DEF_HANDLER SIG_ERR /* tells set_usig() to use default signal handler */
alaffincc2e5552000-07-27 17:13:18 +000086
87/*
88 * The following defines are used to control tst_res and t_result reporting.
89 */
90
91#define TOUTPUT "TOUTPUT" /* The name of the environment variable */
92 /* that can be set to one of the following */
93 /* strings to control tst_res output */
94 /* If not set, TOUT_VERBOSE_S is assumed */
95
alaffincc2e5552000-07-27 17:13:18 +000096/*
robbiewd34d5812005-07-11 22:28:09 +000097 * fork() can't be used on uClinux systems, so use FORK_OR_VFORK instead,
98 * which will run vfork() on uClinux.
99 * mmap() doesn't support MAP_PRIVATE on uClinux systems, so use
100 * MAP_PRIVATE_EXCEPT_UCLINUX instead, which will skip the option on uClinux.
101 * If MAP_PRIVATE really is required, the test can not be run on uClinux.
102 */
103#ifdef UCLINUX
Cyril Hrubis3f0510c2014-02-06 13:50:14 +0100104# define FORK_OR_VFORK tst_vfork
105# define MAP_PRIVATE_EXCEPT_UCLINUX 0
106/* tst_flush() + vfork() */
107pid_t tst_vfork(void);
robbiewd34d5812005-07-11 22:28:09 +0000108#else
Cyril Hrubis3f0510c2014-02-06 13:50:14 +0100109# define FORK_OR_VFORK tst_fork
110# define MAP_PRIVATE_EXCEPT_UCLINUX MAP_PRIVATE
robbiewd34d5812005-07-11 22:28:09 +0000111#endif
112
113/*
Alexey Kodanevbf17abf2014-02-18 18:08:30 +0400114 * Macro to use for making functions called only once in
115 * multi-threaded tests such as init or cleanup function.
116 * The first call to @name_fn function by any thread shall
117 * call the @exec_fn. Subsequent calls shall not call @exec_fn.
118 * *_fn functions must not take any arguments.
119 */
120#define TST_DECLARE_ONCE_FN(name_fn, exec_fn) \
121 void name_fn(void) \
122 { \
123 static pthread_once_t ltp_once = PTHREAD_ONCE_INIT; \
124 pthread_once(&ltp_once, exec_fn); \
125 }
126
127/*
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800128 * lib/forker.c
alaffincc2e5552000-07-27 17:13:18 +0000129 */
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800130extern int Forker_pids[];
131extern int Forker_npids;
132
133/* lib/tst_res.c */
vapier9799ea12009-07-20 02:42:32 +0000134const char *strttype(int ttype);
Mike Frysinger55d8ae52014-02-13 04:11:45 -0500135void tst_res(int ttype, const char *fname, const char *arg_fmt, ...)
subrata_modak0d719e52009-08-30 17:18:18 +0000136 __attribute__ ((format (printf, 3, 4)));
Mike Frysinger55d8ae52014-02-13 04:11:45 -0500137void tst_resm(int ttype, const char *arg_fmt, ...)
subrata_modak0d719e52009-08-30 17:18:18 +0000138 __attribute__ ((format (printf, 2, 3)));
Mike Frysinger55d8ae52014-02-13 04:11:45 -0500139void tst_resm_hexd(int ttype, const void *buf, size_t size, const char *arg_fmt, ...)
Alexey Kodanev04a34ba2013-05-23 11:03:18 +0400140 __attribute__ ((format (printf, 4, 5)));
Mike Frysinger55d8ae52014-02-13 04:11:45 -0500141void tst_brk(int ttype, const char *fname, void (*func)(void), const char *arg_fmt, ...)
Garrett Cooper68d76e02010-12-20 05:49:48 -0800142 __attribute__ ((format (printf, 4, 5)));
Mike Frysinger55d8ae52014-02-13 04:11:45 -0500143void tst_brkm(int ttype, void (*func)(void), const char *arg_fmt, ...)
Cyril Hrubis9ef186f2011-05-11 13:13:12 +0200144 __attribute__ ((format (printf, 3, 4))) LTP_ATTRIBUTE_NORETURN;
subrata_modak88c166c2009-06-09 16:01:20 +0000145void tst_require_root(void (*func)(void));
146int tst_environ(void);
147void tst_exit(void) LTP_ATTRIBUTE_NORETURN;
148void tst_flush(void);
alaffincc2e5552000-07-27 17:13:18 +0000149
Cyril Hrubisddd3b852014-02-03 17:10:04 +0100150/*
151 * tst_flush() + fork
152 */
153pid_t tst_fork(void);
154
Caspar Zhangd59a6592013-03-07 14:59:12 +0800155extern int tst_count;
alaffincc2e5552000-07-27 17:13:18 +0000156
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800157/* lib/tst_sig.c */
subrata_modak88c166c2009-06-09 16:01:20 +0000158void tst_sig(int fork_flag, void (*handler)(), void (*cleanup)());
alaffincc2e5552000-07-27 17:13:18 +0000159
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800160/* lib/tst_tmpdir.c */
Marios Makris60b1baa2012-08-09 20:36:46 +0300161
162/* tst_tmpdir()
163 *
164 * Create a unique temporary directory and chdir() to it. It expects the caller
165 * to have defined/initialized the TCID/TST_TOTAL global variables.
166 * The TESTDIR global variable will be set to the directory that gets used
167 * as the testing directory.
168 *
169 * NOTE: This function must be called BEFORE any activity that would require
170 * CLEANUP. If tst_tmpdir() fails, it cleans up afer itself and calls
171 * tst_exit() (i.e. does not return).
172 */
subrata_modak88c166c2009-06-09 16:01:20 +0000173void tst_tmpdir(void);
Marios Makris60b1baa2012-08-09 20:36:46 +0300174/* tst_rmdir()
175 *
176 * Recursively remove the temporary directory created by tst_tmpdir().
177 * This function is intended ONLY as a companion to tst_tmpdir().
Marios Makris60b1baa2012-08-09 20:36:46 +0300178 */
subrata_modak88c166c2009-06-09 16:01:20 +0000179void tst_rmdir(void);
Marios Makris60b1baa2012-08-09 20:36:46 +0300180/* get_tst_tmpdir()
181 *
182 * Return a copy of the test temp directory as seen by LTP. This is for
183 * path-oriented tests like chroot, etc, that may munge the path a bit.
184 *
185 * FREE VARIABLE AFTER USE IF IT IS REUSED!
186 */
Garrett Cooperdb976802010-12-16 15:56:25 -0800187char *get_tst_tmpdir(void);
Cyril Hrubisab8388c2012-11-28 15:42:17 +0100188/*
189 * Returns 1 if temp directory was created.
190 */
191int tst_tmpdir_created(void);
alaffincc2e5552000-07-27 17:13:18 +0000192
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800193/* lib/get_high_address.c */
subrata_modak88c166c2009-06-09 16:01:20 +0000194char *get_high_address(void);
robbiewf3a83d52002-05-28 16:26:16 +0000195
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800196/* lib/tst_kvercmp.c */
subrata_modak88c166c2009-06-09 16:01:20 +0000197void tst_getkver(int *k1, int *k2, int *k3);
198int tst_kvercmp(int r1, int r2, int r3);
robbiew143090a2005-01-17 22:10:09 +0000199
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800200struct tst_kern_exv {
201 char *dist_name;
202 char *extra_ver;
203};
204
205int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers);
206
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800207/* lib/tst_cwd_has_free.c */
subrata_modak88c166c2009-06-09 16:01:20 +0000208int tst_cwd_has_free(int required_kib);
209
Jan Stancek11a84a62013-10-17 12:44:49 +0200210int tst_is_virt(int virt_type);
211
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800212/* lib/self_exec.c */
Mike Frysinger55d8ae52014-02-13 04:11:45 -0500213void maybe_run_child(void (*child)(), const char *fmt, ...);
214int self_exec(const char *argv0, const char *fmt, ...);
subrata_modak744305e2008-08-19 07:00:49 +0000215
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800216/* Functions from lib/cloner.c */
Zeng Linggang1db62662014-01-09 20:10:11 +0800217int ltp_clone(unsigned long flags, int (*fn)(void *arg), void *arg,
218 size_t stack_size, void *stack);
219int ltp_clone7(unsigned long flags, int (*fn)(void *arg), void *arg,
zenglg.jy5f5cb632013-12-18 17:29:51 +0800220 size_t stack_size, void *stack, ...);
vapierf6d7f092009-11-03 20:07:35 +0000221int ltp_clone_malloc(unsigned long clone_flags, int (*fn)(void *arg),
222 void *arg, size_t stacksize);
223int ltp_clone_quick(unsigned long clone_flags, int (*fn)(void *arg),
224 void *arg);
225#define clone(...) use_the_ltp_clone_functions,do_not_use_clone
226
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800227/* Functions from lib/mount_utils.c */
yaberauneyacfb70632010-01-10 08:01:15 +0000228char *get_block_device(const char *path);
yaberauneyac2a3ba52010-01-10 22:27:14 +0000229char *get_mountpoint(const char *path);
yaberauneyacfb70632010-01-10 08:01:15 +0000230
Garrett Cooperaec33cd2010-12-17 04:14:16 -0800231/* Function from lib/get_path.c */
Subrata Modakca758b72010-05-18 01:28:14 +0530232int tst_get_path(const char *prog_name, char *buf, size_t buf_len);
233
Wanlong Gao3b535a82012-10-18 16:35:14 +0800234/* lib/tst_cpu.c */
235long tst_ncpus(void);
236long tst_ncpus_max(void);
237
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +0400238/* lib/tst_run_cmd.c
239 *
240 * vfork() + execvp() specified program.
241 * @argv: a list of two (at least program name + NULL) or more pointers that
242 * represent the argument list to the new program. The array of pointers
243 * must be terminated by a NULL pointer.
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +0400244 * @stdout_fd: file descriptor where to redirect stdout. Set -1 if
245 * redirection is not needed.
246 * @stderr_fd: file descriptor where to redirect stderr. Set -1 if
247 * redirection is not needed.
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +0400248 */
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +0400249void tst_run_cmd_fds(void (cleanup_fn)(void),
Stanislav Kholmanskikhe63337a2013-08-08 11:18:06 +0400250 const char *const argv[],
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +0400251 int stdout_fd,
252 int stderr_fd);
253
254/* Executes tst_run_cmd_fds() and redirects its output to a file
255 * @stdout_path: path where to redirect stdout. Set NULL if redirection is
256 * not needed.
257 * @stderr_path: path where to redirect stderr. Set NULL if redirection is
258 * not needed.
259 */
260void tst_run_cmd(void (cleanup_fn)(void),
Stanislav Kholmanskikhe63337a2013-08-08 11:18:06 +0400261 const char *const argv[],
Stanislav kholmanskikhbee0a942013-08-06 16:54:37 +0400262 const char *stdout_path,
263 const char *stderr_path);
Alexey Kodanev4bc55ec2013-06-27 18:52:22 +0400264
Cyril Hrubis2492ce22013-08-08 14:24:38 +0200265/* lib/tst_mkfs.c
266 *
267 * @dev: path to a device
268 * @fs_type: filesystem type
269 * @fs_opts: extra mkfs options
270 */
271void tst_mkfs(void (cleanup_fn)(void), const char *dev,
272 const char *fs_type, const char *fs_opts);
273
Stanislav Kholmanskikh1d5d4842013-08-14 10:01:46 +0400274/* lib/tst_fill_file.c
275 *
276 * Creates/ovewrites a file with specified pattern
277 * @path: path to file
278 * @pattern: pattern
279 * @bs: block size
280 * @bcount: blocks amount
281 */
282int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount);
283
Stanislav Kholmanskikhbc44eb32014-01-13 13:35:22 +0400284/* lib/tst_uid_gid.c
285 *
286 * Return the first unused uid and gid
287 */
288uid_t tst_get_unused_uid(void);
289gid_t tst_get_unused_gid(void);
290
subrata_modak744305e2008-08-19 07:00:49 +0000291#ifdef TST_USE_COMPAT16_SYSCALL
292#define TCID_BIT_SUFFIX "_16"
293#elif TST_USE_NEWER64_SYSCALL
294#define TCID_BIT_SUFFIX "_64"
295#else
296#define TCID_BIT_SUFFIX ""
297#endif
subrata_modak88c166c2009-06-09 16:01:20 +0000298#define TCID_DEFINE(ID) char *TCID = (#ID TCID_BIT_SUFFIX)
subrata_modak744305e2008-08-19 07:00:49 +0000299
subrata_modak88c166c2009-06-09 16:01:20 +0000300#endif /* __TEST_H__ */