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