Petr Vorel | acc16bc | 2019-06-13 19:25:30 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz> |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdarg.h> |
| 8 | #include <unistd.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <errno.h> |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 12 | #include <sys/mount.h> |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 13 | #include <sys/types.h> |
| 14 | #include <sys/wait.h> |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 15 | |
| 16 | #define TST_NO_DEFAULT_MAIN |
| 17 | #include "tst_test.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 18 | #include "tst_device.h" |
| 19 | #include "lapi/futex.h" |
Steve Muckle | c20831d | 2017-09-20 13:23:06 -0700 | [diff] [blame] | 20 | #include "lapi/syscalls.h" |
Petr Vorel | 3a0ef86 | 2017-03-01 15:31:08 +0100 | [diff] [blame] | 21 | #include "tst_ansi_color.h" |
Jan Stancek | b95b199 | 2017-10-10 15:47:58 +0200 | [diff] [blame] | 22 | #include "tst_safe_stdio.h" |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 23 | #include "tst_timer_test.h" |
Cyril Hrubis | 2f7c8e9 | 2018-01-22 15:19:31 +0100 | [diff] [blame] | 24 | #include "tst_clocks.h" |
| 25 | #include "tst_timer.h" |
Rafael David Tinoco | 14e4356 | 2019-01-29 15:36:55 -0200 | [diff] [blame] | 26 | #include "tst_wallclock.h" |
Jan Stancek | 9dcbc6d | 2018-11-05 09:00:02 +0100 | [diff] [blame] | 27 | #include "tst_sys_conf.h" |
Cyril Hrubis | f023a61 | 2018-11-27 14:21:17 +0100 | [diff] [blame] | 28 | #include "tst_kconfig.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 29 | |
| 30 | #include "old_resource.h" |
| 31 | #include "old_device.h" |
| 32 | #include "old_tmpdir.h" |
| 33 | |
| 34 | struct tst_test *tst_test; |
| 35 | |
Li Wang | b5d620a | 2017-11-01 13:15:23 +0800 | [diff] [blame] | 36 | static const char *tid; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 37 | static int iterations = 1; |
| 38 | static float duration = -1; |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 39 | static pid_t main_pid, lib_pid; |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 40 | static int mntpoint_mounted; |
Murphy Zhou | a88bbb4 | 2019-05-30 10:53:00 +0800 | [diff] [blame] | 41 | static int ovl_mounted; |
Jan Stancek | 1893e01 | 2018-08-28 16:17:52 +0200 | [diff] [blame] | 42 | static struct timespec tst_start_time; /* valid only for test 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; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 63 | |
| 64 | static char shm_path[1024]; |
| 65 | |
Christian Lanig | 18b780d | 2018-07-23 15:24:50 +0200 | [diff] [blame] | 66 | int TST_ERR; |
| 67 | long TST_RET; |
| 68 | |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 69 | static void do_cleanup(void); |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 70 | static void do_exit(int ret) __attribute__ ((noreturn)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 71 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 72 | static void setup_ipc(void) |
| 73 | { |
| 74 | size_t size = getpagesize(); |
| 75 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 76 | if (access("/dev/shm", F_OK) == 0) { |
| 77 | snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", |
Li Wang | b5d620a | 2017-11-01 13:15:23 +0800 | [diff] [blame] | 78 | tid, getpid()); |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 79 | } else { |
| 80 | char *tmpdir; |
| 81 | |
| 82 | if (!tst_tmpdir_created()) |
| 83 | tst_tmpdir(); |
| 84 | |
| 85 | tmpdir = tst_get_tmpdir(); |
| 86 | snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d", |
Li Wang | b5d620a | 2017-11-01 13:15:23 +0800 | [diff] [blame] | 87 | tmpdir, tid, getpid()); |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 88 | free(tmpdir); |
| 89 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 90 | |
| 91 | ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); |
| 92 | if (ipc_fd < 0) |
| 93 | tst_brk(TBROK | TERRNO, "open(%s)", shm_path); |
Jan Stancek | 2113eef | 2017-10-04 12:25:42 +0200 | [diff] [blame] | 94 | SAFE_CHMOD(shm_path, 0666); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 95 | |
| 96 | SAFE_FTRUNCATE(ipc_fd, size); |
| 97 | |
| 98 | results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); |
| 99 | |
| 100 | /* Checkpoints needs to be accessible from processes started by exec() */ |
Cyril Hrubis | 9e5a076 | 2018-07-31 15:03:21 +0200 | [diff] [blame] | 101 | if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 102 | sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); |
Jan Stancek | 03fc537 | 2017-10-10 13:36:59 +0200 | [diff] [blame] | 103 | putenv(ipc_path); |
| 104 | } else { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 105 | SAFE_UNLINK(shm_path); |
Jan Stancek | 03fc537 | 2017-10-10 13:36:59 +0200 | [diff] [blame] | 106 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 107 | |
| 108 | SAFE_CLOSE(ipc_fd); |
| 109 | |
| 110 | if (tst_test->needs_checkpoints) { |
| 111 | tst_futexes = (char*)results + sizeof(struct results); |
| 112 | tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static void cleanup_ipc(void) |
| 117 | { |
| 118 | size_t size = getpagesize(); |
| 119 | |
| 120 | if (ipc_fd > 0 && close(ipc_fd)) |
| 121 | tst_res(TWARN | TERRNO, "close(ipc_fd) failed"); |
| 122 | |
Cyril Hrubis | 9726b90 | 2017-07-27 11:28:50 +0200 | [diff] [blame] | 123 | if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path)) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 124 | tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path); |
| 125 | |
Cyril Hrubis | 9726b90 | 2017-07-27 11:28:50 +0200 | [diff] [blame] | 126 | if (results) { |
| 127 | msync((void*)results, size, MS_SYNC); |
| 128 | munmap((void*)results, size); |
Yann Sionneau | 511a0e1 | 2019-06-14 10:26:45 +0200 | [diff] [blame] | 129 | results = NULL; |
Cyril Hrubis | 9726b90 | 2017-07-27 11:28:50 +0200 | [diff] [blame] | 130 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void tst_reinit(void) |
| 134 | { |
Jan Stancek | 03fc537 | 2017-10-10 13:36:59 +0200 | [diff] [blame] | 135 | const char *path = getenv(IPC_ENV_VAR); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 136 | size_t size = getpagesize(); |
| 137 | int fd; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 138 | |
| 139 | if (!path) |
Jan Stancek | 03fc537 | 2017-10-10 13:36:59 +0200 | [diff] [blame] | 140 | tst_brk(TBROK, IPC_ENV_VAR" is not defined"); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 141 | |
| 142 | if (access(path, F_OK)) |
| 143 | tst_brk(TBROK, "File %s does not exist!", path); |
| 144 | |
| 145 | fd = SAFE_OPEN(path, O_RDWR); |
| 146 | |
Cyril Hrubis | 9e5a076 | 2018-07-31 15:03:21 +0200 | [diff] [blame] | 147 | results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 148 | tst_futexes = (char*)results + sizeof(struct results); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 149 | tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); |
| 150 | |
| 151 | SAFE_CLOSE(fd); |
| 152 | } |
| 153 | |
Cyril Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame] | 154 | static void update_results(int ttype) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 155 | { |
Cyril Hrubis | d97debf | 2017-02-13 12:37:39 +0100 | [diff] [blame] | 156 | if (!results) |
| 157 | return; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 158 | |
| 159 | switch (ttype) { |
| 160 | case TCONF: |
| 161 | tst_atomic_inc(&results->skipped); |
| 162 | break; |
| 163 | case TPASS: |
| 164 | tst_atomic_inc(&results->passed); |
| 165 | break; |
| 166 | case TWARN: |
| 167 | tst_atomic_inc(&results->warnings); |
| 168 | break; |
| 169 | case TFAIL: |
| 170 | tst_atomic_inc(&results->failed); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void print_result(const char *file, const int lineno, int ttype, |
| 176 | const char *fmt, va_list va) |
| 177 | { |
| 178 | char buf[1024]; |
| 179 | char *str = buf; |
Veronika Kabatova | cecbd0c | 2017-11-07 17:10:42 +0100 | [diff] [blame] | 180 | int ret, size = sizeof(buf), ssize; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 181 | const char *str_errno = NULL; |
| 182 | const char *res; |
| 183 | |
| 184 | switch (TTYPE_RESULT(ttype)) { |
| 185 | case TPASS: |
| 186 | res = "PASS"; |
| 187 | break; |
| 188 | case TFAIL: |
| 189 | res = "FAIL"; |
| 190 | break; |
| 191 | case TBROK: |
| 192 | res = "BROK"; |
| 193 | break; |
| 194 | case TCONF: |
| 195 | res = "CONF"; |
| 196 | break; |
| 197 | case TWARN: |
| 198 | res = "WARN"; |
| 199 | break; |
| 200 | case TINFO: |
| 201 | res = "INFO"; |
| 202 | break; |
| 203 | default: |
| 204 | tst_brk(TBROK, "Invalid ttype value %i", ttype); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 205 | abort(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | if (ttype & TERRNO) |
| 209 | str_errno = tst_strerrno(errno); |
| 210 | |
| 211 | if (ttype & TTERRNO) |
Christian Lanig | 18b780d | 2018-07-23 15:24:50 +0200 | [diff] [blame] | 212 | str_errno = tst_strerrno(TST_ERR); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 213 | |
Richard Palethorpe | fb32e89 | 2018-03-13 16:24:47 +0100 | [diff] [blame] | 214 | if (ttype & TRERRNO) { |
Christian Lanig | 18b780d | 2018-07-23 15:24:50 +0200 | [diff] [blame] | 215 | ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET; |
Richard Palethorpe | fb32e89 | 2018-03-13 16:24:47 +0100 | [diff] [blame] | 216 | str_errno = tst_strerrno(ret); |
| 217 | } |
| 218 | |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 219 | ret = snprintf(str, size, "%s:%i: ", file, lineno); |
| 220 | str += ret; |
| 221 | size -= ret; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 222 | |
Cyril Hrubis | 047c727 | 2017-02-13 16:23:36 +0100 | [diff] [blame] | 223 | if (tst_color_enabled(STDERR_FILENO)) |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 224 | ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype), |
| 225 | res, ANSI_COLOR_RESET); |
| 226 | else |
| 227 | ret = snprintf(str, size, "%s: ", res); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 228 | str += ret; |
| 229 | size -= ret; |
| 230 | |
Veronika Kabatova | cecbd0c | 2017-11-07 17:10:42 +0100 | [diff] [blame] | 231 | ssize = size - 2; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 232 | ret = vsnprintf(str, size, fmt, va); |
Veronika Kabatova | cecbd0c | 2017-11-07 17:10:42 +0100 | [diff] [blame] | 233 | str += MIN(ret, ssize); |
| 234 | size -= MIN(ret, ssize); |
| 235 | if (ret >= ssize) { |
| 236 | tst_res_(file, lineno, TWARN, |
| 237 | "Next message is too long and truncated:"); |
| 238 | } else if (str_errno) { |
| 239 | ssize = size - 2; |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 240 | ret = snprintf(str, size, ": %s", str_errno); |
Veronika Kabatova | cecbd0c | 2017-11-07 17:10:42 +0100 | [diff] [blame] | 241 | str += MIN(ret, ssize); |
| 242 | size -= MIN(ret, ssize); |
| 243 | if (ret >= ssize) |
| 244 | tst_res_(file, lineno, TWARN, |
| 245 | "Next message is too long and truncated:"); |
Petr Vorel | a7f6133 | 2017-01-24 20:47:30 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | snprintf(str, size, "\n"); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 249 | |
| 250 | fputs(buf, stderr); |
| 251 | } |
| 252 | |
| 253 | void tst_vres_(const char *file, const int lineno, int ttype, |
| 254 | const char *fmt, va_list va) |
| 255 | { |
| 256 | print_result(file, lineno, ttype, fmt, va); |
| 257 | |
Cyril Hrubis | 160ffcc | 2017-02-14 10:21:08 +0100 | [diff] [blame] | 258 | update_results(TTYPE_RESULT(ttype)); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 262 | const char *fmt, va_list va); |
| 263 | |
| 264 | static void (*tst_brk_handler)(const char *file, const int lineno, int ttype, |
| 265 | const char *fmt, va_list va) = tst_vbrk_; |
| 266 | |
| 267 | static void tst_cvres(const char *file, const int lineno, int ttype, |
| 268 | const char *fmt, va_list va) |
| 269 | { |
| 270 | if (TTYPE_RESULT(ttype) == TBROK) { |
| 271 | ttype &= ~TTYPE_MASK; |
| 272 | ttype |= TWARN; |
| 273 | } |
| 274 | |
| 275 | print_result(file, lineno, ttype, fmt, va); |
| 276 | update_results(TTYPE_RESULT(ttype)); |
| 277 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 278 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 279 | static void do_test_cleanup(void) |
| 280 | { |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 281 | tst_brk_handler = tst_cvres; |
| 282 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 283 | if (tst_test->cleanup) |
| 284 | tst_test->cleanup(); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 285 | |
Cyril Hrubis | ee761d8 | 2019-07-31 15:02:08 +0200 | [diff] [blame] | 286 | tst_free_all(); |
| 287 | |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 288 | tst_brk_handler = tst_vbrk_; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 289 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 290 | |
| 291 | void tst_vbrk_(const char *file, const int lineno, int ttype, |
| 292 | const char *fmt, va_list va) |
| 293 | { |
| 294 | print_result(file, lineno, ttype, fmt, va); |
| 295 | |
Steve Muckle | c20831d | 2017-09-20 13:23:06 -0700 | [diff] [blame] | 296 | /* |
| 297 | * The getpid implementation in some C library versions may cause cloned |
| 298 | * test threads to show the same pid as their parent when CLONE_VM is |
| 299 | * specified but CLONE_THREAD is not. Use direct syscall to avoid |
| 300 | * cleanup running in the child. |
| 301 | */ |
| 302 | if (syscall(SYS_getpid) == main_pid) |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 303 | do_test_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 304 | |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 305 | if (getpid() == lib_pid) |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 306 | do_exit(TTYPE_RESULT(ttype)); |
Jan Stancek | e0bfa7d | 2016-06-08 15:27:55 +0200 | [diff] [blame] | 307 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 308 | exit(TTYPE_RESULT(ttype)); |
| 309 | } |
| 310 | |
| 311 | void tst_res_(const char *file, const int lineno, int ttype, |
| 312 | const char *fmt, ...) |
| 313 | { |
| 314 | va_list va; |
| 315 | |
| 316 | va_start(va, fmt); |
| 317 | tst_vres_(file, lineno, ttype, fmt, va); |
| 318 | va_end(va); |
| 319 | } |
| 320 | |
| 321 | void tst_brk_(const char *file, const int lineno, int ttype, |
| 322 | const char *fmt, ...) |
| 323 | { |
| 324 | va_list va; |
| 325 | |
| 326 | va_start(va, fmt); |
Cyril Hrubis | 6440c5d | 2017-02-09 15:41:24 +0100 | [diff] [blame] | 327 | tst_brk_handler(file, lineno, ttype, fmt, va); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 328 | va_end(va); |
| 329 | } |
| 330 | |
| 331 | static void check_child_status(pid_t pid, int status) |
| 332 | { |
| 333 | int ret; |
| 334 | |
| 335 | if (WIFSIGNALED(status)) { |
| 336 | tst_brk(TBROK, "Child (%i) killed by signal %s", |
| 337 | pid, tst_strsig(WTERMSIG(status))); |
| 338 | } |
| 339 | |
| 340 | if (!(WIFEXITED(status))) |
Petr Vorel | f885348 | 2017-10-31 09:40:38 +0100 | [diff] [blame] | 341 | tst_brk(TBROK, "Child (%i) exited abnormaly", pid); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 342 | |
| 343 | ret = WEXITSTATUS(status); |
| 344 | switch (ret) { |
| 345 | case TPASS: |
| 346 | break; |
| 347 | case TBROK: |
| 348 | case TCONF: |
| 349 | tst_brk(ret, "Reported by child (%i)", pid); |
Cyril Hrubis | 45cd37f | 2018-08-31 14:25:28 +0200 | [diff] [blame] | 350 | break; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 351 | default: |
| 352 | tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret); |
| 353 | } |
| 354 | } |
| 355 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 356 | void tst_reap_children(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 357 | { |
| 358 | int status; |
| 359 | pid_t pid; |
| 360 | |
| 361 | for (;;) { |
| 362 | pid = wait(&status); |
| 363 | |
| 364 | if (pid > 0) { |
| 365 | check_child_status(pid, status); |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | if (errno == ECHILD) |
| 370 | break; |
| 371 | |
| 372 | if (errno == EINTR) |
| 373 | continue; |
| 374 | |
| 375 | tst_brk(TBROK | TERRNO, "wait() failed"); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
| 380 | pid_t safe_fork(const char *filename, unsigned int lineno) |
| 381 | { |
| 382 | pid_t pid; |
| 383 | |
| 384 | if (!tst_test->forks_child) |
| 385 | tst_brk(TBROK, "test.forks_child must be set!"); |
| 386 | |
Michael Moese | 9da4237 | 2018-03-09 15:16:09 +0100 | [diff] [blame] | 387 | tst_flush(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 388 | |
| 389 | pid = fork(); |
| 390 | if (pid < 0) |
| 391 | tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed"); |
| 392 | |
Cyril Hrubis | 13296b6 | 2019-07-31 17:39:37 +0200 | [diff] [blame^] | 393 | if (!pid) |
| 394 | atexit(tst_free_all); |
| 395 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 396 | return pid; |
| 397 | } |
| 398 | |
| 399 | static struct option { |
| 400 | char *optstr; |
| 401 | char *help; |
| 402 | } options[] = { |
Cyril Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 403 | {"h", "-h Prints this help"}, |
| 404 | {"i:", "-i n Execute test n times"}, |
| 405 | {"I:", "-I x Execute test for n seconds"}, |
| 406 | {"C:", "-C ARG Run child process with ARG arguments (used internally)"}, |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | static void print_help(void) |
| 410 | { |
| 411 | unsigned int i; |
| 412 | |
| 413 | for (i = 0; i < ARRAY_SIZE(options); i++) |
| 414 | fprintf(stderr, "%s\n", options[i].help); |
| 415 | |
| 416 | if (!tst_test->options) |
| 417 | return; |
| 418 | |
| 419 | for (i = 0; tst_test->options[i].optstr; i++) |
Cyril Hrubis | b819c22 | 2016-08-03 14:36:07 +0200 | [diff] [blame] | 420 | fprintf(stderr, "%s\n", tst_test->options[i].help); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void check_option_collision(void) |
| 424 | { |
| 425 | unsigned int i, j; |
| 426 | struct tst_option *toptions = tst_test->options; |
| 427 | |
| 428 | if (!toptions) |
| 429 | return; |
| 430 | |
| 431 | for (i = 0; toptions[i].optstr; i++) { |
| 432 | for (j = 0; j < ARRAY_SIZE(options); j++) { |
| 433 | if (toptions[i].optstr[0] == options[j].optstr[0]) { |
| 434 | tst_brk(TBROK, "Option collision '%s'", |
| 435 | options[j].help); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static unsigned int count_options(void) |
| 442 | { |
| 443 | unsigned int i; |
| 444 | |
| 445 | if (!tst_test->options) |
| 446 | return 0; |
| 447 | |
| 448 | for (i = 0; tst_test->options[i].optstr; i++); |
| 449 | |
| 450 | return i; |
| 451 | } |
| 452 | |
| 453 | static void parse_topt(unsigned int topts_len, int opt, char *optarg) |
| 454 | { |
| 455 | unsigned int i; |
| 456 | struct tst_option *toptions = tst_test->options; |
| 457 | |
| 458 | for (i = 0; i < topts_len; i++) { |
| 459 | if (toptions[i].optstr[0] == opt) |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | if (i >= topts_len) |
| 464 | tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt); |
| 465 | |
Jan Stancek | c07d36c | 2016-08-01 11:44:55 +0200 | [diff] [blame] | 466 | *(toptions[i].arg) = optarg ? optarg : ""; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* see self_exec.c */ |
| 470 | #ifdef UCLINUX |
| 471 | extern char *child_args; |
| 472 | #endif |
| 473 | |
| 474 | static void parse_opts(int argc, char *argv[]) |
| 475 | { |
| 476 | unsigned int i, topts_len = count_options(); |
| 477 | char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len]; |
| 478 | int opt; |
| 479 | |
| 480 | check_option_collision(); |
| 481 | |
| 482 | optstr[0] = 0; |
| 483 | |
| 484 | for (i = 0; i < ARRAY_SIZE(options); i++) |
| 485 | strcat(optstr, options[i].optstr); |
| 486 | |
| 487 | for (i = 0; i < topts_len; i++) |
| 488 | strcat(optstr, tst_test->options[i].optstr); |
| 489 | |
| 490 | while ((opt = getopt(argc, argv, optstr)) > 0) { |
| 491 | switch (opt) { |
| 492 | case '?': |
| 493 | print_help(); |
| 494 | tst_brk(TBROK, "Invalid option"); |
Cyril Hrubis | 45cd37f | 2018-08-31 14:25:28 +0200 | [diff] [blame] | 495 | break; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 496 | case 'h': |
| 497 | print_help(); |
| 498 | exit(0); |
| 499 | case 'i': |
| 500 | iterations = atoi(optarg); |
| 501 | break; |
| 502 | case 'I': |
| 503 | duration = atof(optarg); |
| 504 | break; |
| 505 | case 'C': |
| 506 | #ifdef UCLINUX |
| 507 | child_args = optarg; |
| 508 | #endif |
| 509 | break; |
| 510 | default: |
| 511 | parse_topt(topts_len, opt, optarg); |
| 512 | } |
| 513 | } |
Cyril Hrubis | 9dc07e0 | 2017-10-02 11:11:04 +0200 | [diff] [blame] | 514 | |
| 515 | if (optind < argc) |
| 516 | tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 519 | int tst_parse_int(const char *str, int *val, int min, int max) |
| 520 | { |
| 521 | long rval; |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 522 | |
| 523 | if (!str) |
| 524 | return 0; |
| 525 | |
| 526 | int ret = tst_parse_long(str, &rval, min, max); |
| 527 | |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | *val = (int)rval; |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | int tst_parse_long(const char *str, long *val, long min, long max) |
| 536 | { |
| 537 | long rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 538 | char *end; |
| 539 | |
| 540 | if (!str) |
| 541 | return 0; |
| 542 | |
| 543 | errno = 0; |
| 544 | rval = strtol(str, &end, 10); |
| 545 | |
| 546 | if (str == end || *end != '\0') |
| 547 | return EINVAL; |
| 548 | |
| 549 | if (errno) |
| 550 | return errno; |
| 551 | |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 552 | if (rval > max || rval < min) |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 553 | return ERANGE; |
| 554 | |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 555 | *val = rval; |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | int tst_parse_float(const char *str, float *val, float min, float max) |
| 560 | { |
| 561 | double rval; |
| 562 | char *end; |
| 563 | |
| 564 | if (!str) |
| 565 | return 0; |
| 566 | |
| 567 | errno = 0; |
| 568 | rval = strtod(str, &end); |
| 569 | |
| 570 | if (str == end || *end != '\0') |
| 571 | return EINVAL; |
| 572 | |
| 573 | if (errno) |
| 574 | return errno; |
| 575 | |
| 576 | if (rval > (double)max || rval < (double)min) |
| 577 | return ERANGE; |
| 578 | |
| 579 | *val = (float)rval; |
| 580 | return 0; |
| 581 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 582 | |
Cyril Hrubis | fa49517 | 2016-06-08 16:06:35 +0200 | [diff] [blame] | 583 | static void do_exit(int ret) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 584 | { |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 585 | if (results) { |
| 586 | printf("\nSummary:\n"); |
| 587 | printf("passed %d\n", results->passed); |
| 588 | printf("failed %d\n", results->failed); |
| 589 | printf("skipped %d\n", results->skipped); |
| 590 | printf("warnings %d\n", results->warnings); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 591 | |
Cyril Hrubis | fecdd88 | 2018-01-17 14:51:30 +0100 | [diff] [blame] | 592 | if (results->passed && ret == TCONF) |
| 593 | ret = 0; |
| 594 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 595 | if (results->failed) |
| 596 | ret |= TFAIL; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 597 | |
Cyril Hrubis | 5390d6e | 2017-09-07 15:47:22 +0200 | [diff] [blame] | 598 | if (results->skipped && !results->passed) |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 599 | ret |= TCONF; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 600 | |
Xiao Yang | 11dfc32 | 2016-06-16 15:52:04 +0800 | [diff] [blame] | 601 | if (results->warnings) |
| 602 | ret |= TWARN; |
| 603 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 604 | |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 605 | do_cleanup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 606 | |
| 607 | exit(ret); |
| 608 | } |
| 609 | |
| 610 | void check_kver(void) |
| 611 | { |
| 612 | int v1, v2, v3; |
| 613 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 614 | if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) { |
| 615 | tst_res(TWARN, |
| 616 | "Invalid kernel version %s, expected %%d.%%d.%%d", |
| 617 | tst_test->min_kver); |
| 618 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 619 | |
| 620 | if (tst_kvercmp(v1, v2, v3) < 0) { |
| 621 | tst_brk(TCONF, "The test requires kernel %s or newer", |
| 622 | tst_test->min_kver); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static int results_equal(struct results *a, struct results *b) |
| 627 | { |
| 628 | if (a->passed != b->passed) |
| 629 | return 0; |
| 630 | |
| 631 | if (a->failed != b->failed) |
| 632 | return 0; |
| 633 | |
| 634 | if (a->skipped != b->skipped) |
| 635 | return 0; |
| 636 | |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | static int needs_tmpdir(void) |
| 641 | { |
| 642 | return tst_test->needs_tmpdir || |
| 643 | tst_test->needs_device || |
Cyril Hrubis | 16352e4 | 2018-04-05 16:01:47 +0200 | [diff] [blame] | 644 | tst_test->mntpoint || |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 645 | tst_test->resource_files || |
| 646 | tst_test->needs_checkpoints; |
| 647 | } |
| 648 | |
| 649 | static void copy_resources(void) |
| 650 | { |
| 651 | unsigned int i; |
| 652 | |
| 653 | for (i = 0; tst_test->resource_files[i]; i++) |
| 654 | TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL); |
| 655 | } |
| 656 | |
Cyril Hrubis | a5bf525 | 2017-03-14 15:25:29 +0800 | [diff] [blame] | 657 | static const char *get_tid(char *argv[]) |
| 658 | { |
| 659 | char *p; |
| 660 | |
| 661 | if (!argv[0] || !argv[0][0]) { |
| 662 | tst_res(TINFO, "argv[0] is empty!"); |
| 663 | return "ltp_empty_argv"; |
| 664 | } |
| 665 | |
| 666 | p = strrchr(argv[0], '/'); |
| 667 | if (p) |
| 668 | return p+1; |
| 669 | |
| 670 | return argv[0]; |
| 671 | } |
| 672 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 673 | static struct tst_device tdev; |
| 674 | struct tst_device *tst_device; |
| 675 | |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 676 | static void assert_test_fn(void) |
| 677 | { |
| 678 | int cnt = 0; |
| 679 | |
| 680 | if (tst_test->test) |
| 681 | cnt++; |
| 682 | |
| 683 | if (tst_test->test_all) |
| 684 | cnt++; |
| 685 | |
| 686 | if (tst_test->sample) |
| 687 | cnt++; |
| 688 | |
| 689 | if (!cnt) |
| 690 | tst_brk(TBROK, "No test function speficied"); |
| 691 | |
| 692 | if (cnt != 1) |
| 693 | tst_brk(TBROK, "You can define only one test function"); |
| 694 | |
| 695 | if (tst_test->test && !tst_test->tcnt) |
Eric Biggers | 3614a16 | 2019-03-15 09:42:15 -0700 | [diff] [blame] | 696 | tst_brk(TBROK, "Number of tests (tcnt) must be > 0"); |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 697 | |
| 698 | if (!tst_test->test && tst_test->tcnt) |
| 699 | tst_brk(TBROK, "You can define tcnt only for test()"); |
| 700 | } |
| 701 | |
Cyril Hrubis | e6da867 | 2018-04-05 16:01:48 +0200 | [diff] [blame] | 702 | static int prepare_and_mount_ro_fs(const char *dev, |
| 703 | const char *mntpoint, |
| 704 | const char *fs_type) |
| 705 | { |
| 706 | char buf[PATH_MAX]; |
| 707 | |
| 708 | if (mount(dev, mntpoint, fs_type, 0, NULL)) { |
| 709 | tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)", |
| 710 | dev, mntpoint, fs_type); |
| 711 | return 1; |
| 712 | } |
| 713 | |
| 714 | mntpoint_mounted = 1; |
| 715 | |
| 716 | snprintf(buf, sizeof(buf), "%s/dir/", mntpoint); |
| 717 | SAFE_MKDIR(buf, 0777); |
| 718 | |
| 719 | snprintf(buf, sizeof(buf), "%s/file", mntpoint); |
| 720 | SAFE_FILE_PRINTF(buf, "file content"); |
| 721 | SAFE_CHMOD(buf, 0777); |
| 722 | |
| 723 | SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL); |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
Xiao Yang | 7313a87 | 2018-09-01 12:07:41 +0800 | [diff] [blame] | 728 | static void prepare_and_mount_dev_fs(const char *mntpoint) |
| 729 | { |
| 730 | const char *flags[] = {"nodev", NULL}; |
| 731 | int mounted_nodev; |
| 732 | |
| 733 | mounted_nodev = tst_path_has_mnt_flags(NULL, flags); |
| 734 | if (mounted_nodev) { |
| 735 | tst_res(TINFO, "tmpdir isn't suitable for creating devices, " |
| 736 | "mounting tmpfs without nodev on %s", mntpoint); |
| 737 | SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL); |
| 738 | mntpoint_mounted = 1; |
| 739 | } |
| 740 | } |
| 741 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 742 | static void prepare_device(void) |
| 743 | { |
| 744 | if (tst_test->format_device) { |
| 745 | SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts, |
Cyril Hrubis | a6b0a92 | 2018-09-11 17:15:08 +0200 | [diff] [blame] | 746 | tst_test->dev_extra_opts); |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Cyril Hrubis | e6da867 | 2018-04-05 16:01:48 +0200 | [diff] [blame] | 749 | if (tst_test->needs_rofs) { |
| 750 | prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint, |
| 751 | tdev.fs_type); |
| 752 | return; |
| 753 | } |
| 754 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 755 | if (tst_test->mount_device) { |
| 756 | SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type, |
| 757 | tst_test->mnt_flags, tst_test->mnt_data); |
| 758 | mntpoint_mounted = 1; |
| 759 | } |
| 760 | } |
| 761 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 762 | static void do_setup(int argc, char *argv[]) |
| 763 | { |
| 764 | if (!tst_test) |
| 765 | tst_brk(TBROK, "No tests to run"); |
| 766 | |
Cyril Hrubis | f706a2f | 2017-08-01 17:31:30 +0200 | [diff] [blame] | 767 | if (tst_test->tconf_msg) |
| 768 | tst_brk(TCONF, "%s", tst_test->tconf_msg); |
| 769 | |
Cyril Hrubis | f023a61 | 2018-11-27 14:21:17 +0100 | [diff] [blame] | 770 | if (tst_test->needs_kconfigs) |
| 771 | tst_kconfig_check(tst_test->needs_kconfigs); |
| 772 | |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 773 | assert_test_fn(); |
| 774 | |
Li Wang | b5d620a | 2017-11-01 13:15:23 +0800 | [diff] [blame] | 775 | tid = get_tid(argv); |
| 776 | |
Cyril Hrubis | c459654 | 2017-06-20 15:42:18 +0200 | [diff] [blame] | 777 | if (tst_test->sample) |
| 778 | tst_test = tst_timer_test_setup(tst_test); |
| 779 | |
Cyril Hrubis | 88c220d | 2017-07-27 11:16:39 +0200 | [diff] [blame] | 780 | parse_opts(argc, argv); |
| 781 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 782 | if (tst_test->needs_root && geteuid() != 0) |
| 783 | tst_brk(TCONF, "Test needs to be run as root"); |
| 784 | |
| 785 | if (tst_test->min_kver) |
| 786 | check_kver(); |
| 787 | |
Alexey Kodanev | 1f70b0a | 2018-09-20 14:30:16 +0300 | [diff] [blame] | 788 | if (tst_test->needs_drivers) { |
| 789 | const char *name; |
| 790 | int i; |
| 791 | |
| 792 | for (i = 0; (name = tst_test->needs_drivers[i]); ++i) |
| 793 | if (tst_check_driver(name)) |
| 794 | tst_brk(TCONF, "%s driver not available", name); |
| 795 | } |
| 796 | |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 797 | if (tst_test->format_device) |
| 798 | tst_test->needs_device = 1; |
| 799 | |
| 800 | if (tst_test->mount_device) { |
| 801 | tst_test->needs_device = 1; |
| 802 | tst_test->format_device = 1; |
| 803 | } |
| 804 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 805 | if (tst_test->all_filesystems) |
| 806 | tst_test->needs_device = 1; |
| 807 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 808 | setup_ipc(); |
| 809 | |
Cyril Hrubis | ee761d8 | 2019-07-31 15:02:08 +0200 | [diff] [blame] | 810 | if (tst_test->bufs) |
| 811 | tst_buffers_alloc(tst_test->bufs); |
| 812 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 813 | if (needs_tmpdir() && !tst_tmpdir_created()) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 814 | tst_tmpdir(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 815 | |
Jan Stancek | 9dcbc6d | 2018-11-05 09:00:02 +0100 | [diff] [blame] | 816 | if (tst_test->save_restore) { |
| 817 | const char * const *name = tst_test->save_restore; |
| 818 | |
| 819 | while (*name) { |
| 820 | tst_sys_conf_save(*name); |
| 821 | name++; |
| 822 | } |
| 823 | } |
| 824 | |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 825 | if (tst_test->mntpoint) |
| 826 | SAFE_MKDIR(tst_test->mntpoint, 0777); |
| 827 | |
Xiao Yang | 7313a87 | 2018-09-01 12:07:41 +0800 | [diff] [blame] | 828 | if ((tst_test->needs_devfs || tst_test->needs_rofs || |
| 829 | tst_test->mount_device || tst_test->all_filesystems) && |
| 830 | !tst_test->mntpoint) { |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 831 | tst_brk(TBROK, "tst_test->mntpoint must be set!"); |
| 832 | } |
| 833 | |
Xiao Yang | 7313a87 | 2018-09-01 12:07:41 +0800 | [diff] [blame] | 834 | if (!!tst_test->needs_rofs + !!tst_test->needs_devfs + |
| 835 | !!tst_test->needs_device > 1) { |
| 836 | tst_brk(TBROK, |
| 837 | "Two or more of needs_{rofs, devfs, device} are set"); |
| 838 | } |
| 839 | |
| 840 | if (tst_test->needs_devfs) |
| 841 | prepare_and_mount_dev_fs(tst_test->mntpoint); |
| 842 | |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 843 | if (tst_test->needs_rofs) { |
| 844 | /* If we failed to mount read-only tmpfs. Fallback to |
Cyril Hrubis | e6da867 | 2018-04-05 16:01:48 +0200 | [diff] [blame] | 845 | * using a device with read-only filesystem. |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 846 | */ |
Cyril Hrubis | e6da867 | 2018-04-05 16:01:48 +0200 | [diff] [blame] | 847 | if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) { |
| 848 | tst_res(TINFO, "Can't mount tmpfs read-only, " |
| 849 | "falling back to block device..."); |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 850 | tst_test->needs_device = 1; |
| 851 | tst_test->format_device = 1; |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | if (tst_test->needs_device && !mntpoint_mounted) { |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 856 | tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 857 | |
| 858 | if (!tdev.dev) |
| 859 | tst_brk(TCONF, "Failed to acquire device"); |
| 860 | |
| 861 | tst_device = &tdev; |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 862 | |
| 863 | if (tst_test->dev_fs_type) |
| 864 | tdev.fs_type = tst_test->dev_fs_type; |
| 865 | else |
| 866 | tdev.fs_type = tst_dev_fs_type(); |
| 867 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 868 | if (!tst_test->all_filesystems) |
| 869 | prepare_device(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Murphy Zhou | a88bbb4 | 2019-05-30 10:53:00 +0800 | [diff] [blame] | 872 | if (tst_test->needs_overlay && !tst_test->mount_device) { |
| 873 | tst_brk(TBROK, "tst_test->mount_device must be set"); |
| 874 | } |
| 875 | if (tst_test->needs_overlay && !mntpoint_mounted) { |
| 876 | tst_brk(TBROK, "tst_test->mntpoint must be mounted"); |
| 877 | } |
| 878 | if (tst_test->needs_overlay && !ovl_mounted) { |
| 879 | SAFE_MOUNT_OVERLAY(); |
| 880 | ovl_mounted = 1; |
| 881 | } |
| 882 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 883 | if (tst_test->resource_files) |
| 884 | copy_resources(); |
Rafael David Tinoco | 14e4356 | 2019-01-29 15:36:55 -0200 | [diff] [blame] | 885 | |
| 886 | if (tst_test->restore_wallclock) |
| 887 | tst_wallclock_save(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 888 | } |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 889 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 890 | static void do_test_setup(void) |
| 891 | { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 892 | main_pid = getpid(); |
| 893 | |
| 894 | if (tst_test->setup) |
| 895 | tst_test->setup(); |
| 896 | |
| 897 | if (main_pid != getpid()) |
| 898 | tst_brk(TBROK, "Runaway child in setup()!"); |
| 899 | } |
| 900 | |
| 901 | static void do_cleanup(void) |
| 902 | { |
Murphy Zhou | a88bbb4 | 2019-05-30 10:53:00 +0800 | [diff] [blame] | 903 | if (ovl_mounted) |
| 904 | SAFE_UMOUNT(OVL_MNT); |
| 905 | |
Sandeep Patil | c9a7def | 2017-09-19 12:49:58 -0700 | [diff] [blame] | 906 | if (mntpoint_mounted) |
Cyril Hrubis | 874326d | 2017-02-14 15:59:40 +0100 | [diff] [blame] | 907 | tst_umount(tst_test->mntpoint); |
| 908 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 909 | if (tst_test->needs_device && tdev.dev) |
| 910 | tst_release_device(tdev.dev); |
| 911 | |
Steven Jackson | 9f41dcf | 2016-12-21 20:09:01 +0000 | [diff] [blame] | 912 | if (tst_tmpdir_created()) { |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 913 | /* avoid munmap() on wrong pointer in tst_rmdir() */ |
| 914 | tst_futexes = NULL; |
| 915 | tst_rmdir(); |
| 916 | } |
Jan Stancek | 332540e | 2016-06-08 16:48:22 +0200 | [diff] [blame] | 917 | |
Jan Stancek | 9dcbc6d | 2018-11-05 09:00:02 +0100 | [diff] [blame] | 918 | if (tst_test->save_restore) |
| 919 | tst_sys_conf_restore(0); |
| 920 | |
Rafael David Tinoco | 14e4356 | 2019-01-29 15:36:55 -0200 | [diff] [blame] | 921 | if (tst_test->restore_wallclock) |
| 922 | tst_wallclock_restore(); |
Yann Sionneau | 511a0e1 | 2019-06-14 10:26:45 +0200 | [diff] [blame] | 923 | |
| 924 | cleanup_ipc(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static void run_tests(void) |
| 928 | { |
| 929 | unsigned int i; |
| 930 | struct results saved_results; |
| 931 | |
| 932 | if (!tst_test->test) { |
| 933 | saved_results = *results; |
| 934 | tst_test->test_all(); |
| 935 | |
| 936 | if (getpid() != main_pid) { |
| 937 | exit(0); |
| 938 | } |
| 939 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 940 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 941 | |
| 942 | if (results_equal(&saved_results, results)) |
| 943 | tst_brk(TBROK, "Test haven't reported results!"); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | for (i = 0; i < tst_test->tcnt; i++) { |
| 948 | saved_results = *results; |
| 949 | tst_test->test(i); |
| 950 | |
| 951 | if (getpid() != main_pid) { |
| 952 | exit(0); |
| 953 | } |
| 954 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 955 | tst_reap_children(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 956 | |
| 957 | if (results_equal(&saved_results, results)) |
| 958 | tst_brk(TBROK, "Test %i haven't reported results!", i); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static unsigned long long get_time_ms(void) |
| 963 | { |
Cyril Hrubis | 2f7c8e9 | 2018-01-22 15:19:31 +0100 | [diff] [blame] | 964 | struct timespec ts; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 965 | |
Cyril Hrubis | 2f7c8e9 | 2018-01-22 15:19:31 +0100 | [diff] [blame] | 966 | if (tst_clock_gettime(CLOCK_MONOTONIC, &ts)) |
| 967 | tst_brk(TBROK | TERRNO, "tst_clock_gettime()"); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 968 | |
Cyril Hrubis | 2f7c8e9 | 2018-01-22 15:19:31 +0100 | [diff] [blame] | 969 | return tst_timespec_to_ms(ts); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Jan Stancek | b95b199 | 2017-10-10 15:47:58 +0200 | [diff] [blame] | 972 | static void add_paths(void) |
| 973 | { |
| 974 | char *old_path = getenv("PATH"); |
| 975 | const char *start_dir; |
| 976 | char *new_path; |
| 977 | |
| 978 | start_dir = tst_get_startwd(); |
| 979 | |
| 980 | if (old_path) |
| 981 | SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir); |
| 982 | else |
| 983 | SAFE_ASPRINTF(&new_path, "::%s", start_dir); |
| 984 | |
| 985 | SAFE_SETENV("PATH", new_path, 1); |
| 986 | free(new_path); |
| 987 | } |
| 988 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 989 | static void heartbeat(void) |
| 990 | { |
Jan Stancek | 1893e01 | 2018-08-28 16:17:52 +0200 | [diff] [blame] | 991 | if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time)) |
| 992 | tst_res(TWARN | TERRNO, "tst_clock_gettime() failed"); |
| 993 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 994 | kill(getppid(), SIGUSR1); |
| 995 | } |
| 996 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 997 | static void testrun(void) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 998 | { |
| 999 | unsigned int i = 0; |
| 1000 | unsigned long long stop_time = 0; |
| 1001 | int cont = 1; |
| 1002 | |
Jan Stancek | 1893e01 | 2018-08-28 16:17:52 +0200 | [diff] [blame] | 1003 | heartbeat(); |
Jan Stancek | b95b199 | 2017-10-10 15:47:58 +0200 | [diff] [blame] | 1004 | add_paths(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1005 | do_test_setup(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 1006 | |
| 1007 | if (duration > 0) |
| 1008 | stop_time = get_time_ms() + (unsigned long long)(duration * 1000); |
| 1009 | |
| 1010 | for (;;) { |
| 1011 | cont = 0; |
| 1012 | |
| 1013 | if (i < (unsigned int)iterations) { |
| 1014 | i++; |
| 1015 | cont = 1; |
| 1016 | } |
| 1017 | |
| 1018 | if (stop_time && get_time_ms() < stop_time) |
| 1019 | cont = 1; |
| 1020 | |
| 1021 | if (!cont) |
| 1022 | break; |
| 1023 | |
| 1024 | run_tests(); |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1025 | heartbeat(); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1028 | do_test_cleanup(); |
| 1029 | exit(0); |
| 1030 | } |
| 1031 | |
| 1032 | static pid_t test_pid; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1033 | |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 1034 | |
| 1035 | static volatile sig_atomic_t sigkill_retries; |
| 1036 | |
| 1037 | #define WRITE_MSG(msg) do { \ |
| 1038 | if (write(2, msg, sizeof(msg) - 1)) { \ |
| 1039 | /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \ |
| 1040 | } \ |
| 1041 | } while (0) |
| 1042 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1043 | static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 1044 | { |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 1045 | WRITE_MSG("Test timeouted, sending SIGKILL!\n"); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 1046 | kill(-test_pid, SIGKILL); |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 1047 | alarm(5); |
| 1048 | |
| 1049 | if (++sigkill_retries > 10) { |
| 1050 | WRITE_MSG("Cannot kill test processes!\n"); |
| 1051 | WRITE_MSG("Congratulation, likely test hit a kernel bug.\n"); |
| 1052 | WRITE_MSG("Exitting uncleanly...\n"); |
| 1053 | _exit(TFAIL); |
| 1054 | } |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 1058 | { |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 1059 | alarm(results->timeout); |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 1060 | sigkill_retries = 0; |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 1061 | } |
| 1062 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 1063 | static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 1064 | { |
| 1065 | if (test_pid > 0) { |
Cyril Hrubis | 7916317 | 2017-05-18 10:57:07 +0200 | [diff] [blame] | 1066 | WRITE_MSG("Sending SIGKILL to test process...\n"); |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 1067 | kill(-test_pid, SIGKILL); |
| 1068 | } |
| 1069 | } |
| 1070 | |
Jan Stancek | 1893e01 | 2018-08-28 16:17:52 +0200 | [diff] [blame] | 1071 | unsigned int tst_timeout_remaining(void) |
| 1072 | { |
| 1073 | static struct timespec now; |
| 1074 | unsigned int elapsed; |
| 1075 | |
| 1076 | if (tst_clock_gettime(CLOCK_MONOTONIC, &now)) |
| 1077 | tst_res(TWARN | TERRNO, "tst_clock_gettime() failed"); |
| 1078 | |
| 1079 | elapsed = (tst_timespec_diff_ms(now, tst_start_time) + 500) / 1000; |
| 1080 | if (results->timeout > elapsed) |
| 1081 | return results->timeout - elapsed; |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
Li Wang | 94823cf | 2017-07-18 16:23:00 +0800 | [diff] [blame] | 1086 | void tst_set_timeout(int timeout) |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 1087 | { |
| 1088 | char *mul = getenv("LTP_TIMEOUT_MUL"); |
| 1089 | |
Li Wang | 94823cf | 2017-07-18 16:23:00 +0800 | [diff] [blame] | 1090 | if (timeout == -1) { |
| 1091 | tst_res(TINFO, "Timeout per run is disabled"); |
| 1092 | return; |
| 1093 | } |
| 1094 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 1095 | results->timeout = timeout; |
| 1096 | |
| 1097 | if (mul) { |
| 1098 | float m = atof(mul); |
| 1099 | |
| 1100 | if (m < 1) |
| 1101 | tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul); |
| 1102 | |
| 1103 | results->timeout = results->timeout * m + 0.5; |
| 1104 | } |
| 1105 | |
| 1106 | tst_res(TINFO, "Timeout per run is %uh %02um %02us", |
| 1107 | results->timeout/3600, (results->timeout%3600)/60, |
| 1108 | results->timeout % 60); |
| 1109 | |
| 1110 | if (getpid() == lib_pid) |
| 1111 | alarm(results->timeout); |
| 1112 | else |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1113 | heartbeat(); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1116 | static int fork_testrun(void) |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1117 | { |
| 1118 | int status; |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1119 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 1120 | if (tst_test->timeout) |
| 1121 | tst_set_timeout(tst_test->timeout); |
| 1122 | else |
| 1123 | tst_set_timeout(300); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1124 | |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 1125 | SAFE_SIGNAL(SIGINT, sigint_handler); |
| 1126 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1127 | test_pid = fork(); |
| 1128 | if (test_pid < 0) |
| 1129 | tst_brk(TBROK | TERRNO, "fork()"); |
| 1130 | |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 1131 | if (!test_pid) { |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 1132 | SAFE_SIGNAL(SIGALRM, SIG_DFL); |
| 1133 | SAFE_SIGNAL(SIGUSR1, SIG_DFL); |
| 1134 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 1135 | SAFE_SETPGID(0, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1136 | testrun(); |
Cyril Hrubis | 0f053c8 | 2016-06-07 14:29:39 +0200 | [diff] [blame] | 1137 | } |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1138 | |
| 1139 | SAFE_WAITPID(test_pid, &status, 0); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1140 | alarm(0); |
Cyril Hrubis | a41e994 | 2016-08-04 16:31:06 +0200 | [diff] [blame] | 1141 | SAFE_SIGNAL(SIGINT, SIG_DFL); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1142 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1143 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1144 | return WEXITSTATUS(status); |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 1145 | |
| 1146 | if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) { |
| 1147 | tst_res(TINFO, "If you are running on slow machine, " |
| 1148 | "try exporting LTP_TIMEOUT_MUL > 1"); |
| 1149 | tst_brk(TBROK, "Test killed! (timeout?)"); |
| 1150 | } |
| 1151 | |
| 1152 | if (WIFSIGNALED(status)) |
| 1153 | tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status))); |
| 1154 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | static int run_tcases_per_fs(void) |
| 1159 | { |
| 1160 | int ret = 0; |
| 1161 | unsigned int i; |
Cyril Hrubis | dbe56e5 | 2019-06-19 14:20:19 +0200 | [diff] [blame] | 1162 | const char *const *filesystems = tst_get_supported_fs_types(tst_test->dev_fs_flags); |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1163 | |
| 1164 | if (!filesystems[0]) |
| 1165 | tst_brk(TCONF, "There are no supported filesystems"); |
| 1166 | |
| 1167 | for (i = 0; filesystems[i]; i++) { |
Xiong Zhou | a0d8c2e | 2018-03-24 08:47:51 +0800 | [diff] [blame] | 1168 | |
| 1169 | tst_res(TINFO, "Testing on %s", filesystems[i]); |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1170 | tdev.fs_type = filesystems[i]; |
| 1171 | |
| 1172 | prepare_device(); |
| 1173 | |
| 1174 | ret = fork_testrun(); |
| 1175 | |
| 1176 | if (mntpoint_mounted) { |
| 1177 | tst_umount(tst_test->mntpoint); |
| 1178 | mntpoint_mounted = 0; |
| 1179 | } |
| 1180 | |
| 1181 | if (ret == TCONF) { |
| 1182 | update_results(ret); |
| 1183 | continue; |
| 1184 | } |
| 1185 | |
| 1186 | if (ret == 0) |
| 1187 | continue; |
| 1188 | |
| 1189 | do_exit(ret); |
| 1190 | } |
| 1191 | |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
Cyril Hrubis | ce08216 | 2019-03-06 16:24:29 +0100 | [diff] [blame] | 1195 | unsigned int tst_variant; |
| 1196 | |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1197 | void tst_run_tcases(int argc, char *argv[], struct tst_test *self) |
| 1198 | { |
Cyril Hrubis | ce08216 | 2019-03-06 16:24:29 +0100 | [diff] [blame] | 1199 | int ret = 0; |
| 1200 | unsigned int test_variants = 1; |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1201 | |
| 1202 | lib_pid = getpid(); |
| 1203 | tst_test = self; |
| 1204 | |
| 1205 | do_setup(argc, argv); |
| 1206 | |
| 1207 | TCID = tid; |
| 1208 | |
| 1209 | SAFE_SIGNAL(SIGALRM, alarm_handler); |
| 1210 | SAFE_SIGNAL(SIGUSR1, heartbeat_handler); |
| 1211 | |
Cyril Hrubis | ce08216 | 2019-03-06 16:24:29 +0100 | [diff] [blame] | 1212 | if (tst_test->test_variants) |
| 1213 | test_variants = tst_test->test_variants; |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1214 | |
Cyril Hrubis | ce08216 | 2019-03-06 16:24:29 +0100 | [diff] [blame] | 1215 | for (tst_variant = 0; tst_variant < test_variants; tst_variant++) { |
| 1216 | if (tst_test->all_filesystems) |
| 1217 | ret |= run_tcases_per_fs(); |
| 1218 | else |
| 1219 | ret |= fork_testrun(); |
| 1220 | |
| 1221 | if (ret & ~(TCONF)) |
| 1222 | goto exit; |
| 1223 | } |
| 1224 | |
| 1225 | exit: |
Cyril Hrubis | 35b8a13 | 2017-09-01 10:45:26 +0200 | [diff] [blame] | 1226 | do_exit(ret); |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 1227 | } |
Michael Moese | 1ab33ce | 2018-03-09 15:16:08 +0100 | [diff] [blame] | 1228 | |
| 1229 | |
| 1230 | void tst_flush(void) |
| 1231 | { |
| 1232 | int rval; |
| 1233 | |
| 1234 | rval = fflush(stderr); |
| 1235 | if (rval != 0) |
| 1236 | tst_brk(TBROK | TERRNO, "fflush(stderr) failed"); |
| 1237 | |
| 1238 | rval = fflush(stderr); |
| 1239 | if (rval != 0) |
| 1240 | tst_brk(TBROK | TERRNO, "fflush(stdout) failed"); |
| 1241 | |
| 1242 | } |