blob: 9a24cffc5011a1d659d33853a5aae0ebcb2a4194 [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
Cyril Hrubisca2c7692019-10-22 16:07:56 +020034#define LINUX_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id="
35#define CVE_DB_URL "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-"
36
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010037struct tst_test *tst_test;
38
Li Wangb5d620a2017-11-01 13:15:23 +080039static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010040static int iterations = 1;
41static float duration = -1;
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +020042static float timeout_mul = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020043static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070044static int mntpoint_mounted;
Murphy Zhoua88bbb42019-05-30 10:53:00 +080045static int ovl_mounted;
Jan Stancek1893e012018-08-28 16:17:52 +020046static struct timespec tst_start_time; /* valid only for test pid */
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010047
48struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020049 int passed;
50 int skipped;
51 int failed;
52 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020053 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010054};
55
56static struct results *results;
57
58static int ipc_fd;
59
60extern void *tst_futexes;
61extern unsigned int tst_max_futexes;
62
63#define IPC_ENV_VAR "LTP_IPC_PATH"
64
65static char ipc_path[1024];
66const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010067
68static char shm_path[1024];
69
Christian Lanig18b780d2018-07-23 15:24:50 +020070int TST_ERR;
71long TST_RET;
72
Jan Stancek332540e2016-06-08 16:48:22 +020073static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020074static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020075
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010076static void setup_ipc(void)
77{
78 size_t size = getpagesize();
79
Steven Jackson9f41dcf2016-12-21 20:09:01 +000080 if (access("/dev/shm", F_OK) == 0) {
81 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080082 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000083 } else {
84 char *tmpdir;
85
86 if (!tst_tmpdir_created())
87 tst_tmpdir();
88
89 tmpdir = tst_get_tmpdir();
90 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080091 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000092 free(tmpdir);
93 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010094
95 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
96 if (ipc_fd < 0)
97 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +020098 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010099
100 SAFE_FTRUNCATE(ipc_fd, size);
101
102 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
103
104 /* Checkpoints needs to be accessible from processes started by exec() */
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200105 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100106 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200107 putenv(ipc_path);
108 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100109 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200110 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100111
112 SAFE_CLOSE(ipc_fd);
113
114 if (tst_test->needs_checkpoints) {
115 tst_futexes = (char*)results + sizeof(struct results);
116 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
117 }
118}
119
120static void cleanup_ipc(void)
121{
122 size_t size = getpagesize();
123
124 if (ipc_fd > 0 && close(ipc_fd))
125 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
126
Cyril Hrubis9726b902017-07-27 11:28:50 +0200127 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100128 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
129
Cyril Hrubis9726b902017-07-27 11:28:50 +0200130 if (results) {
131 msync((void*)results, size, MS_SYNC);
132 munmap((void*)results, size);
Yann Sionneau511a0e12019-06-14 10:26:45 +0200133 results = NULL;
Cyril Hrubis9726b902017-07-27 11:28:50 +0200134 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100135}
136
137void tst_reinit(void)
138{
Jan Stancek03fc5372017-10-10 13:36:59 +0200139 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100140 size_t size = getpagesize();
141 int fd;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100142
143 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200144 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100145
146 if (access(path, F_OK))
147 tst_brk(TBROK, "File %s does not exist!", path);
148
149 fd = SAFE_OPEN(path, O_RDWR);
150
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200151 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
152 tst_futexes = (char*)results + sizeof(struct results);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100153 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
154
155 SAFE_CLOSE(fd);
156}
157
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100158static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100159{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100160 if (!results)
161 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100162
163 switch (ttype) {
164 case TCONF:
165 tst_atomic_inc(&results->skipped);
166 break;
167 case TPASS:
168 tst_atomic_inc(&results->passed);
169 break;
170 case TWARN:
171 tst_atomic_inc(&results->warnings);
172 break;
173 case TFAIL:
174 tst_atomic_inc(&results->failed);
175 break;
176 }
177}
178
179static void print_result(const char *file, const int lineno, int ttype,
180 const char *fmt, va_list va)
181{
182 char buf[1024];
183 char *str = buf;
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200184 int ret, size = sizeof(buf), ssize, int_errno;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100185 const char *str_errno = NULL;
186 const char *res;
187
188 switch (TTYPE_RESULT(ttype)) {
189 case TPASS:
190 res = "PASS";
191 break;
192 case TFAIL:
193 res = "FAIL";
194 break;
195 case TBROK:
196 res = "BROK";
197 break;
198 case TCONF:
199 res = "CONF";
200 break;
201 case TWARN:
202 res = "WARN";
203 break;
204 case TINFO:
205 res = "INFO";
206 break;
207 default:
208 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100209 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100210 }
211
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200212 if (ttype & TERRNO) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100213 str_errno = tst_strerrno(errno);
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200214 int_errno = errno;
215 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100216
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200217 if (ttype & TTERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200218 str_errno = tst_strerrno(TST_ERR);
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200219 int_errno = TST_ERR;
220 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100221
Richard Palethorpefb32e892018-03-13 16:24:47 +0100222 if (ttype & TRERRNO) {
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200223 int_errno = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
224 str_errno = tst_strerrno(int_errno);
Richard Palethorpefb32e892018-03-13 16:24:47 +0100225 }
226
Petr Vorela7f61332017-01-24 20:47:30 +0100227 ret = snprintf(str, size, "%s:%i: ", file, lineno);
228 str += ret;
229 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100230
Cyril Hrubis047c7272017-02-13 16:23:36 +0100231 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100232 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
233 res, ANSI_COLOR_RESET);
234 else
235 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100236 str += ret;
237 size -= ret;
238
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100239 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100240 ret = vsnprintf(str, size, fmt, va);
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:");
246 } else if (str_errno) {
247 ssize = size - 2;
Richard Palethorpebf28ca22019-08-19 15:36:18 +0200248 ret = snprintf(str, size, ": %s (%d)", str_errno, int_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100249 str += MIN(ret, ssize);
250 size -= MIN(ret, ssize);
251 if (ret >= ssize)
252 tst_res_(file, lineno, TWARN,
253 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100254 }
255
256 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100257
258 fputs(buf, stderr);
259}
260
261void tst_vres_(const char *file, const int lineno, int ttype,
262 const char *fmt, va_list va)
263{
264 print_result(file, lineno, ttype, fmt, va);
265
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100266 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100267}
268
269void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100270 const char *fmt, va_list va);
271
272static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
273 const char *fmt, va_list va) = tst_vbrk_;
274
275static void tst_cvres(const char *file, const int lineno, int ttype,
276 const char *fmt, va_list va)
277{
278 if (TTYPE_RESULT(ttype) == TBROK) {
279 ttype &= ~TTYPE_MASK;
280 ttype |= TWARN;
281 }
282
283 print_result(file, lineno, ttype, fmt, va);
284 update_results(TTYPE_RESULT(ttype));
285}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100286
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200287static void do_test_cleanup(void)
288{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100289 tst_brk_handler = tst_cvres;
290
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200291 if (tst_test->cleanup)
292 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100293
Cyril Hrubisee761d82019-07-31 15:02:08 +0200294 tst_free_all();
295
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100296 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200297}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100298
299void tst_vbrk_(const char *file, const int lineno, int ttype,
300 const char *fmt, va_list va)
301{
302 print_result(file, lineno, ttype, fmt, va);
303
Steve Mucklec20831d2017-09-20 13:23:06 -0700304 /*
305 * The getpid implementation in some C library versions may cause cloned
306 * test threads to show the same pid as their parent when CLONE_VM is
307 * specified but CLONE_THREAD is not. Use direct syscall to avoid
308 * cleanup running in the child.
309 */
310 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200311 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100312
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200313 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200314 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200315
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100316 exit(TTYPE_RESULT(ttype));
317}
318
319void tst_res_(const char *file, const int lineno, int ttype,
320 const char *fmt, ...)
321{
322 va_list va;
323
324 va_start(va, fmt);
325 tst_vres_(file, lineno, ttype, fmt, va);
326 va_end(va);
327}
328
329void tst_brk_(const char *file, const int lineno, int ttype,
330 const char *fmt, ...)
331{
332 va_list va;
333
334 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100335 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100336 va_end(va);
337}
338
339static void check_child_status(pid_t pid, int status)
340{
341 int ret;
342
343 if (WIFSIGNALED(status)) {
344 tst_brk(TBROK, "Child (%i) killed by signal %s",
345 pid, tst_strsig(WTERMSIG(status)));
346 }
347
348 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100349 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100350
351 ret = WEXITSTATUS(status);
352 switch (ret) {
353 case TPASS:
354 break;
355 case TBROK:
356 case TCONF:
357 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200358 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100359 default:
360 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
361 }
362}
363
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300364void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100365{
366 int status;
367 pid_t pid;
368
369 for (;;) {
370 pid = wait(&status);
371
372 if (pid > 0) {
373 check_child_status(pid, status);
374 continue;
375 }
376
377 if (errno == ECHILD)
378 break;
379
380 if (errno == EINTR)
381 continue;
382
383 tst_brk(TBROK | TERRNO, "wait() failed");
384 }
385}
386
387
388pid_t safe_fork(const char *filename, unsigned int lineno)
389{
390 pid_t pid;
391
392 if (!tst_test->forks_child)
393 tst_brk(TBROK, "test.forks_child must be set!");
394
Michael Moese9da42372018-03-09 15:16:09 +0100395 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100396
397 pid = fork();
398 if (pid < 0)
399 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
400
Cyril Hrubis13296b62019-07-31 17:39:37 +0200401 if (!pid)
402 atexit(tst_free_all);
403
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100404 return pid;
405}
406
407static struct option {
408 char *optstr;
409 char *help;
410} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200411 {"h", "-h Prints this help"},
412 {"i:", "-i n Execute test n times"},
413 {"I:", "-I x Execute test for n seconds"},
414 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100415};
416
417static void print_help(void)
418{
419 unsigned int i;
420
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200421 fprintf(stderr, "Options\n");
422 fprintf(stderr, "-------\n");
423
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100424 for (i = 0; i < ARRAY_SIZE(options); i++)
425 fprintf(stderr, "%s\n", options[i].help);
426
427 if (!tst_test->options)
428 return;
429
430 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200431 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100432}
433
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200434static void print_test_tags(void)
435{
436 unsigned int i;
437 const struct tst_tag *tags = tst_test->tags;
438
439 printf("\nTags\n");
440 printf("----\n");
441
442 if (tags) {
443 for (i = 0; tags[i].name; i++) {
444 if (!strcmp(tags[i].name, "CVE"))
445 printf(CVE_DB_URL "%s\n", tags[i].value);
446 else if (!strcmp(tags[i].name, "linux-git"))
447 printf(LINUX_GIT_URL "%s\n", tags[i].value);
448 else
449 printf("%s: %s\n", tags[i].name, tags[i].value);
450 }
451 }
452
453 printf("\n");
454}
455
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100456static void check_option_collision(void)
457{
458 unsigned int i, j;
459 struct tst_option *toptions = tst_test->options;
460
461 if (!toptions)
462 return;
463
464 for (i = 0; toptions[i].optstr; i++) {
465 for (j = 0; j < ARRAY_SIZE(options); j++) {
466 if (toptions[i].optstr[0] == options[j].optstr[0]) {
467 tst_brk(TBROK, "Option collision '%s'",
468 options[j].help);
469 }
470 }
471 }
472}
473
474static unsigned int count_options(void)
475{
476 unsigned int i;
477
478 if (!tst_test->options)
479 return 0;
480
481 for (i = 0; tst_test->options[i].optstr; i++);
482
483 return i;
484}
485
486static void parse_topt(unsigned int topts_len, int opt, char *optarg)
487{
488 unsigned int i;
489 struct tst_option *toptions = tst_test->options;
490
491 for (i = 0; i < topts_len; i++) {
492 if (toptions[i].optstr[0] == opt)
493 break;
494 }
495
496 if (i >= topts_len)
497 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
498
Cyril Hrubisf2926be2019-11-05 12:58:11 +0100499 if (*toptions[i].arg)
500 tst_res(TWARN, "Option -%c passed multiple times", opt);
501
Jan Stancekc07d36c2016-08-01 11:44:55 +0200502 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100503}
504
505/* see self_exec.c */
506#ifdef UCLINUX
507extern char *child_args;
508#endif
509
510static void parse_opts(int argc, char *argv[])
511{
512 unsigned int i, topts_len = count_options();
513 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
514 int opt;
515
516 check_option_collision();
517
518 optstr[0] = 0;
519
520 for (i = 0; i < ARRAY_SIZE(options); i++)
521 strcat(optstr, options[i].optstr);
522
523 for (i = 0; i < topts_len; i++)
524 strcat(optstr, tst_test->options[i].optstr);
525
526 while ((opt = getopt(argc, argv, optstr)) > 0) {
527 switch (opt) {
528 case '?':
529 print_help();
530 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200531 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100532 case 'h':
533 print_help();
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200534 print_test_tags();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100535 exit(0);
536 case 'i':
537 iterations = atoi(optarg);
538 break;
539 case 'I':
540 duration = atof(optarg);
541 break;
542 case 'C':
543#ifdef UCLINUX
544 child_args = optarg;
545#endif
546 break;
547 default:
548 parse_topt(topts_len, opt, optarg);
549 }
550 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200551
552 if (optind < argc)
553 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100554}
555
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200556int tst_parse_int(const char *str, int *val, int min, int max)
557{
558 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300559
560 if (!str)
561 return 0;
562
563 int ret = tst_parse_long(str, &rval, min, max);
564
565 if (ret)
566 return ret;
567
568 *val = (int)rval;
569 return 0;
570}
571
572int tst_parse_long(const char *str, long *val, long min, long max)
573{
574 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200575 char *end;
576
577 if (!str)
578 return 0;
579
580 errno = 0;
581 rval = strtol(str, &end, 10);
582
583 if (str == end || *end != '\0')
584 return EINVAL;
585
586 if (errno)
587 return errno;
588
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300589 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200590 return ERANGE;
591
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300592 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200593 return 0;
594}
595
596int tst_parse_float(const char *str, float *val, float min, float max)
597{
598 double rval;
599 char *end;
600
601 if (!str)
602 return 0;
603
604 errno = 0;
605 rval = strtod(str, &end);
606
607 if (str == end || *end != '\0')
608 return EINVAL;
609
610 if (errno)
611 return errno;
612
613 if (rval > (double)max || rval < (double)min)
614 return ERANGE;
615
616 *val = (float)rval;
617 return 0;
618}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100619
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200620static void print_colored(const char *str)
621{
622 if (tst_color_enabled(STDOUT_FILENO))
623 printf("%s%s%s", ANSI_COLOR_YELLOW, str, ANSI_COLOR_RESET);
624 else
625 printf("%s", str);
626}
627
628static void print_failure_hints(void)
629{
630 unsigned int i;
631 const struct tst_tag *tags = tst_test->tags;
632
633 if (!tags)
634 return;
635
636 int hint_printed = 0;
637 for (i = 0; tags[i].name; i++) {
638 if (!strcmp(tags[i].name, "linux-git")) {
639 if (!hint_printed) {
640 hint_printed = 1;
641 printf("\n");
642 print_colored("HINT: ");
643 printf("You _MAY_ be missing kernel fixes, see:\n\n");
644 }
645
646 printf(LINUX_GIT_URL "%s\n", tags[i].value);
647 }
648
649 }
650
651 hint_printed = 0;
652 for (i = 0; tags[i].name; i++) {
653 if (!strcmp(tags[i].name, "CVE")) {
654 if (!hint_printed) {
655 hint_printed = 1;
656 printf("\n");
657 print_colored("HINT: ");
658 printf("You _MAY_ be vunerable to CVE(s), see:\n\n");
659 }
660
661 printf(CVE_DB_URL "%s\n", tags[i].value);
662 }
663 }
664}
665
Cyril Hrubisfa495172016-06-08 16:06:35 +0200666static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100667{
Xiao Yang11dfc322016-06-16 15:52:04 +0800668 if (results) {
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100669 if (results->passed && ret == TCONF)
670 ret = 0;
671
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200672 if (results->failed) {
Xiao Yang11dfc322016-06-16 15:52:04 +0800673 ret |= TFAIL;
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200674 print_failure_hints();
675 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100676
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200677 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800678 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100679
Xiao Yang11dfc322016-06-16 15:52:04 +0800680 if (results->warnings)
681 ret |= TWARN;
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200682
683 printf("\nSummary:\n");
684 printf("passed %d\n", results->passed);
685 printf("failed %d\n", results->failed);
686 printf("skipped %d\n", results->skipped);
687 printf("warnings %d\n", results->warnings);
Xiao Yang11dfc322016-06-16 15:52:04 +0800688 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100689
Jan Stancek332540e2016-06-08 16:48:22 +0200690 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100691
692 exit(ret);
693}
694
695void check_kver(void)
696{
697 int v1, v2, v3;
698
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100699 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
700 tst_res(TWARN,
701 "Invalid kernel version %s, expected %%d.%%d.%%d",
702 tst_test->min_kver);
703 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100704
705 if (tst_kvercmp(v1, v2, v3) < 0) {
706 tst_brk(TCONF, "The test requires kernel %s or newer",
707 tst_test->min_kver);
708 }
709}
710
711static int results_equal(struct results *a, struct results *b)
712{
713 if (a->passed != b->passed)
714 return 0;
715
716 if (a->failed != b->failed)
717 return 0;
718
719 if (a->skipped != b->skipped)
720 return 0;
721
722 return 1;
723}
724
725static int needs_tmpdir(void)
726{
727 return tst_test->needs_tmpdir ||
728 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200729 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100730 tst_test->resource_files ||
731 tst_test->needs_checkpoints;
732}
733
734static void copy_resources(void)
735{
736 unsigned int i;
737
738 for (i = 0; tst_test->resource_files[i]; i++)
739 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
740}
741
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800742static const char *get_tid(char *argv[])
743{
744 char *p;
745
746 if (!argv[0] || !argv[0][0]) {
747 tst_res(TINFO, "argv[0] is empty!");
748 return "ltp_empty_argv";
749 }
750
751 p = strrchr(argv[0], '/');
752 if (p)
753 return p+1;
754
755 return argv[0];
756}
757
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100758static struct tst_device tdev;
759struct tst_device *tst_device;
760
Cyril Hrubisc4596542017-06-20 15:42:18 +0200761static void assert_test_fn(void)
762{
763 int cnt = 0;
764
765 if (tst_test->test)
766 cnt++;
767
768 if (tst_test->test_all)
769 cnt++;
770
771 if (tst_test->sample)
772 cnt++;
773
774 if (!cnt)
775 tst_brk(TBROK, "No test function speficied");
776
777 if (cnt != 1)
778 tst_brk(TBROK, "You can define only one test function");
779
780 if (tst_test->test && !tst_test->tcnt)
Eric Biggers3614a162019-03-15 09:42:15 -0700781 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
Cyril Hrubisc4596542017-06-20 15:42:18 +0200782
783 if (!tst_test->test && tst_test->tcnt)
784 tst_brk(TBROK, "You can define tcnt only for test()");
785}
786
Cyril Hrubise6da8672018-04-05 16:01:48 +0200787static int prepare_and_mount_ro_fs(const char *dev,
788 const char *mntpoint,
789 const char *fs_type)
790{
791 char buf[PATH_MAX];
792
793 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
794 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
795 dev, mntpoint, fs_type);
796 return 1;
797 }
798
799 mntpoint_mounted = 1;
800
801 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
802 SAFE_MKDIR(buf, 0777);
803
804 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
805 SAFE_FILE_PRINTF(buf, "file content");
806 SAFE_CHMOD(buf, 0777);
807
808 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
809
810 return 0;
811}
812
Xiao Yang7313a872018-09-01 12:07:41 +0800813static void prepare_and_mount_dev_fs(const char *mntpoint)
814{
815 const char *flags[] = {"nodev", NULL};
816 int mounted_nodev;
817
818 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
819 if (mounted_nodev) {
820 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
821 "mounting tmpfs without nodev on %s", mntpoint);
822 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
823 mntpoint_mounted = 1;
824 }
825}
826
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200827static void prepare_device(void)
828{
829 if (tst_test->format_device) {
830 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200831 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200832 }
833
Cyril Hrubise6da8672018-04-05 16:01:48 +0200834 if (tst_test->needs_rofs) {
835 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
836 tdev.fs_type);
837 return;
838 }
839
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200840 if (tst_test->mount_device) {
841 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
842 tst_test->mnt_flags, tst_test->mnt_data);
843 mntpoint_mounted = 1;
844 }
845}
846
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100847static void do_setup(int argc, char *argv[])
848{
849 if (!tst_test)
850 tst_brk(TBROK, "No tests to run");
851
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200852 if (tst_test->tconf_msg)
853 tst_brk(TCONF, "%s", tst_test->tconf_msg);
854
Cyril Hrubisf023a612018-11-27 14:21:17 +0100855 if (tst_test->needs_kconfigs)
856 tst_kconfig_check(tst_test->needs_kconfigs);
857
Cyril Hrubisc4596542017-06-20 15:42:18 +0200858 assert_test_fn();
859
Li Wangb5d620a2017-11-01 13:15:23 +0800860 tid = get_tid(argv);
861
Cyril Hrubisc4596542017-06-20 15:42:18 +0200862 if (tst_test->sample)
863 tst_test = tst_timer_test_setup(tst_test);
864
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200865 parse_opts(argc, argv);
866
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100867 if (tst_test->needs_root && geteuid() != 0)
868 tst_brk(TCONF, "Test needs to be run as root");
869
870 if (tst_test->min_kver)
871 check_kver();
872
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300873 if (tst_test->needs_drivers) {
874 const char *name;
875 int i;
876
877 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
878 if (tst_check_driver(name))
879 tst_brk(TCONF, "%s driver not available", name);
880 }
881
Cyril Hrubis874326d2017-02-14 15:59:40 +0100882 if (tst_test->format_device)
883 tst_test->needs_device = 1;
884
885 if (tst_test->mount_device) {
886 tst_test->needs_device = 1;
887 tst_test->format_device = 1;
888 }
889
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200890 if (tst_test->all_filesystems)
891 tst_test->needs_device = 1;
892
Li Wang9c4c3e92020-02-08 17:20:13 +0800893 if (tst_test->request_hugepages)
894 tst_request_hugepages(tst_test->request_hugepages);
895
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100896 setup_ipc();
897
Cyril Hrubisee761d82019-07-31 15:02:08 +0200898 if (tst_test->bufs)
899 tst_buffers_alloc(tst_test->bufs);
900
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000901 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100902 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100903
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100904 if (tst_test->save_restore) {
905 const char * const *name = tst_test->save_restore;
906
907 while (*name) {
908 tst_sys_conf_save(*name);
909 name++;
910 }
911 }
912
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700913 if (tst_test->mntpoint)
914 SAFE_MKDIR(tst_test->mntpoint, 0777);
915
Xiao Yang7313a872018-09-01 12:07:41 +0800916 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
917 tst_test->mount_device || tst_test->all_filesystems) &&
918 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700919 tst_brk(TBROK, "tst_test->mntpoint must be set!");
920 }
921
Xiao Yang7313a872018-09-01 12:07:41 +0800922 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
923 !!tst_test->needs_device > 1) {
924 tst_brk(TBROK,
925 "Two or more of needs_{rofs, devfs, device} are set");
926 }
927
928 if (tst_test->needs_devfs)
929 prepare_and_mount_dev_fs(tst_test->mntpoint);
930
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700931 if (tst_test->needs_rofs) {
932 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200933 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700934 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200935 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
936 tst_res(TINFO, "Can't mount tmpfs read-only, "
937 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700938 tst_test->needs_device = 1;
939 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700940 }
941 }
942
943 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100944 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100945
946 if (!tdev.dev)
947 tst_brk(TCONF, "Failed to acquire device");
948
949 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100950
951 if (tst_test->dev_fs_type)
952 tdev.fs_type = tst_test->dev_fs_type;
953 else
954 tdev.fs_type = tst_dev_fs_type();
955
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200956 if (!tst_test->all_filesystems)
957 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100958 }
959
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800960 if (tst_test->needs_overlay && !tst_test->mount_device) {
961 tst_brk(TBROK, "tst_test->mount_device must be set");
962 }
963 if (tst_test->needs_overlay && !mntpoint_mounted) {
964 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
965 }
966 if (tst_test->needs_overlay && !ovl_mounted) {
967 SAFE_MOUNT_OVERLAY();
968 ovl_mounted = 1;
969 }
970
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100971 if (tst_test->resource_files)
972 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200973
974 if (tst_test->restore_wallclock)
975 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200976}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100977
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200978static void do_test_setup(void)
979{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100980 main_pid = getpid();
981
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200982 if (tst_test->caps)
983 tst_cap_setup(tst_test->caps, TST_CAP_REQ);
984
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100985 if (tst_test->setup)
986 tst_test->setup();
987
988 if (main_pid != getpid())
989 tst_brk(TBROK, "Runaway child in setup()!");
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200990
991 if (tst_test->caps)
992 tst_cap_setup(tst_test->caps, TST_CAP_DROP);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100993}
994
995static void do_cleanup(void)
996{
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800997 if (ovl_mounted)
998 SAFE_UMOUNT(OVL_MNT);
999
Sandeep Patilc9a7def2017-09-19 12:49:58 -07001000 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +01001001 tst_umount(tst_test->mntpoint);
1002
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001003 if (tst_test->needs_device && tdev.dev)
1004 tst_release_device(tdev.dev);
1005
Steven Jackson9f41dcf2016-12-21 20:09:01 +00001006 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001007 /* avoid munmap() on wrong pointer in tst_rmdir() */
1008 tst_futexes = NULL;
1009 tst_rmdir();
1010 }
Jan Stancek332540e2016-06-08 16:48:22 +02001011
Li Wang9c4c3e92020-02-08 17:20:13 +08001012 tst_sys_conf_restore(0);
Jan Stancek9dcbc6d2018-11-05 09:00:02 +01001013
Rafael David Tinoco14e43562019-01-29 15:36:55 -02001014 if (tst_test->restore_wallclock)
1015 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +02001016
1017 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001018}
1019
1020static void run_tests(void)
1021{
1022 unsigned int i;
1023 struct results saved_results;
1024
1025 if (!tst_test->test) {
1026 saved_results = *results;
1027 tst_test->test_all();
1028
1029 if (getpid() != main_pid) {
1030 exit(0);
1031 }
1032
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +03001033 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001034
1035 if (results_equal(&saved_results, results))
1036 tst_brk(TBROK, "Test haven't reported results!");
1037 return;
1038 }
1039
1040 for (i = 0; i < tst_test->tcnt; i++) {
1041 saved_results = *results;
1042 tst_test->test(i);
1043
1044 if (getpid() != main_pid) {
1045 exit(0);
1046 }
1047
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +03001048 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001049
1050 if (results_equal(&saved_results, results))
1051 tst_brk(TBROK, "Test %i haven't reported results!", i);
1052 }
1053}
1054
1055static unsigned long long get_time_ms(void)
1056{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001057 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001058
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001059 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
1060 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001061
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001062 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001063}
1064
Jan Stancekb95b1992017-10-10 15:47:58 +02001065static void add_paths(void)
1066{
1067 char *old_path = getenv("PATH");
1068 const char *start_dir;
1069 char *new_path;
1070
1071 start_dir = tst_get_startwd();
1072
1073 if (old_path)
1074 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
1075 else
1076 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
1077
1078 SAFE_SETENV("PATH", new_path, 1);
1079 free(new_path);
1080}
1081
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001082static void heartbeat(void)
1083{
Jan Stancek1893e012018-08-28 16:17:52 +02001084 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
1085 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1086
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001087 kill(getppid(), SIGUSR1);
1088}
1089
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001090static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001091{
1092 unsigned int i = 0;
1093 unsigned long long stop_time = 0;
1094 int cont = 1;
1095
Jan Stancek1893e012018-08-28 16:17:52 +02001096 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001097 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001098 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001099
1100 if (duration > 0)
1101 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1102
1103 for (;;) {
1104 cont = 0;
1105
1106 if (i < (unsigned int)iterations) {
1107 i++;
1108 cont = 1;
1109 }
1110
1111 if (stop_time && get_time_ms() < stop_time)
1112 cont = 1;
1113
1114 if (!cont)
1115 break;
1116
1117 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001118 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001119 }
1120
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001121 do_test_cleanup();
1122 exit(0);
1123}
1124
1125static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001126
Cyril Hrubis79163172017-05-18 10:57:07 +02001127
1128static volatile sig_atomic_t sigkill_retries;
1129
1130#define WRITE_MSG(msg) do { \
1131 if (write(2, msg, sizeof(msg) - 1)) { \
1132 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1133 } \
1134} while (0)
1135
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001136static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1137{
Cyril Hrubis79163172017-05-18 10:57:07 +02001138 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001139 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001140 alarm(5);
1141
1142 if (++sigkill_retries > 10) {
1143 WRITE_MSG("Cannot kill test processes!\n");
1144 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1145 WRITE_MSG("Exitting uncleanly...\n");
1146 _exit(TFAIL);
1147 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001148}
1149
1150static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1151{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001152 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001153 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001154}
1155
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001156static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1157{
1158 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001159 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001160 kill(-test_pid, SIGKILL);
1161 }
1162}
1163
Jan Stancek1893e012018-08-28 16:17:52 +02001164unsigned int tst_timeout_remaining(void)
1165{
1166 static struct timespec now;
1167 unsigned int elapsed;
1168
1169 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1170 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1171
1172 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1173 if (results->timeout > elapsed)
1174 return results->timeout - elapsed;
1175
1176 return 0;
1177}
1178
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001179unsigned int tst_multiply_timeout(unsigned int timeout)
1180{
1181 char *mul;
1182 int ret;
1183
1184 if (timeout_mul == -1) {
1185 mul = getenv("LTP_TIMEOUT_MUL");
1186 if (mul) {
1187 if ((ret = tst_parse_float(mul, &timeout_mul, 1, 10000))) {
1188 tst_brk(TBROK, "Failed to parse LTP_TIMEOUT_MUL: %s",
1189 tst_strerrno(ret));
1190 }
1191 } else {
1192 timeout_mul = 1;
1193 }
1194 }
1195 if (timeout_mul < 1)
1196 tst_brk(TBROK, "LTP_TIMEOUT_MUL must to be int >= 1! (%.2f)",
1197 timeout_mul);
1198
1199 if (timeout < 1)
1200 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
1201
1202 return timeout * timeout_mul;
1203}
1204
Li Wang94823cf2017-07-18 16:23:00 +08001205void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001206{
Li Wang94823cf2017-07-18 16:23:00 +08001207 if (timeout == -1) {
1208 tst_res(TINFO, "Timeout per run is disabled");
1209 return;
1210 }
1211
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001212 if (timeout < 1)
1213 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001214
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001215 results->timeout = tst_multiply_timeout(timeout);
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001216
1217 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1218 results->timeout/3600, (results->timeout%3600)/60,
1219 results->timeout % 60);
1220
1221 if (getpid() == lib_pid)
1222 alarm(results->timeout);
1223 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001224 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001225}
1226
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001227static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001228{
1229 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001230
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001231 if (tst_test->timeout)
1232 tst_set_timeout(tst_test->timeout);
1233 else
1234 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001235
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001236 SAFE_SIGNAL(SIGINT, sigint_handler);
1237
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001238 test_pid = fork();
1239 if (test_pid < 0)
1240 tst_brk(TBROK | TERRNO, "fork()");
1241
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001242 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001243 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1244 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1245 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001246 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001247 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001248 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001249
1250 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001251 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001252 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001253
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001254 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001255 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001256
1257 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1258 tst_res(TINFO, "If you are running on slow machine, "
1259 "try exporting LTP_TIMEOUT_MUL > 1");
1260 tst_brk(TBROK, "Test killed! (timeout?)");
1261 }
1262
1263 if (WIFSIGNALED(status))
1264 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1265
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001266 return 0;
1267}
1268
1269static int run_tcases_per_fs(void)
1270{
1271 int ret = 0;
1272 unsigned int i;
Cyril Hrubisdbe56e52019-06-19 14:20:19 +02001273 const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001274
1275 if (!filesystems[0])
1276 tst_brk(TCONF, "There are no supported filesystems");
1277
1278 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001279
1280 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001281 tdev.fs_type = filesystems[i];
1282
1283 prepare_device();
1284
1285 ret = fork_testrun();
1286
1287 if (mntpoint_mounted) {
1288 tst_umount(tst_test->mntpoint);
1289 mntpoint_mounted = 0;
1290 }
1291
1292 if (ret == TCONF) {
1293 update_results(ret);
1294 continue;
1295 }
1296
1297 if (ret == 0)
1298 continue;
1299
1300 do_exit(ret);
1301 }
1302
1303 return ret;
1304}
1305
Cyril Hrubisce082162019-03-06 16:24:29 +01001306unsigned int tst_variant;
1307
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001308void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1309{
Cyril Hrubisce082162019-03-06 16:24:29 +01001310 int ret = 0;
1311 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001312
1313 lib_pid = getpid();
1314 tst_test = self;
1315
1316 do_setup(argc, argv);
1317
1318 TCID = tid;
1319
1320 SAFE_SIGNAL(SIGALRM, alarm_handler);
1321 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1322
Cyril Hrubisce082162019-03-06 16:24:29 +01001323 if (tst_test->test_variants)
1324 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001325
Cyril Hrubisce082162019-03-06 16:24:29 +01001326 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1327 if (tst_test->all_filesystems)
1328 ret |= run_tcases_per_fs();
1329 else
1330 ret |= fork_testrun();
1331
1332 if (ret & ~(TCONF))
1333 goto exit;
1334 }
1335
1336exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001337 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001338}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001339
1340
1341void tst_flush(void)
1342{
1343 int rval;
1344
1345 rval = fflush(stderr);
1346 if (rval != 0)
1347 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1348
1349 rval = fflush(stderr);
1350 if (rval != 0)
1351 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1352
1353}