blob: c48d718779d8a03fb5d2fcdcf0289ffd15189674 [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
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100522 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
523 tst_res(TWARN,
524 "Invalid kernel version %s, expected %%d.%%d.%%d",
525 tst_test->min_kver);
526 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100527
528 if (tst_kvercmp(v1, v2, v3) < 0) {
529 tst_brk(TCONF, "The test requires kernel %s or newer",
530 tst_test->min_kver);
531 }
532}
533
534static int results_equal(struct results *a, struct results *b)
535{
536 if (a->passed != b->passed)
537 return 0;
538
539 if (a->failed != b->failed)
540 return 0;
541
542 if (a->skipped != b->skipped)
543 return 0;
544
545 return 1;
546}
547
548static int needs_tmpdir(void)
549{
550 return tst_test->needs_tmpdir ||
551 tst_test->needs_device ||
552 tst_test->resource_files ||
553 tst_test->needs_checkpoints;
554}
555
556static void copy_resources(void)
557{
558 unsigned int i;
559
560 for (i = 0; tst_test->resource_files[i]; i++)
561 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
562}
563
564static struct tst_device tdev;
565struct tst_device *tst_device;
566
567static void do_setup(int argc, char *argv[])
568{
569 if (!tst_test)
570 tst_brk(TBROK, "No tests to run");
571
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200572 if (!tst_test->tid)
573 tst_brk(TBROK, "No tid set in test structure");
574
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100575 if (!tst_test->test && !tst_test->test_all)
576 tst_brk(TBROK, "No test function speficied");
577
578 if (tst_test->test && tst_test->test_all)
579 tst_brk(TBROK, "You can define either test() or test_all()");
580
581 if (tst_test->test && !tst_test->tcnt)
582 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
583
584 if (tst_test->test_all && tst_test->tcnt)
585 tst_brk(TBROK, "You can't define tcnt for test_all()");
586
587 if (tst_test->needs_root && geteuid() != 0)
588 tst_brk(TCONF, "Test needs to be run as root");
589
590 if (tst_test->min_kver)
591 check_kver();
592
593 parse_opts(argc, argv);
594
595 setup_ipc();
596
597 if (needs_tmpdir()) {
598 tst_tmpdir();
599 tmpdir_created = 1;
600 }
601
602 if (tst_test->needs_device) {
Li Wangd47bb552016-09-27 14:51:23 +0800603 tdev.dev = tst_acquire_device_(NULL, tst_test->device_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100604 tdev.fs_type = tst_dev_fs_type();
605
606 if (!tdev.dev)
607 tst_brk(TCONF, "Failed to acquire device");
608
609 tst_device = &tdev;
610 }
611
612 if (tst_test->resource_files)
613 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200614}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100615
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200616static void do_test_setup(void)
617{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100618 main_pid = getpid();
619
620 if (tst_test->setup)
621 tst_test->setup();
622
623 if (main_pid != getpid())
624 tst_brk(TBROK, "Runaway child in setup()!");
625}
626
627static void do_cleanup(void)
628{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100629 if (tst_test->needs_device && tdev.dev)
630 tst_release_device(tdev.dev);
631
632 if (needs_tmpdir() && tmpdir_created) {
633 /* avoid munmap() on wrong pointer in tst_rmdir() */
634 tst_futexes = NULL;
635 tst_rmdir();
636 }
Jan Stancek332540e2016-06-08 16:48:22 +0200637
638 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100639}
640
641static void run_tests(void)
642{
643 unsigned int i;
644 struct results saved_results;
645
646 if (!tst_test->test) {
647 saved_results = *results;
648 tst_test->test_all();
649
650 if (getpid() != main_pid) {
651 exit(0);
652 }
653
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300654 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100655
656 if (results_equal(&saved_results, results))
657 tst_brk(TBROK, "Test haven't reported results!");
658 return;
659 }
660
661 for (i = 0; i < tst_test->tcnt; i++) {
662 saved_results = *results;
663 tst_test->test(i);
664
665 if (getpid() != main_pid) {
666 exit(0);
667 }
668
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300669 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100670
671 if (results_equal(&saved_results, results))
672 tst_brk(TBROK, "Test %i haven't reported results!", i);
673 }
674}
675
676static unsigned long long get_time_ms(void)
677{
678 struct timeval tv;
679
680 gettimeofday(&tv, NULL);
681
682 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
683}
684
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200685static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100686{
687 unsigned int i = 0;
688 unsigned long long stop_time = 0;
689 int cont = 1;
690
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200691 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100692
693 if (duration > 0)
694 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
695
696 for (;;) {
697 cont = 0;
698
699 if (i < (unsigned int)iterations) {
700 i++;
701 cont = 1;
702 }
703
704 if (stop_time && get_time_ms() < stop_time)
705 cont = 1;
706
707 if (!cont)
708 break;
709
710 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200711
712 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100713 }
714
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200715 do_test_cleanup();
716 exit(0);
717}
718
719static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200720
721static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
722{
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200723 kill(-test_pid, SIGKILL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200724}
725
726static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
727{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200728 alarm(results->timeout);
729}
730
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200731#define SIGINT_MSG "Sending SIGKILL to test process...\n"
732
733static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
734{
735 if (test_pid > 0) {
736 if (write(2, SIGINT_MSG, sizeof(SIGINT_MSG) - 1)) {
737 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
738 }
739 kill(-test_pid, SIGKILL);
740 }
741}
742
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200743void tst_set_timeout(unsigned int timeout)
744{
745 char *mul = getenv("LTP_TIMEOUT_MUL");
746
747 results->timeout = timeout;
748
749 if (mul) {
750 float m = atof(mul);
751
752 if (m < 1)
753 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
754
755 results->timeout = results->timeout * m + 0.5;
756 }
757
758 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
759 results->timeout/3600, (results->timeout%3600)/60,
760 results->timeout % 60);
761
762 if (getpid() == lib_pid)
763 alarm(results->timeout);
764 else
765 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200766}
767
768void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
769{
770 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200771
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200772 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200773 tst_test = self;
774 TCID = tst_test->tid;
775
776 do_setup(argc, argv);
777
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200778 SAFE_SIGNAL(SIGALRM, alarm_handler);
779 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
780
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200781 if (tst_test->timeout)
782 tst_set_timeout(tst_test->timeout);
783 else
784 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200785
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200786 SAFE_SIGNAL(SIGINT, sigint_handler);
787
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200788 test_pid = fork();
789 if (test_pid < 0)
790 tst_brk(TBROK | TERRNO, "fork()");
791
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200792 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200793 SAFE_SIGNAL(SIGALRM, SIG_DFL);
794 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
795 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200796 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200797 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200798 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200799
800 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200801 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200802 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200803
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200804 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200805 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200806
807 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
808 tst_res(TINFO, "If you are running on slow machine, "
809 "try exporting LTP_TIMEOUT_MUL > 1");
810 tst_brk(TBROK, "Test killed! (timeout?)");
811 }
812
813 if (WIFSIGNALED(status))
814 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
815
Cyril Hrubisfa495172016-06-08 16:06:35 +0200816 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100817}