blob: 39f26147276a66526654f6228d8e46e23a098f9d [file] [log] [blame]
Petr Vorelacc16bc2019-06-13 19:25:30 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01002/*
3 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01004 */
5
6#include <stdio.h>
7#include <stdarg.h>
8#include <unistd.h>
9#include <string.h>
10#include <stdlib.h>
11#include <errno.h>
Sandeep Patilc9a7def2017-09-19 12:49:58 -070012#include <sys/mount.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010013#include <sys/types.h>
14#include <sys/wait.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010015
16#define TST_NO_DEFAULT_MAIN
17#include "tst_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010018#include "tst_device.h"
19#include "lapi/futex.h"
Steve Mucklec20831d2017-09-20 13:23:06 -070020#include "lapi/syscalls.h"
Petr Vorel3a0ef862017-03-01 15:31:08 +010021#include "tst_ansi_color.h"
Jan Stancekb95b1992017-10-10 15:47:58 +020022#include "tst_safe_stdio.h"
Cyril Hrubisc4596542017-06-20 15:42:18 +020023#include "tst_timer_test.h"
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +010024#include "tst_clocks.h"
25#include "tst_timer.h"
Rafael David Tinoco14e43562019-01-29 15:36:55 -020026#include "tst_wallclock.h"
Jan Stancek9dcbc6d2018-11-05 09:00:02 +010027#include "tst_sys_conf.h"
Cyril Hrubisf023a612018-11-27 14:21:17 +010028#include "tst_kconfig.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010029
30#include "old_resource.h"
31#include "old_device.h"
32#include "old_tmpdir.h"
33
34struct tst_test *tst_test;
35
Li Wangb5d620a2017-11-01 13:15:23 +080036static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010037static int iterations = 1;
38static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020039static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070040static int mntpoint_mounted;
Murphy Zhoua88bbb42019-05-30 10:53:00 +080041static int ovl_mounted;
Jan Stancek1893e012018-08-28 16:17:52 +020042static struct timespec tst_start_time; /* valid only for test pid */
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010043
44struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020045 int passed;
46 int skipped;
47 int failed;
48 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020049 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010050};
51
52static struct results *results;
53
54static int ipc_fd;
55
56extern void *tst_futexes;
57extern unsigned int tst_max_futexes;
58
59#define IPC_ENV_VAR "LTP_IPC_PATH"
60
61static char ipc_path[1024];
62const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010063
64static char shm_path[1024];
65
Christian Lanig18b780d2018-07-23 15:24:50 +020066int TST_ERR;
67long TST_RET;
68
Jan Stancek332540e2016-06-08 16:48:22 +020069static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020070static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020071
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010072static void setup_ipc(void)
73{
74 size_t size = getpagesize();
75
Steven Jackson9f41dcf2016-12-21 20:09:01 +000076 if (access("/dev/shm", F_OK) == 0) {
77 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080078 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000079 } else {
80 char *tmpdir;
81
82 if (!tst_tmpdir_created())
83 tst_tmpdir();
84
85 tmpdir = tst_get_tmpdir();
86 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080087 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000088 free(tmpdir);
89 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010090
91 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
92 if (ipc_fd < 0)
93 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +020094 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010095
96 SAFE_FTRUNCATE(ipc_fd, size);
97
98 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
99
100 /* Checkpoints needs to be accessible from processes started by exec() */
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200101 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100102 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200103 putenv(ipc_path);
104 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100105 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200106 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100107
108 SAFE_CLOSE(ipc_fd);
109
110 if (tst_test->needs_checkpoints) {
111 tst_futexes = (char*)results + sizeof(struct results);
112 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
113 }
114}
115
116static void cleanup_ipc(void)
117{
118 size_t size = getpagesize();
119
120 if (ipc_fd > 0 && close(ipc_fd))
121 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
122
Cyril Hrubis9726b902017-07-27 11:28:50 +0200123 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100124 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
125
Cyril Hrubis9726b902017-07-27 11:28:50 +0200126 if (results) {
127 msync((void*)results, size, MS_SYNC);
128 munmap((void*)results, size);
Yann Sionneau511a0e12019-06-14 10:26:45 +0200129 results = NULL;
Cyril Hrubis9726b902017-07-27 11:28:50 +0200130 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100131}
132
133void tst_reinit(void)
134{
Jan Stancek03fc5372017-10-10 13:36:59 +0200135 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100136 size_t size = getpagesize();
137 int fd;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100138
139 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200140 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100141
142 if (access(path, F_OK))
143 tst_brk(TBROK, "File %s does not exist!", path);
144
145 fd = SAFE_OPEN(path, O_RDWR);
146
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200147 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
148 tst_futexes = (char*)results + sizeof(struct results);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100149 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
150
151 SAFE_CLOSE(fd);
152}
153
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100154static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100155{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100156 if (!results)
157 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100158
159 switch (ttype) {
160 case TCONF:
161 tst_atomic_inc(&results->skipped);
162 break;
163 case TPASS:
164 tst_atomic_inc(&results->passed);
165 break;
166 case TWARN:
167 tst_atomic_inc(&results->warnings);
168 break;
169 case TFAIL:
170 tst_atomic_inc(&results->failed);
171 break;
172 }
173}
174
175static void print_result(const char *file, const int lineno, int ttype,
176 const char *fmt, va_list va)
177{
178 char buf[1024];
179 char *str = buf;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100180 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100181 const char *str_errno = NULL;
182 const char *res;
183
184 switch (TTYPE_RESULT(ttype)) {
185 case TPASS:
186 res = "PASS";
187 break;
188 case TFAIL:
189 res = "FAIL";
190 break;
191 case TBROK:
192 res = "BROK";
193 break;
194 case TCONF:
195 res = "CONF";
196 break;
197 case TWARN:
198 res = "WARN";
199 break;
200 case TINFO:
201 res = "INFO";
202 break;
203 default:
204 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100205 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100206 }
207
208 if (ttype & TERRNO)
209 str_errno = tst_strerrno(errno);
210
211 if (ttype & TTERRNO)
Christian Lanig18b780d2018-07-23 15:24:50 +0200212 str_errno = tst_strerrno(TST_ERR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100213
Richard Palethorpefb32e892018-03-13 16:24:47 +0100214 if (ttype & TRERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200215 ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
Richard Palethorpefb32e892018-03-13 16:24:47 +0100216 str_errno = tst_strerrno(ret);
217 }
218
Petr Vorela7f61332017-01-24 20:47:30 +0100219 ret = snprintf(str, size, "%s:%i: ", file, lineno);
220 str += ret;
221 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100222
Cyril Hrubis047c7272017-02-13 16:23:36 +0100223 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100224 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
225 res, ANSI_COLOR_RESET);
226 else
227 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100228 str += ret;
229 size -= ret;
230
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100231 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100232 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100233 str += MIN(ret, ssize);
234 size -= MIN(ret, ssize);
235 if (ret >= ssize) {
236 tst_res_(file, lineno, TWARN,
237 "Next message is too long and truncated:");
238 } else if (str_errno) {
239 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100240 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100241 str += MIN(ret, ssize);
242 size -= MIN(ret, ssize);
243 if (ret >= ssize)
244 tst_res_(file, lineno, TWARN,
245 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100246 }
247
248 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100249
250 fputs(buf, stderr);
251}
252
253void tst_vres_(const char *file, const int lineno, int ttype,
254 const char *fmt, va_list va)
255{
256 print_result(file, lineno, ttype, fmt, va);
257
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100258 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100259}
260
261void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100262 const char *fmt, va_list va);
263
264static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
265 const char *fmt, va_list va) = tst_vbrk_;
266
267static void tst_cvres(const char *file, const int lineno, int ttype,
268 const char *fmt, va_list va)
269{
270 if (TTYPE_RESULT(ttype) == TBROK) {
271 ttype &= ~TTYPE_MASK;
272 ttype |= TWARN;
273 }
274
275 print_result(file, lineno, ttype, fmt, va);
276 update_results(TTYPE_RESULT(ttype));
277}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100278
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200279static void do_test_cleanup(void)
280{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100281 tst_brk_handler = tst_cvres;
282
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200283 if (tst_test->cleanup)
284 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100285
Cyril Hrubisee761d82019-07-31 15:02:08 +0200286 tst_free_all();
287
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100288 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200289}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100290
291void tst_vbrk_(const char *file, const int lineno, int ttype,
292 const char *fmt, va_list va)
293{
294 print_result(file, lineno, ttype, fmt, va);
295
Steve Mucklec20831d2017-09-20 13:23:06 -0700296 /*
297 * The getpid implementation in some C library versions may cause cloned
298 * test threads to show the same pid as their parent when CLONE_VM is
299 * specified but CLONE_THREAD is not. Use direct syscall to avoid
300 * cleanup running in the child.
301 */
302 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200303 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100304
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200305 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200306 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200307
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100308 exit(TTYPE_RESULT(ttype));
309}
310
311void tst_res_(const char *file, const int lineno, int ttype,
312 const char *fmt, ...)
313{
314 va_list va;
315
316 va_start(va, fmt);
317 tst_vres_(file, lineno, ttype, fmt, va);
318 va_end(va);
319}
320
321void tst_brk_(const char *file, const int lineno, int ttype,
322 const char *fmt, ...)
323{
324 va_list va;
325
326 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100327 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100328 va_end(va);
329}
330
331static void check_child_status(pid_t pid, int status)
332{
333 int ret;
334
335 if (WIFSIGNALED(status)) {
336 tst_brk(TBROK, "Child (%i) killed by signal %s",
337 pid, tst_strsig(WTERMSIG(status)));
338 }
339
340 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100341 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100342
343 ret = WEXITSTATUS(status);
344 switch (ret) {
345 case TPASS:
346 break;
347 case TBROK:
348 case TCONF:
349 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200350 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100351 default:
352 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
353 }
354}
355
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300356void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100357{
358 int status;
359 pid_t pid;
360
361 for (;;) {
362 pid = wait(&status);
363
364 if (pid > 0) {
365 check_child_status(pid, status);
366 continue;
367 }
368
369 if (errno == ECHILD)
370 break;
371
372 if (errno == EINTR)
373 continue;
374
375 tst_brk(TBROK | TERRNO, "wait() failed");
376 }
377}
378
379
380pid_t safe_fork(const char *filename, unsigned int lineno)
381{
382 pid_t pid;
383
384 if (!tst_test->forks_child)
385 tst_brk(TBROK, "test.forks_child must be set!");
386
Michael Moese9da42372018-03-09 15:16:09 +0100387 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100388
389 pid = fork();
390 if (pid < 0)
391 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
392
Cyril Hrubis13296b62019-07-31 17:39:37 +0200393 if (!pid)
394 atexit(tst_free_all);
395
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100396 return pid;
397}
398
399static struct option {
400 char *optstr;
401 char *help;
402} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200403 {"h", "-h Prints this help"},
404 {"i:", "-i n Execute test n times"},
405 {"I:", "-I x Execute test for n seconds"},
406 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100407};
408
409static void print_help(void)
410{
411 unsigned int i;
412
413 for (i = 0; i < ARRAY_SIZE(options); i++)
414 fprintf(stderr, "%s\n", options[i].help);
415
416 if (!tst_test->options)
417 return;
418
419 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200420 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100421}
422
423static void check_option_collision(void)
424{
425 unsigned int i, j;
426 struct tst_option *toptions = tst_test->options;
427
428 if (!toptions)
429 return;
430
431 for (i = 0; toptions[i].optstr; i++) {
432 for (j = 0; j < ARRAY_SIZE(options); j++) {
433 if (toptions[i].optstr[0] == options[j].optstr[0]) {
434 tst_brk(TBROK, "Option collision '%s'",
435 options[j].help);
436 }
437 }
438 }
439}
440
441static unsigned int count_options(void)
442{
443 unsigned int i;
444
445 if (!tst_test->options)
446 return 0;
447
448 for (i = 0; tst_test->options[i].optstr; i++);
449
450 return i;
451}
452
453static void parse_topt(unsigned int topts_len, int opt, char *optarg)
454{
455 unsigned int i;
456 struct tst_option *toptions = tst_test->options;
457
458 for (i = 0; i < topts_len; i++) {
459 if (toptions[i].optstr[0] == opt)
460 break;
461 }
462
463 if (i >= topts_len)
464 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
465
Jan Stancekc07d36c2016-08-01 11:44:55 +0200466 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100467}
468
469/* see self_exec.c */
470#ifdef UCLINUX
471extern char *child_args;
472#endif
473
474static void parse_opts(int argc, char *argv[])
475{
476 unsigned int i, topts_len = count_options();
477 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
478 int opt;
479
480 check_option_collision();
481
482 optstr[0] = 0;
483
484 for (i = 0; i < ARRAY_SIZE(options); i++)
485 strcat(optstr, options[i].optstr);
486
487 for (i = 0; i < topts_len; i++)
488 strcat(optstr, tst_test->options[i].optstr);
489
490 while ((opt = getopt(argc, argv, optstr)) > 0) {
491 switch (opt) {
492 case '?':
493 print_help();
494 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200495 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100496 case 'h':
497 print_help();
498 exit(0);
499 case 'i':
500 iterations = atoi(optarg);
501 break;
502 case 'I':
503 duration = atof(optarg);
504 break;
505 case 'C':
506#ifdef UCLINUX
507 child_args = optarg;
508#endif
509 break;
510 default:
511 parse_topt(topts_len, opt, optarg);
512 }
513 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200514
515 if (optind < argc)
516 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100517}
518
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200519int tst_parse_int(const char *str, int *val, int min, int max)
520{
521 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300522
523 if (!str)
524 return 0;
525
526 int ret = tst_parse_long(str, &rval, min, max);
527
528 if (ret)
529 return ret;
530
531 *val = (int)rval;
532 return 0;
533}
534
535int tst_parse_long(const char *str, long *val, long min, long max)
536{
537 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200538 char *end;
539
540 if (!str)
541 return 0;
542
543 errno = 0;
544 rval = strtol(str, &end, 10);
545
546 if (str == end || *end != '\0')
547 return EINVAL;
548
549 if (errno)
550 return errno;
551
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300552 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200553 return ERANGE;
554
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300555 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200556 return 0;
557}
558
559int tst_parse_float(const char *str, float *val, float min, float max)
560{
561 double rval;
562 char *end;
563
564 if (!str)
565 return 0;
566
567 errno = 0;
568 rval = strtod(str, &end);
569
570 if (str == end || *end != '\0')
571 return EINVAL;
572
573 if (errno)
574 return errno;
575
576 if (rval > (double)max || rval < (double)min)
577 return ERANGE;
578
579 *val = (float)rval;
580 return 0;
581}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100582
Cyril Hrubisfa495172016-06-08 16:06:35 +0200583static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100584{
Xiao Yang11dfc322016-06-16 15:52:04 +0800585 if (results) {
586 printf("\nSummary:\n");
587 printf("passed %d\n", results->passed);
588 printf("failed %d\n", results->failed);
589 printf("skipped %d\n", results->skipped);
590 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100591
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100592 if (results->passed && ret == TCONF)
593 ret = 0;
594
Xiao Yang11dfc322016-06-16 15:52:04 +0800595 if (results->failed)
596 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100597
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200598 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800599 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100600
Xiao Yang11dfc322016-06-16 15:52:04 +0800601 if (results->warnings)
602 ret |= TWARN;
603 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100604
Jan Stancek332540e2016-06-08 16:48:22 +0200605 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100606
607 exit(ret);
608}
609
610void check_kver(void)
611{
612 int v1, v2, v3;
613
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100614 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
615 tst_res(TWARN,
616 "Invalid kernel version %s, expected %%d.%%d.%%d",
617 tst_test->min_kver);
618 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100619
620 if (tst_kvercmp(v1, v2, v3) < 0) {
621 tst_brk(TCONF, "The test requires kernel %s or newer",
622 tst_test->min_kver);
623 }
624}
625
626static int results_equal(struct results *a, struct results *b)
627{
628 if (a->passed != b->passed)
629 return 0;
630
631 if (a->failed != b->failed)
632 return 0;
633
634 if (a->skipped != b->skipped)
635 return 0;
636
637 return 1;
638}
639
640static int needs_tmpdir(void)
641{
642 return tst_test->needs_tmpdir ||
643 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200644 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100645 tst_test->resource_files ||
646 tst_test->needs_checkpoints;
647}
648
649static void copy_resources(void)
650{
651 unsigned int i;
652
653 for (i = 0; tst_test->resource_files[i]; i++)
654 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
655}
656
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800657static const char *get_tid(char *argv[])
658{
659 char *p;
660
661 if (!argv[0] || !argv[0][0]) {
662 tst_res(TINFO, "argv[0] is empty!");
663 return "ltp_empty_argv";
664 }
665
666 p = strrchr(argv[0], '/');
667 if (p)
668 return p+1;
669
670 return argv[0];
671}
672
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100673static struct tst_device tdev;
674struct tst_device *tst_device;
675
Cyril Hrubisc4596542017-06-20 15:42:18 +0200676static void assert_test_fn(void)
677{
678 int cnt = 0;
679
680 if (tst_test->test)
681 cnt++;
682
683 if (tst_test->test_all)
684 cnt++;
685
686 if (tst_test->sample)
687 cnt++;
688
689 if (!cnt)
690 tst_brk(TBROK, "No test function speficied");
691
692 if (cnt != 1)
693 tst_brk(TBROK, "You can define only one test function");
694
695 if (tst_test->test && !tst_test->tcnt)
Eric Biggers3614a162019-03-15 09:42:15 -0700696 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
Cyril Hrubisc4596542017-06-20 15:42:18 +0200697
698 if (!tst_test->test && tst_test->tcnt)
699 tst_brk(TBROK, "You can define tcnt only for test()");
700}
701
Cyril Hrubise6da8672018-04-05 16:01:48 +0200702static int prepare_and_mount_ro_fs(const char *dev,
703 const char *mntpoint,
704 const char *fs_type)
705{
706 char buf[PATH_MAX];
707
708 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
709 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
710 dev, mntpoint, fs_type);
711 return 1;
712 }
713
714 mntpoint_mounted = 1;
715
716 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
717 SAFE_MKDIR(buf, 0777);
718
719 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
720 SAFE_FILE_PRINTF(buf, "file content");
721 SAFE_CHMOD(buf, 0777);
722
723 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
724
725 return 0;
726}
727
Xiao Yang7313a872018-09-01 12:07:41 +0800728static void prepare_and_mount_dev_fs(const char *mntpoint)
729{
730 const char *flags[] = {"nodev", NULL};
731 int mounted_nodev;
732
733 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
734 if (mounted_nodev) {
735 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
736 "mounting tmpfs without nodev on %s", mntpoint);
737 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
738 mntpoint_mounted = 1;
739 }
740}
741
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200742static void prepare_device(void)
743{
744 if (tst_test->format_device) {
745 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200746 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200747 }
748
Cyril Hrubise6da8672018-04-05 16:01:48 +0200749 if (tst_test->needs_rofs) {
750 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
751 tdev.fs_type);
752 return;
753 }
754
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200755 if (tst_test->mount_device) {
756 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
757 tst_test->mnt_flags, tst_test->mnt_data);
758 mntpoint_mounted = 1;
759 }
760}
761
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100762static void do_setup(int argc, char *argv[])
763{
764 if (!tst_test)
765 tst_brk(TBROK, "No tests to run");
766
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200767 if (tst_test->tconf_msg)
768 tst_brk(TCONF, "%s", tst_test->tconf_msg);
769
Cyril Hrubisf023a612018-11-27 14:21:17 +0100770 if (tst_test->needs_kconfigs)
771 tst_kconfig_check(tst_test->needs_kconfigs);
772
Cyril Hrubisc4596542017-06-20 15:42:18 +0200773 assert_test_fn();
774
Li Wangb5d620a2017-11-01 13:15:23 +0800775 tid = get_tid(argv);
776
Cyril Hrubisc4596542017-06-20 15:42:18 +0200777 if (tst_test->sample)
778 tst_test = tst_timer_test_setup(tst_test);
779
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200780 parse_opts(argc, argv);
781
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100782 if (tst_test->needs_root && geteuid() != 0)
783 tst_brk(TCONF, "Test needs to be run as root");
784
785 if (tst_test->min_kver)
786 check_kver();
787
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300788 if (tst_test->needs_drivers) {
789 const char *name;
790 int i;
791
792 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
793 if (tst_check_driver(name))
794 tst_brk(TCONF, "%s driver not available", name);
795 }
796
Cyril Hrubis874326d2017-02-14 15:59:40 +0100797 if (tst_test->format_device)
798 tst_test->needs_device = 1;
799
800 if (tst_test->mount_device) {
801 tst_test->needs_device = 1;
802 tst_test->format_device = 1;
803 }
804
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200805 if (tst_test->all_filesystems)
806 tst_test->needs_device = 1;
807
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100808 setup_ipc();
809
Cyril Hrubisee761d82019-07-31 15:02:08 +0200810 if (tst_test->bufs)
811 tst_buffers_alloc(tst_test->bufs);
812
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000813 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100814 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100815
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100816 if (tst_test->save_restore) {
817 const char * const *name = tst_test->save_restore;
818
819 while (*name) {
820 tst_sys_conf_save(*name);
821 name++;
822 }
823 }
824
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700825 if (tst_test->mntpoint)
826 SAFE_MKDIR(tst_test->mntpoint, 0777);
827
Xiao Yang7313a872018-09-01 12:07:41 +0800828 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
829 tst_test->mount_device || tst_test->all_filesystems) &&
830 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700831 tst_brk(TBROK, "tst_test->mntpoint must be set!");
832 }
833
Xiao Yang7313a872018-09-01 12:07:41 +0800834 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
835 !!tst_test->needs_device > 1) {
836 tst_brk(TBROK,
837 "Two or more of needs_{rofs, devfs, device} are set");
838 }
839
840 if (tst_test->needs_devfs)
841 prepare_and_mount_dev_fs(tst_test->mntpoint);
842
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700843 if (tst_test->needs_rofs) {
844 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200845 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700846 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200847 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
848 tst_res(TINFO, "Can't mount tmpfs read-only, "
849 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700850 tst_test->needs_device = 1;
851 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700852 }
853 }
854
855 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100856 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100857
858 if (!tdev.dev)
859 tst_brk(TCONF, "Failed to acquire device");
860
861 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100862
863 if (tst_test->dev_fs_type)
864 tdev.fs_type = tst_test->dev_fs_type;
865 else
866 tdev.fs_type = tst_dev_fs_type();
867
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200868 if (!tst_test->all_filesystems)
869 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100870 }
871
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800872 if (tst_test->needs_overlay && !tst_test->mount_device) {
873 tst_brk(TBROK, "tst_test->mount_device must be set");
874 }
875 if (tst_test->needs_overlay && !mntpoint_mounted) {
876 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
877 }
878 if (tst_test->needs_overlay && !ovl_mounted) {
879 SAFE_MOUNT_OVERLAY();
880 ovl_mounted = 1;
881 }
882
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100883 if (tst_test->resource_files)
884 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200885
886 if (tst_test->restore_wallclock)
887 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200888}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100889
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200890static void do_test_setup(void)
891{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100892 main_pid = getpid();
893
894 if (tst_test->setup)
895 tst_test->setup();
896
897 if (main_pid != getpid())
898 tst_brk(TBROK, "Runaway child in setup()!");
899}
900
901static void do_cleanup(void)
902{
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800903 if (ovl_mounted)
904 SAFE_UMOUNT(OVL_MNT);
905
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700906 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100907 tst_umount(tst_test->mntpoint);
908
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100909 if (tst_test->needs_device && tdev.dev)
910 tst_release_device(tdev.dev);
911
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000912 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100913 /* avoid munmap() on wrong pointer in tst_rmdir() */
914 tst_futexes = NULL;
915 tst_rmdir();
916 }
Jan Stancek332540e2016-06-08 16:48:22 +0200917
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100918 if (tst_test->save_restore)
919 tst_sys_conf_restore(0);
920
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200921 if (tst_test->restore_wallclock)
922 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +0200923
924 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100925}
926
927static void run_tests(void)
928{
929 unsigned int i;
930 struct results saved_results;
931
932 if (!tst_test->test) {
933 saved_results = *results;
934 tst_test->test_all();
935
936 if (getpid() != main_pid) {
937 exit(0);
938 }
939
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300940 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100941
942 if (results_equal(&saved_results, results))
943 tst_brk(TBROK, "Test haven't reported results!");
944 return;
945 }
946
947 for (i = 0; i < tst_test->tcnt; i++) {
948 saved_results = *results;
949 tst_test->test(i);
950
951 if (getpid() != main_pid) {
952 exit(0);
953 }
954
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300955 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100956
957 if (results_equal(&saved_results, results))
958 tst_brk(TBROK, "Test %i haven't reported results!", i);
959 }
960}
961
962static unsigned long long get_time_ms(void)
963{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100964 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100965
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100966 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
967 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100968
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100969 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100970}
971
Jan Stancekb95b1992017-10-10 15:47:58 +0200972static void add_paths(void)
973{
974 char *old_path = getenv("PATH");
975 const char *start_dir;
976 char *new_path;
977
978 start_dir = tst_get_startwd();
979
980 if (old_path)
981 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
982 else
983 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
984
985 SAFE_SETENV("PATH", new_path, 1);
986 free(new_path);
987}
988
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200989static void heartbeat(void)
990{
Jan Stancek1893e012018-08-28 16:17:52 +0200991 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
992 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
993
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200994 kill(getppid(), SIGUSR1);
995}
996
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200997static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100998{
999 unsigned int i = 0;
1000 unsigned long long stop_time = 0;
1001 int cont = 1;
1002
Jan Stancek1893e012018-08-28 16:17:52 +02001003 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001004 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001005 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001006
1007 if (duration > 0)
1008 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1009
1010 for (;;) {
1011 cont = 0;
1012
1013 if (i < (unsigned int)iterations) {
1014 i++;
1015 cont = 1;
1016 }
1017
1018 if (stop_time && get_time_ms() < stop_time)
1019 cont = 1;
1020
1021 if (!cont)
1022 break;
1023
1024 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001025 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001026 }
1027
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001028 do_test_cleanup();
1029 exit(0);
1030}
1031
1032static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001033
Cyril Hrubis79163172017-05-18 10:57:07 +02001034
1035static volatile sig_atomic_t sigkill_retries;
1036
1037#define WRITE_MSG(msg) do { \
1038 if (write(2, msg, sizeof(msg) - 1)) { \
1039 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1040 } \
1041} while (0)
1042
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001043static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1044{
Cyril Hrubis79163172017-05-18 10:57:07 +02001045 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001046 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001047 alarm(5);
1048
1049 if (++sigkill_retries > 10) {
1050 WRITE_MSG("Cannot kill test processes!\n");
1051 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1052 WRITE_MSG("Exitting uncleanly...\n");
1053 _exit(TFAIL);
1054 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001055}
1056
1057static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1058{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001059 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001060 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001061}
1062
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001063static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1064{
1065 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001066 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001067 kill(-test_pid, SIGKILL);
1068 }
1069}
1070
Jan Stancek1893e012018-08-28 16:17:52 +02001071unsigned int tst_timeout_remaining(void)
1072{
1073 static struct timespec now;
1074 unsigned int elapsed;
1075
1076 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1077 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1078
1079 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1080 if (results->timeout > elapsed)
1081 return results->timeout - elapsed;
1082
1083 return 0;
1084}
1085
Li Wang94823cf2017-07-18 16:23:00 +08001086void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001087{
1088 char *mul = getenv("LTP_TIMEOUT_MUL");
1089
Li Wang94823cf2017-07-18 16:23:00 +08001090 if (timeout == -1) {
1091 tst_res(TINFO, "Timeout per run is disabled");
1092 return;
1093 }
1094
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001095 results->timeout = timeout;
1096
1097 if (mul) {
1098 float m = atof(mul);
1099
1100 if (m < 1)
1101 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1102
1103 results->timeout = results->timeout * m + 0.5;
1104 }
1105
1106 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1107 results->timeout/3600, (results->timeout%3600)/60,
1108 results->timeout % 60);
1109
1110 if (getpid() == lib_pid)
1111 alarm(results->timeout);
1112 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001113 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001114}
1115
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001116static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001117{
1118 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001119
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001120 if (tst_test->timeout)
1121 tst_set_timeout(tst_test->timeout);
1122 else
1123 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001124
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001125 SAFE_SIGNAL(SIGINT, sigint_handler);
1126
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001127 test_pid = fork();
1128 if (test_pid < 0)
1129 tst_brk(TBROK | TERRNO, "fork()");
1130
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001131 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001132 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1133 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1134 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001135 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001136 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001137 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001138
1139 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001140 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001141 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001142
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001143 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001144 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001145
1146 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1147 tst_res(TINFO, "If you are running on slow machine, "
1148 "try exporting LTP_TIMEOUT_MUL > 1");
1149 tst_brk(TBROK, "Test killed! (timeout?)");
1150 }
1151
1152 if (WIFSIGNALED(status))
1153 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1154
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001155 return 0;
1156}
1157
1158static int run_tcases_per_fs(void)
1159{
1160 int ret = 0;
1161 unsigned int i;
Cyril Hrubisdbe56e52019-06-19 14:20:19 +02001162 const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001163
1164 if (!filesystems[0])
1165 tst_brk(TCONF, "There are no supported filesystems");
1166
1167 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001168
1169 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001170 tdev.fs_type = filesystems[i];
1171
1172 prepare_device();
1173
1174 ret = fork_testrun();
1175
1176 if (mntpoint_mounted) {
1177 tst_umount(tst_test->mntpoint);
1178 mntpoint_mounted = 0;
1179 }
1180
1181 if (ret == TCONF) {
1182 update_results(ret);
1183 continue;
1184 }
1185
1186 if (ret == 0)
1187 continue;
1188
1189 do_exit(ret);
1190 }
1191
1192 return ret;
1193}
1194
Cyril Hrubisce082162019-03-06 16:24:29 +01001195unsigned int tst_variant;
1196
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001197void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1198{
Cyril Hrubisce082162019-03-06 16:24:29 +01001199 int ret = 0;
1200 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001201
1202 lib_pid = getpid();
1203 tst_test = self;
1204
1205 do_setup(argc, argv);
1206
1207 TCID = tid;
1208
1209 SAFE_SIGNAL(SIGALRM, alarm_handler);
1210 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1211
Cyril Hrubisce082162019-03-06 16:24:29 +01001212 if (tst_test->test_variants)
1213 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001214
Cyril Hrubisce082162019-03-06 16:24:29 +01001215 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1216 if (tst_test->all_filesystems)
1217 ret |= run_tcases_per_fs();
1218 else
1219 ret |= fork_testrun();
1220
1221 if (ret & ~(TCONF))
1222 goto exit;
1223 }
1224
1225exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001226 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001227}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001228
1229
1230void tst_flush(void)
1231{
1232 int rval;
1233
1234 rval = fflush(stderr);
1235 if (rval != 0)
1236 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1237
1238 rval = fflush(stderr);
1239 if (rval != 0)
1240 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1241
1242}