blob: 794aa8555b3aae1e6051bb034cfcc033c8b3e789 [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 Hrubisbbdb9f72016-03-16 15:53:57 +010033
34#include "old_resource.h"
35#include "old_device.h"
36#include "old_tmpdir.h"
37
38struct tst_test *tst_test;
39
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010040static int iterations = 1;
41static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020042static pid_t main_pid, lib_pid;
Cyril Hrubis874326d2017-02-14 15:59:40 +010043static int device_mounted;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010044
45struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020046 int passed;
47 int skipped;
48 int failed;
49 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020050 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010051};
52
53static struct results *results;
54
55static int ipc_fd;
56
57extern void *tst_futexes;
58extern unsigned int tst_max_futexes;
59
60#define IPC_ENV_VAR "LTP_IPC_PATH"
61
62static char ipc_path[1024];
63const char *tst_ipc_path = ipc_path;
64char *const tst_ipc_envp[] = {ipc_path, NULL};
65
66static char shm_path[1024];
67
Jan Stancek332540e2016-06-08 16:48:22 +020068static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020069static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020070
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010071static void setup_ipc(void)
72{
73 size_t size = getpagesize();
74
Steven Jackson9f41dcf2016-12-21 20:09:01 +000075 if (access("/dev/shm", F_OK) == 0) {
76 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
77 tst_test->tid, getpid());
78 } else {
79 char *tmpdir;
80
81 if (!tst_tmpdir_created())
82 tst_tmpdir();
83
84 tmpdir = tst_get_tmpdir();
85 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
86 tmpdir, tst_test->tid, getpid());
87 free(tmpdir);
88 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010089
90 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
91 if (ipc_fd < 0)
92 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
93
94 SAFE_FTRUNCATE(ipc_fd, size);
95
96 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
97
98 /* Checkpoints needs to be accessible from processes started by exec() */
99 if (tst_test->needs_checkpoints)
100 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
101 else
102 SAFE_UNLINK(shm_path);
103
104 SAFE_CLOSE(ipc_fd);
105
106 if (tst_test->needs_checkpoints) {
107 tst_futexes = (char*)results + sizeof(struct results);
108 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
109 }
110}
111
112static void cleanup_ipc(void)
113{
114 size_t size = getpagesize();
115
116 if (ipc_fd > 0 && close(ipc_fd))
117 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
118
119 if (!access(shm_path, F_OK) && unlink(shm_path))
120 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
121
122 msync((void*)results, size, MS_SYNC);
123 munmap((void*)results, size);
124}
125
126void tst_reinit(void)
127{
128 const char *path = getenv("LTP_IPC_PATH");
129 size_t size = getpagesize();
130 int fd;
131 void *ptr;
132
133 if (!path)
134 tst_brk(TBROK, "LTP_IPC_PATH is not defined");
135
136 if (access(path, F_OK))
137 tst_brk(TBROK, "File %s does not exist!", path);
138
139 fd = SAFE_OPEN(path, O_RDWR);
140
141 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
142 tst_futexes = (char*)ptr + sizeof(struct results);
143 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
144
145 SAFE_CLOSE(fd);
146}
147
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100148static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100149{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100150 if (!results)
151 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100152
153 switch (ttype) {
154 case TCONF:
155 tst_atomic_inc(&results->skipped);
156 break;
157 case TPASS:
158 tst_atomic_inc(&results->passed);
159 break;
160 case TWARN:
161 tst_atomic_inc(&results->warnings);
162 break;
163 case TFAIL:
164 tst_atomic_inc(&results->failed);
165 break;
166 }
167}
168
169static void print_result(const char *file, const int lineno, int ttype,
170 const char *fmt, va_list va)
171{
172 char buf[1024];
173 char *str = buf;
174 int ret, size = sizeof(buf);
175 const char *str_errno = NULL;
176 const char *res;
177
178 switch (TTYPE_RESULT(ttype)) {
179 case TPASS:
180 res = "PASS";
181 break;
182 case TFAIL:
183 res = "FAIL";
184 break;
185 case TBROK:
186 res = "BROK";
187 break;
188 case TCONF:
189 res = "CONF";
190 break;
191 case TWARN:
192 res = "WARN";
193 break;
194 case TINFO:
195 res = "INFO";
196 break;
197 default:
198 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100199 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100200 }
201
202 if (ttype & TERRNO)
203 str_errno = tst_strerrno(errno);
204
205 if (ttype & TTERRNO)
206 str_errno = tst_strerrno(TEST_ERRNO);
207
Petr Vorela7f61332017-01-24 20:47:30 +0100208 ret = snprintf(str, size, "%s:%i: ", file, lineno);
209 str += ret;
210 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100211
Cyril Hrubis047c7272017-02-13 16:23:36 +0100212 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100213 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
214 res, ANSI_COLOR_RESET);
215 else
216 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100217 str += ret;
218 size -= ret;
219
220 ret = vsnprintf(str, size, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100221 str += ret;
222 size -= ret;
223
Petr Vorela7f61332017-01-24 20:47:30 +0100224 if (str_errno) {
225 ret = snprintf(str, size, ": %s", str_errno);
226 str += ret;
227 size -= ret;
228 }
229
230 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100231
232 fputs(buf, stderr);
233}
234
235void tst_vres_(const char *file, const int lineno, int ttype,
236 const char *fmt, va_list va)
237{
238 print_result(file, lineno, ttype, fmt, va);
239
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100240 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100241}
242
243void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100244 const char *fmt, va_list va);
245
246static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
247 const char *fmt, va_list va) = tst_vbrk_;
248
249static void tst_cvres(const char *file, const int lineno, int ttype,
250 const char *fmt, va_list va)
251{
252 if (TTYPE_RESULT(ttype) == TBROK) {
253 ttype &= ~TTYPE_MASK;
254 ttype |= TWARN;
255 }
256
257 print_result(file, lineno, ttype, fmt, va);
258 update_results(TTYPE_RESULT(ttype));
259}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100260
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200261static void do_test_cleanup(void)
262{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100263 tst_brk_handler = tst_cvres;
264
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200265 if (tst_test->cleanup)
266 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100267
268 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200269}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100270
271void tst_vbrk_(const char *file, const int lineno, int ttype,
272 const char *fmt, va_list va)
273{
274 print_result(file, lineno, ttype, fmt, va);
275
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200276 if (getpid() == main_pid)
277 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100278
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200279 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200280 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200281
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100282 exit(TTYPE_RESULT(ttype));
283}
284
285void tst_res_(const char *file, const int lineno, int ttype,
286 const char *fmt, ...)
287{
288 va_list va;
289
290 va_start(va, fmt);
291 tst_vres_(file, lineno, ttype, fmt, va);
292 va_end(va);
293}
294
295void tst_brk_(const char *file, const int lineno, int ttype,
296 const char *fmt, ...)
297{
298 va_list va;
299
300 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100301 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100302 va_end(va);
303}
304
305static void check_child_status(pid_t pid, int status)
306{
307 int ret;
308
309 if (WIFSIGNALED(status)) {
310 tst_brk(TBROK, "Child (%i) killed by signal %s",
311 pid, tst_strsig(WTERMSIG(status)));
312 }
313
314 if (!(WIFEXITED(status)))
315 tst_brk(TBROK, "Child (%i) exitted abnormaly", pid);
316
317 ret = WEXITSTATUS(status);
318 switch (ret) {
319 case TPASS:
320 break;
321 case TBROK:
322 case TCONF:
323 tst_brk(ret, "Reported by child (%i)", pid);
324 default:
325 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
326 }
327}
328
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300329void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100330{
331 int status;
332 pid_t pid;
333
334 for (;;) {
335 pid = wait(&status);
336
337 if (pid > 0) {
338 check_child_status(pid, status);
339 continue;
340 }
341
342 if (errno == ECHILD)
343 break;
344
345 if (errno == EINTR)
346 continue;
347
348 tst_brk(TBROK | TERRNO, "wait() failed");
349 }
350}
351
352
353pid_t safe_fork(const char *filename, unsigned int lineno)
354{
355 pid_t pid;
356
357 if (!tst_test->forks_child)
358 tst_brk(TBROK, "test.forks_child must be set!");
359
360 fflush(stdout);
361
362 pid = fork();
363 if (pid < 0)
364 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
365
366 return pid;
367}
368
369static struct option {
370 char *optstr;
371 char *help;
372} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200373 {"h", "-h Prints this help"},
374 {"i:", "-i n Execute test n times"},
375 {"I:", "-I x Execute test for n seconds"},
376 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100377};
378
379static void print_help(void)
380{
381 unsigned int i;
382
383 for (i = 0; i < ARRAY_SIZE(options); i++)
384 fprintf(stderr, "%s\n", options[i].help);
385
386 if (!tst_test->options)
387 return;
388
389 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200390 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100391}
392
393static void check_option_collision(void)
394{
395 unsigned int i, j;
396 struct tst_option *toptions = tst_test->options;
397
398 if (!toptions)
399 return;
400
401 for (i = 0; toptions[i].optstr; i++) {
402 for (j = 0; j < ARRAY_SIZE(options); j++) {
403 if (toptions[i].optstr[0] == options[j].optstr[0]) {
404 tst_brk(TBROK, "Option collision '%s'",
405 options[j].help);
406 }
407 }
408 }
409}
410
411static unsigned int count_options(void)
412{
413 unsigned int i;
414
415 if (!tst_test->options)
416 return 0;
417
418 for (i = 0; tst_test->options[i].optstr; i++);
419
420 return i;
421}
422
423static void parse_topt(unsigned int topts_len, int opt, char *optarg)
424{
425 unsigned int i;
426 struct tst_option *toptions = tst_test->options;
427
428 for (i = 0; i < topts_len; i++) {
429 if (toptions[i].optstr[0] == opt)
430 break;
431 }
432
433 if (i >= topts_len)
434 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
435
Jan Stancekc07d36c2016-08-01 11:44:55 +0200436 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100437}
438
439/* see self_exec.c */
440#ifdef UCLINUX
441extern char *child_args;
442#endif
443
444static void parse_opts(int argc, char *argv[])
445{
446 unsigned int i, topts_len = count_options();
447 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
448 int opt;
449
450 check_option_collision();
451
452 optstr[0] = 0;
453
454 for (i = 0; i < ARRAY_SIZE(options); i++)
455 strcat(optstr, options[i].optstr);
456
457 for (i = 0; i < topts_len; i++)
458 strcat(optstr, tst_test->options[i].optstr);
459
460 while ((opt = getopt(argc, argv, optstr)) > 0) {
461 switch (opt) {
462 case '?':
463 print_help();
464 tst_brk(TBROK, "Invalid option");
465 case 'h':
466 print_help();
467 exit(0);
468 case 'i':
469 iterations = atoi(optarg);
470 break;
471 case 'I':
472 duration = atof(optarg);
473 break;
474 case 'C':
475#ifdef UCLINUX
476 child_args = optarg;
477#endif
478 break;
479 default:
480 parse_topt(topts_len, opt, optarg);
481 }
482 }
483}
484
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200485int tst_parse_int(const char *str, int *val, int min, int max)
486{
487 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300488
489 if (!str)
490 return 0;
491
492 int ret = tst_parse_long(str, &rval, min, max);
493
494 if (ret)
495 return ret;
496
497 *val = (int)rval;
498 return 0;
499}
500
501int tst_parse_long(const char *str, long *val, long min, long max)
502{
503 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200504 char *end;
505
506 if (!str)
507 return 0;
508
509 errno = 0;
510 rval = strtol(str, &end, 10);
511
512 if (str == end || *end != '\0')
513 return EINVAL;
514
515 if (errno)
516 return errno;
517
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300518 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200519 return ERANGE;
520
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300521 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200522 return 0;
523}
524
525int tst_parse_float(const char *str, float *val, float min, float max)
526{
527 double rval;
528 char *end;
529
530 if (!str)
531 return 0;
532
533 errno = 0;
534 rval = strtod(str, &end);
535
536 if (str == end || *end != '\0')
537 return EINVAL;
538
539 if (errno)
540 return errno;
541
542 if (rval > (double)max || rval < (double)min)
543 return ERANGE;
544
545 *val = (float)rval;
546 return 0;
547}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100548
Cyril Hrubisfa495172016-06-08 16:06:35 +0200549static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100550{
Xiao Yang11dfc322016-06-16 15:52:04 +0800551 if (results) {
552 printf("\nSummary:\n");
553 printf("passed %d\n", results->passed);
554 printf("failed %d\n", results->failed);
555 printf("skipped %d\n", results->skipped);
556 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100557
Xiao Yang11dfc322016-06-16 15:52:04 +0800558 if (results->failed)
559 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100560
Xiao Yang11dfc322016-06-16 15:52:04 +0800561 if (results->skipped)
562 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100563
Xiao Yang11dfc322016-06-16 15:52:04 +0800564 if (results->warnings)
565 ret |= TWARN;
566 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100567
Jan Stancek332540e2016-06-08 16:48:22 +0200568 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100569
570 exit(ret);
571}
572
573void check_kver(void)
574{
575 int v1, v2, v3;
576
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100577 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
578 tst_res(TWARN,
579 "Invalid kernel version %s, expected %%d.%%d.%%d",
580 tst_test->min_kver);
581 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100582
583 if (tst_kvercmp(v1, v2, v3) < 0) {
584 tst_brk(TCONF, "The test requires kernel %s or newer",
585 tst_test->min_kver);
586 }
587}
588
589static int results_equal(struct results *a, struct results *b)
590{
591 if (a->passed != b->passed)
592 return 0;
593
594 if (a->failed != b->failed)
595 return 0;
596
597 if (a->skipped != b->skipped)
598 return 0;
599
600 return 1;
601}
602
603static int needs_tmpdir(void)
604{
605 return tst_test->needs_tmpdir ||
606 tst_test->needs_device ||
607 tst_test->resource_files ||
608 tst_test->needs_checkpoints;
609}
610
611static void copy_resources(void)
612{
613 unsigned int i;
614
615 for (i = 0; tst_test->resource_files[i]; i++)
616 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
617}
618
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800619static const char *get_tid(char *argv[])
620{
621 char *p;
622
623 if (!argv[0] || !argv[0][0]) {
624 tst_res(TINFO, "argv[0] is empty!");
625 return "ltp_empty_argv";
626 }
627
628 p = strrchr(argv[0], '/');
629 if (p)
630 return p+1;
631
632 return argv[0];
633}
634
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100635static struct tst_device tdev;
636struct tst_device *tst_device;
637
638static void do_setup(int argc, char *argv[])
639{
640 if (!tst_test)
641 tst_brk(TBROK, "No tests to run");
642
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200643 if (!tst_test->tid)
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800644 tst_test->tid = get_tid(argv);
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200645
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100646 if (!tst_test->test && !tst_test->test_all)
647 tst_brk(TBROK, "No test function speficied");
648
649 if (tst_test->test && tst_test->test_all)
650 tst_brk(TBROK, "You can define either test() or test_all()");
651
652 if (tst_test->test && !tst_test->tcnt)
653 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
654
655 if (tst_test->test_all && tst_test->tcnt)
656 tst_brk(TBROK, "You can't define tcnt for test_all()");
657
658 if (tst_test->needs_root && geteuid() != 0)
659 tst_brk(TCONF, "Test needs to be run as root");
660
661 if (tst_test->min_kver)
662 check_kver();
663
Cyril Hrubis874326d2017-02-14 15:59:40 +0100664 if (tst_test->format_device)
665 tst_test->needs_device = 1;
666
667 if (tst_test->mount_device) {
668 tst_test->needs_device = 1;
669 tst_test->format_device = 1;
670 }
671
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100672 parse_opts(argc, argv);
673
674 setup_ipc();
675
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000676 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100677 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100678
679 if (tst_test->needs_device) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100680 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100681
682 if (!tdev.dev)
683 tst_brk(TCONF, "Failed to acquire device");
684
685 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100686
687 if (tst_test->dev_fs_type)
688 tdev.fs_type = tst_test->dev_fs_type;
689 else
690 tdev.fs_type = tst_dev_fs_type();
691
692 if (tst_test->format_device) {
693 SAFE_MKFS(tdev.dev, tdev.fs_type,
694 tst_test->dev_fs_opts,
695 tst_test->dev_extra_opt);
696 }
697
698 if (tst_test->mount_device) {
699
700 if (!tst_test->mntpoint) {
701 tst_brk(TBROK,
702 "tst_test->mntpoint must be set!");
703 }
704
705 SAFE_MKDIR(tst_test->mntpoint, 0777);
706 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
707 tst_test->mnt_flags, tst_test->mnt_data);
708 device_mounted = 1;
709 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100710 }
711
712 if (tst_test->resource_files)
713 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200714}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100715
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200716static void do_test_setup(void)
717{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100718 main_pid = getpid();
719
720 if (tst_test->setup)
721 tst_test->setup();
722
723 if (main_pid != getpid())
724 tst_brk(TBROK, "Runaway child in setup()!");
725}
726
727static void do_cleanup(void)
728{
Cyril Hrubis874326d2017-02-14 15:59:40 +0100729 if (device_mounted)
730 tst_umount(tst_test->mntpoint);
731
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100732 if (tst_test->needs_device && tdev.dev)
733 tst_release_device(tdev.dev);
734
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000735 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100736 /* avoid munmap() on wrong pointer in tst_rmdir() */
737 tst_futexes = NULL;
738 tst_rmdir();
739 }
Jan Stancek332540e2016-06-08 16:48:22 +0200740
741 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100742}
743
744static void run_tests(void)
745{
746 unsigned int i;
747 struct results saved_results;
748
749 if (!tst_test->test) {
750 saved_results = *results;
751 tst_test->test_all();
752
753 if (getpid() != main_pid) {
754 exit(0);
755 }
756
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300757 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100758
759 if (results_equal(&saved_results, results))
760 tst_brk(TBROK, "Test haven't reported results!");
761 return;
762 }
763
764 for (i = 0; i < tst_test->tcnt; i++) {
765 saved_results = *results;
766 tst_test->test(i);
767
768 if (getpid() != main_pid) {
769 exit(0);
770 }
771
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300772 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100773
774 if (results_equal(&saved_results, results))
775 tst_brk(TBROK, "Test %i haven't reported results!", i);
776 }
777}
778
779static unsigned long long get_time_ms(void)
780{
781 struct timeval tv;
782
783 gettimeofday(&tv, NULL);
784
785 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
786}
787
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200788static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100789{
790 unsigned int i = 0;
791 unsigned long long stop_time = 0;
792 int cont = 1;
793
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200794 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100795
796 if (duration > 0)
797 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
798
799 for (;;) {
800 cont = 0;
801
802 if (i < (unsigned int)iterations) {
803 i++;
804 cont = 1;
805 }
806
807 if (stop_time && get_time_ms() < stop_time)
808 cont = 1;
809
810 if (!cont)
811 break;
812
813 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200814
815 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100816 }
817
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200818 do_test_cleanup();
819 exit(0);
820}
821
822static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200823
Cyril Hrubis79163172017-05-18 10:57:07 +0200824
825static volatile sig_atomic_t sigkill_retries;
826
827#define WRITE_MSG(msg) do { \
828 if (write(2, msg, sizeof(msg) - 1)) { \
829 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
830 } \
831} while (0)
832
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200833static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
834{
Cyril Hrubis79163172017-05-18 10:57:07 +0200835 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200836 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200837 alarm(5);
838
839 if (++sigkill_retries > 10) {
840 WRITE_MSG("Cannot kill test processes!\n");
841 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
842 WRITE_MSG("Exitting uncleanly...\n");
843 _exit(TFAIL);
844 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200845}
846
847static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
848{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200849 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200850 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200851}
852
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200853static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
854{
855 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200856 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200857 kill(-test_pid, SIGKILL);
858 }
859}
860
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200861void tst_set_timeout(unsigned int timeout)
862{
863 char *mul = getenv("LTP_TIMEOUT_MUL");
864
865 results->timeout = timeout;
866
867 if (mul) {
868 float m = atof(mul);
869
870 if (m < 1)
871 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
872
873 results->timeout = results->timeout * m + 0.5;
874 }
875
876 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
877 results->timeout/3600, (results->timeout%3600)/60,
878 results->timeout % 60);
879
880 if (getpid() == lib_pid)
881 alarm(results->timeout);
882 else
883 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200884}
885
886void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
887{
888 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200889
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200890 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200891 tst_test = self;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200892
893 do_setup(argc, argv);
894
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800895 TCID = tst_test->tid;
896
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200897 SAFE_SIGNAL(SIGALRM, alarm_handler);
898 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
899
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200900 if (tst_test->timeout)
901 tst_set_timeout(tst_test->timeout);
902 else
903 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200904
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200905 SAFE_SIGNAL(SIGINT, sigint_handler);
906
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200907 test_pid = fork();
908 if (test_pid < 0)
909 tst_brk(TBROK | TERRNO, "fork()");
910
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200911 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200912 SAFE_SIGNAL(SIGALRM, SIG_DFL);
913 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
914 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200915 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200916 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200917 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200918
919 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200920 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200921 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200922
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200923 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200924 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200925
926 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
927 tst_res(TINFO, "If you are running on slow machine, "
928 "try exporting LTP_TIMEOUT_MUL > 1");
929 tst_brk(TBROK, "Test killed! (timeout?)");
930 }
931
932 if (WIFSIGNALED(status))
933 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
934
Cyril Hrubisfa495172016-06-08 16:06:35 +0200935 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100936}