blob: 54e42b70abe0cebf25a1cc187ae315e9d89be77d [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;
183 int ret, size = sizeof(buf);
184 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
229 ret = vsnprintf(str, size, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100230 str += ret;
231 size -= ret;
232
Petr Vorela7f61332017-01-24 20:47:30 +0100233 if (str_errno) {
234 ret = snprintf(str, size, ": %s", str_errno);
235 str += ret;
236 size -= ret;
237 }
238
239 snprintf(str, size, "\n");
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100240
241 fputs(buf, stderr);
242}
243
244void tst_vres_(const char *file, const int lineno, int ttype,
245 const char *fmt, va_list va)
246{
247 print_result(file, lineno, ttype, fmt, va);
248
Cyril Hrubis160ffcc2017-02-14 10:21:08 +0100249 update_results(TTYPE_RESULT(ttype));
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100250}
251
252void tst_vbrk_(const char *file, const int lineno, int ttype,
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100253 const char *fmt, va_list va);
254
255static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
256 const char *fmt, va_list va) = tst_vbrk_;
257
258static void tst_cvres(const char *file, const int lineno, int ttype,
259 const char *fmt, va_list va)
260{
261 if (TTYPE_RESULT(ttype) == TBROK) {
262 ttype &= ~TTYPE_MASK;
263 ttype |= TWARN;
264 }
265
266 print_result(file, lineno, ttype, fmt, va);
267 update_results(TTYPE_RESULT(ttype));
268}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100269
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200270static void do_test_cleanup(void)
271{
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100272 tst_brk_handler = tst_cvres;
273
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200274 if (tst_test->cleanup)
275 tst_test->cleanup();
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100276
277 tst_brk_handler = tst_vbrk_;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200278}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100279
280void tst_vbrk_(const char *file, const int lineno, int ttype,
281 const char *fmt, va_list va)
282{
283 print_result(file, lineno, ttype, fmt, va);
284
Steve Mucklec20831d2017-09-20 13:23:06 -0700285 /*
286 * The getpid implementation in some C library versions may cause cloned
287 * test threads to show the same pid as their parent when CLONE_VM is
288 * specified but CLONE_THREAD is not. Use direct syscall to avoid
289 * cleanup running in the child.
290 */
291 if (syscall(SYS_getpid) == main_pid)
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200292 do_test_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100293
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200294 if (getpid() == lib_pid)
Cyril Hrubisfa495172016-06-08 16:06:35 +0200295 do_exit(TTYPE_RESULT(ttype));
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200296
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100297 exit(TTYPE_RESULT(ttype));
298}
299
300void tst_res_(const char *file, const int lineno, int ttype,
301 const char *fmt, ...)
302{
303 va_list va;
304
305 va_start(va, fmt);
306 tst_vres_(file, lineno, ttype, fmt, va);
307 va_end(va);
308}
309
310void tst_brk_(const char *file, const int lineno, int ttype,
311 const char *fmt, ...)
312{
313 va_list va;
314
315 va_start(va, fmt);
Cyril Hrubis6440c5d2017-02-09 15:41:24 +0100316 tst_brk_handler(file, lineno, ttype, fmt, va);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100317 va_end(va);
318}
319
320static void check_child_status(pid_t pid, int status)
321{
322 int ret;
323
324 if (WIFSIGNALED(status)) {
325 tst_brk(TBROK, "Child (%i) killed by signal %s",
326 pid, tst_strsig(WTERMSIG(status)));
327 }
328
329 if (!(WIFEXITED(status)))
Petr Vorelf8853482017-10-31 09:40:38 +0100330 tst_brk(TBROK, "Child (%i) exited abnormaly", pid);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100331
332 ret = WEXITSTATUS(status);
333 switch (ret) {
334 case TPASS:
335 break;
336 case TBROK:
337 case TCONF:
338 tst_brk(ret, "Reported by child (%i)", pid);
339 default:
340 tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
341 }
342}
343
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300344void tst_reap_children(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100345{
346 int status;
347 pid_t pid;
348
349 for (;;) {
350 pid = wait(&status);
351
352 if (pid > 0) {
353 check_child_status(pid, status);
354 continue;
355 }
356
357 if (errno == ECHILD)
358 break;
359
360 if (errno == EINTR)
361 continue;
362
363 tst_brk(TBROK | TERRNO, "wait() failed");
364 }
365}
366
367
368pid_t safe_fork(const char *filename, unsigned int lineno)
369{
370 pid_t pid;
371
372 if (!tst_test->forks_child)
373 tst_brk(TBROK, "test.forks_child must be set!");
374
375 fflush(stdout);
376
377 pid = fork();
378 if (pid < 0)
379 tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
380
381 return pid;
382}
383
384static struct option {
385 char *optstr;
386 char *help;
387} options[] = {
Cyril Hrubisb819c222016-08-03 14:36:07 +0200388 {"h", "-h Prints this help"},
389 {"i:", "-i n Execute test n times"},
390 {"I:", "-I x Execute test for n seconds"},
391 {"C:", "-C ARG Run child process with ARG arguments (used internally)"},
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100392};
393
394static void print_help(void)
395{
396 unsigned int i;
397
398 for (i = 0; i < ARRAY_SIZE(options); i++)
399 fprintf(stderr, "%s\n", options[i].help);
400
401 if (!tst_test->options)
402 return;
403
404 for (i = 0; tst_test->options[i].optstr; i++)
Cyril Hrubisb819c222016-08-03 14:36:07 +0200405 fprintf(stderr, "%s\n", tst_test->options[i].help);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100406}
407
408static void check_option_collision(void)
409{
410 unsigned int i, j;
411 struct tst_option *toptions = tst_test->options;
412
413 if (!toptions)
414 return;
415
416 for (i = 0; toptions[i].optstr; i++) {
417 for (j = 0; j < ARRAY_SIZE(options); j++) {
418 if (toptions[i].optstr[0] == options[j].optstr[0]) {
419 tst_brk(TBROK, "Option collision '%s'",
420 options[j].help);
421 }
422 }
423 }
424}
425
426static unsigned int count_options(void)
427{
428 unsigned int i;
429
430 if (!tst_test->options)
431 return 0;
432
433 for (i = 0; tst_test->options[i].optstr; i++);
434
435 return i;
436}
437
438static void parse_topt(unsigned int topts_len, int opt, char *optarg)
439{
440 unsigned int i;
441 struct tst_option *toptions = tst_test->options;
442
443 for (i = 0; i < topts_len; i++) {
444 if (toptions[i].optstr[0] == opt)
445 break;
446 }
447
448 if (i >= topts_len)
449 tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
450
Jan Stancekc07d36c2016-08-01 11:44:55 +0200451 *(toptions[i].arg) = optarg ? optarg : "";
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100452}
453
454/* see self_exec.c */
455#ifdef UCLINUX
456extern char *child_args;
457#endif
458
459static void parse_opts(int argc, char *argv[])
460{
461 unsigned int i, topts_len = count_options();
462 char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
463 int opt;
464
465 check_option_collision();
466
467 optstr[0] = 0;
468
469 for (i = 0; i < ARRAY_SIZE(options); i++)
470 strcat(optstr, options[i].optstr);
471
472 for (i = 0; i < topts_len; i++)
473 strcat(optstr, tst_test->options[i].optstr);
474
475 while ((opt = getopt(argc, argv, optstr)) > 0) {
476 switch (opt) {
477 case '?':
478 print_help();
479 tst_brk(TBROK, "Invalid option");
480 case 'h':
481 print_help();
482 exit(0);
483 case 'i':
484 iterations = atoi(optarg);
485 break;
486 case 'I':
487 duration = atof(optarg);
488 break;
489 case 'C':
490#ifdef UCLINUX
491 child_args = optarg;
492#endif
493 break;
494 default:
495 parse_topt(topts_len, opt, optarg);
496 }
497 }
Cyril Hrubis9dc07e02017-10-02 11:11:04 +0200498
499 if (optind < argc)
500 tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100501}
502
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200503int tst_parse_int(const char *str, int *val, int min, int max)
504{
505 long rval;
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300506
507 if (!str)
508 return 0;
509
510 int ret = tst_parse_long(str, &rval, min, max);
511
512 if (ret)
513 return ret;
514
515 *val = (int)rval;
516 return 0;
517}
518
519int tst_parse_long(const char *str, long *val, long min, long max)
520{
521 long rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200522 char *end;
523
524 if (!str)
525 return 0;
526
527 errno = 0;
528 rval = strtol(str, &end, 10);
529
530 if (str == end || *end != '\0')
531 return EINVAL;
532
533 if (errno)
534 return errno;
535
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300536 if (rval > max || rval < min)
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200537 return ERANGE;
538
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300539 *val = rval;
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200540 return 0;
541}
542
543int tst_parse_float(const char *str, float *val, float min, float max)
544{
545 double rval;
546 char *end;
547
548 if (!str)
549 return 0;
550
551 errno = 0;
552 rval = strtod(str, &end);
553
554 if (str == end || *end != '\0')
555 return EINVAL;
556
557 if (errno)
558 return errno;
559
560 if (rval > (double)max || rval < (double)min)
561 return ERANGE;
562
563 *val = (float)rval;
564 return 0;
565}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100566
Cyril Hrubisfa495172016-06-08 16:06:35 +0200567static void do_exit(int ret)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100568{
Xiao Yang11dfc322016-06-16 15:52:04 +0800569 if (results) {
570 printf("\nSummary:\n");
571 printf("passed %d\n", results->passed);
572 printf("failed %d\n", results->failed);
573 printf("skipped %d\n", results->skipped);
574 printf("warnings %d\n", results->warnings);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100575
Xiao Yang11dfc322016-06-16 15:52:04 +0800576 if (results->failed)
577 ret |= TFAIL;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100578
Cyril Hrubis5390d6e2017-09-07 15:47:22 +0200579 if (results->skipped && !results->passed)
Xiao Yang11dfc322016-06-16 15:52:04 +0800580 ret |= TCONF;
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100581
Xiao Yang11dfc322016-06-16 15:52:04 +0800582 if (results->warnings)
583 ret |= TWARN;
584 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100585
Jan Stancek332540e2016-06-08 16:48:22 +0200586 do_cleanup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100587
588 exit(ret);
589}
590
591void check_kver(void)
592{
593 int v1, v2, v3;
594
Cyril Hrubis4dcfd282016-11-01 15:07:12 +0100595 if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
596 tst_res(TWARN,
597 "Invalid kernel version %s, expected %%d.%%d.%%d",
598 tst_test->min_kver);
599 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100600
601 if (tst_kvercmp(v1, v2, v3) < 0) {
602 tst_brk(TCONF, "The test requires kernel %s or newer",
603 tst_test->min_kver);
604 }
605}
606
607static int results_equal(struct results *a, struct results *b)
608{
609 if (a->passed != b->passed)
610 return 0;
611
612 if (a->failed != b->failed)
613 return 0;
614
615 if (a->skipped != b->skipped)
616 return 0;
617
618 return 1;
619}
620
621static int needs_tmpdir(void)
622{
623 return tst_test->needs_tmpdir ||
624 tst_test->needs_device ||
625 tst_test->resource_files ||
626 tst_test->needs_checkpoints;
627}
628
629static void copy_resources(void)
630{
631 unsigned int i;
632
633 for (i = 0; tst_test->resource_files[i]; i++)
634 TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
635}
636
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800637static const char *get_tid(char *argv[])
638{
639 char *p;
640
641 if (!argv[0] || !argv[0][0]) {
642 tst_res(TINFO, "argv[0] is empty!");
643 return "ltp_empty_argv";
644 }
645
646 p = strrchr(argv[0], '/');
647 if (p)
648 return p+1;
649
650 return argv[0];
651}
652
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100653static struct tst_device tdev;
654struct tst_device *tst_device;
655
Cyril Hrubisc4596542017-06-20 15:42:18 +0200656static void assert_test_fn(void)
657{
658 int cnt = 0;
659
660 if (tst_test->test)
661 cnt++;
662
663 if (tst_test->test_all)
664 cnt++;
665
666 if (tst_test->sample)
667 cnt++;
668
669 if (!cnt)
670 tst_brk(TBROK, "No test function speficied");
671
672 if (cnt != 1)
673 tst_brk(TBROK, "You can define only one test function");
674
675 if (tst_test->test && !tst_test->tcnt)
676 tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
677
678 if (!tst_test->test && tst_test->tcnt)
679 tst_brk(TBROK, "You can define tcnt only for test()");
680}
681
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100682static void do_setup(int argc, char *argv[])
683{
684 if (!tst_test)
685 tst_brk(TBROK, "No tests to run");
686
Cyril Hrubisf706a2f2017-08-01 17:31:30 +0200687 if (tst_test->tconf_msg)
688 tst_brk(TCONF, "%s", tst_test->tconf_msg);
689
Cyril Hrubisc4596542017-06-20 15:42:18 +0200690 assert_test_fn();
691
Li Wangb5d620a2017-11-01 13:15:23 +0800692 tid = get_tid(argv);
693
Cyril Hrubisc4596542017-06-20 15:42:18 +0200694 if (tst_test->sample)
695 tst_test = tst_timer_test_setup(tst_test);
696
Cyril Hrubis88c220d2017-07-27 11:16:39 +0200697 parse_opts(argc, argv);
698
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100699 if (tst_test->needs_root && geteuid() != 0)
700 tst_brk(TCONF, "Test needs to be run as root");
701
702 if (tst_test->min_kver)
703 check_kver();
704
Cyril Hrubis874326d2017-02-14 15:59:40 +0100705 if (tst_test->format_device)
706 tst_test->needs_device = 1;
707
708 if (tst_test->mount_device) {
709 tst_test->needs_device = 1;
710 tst_test->format_device = 1;
711 }
712
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100713 setup_ipc();
714
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000715 if (needs_tmpdir() && !tst_tmpdir_created())
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100716 tst_tmpdir();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100717
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700718 if (tst_test->mntpoint)
719 SAFE_MKDIR(tst_test->mntpoint, 0777);
720
721 if ((tst_test->needs_rofs || tst_test->mount_device) &&
722 !tst_test->mntpoint) {
723 tst_brk(TBROK, "tst_test->mntpoint must be set!");
724 }
725
726 if (tst_test->needs_rofs) {
727 /* If we failed to mount read-only tmpfs. Fallback to
728 * using a device with empty read-only filesystem.
729 */
730 if (mount(NULL, tst_test->mntpoint, "tmpfs", MS_RDONLY, NULL)) {
731 tst_res(TINFO | TERRNO, "Can't mount tmpfs read-only"
732 " at %s, setting up a device instead\n",
733 tst_test->mntpoint);
734 tst_test->mount_device = 1;
735 tst_test->needs_device = 1;
736 tst_test->format_device = 1;
737 tst_test->mnt_flags = MS_RDONLY;
738 } else {
739 mntpoint_mounted = 1;
740 }
741 }
742
743 if (tst_test->needs_device && !mntpoint_mounted) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100744 tdev.dev = tst_acquire_device_(NULL, tst_test->dev_min_size);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100745
746 if (!tdev.dev)
747 tst_brk(TCONF, "Failed to acquire device");
748
749 tst_device = &tdev;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100750
751 if (tst_test->dev_fs_type)
752 tdev.fs_type = tst_test->dev_fs_type;
753 else
754 tdev.fs_type = tst_dev_fs_type();
755
756 if (tst_test->format_device) {
757 SAFE_MKFS(tdev.dev, tdev.fs_type,
758 tst_test->dev_fs_opts,
759 tst_test->dev_extra_opt);
760 }
761
762 if (tst_test->mount_device) {
Cyril Hrubis874326d2017-02-14 15:59:40 +0100763 SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
764 tst_test->mnt_flags, tst_test->mnt_data);
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700765 mntpoint_mounted = 1;
Cyril Hrubis874326d2017-02-14 15:59:40 +0100766 }
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100767 }
768
769 if (tst_test->resource_files)
770 copy_resources();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200771}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100772
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200773static void do_test_setup(void)
774{
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100775 main_pid = getpid();
776
777 if (tst_test->setup)
778 tst_test->setup();
779
780 if (main_pid != getpid())
781 tst_brk(TBROK, "Runaway child in setup()!");
782}
783
784static void do_cleanup(void)
785{
Sandeep Patilc9a7def2017-09-19 12:49:58 -0700786 if (mntpoint_mounted)
Cyril Hrubis874326d2017-02-14 15:59:40 +0100787 tst_umount(tst_test->mntpoint);
788
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100789 if (tst_test->needs_device && tdev.dev)
790 tst_release_device(tdev.dev);
791
Steven Jackson9f41dcf2016-12-21 20:09:01 +0000792 if (tst_tmpdir_created()) {
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100793 /* avoid munmap() on wrong pointer in tst_rmdir() */
794 tst_futexes = NULL;
795 tst_rmdir();
796 }
Jan Stancek332540e2016-06-08 16:48:22 +0200797
798 cleanup_ipc();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100799}
800
801static void run_tests(void)
802{
803 unsigned int i;
804 struct results saved_results;
805
806 if (!tst_test->test) {
807 saved_results = *results;
808 tst_test->test_all();
809
810 if (getpid() != main_pid) {
811 exit(0);
812 }
813
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +0300814 tst_reap_children();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100815
816 if (results_equal(&saved_results, results))
817 tst_brk(TBROK, "Test haven't reported results!");
818 return;
819 }
820
821 for (i = 0; i < tst_test->tcnt; i++) {
822 saved_results = *results;
823 tst_test->test(i);
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 %i haven't reported results!", i);
833 }
834}
835
836static unsigned long long get_time_ms(void)
837{
838 struct timeval tv;
839
840 gettimeofday(&tv, NULL);
841
842 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
843}
844
Jan Stancekb95b1992017-10-10 15:47:58 +0200845static void add_paths(void)
846{
847 char *old_path = getenv("PATH");
848 const char *start_dir;
849 char *new_path;
850
851 start_dir = tst_get_startwd();
852
853 if (old_path)
854 SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
855 else
856 SAFE_ASPRINTF(&new_path, "::%s", start_dir);
857
858 SAFE_SETENV("PATH", new_path, 1);
859 free(new_path);
860}
861
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200862static void testrun(void)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100863{
864 unsigned int i = 0;
865 unsigned long long stop_time = 0;
866 int cont = 1;
867
Jan Stancekb95b1992017-10-10 15:47:58 +0200868 add_paths();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200869 do_test_setup();
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100870
871 if (duration > 0)
872 stop_time = get_time_ms() + (unsigned long long)(duration * 1000);
873
874 for (;;) {
875 cont = 0;
876
877 if (i < (unsigned int)iterations) {
878 i++;
879 cont = 1;
880 }
881
882 if (stop_time && get_time_ms() < stop_time)
883 cont = 1;
884
885 if (!cont)
886 break;
887
888 run_tests();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200889
890 kill(getppid(), SIGUSR1);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100891 }
892
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200893 do_test_cleanup();
894 exit(0);
895}
896
897static pid_t test_pid;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200898
Cyril Hrubis79163172017-05-18 10:57:07 +0200899
900static volatile sig_atomic_t sigkill_retries;
901
902#define WRITE_MSG(msg) do { \
903 if (write(2, msg, sizeof(msg) - 1)) { \
904 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */ \
905 } \
906} while (0)
907
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200908static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
909{
Cyril Hrubis79163172017-05-18 10:57:07 +0200910 WRITE_MSG("Test timeouted, sending SIGKILL!\n");
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200911 kill(-test_pid, SIGKILL);
Cyril Hrubis79163172017-05-18 10:57:07 +0200912 alarm(5);
913
914 if (++sigkill_retries > 10) {
915 WRITE_MSG("Cannot kill test processes!\n");
916 WRITE_MSG("Congratulation, likely test hit a kernel bug.\n");
917 WRITE_MSG("Exitting uncleanly...\n");
918 _exit(TFAIL);
919 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200920}
921
922static void heartbeat_handler(int sig LTP_ATTRIBUTE_UNUSED)
923{
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200924 alarm(results->timeout);
Cyril Hrubis79163172017-05-18 10:57:07 +0200925 sigkill_retries = 0;
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200926}
927
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200928static void sigint_handler(int sig LTP_ATTRIBUTE_UNUSED)
929{
930 if (test_pid > 0) {
Cyril Hrubis79163172017-05-18 10:57:07 +0200931 WRITE_MSG("Sending SIGKILL to test process...\n");
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200932 kill(-test_pid, SIGKILL);
933 }
934}
935
Li Wang94823cf2017-07-18 16:23:00 +0800936void tst_set_timeout(int timeout)
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200937{
938 char *mul = getenv("LTP_TIMEOUT_MUL");
939
Li Wang94823cf2017-07-18 16:23:00 +0800940 if (timeout == -1) {
941 tst_res(TINFO, "Timeout per run is disabled");
942 return;
943 }
944
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200945 results->timeout = timeout;
946
947 if (mul) {
948 float m = atof(mul);
949
950 if (m < 1)
951 tst_brk(TBROK, "Invalid timeout multiplier '%s'", mul);
952
953 results->timeout = results->timeout * m + 0.5;
954 }
955
956 tst_res(TINFO, "Timeout per run is %uh %02um %02us",
957 results->timeout/3600, (results->timeout%3600)/60,
958 results->timeout % 60);
959
960 if (getpid() == lib_pid)
961 alarm(results->timeout);
962 else
963 kill(getppid(), SIGUSR1);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200964}
965
966void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
967{
968 int status;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200969
Jan Stanceke0bfa7d2016-06-08 15:27:55 +0200970 lib_pid = getpid();
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200971 tst_test = self;
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200972
973 do_setup(argc, argv);
974
Li Wangb5d620a2017-11-01 13:15:23 +0800975 TCID = tid;
Cyril Hrubisa5bf5252017-03-14 15:25:29 +0800976
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200977 SAFE_SIGNAL(SIGALRM, alarm_handler);
978 SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
979
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200980 if (tst_test->timeout)
981 tst_set_timeout(tst_test->timeout);
982 else
983 tst_set_timeout(300);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200984
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200985 SAFE_SIGNAL(SIGINT, sigint_handler);
986
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200987 test_pid = fork();
988 if (test_pid < 0)
989 tst_brk(TBROK | TERRNO, "fork()");
990
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200991 if (!test_pid) {
Cyril Hrubisa41e9942016-08-04 16:31:06 +0200992 SAFE_SIGNAL(SIGALRM, SIG_DFL);
993 SAFE_SIGNAL(SIGUSR1, SIG_DFL);
994 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200995 SAFE_SETPGID(0, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200996 testrun();
Cyril Hrubis0f053c82016-06-07 14:29:39 +0200997 }
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200998
999 SAFE_WAITPID(test_pid, &status, 0);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001000 alarm(0);
Cyril Hrubisa41e9942016-08-04 16:31:06 +02001001 SAFE_SIGNAL(SIGINT, SIG_DFL);
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001002
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001003 if (WIFEXITED(status) && WEXITSTATUS(status))
Cyril Hrubisfa495172016-06-08 16:06:35 +02001004 do_exit(WEXITSTATUS(status));
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +02001005
1006 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
1007 tst_res(TINFO, "If you are running on slow machine, "
1008 "try exporting LTP_TIMEOUT_MUL > 1");
1009 tst_brk(TBROK, "Test killed! (timeout?)");
1010 }
1011
1012 if (WIFSIGNALED(status))
1013 tst_brk(TBROK, "Test killed by %s!", tst_strsig(WTERMSIG(status)));
1014
Cyril Hrubisfa495172016-06-08 16:06:35 +02001015 do_exit(0);
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001016}