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