blob: cedab53b3bc9d77aeca59b70d142e0f07a13f24a [file] [log] [blame]
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001/*
2 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdio.h>
19#include <stdarg.h>
20#include <unistd.h>
21#include <string.h>
22#include <stdlib.h>
23#include <errno.h>
Sandeep Patilc9a7def2017-09-19 12:49:58 -070024#include <sys/mount.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010025#include <sys/types.h>
26#include <sys/wait.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010027
28#define TST_NO_DEFAULT_MAIN
29#include "tst_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010030#include "tst_device.h"
31#include "lapi/futex.h"
Steve Mucklec20831d2017-09-20 13:23:06 -070032#include "lapi/syscalls.h"
Petr Vorel3a0ef862017-03-01 15:31:08 +010033#include "tst_ansi_color.h"
Jan Stancekb95b1992017-10-10 15:47:58 +020034#include "tst_safe_stdio.h"
Cyril Hrubisc4596542017-06-20 15:42:18 +020035#include "tst_timer_test.h"
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +010036#include "tst_clocks.h"
37#include "tst_timer.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010038
39#include "old_resource.h"
40#include "old_device.h"
41#include "old_tmpdir.h"
42
43struct tst_test *tst_test;
44
Li Wangb5d620a2017-11-01 13:15:23 +080045static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010046static int iterations = 1;
47static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020048static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070049static int mntpoint_mounted;
Jan Stancek1893e012018-08-28 16:17:52 +020050static struct timespec tst_start_time; /* valid only for test pid */
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010051
52struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020053 int passed;
54 int skipped;
55 int failed;
56 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020057 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010058};
59
60static struct results *results;
61
62static int ipc_fd;
63
64extern void *tst_futexes;
65extern unsigned int tst_max_futexes;
66
67#define IPC_ENV_VAR "LTP_IPC_PATH"
68
69static char ipc_path[1024];
70const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010071
72static char shm_path[1024];
73
Christian Lanig18b780d2018-07-23 15:24:50 +020074int TST_ERR;
75long TST_RET;
76
Jan Stancek332540e2016-06-08 16:48:22 +020077static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020078static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020079
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010080static void setup_ipc(void)
81{
82 size_t size = getpagesize();
83
Steven Jackson9f41dcf2016-12-21 20:09:01 +000084 if (access("/dev/shm", F_OK) == 0) {
85 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080086 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000087 } else {
88 char *tmpdir;
89
90 if (!tst_tmpdir_created())
91 tst_tmpdir();
92
93 tmpdir = tst_get_tmpdir();
94 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080095 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000096 free(tmpdir);
97 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010098
99 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
100 if (ipc_fd < 0)
101 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +0200102 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100103
104 SAFE_FTRUNCATE(ipc_fd, size);
105
106 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
107
108 /* Checkpoints needs to be accessible from processes started by exec() */
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200109 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100110 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200111 putenv(ipc_path);
112 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100113 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200114 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100115
116 SAFE_CLOSE(ipc_fd);
117
118 if (tst_test->needs_checkpoints) {
119 tst_futexes = (char*)results + sizeof(struct results);
120 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
121 }
122}
123
124static void cleanup_ipc(void)
125{
126 size_t size = getpagesize();
127
128 if (ipc_fd > 0 && close(ipc_fd))
129 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
130
Cyril Hrubis9726b902017-07-27 11:28:50 +0200131 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100132 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
133
Cyril Hrubis9726b902017-07-27 11:28:50 +0200134 if (results) {
135 msync((void*)results, size, MS_SYNC);
136 munmap((void*)results, size);
137 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100138}
139
140void tst_reinit(void)
141{
Jan Stancek03fc5372017-10-10 13:36:59 +0200142 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100143 size_t size = getpagesize();
144 int fd;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100145
146 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200147 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100148
149 if (access(path, F_OK))
150 tst_brk(TBROK, "File %s does not exist!", path);
151
152 fd = SAFE_OPEN(path, O_RDWR);
153
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200154 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
155 tst_futexes = (char*)results + sizeof(struct results);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100156 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
157
158 SAFE_CLOSE(fd);
159}
160
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100161static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100162{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100163 if (!results)
164 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100165
166 switch (ttype) {
167 case TCONF:
168 tst_atomic_inc(&results->skipped);
169 break;
170 case TPASS:
171 tst_atomic_inc(&results->passed);
172 break;
173 case TWARN:
174 tst_atomic_inc(&results->warnings);
175 break;
176 case TFAIL:
177 tst_atomic_inc(&results->failed);
178 break;
179 }
180}
181
182static void print_result(const char *file, const int lineno, int ttype,
183 const char *fmt, va_list va)
184{
185 char buf[1024];
186 char *str = buf;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100187 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100188 const char *str_errno = NULL;
189 const char *res;
190
191 switch (TTYPE_RESULT(ttype)) {
192 case TPASS:
193 res = "PASS";
194 break;
195 case TFAIL:
196 res = "FAIL";
197 break;
198 case TBROK:
199 res = "BROK";
200 break;
201 case TCONF:
202 res = "CONF";
203 break;
204 case TWARN:
205 res = "WARN";
206 break;
207 case TINFO:
208 res = "INFO";
209 break;
210 default:
211 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100212 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100213 }
214
215 if (ttype & TERRNO)
216 str_errno = tst_strerrno(errno);
217
218 if (ttype & TTERRNO)
Christian Lanig18b780d2018-07-23 15:24:50 +0200219 str_errno = tst_strerrno(TST_ERR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100220
Richard Palethorpefb32e892018-03-13 16:24:47 +0100221 if (ttype & TRERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200222 ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
Richard Palethorpefb32e892018-03-13 16:24:47 +0100223 str_errno = tst_strerrno(ret);
224 }
225
Petr Vorela7f61332017-01-24 20:47:30 +0100226 ret = snprintf(str, size, "%s:%i: ", file, lineno);
227 str += ret;
228 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100229
Cyril Hrubis047c7272017-02-13 16:23:36 +0100230 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100231 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
232 res, ANSI_COLOR_RESET);
233 else
234 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100235 str += ret;
236 size -= ret;
237
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100238 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100239 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100240 str += MIN(ret, ssize);
241 size -= MIN(ret, ssize);
242 if (ret >= ssize) {
243 tst_res_(file, lineno, TWARN,
244 "Next message is too long and truncated:");
245 } else if (str_errno) {
246 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100247 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100248 str += MIN(ret, ssize);
249 size -= MIN(ret, ssize);
250 if (ret >= ssize)
251 tst_res_(file, lineno, TWARN,
252 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100253 }
254
255 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100256
257 fputs(buf, stderr);
258}
259
260void tst_vres_(const char *file, const int lineno, int ttype,
261 const char *fmt, va_list va)
262{
263 print_result(file, lineno, ttype, fmt, va);
264
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100265 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100266}
267
268void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100269 const char *fmt, va_list va);
270
271static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
272 const char *fmt, va_list va) = tst_vbrk_;
273
274static void tst_cvres(const char *file, const int lineno, int ttype,
275 const char *fmt, va_list va)
276{
277 if (TTYPE_RESULT(ttype) == TBROK) {
278 ttype &= ~TTYPE_MASK;
279 ttype |= TWARN;
280 }
281
282 print_result(file, lineno, ttype, fmt, va);
283 update_results(TTYPE_RESULT(ttype));
284}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100285
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200286static void do_test_cleanup(void)
287{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100288 tst_brk_handler = tst_cvres;
289
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200290 if (tst_test->cleanup)
291 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100292
293 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200294}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100295
296void tst_vbrk_(const char *file, const int lineno, int ttype,
297 const char *fmt, va_list va)
298{
299 print_result(file, lineno, ttype, fmt, va);
300
Steve Mucklec20831d2017-09-20 13:23:06 -0700301 /*
302 * The getpid implementation in some C library versions may cause cloned
303 * test threads to show the same pid as their parent when CLONE_VM is
304 * specified but CLONE_THREAD is not. Use direct syscall to avoid
305 * cleanup running in the child.
306 */
307 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200308 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100309
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200310 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200311 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200312
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100313 exit(TTYPE_RESULT(ttype));
314}
315
316void tst_res_(const char *file, const int lineno, int ttype,
317 const char *fmt, ...)
318{
319 va_list va;
320
321 va_start(va, fmt);
322 tst_vres_(file, lineno, ttype, fmt, va);
323 va_end(va);
324}
325
326void tst_brk_(const char *file, const int lineno, int ttype,
327 const char *fmt, ...)
328{
329 va_list va;
330
331 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100332 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100333 va_end(va);
334}
335
336static void check_child_status(pid_t pid, int status)
337{
338 int ret;
339
340 if (WIFSIGNALED(status)) {
341 tst_brk(TBROK, "Child (%i) killed by signal %s",
342 pid, tst_strsig(WTERMSIG(status)));
343 }
344
345 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100346 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100347
348 ret = WEXITSTATUS(status);
349 switch (ret) {
350 case TPASS:
351 break;
352 case TBROK:
353 case TCONF:
354 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200355 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100356 default:
357 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
358 }
359}
360
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300361void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100362{
363 int status;
364 pid_t pid;
365
366 for (;;) {
367 pid = wait(&status);
368
369 if (pid > 0) {
370 check_child_status(pid, status);
371 continue;
372 }
373
374 if (errno == ECHILD)
375 break;
376
377 if (errno == EINTR)
378 continue;
379
380 tst_brk(TBROK | TERRNO, "wait() failed");
381 }
382}
383
384
385pid_t safe_fork(const char *filename, unsigned int lineno)
386{
387 pid_t pid;
388
389 if (!tst_test->forks_child)
390 tst_brk(TBROK, "test.forks_child must be set!");
391
Michael Moese9da42372018-03-09 15:16:09 +0100392 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100393
394 pid = fork();
395 if (pid < 0)
396 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
397
398 return pid;
399}
400
401static struct option {
402 char *optstr;
403 char *help;
404} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200405 {"h", "-h Prints this help"},
406 {"i:", "-i n Execute test n times"},
407 {"I:", "-I x Execute test for n seconds"},
408 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100409};
410
411static void print_help(void)
412{
413 unsigned int i;
414
415 for (i = 0; i < ARRAY_SIZE(options); i++)
416 fprintf(stderr, "%s\n", options[i].help);
417
418 if (!tst_test->options)
419 return;
420
421 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200422 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100423}
424
425static void check_option_collision(void)
426{
427 unsigned int i, j;
428 struct tst_option *toptions = tst_test->options;
429
430 if (!toptions)
431 return;
432
433 for (i = 0; toptions[i].optstr; i++) {
434 for (j = 0; j < ARRAY_SIZE(options); j++) {
435 if (toptions[i].optstr[0] == options[j].optstr[0]) {
436 tst_brk(TBROK, "Option collision '%s'",
437 options[j].help);
438 }
439 }
440 }
441}
442
443static unsigned int count_options(void)
444{
445 unsigned int i;
446
447 if (!tst_test->options)
448 return 0;
449
450 for (i = 0; tst_test->options[i].optstr; i++);
451
452 return i;
453}
454
455static void parse_topt(unsigned int topts_len, int opt, char *optarg)
456{
457 unsigned int i;
458 struct tst_option *toptions = tst_test->options;
459
460 for (i = 0; i < topts_len; i++) {
461 if (toptions[i].optstr[0] == opt)
462 break;
463 }
464
465 if (i >= topts_len)
466 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
467
Jan Stancekc07d36c2016-08-01 11:44:55 +0200468 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100469}
470
471/* see self_exec.c */
472#ifdef UCLINUX
473extern char *child_args;
474#endif
475
476static void parse_opts(int argc, char *argv[])
477{
478 unsigned int i, topts_len = count_options();
479 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
480 int opt;
481
482 check_option_collision();
483
484 optstr[0] = 0;
485
486 for (i = 0; i < ARRAY_SIZE(options); i++)
487 strcat(optstr, options[i].optstr);
488
489 for (i = 0; i < topts_len; i++)
490 strcat(optstr, tst_test->options[i].optstr);
491
492 while ((opt = getopt(argc, argv, optstr)) > 0) {
493 switch (opt) {
494 case '?':
495 print_help();
496 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200497 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100498 case 'h':
499 print_help();
500 exit(0);
501 case 'i':
502 iterations = atoi(optarg);
503 break;
504 case 'I':
505 duration = atof(optarg);
506 break;
507 case 'C':
508#ifdef UCLINUX
509 child_args = optarg;
510#endif
511 break;
512 default:
513 parse_topt(topts_len, opt, optarg);
514 }
515 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200516
517 if (optind < argc)
518 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100519}
520
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200521int tst_parse_int(const char *str, int *val, int min, int max)
522{
523 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300524
525 if (!str)
526 return 0;
527
528 int ret = tst_parse_long(str, &rval, min, max);
529
530 if (ret)
531 return ret;
532
533 *val = (int)rval;
534 return 0;
535}
536
537int tst_parse_long(const char *str, long *val, long min, long max)
538{
539 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200540 char *end;
541
542 if (!str)
543 return 0;
544
545 errno = 0;
546 rval = strtol(str, &end, 10);
547
548 if (str == end || *end != '\0')
549 return EINVAL;
550
551 if (errno)
552 return errno;
553
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300554 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200555 return ERANGE;
556
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300557 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200558 return 0;
559}
560
561int tst_parse_float(const char *str, float *val, float min, float max)
562{
563 double rval;
564 char *end;
565
566 if (!str)
567 return 0;
568
569 errno = 0;
570 rval = strtod(str, &end);
571
572 if (str == end || *end != '\0')
573 return EINVAL;
574
575 if (errno)
576 return errno;
577
578 if (rval > (double)max || rval < (double)min)
579 return ERANGE;
580
581 *val = (float)rval;
582 return 0;
583}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100584
Cyril Hrubisfa495172016-06-08 16:06:35 +0200585static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100586{
Xiao Yang11dfc322016-06-16 15:52:04 +0800587 if (results) {
588 printf("\nSummary:\n");
589 printf("passed %d\n", results->passed);
590 printf("failed %d\n", results->failed);
591 printf("skipped %d\n", results->skipped);
592 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100593
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100594 if (results->passed && ret == TCONF)
595 ret = 0;
596
Xiao Yang11dfc322016-06-16 15:52:04 +0800597 if (results->failed)
598 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100599
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200600 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800601 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100602
Xiao Yang11dfc322016-06-16 15:52:04 +0800603 if (results->warnings)
604 ret |= TWARN;
605 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100606
Jan Stancek332540e2016-06-08 16:48:22 +0200607 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100608
609 exit(ret);
610}
611
612void check_kver(void)
613{
614 int v1, v2, v3;
615
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100616 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
617 tst_res(TWARN,
618 "Invalid kernel version %s, expected %%d.%%d.%%d",
619 tst_test->min_kver);
620 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100621
622 if (tst_kvercmp(v1, v2, v3) < 0) {
623 tst_brk(TCONF, "The test requires kernel %s or newer",
624 tst_test->min_kver);
625 }
626}
627
628static int results_equal(struct results *a, struct results *b)
629{
630 if (a->passed != b->passed)
631 return 0;
632
633 if (a->failed != b->failed)
634 return 0;
635
636 if (a->skipped != b->skipped)
637 return 0;
638
639 return 1;
640}
641
642static int needs_tmpdir(void)
643{
644 return tst_test->needs_tmpdir ||
645 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200646 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100647 tst_test->resource_files ||
648 tst_test->needs_checkpoints;
649}
650
651static void copy_resources(void)
652{
653 unsigned int i;
654
655 for (i = 0; tst_test->resource_files[i]; i++)
656 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
657}
658
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800659static const char *get_tid(char *argv[])
660{
661 char *p;
662
663 if (!argv[0] || !argv[0][0]) {
664 tst_res(TINFO, "argv[0] is empty!");
665 return "ltp_empty_argv";
666 }
667
668 p = strrchr(argv[0], '/');
669 if (p)
670 return p+1;
671
672 return argv[0];
673}
674
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100675static struct tst_device tdev;
676struct tst_device *tst_device;
677
Cyril Hrubisc4596542017-06-20 15:42:18 +0200678static void assert_test_fn(void)
679{
680 int cnt = 0;
681
682 if (tst_test->test)
683 cnt++;
684
685 if (tst_test->test_all)
686 cnt++;
687
688 if (tst_test->sample)
689 cnt++;
690
691 if (!cnt)
692 tst_brk(TBROK, "No test function speficied");
693
694 if (cnt != 1)
695 tst_brk(TBROK, "You can define only one test function");
696
697 if (tst_test->test && !tst_test->tcnt)
698 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
699
700 if (!tst_test->test && tst_test->tcnt)
701 tst_brk(TBROK, "You can define tcnt only for test()");
702}
703
Cyril Hrubise6da8672018-04-05 16:01:48 +0200704static int prepare_and_mount_ro_fs(const char *dev,
705 const char *mntpoint,
706 const char *fs_type)
707{
708 char buf[PATH_MAX];
709
710 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
711 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
712 dev, mntpoint, fs_type);
713 return 1;
714 }
715
716 mntpoint_mounted = 1;
717
718 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
719 SAFE_MKDIR(buf, 0777);
720
721 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
722 SAFE_FILE_PRINTF(buf, "file content");
723 SAFE_CHMOD(buf, 0777);
724
725 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
726
727 return 0;
728}
729
Xiao Yang7313a872018-09-01 12:07:41 +0800730static void prepare_and_mount_dev_fs(const char *mntpoint)
731{
732 const char *flags[] = {"nodev", NULL};
733 int mounted_nodev;
734
735 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
736 if (mounted_nodev) {
737 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
738 "mounting tmpfs without nodev on %s", mntpoint);
739 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
740 mntpoint_mounted = 1;
741 }
742}
743
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200744static void prepare_device(void)
745{
746 if (tst_test->format_device) {
747 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
748 tst_test->dev_extra_opt);
749 }
750
Cyril Hrubise6da8672018-04-05 16:01:48 +0200751 if (tst_test->needs_rofs) {
752 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
753 tdev.fs_type);
754 return;
755 }
756
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200757 if (tst_test->mount_device) {
758 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
759 tst_test->mnt_flags, tst_test->mnt_data);
760 mntpoint_mounted = 1;
761 }
762}
763
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100764static void do_setup(int argc, char *argv[])
765{
766 if (!tst_test)
767 tst_brk(TBROK, "No tests to run");
768
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200769 if (tst_test->tconf_msg)
770 tst_brk(TCONF, "%s", tst_test->tconf_msg);
771
Cyril Hrubisc4596542017-06-20 15:42:18 +0200772 assert_test_fn();
773
Li Wangb5d620a2017-11-01 13:15:23 +0800774 tid = get_tid(argv);
775
Cyril Hrubisc4596542017-06-20 15:42:18 +0200776 if (tst_test->sample)
777 tst_test = tst_timer_test_setup(tst_test);
778
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200779 parse_opts(argc, argv);
780
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100781 if (tst_test->needs_root && geteuid() != 0)
782 tst_brk(TCONF, "Test needs to be run as root");
783
784 if (tst_test->min_kver)
785 check_kver();
786
Cyril Hrubis874326d2017-02-14 15:59:40 +0100787 if (tst_test->format_device)
788 tst_test->needs_device = 1;
789
790 if (tst_test->mount_device) {
791 tst_test->needs_device = 1;
792 tst_test->format_device = 1;
793 }
794
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200795 if (tst_test->all_filesystems)
796 tst_test->needs_device = 1;
797
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100798 setup_ipc();
799
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000800 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100801 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100802
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700803 if (tst_test->mntpoint)
804 SAFE_MKDIR(tst_test->mntpoint, 0777);
805
Xiao Yang7313a872018-09-01 12:07:41 +0800806 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
807 tst_test->mount_device || tst_test->all_filesystems) &&
808 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700809 tst_brk(TBROK, "tst_test->mntpoint must be set!");
810 }
811
Xiao Yang7313a872018-09-01 12:07:41 +0800812 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
813 !!tst_test->needs_device > 1) {
814 tst_brk(TBROK,
815 "Two or more of needs_{rofs, devfs, device} are set");
816 }
817
818 if (tst_test->needs_devfs)
819 prepare_and_mount_dev_fs(tst_test->mntpoint);
820
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700821 if (tst_test->needs_rofs) {
822 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200823 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700824 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200825 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
826 tst_res(TINFO, "Can't mount tmpfs read-only, "
827 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700828 tst_test->needs_device = 1;
829 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700830 }
831 }
832
833 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100834 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100835
836 if (!tdev.dev)
837 tst_brk(TCONF, "Failed to acquire device");
838
839 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100840
841 if (tst_test->dev_fs_type)
842 tdev.fs_type = tst_test->dev_fs_type;
843 else
844 tdev.fs_type = tst_dev_fs_type();
845
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200846 if (!tst_test->all_filesystems)
847 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100848 }
849
850 if (tst_test->resource_files)
851 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200852}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100853
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200854static void do_test_setup(void)
855{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100856 main_pid = getpid();
857
858 if (tst_test->setup)
859 tst_test->setup();
860
861 if (main_pid != getpid())
862 tst_brk(TBROK, "Runaway child in setup()!");
863}
864
865static void do_cleanup(void)
866{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700867 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100868 tst_umount(tst_test->mntpoint);
869
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100870 if (tst_test->needs_device && tdev.dev)
871 tst_release_device(tdev.dev);
872
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000873 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100874 /* avoid munmap() on wrong pointer in tst_rmdir() */
875 tst_futexes = NULL;
876 tst_rmdir();
877 }
Jan Stancek332540e2016-06-08 16:48:22 +0200878
879 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100880}
881
882static void run_tests(void)
883{
884 unsigned int i;
885 struct results saved_results;
886
887 if (!tst_test->test) {
888 saved_results = *results;
889 tst_test->test_all();
890
891 if (getpid() != main_pid) {
892 exit(0);
893 }
894
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300895 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100896
897 if (results_equal(&saved_results, results))
898 tst_brk(TBROK, "Test haven't reported results!");
899 return;
900 }
901
902 for (i = 0; i < tst_test->tcnt; i++) {
903 saved_results = *results;
904 tst_test->test(i);
905
906 if (getpid() != main_pid) {
907 exit(0);
908 }
909
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300910 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100911
912 if (results_equal(&saved_results, results))
913 tst_brk(TBROK, "Test %i haven't reported results!", i);
914 }
915}
916
917static unsigned long long get_time_ms(void)
918{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100919 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100920
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100921 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
922 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100923
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100924 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100925}
926
Jan Stancekb95b1992017-10-10 15:47:58 +0200927static void add_paths(void)
928{
929 char *old_path = getenv("PATH");
930 const char *start_dir;
931 char *new_path;
932
933 start_dir = tst_get_startwd();
934
935 if (old_path)
936 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
937 else
938 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
939
940 SAFE_SETENV("PATH", new_path, 1);
941 free(new_path);
942}
943
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200944static void heartbeat(void)
945{
Jan Stancek1893e012018-08-28 16:17:52 +0200946 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
947 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
948
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200949 kill(getppid(), SIGUSR1);
950}
951
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200952static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100953{
954 unsigned int i = 0;
955 unsigned long long stop_time = 0;
956 int cont = 1;
957
Jan Stancek1893e012018-08-28 16:17:52 +0200958 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +0200959 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200960 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100961
962 if (duration > 0)
963 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
964
965 for (;;) {
966 cont = 0;
967
968 if (i < (unsigned int)iterations) {
969 i++;
970 cont = 1;
971 }
972
973 if (stop_time && get_time_ms() < stop_time)
974 cont = 1;
975
976 if (!cont)
977 break;
978
979 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200980 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100981 }
982
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200983 do_test_cleanup();
984 exit(0);
985}
986
987static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200988
Cyril Hrubis79163172017-05-18 10:57:07 +0200989
990static volatile sig_atomic_t sigkill_retries;
991
992#define WRITE_MSG(msg) do { \
993 if (write(2, msg, sizeof(msg) - 1)) { \
994 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
995 } \
996} while (0)
997
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200998static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
999{
Cyril Hrubis79163172017-05-18 10:57:07 +02001000 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001001 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001002 alarm(5);
1003
1004 if (++sigkill_retries > 10) {
1005 WRITE_MSG("Cannot kill test processes!\n");
1006 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1007 WRITE_MSG("Exitting uncleanly...\n");
1008 _exit(TFAIL);
1009 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001010}
1011
1012static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1013{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001014 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001015 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001016}
1017
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001018static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1019{
1020 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001021 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001022 kill(-test_pid, SIGKILL);
1023 }
1024}
1025
Jan Stancek1893e012018-08-28 16:17:52 +02001026unsigned int tst_timeout_remaining(void)
1027{
1028 static struct timespec now;
1029 unsigned int elapsed;
1030
1031 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1032 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1033
1034 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1035 if (results->timeout > elapsed)
1036 return results->timeout - elapsed;
1037
1038 return 0;
1039}
1040
Li Wang94823cf2017-07-18 16:23:00 +08001041void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001042{
1043 char *mul = getenv("LTP_TIMEOUT_MUL");
1044
Li Wang94823cf2017-07-18 16:23:00 +08001045 if (timeout == -1) {
1046 tst_res(TINFO, "Timeout per run is disabled");
1047 return;
1048 }
1049
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001050 results->timeout = timeout;
1051
1052 if (mul) {
1053 float m = atof(mul);
1054
1055 if (m < 1)
1056 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1057
1058 results->timeout = results->timeout * m + 0.5;
1059 }
1060
1061 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1062 results->timeout/3600, (results->timeout%3600)/60,
1063 results->timeout % 60);
1064
1065 if (getpid() == lib_pid)
1066 alarm(results->timeout);
1067 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001068 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001069}
1070
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001071static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001072{
1073 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001074
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001075 if (tst_test->timeout)
1076 tst_set_timeout(tst_test->timeout);
1077 else
1078 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001079
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001080 SAFE_SIGNAL(SIGINT, sigint_handler);
1081
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001082 test_pid = fork();
1083 if (test_pid < 0)
1084 tst_brk(TBROK | TERRNO, "fork()");
1085
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001086 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001087 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1088 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1089 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001090 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001091 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001092 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001093
1094 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001095 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001096 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001097
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001098 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001099 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001100
1101 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1102 tst_res(TINFO, "If you are running on slow machine, "
1103 "try exporting LTP_TIMEOUT_MUL > 1");
1104 tst_brk(TBROK, "Test killed! (timeout?)");
1105 }
1106
1107 if (WIFSIGNALED(status))
1108 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1109
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001110 return 0;
1111}
1112
1113static int run_tcases_per_fs(void)
1114{
1115 int ret = 0;
1116 unsigned int i;
1117 const char *const *filesystems = tst_get_supported_fs_types();
1118
1119 if (!filesystems[0])
1120 tst_brk(TCONF, "There are no supported filesystems");
1121
1122 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001123
1124 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001125 tdev.fs_type = filesystems[i];
1126
1127 prepare_device();
1128
1129 ret = fork_testrun();
1130
1131 if (mntpoint_mounted) {
1132 tst_umount(tst_test->mntpoint);
1133 mntpoint_mounted = 0;
1134 }
1135
1136 if (ret == TCONF) {
1137 update_results(ret);
1138 continue;
1139 }
1140
1141 if (ret == 0)
1142 continue;
1143
1144 do_exit(ret);
1145 }
1146
1147 return ret;
1148}
1149
1150void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1151{
1152 int ret;
1153
1154 lib_pid = getpid();
1155 tst_test = self;
1156
1157 do_setup(argc, argv);
1158
1159 TCID = tid;
1160
1161 SAFE_SIGNAL(SIGALRM, alarm_handler);
1162 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1163
1164 if (tst_test->all_filesystems)
1165 ret = run_tcases_per_fs();
1166 else
1167 ret = fork_testrun();
1168
1169 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001170}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001171
1172
1173void tst_flush(void)
1174{
1175 int rval;
1176
1177 rval = fflush(stderr);
1178 if (rval != 0)
1179 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1180
1181 rval = fflush(stderr);
1182 if (rval != 0)
1183 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1184
1185}