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); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame^] | 198 | abort(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 199 | } |
| 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 Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 207 | ret = snprintf(str, size, "%s:%i: ", file, lineno); |
| 208 | str += ret; |
| 209 | size -= ret; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 210 | |
Cyril Hrubis | 047c727 | 2017-02-13 16:23:36 +0100 | [diff] [blame] | 211 | if (tst_color_enabled(STDERR_FILENO)) |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 212 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 216 | str += ret; |
| 217 | size -= ret; |
| 218 | |
| 219 | ret = vsnprintf(str, size, fmt, va); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 220 | str += ret; |
| 221 | size -= ret; |
| 222 | |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 223 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 230 | |
| 231 | fputs(buf, stderr); |
| 232 | } |
| 233 | |
| 234 | void 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 Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame] | 239 | update_results(TTYPE_RESULT(ttype)); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame^] | 243 | const char *fmt, va_list va); |
| 244 | |
| 245 | static void (*tst_brk_handler)(const char *file, const int lineno, int ttype, |
| 246 | const char *fmt, va_list va) = tst_vbrk_; |
| 247 | |
| 248 | static 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 259 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 260 | static void do_test_cleanup(void) |
| 261 | { |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame^] | 262 | tst_brk_handler = tst_cvres; |
| 263 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 264 | if (tst_test->cleanup) |
| 265 | tst_test->cleanup(); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame^] | 266 | |
| 267 | tst_brk_handler = tst_vbrk_; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 268 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 269 | |
| 270 | void 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 275 | if (getpid() == main_pid) |
| 276 | do_test_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 277 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 278 | if (getpid() == lib_pid) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 279 | do_exit(TTYPE_RESULT(ttype)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 280 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 281 | exit(TTYPE_RESULT(ttype)); |
| 282 | } |
| 283 | |
| 284 | void 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 | |
| 294 | void 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 Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame^] | 300 | tst_brk_handler(file, lineno, ttype, fmt, va); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 301 | va_end(va); |
| 302 | } |
| 303 | |
| 304 | static 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 Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 328 | void tst_reap_children(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 329 | { |
| 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 | |
| 352 | pid_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 | |
| 368 | static struct option { |
| 369 | char *optstr; |
| 370 | char *help; |
| 371 | } options[] = { |
Cyril Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 372 | {"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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | static 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 Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 389 | fprintf(stderr, "%s\n", tst_test->options[i].help); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static 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 | |
| 410 | static 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 | |
| 422 | static 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 Stancek | c07d36c | 2016-08-01 11:44:55 +0200 | [diff] [blame] | 435 | *(toptions[i].arg) = optarg ? optarg : ""; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* see self_exec.c */ |
| 439 | #ifdef UCLINUX |
| 440 | extern char *child_args; |
| 441 | #endif |
| 442 | |
| 443 | static 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 Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 484 | int tst_parse_int(const char *str, int *val, int min, int max) |
| 485 | { |
| 486 | long rval; |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 487 | |
| 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 | |
| 500 | int tst_parse_long(const char *str, long *val, long min, long max) |
| 501 | { |
| 502 | long rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 503 | 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 Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 517 | if (rval > max || rval < min) |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 518 | return ERANGE; |
| 519 | |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 520 | *val = rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | int 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 547 | |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 548 | static void do_exit(int ret) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 549 | { |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 550 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 556 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 557 | if (results->failed) |
| 558 | ret |= TFAIL; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 559 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 560 | if (results->skipped) |
| 561 | ret |= TCONF; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 562 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 563 | if (results->warnings) |
| 564 | ret |= TWARN; |
| 565 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 566 | |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 567 | do_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 568 | |
| 569 | exit(ret); |
| 570 | } |
| 571 | |
| 572 | void check_kver(void) |
| 573 | { |
| 574 | int v1, v2, v3; |
| 575 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 576 | 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 581 | |
| 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 | |
| 588 | static 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 | |
| 602 | static 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 | |
| 610 | static 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 | |
| 618 | static struct tst_device tdev; |
| 619 | struct tst_device *tst_device; |
| 620 | |
| 621 | static void do_setup(int argc, char *argv[]) |
| 622 | { |
| 623 | if (!tst_test) |
| 624 | tst_brk(TBROK, "No tests to run"); |
| 625 | |
Cyril Hrubis | f5f208b | 2016-08-04 16:10:21 +0200 | [diff] [blame] | 626 | if (!tst_test->tid) |
| 627 | tst_brk(TBROK, "No tid set in test structure"); |
| 628 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 629 | 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 Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 651 | if (needs_tmpdir() && !tst_tmpdir_created()) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 652 | tst_tmpdir(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 653 | |
| 654 | if (tst_test->needs_device) { |
Li Wang | d47bb55 | 2016-09-27 14:51:23 +0800 | [diff] [blame] | 655 | tdev.dev = tst_acquire_device_(NULL, tst_test->device_min_size); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 656 | 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 666 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 667 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 668 | static void do_test_setup(void) |
| 669 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 670 | 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 | |
| 679 | static void do_cleanup(void) |
| 680 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 681 | if (tst_test->needs_device && tdev.dev) |
| 682 | tst_release_device(tdev.dev); |
| 683 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 684 | if (tst_tmpdir_created()) { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 685 | /* avoid munmap() on wrong pointer in tst_rmdir() */ |
| 686 | tst_futexes = NULL; |
| 687 | tst_rmdir(); |
| 688 | } |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 689 | |
| 690 | cleanup_ipc(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static 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 Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 706 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 707 | |
| 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 Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 721 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 722 | |
| 723 | if (results_equal(&saved_results, results)) |
| 724 | tst_brk(TBROK, "Test %i haven't reported results!", i); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | static 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 737 | static void testrun(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 738 | { |
| 739 | unsigned int i = 0; |
| 740 | unsigned long long stop_time = 0; |
| 741 | int cont = 1; |
| 742 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 743 | do_test_setup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 744 | |
| 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 763 | |
| 764 | kill(getppid(), SIGUSR1); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 767 | do_test_cleanup(); |
| 768 | exit(0); |
| 769 | } |
| 770 | |
| 771 | static pid_t test_pid; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 772 | |
| 773 | static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 774 | { |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 775 | kill(-test_pid, SIGKILL); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 779 | { |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 780 | alarm(results->timeout); |
| 781 | } |
| 782 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 783 | #define SIGINT_MSG "Sending SIGKILL to test process...\n" |
| 784 | |
| 785 | static 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 Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 795 | void 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 Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void tst_run_tcases(int argc, char *argv[], struct tst_test *self) |
| 821 | { |
| 822 | int status; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 823 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 824 | lib_pid = getpid(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 825 | tst_test = self; |
| 826 | TCID = tst_test->tid; |
| 827 | |
| 828 | do_setup(argc, argv); |
| 829 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 830 | SAFE_SIGNAL(SIGALRM, alarm_handler); |
| 831 | SAFE_SIGNAL(SIGUSR1, heartbeat_handler); |
| 832 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 833 | if (tst_test->timeout) |
| 834 | tst_set_timeout(tst_test->timeout); |
| 835 | else |
| 836 | tst_set_timeout(300); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 837 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 838 | SAFE_SIGNAL(SIGINT, sigint_handler); |
| 839 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 840 | test_pid = fork(); |
| 841 | if (test_pid < 0) |
| 842 | tst_brk(TBROK | TERRNO, "fork()"); |
| 843 | |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 844 | if (!test_pid) { |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 845 | SAFE_SIGNAL(SIGALRM, SIG_DFL); |
| 846 | SAFE_SIGNAL(SIGUSR1, SIG_DFL); |
| 847 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 848 | SAFE_SETPGID(0, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 849 | testrun(); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 850 | } |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 851 | |
| 852 | SAFE_WAITPID(test_pid, &status, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 853 | alarm(0); |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 854 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 855 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 856 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 857 | do_exit(WEXITSTATUS(status)); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 858 | |
| 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 Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 868 | do_exit(0); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 869 | } |