xref: /aosp_15_r20/external/ltp/lib/tst_test.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015-2016 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2016-2024
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <stdarg.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <math.h>
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_device.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/abisize.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/futex.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_ansi_color.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "tst_wallclock.h"
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_sys_conf.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_kconfig.h"
33*49cdfc7eSAndroid Build Coastguard Worker #include "tst_private.h"
34*49cdfc7eSAndroid Build Coastguard Worker #include "old_resource.h"
35*49cdfc7eSAndroid Build Coastguard Worker #include "old_device.h"
36*49cdfc7eSAndroid Build Coastguard Worker #include "old_tmpdir.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "ltp-version.h"
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker /*
40*49cdfc7eSAndroid Build Coastguard Worker  * Hack to get TCID defined in newlib tests
41*49cdfc7eSAndroid Build Coastguard Worker  */
42*49cdfc7eSAndroid Build Coastguard Worker const char *TCID __attribute__((weak));
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker /* update also docparse/testinfo.pl */
45*49cdfc7eSAndroid Build Coastguard Worker #define LINUX_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id="
46*49cdfc7eSAndroid Build Coastguard Worker #define LINUX_STABLE_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id="
47*49cdfc7eSAndroid Build Coastguard Worker #define GLIBC_GIT_URL "https://sourceware.org/git/?p=glibc.git;a=commit;h="
48*49cdfc7eSAndroid Build Coastguard Worker #define MUSL_GIT_URL "https://git.musl-libc.org/cgit/musl/commit/src/linux/clone.c?id="
49*49cdfc7eSAndroid Build Coastguard Worker #define CVE_DB_URL "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-"
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker #define DEFAULT_TIMEOUT 30
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker struct tst_test *tst_test;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static const char *tid;
56*49cdfc7eSAndroid Build Coastguard Worker static int iterations = 1;
57*49cdfc7eSAndroid Build Coastguard Worker static float duration = -1;
58*49cdfc7eSAndroid Build Coastguard Worker static float timeout_mul = -1;
59*49cdfc7eSAndroid Build Coastguard Worker static pid_t main_pid, lib_pid;
60*49cdfc7eSAndroid Build Coastguard Worker static int mntpoint_mounted;
61*49cdfc7eSAndroid Build Coastguard Worker static int ovl_mounted;
62*49cdfc7eSAndroid Build Coastguard Worker static struct timespec tst_start_time; /* valid only for test pid */
63*49cdfc7eSAndroid Build Coastguard Worker static int tdebug;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker struct results {
66*49cdfc7eSAndroid Build Coastguard Worker 	int passed;
67*49cdfc7eSAndroid Build Coastguard Worker 	int skipped;
68*49cdfc7eSAndroid Build Coastguard Worker 	int failed;
69*49cdfc7eSAndroid Build Coastguard Worker 	int warnings;
70*49cdfc7eSAndroid Build Coastguard Worker 	int broken;
71*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int timeout;
72*49cdfc7eSAndroid Build Coastguard Worker 	int max_runtime;
73*49cdfc7eSAndroid Build Coastguard Worker };
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker static struct results *results;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker static int ipc_fd;
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker extern void *tst_futexes;
80*49cdfc7eSAndroid Build Coastguard Worker extern unsigned int tst_max_futexes;
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static char ipc_path[1064];
83*49cdfc7eSAndroid Build Coastguard Worker const char *tst_ipc_path = ipc_path;
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker static char shm_path[1024];
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker int TST_ERR;
88*49cdfc7eSAndroid Build Coastguard Worker int TST_PASS;
89*49cdfc7eSAndroid Build Coastguard Worker long TST_RET;
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker static void do_cleanup(void);
92*49cdfc7eSAndroid Build Coastguard Worker static void do_exit(int ret) __attribute__ ((noreturn));
93*49cdfc7eSAndroid Build Coastguard Worker 
setup_ipc(void)94*49cdfc7eSAndroid Build Coastguard Worker static void setup_ipc(void)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker 	size_t size = getpagesize();
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	if (access("/dev/shm", F_OK) == 0) {
99*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
100*49cdfc7eSAndroid Build Coastguard Worker 			 tid, getpid());
101*49cdfc7eSAndroid Build Coastguard Worker 	} else {
102*49cdfc7eSAndroid Build Coastguard Worker 		char *tmpdir;
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 		if (!tst_tmpdir_created())
105*49cdfc7eSAndroid Build Coastguard Worker 			tst_tmpdir();
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 		tmpdir = tst_get_tmpdir();
108*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
109*49cdfc7eSAndroid Build Coastguard Worker 			 tmpdir, tid, getpid());
110*49cdfc7eSAndroid Build Coastguard Worker 		free(tmpdir);
111*49cdfc7eSAndroid Build Coastguard Worker 	}
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
114*49cdfc7eSAndroid Build Coastguard Worker 	if (ipc_fd < 0)
115*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
116*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(shm_path, 0666);
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FTRUNCATE(ipc_fd, size);
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	/* Checkpoints needs to be accessible from processes started by exec() */
123*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
124*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
125*49cdfc7eSAndroid Build Coastguard Worker 		putenv(ipc_path);
126*49cdfc7eSAndroid Build Coastguard Worker 	} else {
127*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UNLINK(shm_path);
128*49cdfc7eSAndroid Build Coastguard Worker 	}
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(ipc_fd);
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_checkpoints) {
133*49cdfc7eSAndroid Build Coastguard Worker 		tst_futexes = (char *)results + sizeof(struct results);
134*49cdfc7eSAndroid Build Coastguard Worker 		tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
135*49cdfc7eSAndroid Build Coastguard Worker 	}
136*49cdfc7eSAndroid Build Coastguard Worker }
137*49cdfc7eSAndroid Build Coastguard Worker 
cleanup_ipc(void)138*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_ipc(void)
139*49cdfc7eSAndroid Build Coastguard Worker {
140*49cdfc7eSAndroid Build Coastguard Worker 	size_t size = getpagesize();
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	if (ipc_fd > 0 && close(ipc_fd))
143*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 	if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
146*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	if (results) {
149*49cdfc7eSAndroid Build Coastguard Worker 		msync((void *)results, size, MS_SYNC);
150*49cdfc7eSAndroid Build Coastguard Worker 		munmap((void *)results, size);
151*49cdfc7eSAndroid Build Coastguard Worker 		results = NULL;
152*49cdfc7eSAndroid Build Coastguard Worker 	}
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker 
tst_reinit(void)155*49cdfc7eSAndroid Build Coastguard Worker void tst_reinit(void)
156*49cdfc7eSAndroid Build Coastguard Worker {
157*49cdfc7eSAndroid Build Coastguard Worker 	const char *path = getenv(IPC_ENV_VAR);
158*49cdfc7eSAndroid Build Coastguard Worker 	size_t size = getpagesize();
159*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker 	if (!path)
162*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, IPC_ENV_VAR" is not defined");
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	if (access(path, F_OK))
165*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "File %s does not exist!", path);
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(path, O_RDWR);
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
170*49cdfc7eSAndroid Build Coastguard Worker 	tst_futexes = (char *)results + sizeof(struct results);
171*49cdfc7eSAndroid Build Coastguard Worker 	tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker 
update_results(int ttype)176*49cdfc7eSAndroid Build Coastguard Worker static void update_results(int ttype)
177*49cdfc7eSAndroid Build Coastguard Worker {
178*49cdfc7eSAndroid Build Coastguard Worker 	if (!results)
179*49cdfc7eSAndroid Build Coastguard Worker 		return;
180*49cdfc7eSAndroid Build Coastguard Worker 
181*49cdfc7eSAndroid Build Coastguard Worker 	switch (ttype) {
182*49cdfc7eSAndroid Build Coastguard Worker 	case TCONF:
183*49cdfc7eSAndroid Build Coastguard Worker 		tst_atomic_inc(&results->skipped);
184*49cdfc7eSAndroid Build Coastguard Worker 	break;
185*49cdfc7eSAndroid Build Coastguard Worker 	case TPASS:
186*49cdfc7eSAndroid Build Coastguard Worker 		tst_atomic_inc(&results->passed);
187*49cdfc7eSAndroid Build Coastguard Worker 	break;
188*49cdfc7eSAndroid Build Coastguard Worker 	case TWARN:
189*49cdfc7eSAndroid Build Coastguard Worker 		tst_atomic_inc(&results->warnings);
190*49cdfc7eSAndroid Build Coastguard Worker 	break;
191*49cdfc7eSAndroid Build Coastguard Worker 	case TFAIL:
192*49cdfc7eSAndroid Build Coastguard Worker 		tst_atomic_inc(&results->failed);
193*49cdfc7eSAndroid Build Coastguard Worker 	break;
194*49cdfc7eSAndroid Build Coastguard Worker 	case TBROK:
195*49cdfc7eSAndroid Build Coastguard Worker 		tst_atomic_inc(&results->broken);
196*49cdfc7eSAndroid Build Coastguard Worker 	break;
197*49cdfc7eSAndroid Build Coastguard Worker 	}
198*49cdfc7eSAndroid Build Coastguard Worker }
199*49cdfc7eSAndroid Build Coastguard Worker 
print_result(const char * file,const int lineno,int ttype,const char * fmt,va_list va)200*49cdfc7eSAndroid Build Coastguard Worker static void print_result(const char *file, const int lineno, int ttype,
201*49cdfc7eSAndroid Build Coastguard Worker 			 const char *fmt, va_list va)
202*49cdfc7eSAndroid Build Coastguard Worker {
203*49cdfc7eSAndroid Build Coastguard Worker 	char buf[1024];
204*49cdfc7eSAndroid Build Coastguard Worker 	char *str = buf;
205*49cdfc7eSAndroid Build Coastguard Worker 	int ret, size = sizeof(buf), ssize, int_errno, buflen;
206*49cdfc7eSAndroid Build Coastguard Worker 	const char *str_errno = NULL;
207*49cdfc7eSAndroid Build Coastguard Worker 	const char *res;
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	switch (TTYPE_RESULT(ttype)) {
210*49cdfc7eSAndroid Build Coastguard Worker 	case TPASS:
211*49cdfc7eSAndroid Build Coastguard Worker 		res = "TPASS";
212*49cdfc7eSAndroid Build Coastguard Worker 	break;
213*49cdfc7eSAndroid Build Coastguard Worker 	case TFAIL:
214*49cdfc7eSAndroid Build Coastguard Worker 		res = "TFAIL";
215*49cdfc7eSAndroid Build Coastguard Worker 	break;
216*49cdfc7eSAndroid Build Coastguard Worker 	case TBROK:
217*49cdfc7eSAndroid Build Coastguard Worker 		res = "TBROK";
218*49cdfc7eSAndroid Build Coastguard Worker 	break;
219*49cdfc7eSAndroid Build Coastguard Worker 	case TCONF:
220*49cdfc7eSAndroid Build Coastguard Worker 		res = "TCONF";
221*49cdfc7eSAndroid Build Coastguard Worker 	break;
222*49cdfc7eSAndroid Build Coastguard Worker 	case TWARN:
223*49cdfc7eSAndroid Build Coastguard Worker 		res = "TWARN";
224*49cdfc7eSAndroid Build Coastguard Worker 	break;
225*49cdfc7eSAndroid Build Coastguard Worker 	case TINFO:
226*49cdfc7eSAndroid Build Coastguard Worker 		res = "TINFO";
227*49cdfc7eSAndroid Build Coastguard Worker 	break;
228*49cdfc7eSAndroid Build Coastguard Worker 	case TDEBUG:
229*49cdfc7eSAndroid Build Coastguard Worker 		res = "TDEBUG";
230*49cdfc7eSAndroid Build Coastguard Worker 	break;
231*49cdfc7eSAndroid Build Coastguard Worker 	default:
232*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Invalid ttype value %i", ttype);
233*49cdfc7eSAndroid Build Coastguard Worker 		abort();
234*49cdfc7eSAndroid Build Coastguard Worker 	}
235*49cdfc7eSAndroid Build Coastguard Worker 
236*49cdfc7eSAndroid Build Coastguard Worker 	if (ttype & TERRNO) {
237*49cdfc7eSAndroid Build Coastguard Worker 		str_errno = tst_strerrno(errno);
238*49cdfc7eSAndroid Build Coastguard Worker 		int_errno = errno;
239*49cdfc7eSAndroid Build Coastguard Worker 	}
240*49cdfc7eSAndroid Build Coastguard Worker 
241*49cdfc7eSAndroid Build Coastguard Worker 	if (ttype & TTERRNO) {
242*49cdfc7eSAndroid Build Coastguard Worker 		str_errno = tst_strerrno(TST_ERR);
243*49cdfc7eSAndroid Build Coastguard Worker 		int_errno = TST_ERR;
244*49cdfc7eSAndroid Build Coastguard Worker 	}
245*49cdfc7eSAndroid Build Coastguard Worker 
246*49cdfc7eSAndroid Build Coastguard Worker 	if (ttype & TRERRNO) {
247*49cdfc7eSAndroid Build Coastguard Worker 		int_errno = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
248*49cdfc7eSAndroid Build Coastguard Worker 		str_errno = tst_strerrno(int_errno);
249*49cdfc7eSAndroid Build Coastguard Worker 	}
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker 	ret = snprintf(str, size, "%s:%i: ", file, lineno);
252*49cdfc7eSAndroid Build Coastguard Worker 	str += ret;
253*49cdfc7eSAndroid Build Coastguard Worker 	size -= ret;
254*49cdfc7eSAndroid Build Coastguard Worker 
255*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_color_enabled(STDERR_FILENO))
256*49cdfc7eSAndroid Build Coastguard Worker 		ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
257*49cdfc7eSAndroid Build Coastguard Worker 			       res, ANSI_COLOR_RESET);
258*49cdfc7eSAndroid Build Coastguard Worker 	else
259*49cdfc7eSAndroid Build Coastguard Worker 		ret = snprintf(str, size, "%s: ", res);
260*49cdfc7eSAndroid Build Coastguard Worker 	str += ret;
261*49cdfc7eSAndroid Build Coastguard Worker 	size -= ret;
262*49cdfc7eSAndroid Build Coastguard Worker 
263*49cdfc7eSAndroid Build Coastguard Worker 	ssize = size - 2;
264*49cdfc7eSAndroid Build Coastguard Worker 	ret = vsnprintf(str, size, fmt, va);
265*49cdfc7eSAndroid Build Coastguard Worker 	str += MIN(ret, ssize);
266*49cdfc7eSAndroid Build Coastguard Worker 	size -= MIN(ret, ssize);
267*49cdfc7eSAndroid Build Coastguard Worker 	if (ret >= ssize) {
268*49cdfc7eSAndroid Build Coastguard Worker 		tst_res_(file, lineno, TWARN,
269*49cdfc7eSAndroid Build Coastguard Worker 				"Next message is too long and truncated:");
270*49cdfc7eSAndroid Build Coastguard Worker 	} else if (str_errno) {
271*49cdfc7eSAndroid Build Coastguard Worker 		ssize = size - 2;
272*49cdfc7eSAndroid Build Coastguard Worker 		ret = snprintf(str, size, ": %s (%d)", str_errno, int_errno);
273*49cdfc7eSAndroid Build Coastguard Worker 		str += MIN(ret, ssize);
274*49cdfc7eSAndroid Build Coastguard Worker 		size -= MIN(ret, ssize);
275*49cdfc7eSAndroid Build Coastguard Worker 		if (ret >= ssize)
276*49cdfc7eSAndroid Build Coastguard Worker 			tst_res_(file, lineno, TWARN,
277*49cdfc7eSAndroid Build Coastguard Worker 				"Next message is too long and truncated:");
278*49cdfc7eSAndroid Build Coastguard Worker 	}
279*49cdfc7eSAndroid Build Coastguard Worker 
280*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(str, size, "\n");
281*49cdfc7eSAndroid Build Coastguard Worker 
282*49cdfc7eSAndroid Build Coastguard Worker 	/* we might be called from signal handler, so use write() */
283*49cdfc7eSAndroid Build Coastguard Worker 	buflen = str - buf + 1;
284*49cdfc7eSAndroid Build Coastguard Worker 	str = buf;
285*49cdfc7eSAndroid Build Coastguard Worker 	while (buflen) {
286*49cdfc7eSAndroid Build Coastguard Worker 		ret = write(STDERR_FILENO, str, buflen);
287*49cdfc7eSAndroid Build Coastguard Worker 		if (ret <= 0)
288*49cdfc7eSAndroid Build Coastguard Worker 			break;
289*49cdfc7eSAndroid Build Coastguard Worker 
290*49cdfc7eSAndroid Build Coastguard Worker 		str += ret;
291*49cdfc7eSAndroid Build Coastguard Worker 		buflen -= ret;
292*49cdfc7eSAndroid Build Coastguard Worker 	}
293*49cdfc7eSAndroid Build Coastguard Worker }
294*49cdfc7eSAndroid Build Coastguard Worker 
tst_vres_(const char * file,const int lineno,int ttype,const char * fmt,va_list va)295*49cdfc7eSAndroid Build Coastguard Worker void tst_vres_(const char *file, const int lineno, int ttype, const char *fmt,
296*49cdfc7eSAndroid Build Coastguard Worker 	       va_list va)
297*49cdfc7eSAndroid Build Coastguard Worker {
298*49cdfc7eSAndroid Build Coastguard Worker 	print_result(file, lineno, ttype, fmt, va);
299*49cdfc7eSAndroid Build Coastguard Worker 
300*49cdfc7eSAndroid Build Coastguard Worker 	update_results(TTYPE_RESULT(ttype));
301*49cdfc7eSAndroid Build Coastguard Worker }
302*49cdfc7eSAndroid Build Coastguard Worker 
303*49cdfc7eSAndroid Build Coastguard Worker void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,
304*49cdfc7eSAndroid Build Coastguard Worker 	       va_list va);
305*49cdfc7eSAndroid Build Coastguard Worker 
306*49cdfc7eSAndroid Build Coastguard Worker static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
307*49cdfc7eSAndroid Build Coastguard Worker 			       const char *fmt, va_list va) = tst_vbrk_;
308*49cdfc7eSAndroid Build Coastguard Worker 
tst_cvres(const char * file,const int lineno,int ttype,const char * fmt,va_list va)309*49cdfc7eSAndroid Build Coastguard Worker static void tst_cvres(const char *file, const int lineno, int ttype,
310*49cdfc7eSAndroid Build Coastguard Worker 		      const char *fmt, va_list va)
311*49cdfc7eSAndroid Build Coastguard Worker {
312*49cdfc7eSAndroid Build Coastguard Worker 	if (TTYPE_RESULT(ttype) == TBROK) {
313*49cdfc7eSAndroid Build Coastguard Worker 		ttype &= ~TTYPE_MASK;
314*49cdfc7eSAndroid Build Coastguard Worker 		ttype |= TWARN;
315*49cdfc7eSAndroid Build Coastguard Worker 	}
316*49cdfc7eSAndroid Build Coastguard Worker 
317*49cdfc7eSAndroid Build Coastguard Worker 	print_result(file, lineno, ttype, fmt, va);
318*49cdfc7eSAndroid Build Coastguard Worker 	update_results(TTYPE_RESULT(ttype));
319*49cdfc7eSAndroid Build Coastguard Worker }
320*49cdfc7eSAndroid Build Coastguard Worker 
do_test_cleanup(void)321*49cdfc7eSAndroid Build Coastguard Worker static void do_test_cleanup(void)
322*49cdfc7eSAndroid Build Coastguard Worker {
323*49cdfc7eSAndroid Build Coastguard Worker 	tst_brk_handler = tst_cvres;
324*49cdfc7eSAndroid Build Coastguard Worker 
325*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->cleanup)
326*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->cleanup();
327*49cdfc7eSAndroid Build Coastguard Worker 
328*49cdfc7eSAndroid Build Coastguard Worker 	tst_free_all();
329*49cdfc7eSAndroid Build Coastguard Worker 
330*49cdfc7eSAndroid Build Coastguard Worker 	tst_brk_handler = tst_vbrk_;
331*49cdfc7eSAndroid Build Coastguard Worker }
332*49cdfc7eSAndroid Build Coastguard Worker 
tst_vbrk_(const char * file,const int lineno,int ttype,const char * fmt,va_list va)333*49cdfc7eSAndroid Build Coastguard Worker void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,
334*49cdfc7eSAndroid Build Coastguard Worker 	       va_list va)
335*49cdfc7eSAndroid Build Coastguard Worker {
336*49cdfc7eSAndroid Build Coastguard Worker 	print_result(file, lineno, ttype, fmt, va);
337*49cdfc7eSAndroid Build Coastguard Worker 	update_results(TTYPE_RESULT(ttype));
338*49cdfc7eSAndroid Build Coastguard Worker 
339*49cdfc7eSAndroid Build Coastguard Worker 	/*
340*49cdfc7eSAndroid Build Coastguard Worker 	 * The getpid implementation in some C library versions may cause cloned
341*49cdfc7eSAndroid Build Coastguard Worker 	 * test threads to show the same pid as their parent when CLONE_VM is
342*49cdfc7eSAndroid Build Coastguard Worker 	 * specified but CLONE_THREAD is not. Use direct syscall to avoid
343*49cdfc7eSAndroid Build Coastguard Worker 	 * cleanup running in the child.
344*49cdfc7eSAndroid Build Coastguard Worker 	 */
345*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_getpid() == main_pid)
346*49cdfc7eSAndroid Build Coastguard Worker 		do_test_cleanup();
347*49cdfc7eSAndroid Build Coastguard Worker 
348*49cdfc7eSAndroid Build Coastguard Worker 	if (getpid() == lib_pid)
349*49cdfc7eSAndroid Build Coastguard Worker 		do_exit(TTYPE_RESULT(ttype));
350*49cdfc7eSAndroid Build Coastguard Worker 
351*49cdfc7eSAndroid Build Coastguard Worker 	exit(TTYPE_RESULT(ttype));
352*49cdfc7eSAndroid Build Coastguard Worker }
353*49cdfc7eSAndroid Build Coastguard Worker 
tst_res_(const char * file,const int lineno,int ttype,const char * fmt,...)354*49cdfc7eSAndroid Build Coastguard Worker void tst_res_(const char *file, const int lineno, int ttype,
355*49cdfc7eSAndroid Build Coastguard Worker 	      const char *fmt, ...)
356*49cdfc7eSAndroid Build Coastguard Worker {
357*49cdfc7eSAndroid Build Coastguard Worker 	va_list va;
358*49cdfc7eSAndroid Build Coastguard Worker 
359*49cdfc7eSAndroid Build Coastguard Worker 	if (ttype == TDEBUG && !tdebug)
360*49cdfc7eSAndroid Build Coastguard Worker 		return;
361*49cdfc7eSAndroid Build Coastguard Worker 
362*49cdfc7eSAndroid Build Coastguard Worker 	va_start(va, fmt);
363*49cdfc7eSAndroid Build Coastguard Worker 	tst_vres_(file, lineno, ttype, fmt, va);
364*49cdfc7eSAndroid Build Coastguard Worker 	va_end(va);
365*49cdfc7eSAndroid Build Coastguard Worker }
366*49cdfc7eSAndroid Build Coastguard Worker 
tst_brk_(const char * file,const int lineno,int ttype,const char * fmt,...)367*49cdfc7eSAndroid Build Coastguard Worker void tst_brk_(const char *file, const int lineno, int ttype,
368*49cdfc7eSAndroid Build Coastguard Worker 	      const char *fmt, ...)
369*49cdfc7eSAndroid Build Coastguard Worker {
370*49cdfc7eSAndroid Build Coastguard Worker 	va_list va;
371*49cdfc7eSAndroid Build Coastguard Worker 
372*49cdfc7eSAndroid Build Coastguard Worker 	va_start(va, fmt);
373*49cdfc7eSAndroid Build Coastguard Worker 	tst_brk_handler(file, lineno, ttype, fmt, va);
374*49cdfc7eSAndroid Build Coastguard Worker 	va_end(va);
375*49cdfc7eSAndroid Build Coastguard Worker }
376*49cdfc7eSAndroid Build Coastguard Worker 
tst_printf(const char * const fmt,...)377*49cdfc7eSAndroid Build Coastguard Worker void tst_printf(const char *const fmt, ...)
378*49cdfc7eSAndroid Build Coastguard Worker {
379*49cdfc7eSAndroid Build Coastguard Worker 	va_list va;
380*49cdfc7eSAndroid Build Coastguard Worker 
381*49cdfc7eSAndroid Build Coastguard Worker 	va_start(va, fmt);
382*49cdfc7eSAndroid Build Coastguard Worker 	vdprintf(STDERR_FILENO, fmt, va);
383*49cdfc7eSAndroid Build Coastguard Worker 	va_end(va);
384*49cdfc7eSAndroid Build Coastguard Worker }
385*49cdfc7eSAndroid Build Coastguard Worker 
check_child_status(pid_t pid,int status)386*49cdfc7eSAndroid Build Coastguard Worker static void check_child_status(pid_t pid, int status)
387*49cdfc7eSAndroid Build Coastguard Worker {
388*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
389*49cdfc7eSAndroid Build Coastguard Worker 
390*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFSIGNALED(status)) {
391*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Child (%i) killed by signal %s", pid,
392*49cdfc7eSAndroid Build Coastguard Worker 			tst_strsig(WTERMSIG(status)));
393*49cdfc7eSAndroid Build Coastguard Worker 	}
394*49cdfc7eSAndroid Build Coastguard Worker 
395*49cdfc7eSAndroid Build Coastguard Worker 	if (!(WIFEXITED(status)))
396*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Child (%i) exited abnormally", pid);
397*49cdfc7eSAndroid Build Coastguard Worker 
398*49cdfc7eSAndroid Build Coastguard Worker 	ret = WEXITSTATUS(status);
399*49cdfc7eSAndroid Build Coastguard Worker 	switch (ret) {
400*49cdfc7eSAndroid Build Coastguard Worker 	case TPASS:
401*49cdfc7eSAndroid Build Coastguard Worker 	case TBROK:
402*49cdfc7eSAndroid Build Coastguard Worker 	case TCONF:
403*49cdfc7eSAndroid Build Coastguard Worker 	break;
404*49cdfc7eSAndroid Build Coastguard Worker 	default:
405*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
406*49cdfc7eSAndroid Build Coastguard Worker 	}
407*49cdfc7eSAndroid Build Coastguard Worker }
408*49cdfc7eSAndroid Build Coastguard Worker 
tst_reap_children(void)409*49cdfc7eSAndroid Build Coastguard Worker void tst_reap_children(void)
410*49cdfc7eSAndroid Build Coastguard Worker {
411*49cdfc7eSAndroid Build Coastguard Worker 	int status;
412*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
413*49cdfc7eSAndroid Build Coastguard Worker 
414*49cdfc7eSAndroid Build Coastguard Worker 	for (;;) {
415*49cdfc7eSAndroid Build Coastguard Worker 		pid = wait(&status);
416*49cdfc7eSAndroid Build Coastguard Worker 
417*49cdfc7eSAndroid Build Coastguard Worker 		if (pid > 0) {
418*49cdfc7eSAndroid Build Coastguard Worker 			check_child_status(pid, status);
419*49cdfc7eSAndroid Build Coastguard Worker 			continue;
420*49cdfc7eSAndroid Build Coastguard Worker 		}
421*49cdfc7eSAndroid Build Coastguard Worker 
422*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == ECHILD)
423*49cdfc7eSAndroid Build Coastguard Worker 			break;
424*49cdfc7eSAndroid Build Coastguard Worker 
425*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == EINTR)
426*49cdfc7eSAndroid Build Coastguard Worker 			continue;
427*49cdfc7eSAndroid Build Coastguard Worker 
428*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "wait() failed");
429*49cdfc7eSAndroid Build Coastguard Worker 	}
430*49cdfc7eSAndroid Build Coastguard Worker }
431*49cdfc7eSAndroid Build Coastguard Worker 
432*49cdfc7eSAndroid Build Coastguard Worker 
safe_fork(const char * filename,unsigned int lineno)433*49cdfc7eSAndroid Build Coastguard Worker pid_t safe_fork(const char *filename, unsigned int lineno)
434*49cdfc7eSAndroid Build Coastguard Worker {
435*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
436*49cdfc7eSAndroid Build Coastguard Worker 
437*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->forks_child)
438*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "test.forks_child must be set!");
439*49cdfc7eSAndroid Build Coastguard Worker 
440*49cdfc7eSAndroid Build Coastguard Worker 	tst_flush();
441*49cdfc7eSAndroid Build Coastguard Worker 
442*49cdfc7eSAndroid Build Coastguard Worker 	pid = fork();
443*49cdfc7eSAndroid Build Coastguard Worker 	if (pid < 0)
444*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
445*49cdfc7eSAndroid Build Coastguard Worker 
446*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid)
447*49cdfc7eSAndroid Build Coastguard Worker 		atexit(tst_free_all);
448*49cdfc7eSAndroid Build Coastguard Worker 
449*49cdfc7eSAndroid Build Coastguard Worker 	return pid;
450*49cdfc7eSAndroid Build Coastguard Worker }
451*49cdfc7eSAndroid Build Coastguard Worker 
452*49cdfc7eSAndroid Build Coastguard Worker /* too fast creating namespaces => retrying */
453*49cdfc7eSAndroid Build Coastguard Worker #define TST_CHECK_ENOSPC(x) ((x) >= 0 || !(errno == ENOSPC))
454*49cdfc7eSAndroid Build Coastguard Worker 
safe_clone(const char * file,const int lineno,const struct tst_clone_args * args)455*49cdfc7eSAndroid Build Coastguard Worker pid_t safe_clone(const char *file, const int lineno,
456*49cdfc7eSAndroid Build Coastguard Worker 		 const struct tst_clone_args *args)
457*49cdfc7eSAndroid Build Coastguard Worker {
458*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
459*49cdfc7eSAndroid Build Coastguard Worker 
460*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->forks_child)
461*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "test.forks_child must be set!");
462*49cdfc7eSAndroid Build Coastguard Worker 
463*49cdfc7eSAndroid Build Coastguard Worker 	pid = TST_RETRY_FUNC(tst_clone(args), TST_CHECK_ENOSPC);
464*49cdfc7eSAndroid Build Coastguard Worker 
465*49cdfc7eSAndroid Build Coastguard Worker 	switch (pid) {
466*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
467*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno, TBROK | TERRNO, "clone3 failed");
468*49cdfc7eSAndroid Build Coastguard Worker 		break;
469*49cdfc7eSAndroid Build Coastguard Worker 	case -2:
470*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno, TBROK | TERRNO, "clone failed");
471*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
472*49cdfc7eSAndroid Build Coastguard Worker 	}
473*49cdfc7eSAndroid Build Coastguard Worker 
474*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid)
475*49cdfc7eSAndroid Build Coastguard Worker 		atexit(tst_free_all);
476*49cdfc7eSAndroid Build Coastguard Worker 
477*49cdfc7eSAndroid Build Coastguard Worker 	return pid;
478*49cdfc7eSAndroid Build Coastguard Worker }
479*49cdfc7eSAndroid Build Coastguard Worker 
parse_mul(float * mul,const char * env_name,float min,float max)480*49cdfc7eSAndroid Build Coastguard Worker static void parse_mul(float *mul, const char *env_name, float min, float max)
481*49cdfc7eSAndroid Build Coastguard Worker {
482*49cdfc7eSAndroid Build Coastguard Worker 	char *str_mul;
483*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
484*49cdfc7eSAndroid Build Coastguard Worker 
485*49cdfc7eSAndroid Build Coastguard Worker 	if (*mul > 0)
486*49cdfc7eSAndroid Build Coastguard Worker 		return;
487*49cdfc7eSAndroid Build Coastguard Worker 
488*49cdfc7eSAndroid Build Coastguard Worker 	str_mul = getenv(env_name);
489*49cdfc7eSAndroid Build Coastguard Worker 
490*49cdfc7eSAndroid Build Coastguard Worker 	if (!str_mul) {
491*49cdfc7eSAndroid Build Coastguard Worker 		*mul = 1;
492*49cdfc7eSAndroid Build Coastguard Worker 		return;
493*49cdfc7eSAndroid Build Coastguard Worker 	}
494*49cdfc7eSAndroid Build Coastguard Worker 
495*49cdfc7eSAndroid Build Coastguard Worker 	ret = tst_parse_float(str_mul, mul, min, max);
496*49cdfc7eSAndroid Build Coastguard Worker 	if (ret) {
497*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Failed to parse %s: %s",
498*49cdfc7eSAndroid Build Coastguard Worker 			env_name, tst_strerrno(ret));
499*49cdfc7eSAndroid Build Coastguard Worker 	}
500*49cdfc7eSAndroid Build Coastguard Worker }
501*49cdfc7eSAndroid Build Coastguard Worker 
multiply_runtime(int max_runtime)502*49cdfc7eSAndroid Build Coastguard Worker static int multiply_runtime(int max_runtime)
503*49cdfc7eSAndroid Build Coastguard Worker {
504*49cdfc7eSAndroid Build Coastguard Worker 	static float runtime_mul = -1;
505*49cdfc7eSAndroid Build Coastguard Worker 
506*49cdfc7eSAndroid Build Coastguard Worker 	if (max_runtime <= 0)
507*49cdfc7eSAndroid Build Coastguard Worker 		return max_runtime;
508*49cdfc7eSAndroid Build Coastguard Worker 
509*49cdfc7eSAndroid Build Coastguard Worker 	parse_mul(&runtime_mul, "LTP_RUNTIME_MUL", 0.0099, 100);
510*49cdfc7eSAndroid Build Coastguard Worker 
511*49cdfc7eSAndroid Build Coastguard Worker 	return max_runtime * runtime_mul;
512*49cdfc7eSAndroid Build Coastguard Worker }
513*49cdfc7eSAndroid Build Coastguard Worker 
514*49cdfc7eSAndroid Build Coastguard Worker static struct option {
515*49cdfc7eSAndroid Build Coastguard Worker 	char *optstr;
516*49cdfc7eSAndroid Build Coastguard Worker 	char *help;
517*49cdfc7eSAndroid Build Coastguard Worker } options[] = {
518*49cdfc7eSAndroid Build Coastguard Worker 	{"h",  "-h       Prints this help"},
519*49cdfc7eSAndroid Build Coastguard Worker 	{"i:", "-i n     Execute test n times"},
520*49cdfc7eSAndroid Build Coastguard Worker 	{"I:", "-I x     Execute test for n seconds"},
521*49cdfc7eSAndroid Build Coastguard Worker 	{"D",  "-D       Prints debug information"},
522*49cdfc7eSAndroid Build Coastguard Worker 	{"V",  "-V       Prints LTP version"},
523*49cdfc7eSAndroid Build Coastguard Worker };
524*49cdfc7eSAndroid Build Coastguard Worker 
print_help(void)525*49cdfc7eSAndroid Build Coastguard Worker static void print_help(void)
526*49cdfc7eSAndroid Build Coastguard Worker {
527*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
528*49cdfc7eSAndroid Build Coastguard Worker 	int timeout, runtime;
529*49cdfc7eSAndroid Build Coastguard Worker 
530*49cdfc7eSAndroid Build Coastguard Worker 	/* see doc/User-Guidelines.asciidoc, which lists also shell API variables */
531*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "Environment Variables\n");
532*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "---------------------\n");
533*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "KCONFIG_PATH         Specify kernel config file\n");
534*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "KCONFIG_SKIP_CHECK   Skip kernel config check if variable set (not set by default)\n");
535*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTPROOT              Prefix for installed LTP (default: /opt/ltp)\n");
536*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_COLORIZE_OUTPUT  Force colorized output behaviour (y/1 always, n/0: never)\n");
537*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_DEV              Path to the block device to be used (for .needs_device)\n");
538*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_DEV_FS_TYPE      Filesystem used for testing (default: %s)\n", DEFAULT_FS_TYPE);
539*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_SINGLE_FS_TYPE   Testing only - specifies filesystem instead all supported (for .all_filesystems)\n");
540*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_TIMEOUT_MUL      Timeout multiplier (must be a number >=1)\n");
541*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_RUNTIME_MUL      Runtime multiplier (must be a number >=1)\n");
542*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "LTP_VIRT_OVERRIDE    Overrides virtual machine detection (values: \"\"|kvm|microsoft|xen|zvm)\n");
543*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "TMPDIR               Base directory for template directory (for .needs_tmpdir, default: %s)\n", TEMPDIR);
544*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "\n");
545*49cdfc7eSAndroid Build Coastguard Worker 
546*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "Timeout and runtime\n");
547*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "-------------------\n");
548*49cdfc7eSAndroid Build Coastguard Worker 
549*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->max_runtime) {
550*49cdfc7eSAndroid Build Coastguard Worker 		runtime = multiply_runtime(tst_test->max_runtime);
551*49cdfc7eSAndroid Build Coastguard Worker 
552*49cdfc7eSAndroid Build Coastguard Worker 		if (runtime == TST_UNLIMITED_RUNTIME) {
553*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "Test iteration runtime is not limited\n");
554*49cdfc7eSAndroid Build Coastguard Worker 		} else {
555*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "Test iteration runtime cap %ih %im %is\n",
556*49cdfc7eSAndroid Build Coastguard Worker 				runtime/3600, (runtime%3600)/60, runtime % 60);
557*49cdfc7eSAndroid Build Coastguard Worker 		}
558*49cdfc7eSAndroid Build Coastguard Worker 	}
559*49cdfc7eSAndroid Build Coastguard Worker 
560*49cdfc7eSAndroid Build Coastguard Worker 	timeout = tst_multiply_timeout(DEFAULT_TIMEOUT);
561*49cdfc7eSAndroid Build Coastguard Worker 
562*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "Test timeout (not including runtime) %ih %im %is\n",
563*49cdfc7eSAndroid Build Coastguard Worker 		timeout/3600, (timeout%3600)/60, timeout % 60);
564*49cdfc7eSAndroid Build Coastguard Worker 
565*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "\n");
566*49cdfc7eSAndroid Build Coastguard Worker 
567*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "Options\n");
568*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "-------\n");
569*49cdfc7eSAndroid Build Coastguard Worker 
570*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(options); i++)
571*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "%s\n", options[i].help);
572*49cdfc7eSAndroid Build Coastguard Worker 
573*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->options)
574*49cdfc7eSAndroid Build Coastguard Worker 		return;
575*49cdfc7eSAndroid Build Coastguard Worker 
576*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; tst_test->options[i].optstr; i++) {
577*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "-%c\t %s\n",
578*49cdfc7eSAndroid Build Coastguard Worker 			tst_test->options[i].optstr[0],
579*49cdfc7eSAndroid Build Coastguard Worker 			tst_test->options[i].help);
580*49cdfc7eSAndroid Build Coastguard Worker 	}
581*49cdfc7eSAndroid Build Coastguard Worker }
582*49cdfc7eSAndroid Build Coastguard Worker 
print_test_tags(void)583*49cdfc7eSAndroid Build Coastguard Worker static void print_test_tags(void)
584*49cdfc7eSAndroid Build Coastguard Worker {
585*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
586*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_tag *tags = tst_test->tags;
587*49cdfc7eSAndroid Build Coastguard Worker 
588*49cdfc7eSAndroid Build Coastguard Worker 	if (!tags)
589*49cdfc7eSAndroid Build Coastguard Worker 		return;
590*49cdfc7eSAndroid Build Coastguard Worker 
591*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "\nTags\n");
592*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "----\n");
593*49cdfc7eSAndroid Build Coastguard Worker 
594*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; tags[i].name; i++) {
595*49cdfc7eSAndroid Build Coastguard Worker 		if (!strcmp(tags[i].name, "CVE"))
596*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, CVE_DB_URL "%s\n", tags[i].value);
597*49cdfc7eSAndroid Build Coastguard Worker 		else if (!strcmp(tags[i].name, "linux-git"))
598*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, LINUX_GIT_URL "%s\n", tags[i].value);
599*49cdfc7eSAndroid Build Coastguard Worker 		else if (!strcmp(tags[i].name, "linux-stable-git"))
600*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, LINUX_STABLE_GIT_URL "%s\n", tags[i].value);
601*49cdfc7eSAndroid Build Coastguard Worker 		else if (!strcmp(tags[i].name, "glibc-git"))
602*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, GLIBC_GIT_URL "%s\n", tags[i].value);
603*49cdfc7eSAndroid Build Coastguard Worker 		else if (!strcmp(tags[i].name, "musl-git"))
604*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, MUSL_GIT_URL "%s\n", tags[i].value);
605*49cdfc7eSAndroid Build Coastguard Worker 		else
606*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "%s: %s\n", tags[i].name, tags[i].value);
607*49cdfc7eSAndroid Build Coastguard Worker 	}
608*49cdfc7eSAndroid Build Coastguard Worker 
609*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "\n");
610*49cdfc7eSAndroid Build Coastguard Worker }
611*49cdfc7eSAndroid Build Coastguard Worker 
check_option_collision(void)612*49cdfc7eSAndroid Build Coastguard Worker static void check_option_collision(void)
613*49cdfc7eSAndroid Build Coastguard Worker {
614*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i, j;
615*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_option *toptions = tst_test->options;
616*49cdfc7eSAndroid Build Coastguard Worker 
617*49cdfc7eSAndroid Build Coastguard Worker 	if (!toptions)
618*49cdfc7eSAndroid Build Coastguard Worker 		return;
619*49cdfc7eSAndroid Build Coastguard Worker 
620*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; toptions[i].optstr; i++) {
621*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < ARRAY_SIZE(options); j++) {
622*49cdfc7eSAndroid Build Coastguard Worker 			if (toptions[i].optstr[0] == options[j].optstr[0]) {
623*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK, "Option collision '%s'",
624*49cdfc7eSAndroid Build Coastguard Worker 					options[j].help);
625*49cdfc7eSAndroid Build Coastguard Worker 			}
626*49cdfc7eSAndroid Build Coastguard Worker 		}
627*49cdfc7eSAndroid Build Coastguard Worker 	}
628*49cdfc7eSAndroid Build Coastguard Worker }
629*49cdfc7eSAndroid Build Coastguard Worker 
count_options(void)630*49cdfc7eSAndroid Build Coastguard Worker static unsigned int count_options(void)
631*49cdfc7eSAndroid Build Coastguard Worker {
632*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
633*49cdfc7eSAndroid Build Coastguard Worker 
634*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->options)
635*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
636*49cdfc7eSAndroid Build Coastguard Worker 
637*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; tst_test->options[i].optstr; i++)
638*49cdfc7eSAndroid Build Coastguard Worker 		;
639*49cdfc7eSAndroid Build Coastguard Worker 
640*49cdfc7eSAndroid Build Coastguard Worker 	return i;
641*49cdfc7eSAndroid Build Coastguard Worker }
642*49cdfc7eSAndroid Build Coastguard Worker 
parse_topt(unsigned int topts_len,int opt,char * optarg)643*49cdfc7eSAndroid Build Coastguard Worker static void parse_topt(unsigned int topts_len, int opt, char *optarg)
644*49cdfc7eSAndroid Build Coastguard Worker {
645*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
646*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_option *toptions = tst_test->options;
647*49cdfc7eSAndroid Build Coastguard Worker 
648*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < topts_len; i++) {
649*49cdfc7eSAndroid Build Coastguard Worker 		if (toptions[i].optstr[0] == opt)
650*49cdfc7eSAndroid Build Coastguard Worker 			break;
651*49cdfc7eSAndroid Build Coastguard Worker 	}
652*49cdfc7eSAndroid Build Coastguard Worker 
653*49cdfc7eSAndroid Build Coastguard Worker 	if (i >= topts_len)
654*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
655*49cdfc7eSAndroid Build Coastguard Worker 
656*49cdfc7eSAndroid Build Coastguard Worker 	if (*toptions[i].arg)
657*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN, "Option -%c passed multiple times", opt);
658*49cdfc7eSAndroid Build Coastguard Worker 
659*49cdfc7eSAndroid Build Coastguard Worker 	*(toptions[i].arg) = optarg ? optarg : "";
660*49cdfc7eSAndroid Build Coastguard Worker }
661*49cdfc7eSAndroid Build Coastguard Worker 
parse_opts(int argc,char * argv[])662*49cdfc7eSAndroid Build Coastguard Worker static void parse_opts(int argc, char *argv[])
663*49cdfc7eSAndroid Build Coastguard Worker {
664*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i, topts_len = count_options();
665*49cdfc7eSAndroid Build Coastguard Worker 	char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
666*49cdfc7eSAndroid Build Coastguard Worker 	int opt;
667*49cdfc7eSAndroid Build Coastguard Worker 
668*49cdfc7eSAndroid Build Coastguard Worker 	check_option_collision();
669*49cdfc7eSAndroid Build Coastguard Worker 
670*49cdfc7eSAndroid Build Coastguard Worker 	optstr[0] = 0;
671*49cdfc7eSAndroid Build Coastguard Worker 
672*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(options); i++)
673*49cdfc7eSAndroid Build Coastguard Worker 		strcat(optstr, options[i].optstr);
674*49cdfc7eSAndroid Build Coastguard Worker 
675*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < topts_len; i++)
676*49cdfc7eSAndroid Build Coastguard Worker 		strcat(optstr, tst_test->options[i].optstr);
677*49cdfc7eSAndroid Build Coastguard Worker 
678*49cdfc7eSAndroid Build Coastguard Worker 	while ((opt = getopt(argc, argv, optstr)) > 0) {
679*49cdfc7eSAndroid Build Coastguard Worker 		switch (opt) {
680*49cdfc7eSAndroid Build Coastguard Worker 		case '?':
681*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
682*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK, "Invalid option");
683*49cdfc7eSAndroid Build Coastguard Worker 		break;
684*49cdfc7eSAndroid Build Coastguard Worker 		case 'D':
685*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "Enabling debug info");
686*49cdfc7eSAndroid Build Coastguard Worker 			tdebug = 1;
687*49cdfc7eSAndroid Build Coastguard Worker 		break;
688*49cdfc7eSAndroid Build Coastguard Worker 		case 'h':
689*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
690*49cdfc7eSAndroid Build Coastguard Worker 			print_test_tags();
691*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
692*49cdfc7eSAndroid Build Coastguard Worker 		case 'i':
693*49cdfc7eSAndroid Build Coastguard Worker 			iterations = SAFE_STRTOL(optarg, 0, INT_MAX);
694*49cdfc7eSAndroid Build Coastguard Worker 		break;
695*49cdfc7eSAndroid Build Coastguard Worker 		case 'I':
696*49cdfc7eSAndroid Build Coastguard Worker 			if (tst_test->max_runtime > 0)
697*49cdfc7eSAndroid Build Coastguard Worker 				tst_test->max_runtime = SAFE_STRTOL(optarg, 1, INT_MAX);
698*49cdfc7eSAndroid Build Coastguard Worker 			else
699*49cdfc7eSAndroid Build Coastguard Worker 				duration = SAFE_STRTOF(optarg, 0.1, HUGE_VALF);
700*49cdfc7eSAndroid Build Coastguard Worker 		break;
701*49cdfc7eSAndroid Build Coastguard Worker 		case 'V':
702*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "LTP version: " LTP_VERSION "\n");
703*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
704*49cdfc7eSAndroid Build Coastguard Worker 		break;
705*49cdfc7eSAndroid Build Coastguard Worker 		default:
706*49cdfc7eSAndroid Build Coastguard Worker 			parse_topt(topts_len, opt, optarg);
707*49cdfc7eSAndroid Build Coastguard Worker 		}
708*49cdfc7eSAndroid Build Coastguard Worker 	}
709*49cdfc7eSAndroid Build Coastguard Worker 
710*49cdfc7eSAndroid Build Coastguard Worker 	if (optind < argc)
711*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
712*49cdfc7eSAndroid Build Coastguard Worker }
713*49cdfc7eSAndroid Build Coastguard Worker 
tst_parse_int(const char * str,int * val,int min,int max)714*49cdfc7eSAndroid Build Coastguard Worker int tst_parse_int(const char *str, int *val, int min, int max)
715*49cdfc7eSAndroid Build Coastguard Worker {
716*49cdfc7eSAndroid Build Coastguard Worker 	long rval;
717*49cdfc7eSAndroid Build Coastguard Worker 
718*49cdfc7eSAndroid Build Coastguard Worker 	if (!str)
719*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
720*49cdfc7eSAndroid Build Coastguard Worker 
721*49cdfc7eSAndroid Build Coastguard Worker 	int ret = tst_parse_long(str, &rval, min, max);
722*49cdfc7eSAndroid Build Coastguard Worker 
723*49cdfc7eSAndroid Build Coastguard Worker 	if (ret)
724*49cdfc7eSAndroid Build Coastguard Worker 		return ret;
725*49cdfc7eSAndroid Build Coastguard Worker 
726*49cdfc7eSAndroid Build Coastguard Worker 	*val = (int)rval;
727*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
728*49cdfc7eSAndroid Build Coastguard Worker }
729*49cdfc7eSAndroid Build Coastguard Worker 
tst_parse_long(const char * str,long * val,long min,long max)730*49cdfc7eSAndroid Build Coastguard Worker int tst_parse_long(const char *str, long *val, long min, long max)
731*49cdfc7eSAndroid Build Coastguard Worker {
732*49cdfc7eSAndroid Build Coastguard Worker 	long rval;
733*49cdfc7eSAndroid Build Coastguard Worker 	char *end;
734*49cdfc7eSAndroid Build Coastguard Worker 
735*49cdfc7eSAndroid Build Coastguard Worker 	if (!str)
736*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
737*49cdfc7eSAndroid Build Coastguard Worker 
738*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
739*49cdfc7eSAndroid Build Coastguard Worker 	rval = strtol(str, &end, 10);
740*49cdfc7eSAndroid Build Coastguard Worker 
741*49cdfc7eSAndroid Build Coastguard Worker 	if (str == end || *end != '\0')
742*49cdfc7eSAndroid Build Coastguard Worker 		return EINVAL;
743*49cdfc7eSAndroid Build Coastguard Worker 
744*49cdfc7eSAndroid Build Coastguard Worker 	if (errno)
745*49cdfc7eSAndroid Build Coastguard Worker 		return errno;
746*49cdfc7eSAndroid Build Coastguard Worker 
747*49cdfc7eSAndroid Build Coastguard Worker 	if (rval > max || rval < min)
748*49cdfc7eSAndroid Build Coastguard Worker 		return ERANGE;
749*49cdfc7eSAndroid Build Coastguard Worker 
750*49cdfc7eSAndroid Build Coastguard Worker 	*val = rval;
751*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
752*49cdfc7eSAndroid Build Coastguard Worker }
753*49cdfc7eSAndroid Build Coastguard Worker 
tst_parse_float(const char * str,float * val,float min,float max)754*49cdfc7eSAndroid Build Coastguard Worker int tst_parse_float(const char *str, float *val, float min, float max)
755*49cdfc7eSAndroid Build Coastguard Worker {
756*49cdfc7eSAndroid Build Coastguard Worker 	double rval;
757*49cdfc7eSAndroid Build Coastguard Worker 	char *end;
758*49cdfc7eSAndroid Build Coastguard Worker 
759*49cdfc7eSAndroid Build Coastguard Worker 	if (!str)
760*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
761*49cdfc7eSAndroid Build Coastguard Worker 
762*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
763*49cdfc7eSAndroid Build Coastguard Worker 	rval = strtod(str, &end);
764*49cdfc7eSAndroid Build Coastguard Worker 
765*49cdfc7eSAndroid Build Coastguard Worker 	if (str == end || *end != '\0')
766*49cdfc7eSAndroid Build Coastguard Worker 		return EINVAL;
767*49cdfc7eSAndroid Build Coastguard Worker 
768*49cdfc7eSAndroid Build Coastguard Worker 	if (errno)
769*49cdfc7eSAndroid Build Coastguard Worker 		return errno;
770*49cdfc7eSAndroid Build Coastguard Worker 
771*49cdfc7eSAndroid Build Coastguard Worker 	if (rval > (double)max || rval < (double)min)
772*49cdfc7eSAndroid Build Coastguard Worker 		return ERANGE;
773*49cdfc7eSAndroid Build Coastguard Worker 
774*49cdfc7eSAndroid Build Coastguard Worker 	*val = (float)rval;
775*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
776*49cdfc7eSAndroid Build Coastguard Worker }
777*49cdfc7eSAndroid Build Coastguard Worker 
tst_parse_filesize(const char * str,long long * val,long long min,long long max)778*49cdfc7eSAndroid Build Coastguard Worker int tst_parse_filesize(const char *str, long long *val, long long min, long long max)
779*49cdfc7eSAndroid Build Coastguard Worker {
780*49cdfc7eSAndroid Build Coastguard Worker 	long long rval;
781*49cdfc7eSAndroid Build Coastguard Worker 	char *end;
782*49cdfc7eSAndroid Build Coastguard Worker 
783*49cdfc7eSAndroid Build Coastguard Worker 	if (!str)
784*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
785*49cdfc7eSAndroid Build Coastguard Worker 
786*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
787*49cdfc7eSAndroid Build Coastguard Worker 	rval = strtoll(str, &end, 0);
788*49cdfc7eSAndroid Build Coastguard Worker 
789*49cdfc7eSAndroid Build Coastguard Worker 	if (str == end || (end[0] && end[1]))
790*49cdfc7eSAndroid Build Coastguard Worker 		return EINVAL;
791*49cdfc7eSAndroid Build Coastguard Worker 
792*49cdfc7eSAndroid Build Coastguard Worker 	if (errno)
793*49cdfc7eSAndroid Build Coastguard Worker 		return errno;
794*49cdfc7eSAndroid Build Coastguard Worker 
795*49cdfc7eSAndroid Build Coastguard Worker 	switch (*end) {
796*49cdfc7eSAndroid Build Coastguard Worker 	case 'g':
797*49cdfc7eSAndroid Build Coastguard Worker 	case 'G':
798*49cdfc7eSAndroid Build Coastguard Worker 		rval *= (1024 * 1024 * 1024);
799*49cdfc7eSAndroid Build Coastguard Worker 		break;
800*49cdfc7eSAndroid Build Coastguard Worker 	case 'm':
801*49cdfc7eSAndroid Build Coastguard Worker 	case 'M':
802*49cdfc7eSAndroid Build Coastguard Worker 		rval *= (1024 * 1024);
803*49cdfc7eSAndroid Build Coastguard Worker 		break;
804*49cdfc7eSAndroid Build Coastguard Worker 	case 'k':
805*49cdfc7eSAndroid Build Coastguard Worker 	case 'K':
806*49cdfc7eSAndroid Build Coastguard Worker 		rval *= 1024;
807*49cdfc7eSAndroid Build Coastguard Worker 		break;
808*49cdfc7eSAndroid Build Coastguard Worker 	default:
809*49cdfc7eSAndroid Build Coastguard Worker 		break;
810*49cdfc7eSAndroid Build Coastguard Worker 	}
811*49cdfc7eSAndroid Build Coastguard Worker 
812*49cdfc7eSAndroid Build Coastguard Worker 	if (rval > max || rval < min)
813*49cdfc7eSAndroid Build Coastguard Worker 		return ERANGE;
814*49cdfc7eSAndroid Build Coastguard Worker 
815*49cdfc7eSAndroid Build Coastguard Worker 	*val = rval;
816*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
817*49cdfc7eSAndroid Build Coastguard Worker }
818*49cdfc7eSAndroid Build Coastguard Worker 
print_colored(const char * str)819*49cdfc7eSAndroid Build Coastguard Worker static void print_colored(const char *str)
820*49cdfc7eSAndroid Build Coastguard Worker {
821*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_color_enabled(STDOUT_FILENO))
822*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "%s%s%s", ANSI_COLOR_YELLOW, str, ANSI_COLOR_RESET);
823*49cdfc7eSAndroid Build Coastguard Worker 	else
824*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "%s", str);
825*49cdfc7eSAndroid Build Coastguard Worker }
826*49cdfc7eSAndroid Build Coastguard Worker 
print_failure_hint(const char * tag,const char * hint,const char * url)827*49cdfc7eSAndroid Build Coastguard Worker static void print_failure_hint(const char *tag, const char *hint,
828*49cdfc7eSAndroid Build Coastguard Worker 			       const char *url)
829*49cdfc7eSAndroid Build Coastguard Worker {
830*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_tag *tags = tst_test->tags;
831*49cdfc7eSAndroid Build Coastguard Worker 
832*49cdfc7eSAndroid Build Coastguard Worker 	if (!tags)
833*49cdfc7eSAndroid Build Coastguard Worker 		return;
834*49cdfc7eSAndroid Build Coastguard Worker 
835*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
836*49cdfc7eSAndroid Build Coastguard Worker 	int hint_printed = 0;
837*49cdfc7eSAndroid Build Coastguard Worker 
838*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; tags[i].name; i++) {
839*49cdfc7eSAndroid Build Coastguard Worker 		if (!strcmp(tags[i].name, tag)) {
840*49cdfc7eSAndroid Build Coastguard Worker 			if (!hint_printed) {
841*49cdfc7eSAndroid Build Coastguard Worker 				hint_printed = 1;
842*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr, "\n");
843*49cdfc7eSAndroid Build Coastguard Worker 				print_colored("HINT: ");
844*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr, "You _MAY_ be %s:\n\n", hint);
845*49cdfc7eSAndroid Build Coastguard Worker 			}
846*49cdfc7eSAndroid Build Coastguard Worker 
847*49cdfc7eSAndroid Build Coastguard Worker 			if (url)
848*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr, "%s%s\n", url, tags[i].value);
849*49cdfc7eSAndroid Build Coastguard Worker 			else
850*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr, "%s\n", tags[i].value);
851*49cdfc7eSAndroid Build Coastguard Worker 		}
852*49cdfc7eSAndroid Build Coastguard Worker 	}
853*49cdfc7eSAndroid Build Coastguard Worker }
854*49cdfc7eSAndroid Build Coastguard Worker 
855*49cdfc7eSAndroid Build Coastguard Worker /* update also docparse/testinfo.pl */
print_failure_hints(void)856*49cdfc7eSAndroid Build Coastguard Worker static void print_failure_hints(void)
857*49cdfc7eSAndroid Build Coastguard Worker {
858*49cdfc7eSAndroid Build Coastguard Worker 	print_failure_hint("linux-git", "missing kernel fixes", LINUX_GIT_URL);
859*49cdfc7eSAndroid Build Coastguard Worker 	print_failure_hint("linux-stable-git", "missing stable kernel fixes",
860*49cdfc7eSAndroid Build Coastguard Worker 					   LINUX_STABLE_GIT_URL);
861*49cdfc7eSAndroid Build Coastguard Worker 	print_failure_hint("glibc-git", "missing glibc fixes", GLIBC_GIT_URL);
862*49cdfc7eSAndroid Build Coastguard Worker 	print_failure_hint("musl-git", "missing musl fixes", MUSL_GIT_URL);
863*49cdfc7eSAndroid Build Coastguard Worker 	print_failure_hint("CVE", "vulnerable to CVE(s)", CVE_DB_URL);
864*49cdfc7eSAndroid Build Coastguard Worker 	print_failure_hint("known-fail", "hit by known kernel failures", NULL);
865*49cdfc7eSAndroid Build Coastguard Worker }
866*49cdfc7eSAndroid Build Coastguard Worker 
do_exit(int ret)867*49cdfc7eSAndroid Build Coastguard Worker static void do_exit(int ret)
868*49cdfc7eSAndroid Build Coastguard Worker {
869*49cdfc7eSAndroid Build Coastguard Worker 	if (results) {
870*49cdfc7eSAndroid Build Coastguard Worker 		if (results->passed && ret == TCONF)
871*49cdfc7eSAndroid Build Coastguard Worker 			ret = 0;
872*49cdfc7eSAndroid Build Coastguard Worker 
873*49cdfc7eSAndroid Build Coastguard Worker 		if (results->failed) {
874*49cdfc7eSAndroid Build Coastguard Worker 			ret |= TFAIL;
875*49cdfc7eSAndroid Build Coastguard Worker 			print_failure_hints();
876*49cdfc7eSAndroid Build Coastguard Worker 		}
877*49cdfc7eSAndroid Build Coastguard Worker 
878*49cdfc7eSAndroid Build Coastguard Worker 		if (results->skipped && !results->passed)
879*49cdfc7eSAndroid Build Coastguard Worker 			ret |= TCONF;
880*49cdfc7eSAndroid Build Coastguard Worker 
881*49cdfc7eSAndroid Build Coastguard Worker 		if (results->warnings)
882*49cdfc7eSAndroid Build Coastguard Worker 			ret |= TWARN;
883*49cdfc7eSAndroid Build Coastguard Worker 
884*49cdfc7eSAndroid Build Coastguard Worker 		if (results->broken) {
885*49cdfc7eSAndroid Build Coastguard Worker 			ret |= TBROK;
886*49cdfc7eSAndroid Build Coastguard Worker 			print_failure_hints();
887*49cdfc7eSAndroid Build Coastguard Worker 		}
888*49cdfc7eSAndroid Build Coastguard Worker 
889*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "\nSummary:\n");
890*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "passed   %d\n", results->passed);
891*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "failed   %d\n", results->failed);
892*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "broken   %d\n", results->broken);
893*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "skipped  %d\n", results->skipped);
894*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "warnings %d\n", results->warnings);
895*49cdfc7eSAndroid Build Coastguard Worker 	}
896*49cdfc7eSAndroid Build Coastguard Worker 
897*49cdfc7eSAndroid Build Coastguard Worker 	do_cleanup();
898*49cdfc7eSAndroid Build Coastguard Worker 
899*49cdfc7eSAndroid Build Coastguard Worker 	exit(ret);
900*49cdfc7eSAndroid Build Coastguard Worker }
901*49cdfc7eSAndroid Build Coastguard Worker 
check_kver(void)902*49cdfc7eSAndroid Build Coastguard Worker void check_kver(void)
903*49cdfc7eSAndroid Build Coastguard Worker {
904*49cdfc7eSAndroid Build Coastguard Worker 	int v1, v2, v3;
905*49cdfc7eSAndroid Build Coastguard Worker 
906*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
907*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN,
908*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid kernel version %s, expected %%d.%%d.%%d",
909*49cdfc7eSAndroid Build Coastguard Worker 			tst_test->min_kver);
910*49cdfc7eSAndroid Build Coastguard Worker 	}
911*49cdfc7eSAndroid Build Coastguard Worker 
912*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_kvercmp(v1, v2, v3) < 0) {
913*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "The test requires kernel %s or newer",
914*49cdfc7eSAndroid Build Coastguard Worker 			tst_test->min_kver);
915*49cdfc7eSAndroid Build Coastguard Worker 	}
916*49cdfc7eSAndroid Build Coastguard Worker }
917*49cdfc7eSAndroid Build Coastguard Worker 
results_equal(struct results * a,struct results * b)918*49cdfc7eSAndroid Build Coastguard Worker static int results_equal(struct results *a, struct results *b)
919*49cdfc7eSAndroid Build Coastguard Worker {
920*49cdfc7eSAndroid Build Coastguard Worker 	if (a->passed != b->passed)
921*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
922*49cdfc7eSAndroid Build Coastguard Worker 
923*49cdfc7eSAndroid Build Coastguard Worker 	if (a->failed != b->failed)
924*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
925*49cdfc7eSAndroid Build Coastguard Worker 
926*49cdfc7eSAndroid Build Coastguard Worker 	if (a->skipped != b->skipped)
927*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
928*49cdfc7eSAndroid Build Coastguard Worker 
929*49cdfc7eSAndroid Build Coastguard Worker 	if (a->broken != b->broken)
930*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
931*49cdfc7eSAndroid Build Coastguard Worker 
932*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
933*49cdfc7eSAndroid Build Coastguard Worker }
934*49cdfc7eSAndroid Build Coastguard Worker 
needs_tmpdir(void)935*49cdfc7eSAndroid Build Coastguard Worker static int needs_tmpdir(void)
936*49cdfc7eSAndroid Build Coastguard Worker {
937*49cdfc7eSAndroid Build Coastguard Worker 	return tst_test->needs_tmpdir ||
938*49cdfc7eSAndroid Build Coastguard Worker 	       tst_test->needs_device ||
939*49cdfc7eSAndroid Build Coastguard Worker 	       tst_test->mntpoint ||
940*49cdfc7eSAndroid Build Coastguard Worker 	       tst_test->resource_files ||
941*49cdfc7eSAndroid Build Coastguard Worker 	       tst_test->needs_checkpoints;
942*49cdfc7eSAndroid Build Coastguard Worker }
943*49cdfc7eSAndroid Build Coastguard Worker 
copy_resources(void)944*49cdfc7eSAndroid Build Coastguard Worker static void copy_resources(void)
945*49cdfc7eSAndroid Build Coastguard Worker {
946*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
947*49cdfc7eSAndroid Build Coastguard Worker 
948*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; tst_test->resource_files[i]; i++)
949*49cdfc7eSAndroid Build Coastguard Worker 		TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
950*49cdfc7eSAndroid Build Coastguard Worker }
951*49cdfc7eSAndroid Build Coastguard Worker 
get_tid(char * argv[])952*49cdfc7eSAndroid Build Coastguard Worker static const char *get_tid(char *argv[])
953*49cdfc7eSAndroid Build Coastguard Worker {
954*49cdfc7eSAndroid Build Coastguard Worker 	char *p;
955*49cdfc7eSAndroid Build Coastguard Worker 
956*49cdfc7eSAndroid Build Coastguard Worker 	if (!argv[0] || !argv[0][0]) {
957*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "argv[0] is empty!");
958*49cdfc7eSAndroid Build Coastguard Worker 		return "ltp_empty_argv";
959*49cdfc7eSAndroid Build Coastguard Worker 	}
960*49cdfc7eSAndroid Build Coastguard Worker 
961*49cdfc7eSAndroid Build Coastguard Worker 	p = strrchr(argv[0], '/');
962*49cdfc7eSAndroid Build Coastguard Worker 	if (p)
963*49cdfc7eSAndroid Build Coastguard Worker 		return p+1;
964*49cdfc7eSAndroid Build Coastguard Worker 
965*49cdfc7eSAndroid Build Coastguard Worker 	return argv[0];
966*49cdfc7eSAndroid Build Coastguard Worker }
967*49cdfc7eSAndroid Build Coastguard Worker 
968*49cdfc7eSAndroid Build Coastguard Worker static struct tst_device tdev;
969*49cdfc7eSAndroid Build Coastguard Worker struct tst_device *tst_device;
970*49cdfc7eSAndroid Build Coastguard Worker 
assert_test_fn(void)971*49cdfc7eSAndroid Build Coastguard Worker static void assert_test_fn(void)
972*49cdfc7eSAndroid Build Coastguard Worker {
973*49cdfc7eSAndroid Build Coastguard Worker 	int cnt = 0;
974*49cdfc7eSAndroid Build Coastguard Worker 
975*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->test)
976*49cdfc7eSAndroid Build Coastguard Worker 		cnt++;
977*49cdfc7eSAndroid Build Coastguard Worker 
978*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->test_all)
979*49cdfc7eSAndroid Build Coastguard Worker 		cnt++;
980*49cdfc7eSAndroid Build Coastguard Worker 
981*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->sample)
982*49cdfc7eSAndroid Build Coastguard Worker 		cnt++;
983*49cdfc7eSAndroid Build Coastguard Worker 
984*49cdfc7eSAndroid Build Coastguard Worker 	if (!cnt)
985*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "No test function specified");
986*49cdfc7eSAndroid Build Coastguard Worker 
987*49cdfc7eSAndroid Build Coastguard Worker 	if (cnt != 1)
988*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "You can define only one test function");
989*49cdfc7eSAndroid Build Coastguard Worker 
990*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->test && !tst_test->tcnt)
991*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
992*49cdfc7eSAndroid Build Coastguard Worker 
993*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->test && tst_test->tcnt)
994*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "You can define tcnt only for test()");
995*49cdfc7eSAndroid Build Coastguard Worker }
996*49cdfc7eSAndroid Build Coastguard Worker 
prepare_and_mount_ro_fs(const char * dev,const char * mntpoint,const char * fs_type)997*49cdfc7eSAndroid Build Coastguard Worker static int prepare_and_mount_ro_fs(const char *dev, const char *mntpoint,
998*49cdfc7eSAndroid Build Coastguard Worker 				   const char *fs_type)
999*49cdfc7eSAndroid Build Coastguard Worker {
1000*49cdfc7eSAndroid Build Coastguard Worker 	char buf[PATH_MAX];
1001*49cdfc7eSAndroid Build Coastguard Worker 
1002*49cdfc7eSAndroid Build Coastguard Worker 	if (mount(dev, mntpoint, fs_type, 0, NULL)) {
1003*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
1004*49cdfc7eSAndroid Build Coastguard Worker 			dev, mntpoint, fs_type);
1005*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
1006*49cdfc7eSAndroid Build Coastguard Worker 	}
1007*49cdfc7eSAndroid Build Coastguard Worker 
1008*49cdfc7eSAndroid Build Coastguard Worker 	mntpoint_mounted = 1;
1009*49cdfc7eSAndroid Build Coastguard Worker 
1010*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
1011*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(buf, 0777);
1012*49cdfc7eSAndroid Build Coastguard Worker 
1013*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(buf, sizeof(buf), "%s/file", mntpoint);
1014*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(buf, "file content");
1015*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(buf, 0777);
1016*49cdfc7eSAndroid Build Coastguard Worker 
1017*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
1018*49cdfc7eSAndroid Build Coastguard Worker 
1019*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
1020*49cdfc7eSAndroid Build Coastguard Worker }
1021*49cdfc7eSAndroid Build Coastguard Worker 
prepare_and_mount_dev_fs(const char * mntpoint)1022*49cdfc7eSAndroid Build Coastguard Worker static void prepare_and_mount_dev_fs(const char *mntpoint)
1023*49cdfc7eSAndroid Build Coastguard Worker {
1024*49cdfc7eSAndroid Build Coastguard Worker 	const char *flags[] = {"nodev", NULL};
1025*49cdfc7eSAndroid Build Coastguard Worker 	int mounted_nodev;
1026*49cdfc7eSAndroid Build Coastguard Worker 
1027*49cdfc7eSAndroid Build Coastguard Worker 	mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
1028*49cdfc7eSAndroid Build Coastguard Worker 	if (mounted_nodev) {
1029*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
1030*49cdfc7eSAndroid Build Coastguard Worker 			"mounting tmpfs without nodev on %s", mntpoint);
1031*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
1032*49cdfc7eSAndroid Build Coastguard Worker 		mntpoint_mounted = 1;
1033*49cdfc7eSAndroid Build Coastguard Worker 	}
1034*49cdfc7eSAndroid Build Coastguard Worker }
1035*49cdfc7eSAndroid Build Coastguard Worker 
prepare_and_mount_hugetlb_fs(void)1036*49cdfc7eSAndroid Build Coastguard Worker static void prepare_and_mount_hugetlb_fs(void)
1037*49cdfc7eSAndroid Build Coastguard Worker {
1038*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT("none", tst_test->mntpoint, "hugetlbfs", 0, NULL);
1039*49cdfc7eSAndroid Build Coastguard Worker 	mntpoint_mounted = 1;
1040*49cdfc7eSAndroid Build Coastguard Worker }
1041*49cdfc7eSAndroid Build Coastguard Worker 
tst_creat_unlinked(const char * path,int flags)1042*49cdfc7eSAndroid Build Coastguard Worker int tst_creat_unlinked(const char *path, int flags)
1043*49cdfc7eSAndroid Build Coastguard Worker {
1044*49cdfc7eSAndroid Build Coastguard Worker 	char template[PATH_MAX];
1045*49cdfc7eSAndroid Build Coastguard Worker 	int len, c, range;
1046*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
1047*49cdfc7eSAndroid Build Coastguard Worker 	int start[3] = {'0', 'a', 'A'};
1048*49cdfc7eSAndroid Build Coastguard Worker 
1049*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(template, PATH_MAX, "%s/ltp_%.3sXXXXXX",
1050*49cdfc7eSAndroid Build Coastguard Worker 			path, tid);
1051*49cdfc7eSAndroid Build Coastguard Worker 
1052*49cdfc7eSAndroid Build Coastguard Worker 	len = strlen(template) - 1;
1053*49cdfc7eSAndroid Build Coastguard Worker 	while (template[len] == 'X') {
1054*49cdfc7eSAndroid Build Coastguard Worker 		c = rand() % 3;
1055*49cdfc7eSAndroid Build Coastguard Worker 		range = start[c] == '0' ? 10 : 26;
1056*49cdfc7eSAndroid Build Coastguard Worker 		c = start[c] + (rand() % range);
1057*49cdfc7eSAndroid Build Coastguard Worker 		template[len--] = (char)c;
1058*49cdfc7eSAndroid Build Coastguard Worker 	}
1059*49cdfc7eSAndroid Build Coastguard Worker 
1060*49cdfc7eSAndroid Build Coastguard Worker 	flags |= O_CREAT|O_EXCL|O_RDWR;
1061*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(template, flags);
1062*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(template);
1063*49cdfc7eSAndroid Build Coastguard Worker 	return fd;
1064*49cdfc7eSAndroid Build Coastguard Worker }
1065*49cdfc7eSAndroid Build Coastguard Worker 
limit_tmpfs_mount_size(const char * mnt_data,char * buf,size_t buf_size,const char * fs_type)1066*49cdfc7eSAndroid Build Coastguard Worker static const char *limit_tmpfs_mount_size(const char *mnt_data,
1067*49cdfc7eSAndroid Build Coastguard Worker 		char *buf, size_t buf_size, const char *fs_type)
1068*49cdfc7eSAndroid Build Coastguard Worker {
1069*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int tmpfs_size;
1070*49cdfc7eSAndroid Build Coastguard Worker 
1071*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(fs_type, "tmpfs"))
1072*49cdfc7eSAndroid Build Coastguard Worker 		return mnt_data;
1073*49cdfc7eSAndroid Build Coastguard Worker 
1074*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->dev_min_size)
1075*49cdfc7eSAndroid Build Coastguard Worker 		tmpfs_size = 32;
1076*49cdfc7eSAndroid Build Coastguard Worker 	else
1077*49cdfc7eSAndroid Build Coastguard Worker 		tmpfs_size = tdev.size;
1078*49cdfc7eSAndroid Build Coastguard Worker 
1079*49cdfc7eSAndroid Build Coastguard Worker 	if ((tst_available_mem() / 1024) < (tmpfs_size * 2))
1080*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "No enough memory for tmpfs use");
1081*49cdfc7eSAndroid Build Coastguard Worker 
1082*49cdfc7eSAndroid Build Coastguard Worker 	if (mnt_data)
1083*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(buf, buf_size, "%s,size=%uM", mnt_data, tmpfs_size);
1084*49cdfc7eSAndroid Build Coastguard Worker 	else
1085*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(buf, buf_size, "size=%uM", tmpfs_size);
1086*49cdfc7eSAndroid Build Coastguard Worker 
1087*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Limiting tmpfs size to %uMB", tmpfs_size);
1088*49cdfc7eSAndroid Build Coastguard Worker 
1089*49cdfc7eSAndroid Build Coastguard Worker 	return buf;
1090*49cdfc7eSAndroid Build Coastguard Worker }
1091*49cdfc7eSAndroid Build Coastguard Worker 
get_device_name(const char * fs_type)1092*49cdfc7eSAndroid Build Coastguard Worker static const char *get_device_name(const char *fs_type)
1093*49cdfc7eSAndroid Build Coastguard Worker {
1094*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(fs_type, "tmpfs"))
1095*49cdfc7eSAndroid Build Coastguard Worker 		return "ltp-tmpfs";
1096*49cdfc7eSAndroid Build Coastguard Worker 	else
1097*49cdfc7eSAndroid Build Coastguard Worker 		return tdev.dev;
1098*49cdfc7eSAndroid Build Coastguard Worker }
1099*49cdfc7eSAndroid Build Coastguard Worker 
prepare_device(void)1100*49cdfc7eSAndroid Build Coastguard Worker static void prepare_device(void)
1101*49cdfc7eSAndroid Build Coastguard Worker {
1102*49cdfc7eSAndroid Build Coastguard Worker 	const char *mnt_data;
1103*49cdfc7eSAndroid Build Coastguard Worker 	char buf[1024];
1104*49cdfc7eSAndroid Build Coastguard Worker 
1105*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->format_device) {
1106*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
1107*49cdfc7eSAndroid Build Coastguard Worker 			  tst_test->dev_extra_opts);
1108*49cdfc7eSAndroid Build Coastguard Worker 	}
1109*49cdfc7eSAndroid Build Coastguard Worker 
1110*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_rofs) {
1111*49cdfc7eSAndroid Build Coastguard Worker 		prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
1112*49cdfc7eSAndroid Build Coastguard Worker 					tdev.fs_type);
1113*49cdfc7eSAndroid Build Coastguard Worker 		return;
1114*49cdfc7eSAndroid Build Coastguard Worker 	}
1115*49cdfc7eSAndroid Build Coastguard Worker 
1116*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->mount_device) {
1117*49cdfc7eSAndroid Build Coastguard Worker 		mnt_data = limit_tmpfs_mount_size(tst_test->mnt_data,
1118*49cdfc7eSAndroid Build Coastguard Worker 				buf, sizeof(buf), tdev.fs_type);
1119*49cdfc7eSAndroid Build Coastguard Worker 
1120*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MOUNT(get_device_name(tdev.fs_type), tst_test->mntpoint,
1121*49cdfc7eSAndroid Build Coastguard Worker 				tdev.fs_type, tst_test->mnt_flags, mnt_data);
1122*49cdfc7eSAndroid Build Coastguard Worker 		mntpoint_mounted = 1;
1123*49cdfc7eSAndroid Build Coastguard Worker 	}
1124*49cdfc7eSAndroid Build Coastguard Worker }
1125*49cdfc7eSAndroid Build Coastguard Worker 
do_cgroup_requires(void)1126*49cdfc7eSAndroid Build Coastguard Worker static void do_cgroup_requires(void)
1127*49cdfc7eSAndroid Build Coastguard Worker {
1128*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_cg_opts cg_opts = {
1129*49cdfc7eSAndroid Build Coastguard Worker 		.needs_ver = tst_test->needs_cgroup_ver,
1130*49cdfc7eSAndroid Build Coastguard Worker 		.needs_nsdelegate = tst_test->needs_cgroup_nsdelegate,
1131*49cdfc7eSAndroid Build Coastguard Worker 	};
1132*49cdfc7eSAndroid Build Coastguard Worker 	const char *const *ctrl_names = tst_test->needs_cgroup_ctrls;
1133*49cdfc7eSAndroid Build Coastguard Worker 
1134*49cdfc7eSAndroid Build Coastguard Worker 	for (; *ctrl_names; ctrl_names++)
1135*49cdfc7eSAndroid Build Coastguard Worker 		tst_cg_require(*ctrl_names, &cg_opts);
1136*49cdfc7eSAndroid Build Coastguard Worker 
1137*49cdfc7eSAndroid Build Coastguard Worker 	tst_cg_init();
1138*49cdfc7eSAndroid Build Coastguard Worker }
1139*49cdfc7eSAndroid Build Coastguard Worker 
1140*49cdfc7eSAndroid Build Coastguard Worker #define tst_set_ulimit(conf) \
1141*49cdfc7eSAndroid Build Coastguard Worker 	set_ulimit_(__FILE__, __LINE__, (conf))
1142*49cdfc7eSAndroid Build Coastguard Worker 
1143*49cdfc7eSAndroid Build Coastguard Worker /*
1144*49cdfc7eSAndroid Build Coastguard Worker  * Set resource limits.
1145*49cdfc7eSAndroid Build Coastguard Worker  */
set_ulimit_(const char * file,const int lineno,const struct tst_ulimit_val * conf)1146*49cdfc7eSAndroid Build Coastguard Worker static void set_ulimit_(const char *file, const int lineno, const struct tst_ulimit_val *conf)
1147*49cdfc7eSAndroid Build Coastguard Worker {
1148*49cdfc7eSAndroid Build Coastguard Worker 	struct rlimit rlim;
1149*49cdfc7eSAndroid Build Coastguard Worker 
1150*49cdfc7eSAndroid Build Coastguard Worker 	safe_getrlimit(file, lineno, conf->resource, &rlim);
1151*49cdfc7eSAndroid Build Coastguard Worker 
1152*49cdfc7eSAndroid Build Coastguard Worker 	rlim.rlim_cur = conf->rlim_cur;
1153*49cdfc7eSAndroid Build Coastguard Worker 
1154*49cdfc7eSAndroid Build Coastguard Worker 	if (conf->rlim_cur > rlim.rlim_max)
1155*49cdfc7eSAndroid Build Coastguard Worker 		rlim.rlim_max = conf->rlim_cur;
1156*49cdfc7eSAndroid Build Coastguard Worker 
1157*49cdfc7eSAndroid Build Coastguard Worker 	tst_res_(file, lineno, TINFO, "Set ulimit resource: %d rlim_cur: %lu rlim_max: %lu",
1158*49cdfc7eSAndroid Build Coastguard Worker 		conf->resource, rlim.rlim_cur, rlim.rlim_max);
1159*49cdfc7eSAndroid Build Coastguard Worker 
1160*49cdfc7eSAndroid Build Coastguard Worker 	safe_setrlimit(file, lineno, conf->resource, &rlim);
1161*49cdfc7eSAndroid Build Coastguard Worker }
1162*49cdfc7eSAndroid Build Coastguard Worker 
do_setup(int argc,char * argv[])1163*49cdfc7eSAndroid Build Coastguard Worker static void do_setup(int argc, char *argv[])
1164*49cdfc7eSAndroid Build Coastguard Worker {
1165*49cdfc7eSAndroid Build Coastguard Worker 	char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
1166*49cdfc7eSAndroid Build Coastguard Worker 
1167*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test)
1168*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "No tests to run");
1169*49cdfc7eSAndroid Build Coastguard Worker 
1170*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->max_runtime < -1) {
1171*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Invalid runtime value %i",
1172*49cdfc7eSAndroid Build Coastguard Worker 			results->max_runtime);
1173*49cdfc7eSAndroid Build Coastguard Worker 	}
1174*49cdfc7eSAndroid Build Coastguard Worker 
1175*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->tconf_msg)
1176*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "%s", tst_test->tconf_msg);
1177*49cdfc7eSAndroid Build Coastguard Worker 
1178*49cdfc7eSAndroid Build Coastguard Worker 	assert_test_fn();
1179*49cdfc7eSAndroid Build Coastguard Worker 
1180*49cdfc7eSAndroid Build Coastguard Worker 	TCID = tid = get_tid(argv);
1181*49cdfc7eSAndroid Build Coastguard Worker 
1182*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->sample)
1183*49cdfc7eSAndroid Build Coastguard Worker 		tst_test = tst_timer_test_setup(tst_test);
1184*49cdfc7eSAndroid Build Coastguard Worker 
1185*49cdfc7eSAndroid Build Coastguard Worker 	parse_opts(argc, argv);
1186*49cdfc7eSAndroid Build Coastguard Worker 
1187*49cdfc7eSAndroid Build Coastguard Worker 	if (tdebug_env && (!strcmp(tdebug_env, "1") || !strcmp(tdebug_env, "y"))) {
1188*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Enabling debug info");
1189*49cdfc7eSAndroid Build Coastguard Worker 		tdebug = 1;
1190*49cdfc7eSAndroid Build Coastguard Worker 	}
1191*49cdfc7eSAndroid Build Coastguard Worker 
1192*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_kconfigs && tst_kconfig_check(tst_test->needs_kconfigs))
1193*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Aborting due to unsuitable kernel config, see above!");
1194*49cdfc7eSAndroid Build Coastguard Worker 
1195*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_root && geteuid() != 0)
1196*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Test needs to be run as root");
1197*49cdfc7eSAndroid Build Coastguard Worker 
1198*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->min_kver)
1199*49cdfc7eSAndroid Build Coastguard Worker 		check_kver();
1200*49cdfc7eSAndroid Build Coastguard Worker 
1201*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->supported_archs && !tst_is_on_arch(tst_test->supported_archs))
1202*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "This arch '%s' is not supported for test!", tst_arch.name);
1203*49cdfc7eSAndroid Build Coastguard Worker 
1204*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->skip_in_lockdown && tst_lockdown_enabled() > 0)
1205*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Kernel is locked down, skipping test");
1206*49cdfc7eSAndroid Build Coastguard Worker 
1207*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->skip_in_secureboot && tst_secureboot_enabled() > 0)
1208*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "SecureBoot enabled, skipping test");
1209*49cdfc7eSAndroid Build Coastguard Worker 
1210*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->skip_in_compat && tst_is_compat_mode())
1211*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Not supported in 32-bit compat mode");
1212*49cdfc7eSAndroid Build Coastguard Worker 
1213*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_abi_bits && !tst_abi_bits(tst_test->needs_abi_bits))
1214*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "%dbit ABI is not supported", tst_test->needs_abi_bits);
1215*49cdfc7eSAndroid Build Coastguard Worker 
1216*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_cmds) {
1217*49cdfc7eSAndroid Build Coastguard Worker 		const char *cmd;
1218*49cdfc7eSAndroid Build Coastguard Worker 		int i;
1219*49cdfc7eSAndroid Build Coastguard Worker 
1220*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; (cmd = tst_test->needs_cmds[i]); ++i)
1221*49cdfc7eSAndroid Build Coastguard Worker 			tst_check_cmd(cmd);
1222*49cdfc7eSAndroid Build Coastguard Worker 	}
1223*49cdfc7eSAndroid Build Coastguard Worker 
1224*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_drivers) {
1225*49cdfc7eSAndroid Build Coastguard Worker 		const char *name;
1226*49cdfc7eSAndroid Build Coastguard Worker 		int i;
1227*49cdfc7eSAndroid Build Coastguard Worker 
1228*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
1229*49cdfc7eSAndroid Build Coastguard Worker 			if (tst_check_driver(name))
1230*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TCONF, "%s driver not available", name);
1231*49cdfc7eSAndroid Build Coastguard Worker 	}
1232*49cdfc7eSAndroid Build Coastguard Worker 
1233*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->mount_device)
1234*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->format_device = 1;
1235*49cdfc7eSAndroid Build Coastguard Worker 
1236*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->format_device)
1237*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->needs_device = 1;
1238*49cdfc7eSAndroid Build Coastguard Worker 
1239*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->all_filesystems)
1240*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->needs_device = 1;
1241*49cdfc7eSAndroid Build Coastguard Worker 
1242*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->min_cpus > (unsigned long)tst_ncpus())
1243*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Test needs at least %lu CPUs online", tst_test->min_cpus);
1244*49cdfc7eSAndroid Build Coastguard Worker 
1245*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->min_mem_avail > (unsigned long)(tst_available_mem() / 1024))
1246*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Test needs at least %luMB MemAvailable", tst_test->min_mem_avail);
1247*49cdfc7eSAndroid Build Coastguard Worker 
1248*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->min_swap_avail > (unsigned long)(tst_available_swap() / 1024))
1249*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Test needs at least %luMB SwapFree", tst_test->min_swap_avail);
1250*49cdfc7eSAndroid Build Coastguard Worker 
1251*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->hugepages.number)
1252*49cdfc7eSAndroid Build Coastguard Worker 		tst_reserve_hugepages(&tst_test->hugepages);
1253*49cdfc7eSAndroid Build Coastguard Worker 
1254*49cdfc7eSAndroid Build Coastguard Worker 	setup_ipc();
1255*49cdfc7eSAndroid Build Coastguard Worker 
1256*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->bufs)
1257*49cdfc7eSAndroid Build Coastguard Worker 		tst_buffers_alloc(tst_test->bufs);
1258*49cdfc7eSAndroid Build Coastguard Worker 
1259*49cdfc7eSAndroid Build Coastguard Worker 	if (needs_tmpdir() && !tst_tmpdir_created())
1260*49cdfc7eSAndroid Build Coastguard Worker 		tst_tmpdir();
1261*49cdfc7eSAndroid Build Coastguard Worker 
1262*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->save_restore) {
1263*49cdfc7eSAndroid Build Coastguard Worker 		const struct tst_path_val *pvl = tst_test->save_restore;
1264*49cdfc7eSAndroid Build Coastguard Worker 
1265*49cdfc7eSAndroid Build Coastguard Worker 		while (pvl->path) {
1266*49cdfc7eSAndroid Build Coastguard Worker 			tst_sys_conf_save(pvl);
1267*49cdfc7eSAndroid Build Coastguard Worker 			pvl++;
1268*49cdfc7eSAndroid Build Coastguard Worker 		}
1269*49cdfc7eSAndroid Build Coastguard Worker 	}
1270*49cdfc7eSAndroid Build Coastguard Worker 
1271*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->ulimit) {
1272*49cdfc7eSAndroid Build Coastguard Worker 		const struct tst_ulimit_val *pvl = tst_test->ulimit;
1273*49cdfc7eSAndroid Build Coastguard Worker 
1274*49cdfc7eSAndroid Build Coastguard Worker 		while (pvl->resource) {
1275*49cdfc7eSAndroid Build Coastguard Worker 			tst_set_ulimit(pvl);
1276*49cdfc7eSAndroid Build Coastguard Worker 			pvl++;
1277*49cdfc7eSAndroid Build Coastguard Worker 		}
1278*49cdfc7eSAndroid Build Coastguard Worker 	}
1279*49cdfc7eSAndroid Build Coastguard Worker 
1280*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->mntpoint)
1281*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MKDIR(tst_test->mntpoint, 0777);
1282*49cdfc7eSAndroid Build Coastguard Worker 
1283*49cdfc7eSAndroid Build Coastguard Worker 	if ((tst_test->needs_devfs || tst_test->needs_rofs ||
1284*49cdfc7eSAndroid Build Coastguard Worker 	     tst_test->mount_device || tst_test->all_filesystems ||
1285*49cdfc7eSAndroid Build Coastguard Worker 		 tst_test->needs_hugetlbfs) &&
1286*49cdfc7eSAndroid Build Coastguard Worker 	     !tst_test->mntpoint) {
1287*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "tst_test->mntpoint must be set!");
1288*49cdfc7eSAndroid Build Coastguard Worker 	}
1289*49cdfc7eSAndroid Build Coastguard Worker 
1290*49cdfc7eSAndroid Build Coastguard Worker 	if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
1291*49cdfc7eSAndroid Build Coastguard Worker 	    !!tst_test->needs_device + !!tst_test->needs_hugetlbfs > 1) {
1292*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK,
1293*49cdfc7eSAndroid Build Coastguard Worker 			"Two or more of needs_{rofs, devfs, device, hugetlbfs} are set");
1294*49cdfc7eSAndroid Build Coastguard Worker 	}
1295*49cdfc7eSAndroid Build Coastguard Worker 
1296*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_devfs)
1297*49cdfc7eSAndroid Build Coastguard Worker 		prepare_and_mount_dev_fs(tst_test->mntpoint);
1298*49cdfc7eSAndroid Build Coastguard Worker 
1299*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_rofs) {
1300*49cdfc7eSAndroid Build Coastguard Worker 		/* If we failed to mount read-only tmpfs. Fallback to
1301*49cdfc7eSAndroid Build Coastguard Worker 		 * using a device with read-only filesystem.
1302*49cdfc7eSAndroid Build Coastguard Worker 		 */
1303*49cdfc7eSAndroid Build Coastguard Worker 		if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
1304*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "Can't mount tmpfs read-only, "
1305*49cdfc7eSAndroid Build Coastguard Worker 				"falling back to block device...");
1306*49cdfc7eSAndroid Build Coastguard Worker 			tst_test->needs_device = 1;
1307*49cdfc7eSAndroid Build Coastguard Worker 			tst_test->format_device = 1;
1308*49cdfc7eSAndroid Build Coastguard Worker 		}
1309*49cdfc7eSAndroid Build Coastguard Worker 	}
1310*49cdfc7eSAndroid Build Coastguard Worker 
1311*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_hugetlbfs)
1312*49cdfc7eSAndroid Build Coastguard Worker 		prepare_and_mount_hugetlb_fs();
1313*49cdfc7eSAndroid Build Coastguard Worker 
1314*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_device && !mntpoint_mounted) {
1315*49cdfc7eSAndroid Build Coastguard Worker 		tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
1316*49cdfc7eSAndroid Build Coastguard Worker 
1317*49cdfc7eSAndroid Build Coastguard Worker 		if (!tdev.dev)
1318*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF, "Failed to acquire device");
1319*49cdfc7eSAndroid Build Coastguard Worker 
1320*49cdfc7eSAndroid Build Coastguard Worker 		tdev.size = tst_get_device_size(tdev.dev);
1321*49cdfc7eSAndroid Build Coastguard Worker 
1322*49cdfc7eSAndroid Build Coastguard Worker 		tst_device = &tdev;
1323*49cdfc7eSAndroid Build Coastguard Worker 
1324*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_test->dev_fs_type)
1325*49cdfc7eSAndroid Build Coastguard Worker 			tdev.fs_type = tst_test->dev_fs_type;
1326*49cdfc7eSAndroid Build Coastguard Worker 		else
1327*49cdfc7eSAndroid Build Coastguard Worker 			tdev.fs_type = tst_dev_fs_type();
1328*49cdfc7eSAndroid Build Coastguard Worker 
1329*49cdfc7eSAndroid Build Coastguard Worker 		if (!tst_test->all_filesystems)
1330*49cdfc7eSAndroid Build Coastguard Worker 			prepare_device();
1331*49cdfc7eSAndroid Build Coastguard Worker 	}
1332*49cdfc7eSAndroid Build Coastguard Worker 
1333*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_overlay && !tst_test->mount_device)
1334*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "tst_test->mount_device must be set");
1335*49cdfc7eSAndroid Build Coastguard Worker 
1336*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_overlay && !mntpoint_mounted)
1337*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "tst_test->mntpoint must be mounted");
1338*49cdfc7eSAndroid Build Coastguard Worker 
1339*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_overlay && !ovl_mounted) {
1340*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MOUNT_OVERLAY();
1341*49cdfc7eSAndroid Build Coastguard Worker 		ovl_mounted = 1;
1342*49cdfc7eSAndroid Build Coastguard Worker 	}
1343*49cdfc7eSAndroid Build Coastguard Worker 
1344*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->resource_files)
1345*49cdfc7eSAndroid Build Coastguard Worker 		copy_resources();
1346*49cdfc7eSAndroid Build Coastguard Worker 
1347*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->restore_wallclock)
1348*49cdfc7eSAndroid Build Coastguard Worker 		tst_wallclock_save();
1349*49cdfc7eSAndroid Build Coastguard Worker 
1350*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->taint_check)
1351*49cdfc7eSAndroid Build Coastguard Worker 		tst_taint_init(tst_test->taint_check);
1352*49cdfc7eSAndroid Build Coastguard Worker 
1353*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_cgroup_ctrls)
1354*49cdfc7eSAndroid Build Coastguard Worker 		do_cgroup_requires();
1355*49cdfc7eSAndroid Build Coastguard Worker 	else if (tst_test->needs_cgroup_ver)
1356*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "tst_test->needs_cgroup_ctrls must be set");
1357*49cdfc7eSAndroid Build Coastguard Worker }
1358*49cdfc7eSAndroid Build Coastguard Worker 
do_test_setup(void)1359*49cdfc7eSAndroid Build Coastguard Worker static void do_test_setup(void)
1360*49cdfc7eSAndroid Build Coastguard Worker {
1361*49cdfc7eSAndroid Build Coastguard Worker 	main_pid = getpid();
1362*49cdfc7eSAndroid Build Coastguard Worker 
1363*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->all_filesystems && tst_test->skip_filesystems) {
1364*49cdfc7eSAndroid Build Coastguard Worker 		long fs_type = tst_fs_type(".");
1365*49cdfc7eSAndroid Build Coastguard Worker 		const char *fs_name = tst_fs_type_name(fs_type);
1366*49cdfc7eSAndroid Build Coastguard Worker 
1367*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_fs_in_skiplist(fs_name, tst_test->skip_filesystems)) {
1368*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF, "%s is not supported by the test",
1369*49cdfc7eSAndroid Build Coastguard Worker 				fs_name);
1370*49cdfc7eSAndroid Build Coastguard Worker 		}
1371*49cdfc7eSAndroid Build Coastguard Worker 
1372*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "%s is supported by the test", fs_name);
1373*49cdfc7eSAndroid Build Coastguard Worker 	}
1374*49cdfc7eSAndroid Build Coastguard Worker 
1375*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->caps)
1376*49cdfc7eSAndroid Build Coastguard Worker 		tst_cap_setup(tst_test->caps, TST_CAP_REQ);
1377*49cdfc7eSAndroid Build Coastguard Worker 
1378*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->setup)
1379*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->setup();
1380*49cdfc7eSAndroid Build Coastguard Worker 
1381*49cdfc7eSAndroid Build Coastguard Worker 	if (main_pid != tst_getpid())
1382*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Runaway child in setup()!");
1383*49cdfc7eSAndroid Build Coastguard Worker 
1384*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->caps)
1385*49cdfc7eSAndroid Build Coastguard Worker 		tst_cap_setup(tst_test->caps, TST_CAP_DROP);
1386*49cdfc7eSAndroid Build Coastguard Worker }
1387*49cdfc7eSAndroid Build Coastguard Worker 
do_cleanup(void)1388*49cdfc7eSAndroid Build Coastguard Worker static void do_cleanup(void)
1389*49cdfc7eSAndroid Build Coastguard Worker {
1390*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_cgroup_ctrls)
1391*49cdfc7eSAndroid Build Coastguard Worker 		tst_cg_cleanup();
1392*49cdfc7eSAndroid Build Coastguard Worker 
1393*49cdfc7eSAndroid Build Coastguard Worker 	if (ovl_mounted)
1394*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UMOUNT(OVL_MNT);
1395*49cdfc7eSAndroid Build Coastguard Worker 
1396*49cdfc7eSAndroid Build Coastguard Worker 	if (mntpoint_mounted)
1397*49cdfc7eSAndroid Build Coastguard Worker 		tst_umount(tst_test->mntpoint);
1398*49cdfc7eSAndroid Build Coastguard Worker 
1399*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->needs_device && tdev.dev)
1400*49cdfc7eSAndroid Build Coastguard Worker 		tst_release_device(tdev.dev);
1401*49cdfc7eSAndroid Build Coastguard Worker 
1402*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_tmpdir_created()) {
1403*49cdfc7eSAndroid Build Coastguard Worker 		/* avoid munmap() on wrong pointer in tst_rmdir() */
1404*49cdfc7eSAndroid Build Coastguard Worker 		tst_futexes = NULL;
1405*49cdfc7eSAndroid Build Coastguard Worker 		tst_rmdir();
1406*49cdfc7eSAndroid Build Coastguard Worker 	}
1407*49cdfc7eSAndroid Build Coastguard Worker 
1408*49cdfc7eSAndroid Build Coastguard Worker 	tst_sys_conf_restore(0);
1409*49cdfc7eSAndroid Build Coastguard Worker 
1410*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->restore_wallclock)
1411*49cdfc7eSAndroid Build Coastguard Worker 		tst_wallclock_restore();
1412*49cdfc7eSAndroid Build Coastguard Worker 
1413*49cdfc7eSAndroid Build Coastguard Worker 	cleanup_ipc();
1414*49cdfc7eSAndroid Build Coastguard Worker }
1415*49cdfc7eSAndroid Build Coastguard Worker 
heartbeat(void)1416*49cdfc7eSAndroid Build Coastguard Worker static void heartbeat(void)
1417*49cdfc7eSAndroid Build Coastguard Worker {
1418*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
1419*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1420*49cdfc7eSAndroid Build Coastguard Worker 
1421*49cdfc7eSAndroid Build Coastguard Worker 	if (getppid() == 1) {
1422*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Main test process might have exit!");
1423*49cdfc7eSAndroid Build Coastguard Worker 		/*
1424*49cdfc7eSAndroid Build Coastguard Worker 		 * We need kill the task group immediately since the
1425*49cdfc7eSAndroid Build Coastguard Worker 		 * main process has exit.
1426*49cdfc7eSAndroid Build Coastguard Worker 		 */
1427*49cdfc7eSAndroid Build Coastguard Worker 		kill(0, SIGKILL);
1428*49cdfc7eSAndroid Build Coastguard Worker 		exit(TBROK);
1429*49cdfc7eSAndroid Build Coastguard Worker 	}
1430*49cdfc7eSAndroid Build Coastguard Worker 
1431*49cdfc7eSAndroid Build Coastguard Worker 	kill(getppid(), SIGUSR1);
1432*49cdfc7eSAndroid Build Coastguard Worker }
1433*49cdfc7eSAndroid Build Coastguard Worker 
run_tests(void)1434*49cdfc7eSAndroid Build Coastguard Worker static void run_tests(void)
1435*49cdfc7eSAndroid Build Coastguard Worker {
1436*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
1437*49cdfc7eSAndroid Build Coastguard Worker 	struct results saved_results;
1438*49cdfc7eSAndroid Build Coastguard Worker 
1439*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_test->test) {
1440*49cdfc7eSAndroid Build Coastguard Worker 		saved_results = *results;
1441*49cdfc7eSAndroid Build Coastguard Worker 		heartbeat();
1442*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->test_all();
1443*49cdfc7eSAndroid Build Coastguard Worker 
1444*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_getpid() != main_pid)
1445*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
1446*49cdfc7eSAndroid Build Coastguard Worker 
1447*49cdfc7eSAndroid Build Coastguard Worker 		tst_reap_children();
1448*49cdfc7eSAndroid Build Coastguard Worker 
1449*49cdfc7eSAndroid Build Coastguard Worker 		if (results_equal(&saved_results, results))
1450*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK, "Test haven't reported results!");
1451*49cdfc7eSAndroid Build Coastguard Worker 		return;
1452*49cdfc7eSAndroid Build Coastguard Worker 	}
1453*49cdfc7eSAndroid Build Coastguard Worker 
1454*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < tst_test->tcnt; i++) {
1455*49cdfc7eSAndroid Build Coastguard Worker 		saved_results = *results;
1456*49cdfc7eSAndroid Build Coastguard Worker 		heartbeat();
1457*49cdfc7eSAndroid Build Coastguard Worker 		tst_test->test(i);
1458*49cdfc7eSAndroid Build Coastguard Worker 
1459*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_getpid() != main_pid)
1460*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
1461*49cdfc7eSAndroid Build Coastguard Worker 
1462*49cdfc7eSAndroid Build Coastguard Worker 		tst_reap_children();
1463*49cdfc7eSAndroid Build Coastguard Worker 
1464*49cdfc7eSAndroid Build Coastguard Worker 		if (results_equal(&saved_results, results))
1465*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK, "Test %i haven't reported results!", i);
1466*49cdfc7eSAndroid Build Coastguard Worker 	}
1467*49cdfc7eSAndroid Build Coastguard Worker }
1468*49cdfc7eSAndroid Build Coastguard Worker 
get_time_ms(void)1469*49cdfc7eSAndroid Build Coastguard Worker static unsigned long long get_time_ms(void)
1470*49cdfc7eSAndroid Build Coastguard Worker {
1471*49cdfc7eSAndroid Build Coastguard Worker 	struct timespec ts;
1472*49cdfc7eSAndroid Build Coastguard Worker 
1473*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
1474*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
1475*49cdfc7eSAndroid Build Coastguard Worker 
1476*49cdfc7eSAndroid Build Coastguard Worker 	return tst_timespec_to_ms(ts);
1477*49cdfc7eSAndroid Build Coastguard Worker }
1478*49cdfc7eSAndroid Build Coastguard Worker 
add_paths(void)1479*49cdfc7eSAndroid Build Coastguard Worker static void add_paths(void)
1480*49cdfc7eSAndroid Build Coastguard Worker {
1481*49cdfc7eSAndroid Build Coastguard Worker 	char *old_path = getenv("PATH");
1482*49cdfc7eSAndroid Build Coastguard Worker 	const char *start_dir;
1483*49cdfc7eSAndroid Build Coastguard Worker 	char *new_path;
1484*49cdfc7eSAndroid Build Coastguard Worker 
1485*49cdfc7eSAndroid Build Coastguard Worker 	start_dir = tst_get_startwd();
1486*49cdfc7eSAndroid Build Coastguard Worker 
1487*49cdfc7eSAndroid Build Coastguard Worker 	if (old_path)
1488*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
1489*49cdfc7eSAndroid Build Coastguard Worker 	else
1490*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_ASPRINTF(&new_path, "::%s", start_dir);
1491*49cdfc7eSAndroid Build Coastguard Worker 
1492*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETENV("PATH", new_path, 1);
1493*49cdfc7eSAndroid Build Coastguard Worker 	free(new_path);
1494*49cdfc7eSAndroid Build Coastguard Worker }
1495*49cdfc7eSAndroid Build Coastguard Worker 
testrun(void)1496*49cdfc7eSAndroid Build Coastguard Worker static void testrun(void)
1497*49cdfc7eSAndroid Build Coastguard Worker {
1498*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i = 0;
1499*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long long stop_time = 0;
1500*49cdfc7eSAndroid Build Coastguard Worker 	int cont = 1;
1501*49cdfc7eSAndroid Build Coastguard Worker 
1502*49cdfc7eSAndroid Build Coastguard Worker 	heartbeat();
1503*49cdfc7eSAndroid Build Coastguard Worker 	add_paths();
1504*49cdfc7eSAndroid Build Coastguard Worker 	do_test_setup();
1505*49cdfc7eSAndroid Build Coastguard Worker 
1506*49cdfc7eSAndroid Build Coastguard Worker 	if (duration > 0)
1507*49cdfc7eSAndroid Build Coastguard Worker 		stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1508*49cdfc7eSAndroid Build Coastguard Worker 
1509*49cdfc7eSAndroid Build Coastguard Worker 	for (;;) {
1510*49cdfc7eSAndroid Build Coastguard Worker 		cont = 0;
1511*49cdfc7eSAndroid Build Coastguard Worker 
1512*49cdfc7eSAndroid Build Coastguard Worker 		if (i < (unsigned int)iterations) {
1513*49cdfc7eSAndroid Build Coastguard Worker 			i++;
1514*49cdfc7eSAndroid Build Coastguard Worker 			cont = 1;
1515*49cdfc7eSAndroid Build Coastguard Worker 		}
1516*49cdfc7eSAndroid Build Coastguard Worker 
1517*49cdfc7eSAndroid Build Coastguard Worker 		if (stop_time && get_time_ms() < stop_time)
1518*49cdfc7eSAndroid Build Coastguard Worker 			cont = 1;
1519*49cdfc7eSAndroid Build Coastguard Worker 
1520*49cdfc7eSAndroid Build Coastguard Worker 		if (!cont)
1521*49cdfc7eSAndroid Build Coastguard Worker 			break;
1522*49cdfc7eSAndroid Build Coastguard Worker 
1523*49cdfc7eSAndroid Build Coastguard Worker 		run_tests();
1524*49cdfc7eSAndroid Build Coastguard Worker 		heartbeat();
1525*49cdfc7eSAndroid Build Coastguard Worker 	}
1526*49cdfc7eSAndroid Build Coastguard Worker 
1527*49cdfc7eSAndroid Build Coastguard Worker 	do_test_cleanup();
1528*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
1529*49cdfc7eSAndroid Build Coastguard Worker }
1530*49cdfc7eSAndroid Build Coastguard Worker 
1531*49cdfc7eSAndroid Build Coastguard Worker static pid_t test_pid;
1532*49cdfc7eSAndroid Build Coastguard Worker 
1533*49cdfc7eSAndroid Build Coastguard Worker 
1534*49cdfc7eSAndroid Build Coastguard Worker static volatile sig_atomic_t sigkill_retries;
1535*49cdfc7eSAndroid Build Coastguard Worker 
1536*49cdfc7eSAndroid Build Coastguard Worker #define WRITE_MSG(msg) do { \
1537*49cdfc7eSAndroid Build Coastguard Worker 	if (write(2, msg, sizeof(msg) - 1)) { \
1538*49cdfc7eSAndroid Build Coastguard Worker 		/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1539*49cdfc7eSAndroid Build Coastguard Worker 	} \
1540*49cdfc7eSAndroid Build Coastguard Worker } while (0)
1541*49cdfc7eSAndroid Build Coastguard Worker 
alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)1542*49cdfc7eSAndroid Build Coastguard Worker static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1543*49cdfc7eSAndroid Build Coastguard Worker {
1544*49cdfc7eSAndroid Build Coastguard Worker 	WRITE_MSG("Test timeouted, sending SIGKILL!\n");
1545*49cdfc7eSAndroid Build Coastguard Worker 	kill(-test_pid, SIGKILL);
1546*49cdfc7eSAndroid Build Coastguard Worker 	alarm(5);
1547*49cdfc7eSAndroid Build Coastguard Worker 
1548*49cdfc7eSAndroid Build Coastguard Worker 	if (++sigkill_retries > 10) {
1549*49cdfc7eSAndroid Build Coastguard Worker 		WRITE_MSG("Cannot kill test processes!\n");
1550*49cdfc7eSAndroid Build Coastguard Worker 		WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1551*49cdfc7eSAndroid Build Coastguard Worker 		WRITE_MSG("Exiting uncleanly...\n");
1552*49cdfc7eSAndroid Build Coastguard Worker 		_exit(TFAIL);
1553*49cdfc7eSAndroid Build Coastguard Worker 	}
1554*49cdfc7eSAndroid Build Coastguard Worker }
1555*49cdfc7eSAndroid Build Coastguard Worker 
heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)1556*49cdfc7eSAndroid Build Coastguard Worker static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1557*49cdfc7eSAndroid Build Coastguard Worker {
1558*49cdfc7eSAndroid Build Coastguard Worker 	alarm(results->timeout);
1559*49cdfc7eSAndroid Build Coastguard Worker 	sigkill_retries = 0;
1560*49cdfc7eSAndroid Build Coastguard Worker }
1561*49cdfc7eSAndroid Build Coastguard Worker 
sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)1562*49cdfc7eSAndroid Build Coastguard Worker static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1563*49cdfc7eSAndroid Build Coastguard Worker {
1564*49cdfc7eSAndroid Build Coastguard Worker 	if (test_pid > 0) {
1565*49cdfc7eSAndroid Build Coastguard Worker 		WRITE_MSG("Sending SIGKILL to test process...\n");
1566*49cdfc7eSAndroid Build Coastguard Worker 		kill(-test_pid, SIGKILL);
1567*49cdfc7eSAndroid Build Coastguard Worker 	}
1568*49cdfc7eSAndroid Build Coastguard Worker }
1569*49cdfc7eSAndroid Build Coastguard Worker 
tst_remaining_runtime(void)1570*49cdfc7eSAndroid Build Coastguard Worker unsigned int tst_remaining_runtime(void)
1571*49cdfc7eSAndroid Build Coastguard Worker {
1572*49cdfc7eSAndroid Build Coastguard Worker 	static struct timespec now;
1573*49cdfc7eSAndroid Build Coastguard Worker 	int elapsed;
1574*49cdfc7eSAndroid Build Coastguard Worker 
1575*49cdfc7eSAndroid Build Coastguard Worker 	if (results->max_runtime == TST_UNLIMITED_RUNTIME)
1576*49cdfc7eSAndroid Build Coastguard Worker 		return UINT_MAX;
1577*49cdfc7eSAndroid Build Coastguard Worker 
1578*49cdfc7eSAndroid Build Coastguard Worker 	if (results->max_runtime == 0)
1579*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Runtime not set!");
1580*49cdfc7eSAndroid Build Coastguard Worker 
1581*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1582*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1583*49cdfc7eSAndroid Build Coastguard Worker 
1584*49cdfc7eSAndroid Build Coastguard Worker 	elapsed = tst_timespec_diff_ms(now, tst_start_time) / 1000;
1585*49cdfc7eSAndroid Build Coastguard Worker 	if (results->max_runtime > elapsed)
1586*49cdfc7eSAndroid Build Coastguard Worker 		return results->max_runtime - elapsed;
1587*49cdfc7eSAndroid Build Coastguard Worker 
1588*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
1589*49cdfc7eSAndroid Build Coastguard Worker }
1590*49cdfc7eSAndroid Build Coastguard Worker 
1591*49cdfc7eSAndroid Build Coastguard Worker 
tst_multiply_timeout(unsigned int timeout)1592*49cdfc7eSAndroid Build Coastguard Worker unsigned int tst_multiply_timeout(unsigned int timeout)
1593*49cdfc7eSAndroid Build Coastguard Worker {
1594*49cdfc7eSAndroid Build Coastguard Worker 	parse_mul(&timeout_mul, "LTP_TIMEOUT_MUL", 0.099, 10000);
1595*49cdfc7eSAndroid Build Coastguard Worker 
1596*49cdfc7eSAndroid Build Coastguard Worker 	if (timeout < 1)
1597*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
1598*49cdfc7eSAndroid Build Coastguard Worker 
1599*49cdfc7eSAndroid Build Coastguard Worker 	return timeout * timeout_mul;
1600*49cdfc7eSAndroid Build Coastguard Worker }
1601*49cdfc7eSAndroid Build Coastguard Worker 
set_timeout(void)1602*49cdfc7eSAndroid Build Coastguard Worker static void set_timeout(void)
1603*49cdfc7eSAndroid Build Coastguard Worker {
1604*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int timeout = DEFAULT_TIMEOUT;
1605*49cdfc7eSAndroid Build Coastguard Worker 
1606*49cdfc7eSAndroid Build Coastguard Worker 	if (results->max_runtime == TST_UNLIMITED_RUNTIME) {
1607*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Timeout per run is disabled");
1608*49cdfc7eSAndroid Build Coastguard Worker 		return;
1609*49cdfc7eSAndroid Build Coastguard Worker 	}
1610*49cdfc7eSAndroid Build Coastguard Worker 
1611*49cdfc7eSAndroid Build Coastguard Worker 	if (results->max_runtime < 0) {
1612*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "max_runtime must to be >= -1! (%d)",
1613*49cdfc7eSAndroid Build Coastguard Worker 			results->max_runtime);
1614*49cdfc7eSAndroid Build Coastguard Worker 	}
1615*49cdfc7eSAndroid Build Coastguard Worker 
1616*49cdfc7eSAndroid Build Coastguard Worker 	results->timeout = tst_multiply_timeout(timeout) + results->max_runtime;
1617*49cdfc7eSAndroid Build Coastguard Worker 
1618*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1619*49cdfc7eSAndroid Build Coastguard Worker 		results->timeout/3600, (results->timeout%3600)/60,
1620*49cdfc7eSAndroid Build Coastguard Worker 		results->timeout % 60);
1621*49cdfc7eSAndroid Build Coastguard Worker }
1622*49cdfc7eSAndroid Build Coastguard Worker 
tst_set_max_runtime(int max_runtime)1623*49cdfc7eSAndroid Build Coastguard Worker void tst_set_max_runtime(int max_runtime)
1624*49cdfc7eSAndroid Build Coastguard Worker {
1625*49cdfc7eSAndroid Build Coastguard Worker 	results->max_runtime = multiply_runtime(max_runtime);
1626*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Updating max runtime to %uh %02um %02us",
1627*49cdfc7eSAndroid Build Coastguard Worker 		max_runtime/3600, (max_runtime%3600)/60, max_runtime % 60);
1628*49cdfc7eSAndroid Build Coastguard Worker 	set_timeout();
1629*49cdfc7eSAndroid Build Coastguard Worker 	heartbeat();
1630*49cdfc7eSAndroid Build Coastguard Worker }
1631*49cdfc7eSAndroid Build Coastguard Worker 
fork_testrun(void)1632*49cdfc7eSAndroid Build Coastguard Worker static int fork_testrun(void)
1633*49cdfc7eSAndroid Build Coastguard Worker {
1634*49cdfc7eSAndroid Build Coastguard Worker 	int status;
1635*49cdfc7eSAndroid Build Coastguard Worker 
1636*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGINT, sigint_handler);
1637*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGTERM, sigint_handler);
1638*49cdfc7eSAndroid Build Coastguard Worker 
1639*49cdfc7eSAndroid Build Coastguard Worker 	alarm(results->timeout);
1640*49cdfc7eSAndroid Build Coastguard Worker 
1641*49cdfc7eSAndroid Build Coastguard Worker 	test_pid = fork();
1642*49cdfc7eSAndroid Build Coastguard Worker 	if (test_pid < 0)
1643*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "fork()");
1644*49cdfc7eSAndroid Build Coastguard Worker 
1645*49cdfc7eSAndroid Build Coastguard Worker 	if (!test_pid) {
1646*49cdfc7eSAndroid Build Coastguard Worker 		tst_disable_oom_protection(0);
1647*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SIGNAL(SIGALRM, SIG_DFL);
1648*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1649*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SIGNAL(SIGTERM, SIG_DFL);
1650*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SIGNAL(SIGINT, SIG_DFL);
1651*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETPGID(0, 0);
1652*49cdfc7eSAndroid Build Coastguard Worker 		testrun();
1653*49cdfc7eSAndroid Build Coastguard Worker 	}
1654*49cdfc7eSAndroid Build Coastguard Worker 
1655*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(test_pid, &status, 0);
1656*49cdfc7eSAndroid Build Coastguard Worker 	alarm(0);
1657*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGTERM, SIG_DFL);
1658*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGINT, SIG_DFL);
1659*49cdfc7eSAndroid Build Coastguard Worker 
1660*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->taint_check && tst_taint_check()) {
1661*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Kernel is now tainted.");
1662*49cdfc7eSAndroid Build Coastguard Worker 		return TFAIL;
1663*49cdfc7eSAndroid Build Coastguard Worker 	}
1664*49cdfc7eSAndroid Build Coastguard Worker 
1665*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->forks_child && kill(-test_pid, SIGKILL) == 0)
1666*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Killed the leftover descendant processes");
1667*49cdfc7eSAndroid Build Coastguard Worker 
1668*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status) && WEXITSTATUS(status))
1669*49cdfc7eSAndroid Build Coastguard Worker 		return WEXITSTATUS(status);
1670*49cdfc7eSAndroid Build Coastguard Worker 
1671*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1672*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "If you are running on slow machine, "
1673*49cdfc7eSAndroid Build Coastguard Worker 			       "try exporting LTP_TIMEOUT_MUL > 1");
1674*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Test killed! (timeout?)");
1675*49cdfc7eSAndroid Build Coastguard Worker 	}
1676*49cdfc7eSAndroid Build Coastguard Worker 
1677*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFSIGNALED(status))
1678*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1679*49cdfc7eSAndroid Build Coastguard Worker 
1680*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
1681*49cdfc7eSAndroid Build Coastguard Worker }
1682*49cdfc7eSAndroid Build Coastguard Worker 
run_tcases_per_fs(void)1683*49cdfc7eSAndroid Build Coastguard Worker static int run_tcases_per_fs(void)
1684*49cdfc7eSAndroid Build Coastguard Worker {
1685*49cdfc7eSAndroid Build Coastguard Worker 	int ret = 0;
1686*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
1687*49cdfc7eSAndroid Build Coastguard Worker 	const char *const *filesystems = tst_get_supported_fs_types(tst_test->skip_filesystems);
1688*49cdfc7eSAndroid Build Coastguard Worker 
1689*49cdfc7eSAndroid Build Coastguard Worker 	if (!filesystems[0])
1690*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "There are no supported filesystems");
1691*49cdfc7eSAndroid Build Coastguard Worker 
1692*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; filesystems[i]; i++) {
1693*49cdfc7eSAndroid Build Coastguard Worker 
1694*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "=== Testing on %s ===", filesystems[i]);
1695*49cdfc7eSAndroid Build Coastguard Worker 		tdev.fs_type = filesystems[i];
1696*49cdfc7eSAndroid Build Coastguard Worker 
1697*49cdfc7eSAndroid Build Coastguard Worker 		prepare_device();
1698*49cdfc7eSAndroid Build Coastguard Worker 
1699*49cdfc7eSAndroid Build Coastguard Worker 		ret = fork_testrun();
1700*49cdfc7eSAndroid Build Coastguard Worker 
1701*49cdfc7eSAndroid Build Coastguard Worker 		if (mntpoint_mounted) {
1702*49cdfc7eSAndroid Build Coastguard Worker 			tst_umount(tst_test->mntpoint);
1703*49cdfc7eSAndroid Build Coastguard Worker 			mntpoint_mounted = 0;
1704*49cdfc7eSAndroid Build Coastguard Worker 		}
1705*49cdfc7eSAndroid Build Coastguard Worker 
1706*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == TCONF)
1707*49cdfc7eSAndroid Build Coastguard Worker 			continue;
1708*49cdfc7eSAndroid Build Coastguard Worker 
1709*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == 0)
1710*49cdfc7eSAndroid Build Coastguard Worker 			continue;
1711*49cdfc7eSAndroid Build Coastguard Worker 
1712*49cdfc7eSAndroid Build Coastguard Worker 		do_exit(ret);
1713*49cdfc7eSAndroid Build Coastguard Worker 	}
1714*49cdfc7eSAndroid Build Coastguard Worker 
1715*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
1716*49cdfc7eSAndroid Build Coastguard Worker }
1717*49cdfc7eSAndroid Build Coastguard Worker 
1718*49cdfc7eSAndroid Build Coastguard Worker unsigned int tst_variant;
1719*49cdfc7eSAndroid Build Coastguard Worker 
tst_run_tcases(int argc,char * argv[],struct tst_test * self)1720*49cdfc7eSAndroid Build Coastguard Worker void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1721*49cdfc7eSAndroid Build Coastguard Worker {
1722*49cdfc7eSAndroid Build Coastguard Worker 	int ret = 0;
1723*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int test_variants = 1;
1724*49cdfc7eSAndroid Build Coastguard Worker 
1725*49cdfc7eSAndroid Build Coastguard Worker 	lib_pid = getpid();
1726*49cdfc7eSAndroid Build Coastguard Worker 	tst_test = self;
1727*49cdfc7eSAndroid Build Coastguard Worker 
1728*49cdfc7eSAndroid Build Coastguard Worker 	do_setup(argc, argv);
1729*49cdfc7eSAndroid Build Coastguard Worker 	tst_enable_oom_protection(lib_pid);
1730*49cdfc7eSAndroid Build Coastguard Worker 
1731*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGALRM, alarm_handler);
1732*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1733*49cdfc7eSAndroid Build Coastguard Worker 
1734*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "LTP version: "LTP_VERSION);
1735*49cdfc7eSAndroid Build Coastguard Worker 
1736*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->max_runtime)
1737*49cdfc7eSAndroid Build Coastguard Worker 		results->max_runtime = multiply_runtime(tst_test->max_runtime);
1738*49cdfc7eSAndroid Build Coastguard Worker 
1739*49cdfc7eSAndroid Build Coastguard Worker 	set_timeout();
1740*49cdfc7eSAndroid Build Coastguard Worker 
1741*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_test->test_variants)
1742*49cdfc7eSAndroid Build Coastguard Worker 		test_variants = tst_test->test_variants;
1743*49cdfc7eSAndroid Build Coastguard Worker 
1744*49cdfc7eSAndroid Build Coastguard Worker 	for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1745*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_test->all_filesystems)
1746*49cdfc7eSAndroid Build Coastguard Worker 			ret |= run_tcases_per_fs();
1747*49cdfc7eSAndroid Build Coastguard Worker 		else
1748*49cdfc7eSAndroid Build Coastguard Worker 			ret |= fork_testrun();
1749*49cdfc7eSAndroid Build Coastguard Worker 
1750*49cdfc7eSAndroid Build Coastguard Worker 		if (ret & ~(TCONF))
1751*49cdfc7eSAndroid Build Coastguard Worker 			goto exit;
1752*49cdfc7eSAndroid Build Coastguard Worker 	}
1753*49cdfc7eSAndroid Build Coastguard Worker 
1754*49cdfc7eSAndroid Build Coastguard Worker exit:
1755*49cdfc7eSAndroid Build Coastguard Worker 	do_exit(ret);
1756*49cdfc7eSAndroid Build Coastguard Worker }
1757*49cdfc7eSAndroid Build Coastguard Worker 
1758*49cdfc7eSAndroid Build Coastguard Worker 
tst_flush(void)1759*49cdfc7eSAndroid Build Coastguard Worker void tst_flush(void)
1760*49cdfc7eSAndroid Build Coastguard Worker {
1761*49cdfc7eSAndroid Build Coastguard Worker 	int rval;
1762*49cdfc7eSAndroid Build Coastguard Worker 
1763*49cdfc7eSAndroid Build Coastguard Worker 	rval = fflush(stderr);
1764*49cdfc7eSAndroid Build Coastguard Worker 	if (rval != 0)
1765*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1766*49cdfc7eSAndroid Build Coastguard Worker 
1767*49cdfc7eSAndroid Build Coastguard Worker 	rval = fflush(stdout);
1768*49cdfc7eSAndroid Build Coastguard Worker 	if (rval != 0)
1769*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1770*49cdfc7eSAndroid Build Coastguard Worker 
1771*49cdfc7eSAndroid Build Coastguard Worker }
1772