1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
4 * Copyright (c) Linux Test Project, 2016-2021
5 */
6
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <sys/mount.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <math.h>
18
19 #define TST_NO_DEFAULT_MAIN
20 #include "tst_test.h"
21 #include "tst_device.h"
22 #include "lapi/abisize.h"
23 #include "lapi/futex.h"
24 #include "lapi/syscalls.h"
25 #include "tst_ansi_color.h"
26 #include "tst_safe_stdio.h"
27 #include "tst_timer_test.h"
28 #include "tst_clocks.h"
29 #include "tst_timer.h"
30 #include "tst_wallclock.h"
31 #include "tst_sys_conf.h"
32 #include "tst_kconfig.h"
33 #include "tst_private.h"
34 #include "old_resource.h"
35 #include "old_device.h"
36 #include "old_tmpdir.h"
37
38 /*
39 * Hack to get TCID defined in newlib tests
40 */
41 const char *TCID __attribute__((weak));
42
43 /* update also docparse/testinfo.pl */
44 #define LINUX_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id="
45 #define LINUX_STABLE_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id="
46 #define GLIBC_GIT_URL "https://sourceware.org/git/?p=glibc.git;a=commit;h="
47 #define CVE_DB_URL "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-"
48
49 #define DEFAULT_TIMEOUT 30
50
51 struct tst_test *tst_test;
52
53 static const char *tid;
54 static int iterations = 1;
55 static float duration = -1;
56 static float timeout_mul = -1;
57 static pid_t main_pid, lib_pid;
58 static int mntpoint_mounted;
59 static int ovl_mounted;
60 static struct timespec tst_start_time; /* valid only for test pid */
61
62 struct results {
63 int passed;
64 int skipped;
65 int failed;
66 int warnings;
67 int broken;
68 unsigned int timeout;
69 int max_runtime;
70 };
71
72 static struct results *results;
73
74 static int ipc_fd;
75
76 extern void *tst_futexes;
77 extern unsigned int tst_max_futexes;
78
79 static char ipc_path[1064];
80 const char *tst_ipc_path = ipc_path;
81
82 static char shm_path[1024];
83
84 int TST_ERR;
85 int TST_PASS;
86 long TST_RET;
87
88 static void do_cleanup(void);
89 static void do_exit(int ret) __attribute__ ((noreturn));
90
setup_ipc(void)91 static void setup_ipc(void)
92 {
93 size_t size = getpagesize();
94
95 if (access("/dev/shm", F_OK) == 0) {
96 snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
97 tid, getpid());
98 } else {
99 char *tmpdir;
100
101 if (!tst_tmpdir_created())
102 tst_tmpdir();
103
104 tmpdir = tst_get_tmpdir();
105 snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
106 tmpdir, tid, getpid());
107 free(tmpdir);
108 }
109
110 ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
111 if (ipc_fd < 0)
112 tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
113 SAFE_CHMOD(shm_path, 0666);
114
115 SAFE_FTRUNCATE(ipc_fd, size);
116
117 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
118
119 /* Checkpoints needs to be accessible from processes started by exec() */
120 if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
121 sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
122 putenv(ipc_path);
123 } else {
124 SAFE_UNLINK(shm_path);
125 }
126
127 SAFE_CLOSE(ipc_fd);
128
129 if (tst_test->needs_checkpoints) {
130 tst_futexes = (char*)results + sizeof(struct results);
131 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
132 }
133 }
134
cleanup_ipc(void)135 static void cleanup_ipc(void)
136 {
137 size_t size = getpagesize();
138
139 if (ipc_fd > 0 && close(ipc_fd))
140 tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
141
142 if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
143 tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
144
145 if (results) {
146 msync((void*)results, size, MS_SYNC);
147 munmap((void*)results, size);
148 results = NULL;
149 }
150 }
151
tst_reinit(void)152 void tst_reinit(void)
153 {
154 const char *path = getenv(IPC_ENV_VAR);
155 size_t size = getpagesize();
156 int fd;
157
158 if (!path)
159 tst_brk(TBROK, IPC_ENV_VAR" is not defined");
160
161 if (access(path, F_OK))
162 tst_brk(TBROK, "File %s does not exist!", path);
163
164 fd = SAFE_OPEN(path, O_RDWR);
165
166 results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
167 tst_futexes = (char*)results + sizeof(struct results);
168 tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
169
170 SAFE_CLOSE(fd);
171 }
172
update_results(int ttype)173 static void update_results(int ttype)
174 {
175 if (!results)
176 return;
177
178 switch (ttype) {
179 case TCONF:
180 tst_atomic_inc(&results->skipped);
181 break;
182 case TPASS:
183 tst_atomic_inc(&results->passed);
184 break;
185 case TWARN:
186 tst_atomic_inc(&results->warnings);
187 break;
188 case TFAIL:
189 tst_atomic_inc(&results->failed);
190 break;
191 case TBROK:
192 tst_atomic_inc(&results->broken);
193 break;
194 }
195 }
196
print_result(const char * file,const int lineno,int ttype,const char * fmt,va_list va)197 static void print_result(const char *file, const int lineno, int ttype,
198 const char *fmt, va_list va)
199 {
200 char buf[1024];
201 char *str = buf;
202 int ret, size = sizeof(buf), ssize, int_errno, buflen;
203 const char *str_errno = NULL;
204 const char *res;
205
206 switch (TTYPE_RESULT(ttype)) {
207 case TPASS:
208 res = "TPASS";
209 break;
210 case TFAIL:
211 res = "TFAIL";
212 break;
213 case TBROK:
214 res = "TBROK";
215 break;
216 case TCONF:
217 res = "TCONF";
218 break;
219 case TWARN:
220 res = "TWARN";
221 break;
222 case TINFO:
223 res = "TINFO";
224 break;
225 default:
226 tst_brk(TBROK, "Invalid ttype value %i", ttype);
227 abort();
228 }
229
230 if (ttype & TERRNO) {
231 str_errno = tst_strerrno(errno);
232 int_errno = errno;
233 }
234
235 if (ttype & TTERRNO) {
236 str_errno = tst_strerrno(TST_ERR);
237 int_errno = TST_ERR;
238 }
239
240 if (ttype & TRERRNO) {
241 int_errno = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
242 str_errno = tst_strerrno(int_errno);
243 }
244
245 ret = snprintf(str, size, "%s:%i: ", file, lineno);
246 str += ret;
247 size -= ret;
248
249 if (tst_color_enabled(STDERR_FILENO))
250 ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
251 res, ANSI_COLOR_RESET);
252 else
253 ret = snprintf(str, size, "%s: ", res);
254 str += ret;
255 size -= ret;
256
257 ssize = size - 2;
258 ret = vsnprintf(str, size, fmt, va);
259 str += MIN(ret, ssize);
260 size -= MIN(ret, ssize);
261 if (ret >= ssize) {
262 tst_res_(file, lineno, TWARN,
263 "Next message is too long and truncated:");
264 } else if (str_errno) {
265 ssize = size - 2;
266 ret = snprintf(str, size, ": %s (%d)", str_errno, int_errno);
267 str += MIN(ret, ssize);
268 size -= MIN(ret, ssize);
269 if (ret >= ssize)
270 tst_res_(file, lineno, TWARN,
271 "Next message is too long and truncated:");
272 }
273
274 snprintf(str, size, "\n");
275
276 /* we might be called from signal handler, so use write() */
277 buflen = str - buf + 1;
278 str = buf;
279 while (buflen) {
280 ret = write(STDERR_FILENO, str, buflen);
281 if (ret <= 0)
282 break;
283
284 str += ret;
285 buflen -= ret;
286 }
287 }
288
tst_vres_(const char * file,const int lineno,int ttype,const char * fmt,va_list va)289 void tst_vres_(const char *file, const int lineno, int ttype,
290 const char *fmt, va_list va)
291 {
292 print_result(file, lineno, ttype, fmt, va);
293
294 update_results(TTYPE_RESULT(ttype));
295 }
296
297 void tst_vbrk_(const char *file, const int lineno, int ttype,
298 const char *fmt, va_list va);
299
300 static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
301 const char *fmt, va_list va) = tst_vbrk_;
302
tst_cvres(const char * file,const int lineno,int ttype,const char * fmt,va_list va)303 static void tst_cvres(const char *file, const int lineno, int ttype,
304 const char *fmt, va_list va)
305 {
306 if (TTYPE_RESULT(ttype) == TBROK) {
307 ttype &= ~TTYPE_MASK;
308 ttype |= TWARN;
309 }
310
311 print_result(file, lineno, ttype, fmt, va);
312 update_results(TTYPE_RESULT(ttype));
313 }
314
do_test_cleanup(void)315 static void do_test_cleanup(void)
316 {
317 tst_brk_handler = tst_cvres;
318
319 if (tst_test->cleanup)
320 tst_test->cleanup();
321
322 tst_free_all();
323
324 tst_brk_handler = tst_vbrk_;
325 }
326
tst_vbrk_(const char * file,const int lineno,int ttype,const char * fmt,va_list va)327 void tst_vbrk_(const char *file, const int lineno, int ttype,
328 const char *fmt, va_list va)
329 {
330 print_result(file, lineno, ttype, fmt, va);
331 update_results(TTYPE_RESULT(ttype));
332
333 /*
334 * The getpid implementation in some C library versions may cause cloned
335 * test threads to show the same pid as their parent when CLONE_VM is
336 * specified but CLONE_THREAD is not. Use direct syscall to avoid
337 * cleanup running in the child.
338 */
339 if (syscall(SYS_getpid) == main_pid)
340 do_test_cleanup();
341
342 if (getpid() == lib_pid)
343 do_exit(TTYPE_RESULT(ttype));
344
345 exit(TTYPE_RESULT(ttype));
346 }
347
tst_res_(const char * file,const int lineno,int ttype,const char * fmt,...)348 void tst_res_(const char *file, const int lineno, int ttype,
349 const char *fmt, ...)
350 {
351 va_list va;
352
353 va_start(va, fmt);
354 tst_vres_(file, lineno, ttype, fmt, va);
355 va_end(va);
356 }
357
tst_brk_(const char * file,const int lineno,int ttype,const char * fmt,...)358 void tst_brk_(const char *file, const int lineno, int ttype,
359 const char *fmt, ...)
360 {
361 va_list va;
362
363 va_start(va, fmt);
364 tst_brk_handler(file, lineno, ttype, fmt, va);
365 va_end(va);
366 }
367
tst_printf(const char * const fmt,...)368 void tst_printf(const char *const fmt, ...)
369 {
370 va_list va;
371
372 va_start(va, fmt);
373 vdprintf(STDERR_FILENO, fmt, va);
374 va_end(va);
375 }
376
check_child_status(pid_t pid,int status)377 static void check_child_status(pid_t pid, int status)
378 {
379 int ret;
380
381 if (WIFSIGNALED(status)) {
382 tst_brk(TBROK, "Child (%i) killed by signal %s",
383 pid, tst_strsig(WTERMSIG(status)));
384 }
385
386 if (!(WIFEXITED(status)))
387 tst_brk(TBROK, "Child (%i) exited abnormally", pid);
388
389 ret = WEXITSTATUS(status);
390 switch (ret) {
391 case TPASS:
392 case TBROK:
393 case TCONF:
394 break;
395 default:
396 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
397 }
398 }
399
tst_reap_children(void)400 void tst_reap_children(void)
401 {
402 int status;
403 pid_t pid;
404
405 for (;;) {
406 pid = wait(&status);
407
408 if (pid > 0) {
409 check_child_status(pid, status);
410 continue;
411 }
412
413 if (errno == ECHILD)
414 break;
415
416 if (errno == EINTR)
417 continue;
418
419 tst_brk(TBROK | TERRNO, "wait() failed");
420 }
421 }
422
423
safe_fork(const char * filename,unsigned int lineno)424 pid_t safe_fork(const char *filename, unsigned int lineno)
425 {
426 pid_t pid;
427
428 if (!tst_test->forks_child)
429 tst_brk(TBROK, "test.forks_child must be set!");
430
431 tst_flush();
432
433 pid = fork();
434 if (pid < 0)
435 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
436
437 if (!pid)
438 atexit(tst_free_all);
439
440 return pid;
441 }
442
443 /* too fast creating namespaces => retrying */
444 #define TST_CHECK_ENOSPC(x) ((x) >= 0 || !(errno == ENOSPC))
445
safe_clone(const char * file,const int lineno,const struct tst_clone_args * args)446 pid_t safe_clone(const char *file, const int lineno,
447 const struct tst_clone_args *args)
448 {
449 pid_t pid;
450
451 if (!tst_test->forks_child)
452 tst_brk(TBROK, "test.forks_child must be set!");
453
454 pid = TST_RETRY_FUNC(tst_clone(args), TST_CHECK_ENOSPC);
455
456 switch (pid) {
457 case -1:
458 tst_brk_(file, lineno, TBROK | TERRNO, "clone3 failed");
459 break;
460 case -2:
461 tst_brk_(file, lineno, TBROK | TERRNO, "clone failed");
462 return -1;
463 }
464
465 if (!pid)
466 atexit(tst_free_all);
467
468 return pid;
469 }
470
parse_mul(float * mul,const char * env_name,float min,float max)471 static void parse_mul(float *mul, const char *env_name, float min, float max)
472 {
473 char *str_mul;
474 int ret;
475
476 if (*mul > 0)
477 return;
478
479 str_mul = getenv(env_name);
480
481 if (!str_mul) {
482 *mul = 1;
483 return;
484 }
485
486 ret = tst_parse_float(str_mul, mul, min, max);
487 if (ret) {
488 tst_brk(TBROK, "Failed to parse %s: %s",
489 env_name, tst_strerrno(ret));
490 }
491 }
492
multiply_runtime(int max_runtime)493 static int multiply_runtime(int max_runtime)
494 {
495 static float runtime_mul = -1;
496
497 if (max_runtime <= 0)
498 return max_runtime;
499
500 parse_mul(&runtime_mul, "LTP_RUNTIME_MUL", 0.0099, 100);
501
502 return max_runtime * runtime_mul;
503 }
504
505 static struct option {
506 char *optstr;
507 char *help;
508 } options[] = {
509 {"h", "-h Prints this help"},
510 {"i:", "-i n Execute test n times"},
511 {"I:", "-I x Execute test for n seconds"},
512 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
513 };
514
print_help(void)515 static void print_help(void)
516 {
517 unsigned int i;
518 int timeout, runtime;
519
520 /* see doc/user-guide.txt, which lists also shell API variables */
521 fprintf(stderr, "Environment Variables\n");
522 fprintf(stderr, "---------------------\n");
523 fprintf(stderr, "KCONFIG_PATH Specify kernel config file\n");
524 fprintf(stderr, "KCONFIG_SKIP_CHECK Skip kernel config check if variable set (not set by default)\n");
525 fprintf(stderr, "LTPROOT Prefix for installed LTP (default: /opt/ltp)\n");
526 fprintf(stderr, "LTP_COLORIZE_OUTPUT Force colorized output behaviour (y/1 always, n/0: never)\n");
527 fprintf(stderr, "LTP_DEV Path to the block device to be used (for .needs_device)\n");
528 fprintf(stderr, "LTP_DEV_FS_TYPE Filesystem used for testing (default: %s)\n", DEFAULT_FS_TYPE);
529 fprintf(stderr, "LTP_SINGLE_FS_TYPE Testing only - specifies filesystem instead all supported (for .all_filesystems)\n");
530 fprintf(stderr, "LTP_TIMEOUT_MUL Timeout multiplier (must be a number >=1)\n");
531 fprintf(stderr, "LTP_RUNTIME_MUL Runtime multiplier (must be a number >=1)\n");
532 fprintf(stderr, "LTP_VIRT_OVERRIDE Overrides virtual machine detection (values: \"\"|kvm|microsoft|xen|zvm)\n");
533 fprintf(stderr, "TMPDIR Base directory for template directory (for .needs_tmpdir, default: %s)\n", TEMPDIR);
534 fprintf(stderr, "\n");
535
536 fprintf(stderr, "Timeout and runtime\n");
537 fprintf(stderr, "-------------------\n");
538
539 if (tst_test->max_runtime) {
540 runtime = multiply_runtime(tst_test->max_runtime);
541
542 if (runtime == TST_UNLIMITED_RUNTIME) {
543 fprintf(stderr, "Test iteration runtime is not limited\n");
544 } else {
545 fprintf(stderr, "Test iteration runtime cap %ih %im %is\n",
546 runtime/3600, (runtime%3600)/60, runtime % 60);
547 }
548 }
549
550 timeout = tst_multiply_timeout(DEFAULT_TIMEOUT);
551
552 fprintf(stderr, "Test timeout (not including runtime) %ih %im %is\n",
553 timeout/3600, (timeout%3600)/60, timeout % 60);
554
555 fprintf(stderr, "\n");
556
557 fprintf(stderr, "Options\n");
558 fprintf(stderr, "-------\n");
559
560 for (i = 0; i < ARRAY_SIZE(options); i++)
561 fprintf(stderr, "%s\n", options[i].help);
562
563 if (!tst_test->options)
564 return;
565
566 for (i = 0; tst_test->options[i].optstr; i++) {
567 fprintf(stderr, "-%c\t %s\n",
568 tst_test->options[i].optstr[0],
569 tst_test->options[i].help);
570 }
571 }
572
print_test_tags(void)573 static void print_test_tags(void)
574 {
575 unsigned int i;
576 const struct tst_tag *tags = tst_test->tags;
577
578 if (!tags)
579 return;
580
581 fprintf(stderr, "\nTags\n");
582 fprintf(stderr, "----\n");
583
584 for (i = 0; tags[i].name; i++) {
585 if (!strcmp(tags[i].name, "CVE"))
586 fprintf(stderr, CVE_DB_URL "%s\n", tags[i].value);
587 else if (!strcmp(tags[i].name, "linux-git"))
588 fprintf(stderr, LINUX_GIT_URL "%s\n", tags[i].value);
589 else if (!strcmp(tags[i].name, "linux-stable-git"))
590 fprintf(stderr, LINUX_STABLE_GIT_URL "%s\n", tags[i].value);
591 else if (!strcmp(tags[i].name, "glibc-git"))
592 fprintf(stderr, GLIBC_GIT_URL "%s\n", tags[i].value);
593 else
594 fprintf(stderr, "%s: %s\n", tags[i].name, tags[i].value);
595 }
596
597 fprintf(stderr, "\n");
598 }
599
check_option_collision(void)600 static void check_option_collision(void)
601 {
602 unsigned int i, j;
603 struct tst_option *toptions = tst_test->options;
604
605 if (!toptions)
606 return;
607
608 for (i = 0; toptions[i].optstr; i++) {
609 for (j = 0; j < ARRAY_SIZE(options); j++) {
610 if (toptions[i].optstr[0] == options[j].optstr[0]) {
611 tst_brk(TBROK, "Option collision '%s'",
612 options[j].help);
613 }
614 }
615 }
616 }
617
count_options(void)618 static unsigned int count_options(void)
619 {
620 unsigned int i;
621
622 if (!tst_test->options)
623 return 0;
624
625 for (i = 0; tst_test->options[i].optstr; i++);
626
627 return i;
628 }
629
parse_topt(unsigned int topts_len,int opt,char * optarg)630 static void parse_topt(unsigned int topts_len, int opt, char *optarg)
631 {
632 unsigned int i;
633 struct tst_option *toptions = tst_test->options;
634
635 for (i = 0; i < topts_len; i++) {
636 if (toptions[i].optstr[0] == opt)
637 break;
638 }
639
640 if (i >= topts_len)
641 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
642
643 if (*toptions[i].arg)
644 tst_res(TWARN, "Option -%c passed multiple times", opt);
645
646 *(toptions[i].arg) = optarg ? optarg : "";
647 }
648
649 /* see self_exec.c */
650 #ifdef UCLINUX
651 extern char *child_args;
652 #endif
653
parse_opts(int argc,char * argv[])654 static void parse_opts(int argc, char *argv[])
655 {
656 unsigned int i, topts_len = count_options();
657 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
658 int opt;
659
660 check_option_collision();
661
662 optstr[0] = 0;
663
664 for (i = 0; i < ARRAY_SIZE(options); i++)
665 strcat(optstr, options[i].optstr);
666
667 for (i = 0; i < topts_len; i++)
668 strcat(optstr, tst_test->options[i].optstr);
669
670 while ((opt = getopt(argc, argv, optstr)) > 0) {
671 switch (opt) {
672 case '?':
673 print_help();
674 tst_brk(TBROK, "Invalid option");
675 break;
676 case 'h':
677 print_help();
678 print_test_tags();
679 exit(0);
680 case 'i':
681 iterations = SAFE_STRTOL(optarg, 1, INT_MAX);
682 break;
683 case 'I':
684 if (tst_test->max_runtime > 0)
685 tst_test->max_runtime = SAFE_STRTOL(optarg, 1, INT_MAX);
686 else
687 duration = SAFE_STRTOF(optarg, 0.1, HUGE_VALF);
688 break;
689 case 'C':
690 #ifdef UCLINUX
691 child_args = optarg;
692 #endif
693 break;
694 default:
695 parse_topt(topts_len, opt, optarg);
696 }
697 }
698
699 if (optind < argc)
700 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
701 }
702
tst_parse_int(const char * str,int * val,int min,int max)703 int tst_parse_int(const char *str, int *val, int min, int max)
704 {
705 long rval;
706
707 if (!str)
708 return 0;
709
710 int ret = tst_parse_long(str, &rval, min, max);
711
712 if (ret)
713 return ret;
714
715 *val = (int)rval;
716 return 0;
717 }
718
tst_parse_long(const char * str,long * val,long min,long max)719 int tst_parse_long(const char *str, long *val, long min, long max)
720 {
721 long rval;
722 char *end;
723
724 if (!str)
725 return 0;
726
727 errno = 0;
728 rval = strtol(str, &end, 10);
729
730 if (str == end || *end != '\0')
731 return EINVAL;
732
733 if (errno)
734 return errno;
735
736 if (rval > max || rval < min)
737 return ERANGE;
738
739 *val = rval;
740 return 0;
741 }
742
tst_parse_float(const char * str,float * val,float min,float max)743 int tst_parse_float(const char *str, float *val, float min, float max)
744 {
745 double rval;
746 char *end;
747
748 if (!str)
749 return 0;
750
751 errno = 0;
752 rval = strtod(str, &end);
753
754 if (str == end || *end != '\0')
755 return EINVAL;
756
757 if (errno)
758 return errno;
759
760 if (rval > (double)max || rval < (double)min)
761 return ERANGE;
762
763 *val = (float)rval;
764 return 0;
765 }
766
tst_parse_filesize(const char * str,long long * val,long long min,long long max)767 int tst_parse_filesize(const char *str, long long *val, long long min, long long max)
768 {
769 long long rval;
770 char *end;
771
772 if (!str)
773 return 0;
774
775 errno = 0;
776 rval = strtoll(str, &end, 0);
777
778 if (str == end || (end[0] && end[1]))
779 return EINVAL;
780
781 if (errno)
782 return errno;
783
784 switch (*end) {
785 case 'g':
786 case 'G':
787 rval *= (1024 * 1024 * 1024);
788 break;
789 case 'm':
790 case 'M':
791 rval *= (1024 * 1024);
792 break;
793 case 'k':
794 case 'K':
795 rval *= 1024;
796 break;
797 default:
798 break;
799 }
800
801 if (rval > max || rval < min)
802 return ERANGE;
803
804 *val = rval;
805 return 0;
806 }
807
print_colored(const char * str)808 static void print_colored(const char *str)
809 {
810 if (tst_color_enabled(STDOUT_FILENO))
811 fprintf(stderr, "%s%s%s", ANSI_COLOR_YELLOW, str, ANSI_COLOR_RESET);
812 else
813 fprintf(stderr, "%s", str);
814 }
815
print_failure_hint(const char * tag,const char * hint,const char * url)816 static void print_failure_hint(const char *tag, const char *hint,
817 const char *url)
818 {
819 const struct tst_tag *tags = tst_test->tags;
820
821 if (!tags)
822 return;
823
824 unsigned int i;
825 int hint_printed = 0;
826
827 for (i = 0; tags[i].name; i++) {
828 if (!strcmp(tags[i].name, tag)) {
829 if (!hint_printed) {
830 hint_printed = 1;
831 fprintf(stderr, "\n");
832 print_colored("HINT: ");
833 fprintf(stderr, "You _MAY_ be %s:\n\n", hint);
834 }
835
836 if (url)
837 fprintf(stderr, "%s%s\n", url, tags[i].value);
838 else
839 fprintf(stderr, "%s\n", tags[i].value);
840 }
841 }
842 }
843
844 /* update also docparse/testinfo.pl */
print_failure_hints(void)845 static void print_failure_hints(void)
846 {
847 print_failure_hint("linux-git", "missing kernel fixes", LINUX_GIT_URL);
848 print_failure_hint("linux-stable-git", "missing stable kernel fixes",
849 LINUX_STABLE_GIT_URL);
850 print_failure_hint("glibc-git", "missing glibc fixes", GLIBC_GIT_URL);
851 print_failure_hint("CVE", "vulnerable to CVE(s)", CVE_DB_URL);
852 print_failure_hint("known-fail", "hit by known kernel failures", NULL);
853 }
854
do_exit(int ret)855 static void do_exit(int ret)
856 {
857 if (results) {
858 if (results->passed && ret == TCONF)
859 ret = 0;
860
861 if (results->failed) {
862 ret |= TFAIL;
863 print_failure_hints();
864 }
865
866 if (results->skipped && !results->passed)
867 ret |= TCONF;
868
869 if (results->warnings)
870 ret |= TWARN;
871
872 if (results->broken)
873 ret |= TBROK;
874
875 fprintf(stderr, "\nSummary:\n");
876 fprintf(stderr, "passed %d\n", results->passed);
877 fprintf(stderr, "failed %d\n", results->failed);
878 fprintf(stderr, "broken %d\n", results->broken);
879 fprintf(stderr, "skipped %d\n", results->skipped);
880 fprintf(stderr, "warnings %d\n", results->warnings);
881 }
882
883 do_cleanup();
884
885 exit(ret);
886 }
887
check_kver(void)888 void check_kver(void)
889 {
890 int v1, v2, v3;
891
892 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
893 tst_res(TWARN,
894 "Invalid kernel version %s, expected %%d.%%d.%%d",
895 tst_test->min_kver);
896 }
897
898 if (tst_kvercmp(v1, v2, v3) < 0) {
899 tst_brk(TCONF, "The test requires kernel %s or newer",
900 tst_test->min_kver);
901 }
902 }
903
results_equal(struct results * a,struct results * b)904 static int results_equal(struct results *a, struct results *b)
905 {
906 if (a->passed != b->passed)
907 return 0;
908
909 if (a->failed != b->failed)
910 return 0;
911
912 if (a->skipped != b->skipped)
913 return 0;
914
915 if (a->broken != b->broken)
916 return 0;
917
918 return 1;
919 }
920
needs_tmpdir(void)921 static int needs_tmpdir(void)
922 {
923 return tst_test->needs_tmpdir ||
924 tst_test->needs_device ||
925 tst_test->mntpoint ||
926 tst_test->resource_files ||
927 tst_test->needs_checkpoints;
928 }
929
copy_resources(void)930 static void copy_resources(void)
931 {
932 unsigned int i;
933
934 for (i = 0; tst_test->resource_files[i]; i++)
935 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
936 }
937
get_tid(char * argv[])938 static const char *get_tid(char *argv[])
939 {
940 char *p;
941
942 if (!argv[0] || !argv[0][0]) {
943 tst_res(TINFO, "argv[0] is empty!");
944 return "ltp_empty_argv";
945 }
946
947 p = strrchr(argv[0], '/');
948 if (p)
949 return p+1;
950
951 return argv[0];
952 }
953
954 static struct tst_device tdev;
955 struct tst_device *tst_device;
956
assert_test_fn(void)957 static void assert_test_fn(void)
958 {
959 int cnt = 0;
960
961 if (tst_test->test)
962 cnt++;
963
964 if (tst_test->test_all)
965 cnt++;
966
967 if (tst_test->sample)
968 cnt++;
969
970 if (!cnt)
971 tst_brk(TBROK, "No test function specified");
972
973 if (cnt != 1)
974 tst_brk(TBROK, "You can define only one test function");
975
976 if (tst_test->test && !tst_test->tcnt)
977 tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
978
979 if (!tst_test->test && tst_test->tcnt)
980 tst_brk(TBROK, "You can define tcnt only for test()");
981 }
982
prepare_and_mount_ro_fs(const char * dev,const char * mntpoint,const char * fs_type)983 static int prepare_and_mount_ro_fs(const char *dev,
984 const char *mntpoint,
985 const char *fs_type)
986 {
987 char buf[PATH_MAX];
988
989 if (mount(dev, mntpoint, fs_type, 0, NULL)) {
990 tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
991 dev, mntpoint, fs_type);
992 return 1;
993 }
994
995 mntpoint_mounted = 1;
996
997 snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
998 SAFE_MKDIR(buf, 0777);
999
1000 snprintf(buf, sizeof(buf), "%s/file", mntpoint);
1001 SAFE_FILE_PRINTF(buf, "file content");
1002 SAFE_CHMOD(buf, 0777);
1003
1004 SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
1005
1006 return 0;
1007 }
1008
prepare_and_mount_dev_fs(const char * mntpoint)1009 static void prepare_and_mount_dev_fs(const char *mntpoint)
1010 {
1011 const char *flags[] = {"nodev", NULL};
1012 int mounted_nodev;
1013
1014 mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
1015 if (mounted_nodev) {
1016 tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
1017 "mounting tmpfs without nodev on %s", mntpoint);
1018 SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
1019 mntpoint_mounted = 1;
1020 }
1021 }
1022
prepare_and_mount_hugetlb_fs(void)1023 static void prepare_and_mount_hugetlb_fs(void)
1024 {
1025 SAFE_MOUNT("none", tst_test->mntpoint, "hugetlbfs", 0, NULL);
1026 mntpoint_mounted = 1;
1027 }
1028
tst_creat_unlinked(const char * path,int flags)1029 int tst_creat_unlinked(const char *path, int flags)
1030 {
1031 char template[PATH_MAX];
1032 int len, c, range;
1033 int fd;
1034 int start[3] = {'0', 'a', 'A'};
1035
1036 snprintf(template, PATH_MAX, "%s/ltp_%.3sXXXXXX",
1037 path, tid);
1038
1039 len = strlen(template) - 1;
1040 while (template[len] == 'X') {
1041 c = rand() % 3;
1042 range = start[c] == '0' ? 10 : 26;
1043 c = start[c] + (rand() % range);
1044 template[len--] = (char)c;
1045 }
1046
1047 flags |= O_CREAT|O_EXCL|O_RDWR;
1048 fd = SAFE_OPEN(template, flags);
1049 SAFE_UNLINK(template);
1050 return fd;
1051 }
1052
limit_tmpfs_mount_size(const char * mnt_data,char * buf,size_t buf_size,const char * fs_type)1053 static const char *limit_tmpfs_mount_size(const char *mnt_data,
1054 char *buf, size_t buf_size, const char *fs_type)
1055 {
1056 unsigned int tmpfs_size;
1057
1058 if (strcmp(fs_type, "tmpfs"))
1059 return mnt_data;
1060
1061 if (!tst_test->dev_min_size)
1062 tmpfs_size = 32;
1063 else
1064 tmpfs_size = tdev.size;
1065
1066 if ((tst_available_mem() / 1024) < (tmpfs_size * 2))
1067 tst_brk(TCONF, "No enough memory for tmpfs use");
1068
1069 if (mnt_data)
1070 snprintf(buf, buf_size, "%s,size=%uM", mnt_data, tmpfs_size);
1071 else
1072 snprintf(buf, buf_size, "size=%uM", tmpfs_size);
1073
1074 tst_res(TINFO, "Limiting tmpfs size to %uMB", tmpfs_size);
1075
1076 return buf;
1077 }
1078
get_device_name(const char * fs_type)1079 static const char *get_device_name(const char *fs_type)
1080 {
1081 if (!strcmp(fs_type, "tmpfs"))
1082 return "ltp-tmpfs";
1083 else
1084 return tdev.dev;
1085 }
1086
prepare_device(void)1087 static void prepare_device(void)
1088 {
1089 const char *mnt_data;
1090 char buf[1024];
1091
1092 if (tst_test->format_device) {
1093 SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
1094 tst_test->dev_extra_opts);
1095 }
1096
1097 if (tst_test->needs_rofs) {
1098 prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
1099 tdev.fs_type);
1100 return;
1101 }
1102
1103 if (tst_test->mount_device) {
1104 mnt_data = limit_tmpfs_mount_size(tst_test->mnt_data,
1105 buf, sizeof(buf), tdev.fs_type);
1106
1107 SAFE_MOUNT(get_device_name(tdev.fs_type), tst_test->mntpoint,
1108 tdev.fs_type, tst_test->mnt_flags, mnt_data);
1109 mntpoint_mounted = 1;
1110 }
1111 }
1112
do_cgroup_requires(void)1113 static void do_cgroup_requires(void)
1114 {
1115 const struct tst_cg_opts cg_opts = {
1116 .needs_ver = tst_test->needs_cgroup_ver,
1117 };
1118 const char *const *ctrl_names = tst_test->needs_cgroup_ctrls;
1119
1120 for (; *ctrl_names; ctrl_names++)
1121 tst_cg_require(*ctrl_names, &cg_opts);
1122
1123 tst_cg_init();
1124 }
1125
do_setup(int argc,char * argv[])1126 static void do_setup(int argc, char *argv[])
1127 {
1128 if (!tst_test)
1129 tst_brk(TBROK, "No tests to run");
1130
1131 if (tst_test->max_runtime < -1) {
1132 tst_brk(TBROK, "Invalid runtime value %i",
1133 results->max_runtime);
1134 }
1135
1136 if (tst_test->tconf_msg)
1137 tst_brk(TCONF, "%s", tst_test->tconf_msg);
1138
1139 assert_test_fn();
1140
1141 TCID = tid = get_tid(argv);
1142
1143 if (tst_test->sample)
1144 tst_test = tst_timer_test_setup(tst_test);
1145
1146 parse_opts(argc, argv);
1147
1148 if (tst_test->needs_kconfigs && tst_kconfig_check(tst_test->needs_kconfigs))
1149 tst_brk(TCONF, "Aborting due to unsuitable kernel config, see above!");
1150
1151 if (tst_test->needs_root && geteuid() != 0)
1152 tst_brk(TCONF, "Test needs to be run as root");
1153
1154 if (tst_test->min_kver)
1155 check_kver();
1156
1157 if (tst_test->supported_archs && !tst_is_on_arch(tst_test->supported_archs))
1158 tst_brk(TCONF, "This arch '%s' is not supported for test!", tst_arch.name);
1159
1160 if (tst_test->skip_in_lockdown && tst_lockdown_enabled())
1161 tst_brk(TCONF, "Kernel is locked down, skipping test");
1162
1163 if (tst_test->skip_in_compat && TST_ABI != tst_kernel_bits())
1164 tst_brk(TCONF, "Not supported in 32-bit compat mode");
1165
1166 if (tst_test->needs_cmds) {
1167 const char *cmd;
1168 int i;
1169
1170 for (i = 0; (cmd = tst_test->needs_cmds[i]); ++i)
1171 tst_check_cmd(cmd);
1172 }
1173
1174 if (tst_test->needs_drivers) {
1175 const char *name;
1176 int i;
1177
1178 for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
1179 if (tst_check_driver(name))
1180 tst_brk(TCONF, "%s driver not available", name);
1181 }
1182
1183 if (tst_test->mount_device)
1184 tst_test->format_device = 1;
1185
1186 if (tst_test->format_device)
1187 tst_test->needs_device = 1;
1188
1189 if (tst_test->all_filesystems)
1190 tst_test->needs_device = 1;
1191
1192 if (tst_test->min_cpus > (unsigned long)tst_ncpus())
1193 tst_brk(TCONF, "Test needs at least %lu CPUs online", tst_test->min_cpus);
1194
1195 if (tst_test->min_mem_avail > (unsigned long)(tst_available_mem() / 1024))
1196 tst_brk(TCONF, "Test needs at least %luMB MemAvailable", tst_test->min_mem_avail);
1197
1198 if (tst_test->hugepages.number)
1199 tst_reserve_hugepages(&tst_test->hugepages);
1200
1201 setup_ipc();
1202
1203 if (tst_test->bufs)
1204 tst_buffers_alloc(tst_test->bufs);
1205
1206 if (needs_tmpdir() && !tst_tmpdir_created())
1207 tst_tmpdir();
1208
1209 if (tst_test->save_restore) {
1210 const struct tst_path_val *pvl = tst_test->save_restore;
1211
1212 while (pvl->path) {
1213 tst_sys_conf_save(pvl);
1214 pvl++;
1215 }
1216 }
1217
1218 if (tst_test->mntpoint)
1219 SAFE_MKDIR(tst_test->mntpoint, 0777);
1220
1221 if ((tst_test->needs_devfs || tst_test->needs_rofs ||
1222 tst_test->mount_device || tst_test->all_filesystems ||
1223 tst_test->needs_hugetlbfs) &&
1224 !tst_test->mntpoint) {
1225 tst_brk(TBROK, "tst_test->mntpoint must be set!");
1226 }
1227
1228 if (!!tst_test->needs_rofs + !!tst_test->needs_devfs +
1229 !!tst_test->needs_device + !!tst_test->needs_hugetlbfs > 1) {
1230 tst_brk(TBROK,
1231 "Two or more of needs_{rofs, devfs, device, hugetlbfs} are set");
1232 }
1233
1234 if (tst_test->needs_devfs)
1235 prepare_and_mount_dev_fs(tst_test->mntpoint);
1236
1237 if (tst_test->needs_rofs) {
1238 /* If we failed to mount read-only tmpfs. Fallback to
1239 * using a device with read-only filesystem.
1240 */
1241 if (prepare_and_mount_ro_fs(NULL, tst_test->mntpoint, "tmpfs")) {
1242 tst_res(TINFO, "Can't mount tmpfs read-only, "
1243 "falling back to block device...");
1244 tst_test->needs_device = 1;
1245 tst_test->format_device = 1;
1246 }
1247 }
1248
1249 if (tst_test->needs_hugetlbfs)
1250 prepare_and_mount_hugetlb_fs();
1251
1252 if (tst_test->needs_device && !mntpoint_mounted) {
1253 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
1254
1255 if (!tdev.dev)
1256 tst_brk(TCONF, "Failed to acquire device");
1257
1258 tdev.size = tst_get_device_size(tdev.dev);
1259
1260 tst_device = &tdev;
1261
1262 if (tst_test->dev_fs_type)
1263 tdev.fs_type = tst_test->dev_fs_type;
1264 else
1265 tdev.fs_type = tst_dev_fs_type();
1266
1267 if (!tst_test->all_filesystems)
1268 prepare_device();
1269 }
1270
1271 if (tst_test->needs_overlay && !tst_test->mount_device) {
1272 tst_brk(TBROK, "tst_test->mount_device must be set");
1273 }
1274 if (tst_test->needs_overlay && !mntpoint_mounted) {
1275 tst_brk(TBROK, "tst_test->mntpoint must be mounted");
1276 }
1277 if (tst_test->needs_overlay && !ovl_mounted) {
1278 SAFE_MOUNT_OVERLAY();
1279 ovl_mounted = 1;
1280 }
1281
1282 if (tst_test->resource_files)
1283 copy_resources();
1284
1285 if (tst_test->restore_wallclock)
1286 tst_wallclock_save();
1287
1288 if (tst_test->taint_check)
1289 tst_taint_init(tst_test->taint_check);
1290
1291 if (tst_test->needs_cgroup_ctrls)
1292 do_cgroup_requires();
1293 else if (tst_test->needs_cgroup_ver)
1294 tst_brk(TBROK, "tst_test->needs_cgroup_ctrls must be set");
1295 }
1296
do_test_setup(void)1297 static void do_test_setup(void)
1298 {
1299 main_pid = getpid();
1300
1301 if (!tst_test->all_filesystems && tst_test->skip_filesystems) {
1302 long fs_type = tst_fs_type(".");
1303 const char *fs_name = tst_fs_type_name(fs_type);
1304
1305 if (tst_fs_in_skiplist(fs_name, tst_test->skip_filesystems)) {
1306 tst_brk(TCONF, "%s is not supported by the test",
1307 fs_name);
1308 }
1309
1310 tst_res(TINFO, "%s is supported by the test", fs_name);
1311 }
1312
1313 if (tst_test->caps)
1314 tst_cap_setup(tst_test->caps, TST_CAP_REQ);
1315
1316 if (tst_test->setup)
1317 tst_test->setup();
1318
1319 if (main_pid != getpid())
1320 tst_brk(TBROK, "Runaway child in setup()!");
1321
1322 if (tst_test->caps)
1323 tst_cap_setup(tst_test->caps, TST_CAP_DROP);
1324 }
1325
do_cleanup(void)1326 static void do_cleanup(void)
1327 {
1328 if (tst_test->needs_cgroup_ctrls)
1329 tst_cg_cleanup();
1330
1331 if (ovl_mounted)
1332 SAFE_UMOUNT(OVL_MNT);
1333
1334 if (mntpoint_mounted)
1335 tst_umount(tst_test->mntpoint);
1336
1337 if (tst_test->needs_device && tdev.dev)
1338 tst_release_device(tdev.dev);
1339
1340 if (tst_tmpdir_created()) {
1341 /* avoid munmap() on wrong pointer in tst_rmdir() */
1342 tst_futexes = NULL;
1343 tst_rmdir();
1344 }
1345
1346 tst_sys_conf_restore(0);
1347
1348 if (tst_test->restore_wallclock)
1349 tst_wallclock_restore();
1350
1351 cleanup_ipc();
1352 }
1353
heartbeat(void)1354 static void heartbeat(void)
1355 {
1356 if (tst_clock_gettime(CLOCK_MONOTONIC, &tst_start_time))
1357 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1358
1359 if (getppid() == 1) {
1360 tst_res(TFAIL, "Main test process might have exit!");
1361 /*
1362 * We need kill the task group immediately since the
1363 * main process has exit.
1364 */
1365 kill(0, SIGKILL);
1366 exit(TBROK);
1367 }
1368
1369 kill(getppid(), SIGUSR1);
1370 }
1371
run_tests(void)1372 static void run_tests(void)
1373 {
1374 unsigned int i;
1375 struct results saved_results;
1376
1377 if (!tst_test->test) {
1378 saved_results = *results;
1379 heartbeat();
1380 tst_test->test_all();
1381
1382 if (getpid() != main_pid) {
1383 exit(0);
1384 }
1385
1386 tst_reap_children();
1387
1388 if (results_equal(&saved_results, results))
1389 tst_brk(TBROK, "Test haven't reported results!");
1390 return;
1391 }
1392
1393 for (i = 0; i < tst_test->tcnt; i++) {
1394 saved_results = *results;
1395 heartbeat();
1396 tst_test->test(i);
1397
1398 if (getpid() != main_pid) {
1399 exit(0);
1400 }
1401
1402 tst_reap_children();
1403
1404 if (results_equal(&saved_results, results))
1405 tst_brk(TBROK, "Test %i haven't reported results!", i);
1406 }
1407 }
1408
get_time_ms(void)1409 static unsigned long long get_time_ms(void)
1410 {
1411 struct timespec ts;
1412
1413 if (tst_clock_gettime(CLOCK_MONOTONIC, &ts))
1414 tst_brk(TBROK | TERRNO, "tst_clock_gettime()");
1415
1416 return tst_timespec_to_ms(ts);
1417 }
1418
add_paths(void)1419 static void add_paths(void)
1420 {
1421 char *old_path = getenv("PATH");
1422 const char *start_dir;
1423 char *new_path;
1424
1425 start_dir = tst_get_startwd();
1426
1427 if (old_path)
1428 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
1429 else
1430 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
1431
1432 SAFE_SETENV("PATH", new_path, 1);
1433 free(new_path);
1434 }
1435
testrun(void)1436 static void testrun(void)
1437 {
1438 unsigned int i = 0;
1439 unsigned long long stop_time = 0;
1440 int cont = 1;
1441
1442 heartbeat();
1443 add_paths();
1444 do_test_setup();
1445
1446 if (duration > 0)
1447 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
1448
1449 for (;;) {
1450 cont = 0;
1451
1452 if (i < (unsigned int)iterations) {
1453 i++;
1454 cont = 1;
1455 }
1456
1457 if (stop_time && get_time_ms() < stop_time)
1458 cont = 1;
1459
1460 if (!cont)
1461 break;
1462
1463 run_tests();
1464 heartbeat();
1465 }
1466
1467 do_test_cleanup();
1468 exit(0);
1469 }
1470
1471 static pid_t test_pid;
1472
1473
1474 static volatile sig_atomic_t sigkill_retries;
1475
1476 #define WRITE_MSG(msg) do { \
1477 if (write(2, msg, sizeof(msg) - 1)) { \
1478 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
1479 } \
1480 } while (0)
1481
alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)1482 static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
1483 {
1484 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
1485 kill(-test_pid, SIGKILL);
1486 alarm(5);
1487
1488 if (++sigkill_retries > 10) {
1489 WRITE_MSG("Cannot kill test processes!\n");
1490 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
1491 WRITE_MSG("Exiting uncleanly...\n");
1492 _exit(TFAIL);
1493 }
1494 }
1495
heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)1496 static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
1497 {
1498 alarm(results->timeout);
1499 sigkill_retries = 0;
1500 }
1501
sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)1502 static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
1503 {
1504 if (test_pid > 0) {
1505 WRITE_MSG("Sending SIGKILL to test process...\n");
1506 kill(-test_pid, SIGKILL);
1507 }
1508 }
1509
tst_remaining_runtime(void)1510 unsigned int tst_remaining_runtime(void)
1511 {
1512 static struct timespec now;
1513 int elapsed;
1514
1515 if (results->max_runtime == TST_UNLIMITED_RUNTIME)
1516 return UINT_MAX;
1517
1518 if (results->max_runtime == 0)
1519 tst_brk(TBROK, "Runtime not set!");
1520
1521 if (tst_clock_gettime(CLOCK_MONOTONIC, &now))
1522 tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
1523
1524 elapsed = tst_timespec_diff_ms(now, tst_start_time) / 1000;
1525 if (results->max_runtime > elapsed)
1526 return results->max_runtime - elapsed;
1527
1528 return 0;
1529 }
1530
1531
tst_multiply_timeout(unsigned int timeout)1532 unsigned int tst_multiply_timeout(unsigned int timeout)
1533 {
1534 parse_mul(&timeout_mul, "LTP_TIMEOUT_MUL", 0.099, 10000);
1535
1536 if (timeout < 1)
1537 tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
1538
1539 return timeout * timeout_mul;
1540 }
1541
set_timeout(void)1542 static void set_timeout(void)
1543 {
1544 unsigned int timeout = DEFAULT_TIMEOUT;
1545
1546 if (results->max_runtime == TST_UNLIMITED_RUNTIME) {
1547 tst_res(TINFO, "Timeout per run is disabled");
1548 return;
1549 }
1550
1551 if (results->max_runtime < 0) {
1552 tst_brk(TBROK, "max_runtime must to be >= -1! (%d)",
1553 results->max_runtime);
1554 }
1555
1556 results->timeout = tst_multiply_timeout(timeout) + results->max_runtime;
1557
1558 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
1559 results->timeout/3600, (results->timeout%3600)/60,
1560 results->timeout % 60);
1561 }
1562
tst_set_max_runtime(int max_runtime)1563 void tst_set_max_runtime(int max_runtime)
1564 {
1565 results->max_runtime = multiply_runtime(max_runtime);
1566 tst_res(TINFO, "Updating max runtime to %uh %02um %02us",
1567 max_runtime/3600, (max_runtime%3600)/60, max_runtime % 60);
1568 set_timeout();
1569 heartbeat();
1570 }
1571
fork_testrun(void)1572 static int fork_testrun(void)
1573 {
1574 int status;
1575
1576 SAFE_SIGNAL(SIGINT, sigint_handler);
1577 SAFE_SIGNAL(SIGTERM, sigint_handler);
1578
1579 alarm(results->timeout);
1580
1581 test_pid = fork();
1582 if (test_pid < 0)
1583 tst_brk(TBROK | TERRNO, "fork()");
1584
1585 if (!test_pid) {
1586 tst_disable_oom_protection(0);
1587 SAFE_SIGNAL(SIGALRM, SIG_DFL);
1588 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
1589 SAFE_SIGNAL(SIGTERM, SIG_DFL);
1590 SAFE_SIGNAL(SIGINT, SIG_DFL);
1591 SAFE_SETPGID(0, 0);
1592 testrun();
1593 }
1594
1595 SAFE_WAITPID(test_pid, &status, 0);
1596 alarm(0);
1597 SAFE_SIGNAL(SIGTERM, SIG_DFL);
1598 SAFE_SIGNAL(SIGINT, SIG_DFL);
1599
1600 if (tst_test->taint_check && tst_taint_check()) {
1601 tst_res(TFAIL, "Kernel is now tainted.");
1602 return TFAIL;
1603 }
1604
1605 if (tst_test->forks_child && kill(-test_pid, SIGKILL) == 0)
1606 tst_res(TINFO, "Killed the leftover descendant processes");
1607
1608 if (WIFEXITED(status) && WEXITSTATUS(status))
1609 return WEXITSTATUS(status);
1610
1611 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1612 tst_res(TINFO, "If you are running on slow machine, "
1613 "try exporting LTP_TIMEOUT_MUL > 1");
1614 tst_brk(TBROK, "Test killed! (timeout?)");
1615 }
1616
1617 if (WIFSIGNALED(status))
1618 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1619
1620 return 0;
1621 }
1622
run_tcases_per_fs(void)1623 static int run_tcases_per_fs(void)
1624 {
1625 int ret = 0;
1626 unsigned int i;
1627 const char *const *filesystems = tst_get_supported_fs_types(tst_test->skip_filesystems);
1628
1629 if (!filesystems[0])
1630 tst_brk(TCONF, "There are no supported filesystems");
1631
1632 for (i = 0; filesystems[i]; i++) {
1633
1634 tst_res(TINFO, "=== Testing on %s ===", filesystems[i]);
1635 tdev.fs_type = filesystems[i];
1636
1637 prepare_device();
1638
1639 ret = fork_testrun();
1640
1641 if (mntpoint_mounted) {
1642 tst_umount(tst_test->mntpoint);
1643 mntpoint_mounted = 0;
1644 }
1645
1646 if (ret == TCONF)
1647 continue;
1648
1649 if (ret == 0)
1650 continue;
1651
1652 do_exit(ret);
1653 }
1654
1655 return ret;
1656 }
1657
1658 unsigned int tst_variant;
1659
tst_run_tcases(int argc,char * argv[],struct tst_test * self)1660 void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
1661 {
1662 int ret = 0;
1663 unsigned int test_variants = 1;
1664
1665 lib_pid = getpid();
1666 tst_test = self;
1667
1668 do_setup(argc, argv);
1669 tst_enable_oom_protection(lib_pid);
1670
1671 SAFE_SIGNAL(SIGALRM, alarm_handler);
1672 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
1673
1674 if (tst_test->max_runtime)
1675 results->max_runtime = multiply_runtime(tst_test->max_runtime);
1676
1677 set_timeout();
1678
1679 if (tst_test->test_variants)
1680 test_variants = tst_test->test_variants;
1681
1682 for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
1683 if (tst_test->all_filesystems)
1684 ret |= run_tcases_per_fs();
1685 else
1686 ret |= fork_testrun();
1687
1688 if (ret & ~(TCONF))
1689 goto exit;
1690 }
1691
1692 exit:
1693 do_exit(ret);
1694 }
1695
1696
tst_flush(void)1697 void tst_flush(void)
1698 {
1699 int rval;
1700
1701 rval = fflush(stderr);
1702 if (rval != 0)
1703 tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
1704
1705 rval = fflush(stdout);
1706 if (rval != 0)
1707 tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
1708
1709 }
1710