blob: da3e0c8a07c51c12a286072f93d40aa94afa46a5 [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"
Jan Stancek9dcbc6d2018-11-05 09:00:02 +010038#include "tst_sys_conf.h"
Cyril Hrubisf023a612018-11-27 14:21:17 +010039#include "tst_kconfig.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010040
41#include "old_resource.h"
42#include "old_device.h"
43#include "old_tmpdir.h"
44
45struct tst_test *tst_test;
46
Li Wangb5d620a2017-11-01 13:15:23 +080047static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010048static int iterations = 1;
49static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020050static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070051static int mntpoint_mounted;
Jan Stancek1893e012018-08-28 16:17:52 +020052static struct timespec tst_start_time; /* valid only for test pid */
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010053
54struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020055 int passed;
56 int skipped;
57 int failed;
58 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020059 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010060};
61
62static struct results *results;
63
64static int ipc_fd;
65
66extern void *tst_futexes;
67extern unsigned int tst_max_futexes;
68
69#define IPC_ENV_VAR "LTP_IPC_PATH"
70
71static char ipc_path[1024];
72const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010073
74static char shm_path[1024];
75
Christian Lanig18b780d2018-07-23 15:24:50 +020076int TST_ERR;
77long TST_RET;
78
Jan Stancek332540e2016-06-08 16:48:22 +020079static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020080static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020081
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010082static void setup_ipc(void)
83{
84 size_t size = getpagesize();
85
Steven Jackson9f41dcf2016-12-21 20:09:01 +000086 if (access("/dev/shm", F_OK) == 0) {
87 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080088 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000089 } else {
90 char *tmpdir;
91
92 if (!tst_tmpdir_created())
93 tst_tmpdir();
94
95 tmpdir = tst_get_tmpdir();
96 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080097 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000098 free(tmpdir);
99 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100100
101 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
102 if (ipc_fd < 0)
103 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +0200104 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100105
106 SAFE_FTRUNCATE(ipc_fd, size);
107
108 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
109
110 /* Checkpoints needs to be accessible from processes started by exec() */
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200111 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100112 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200113 putenv(ipc_path);
114 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100115 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200116 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100117
118 SAFE_CLOSE(ipc_fd);
119
120 if (tst_test->needs_checkpoints) {
121 tst_futexes = (char*)results + sizeof(struct results);
122 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
123 }
124}
125
126static void cleanup_ipc(void)
127{
128 size_t size = getpagesize();
129
130 if (ipc_fd > 0 && close(ipc_fd))
131 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
132
Cyril Hrubis9726b902017-07-27 11:28:50 +0200133 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100134 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
135
Cyril Hrubis9726b902017-07-27 11:28:50 +0200136 if (results) {
137 msync((void*)results, size, MS_SYNC);
138 munmap((void*)results, size);
139 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100140}
141
142void tst_reinit(void)
143{
Jan Stancek03fc5372017-10-10 13:36:59 +0200144 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100145 size_t size = getpagesize();
146 int fd;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100147
148 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200149 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100150
151 if (access(path, F_OK))
152 tst_brk(TBROK, "File %s does not exist!", path);
153
154 fd = SAFE_OPEN(path, O_RDWR);
155
Cyril Hrubis9e5a0762018-07-31 15:03:21 +0200156 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
157 tst_futexes = (char*)results + sizeof(struct results);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100158 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
159
160 SAFE_CLOSE(fd);
161}
162
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100163static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100164{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100165 if (!results)
166 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100167
168 switch (ttype) {
169 case TCONF:
170 tst_atomic_inc(&results->skipped);
171 break;
172 case TPASS:
173 tst_atomic_inc(&results->passed);
174 break;
175 case TWARN:
176 tst_atomic_inc(&results->warnings);
177 break;
178 case TFAIL:
179 tst_atomic_inc(&results->failed);
180 break;
181 }
182}
183
184static void print_result(const char *file, const int lineno, int ttype,
185 const char *fmt, va_list va)
186{
187 char buf[1024];
188 char *str = buf;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100189 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100190 const char *str_errno = NULL;
191 const char *res;
192
193 switch (TTYPE_RESULT(ttype)) {
194 case TPASS:
195 res = "PASS";
196 break;
197 case TFAIL:
198 res = "FAIL";
199 break;
200 case TBROK:
201 res = "BROK";
202 break;
203 case TCONF:
204 res = "CONF";
205 break;
206 case TWARN:
207 res = "WARN";
208 break;
209 case TINFO:
210 res = "INFO";
211 break;
212 default:
213 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100214 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100215 }
216
217 if (ttype & TERRNO)
218 str_errno = tst_strerrno(errno);
219
220 if (ttype & TTERRNO)
Christian Lanig18b780d2018-07-23 15:24:50 +0200221 str_errno = tst_strerrno(TST_ERR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100222
Richard Palethorpefb32e892018-03-13 16:24:47 +0100223 if (ttype & TRERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200224 ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
Richard Palethorpefb32e892018-03-13 16:24:47 +0100225 str_errno = tst_strerrno(ret);
226 }
227
Petr Vorela7f61332017-01-24 20:47:30 +0100228 ret = snprintf(str, size, "%s:%i: ", file, lineno);
229 str += ret;
230 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100231
Cyril Hrubis047c7272017-02-13 16:23:36 +0100232 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100233 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
234 res, ANSI_COLOR_RESET);
235 else
236 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100237 str += ret;
238 size -= ret;
239
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100240 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100241 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100242 str += MIN(ret, ssize);
243 size -= MIN(ret, ssize);
244 if (ret >= ssize) {
245 tst_res_(file, lineno, TWARN,
246 "Next message is too long and truncated:");
247 } else if (str_errno) {
248 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100249 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100250 str += MIN(ret, ssize);
251 size -= MIN(ret, ssize);
252 if (ret >= ssize)
253 tst_res_(file, lineno, TWARN,
254 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100255 }
256
257 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100258
259 fputs(buf, stderr);
260}
261
262void tst_vres_(const char *file, const int lineno, int ttype,
263 const char *fmt, va_list va)
264{
265 print_result(file, lineno, ttype, fmt, va);
266
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100267 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100268}
269
270void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100271 const char *fmt, va_list va);
272
273static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
274 const char *fmt, va_list va) = tst_vbrk_;
275
276static void tst_cvres(const char *file, const int lineno, int ttype,
277 const char *fmt, va_list va)
278{
279 if (TTYPE_RESULT(ttype) == TBROK) {
280 ttype &= ~TTYPE_MASK;
281 ttype |= TWARN;
282 }
283
284 print_result(file, lineno, ttype, fmt, va);
285 update_results(TTYPE_RESULT(ttype));
286}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100287
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200288static void do_test_cleanup(void)
289{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100290 tst_brk_handler = tst_cvres;
291
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200292 if (tst_test->cleanup)
293 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100294
295 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200296}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100297
298void tst_vbrk_(const char *file, const int lineno, int ttype,
299 const char *fmt, va_list va)
300{
301 print_result(file, lineno, ttype, fmt, va);
302
Steve Mucklec20831d2017-09-20 13:23:06 -0700303 /*
304 * The getpid implementation in some C library versions may cause cloned
305 * test threads to show the same pid as their parent when CLONE_VM is
306 * specified but CLONE_THREAD is not. Use direct syscall to avoid
307 * cleanup running in the child.
308 */
309 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200310 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100311
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200312 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200313 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200314
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100315 exit(TTYPE_RESULT(ttype));
316}
317
318void tst_res_(const char *file, const int lineno, int ttype,
319 const char *fmt, ...)
320{
321 va_list va;
322
323 va_start(va, fmt);
324 tst_vres_(file, lineno, ttype, fmt, va);
325 va_end(va);
326}
327
328void tst_brk_(const char *file, const int lineno, int ttype,
329 const char *fmt, ...)
330{
331 va_list va;
332
333 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100334 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100335 va_end(va);
336}
337
338static void check_child_status(pid_t pid, int status)
339{
340 int ret;
341
342 if (WIFSIGNALED(status)) {
343 tst_brk(TBROK, "Child (%i) killed by signal %s",
344 pid, tst_strsig(WTERMSIG(status)));
345 }
346
347 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100348 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100349
350 ret = WEXITSTATUS(status);
351 switch (ret) {
352 case TPASS:
353 break;
354 case TBROK:
355 case TCONF:
356 tst_brk(ret, "Reported by child (%i)", pid);
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200357 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100358 default:
359 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
360 }
361}
362
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300363void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100364{
365 int status;
366 pid_t pid;
367
368 for (;;) {
369 pid = wait(&status);
370
371 if (pid > 0) {
372 check_child_status(pid, status);
373 continue;
374 }
375
376 if (errno == ECHILD)
377 break;
378
379 if (errno == EINTR)
380 continue;
381
382 tst_brk(TBROK | TERRNO, "wait() failed");
383 }
384}
385
386
387pid_t safe_fork(const char *filename, unsigned int lineno)
388{
389 pid_t pid;
390
391 if (!tst_test->forks_child)
392 tst_brk(TBROK, "test.forks_child must be set!");
393
Michael Moese9da42372018-03-09 15:16:09 +0100394 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100395
396 pid = fork();
397 if (pid < 0)
398 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
399
400 return pid;
401}
402
403static struct option {
404 char *optstr;
405 char *help;
406} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200407 {"h", "-h Prints this help"},
408 {"i:", "-i n Execute test n times"},
409 {"I:", "-I x Execute test for n seconds"},
410 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100411};
412
413static void print_help(void)
414{
415 unsigned int i;
416
417 for (i = 0; i < ARRAY_SIZE(options); i++)
418 fprintf(stderr, "%s\n", options[i].help);
419
420 if (!tst_test->options)
421 return;
422
423 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200424 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100425}
426
427static void check_option_collision(void)
428{
429 unsigned int i, j;
430 struct tst_option *toptions = tst_test->options;
431
432 if (!toptions)
433 return;
434
435 for (i = 0; toptions[i].optstr; i++) {
436 for (j = 0; j < ARRAY_SIZE(options); j++) {
437 if (toptions[i].optstr[0] == options[j].optstr[0]) {
438 tst_brk(TBROK, "Option collision '%s'",
439 options[j].help);
440 }
441 }
442 }
443}
444
445static unsigned int count_options(void)
446{
447 unsigned int i;
448
449 if (!tst_test->options)
450 return 0;
451
452 for (i = 0; tst_test->options[i].optstr; i++);
453
454 return i;
455}
456
457static void parse_topt(unsigned int topts_len, int opt, char *optarg)
458{
459 unsigned int i;
460 struct tst_option *toptions = tst_test->options;
461
462 for (i = 0; i < topts_len; i++) {
463 if (toptions[i].optstr[0] == opt)
464 break;
465 }
466
467 if (i >= topts_len)
468 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
469
Jan Stancekc07d36c2016-08-01 11:44:55 +0200470 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100471}
472
473/* see self_exec.c */
474#ifdef UCLINUX
475extern char *child_args;
476#endif
477
478static void parse_opts(int argc, char *argv[])
479{
480 unsigned int i, topts_len = count_options();
481 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
482 int opt;
483
484 check_option_collision();
485
486 optstr[0] = 0;
487
488 for (i = 0; i < ARRAY_SIZE(options); i++)
489 strcat(optstr, options[i].optstr);
490
491 for (i = 0; i < topts_len; i++)
492 strcat(optstr, tst_test->options[i].optstr);
493
494 while ((opt = getopt(argc, argv, optstr)) > 0) {
495 switch (opt) {
496 case '?':
497 print_help();
498 tst_brk(TBROK, "Invalid option");
Cyril Hrubis45cd37f2018-08-31 14:25:28 +0200499 break;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100500 case 'h':
501 print_help();
502 exit(0);
503 case 'i':
504 iterations = atoi(optarg);
505 break;
506 case 'I':
507 duration = atof(optarg);
508 break;
509 case 'C':
510#ifdef UCLINUX
511 child_args = optarg;
512#endif
513 break;
514 default:
515 parse_topt(topts_len, opt, optarg);
516 }
517 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200518
519 if (optind < argc)
520 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100521}
522
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200523int tst_parse_int(const char *str, int *val, int min, int max)
524{
525 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300526
527 if (!str)
528 return 0;
529
530 int ret = tst_parse_long(str, &rval, min, max);
531
532 if (ret)
533 return ret;
534
535 *val = (int)rval;
536 return 0;
537}
538
539int tst_parse_long(const char *str, long *val, long min, long max)
540{
541 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200542 char *end;
543
544 if (!str)
545 return 0;
546
547 errno = 0;
548 rval = strtol(str, &end, 10);
549
550 if (str == end || *end != '\0')
551 return EINVAL;
552
553 if (errno)
554 return errno;
555
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300556 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200557 return ERANGE;
558
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300559 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200560 return 0;
561}
562
563int tst_parse_float(const char *str, float *val, float min, float max)
564{
565 double rval;
566 char *end;
567
568 if (!str)
569 return 0;
570
571 errno = 0;
572 rval = strtod(str, &end);
573
574 if (str == end || *end != '\0')
575 return EINVAL;
576
577 if (errno)
578 return errno;
579
580 if (rval > (double)max || rval < (double)min)
581 return ERANGE;
582
583 *val = (float)rval;
584 return 0;
585}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100586
Cyril Hrubisfa495172016-06-08 16:06:35 +0200587static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100588{
Xiao Yang11dfc322016-06-16 15:52:04 +0800589 if (results) {
590 printf("\nSummary:\n");
591 printf("passed %d\n", results->passed);
592 printf("failed %d\n", results->failed);
593 printf("skipped %d\n", results->skipped);
594 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100595
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100596 if (results->passed && ret == TCONF)
597 ret = 0;
598
Xiao Yang11dfc322016-06-16 15:52:04 +0800599 if (results->failed)
600 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100601
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200602 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800603 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100604
Xiao Yang11dfc322016-06-16 15:52:04 +0800605 if (results->warnings)
606 ret |= TWARN;
607 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100608
Jan Stancek332540e2016-06-08 16:48:22 +0200609 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100610
611 exit(ret);
612}
613
614void check_kver(void)
615{
616 int v1, v2, v3;
617
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100618 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
619 tst_res(TWARN,
620 "Invalid kernel version %s, expected %%d.%%d.%%d",
621 tst_test->min_kver);
622 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100623
624 if (tst_kvercmp(v1, v2, v3) < 0) {
625 tst_brk(TCONF, "The test requires kernel %s or newer",
626 tst_test->min_kver);
627 }
628}
629
630static int results_equal(struct results *a, struct results *b)
631{
632 if (a->passed != b->passed)
633 return 0;
634
635 if (a->failed != b->failed)
636 return 0;
637
638 if (a->skipped != b->skipped)
639 return 0;
640
641 return 1;
642}
643
644static int needs_tmpdir(void)
645{
646 return tst_test->needs_tmpdir ||
647 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200648 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100649 tst_test->resource_files ||
650 tst_test->needs_checkpoints;
651}
652
653static void copy_resources(void)
654{
655 unsigned int i;
656
657 for (i = 0; tst_test->resource_files[i]; i++)
658 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
659}
660
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800661static const char *get_tid(char *argv[])
662{
663 char *p;
664
665 if (!argv[0] || !argv[0][0]) {
666 tst_res(TINFO, "argv[0] is empty!");
667 return "ltp_empty_argv";
668 }
669
670 p = strrchr(argv[0], '/');
671 if (p)
672 return p+1;
673
674 return argv[0];
675}
676
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100677static struct tst_device tdev;
678struct tst_device *tst_device;
679
Cyril Hrubisc4596542017-06-20 15:42:18 +0200680static void assert_test_fn(void)
681{
682 int cnt = 0;
683
684 if (tst_test->test)
685 cnt++;
686
687 if (tst_test->test_all)
688 cnt++;
689
690 if (tst_test->sample)
691 cnt++;
692
693 if (!cnt)
694 tst_brk(TBROK, "No test function speficied");
695
696 if (cnt != 1)
697 tst_brk(TBROK, "You can define only one test function");
698
699 if (tst_test->test && !tst_test->tcnt)
700 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
701
702 if (!tst_test->test && tst_test->tcnt)
703 tst_brk(TBROK, "You can define tcnt only for test()");
704}
705
Cyril Hrubise6da8672018-04-05 16:01:48 +0200706static int prepare_and_mount_ro_fs(const char *dev,
707 const char *mntpoint,
708 const char *fs_type)
709{
710 char buf[PATH_MAX];
711
712 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
713 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
714 dev, mntpoint, fs_type);
715 return 1;
716 }
717
718 mntpoint_mounted = 1;
719
720 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
721 SAFE_MKDIR(buf, 0777);
722
723 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
724 SAFE_FILE_PRINTF(buf, "file content");
725 SAFE_CHMOD(buf, 0777);
726
727 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
728
729 return 0;
730}
731
Xiao Yang7313a872018-09-01 12:07:41 +0800732static void prepare_and_mount_dev_fs(const char *mntpoint)
733{
734 const char *flags[] = {"nodev", NULL};
735 int mounted_nodev;
736
737 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
738 if (mounted_nodev) {
739 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
740 "mounting tmpfs without nodev on %s", mntpoint);
741 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
742 mntpoint_mounted = 1;
743 }
744}
745
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200746static void prepare_device(void)
747{
748 if (tst_test->format_device) {
749 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
Cyril Hrubisa6b0a922018-09-11 17:15:08 +0200750 tst_test->dev_extra_opts);
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200751 }
752
Cyril Hrubise6da8672018-04-05 16:01:48 +0200753 if (tst_test->needs_rofs) {
754 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
755 tdev.fs_type);
756 return;
757 }
758
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200759 if (tst_test->mount_device) {
760 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
761 tst_test->mnt_flags, tst_test->mnt_data);
762 mntpoint_mounted = 1;
763 }
764}
765
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100766static void do_setup(int argc, char *argv[])
767{
768 if (!tst_test)
769 tst_brk(TBROK, "No tests to run");
770
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200771 if (tst_test->tconf_msg)
772 tst_brk(TCONF, "%s", tst_test->tconf_msg);
773
Cyril Hrubisf023a612018-11-27 14:21:17 +0100774 if (tst_test->needs_kconfigs)
775 tst_kconfig_check(tst_test->needs_kconfigs);
776
Cyril Hrubisc4596542017-06-20 15:42:18 +0200777 assert_test_fn();
778
Li Wangb5d620a2017-11-01 13:15:23 +0800779 tid = get_tid(argv);
780
Cyril Hrubisc4596542017-06-20 15:42:18 +0200781 if (tst_test->sample)
782 tst_test = tst_timer_test_setup(tst_test);
783
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200784 parse_opts(argc, argv);
785
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100786 if (tst_test->needs_root && geteuid() != 0)
787 tst_brk(TCONF, "Test needs to be run as root");
788
789 if (tst_test->min_kver)
790 check_kver();
791
Alexey Kodanev1f70b0a2018-09-20 14:30:16 +0300792 if (tst_test->needs_drivers) {
793 const char *name;
794 int i;
795
796 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
797 if (tst_check_driver(name))
798 tst_brk(TCONF, "%s driver not available", name);
799 }
800
Cyril Hrubis874326d2017-02-14 15:59:40 +0100801 if (tst_test->format_device)
802 tst_test->needs_device = 1;
803
804 if (tst_test->mount_device) {
805 tst_test->needs_device = 1;
806 tst_test->format_device = 1;
807 }
808
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200809 if (tst_test->all_filesystems)
810 tst_test->needs_device = 1;
811
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100812 setup_ipc();
813
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000814 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100815 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100816
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100817 if (tst_test->save_restore) {
818 const char * const *name = tst_test->save_restore;
819
820 while (*name) {
821 tst_sys_conf_save(*name);
822 name++;
823 }
824 }
825
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700826 if (tst_test->mntpoint)
827 SAFE_MKDIR(tst_test->mntpoint, 0777);
828
Xiao Yang7313a872018-09-01 12:07:41 +0800829 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
830 tst_test->mount_device || tst_test->all_filesystems) &&
831 !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700832 tst_brk(TBROK, "tst_test->mntpoint must be set!");
833 }
834
Xiao Yang7313a872018-09-01 12:07:41 +0800835 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
836 !!tst_test->needs_device > 1) {
837 tst_brk(TBROK,
838 "Two or more of needs_{rofs, devfs, device} are set");
839 }
840
841 if (tst_test->needs_devfs)
842 prepare_and_mount_dev_fs(tst_test->mntpoint);
843
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700844 if (tst_test->needs_rofs) {
845 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200846 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700847 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200848 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
849 tst_res(TINFO, "Can't mount tmpfs read-only, "
850 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700851 tst_test->needs_device = 1;
852 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700853 }
854 }
855
856 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100857 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100858
859 if (!tdev.dev)
860 tst_brk(TCONF, "Failed to acquire device");
861
862 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100863
864 if (tst_test->dev_fs_type)
865 tdev.fs_type = tst_test->dev_fs_type;
866 else
867 tdev.fs_type = tst_dev_fs_type();
868
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200869 if (!tst_test->all_filesystems)
870 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100871 }
872
873 if (tst_test->resource_files)
874 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200875}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100876
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200877static void do_test_setup(void)
878{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100879 main_pid = getpid();
880
881 if (tst_test->setup)
882 tst_test->setup();
883
884 if (main_pid != getpid())
885 tst_brk(TBROK, "Runaway child in setup()!");
886}
887
888static void do_cleanup(void)
889{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700890 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100891 tst_umount(tst_test->mntpoint);
892
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100893 if (tst_test->needs_device && tdev.dev)
894 tst_release_device(tdev.dev);
895
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000896 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100897 /* avoid munmap() on wrong pointer in tst_rmdir() */
898 tst_futexes = NULL;
899 tst_rmdir();
900 }
Jan Stancek332540e2016-06-08 16:48:22 +0200901
Jan Stancek9dcbc6d2018-11-05 09:00:02 +0100902 if (tst_test->save_restore)
903 tst_sys_conf_restore(0);
904
Jan Stancek332540e2016-06-08 16:48:22 +0200905 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100906}
907
908static void run_tests(void)
909{
910 unsigned int i;
911 struct results saved_results;
912
913 if (!tst_test->test) {
914 saved_results = *results;
915 tst_test->test_all();
916
917 if (getpid() != main_pid) {
918 exit(0);
919 }
920
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300921 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100922
923 if (results_equal(&saved_results, results))
924 tst_brk(TBROK, "Test haven't reported results!");
925 return;
926 }
927
928 for (i = 0; i < tst_test->tcnt; i++) {
929 saved_results = *results;
930 tst_test->test(i);
931
932 if (getpid() != main_pid) {
933 exit(0);
934 }
935
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300936 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100937
938 if (results_equal(&saved_results, results))
939 tst_brk(TBROK, "Test %i haven't reported results!", i);
940 }
941}
942
943static unsigned long long get_time_ms(void)
944{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100945 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100946
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100947 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
948 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100949
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100950 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100951}
952
Jan Stancekb95b1992017-10-10 15:47:58 +0200953static void add_paths(void)
954{
955 char *old_path = getenv("PATH");
956 const char *start_dir;
957 char *new_path;
958
959 start_dir = tst_get_startwd();
960
961 if (old_path)
962 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
963 else
964 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
965
966 SAFE_SETENV("PATH", new_path, 1);
967 free(new_path);
968}
969
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200970static void heartbeat(void)
971{
Jan Stancek1893e012018-08-28 16:17:52 +0200972 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
973 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
974
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200975 kill(getppid(), SIGUSR1);
976}
977
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200978static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100979{
980 unsigned int i = 0;
981 unsigned long long stop_time = 0;
982 int cont = 1;
983
Jan Stancek1893e012018-08-28 16:17:52 +0200984 heartbeat();
Jan Stancekb95b1992017-10-10 15:47:58 +0200985 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200986 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100987
988 if (duration > 0)
989 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
990
991 for (;;) {
992 cont = 0;
993
994 if (i < (unsigned int)iterations) {
995 i++;
996 cont = 1;
997 }
998
999 if (stop_time && get_time_ms() < stop_time)
1000 cont = 1;
1001
1002 if (!cont)
1003 break;
1004
1005 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001006 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001007 }
1008
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001009 do_test_cleanup();
1010 exit(0);
1011}
1012
1013static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001014
Cyril Hrubis79163172017-05-18 10:57:07 +02001015
1016static volatile sig_atomic_t sigkill_retries;
1017
1018#define WRITE_MSG(msg) do { \
1019 if (write(2, msg, sizeof(msg) - 1)) { \
1020 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1021 } \
1022} while (0)
1023
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001024static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1025{
Cyril Hrubis79163172017-05-18 10:57:07 +02001026 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001027 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +02001028 alarm(5);
1029
1030 if (++sigkill_retries > 10) {
1031 WRITE_MSG("Cannot kill test processes!\n");
1032 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1033 WRITE_MSG("Exitting uncleanly...\n");
1034 _exit(TFAIL);
1035 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001036}
1037
1038static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1039{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001040 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +02001041 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001042}
1043
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001044static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1045{
1046 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +02001047 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001048 kill(-test_pid, SIGKILL);
1049 }
1050}
1051
Jan Stancek1893e012018-08-28 16:17:52 +02001052unsigned int tst_timeout_remaining(void)
1053{
1054 static struct timespec now;
1055 unsigned int elapsed;
1056
1057 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1058 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1059
1060 elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000;
1061 if (results->timeout > elapsed)
1062 return results->timeout - elapsed;
1063
1064 return 0;
1065}
1066
Li Wang94823cf2017-07-18 16:23:00 +08001067void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001068{
1069 char *mul = getenv("LTP_TIMEOUT_MUL");
1070
Li Wang94823cf2017-07-18 16:23:00 +08001071 if (timeout == -1) {
1072 tst_res(TINFO, "Timeout per run is disabled");
1073 return;
1074 }
1075
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001076 results->timeout = timeout;
1077
1078 if (mul) {
1079 float m = atof(mul);
1080
1081 if (m < 1)
1082 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1083
1084 results->timeout = results->timeout * m + 0.5;
1085 }
1086
1087 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1088 results->timeout/3600, (results->timeout%3600)/60,
1089 results->timeout % 60);
1090
1091 if (getpid() == lib_pid)
1092 alarm(results->timeout);
1093 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001094 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001095}
1096
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001097static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001098{
1099 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001100
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001101 if (tst_test->timeout)
1102 tst_set_timeout(tst_test->timeout);
1103 else
1104 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001105
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001106 SAFE_SIGNAL(SIGINT, sigint_handler);
1107
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001108 test_pid = fork();
1109 if (test_pid < 0)
1110 tst_brk(TBROK | TERRNO, "fork()");
1111
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001112 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001113 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1114 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1115 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001116 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001117 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001118 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001119
1120 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001121 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001122 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001123
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001124 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001125 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001126
1127 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1128 tst_res(TINFO, "If you are running on slow machine, "
1129 "try exporting LTP_TIMEOUT_MUL > 1");
1130 tst_brk(TBROK, "Test killed! (timeout?)");
1131 }
1132
1133 if (WIFSIGNALED(status))
1134 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1135
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001136 return 0;
1137}
1138
1139static int run_tcases_per_fs(void)
1140{
1141 int ret = 0;
1142 unsigned int i;
1143 const char *const *filesystems = tst_get_supported_fs_types();
1144
1145 if (!filesystems[0])
1146 tst_brk(TCONF, "There are no supported filesystems");
1147
1148 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001149
1150 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001151 tdev.fs_type = filesystems[i];
1152
1153 prepare_device();
1154
1155 ret = fork_testrun();
1156
1157 if (mntpoint_mounted) {
1158 tst_umount(tst_test->mntpoint);
1159 mntpoint_mounted = 0;
1160 }
1161
1162 if (ret == TCONF) {
1163 update_results(ret);
1164 continue;
1165 }
1166
1167 if (ret == 0)
1168 continue;
1169
1170 do_exit(ret);
1171 }
1172
1173 return ret;
1174}
1175
1176void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1177{
1178 int ret;
1179
1180 lib_pid = getpid();
1181 tst_test = self;
1182
1183 do_setup(argc, argv);
1184
1185 TCID = tid;
1186
1187 SAFE_SIGNAL(SIGALRM, alarm_handler);
1188 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1189
1190 if (tst_test->all_filesystems)
1191 ret = run_tcases_per_fs();
1192 else
1193 ret = fork_testrun();
1194
1195 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001196}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001197
1198
1199void tst_flush(void)
1200{
1201 int rval;
1202
1203 rval = fflush(stderr);
1204 if (rval != 0)
1205 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1206
1207 rval = fflush(stderr);
1208 if (rval != 0)
1209 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1210
1211}