blob: d2cf926080f3c8f3aa9801f8f665a66f8e9dee88 [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;
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200180 int ret, size = sizeof(buf), ssize, int_errno;
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
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200208 if (ttype & TERRNO) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100209 str_errno = tst_strerrno(errno);
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200210 int_errno = errno;
211 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100212
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200213 if (ttype & TTERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200214 str_errno = tst_strerrno(TST_ERR);
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200215 int_errno = TST_ERR;
216 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100217
Richard Palethorpefb32e892018-03-13 16:24:47 +0100218 if (ttype & TRERRNO) {
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200219 int_errno = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
220 str_errno = tst_strerrno(int_errno);
Richard Palethorpefb32e892018-03-13 16:24:47 +0100221 }
222
Petr Vorela7f61332017-01-24 20:47:30 +0100223 ret = snprintf(str, size, "%s:%i: ", file, lineno);
224 str += ret;
225 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100226
Cyril Hrubis047c7272017-02-13 16:23:36 +0100227 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100228 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
229 res, ANSI_COLOR_RESET);
230 else
231 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100232 str += ret;
233 size -= ret;
234
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100235 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100236 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100237 str += MIN(ret, ssize);
238 size -= MIN(ret, ssize);
239 if (ret >= ssize) {
240 tst_res_(file, lineno, TWARN,
241 "Next message is too long and truncated:");
242 } else if (str_errno) {
243 ssize = size - 2;
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200244 ret = snprintf(str, size, ": %s (%d)", str_errno, int_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100245 str += MIN(ret, ssize);
246 size -= MIN(ret, ssize);
247 if (ret >= ssize)
248 tst_res_(file, lineno, TWARN,
249 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100250 }
251
252 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100253
254 fputs(buf, stderr);
255}
256
257void tst_vres_(const char *file, const int lineno, int ttype,
258 const char *fmt, va_list va)
259{
260 print_result(file, lineno, ttype, fmt, va);
261
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100262 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100263}
264
265void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100266 const char *fmt, va_list va);
267
268static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
269 const char *fmt, va_list va) = tst_vbrk_;
270
271static void tst_cvres(const char *file, const int lineno, int ttype,
272 const char *fmt, va_list va)
273{
274 if (TTYPE_RESULT(ttype) == TBROK) {
275 ttype &= ~TTYPE_MASK;
276 ttype |= TWARN;
277 }
278
279 print_result(file, lineno, ttype, fmt, va);
280 update_results(TTYPE_RESULT(ttype));
281}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100282
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200283static void do_test_cleanup(void)
284{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100285 tst_brk_handler = tst_cvres;
286
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200287 if (tst_test->cleanup)
288 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100289
Cyril Hrubisee761d82019-07-31 15:02:08 +0200290 tst_free_all();
291
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100292 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200293}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100294
295void tst_vbrk_(const char *file, const int lineno, int ttype,
296 const char *fmt, va_list va)
297{
298 print_result(file, lineno, ttype, fmt, va);
299
Steve Mucklec20831d2017-09-20 13:23:06 -0700300 /*
301 * The getpid implementation in some C library versions may cause cloned
302 * test threads to show the same pid as their parent when CLONE_VM is
303 * specified but CLONE_THREAD is not. Use direct syscall to avoid
304 * cleanup running in the child.
305 */
306 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200307 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100308
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200309 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200310 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200311
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100312 exit(TTYPE_RESULT(ttype));
313}
314
315void tst_res_(const char *file, const int lineno, int ttype,
316 const char *fmt, ...)
317{
318 va_list va;
319
320 va_start(va, fmt);
321 tst_vres_(file, lineno, ttype, fmt, va);
322 va_end(va);
323}
324
325void tst_brk_(const char *file, const int lineno, int ttype,
326 const char *fmt, ...)
327{
328 va_list va;
329
330 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100331 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100332 va_end(va);
333}
334
335static void check_child_status(pid_t pid, int status)
336{
337 int ret;
338
339 if (WIFSIGNALED(status)) {
340 tst_brk(TBROK, "Child (%i) killed by signal %s",
341 pid, tst_strsig(WTERMSIG(status)));
342 }
343
344 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100345 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100346
347 ret = WEXITSTATUS(status);
348 switch (ret) {
349 case TPASS:
350 break;
351 case TBROK:
352 case TCONF:
353 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200354 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100355 default:
356 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
357 }
358}
359
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300360void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100361{
362 int status;
363 pid_t pid;
364
365 for (;;) {
366 pid = wait(&status);
367
368 if (pid > 0) {
369 check_child_status(pid, status);
370 continue;
371 }
372
373 if (errno == ECHILD)
374 break;
375
376 if (errno == EINTR)
377 continue;
378
379 tst_brk(TBROK | TERRNO, "wait() failed");
380 }
381}
382
383
384pid_t safe_fork(const char *filename, unsigned int lineno)
385{
386 pid_t pid;
387
388 if (!tst_test->forks_child)
389 tst_brk(TBROK, "test.forks_child must be set!");
390
Michael Moese9da42372018-03-09 15:16:09 +0100391 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100392
393 pid = fork();
394 if (pid < 0)
395 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
396
Cyril Hrubis13296b62019-07-31 17:39:37 +0200397 if (!pid)
398 atexit(tst_free_all);
399
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100400 return pid;
401}
402
403static struct option {
404 char *optstr;
405 char *help;
406} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200407 {"h", "-h Prints this help"},
408 {"i:", "-i n Execute test n times"},
409 {"I:", "-I x Execute test for n seconds"},
410 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100411};
412
413static void print_help(void)
414{
415 unsigned int i;
416
417 for (i = 0; i < ARRAY_SIZE(options); i++)
418 fprintf(stderr, "%s\n", options[i].help);
419
420 if (!tst_test->options)
421 return;
422
423 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200424 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100425}
426
427static void check_option_collision(void)
428{
429 unsigned int i, j;
430 struct tst_option *toptions = tst_test->options;
431
432 if (!toptions)
433 return;
434
435 for (i = 0; toptions[i].optstr; i++) {
436 for (j = 0; j < ARRAY_SIZE(options); j++) {
437 if (toptions[i].optstr[0] == options[j].optstr[0]) {
438 tst_brk(TBROK, "Option collision '%s'",
439 options[j].help);
440 }
441 }
442 }
443}
444
445static unsigned int count_options(void)
446{
447 unsigned int i;
448
449 if (!tst_test->options)
450 return 0;
451
452 for (i = 0; tst_test->options[i].optstr; i++);
453
454 return i;
455}
456
457static void parse_topt(unsigned int topts_len, int opt, char *optarg)
458{
459 unsigned int i;
460 struct tst_option *toptions = tst_test->options;
461
462 for (i = 0; i < topts_len; i++) {
463 if (toptions[i].optstr[0] == opt)
464 break;
465 }
466
467 if (i >= topts_len)
468 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
469
Jan Stancekc07d36c2016-08-01 11:44:55 +0200470 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100471}
472
473/* see self_exec.c */
474#ifdef UCLINUX
475extern char *child_args;
476#endif
477
478static void parse_opts(int argc, char *argv[])
479{
480 unsigned int i, topts_len = count_options();
481 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
482 int opt;
483
484 check_option_collision();
485
486 optstr[0] = 0;
487
488 for (i = 0; i < ARRAY_SIZE(options); i++)
489 strcat(optstr, options[i].optstr);
490
491 for (i = 0; i < topts_len; i++)
492 strcat(optstr, tst_test->options[i].optstr);
493
494 while ((opt = getopt(argc, argv, optstr)) > 0) {
495 switch (opt) {
496 case '?':
497 print_help();
498 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200499 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100500 case 'h':
501 print_help();
502 exit(0);
503 case 'i':
504 iterations = atoi(optarg);
505 break;
506 case 'I':
507 duration = atof(optarg);
508 break;
509 case 'C':
510#ifdef UCLINUX
511 child_args = optarg;
512#endif
513 break;
514 default:
515 parse_topt(topts_len, opt, optarg);
516 }
517 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200518
519 if (optind < argc)
520 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100521}
522
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200523int tst_parse_int(const char *str, int *val, int min, int max)
524{
525 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300526
527 if (!str)
528 return 0;
529
530 int ret = tst_parse_long(str, &rval, min, max);
531
532 if (ret)
533 return ret;
534
535 *val = (int)rval;
536 return 0;
537}
538
539int tst_parse_long(const char *str, long *val, long min, long max)
540{
541 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200542 char *end;
543
544 if (!str)
545 return 0;
546
547 errno = 0;
548 rval = strtol(str, &end, 10);
549
550 if (str == end || *end != '\0')
551 return EINVAL;
552
553 if (errno)
554 return errno;
555
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300556 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200557 return ERANGE;
558
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300559 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200560 return 0;
561}
562
563int tst_parse_float(const char *str, float *val, float min, float max)
564{
565 double rval;
566 char *end;
567
568 if (!str)
569 return 0;
570
571 errno = 0;
572 rval = strtod(str, &end);
573
574 if (str == end || *end != '\0')
575 return EINVAL;
576
577 if (errno)
578 return errno;
579
580 if (rval > (double)max || rval < (double)min)
581 return ERANGE;
582
583 *val = (float)rval;
584 return 0;
585}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100586
Cyril Hrubisfa495172016-06-08 16:06:35 +0200587static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100588{
Xiao Yang11dfc322016-06-16 15:52:04 +0800589 if (results) {
590 printf("\nSummary:\n");
591 printf("passed %d\n", results->passed);
592 printf("failed %d\n", results->failed);
593 printf("skipped %d\n", results->skipped);
594 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100595
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100596 if (results->passed && ret == TCONF)
597 ret = 0;
598
Xiao Yang11dfc322016-06-16 15:52:04 +0800599 if (results->failed)
600 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100601
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200602 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800603 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100604
Xiao Yang11dfc322016-06-16 15:52:04 +0800605 if (results->warnings)
606 ret |= TWARN;
607 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100608
Jan Stancek332540e2016-06-08 16:48:22 +0200609 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100610
611 exit(ret);
612}
613
614void check_kver(void)
615{
616 int v1, v2, v3;
617
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100618 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
619 tst_res(TWARN,
620 "Invalid kernel version %s, expected %%d.%%d.%%d",
621 tst_test->min_kver);
622 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100623
624 if (tst_kvercmp(v1, v2, v3) < 0) {
625 tst_brk(TCONF, "The test requires kernel %s or newer",
626 tst_test->min_kver);
627 }
628}
629
630static int results_equal(struct results *a, struct results *b)
631{
632 if (a->passed != b->passed)
633 return 0;
634
635 if (a->failed != b->failed)
636 return 0;
637
638 if (a->skipped != b->skipped)
639 return 0;
640
641 return 1;
642}
643
644static int needs_tmpdir(void)
645{
646 return tst_test->needs_tmpdir ||
647 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200648 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100649 tst_test->resource_files ||
650 tst_test->needs_checkpoints;
651}
652
653static void copy_resources(void)
654{
655 unsigned int i;
656
657 for (i = 0; tst_test->resource_files[i]; i++)
658 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
659}
660
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800661static const char *get_tid(char *argv[])
662{
663 char *p;
664
665 if (!argv[0] || !argv[0][0]) {
666 tst_res(TINFO, "argv[0] is empty!");
667 return "ltp_empty_argv";
668 }
669
670 p = strrchr(argv[0], '/');
671 if (p)
672 return p+1;
673
674 return argv[0];
675}
676
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100677static struct tst_device tdev;
678struct tst_device *tst_device;
679
Cyril Hrubisc4596542017-06-20 15:42:18 +0200680static void assert_test_fn(void)
681{
682 int cnt = 0;
683
684 if (tst_test->test)
685 cnt++;
686
687 if (tst_test->test_all)
688 cnt++;
689
690 if (tst_test->sample)
691 cnt++;
692
693 if (!cnt)
694 tst_brk(TBROK, "No test function speficied");
695
696 if (cnt != 1)
697 tst_brk(TBROK, "You can define only one test function");
698
699 if (tst_test->test && !tst_test->tcnt)
Eric Biggers3614a162019-03-15 09:42:15 -0700700 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
Cyril Hrubisc4596542017-06-20 15:42:18 +0200701
702 if (!tst_test->test && tst_test->tcnt)
703 tst_brk(TBROK, "You can define tcnt only for test()");
704}
705
Cyril Hrubise6da8672018-04-05 16:01:48 +0200706static int prepare_and_mount_ro_fs(const char *dev,
707 const char *mntpoint,
708 const char *fs_type)
709{
710 char buf[PATH_MAX];
711
712 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
713 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
714 dev, mntpoint, fs_type);
715 return 1;
716 }
717
718 mntpoint_mounted = 1;
719
720 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
721 SAFE_MKDIR(buf, 0777);
722
723 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
724 SAFE_FILE_PRINTF(buf, "file content");
725 SAFE_CHMOD(buf, 0777);
726
727 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
728
729 return 0;
730}
731
Xiao Yang7313a872018-09-01 12:07:41 +0800732static void prepare_and_mount_dev_fs(const char *mntpoint)
733{
734 const char *flags[] = {"nodev", NULL};
735 int mounted_nodev;
736
737 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
738 if (mounted_nodev) {
739 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
740 "mounting tmpfs without nodev on %s", mntpoint);
741 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
742 mntpoint_mounted = 1;
743 }
744}
745
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200746static void prepare_device(void)
747{
748 if (tst_test->format_device) {
749 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200750 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200751 }
752
Cyril Hrubise6da8672018-04-05 16:01:48 +0200753 if (tst_test->needs_rofs) {
754 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
755 tdev.fs_type);
756 return;
757 }
758
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200759 if (tst_test->mount_device) {
760 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
761 tst_test->mnt_flags, tst_test->mnt_data);
762 mntpoint_mounted = 1;
763 }
764}
765
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100766static void do_setup(int argc, char *argv[])
767{
768 if (!tst_test)
769 tst_brk(TBROK, "No tests to run");
770
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200771 if (tst_test->tconf_msg)
772 tst_brk(TCONF, "%s", tst_test->tconf_msg);
773
Cyril Hrubisf023a612018-11-27 14:21:17 +0100774 if (tst_test->needs_kconfigs)
775 tst_kconfig_check(tst_test->needs_kconfigs);
776
Cyril Hrubisc4596542017-06-20 15:42:18 +0200777 assert_test_fn();
778
Li Wangb5d620a2017-11-01 13:15:23 +0800779 tid = get_tid(argv);
780
Cyril Hrubisc4596542017-06-20 15:42:18 +0200781 if (tst_test->sample)
782 tst_test = tst_timer_test_setup(tst_test);
783
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200784 parse_opts(argc, argv);
785
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100786 if (tst_test->needs_root && geteuid() != 0)
787 tst_brk(TCONF, "Test needs to be run as root");
788
789 if (tst_test->min_kver)
790 check_kver();
791
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300792 if (tst_test->needs_drivers) {
793 const char *name;
794 int i;
795
796 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
797 if (tst_check_driver(name))
798 tst_brk(TCONF, "%s driver not available", name);
799 }
800
Cyril Hrubis874326d2017-02-14 15:59:40 +0100801 if (tst_test->format_device)
802 tst_test->needs_device = 1;
803
804 if (tst_test->mount_device) {
805 tst_test->needs_device = 1;
806 tst_test->format_device = 1;
807 }
808
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200809 if (tst_test->all_filesystems)
810 tst_test->needs_device = 1;
811
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100812 setup_ipc();
813
Cyril Hrubisee761d82019-07-31 15:02:08 +0200814 if (tst_test->bufs)
815 tst_buffers_alloc(tst_test->bufs);
816
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000817 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100818 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100819
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100820 if (tst_test->save_restore) {
821 const char * const *name = tst_test->save_restore;
822
823 while (*name) {
824 tst_sys_conf_save(*name);
825 name++;
826 }
827 }
828
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700829 if (tst_test->mntpoint)
830 SAFE_MKDIR(tst_test->mntpoint, 0777);
831
Xiao Yang7313a872018-09-01 12:07:41 +0800832 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
833 tst_test->mount_device || tst_test->all_filesystems) &&
834 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700835 tst_brk(TBROK, "tst_test->mntpoint must be set!");
836 }
837
Xiao Yang7313a872018-09-01 12:07:41 +0800838 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
839 !!tst_test->needs_device > 1) {
840 tst_brk(TBROK,
841 "Two or more of needs_{rofs, devfs, device} are set");
842 }
843
844 if (tst_test->needs_devfs)
845 prepare_and_mount_dev_fs(tst_test->mntpoint);
846
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700847 if (tst_test->needs_rofs) {
848 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200849 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700850 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200851 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
852 tst_res(TINFO, "Can't mount tmpfs read-only, "
853 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700854 tst_test->needs_device = 1;
855 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700856 }
857 }
858
859 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100860 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100861
862 if (!tdev.dev)
863 tst_brk(TCONF, "Failed to acquire device");
864
865 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100866
867 if (tst_test->dev_fs_type)
868 tdev.fs_type = tst_test->dev_fs_type;
869 else
870 tdev.fs_type = tst_dev_fs_type();
871
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200872 if (!tst_test->all_filesystems)
873 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100874 }
875
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800876 if (tst_test->needs_overlay && !tst_test->mount_device) {
877 tst_brk(TBROK, "tst_test->mount_device must be set");
878 }
879 if (tst_test->needs_overlay && !mntpoint_mounted) {
880 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
881 }
882 if (tst_test->needs_overlay && !ovl_mounted) {
883 SAFE_MOUNT_OVERLAY();
884 ovl_mounted = 1;
885 }
886
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100887 if (tst_test->resource_files)
888 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200889
890 if (tst_test->restore_wallclock)
891 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200892}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100893
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200894static void do_test_setup(void)
895{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100896 main_pid = getpid();
897
898 if (tst_test->setup)
899 tst_test->setup();
900
901 if (main_pid != getpid())
902 tst_brk(TBROK, "Runaway child in setup()!");
903}
904
905static void do_cleanup(void)
906{
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800907 if (ovl_mounted)
908 SAFE_UMOUNT(OVL_MNT);
909
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700910 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100911 tst_umount(tst_test->mntpoint);
912
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100913 if (tst_test->needs_device && tdev.dev)
914 tst_release_device(tdev.dev);
915
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000916 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100917 /* avoid munmap() on wrong pointer in tst_rmdir() */
918 tst_futexes = NULL;
919 tst_rmdir();
920 }
Jan Stancek332540e2016-06-08 16:48:22 +0200921
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100922 if (tst_test->save_restore)
923 tst_sys_conf_restore(0);
924
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200925 if (tst_test->restore_wallclock)
926 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +0200927
928 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100929}
930
931static void run_tests(void)
932{
933 unsigned int i;
934 struct results saved_results;
935
936 if (!tst_test->test) {
937 saved_results = *results;
938 tst_test->test_all();
939
940 if (getpid() != main_pid) {
941 exit(0);
942 }
943
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300944 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100945
946 if (results_equal(&saved_results, results))
947 tst_brk(TBROK, "Test haven't reported results!");
948 return;
949 }
950
951 for (i = 0; i < tst_test->tcnt; i++) {
952 saved_results = *results;
953 tst_test->test(i);
954
955 if (getpid() != main_pid) {
956 exit(0);
957 }
958
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300959 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100960
961 if (results_equal(&saved_results, results))
962 tst_brk(TBROK, "Test %i haven't reported results!", i);
963 }
964}
965
966static unsigned long long get_time_ms(void)
967{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100968 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100969
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100970 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
971 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100972
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100973 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100974}
975
Jan Stancekb95b1992017-10-10 15:47:58 +0200976static void add_paths(void)
977{
978 char *old_path = getenv("PATH");
979 const char *start_dir;
980 char *new_path;
981
982 start_dir = tst_get_startwd();
983
984 if (old_path)
985 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
986 else
987 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
988
989 SAFE_SETENV("PATH", new_path, 1);
990 free(new_path);
991}
992
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200993static void heartbeat(void)
994{
Jan Stancek1893e012018-08-28 16:17:52 +0200995 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
996 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
997
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200998 kill(getppid(), SIGUSR1);
999}
1000
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001001static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001002{
1003 unsigned int i = 0;
1004 unsigned long long stop_time = 0;
1005 int cont = 1;
1006
Jan Stancek1893e012018-08-28 16:17:52 +02001007 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001008 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001009 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001010
1011 if (duration > 0)
1012 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1013
1014 for (;;) {
1015 cont = 0;
1016
1017 if (i < (unsigned int)iterations) {
1018 i++;
1019 cont = 1;
1020 }
1021
1022 if (stop_time && get_time_ms() < stop_time)
1023 cont = 1;
1024
1025 if (!cont)
1026 break;
1027
1028 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001029 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001030 }
1031
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001032 do_test_cleanup();
1033 exit(0);
1034}
1035
1036static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001037
Cyril Hrubis79163172017-05-18 10:57:07 +02001038
1039static volatile sig_atomic_t sigkill_retries;
1040
1041#define WRITE_MSG(msg) do { \
1042 if (write(2, msg, sizeof(msg) - 1)) { \
1043 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1044 } \
1045} while (0)
1046
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001047static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1048{
Cyril Hrubis79163172017-05-18 10:57:07 +02001049 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001050 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001051 alarm(5);
1052
1053 if (++sigkill_retries > 10) {
1054 WRITE_MSG("Cannot kill test processes!\n");
1055 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1056 WRITE_MSG("Exitting uncleanly...\n");
1057 _exit(TFAIL);
1058 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001059}
1060
1061static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1062{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001063 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001064 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001065}
1066
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001067static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1068{
1069 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001070 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001071 kill(-test_pid, SIGKILL);
1072 }
1073}
1074
Jan Stancek1893e012018-08-28 16:17:52 +02001075unsigned int tst_timeout_remaining(void)
1076{
1077 static struct timespec now;
1078 unsigned int elapsed;
1079
1080 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1081 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1082
1083 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1084 if (results->timeout > elapsed)
1085 return results->timeout - elapsed;
1086
1087 return 0;
1088}
1089
Li Wang94823cf2017-07-18 16:23:00 +08001090void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001091{
1092 char *mul = getenv("LTP_TIMEOUT_MUL");
1093
Li Wang94823cf2017-07-18 16:23:00 +08001094 if (timeout == -1) {
1095 tst_res(TINFO, "Timeout per run is disabled");
1096 return;
1097 }
1098
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001099 results->timeout = timeout;
1100
1101 if (mul) {
1102 float m = atof(mul);
1103
1104 if (m < 1)
1105 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1106
1107 results->timeout = results->timeout * m + 0.5;
1108 }
1109
1110 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1111 results->timeout/3600, (results->timeout%3600)/60,
1112 results->timeout % 60);
1113
1114 if (getpid() == lib_pid)
1115 alarm(results->timeout);
1116 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001117 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001118}
1119
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001120static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001121{
1122 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001123
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001124 if (tst_test->timeout)
1125 tst_set_timeout(tst_test->timeout);
1126 else
1127 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001128
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001129 SAFE_SIGNAL(SIGINT, sigint_handler);
1130
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001131 test_pid = fork();
1132 if (test_pid < 0)
1133 tst_brk(TBROK | TERRNO, "fork()");
1134
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001135 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001136 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1137 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1138 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001139 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001140 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001141 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001142
1143 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001144 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001145 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001146
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001147 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001148 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001149
1150 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1151 tst_res(TINFO, "If you are running on slow machine, "
1152 "try exporting LTP_TIMEOUT_MUL > 1");
1153 tst_brk(TBROK, "Test killed! (timeout?)");
1154 }
1155
1156 if (WIFSIGNALED(status))
1157 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1158
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001159 return 0;
1160}
1161
1162static int run_tcases_per_fs(void)
1163{
1164 int ret = 0;
1165 unsigned int i;
Cyril Hrubisdbe56e52019-06-19 14:20:19 +02001166 const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001167
1168 if (!filesystems[0])
1169 tst_brk(TCONF, "There are no supported filesystems");
1170
1171 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001172
1173 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001174 tdev.fs_type = filesystems[i];
1175
1176 prepare_device();
1177
1178 ret = fork_testrun();
1179
1180 if (mntpoint_mounted) {
1181 tst_umount(tst_test->mntpoint);
1182 mntpoint_mounted = 0;
1183 }
1184
1185 if (ret == TCONF) {
1186 update_results(ret);
1187 continue;
1188 }
1189
1190 if (ret == 0)
1191 continue;
1192
1193 do_exit(ret);
1194 }
1195
1196 return ret;
1197}
1198
Cyril Hrubisce082162019-03-06 16:24:29 +01001199unsigned int tst_variant;
1200
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001201void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1202{
Cyril Hrubisce082162019-03-06 16:24:29 +01001203 int ret = 0;
1204 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001205
1206 lib_pid = getpid();
1207 tst_test = self;
1208
1209 do_setup(argc, argv);
1210
1211 TCID = tid;
1212
1213 SAFE_SIGNAL(SIGALRM, alarm_handler);
1214 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1215
Cyril Hrubisce082162019-03-06 16:24:29 +01001216 if (tst_test->test_variants)
1217 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001218
Cyril Hrubisce082162019-03-06 16:24:29 +01001219 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1220 if (tst_test->all_filesystems)
1221 ret |= run_tcases_per_fs();
1222 else
1223 ret |= fork_testrun();
1224
1225 if (ret & ~(TCONF))
1226 goto exit;
1227 }
1228
1229exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001230 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001231}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001232
1233
1234void tst_flush(void)
1235{
1236 int rval;
1237
1238 rval = fflush(stderr);
1239 if (rval != 0)
1240 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1241
1242 rval = fflush(stderr);
1243 if (rval != 0)
1244 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1245
1246}