blob: 008bcefe0474780463c53317b2de18aa804c4ca8 [file] [log] [blame]
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001/*
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 Patilc9a7def2017-09-19 12:49:58 -070024#include <sys/mount.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010025#include <sys/types.h>
26#include <sys/wait.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010027
28#define TST_NO_DEFAULT_MAIN
29#include "tst_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010030#include "tst_device.h"
31#include "lapi/futex.h"
Steve Mucklec20831d2017-09-20 13:23:06 -070032#include "lapi/syscalls.h"
Petr Vorel3a0ef862017-03-01 15:31:08 +010033#include "tst_ansi_color.h"
Jan Stancekb95b1992017-10-10 15:47:58 +020034#include "tst_safe_stdio.h"
Cyril Hrubisc4596542017-06-20 15:42:18 +020035#include "tst_timer_test.h"
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +010036#include "tst_clocks.h"
37#include "tst_timer.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010038
39#include "old_resource.h"
40#include "old_device.h"
41#include "old_tmpdir.h"
42
43struct tst_test *tst_test;
44
Li Wangb5d620a2017-11-01 13:15:23 +080045static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010046static int iterations = 1;
47static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020048static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070049static int mntpoint_mounted;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010050
51struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020052 int passed;
53 int skipped;
54 int failed;
55 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020056 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010057};
58
59static struct results *results;
60
61static int ipc_fd;
62
63extern void *tst_futexes;
64extern unsigned int tst_max_futexes;
65
66#define IPC_ENV_VAR "LTP_IPC_PATH"
67
68static char ipc_path[1024];
69const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010070
71static char shm_path[1024];
72
Christian Lanig18b780d2018-07-23 15:24:50 +020073int TST_ERR;
74long TST_RET;
75
Jan Stancek332540e2016-06-08 16:48:22 +020076static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020077static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020078
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010079static void setup_ipc(void)
80{
81 size_t size = getpagesize();
82
Steven Jackson9f41dcf2016-12-21 20:09:01 +000083 if (access("/dev/shm", F_OK) == 0) {
84 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080085 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000086 } 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 Wangb5d620a2017-11-01 13:15:23 +080094 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000095 free(tmpdir);
96 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010097
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 Stancek2113eef2017-10-04 12:25:42 +0200101 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100102
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 Stancek03fc5372017-10-10 13:36:59 +0200108 if (tst_test->needs_checkpoints) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100109 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200110 putenv(ipc_path);
111 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100112 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200113 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100114
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
123static 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 Hrubis9726b902017-07-27 11:28:50 +0200130 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100131 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
132
Cyril Hrubis9726b902017-07-27 11:28:50 +0200133 if (results) {
134 msync((void*)results, size, MS_SYNC);
135 munmap((void*)results, size);
136 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100137}
138
139void tst_reinit(void)
140{
Jan Stancek03fc5372017-10-10 13:36:59 +0200141 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100142 size_t size = getpagesize();
143 int fd;
144 void *ptr;
145
146 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200147 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100148
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 Hrubis160ffcc2017-02-14 10:21:08 +0100161static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100162{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100163 if (!results)
164 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100165
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
182static 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 Kabatovacecbd0c2017-11-07 17:10:42 +0100187 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100188 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 Hrubis6440c5d2017-02-09 15:41:24 +0100212 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100213 }
214
215 if (ttype & TERRNO)
216 str_errno = tst_strerrno(errno);
217
218 if (ttype & TTERRNO)
Christian Lanig18b780d2018-07-23 15:24:50 +0200219 str_errno = tst_strerrno(TST_ERR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100220
Richard Palethorpefb32e892018-03-13 16:24:47 +0100221 if (ttype & TRERRNO) {
Christian Lanig18b780d2018-07-23 15:24:50 +0200222 ret = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
Richard Palethorpefb32e892018-03-13 16:24:47 +0100223 str_errno = tst_strerrno(ret);
224 }
225
Petr Vorela7f61332017-01-24 20:47:30 +0100226 ret = snprintf(str, size, "%s:%i: ", file, lineno);
227 str += ret;
228 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100229
Cyril Hrubis047c7272017-02-13 16:23:36 +0100230 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100231 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100235 str += ret;
236 size -= ret;
237
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100238 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100239 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100240 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 Vorela7f61332017-01-24 20:47:30 +0100247 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100248 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 Vorela7f61332017-01-24 20:47:30 +0100253 }
254
255 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100256
257 fputs(buf, stderr);
258}
259
260void 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 Hrubis160ffcc2017-02-14 10:21:08 +0100265 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100266}
267
268void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100269 const char *fmt, va_list va);
270
271static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
272 const char *fmt, va_list va) = tst_vbrk_;
273
274static 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100285
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200286static void do_test_cleanup(void)
287{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100288 tst_brk_handler = tst_cvres;
289
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200290 if (tst_test->cleanup)
291 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100292
293 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200294}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100295
296void 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 Mucklec20831d2017-09-20 13:23:06 -0700301 /*
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 Hrubis4aebb6c2016-06-07 13:40:41 +0200308 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100309
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200310 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200311 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200312
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100313 exit(TTYPE_RESULT(ttype));
314}
315
316void 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
326void 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 Hrubis6440c5d2017-02-09 15:41:24 +0100332 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100333 va_end(va);
334}
335
336static 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 Vorelf8853482017-10-31 09:40:38 +0100346 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100347
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 Kholmanskikh6b56aa72016-08-04 17:16:31 +0300360void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100361{
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
384pid_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 Moese9da42372018-03-09 15:16:09 +0100391 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100392
393 pid = fork();
394 if (pid < 0)
395 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
396
397 return pid;
398}
399
400static struct option {
401 char *optstr;
402 char *help;
403} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200404 {"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 Hrubisbbdb9f72016-03-16 15:53:57 +0100408};
409
410static 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 Hrubisb819c222016-08-03 14:36:07 +0200421 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100422}
423
424static 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
442static 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
454static 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 Stancekc07d36c2016-08-01 11:44:55 +0200467 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100468}
469
470/* see self_exec.c */
471#ifdef UCLINUX
472extern char *child_args;
473#endif
474
475static 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 Hrubis9dc07e02017-10-02 11:11:04 +0200514
515 if (optind < argc)
516 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100517}
518
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200519int tst_parse_int(const char *str, int *val, int min, int max)
520{
521 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300522
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
535int tst_parse_long(const char *str, long *val, long min, long max)
536{
537 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200538 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 Kodanevdd90c002016-12-18 00:36:00 +0300552 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200553 return ERANGE;
554
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300555 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200556 return 0;
557}
558
559int 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100582
Cyril Hrubisfa495172016-06-08 16:06:35 +0200583static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100584{
Xiao Yang11dfc322016-06-16 15:52:04 +0800585 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100591
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100592 if (results->passed && ret == TCONF)
593 ret = 0;
594
Xiao Yang11dfc322016-06-16 15:52:04 +0800595 if (results->failed)
596 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100597
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200598 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800599 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100600
Xiao Yang11dfc322016-06-16 15:52:04 +0800601 if (results->warnings)
602 ret |= TWARN;
603 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100604
Jan Stancek332540e2016-06-08 16:48:22 +0200605 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100606
607 exit(ret);
608}
609
610void check_kver(void)
611{
612 int v1, v2, v3;
613
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100614 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100619
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
626static 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
640static int needs_tmpdir(void)
641{
642 return tst_test->needs_tmpdir ||
643 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200644 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100645 tst_test->resource_files ||
646 tst_test->needs_checkpoints;
647}
648
649static 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 Hrubisa5bf5252017-03-14 15:25:29 +0800657static 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100673static struct tst_device tdev;
674struct tst_device *tst_device;
675
Cyril Hrubisc4596542017-06-20 15:42:18 +0200676static 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 Hrubise6da8672018-04-05 16:01:48 +0200702static 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 Hrubis35b8a132017-09-01 10:45:26 +0200728static 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 Hrubise6da8672018-04-05 16:01:48 +0200735 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 Hrubis35b8a132017-09-01 10:45:26 +0200741 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 Hrubisbbdb9f72016-03-16 15:53:57 +0100748static void do_setup(int argc, char *argv[])
749{
750 if (!tst_test)
751 tst_brk(TBROK, "No tests to run");
752
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200753 if (tst_test->tconf_msg)
754 tst_brk(TCONF, "%s", tst_test->tconf_msg);
755
Cyril Hrubisc4596542017-06-20 15:42:18 +0200756 assert_test_fn();
757
Li Wangb5d620a2017-11-01 13:15:23 +0800758 tid = get_tid(argv);
759
Cyril Hrubisc4596542017-06-20 15:42:18 +0200760 if (tst_test->sample)
761 tst_test = tst_timer_test_setup(tst_test);
762
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200763 parse_opts(argc, argv);
764
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100765 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 Hrubis874326d2017-02-14 15:59:40 +0100771 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 Hrubis35b8a132017-09-01 10:45:26 +0200779 if (tst_test->all_filesystems)
780 tst_test->needs_device = 1;
781
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100782 setup_ipc();
783
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000784 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100785 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100786
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700787 if (tst_test->mntpoint)
788 SAFE_MKDIR(tst_test->mntpoint, 0777);
789
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200790 if ((tst_test->needs_rofs || tst_test->mount_device ||
791 tst_test->all_filesystems) && !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700792 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 Hrubise6da8672018-04-05 16:01:48 +0200797 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700798 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200799 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 Patilc9a7def2017-09-19 12:49:58 -0700802 tst_test->needs_device = 1;
803 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700804 }
805 }
806
807 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100808 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100809
810 if (!tdev.dev)
811 tst_brk(TCONF, "Failed to acquire device");
812
813 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100814
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 Hrubis35b8a132017-09-01 10:45:26 +0200820 if (!tst_test->all_filesystems)
821 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100822 }
823
824 if (tst_test->resource_files)
825 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200826}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100827
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200828static void do_test_setup(void)
829{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100830 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
839static void do_cleanup(void)
840{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700841 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100842 tst_umount(tst_test->mntpoint);
843
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100844 if (tst_test->needs_device && tdev.dev)
845 tst_release_device(tdev.dev);
846
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000847 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100848 /* avoid munmap() on wrong pointer in tst_rmdir() */
849 tst_futexes = NULL;
850 tst_rmdir();
851 }
Jan Stancek332540e2016-06-08 16:48:22 +0200852
853 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100854}
855
856static 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 Kholmanskikh6b56aa72016-08-04 17:16:31 +0300869 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100870
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 Kholmanskikh6b56aa72016-08-04 17:16:31 +0300884 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100885
886 if (results_equal(&saved_results, results))
887 tst_brk(TBROK, "Test %i haven't reported results!", i);
888 }
889}
890
891static unsigned long long get_time_ms(void)
892{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100893 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100894
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100895 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
896 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100897
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100898 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100899}
900
Jan Stancekb95b1992017-10-10 15:47:58 +0200901static 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 Hrubis35b8a132017-09-01 10:45:26 +0200918static void heartbeat(void)
919{
920 kill(getppid(), SIGUSR1);
921}
922
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200923static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100924{
925 unsigned int i = 0;
926 unsigned long long stop_time = 0;
927 int cont = 1;
928
Jan Stancekb95b1992017-10-10 15:47:58 +0200929 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200930 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100931
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 Hrubis35b8a132017-09-01 10:45:26 +0200950 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100951 }
952
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200953 do_test_cleanup();
954 exit(0);
955}
956
957static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200958
Cyril Hrubis79163172017-05-18 10:57:07 +0200959
960static 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 Hrubis4aebb6c2016-06-07 13:40:41 +0200968static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
969{
Cyril Hrubis79163172017-05-18 10:57:07 +0200970 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200971 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200972 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 Hrubis4aebb6c2016-06-07 13:40:41 +0200980}
981
982static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
983{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200984 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200985 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200986}
987
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200988static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
989{
990 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200991 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200992 kill(-test_pid, SIGKILL);
993 }
994}
995
Li Wang94823cf2017-07-18 16:23:00 +0800996void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200997{
998 char *mul = getenv("LTP_TIMEOUT_MUL");
999
Li Wang94823cf2017-07-18 16:23:00 +08001000 if (timeout == -1) {
1001 tst_res(TINFO, "Timeout per run is disabled");
1002 return;
1003 }
1004
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001005 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 Hrubis35b8a132017-09-01 10:45:26 +02001023 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001024}
1025
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001026static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001027{
1028 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001029
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001030 if (tst_test->timeout)
1031 tst_set_timeout(tst_test->timeout);
1032 else
1033 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001034
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001035 SAFE_SIGNAL(SIGINT, sigint_handler);
1036
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001037 test_pid = fork();
1038 if (test_pid < 0)
1039 tst_brk(TBROK | TERRNO, "fork()");
1040
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001041 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001042 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1043 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1044 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001045 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001046 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001047 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001048
1049 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001050 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001051 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001052
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001053 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001054 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001055
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 Hrubis35b8a132017-09-01 10:45:26 +02001065 return 0;
1066}
1067
1068static 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 Zhoua0d8c2e2018-03-24 08:47:51 +08001078
1079 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001080 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
1105void 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 Hrubisbbdb9f72016-03-16 15:53:57 +01001125}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001126
1127
1128void 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}