blob: 12ca0518e4e34daa6f8bcca8fa324de25d460192 [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"
32
33#include "old_resource.h"
34#include "old_device.h"
35#include "old_tmpdir.h"
36
37struct tst_test *tst_test;
38
39static char tmpdir_created;
40static int iterations = 1;
41static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020042static pid_t main_pid, lib_pid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010043
44struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020045 int passed;
46 int skipped;
47 int failed;
48 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020049 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010050};
51
52static struct results *results;
53
54static int ipc_fd;
55
56extern void *tst_futexes;
57extern unsigned int tst_max_futexes;
58
59#define IPC_ENV_VAR "LTP_IPC_PATH"
60
61static char ipc_path[1024];
62const char *tst_ipc_path = ipc_path;
63char *const tst_ipc_envp[] = {ipc_path, NULL};
64
65static char shm_path[1024];
66
Jan Stancek332540e2016-06-08 16:48:22 +020067static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020068static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020069
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010070static void setup_ipc(void)
71{
72 size_t size = getpagesize();
73
74 //TODO: Fallback to tst_tmpdir() if /dev/shm does not exits?
75 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
76 tst_test->tid, getpid());
77
78 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
79 if (ipc_fd < 0)
80 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
81
82 SAFE_FTRUNCATE(ipc_fd, size);
83
84 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
85
86 /* Checkpoints needs to be accessible from processes started by exec() */
87 if (tst_test->needs_checkpoints)
88 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
89 else
90 SAFE_UNLINK(shm_path);
91
92 SAFE_CLOSE(ipc_fd);
93
94 if (tst_test->needs_checkpoints) {
95 tst_futexes = (char*)results + sizeof(struct results);
96 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
97 }
98}
99
100static void cleanup_ipc(void)
101{
102 size_t size = getpagesize();
103
104 if (ipc_fd > 0 && close(ipc_fd))
105 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
106
107 if (!access(shm_path, F_OK) && unlink(shm_path))
108 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
109
110 msync((void*)results, size, MS_SYNC);
111 munmap((void*)results, size);
112}
113
114void tst_reinit(void)
115{
116 const char *path = getenv("LTP_IPC_PATH");
117 size_t size = getpagesize();
118 int fd;
119 void *ptr;
120
121 if (!path)
122 tst_brk(TBROK, "LTP_IPC_PATH is not defined");
123
124 if (access(path, F_OK))
125 tst_brk(TBROK, "File %s does not exist!", path);
126
127 fd = SAFE_OPEN(path, O_RDWR);
128
129 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
130 tst_futexes = (char*)ptr + sizeof(struct results);
131 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
132
133 SAFE_CLOSE(fd);
134}
135
136static void update_results(const char *file, unsigned int lineno, int ttype)
137{
138 if (!results) {
139 tst_brk(TBROK,
140 "%s: %d: Results IPC not initialized!", file, lineno);
141 }
142
143 switch (ttype) {
144 case TCONF:
145 tst_atomic_inc(&results->skipped);
146 break;
147 case TPASS:
148 tst_atomic_inc(&results->passed);
149 break;
150 case TWARN:
151 tst_atomic_inc(&results->warnings);
152 break;
153 case TFAIL:
154 tst_atomic_inc(&results->failed);
155 break;
156 }
157}
158
159static void print_result(const char *file, const int lineno, int ttype,
160 const char *fmt, va_list va)
161{
162 char buf[1024];
163 char *str = buf;
164 int ret, size = sizeof(buf);
165 const char *str_errno = NULL;
166 const char *res;
167
168 switch (TTYPE_RESULT(ttype)) {
169 case TPASS:
170 res = "PASS";
171 break;
172 case TFAIL:
173 res = "FAIL";
174 break;
175 case TBROK:
176 res = "BROK";
177 break;
178 case TCONF:
179 res = "CONF";
180 break;
181 case TWARN:
182 res = "WARN";
183 break;
184 case TINFO:
185 res = "INFO";
186 break;
187 default:
188 tst_brk(TBROK, "Invalid ttype value %i", ttype);
189 }
190
191 if (ttype & TERRNO)
192 str_errno = tst_strerrno(errno);
193
194 if (ttype & TTERRNO)
195 str_errno = tst_strerrno(TEST_ERRNO);
196
197 ret = snprintf(str, size, "%s:%i: %s: ", file, lineno, res);
198
199 str += ret;
200 size -= ret;
201
202 ret = vsnprintf(str, size, fmt, va);
203
204 str += ret;
205 size -= ret;
206
207 if (str_errno)
208 snprintf(str, size, ": %s\n", str_errno);
209 else
210 snprintf(str, size, "\n");
211
212 fputs(buf, stderr);
213}
214
215void tst_vres_(const char *file, const int lineno, int ttype,
216 const char *fmt, va_list va)
217{
218 print_result(file, lineno, ttype, fmt, va);
219
220 update_results(file, lineno, TTYPE_RESULT(ttype));
221}
222
223void tst_vbrk_(const char *file, const int lineno, int ttype,
224 const char *fmt, va_list va) __attribute__((noreturn));
225
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200226static void do_test_cleanup(void)
227{
228 if (tst_test->cleanup)
229 tst_test->cleanup();
230}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100231
232void tst_vbrk_(const char *file, const int lineno, int ttype,
233 const char *fmt, va_list va)
234{
235 print_result(file, lineno, ttype, fmt, va);
236
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200237 if (getpid() == main_pid)
238 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100239
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200240 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200241 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200242
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100243 exit(TTYPE_RESULT(ttype));
244}
245
246void tst_res_(const char *file, const int lineno, int ttype,
247 const char *fmt, ...)
248{
249 va_list va;
250
251 va_start(va, fmt);
252 tst_vres_(file, lineno, ttype, fmt, va);
253 va_end(va);
254}
255
256void tst_brk_(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_vbrk_(file, lineno, ttype, fmt, va);
263 va_end(va);
264}
265
266static void check_child_status(pid_t pid, int status)
267{
268 int ret;
269
270 if (WIFSIGNALED(status)) {
271 tst_brk(TBROK, "Child (%i) killed by signal %s",
272 pid, tst_strsig(WTERMSIG(status)));
273 }
274
275 if (!(WIFEXITED(status)))
276 tst_brk(TBROK, "Child (%i) exitted abnormaly", pid);
277
278 ret = WEXITSTATUS(status);
279 switch (ret) {
280 case TPASS:
281 break;
282 case TBROK:
283 case TCONF:
284 tst_brk(ret, "Reported by child (%i)", pid);
285 default:
286 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
287 }
288}
289
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300290void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100291{
292 int status;
293 pid_t pid;
294
295 for (;;) {
296 pid = wait(&status);
297
298 if (pid > 0) {
299 check_child_status(pid, status);
300 continue;
301 }
302
303 if (errno == ECHILD)
304 break;
305
306 if (errno == EINTR)
307 continue;
308
309 tst_brk(TBROK | TERRNO, "wait() failed");
310 }
311}
312
313
314pid_t safe_fork(const char *filename, unsigned int lineno)
315{
316 pid_t pid;
317
318 if (!tst_test->forks_child)
319 tst_brk(TBROK, "test.forks_child must be set!");
320
321 fflush(stdout);
322
323 pid = fork();
324 if (pid < 0)
325 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
326
327 return pid;
328}
329
330static struct option {
331 char *optstr;
332 char *help;
333} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200334 {"h", "-h Prints this help"},
335 {"i:", "-i n Execute test n times"},
336 {"I:", "-I x Execute test for n seconds"},
337 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100338};
339
340static void print_help(void)
341{
342 unsigned int i;
343
344 for (i = 0; i < ARRAY_SIZE(options); i++)
345 fprintf(stderr, "%s\n", options[i].help);
346
347 if (!tst_test->options)
348 return;
349
350 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200351 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100352}
353
354static void check_option_collision(void)
355{
356 unsigned int i, j;
357 struct tst_option *toptions = tst_test->options;
358
359 if (!toptions)
360 return;
361
362 for (i = 0; toptions[i].optstr; i++) {
363 for (j = 0; j < ARRAY_SIZE(options); j++) {
364 if (toptions[i].optstr[0] == options[j].optstr[0]) {
365 tst_brk(TBROK, "Option collision '%s'",
366 options[j].help);
367 }
368 }
369 }
370}
371
372static unsigned int count_options(void)
373{
374 unsigned int i;
375
376 if (!tst_test->options)
377 return 0;
378
379 for (i = 0; tst_test->options[i].optstr; i++);
380
381 return i;
382}
383
384static void parse_topt(unsigned int topts_len, int opt, char *optarg)
385{
386 unsigned int i;
387 struct tst_option *toptions = tst_test->options;
388
389 for (i = 0; i < topts_len; i++) {
390 if (toptions[i].optstr[0] == opt)
391 break;
392 }
393
394 if (i >= topts_len)
395 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
396
Jan Stancekc07d36c2016-08-01 11:44:55 +0200397 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100398}
399
400/* see self_exec.c */
401#ifdef UCLINUX
402extern char *child_args;
403#endif
404
405static void parse_opts(int argc, char *argv[])
406{
407 unsigned int i, topts_len = count_options();
408 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
409 int opt;
410
411 check_option_collision();
412
413 optstr[0] = 0;
414
415 for (i = 0; i < ARRAY_SIZE(options); i++)
416 strcat(optstr, options[i].optstr);
417
418 for (i = 0; i < topts_len; i++)
419 strcat(optstr, tst_test->options[i].optstr);
420
421 while ((opt = getopt(argc, argv, optstr)) > 0) {
422 switch (opt) {
423 case '?':
424 print_help();
425 tst_brk(TBROK, "Invalid option");
426 case 'h':
427 print_help();
428 exit(0);
429 case 'i':
430 iterations = atoi(optarg);
431 break;
432 case 'I':
433 duration = atof(optarg);
434 break;
435 case 'C':
436#ifdef UCLINUX
437 child_args = optarg;
438#endif
439 break;
440 default:
441 parse_topt(topts_len, opt, optarg);
442 }
443 }
444}
445
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200446int tst_parse_int(const char *str, int *val, int min, int max)
447{
448 long rval;
449 char *end;
450
451 if (!str)
452 return 0;
453
454 errno = 0;
455 rval = strtol(str, &end, 10);
456
457 if (str == end || *end != '\0')
458 return EINVAL;
459
460 if (errno)
461 return errno;
462
463 if (rval > (long)max || rval < (long)min)
464 return ERANGE;
465
466 *val = (int)rval;
467 return 0;
468}
469
470int tst_parse_float(const char *str, float *val, float min, float max)
471{
472 double rval;
473 char *end;
474
475 if (!str)
476 return 0;
477
478 errno = 0;
479 rval = strtod(str, &end);
480
481 if (str == end || *end != '\0')
482 return EINVAL;
483
484 if (errno)
485 return errno;
486
487 if (rval > (double)max || rval < (double)min)
488 return ERANGE;
489
490 *val = (float)rval;
491 return 0;
492}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100493
Cyril Hrubisfa495172016-06-08 16:06:35 +0200494static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100495{
Xiao Yang11dfc322016-06-16 15:52:04 +0800496 if (results) {
497 printf("\nSummary:\n");
498 printf("passed %d\n", results->passed);
499 printf("failed %d\n", results->failed);
500 printf("skipped %d\n", results->skipped);
501 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100502
Xiao Yang11dfc322016-06-16 15:52:04 +0800503 if (results->failed)
504 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100505
Xiao Yang11dfc322016-06-16 15:52:04 +0800506 if (results->skipped)
507 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100508
Xiao Yang11dfc322016-06-16 15:52:04 +0800509 if (results->warnings)
510 ret |= TWARN;
511 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100512
Jan Stancek332540e2016-06-08 16:48:22 +0200513 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100514
515 exit(ret);
516}
517
518void check_kver(void)
519{
520 int v1, v2, v3;
521
522 tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3);
523
524 if (tst_kvercmp(v1, v2, v3) < 0) {
525 tst_brk(TCONF, "The test requires kernel %s or newer",
526 tst_test->min_kver);
527 }
528}
529
530static int results_equal(struct results *a, struct results *b)
531{
532 if (a->passed != b->passed)
533 return 0;
534
535 if (a->failed != b->failed)
536 return 0;
537
538 if (a->skipped != b->skipped)
539 return 0;
540
541 return 1;
542}
543
544static int needs_tmpdir(void)
545{
546 return tst_test->needs_tmpdir ||
547 tst_test->needs_device ||
548 tst_test->resource_files ||
549 tst_test->needs_checkpoints;
550}
551
552static void copy_resources(void)
553{
554 unsigned int i;
555
556 for (i = 0; tst_test->resource_files[i]; i++)
557 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
558}
559
560static struct tst_device tdev;
561struct tst_device *tst_device;
562
563static void do_setup(int argc, char *argv[])
564{
565 if (!tst_test)
566 tst_brk(TBROK, "No tests to run");
567
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200568 if (!tst_test->tid)
569 tst_brk(TBROK, "No tid set in test structure");
570
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100571 if (!tst_test->test && !tst_test->test_all)
572 tst_brk(TBROK, "No test function speficied");
573
574 if (tst_test->test && tst_test->test_all)
575 tst_brk(TBROK, "You can define either test() or test_all()");
576
577 if (tst_test->test && !tst_test->tcnt)
578 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
579
580 if (tst_test->test_all && tst_test->tcnt)
581 tst_brk(TBROK, "You can't define tcnt for test_all()");
582
583 if (tst_test->needs_root && geteuid() != 0)
584 tst_brk(TCONF, "Test needs to be run as root");
585
586 if (tst_test->min_kver)
587 check_kver();
588
589 parse_opts(argc, argv);
590
591 setup_ipc();
592
593 if (needs_tmpdir()) {
594 tst_tmpdir();
595 tmpdir_created = 1;
596 }
597
598 if (tst_test->needs_device) {
599 tdev.dev = tst_acquire_device(NULL);
600 tdev.fs_type = tst_dev_fs_type();
601
602 if (!tdev.dev)
603 tst_brk(TCONF, "Failed to acquire device");
604
605 tst_device = &tdev;
606 }
607
608 if (tst_test->resource_files)
609 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200610}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100611
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200612static void do_test_setup(void)
613{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100614 main_pid = getpid();
615
616 if (tst_test->setup)
617 tst_test->setup();
618
619 if (main_pid != getpid())
620 tst_brk(TBROK, "Runaway child in setup()!");
621}
622
623static void do_cleanup(void)
624{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100625 if (tst_test->needs_device && tdev.dev)
626 tst_release_device(tdev.dev);
627
628 if (needs_tmpdir() && tmpdir_created) {
629 /* avoid munmap() on wrong pointer in tst_rmdir() */
630 tst_futexes = NULL;
631 tst_rmdir();
632 }
Jan Stancek332540e2016-06-08 16:48:22 +0200633
634 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100635}
636
637static void run_tests(void)
638{
639 unsigned int i;
640 struct results saved_results;
641
642 if (!tst_test->test) {
643 saved_results = *results;
644 tst_test->test_all();
645
646 if (getpid() != main_pid) {
647 exit(0);
648 }
649
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300650 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100651
652 if (results_equal(&saved_results, results))
653 tst_brk(TBROK, "Test haven't reported results!");
654 return;
655 }
656
657 for (i = 0; i < tst_test->tcnt; i++) {
658 saved_results = *results;
659 tst_test->test(i);
660
661 if (getpid() != main_pid) {
662 exit(0);
663 }
664
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300665 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100666
667 if (results_equal(&saved_results, results))
668 tst_brk(TBROK, "Test %i haven't reported results!", i);
669 }
670}
671
672static unsigned long long get_time_ms(void)
673{
674 struct timeval tv;
675
676 gettimeofday(&tv, NULL);
677
678 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
679}
680
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200681static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100682{
683 unsigned int i = 0;
684 unsigned long long stop_time = 0;
685 int cont = 1;
686
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200687 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100688
689 if (duration > 0)
690 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
691
692 for (;;) {
693 cont = 0;
694
695 if (i < (unsigned int)iterations) {
696 i++;
697 cont = 1;
698 }
699
700 if (stop_time && get_time_ms() < stop_time)
701 cont = 1;
702
703 if (!cont)
704 break;
705
706 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200707
708 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100709 }
710
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200711 do_test_cleanup();
712 exit(0);
713}
714
715static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200716
717static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
718{
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200719 kill(-test_pid, SIGKILL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200720}
721
722static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
723{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200724 alarm(results->timeout);
725}
726
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200727#define SIGINT_MSG "Sending SIGKILL to test process...\n"
728
729static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
730{
731 if (test_pid > 0) {
732 if (write(2, SIGINT_MSG, sizeof(SIGINT_MSG) - 1)) {
733 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
734 }
735 kill(-test_pid, SIGKILL);
736 }
737}
738
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200739void tst_set_timeout(unsigned int timeout)
740{
741 char *mul = getenv("LTP_TIMEOUT_MUL");
742
743 results->timeout = timeout;
744
745 if (mul) {
746 float m = atof(mul);
747
748 if (m < 1)
749 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
750
751 results->timeout = results->timeout * m + 0.5;
752 }
753
754 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
755 results->timeout/3600, (results->timeout%3600)/60,
756 results->timeout % 60);
757
758 if (getpid() == lib_pid)
759 alarm(results->timeout);
760 else
761 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200762}
763
764void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
765{
766 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200767
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200768 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200769 tst_test = self;
770 TCID = tst_test->tid;
771
772 do_setup(argc, argv);
773
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200774 SAFE_SIGNAL(SIGALRM, alarm_handler);
775 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
776
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200777 if (tst_test->timeout)
778 tst_set_timeout(tst_test->timeout);
779 else
780 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200781
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200782 SAFE_SIGNAL(SIGINT, sigint_handler);
783
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200784 test_pid = fork();
785 if (test_pid < 0)
786 tst_brk(TBROK | TERRNO, "fork()");
787
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200788 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200789 SAFE_SIGNAL(SIGALRM, SIG_DFL);
790 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
791 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200792 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200793 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200794 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200795
796 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200797 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200798 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200799
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200800 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200801 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200802
803 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
804 tst_res(TINFO, "If you are running on slow machine, "
805 "try exporting LTP_TIMEOUT_MUL > 1");
806 tst_brk(TBROK, "Test killed! (timeout?)");
807 }
808
809 if (WIFSIGNALED(status))
810 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
811
Cyril Hrubisfa495172016-06-08 16:06:35 +0200812 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100813}