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