blob: 8d7dee2cc0993230e8efbf86f62d25ef69a937b4 [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
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100893 setup_ipc();
894
Cyril Hrubisee761d82019-07-31 15:02:08 +0200895 if (tst_test->bufs)
896 tst_buffers_alloc(tst_test->bufs);
897
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000898 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100899 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100900
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100901 if (tst_test->save_restore) {
902 const char * const *name = tst_test->save_restore;
903
904 while (*name) {
905 tst_sys_conf_save(*name);
906 name++;
907 }
908 }
909
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700910 if (tst_test->mntpoint)
911 SAFE_MKDIR(tst_test->mntpoint, 0777);
912
Xiao Yang7313a872018-09-01 12:07:41 +0800913 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
914 tst_test->mount_device || tst_test->all_filesystems) &&
915 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700916 tst_brk(TBROK, "tst_test->mntpoint must be set!");
917 }
918
Xiao Yang7313a872018-09-01 12:07:41 +0800919 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
920 !!tst_test->needs_device > 1) {
921 tst_brk(TBROK,
922 "Two or more of needs_{rofs, devfs, device} are set");
923 }
924
925 if (tst_test->needs_devfs)
926 prepare_and_mount_dev_fs(tst_test->mntpoint);
927
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700928 if (tst_test->needs_rofs) {
929 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200930 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700931 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200932 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
933 tst_res(TINFO, "Can't mount tmpfs read-only, "
934 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700935 tst_test->needs_device = 1;
936 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700937 }
938 }
939
940 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100941 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100942
943 if (!tdev.dev)
944 tst_brk(TCONF, "Failed to acquire device");
945
946 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100947
948 if (tst_test->dev_fs_type)
949 tdev.fs_type = tst_test->dev_fs_type;
950 else
951 tdev.fs_type = tst_dev_fs_type();
952
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200953 if (!tst_test->all_filesystems)
954 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100955 }
956
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800957 if (tst_test->needs_overlay && !tst_test->mount_device) {
958 tst_brk(TBROK, "tst_test->mount_device must be set");
959 }
960 if (tst_test->needs_overlay && !mntpoint_mounted) {
961 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
962 }
963 if (tst_test->needs_overlay && !ovl_mounted) {
964 SAFE_MOUNT_OVERLAY();
965 ovl_mounted = 1;
966 }
967
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100968 if (tst_test->resource_files)
969 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200970
971 if (tst_test->restore_wallclock)
972 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200973}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100974
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200975static void do_test_setup(void)
976{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100977 main_pid = getpid();
978
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200979 if (tst_test->caps)
980 tst_cap_setup(tst_test->caps, TST_CAP_REQ);
981
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100982 if (tst_test->setup)
983 tst_test->setup();
984
985 if (main_pid != getpid())
986 tst_brk(TBROK, "Runaway child in setup()!");
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200987
988 if (tst_test->caps)
989 tst_cap_setup(tst_test->caps, TST_CAP_DROP);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100990}
991
992static void do_cleanup(void)
993{
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800994 if (ovl_mounted)
995 SAFE_UMOUNT(OVL_MNT);
996
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700997 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100998 tst_umount(tst_test->mntpoint);
999
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001000 if (tst_test->needs_device && tdev.dev)
1001 tst_release_device(tdev.dev);
1002
Steven Jackson9f41dcf2016-12-21 20:09:01 +00001003 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001004 /* avoid munmap() on wrong pointer in tst_rmdir() */
1005 tst_futexes = NULL;
1006 tst_rmdir();
1007 }
Jan Stancek332540e2016-06-08 16:48:22 +02001008
Jan Stancek9dcbc6d2018-11-05 09:00:02 +01001009 if (tst_test->save_restore)
1010 tst_sys_conf_restore(0);
1011
Rafael David Tinoco14e43562019-01-29 15:36:55 -02001012 if (tst_test->restore_wallclock)
1013 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +02001014
1015 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001016}
1017
1018static void run_tests(void)
1019{
1020 unsigned int i;
1021 struct results saved_results;
1022
1023 if (!tst_test->test) {
1024 saved_results = *results;
1025 tst_test->test_all();
1026
1027 if (getpid() != main_pid) {
1028 exit(0);
1029 }
1030
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +03001031 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001032
1033 if (results_equal(&saved_results, results))
1034 tst_brk(TBROK, "Test haven't reported results!");
1035 return;
1036 }
1037
1038 for (i = 0; i < tst_test->tcnt; i++) {
1039 saved_results = *results;
1040 tst_test->test(i);
1041
1042 if (getpid() != main_pid) {
1043 exit(0);
1044 }
1045
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +03001046 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001047
1048 if (results_equal(&saved_results, results))
1049 tst_brk(TBROK, "Test %i haven't reported results!", i);
1050 }
1051}
1052
1053static unsigned long long get_time_ms(void)
1054{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001055 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001056
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001057 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
1058 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001059
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001060 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001061}
1062
Jan Stancekb95b1992017-10-10 15:47:58 +02001063static void add_paths(void)
1064{
1065 char *old_path = getenv("PATH");
1066 const char *start_dir;
1067 char *new_path;
1068
1069 start_dir = tst_get_startwd();
1070
1071 if (old_path)
1072 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
1073 else
1074 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
1075
1076 SAFE_SETENV("PATH", new_path, 1);
1077 free(new_path);
1078}
1079
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001080static void heartbeat(void)
1081{
Jan Stancek1893e012018-08-28 16:17:52 +02001082 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
1083 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1084
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001085 kill(getppid(), SIGUSR1);
1086}
1087
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001088static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001089{
1090 unsigned int i = 0;
1091 unsigned long long stop_time = 0;
1092 int cont = 1;
1093
Jan Stancek1893e012018-08-28 16:17:52 +02001094 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001095 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001096 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001097
1098 if (duration > 0)
1099 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1100
1101 for (;;) {
1102 cont = 0;
1103
1104 if (i < (unsigned int)iterations) {
1105 i++;
1106 cont = 1;
1107 }
1108
1109 if (stop_time && get_time_ms() < stop_time)
1110 cont = 1;
1111
1112 if (!cont)
1113 break;
1114
1115 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001116 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001117 }
1118
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001119 do_test_cleanup();
1120 exit(0);
1121}
1122
1123static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001124
Cyril Hrubis79163172017-05-18 10:57:07 +02001125
1126static volatile sig_atomic_t sigkill_retries;
1127
1128#define WRITE_MSG(msg) do { \
1129 if (write(2, msg, sizeof(msg) - 1)) { \
1130 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1131 } \
1132} while (0)
1133
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001134static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1135{
Cyril Hrubis79163172017-05-18 10:57:07 +02001136 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001137 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001138 alarm(5);
1139
1140 if (++sigkill_retries > 10) {
1141 WRITE_MSG("Cannot kill test processes!\n");
1142 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1143 WRITE_MSG("Exitting uncleanly...\n");
1144 _exit(TFAIL);
1145 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001146}
1147
1148static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1149{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001150 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001151 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001152}
1153
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001154static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1155{
1156 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001157 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001158 kill(-test_pid, SIGKILL);
1159 }
1160}
1161
Jan Stancek1893e012018-08-28 16:17:52 +02001162unsigned int tst_timeout_remaining(void)
1163{
1164 static struct timespec now;
1165 unsigned int elapsed;
1166
1167 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1168 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1169
1170 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1171 if (results->timeout > elapsed)
1172 return results->timeout - elapsed;
1173
1174 return 0;
1175}
1176
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001177unsigned int tst_multiply_timeout(unsigned int timeout)
1178{
1179 char *mul;
1180 int ret;
1181
1182 if (timeout_mul == -1) {
1183 mul = getenv("LTP_TIMEOUT_MUL");
1184 if (mul) {
1185 if ((ret = tst_parse_float(mul, &timeout_mul, 1, 10000))) {
1186 tst_brk(TBROK, "Failed to parse LTP_TIMEOUT_MUL: %s",
1187 tst_strerrno(ret));
1188 }
1189 } else {
1190 timeout_mul = 1;
1191 }
1192 }
1193 if (timeout_mul < 1)
1194 tst_brk(TBROK, "LTP_TIMEOUT_MUL must to be int >= 1! (%.2f)",
1195 timeout_mul);
1196
1197 if (timeout < 1)
1198 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
1199
1200 return timeout * timeout_mul;
1201}
1202
Li Wang94823cf2017-07-18 16:23:00 +08001203void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001204{
Li Wang94823cf2017-07-18 16:23:00 +08001205 if (timeout == -1) {
1206 tst_res(TINFO, "Timeout per run is disabled");
1207 return;
1208 }
1209
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001210 if (timeout < 1)
1211 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001212
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001213 results->timeout = tst_multiply_timeout(timeout);
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001214
1215 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1216 results->timeout/3600, (results->timeout%3600)/60,
1217 results->timeout % 60);
1218
1219 if (getpid() == lib_pid)
1220 alarm(results->timeout);
1221 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001222 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001223}
1224
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001225static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001226{
1227 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001228
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001229 if (tst_test->timeout)
1230 tst_set_timeout(tst_test->timeout);
1231 else
1232 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001233
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001234 SAFE_SIGNAL(SIGINT, sigint_handler);
1235
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001236 test_pid = fork();
1237 if (test_pid < 0)
1238 tst_brk(TBROK | TERRNO, "fork()");
1239
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001240 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001241 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1242 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1243 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001244 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001245 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001246 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001247
1248 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001249 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001250 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001251
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001252 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001253 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001254
1255 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1256 tst_res(TINFO, "If you are running on slow machine, "
1257 "try exporting LTP_TIMEOUT_MUL > 1");
1258 tst_brk(TBROK, "Test killed! (timeout?)");
1259 }
1260
1261 if (WIFSIGNALED(status))
1262 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1263
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001264 return 0;
1265}
1266
1267static int run_tcases_per_fs(void)
1268{
1269 int ret = 0;
1270 unsigned int i;
Cyril Hrubisdbe56e52019-06-19 14:20:19 +02001271 const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001272
1273 if (!filesystems[0])
1274 tst_brk(TCONF, "There are no supported filesystems");
1275
1276 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001277
1278 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001279 tdev.fs_type = filesystems[i];
1280
1281 prepare_device();
1282
1283 ret = fork_testrun();
1284
1285 if (mntpoint_mounted) {
1286 tst_umount(tst_test->mntpoint);
1287 mntpoint_mounted = 0;
1288 }
1289
1290 if (ret == TCONF) {
1291 update_results(ret);
1292 continue;
1293 }
1294
1295 if (ret == 0)
1296 continue;
1297
1298 do_exit(ret);
1299 }
1300
1301 return ret;
1302}
1303
Cyril Hrubisce082162019-03-06 16:24:29 +01001304unsigned int tst_variant;
1305
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001306void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1307{
Cyril Hrubisce082162019-03-06 16:24:29 +01001308 int ret = 0;
1309 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001310
1311 lib_pid = getpid();
1312 tst_test = self;
1313
1314 do_setup(argc, argv);
1315
1316 TCID = tid;
1317
1318 SAFE_SIGNAL(SIGALRM, alarm_handler);
1319 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1320
Cyril Hrubisce082162019-03-06 16:24:29 +01001321 if (tst_test->test_variants)
1322 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001323
Cyril Hrubisce082162019-03-06 16:24:29 +01001324 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1325 if (tst_test->all_filesystems)
1326 ret |= run_tcases_per_fs();
1327 else
1328 ret |= fork_testrun();
1329
1330 if (ret & ~(TCONF))
1331 goto exit;
1332 }
1333
1334exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001335 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001336}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001337
1338
1339void tst_flush(void)
1340{
1341 int rval;
1342
1343 rval = fflush(stderr);
1344 if (rval != 0)
1345 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1346
1347 rval = fflush(stderr);
1348 if (rval != 0)
1349 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1350
1351}