Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 1 | /* |
| 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" |
| 30 | #include "tst_kvercmp.h" |
| 31 | #include "tst_device.h" |
| 32 | #include "lapi/futex.h" |
| 33 | |
| 34 | #include "old_resource.h" |
| 35 | #include "old_device.h" |
| 36 | #include "old_tmpdir.h" |
| 37 | |
| 38 | struct tst_test *tst_test; |
| 39 | |
| 40 | static char tmpdir_created; |
| 41 | static int iterations = 1; |
| 42 | static float duration = -1; |
| 43 | static pid_t main_pid; |
| 44 | |
| 45 | struct results { |
Jan Stancek | c54ca05 | 2016-04-13 12:31:13 +0200 | [diff] [blame] | 46 | int passed; |
| 47 | int skipped; |
| 48 | int failed; |
| 49 | int warnings; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static struct results *results; |
| 53 | |
| 54 | static int ipc_fd; |
| 55 | |
| 56 | extern void *tst_futexes; |
| 57 | extern unsigned int tst_max_futexes; |
| 58 | |
| 59 | #define IPC_ENV_VAR "LTP_IPC_PATH" |
| 60 | |
| 61 | static char ipc_path[1024]; |
| 62 | const char *tst_ipc_path = ipc_path; |
| 63 | char *const tst_ipc_envp[] = {ipc_path, NULL}; |
| 64 | |
| 65 | static char shm_path[1024]; |
| 66 | |
| 67 | static void setup_ipc(void) |
| 68 | { |
| 69 | size_t size = getpagesize(); |
| 70 | |
| 71 | //TODO: Fallback to tst_tmpdir() if /dev/shm does not exits? |
| 72 | snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", |
| 73 | tst_test->tid, getpid()); |
| 74 | |
| 75 | ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); |
| 76 | if (ipc_fd < 0) |
| 77 | tst_brk(TBROK | TERRNO, "open(%s)", shm_path); |
| 78 | |
| 79 | SAFE_FTRUNCATE(ipc_fd, size); |
| 80 | |
| 81 | results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); |
| 82 | |
| 83 | /* Checkpoints needs to be accessible from processes started by exec() */ |
| 84 | if (tst_test->needs_checkpoints) |
| 85 | sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); |
| 86 | else |
| 87 | SAFE_UNLINK(shm_path); |
| 88 | |
| 89 | SAFE_CLOSE(ipc_fd); |
| 90 | |
| 91 | if (tst_test->needs_checkpoints) { |
| 92 | tst_futexes = (char*)results + sizeof(struct results); |
| 93 | tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void cleanup_ipc(void) |
| 98 | { |
| 99 | size_t size = getpagesize(); |
| 100 | |
| 101 | if (ipc_fd > 0 && close(ipc_fd)) |
| 102 | tst_res(TWARN | TERRNO, "close(ipc_fd) failed"); |
| 103 | |
| 104 | if (!access(shm_path, F_OK) && unlink(shm_path)) |
| 105 | tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path); |
| 106 | |
| 107 | msync((void*)results, size, MS_SYNC); |
| 108 | munmap((void*)results, size); |
| 109 | } |
| 110 | |
| 111 | void tst_reinit(void) |
| 112 | { |
| 113 | const char *path = getenv("LTP_IPC_PATH"); |
| 114 | size_t size = getpagesize(); |
| 115 | int fd; |
| 116 | void *ptr; |
| 117 | |
| 118 | if (!path) |
| 119 | tst_brk(TBROK, "LTP_IPC_PATH is not defined"); |
| 120 | |
| 121 | if (access(path, F_OK)) |
| 122 | tst_brk(TBROK, "File %s does not exist!", path); |
| 123 | |
| 124 | fd = SAFE_OPEN(path, O_RDWR); |
| 125 | |
| 126 | ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 127 | tst_futexes = (char*)ptr + sizeof(struct results); |
| 128 | tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); |
| 129 | |
| 130 | SAFE_CLOSE(fd); |
| 131 | } |
| 132 | |
| 133 | static void update_results(const char *file, unsigned int lineno, int ttype) |
| 134 | { |
| 135 | if (!results) { |
| 136 | tst_brk(TBROK, |
| 137 | "%s: %d: Results IPC not initialized!", file, lineno); |
| 138 | } |
| 139 | |
| 140 | switch (ttype) { |
| 141 | case TCONF: |
| 142 | tst_atomic_inc(&results->skipped); |
| 143 | break; |
| 144 | case TPASS: |
| 145 | tst_atomic_inc(&results->passed); |
| 146 | break; |
| 147 | case TWARN: |
| 148 | tst_atomic_inc(&results->warnings); |
| 149 | break; |
| 150 | case TFAIL: |
| 151 | tst_atomic_inc(&results->failed); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void print_result(const char *file, const int lineno, int ttype, |
| 157 | const char *fmt, va_list va) |
| 158 | { |
| 159 | char buf[1024]; |
| 160 | char *str = buf; |
| 161 | int ret, size = sizeof(buf); |
| 162 | const char *str_errno = NULL; |
| 163 | const char *res; |
| 164 | |
| 165 | switch (TTYPE_RESULT(ttype)) { |
| 166 | case TPASS: |
| 167 | res = "PASS"; |
| 168 | break; |
| 169 | case TFAIL: |
| 170 | res = "FAIL"; |
| 171 | break; |
| 172 | case TBROK: |
| 173 | res = "BROK"; |
| 174 | break; |
| 175 | case TCONF: |
| 176 | res = "CONF"; |
| 177 | break; |
| 178 | case TWARN: |
| 179 | res = "WARN"; |
| 180 | break; |
| 181 | case TINFO: |
| 182 | res = "INFO"; |
| 183 | break; |
| 184 | default: |
| 185 | tst_brk(TBROK, "Invalid ttype value %i", ttype); |
| 186 | } |
| 187 | |
| 188 | if (ttype & TERRNO) |
| 189 | str_errno = tst_strerrno(errno); |
| 190 | |
| 191 | if (ttype & TTERRNO) |
| 192 | str_errno = tst_strerrno(TEST_ERRNO); |
| 193 | |
| 194 | ret = snprintf(str, size, "%s:%i: %s: ", file, lineno, res); |
| 195 | |
| 196 | str += ret; |
| 197 | size -= ret; |
| 198 | |
| 199 | ret = vsnprintf(str, size, fmt, va); |
| 200 | |
| 201 | str += ret; |
| 202 | size -= ret; |
| 203 | |
| 204 | if (str_errno) |
| 205 | snprintf(str, size, ": %s\n", str_errno); |
| 206 | else |
| 207 | snprintf(str, size, "\n"); |
| 208 | |
| 209 | fputs(buf, stderr); |
| 210 | } |
| 211 | |
| 212 | void tst_vres_(const char *file, const int lineno, int ttype, |
| 213 | const char *fmt, va_list va) |
| 214 | { |
| 215 | print_result(file, lineno, ttype, fmt, va); |
| 216 | |
| 217 | update_results(file, lineno, TTYPE_RESULT(ttype)); |
| 218 | } |
| 219 | |
| 220 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
| 221 | const char *fmt, va_list va) __attribute__((noreturn)); |
| 222 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 223 | static void do_test_cleanup(void) |
| 224 | { |
| 225 | if (tst_test->cleanup) |
| 226 | tst_test->cleanup(); |
| 227 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 228 | |
| 229 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
| 230 | const char *fmt, va_list va) |
| 231 | { |
| 232 | print_result(file, lineno, ttype, fmt, va); |
| 233 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 234 | if (getpid() == main_pid) |
| 235 | do_test_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 236 | |
| 237 | exit(TTYPE_RESULT(ttype)); |
| 238 | } |
| 239 | |
| 240 | void tst_res_(const char *file, const int lineno, int ttype, |
| 241 | const char *fmt, ...) |
| 242 | { |
| 243 | va_list va; |
| 244 | |
| 245 | va_start(va, fmt); |
| 246 | tst_vres_(file, lineno, ttype, fmt, va); |
| 247 | va_end(va); |
| 248 | } |
| 249 | |
| 250 | void tst_brk_(const char *file, const int lineno, int ttype, |
| 251 | const char *fmt, ...) |
| 252 | { |
| 253 | va_list va; |
| 254 | |
| 255 | va_start(va, fmt); |
| 256 | tst_vbrk_(file, lineno, ttype, fmt, va); |
| 257 | va_end(va); |
| 258 | } |
| 259 | |
| 260 | static void check_child_status(pid_t pid, int status) |
| 261 | { |
| 262 | int ret; |
| 263 | |
| 264 | if (WIFSIGNALED(status)) { |
| 265 | tst_brk(TBROK, "Child (%i) killed by signal %s", |
| 266 | pid, tst_strsig(WTERMSIG(status))); |
| 267 | } |
| 268 | |
| 269 | if (!(WIFEXITED(status))) |
| 270 | tst_brk(TBROK, "Child (%i) exitted abnormaly", pid); |
| 271 | |
| 272 | ret = WEXITSTATUS(status); |
| 273 | switch (ret) { |
| 274 | case TPASS: |
| 275 | break; |
| 276 | case TBROK: |
| 277 | case TCONF: |
| 278 | tst_brk(ret, "Reported by child (%i)", pid); |
| 279 | default: |
| 280 | tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static void reap_children(void) |
| 285 | { |
| 286 | int status; |
| 287 | pid_t pid; |
| 288 | |
| 289 | for (;;) { |
| 290 | pid = wait(&status); |
| 291 | |
| 292 | if (pid > 0) { |
| 293 | check_child_status(pid, status); |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | if (errno == ECHILD) |
| 298 | break; |
| 299 | |
| 300 | if (errno == EINTR) |
| 301 | continue; |
| 302 | |
| 303 | tst_brk(TBROK | TERRNO, "wait() failed"); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | pid_t safe_fork(const char *filename, unsigned int lineno) |
| 309 | { |
| 310 | pid_t pid; |
| 311 | |
| 312 | if (!tst_test->forks_child) |
| 313 | tst_brk(TBROK, "test.forks_child must be set!"); |
| 314 | |
| 315 | fflush(stdout); |
| 316 | |
| 317 | pid = fork(); |
| 318 | if (pid < 0) |
| 319 | tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed"); |
| 320 | |
| 321 | return pid; |
| 322 | } |
| 323 | |
| 324 | static struct option { |
| 325 | char *optstr; |
| 326 | char *help; |
| 327 | } options[] = { |
| 328 | {"h", "-h Prints this help"}, |
| 329 | {"i:", "-i n Execute test n times"}, |
| 330 | {"I:", "-I x Execute test for n seconds"}, |
| 331 | {"C:", "-C ARG Run child process with ARG arguments (used internally)"}, |
| 332 | }; |
| 333 | |
| 334 | static void print_help(void) |
| 335 | { |
| 336 | unsigned int i; |
| 337 | |
| 338 | for (i = 0; i < ARRAY_SIZE(options); i++) |
| 339 | fprintf(stderr, "%s\n", options[i].help); |
| 340 | |
| 341 | if (!tst_test->options) |
| 342 | return; |
| 343 | |
| 344 | for (i = 0; tst_test->options[i].optstr; i++) |
| 345 | fprintf(stderr, "%s", tst_test->options[i].help); |
| 346 | } |
| 347 | |
| 348 | static void check_option_collision(void) |
| 349 | { |
| 350 | unsigned int i, j; |
| 351 | struct tst_option *toptions = tst_test->options; |
| 352 | |
| 353 | if (!toptions) |
| 354 | return; |
| 355 | |
| 356 | for (i = 0; toptions[i].optstr; i++) { |
| 357 | for (j = 0; j < ARRAY_SIZE(options); j++) { |
| 358 | if (toptions[i].optstr[0] == options[j].optstr[0]) { |
| 359 | tst_brk(TBROK, "Option collision '%s'", |
| 360 | options[j].help); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | static unsigned int count_options(void) |
| 367 | { |
| 368 | unsigned int i; |
| 369 | |
| 370 | if (!tst_test->options) |
| 371 | return 0; |
| 372 | |
| 373 | for (i = 0; tst_test->options[i].optstr; i++); |
| 374 | |
| 375 | return i; |
| 376 | } |
| 377 | |
| 378 | static void parse_topt(unsigned int topts_len, int opt, char *optarg) |
| 379 | { |
| 380 | unsigned int i; |
| 381 | struct tst_option *toptions = tst_test->options; |
| 382 | |
| 383 | for (i = 0; i < topts_len; i++) { |
| 384 | if (toptions[i].optstr[0] == opt) |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | if (i >= topts_len) |
| 389 | tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt); |
| 390 | |
| 391 | *(toptions[i].arg) = optarg; |
| 392 | } |
| 393 | |
| 394 | /* see self_exec.c */ |
| 395 | #ifdef UCLINUX |
| 396 | extern char *child_args; |
| 397 | #endif |
| 398 | |
| 399 | static void parse_opts(int argc, char *argv[]) |
| 400 | { |
| 401 | unsigned int i, topts_len = count_options(); |
| 402 | char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len]; |
| 403 | int opt; |
| 404 | |
| 405 | check_option_collision(); |
| 406 | |
| 407 | optstr[0] = 0; |
| 408 | |
| 409 | for (i = 0; i < ARRAY_SIZE(options); i++) |
| 410 | strcat(optstr, options[i].optstr); |
| 411 | |
| 412 | for (i = 0; i < topts_len; i++) |
| 413 | strcat(optstr, tst_test->options[i].optstr); |
| 414 | |
| 415 | while ((opt = getopt(argc, argv, optstr)) > 0) { |
| 416 | switch (opt) { |
| 417 | case '?': |
| 418 | print_help(); |
| 419 | tst_brk(TBROK, "Invalid option"); |
| 420 | case 'h': |
| 421 | print_help(); |
| 422 | exit(0); |
| 423 | case 'i': |
| 424 | iterations = atoi(optarg); |
| 425 | break; |
| 426 | case 'I': |
| 427 | duration = atof(optarg); |
| 428 | break; |
| 429 | case 'C': |
| 430 | #ifdef UCLINUX |
| 431 | child_args = optarg; |
| 432 | #endif |
| 433 | break; |
| 434 | default: |
| 435 | parse_topt(topts_len, opt, optarg); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static void do_exit(void) __attribute__ ((noreturn)); |
| 441 | |
| 442 | static void do_exit(void) |
| 443 | { |
| 444 | int ret = 0; |
| 445 | |
| 446 | printf("\nSummary:\n"); |
Jan Stancek | c54ca05 | 2016-04-13 12:31:13 +0200 | [diff] [blame] | 447 | printf("passed %d\n", results->passed); |
| 448 | printf("failed %d\n", results->failed); |
| 449 | printf("skipped %d\n", results->skipped); |
| 450 | printf("warnings %d\n", results->warnings); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 451 | |
| 452 | if (results->failed) |
| 453 | ret |= TFAIL; |
| 454 | |
| 455 | if (results->skipped) |
| 456 | ret |= TCONF; |
| 457 | |
| 458 | if (results->warnings) |
| 459 | ret |= TWARN; |
| 460 | |
| 461 | cleanup_ipc(); |
| 462 | |
| 463 | exit(ret); |
| 464 | } |
| 465 | |
| 466 | void check_kver(void) |
| 467 | { |
| 468 | int v1, v2, v3; |
| 469 | |
| 470 | tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3); |
| 471 | |
| 472 | if (tst_kvercmp(v1, v2, v3) < 0) { |
| 473 | tst_brk(TCONF, "The test requires kernel %s or newer", |
| 474 | tst_test->min_kver); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | static int results_equal(struct results *a, struct results *b) |
| 479 | { |
| 480 | if (a->passed != b->passed) |
| 481 | return 0; |
| 482 | |
| 483 | if (a->failed != b->failed) |
| 484 | return 0; |
| 485 | |
| 486 | if (a->skipped != b->skipped) |
| 487 | return 0; |
| 488 | |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | static int needs_tmpdir(void) |
| 493 | { |
| 494 | return tst_test->needs_tmpdir || |
| 495 | tst_test->needs_device || |
| 496 | tst_test->resource_files || |
| 497 | tst_test->needs_checkpoints; |
| 498 | } |
| 499 | |
| 500 | static void copy_resources(void) |
| 501 | { |
| 502 | unsigned int i; |
| 503 | |
| 504 | for (i = 0; tst_test->resource_files[i]; i++) |
| 505 | TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL); |
| 506 | } |
| 507 | |
| 508 | static struct tst_device tdev; |
| 509 | struct tst_device *tst_device; |
| 510 | |
| 511 | static void do_setup(int argc, char *argv[]) |
| 512 | { |
| 513 | if (!tst_test) |
| 514 | tst_brk(TBROK, "No tests to run"); |
| 515 | |
| 516 | if (!tst_test->test && !tst_test->test_all) |
| 517 | tst_brk(TBROK, "No test function speficied"); |
| 518 | |
| 519 | if (tst_test->test && tst_test->test_all) |
| 520 | tst_brk(TBROK, "You can define either test() or test_all()"); |
| 521 | |
| 522 | if (tst_test->test && !tst_test->tcnt) |
| 523 | tst_brk(TBROK, "Number of tests (tcnt) must not be > 0"); |
| 524 | |
| 525 | if (tst_test->test_all && tst_test->tcnt) |
| 526 | tst_brk(TBROK, "You can't define tcnt for test_all()"); |
| 527 | |
| 528 | if (tst_test->needs_root && geteuid() != 0) |
| 529 | tst_brk(TCONF, "Test needs to be run as root"); |
| 530 | |
| 531 | if (tst_test->min_kver) |
| 532 | check_kver(); |
| 533 | |
| 534 | parse_opts(argc, argv); |
| 535 | |
| 536 | setup_ipc(); |
| 537 | |
| 538 | if (needs_tmpdir()) { |
| 539 | tst_tmpdir(); |
| 540 | tmpdir_created = 1; |
| 541 | } |
| 542 | |
| 543 | if (tst_test->needs_device) { |
| 544 | tdev.dev = tst_acquire_device(NULL); |
| 545 | tdev.fs_type = tst_dev_fs_type(); |
| 546 | |
| 547 | if (!tdev.dev) |
| 548 | tst_brk(TCONF, "Failed to acquire device"); |
| 549 | |
| 550 | tst_device = &tdev; |
| 551 | } |
| 552 | |
| 553 | if (tst_test->resource_files) |
| 554 | copy_resources(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 555 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 556 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 557 | static void do_test_setup(void) |
| 558 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 559 | main_pid = getpid(); |
| 560 | |
| 561 | if (tst_test->setup) |
| 562 | tst_test->setup(); |
| 563 | |
| 564 | if (main_pid != getpid()) |
| 565 | tst_brk(TBROK, "Runaway child in setup()!"); |
| 566 | } |
| 567 | |
| 568 | static void do_cleanup(void) |
| 569 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 570 | if (tst_test->needs_device && tdev.dev) |
| 571 | tst_release_device(tdev.dev); |
| 572 | |
| 573 | if (needs_tmpdir() && tmpdir_created) { |
| 574 | /* avoid munmap() on wrong pointer in tst_rmdir() */ |
| 575 | tst_futexes = NULL; |
| 576 | tst_rmdir(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void run_tests(void) |
| 581 | { |
| 582 | unsigned int i; |
| 583 | struct results saved_results; |
| 584 | |
| 585 | if (!tst_test->test) { |
| 586 | saved_results = *results; |
| 587 | tst_test->test_all(); |
| 588 | |
| 589 | if (getpid() != main_pid) { |
| 590 | exit(0); |
| 591 | } |
| 592 | |
| 593 | reap_children(); |
| 594 | |
| 595 | if (results_equal(&saved_results, results)) |
| 596 | tst_brk(TBROK, "Test haven't reported results!"); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | for (i = 0; i < tst_test->tcnt; i++) { |
| 601 | saved_results = *results; |
| 602 | tst_test->test(i); |
| 603 | |
| 604 | if (getpid() != main_pid) { |
| 605 | exit(0); |
| 606 | } |
| 607 | |
| 608 | reap_children(); |
| 609 | |
| 610 | if (results_equal(&saved_results, results)) |
| 611 | tst_brk(TBROK, "Test %i haven't reported results!", i); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static unsigned long long get_time_ms(void) |
| 616 | { |
| 617 | struct timeval tv; |
| 618 | |
| 619 | gettimeofday(&tv, NULL); |
| 620 | |
| 621 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 622 | } |
| 623 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 624 | static void testrun(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 625 | { |
| 626 | unsigned int i = 0; |
| 627 | unsigned long long stop_time = 0; |
| 628 | int cont = 1; |
| 629 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 630 | do_test_setup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 631 | |
| 632 | if (duration > 0) |
| 633 | stop_time = get_time_ms() + (unsigned long long)(duration * 1000); |
| 634 | |
| 635 | for (;;) { |
| 636 | cont = 0; |
| 637 | |
| 638 | if (i < (unsigned int)iterations) { |
| 639 | i++; |
| 640 | cont = 1; |
| 641 | } |
| 642 | |
| 643 | if (stop_time && get_time_ms() < stop_time) |
| 644 | cont = 1; |
| 645 | |
| 646 | if (!cont) |
| 647 | break; |
| 648 | |
| 649 | run_tests(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 650 | |
| 651 | kill(getppid(), SIGUSR1); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 654 | do_test_cleanup(); |
| 655 | exit(0); |
| 656 | } |
| 657 | |
| 658 | static pid_t test_pid; |
| 659 | static unsigned int timeout = 300; |
| 660 | |
| 661 | static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 662 | { |
| 663 | kill(test_pid, SIGKILL); |
| 664 | } |
| 665 | |
| 666 | static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 667 | { |
| 668 | alarm(timeout); |
| 669 | } |
| 670 | |
| 671 | void tst_run_tcases(int argc, char *argv[], struct tst_test *self) |
| 672 | { |
| 673 | int status; |
| 674 | char *mul; |
| 675 | |
| 676 | tst_test = self; |
| 677 | TCID = tst_test->tid; |
| 678 | |
| 679 | do_setup(argc, argv); |
| 680 | |
| 681 | if (tst_test->timeout) |
| 682 | timeout = tst_test->timeout; |
| 683 | |
| 684 | mul = getenv("LTP_TIMEOUT_MUL"); |
| 685 | if (mul) { |
| 686 | float m = atof(mul); |
| 687 | |
| 688 | if (m < 1) |
| 689 | tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul); |
| 690 | |
| 691 | timeout = timeout * m + 0.5; |
| 692 | } |
| 693 | |
| 694 | tst_res(TINFO, "Timeout per run is %us", timeout); |
| 695 | |
| 696 | SAFE_SIGNAL(SIGALRM, alarm_handler); |
| 697 | SAFE_SIGNAL(SIGUSR1, heartbeat_handler); |
| 698 | |
| 699 | alarm(timeout); |
| 700 | |
| 701 | test_pid = fork(); |
| 702 | if (test_pid < 0) |
| 703 | tst_brk(TBROK | TERRNO, "fork()"); |
| 704 | |
| 705 | if (!test_pid) |
| 706 | testrun(); |
| 707 | |
| 708 | SAFE_WAITPID(test_pid, &status, 0); |
| 709 | |
| 710 | alarm(0); |
| 711 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 712 | do_cleanup(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame^] | 713 | |
| 714 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
| 715 | exit(WEXITSTATUS(status)); |
| 716 | |
| 717 | if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) { |
| 718 | tst_res(TINFO, "If you are running on slow machine, " |
| 719 | "try exporting LTP_TIMEOUT_MUL > 1"); |
| 720 | tst_brk(TBROK, "Test killed! (timeout?)"); |
| 721 | } |
| 722 | |
| 723 | if (WIFSIGNALED(status)) |
| 724 | tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status))); |
| 725 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 726 | do_exit(); |
| 727 | } |