blob: becb17f4ef355c5378a0cbf3ef97b1961041d281 [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 Vorela7f61332017-01-24 20:47:30 +010032#include "tst_ansi_colors.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
40static char tmpdir_created;
41static int iterations = 1;
42static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020043static pid_t main_pid, lib_pid;
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
75 //TODO: Fallback to tst_tmpdir() if /dev/shm does not exits?
76 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
77 tst_test->tid, getpid());
78
79 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
80 if (ipc_fd < 0)
81 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
82
83 SAFE_FTRUNCATE(ipc_fd, size);
84
85 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
86
87 /* Checkpoints needs to be accessible from processes started by exec() */
88 if (tst_test->needs_checkpoints)
89 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
90 else
91 SAFE_UNLINK(shm_path);
92
93 SAFE_CLOSE(ipc_fd);
94
95 if (tst_test->needs_checkpoints) {
96 tst_futexes = (char*)results + sizeof(struct results);
97 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
98 }
99}
100
101static void cleanup_ipc(void)
102{
103 size_t size = getpagesize();
104
105 if (ipc_fd > 0 && close(ipc_fd))
106 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
107
108 if (!access(shm_path, F_OK) && unlink(shm_path))
109 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
110
111 msync((void*)results, size, MS_SYNC);
112 munmap((void*)results, size);
113}
114
115void tst_reinit(void)
116{
117 const char *path = getenv("LTP_IPC_PATH");
118 size_t size = getpagesize();
119 int fd;
120 void *ptr;
121
122 if (!path)
123 tst_brk(TBROK, "LTP_IPC_PATH is not defined");
124
125 if (access(path, F_OK))
126 tst_brk(TBROK, "File %s does not exist!", path);
127
128 fd = SAFE_OPEN(path, O_RDWR);
129
130 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
131 tst_futexes = (char*)ptr + sizeof(struct results);
132 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
133
134 SAFE_CLOSE(fd);
135}
136
137static void update_results(const char *file, unsigned int lineno, int ttype)
138{
139 if (!results) {
140 tst_brk(TBROK,
141 "%s: %d: Results IPC not initialized!", file, lineno);
142 }
143
144 switch (ttype) {
145 case TCONF:
146 tst_atomic_inc(&results->skipped);
147 break;
148 case TPASS:
149 tst_atomic_inc(&results->passed);
150 break;
151 case TWARN:
152 tst_atomic_inc(&results->warnings);
153 break;
154 case TFAIL:
155 tst_atomic_inc(&results->failed);
156 break;
157 }
158}
159
160static void print_result(const char *file, const int lineno, int ttype,
161 const char *fmt, va_list va)
162{
163 char buf[1024];
164 char *str = buf;
165 int ret, size = sizeof(buf);
166 const char *str_errno = NULL;
167 const char *res;
168
169 switch (TTYPE_RESULT(ttype)) {
170 case TPASS:
171 res = "PASS";
172 break;
173 case TFAIL:
174 res = "FAIL";
175 break;
176 case TBROK:
177 res = "BROK";
178 break;
179 case TCONF:
180 res = "CONF";
181 break;
182 case TWARN:
183 res = "WARN";
184 break;
185 case TINFO:
186 res = "INFO";
187 break;
188 default:
189 tst_brk(TBROK, "Invalid ttype value %i", ttype);
190 }
191
192 if (ttype & TERRNO)
193 str_errno = tst_strerrno(errno);
194
195 if (ttype & TTERRNO)
196 str_errno = tst_strerrno(TEST_ERRNO);
197
Petr Vorela7f61332017-01-24 20:47:30 +0100198 ret = snprintf(str, size, "%s:%i: ", file, lineno);
199 str += ret;
200 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100201
Petr Vorela7f61332017-01-24 20:47:30 +0100202 if (tst_color_enabled())
203 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
204 res, ANSI_COLOR_RESET);
205 else
206 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100207 str += ret;
208 size -= ret;
209
210 ret = vsnprintf(str, size, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100211 str += ret;
212 size -= ret;
213
Petr Vorela7f61332017-01-24 20:47:30 +0100214 if (str_errno) {
215 ret = snprintf(str, size, ": %s", str_errno);
216 str += ret;
217 size -= ret;
218 }
219
220 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100221
222 fputs(buf, stderr);
223}
224
225void tst_vres_(const char *file, const int lineno, int ttype,
226 const char *fmt, va_list va)
227{
228 print_result(file, lineno, ttype, fmt, va);
229
230 update_results(file, lineno, TTYPE_RESULT(ttype));
231}
232
233void tst_vbrk_(const char *file, const int lineno, int ttype,
234 const char *fmt, va_list va) __attribute__((noreturn));
235
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200236static void do_test_cleanup(void)
237{
238 if (tst_test->cleanup)
239 tst_test->cleanup();
240}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100241
242void tst_vbrk_(const char *file, const int lineno, int ttype,
243 const char *fmt, va_list va)
244{
245 print_result(file, lineno, ttype, fmt, va);
246
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200247 if (getpid() == main_pid)
248 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100249
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200250 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200251 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200252
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100253 exit(TTYPE_RESULT(ttype));
254}
255
256void tst_res_(const char *file, const int lineno, int ttype,
257 const char *fmt, ...)
258{
259 va_list va;
260
261 va_start(va, fmt);
262 tst_vres_(file, lineno, ttype, fmt, va);
263 va_end(va);
264}
265
266void tst_brk_(const char *file, const int lineno, int ttype,
267 const char *fmt, ...)
268{
269 va_list va;
270
271 va_start(va, fmt);
272 tst_vbrk_(file, lineno, ttype, fmt, va);
273 va_end(va);
274}
275
276static void check_child_status(pid_t pid, int status)
277{
278 int ret;
279
280 if (WIFSIGNALED(status)) {
281 tst_brk(TBROK, "Child (%i) killed by signal %s",
282 pid, tst_strsig(WTERMSIG(status)));
283 }
284
285 if (!(WIFEXITED(status)))
286 tst_brk(TBROK, "Child (%i) exitted abnormaly", pid);
287
288 ret = WEXITSTATUS(status);
289 switch (ret) {
290 case TPASS:
291 break;
292 case TBROK:
293 case TCONF:
294 tst_brk(ret, "Reported by child (%i)", pid);
295 default:
296 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
297 }
298}
299
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300300void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100301{
302 int status;
303 pid_t pid;
304
305 for (;;) {
306 pid = wait(&status);
307
308 if (pid > 0) {
309 check_child_status(pid, status);
310 continue;
311 }
312
313 if (errno == ECHILD)
314 break;
315
316 if (errno == EINTR)
317 continue;
318
319 tst_brk(TBROK | TERRNO, "wait() failed");
320 }
321}
322
323
324pid_t safe_fork(const char *filename, unsigned int lineno)
325{
326 pid_t pid;
327
328 if (!tst_test->forks_child)
329 tst_brk(TBROK, "test.forks_child must be set!");
330
331 fflush(stdout);
332
333 pid = fork();
334 if (pid < 0)
335 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
336
337 return pid;
338}
339
340static struct option {
341 char *optstr;
342 char *help;
343} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200344 {"h", "-h Prints this help"},
345 {"i:", "-i n Execute test n times"},
346 {"I:", "-I x Execute test for n seconds"},
347 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100348};
349
350static void print_help(void)
351{
352 unsigned int i;
353
354 for (i = 0; i < ARRAY_SIZE(options); i++)
355 fprintf(stderr, "%s\n", options[i].help);
356
357 if (!tst_test->options)
358 return;
359
360 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200361 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100362}
363
364static void check_option_collision(void)
365{
366 unsigned int i, j;
367 struct tst_option *toptions = tst_test->options;
368
369 if (!toptions)
370 return;
371
372 for (i = 0; toptions[i].optstr; i++) {
373 for (j = 0; j < ARRAY_SIZE(options); j++) {
374 if (toptions[i].optstr[0] == options[j].optstr[0]) {
375 tst_brk(TBROK, "Option collision '%s'",
376 options[j].help);
377 }
378 }
379 }
380}
381
382static unsigned int count_options(void)
383{
384 unsigned int i;
385
386 if (!tst_test->options)
387 return 0;
388
389 for (i = 0; tst_test->options[i].optstr; i++);
390
391 return i;
392}
393
394static void parse_topt(unsigned int topts_len, int opt, char *optarg)
395{
396 unsigned int i;
397 struct tst_option *toptions = tst_test->options;
398
399 for (i = 0; i < topts_len; i++) {
400 if (toptions[i].optstr[0] == opt)
401 break;
402 }
403
404 if (i >= topts_len)
405 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
406
Jan Stancekc07d36c2016-08-01 11:44:55 +0200407 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100408}
409
410/* see self_exec.c */
411#ifdef UCLINUX
412extern char *child_args;
413#endif
414
415static void parse_opts(int argc, char *argv[])
416{
417 unsigned int i, topts_len = count_options();
418 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
419 int opt;
420
421 check_option_collision();
422
423 optstr[0] = 0;
424
425 for (i = 0; i < ARRAY_SIZE(options); i++)
426 strcat(optstr, options[i].optstr);
427
428 for (i = 0; i < topts_len; i++)
429 strcat(optstr, tst_test->options[i].optstr);
430
431 while ((opt = getopt(argc, argv, optstr)) > 0) {
432 switch (opt) {
433 case '?':
434 print_help();
435 tst_brk(TBROK, "Invalid option");
436 case 'h':
437 print_help();
438 exit(0);
439 case 'i':
440 iterations = atoi(optarg);
441 break;
442 case 'I':
443 duration = atof(optarg);
444 break;
445 case 'C':
446#ifdef UCLINUX
447 child_args = optarg;
448#endif
449 break;
450 default:
451 parse_topt(topts_len, opt, optarg);
452 }
453 }
454}
455
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200456int tst_parse_int(const char *str, int *val, int min, int max)
457{
458 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300459
460 if (!str)
461 return 0;
462
463 int ret = tst_parse_long(str, &rval, min, max);
464
465 if (ret)
466 return ret;
467
468 *val = (int)rval;
469 return 0;
470}
471
472int tst_parse_long(const char *str, long *val, long min, long max)
473{
474 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200475 char *end;
476
477 if (!str)
478 return 0;
479
480 errno = 0;
481 rval = strtol(str, &end, 10);
482
483 if (str == end || *end != '\0')
484 return EINVAL;
485
486 if (errno)
487 return errno;
488
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300489 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200490 return ERANGE;
491
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300492 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200493 return 0;
494}
495
496int tst_parse_float(const char *str, float *val, float min, float max)
497{
498 double rval;
499 char *end;
500
501 if (!str)
502 return 0;
503
504 errno = 0;
505 rval = strtod(str, &end);
506
507 if (str == end || *end != '\0')
508 return EINVAL;
509
510 if (errno)
511 return errno;
512
513 if (rval > (double)max || rval < (double)min)
514 return ERANGE;
515
516 *val = (float)rval;
517 return 0;
518}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100519
Cyril Hrubisfa495172016-06-08 16:06:35 +0200520static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100521{
Xiao Yang11dfc322016-06-16 15:52:04 +0800522 if (results) {
523 printf("\nSummary:\n");
524 printf("passed %d\n", results->passed);
525 printf("failed %d\n", results->failed);
526 printf("skipped %d\n", results->skipped);
527 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100528
Xiao Yang11dfc322016-06-16 15:52:04 +0800529 if (results->failed)
530 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100531
Xiao Yang11dfc322016-06-16 15:52:04 +0800532 if (results->skipped)
533 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100534
Xiao Yang11dfc322016-06-16 15:52:04 +0800535 if (results->warnings)
536 ret |= TWARN;
537 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100538
Jan Stancek332540e2016-06-08 16:48:22 +0200539 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100540
541 exit(ret);
542}
543
544void check_kver(void)
545{
546 int v1, v2, v3;
547
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100548 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
549 tst_res(TWARN,
550 "Invalid kernel version %s, expected %%d.%%d.%%d",
551 tst_test->min_kver);
552 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100553
554 if (tst_kvercmp(v1, v2, v3) < 0) {
555 tst_brk(TCONF, "The test requires kernel %s or newer",
556 tst_test->min_kver);
557 }
558}
559
560static int results_equal(struct results *a, struct results *b)
561{
562 if (a->passed != b->passed)
563 return 0;
564
565 if (a->failed != b->failed)
566 return 0;
567
568 if (a->skipped != b->skipped)
569 return 0;
570
571 return 1;
572}
573
574static int needs_tmpdir(void)
575{
576 return tst_test->needs_tmpdir ||
577 tst_test->needs_device ||
578 tst_test->resource_files ||
579 tst_test->needs_checkpoints;
580}
581
582static void copy_resources(void)
583{
584 unsigned int i;
585
586 for (i = 0; tst_test->resource_files[i]; i++)
587 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
588}
589
590static struct tst_device tdev;
591struct tst_device *tst_device;
592
593static void do_setup(int argc, char *argv[])
594{
595 if (!tst_test)
596 tst_brk(TBROK, "No tests to run");
597
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200598 if (!tst_test->tid)
599 tst_brk(TBROK, "No tid set in test structure");
600
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100601 if (!tst_test->test && !tst_test->test_all)
602 tst_brk(TBROK, "No test function speficied");
603
604 if (tst_test->test && tst_test->test_all)
605 tst_brk(TBROK, "You can define either test() or test_all()");
606
607 if (tst_test->test && !tst_test->tcnt)
608 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
609
610 if (tst_test->test_all && tst_test->tcnt)
611 tst_brk(TBROK, "You can't define tcnt for test_all()");
612
613 if (tst_test->needs_root && geteuid() != 0)
614 tst_brk(TCONF, "Test needs to be run as root");
615
616 if (tst_test->min_kver)
617 check_kver();
618
619 parse_opts(argc, argv);
620
621 setup_ipc();
622
623 if (needs_tmpdir()) {
624 tst_tmpdir();
625 tmpdir_created = 1;
626 }
627
628 if (tst_test->needs_device) {
Li Wangd47bb552016-09-27 14:51:23 +0800629 tdev.dev = tst_acquire_device_(NULL, tst_test->device_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100630 tdev.fs_type = tst_dev_fs_type();
631
632 if (!tdev.dev)
633 tst_brk(TCONF, "Failed to acquire device");
634
635 tst_device = &tdev;
636 }
637
638 if (tst_test->resource_files)
639 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200640}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100641
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200642static void do_test_setup(void)
643{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100644 main_pid = getpid();
645
646 if (tst_test->setup)
647 tst_test->setup();
648
649 if (main_pid != getpid())
650 tst_brk(TBROK, "Runaway child in setup()!");
651}
652
653static void do_cleanup(void)
654{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100655 if (tst_test->needs_device && tdev.dev)
656 tst_release_device(tdev.dev);
657
658 if (needs_tmpdir() && tmpdir_created) {
659 /* avoid munmap() on wrong pointer in tst_rmdir() */
660 tst_futexes = NULL;
661 tst_rmdir();
662 }
Jan Stancek332540e2016-06-08 16:48:22 +0200663
664 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100665}
666
667static void run_tests(void)
668{
669 unsigned int i;
670 struct results saved_results;
671
672 if (!tst_test->test) {
673 saved_results = *results;
674 tst_test->test_all();
675
676 if (getpid() != main_pid) {
677 exit(0);
678 }
679
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300680 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100681
682 if (results_equal(&saved_results, results))
683 tst_brk(TBROK, "Test haven't reported results!");
684 return;
685 }
686
687 for (i = 0; i < tst_test->tcnt; i++) {
688 saved_results = *results;
689 tst_test->test(i);
690
691 if (getpid() != main_pid) {
692 exit(0);
693 }
694
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300695 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100696
697 if (results_equal(&saved_results, results))
698 tst_brk(TBROK, "Test %i haven't reported results!", i);
699 }
700}
701
702static unsigned long long get_time_ms(void)
703{
704 struct timeval tv;
705
706 gettimeofday(&tv, NULL);
707
708 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
709}
710
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200711static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100712{
713 unsigned int i = 0;
714 unsigned long long stop_time = 0;
715 int cont = 1;
716
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200717 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100718
719 if (duration > 0)
720 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
721
722 for (;;) {
723 cont = 0;
724
725 if (i < (unsigned int)iterations) {
726 i++;
727 cont = 1;
728 }
729
730 if (stop_time && get_time_ms() < stop_time)
731 cont = 1;
732
733 if (!cont)
734 break;
735
736 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200737
738 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100739 }
740
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200741 do_test_cleanup();
742 exit(0);
743}
744
745static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200746
747static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
748{
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200749 kill(-test_pid, SIGKILL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200750}
751
752static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
753{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200754 alarm(results->timeout);
755}
756
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200757#define SIGINT_MSG "Sending SIGKILL to test process...\n"
758
759static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
760{
761 if (test_pid > 0) {
762 if (write(2, SIGINT_MSG, sizeof(SIGINT_MSG) - 1)) {
763 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
764 }
765 kill(-test_pid, SIGKILL);
766 }
767}
768
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200769void tst_set_timeout(unsigned int timeout)
770{
771 char *mul = getenv("LTP_TIMEOUT_MUL");
772
773 results->timeout = timeout;
774
775 if (mul) {
776 float m = atof(mul);
777
778 if (m < 1)
779 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
780
781 results->timeout = results->timeout * m + 0.5;
782 }
783
784 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
785 results->timeout/3600, (results->timeout%3600)/60,
786 results->timeout % 60);
787
788 if (getpid() == lib_pid)
789 alarm(results->timeout);
790 else
791 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200792}
793
794void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
795{
796 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200797
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200798 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200799 tst_test = self;
800 TCID = tst_test->tid;
801
802 do_setup(argc, argv);
803
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200804 SAFE_SIGNAL(SIGALRM, alarm_handler);
805 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
806
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200807 if (tst_test->timeout)
808 tst_set_timeout(tst_test->timeout);
809 else
810 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200811
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200812 SAFE_SIGNAL(SIGINT, sigint_handler);
813
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200814 test_pid = fork();
815 if (test_pid < 0)
816 tst_brk(TBROK | TERRNO, "fork()");
817
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200818 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200819 SAFE_SIGNAL(SIGALRM, SIG_DFL);
820 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
821 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200822 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200823 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200824 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200825
826 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200827 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200828 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200829
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200830 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200831 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200832
833 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
834 tst_res(TINFO, "If you are running on slow machine, "
835 "try exporting LTP_TIMEOUT_MUL > 1");
836 tst_brk(TBROK, "Test killed! (timeout?)");
837 }
838
839 if (WIFSIGNALED(status))
840 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
841
Cyril Hrubisfa495172016-06-08 16:06:35 +0200842 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100843}