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