blob: 80808854e563c805989f57d34aa8ae3abde8b0d6 [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
Jan Stancek332540e2016-06-08 16:48:22 +020073static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020074static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020075
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010076static void setup_ipc(void)
77{
78 size_t size = getpagesize();
79
Steven Jackson9f41dcf2016-12-21 20:09:01 +000080 if (access("/dev/shm", F_OK) == 0) {
81 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080082 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000083 } 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 Wangb5d620a2017-11-01 13:15:23 +080091 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000092 free(tmpdir);
93 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010094
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 Stancek2113eef2017-10-04 12:25:42 +020098 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010099
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() */
Jan Stancek03fc5372017-10-10 13:36:59 +0200105 if (tst_test->needs_checkpoints) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100106 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200107 putenv(ipc_path);
108 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100109 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200110 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100111
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
120static 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 Hrubis9726b902017-07-27 11:28:50 +0200127 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100128 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
129
Cyril Hrubis9726b902017-07-27 11:28:50 +0200130 if (results) {
131 msync((void*)results, size, MS_SYNC);
132 munmap((void*)results, size);
133 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100134}
135
136void tst_reinit(void)
137{
Jan Stancek03fc5372017-10-10 13:36:59 +0200138 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100139 size_t size = getpagesize();
140 int fd;
141 void *ptr;
142
143 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200144 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100145
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
151 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
152 tst_futexes = (char*)ptr + sizeof(struct results);
153 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
154
155 SAFE_CLOSE(fd);
156}
157
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100158static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100159{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100160 if (!results)
161 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100162
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
179static 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;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100184 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100185 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 Hrubis6440c5d2017-02-09 15:41:24 +0100209 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100210 }
211
212 if (ttype & TERRNO)
213 str_errno = tst_strerrno(errno);
214
215 if (ttype & TTERRNO)
216 str_errno = tst_strerrno(TEST_ERRNO);
217
Richard Palethorpefb32e892018-03-13 16:24:47 +0100218 if (ttype & TRERRNO) {
219 ret = TEST_RETURN < 0 ? -(int)TEST_RETURN : (int)TEST_RETURN;
220 str_errno = tst_strerrno(ret);
221 }
222
Petr Vorela7f61332017-01-24 20:47:30 +0100223 ret = snprintf(str, size, "%s:%i: ", file, lineno);
224 str += ret;
225 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100226
Cyril Hrubis047c7272017-02-13 16:23:36 +0100227 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100228 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
229 res, ANSI_COLOR_RESET);
230 else
231 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100232 str += ret;
233 size -= ret;
234
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100235 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100236 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100237 str += MIN(ret, ssize);
238 size -= MIN(ret, ssize);
239 if (ret >= ssize) {
240 tst_res_(file, lineno, TWARN,
241 "Next message is too long and truncated:");
242 } else if (str_errno) {
243 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100244 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100245 str += MIN(ret, ssize);
246 size -= MIN(ret, ssize);
247 if (ret >= ssize)
248 tst_res_(file, lineno, TWARN,
249 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100250 }
251
252 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100253
254 fputs(buf, stderr);
255}
256
257void tst_vres_(const char *file, const int lineno, int ttype,
258 const char *fmt, va_list va)
259{
260 print_result(file, lineno, ttype, fmt, va);
261
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100262 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100263}
264
265void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100266 const char *fmt, va_list va);
267
268static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
269 const char *fmt, va_list va) = tst_vbrk_;
270
271static void tst_cvres(const char *file, const int lineno, int ttype,
272 const char *fmt, va_list va)
273{
274 if (TTYPE_RESULT(ttype) == TBROK) {
275 ttype &= ~TTYPE_MASK;
276 ttype |= TWARN;
277 }
278
279 print_result(file, lineno, ttype, fmt, va);
280 update_results(TTYPE_RESULT(ttype));
281}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100282
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200283static void do_test_cleanup(void)
284{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100285 tst_brk_handler = tst_cvres;
286
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200287 if (tst_test->cleanup)
288 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100289
290 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200291}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100292
293void tst_vbrk_(const char *file, const int lineno, int ttype,
294 const char *fmt, va_list va)
295{
296 print_result(file, lineno, ttype, fmt, va);
297
Steve Mucklec20831d2017-09-20 13:23:06 -0700298 /*
299 * The getpid implementation in some C library versions may cause cloned
300 * test threads to show the same pid as their parent when CLONE_VM is
301 * specified but CLONE_THREAD is not. Use direct syscall to avoid
302 * cleanup running in the child.
303 */
304 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200305 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100306
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200307 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200308 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200309
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100310 exit(TTYPE_RESULT(ttype));
311}
312
313void tst_res_(const char *file, const int lineno, int ttype,
314 const char *fmt, ...)
315{
316 va_list va;
317
318 va_start(va, fmt);
319 tst_vres_(file, lineno, ttype, fmt, va);
320 va_end(va);
321}
322
323void tst_brk_(const char *file, const int lineno, int ttype,
324 const char *fmt, ...)
325{
326 va_list va;
327
328 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100329 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100330 va_end(va);
331}
332
333static void check_child_status(pid_t pid, int status)
334{
335 int ret;
336
337 if (WIFSIGNALED(status)) {
338 tst_brk(TBROK, "Child (%i) killed by signal %s",
339 pid, tst_strsig(WTERMSIG(status)));
340 }
341
342 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100343 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100344
345 ret = WEXITSTATUS(status);
346 switch (ret) {
347 case TPASS:
348 break;
349 case TBROK:
350 case TCONF:
351 tst_brk(ret, "Reported by child (%i)", pid);
352 default:
353 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
354 }
355}
356
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300357void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100358{
359 int status;
360 pid_t pid;
361
362 for (;;) {
363 pid = wait(&status);
364
365 if (pid > 0) {
366 check_child_status(pid, status);
367 continue;
368 }
369
370 if (errno == ECHILD)
371 break;
372
373 if (errno == EINTR)
374 continue;
375
376 tst_brk(TBROK | TERRNO, "wait() failed");
377 }
378}
379
380
381pid_t safe_fork(const char *filename, unsigned int lineno)
382{
383 pid_t pid;
384
385 if (!tst_test->forks_child)
386 tst_brk(TBROK, "test.forks_child must be set!");
387
Michael Moese9da42372018-03-09 15:16:09 +0100388 tst_flush();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100389
390 pid = fork();
391 if (pid < 0)
392 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
393
394 return pid;
395}
396
397static struct option {
398 char *optstr;
399 char *help;
400} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200401 {"h", "-h Prints this help"},
402 {"i:", "-i n Execute test n times"},
403 {"I:", "-I x Execute test for n seconds"},
404 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100405};
406
407static void print_help(void)
408{
409 unsigned int i;
410
411 for (i = 0; i < ARRAY_SIZE(options); i++)
412 fprintf(stderr, "%s\n", options[i].help);
413
414 if (!tst_test->options)
415 return;
416
417 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200418 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100419}
420
421static void check_option_collision(void)
422{
423 unsigned int i, j;
424 struct tst_option *toptions = tst_test->options;
425
426 if (!toptions)
427 return;
428
429 for (i = 0; toptions[i].optstr; i++) {
430 for (j = 0; j < ARRAY_SIZE(options); j++) {
431 if (toptions[i].optstr[0] == options[j].optstr[0]) {
432 tst_brk(TBROK, "Option collision '%s'",
433 options[j].help);
434 }
435 }
436 }
437}
438
439static unsigned int count_options(void)
440{
441 unsigned int i;
442
443 if (!tst_test->options)
444 return 0;
445
446 for (i = 0; tst_test->options[i].optstr; i++);
447
448 return i;
449}
450
451static void parse_topt(unsigned int topts_len, int opt, char *optarg)
452{
453 unsigned int i;
454 struct tst_option *toptions = tst_test->options;
455
456 for (i = 0; i < topts_len; i++) {
457 if (toptions[i].optstr[0] == opt)
458 break;
459 }
460
461 if (i >= topts_len)
462 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
463
Jan Stancekc07d36c2016-08-01 11:44:55 +0200464 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100465}
466
467/* see self_exec.c */
468#ifdef UCLINUX
469extern char *child_args;
470#endif
471
472static void parse_opts(int argc, char *argv[])
473{
474 unsigned int i, topts_len = count_options();
475 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
476 int opt;
477
478 check_option_collision();
479
480 optstr[0] = 0;
481
482 for (i = 0; i < ARRAY_SIZE(options); i++)
483 strcat(optstr, options[i].optstr);
484
485 for (i = 0; i < topts_len; i++)
486 strcat(optstr, tst_test->options[i].optstr);
487
488 while ((opt = getopt(argc, argv, optstr)) > 0) {
489 switch (opt) {
490 case '?':
491 print_help();
492 tst_brk(TBROK, "Invalid option");
493 case 'h':
494 print_help();
495 exit(0);
496 case 'i':
497 iterations = atoi(optarg);
498 break;
499 case 'I':
500 duration = atof(optarg);
501 break;
502 case 'C':
503#ifdef UCLINUX
504 child_args = optarg;
505#endif
506 break;
507 default:
508 parse_topt(topts_len, opt, optarg);
509 }
510 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200511
512 if (optind < argc)
513 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100514}
515
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200516int tst_parse_int(const char *str, int *val, int min, int max)
517{
518 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300519
520 if (!str)
521 return 0;
522
523 int ret = tst_parse_long(str, &rval, min, max);
524
525 if (ret)
526 return ret;
527
528 *val = (int)rval;
529 return 0;
530}
531
532int tst_parse_long(const char *str, long *val, long min, long max)
533{
534 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200535 char *end;
536
537 if (!str)
538 return 0;
539
540 errno = 0;
541 rval = strtol(str, &end, 10);
542
543 if (str == end || *end != '\0')
544 return EINVAL;
545
546 if (errno)
547 return errno;
548
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300549 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200550 return ERANGE;
551
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300552 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200553 return 0;
554}
555
556int tst_parse_float(const char *str, float *val, float min, float max)
557{
558 double rval;
559 char *end;
560
561 if (!str)
562 return 0;
563
564 errno = 0;
565 rval = strtod(str, &end);
566
567 if (str == end || *end != '\0')
568 return EINVAL;
569
570 if (errno)
571 return errno;
572
573 if (rval > (double)max || rval < (double)min)
574 return ERANGE;
575
576 *val = (float)rval;
577 return 0;
578}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100579
Cyril Hrubisfa495172016-06-08 16:06:35 +0200580static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100581{
Xiao Yang11dfc322016-06-16 15:52:04 +0800582 if (results) {
583 printf("\nSummary:\n");
584 printf("passed %d\n", results->passed);
585 printf("failed %d\n", results->failed);
586 printf("skipped %d\n", results->skipped);
587 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100588
Cyril Hrubisfecdd882018-01-17 14:51:30 +0100589 if (results->passed && ret == TCONF)
590 ret = 0;
591
Xiao Yang11dfc322016-06-16 15:52:04 +0800592 if (results->failed)
593 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100594
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200595 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800596 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100597
Xiao Yang11dfc322016-06-16 15:52:04 +0800598 if (results->warnings)
599 ret |= TWARN;
600 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100601
Jan Stancek332540e2016-06-08 16:48:22 +0200602 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100603
604 exit(ret);
605}
606
607void check_kver(void)
608{
609 int v1, v2, v3;
610
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100611 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
612 tst_res(TWARN,
613 "Invalid kernel version %s, expected %%d.%%d.%%d",
614 tst_test->min_kver);
615 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100616
617 if (tst_kvercmp(v1, v2, v3) < 0) {
618 tst_brk(TCONF, "The test requires kernel %s or newer",
619 tst_test->min_kver);
620 }
621}
622
623static int results_equal(struct results *a, struct results *b)
624{
625 if (a->passed != b->passed)
626 return 0;
627
628 if (a->failed != b->failed)
629 return 0;
630
631 if (a->skipped != b->skipped)
632 return 0;
633
634 return 1;
635}
636
637static int needs_tmpdir(void)
638{
639 return tst_test->needs_tmpdir ||
640 tst_test->needs_device ||
Cyril Hrubis16352e42018-04-05 16:01:47 +0200641 tst_test->mntpoint ||
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100642 tst_test->resource_files ||
643 tst_test->needs_checkpoints;
644}
645
646static void copy_resources(void)
647{
648 unsigned int i;
649
650 for (i = 0; tst_test->resource_files[i]; i++)
651 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
652}
653
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800654static const char *get_tid(char *argv[])
655{
656 char *p;
657
658 if (!argv[0] || !argv[0][0]) {
659 tst_res(TINFO, "argv[0] is empty!");
660 return "ltp_empty_argv";
661 }
662
663 p = strrchr(argv[0], '/');
664 if (p)
665 return p+1;
666
667 return argv[0];
668}
669
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100670static struct tst_device tdev;
671struct tst_device *tst_device;
672
Cyril Hrubisc4596542017-06-20 15:42:18 +0200673static void assert_test_fn(void)
674{
675 int cnt = 0;
676
677 if (tst_test->test)
678 cnt++;
679
680 if (tst_test->test_all)
681 cnt++;
682
683 if (tst_test->sample)
684 cnt++;
685
686 if (!cnt)
687 tst_brk(TBROK, "No test function speficied");
688
689 if (cnt != 1)
690 tst_brk(TBROK, "You can define only one test function");
691
692 if (tst_test->test && !tst_test->tcnt)
693 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
694
695 if (!tst_test->test && tst_test->tcnt)
696 tst_brk(TBROK, "You can define tcnt only for test()");
697}
698
Cyril Hrubise6da8672018-04-05 16:01:48 +0200699static int prepare_and_mount_ro_fs(const char *dev,
700 const char *mntpoint,
701 const char *fs_type)
702{
703 char buf[PATH_MAX];
704
705 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
706 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
707 dev, mntpoint, fs_type);
708 return 1;
709 }
710
711 mntpoint_mounted = 1;
712
713 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
714 SAFE_MKDIR(buf, 0777);
715
716 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
717 SAFE_FILE_PRINTF(buf, "file content");
718 SAFE_CHMOD(buf, 0777);
719
720 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
721
722 return 0;
723}
724
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200725static void prepare_device(void)
726{
727 if (tst_test->format_device) {
728 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
729 tst_test->dev_extra_opt);
730 }
731
Cyril Hrubise6da8672018-04-05 16:01:48 +0200732 if (tst_test->needs_rofs) {
733 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
734 tdev.fs_type);
735 return;
736 }
737
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200738 if (tst_test->mount_device) {
739 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
740 tst_test->mnt_flags, tst_test->mnt_data);
741 mntpoint_mounted = 1;
742 }
743}
744
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100745static void do_setup(int argc, char *argv[])
746{
747 if (!tst_test)
748 tst_brk(TBROK, "No tests to run");
749
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200750 if (tst_test->tconf_msg)
751 tst_brk(TCONF, "%s", tst_test->tconf_msg);
752
Cyril Hrubisc4596542017-06-20 15:42:18 +0200753 assert_test_fn();
754
Li Wangb5d620a2017-11-01 13:15:23 +0800755 tid = get_tid(argv);
756
Cyril Hrubisc4596542017-06-20 15:42:18 +0200757 if (tst_test->sample)
758 tst_test = tst_timer_test_setup(tst_test);
759
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200760 parse_opts(argc, argv);
761
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100762 if (tst_test->needs_root && geteuid() != 0)
763 tst_brk(TCONF, "Test needs to be run as root");
764
765 if (tst_test->min_kver)
766 check_kver();
767
Cyril Hrubis874326d2017-02-14 15:59:40 +0100768 if (tst_test->format_device)
769 tst_test->needs_device = 1;
770
771 if (tst_test->mount_device) {
772 tst_test->needs_device = 1;
773 tst_test->format_device = 1;
774 }
775
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200776 if (tst_test->all_filesystems)
777 tst_test->needs_device = 1;
778
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100779 setup_ipc();
780
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000781 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100782 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100783
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700784 if (tst_test->mntpoint)
785 SAFE_MKDIR(tst_test->mntpoint, 0777);
786
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200787 if ((tst_test->needs_rofs || tst_test->mount_device ||
788 tst_test->all_filesystems) && !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700789 tst_brk(TBROK, "tst_test->mntpoint must be set!");
790 }
791
792 if (tst_test->needs_rofs) {
793 /* If we failed to mount read-only tmpfs. Fallback to
Cyril Hrubise6da8672018-04-05 16:01:48 +0200794 * using a device with read-only filesystem.
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700795 */
Cyril Hrubise6da8672018-04-05 16:01:48 +0200796 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
797 tst_res(TINFO, "Can't mount tmpfs read-only, "
798 "falling back to block device...");
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700799 tst_test->needs_device = 1;
800 tst_test->format_device = 1;
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700801 }
802 }
803
804 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100805 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100806
807 if (!tdev.dev)
808 tst_brk(TCONF, "Failed to acquire device");
809
810 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100811
812 if (tst_test->dev_fs_type)
813 tdev.fs_type = tst_test->dev_fs_type;
814 else
815 tdev.fs_type = tst_dev_fs_type();
816
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200817 if (!tst_test->all_filesystems)
818 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100819 }
820
821 if (tst_test->resource_files)
822 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200823}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100824
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200825static void do_test_setup(void)
826{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100827 main_pid = getpid();
828
829 if (tst_test->setup)
830 tst_test->setup();
831
832 if (main_pid != getpid())
833 tst_brk(TBROK, "Runaway child in setup()!");
834}
835
836static void do_cleanup(void)
837{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700838 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100839 tst_umount(tst_test->mntpoint);
840
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100841 if (tst_test->needs_device && tdev.dev)
842 tst_release_device(tdev.dev);
843
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000844 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100845 /* avoid munmap() on wrong pointer in tst_rmdir() */
846 tst_futexes = NULL;
847 tst_rmdir();
848 }
Jan Stancek332540e2016-06-08 16:48:22 +0200849
850 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100851}
852
853static void run_tests(void)
854{
855 unsigned int i;
856 struct results saved_results;
857
858 if (!tst_test->test) {
859 saved_results = *results;
860 tst_test->test_all();
861
862 if (getpid() != main_pid) {
863 exit(0);
864 }
865
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300866 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100867
868 if (results_equal(&saved_results, results))
869 tst_brk(TBROK, "Test haven't reported results!");
870 return;
871 }
872
873 for (i = 0; i < tst_test->tcnt; i++) {
874 saved_results = *results;
875 tst_test->test(i);
876
877 if (getpid() != main_pid) {
878 exit(0);
879 }
880
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300881 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100882
883 if (results_equal(&saved_results, results))
884 tst_brk(TBROK, "Test %i haven't reported results!", i);
885 }
886}
887
888static unsigned long long get_time_ms(void)
889{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100890 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100891
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100892 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
893 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100894
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100895 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100896}
897
Jan Stancekb95b1992017-10-10 15:47:58 +0200898static void add_paths(void)
899{
900 char *old_path = getenv("PATH");
901 const char *start_dir;
902 char *new_path;
903
904 start_dir = tst_get_startwd();
905
906 if (old_path)
907 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
908 else
909 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
910
911 SAFE_SETENV("PATH", new_path, 1);
912 free(new_path);
913}
914
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200915static void heartbeat(void)
916{
917 kill(getppid(), SIGUSR1);
918}
919
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200920static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100921{
922 unsigned int i = 0;
923 unsigned long long stop_time = 0;
924 int cont = 1;
925
Jan Stancekb95b1992017-10-10 15:47:58 +0200926 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200927 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100928
929 if (duration > 0)
930 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
931
932 for (;;) {
933 cont = 0;
934
935 if (i < (unsigned int)iterations) {
936 i++;
937 cont = 1;
938 }
939
940 if (stop_time && get_time_ms() < stop_time)
941 cont = 1;
942
943 if (!cont)
944 break;
945
946 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200947 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100948 }
949
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200950 do_test_cleanup();
951 exit(0);
952}
953
954static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200955
Cyril Hrubis79163172017-05-18 10:57:07 +0200956
957static volatile sig_atomic_t sigkill_retries;
958
959#define WRITE_MSG(msg) do { \
960 if (write(2, msg, sizeof(msg) - 1)) { \
961 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
962 } \
963} while (0)
964
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200965static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
966{
Cyril Hrubis79163172017-05-18 10:57:07 +0200967 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200968 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200969 alarm(5);
970
971 if (++sigkill_retries > 10) {
972 WRITE_MSG("Cannot kill test processes!\n");
973 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
974 WRITE_MSG("Exitting uncleanly...\n");
975 _exit(TFAIL);
976 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200977}
978
979static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
980{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200981 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200982 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200983}
984
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200985static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
986{
987 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200988 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200989 kill(-test_pid, SIGKILL);
990 }
991}
992
Li Wang94823cf2017-07-18 16:23:00 +0800993void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200994{
995 char *mul = getenv("LTP_TIMEOUT_MUL");
996
Li Wang94823cf2017-07-18 16:23:00 +0800997 if (timeout == -1) {
998 tst_res(TINFO, "Timeout per run is disabled");
999 return;
1000 }
1001
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001002 results->timeout = timeout;
1003
1004 if (mul) {
1005 float m = atof(mul);
1006
1007 if (m < 1)
1008 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
1009
1010 results->timeout = results->timeout * m + 0.5;
1011 }
1012
1013 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1014 results->timeout/3600, (results->timeout%3600)/60,
1015 results->timeout % 60);
1016
1017 if (getpid() == lib_pid)
1018 alarm(results->timeout);
1019 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001020 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001021}
1022
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001023static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001024{
1025 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001026
Cyril Hrubis2ad59b72016-08-03 15:53:55 +02001027 if (tst_test->timeout)
1028 tst_set_timeout(tst_test->timeout);
1029 else
1030 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001031
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001032 SAFE_SIGNAL(SIGINT, sigint_handler);
1033
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001034 test_pid = fork();
1035 if (test_pid < 0)
1036 tst_brk(TBROK | TERRNO, "fork()");
1037
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001038 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001039 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1040 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1041 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001042 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001043 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001044 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001045
1046 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001047 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001048 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001049
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001050 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001051 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001052
1053 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1054 tst_res(TINFO, "If you are running on slow machine, "
1055 "try exporting LTP_TIMEOUT_MUL > 1");
1056 tst_brk(TBROK, "Test killed! (timeout?)");
1057 }
1058
1059 if (WIFSIGNALED(status))
1060 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1061
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001062 return 0;
1063}
1064
1065static int run_tcases_per_fs(void)
1066{
1067 int ret = 0;
1068 unsigned int i;
1069 const char *const *filesystems = tst_get_supported_fs_types();
1070
1071 if (!filesystems[0])
1072 tst_brk(TCONF, "There are no supported filesystems");
1073
1074 for (i = 0; filesystems[i]; i++) {
Xiong Zhoua0d8c2e2018-03-24 08:47:51 +08001075
1076 tst_res(TINFO, "Testing on %s", filesystems[i]);
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001077 tdev.fs_type = filesystems[i];
1078
1079 prepare_device();
1080
1081 ret = fork_testrun();
1082
1083 if (mntpoint_mounted) {
1084 tst_umount(tst_test->mntpoint);
1085 mntpoint_mounted = 0;
1086 }
1087
1088 if (ret == TCONF) {
1089 update_results(ret);
1090 continue;
1091 }
1092
1093 if (ret == 0)
1094 continue;
1095
1096 do_exit(ret);
1097 }
1098
1099 return ret;
1100}
1101
1102void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1103{
1104 int ret;
1105
1106 lib_pid = getpid();
1107 tst_test = self;
1108
1109 do_setup(argc, argv);
1110
1111 TCID = tid;
1112
1113 SAFE_SIGNAL(SIGALRM, alarm_handler);
1114 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1115
1116 if (tst_test->all_filesystems)
1117 ret = run_tcases_per_fs();
1118 else
1119 ret = fork_testrun();
1120
1121 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001122}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001123
1124
1125void tst_flush(void)
1126{
1127 int rval;
1128
1129 rval = fflush(stderr);
1130 if (rval != 0)
1131 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1132
1133 rval = fflush(stderr);
1134 if (rval != 0)
1135 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1136
1137}