blob: ba5eab01124279e88ce42644d122b5936c97f7d7 [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;
Jan Stancek1893e012018-08-28 16:17:52 +020053static struct timespec tst_start_time; /* valid only for test pid */
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010054
55struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020056 int passed;
57 int skipped;
58 int failed;
59 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020060 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010061};
62
63static struct results *results;
64
65static int ipc_fd;
66
67extern void *tst_futexes;
68extern unsigned int tst_max_futexes;
69
70#define IPC_ENV_VAR "LTP_IPC_PATH"
71
72static char ipc_path[1024];
73const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010074
75static char shm_path[1024];
76
Christian Lanig18b780d2018-07-23 15:24:50 +020077int TST_ERR;
78long TST_RET;
79
Jan Stancek332540e2016-06-08 16:48:22 +020080static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020081static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020082
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010083static void setup_ipc(void)
84{
85 size_t size = getpagesize();
86
Steven Jackson9f41dcf2016-12-21 20:09:01 +000087 if (access("/dev/shm", F_OK) == 0) {
88 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080089 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000090 } else {
91 char *tmpdir;
92
93 if (!tst_tmpdir_created())
94 tst_tmpdir();
95
96 tmpdir = tst_get_tmpdir();
97 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080098 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000099 free(tmpdir);
100 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100101
102 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
103 if (ipc_fd < 0)
104 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +0200105 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100106
107 SAFE_FTRUNCATE(ipc_fd, size);
108
109 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
110
111 /* Checkpoints needs to be accessible from processes started by exec() */
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200112 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100113 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200114 putenv(ipc_path);
115 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100116 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200117 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100118
119 SAFE_CLOSE(ipc_fd);
120
121 if (tst_test->needs_checkpoints) {
122 tst_futexes = (char*)results + sizeof(struct results);
123 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
124 }
125}
126
127static void cleanup_ipc(void)
128{
129 size_t size = getpagesize();
130
131 if (ipc_fd > 0 && close(ipc_fd))
132 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
133
Cyril Hrubis9726b902017-07-27 11:28:50 +0200134 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100135 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
136
Cyril Hrubis9726b902017-07-27 11:28:50 +0200137 if (results) {
138 msync((void*)results, size, MS_SYNC);
139 munmap((void*)results, size);
140 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100141}
142
143void tst_reinit(void)
144{
Jan Stancek03fc5372017-10-10 13:36:59 +0200145 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100146 size_t size = getpagesize();
147 int fd;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100148
149 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200150 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100151
152 if (access(path, F_OK))
153 tst_brk(TBROK, "File %s does not exist!", path);
154
155 fd = SAFE_OPEN(path, O_RDWR);
156
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200157 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
158 tst_futexes = (char*)results + sizeof(struct results);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100159 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
160
161 SAFE_CLOSE(fd);
162}
163
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100164static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100165{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100166 if (!results)
167 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100168
169 switch (ttype) {
170 case TCONF:
171 tst_atomic_inc(&results->skipped);
172 break;
173 case TPASS:
174 tst_atomic_inc(&results->passed);
175 break;
176 case TWARN:
177 tst_atomic_inc(&results->warnings);
178 break;
179 case TFAIL:
180 tst_atomic_inc(&results->failed);
181 break;
182 }
183}
184
185static void print_result(const char *file, const int lineno, int ttype,
186 const char *fmt, va_list va)
187{
188 char buf[1024];
189 char *str = buf;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100190 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100191 const char *str_errno = NULL;
192 const char *res;
193
194 switch (TTYPE_RESULT(ttype)) {
195 case TPASS:
196 res = "PASS";
197 break;
198 case TFAIL:
199 res = "FAIL";
200 break;
201 case TBROK:
202 res = "BROK";
203 break;
204 case TCONF:
205 res = "CONF";
206 break;
207 case TWARN:
208 res = "WARN";
209 break;
210 case TINFO:
211 res = "INFO";
212 break;
213 default:
214 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100215 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100216 }
217
218 if (ttype & TERRNO)
219 str_errno = tst_strerrno(errno);
220
221 if (ttype & TTERRNO)
Christian Lanig18b780d2018-07-23 15:24:50 +0200222 str_errno = tst_strerrno(TST_ERR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100223
Richard Palethorpefb32e892018-03-13 16:24:47 +0100224 if (ttype & TRERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200225 ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
Richard Palethorpefb32e892018-03-13 16:24:47 +0100226 str_errno = tst_strerrno(ret);
227 }
228
Petr Vorela7f61332017-01-24 20:47:30 +0100229 ret = snprintf(str, size, "%s:%i: ", file, lineno);
230 str += ret;
231 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100232
Cyril Hrubis047c7272017-02-13 16:23:36 +0100233 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100234 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
235 res, ANSI_COLOR_RESET);
236 else
237 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100238 str += ret;
239 size -= ret;
240
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100241 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100242 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100243 str += MIN(ret, ssize);
244 size -= MIN(ret, ssize);
245 if (ret >= ssize) {
246 tst_res_(file, lineno, TWARN,
247 "Next message is too long and truncated:");
248 } else if (str_errno) {
249 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100250 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100251 str += MIN(ret, ssize);
252 size -= MIN(ret, ssize);
253 if (ret >= ssize)
254 tst_res_(file, lineno, TWARN,
255 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100256 }
257
258 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100259
260 fputs(buf, stderr);
261}
262
263void tst_vres_(const char *file, const int lineno, int ttype,
264 const char *fmt, va_list va)
265{
266 print_result(file, lineno, ttype, fmt, va);
267
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100268 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100269}
270
271void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100272 const char *fmt, va_list va);
273
274static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
275 const char *fmt, va_list va) = tst_vbrk_;
276
277static void tst_cvres(const char *file, const int lineno, int ttype,
278 const char *fmt, va_list va)
279{
280 if (TTYPE_RESULT(ttype) == TBROK) {
281 ttype &= ~TTYPE_MASK;
282 ttype |= TWARN;
283 }
284
285 print_result(file, lineno, ttype, fmt, va);
286 update_results(TTYPE_RESULT(ttype));
287}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100288
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200289static void do_test_cleanup(void)
290{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100291 tst_brk_handler = tst_cvres;
292
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200293 if (tst_test->cleanup)
294 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100295
296 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
401 return pid;
402}
403
404static struct option {
405 char *optstr;
406 char *help;
407} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200408 {"h", "-h Prints this help"},
409 {"i:", "-i n Execute test n times"},
410 {"I:", "-I x Execute test for n seconds"},
411 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100412};
413
414static void print_help(void)
415{
416 unsigned int i;
417
418 for (i = 0; i < ARRAY_SIZE(options); i++)
419 fprintf(stderr, "%s\n", options[i].help);
420
421 if (!tst_test->options)
422 return;
423
424 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200425 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100426}
427
428static void check_option_collision(void)
429{
430 unsigned int i, j;
431 struct tst_option *toptions = tst_test->options;
432
433 if (!toptions)
434 return;
435
436 for (i = 0; toptions[i].optstr; i++) {
437 for (j = 0; j < ARRAY_SIZE(options); j++) {
438 if (toptions[i].optstr[0] == options[j].optstr[0]) {
439 tst_brk(TBROK, "Option collision '%s'",
440 options[j].help);
441 }
442 }
443 }
444}
445
446static unsigned int count_options(void)
447{
448 unsigned int i;
449
450 if (!tst_test->options)
451 return 0;
452
453 for (i = 0; tst_test->options[i].optstr; i++);
454
455 return i;
456}
457
458static void parse_topt(unsigned int topts_len, int opt, char *optarg)
459{
460 unsigned int i;
461 struct tst_option *toptions = tst_test->options;
462
463 for (i = 0; i < topts_len; i++) {
464 if (toptions[i].optstr[0] == opt)
465 break;
466 }
467
468 if (i >= topts_len)
469 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
470
Jan Stancekc07d36c2016-08-01 11:44:55 +0200471 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100472}
473
474/* see self_exec.c */
475#ifdef UCLINUX
476extern char *child_args;
477#endif
478
479static void parse_opts(int argc, char *argv[])
480{
481 unsigned int i, topts_len = count_options();
482 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
483 int opt;
484
485 check_option_collision();
486
487 optstr[0] = 0;
488
489 for (i = 0; i < ARRAY_SIZE(options); i++)
490 strcat(optstr, options[i].optstr);
491
492 for (i = 0; i < topts_len; i++)
493 strcat(optstr, tst_test->options[i].optstr);
494
495 while ((opt = getopt(argc, argv, optstr)) > 0) {
496 switch (opt) {
497 case '?':
498 print_help();
499 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200500 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100501 case 'h':
502 print_help();
503 exit(0);
504 case 'i':
505 iterations = atoi(optarg);
506 break;
507 case 'I':
508 duration = atof(optarg);
509 break;
510 case 'C':
511#ifdef UCLINUX
512 child_args = optarg;
513#endif
514 break;
515 default:
516 parse_topt(topts_len, opt, optarg);
517 }
518 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200519
520 if (optind < argc)
521 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100522}
523
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200524int tst_parse_int(const char *str, int *val, int min, int max)
525{
526 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300527
528 if (!str)
529 return 0;
530
531 int ret = tst_parse_long(str, &rval, min, max);
532
533 if (ret)
534 return ret;
535
536 *val = (int)rval;
537 return 0;
538}
539
540int tst_parse_long(const char *str, long *val, long min, long max)
541{
542 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200543 char *end;
544
545 if (!str)
546 return 0;
547
548 errno = 0;
549 rval = strtol(str, &end, 10);
550
551 if (str == end || *end != '\0')
552 return EINVAL;
553
554 if (errno)
555 return errno;
556
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300557 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200558 return ERANGE;
559
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300560 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200561 return 0;
562}
563
564int tst_parse_float(const char *str, float *val, float min, float max)
565{
566 double rval;
567 char *end;
568
569 if (!str)
570 return 0;
571
572 errno = 0;
573 rval = strtod(str, &end);
574
575 if (str == end || *end != '\0')
576 return EINVAL;
577
578 if (errno)
579 return errno;
580
581 if (rval > (double)max || rval < (double)min)
582 return ERANGE;
583
584 *val = (float)rval;
585 return 0;
586}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100587
Cyril Hrubisfa495172016-06-08 16:06:35 +0200588static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100589{
Xiao Yang11dfc322016-06-16 15:52:04 +0800590 if (results) {
591 printf("\nSummary:\n");
592 printf("passed %d\n", results->passed);
593 printf("failed %d\n", results->failed);
594 printf("skipped %d\n", results->skipped);
595 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100596
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100597 if (results->passed && ret == TCONF)
598 ret = 0;
599
Xiao Yang11dfc322016-06-16 15:52:04 +0800600 if (results->failed)
601 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100602
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200603 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800604 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100605
Xiao Yang11dfc322016-06-16 15:52:04 +0800606 if (results->warnings)
607 ret |= TWARN;
608 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100609
Jan Stancek332540e2016-06-08 16:48:22 +0200610 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100611
612 exit(ret);
613}
614
615void check_kver(void)
616{
617 int v1, v2, v3;
618
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100619 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
620 tst_res(TWARN,
621 "Invalid kernel version %s, expected %%d.%%d.%%d",
622 tst_test->min_kver);
623 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100624
625 if (tst_kvercmp(v1, v2, v3) < 0) {
626 tst_brk(TCONF, "The test requires kernel %s or newer",
627 tst_test->min_kver);
628 }
629}
630
631static int results_equal(struct results *a, struct results *b)
632{
633 if (a->passed != b->passed)
634 return 0;
635
636 if (a->failed != b->failed)
637 return 0;
638
639 if (a->skipped != b->skipped)
640 return 0;
641
642 return 1;
643}
644
645static int needs_tmpdir(void)
646{
647 return tst_test->needs_tmpdir ||
648 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200649 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100650 tst_test->resource_files ||
651 tst_test->needs_checkpoints;
652}
653
654static void copy_resources(void)
655{
656 unsigned int i;
657
658 for (i = 0; tst_test->resource_files[i]; i++)
659 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
660}
661
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800662static const char *get_tid(char *argv[])
663{
664 char *p;
665
666 if (!argv[0] || !argv[0][0]) {
667 tst_res(TINFO, "argv[0] is empty!");
668 return "ltp_empty_argv";
669 }
670
671 p = strrchr(argv[0], '/');
672 if (p)
673 return p+1;
674
675 return argv[0];
676}
677
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100678static struct tst_device tdev;
679struct tst_device *tst_device;
680
Cyril Hrubisc4596542017-06-20 15:42:18 +0200681static void assert_test_fn(void)
682{
683 int cnt = 0;
684
685 if (tst_test->test)
686 cnt++;
687
688 if (tst_test->test_all)
689 cnt++;
690
691 if (tst_test->sample)
692 cnt++;
693
694 if (!cnt)
695 tst_brk(TBROK, "No test function speficied");
696
697 if (cnt != 1)
698 tst_brk(TBROK, "You can define only one test function");
699
700 if (tst_test->test && !tst_test->tcnt)
Eric Biggers3614a162019-03-15 09:42:15 -0700701 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
Cyril Hrubisc4596542017-06-20 15:42:18 +0200702
703 if (!tst_test->test && tst_test->tcnt)
704 tst_brk(TBROK, "You can define tcnt only for test()");
705}
706
Cyril Hrubise6da8672018-04-05 16:01:48 +0200707static int prepare_and_mount_ro_fs(const char *dev,
708 const char *mntpoint,
709 const char *fs_type)
710{
711 char buf[PATH_MAX];
712
713 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
714 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
715 dev, mntpoint, fs_type);
716 return 1;
717 }
718
719 mntpoint_mounted = 1;
720
721 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
722 SAFE_MKDIR(buf, 0777);
723
724 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
725 SAFE_FILE_PRINTF(buf, "file content");
726 SAFE_CHMOD(buf, 0777);
727
728 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
729
730 return 0;
731}
732
Xiao Yang7313a872018-09-01 12:07:41 +0800733static void prepare_and_mount_dev_fs(const char *mntpoint)
734{
735 const char *flags[] = {"nodev", NULL};
736 int mounted_nodev;
737
738 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
739 if (mounted_nodev) {
740 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
741 "mounting tmpfs without nodev on %s", mntpoint);
742 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
743 mntpoint_mounted = 1;
744 }
745}
746
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200747static void prepare_device(void)
748{
749 if (tst_test->format_device) {
750 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200751 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200752 }
753
Cyril Hrubise6da8672018-04-05 16:01:48 +0200754 if (tst_test->needs_rofs) {
755 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
756 tdev.fs_type);
757 return;
758 }
759
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200760 if (tst_test->mount_device) {
761 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
762 tst_test->mnt_flags, tst_test->mnt_data);
763 mntpoint_mounted = 1;
764 }
765}
766
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100767static void do_setup(int argc, char *argv[])
768{
769 if (!tst_test)
770 tst_brk(TBROK, "No tests to run");
771
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200772 if (tst_test->tconf_msg)
773 tst_brk(TCONF, "%s", tst_test->tconf_msg);
774
Cyril Hrubisf023a612018-11-27 14:21:17 +0100775 if (tst_test->needs_kconfigs)
776 tst_kconfig_check(tst_test->needs_kconfigs);
777
Cyril Hrubisc4596542017-06-20 15:42:18 +0200778 assert_test_fn();
779
Li Wangb5d620a2017-11-01 13:15:23 +0800780 tid = get_tid(argv);
781
Cyril Hrubisc4596542017-06-20 15:42:18 +0200782 if (tst_test->sample)
783 tst_test = tst_timer_test_setup(tst_test);
784
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200785 parse_opts(argc, argv);
786
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100787 if (tst_test->needs_root && geteuid() != 0)
788 tst_brk(TCONF, "Test needs to be run as root");
789
790 if (tst_test->min_kver)
791 check_kver();
792
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300793 if (tst_test->needs_drivers) {
794 const char *name;
795 int i;
796
797 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
798 if (tst_check_driver(name))
799 tst_brk(TCONF, "%s driver not available", name);
800 }
801
Cyril Hrubis874326d2017-02-14 15:59:40 +0100802 if (tst_test->format_device)
803 tst_test->needs_device = 1;
804
805 if (tst_test->mount_device) {
806 tst_test->needs_device = 1;
807 tst_test->format_device = 1;
808 }
809
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200810 if (tst_test->all_filesystems)
811 tst_test->needs_device = 1;
812
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100813 setup_ipc();
814
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000815 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100816 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100817
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100818 if (tst_test->save_restore) {
819 const char * const *name = tst_test->save_restore;
820
821 while (*name) {
822 tst_sys_conf_save(*name);
823 name++;
824 }
825 }
826
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700827 if (tst_test->mntpoint)
828 SAFE_MKDIR(tst_test->mntpoint, 0777);
829
Xiao Yang7313a872018-09-01 12:07:41 +0800830 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
831 tst_test->mount_device || tst_test->all_filesystems) &&
832 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700833 tst_brk(TBROK, "tst_test->mntpoint must be set!");
834 }
835
Xiao Yang7313a872018-09-01 12:07:41 +0800836 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
837 !!tst_test->needs_device > 1) {
838 tst_brk(TBROK,
839 "Two or more of needs_{rofs, devfs, device} are set");
840 }
841
842 if (tst_test->needs_devfs)
843 prepare_and_mount_dev_fs(tst_test->mntpoint);
844
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700845 if (tst_test->needs_rofs) {
846 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200847 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700848 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200849 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
850 tst_res(TINFO, "Can't mount tmpfs read-only, "
851 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700852 tst_test->needs_device = 1;
853 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700854 }
855 }
856
857 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100858 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100859
860 if (!tdev.dev)
861 tst_brk(TCONF, "Failed to acquire device");
862
863 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100864
865 if (tst_test->dev_fs_type)
866 tdev.fs_type = tst_test->dev_fs_type;
867 else
868 tdev.fs_type = tst_dev_fs_type();
869
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200870 if (!tst_test->all_filesystems)
871 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100872 }
873
874 if (tst_test->resource_files)
875 copy_resources();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200876
877 if (tst_test->restore_wallclock)
878 tst_wallclock_save();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200879}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100880
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200881static void do_test_setup(void)
882{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100883 main_pid = getpid();
884
885 if (tst_test->setup)
886 tst_test->setup();
887
888 if (main_pid != getpid())
889 tst_brk(TBROK, "Runaway child in setup()!");
890}
891
892static void do_cleanup(void)
893{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700894 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100895 tst_umount(tst_test->mntpoint);
896
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100897 if (tst_test->needs_device && tdev.dev)
898 tst_release_device(tdev.dev);
899
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000900 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100901 /* avoid munmap() on wrong pointer in tst_rmdir() */
902 tst_futexes = NULL;
903 tst_rmdir();
904 }
Jan Stancek332540e2016-06-08 16:48:22 +0200905
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100906 if (tst_test->save_restore)
907 tst_sys_conf_restore(0);
908
Jan Stancek332540e2016-06-08 16:48:22 +0200909 cleanup_ipc();
Rafael David Tinoco14e43562019-01-29 15:36:55 -0200910
911 if (tst_test->restore_wallclock)
912 tst_wallclock_restore();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100913}
914
915static void run_tests(void)
916{
917 unsigned int i;
918 struct results saved_results;
919
920 if (!tst_test->test) {
921 saved_results = *results;
922 tst_test->test_all();
923
924 if (getpid() != main_pid) {
925 exit(0);
926 }
927
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300928 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100929
930 if (results_equal(&saved_results, results))
931 tst_brk(TBROK, "Test haven't reported results!");
932 return;
933 }
934
935 for (i = 0; i < tst_test->tcnt; i++) {
936 saved_results = *results;
937 tst_test->test(i);
938
939 if (getpid() != main_pid) {
940 exit(0);
941 }
942
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300943 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100944
945 if (results_equal(&saved_results, results))
946 tst_brk(TBROK, "Test %i haven't reported results!", i);
947 }
948}
949
950static unsigned long long get_time_ms(void)
951{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100952 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100953
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100954 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
955 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100956
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100957 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100958}
959
Jan Stancekb95b1992017-10-10 15:47:58 +0200960static void add_paths(void)
961{
962 char *old_path = getenv("PATH");
963 const char *start_dir;
964 char *new_path;
965
966 start_dir = tst_get_startwd();
967
968 if (old_path)
969 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
970 else
971 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
972
973 SAFE_SETENV("PATH", new_path, 1);
974 free(new_path);
975}
976
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200977static void heartbeat(void)
978{
Jan Stancek1893e012018-08-28 16:17:52 +0200979 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
980 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
981
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200982 kill(getppid(), SIGUSR1);
983}
984
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200985static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100986{
987 unsigned int i = 0;
988 unsigned long long stop_time = 0;
989 int cont = 1;
990
Jan Stancek1893e012018-08-28 16:17:52 +0200991 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +0200992 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200993 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100994
995 if (duration > 0)
996 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
997
998 for (;;) {
999 cont = 0;
1000
1001 if (i < (unsigned int)iterations) {
1002 i++;
1003 cont = 1;
1004 }
1005
1006 if (stop_time && get_time_ms() < stop_time)
1007 cont = 1;
1008
1009 if (!cont)
1010 break;
1011
1012 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001013 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001014 }
1015
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001016 do_test_cleanup();
1017 exit(0);
1018}
1019
1020static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001021
Cyril Hrubis79163172017-05-18 10:57:07 +02001022
1023static volatile sig_atomic_t sigkill_retries;
1024
1025#define WRITE_MSG(msg) do { \
1026 if (write(2, msg, sizeof(msg) - 1)) { \
1027 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1028 } \
1029} while (0)
1030
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001031static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1032{
Cyril Hrubis79163172017-05-18 10:57:07 +02001033 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001034 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001035 alarm(5);
1036
1037 if (++sigkill_retries > 10) {
1038 WRITE_MSG("Cannot kill test processes!\n");
1039 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1040 WRITE_MSG("Exitting uncleanly...\n");
1041 _exit(TFAIL);
1042 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001043}
1044
1045static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1046{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001047 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001048 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001049}
1050
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001051static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1052{
1053 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001054 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001055 kill(-test_pid, SIGKILL);
1056 }
1057}
1058
Jan Stancek1893e012018-08-28 16:17:52 +02001059unsigned int tst_timeout_remaining(void)
1060{
1061 static struct timespec now;
1062 unsigned int elapsed;
1063
1064 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1065 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1066
1067 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1068 if (results->timeout > elapsed)
1069 return results->timeout - elapsed;
1070
1071 return 0;
1072}
1073
Li Wang94823cf2017-07-18 16:23:00 +08001074void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001075{
1076 char *mul = getenv("LTP_TIMEOUT_MUL");
1077
Li Wang94823cf2017-07-18 16:23:00 +08001078 if (timeout == -1) {
1079 tst_res(TINFO, "Timeout per run is disabled");
1080 return;
1081 }
1082
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001083 results->timeout = timeout;
1084
1085 if (mul) {
1086 float m = atof(mul);
1087
1088 if (m < 1)
1089 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1090
1091 results->timeout = results->timeout * m + 0.5;
1092 }
1093
1094 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1095 results->timeout/3600, (results->timeout%3600)/60,
1096 results->timeout % 60);
1097
1098 if (getpid() == lib_pid)
1099 alarm(results->timeout);
1100 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001101 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001102}
1103
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001104static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001105{
1106 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001107
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001108 if (tst_test->timeout)
1109 tst_set_timeout(tst_test->timeout);
1110 else
1111 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001112
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001113 SAFE_SIGNAL(SIGINT, sigint_handler);
1114
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001115 test_pid = fork();
1116 if (test_pid < 0)
1117 tst_brk(TBROK | TERRNO, "fork()");
1118
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001119 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001120 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1121 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1122 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001123 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001124 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001125 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001126
1127 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001128 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001129 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001130
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001131 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001132 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001133
1134 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1135 tst_res(TINFO, "If you are running on slow machine, "
1136 "try exporting LTP_TIMEOUT_MUL > 1");
1137 tst_brk(TBROK, "Test killed! (timeout?)");
1138 }
1139
1140 if (WIFSIGNALED(status))
1141 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1142
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001143 return 0;
1144}
1145
1146static int run_tcases_per_fs(void)
1147{
1148 int ret = 0;
1149 unsigned int i;
1150 const char *const *filesystems = tst_get_supported_fs_types();
1151
1152 if (!filesystems[0])
1153 tst_brk(TCONF, "There are no supported filesystems");
1154
1155 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001156
1157 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001158 tdev.fs_type = filesystems[i];
1159
1160 prepare_device();
1161
1162 ret = fork_testrun();
1163
1164 if (mntpoint_mounted) {
1165 tst_umount(tst_test->mntpoint);
1166 mntpoint_mounted = 0;
1167 }
1168
1169 if (ret == TCONF) {
1170 update_results(ret);
1171 continue;
1172 }
1173
1174 if (ret == 0)
1175 continue;
1176
1177 do_exit(ret);
1178 }
1179
1180 return ret;
1181}
1182
1183void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1184{
1185 int ret;
1186
1187 lib_pid = getpid();
1188 tst_test = self;
1189
1190 do_setup(argc, argv);
1191
1192 TCID = tid;
1193
1194 SAFE_SIGNAL(SIGALRM, alarm_handler);
1195 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1196
1197 if (tst_test->all_filesystems)
1198 ret = run_tcases_per_fs();
1199 else
1200 ret = fork_testrun();
1201
1202 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001203}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001204
1205
1206void tst_flush(void)
1207{
1208 int rval;
1209
1210 rval = fflush(stderr);
1211 if (rval != 0)
1212 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1213
1214 rval = fflush(stderr);
1215 if (rval != 0)
1216 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1217
1218}