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> |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 24 | #include <sys/mount.h> |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <sys/time.h> |
| 28 | |
| 29 | #define TST_NO_DEFAULT_MAIN |
| 30 | #include "tst_test.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 31 | #include "tst_device.h" |
| 32 | #include "lapi/futex.h" |
Steve Muckle | c20831d | 2017-09-20 13:23:06 -0700 | [diff] [blame] | 33 | #include "lapi/syscalls.h" |
Petr Vorel | 3a0ef86 | 2017-03-01 15:31:08 +0100 | [diff] [blame] | 34 | #include "tst_ansi_color.h" |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 35 | #include "tst_timer_test.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 36 | |
| 37 | #include "old_resource.h" |
| 38 | #include "old_device.h" |
| 39 | #include "old_tmpdir.h" |
| 40 | |
| 41 | struct tst_test *tst_test; |
| 42 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 43 | static int iterations = 1; |
| 44 | static float duration = -1; |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 45 | static pid_t main_pid, lib_pid; |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 46 | static int mntpoint_mounted; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 47 | |
| 48 | struct results { |
Jan Stancek | c54ca05 | 2016-04-13 12:31:13 +0200 | [diff] [blame] | 49 | int passed; |
| 50 | int skipped; |
| 51 | int failed; |
| 52 | int warnings; |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 53 | unsigned int timeout; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static struct results *results; |
| 57 | |
| 58 | static int ipc_fd; |
| 59 | |
| 60 | extern void *tst_futexes; |
| 61 | extern unsigned int tst_max_futexes; |
| 62 | |
| 63 | #define IPC_ENV_VAR "LTP_IPC_PATH" |
| 64 | |
| 65 | static char ipc_path[1024]; |
| 66 | const char *tst_ipc_path = ipc_path; |
| 67 | char *const tst_ipc_envp[] = {ipc_path, NULL}; |
| 68 | |
| 69 | static char shm_path[1024]; |
| 70 | |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 71 | static void do_cleanup(void); |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 72 | static void do_exit(int ret) __attribute__ ((noreturn)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 73 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 74 | static void setup_ipc(void) |
| 75 | { |
| 76 | size_t size = getpagesize(); |
| 77 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 78 | if (access("/dev/shm", F_OK) == 0) { |
| 79 | snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", |
| 80 | tst_test->tid, getpid()); |
| 81 | } else { |
| 82 | char *tmpdir; |
| 83 | |
| 84 | if (!tst_tmpdir_created()) |
| 85 | tst_tmpdir(); |
| 86 | |
| 87 | tmpdir = tst_get_tmpdir(); |
| 88 | snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d", |
| 89 | tmpdir, tst_test->tid, getpid()); |
| 90 | free(tmpdir); |
| 91 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 92 | |
| 93 | ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); |
| 94 | if (ipc_fd < 0) |
| 95 | tst_brk(TBROK | TERRNO, "open(%s)", shm_path); |
| 96 | |
| 97 | SAFE_FTRUNCATE(ipc_fd, size); |
| 98 | |
| 99 | results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); |
| 100 | |
| 101 | /* Checkpoints needs to be accessible from processes started by exec() */ |
| 102 | if (tst_test->needs_checkpoints) |
| 103 | sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); |
| 104 | else |
| 105 | SAFE_UNLINK(shm_path); |
| 106 | |
| 107 | SAFE_CLOSE(ipc_fd); |
| 108 | |
| 109 | if (tst_test->needs_checkpoints) { |
| 110 | tst_futexes = (char*)results + sizeof(struct results); |
| 111 | tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static void cleanup_ipc(void) |
| 116 | { |
| 117 | size_t size = getpagesize(); |
| 118 | |
| 119 | if (ipc_fd > 0 && close(ipc_fd)) |
| 120 | tst_res(TWARN | TERRNO, "close(ipc_fd) failed"); |
| 121 | |
Cyril Hrubis | 9726b90 | 2017-07-27 11:28:50 +0200 | [diff] [blame] | 122 | if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path)) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 123 | tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path); |
| 124 | |
Cyril Hrubis | 9726b90 | 2017-07-27 11:28:50 +0200 | [diff] [blame] | 125 | if (results) { |
| 126 | msync((void*)results, size, MS_SYNC); |
| 127 | munmap((void*)results, size); |
| 128 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void tst_reinit(void) |
| 132 | { |
| 133 | const char *path = getenv("LTP_IPC_PATH"); |
| 134 | size_t size = getpagesize(); |
| 135 | int fd; |
| 136 | void *ptr; |
| 137 | |
| 138 | if (!path) |
| 139 | tst_brk(TBROK, "LTP_IPC_PATH is not defined"); |
| 140 | |
| 141 | if (access(path, F_OK)) |
| 142 | tst_brk(TBROK, "File %s does not exist!", path); |
| 143 | |
| 144 | fd = SAFE_OPEN(path, O_RDWR); |
| 145 | |
| 146 | ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 147 | tst_futexes = (char*)ptr + sizeof(struct results); |
| 148 | tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); |
| 149 | |
| 150 | SAFE_CLOSE(fd); |
| 151 | } |
| 152 | |
Cyril Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame] | 153 | static void update_results(int ttype) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 154 | { |
Cyril Hrubis | d97debf | 2017-02-13 12:37:39 +0100 | [diff] [blame] | 155 | if (!results) |
| 156 | return; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 157 | |
| 158 | switch (ttype) { |
| 159 | case TCONF: |
| 160 | tst_atomic_inc(&results->skipped); |
| 161 | break; |
| 162 | case TPASS: |
| 163 | tst_atomic_inc(&results->passed); |
| 164 | break; |
| 165 | case TWARN: |
| 166 | tst_atomic_inc(&results->warnings); |
| 167 | break; |
| 168 | case TFAIL: |
| 169 | tst_atomic_inc(&results->failed); |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void print_result(const char *file, const int lineno, int ttype, |
| 175 | const char *fmt, va_list va) |
| 176 | { |
| 177 | char buf[1024]; |
| 178 | char *str = buf; |
| 179 | int ret, size = sizeof(buf); |
| 180 | const char *str_errno = NULL; |
| 181 | const char *res; |
| 182 | |
| 183 | switch (TTYPE_RESULT(ttype)) { |
| 184 | case TPASS: |
| 185 | res = "PASS"; |
| 186 | break; |
| 187 | case TFAIL: |
| 188 | res = "FAIL"; |
| 189 | break; |
| 190 | case TBROK: |
| 191 | res = "BROK"; |
| 192 | break; |
| 193 | case TCONF: |
| 194 | res = "CONF"; |
| 195 | break; |
| 196 | case TWARN: |
| 197 | res = "WARN"; |
| 198 | break; |
| 199 | case TINFO: |
| 200 | res = "INFO"; |
| 201 | break; |
| 202 | default: |
| 203 | tst_brk(TBROK, "Invalid ttype value %i", ttype); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 204 | abort(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (ttype & TERRNO) |
| 208 | str_errno = tst_strerrno(errno); |
| 209 | |
| 210 | if (ttype & TTERRNO) |
| 211 | str_errno = tst_strerrno(TEST_ERRNO); |
| 212 | |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 213 | ret = snprintf(str, size, "%s:%i: ", file, lineno); |
| 214 | str += ret; |
| 215 | size -= ret; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 216 | |
Cyril Hrubis | 047c727 | 2017-02-13 16:23:36 +0100 | [diff] [blame] | 217 | if (tst_color_enabled(STDERR_FILENO)) |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 218 | ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype), |
| 219 | res, ANSI_COLOR_RESET); |
| 220 | else |
| 221 | ret = snprintf(str, size, "%s: ", res); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 222 | str += ret; |
| 223 | size -= ret; |
| 224 | |
| 225 | ret = vsnprintf(str, size, fmt, va); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 226 | str += ret; |
| 227 | size -= ret; |
| 228 | |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 229 | if (str_errno) { |
| 230 | ret = snprintf(str, size, ": %s", str_errno); |
| 231 | str += ret; |
| 232 | size -= ret; |
| 233 | } |
| 234 | |
| 235 | snprintf(str, size, "\n"); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 236 | |
| 237 | fputs(buf, stderr); |
| 238 | } |
| 239 | |
| 240 | void tst_vres_(const char *file, const int lineno, int ttype, |
| 241 | const char *fmt, va_list va) |
| 242 | { |
| 243 | print_result(file, lineno, ttype, fmt, va); |
| 244 | |
Cyril Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame] | 245 | update_results(TTYPE_RESULT(ttype)); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 249 | const char *fmt, va_list va); |
| 250 | |
| 251 | static void (*tst_brk_handler)(const char *file, const int lineno, int ttype, |
| 252 | const char *fmt, va_list va) = tst_vbrk_; |
| 253 | |
| 254 | static void tst_cvres(const char *file, const int lineno, int ttype, |
| 255 | const char *fmt, va_list va) |
| 256 | { |
| 257 | if (TTYPE_RESULT(ttype) == TBROK) { |
| 258 | ttype &= ~TTYPE_MASK; |
| 259 | ttype |= TWARN; |
| 260 | } |
| 261 | |
| 262 | print_result(file, lineno, ttype, fmt, va); |
| 263 | update_results(TTYPE_RESULT(ttype)); |
| 264 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 265 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 266 | static void do_test_cleanup(void) |
| 267 | { |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 268 | tst_brk_handler = tst_cvres; |
| 269 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 270 | if (tst_test->cleanup) |
| 271 | tst_test->cleanup(); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 272 | |
| 273 | tst_brk_handler = tst_vbrk_; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 274 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 275 | |
| 276 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
| 277 | const char *fmt, va_list va) |
| 278 | { |
| 279 | print_result(file, lineno, ttype, fmt, va); |
| 280 | |
Steve Muckle | c20831d | 2017-09-20 13:23:06 -0700 | [diff] [blame] | 281 | /* |
| 282 | * The getpid implementation in some C library versions may cause cloned |
| 283 | * test threads to show the same pid as their parent when CLONE_VM is |
| 284 | * specified but CLONE_THREAD is not. Use direct syscall to avoid |
| 285 | * cleanup running in the child. |
| 286 | */ |
| 287 | if (syscall(SYS_getpid) == main_pid) |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 288 | do_test_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 289 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 290 | if (getpid() == lib_pid) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 291 | do_exit(TTYPE_RESULT(ttype)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 292 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 293 | exit(TTYPE_RESULT(ttype)); |
| 294 | } |
| 295 | |
| 296 | void tst_res_(const char *file, const int lineno, int ttype, |
| 297 | const char *fmt, ...) |
| 298 | { |
| 299 | va_list va; |
| 300 | |
| 301 | va_start(va, fmt); |
| 302 | tst_vres_(file, lineno, ttype, fmt, va); |
| 303 | va_end(va); |
| 304 | } |
| 305 | |
| 306 | void tst_brk_(const char *file, const int lineno, int ttype, |
| 307 | const char *fmt, ...) |
| 308 | { |
| 309 | va_list va; |
| 310 | |
| 311 | va_start(va, fmt); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 312 | tst_brk_handler(file, lineno, ttype, fmt, va); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 313 | va_end(va); |
| 314 | } |
| 315 | |
| 316 | static void check_child_status(pid_t pid, int status) |
| 317 | { |
| 318 | int ret; |
| 319 | |
| 320 | if (WIFSIGNALED(status)) { |
| 321 | tst_brk(TBROK, "Child (%i) killed by signal %s", |
| 322 | pid, tst_strsig(WTERMSIG(status))); |
| 323 | } |
| 324 | |
| 325 | if (!(WIFEXITED(status))) |
| 326 | tst_brk(TBROK, "Child (%i) exitted abnormaly", pid); |
| 327 | |
| 328 | ret = WEXITSTATUS(status); |
| 329 | switch (ret) { |
| 330 | case TPASS: |
| 331 | break; |
| 332 | case TBROK: |
| 333 | case TCONF: |
| 334 | tst_brk(ret, "Reported by child (%i)", pid); |
| 335 | default: |
| 336 | tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret); |
| 337 | } |
| 338 | } |
| 339 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 340 | void tst_reap_children(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 341 | { |
| 342 | int status; |
| 343 | pid_t pid; |
| 344 | |
| 345 | for (;;) { |
| 346 | pid = wait(&status); |
| 347 | |
| 348 | if (pid > 0) { |
| 349 | check_child_status(pid, status); |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | if (errno == ECHILD) |
| 354 | break; |
| 355 | |
| 356 | if (errno == EINTR) |
| 357 | continue; |
| 358 | |
| 359 | tst_brk(TBROK | TERRNO, "wait() failed"); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | |
| 364 | pid_t safe_fork(const char *filename, unsigned int lineno) |
| 365 | { |
| 366 | pid_t pid; |
| 367 | |
| 368 | if (!tst_test->forks_child) |
| 369 | tst_brk(TBROK, "test.forks_child must be set!"); |
| 370 | |
| 371 | fflush(stdout); |
| 372 | |
| 373 | pid = fork(); |
| 374 | if (pid < 0) |
| 375 | tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed"); |
| 376 | |
| 377 | return pid; |
| 378 | } |
| 379 | |
| 380 | static struct option { |
| 381 | char *optstr; |
| 382 | char *help; |
| 383 | } options[] = { |
Cyril Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 384 | {"h", "-h Prints this help"}, |
| 385 | {"i:", "-i n Execute test n times"}, |
| 386 | {"I:", "-I x Execute test for n seconds"}, |
| 387 | {"C:", "-C ARG Run child process with ARG arguments (used internally)"}, |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | static void print_help(void) |
| 391 | { |
| 392 | unsigned int i; |
| 393 | |
| 394 | for (i = 0; i < ARRAY_SIZE(options); i++) |
| 395 | fprintf(stderr, "%s\n", options[i].help); |
| 396 | |
| 397 | if (!tst_test->options) |
| 398 | return; |
| 399 | |
| 400 | for (i = 0; tst_test->options[i].optstr; i++) |
Cyril Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 401 | fprintf(stderr, "%s\n", tst_test->options[i].help); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static void check_option_collision(void) |
| 405 | { |
| 406 | unsigned int i, j; |
| 407 | struct tst_option *toptions = tst_test->options; |
| 408 | |
| 409 | if (!toptions) |
| 410 | return; |
| 411 | |
| 412 | for (i = 0; toptions[i].optstr; i++) { |
| 413 | for (j = 0; j < ARRAY_SIZE(options); j++) { |
| 414 | if (toptions[i].optstr[0] == options[j].optstr[0]) { |
| 415 | tst_brk(TBROK, "Option collision '%s'", |
| 416 | options[j].help); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static unsigned int count_options(void) |
| 423 | { |
| 424 | unsigned int i; |
| 425 | |
| 426 | if (!tst_test->options) |
| 427 | return 0; |
| 428 | |
| 429 | for (i = 0; tst_test->options[i].optstr; i++); |
| 430 | |
| 431 | return i; |
| 432 | } |
| 433 | |
| 434 | static void parse_topt(unsigned int topts_len, int opt, char *optarg) |
| 435 | { |
| 436 | unsigned int i; |
| 437 | struct tst_option *toptions = tst_test->options; |
| 438 | |
| 439 | for (i = 0; i < topts_len; i++) { |
| 440 | if (toptions[i].optstr[0] == opt) |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | if (i >= topts_len) |
| 445 | tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt); |
| 446 | |
Jan Stancek | c07d36c | 2016-08-01 11:44:55 +0200 | [diff] [blame] | 447 | *(toptions[i].arg) = optarg ? optarg : ""; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* see self_exec.c */ |
| 451 | #ifdef UCLINUX |
| 452 | extern char *child_args; |
| 453 | #endif |
| 454 | |
| 455 | static void parse_opts(int argc, char *argv[]) |
| 456 | { |
| 457 | unsigned int i, topts_len = count_options(); |
| 458 | char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len]; |
| 459 | int opt; |
| 460 | |
| 461 | check_option_collision(); |
| 462 | |
| 463 | optstr[0] = 0; |
| 464 | |
| 465 | for (i = 0; i < ARRAY_SIZE(options); i++) |
| 466 | strcat(optstr, options[i].optstr); |
| 467 | |
| 468 | for (i = 0; i < topts_len; i++) |
| 469 | strcat(optstr, tst_test->options[i].optstr); |
| 470 | |
| 471 | while ((opt = getopt(argc, argv, optstr)) > 0) { |
| 472 | switch (opt) { |
| 473 | case '?': |
| 474 | print_help(); |
| 475 | tst_brk(TBROK, "Invalid option"); |
| 476 | case 'h': |
| 477 | print_help(); |
| 478 | exit(0); |
| 479 | case 'i': |
| 480 | iterations = atoi(optarg); |
| 481 | break; |
| 482 | case 'I': |
| 483 | duration = atof(optarg); |
| 484 | break; |
| 485 | case 'C': |
| 486 | #ifdef UCLINUX |
| 487 | child_args = optarg; |
| 488 | #endif |
| 489 | break; |
| 490 | default: |
| 491 | parse_topt(topts_len, opt, optarg); |
| 492 | } |
| 493 | } |
Cyril Hrubis | 9dc07e0 | 2017-10-02 11:11:04 +0200 | [diff] [blame^] | 494 | |
| 495 | if (optind < argc) |
| 496 | tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 499 | int tst_parse_int(const char *str, int *val, int min, int max) |
| 500 | { |
| 501 | long rval; |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 502 | |
| 503 | if (!str) |
| 504 | return 0; |
| 505 | |
| 506 | int ret = tst_parse_long(str, &rval, min, max); |
| 507 | |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | |
| 511 | *val = (int)rval; |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | int tst_parse_long(const char *str, long *val, long min, long max) |
| 516 | { |
| 517 | long rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 518 | char *end; |
| 519 | |
| 520 | if (!str) |
| 521 | return 0; |
| 522 | |
| 523 | errno = 0; |
| 524 | rval = strtol(str, &end, 10); |
| 525 | |
| 526 | if (str == end || *end != '\0') |
| 527 | return EINVAL; |
| 528 | |
| 529 | if (errno) |
| 530 | return errno; |
| 531 | |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 532 | if (rval > max || rval < min) |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 533 | return ERANGE; |
| 534 | |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 535 | *val = rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int tst_parse_float(const char *str, float *val, float min, float max) |
| 540 | { |
| 541 | double rval; |
| 542 | char *end; |
| 543 | |
| 544 | if (!str) |
| 545 | return 0; |
| 546 | |
| 547 | errno = 0; |
| 548 | rval = strtod(str, &end); |
| 549 | |
| 550 | if (str == end || *end != '\0') |
| 551 | return EINVAL; |
| 552 | |
| 553 | if (errno) |
| 554 | return errno; |
| 555 | |
| 556 | if (rval > (double)max || rval < (double)min) |
| 557 | return ERANGE; |
| 558 | |
| 559 | *val = (float)rval; |
| 560 | return 0; |
| 561 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 562 | |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 563 | static void do_exit(int ret) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 564 | { |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 565 | if (results) { |
| 566 | printf("\nSummary:\n"); |
| 567 | printf("passed %d\n", results->passed); |
| 568 | printf("failed %d\n", results->failed); |
| 569 | printf("skipped %d\n", results->skipped); |
| 570 | printf("warnings %d\n", results->warnings); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 571 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 572 | if (results->failed) |
| 573 | ret |= TFAIL; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 574 | |
Cyril Hrubis | 5390d6e | 2017-09-07 15:47:22 +0200 | [diff] [blame] | 575 | if (results->skipped && !results->passed) |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 576 | ret |= TCONF; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 577 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 578 | if (results->warnings) |
| 579 | ret |= TWARN; |
| 580 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 581 | |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 582 | do_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 583 | |
| 584 | exit(ret); |
| 585 | } |
| 586 | |
| 587 | void check_kver(void) |
| 588 | { |
| 589 | int v1, v2, v3; |
| 590 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 591 | if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) { |
| 592 | tst_res(TWARN, |
| 593 | "Invalid kernel version %s, expected %%d.%%d.%%d", |
| 594 | tst_test->min_kver); |
| 595 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 596 | |
| 597 | if (tst_kvercmp(v1, v2, v3) < 0) { |
| 598 | tst_brk(TCONF, "The test requires kernel %s or newer", |
| 599 | tst_test->min_kver); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | static int results_equal(struct results *a, struct results *b) |
| 604 | { |
| 605 | if (a->passed != b->passed) |
| 606 | return 0; |
| 607 | |
| 608 | if (a->failed != b->failed) |
| 609 | return 0; |
| 610 | |
| 611 | if (a->skipped != b->skipped) |
| 612 | return 0; |
| 613 | |
| 614 | return 1; |
| 615 | } |
| 616 | |
| 617 | static int needs_tmpdir(void) |
| 618 | { |
| 619 | return tst_test->needs_tmpdir || |
| 620 | tst_test->needs_device || |
| 621 | tst_test->resource_files || |
| 622 | tst_test->needs_checkpoints; |
| 623 | } |
| 624 | |
| 625 | static void copy_resources(void) |
| 626 | { |
| 627 | unsigned int i; |
| 628 | |
| 629 | for (i = 0; tst_test->resource_files[i]; i++) |
| 630 | TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL); |
| 631 | } |
| 632 | |
Cyril Hrubis | a5bf525 | 2017-03-14 15:25:29 +0800 | [diff] [blame] | 633 | static const char *get_tid(char *argv[]) |
| 634 | { |
| 635 | char *p; |
| 636 | |
| 637 | if (!argv[0] || !argv[0][0]) { |
| 638 | tst_res(TINFO, "argv[0] is empty!"); |
| 639 | return "ltp_empty_argv"; |
| 640 | } |
| 641 | |
| 642 | p = strrchr(argv[0], '/'); |
| 643 | if (p) |
| 644 | return p+1; |
| 645 | |
| 646 | return argv[0]; |
| 647 | } |
| 648 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 649 | static struct tst_device tdev; |
| 650 | struct tst_device *tst_device; |
| 651 | |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 652 | static void assert_test_fn(void) |
| 653 | { |
| 654 | int cnt = 0; |
| 655 | |
| 656 | if (tst_test->test) |
| 657 | cnt++; |
| 658 | |
| 659 | if (tst_test->test_all) |
| 660 | cnt++; |
| 661 | |
| 662 | if (tst_test->sample) |
| 663 | cnt++; |
| 664 | |
| 665 | if (!cnt) |
| 666 | tst_brk(TBROK, "No test function speficied"); |
| 667 | |
| 668 | if (cnt != 1) |
| 669 | tst_brk(TBROK, "You can define only one test function"); |
| 670 | |
| 671 | if (tst_test->test && !tst_test->tcnt) |
| 672 | tst_brk(TBROK, "Number of tests (tcnt) must not be > 0"); |
| 673 | |
| 674 | if (!tst_test->test && tst_test->tcnt) |
| 675 | tst_brk(TBROK, "You can define tcnt only for test()"); |
| 676 | } |
| 677 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 678 | static void do_setup(int argc, char *argv[]) |
| 679 | { |
| 680 | if (!tst_test) |
| 681 | tst_brk(TBROK, "No tests to run"); |
| 682 | |
Cyril Hrubis | f706a2f | 2017-08-01 17:31:30 +0200 | [diff] [blame] | 683 | if (tst_test->tconf_msg) |
| 684 | tst_brk(TCONF, "%s", tst_test->tconf_msg); |
| 685 | |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 686 | assert_test_fn(); |
| 687 | |
| 688 | if (tst_test->sample) |
| 689 | tst_test = tst_timer_test_setup(tst_test); |
| 690 | |
Cyril Hrubis | f5f208b | 2016-08-04 16:10:21 +0200 | [diff] [blame] | 691 | if (!tst_test->tid) |
Cyril Hrubis | a5bf525 | 2017-03-14 15:25:29 +0800 | [diff] [blame] | 692 | tst_test->tid = get_tid(argv); |
Cyril Hrubis | f5f208b | 2016-08-04 16:10:21 +0200 | [diff] [blame] | 693 | |
Cyril Hrubis | 88c220d | 2017-07-27 11:16:39 +0200 | [diff] [blame] | 694 | parse_opts(argc, argv); |
| 695 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 696 | if (tst_test->needs_root && geteuid() != 0) |
| 697 | tst_brk(TCONF, "Test needs to be run as root"); |
| 698 | |
| 699 | if (tst_test->min_kver) |
| 700 | check_kver(); |
| 701 | |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 702 | if (tst_test->format_device) |
| 703 | tst_test->needs_device = 1; |
| 704 | |
| 705 | if (tst_test->mount_device) { |
| 706 | tst_test->needs_device = 1; |
| 707 | tst_test->format_device = 1; |
| 708 | } |
| 709 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 710 | setup_ipc(); |
| 711 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 712 | if (needs_tmpdir() && !tst_tmpdir_created()) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 713 | tst_tmpdir(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 714 | |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 715 | if (tst_test->mntpoint) |
| 716 | SAFE_MKDIR(tst_test->mntpoint, 0777); |
| 717 | |
| 718 | if ((tst_test->needs_rofs || tst_test->mount_device) && |
| 719 | !tst_test->mntpoint) { |
| 720 | tst_brk(TBROK, "tst_test->mntpoint must be set!"); |
| 721 | } |
| 722 | |
| 723 | if (tst_test->needs_rofs) { |
| 724 | /* If we failed to mount read-only tmpfs. Fallback to |
| 725 | * using a device with empty read-only filesystem. |
| 726 | */ |
| 727 | if (mount(NULL, tst_test->mntpoint, "tmpfs", MS_RDONLY, NULL)) { |
| 728 | tst_res(TINFO | TERRNO, "Can't mount tmpfs read-only" |
| 729 | " at %s, setting up a device instead\n", |
| 730 | tst_test->mntpoint); |
| 731 | tst_test->mount_device = 1; |
| 732 | tst_test->needs_device = 1; |
| 733 | tst_test->format_device = 1; |
| 734 | tst_test->mnt_flags = MS_RDONLY; |
| 735 | } else { |
| 736 | mntpoint_mounted = 1; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (tst_test->needs_device && !mntpoint_mounted) { |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 741 | tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 742 | |
| 743 | if (!tdev.dev) |
| 744 | tst_brk(TCONF, "Failed to acquire device"); |
| 745 | |
| 746 | tst_device = &tdev; |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 747 | |
| 748 | if (tst_test->dev_fs_type) |
| 749 | tdev.fs_type = tst_test->dev_fs_type; |
| 750 | else |
| 751 | tdev.fs_type = tst_dev_fs_type(); |
| 752 | |
| 753 | if (tst_test->format_device) { |
| 754 | SAFE_MKFS(tdev.dev, tdev.fs_type, |
| 755 | tst_test->dev_fs_opts, |
| 756 | tst_test->dev_extra_opt); |
| 757 | } |
| 758 | |
| 759 | if (tst_test->mount_device) { |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 760 | SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type, |
| 761 | tst_test->mnt_flags, tst_test->mnt_data); |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 762 | mntpoint_mounted = 1; |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 763 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | if (tst_test->resource_files) |
| 767 | copy_resources(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 768 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 769 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 770 | static void do_test_setup(void) |
| 771 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 772 | main_pid = getpid(); |
| 773 | |
| 774 | if (tst_test->setup) |
| 775 | tst_test->setup(); |
| 776 | |
| 777 | if (main_pid != getpid()) |
| 778 | tst_brk(TBROK, "Runaway child in setup()!"); |
| 779 | } |
| 780 | |
| 781 | static void do_cleanup(void) |
| 782 | { |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 783 | if (mntpoint_mounted) |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 784 | tst_umount(tst_test->mntpoint); |
| 785 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 786 | if (tst_test->needs_device && tdev.dev) |
| 787 | tst_release_device(tdev.dev); |
| 788 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 789 | if (tst_tmpdir_created()) { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 790 | /* avoid munmap() on wrong pointer in tst_rmdir() */ |
| 791 | tst_futexes = NULL; |
| 792 | tst_rmdir(); |
| 793 | } |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 794 | |
| 795 | cleanup_ipc(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static void run_tests(void) |
| 799 | { |
| 800 | unsigned int i; |
| 801 | struct results saved_results; |
| 802 | |
| 803 | if (!tst_test->test) { |
| 804 | saved_results = *results; |
| 805 | tst_test->test_all(); |
| 806 | |
| 807 | if (getpid() != main_pid) { |
| 808 | exit(0); |
| 809 | } |
| 810 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 811 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 812 | |
| 813 | if (results_equal(&saved_results, results)) |
| 814 | tst_brk(TBROK, "Test haven't reported results!"); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | for (i = 0; i < tst_test->tcnt; i++) { |
| 819 | saved_results = *results; |
| 820 | tst_test->test(i); |
| 821 | |
| 822 | if (getpid() != main_pid) { |
| 823 | exit(0); |
| 824 | } |
| 825 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 826 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 827 | |
| 828 | if (results_equal(&saved_results, results)) |
| 829 | tst_brk(TBROK, "Test %i haven't reported results!", i); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | static unsigned long long get_time_ms(void) |
| 834 | { |
| 835 | struct timeval tv; |
| 836 | |
| 837 | gettimeofday(&tv, NULL); |
| 838 | |
| 839 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 840 | } |
| 841 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 842 | static void testrun(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 843 | { |
| 844 | unsigned int i = 0; |
| 845 | unsigned long long stop_time = 0; |
| 846 | int cont = 1; |
| 847 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 848 | do_test_setup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 849 | |
| 850 | if (duration > 0) |
| 851 | stop_time = get_time_ms() + (unsigned long long)(duration * 1000); |
| 852 | |
| 853 | for (;;) { |
| 854 | cont = 0; |
| 855 | |
| 856 | if (i < (unsigned int)iterations) { |
| 857 | i++; |
| 858 | cont = 1; |
| 859 | } |
| 860 | |
| 861 | if (stop_time && get_time_ms() < stop_time) |
| 862 | cont = 1; |
| 863 | |
| 864 | if (!cont) |
| 865 | break; |
| 866 | |
| 867 | run_tests(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 868 | |
| 869 | kill(getppid(), SIGUSR1); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 872 | do_test_cleanup(); |
| 873 | exit(0); |
| 874 | } |
| 875 | |
| 876 | static pid_t test_pid; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 877 | |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 878 | |
| 879 | static volatile sig_atomic_t sigkill_retries; |
| 880 | |
| 881 | #define WRITE_MSG(msg) do { \ |
| 882 | if (write(2, msg, sizeof(msg) - 1)) { \ |
| 883 | /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \ |
| 884 | } \ |
| 885 | } while (0) |
| 886 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 887 | static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 888 | { |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 889 | WRITE_MSG("Test timeouted, sending SIGKILL!\n"); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 890 | kill(-test_pid, SIGKILL); |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 891 | alarm(5); |
| 892 | |
| 893 | if (++sigkill_retries > 10) { |
| 894 | WRITE_MSG("Cannot kill test processes!\n"); |
| 895 | WRITE_MSG("Congratulation, likely test hit a kernel bug.\n"); |
| 896 | WRITE_MSG("Exitting uncleanly...\n"); |
| 897 | _exit(TFAIL); |
| 898 | } |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 902 | { |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 903 | alarm(results->timeout); |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 904 | sigkill_retries = 0; |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 907 | static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 908 | { |
| 909 | if (test_pid > 0) { |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 910 | WRITE_MSG("Sending SIGKILL to test process...\n"); |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 911 | kill(-test_pid, SIGKILL); |
| 912 | } |
| 913 | } |
| 914 | |
Li Wang | 94823cf | 2017-07-18 16:23:00 +0800 | [diff] [blame] | 915 | void tst_set_timeout(int timeout) |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 916 | { |
| 917 | char *mul = getenv("LTP_TIMEOUT_MUL"); |
| 918 | |
Li Wang | 94823cf | 2017-07-18 16:23:00 +0800 | [diff] [blame] | 919 | if (timeout == -1) { |
| 920 | tst_res(TINFO, "Timeout per run is disabled"); |
| 921 | return; |
| 922 | } |
| 923 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 924 | results->timeout = timeout; |
| 925 | |
| 926 | if (mul) { |
| 927 | float m = atof(mul); |
| 928 | |
| 929 | if (m < 1) |
| 930 | tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul); |
| 931 | |
| 932 | results->timeout = results->timeout * m + 0.5; |
| 933 | } |
| 934 | |
| 935 | tst_res(TINFO, "Timeout per run is %uh %02um %02us", |
| 936 | results->timeout/3600, (results->timeout%3600)/60, |
| 937 | results->timeout % 60); |
| 938 | |
| 939 | if (getpid() == lib_pid) |
| 940 | alarm(results->timeout); |
| 941 | else |
| 942 | kill(getppid(), SIGUSR1); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | void tst_run_tcases(int argc, char *argv[], struct tst_test *self) |
| 946 | { |
| 947 | int status; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 948 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 949 | lib_pid = getpid(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 950 | tst_test = self; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 951 | |
| 952 | do_setup(argc, argv); |
| 953 | |
Cyril Hrubis | a5bf525 | 2017-03-14 15:25:29 +0800 | [diff] [blame] | 954 | TCID = tst_test->tid; |
| 955 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 956 | SAFE_SIGNAL(SIGALRM, alarm_handler); |
| 957 | SAFE_SIGNAL(SIGUSR1, heartbeat_handler); |
| 958 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 959 | if (tst_test->timeout) |
| 960 | tst_set_timeout(tst_test->timeout); |
| 961 | else |
| 962 | tst_set_timeout(300); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 963 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 964 | SAFE_SIGNAL(SIGINT, sigint_handler); |
| 965 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 966 | test_pid = fork(); |
| 967 | if (test_pid < 0) |
| 968 | tst_brk(TBROK | TERRNO, "fork()"); |
| 969 | |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 970 | if (!test_pid) { |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 971 | SAFE_SIGNAL(SIGALRM, SIG_DFL); |
| 972 | SAFE_SIGNAL(SIGUSR1, SIG_DFL); |
| 973 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 974 | SAFE_SETPGID(0, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 975 | testrun(); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 976 | } |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 977 | |
| 978 | SAFE_WAITPID(test_pid, &status, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 979 | alarm(0); |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 980 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 981 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 982 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 983 | do_exit(WEXITSTATUS(status)); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 984 | |
| 985 | if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) { |
| 986 | tst_res(TINFO, "If you are running on slow machine, " |
| 987 | "try exporting LTP_TIMEOUT_MUL > 1"); |
| 988 | tst_brk(TBROK, "Test killed! (timeout?)"); |
| 989 | } |
| 990 | |
| 991 | if (WIFSIGNALED(status)) |
| 992 | tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status))); |
| 993 | |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 994 | do_exit(0); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 995 | } |