blob: 6239acf89cd7a257f822317db392d777885e4641 [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
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200898 if (tst_test->caps)
899 tst_cap_setup(tst_test->caps, TST_CAP_REQ);
900
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100901 if (tst_test->setup)
902 tst_test->setup();
903
904 if (main_pid != getpid())
905 tst_brk(TBROK, "Runaway child in setup()!");
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200906
907 if (tst_test->caps)
908 tst_cap_setup(tst_test->caps, TST_CAP_DROP);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100909}
910
911static void do_cleanup(void)
912{
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800913 if (ovl_mounted)
914 SAFE_UMOUNT(OVL_MNT);
915
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700916 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100917 tst_umount(tst_test->mntpoint);
918
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100919 if (tst_test->needs_device && tdev.dev)
920 tst_release_device(tdev.dev);
921
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000922 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100923 /* avoid munmap() on wrong pointer in tst_rmdir() */
924 tst_futexes = NULL;
925 tst_rmdir();
926 }
Jan Stancek332540e2016-06-08 16:48:22 +0200927
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100928 if (tst_test->save_restore)
929 tst_sys_conf_restore(0);
930
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200931 if (tst_test->restore_wallclock)
932 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +0200933
934 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100935}
936
937static void run_tests(void)
938{
939 unsigned int i;
940 struct results saved_results;
941
942 if (!tst_test->test) {
943 saved_results = *results;
944 tst_test->test_all();
945
946 if (getpid() != main_pid) {
947 exit(0);
948 }
949
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300950 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100951
952 if (results_equal(&saved_results, results))
953 tst_brk(TBROK, "Test haven't reported results!");
954 return;
955 }
956
957 for (i = 0; i < tst_test->tcnt; i++) {
958 saved_results = *results;
959 tst_test->test(i);
960
961 if (getpid() != main_pid) {
962 exit(0);
963 }
964
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300965 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100966
967 if (results_equal(&saved_results, results))
968 tst_brk(TBROK, "Test %i haven't reported results!", i);
969 }
970}
971
972static unsigned long long get_time_ms(void)
973{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100974 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100975
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100976 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
977 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100978
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100979 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100980}
981
Jan Stancekb95b1992017-10-10 15:47:58 +0200982static void add_paths(void)
983{
984 char *old_path = getenv("PATH");
985 const char *start_dir;
986 char *new_path;
987
988 start_dir = tst_get_startwd();
989
990 if (old_path)
991 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
992 else
993 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
994
995 SAFE_SETENV("PATH", new_path, 1);
996 free(new_path);
997}
998
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200999static void heartbeat(void)
1000{
Jan Stancek1893e012018-08-28 16:17:52 +02001001 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
1002 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1003
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001004 kill(getppid(), SIGUSR1);
1005}
1006
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001007static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001008{
1009 unsigned int i = 0;
1010 unsigned long long stop_time = 0;
1011 int cont = 1;
1012
Jan Stancek1893e012018-08-28 16:17:52 +02001013 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001014 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001015 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001016
1017 if (duration > 0)
1018 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1019
1020 for (;;) {
1021 cont = 0;
1022
1023 if (i < (unsigned int)iterations) {
1024 i++;
1025 cont = 1;
1026 }
1027
1028 if (stop_time && get_time_ms() < stop_time)
1029 cont = 1;
1030
1031 if (!cont)
1032 break;
1033
1034 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001035 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001036 }
1037
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001038 do_test_cleanup();
1039 exit(0);
1040}
1041
1042static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001043
Cyril Hrubis79163172017-05-18 10:57:07 +02001044
1045static volatile sig_atomic_t sigkill_retries;
1046
1047#define WRITE_MSG(msg) do { \
1048 if (write(2, msg, sizeof(msg) - 1)) { \
1049 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1050 } \
1051} while (0)
1052
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001053static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1054{
Cyril Hrubis79163172017-05-18 10:57:07 +02001055 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001056 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001057 alarm(5);
1058
1059 if (++sigkill_retries > 10) {
1060 WRITE_MSG("Cannot kill test processes!\n");
1061 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1062 WRITE_MSG("Exitting uncleanly...\n");
1063 _exit(TFAIL);
1064 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001065}
1066
1067static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1068{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001069 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001070 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001071}
1072
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001073static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1074{
1075 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001076 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001077 kill(-test_pid, SIGKILL);
1078 }
1079}
1080
Jan Stancek1893e012018-08-28 16:17:52 +02001081unsigned int tst_timeout_remaining(void)
1082{
1083 static struct timespec now;
1084 unsigned int elapsed;
1085
1086 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1087 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1088
1089 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1090 if (results->timeout > elapsed)
1091 return results->timeout - elapsed;
1092
1093 return 0;
1094}
1095
Li Wang94823cf2017-07-18 16:23:00 +08001096void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001097{
1098 char *mul = getenv("LTP_TIMEOUT_MUL");
1099
Li Wang94823cf2017-07-18 16:23:00 +08001100 if (timeout == -1) {
1101 tst_res(TINFO, "Timeout per run is disabled");
1102 return;
1103 }
1104
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001105 results->timeout = timeout;
1106
1107 if (mul) {
1108 float m = atof(mul);
1109
1110 if (m < 1)
1111 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1112
1113 results->timeout = results->timeout * m + 0.5;
1114 }
1115
1116 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1117 results->timeout/3600, (results->timeout%3600)/60,
1118 results->timeout % 60);
1119
1120 if (getpid() == lib_pid)
1121 alarm(results->timeout);
1122 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001123 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001124}
1125
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001126static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001127{
1128 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001129
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001130 if (tst_test->timeout)
1131 tst_set_timeout(tst_test->timeout);
1132 else
1133 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001134
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001135 SAFE_SIGNAL(SIGINT, sigint_handler);
1136
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001137 test_pid = fork();
1138 if (test_pid < 0)
1139 tst_brk(TBROK | TERRNO, "fork()");
1140
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001141 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001142 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1143 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1144 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001145 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001146 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001147 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001148
1149 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001150 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001151 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001152
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001153 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001154 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001155
1156 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1157 tst_res(TINFO, "If you are running on slow machine, "
1158 "try exporting LTP_TIMEOUT_MUL > 1");
1159 tst_brk(TBROK, "Test killed! (timeout?)");
1160 }
1161
1162 if (WIFSIGNALED(status))
1163 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1164
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001165 return 0;
1166}
1167
1168static int run_tcases_per_fs(void)
1169{
1170 int ret = 0;
1171 unsigned int i;
Cyril Hrubisdbe56e52019-06-19 14:20:19 +02001172 const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001173
1174 if (!filesystems[0])
1175 tst_brk(TCONF, "There are no supported filesystems");
1176
1177 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001178
1179 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001180 tdev.fs_type = filesystems[i];
1181
1182 prepare_device();
1183
1184 ret = fork_testrun();
1185
1186 if (mntpoint_mounted) {
1187 tst_umount(tst_test->mntpoint);
1188 mntpoint_mounted = 0;
1189 }
1190
1191 if (ret == TCONF) {
1192 update_results(ret);
1193 continue;
1194 }
1195
1196 if (ret == 0)
1197 continue;
1198
1199 do_exit(ret);
1200 }
1201
1202 return ret;
1203}
1204
Cyril Hrubisce082162019-03-06 16:24:29 +01001205unsigned int tst_variant;
1206
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001207void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1208{
Cyril Hrubisce082162019-03-06 16:24:29 +01001209 int ret = 0;
1210 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001211
1212 lib_pid = getpid();
1213 tst_test = self;
1214
1215 do_setup(argc, argv);
1216
1217 TCID = tid;
1218
1219 SAFE_SIGNAL(SIGALRM, alarm_handler);
1220 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1221
Cyril Hrubisce082162019-03-06 16:24:29 +01001222 if (tst_test->test_variants)
1223 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001224
Cyril Hrubisce082162019-03-06 16:24:29 +01001225 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1226 if (tst_test->all_filesystems)
1227 ret |= run_tcases_per_fs();
1228 else
1229 ret |= fork_testrun();
1230
1231 if (ret & ~(TCONF))
1232 goto exit;
1233 }
1234
1235exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001236 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001237}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001238
1239
1240void tst_flush(void)
1241{
1242 int rval;
1243
1244 rval = fflush(stderr);
1245 if (rval != 0)
1246 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1247
1248 rval = fflush(stderr);
1249 if (rval != 0)
1250 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1251
1252}