blob: f19f76340d58efe0d8efe7e6321f6af7ee52335f [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);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100198 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100199 }
200
201 if (ttype & TERRNO)
202 str_errno = tst_strerrno(errno);
203
204 if (ttype & TTERRNO)
205 str_errno = tst_strerrno(TEST_ERRNO);
206
Petr Vorela7f61332017-01-24 20:47:30 +0100207 ret = snprintf(str, size, "%s:%i: ", file, lineno);
208 str += ret;
209 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100210
Cyril Hrubis047c7272017-02-13 16:23:36 +0100211 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100212 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
213 res, ANSI_COLOR_RESET);
214 else
215 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100216 str += ret;
217 size -= ret;
218
219 ret = vsnprintf(str, size, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100220 str += ret;
221 size -= ret;
222
Petr Vorela7f61332017-01-24 20:47:30 +0100223 if (str_errno) {
224 ret = snprintf(str, size, ": %s", str_errno);
225 str += ret;
226 size -= ret;
227 }
228
229 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100230
231 fputs(buf, stderr);
232}
233
234void tst_vres_(const char *file, const int lineno, int ttype,
235 const char *fmt, va_list va)
236{
237 print_result(file, lineno, ttype, fmt, va);
238
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100239 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100240}
241
242void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100243 const char *fmt, va_list va);
244
245static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
246 const char *fmt, va_list va) = tst_vbrk_;
247
248static void tst_cvres(const char *file, const int lineno, int ttype,
249 const char *fmt, va_list va)
250{
251 if (TTYPE_RESULT(ttype) == TBROK) {
252 ttype &= ~TTYPE_MASK;
253 ttype |= TWARN;
254 }
255
256 print_result(file, lineno, ttype, fmt, va);
257 update_results(TTYPE_RESULT(ttype));
258}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100259
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200260static void do_test_cleanup(void)
261{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100262 tst_brk_handler = tst_cvres;
263
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200264 if (tst_test->cleanup)
265 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100266
267 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200268}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100269
270void tst_vbrk_(const char *file, const int lineno, int ttype,
271 const char *fmt, va_list va)
272{
273 print_result(file, lineno, ttype, fmt, va);
274
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200275 if (getpid() == main_pid)
276 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100277
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200278 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200279 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200280
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100281 exit(TTYPE_RESULT(ttype));
282}
283
284void tst_res_(const char *file, const int lineno, int ttype,
285 const char *fmt, ...)
286{
287 va_list va;
288
289 va_start(va, fmt);
290 tst_vres_(file, lineno, ttype, fmt, va);
291 va_end(va);
292}
293
294void tst_brk_(const char *file, const int lineno, int ttype,
295 const char *fmt, ...)
296{
297 va_list va;
298
299 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100300 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100301 va_end(va);
302}
303
304static void check_child_status(pid_t pid, int status)
305{
306 int ret;
307
308 if (WIFSIGNALED(status)) {
309 tst_brk(TBROK, "Child (%i) killed by signal %s",
310 pid, tst_strsig(WTERMSIG(status)));
311 }
312
313 if (!(WIFEXITED(status)))
314 tst_brk(TBROK, "Child (%i) exitted abnormaly", pid);
315
316 ret = WEXITSTATUS(status);
317 switch (ret) {
318 case TPASS:
319 break;
320 case TBROK:
321 case TCONF:
322 tst_brk(ret, "Reported by child (%i)", pid);
323 default:
324 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
325 }
326}
327
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300328void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100329{
330 int status;
331 pid_t pid;
332
333 for (;;) {
334 pid = wait(&status);
335
336 if (pid > 0) {
337 check_child_status(pid, status);
338 continue;
339 }
340
341 if (errno == ECHILD)
342 break;
343
344 if (errno == EINTR)
345 continue;
346
347 tst_brk(TBROK | TERRNO, "wait() failed");
348 }
349}
350
351
352pid_t safe_fork(const char *filename, unsigned int lineno)
353{
354 pid_t pid;
355
356 if (!tst_test->forks_child)
357 tst_brk(TBROK, "test.forks_child must be set!");
358
359 fflush(stdout);
360
361 pid = fork();
362 if (pid < 0)
363 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
364
365 return pid;
366}
367
368static struct option {
369 char *optstr;
370 char *help;
371} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200372 {"h", "-h Prints this help"},
373 {"i:", "-i n Execute test n times"},
374 {"I:", "-I x Execute test for n seconds"},
375 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100376};
377
378static void print_help(void)
379{
380 unsigned int i;
381
382 for (i = 0; i < ARRAY_SIZE(options); i++)
383 fprintf(stderr, "%s\n", options[i].help);
384
385 if (!tst_test->options)
386 return;
387
388 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200389 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100390}
391
392static void check_option_collision(void)
393{
394 unsigned int i, j;
395 struct tst_option *toptions = tst_test->options;
396
397 if (!toptions)
398 return;
399
400 for (i = 0; toptions[i].optstr; i++) {
401 for (j = 0; j < ARRAY_SIZE(options); j++) {
402 if (toptions[i].optstr[0] == options[j].optstr[0]) {
403 tst_brk(TBROK, "Option collision '%s'",
404 options[j].help);
405 }
406 }
407 }
408}
409
410static unsigned int count_options(void)
411{
412 unsigned int i;
413
414 if (!tst_test->options)
415 return 0;
416
417 for (i = 0; tst_test->options[i].optstr; i++);
418
419 return i;
420}
421
422static void parse_topt(unsigned int topts_len, int opt, char *optarg)
423{
424 unsigned int i;
425 struct tst_option *toptions = tst_test->options;
426
427 for (i = 0; i < topts_len; i++) {
428 if (toptions[i].optstr[0] == opt)
429 break;
430 }
431
432 if (i >= topts_len)
433 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
434
Jan Stancekc07d36c2016-08-01 11:44:55 +0200435 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100436}
437
438/* see self_exec.c */
439#ifdef UCLINUX
440extern char *child_args;
441#endif
442
443static void parse_opts(int argc, char *argv[])
444{
445 unsigned int i, topts_len = count_options();
446 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
447 int opt;
448
449 check_option_collision();
450
451 optstr[0] = 0;
452
453 for (i = 0; i < ARRAY_SIZE(options); i++)
454 strcat(optstr, options[i].optstr);
455
456 for (i = 0; i < topts_len; i++)
457 strcat(optstr, tst_test->options[i].optstr);
458
459 while ((opt = getopt(argc, argv, optstr)) > 0) {
460 switch (opt) {
461 case '?':
462 print_help();
463 tst_brk(TBROK, "Invalid option");
464 case 'h':
465 print_help();
466 exit(0);
467 case 'i':
468 iterations = atoi(optarg);
469 break;
470 case 'I':
471 duration = atof(optarg);
472 break;
473 case 'C':
474#ifdef UCLINUX
475 child_args = optarg;
476#endif
477 break;
478 default:
479 parse_topt(topts_len, opt, optarg);
480 }
481 }
482}
483
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200484int tst_parse_int(const char *str, int *val, int min, int max)
485{
486 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300487
488 if (!str)
489 return 0;
490
491 int ret = tst_parse_long(str, &rval, min, max);
492
493 if (ret)
494 return ret;
495
496 *val = (int)rval;
497 return 0;
498}
499
500int tst_parse_long(const char *str, long *val, long min, long max)
501{
502 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200503 char *end;
504
505 if (!str)
506 return 0;
507
508 errno = 0;
509 rval = strtol(str, &end, 10);
510
511 if (str == end || *end != '\0')
512 return EINVAL;
513
514 if (errno)
515 return errno;
516
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300517 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200518 return ERANGE;
519
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300520 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200521 return 0;
522}
523
524int tst_parse_float(const char *str, float *val, float min, float max)
525{
526 double rval;
527 char *end;
528
529 if (!str)
530 return 0;
531
532 errno = 0;
533 rval = strtod(str, &end);
534
535 if (str == end || *end != '\0')
536 return EINVAL;
537
538 if (errno)
539 return errno;
540
541 if (rval > (double)max || rval < (double)min)
542 return ERANGE;
543
544 *val = (float)rval;
545 return 0;
546}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100547
Cyril Hrubisfa495172016-06-08 16:06:35 +0200548static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100549{
Xiao Yang11dfc322016-06-16 15:52:04 +0800550 if (results) {
551 printf("\nSummary:\n");
552 printf("passed %d\n", results->passed);
553 printf("failed %d\n", results->failed);
554 printf("skipped %d\n", results->skipped);
555 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100556
Xiao Yang11dfc322016-06-16 15:52:04 +0800557 if (results->failed)
558 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100559
Xiao Yang11dfc322016-06-16 15:52:04 +0800560 if (results->skipped)
561 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100562
Xiao Yang11dfc322016-06-16 15:52:04 +0800563 if (results->warnings)
564 ret |= TWARN;
565 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100566
Jan Stancek332540e2016-06-08 16:48:22 +0200567 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100568
569 exit(ret);
570}
571
572void check_kver(void)
573{
574 int v1, v2, v3;
575
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100576 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
577 tst_res(TWARN,
578 "Invalid kernel version %s, expected %%d.%%d.%%d",
579 tst_test->min_kver);
580 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100581
582 if (tst_kvercmp(v1, v2, v3) < 0) {
583 tst_brk(TCONF, "The test requires kernel %s or newer",
584 tst_test->min_kver);
585 }
586}
587
588static int results_equal(struct results *a, struct results *b)
589{
590 if (a->passed != b->passed)
591 return 0;
592
593 if (a->failed != b->failed)
594 return 0;
595
596 if (a->skipped != b->skipped)
597 return 0;
598
599 return 1;
600}
601
602static int needs_tmpdir(void)
603{
604 return tst_test->needs_tmpdir ||
605 tst_test->needs_device ||
606 tst_test->resource_files ||
607 tst_test->needs_checkpoints;
608}
609
610static void copy_resources(void)
611{
612 unsigned int i;
613
614 for (i = 0; tst_test->resource_files[i]; i++)
615 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
616}
617
618static struct tst_device tdev;
619struct tst_device *tst_device;
620
621static void do_setup(int argc, char *argv[])
622{
623 if (!tst_test)
624 tst_brk(TBROK, "No tests to run");
625
Cyril Hrubisf5f208b2016-08-04 16:10:21 +0200626 if (!tst_test->tid)
627 tst_brk(TBROK, "No tid set in test structure");
628
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100629 if (!tst_test->test && !tst_test->test_all)
630 tst_brk(TBROK, "No test function speficied");
631
632 if (tst_test->test && tst_test->test_all)
633 tst_brk(TBROK, "You can define either test() or test_all()");
634
635 if (tst_test->test && !tst_test->tcnt)
636 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
637
638 if (tst_test->test_all && tst_test->tcnt)
639 tst_brk(TBROK, "You can't define tcnt for test_all()");
640
641 if (tst_test->needs_root && geteuid() != 0)
642 tst_brk(TCONF, "Test needs to be run as root");
643
644 if (tst_test->min_kver)
645 check_kver();
646
647 parse_opts(argc, argv);
648
649 setup_ipc();
650
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000651 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100652 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100653
654 if (tst_test->needs_device) {
Li Wangd47bb552016-09-27 14:51:23 +0800655 tdev.dev = tst_acquire_device_(NULL, tst_test->device_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100656 tdev.fs_type = tst_dev_fs_type();
657
658 if (!tdev.dev)
659 tst_brk(TCONF, "Failed to acquire device");
660
661 tst_device = &tdev;
662 }
663
664 if (tst_test->resource_files)
665 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200666}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100667
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200668static void do_test_setup(void)
669{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100670 main_pid = getpid();
671
672 if (tst_test->setup)
673 tst_test->setup();
674
675 if (main_pid != getpid())
676 tst_brk(TBROK, "Runaway child in setup()!");
677}
678
679static void do_cleanup(void)
680{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100681 if (tst_test->needs_device && tdev.dev)
682 tst_release_device(tdev.dev);
683
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000684 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100685 /* avoid munmap() on wrong pointer in tst_rmdir() */
686 tst_futexes = NULL;
687 tst_rmdir();
688 }
Jan Stancek332540e2016-06-08 16:48:22 +0200689
690 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100691}
692
693static void run_tests(void)
694{
695 unsigned int i;
696 struct results saved_results;
697
698 if (!tst_test->test) {
699 saved_results = *results;
700 tst_test->test_all();
701
702 if (getpid() != main_pid) {
703 exit(0);
704 }
705
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300706 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100707
708 if (results_equal(&saved_results, results))
709 tst_brk(TBROK, "Test haven't reported results!");
710 return;
711 }
712
713 for (i = 0; i < tst_test->tcnt; i++) {
714 saved_results = *results;
715 tst_test->test(i);
716
717 if (getpid() != main_pid) {
718 exit(0);
719 }
720
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300721 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100722
723 if (results_equal(&saved_results, results))
724 tst_brk(TBROK, "Test %i haven't reported results!", i);
725 }
726}
727
728static unsigned long long get_time_ms(void)
729{
730 struct timeval tv;
731
732 gettimeofday(&tv, NULL);
733
734 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
735}
736
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200737static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100738{
739 unsigned int i = 0;
740 unsigned long long stop_time = 0;
741 int cont = 1;
742
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200743 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100744
745 if (duration > 0)
746 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
747
748 for (;;) {
749 cont = 0;
750
751 if (i < (unsigned int)iterations) {
752 i++;
753 cont = 1;
754 }
755
756 if (stop_time && get_time_ms() < stop_time)
757 cont = 1;
758
759 if (!cont)
760 break;
761
762 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200763
764 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100765 }
766
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200767 do_test_cleanup();
768 exit(0);
769}
770
771static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200772
773static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
774{
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200775 kill(-test_pid, SIGKILL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200776}
777
778static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
779{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200780 alarm(results->timeout);
781}
782
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200783#define SIGINT_MSG "Sending SIGKILL to test process...\n"
784
785static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
786{
787 if (test_pid > 0) {
788 if (write(2, SIGINT_MSG, sizeof(SIGINT_MSG) - 1)) {
789 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
790 }
791 kill(-test_pid, SIGKILL);
792 }
793}
794
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200795void tst_set_timeout(unsigned int timeout)
796{
797 char *mul = getenv("LTP_TIMEOUT_MUL");
798
799 results->timeout = timeout;
800
801 if (mul) {
802 float m = atof(mul);
803
804 if (m < 1)
805 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
806
807 results->timeout = results->timeout * m + 0.5;
808 }
809
810 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
811 results->timeout/3600, (results->timeout%3600)/60,
812 results->timeout % 60);
813
814 if (getpid() == lib_pid)
815 alarm(results->timeout);
816 else
817 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200818}
819
820void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
821{
822 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200823
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200824 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200825 tst_test = self;
826 TCID = tst_test->tid;
827
828 do_setup(argc, argv);
829
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200830 SAFE_SIGNAL(SIGALRM, alarm_handler);
831 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
832
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200833 if (tst_test->timeout)
834 tst_set_timeout(tst_test->timeout);
835 else
836 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200837
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200838 SAFE_SIGNAL(SIGINT, sigint_handler);
839
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200840 test_pid = fork();
841 if (test_pid < 0)
842 tst_brk(TBROK | TERRNO, "fork()");
843
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200844 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200845 SAFE_SIGNAL(SIGALRM, SIG_DFL);
846 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
847 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200848 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200849 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200850 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200851
852 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200853 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200854 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200855
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200856 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +0200857 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200858
859 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
860 tst_res(TINFO, "If you are running on slow machine, "
861 "try exporting LTP_TIMEOUT_MUL > 1");
862 tst_brk(TBROK, "Test killed! (timeout?)");
863 }
864
865 if (WIFSIGNALED(status))
866 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
867
Cyril Hrubisfa495172016-06-08 16:06:35 +0200868 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100869}