blob: 16ea64fe96f4d7f56bfdfb0a08e52e8f7570ec91 [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>
24#include <sys/types.h>
25#include <sys/wait.h>
26#include <sys/time.h>
27
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"
Petr Vorel3a0ef862017-03-01 15:31:08 +010032#include "tst_ansi_color.h"
Cyril Hrubisc4596542017-06-20 15:42:18 +020033#include "tst_timer_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010034
35#include "old_resource.h"
36#include "old_device.h"
37#include "old_tmpdir.h"
38
39struct tst_test *tst_test;
40
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010041static int iterations = 1;
42static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020043static pid_t main_pid, lib_pid;
Cyril Hrubis874326d2017-02-14 15:59:40 +010044static int device_mounted;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010045
46struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020047 int passed;
48 int skipped;
49 int failed;
50 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020051 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010052};
53
54static struct results *results;
55
56static int ipc_fd;
57
58extern void *tst_futexes;
59extern unsigned int tst_max_futexes;
60
61#define IPC_ENV_VAR "LTP_IPC_PATH"
62
63static char ipc_path[1024];
64const char *tst_ipc_path = ipc_path;
65char *const tst_ipc_envp[] = {ipc_path, NULL};
66
67static char shm_path[1024];
68
Jan Stancek332540e2016-06-08 16:48:22 +020069static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020070static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020071
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010072static void setup_ipc(void)
73{
74 size_t size = getpagesize();
75
Steven Jackson9f41dcf2016-12-21 20:09:01 +000076 if (access("/dev/shm", F_OK) == 0) {
77 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
78 tst_test->tid, getpid());
79 } else {
80 char *tmpdir;
81
82 if (!tst_tmpdir_created())
83 tst_tmpdir();
84
85 tmpdir = tst_get_tmpdir();
86 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
87 tmpdir, tst_test->tid, getpid());
88 free(tmpdir);
89 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010090
91 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
92 if (ipc_fd < 0)
93 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
94
95 SAFE_FTRUNCATE(ipc_fd, size);
96
97 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
98
99 /* Checkpoints needs to be accessible from processes started by exec() */
100 if (tst_test->needs_checkpoints)
101 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
102 else
103 SAFE_UNLINK(shm_path);
104
105 SAFE_CLOSE(ipc_fd);
106
107 if (tst_test->needs_checkpoints) {
108 tst_futexes = (char*)results + sizeof(struct results);
109 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
110 }
111}
112
113static void cleanup_ipc(void)
114{
115 size_t size = getpagesize();
116
117 if (ipc_fd > 0 && close(ipc_fd))
118 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
119
120 if (!access(shm_path, F_OK) && unlink(shm_path))
121 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
122
123 msync((void*)results, size, MS_SYNC);
124 munmap((void*)results, size);
125}
126
127void tst_reinit(void)
128{
129 const char *path = getenv("LTP_IPC_PATH");
130 size_t size = getpagesize();
131 int fd;
132 void *ptr;
133
134 if (!path)
135 tst_brk(TBROK, "LTP_IPC_PATH is not defined");
136
137 if (access(path, F_OK))
138 tst_brk(TBROK, "File %s does not exist!", path);
139
140 fd = SAFE_OPEN(path, O_RDWR);
141
142 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
143 tst_futexes = (char*)ptr + sizeof(struct results);
144 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
145
146 SAFE_CLOSE(fd);
147}
148
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100149static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100150{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100151 if (!results)
152 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100153
154 switch (ttype) {
155 case TCONF:
156 tst_atomic_inc(&results->skipped);
157 break;
158 case TPASS:
159 tst_atomic_inc(&results->passed);
160 break;
161 case TWARN:
162 tst_atomic_inc(&results->warnings);
163 break;
164 case TFAIL:
165 tst_atomic_inc(&results->failed);
166 break;
167 }
168}
169
170static void print_result(const char *file, const int lineno, int ttype,
171 const char *fmt, va_list va)
172{
173 char buf[1024];
174 char *str = buf;
175 int ret, size = sizeof(buf);
176 const char *str_errno = NULL;
177 const char *res;
178
179 switch (TTYPE_RESULT(ttype)) {
180 case TPASS:
181 res = "PASS";
182 break;
183 case TFAIL:
184 res = "FAIL";
185 break;
186 case TBROK:
187 res = "BROK";
188 break;
189 case TCONF:
190 res = "CONF";
191 break;
192 case TWARN:
193 res = "WARN";
194 break;
195 case TINFO:
196 res = "INFO";
197 break;
198 default:
199 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100200 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100201 }
202
203 if (ttype & TERRNO)
204 str_errno = tst_strerrno(errno);
205
206 if (ttype & TTERRNO)
207 str_errno = tst_strerrno(TEST_ERRNO);
208
Petr Vorela7f61332017-01-24 20:47:30 +0100209 ret = snprintf(str, size, "%s:%i: ", file, lineno);
210 str += ret;
211 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100212
Cyril Hrubis047c7272017-02-13 16:23:36 +0100213 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100214 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
215 res, ANSI_COLOR_RESET);
216 else
217 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100218 str += ret;
219 size -= ret;
220
221 ret = vsnprintf(str, size, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100222 str += ret;
223 size -= ret;
224
Petr Vorela7f61332017-01-24 20:47:30 +0100225 if (str_errno) {
226 ret = snprintf(str, size, ": %s", str_errno);
227 str += ret;
228 size -= ret;
229 }
230
231 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100232
233 fputs(buf, stderr);
234}
235
236void tst_vres_(const char *file, const int lineno, int ttype,
237 const char *fmt, va_list va)
238{
239 print_result(file, lineno, ttype, fmt, va);
240
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100241 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100242}
243
244void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100245 const char *fmt, va_list va);
246
247static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
248 const char *fmt, va_list va) = tst_vbrk_;
249
250static void tst_cvres(const char *file, const int lineno, int ttype,
251 const char *fmt, va_list va)
252{
253 if (TTYPE_RESULT(ttype) == TBROK) {
254 ttype &= ~TTYPE_MASK;
255 ttype |= TWARN;
256 }
257
258 print_result(file, lineno, ttype, fmt, va);
259 update_results(TTYPE_RESULT(ttype));
260}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100261
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200262static void do_test_cleanup(void)
263{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100264 tst_brk_handler = tst_cvres;
265
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200266 if (tst_test->cleanup)
267 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100268
269 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200270}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100271
272void tst_vbrk_(const char *file, const int lineno, int ttype,
273 const char *fmt, va_list va)
274{
275 print_result(file, lineno, ttype, fmt, va);
276
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200277 if (getpid() == main_pid)
278 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100279
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200280 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200281 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200282
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100283 exit(TTYPE_RESULT(ttype));
284}
285
286void tst_res_(const char *file, const int lineno, int ttype,
287 const char *fmt, ...)
288{
289 va_list va;
290
291 va_start(va, fmt);
292 tst_vres_(file, lineno, ttype, fmt, va);
293 va_end(va);
294}
295
296void tst_brk_(const char *file, const int lineno, int ttype,
297 const char *fmt, ...)
298{
299 va_list va;
300
301 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100302 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100303 va_end(va);
304}
305
306static void check_child_status(pid_t pid, int status)
307{
308 int ret;
309
310 if (WIFSIGNALED(status)) {
311 tst_brk(TBROK, "Child (%i) killed by signal %s",
312 pid, tst_strsig(WTERMSIG(status)));
313 }
314
315 if (!(WIFEXITED(status)))
316 tst_brk(TBROK, "Child (%i) exitted abnormaly", pid);
317
318 ret = WEXITSTATUS(status);
319 switch (ret) {
320 case TPASS:
321 break;
322 case TBROK:
323 case TCONF:
324 tst_brk(ret, "Reported by child (%i)", pid);
325 default:
326 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
327 }
328}
329
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300330void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100331{
332 int status;
333 pid_t pid;
334
335 for (;;) {
336 pid = wait(&status);
337
338 if (pid > 0) {
339 check_child_status(pid, status);
340 continue;
341 }
342
343 if (errno == ECHILD)
344 break;
345
346 if (errno == EINTR)
347 continue;
348
349 tst_brk(TBROK | TERRNO, "wait() failed");
350 }
351}
352
353
354pid_t safe_fork(const char *filename, unsigned int lineno)
355{
356 pid_t pid;
357
358 if (!tst_test->forks_child)
359 tst_brk(TBROK, "test.forks_child must be set!");
360
361 fflush(stdout);
362
363 pid = fork();
364 if (pid < 0)
365 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
366
367 return pid;
368}
369
370static struct option {
371 char *optstr;
372 char *help;
373} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200374 {"h", "-h Prints this help"},
375 {"i:", "-i n Execute test n times"},
376 {"I:", "-I x Execute test for n seconds"},
377 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100378};
379
380static void print_help(void)
381{
382 unsigned int i;
383
384 for (i = 0; i < ARRAY_SIZE(options); i++)
385 fprintf(stderr, "%s\n", options[i].help);
386
387 if (!tst_test->options)
388 return;
389
390 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200391 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100392}
393
394static void check_option_collision(void)
395{
396 unsigned int i, j;
397 struct tst_option *toptions = tst_test->options;
398
399 if (!toptions)
400 return;
401
402 for (i = 0; toptions[i].optstr; i++) {
403 for (j = 0; j < ARRAY_SIZE(options); j++) {
404 if (toptions[i].optstr[0] == options[j].optstr[0]) {
405 tst_brk(TBROK, "Option collision '%s'",
406 options[j].help);
407 }
408 }
409 }
410}
411
412static unsigned int count_options(void)
413{
414 unsigned int i;
415
416 if (!tst_test->options)
417 return 0;
418
419 for (i = 0; tst_test->options[i].optstr; i++);
420
421 return i;
422}
423
424static void parse_topt(unsigned int topts_len, int opt, char *optarg)
425{
426 unsigned int i;
427 struct tst_option *toptions = tst_test->options;
428
429 for (i = 0; i < topts_len; i++) {
430 if (toptions[i].optstr[0] == opt)
431 break;
432 }
433
434 if (i >= topts_len)
435 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
436
Jan Stancekc07d36c2016-08-01 11:44:55 +0200437 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100438}
439
440/* see self_exec.c */
441#ifdef UCLINUX
442extern char *child_args;
443#endif
444
445static void parse_opts(int argc, char *argv[])
446{
447 unsigned int i, topts_len = count_options();
448 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
449 int opt;
450
451 check_option_collision();
452
453 optstr[0] = 0;
454
455 for (i = 0; i < ARRAY_SIZE(options); i++)
456 strcat(optstr, options[i].optstr);
457
458 for (i = 0; i < topts_len; i++)
459 strcat(optstr, tst_test->options[i].optstr);
460
461 while ((opt = getopt(argc, argv, optstr)) > 0) {
462 switch (opt) {
463 case '?':
464 print_help();
465 tst_brk(TBROK, "Invalid option");
466 case 'h':
467 print_help();
468 exit(0);
469 case 'i':
470 iterations = atoi(optarg);
471 break;
472 case 'I':
473 duration = atof(optarg);
474 break;
475 case 'C':
476#ifdef UCLINUX
477 child_args = optarg;
478#endif
479 break;
480 default:
481 parse_topt(topts_len, opt, optarg);
482 }
483 }
484}
485
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200486int tst_parse_int(const char *str, int *val, int min, int max)
487{
488 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300489
490 if (!str)
491 return 0;
492
493 int ret = tst_parse_long(str, &rval, min, max);
494
495 if (ret)
496 return ret;
497
498 *val = (int)rval;
499 return 0;
500}
501
502int tst_parse_long(const char *str, long *val, long min, long max)
503{
504 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200505 char *end;
506
507 if (!str)
508 return 0;
509
510 errno = 0;
511 rval = strtol(str, &end, 10);
512
513 if (str == end || *end != '\0')
514 return EINVAL;
515
516 if (errno)
517 return errno;
518
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300519 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200520 return ERANGE;
521
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300522 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200523 return 0;
524}
525
526int tst_parse_float(const char *str, float *val, float min, float max)
527{
528 double rval;
529 char *end;
530
531 if (!str)
532 return 0;
533
534 errno = 0;
535 rval = strtod(str, &end);
536
537 if (str == end || *end != '\0')
538 return EINVAL;
539
540 if (errno)
541 return errno;
542
543 if (rval > (double)max || rval < (double)min)
544 return ERANGE;
545
546 *val = (float)rval;
547 return 0;
548}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100549
Cyril Hrubisfa495172016-06-08 16:06:35 +0200550static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100551{
Xiao Yang11dfc322016-06-16 15:52:04 +0800552 if (results) {
553 printf("\nSummary:\n");
554 printf("passed %d\n", results->passed);
555 printf("failed %d\n", results->failed);
556 printf("skipped %d\n", results->skipped);
557 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100558
Xiao Yang11dfc322016-06-16 15:52:04 +0800559 if (results->failed)
560 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100561
Xiao Yang11dfc322016-06-16 15:52:04 +0800562 if (results->skipped)
563 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100564
Xiao Yang11dfc322016-06-16 15:52:04 +0800565 if (results->warnings)
566 ret |= TWARN;
567 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100568
Jan Stancek332540e2016-06-08 16:48:22 +0200569 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100570
571 exit(ret);
572}
573
574void check_kver(void)
575{
576 int v1, v2, v3;
577
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100578 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
579 tst_res(TWARN,
580 "Invalid kernel version %s, expected %%d.%%d.%%d",
581 tst_test->min_kver);
582 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100583
584 if (tst_kvercmp(v1, v2, v3) < 0) {
585 tst_brk(TCONF, "The test requires kernel %s or newer",
586 tst_test->min_kver);
587 }
588}
589
590static int results_equal(struct results *a, struct results *b)
591{
592 if (a->passed != b->passed)
593 return 0;
594
595 if (a->failed != b->failed)
596 return 0;
597
598 if (a->skipped != b->skipped)
599 return 0;
600
601 return 1;
602}
603
604static int needs_tmpdir(void)
605{
606 return tst_test->needs_tmpdir ||
607 tst_test->needs_device ||
608 tst_test->resource_files ||
609 tst_test->needs_checkpoints;
610}
611
612static void copy_resources(void)
613{
614 unsigned int i;
615
616 for (i = 0; tst_test->resource_files[i]; i++)
617 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
618}
619
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800620static const char *get_tid(char *argv[])
621{
622 char *p;
623
624 if (!argv[0] || !argv[0][0]) {
625 tst_res(TINFO, "argv[0] is empty!");
626 return "ltp_empty_argv";
627 }
628
629 p = strrchr(argv[0], '/');
630 if (p)
631 return p+1;
632
633 return argv[0];
634}
635
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100636static struct tst_device tdev;
637struct tst_device *tst_device;
638
Cyril Hrubisc4596542017-06-20 15:42:18 +0200639static void assert_test_fn(void)
640{
641 int cnt = 0;
642
643 if (tst_test->test)
644 cnt++;
645
646 if (tst_test->test_all)
647 cnt++;
648
649 if (tst_test->sample)
650 cnt++;
651
652 if (!cnt)
653 tst_brk(TBROK, "No test function speficied");
654
655 if (cnt != 1)
656 tst_brk(TBROK, "You can define only one test function");
657
658 if (tst_test->test && !tst_test->tcnt)
659 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
660
661 if (!tst_test->test && tst_test->tcnt)
662 tst_brk(TBROK, "You can define tcnt only for test()");
663}
664
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100665static void do_setup(int argc, char *argv[])
666{
667 if (!tst_test)
668 tst_brk(TBROK, "No tests to run");
669
Cyril Hrubisc4596542017-06-20 15:42:18 +0200670 assert_test_fn();
671
672 if (tst_test->sample)
673 tst_test = tst_timer_test_setup(tst_test);
674
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200675 if (!tst_test->tid)
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800676 tst_test->tid = get_tid(argv);
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200677
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100678 if (tst_test->needs_root && geteuid() != 0)
679 tst_brk(TCONF, "Test needs to be run as root");
680
681 if (tst_test->min_kver)
682 check_kver();
683
Cyril Hrubis874326d2017-02-14 15:59:40 +0100684 if (tst_test->format_device)
685 tst_test->needs_device = 1;
686
687 if (tst_test->mount_device) {
688 tst_test->needs_device = 1;
689 tst_test->format_device = 1;
690 }
691
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100692 parse_opts(argc, argv);
693
694 setup_ipc();
695
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000696 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100697 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100698
699 if (tst_test->needs_device) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100700 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100701
702 if (!tdev.dev)
703 tst_brk(TCONF, "Failed to acquire device");
704
705 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100706
707 if (tst_test->dev_fs_type)
708 tdev.fs_type = tst_test->dev_fs_type;
709 else
710 tdev.fs_type = tst_dev_fs_type();
711
712 if (tst_test->format_device) {
713 SAFE_MKFS(tdev.dev, tdev.fs_type,
714 tst_test->dev_fs_opts,
715 tst_test->dev_extra_opt);
716 }
717
718 if (tst_test->mount_device) {
719
720 if (!tst_test->mntpoint) {
721 tst_brk(TBROK,
722 "tst_test->mntpoint must be set!");
723 }
724
725 SAFE_MKDIR(tst_test->mntpoint, 0777);
726 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
727 tst_test->mnt_flags, tst_test->mnt_data);
728 device_mounted = 1;
729 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100730 }
731
732 if (tst_test->resource_files)
733 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200734}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100735
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200736static void do_test_setup(void)
737{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100738 main_pid = getpid();
739
740 if (tst_test->setup)
741 tst_test->setup();
742
743 if (main_pid != getpid())
744 tst_brk(TBROK, "Runaway child in setup()!");
745}
746
747static void do_cleanup(void)
748{
Cyril Hrubis874326d2017-02-14 15:59:40 +0100749 if (device_mounted)
750 tst_umount(tst_test->mntpoint);
751
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100752 if (tst_test->needs_device && tdev.dev)
753 tst_release_device(tdev.dev);
754
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000755 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100756 /* avoid munmap() on wrong pointer in tst_rmdir() */
757 tst_futexes = NULL;
758 tst_rmdir();
759 }
Jan Stancek332540e2016-06-08 16:48:22 +0200760
761 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100762}
763
764static void run_tests(void)
765{
766 unsigned int i;
767 struct results saved_results;
768
769 if (!tst_test->test) {
770 saved_results = *results;
771 tst_test->test_all();
772
773 if (getpid() != main_pid) {
774 exit(0);
775 }
776
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300777 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100778
779 if (results_equal(&saved_results, results))
780 tst_brk(TBROK, "Test haven't reported results!");
781 return;
782 }
783
784 for (i = 0; i < tst_test->tcnt; i++) {
785 saved_results = *results;
786 tst_test->test(i);
787
788 if (getpid() != main_pid) {
789 exit(0);
790 }
791
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300792 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100793
794 if (results_equal(&saved_results, results))
795 tst_brk(TBROK, "Test %i haven't reported results!", i);
796 }
797}
798
799static unsigned long long get_time_ms(void)
800{
801 struct timeval tv;
802
803 gettimeofday(&tv, NULL);
804
805 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
806}
807
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200808static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100809{
810 unsigned int i = 0;
811 unsigned long long stop_time = 0;
812 int cont = 1;
813
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200814 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100815
816 if (duration > 0)
817 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
818
819 for (;;) {
820 cont = 0;
821
822 if (i < (unsigned int)iterations) {
823 i++;
824 cont = 1;
825 }
826
827 if (stop_time && get_time_ms() < stop_time)
828 cont = 1;
829
830 if (!cont)
831 break;
832
833 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200834
835 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100836 }
837
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200838 do_test_cleanup();
839 exit(0);
840}
841
842static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200843
Cyril Hrubis79163172017-05-18 10:57:07 +0200844
845static volatile sig_atomic_t sigkill_retries;
846
847#define WRITE_MSG(msg) do { \
848 if (write(2, msg, sizeof(msg) - 1)) { \
849 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
850 } \
851} while (0)
852
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200853static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
854{
Cyril Hrubis79163172017-05-18 10:57:07 +0200855 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200856 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200857 alarm(5);
858
859 if (++sigkill_retries > 10) {
860 WRITE_MSG("Cannot kill test processes!\n");
861 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
862 WRITE_MSG("Exitting uncleanly...\n");
863 _exit(TFAIL);
864 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200865}
866
867static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
868{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200869 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200870 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200871}
872
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200873static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
874{
875 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200876 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200877 kill(-test_pid, SIGKILL);
878 }
879}
880
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200881void tst_set_timeout(unsigned int timeout)
882{
883 char *mul = getenv("LTP_TIMEOUT_MUL");
884
885 results->timeout = timeout;
886
887 if (mul) {
888 float m = atof(mul);
889
890 if (m < 1)
891 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
892
893 results->timeout = results->timeout * m + 0.5;
894 }
895
896 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
897 results->timeout/3600, (results->timeout%3600)/60,
898 results->timeout % 60);
899
900 if (getpid() == lib_pid)
901 alarm(results->timeout);
902 else
903 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200904}
905
906void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
907{
908 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200909
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200910 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200911 tst_test = self;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200912
913 do_setup(argc, argv);
914
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800915 TCID = tst_test->tid;
916
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200917 SAFE_SIGNAL(SIGALRM, alarm_handler);
918 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
919
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200920 if (tst_test->timeout)
921 tst_set_timeout(tst_test->timeout);
922 else
923 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200924
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200925 SAFE_SIGNAL(SIGINT, sigint_handler);
926
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200927 test_pid = fork();
928 if (test_pid < 0)
929 tst_brk(TBROK | TERRNO, "fork()");
930
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200931 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200932 SAFE_SIGNAL(SIGALRM, SIG_DFL);
933 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
934 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200935 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200936 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200937 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200938
939 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200940 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200941 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200942
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200943 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200944 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200945
946 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
947 tst_res(TINFO, "If you are running on slow machine, "
948 "try exporting LTP_TIMEOUT_MUL > 1");
949 tst_brk(TBROK, "Test killed! (timeout?)");
950 }
951
952 if (WIFSIGNALED(status))
953 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
954
Cyril Hrubisfa495172016-06-08 16:06:35 +0200955 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100956}