blob: 7395996c00fb6878d751f60cbd399e2d3ece8b40 [file] [log] [blame]
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001/*
2 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdio.h>
19#include <stdarg.h>
20#include <unistd.h>
21#include <string.h>
22#include <stdlib.h>
23#include <errno.h>
Sandeep Patilc9a7def2017-09-19 12:49:58 -070024#include <sys/mount.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010025#include <sys/types.h>
26#include <sys/wait.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010027
28#define TST_NO_DEFAULT_MAIN
29#include "tst_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010030#include "tst_device.h"
31#include "lapi/futex.h"
Steve Mucklec20831d2017-09-20 13:23:06 -070032#include "lapi/syscalls.h"
Petr Vorel3a0ef862017-03-01 15:31:08 +010033#include "tst_ansi_color.h"
Jan Stancekb95b1992017-10-10 15:47:58 +020034#include "tst_safe_stdio.h"
Cyril Hrubisc4596542017-06-20 15:42:18 +020035#include "tst_timer_test.h"
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +010036#include "tst_clocks.h"
37#include "tst_timer.h"
Rafael David Tinoco14e43562019-01-29 15:36:55 -020038#include "tst_wallclock.h"
Jan Stancek9dcbc6d2018-11-05 09:00:02 +010039#include "tst_sys_conf.h"
Cyril Hrubisf023a612018-11-27 14:21:17 +010040#include "tst_kconfig.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010041
42#include "old_resource.h"
43#include "old_device.h"
44#include "old_tmpdir.h"
45
46struct tst_test *tst_test;
47
Li Wangb5d620a2017-11-01 13:15:23 +080048static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010049static int iterations = 1;
50static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020051static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070052static int mntpoint_mounted;
Murphy Zhoua88bbb42019-05-30 10:53:00 +080053static int ovl_mounted;
Jan Stancek1893e012018-08-28 16:17:52 +020054static struct timespec tst_start_time; /* valid only for test pid */
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010055
56struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020057 int passed;
58 int skipped;
59 int failed;
60 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020061 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010062};
63
64static struct results *results;
65
66static int ipc_fd;
67
68extern void *tst_futexes;
69extern unsigned int tst_max_futexes;
70
71#define IPC_ENV_VAR "LTP_IPC_PATH"
72
73static char ipc_path[1024];
74const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010075
76static char shm_path[1024];
77
Christian Lanig18b780d2018-07-23 15:24:50 +020078int TST_ERR;
79long TST_RET;
80
Jan Stancek332540e2016-06-08 16:48:22 +020081static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020082static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020083
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010084static void setup_ipc(void)
85{
86 size_t size = getpagesize();
87
Steven Jackson9f41dcf2016-12-21 20:09:01 +000088 if (access("/dev/shm", F_OK) == 0) {
89 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080090 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000091 } else {
92 char *tmpdir;
93
94 if (!tst_tmpdir_created())
95 tst_tmpdir();
96
97 tmpdir = tst_get_tmpdir();
98 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080099 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000100 free(tmpdir);
101 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100102
103 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
104 if (ipc_fd < 0)
105 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +0200106 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100107
108 SAFE_FTRUNCATE(ipc_fd, size);
109
110 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
111
112 /* Checkpoints needs to be accessible from processes started by exec() */
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200113 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100114 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200115 putenv(ipc_path);
116 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100117 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200118 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100119
120 SAFE_CLOSE(ipc_fd);
121
122 if (tst_test->needs_checkpoints) {
123 tst_futexes = (char*)results + sizeof(struct results);
124 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
125 }
126}
127
128static void cleanup_ipc(void)
129{
130 size_t size = getpagesize();
131
132 if (ipc_fd > 0 && close(ipc_fd))
133 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
134
Cyril Hrubis9726b902017-07-27 11:28:50 +0200135 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100136 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
137
Cyril Hrubis9726b902017-07-27 11:28:50 +0200138 if (results) {
139 msync((void*)results, size, MS_SYNC);
140 munmap((void*)results, size);
Yann Sionneau511a0e12019-06-14 10:26:45 +0200141 results = NULL;
Cyril Hrubis9726b902017-07-27 11:28:50 +0200142 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100143}
144
145void tst_reinit(void)
146{
Jan Stancek03fc5372017-10-10 13:36:59 +0200147 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100148 size_t size = getpagesize();
149 int fd;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100150
151 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200152 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100153
154 if (access(path, F_OK))
155 tst_brk(TBROK, "File %s does not exist!", path);
156
157 fd = SAFE_OPEN(path, O_RDWR);
158
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200159 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
160 tst_futexes = (char*)results + sizeof(struct results);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100161 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
162
163 SAFE_CLOSE(fd);
164}
165
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100166static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100167{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100168 if (!results)
169 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100170
171 switch (ttype) {
172 case TCONF:
173 tst_atomic_inc(&results->skipped);
174 break;
175 case TPASS:
176 tst_atomic_inc(&results->passed);
177 break;
178 case TWARN:
179 tst_atomic_inc(&results->warnings);
180 break;
181 case TFAIL:
182 tst_atomic_inc(&results->failed);
183 break;
184 }
185}
186
187static void print_result(const char *file, const int lineno, int ttype,
188 const char *fmt, va_list va)
189{
190 char buf[1024];
191 char *str = buf;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100192 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100193 const char *str_errno = NULL;
194 const char *res;
195
196 switch (TTYPE_RESULT(ttype)) {
197 case TPASS:
198 res = "PASS";
199 break;
200 case TFAIL:
201 res = "FAIL";
202 break;
203 case TBROK:
204 res = "BROK";
205 break;
206 case TCONF:
207 res = "CONF";
208 break;
209 case TWARN:
210 res = "WARN";
211 break;
212 case TINFO:
213 res = "INFO";
214 break;
215 default:
216 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100217 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100218 }
219
220 if (ttype & TERRNO)
221 str_errno = tst_strerrno(errno);
222
223 if (ttype & TTERRNO)
Christian Lanig18b780d2018-07-23 15:24:50 +0200224 str_errno = tst_strerrno(TST_ERR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100225
Richard Palethorpefb32e892018-03-13 16:24:47 +0100226 if (ttype & TRERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200227 ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
Richard Palethorpefb32e892018-03-13 16:24:47 +0100228 str_errno = tst_strerrno(ret);
229 }
230
Petr Vorela7f61332017-01-24 20:47:30 +0100231 ret = snprintf(str, size, "%s:%i: ", file, lineno);
232 str += ret;
233 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100234
Cyril Hrubis047c7272017-02-13 16:23:36 +0100235 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100236 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
237 res, ANSI_COLOR_RESET);
238 else
239 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100240 str += ret;
241 size -= ret;
242
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100243 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100244 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100245 str += MIN(ret, ssize);
246 size -= MIN(ret, ssize);
247 if (ret >= ssize) {
248 tst_res_(file, lineno, TWARN,
249 "Next message is too long and truncated:");
250 } else if (str_errno) {
251 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100252 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100253 str += MIN(ret, ssize);
254 size -= MIN(ret, ssize);
255 if (ret >= ssize)
256 tst_res_(file, lineno, TWARN,
257 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100258 }
259
260 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100261
262 fputs(buf, stderr);
263}
264
265void tst_vres_(const char *file, const int lineno, int ttype,
266 const char *fmt, va_list va)
267{
268 print_result(file, lineno, ttype, fmt, va);
269
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100270 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100271}
272
273void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100274 const char *fmt, va_list va);
275
276static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
277 const char *fmt, va_list va) = tst_vbrk_;
278
279static void tst_cvres(const char *file, const int lineno, int ttype,
280 const char *fmt, va_list va)
281{
282 if (TTYPE_RESULT(ttype) == TBROK) {
283 ttype &= ~TTYPE_MASK;
284 ttype |= TWARN;
285 }
286
287 print_result(file, lineno, ttype, fmt, va);
288 update_results(TTYPE_RESULT(ttype));
289}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100290
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200291static void do_test_cleanup(void)
292{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100293 tst_brk_handler = tst_cvres;
294
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200295 if (tst_test->cleanup)
296 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100297
298 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200299}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100300
301void tst_vbrk_(const char *file, const int lineno, int ttype,
302 const char *fmt, va_list va)
303{
304 print_result(file, lineno, ttype, fmt, va);
305
Steve Mucklec20831d2017-09-20 13:23:06 -0700306 /*
307 * The getpid implementation in some C library versions may cause cloned
308 * test threads to show the same pid as their parent when CLONE_VM is
309 * specified but CLONE_THREAD is not. Use direct syscall to avoid
310 * cleanup running in the child.
311 */
312 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200313 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100314
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200315 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200316 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200317
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100318 exit(TTYPE_RESULT(ttype));
319}
320
321void tst_res_(const char *file, const int lineno, int ttype,
322 const char *fmt, ...)
323{
324 va_list va;
325
326 va_start(va, fmt);
327 tst_vres_(file, lineno, ttype, fmt, va);
328 va_end(va);
329}
330
331void tst_brk_(const char *file, const int lineno, int ttype,
332 const char *fmt, ...)
333{
334 va_list va;
335
336 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100337 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100338 va_end(va);
339}
340
341static void check_child_status(pid_t pid, int status)
342{
343 int ret;
344
345 if (WIFSIGNALED(status)) {
346 tst_brk(TBROK, "Child (%i) killed by signal %s",
347 pid, tst_strsig(WTERMSIG(status)));
348 }
349
350 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100351 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100352
353 ret = WEXITSTATUS(status);
354 switch (ret) {
355 case TPASS:
356 break;
357 case TBROK:
358 case TCONF:
359 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200360 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100361 default:
362 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
363 }
364}
365
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300366void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100367{
368 int status;
369 pid_t pid;
370
371 for (;;) {
372 pid = wait(&status);
373
374 if (pid > 0) {
375 check_child_status(pid, status);
376 continue;
377 }
378
379 if (errno == ECHILD)
380 break;
381
382 if (errno == EINTR)
383 continue;
384
385 tst_brk(TBROK | TERRNO, "wait() failed");
386 }
387}
388
389
390pid_t safe_fork(const char *filename, unsigned int lineno)
391{
392 pid_t pid;
393
394 if (!tst_test->forks_child)
395 tst_brk(TBROK, "test.forks_child must be set!");
396
Michael Moese9da42372018-03-09 15:16:09 +0100397 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100398
399 pid = fork();
400 if (pid < 0)
401 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
402
403 return pid;
404}
405
406static struct option {
407 char *optstr;
408 char *help;
409} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200410 {"h", "-h Prints this help"},
411 {"i:", "-i n Execute test n times"},
412 {"I:", "-I x Execute test for n seconds"},
413 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100414};
415
416static void print_help(void)
417{
418 unsigned int i;
419
420 for (i = 0; i < ARRAY_SIZE(options); i++)
421 fprintf(stderr, "%s\n", options[i].help);
422
423 if (!tst_test->options)
424 return;
425
426 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200427 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100428}
429
430static void check_option_collision(void)
431{
432 unsigned int i, j;
433 struct tst_option *toptions = tst_test->options;
434
435 if (!toptions)
436 return;
437
438 for (i = 0; toptions[i].optstr; i++) {
439 for (j = 0; j < ARRAY_SIZE(options); j++) {
440 if (toptions[i].optstr[0] == options[j].optstr[0]) {
441 tst_brk(TBROK, "Option collision '%s'",
442 options[j].help);
443 }
444 }
445 }
446}
447
448static unsigned int count_options(void)
449{
450 unsigned int i;
451
452 if (!tst_test->options)
453 return 0;
454
455 for (i = 0; tst_test->options[i].optstr; i++);
456
457 return i;
458}
459
460static void parse_topt(unsigned int topts_len, int opt, char *optarg)
461{
462 unsigned int i;
463 struct tst_option *toptions = tst_test->options;
464
465 for (i = 0; i < topts_len; i++) {
466 if (toptions[i].optstr[0] == opt)
467 break;
468 }
469
470 if (i >= topts_len)
471 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
472
Jan Stancekc07d36c2016-08-01 11:44:55 +0200473 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100474}
475
476/* see self_exec.c */
477#ifdef UCLINUX
478extern char *child_args;
479#endif
480
481static void parse_opts(int argc, char *argv[])
482{
483 unsigned int i, topts_len = count_options();
484 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
485 int opt;
486
487 check_option_collision();
488
489 optstr[0] = 0;
490
491 for (i = 0; i < ARRAY_SIZE(options); i++)
492 strcat(optstr, options[i].optstr);
493
494 for (i = 0; i < topts_len; i++)
495 strcat(optstr, tst_test->options[i].optstr);
496
497 while ((opt = getopt(argc, argv, optstr)) > 0) {
498 switch (opt) {
499 case '?':
500 print_help();
501 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200502 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100503 case 'h':
504 print_help();
505 exit(0);
506 case 'i':
507 iterations = atoi(optarg);
508 break;
509 case 'I':
510 duration = atof(optarg);
511 break;
512 case 'C':
513#ifdef UCLINUX
514 child_args = optarg;
515#endif
516 break;
517 default:
518 parse_topt(topts_len, opt, optarg);
519 }
520 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200521
522 if (optind < argc)
523 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100524}
525
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200526int tst_parse_int(const char *str, int *val, int min, int max)
527{
528 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300529
530 if (!str)
531 return 0;
532
533 int ret = tst_parse_long(str, &rval, min, max);
534
535 if (ret)
536 return ret;
537
538 *val = (int)rval;
539 return 0;
540}
541
542int tst_parse_long(const char *str, long *val, long min, long max)
543{
544 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200545 char *end;
546
547 if (!str)
548 return 0;
549
550 errno = 0;
551 rval = strtol(str, &end, 10);
552
553 if (str == end || *end != '\0')
554 return EINVAL;
555
556 if (errno)
557 return errno;
558
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300559 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200560 return ERANGE;
561
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300562 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200563 return 0;
564}
565
566int tst_parse_float(const char *str, float *val, float min, float max)
567{
568 double rval;
569 char *end;
570
571 if (!str)
572 return 0;
573
574 errno = 0;
575 rval = strtod(str, &end);
576
577 if (str == end || *end != '\0')
578 return EINVAL;
579
580 if (errno)
581 return errno;
582
583 if (rval > (double)max || rval < (double)min)
584 return ERANGE;
585
586 *val = (float)rval;
587 return 0;
588}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100589
Cyril Hrubisfa495172016-06-08 16:06:35 +0200590static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100591{
Xiao Yang11dfc322016-06-16 15:52:04 +0800592 if (results) {
593 printf("\nSummary:\n");
594 printf("passed %d\n", results->passed);
595 printf("failed %d\n", results->failed);
596 printf("skipped %d\n", results->skipped);
597 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100598
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100599 if (results->passed && ret == TCONF)
600 ret = 0;
601
Xiao Yang11dfc322016-06-16 15:52:04 +0800602 if (results->failed)
603 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100604
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200605 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800606 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100607
Xiao Yang11dfc322016-06-16 15:52:04 +0800608 if (results->warnings)
609 ret |= TWARN;
610 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100611
Jan Stancek332540e2016-06-08 16:48:22 +0200612 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100613
614 exit(ret);
615}
616
617void check_kver(void)
618{
619 int v1, v2, v3;
620
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100621 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
622 tst_res(TWARN,
623 "Invalid kernel version %s, expected %%d.%%d.%%d",
624 tst_test->min_kver);
625 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100626
627 if (tst_kvercmp(v1, v2, v3) < 0) {
628 tst_brk(TCONF, "The test requires kernel %s or newer",
629 tst_test->min_kver);
630 }
631}
632
633static int results_equal(struct results *a, struct results *b)
634{
635 if (a->passed != b->passed)
636 return 0;
637
638 if (a->failed != b->failed)
639 return 0;
640
641 if (a->skipped != b->skipped)
642 return 0;
643
644 return 1;
645}
646
647static int needs_tmpdir(void)
648{
649 return tst_test->needs_tmpdir ||
650 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200651 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100652 tst_test->resource_files ||
653 tst_test->needs_checkpoints;
654}
655
656static void copy_resources(void)
657{
658 unsigned int i;
659
660 for (i = 0; tst_test->resource_files[i]; i++)
661 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
662}
663
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800664static const char *get_tid(char *argv[])
665{
666 char *p;
667
668 if (!argv[0] || !argv[0][0]) {
669 tst_res(TINFO, "argv[0] is empty!");
670 return "ltp_empty_argv";
671 }
672
673 p = strrchr(argv[0], '/');
674 if (p)
675 return p+1;
676
677 return argv[0];
678}
679
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100680static struct tst_device tdev;
681struct tst_device *tst_device;
682
Cyril Hrubisc4596542017-06-20 15:42:18 +0200683static void assert_test_fn(void)
684{
685 int cnt = 0;
686
687 if (tst_test->test)
688 cnt++;
689
690 if (tst_test->test_all)
691 cnt++;
692
693 if (tst_test->sample)
694 cnt++;
695
696 if (!cnt)
697 tst_brk(TBROK, "No test function speficied");
698
699 if (cnt != 1)
700 tst_brk(TBROK, "You can define only one test function");
701
702 if (tst_test->test && !tst_test->tcnt)
Eric Biggers3614a162019-03-15 09:42:15 -0700703 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
Cyril Hrubisc4596542017-06-20 15:42:18 +0200704
705 if (!tst_test->test && tst_test->tcnt)
706 tst_brk(TBROK, "You can define tcnt only for test()");
707}
708
Cyril Hrubise6da8672018-04-05 16:01:48 +0200709static int prepare_and_mount_ro_fs(const char *dev,
710 const char *mntpoint,
711 const char *fs_type)
712{
713 char buf[PATH_MAX];
714
715 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
716 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
717 dev, mntpoint, fs_type);
718 return 1;
719 }
720
721 mntpoint_mounted = 1;
722
723 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
724 SAFE_MKDIR(buf, 0777);
725
726 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
727 SAFE_FILE_PRINTF(buf, "file content");
728 SAFE_CHMOD(buf, 0777);
729
730 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
731
732 return 0;
733}
734
Xiao Yang7313a872018-09-01 12:07:41 +0800735static void prepare_and_mount_dev_fs(const char *mntpoint)
736{
737 const char *flags[] = {"nodev", NULL};
738 int mounted_nodev;
739
740 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
741 if (mounted_nodev) {
742 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
743 "mounting tmpfs without nodev on %s", mntpoint);
744 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
745 mntpoint_mounted = 1;
746 }
747}
748
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200749static void prepare_device(void)
750{
751 if (tst_test->format_device) {
752 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200753 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200754 }
755
Cyril Hrubise6da8672018-04-05 16:01:48 +0200756 if (tst_test->needs_rofs) {
757 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
758 tdev.fs_type);
759 return;
760 }
761
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200762 if (tst_test->mount_device) {
763 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
764 tst_test->mnt_flags, tst_test->mnt_data);
765 mntpoint_mounted = 1;
766 }
767}
768
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100769static void do_setup(int argc, char *argv[])
770{
771 if (!tst_test)
772 tst_brk(TBROK, "No tests to run");
773
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200774 if (tst_test->tconf_msg)
775 tst_brk(TCONF, "%s", tst_test->tconf_msg);
776
Cyril Hrubisf023a612018-11-27 14:21:17 +0100777 if (tst_test->needs_kconfigs)
778 tst_kconfig_check(tst_test->needs_kconfigs);
779
Cyril Hrubisc4596542017-06-20 15:42:18 +0200780 assert_test_fn();
781
Li Wangb5d620a2017-11-01 13:15:23 +0800782 tid = get_tid(argv);
783
Cyril Hrubisc4596542017-06-20 15:42:18 +0200784 if (tst_test->sample)
785 tst_test = tst_timer_test_setup(tst_test);
786
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200787 parse_opts(argc, argv);
788
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100789 if (tst_test->needs_root && geteuid() != 0)
790 tst_brk(TCONF, "Test needs to be run as root");
791
792 if (tst_test->min_kver)
793 check_kver();
794
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300795 if (tst_test->needs_drivers) {
796 const char *name;
797 int i;
798
799 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
800 if (tst_check_driver(name))
801 tst_brk(TCONF, "%s driver not available", name);
802 }
803
Cyril Hrubis874326d2017-02-14 15:59:40 +0100804 if (tst_test->format_device)
805 tst_test->needs_device = 1;
806
807 if (tst_test->mount_device) {
808 tst_test->needs_device = 1;
809 tst_test->format_device = 1;
810 }
811
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200812 if (tst_test->all_filesystems)
813 tst_test->needs_device = 1;
814
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100815 setup_ipc();
816
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000817 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100818 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100819
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100820 if (tst_test->save_restore) {
821 const char * const *name = tst_test->save_restore;
822
823 while (*name) {
824 tst_sys_conf_save(*name);
825 name++;
826 }
827 }
828
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700829 if (tst_test->mntpoint)
830 SAFE_MKDIR(tst_test->mntpoint, 0777);
831
Xiao Yang7313a872018-09-01 12:07:41 +0800832 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
833 tst_test->mount_device || tst_test->all_filesystems) &&
834 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700835 tst_brk(TBROK, "tst_test->mntpoint must be set!");
836 }
837
Xiao Yang7313a872018-09-01 12:07:41 +0800838 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
839 !!tst_test->needs_device > 1) {
840 tst_brk(TBROK,
841 "Two or more of needs_{rofs, devfs, device} are set");
842 }
843
844 if (tst_test->needs_devfs)
845 prepare_and_mount_dev_fs(tst_test->mntpoint);
846
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700847 if (tst_test->needs_rofs) {
848 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200849 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700850 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200851 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
852 tst_res(TINFO, "Can't mount tmpfs read-only, "
853 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700854 tst_test->needs_device = 1;
855 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700856 }
857 }
858
859 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100860 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100861
862 if (!tdev.dev)
863 tst_brk(TCONF, "Failed to acquire device");
864
865 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100866
867 if (tst_test->dev_fs_type)
868 tdev.fs_type = tst_test->dev_fs_type;
869 else
870 tdev.fs_type = tst_dev_fs_type();
871
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200872 if (!tst_test->all_filesystems)
873 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100874 }
875
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800876 if (tst_test->needs_overlay && !tst_test->mount_device) {
877 tst_brk(TBROK, "tst_test->mount_device must be set");
878 }
879 if (tst_test->needs_overlay && !mntpoint_mounted) {
880 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
881 }
882 if (tst_test->needs_overlay && !ovl_mounted) {
883 SAFE_MOUNT_OVERLAY();
884 ovl_mounted = 1;
885 }
886
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100887 if (tst_test->resource_files)
888 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200889
890 if (tst_test->restore_wallclock)
891 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200892}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100893
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200894static void do_test_setup(void)
895{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100896 main_pid = getpid();
897
898 if (tst_test->setup)
899 tst_test->setup();
900
901 if (main_pid != getpid())
902 tst_brk(TBROK, "Runaway child in setup()!");
903}
904
905static void do_cleanup(void)
906{
Murphy Zhoua88bbb42019-05-30 10:53:00 +0800907 if (ovl_mounted)
908 SAFE_UMOUNT(OVL_MNT);
909
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700910 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100911 tst_umount(tst_test->mntpoint);
912
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100913 if (tst_test->needs_device && tdev.dev)
914 tst_release_device(tdev.dev);
915
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000916 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100917 /* avoid munmap() on wrong pointer in tst_rmdir() */
918 tst_futexes = NULL;
919 tst_rmdir();
920 }
Jan Stancek332540e2016-06-08 16:48:22 +0200921
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100922 if (tst_test->save_restore)
923 tst_sys_conf_restore(0);
924
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200925 if (tst_test->restore_wallclock)
926 tst_wallclock_restore();
Yann Sionneau511a0e12019-06-14 10:26:45 +0200927
928 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100929}
930
931static void run_tests(void)
932{
933 unsigned int i;
934 struct results saved_results;
935
936 if (!tst_test->test) {
937 saved_results = *results;
938 tst_test->test_all();
939
940 if (getpid() != main_pid) {
941 exit(0);
942 }
943
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300944 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100945
946 if (results_equal(&saved_results, results))
947 tst_brk(TBROK, "Test haven't reported results!");
948 return;
949 }
950
951 for (i = 0; i < tst_test->tcnt; i++) {
952 saved_results = *results;
953 tst_test->test(i);
954
955 if (getpid() != main_pid) {
956 exit(0);
957 }
958
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300959 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100960
961 if (results_equal(&saved_results, results))
962 tst_brk(TBROK, "Test %i haven't reported results!", i);
963 }
964}
965
966static unsigned long long get_time_ms(void)
967{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100968 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100969
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100970 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
971 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100972
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100973 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100974}
975
Jan Stancekb95b1992017-10-10 15:47:58 +0200976static void add_paths(void)
977{
978 char *old_path = getenv("PATH");
979 const char *start_dir;
980 char *new_path;
981
982 start_dir = tst_get_startwd();
983
984 if (old_path)
985 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
986 else
987 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
988
989 SAFE_SETENV("PATH", new_path, 1);
990 free(new_path);
991}
992
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200993static void heartbeat(void)
994{
Jan Stancek1893e012018-08-28 16:17:52 +0200995 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
996 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
997
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200998 kill(getppid(), SIGUSR1);
999}
1000
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001001static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001002{
1003 unsigned int i = 0;
1004 unsigned long long stop_time = 0;
1005 int cont = 1;
1006
Jan Stancek1893e012018-08-28 16:17:52 +02001007 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +02001008 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001009 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001010
1011 if (duration > 0)
1012 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1013
1014 for (;;) {
1015 cont = 0;
1016
1017 if (i < (unsigned int)iterations) {
1018 i++;
1019 cont = 1;
1020 }
1021
1022 if (stop_time && get_time_ms() < stop_time)
1023 cont = 1;
1024
1025 if (!cont)
1026 break;
1027
1028 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001029 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001030 }
1031
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001032 do_test_cleanup();
1033 exit(0);
1034}
1035
1036static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001037
Cyril Hrubis79163172017-05-18 10:57:07 +02001038
1039static volatile sig_atomic_t sigkill_retries;
1040
1041#define WRITE_MSG(msg) do { \
1042 if (write(2, msg, sizeof(msg) - 1)) { \
1043 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1044 } \
1045} while (0)
1046
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001047static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1048{
Cyril Hrubis79163172017-05-18 10:57:07 +02001049 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001050 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001051 alarm(5);
1052
1053 if (++sigkill_retries > 10) {
1054 WRITE_MSG("Cannot kill test processes!\n");
1055 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1056 WRITE_MSG("Exitting uncleanly...\n");
1057 _exit(TFAIL);
1058 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001059}
1060
1061static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1062{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001063 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001064 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001065}
1066
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001067static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1068{
1069 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001070 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001071 kill(-test_pid, SIGKILL);
1072 }
1073}
1074
Jan Stancek1893e012018-08-28 16:17:52 +02001075unsigned int tst_timeout_remaining(void)
1076{
1077 static struct timespec now;
1078 unsigned int elapsed;
1079
1080 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1081 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1082
1083 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1084 if (results->timeout > elapsed)
1085 return results->timeout - elapsed;
1086
1087 return 0;
1088}
1089
Li Wang94823cf2017-07-18 16:23:00 +08001090void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001091{
1092 char *mul = getenv("LTP_TIMEOUT_MUL");
1093
Li Wang94823cf2017-07-18 16:23:00 +08001094 if (timeout == -1) {
1095 tst_res(TINFO, "Timeout per run is disabled");
1096 return;
1097 }
1098
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001099 results->timeout = timeout;
1100
1101 if (mul) {
1102 float m = atof(mul);
1103
1104 if (m < 1)
1105 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1106
1107 results->timeout = results->timeout * m + 0.5;
1108 }
1109
1110 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1111 results->timeout/3600, (results->timeout%3600)/60,
1112 results->timeout % 60);
1113
1114 if (getpid() == lib_pid)
1115 alarm(results->timeout);
1116 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001117 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001118}
1119
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001120static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001121{
1122 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001123
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001124 if (tst_test->timeout)
1125 tst_set_timeout(tst_test->timeout);
1126 else
1127 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001128
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001129 SAFE_SIGNAL(SIGINT, sigint_handler);
1130
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001131 test_pid = fork();
1132 if (test_pid < 0)
1133 tst_brk(TBROK | TERRNO, "fork()");
1134
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001135 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001136 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1137 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1138 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001139 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001140 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001141 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001142
1143 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001144 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001145 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001146
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001147 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001148 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001149
1150 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1151 tst_res(TINFO, "If you are running on slow machine, "
1152 "try exporting LTP_TIMEOUT_MUL > 1");
1153 tst_brk(TBROK, "Test killed! (timeout?)");
1154 }
1155
1156 if (WIFSIGNALED(status))
1157 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1158
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001159 return 0;
1160}
1161
1162static int run_tcases_per_fs(void)
1163{
1164 int ret = 0;
1165 unsigned int i;
1166 const char *const *filesystems = tst_get_supported_fs_types();
1167
1168 if (!filesystems[0])
1169 tst_brk(TCONF, "There are no supported filesystems");
1170
1171 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001172
1173 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001174 tdev.fs_type = filesystems[i];
1175
1176 prepare_device();
1177
1178 ret = fork_testrun();
1179
1180 if (mntpoint_mounted) {
1181 tst_umount(tst_test->mntpoint);
1182 mntpoint_mounted = 0;
1183 }
1184
1185 if (ret == TCONF) {
1186 update_results(ret);
1187 continue;
1188 }
1189
1190 if (ret == 0)
1191 continue;
1192
1193 do_exit(ret);
1194 }
1195
1196 return ret;
1197}
1198
Cyril Hrubisce082162019-03-06 16:24:29 +01001199unsigned int tst_variant;
1200
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001201void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1202{
Cyril Hrubisce082162019-03-06 16:24:29 +01001203 int ret = 0;
1204 unsigned int test_variants = 1;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001205
1206 lib_pid = getpid();
1207 tst_test = self;
1208
1209 do_setup(argc, argv);
1210
1211 TCID = tid;
1212
1213 SAFE_SIGNAL(SIGALRM, alarm_handler);
1214 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1215
Cyril Hrubisce082162019-03-06 16:24:29 +01001216 if (tst_test->test_variants)
1217 test_variants = tst_test->test_variants;
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001218
Cyril Hrubisce082162019-03-06 16:24:29 +01001219 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1220 if (tst_test->all_filesystems)
1221 ret |= run_tcases_per_fs();
1222 else
1223 ret |= fork_testrun();
1224
1225 if (ret & ~(TCONF))
1226 goto exit;
1227 }
1228
1229exit:
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001230 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001231}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001232
1233
1234void tst_flush(void)
1235{
1236 int rval;
1237
1238 rval = fflush(stderr);
1239 if (rval != 0)
1240 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1241
1242 rval = fflush(stderr);
1243 if (rval != 0)
1244 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1245
1246}