blob: 220d7fdfc548ce85d3e4e3493310e4b528d7e791 [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;
Jan Stancekf274eb42020-02-21 16:50:13 +0100184 int ret, size = sizeof(buf), ssize, int_errno, buflen;
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
Jan Stancekf274eb42020-02-21 16:50:13 +0100258 /* we might be called from signal handler, so use write() */
259 buflen = str - buf + 1;
260 str = buf;
261 while (buflen) {
262 ret = write(STDERR_FILENO, str, buflen);
263 if (ret <= 0)
264 break;
265
266 str += ret;
267 buflen -= ret;
268 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100269}
270
271void tst_vres_(const char *file, const int lineno, int ttype,
272 const char *fmt, va_list va)
273{
274 print_result(file, lineno, ttype, fmt, va);
275
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100276 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100277}
278
279void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100280 const char *fmt, va_list va);
281
282static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
283 const char *fmt, va_list va) = tst_vbrk_;
284
285static void tst_cvres(const char *file, const int lineno, int ttype,
286 const char *fmt, va_list va)
287{
288 if (TTYPE_RESULT(ttype) == TBROK) {
289 ttype &= ~TTYPE_MASK;
290 ttype |= TWARN;
291 }
292
293 print_result(file, lineno, ttype, fmt, va);
294 update_results(TTYPE_RESULT(ttype));
295}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100296
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200297static void do_test_cleanup(void)
298{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100299 tst_brk_handler = tst_cvres;
300
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200301 if (tst_test->cleanup)
302 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100303
Cyril Hrubisee761d82019-07-31 15:02:08 +0200304 tst_free_all();
305
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100306 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200307}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100308
309void tst_vbrk_(const char *file, const int lineno, int ttype,
310 const char *fmt, va_list va)
311{
312 print_result(file, lineno, ttype, fmt, va);
313
Steve Mucklec20831d2017-09-20 13:23:06 -0700314 /*
315 * The getpid implementation in some C library versions may cause cloned
316 * test threads to show the same pid as their parent when CLONE_VM is
317 * specified but CLONE_THREAD is not. Use direct syscall to avoid
318 * cleanup running in the child.
319 */
320 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200321 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100322
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200323 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200324 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200325
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100326 exit(TTYPE_RESULT(ttype));
327}
328
329void tst_res_(const char *file, const int lineno, int ttype,
330 const char *fmt, ...)
331{
332 va_list va;
333
334 va_start(va, fmt);
335 tst_vres_(file, lineno, ttype, fmt, va);
336 va_end(va);
337}
338
339void tst_brk_(const char *file, const int lineno, int ttype,
340 const char *fmt, ...)
341{
342 va_list va;
343
344 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100345 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100346 va_end(va);
347}
348
349static void check_child_status(pid_t pid, int status)
350{
351 int ret;
352
353 if (WIFSIGNALED(status)) {
354 tst_brk(TBROK, "Child (%i) killed by signal %s",
355 pid, tst_strsig(WTERMSIG(status)));
356 }
357
358 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100359 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100360
361 ret = WEXITSTATUS(status);
362 switch (ret) {
363 case TPASS:
364 break;
365 case TBROK:
366 case TCONF:
367 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200368 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100369 default:
370 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
371 }
372}
373
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300374void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100375{
376 int status;
377 pid_t pid;
378
379 for (;;) {
380 pid = wait(&status);
381
382 if (pid > 0) {
383 check_child_status(pid, status);
384 continue;
385 }
386
387 if (errno == ECHILD)
388 break;
389
390 if (errno == EINTR)
391 continue;
392
393 tst_brk(TBROK | TERRNO, "wait() failed");
394 }
395}
396
397
398pid_t safe_fork(const char *filename, unsigned int lineno)
399{
400 pid_t pid;
401
402 if (!tst_test->forks_child)
403 tst_brk(TBROK, "test.forks_child must be set!");
404
Michael Moese9da42372018-03-09 15:16:09 +0100405 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100406
407 pid = fork();
408 if (pid < 0)
409 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
410
Cyril Hrubis13296b62019-07-31 17:39:37 +0200411 if (!pid)
412 atexit(tst_free_all);
413
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100414 return pid;
415}
416
417static struct option {
418 char *optstr;
419 char *help;
420} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200421 {"h", "-h Prints this help"},
422 {"i:", "-i n Execute test n times"},
423 {"I:", "-I x Execute test for n seconds"},
424 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100425};
426
427static void print_help(void)
428{
429 unsigned int i;
430
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200431 fprintf(stderr, "Options\n");
432 fprintf(stderr, "-------\n");
433
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100434 for (i = 0; i < ARRAY_SIZE(options); i++)
435 fprintf(stderr, "%s\n", options[i].help);
436
437 if (!tst_test->options)
438 return;
439
440 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200441 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100442}
443
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200444static void print_test_tags(void)
445{
446 unsigned int i;
447 const struct tst_tag *tags = tst_test->tags;
448
449 printf("\nTags\n");
450 printf("----\n");
451
452 if (tags) {
453 for (i = 0; tags[i].name; i++) {
454 if (!strcmp(tags[i].name, "CVE"))
455 printf(CVE_DB_URL "%s\n", tags[i].value);
456 else if (!strcmp(tags[i].name, "linux-git"))
457 printf(LINUX_GIT_URL "%s\n", tags[i].value);
458 else
459 printf("%s: %s\n", tags[i].name, tags[i].value);
460 }
461 }
462
463 printf("\n");
464}
465
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100466static void check_option_collision(void)
467{
468 unsigned int i, j;
469 struct tst_option *toptions = tst_test->options;
470
471 if (!toptions)
472 return;
473
474 for (i = 0; toptions[i].optstr; i++) {
475 for (j = 0; j < ARRAY_SIZE(options); j++) {
476 if (toptions[i].optstr[0] == options[j].optstr[0]) {
477 tst_brk(TBROK, "Option collision '%s'",
478 options[j].help);
479 }
480 }
481 }
482}
483
484static unsigned int count_options(void)
485{
486 unsigned int i;
487
488 if (!tst_test->options)
489 return 0;
490
491 for (i = 0; tst_test->options[i].optstr; i++);
492
493 return i;
494}
495
496static void parse_topt(unsigned int topts_len, int opt, char *optarg)
497{
498 unsigned int i;
499 struct tst_option *toptions = tst_test->options;
500
501 for (i = 0; i < topts_len; i++) {
502 if (toptions[i].optstr[0] == opt)
503 break;
504 }
505
506 if (i >= topts_len)
507 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
508
Cyril Hrubisf2926be2019-11-05 12:58:11 +0100509 if (*toptions[i].arg)
510 tst_res(TWARN, "Option -%c passed multiple times", opt);
511
Jan Stancekc07d36c2016-08-01 11:44:55 +0200512 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100513}
514
515/* see self_exec.c */
516#ifdef UCLINUX
517extern char *child_args;
518#endif
519
520static void parse_opts(int argc, char *argv[])
521{
522 unsigned int i, topts_len = count_options();
523 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
524 int opt;
525
526 check_option_collision();
527
528 optstr[0] = 0;
529
530 for (i = 0; i < ARRAY_SIZE(options); i++)
531 strcat(optstr, options[i].optstr);
532
533 for (i = 0; i < topts_len; i++)
534 strcat(optstr, tst_test->options[i].optstr);
535
536 while ((opt = getopt(argc, argv, optstr)) > 0) {
537 switch (opt) {
538 case '?':
539 print_help();
540 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200541 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100542 case 'h':
543 print_help();
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200544 print_test_tags();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100545 exit(0);
546 case 'i':
547 iterations = atoi(optarg);
548 break;
549 case 'I':
550 duration = atof(optarg);
551 break;
552 case 'C':
553#ifdef UCLINUX
554 child_args = optarg;
555#endif
556 break;
557 default:
558 parse_topt(topts_len, opt, optarg);
559 }
560 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200561
562 if (optind < argc)
563 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100564}
565
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200566int tst_parse_int(const char *str, int *val, int min, int max)
567{
568 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300569
570 if (!str)
571 return 0;
572
573 int ret = tst_parse_long(str, &rval, min, max);
574
575 if (ret)
576 return ret;
577
578 *val = (int)rval;
579 return 0;
580}
581
582int tst_parse_long(const char *str, long *val, long min, long max)
583{
584 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200585 char *end;
586
587 if (!str)
588 return 0;
589
590 errno = 0;
591 rval = strtol(str, &end, 10);
592
593 if (str == end || *end != '\0')
594 return EINVAL;
595
596 if (errno)
597 return errno;
598
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300599 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200600 return ERANGE;
601
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300602 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200603 return 0;
604}
605
606int tst_parse_float(const char *str, float *val, float min, float max)
607{
608 double rval;
609 char *end;
610
611 if (!str)
612 return 0;
613
614 errno = 0;
615 rval = strtod(str, &end);
616
617 if (str == end || *end != '\0')
618 return EINVAL;
619
620 if (errno)
621 return errno;
622
623 if (rval > (double)max || rval < (double)min)
624 return ERANGE;
625
626 *val = (float)rval;
627 return 0;
628}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100629
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200630static void print_colored(const char *str)
631{
632 if (tst_color_enabled(STDOUT_FILENO))
633 printf("%s%s%s", ANSI_COLOR_YELLOW, str, ANSI_COLOR_RESET);
634 else
635 printf("%s", str);
636}
637
638static void print_failure_hints(void)
639{
640 unsigned int i;
641 const struct tst_tag *tags = tst_test->tags;
642
643 if (!tags)
644 return;
645
646 int hint_printed = 0;
647 for (i = 0; tags[i].name; i++) {
648 if (!strcmp(tags[i].name, "linux-git")) {
649 if (!hint_printed) {
650 hint_printed = 1;
651 printf("\n");
652 print_colored("HINT: ");
653 printf("You _MAY_ be missing kernel fixes, see:\n\n");
654 }
655
656 printf(LINUX_GIT_URL "%s\n", tags[i].value);
657 }
658
659 }
660
661 hint_printed = 0;
662 for (i = 0; tags[i].name; i++) {
663 if (!strcmp(tags[i].name, "CVE")) {
664 if (!hint_printed) {
665 hint_printed = 1;
666 printf("\n");
667 print_colored("HINT: ");
668 printf("You _MAY_ be vunerable to CVE(s), see:\n\n");
669 }
670
671 printf(CVE_DB_URL "%s\n", tags[i].value);
672 }
673 }
674}
675
Cyril Hrubisfa495172016-06-08 16:06:35 +0200676static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100677{
Xiao Yang11dfc322016-06-16 15:52:04 +0800678 if (results) {
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100679 if (results->passed && ret == TCONF)
680 ret = 0;
681
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200682 if (results->failed) {
Xiao Yang11dfc322016-06-16 15:52:04 +0800683 ret |= TFAIL;
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200684 print_failure_hints();
685 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100686
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200687 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800688 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100689
Xiao Yang11dfc322016-06-16 15:52:04 +0800690 if (results->warnings)
691 ret |= TWARN;
Cyril Hrubisca2c7692019-10-22 16:07:56 +0200692
693 printf("\nSummary:\n");
694 printf("passed %d\n", results->passed);
695 printf("failed %d\n", results->failed);
696 printf("skipped %d\n", results->skipped);
697 printf("warnings %d\n", results->warnings);
Xiao Yang11dfc322016-06-16 15:52:04 +0800698 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100699
Jan Stancek332540e2016-06-08 16:48:22 +0200700 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100701
702 exit(ret);
703}
704
705void check_kver(void)
706{
707 int v1, v2, v3;
708
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100709 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
710 tst_res(TWARN,
711 "Invalid kernel version %s, expected %%d.%%d.%%d",
712 tst_test->min_kver);
713 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100714
715 if (tst_kvercmp(v1, v2, v3) < 0) {
716 tst_brk(TCONF, "The test requires kernel %s or newer",
717 tst_test->min_kver);
718 }
719}
720
721static int results_equal(struct results *a, struct results *b)
722{
723 if (a->passed != b->passed)
724 return 0;
725
726 if (a->failed != b->failed)
727 return 0;
728
729 if (a->skipped != b->skipped)
730 return 0;
731
732 return 1;
733}
734
735static int needs_tmpdir(void)
736{
737 return tst_test->needs_tmpdir ||
738 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200739 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100740 tst_test->resource_files ||
741 tst_test->needs_checkpoints;
742}
743
744static void copy_resources(void)
745{
746 unsigned int i;
747
748 for (i = 0; tst_test->resource_files[i]; i++)
749 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
750}
751
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800752static const char *get_tid(char *argv[])
753{
754 char *p;
755
756 if (!argv[0] || !argv[0][0]) {
757 tst_res(TINFO, "argv[0] is empty!");
758 return "ltp_empty_argv";
759 }
760
761 p = strrchr(argv[0], '/');
762 if (p)
763 return p+1;
764
765 return argv[0];
766}
767
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100768static struct tst_device tdev;
769struct tst_device *tst_device;
770
Cyril Hrubisc4596542017-06-20 15:42:18 +0200771static void assert_test_fn(void)
772{
773 int cnt = 0;
774
775 if (tst_test->test)
776 cnt++;
777
778 if (tst_test->test_all)
779 cnt++;
780
781 if (tst_test->sample)
782 cnt++;
783
784 if (!cnt)
785 tst_brk(TBROK, "No test function speficied");
786
787 if (cnt != 1)
788 tst_brk(TBROK, "You can define only one test function");
789
790 if (tst_test->test && !tst_test->tcnt)
Eric Biggers3614a162019-03-15 09:42:15 -0700791 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
Cyril Hrubisc4596542017-06-20 15:42:18 +0200792
793 if (!tst_test->test && tst_test->tcnt)
794 tst_brk(TBROK, "You can define tcnt only for test()");
795}
796
Cyril Hrubise6da8672018-04-05 16:01:48 +0200797static int prepare_and_mount_ro_fs(const char *dev,
798 const char *mntpoint,
799 const char *fs_type)
800{
801 char buf[PATH_MAX];
802
803 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
804 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
805 dev, mntpoint, fs_type);
806 return 1;
807 }
808
809 mntpoint_mounted = 1;
810
811 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
812 SAFE_MKDIR(buf, 0777);
813
814 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
815 SAFE_FILE_PRINTF(buf, "file content");
816 SAFE_CHMOD(buf, 0777);
817
818 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
819
820 return 0;
821}
822
Xiao Yang7313a872018-09-01 12:07:41 +0800823static void prepare_and_mount_dev_fs(const char *mntpoint)
824{
825 const char *flags[] = {"nodev", NULL};
826 int mounted_nodev;
827
828 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
829 if (mounted_nodev) {
830 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
831 "mounting tmpfs without nodev on %s", mntpoint);
832 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
833 mntpoint_mounted = 1;
834 }
835}
836
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200837static void prepare_device(void)
838{
839 if (tst_test->format_device) {
840 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200841 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200842 }
843
Cyril Hrubise6da8672018-04-05 16:01:48 +0200844 if (tst_test->needs_rofs) {
845 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
846 tdev.fs_type);
847 return;
848 }
849
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200850 if (tst_test->mount_device) {
851 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
852 tst_test->mnt_flags, tst_test->mnt_data);
853 mntpoint_mounted = 1;
854 }
855}
856
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100857static void do_setup(int argc, char *argv[])
858{
859 if (!tst_test)
860 tst_brk(TBROK, "No tests to run");
861
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200862 if (tst_test->tconf_msg)
863 tst_brk(TCONF, "%s", tst_test->tconf_msg);
864
Cyril Hrubisf023a612018-11-27 14:21:17 +0100865 if (tst_test->needs_kconfigs)
866 tst_kconfig_check(tst_test->needs_kconfigs);
867
Cyril Hrubisc4596542017-06-20 15:42:18 +0200868 assert_test_fn();
869
Li Wangb5d620a2017-11-01 13:15:23 +0800870 tid = get_tid(argv);
871
Cyril Hrubisc4596542017-06-20 15:42:18 +0200872 if (tst_test->sample)
873 tst_test = tst_timer_test_setup(tst_test);
874
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200875 parse_opts(argc, argv);
876
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100877 if (tst_test->needs_root && geteuid() != 0)
878 tst_brk(TCONF, "Test needs to be run as root");
879
880 if (tst_test->min_kver)
881 check_kver();
882
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300883 if (tst_test->needs_drivers) {
884 const char *name;
885 int i;
886
887 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
888 if (tst_check_driver(name))
889 tst_brk(TCONF, "%s driver not available", name);
890 }
891
Cyril Hrubis874326d2017-02-14 15:59:40 +0100892 if (tst_test->format_device)
893 tst_test->needs_device = 1;
894
895 if (tst_test->mount_device) {
896 tst_test->needs_device = 1;
897 tst_test->format_device = 1;
898 }
899
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200900 if (tst_test->all_filesystems)
901 tst_test->needs_device = 1;
902
Li Wang9c4c3e92020-02-08 17:20:13 +0800903 if (tst_test->request_hugepages)
904 tst_request_hugepages(tst_test->request_hugepages);
905
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100906 setup_ipc();
907
Cyril Hrubisee761d82019-07-31 15:02:08 +0200908 if (tst_test->bufs)
909 tst_buffers_alloc(tst_test->bufs);
910
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000911 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100912 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100913
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100914 if (tst_test->save_restore) {
915 const char * const *name = tst_test->save_restore;
916
917 while (*name) {
918 tst_sys_conf_save(*name);
919 name++;
920 }
921 }
922
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700923 if (tst_test->mntpoint)
924 SAFE_MKDIR(tst_test->mntpoint, 0777);
925
Xiao Yang7313a872018-09-01 12:07:41 +0800926 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
927 tst_test->mount_device || tst_test->all_filesystems) &&
928 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700929 tst_brk(TBROK, "tst_test->mntpoint must be set!");
930 }
931
Xiao Yang7313a872018-09-01 12:07:41 +0800932 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
933 !!tst_test->needs_device > 1) {
934 tst_brk(TBROK,
935 "Two or more of needs_{rofs, devfs, device} are set");
936 }
937
938 if (tst_test->needs_devfs)
939 prepare_and_mount_dev_fs(tst_test->mntpoint);
940
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700941 if (tst_test->needs_rofs) {
942 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200943 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700944 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200945 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
946 tst_res(TINFO, "Can't mount tmpfs read-only, "
947 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700948 tst_test->needs_device = 1;
949 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700950 }
951 }
952
953 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100954 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100955
956 if (!tdev.dev)
957 tst_brk(TCONF, "Failed to acquire device");
958
959 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100960
961 if (tst_test->dev_fs_type)
962 tdev.fs_type = tst_test->dev_fs_type;
963 else
964 tdev.fs_type = tst_dev_fs_type();
965
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200966 if (!tst_test->all_filesystems)
967 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100968 }
969
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800970 if (tst_test->needs_overlay && !tst_test->mount_device) {
971 tst_brk(TBROK, "tst_test->mount_device must be set");
972 }
973 if (tst_test->needs_overlay && !mntpoint_mounted) {
974 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
975 }
976 if (tst_test->needs_overlay && !ovl_mounted) {
977 SAFE_MOUNT_OVERLAY();
978 ovl_mounted = 1;
979 }
980
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100981 if (tst_test->resource_files)
982 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200983
984 if (tst_test->restore_wallclock)
985 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200986}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100987
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200988static void do_test_setup(void)
989{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100990 main_pid = getpid();
991
Richard Palethorpe778c6ff2019-09-04 14:11:46 +0200992 if (tst_test->caps)
993 tst_cap_setup(tst_test->caps, TST_CAP_REQ);
994
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100995 if (tst_test->setup)
996 tst_test->setup();
997
998 if (main_pid != getpid())
999 tst_brk(TBROK, "Runaway child in setup()!");
Richard Palethorpe778c6ff2019-09-04 14:11:46 +02001000
1001 if (tst_test->caps)
1002 tst_cap_setup(tst_test->caps, TST_CAP_DROP);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001003}
1004
1005static void do_cleanup(void)
1006{
Murphy Zhoua88bbb42019-05-30 10:53:00 +08001007 if (ovl_mounted)
1008 SAFE_UMOUNT(OVL_MNT);
1009
Sandeep Patilc9a7def2017-09-19 12:49:58 -07001010 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +01001011 tst_umount(tst_test->mntpoint);
1012
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001013 if (tst_test->needs_device && tdev.dev)
1014 tst_release_device(tdev.dev);
1015
Steven Jackson9f41dcf2016-12-21 20:09:01 +00001016 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001017 /* avoid munmap() on wrong pointer in tst_rmdir() */
1018 tst_futexes = NULL;
1019 tst_rmdir();
1020 }
Jan Stancek332540e2016-06-08 16:48:22 +02001021
Li Wang9c4c3e92020-02-08 17:20:13 +08001022 tst_sys_conf_restore(0);
Jan Stancek9dcbc6d2018-11-05 09:00:02 +01001023
Rafael David Tinoco14e43562019-01-29 15:36:55 -02001024 if (tst_test->restore_wallclock)
1025 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +02001026
1027 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001028}
1029
1030static void run_tests(void)
1031{
1032 unsigned int i;
1033 struct results saved_results;
1034
1035 if (!tst_test->test) {
1036 saved_results = *results;
1037 tst_test->test_all();
1038
1039 if (getpid() != main_pid) {
1040 exit(0);
1041 }
1042
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +03001043 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001044
1045 if (results_equal(&saved_results, results))
1046 tst_brk(TBROK, "Test haven't reported results!");
1047 return;
1048 }
1049
1050 for (i = 0; i < tst_test->tcnt; i++) {
1051 saved_results = *results;
1052 tst_test->test(i);
1053
1054 if (getpid() != main_pid) {
1055 exit(0);
1056 }
1057
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +03001058 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001059
1060 if (results_equal(&saved_results, results))
1061 tst_brk(TBROK, "Test %i haven't reported results!", i);
1062 }
1063}
1064
1065static unsigned long long get_time_ms(void)
1066{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001067 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001068
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001069 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
1070 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001071
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +01001072 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001073}
1074
Jan Stancekb95b1992017-10-10 15:47:58 +02001075static void add_paths(void)
1076{
1077 char *old_path = getenv("PATH");
1078 const char *start_dir;
1079 char *new_path;
1080
1081 start_dir = tst_get_startwd();
1082
1083 if (old_path)
1084 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
1085 else
1086 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
1087
1088 SAFE_SETENV("PATH", new_path, 1);
1089 free(new_path);
1090}
1091
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001092static void heartbeat(void)
1093{
Jan Stancek1893e012018-08-28 16:17:52 +02001094 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
1095 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1096
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001097 kill(getppid(), SIGUSR1);
1098}
1099
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001100static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001101{
1102 unsigned int i = 0;
1103 unsigned long long stop_time = 0;
1104 int cont = 1;
1105
Jan Stancek1893e012018-08-28 16:17:52 +02001106 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001107 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001108 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001109
1110 if (duration > 0)
1111 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1112
1113 for (;;) {
1114 cont = 0;
1115
1116 if (i < (unsigned int)iterations) {
1117 i++;
1118 cont = 1;
1119 }
1120
1121 if (stop_time && get_time_ms() < stop_time)
1122 cont = 1;
1123
1124 if (!cont)
1125 break;
1126
1127 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001128 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001129 }
1130
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001131 do_test_cleanup();
1132 exit(0);
1133}
1134
1135static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001136
Cyril Hrubis79163172017-05-18 10:57:07 +02001137
1138static volatile sig_atomic_t sigkill_retries;
1139
1140#define WRITE_MSG(msg) do { \
1141 if (write(2, msg, sizeof(msg) - 1)) { \
1142 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1143 } \
1144} while (0)
1145
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001146static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1147{
Cyril Hrubis79163172017-05-18 10:57:07 +02001148 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001149 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001150 alarm(5);
1151
1152 if (++sigkill_retries > 10) {
1153 WRITE_MSG("Cannot kill test processes!\n");
1154 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1155 WRITE_MSG("Exitting uncleanly...\n");
1156 _exit(TFAIL);
1157 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001158}
1159
1160static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1161{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001162 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001163 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001164}
1165
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001166static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1167{
1168 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001169 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001170 kill(-test_pid, SIGKILL);
1171 }
1172}
1173
Jan Stancek1893e012018-08-28 16:17:52 +02001174unsigned int tst_timeout_remaining(void)
1175{
1176 static struct timespec now;
1177 unsigned int elapsed;
1178
1179 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1180 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1181
1182 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1183 if (results->timeout > elapsed)
1184 return results->timeout - elapsed;
1185
1186 return 0;
1187}
1188
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001189unsigned int tst_multiply_timeout(unsigned int timeout)
1190{
1191 char *mul;
1192 int ret;
1193
1194 if (timeout_mul == -1) {
1195 mul = getenv("LTP_TIMEOUT_MUL");
1196 if (mul) {
1197 if ((ret = tst_parse_float(mul, &timeout_mul, 1, 10000))) {
1198 tst_brk(TBROK, "Failed to parse LTP_TIMEOUT_MUL: %s",
1199 tst_strerrno(ret));
1200 }
1201 } else {
1202 timeout_mul = 1;
1203 }
1204 }
1205 if (timeout_mul < 1)
1206 tst_brk(TBROK, "LTP_TIMEOUT_MUL must to be int >= 1! (%.2f)",
1207 timeout_mul);
1208
1209 if (timeout < 1)
1210 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
1211
1212 return timeout * timeout_mul;
1213}
1214
Li Wang94823cf2017-07-18 16:23:00 +08001215void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001216{
Li Wang94823cf2017-07-18 16:23:00 +08001217 if (timeout == -1) {
1218 tst_res(TINFO, "Timeout per run is disabled");
1219 return;
1220 }
1221
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001222 if (timeout < 1)
1223 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001224
Clemens Famulla-Conrad56aa4382019-10-18 14:44:59 +02001225 results->timeout = tst_multiply_timeout(timeout);
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001226
1227 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1228 results->timeout/3600, (results->timeout%3600)/60,
1229 results->timeout % 60);
1230
1231 if (getpid() == lib_pid)
1232 alarm(results->timeout);
1233 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001234 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001235}
1236
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001237static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001238{
1239 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001240
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001241 if (tst_test->timeout)
1242 tst_set_timeout(tst_test->timeout);
1243 else
1244 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001245
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001246 SAFE_SIGNAL(SIGINT, sigint_handler);
1247
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001248 test_pid = fork();
1249 if (test_pid < 0)
1250 tst_brk(TBROK | TERRNO, "fork()");
1251
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001252 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001253 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1254 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1255 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001256 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001257 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001258 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001259
1260 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001261 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001262 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001263
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001264 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001265 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001266
1267 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1268 tst_res(TINFO, "If you are running on slow machine, "
1269 "try exporting LTP_TIMEOUT_MUL > 1");
1270 tst_brk(TBROK, "Test killed! (timeout?)");
1271 }
1272
1273 if (WIFSIGNALED(status))
1274 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1275
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001276 return 0;
1277}
1278
1279static int run_tcases_per_fs(void)
1280{
1281 int ret = 0;
1282 unsigned int i;
Cyril Hrubisdbe56e52019-06-19 14:20:19 +02001283 const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001284
1285 if (!filesystems[0])
1286 tst_brk(TCONF, "There are no supported filesystems");
1287
1288 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001289
1290 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001291 tdev.fs_type = filesystems[i];
1292
1293 prepare_device();
1294
1295 ret = fork_testrun();
1296
1297 if (mntpoint_mounted) {
1298 tst_umount(tst_test->mntpoint);
1299 mntpoint_mounted = 0;
1300 }
1301
1302 if (ret == TCONF) {
1303 update_results(ret);
1304 continue;
1305 }
1306
1307 if (ret == 0)
1308 continue;
1309
1310 do_exit(ret);
1311 }
1312
1313 return ret;
1314}
1315
Cyril Hrubisce082162019-03-06 16:24:29 +01001316unsigned int tst_variant;
1317
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001318void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1319{
Cyril Hrubisce082162019-03-06 16:24:29 +01001320 int ret = 0;
1321 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001322
1323 lib_pid = getpid();
1324 tst_test = self;
1325
1326 do_setup(argc, argv);
1327
1328 TCID = tid;
1329
1330 SAFE_SIGNAL(SIGALRM, alarm_handler);
1331 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1332
Cyril Hrubisce082162019-03-06 16:24:29 +01001333 if (tst_test->test_variants)
1334 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001335
Cyril Hrubisce082162019-03-06 16:24:29 +01001336 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1337 if (tst_test->all_filesystems)
1338 ret |= run_tcases_per_fs();
1339 else
1340 ret |= fork_testrun();
1341
1342 if (ret & ~(TCONF))
1343 goto exit;
1344 }
1345
1346exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001347 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001348}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001349
1350
1351void tst_flush(void)
1352{
1353 int rval;
1354
1355 rval = fflush(stderr);
1356 if (rval != 0)
1357 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1358
1359 rval = fflush(stderr);
1360 if (rval != 0)
1361 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1362
1363}