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" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 30 | #include "tst_device.h" |
| 31 | #include "lapi/futex.h" |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 32 | #include "tst_ansi_colors.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 33 | |
| 34 | #include "old_resource.h" |
| 35 | #include "old_device.h" |
| 36 | #include "old_tmpdir.h" |
| 37 | |
| 38 | struct tst_test *tst_test; |
| 39 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 40 | static int iterations = 1; |
| 41 | static float duration = -1; |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 42 | static pid_t main_pid, lib_pid; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 43 | |
| 44 | struct results { |
Jan Stancek | c54ca05 | 2016-04-13 12:31:13 +0200 | [diff] [blame] | 45 | int passed; |
| 46 | int skipped; |
| 47 | int failed; |
| 48 | int warnings; |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 49 | unsigned int timeout; |
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 | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 67 | static void do_cleanup(void); |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 68 | static void do_exit(int ret) __attribute__ ((noreturn)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 69 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 70 | static void setup_ipc(void) |
| 71 | { |
| 72 | size_t size = getpagesize(); |
| 73 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 74 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 88 | |
| 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 | |
| 111 | static 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 | |
| 125 | void 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 Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame^] | 147 | static void update_results(int ttype) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 148 | { |
Cyril Hrubis | d97debf | 2017-02-13 12:37:39 +0100 | [diff] [blame] | 149 | if (!results) |
| 150 | return; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 151 | |
| 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 | |
| 168 | static 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 Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 206 | ret = snprintf(str, size, "%s:%i: ", file, lineno); |
| 207 | str += ret; |
| 208 | size -= ret; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 209 | |
Cyril Hrubis | 047c727 | 2017-02-13 16:23:36 +0100 | [diff] [blame] | 210 | if (tst_color_enabled(STDERR_FILENO)) |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 211 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 215 | str += ret; |
| 216 | size -= ret; |
| 217 | |
| 218 | ret = vsnprintf(str, size, fmt, va); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 219 | str += ret; |
| 220 | size -= ret; |
| 221 | |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 222 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 229 | |
| 230 | fputs(buf, stderr); |
| 231 | } |
| 232 | |
| 233 | void 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 Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame^] | 238 | update_results(TTYPE_RESULT(ttype)); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
| 242 | const char *fmt, va_list va) __attribute__((noreturn)); |
| 243 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 244 | static void do_test_cleanup(void) |
| 245 | { |
| 246 | if (tst_test->cleanup) |
| 247 | tst_test->cleanup(); |
| 248 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 249 | |
| 250 | void 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 255 | if (getpid() == main_pid) |
| 256 | do_test_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 257 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 258 | if (getpid() == lib_pid) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 259 | do_exit(TTYPE_RESULT(ttype)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 260 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 261 | exit(TTYPE_RESULT(ttype)); |
| 262 | } |
| 263 | |
| 264 | void 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 | |
| 274 | void 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 | |
| 284 | static 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 Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 308 | void tst_reap_children(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 309 | { |
| 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 | |
| 332 | pid_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 | |
| 348 | static struct option { |
| 349 | char *optstr; |
| 350 | char *help; |
| 351 | } options[] = { |
Cyril Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 352 | {"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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | static 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 Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 369 | fprintf(stderr, "%s\n", tst_test->options[i].help); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static 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 | |
| 390 | static 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 | |
| 402 | static 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 Stancek | c07d36c | 2016-08-01 11:44:55 +0200 | [diff] [blame] | 415 | *(toptions[i].arg) = optarg ? optarg : ""; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* see self_exec.c */ |
| 419 | #ifdef UCLINUX |
| 420 | extern char *child_args; |
| 421 | #endif |
| 422 | |
| 423 | static 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 Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 464 | int tst_parse_int(const char *str, int *val, int min, int max) |
| 465 | { |
| 466 | long rval; |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 467 | |
| 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 | |
| 480 | int tst_parse_long(const char *str, long *val, long min, long max) |
| 481 | { |
| 482 | long rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 483 | 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 Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 497 | if (rval > max || rval < min) |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 498 | return ERANGE; |
| 499 | |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 500 | *val = rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | int 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 527 | |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 528 | static void do_exit(int ret) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 529 | { |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 530 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 536 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 537 | if (results->failed) |
| 538 | ret |= TFAIL; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 539 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 540 | if (results->skipped) |
| 541 | ret |= TCONF; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 542 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 543 | if (results->warnings) |
| 544 | ret |= TWARN; |
| 545 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 546 | |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 547 | do_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 548 | |
| 549 | exit(ret); |
| 550 | } |
| 551 | |
| 552 | void check_kver(void) |
| 553 | { |
| 554 | int v1, v2, v3; |
| 555 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 556 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 561 | |
| 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 | |
| 568 | static 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 | |
| 582 | static 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 | |
| 590 | static 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 | |
| 598 | static struct tst_device tdev; |
| 599 | struct tst_device *tst_device; |
| 600 | |
| 601 | static void do_setup(int argc, char *argv[]) |
| 602 | { |
| 603 | if (!tst_test) |
| 604 | tst_brk(TBROK, "No tests to run"); |
| 605 | |
Cyril Hrubis | f5f208b | 2016-08-04 16:10:21 +0200 | [diff] [blame] | 606 | if (!tst_test->tid) |
| 607 | tst_brk(TBROK, "No tid set in test structure"); |
| 608 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 609 | 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 Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 631 | if (needs_tmpdir() && !tst_tmpdir_created()) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 632 | tst_tmpdir(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 633 | |
| 634 | if (tst_test->needs_device) { |
Li Wang | d47bb55 | 2016-09-27 14:51:23 +0800 | [diff] [blame] | 635 | tdev.dev = tst_acquire_device_(NULL, tst_test->device_min_size); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 636 | 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 646 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 647 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 648 | static void do_test_setup(void) |
| 649 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 650 | 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 | |
| 659 | static void do_cleanup(void) |
| 660 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 661 | if (tst_test->needs_device && tdev.dev) |
| 662 | tst_release_device(tdev.dev); |
| 663 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 664 | if (tst_tmpdir_created()) { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 665 | /* avoid munmap() on wrong pointer in tst_rmdir() */ |
| 666 | tst_futexes = NULL; |
| 667 | tst_rmdir(); |
| 668 | } |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 669 | |
| 670 | cleanup_ipc(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static 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 Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 686 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 687 | |
| 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 Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 701 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 702 | |
| 703 | if (results_equal(&saved_results, results)) |
| 704 | tst_brk(TBROK, "Test %i haven't reported results!", i); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | static 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 717 | static void testrun(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 718 | { |
| 719 | unsigned int i = 0; |
| 720 | unsigned long long stop_time = 0; |
| 721 | int cont = 1; |
| 722 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 723 | do_test_setup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 724 | |
| 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 743 | |
| 744 | kill(getppid(), SIGUSR1); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 747 | do_test_cleanup(); |
| 748 | exit(0); |
| 749 | } |
| 750 | |
| 751 | static pid_t test_pid; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 752 | |
| 753 | static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 754 | { |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 755 | kill(-test_pid, SIGKILL); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 759 | { |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 760 | alarm(results->timeout); |
| 761 | } |
| 762 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 763 | #define SIGINT_MSG "Sending SIGKILL to test process...\n" |
| 764 | |
| 765 | static 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 Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 775 | void 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | void tst_run_tcases(int argc, char *argv[], struct tst_test *self) |
| 801 | { |
| 802 | int status; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 803 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 804 | lib_pid = getpid(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 805 | tst_test = self; |
| 806 | TCID = tst_test->tid; |
| 807 | |
| 808 | do_setup(argc, argv); |
| 809 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 810 | SAFE_SIGNAL(SIGALRM, alarm_handler); |
| 811 | SAFE_SIGNAL(SIGUSR1, heartbeat_handler); |
| 812 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 813 | if (tst_test->timeout) |
| 814 | tst_set_timeout(tst_test->timeout); |
| 815 | else |
| 816 | tst_set_timeout(300); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 817 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 818 | SAFE_SIGNAL(SIGINT, sigint_handler); |
| 819 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 820 | test_pid = fork(); |
| 821 | if (test_pid < 0) |
| 822 | tst_brk(TBROK | TERRNO, "fork()"); |
| 823 | |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 824 | if (!test_pid) { |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 825 | SAFE_SIGNAL(SIGALRM, SIG_DFL); |
| 826 | SAFE_SIGNAL(SIGUSR1, SIG_DFL); |
| 827 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 828 | SAFE_SETPGID(0, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 829 | testrun(); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 830 | } |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 831 | |
| 832 | SAFE_WAITPID(test_pid, &status, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 833 | alarm(0); |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 834 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 835 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 836 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 837 | do_exit(WEXITSTATUS(status)); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 838 | |
| 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 Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 848 | do_exit(0); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 849 | } |