blob: e7b9cddf1149e2f3005d612122b2d4c9249d9dd6 [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>
27#include <sys/time.h>
28
29#define TST_NO_DEFAULT_MAIN
30#include "tst_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010031#include "tst_device.h"
32#include "lapi/futex.h"
Steve Mucklec20831d2017-09-20 13:23:06 -070033#include "lapi/syscalls.h"
Petr Vorel3a0ef862017-03-01 15:31:08 +010034#include "tst_ansi_color.h"
Jan Stancekb95b1992017-10-10 15:47:58 +020035#include "tst_safe_stdio.h"
Cyril Hrubisc4596542017-06-20 15:42:18 +020036#include "tst_timer_test.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010037
38#include "old_resource.h"
39#include "old_device.h"
40#include "old_tmpdir.h"
41
42struct tst_test *tst_test;
43
Li Wangb5d620a2017-11-01 13:15:23 +080044static const char *tid;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010045static int iterations = 1;
46static float duration = -1;
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020047static pid_t main_pid, lib_pid;
Sandeep Patilc9a7def2017-09-19 12:49:58 -070048static int mntpoint_mounted;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010049
50struct results {
Jan Stancekc54ca052016-04-13 12:31:13 +020051 int passed;
52 int skipped;
53 int failed;
54 int warnings;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +020055 unsigned int timeout;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010056};
57
58static struct results *results;
59
60static int ipc_fd;
61
62extern void *tst_futexes;
63extern unsigned int tst_max_futexes;
64
65#define IPC_ENV_VAR "LTP_IPC_PATH"
66
67static char ipc_path[1024];
68const char *tst_ipc_path = ipc_path;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010069
70static char shm_path[1024];
71
Jan Stancek332540e2016-06-08 16:48:22 +020072static void do_cleanup(void);
Cyril Hrubisfa495172016-06-08 16:06:35 +020073static void do_exit(int ret) __attribute__ ((noreturn));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +020074
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010075static void setup_ipc(void)
76{
77 size_t size = getpagesize();
78
Steven Jackson9f41dcf2016-12-21 20:09:01 +000079 if (access("/dev/shm", F_OK) == 0) {
80 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080081 tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000082 } else {
83 char *tmpdir;
84
85 if (!tst_tmpdir_created())
86 tst_tmpdir();
87
88 tmpdir = tst_get_tmpdir();
89 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
Li Wangb5d620a2017-11-01 13:15:23 +080090 tmpdir, tid, getpid());
Steven Jackson9f41dcf2016-12-21 20:09:01 +000091 free(tmpdir);
92 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010093
94 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
95 if (ipc_fd < 0)
96 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
Jan Stancek2113eef2017-10-04 12:25:42 +020097 SAFE_CHMOD(shm_path, 0666);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010098
99 SAFE_FTRUNCATE(ipc_fd, size);
100
101 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
102
103 /* Checkpoints needs to be accessible from processes started by exec() */
Jan Stancek03fc5372017-10-10 13:36:59 +0200104 if (tst_test->needs_checkpoints) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100105 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200106 putenv(ipc_path);
107 } else {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100108 SAFE_UNLINK(shm_path);
Jan Stancek03fc5372017-10-10 13:36:59 +0200109 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100110
111 SAFE_CLOSE(ipc_fd);
112
113 if (tst_test->needs_checkpoints) {
114 tst_futexes = (char*)results + sizeof(struct results);
115 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
116 }
117}
118
119static void cleanup_ipc(void)
120{
121 size_t size = getpagesize();
122
123 if (ipc_fd > 0 && close(ipc_fd))
124 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
125
Cyril Hrubis9726b902017-07-27 11:28:50 +0200126 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100127 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
128
Cyril Hrubis9726b902017-07-27 11:28:50 +0200129 if (results) {
130 msync((void*)results, size, MS_SYNC);
131 munmap((void*)results, size);
132 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100133}
134
135void tst_reinit(void)
136{
Jan Stancek03fc5372017-10-10 13:36:59 +0200137 const char *path = getenv(IPC_ENV_VAR);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100138 size_t size = getpagesize();
139 int fd;
140 void *ptr;
141
142 if (!path)
Jan Stancek03fc5372017-10-10 13:36:59 +0200143 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100144
145 if (access(path, F_OK))
146 tst_brk(TBROK, "File %s does not exist!", path);
147
148 fd = SAFE_OPEN(path, O_RDWR);
149
150 ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
151 tst_futexes = (char*)ptr + sizeof(struct results);
152 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
153
154 SAFE_CLOSE(fd);
155}
156
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100157static void update_results(int ttype)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100158{
Cyril Hrubisd97debf2017-02-13 12:37:39 +0100159 if (!results)
160 return;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100161
162 switch (ttype) {
163 case TCONF:
164 tst_atomic_inc(&results->skipped);
165 break;
166 case TPASS:
167 tst_atomic_inc(&results->passed);
168 break;
169 case TWARN:
170 tst_atomic_inc(&results->warnings);
171 break;
172 case TFAIL:
173 tst_atomic_inc(&results->failed);
174 break;
175 }
176}
177
178static void print_result(const char *file, const int lineno, int ttype,
179 const char *fmt, va_list va)
180{
181 char buf[1024];
182 char *str = buf;
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100183 int ret, size = sizeof(buf), ssize;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100184 const char *str_errno = NULL;
185 const char *res;
186
187 switch (TTYPE_RESULT(ttype)) {
188 case TPASS:
189 res = "PASS";
190 break;
191 case TFAIL:
192 res = "FAIL";
193 break;
194 case TBROK:
195 res = "BROK";
196 break;
197 case TCONF:
198 res = "CONF";
199 break;
200 case TWARN:
201 res = "WARN";
202 break;
203 case TINFO:
204 res = "INFO";
205 break;
206 default:
207 tst_brk(TBROK, "Invalid ttype value %i", ttype);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100208 abort();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100209 }
210
211 if (ttype & TERRNO)
212 str_errno = tst_strerrno(errno);
213
214 if (ttype & TTERRNO)
215 str_errno = tst_strerrno(TEST_ERRNO);
216
Petr Vorela7f61332017-01-24 20:47:30 +0100217 ret = snprintf(str, size, "%s:%i: ", file, lineno);
218 str += ret;
219 size -= ret;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100220
Cyril Hrubis047c7272017-02-13 16:23:36 +0100221 if (tst_color_enabled(STDERR_FILENO))
Petr Vorela7f61332017-01-24 20:47:30 +0100222 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
223 res, ANSI_COLOR_RESET);
224 else
225 ret = snprintf(str, size, "%s: ", res);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100226 str += ret;
227 size -= ret;
228
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100229 ssize = size - 2;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100230 ret = vsnprintf(str, size, fmt, va);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100231 str += MIN(ret, ssize);
232 size -= MIN(ret, ssize);
233 if (ret >= ssize) {
234 tst_res_(file, lineno, TWARN,
235 "Next message is too long and truncated:");
236 } else if (str_errno) {
237 ssize = size - 2;
Petr Vorela7f61332017-01-24 20:47:30 +0100238 ret = snprintf(str, size, ": %s", str_errno);
Veronika Kabatovacecbd0c2017-11-07 17:10:42 +0100239 str += MIN(ret, ssize);
240 size -= MIN(ret, ssize);
241 if (ret >= ssize)
242 tst_res_(file, lineno, TWARN,
243 "Next message is too long and truncated:");
Petr Vorela7f61332017-01-24 20:47:30 +0100244 }
245
246 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100247
248 fputs(buf, stderr);
249}
250
251void tst_vres_(const char *file, const int lineno, int ttype,
252 const char *fmt, va_list va)
253{
254 print_result(file, lineno, ttype, fmt, va);
255
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100256 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100257}
258
259void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100260 const char *fmt, va_list va);
261
262static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
263 const char *fmt, va_list va) = tst_vbrk_;
264
265static void tst_cvres(const char *file, const int lineno, int ttype,
266 const char *fmt, va_list va)
267{
268 if (TTYPE_RESULT(ttype) == TBROK) {
269 ttype &= ~TTYPE_MASK;
270 ttype |= TWARN;
271 }
272
273 print_result(file, lineno, ttype, fmt, va);
274 update_results(TTYPE_RESULT(ttype));
275}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100276
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200277static void do_test_cleanup(void)
278{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100279 tst_brk_handler = tst_cvres;
280
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200281 if (tst_test->cleanup)
282 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100283
284 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200285}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100286
287void tst_vbrk_(const char *file, const int lineno, int ttype,
288 const char *fmt, va_list va)
289{
290 print_result(file, lineno, ttype, fmt, va);
291
Steve Mucklec20831d2017-09-20 13:23:06 -0700292 /*
293 * The getpid implementation in some C library versions may cause cloned
294 * test threads to show the same pid as their parent when CLONE_VM is
295 * specified but CLONE_THREAD is not. Use direct syscall to avoid
296 * cleanup running in the child.
297 */
298 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200299 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100300
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200301 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200302 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200303
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100304 exit(TTYPE_RESULT(ttype));
305}
306
307void tst_res_(const char *file, const int lineno, int ttype,
308 const char *fmt, ...)
309{
310 va_list va;
311
312 va_start(va, fmt);
313 tst_vres_(file, lineno, ttype, fmt, va);
314 va_end(va);
315}
316
317void tst_brk_(const char *file, const int lineno, int ttype,
318 const char *fmt, ...)
319{
320 va_list va;
321
322 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100323 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100324 va_end(va);
325}
326
327static void check_child_status(pid_t pid, int status)
328{
329 int ret;
330
331 if (WIFSIGNALED(status)) {
332 tst_brk(TBROK, "Child (%i) killed by signal %s",
333 pid, tst_strsig(WTERMSIG(status)));
334 }
335
336 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100337 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100338
339 ret = WEXITSTATUS(status);
340 switch (ret) {
341 case TPASS:
342 break;
343 case TBROK:
344 case TCONF:
345 tst_brk(ret, "Reported by child (%i)", pid);
346 default:
347 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
348 }
349}
350
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300351void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100352{
353 int status;
354 pid_t pid;
355
356 for (;;) {
357 pid = wait(&status);
358
359 if (pid > 0) {
360 check_child_status(pid, status);
361 continue;
362 }
363
364 if (errno == ECHILD)
365 break;
366
367 if (errno == EINTR)
368 continue;
369
370 tst_brk(TBROK | TERRNO, "wait() failed");
371 }
372}
373
374
375pid_t safe_fork(const char *filename, unsigned int lineno)
376{
377 pid_t pid;
378
379 if (!tst_test->forks_child)
380 tst_brk(TBROK, "test.forks_child must be set!");
381
382 fflush(stdout);
383
384 pid = fork();
385 if (pid < 0)
386 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
387
388 return pid;
389}
390
391static struct option {
392 char *optstr;
393 char *help;
394} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200395 {"h", "-h Prints this help"},
396 {"i:", "-i n Execute test n times"},
397 {"I:", "-I x Execute test for n seconds"},
398 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100399};
400
401static void print_help(void)
402{
403 unsigned int i;
404
405 for (i = 0; i < ARRAY_SIZE(options); i++)
406 fprintf(stderr, "%s\n", options[i].help);
407
408 if (!tst_test->options)
409 return;
410
411 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200412 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100413}
414
415static void check_option_collision(void)
416{
417 unsigned int i, j;
418 struct tst_option *toptions = tst_test->options;
419
420 if (!toptions)
421 return;
422
423 for (i = 0; toptions[i].optstr; i++) {
424 for (j = 0; j < ARRAY_SIZE(options); j++) {
425 if (toptions[i].optstr[0] == options[j].optstr[0]) {
426 tst_brk(TBROK, "Option collision '%s'",
427 options[j].help);
428 }
429 }
430 }
431}
432
433static unsigned int count_options(void)
434{
435 unsigned int i;
436
437 if (!tst_test->options)
438 return 0;
439
440 for (i = 0; tst_test->options[i].optstr; i++);
441
442 return i;
443}
444
445static void parse_topt(unsigned int topts_len, int opt, char *optarg)
446{
447 unsigned int i;
448 struct tst_option *toptions = tst_test->options;
449
450 for (i = 0; i < topts_len; i++) {
451 if (toptions[i].optstr[0] == opt)
452 break;
453 }
454
455 if (i >= topts_len)
456 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
457
Jan Stancekc07d36c2016-08-01 11:44:55 +0200458 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100459}
460
461/* see self_exec.c */
462#ifdef UCLINUX
463extern char *child_args;
464#endif
465
466static void parse_opts(int argc, char *argv[])
467{
468 unsigned int i, topts_len = count_options();
469 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
470 int opt;
471
472 check_option_collision();
473
474 optstr[0] = 0;
475
476 for (i = 0; i < ARRAY_SIZE(options); i++)
477 strcat(optstr, options[i].optstr);
478
479 for (i = 0; i < topts_len; i++)
480 strcat(optstr, tst_test->options[i].optstr);
481
482 while ((opt = getopt(argc, argv, optstr)) > 0) {
483 switch (opt) {
484 case '?':
485 print_help();
486 tst_brk(TBROK, "Invalid option");
487 case 'h':
488 print_help();
489 exit(0);
490 case 'i':
491 iterations = atoi(optarg);
492 break;
493 case 'I':
494 duration = atof(optarg);
495 break;
496 case 'C':
497#ifdef UCLINUX
498 child_args = optarg;
499#endif
500 break;
501 default:
502 parse_topt(topts_len, opt, optarg);
503 }
504 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200505
506 if (optind < argc)
507 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100508}
509
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200510int tst_parse_int(const char *str, int *val, int min, int max)
511{
512 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300513
514 if (!str)
515 return 0;
516
517 int ret = tst_parse_long(str, &rval, min, max);
518
519 if (ret)
520 return ret;
521
522 *val = (int)rval;
523 return 0;
524}
525
526int tst_parse_long(const char *str, long *val, long min, long max)
527{
528 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200529 char *end;
530
531 if (!str)
532 return 0;
533
534 errno = 0;
535 rval = strtol(str, &end, 10);
536
537 if (str == end || *end != '\0')
538 return EINVAL;
539
540 if (errno)
541 return errno;
542
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300543 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200544 return ERANGE;
545
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300546 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200547 return 0;
548}
549
550int tst_parse_float(const char *str, float *val, float min, float max)
551{
552 double rval;
553 char *end;
554
555 if (!str)
556 return 0;
557
558 errno = 0;
559 rval = strtod(str, &end);
560
561 if (str == end || *end != '\0')
562 return EINVAL;
563
564 if (errno)
565 return errno;
566
567 if (rval > (double)max || rval < (double)min)
568 return ERANGE;
569
570 *val = (float)rval;
571 return 0;
572}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100573
Cyril Hrubisfa495172016-06-08 16:06:35 +0200574static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100575{
Xiao Yang11dfc322016-06-16 15:52:04 +0800576 if (results) {
577 printf("\nSummary:\n");
578 printf("passed %d\n", results->passed);
579 printf("failed %d\n", results->failed);
580 printf("skipped %d\n", results->skipped);
581 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100582
Xiao Yang11dfc322016-06-16 15:52:04 +0800583 if (results->failed)
584 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100585
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200586 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800587 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100588
Xiao Yang11dfc322016-06-16 15:52:04 +0800589 if (results->warnings)
590 ret |= TWARN;
591 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100592
Jan Stancek332540e2016-06-08 16:48:22 +0200593 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100594
595 exit(ret);
596}
597
598void check_kver(void)
599{
600 int v1, v2, v3;
601
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100602 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
603 tst_res(TWARN,
604 "Invalid kernel version %s, expected %%d.%%d.%%d",
605 tst_test->min_kver);
606 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100607
608 if (tst_kvercmp(v1, v2, v3) < 0) {
609 tst_brk(TCONF, "The test requires kernel %s or newer",
610 tst_test->min_kver);
611 }
612}
613
614static int results_equal(struct results *a, struct results *b)
615{
616 if (a->passed != b->passed)
617 return 0;
618
619 if (a->failed != b->failed)
620 return 0;
621
622 if (a->skipped != b->skipped)
623 return 0;
624
625 return 1;
626}
627
628static int needs_tmpdir(void)
629{
630 return tst_test->needs_tmpdir ||
631 tst_test->needs_device ||
632 tst_test->resource_files ||
633 tst_test->needs_checkpoints;
634}
635
636static void copy_resources(void)
637{
638 unsigned int i;
639
640 for (i = 0; tst_test->resource_files[i]; i++)
641 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
642}
643
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800644static const char *get_tid(char *argv[])
645{
646 char *p;
647
648 if (!argv[0] || !argv[0][0]) {
649 tst_res(TINFO, "argv[0] is empty!");
650 return "ltp_empty_argv";
651 }
652
653 p = strrchr(argv[0], '/');
654 if (p)
655 return p+1;
656
657 return argv[0];
658}
659
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100660static struct tst_device tdev;
661struct tst_device *tst_device;
662
Cyril Hrubisc4596542017-06-20 15:42:18 +0200663static void assert_test_fn(void)
664{
665 int cnt = 0;
666
667 if (tst_test->test)
668 cnt++;
669
670 if (tst_test->test_all)
671 cnt++;
672
673 if (tst_test->sample)
674 cnt++;
675
676 if (!cnt)
677 tst_brk(TBROK, "No test function speficied");
678
679 if (cnt != 1)
680 tst_brk(TBROK, "You can define only one test function");
681
682 if (tst_test->test && !tst_test->tcnt)
683 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
684
685 if (!tst_test->test && tst_test->tcnt)
686 tst_brk(TBROK, "You can define tcnt only for test()");
687}
688
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200689static void prepare_device(void)
690{
691 if (tst_test->format_device) {
692 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
693 tst_test->dev_extra_opt);
694 }
695
696 if (tst_test->mount_device) {
697 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
698 tst_test->mnt_flags, tst_test->mnt_data);
699 mntpoint_mounted = 1;
700 }
701}
702
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100703static void do_setup(int argc, char *argv[])
704{
705 if (!tst_test)
706 tst_brk(TBROK, "No tests to run");
707
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200708 if (tst_test->tconf_msg)
709 tst_brk(TCONF, "%s", tst_test->tconf_msg);
710
Cyril Hrubisc4596542017-06-20 15:42:18 +0200711 assert_test_fn();
712
Li Wangb5d620a2017-11-01 13:15:23 +0800713 tid = get_tid(argv);
714
Cyril Hrubisc4596542017-06-20 15:42:18 +0200715 if (tst_test->sample)
716 tst_test = tst_timer_test_setup(tst_test);
717
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200718 parse_opts(argc, argv);
719
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100720 if (tst_test->needs_root && geteuid() != 0)
721 tst_brk(TCONF, "Test needs to be run as root");
722
723 if (tst_test->min_kver)
724 check_kver();
725
Cyril Hrubis874326d2017-02-14 15:59:40 +0100726 if (tst_test->format_device)
727 tst_test->needs_device = 1;
728
729 if (tst_test->mount_device) {
730 tst_test->needs_device = 1;
731 tst_test->format_device = 1;
732 }
733
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200734 if (tst_test->all_filesystems)
735 tst_test->needs_device = 1;
736
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100737 setup_ipc();
738
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000739 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100740 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100741
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700742 if (tst_test->mntpoint)
743 SAFE_MKDIR(tst_test->mntpoint, 0777);
744
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200745 if ((tst_test->needs_rofs || tst_test->mount_device ||
746 tst_test->all_filesystems) && !tst_test->mntpoint) {
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700747 tst_brk(TBROK, "tst_test->mntpoint must be set!");
748 }
749
750 if (tst_test->needs_rofs) {
751 /* If we failed to mount read-only tmpfs. Fallback to
752 * using a device with empty read-only filesystem.
753 */
754 if (mount(NULL, tst_test->mntpoint, "tmpfs", MS_RDONLY, NULL)) {
755 tst_res(TINFO | TERRNO, "Can't mount tmpfs read-only"
756 " at %s, setting up a device instead\n",
757 tst_test->mntpoint);
758 tst_test->mount_device = 1;
759 tst_test->needs_device = 1;
760 tst_test->format_device = 1;
761 tst_test->mnt_flags = MS_RDONLY;
762 } else {
763 mntpoint_mounted = 1;
764 }
765 }
766
767 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100768 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100769
770 if (!tdev.dev)
771 tst_brk(TCONF, "Failed to acquire device");
772
773 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100774
775 if (tst_test->dev_fs_type)
776 tdev.fs_type = tst_test->dev_fs_type;
777 else
778 tdev.fs_type = tst_dev_fs_type();
779
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200780 if (!tst_test->all_filesystems)
781 prepare_device();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100782 }
783
784 if (tst_test->resource_files)
785 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200786}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100787
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200788static void do_test_setup(void)
789{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100790 main_pid = getpid();
791
792 if (tst_test->setup)
793 tst_test->setup();
794
795 if (main_pid != getpid())
796 tst_brk(TBROK, "Runaway child in setup()!");
797}
798
799static void do_cleanup(void)
800{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700801 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100802 tst_umount(tst_test->mntpoint);
803
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100804 if (tst_test->needs_device && tdev.dev)
805 tst_release_device(tdev.dev);
806
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000807 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100808 /* avoid munmap() on wrong pointer in tst_rmdir() */
809 tst_futexes = NULL;
810 tst_rmdir();
811 }
Jan Stancek332540e2016-06-08 16:48:22 +0200812
813 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100814}
815
816static void run_tests(void)
817{
818 unsigned int i;
819 struct results saved_results;
820
821 if (!tst_test->test) {
822 saved_results = *results;
823 tst_test->test_all();
824
825 if (getpid() != main_pid) {
826 exit(0);
827 }
828
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300829 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100830
831 if (results_equal(&saved_results, results))
832 tst_brk(TBROK, "Test haven't reported results!");
833 return;
834 }
835
836 for (i = 0; i < tst_test->tcnt; i++) {
837 saved_results = *results;
838 tst_test->test(i);
839
840 if (getpid() != main_pid) {
841 exit(0);
842 }
843
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300844 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100845
846 if (results_equal(&saved_results, results))
847 tst_brk(TBROK, "Test %i haven't reported results!", i);
848 }
849}
850
851static unsigned long long get_time_ms(void)
852{
853 struct timeval tv;
854
855 gettimeofday(&tv, NULL);
856
857 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
858}
859
Jan Stancekb95b1992017-10-10 15:47:58 +0200860static void add_paths(void)
861{
862 char *old_path = getenv("PATH");
863 const char *start_dir;
864 char *new_path;
865
866 start_dir = tst_get_startwd();
867
868 if (old_path)
869 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
870 else
871 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
872
873 SAFE_SETENV("PATH", new_path, 1);
874 free(new_path);
875}
876
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200877static void heartbeat(void)
878{
879 kill(getppid(), SIGUSR1);
880}
881
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200882static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100883{
884 unsigned int i = 0;
885 unsigned long long stop_time = 0;
886 int cont = 1;
887
Jan Stancekb95b1992017-10-10 15:47:58 +0200888 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200889 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100890
891 if (duration > 0)
892 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
893
894 for (;;) {
895 cont = 0;
896
897 if (i < (unsigned int)iterations) {
898 i++;
899 cont = 1;
900 }
901
902 if (stop_time && get_time_ms() < stop_time)
903 cont = 1;
904
905 if (!cont)
906 break;
907
908 run_tests();
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200909 heartbeat();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100910 }
911
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200912 do_test_cleanup();
913 exit(0);
914}
915
916static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200917
Cyril Hrubis79163172017-05-18 10:57:07 +0200918
919static volatile sig_atomic_t sigkill_retries;
920
921#define WRITE_MSG(msg) do { \
922 if (write(2, msg, sizeof(msg) - 1)) { \
923 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
924 } \
925} while (0)
926
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200927static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
928{
Cyril Hrubis79163172017-05-18 10:57:07 +0200929 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200930 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200931 alarm(5);
932
933 if (++sigkill_retries > 10) {
934 WRITE_MSG("Cannot kill test processes!\n");
935 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
936 WRITE_MSG("Exitting uncleanly...\n");
937 _exit(TFAIL);
938 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200939}
940
941static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
942{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200943 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200944 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200945}
946
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200947static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
948{
949 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200950 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200951 kill(-test_pid, SIGKILL);
952 }
953}
954
Li Wang94823cf2017-07-18 16:23:00 +0800955void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200956{
957 char *mul = getenv("LTP_TIMEOUT_MUL");
958
Li Wang94823cf2017-07-18 16:23:00 +0800959 if (timeout == -1) {
960 tst_res(TINFO, "Timeout per run is disabled");
961 return;
962 }
963
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200964 results->timeout = timeout;
965
966 if (mul) {
967 float m = atof(mul);
968
969 if (m < 1)
970 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
971
972 results->timeout = results->timeout * m + 0.5;
973 }
974
975 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
976 results->timeout/3600, (results->timeout%3600)/60,
977 results->timeout % 60);
978
979 if (getpid() == lib_pid)
980 alarm(results->timeout);
981 else
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200982 heartbeat();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200983}
984
Cyril Hrubis35b8a132017-09-01 10:45:26 +0200985static int fork_testrun(void)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200986{
987 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200988
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200989 if (tst_test->timeout)
990 tst_set_timeout(tst_test->timeout);
991 else
992 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200993
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200994 SAFE_SIGNAL(SIGINT, sigint_handler);
995
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200996 test_pid = fork();
997 if (test_pid < 0)
998 tst_brk(TBROK | TERRNO, "fork()");
999
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001000 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001001 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1002 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1003 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001004 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001005 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +02001006 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001007
1008 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001009 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001010 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001011
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001012 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001013 return WEXITSTATUS(status);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001014
1015 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1016 tst_res(TINFO, "If you are running on slow machine, "
1017 "try exporting LTP_TIMEOUT_MUL > 1");
1018 tst_brk(TBROK, "Test killed! (timeout?)");
1019 }
1020
1021 if (WIFSIGNALED(status))
1022 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1023
Cyril Hrubis35b8a132017-09-01 10:45:26 +02001024 return 0;
1025}
1026
1027static int run_tcases_per_fs(void)
1028{
1029 int ret = 0;
1030 unsigned int i;
1031 const char *const *filesystems = tst_get_supported_fs_types();
1032
1033 if (!filesystems[0])
1034 tst_brk(TCONF, "There are no supported filesystems");
1035
1036 for (i = 0; filesystems[i]; i++) {
1037 tdev.fs_type = filesystems[i];
1038
1039 prepare_device();
1040
1041 ret = fork_testrun();
1042
1043 if (mntpoint_mounted) {
1044 tst_umount(tst_test->mntpoint);
1045 mntpoint_mounted = 0;
1046 }
1047
1048 if (ret == TCONF) {
1049 update_results(ret);
1050 continue;
1051 }
1052
1053 if (ret == 0)
1054 continue;
1055
1056 do_exit(ret);
1057 }
1058
1059 return ret;
1060}
1061
1062void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1063{
1064 int ret;
1065
1066 lib_pid = getpid();
1067 tst_test = self;
1068
1069 do_setup(argc, argv);
1070
1071 TCID = tid;
1072
1073 SAFE_SIGNAL(SIGALRM, alarm_handler);
1074 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1075
1076 if (tst_test->all_filesystems)
1077 ret = run_tcases_per_fs();
1078 else
1079 ret = fork_testrun();
1080
1081 do_exit(ret);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001082}