blob: b5fce33b0f8603956a453441391f6a8830d6f388 [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 ||
641 tst_test->resource_files ||
642 tst_test->needs_checkpoints;
643}
644
645static void copy_resources(void)
646{
647 unsigned int i;
648
649 for (i = 0; tst_test->resource_files[i]; i++)
650 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
651}
652
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800653static const char *get_tid(char *argv[])
654{
655 char *p;
656
657 if (!argv[0] || !argv[0][0]) {
658 tst_res(TINFO, "argv[0] is empty!");
659 return "ltp_empty_argv";
660 }
661
662 p = strrchr(argv[0], '/');
663 if (p)
664 return p+1;
665
666 return argv[0];
667}
668
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100669static struct tst_device tdev;
670struct tst_device *tst_device;
671
Cyril Hrubisc4596542017-06-20 15:42:18 +0200672static void assert_test_fn(void)
673{
674 int cnt = 0;
675
676 if (tst_test->test)
677 cnt++;
678
679 if (tst_test->test_all)
680 cnt++;
681
682 if (tst_test->sample)
683 cnt++;
684
685 if (!cnt)
686 tst_brk(TBROK, "No test function speficied");
687
688 if (cnt != 1)
689 tst_brk(TBROK, "You can define only one test function");
690
691 if (tst_test->test && !tst_test->tcnt)
692 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
693
694 if (!tst_test->test && tst_test->tcnt)
695 tst_brk(TBROK, "You can define tcnt only for test()");
696}
697
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200698static void prepare_device(void)
699{
700 if (tst_test->format_device) {
701 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
702 tst_test->dev_extra_opt);
703 }
704
705 if (tst_test->mount_device) {
706 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
707 tst_test->mnt_flags, tst_test->mnt_data);
708 mntpoint_mounted = 1;
709 }
710}
711
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100712static void do_setup(int argc, char *argv[])
713{
714 if (!tst_test)
715 tst_brk(TBROK, "No tests to run");
716
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200717 if (tst_test->tconf_msg)
718 tst_brk(TCONF, "%s", tst_test->tconf_msg);
719
Cyril Hrubisc4596542017-06-20 15:42:18 +0200720 assert_test_fn();
721
Li Wangb5d620a2017-11-01 13:15:23 +0800722 tid = get_tid(argv);
723
Cyril Hrubisc4596542017-06-20 15:42:18 +0200724 if (tst_test->sample)
725 tst_test = tst_timer_test_setup(tst_test);
726
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200727 parse_opts(argc, argv);
728
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100729 if (tst_test->needs_root && geteuid() != 0)
730 tst_brk(TCONF, "Test needs to be run as root");
731
732 if (tst_test->min_kver)
733 check_kver();
734
Cyril Hrubis874326d2017-02-14 15:59:40 +0100735 if (tst_test->format_device)
736 tst_test->needs_device = 1;
737
738 if (tst_test->mount_device) {
739 tst_test->needs_device = 1;
740 tst_test->format_device = 1;
741 }
742
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200743 if (tst_test->all_filesystems)
744 tst_test->needs_device = 1;
745
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100746 setup_ipc();
747
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000748 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100749 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100750
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700751 if (tst_test->mntpoint)
752 SAFE_MKDIR(tst_test->mntpoint, 0777);
753
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200754 if ((tst_test->needs_rofs || tst_test->mount_device ||
755 tst_test->all_filesystems) && !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700756 tst_brk(TBROK, "tst_test->mntpoint must be set!");
757 }
758
759 if (tst_test->needs_rofs) {
760 /* If we failed to mount read-only tmpfs. Fallback to
761 * using a device with empty read-only filesystem.
762 */
763 if (mount(NULL, tst_test->mntpoint, "tmpfs", MS_RDONLY, NULL)) {
764 tst_res(TINFO | TERRNO, "Can't mount tmpfs read-only"
765 " at %s, setting up a device instead\n",
766 tst_test->mntpoint);
767 tst_test->mount_device = 1;
768 tst_test->needs_device = 1;
769 tst_test->format_device = 1;
770 tst_test->mnt_flags = MS_RDONLY;
771 } else {
772 mntpoint_mounted = 1;
773 }
774 }
775
776 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100777 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100778
779 if (!tdev.dev)
780 tst_brk(TCONF, "Failed to acquire device");
781
782 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100783
784 if (tst_test->dev_fs_type)
785 tdev.fs_type = tst_test->dev_fs_type;
786 else
787 tdev.fs_type = tst_dev_fs_type();
788
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200789 if (!tst_test->all_filesystems)
790 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100791 }
792
793 if (tst_test->resource_files)
794 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200795}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100796
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200797static void do_test_setup(void)
798{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100799 main_pid = getpid();
800
801 if (tst_test->setup)
802 tst_test->setup();
803
804 if (main_pid != getpid())
805 tst_brk(TBROK, "Runaway child in setup()!");
806}
807
808static void do_cleanup(void)
809{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700810 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100811 tst_umount(tst_test->mntpoint);
812
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100813 if (tst_test->needs_device && tdev.dev)
814 tst_release_device(tdev.dev);
815
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000816 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100817 /* avoid munmap() on wrong pointer in tst_rmdir() */
818 tst_futexes = NULL;
819 tst_rmdir();
820 }
Jan Stancek332540e2016-06-08 16:48:22 +0200821
822 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100823}
824
825static void run_tests(void)
826{
827 unsigned int i;
828 struct results saved_results;
829
830 if (!tst_test->test) {
831 saved_results = *results;
832 tst_test->test_all();
833
834 if (getpid() != main_pid) {
835 exit(0);
836 }
837
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300838 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100839
840 if (results_equal(&saved_results, results))
841 tst_brk(TBROK, "Test haven't reported results!");
842 return;
843 }
844
845 for (i = 0; i < tst_test->tcnt; i++) {
846 saved_results = *results;
847 tst_test->test(i);
848
849 if (getpid() != main_pid) {
850 exit(0);
851 }
852
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300853 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100854
855 if (results_equal(&saved_results, results))
856 tst_brk(TBROK, "Test %i haven't reported results!", i);
857 }
858}
859
860static unsigned long long get_time_ms(void)
861{
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100862 struct timespec ts;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100863
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100864 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
865 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100866
Cyril Hrubis2f7c8e92018-01-22 15:19:31 +0100867 return tst_timespec_to_ms(ts);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100868}
869
Jan Stancekb95b1992017-10-10 15:47:58 +0200870static void add_paths(void)
871{
872 char *old_path = getenv("PATH");
873 const char *start_dir;
874 char *new_path;
875
876 start_dir = tst_get_startwd();
877
878 if (old_path)
879 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
880 else
881 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
882
883 SAFE_SETENV("PATH", new_path, 1);
884 free(new_path);
885}
886
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200887static void heartbeat(void)
888{
889 kill(getppid(), SIGUSR1);
890}
891
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200892static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100893{
894 unsigned int i = 0;
895 unsigned long long stop_time = 0;
896 int cont = 1;
897
Jan Stancekb95b1992017-10-10 15:47:58 +0200898 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200899 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100900
901 if (duration > 0)
902 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
903
904 for (;;) {
905 cont = 0;
906
907 if (i < (unsigned int)iterations) {
908 i++;
909 cont = 1;
910 }
911
912 if (stop_time && get_time_ms() < stop_time)
913 cont = 1;
914
915 if (!cont)
916 break;
917
918 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200919 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100920 }
921
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200922 do_test_cleanup();
923 exit(0);
924}
925
926static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200927
Cyril Hrubis79163172017-05-18 10:57:07 +0200928
929static volatile sig_atomic_t sigkill_retries;
930
931#define WRITE_MSG(msg) do { \
932 if (write(2, msg, sizeof(msg) - 1)) { \
933 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
934 } \
935} while (0)
936
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200937static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
938{
Cyril Hrubis79163172017-05-18 10:57:07 +0200939 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200940 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200941 alarm(5);
942
943 if (++sigkill_retries > 10) {
944 WRITE_MSG("Cannot kill test processes!\n");
945 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
946 WRITE_MSG("Exitting uncleanly...\n");
947 _exit(TFAIL);
948 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200949}
950
951static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
952{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200953 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200954 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200955}
956
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200957static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
958{
959 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200960 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200961 kill(-test_pid, SIGKILL);
962 }
963}
964
Li Wang94823cf2017-07-18 16:23:00 +0800965void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200966{
967 char *mul = getenv("LTP_TIMEOUT_MUL");
968
Li Wang94823cf2017-07-18 16:23:00 +0800969 if (timeout == -1) {
970 tst_res(TINFO, "Timeout per run is disabled");
971 return;
972 }
973
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200974 results->timeout = timeout;
975
976 if (mul) {
977 float m = atof(mul);
978
979 if (m < 1)
980 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
981
982 results->timeout = results->timeout * m + 0.5;
983 }
984
985 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
986 results->timeout/3600, (results->timeout%3600)/60,
987 results->timeout % 60);
988
989 if (getpid() == lib_pid)
990 alarm(results->timeout);
991 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200992 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200993}
994
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200995static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200996{
997 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200998
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200999 if (tst_test->timeout)
1000 tst_set_timeout(tst_test->timeout);
1001 else
1002 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001003
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001004 SAFE_SIGNAL(SIGINT, sigint_handler);
1005
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001006 test_pid = fork();
1007 if (test_pid < 0)
1008 tst_brk(TBROK | TERRNO, "fork()");
1009
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001010 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001011 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1012 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1013 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001014 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001015 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001016 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001017
1018 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001019 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001020 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001021
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001022 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001023 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001024
1025 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1026 tst_res(TINFO, "If you are running on slow machine, "
1027 "try exporting LTP_TIMEOUT_MUL > 1");
1028 tst_brk(TBROK, "Test killed! (timeout?)");
1029 }
1030
1031 if (WIFSIGNALED(status))
1032 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1033
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001034 return 0;
1035}
1036
1037static int run_tcases_per_fs(void)
1038{
1039 int ret = 0;
1040 unsigned int i;
1041 const char *const *filesystems = tst_get_supported_fs_types();
1042
1043 if (!filesystems[0])
1044 tst_brk(TCONF, "There are no supported filesystems");
1045
1046 for (i = 0; filesystems[i]; i++) {
1047 tdev.fs_type = filesystems[i];
1048
1049 prepare_device();
1050
1051 ret = fork_testrun();
1052
1053 if (mntpoint_mounted) {
1054 tst_umount(tst_test->mntpoint);
1055 mntpoint_mounted = 0;
1056 }
1057
1058 if (ret == TCONF) {
1059 update_results(ret);
1060 continue;
1061 }
1062
1063 if (ret == 0)
1064 continue;
1065
1066 do_exit(ret);
1067 }
1068
1069 return ret;
1070}
1071
1072void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1073{
1074 int ret;
1075
1076 lib_pid = getpid();
1077 tst_test = self;
1078
1079 do_setup(argc, argv);
1080
1081 TCID = tid;
1082
1083 SAFE_SIGNAL(SIGALRM, alarm_handler);
1084 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1085
1086 if (tst_test->all_filesystems)
1087 ret = run_tcases_per_fs();
1088 else
1089 ret = fork_testrun();
1090
1091 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001092}
Michael Moese1ab33ce2018-03-09 15:16:08 +01001093
1094
1095void tst_flush(void)
1096{
1097 int rval;
1098
1099 rval = fflush(stderr);
1100 if (rval != 0)
1101 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1102
1103 rval = fflush(stderr);
1104 if (rval != 0)
1105 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1106
1107}