blob: 8e24a940b1ea4bb3e1967f3258d734d30306b6c7 [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
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 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
Steven Jackson9f41dcf2016-12-21 20:09:01 +000074 if (access("/dev/shm", F_OK) == 0) {
75 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
76 tst_test->tid, getpid());
77 } else {
78 char *tmpdir;
79
80 if (!tst_tmpdir_created())
81 tst_tmpdir();
82
83 tmpdir = tst_get_tmpdir();
84 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
85 tmpdir, tst_test->tid, getpid());
86 free(tmpdir);
87 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010088
89 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
90 if (ipc_fd < 0)
91 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
92
93 SAFE_FTRUNCATE(ipc_fd, size);
94
95 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
96
97 /* Checkpoints needs to be accessible from processes started by exec() */
98 if (tst_test->needs_checkpoints)
99 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
100 else
101 SAFE_UNLINK(shm_path);
102
103 SAFE_CLOSE(ipc_fd);
104
105 if (tst_test->needs_checkpoints) {
106 tst_futexes = (char*)results + sizeof(struct results);
107 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
108 }
109}
110
111static void cleanup_ipc(void)
112{
113 size_t size = getpagesize();
114
115 if (ipc_fd > 0 && close(ipc_fd))
116 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
117
118 if (!access(shm_path, F_OK) && unlink(shm_path))
119 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
120
121 msync((void*)results, size, MS_SYNC);
122 munmap((void*)results, size);
123}
124
125void tst_reinit(void)
126{
127 const char *path = getenv("LTP_IPC_PATH");
128 size_t size = getpagesize();
129 int fd;
130 void *ptr;
131
132 if (!path)
133 tst_brk(TBROK, "LTP_IPC_PATH is not defined");
134
135 if (access(path, F_OK))
136 tst_brk(TBROK, "File %s does not exist!", path);
137
138 fd = SAFE_OPEN(path, O_RDWR);
139
140 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
141 tst_futexes = (char*)ptr + sizeof(struct results);
142 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
143
144 SAFE_CLOSE(fd);
145}
146
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100147static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100148{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100149 if (!results)
150 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100151
152 switch (ttype) {
153 case TCONF:
154 tst_atomic_inc(&results->skipped);
155 break;
156 case TPASS:
157 tst_atomic_inc(&results->passed);
158 break;
159 case TWARN:
160 tst_atomic_inc(&results->warnings);
161 break;
162 case TFAIL:
163 tst_atomic_inc(&results->failed);
164 break;
165 }
166}
167
168static void print_result(const char *file, const int lineno, int ttype,
169 const char *fmt, va_list va)
170{
171 char buf[1024];
172 char *str = buf;
173 int ret, size = sizeof(buf);
174 const char *str_errno = NULL;
175 const char *res;
176
177 switch (TTYPE_RESULT(ttype)) {
178 case TPASS:
179 res = "PASS";
180 break;
181 case TFAIL:
182 res = "FAIL";
183 break;
184 case TBROK:
185 res = "BROK";
186 break;
187 case TCONF:
188 res = "CONF";
189 break;
190 case TWARN:
191 res = "WARN";
192 break;
193 case TINFO:
194 res = "INFO";
195 break;
196 default:
197 tst_brk(TBROK, "Invalid ttype value %i", ttype);
198 }
199
200 if (ttype & TERRNO)
201 str_errno = tst_strerrno(errno);
202
203 if (ttype & TTERRNO)
204 str_errno = tst_strerrno(TEST_ERRNO);
205
Petr Vorela7f61332017-01-24 20:47:30 +0100206 ret = snprintf(str, size, "%s:%i: ", file, lineno);
207 str += ret;
208 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100209
Cyril Hrubis047c7272017-02-13 16:23:36 +0100210 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100211 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
212 res, ANSI_COLOR_RESET);
213 else
214 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100215 str += ret;
216 size -= ret;
217
218 ret = vsnprintf(str, size, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100219 str += ret;
220 size -= ret;
221
Petr Vorela7f61332017-01-24 20:47:30 +0100222 if (str_errno) {
223 ret = snprintf(str, size, ": %s", str_errno);
224 str += ret;
225 size -= ret;
226 }
227
228 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100229
230 fputs(buf, stderr);
231}
232
233void tst_vres_(const char *file, const int lineno, int ttype,
234 const char *fmt, va_list va)
235{
236 print_result(file, lineno, ttype, fmt, va);
237
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100238 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100239}
240
241void tst_vbrk_(const char *file, const int lineno, int ttype,
242 const char *fmt, va_list va) __attribute__((noreturn));
243
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200244static void do_test_cleanup(void)
245{
246 if (tst_test->cleanup)
247 tst_test->cleanup();
248}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100249
250void tst_vbrk_(const char *file, const int lineno, int ttype,
251 const char *fmt, va_list va)
252{
253 print_result(file, lineno, ttype, fmt, va);
254
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200255 if (getpid() == main_pid)
256 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100257
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200258 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200259 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200260
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100261 exit(TTYPE_RESULT(ttype));
262}
263
264void tst_res_(const char *file, const int lineno, int ttype,
265 const char *fmt, ...)
266{
267 va_list va;
268
269 va_start(va, fmt);
270 tst_vres_(file, lineno, ttype, fmt, va);
271 va_end(va);
272}
273
274void tst_brk_(const char *file, const int lineno, int ttype,
275 const char *fmt, ...)
276{
277 va_list va;
278
279 va_start(va, fmt);
280 tst_vbrk_(file, lineno, ttype, fmt, va);
281 va_end(va);
282}
283
284static void check_child_status(pid_t pid, int status)
285{
286 int ret;
287
288 if (WIFSIGNALED(status)) {
289 tst_brk(TBROK, "Child (%i) killed by signal %s",
290 pid, tst_strsig(WTERMSIG(status)));
291 }
292
293 if (!(WIFEXITED(status)))
294 tst_brk(TBROK, "Child (%i) exitted abnormaly", pid);
295
296 ret = WEXITSTATUS(status);
297 switch (ret) {
298 case TPASS:
299 break;
300 case TBROK:
301 case TCONF:
302 tst_brk(ret, "Reported by child (%i)", pid);
303 default:
304 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
305 }
306}
307
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300308void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100309{
310 int status;
311 pid_t pid;
312
313 for (;;) {
314 pid = wait(&status);
315
316 if (pid > 0) {
317 check_child_status(pid, status);
318 continue;
319 }
320
321 if (errno == ECHILD)
322 break;
323
324 if (errno == EINTR)
325 continue;
326
327 tst_brk(TBROK | TERRNO, "wait() failed");
328 }
329}
330
331
332pid_t safe_fork(const char *filename, unsigned int lineno)
333{
334 pid_t pid;
335
336 if (!tst_test->forks_child)
337 tst_brk(TBROK, "test.forks_child must be set!");
338
339 fflush(stdout);
340
341 pid = fork();
342 if (pid < 0)
343 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
344
345 return pid;
346}
347
348static struct option {
349 char *optstr;
350 char *help;
351} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200352 {"h", "-h Prints this help"},
353 {"i:", "-i n Execute test n times"},
354 {"I:", "-I x Execute test for n seconds"},
355 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100356};
357
358static void print_help(void)
359{
360 unsigned int i;
361
362 for (i = 0; i < ARRAY_SIZE(options); i++)
363 fprintf(stderr, "%s\n", options[i].help);
364
365 if (!tst_test->options)
366 return;
367
368 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200369 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100370}
371
372static void check_option_collision(void)
373{
374 unsigned int i, j;
375 struct tst_option *toptions = tst_test->options;
376
377 if (!toptions)
378 return;
379
380 for (i = 0; toptions[i].optstr; i++) {
381 for (j = 0; j < ARRAY_SIZE(options); j++) {
382 if (toptions[i].optstr[0] == options[j].optstr[0]) {
383 tst_brk(TBROK, "Option collision '%s'",
384 options[j].help);
385 }
386 }
387 }
388}
389
390static unsigned int count_options(void)
391{
392 unsigned int i;
393
394 if (!tst_test->options)
395 return 0;
396
397 for (i = 0; tst_test->options[i].optstr; i++);
398
399 return i;
400}
401
402static void parse_topt(unsigned int topts_len, int opt, char *optarg)
403{
404 unsigned int i;
405 struct tst_option *toptions = tst_test->options;
406
407 for (i = 0; i < topts_len; i++) {
408 if (toptions[i].optstr[0] == opt)
409 break;
410 }
411
412 if (i >= topts_len)
413 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
414
Jan Stancekc07d36c2016-08-01 11:44:55 +0200415 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100416}
417
418/* see self_exec.c */
419#ifdef UCLINUX
420extern char *child_args;
421#endif
422
423static void parse_opts(int argc, char *argv[])
424{
425 unsigned int i, topts_len = count_options();
426 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
427 int opt;
428
429 check_option_collision();
430
431 optstr[0] = 0;
432
433 for (i = 0; i < ARRAY_SIZE(options); i++)
434 strcat(optstr, options[i].optstr);
435
436 for (i = 0; i < topts_len; i++)
437 strcat(optstr, tst_test->options[i].optstr);
438
439 while ((opt = getopt(argc, argv, optstr)) > 0) {
440 switch (opt) {
441 case '?':
442 print_help();
443 tst_brk(TBROK, "Invalid option");
444 case 'h':
445 print_help();
446 exit(0);
447 case 'i':
448 iterations = atoi(optarg);
449 break;
450 case 'I':
451 duration = atof(optarg);
452 break;
453 case 'C':
454#ifdef UCLINUX
455 child_args = optarg;
456#endif
457 break;
458 default:
459 parse_topt(topts_len, opt, optarg);
460 }
461 }
462}
463
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200464int tst_parse_int(const char *str, int *val, int min, int max)
465{
466 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300467
468 if (!str)
469 return 0;
470
471 int ret = tst_parse_long(str, &rval, min, max);
472
473 if (ret)
474 return ret;
475
476 *val = (int)rval;
477 return 0;
478}
479
480int tst_parse_long(const char *str, long *val, long min, long max)
481{
482 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200483 char *end;
484
485 if (!str)
486 return 0;
487
488 errno = 0;
489 rval = strtol(str, &end, 10);
490
491 if (str == end || *end != '\0')
492 return EINVAL;
493
494 if (errno)
495 return errno;
496
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300497 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200498 return ERANGE;
499
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300500 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200501 return 0;
502}
503
504int tst_parse_float(const char *str, float *val, float min, float max)
505{
506 double rval;
507 char *end;
508
509 if (!str)
510 return 0;
511
512 errno = 0;
513 rval = strtod(str, &end);
514
515 if (str == end || *end != '\0')
516 return EINVAL;
517
518 if (errno)
519 return errno;
520
521 if (rval > (double)max || rval < (double)min)
522 return ERANGE;
523
524 *val = (float)rval;
525 return 0;
526}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100527
Cyril Hrubisfa495172016-06-08 16:06:35 +0200528static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100529{
Xiao Yang11dfc322016-06-16 15:52:04 +0800530 if (results) {
531 printf("\nSummary:\n");
532 printf("passed %d\n", results->passed);
533 printf("failed %d\n", results->failed);
534 printf("skipped %d\n", results->skipped);
535 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100536
Xiao Yang11dfc322016-06-16 15:52:04 +0800537 if (results->failed)
538 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100539
Xiao Yang11dfc322016-06-16 15:52:04 +0800540 if (results->skipped)
541 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100542
Xiao Yang11dfc322016-06-16 15:52:04 +0800543 if (results->warnings)
544 ret |= TWARN;
545 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100546
Jan Stancek332540e2016-06-08 16:48:22 +0200547 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100548
549 exit(ret);
550}
551
552void check_kver(void)
553{
554 int v1, v2, v3;
555
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100556 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
557 tst_res(TWARN,
558 "Invalid kernel version %s, expected %%d.%%d.%%d",
559 tst_test->min_kver);
560 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100561
562 if (tst_kvercmp(v1, v2, v3) < 0) {
563 tst_brk(TCONF, "The test requires kernel %s or newer",
564 tst_test->min_kver);
565 }
566}
567
568static int results_equal(struct results *a, struct results *b)
569{
570 if (a->passed != b->passed)
571 return 0;
572
573 if (a->failed != b->failed)
574 return 0;
575
576 if (a->skipped != b->skipped)
577 return 0;
578
579 return 1;
580}
581
582static int needs_tmpdir(void)
583{
584 return tst_test->needs_tmpdir ||
585 tst_test->needs_device ||
586 tst_test->resource_files ||
587 tst_test->needs_checkpoints;
588}
589
590static void copy_resources(void)
591{
592 unsigned int i;
593
594 for (i = 0; tst_test->resource_files[i]; i++)
595 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
596}
597
598static struct tst_device tdev;
599struct tst_device *tst_device;
600
601static void do_setup(int argc, char *argv[])
602{
603 if (!tst_test)
604 tst_brk(TBROK, "No tests to run");
605
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200606 if (!tst_test->tid)
607 tst_brk(TBROK, "No tid set in test structure");
608
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100609 if (!tst_test->test && !tst_test->test_all)
610 tst_brk(TBROK, "No test function speficied");
611
612 if (tst_test->test && tst_test->test_all)
613 tst_brk(TBROK, "You can define either test() or test_all()");
614
615 if (tst_test->test && !tst_test->tcnt)
616 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
617
618 if (tst_test->test_all && tst_test->tcnt)
619 tst_brk(TBROK, "You can't define tcnt for test_all()");
620
621 if (tst_test->needs_root && geteuid() != 0)
622 tst_brk(TCONF, "Test needs to be run as root");
623
624 if (tst_test->min_kver)
625 check_kver();
626
627 parse_opts(argc, argv);
628
629 setup_ipc();
630
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000631 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100632 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100633
634 if (tst_test->needs_device) {
Li Wangd47bb552016-09-27 14:51:23 +0800635 tdev.dev = tst_acquire_device_(NULL, tst_test->device_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100636 tdev.fs_type = tst_dev_fs_type();
637
638 if (!tdev.dev)
639 tst_brk(TCONF, "Failed to acquire device");
640
641 tst_device = &tdev;
642 }
643
644 if (tst_test->resource_files)
645 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200646}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100647
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200648static void do_test_setup(void)
649{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100650 main_pid = getpid();
651
652 if (tst_test->setup)
653 tst_test->setup();
654
655 if (main_pid != getpid())
656 tst_brk(TBROK, "Runaway child in setup()!");
657}
658
659static void do_cleanup(void)
660{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100661 if (tst_test->needs_device && tdev.dev)
662 tst_release_device(tdev.dev);
663
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000664 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100665 /* avoid munmap() on wrong pointer in tst_rmdir() */
666 tst_futexes = NULL;
667 tst_rmdir();
668 }
Jan Stancek332540e2016-06-08 16:48:22 +0200669
670 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100671}
672
673static void run_tests(void)
674{
675 unsigned int i;
676 struct results saved_results;
677
678 if (!tst_test->test) {
679 saved_results = *results;
680 tst_test->test_all();
681
682 if (getpid() != main_pid) {
683 exit(0);
684 }
685
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300686 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100687
688 if (results_equal(&saved_results, results))
689 tst_brk(TBROK, "Test haven't reported results!");
690 return;
691 }
692
693 for (i = 0; i < tst_test->tcnt; i++) {
694 saved_results = *results;
695 tst_test->test(i);
696
697 if (getpid() != main_pid) {
698 exit(0);
699 }
700
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300701 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100702
703 if (results_equal(&saved_results, results))
704 tst_brk(TBROK, "Test %i haven't reported results!", i);
705 }
706}
707
708static unsigned long long get_time_ms(void)
709{
710 struct timeval tv;
711
712 gettimeofday(&tv, NULL);
713
714 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
715}
716
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200717static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100718{
719 unsigned int i = 0;
720 unsigned long long stop_time = 0;
721 int cont = 1;
722
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200723 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100724
725 if (duration > 0)
726 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
727
728 for (;;) {
729 cont = 0;
730
731 if (i < (unsigned int)iterations) {
732 i++;
733 cont = 1;
734 }
735
736 if (stop_time && get_time_ms() < stop_time)
737 cont = 1;
738
739 if (!cont)
740 break;
741
742 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200743
744 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100745 }
746
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200747 do_test_cleanup();
748 exit(0);
749}
750
751static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200752
753static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
754{
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200755 kill(-test_pid, SIGKILL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200756}
757
758static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
759{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200760 alarm(results->timeout);
761}
762
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200763#define SIGINT_MSG "Sending SIGKILL to test process...\n"
764
765static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
766{
767 if (test_pid > 0) {
768 if (write(2, SIGINT_MSG, sizeof(SIGINT_MSG) - 1)) {
769 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
770 }
771 kill(-test_pid, SIGKILL);
772 }
773}
774
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200775void tst_set_timeout(unsigned int timeout)
776{
777 char *mul = getenv("LTP_TIMEOUT_MUL");
778
779 results->timeout = timeout;
780
781 if (mul) {
782 float m = atof(mul);
783
784 if (m < 1)
785 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
786
787 results->timeout = results->timeout * m + 0.5;
788 }
789
790 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
791 results->timeout/3600, (results->timeout%3600)/60,
792 results->timeout % 60);
793
794 if (getpid() == lib_pid)
795 alarm(results->timeout);
796 else
797 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200798}
799
800void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
801{
802 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200803
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200804 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200805 tst_test = self;
806 TCID = tst_test->tid;
807
808 do_setup(argc, argv);
809
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200810 SAFE_SIGNAL(SIGALRM, alarm_handler);
811 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
812
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200813 if (tst_test->timeout)
814 tst_set_timeout(tst_test->timeout);
815 else
816 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200817
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200818 SAFE_SIGNAL(SIGINT, sigint_handler);
819
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200820 test_pid = fork();
821 if (test_pid < 0)
822 tst_brk(TBROK | TERRNO, "fork()");
823
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200824 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200825 SAFE_SIGNAL(SIGALRM, SIG_DFL);
826 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
827 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200828 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200829 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200830 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200831
832 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200833 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200834 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200835
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200836 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200837 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200838
839 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
840 tst_res(TINFO, "If you are running on slow machine, "
841 "try exporting LTP_TIMEOUT_MUL > 1");
842 tst_brk(TBROK, "Test killed! (timeout?)");
843 }
844
845 if (WIFSIGNALED(status))
846 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
847
Cyril Hrubisfa495172016-06-08 16:06:35 +0200848 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100849}