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