forked from linux-test-project/ltp
/
tst_test.c
1500 lines (1159 loc) · 30.5 KB
/
tst_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
* Copyright (c) Linux Test Project, 2016-2021
*/
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#define TST_NO_DEFAULT_MAIN
#include "tst_test.h"
#include "tst_device.h"
#include "lapi/futex.h"
#include "lapi/syscalls.h"
#include "tst_ansi_color.h"
#include "tst_safe_stdio.h"
#include "tst_timer_test.h"
#include "tst_clocks.h"
#include "tst_timer.h"
#include "tst_wallclock.h"
#include "tst_sys_conf.h"
#include "tst_kconfig.h"
#include "old_resource.h"
#include "old_device.h"
#include "old_tmpdir.h"
/*
* Hack to get TCID defined in newlib tests
*/
const char *TCID __attribute__((weak));
/* update also docparse/testinfo.pl */
#define LINUX_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id="
#define LINUX_STABLE_GIT_URL "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id="
#define GLIBC_GIT_URL "https://sourceware.org/git/?p=glibc.git;a=commit;h="
#define CVE_DB_URL "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-"
struct tst_test *tst_test;
static const char *tid;
static int iterations = 1;
static float duration = -1;
static float timeout_mul = -1;
static pid_t main_pid, lib_pid;
static int mntpoint_mounted;
static int ovl_mounted;
static struct timespec tst_start_time; /* valid only for test pid */
struct results {
int passed;
int skipped;
int failed;
int warnings;
int broken;
unsigned int timeout;
};
static struct results *results;
static int ipc_fd;
extern void *tst_futexes;
extern unsigned int tst_max_futexes;
static char ipc_path[1064];
const char *tst_ipc_path = ipc_path;
static char shm_path[1024];
int TST_ERR;
int TST_PASS;
long TST_RET;
static void do_cleanup(void);
static void do_exit(int ret) __attribute__ ((noreturn));
static void setup_ipc(void)
{
size_t size = getpagesize();
if (access("/dev/shm", F_OK) == 0) {
snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d",
tid, getpid());
} else {
char *tmpdir;
if (!tst_tmpdir_created())
tst_tmpdir();
tmpdir = tst_get_tmpdir();
snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d",
tmpdir, tid, getpid());
free(tmpdir);
}
ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600);
if (ipc_fd < 0)
tst_brk(TBROK | TERRNO, "open(%s)", shm_path);
SAFE_CHMOD(shm_path, 0666);
SAFE_FTRUNCATE(ipc_fd, size);
results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
/* Checkpoints needs to be accessible from processes started by exec() */
if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path);
putenv(ipc_path);
} else {
SAFE_UNLINK(shm_path);
}
SAFE_CLOSE(ipc_fd);
if (tst_test->needs_checkpoints) {
tst_futexes = (char*)results + sizeof(struct results);
tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
}
}
static void cleanup_ipc(void)
{
size_t size = getpagesize();
if (ipc_fd > 0 && close(ipc_fd))
tst_res(TWARN | TERRNO, "close(ipc_fd) failed");
if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
if (results) {
msync((void*)results, size, MS_SYNC);
munmap((void*)results, size);
results = NULL;
}
}
void tst_reinit(void)
{
const char *path = getenv(IPC_ENV_VAR);
size_t size = getpagesize();
int fd;
if (!path)
tst_brk(TBROK, IPC_ENV_VAR" is not defined");
if (access(path, F_OK))
tst_brk(TBROK, "File %s does not exist!", path);
fd = SAFE_OPEN(path, O_RDWR);
results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
tst_futexes = (char*)results + sizeof(struct results);
tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
SAFE_CLOSE(fd);
}
static void update_results(int ttype)
{
if (!results)
return;
switch (ttype) {
case TCONF:
tst_atomic_inc(&results->skipped);
break;
case TPASS:
tst_atomic_inc(&results->passed);
break;
case TWARN:
tst_atomic_inc(&results->warnings);
break;
case TFAIL:
tst_atomic_inc(&results->failed);
break;
case TBROK:
tst_atomic_inc(&results->broken);
break;
}
}
static void print_result(const char *file, const int lineno, int ttype,
const char *fmt, va_list va)
{
char buf[1024];
char *str = buf;
int ret, size = sizeof(buf), ssize, int_errno, buflen;
const char *str_errno = NULL;
const char *res;
switch (TTYPE_RESULT(ttype)) {
case TPASS:
res = "TPASS";
break;
case TFAIL:
res = "TFAIL";
break;
case TBROK:
res = "TBROK";
break;
case TCONF:
res = "TCONF";
break;
case TWARN:
res = "TWARN";
break;
case TINFO:
res = "TINFO";
break;
default:
tst_brk(TBROK, "Invalid ttype value %i", ttype);
abort();
}
if (ttype & TERRNO) {
str_errno = tst_strerrno(errno);
int_errno = errno;
}
if (ttype & TTERRNO) {
str_errno = tst_strerrno(TST_ERR);
int_errno = TST_ERR;
}
if (ttype & TRERRNO) {
int_errno = TST_RET < 0 ? -(int)TST_RET : (int)TST_RET;
str_errno = tst_strerrno(int_errno);
}
ret = snprintf(str, size, "%s:%i: ", file, lineno);
str += ret;
size -= ret;
if (tst_color_enabled(STDERR_FILENO))
ret = snprintf(str, size, "%s%s: %s", tst_ttype2color(ttype),
res, ANSI_COLOR_RESET);
else
ret = snprintf(str, size, "%s: ", res);
str += ret;
size -= ret;
ssize = size - 2;
ret = vsnprintf(str, size, fmt, va);
str += MIN(ret, ssize);
size -= MIN(ret, ssize);
if (ret >= ssize) {
tst_res_(file, lineno, TWARN,
"Next message is too long and truncated:");
} else if (str_errno) {
ssize = size - 2;
ret = snprintf(str, size, ": %s (%d)", str_errno, int_errno);
str += MIN(ret, ssize);
size -= MIN(ret, ssize);
if (ret >= ssize)
tst_res_(file, lineno, TWARN,
"Next message is too long and truncated:");
}
snprintf(str, size, "\n");
/* we might be called from signal handler, so use write() */
buflen = str - buf + 1;
str = buf;
while (buflen) {
ret = write(STDERR_FILENO, str, buflen);
if (ret <= 0)
break;
str += ret;
buflen -= ret;
}
}
void tst_vres_(const char *file, const int lineno, int ttype,
const char *fmt, va_list va)
{
print_result(file, lineno, ttype, fmt, va);
update_results(TTYPE_RESULT(ttype));
}
void tst_vbrk_(const char *file, const int lineno, int ttype,
const char *fmt, va_list va);
static void (*tst_brk_handler)(const char *file, const int lineno, int ttype,
const char *fmt, va_list va) = tst_vbrk_;
static void tst_cvres(const char *file, const int lineno, int ttype,
const char *fmt, va_list va)
{
if (TTYPE_RESULT(ttype) == TBROK) {
ttype &= ~TTYPE_MASK;
ttype |= TWARN;
}
print_result(file, lineno, ttype, fmt, va);
update_results(TTYPE_RESULT(ttype));
}
static void do_test_cleanup(void)
{
tst_brk_handler = tst_cvres;
if (tst_test->cleanup)
tst_test->cleanup();
tst_free_all();
tst_brk_handler = tst_vbrk_;
}
void tst_vbrk_(const char *file, const int lineno, int ttype,
const char *fmt, va_list va)
{
print_result(file, lineno, ttype, fmt, va);
update_results(TTYPE_RESULT(ttype));
/*
* The getpid implementation in some C library versions may cause cloned
* test threads to show the same pid as their parent when CLONE_VM is
* specified but CLONE_THREAD is not. Use direct syscall to avoid
* cleanup running in the child.
*/
if (syscall(SYS_getpid) == main_pid)
do_test_cleanup();
if (getpid() == lib_pid)
do_exit(TTYPE_RESULT(ttype));
exit(TTYPE_RESULT(ttype));
}
void tst_res_(const char *file, const int lineno, int ttype,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
tst_vres_(file, lineno, ttype, fmt, va);
va_end(va);
}
void tst_brk_(const char *file, const int lineno, int ttype,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
tst_brk_handler(file, lineno, ttype, fmt, va);
va_end(va);
}
void tst_printf(const char *const fmt, ...)
{
va_list va;
va_start(va, fmt);
vdprintf(STDERR_FILENO, fmt, va);
va_end(va);
}
static void check_child_status(pid_t pid, int status)
{
int ret;
if (WIFSIGNALED(status)) {
tst_brk(TBROK, "Child (%i) killed by signal %s",
pid, tst_strsig(WTERMSIG(status)));
}
if (!(WIFEXITED(status)))
tst_brk(TBROK, "Child (%i) exited abnormally", pid);
ret = WEXITSTATUS(status);
switch (ret) {
case TPASS:
case TBROK:
case TCONF:
break;
default:
tst_brk(TBROK, "Invalid child (%i) exit value %i", pid, ret);
}
}
void tst_reap_children(void)
{
int status;
pid_t pid;
for (;;) {
pid = wait(&status);
if (pid > 0) {
check_child_status(pid, status);
continue;
}
if (errno == ECHILD)
break;
if (errno == EINTR)
continue;
tst_brk(TBROK | TERRNO, "wait() failed");
}
}
pid_t safe_fork(const char *filename, unsigned int lineno)
{
pid_t pid;
if (!tst_test->forks_child)
tst_brk(TBROK, "test.forks_child must be set!");
tst_flush();
pid = fork();
if (pid < 0)
tst_brk_(filename, lineno, TBROK | TERRNO, "fork() failed");
if (!pid)
atexit(tst_free_all);
return pid;
}
pid_t safe_clone(const char *file, const int lineno,
const struct tst_clone_args *args)
{
pid_t pid;
if (!tst_test->forks_child)
tst_brk(TBROK, "test.forks_child must be set!");
pid = tst_clone(args);
switch (pid) {
case -1:
tst_brk_(file, lineno, TBROK | TERRNO, "clone3 failed");
break;
case -2:
tst_brk_(file, lineno, TBROK | TERRNO, "clone failed");
return -1;
}
if (!pid)
atexit(tst_free_all);
return pid;
}
static struct option {
char *optstr;
char *help;
} options[] = {
{"h", "-h Prints this help"},
{"i:", "-i n Execute test n times"},
{"I:", "-I x Execute test for n seconds"},
{"C:", "-C ARG Run child process with ARG arguments (used internally)"},
};
static void print_help(void)
{
unsigned int i;
fprintf(stderr, "Options\n");
fprintf(stderr, "-------\n");
for (i = 0; i < ARRAY_SIZE(options); i++)
fprintf(stderr, "%s\n", options[i].help);
if (!tst_test->options)
return;
for (i = 0; tst_test->options[i].optstr; i++)
fprintf(stderr, "%s\n", tst_test->options[i].help);
}
static void print_test_tags(void)
{
unsigned int i;
const struct tst_tag *tags = tst_test->tags;
if (!tags)
return;
fprintf(stderr, "\nTags\n");
fprintf(stderr, "----\n");
for (i = 0; tags[i].name; i++) {
if (!strcmp(tags[i].name, "CVE"))
fprintf(stderr, CVE_DB_URL "%s\n", tags[i].value);
else if (!strcmp(tags[i].name, "linux-git"))
fprintf(stderr, LINUX_GIT_URL "%s\n", tags[i].value);
else if (!strcmp(tags[i].name, "linux-stable-git"))
fprintf(stderr, LINUX_STABLE_GIT_URL "%s\n", tags[i].value);
else if (!strcmp(tags[i].name, "glibc-git"))
fprintf(stderr, GLIBC_GIT_URL "%s\n", tags[i].value);
else
fprintf(stderr, "%s: %s\n", tags[i].name, tags[i].value);
}
fprintf(stderr, "\n");
}
static void check_option_collision(void)
{
unsigned int i, j;
struct tst_option *toptions = tst_test->options;
if (!toptions)
return;
for (i = 0; toptions[i].optstr; i++) {
for (j = 0; j < ARRAY_SIZE(options); j++) {
if (toptions[i].optstr[0] == options[j].optstr[0]) {
tst_brk(TBROK, "Option collision '%s'",
options[j].help);
}
}
}
}
static unsigned int count_options(void)
{
unsigned int i;
if (!tst_test->options)
return 0;
for (i = 0; tst_test->options[i].optstr; i++);
return i;
}
static void parse_topt(unsigned int topts_len, int opt, char *optarg)
{
unsigned int i;
struct tst_option *toptions = tst_test->options;
for (i = 0; i < topts_len; i++) {
if (toptions[i].optstr[0] == opt)
break;
}
if (i >= topts_len)
tst_brk(TBROK, "Invalid option '%c' (should not happen)", opt);
if (*toptions[i].arg)
tst_res(TWARN, "Option -%c passed multiple times", opt);
*(toptions[i].arg) = optarg ? optarg : "";
}
/* see self_exec.c */
#ifdef UCLINUX
extern char *child_args;
#endif
static void parse_opts(int argc, char *argv[])
{
unsigned int i, topts_len = count_options();
char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
int opt;
check_option_collision();
optstr[0] = 0;
for (i = 0; i < ARRAY_SIZE(options); i++)
strcat(optstr, options[i].optstr);
for (i = 0; i < topts_len; i++)
strcat(optstr, tst_test->options[i].optstr);
while ((opt = getopt(argc, argv, optstr)) > 0) {
switch (opt) {
case '?':
print_help();
tst_brk(TBROK, "Invalid option");
break;
case 'h':
print_help();
print_test_tags();
exit(0);
case 'i':
iterations = atoi(optarg);
break;
case 'I':
duration = atof(optarg);
break;
case 'C':
#ifdef UCLINUX
child_args = optarg;
#endif
break;
default:
parse_topt(topts_len, opt, optarg);
}
}
if (optind < argc)
tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
}
int tst_parse_int(const char *str, int *val, int min, int max)
{
long rval;
if (!str)
return 0;
int ret = tst_parse_long(str, &rval, min, max);
if (ret)
return ret;
*val = (int)rval;
return 0;
}
int tst_parse_long(const char *str, long *val, long min, long max)
{
long rval;
char *end;
if (!str)
return 0;
errno = 0;
rval = strtol(str, &end, 10);
if (str == end || *end != '\0')
return EINVAL;
if (errno)
return errno;
if (rval > max || rval < min)
return ERANGE;
*val = rval;
return 0;
}
int tst_parse_float(const char *str, float *val, float min, float max)
{
double rval;
char *end;
if (!str)
return 0;
errno = 0;
rval = strtod(str, &end);
if (str == end || *end != '\0')
return EINVAL;
if (errno)
return errno;
if (rval > (double)max || rval < (double)min)
return ERANGE;
*val = (float)rval;
return 0;
}
static void print_colored(const char *str)
{
if (tst_color_enabled(STDOUT_FILENO))
fprintf(stderr, "%s%s%s", ANSI_COLOR_YELLOW, str, ANSI_COLOR_RESET);
else
fprintf(stderr, "%s", str);
}
static void print_failure_hint(const char *tag, const char *hint,
const char *url)
{
const struct tst_tag *tags = tst_test->tags;
if (!tags)
return;
unsigned int i;
int hint_printed = 0;
for (i = 0; tags[i].name; i++) {
if (!strcmp(tags[i].name, tag)) {
if (!hint_printed) {
hint_printed = 1;
fprintf(stderr, "\n");
print_colored("HINT: ");
fprintf(stderr, "You _MAY_ be %s, see:\n\n", hint);
}
fprintf(stderr, "%s%s\n", url, tags[i].value);
}
}
}
/* update also docparse/testinfo.pl */
static void print_failure_hints(void)
{
print_failure_hint("linux-git", "missing kernel fixes", LINUX_GIT_URL);
print_failure_hint("linux-stable-git", "missing stable kernel fixes",
LINUX_STABLE_GIT_URL);
print_failure_hint("glibc-git", "missing glibc fixes", GLIBC_GIT_URL);
print_failure_hint("CVE", "vulnerable to CVE(s)", CVE_DB_URL);
}
static void do_exit(int ret)
{
if (results) {
if (results->passed && ret == TCONF)
ret = 0;
if (results->failed) {
ret |= TFAIL;
print_failure_hints();
}
if (results->skipped && !results->passed)
ret |= TCONF;
if (results->warnings)
ret |= TWARN;
if (results->broken)
ret |= TBROK;
fprintf(stderr, "\nSummary:\n");
fprintf(stderr, "passed %d\n", results->passed);
fprintf(stderr, "failed %d\n", results->failed);
fprintf(stderr, "broken %d\n", results->broken);
fprintf(stderr, "skipped %d\n", results->skipped);
fprintf(stderr, "warnings %d\n", results->warnings);
}
do_cleanup();
exit(ret);
}
void check_kver(void)
{
int v1, v2, v3;
if (tst_parse_kver(tst_test->min_kver, &v1, &v2, &v3)) {
tst_res(TWARN,
"Invalid kernel version %s, expected %%d.%%d.%%d",
tst_test->min_kver);
}
if (tst_kvercmp(v1, v2, v3) < 0) {
tst_brk(TCONF, "The test requires kernel %s or newer",
tst_test->min_kver);
}
}
static int results_equal(struct results *a, struct results *b)
{
if (a->passed != b->passed)
return 0;
if (a->failed != b->failed)
return 0;
if (a->skipped != b->skipped)
return 0;
if (a->broken != b->broken)
return 0;
return 1;
}
static int needs_tmpdir(void)
{
return tst_test->needs_tmpdir ||
tst_test->needs_device ||
tst_test->mntpoint ||
tst_test->resource_files ||
tst_test->needs_checkpoints;
}
static void copy_resources(void)
{
unsigned int i;
for (i = 0; tst_test->resource_files[i]; i++)
TST_RESOURCE_COPY(NULL, tst_test->resource_files[i], NULL);
}
static const char *get_tid(char *argv[])
{
char *p;
if (!argv[0] || !argv[0][0]) {
tst_res(TINFO, "argv[0] is empty!");
return "ltp_empty_argv";
}
p = strrchr(argv[0], '/');
if (p)
return p+1;
return argv[0];
}
static struct tst_device tdev;
struct tst_device *tst_device;
static void assert_test_fn(void)
{
int cnt = 0;
if (tst_test->test)
cnt++;
if (tst_test->test_all)
cnt++;
if (tst_test->sample)
cnt++;
if (!cnt)
tst_brk(TBROK, "No test function specified");
if (cnt != 1)
tst_brk(TBROK, "You can define only one test function");
if (tst_test->test && !tst_test->tcnt)
tst_brk(TBROK, "Number of tests (tcnt) must be > 0");
if (!tst_test->test && tst_test->tcnt)
tst_brk(TBROK, "You can define tcnt only for test()");
}
static int prepare_and_mount_ro_fs(const char *dev,
const char *mntpoint,
const char *fs_type)
{
char buf[PATH_MAX];
if (mount(dev, mntpoint, fs_type, 0, NULL)) {
tst_res(TINFO | TERRNO, "Can't mount %s at %s (%s)",
dev, mntpoint, fs_type);
return 1;
}
mntpoint_mounted = 1;
snprintf(buf, sizeof(buf), "%s/dir/", mntpoint);
SAFE_MKDIR(buf, 0777);
snprintf(buf, sizeof(buf), "%s/file", mntpoint);
SAFE_FILE_PRINTF(buf, "file content");
SAFE_CHMOD(buf, 0777);
SAFE_MOUNT(dev, mntpoint, fs_type, MS_REMOUNT | MS_RDONLY, NULL);
return 0;
}
static void prepare_and_mount_dev_fs(const char *mntpoint)
{
const char *flags[] = {"nodev", NULL};
int mounted_nodev;
mounted_nodev = tst_path_has_mnt_flags(NULL, flags);
if (mounted_nodev) {
tst_res(TINFO, "tmpdir isn't suitable for creating devices, "
"mounting tmpfs without nodev on %s", mntpoint);
SAFE_MOUNT(NULL, mntpoint, "tmpfs", 0, NULL);
mntpoint_mounted = 1;
}
}
static const char *limit_tmpfs_mount_size(const char *mnt_data,
char *buf, size_t buf_size, const char *fs_type)
{
unsigned int tmpfs_size;
if (strcmp(fs_type, "tmpfs"))
return mnt_data;
if (!tst_test->dev_min_size)
tmpfs_size = 32;
else
tmpfs_size = tdev.size;
if ((tst_available_mem() / 1024) < (tmpfs_size * 2))
tst_brk(TCONF, "No enough memory for tmpfs use");
if (mnt_data)
snprintf(buf, buf_size, "%s,size=%uM", mnt_data, tmpfs_size);
else
snprintf(buf, buf_size, "size=%uM", tmpfs_size);
tst_res(TINFO, "Limiting tmpfs size to %uMB", tmpfs_size);
return buf;
}
static const char *get_device_name(const char *fs_type)
{
if (!strcmp(fs_type, "tmpfs"))
return "ltp-tmpfs";
else
return tdev.dev;
}
static void prepare_device(void)
{
const char *mnt_data;
char buf[1024];
if (tst_test->format_device) {
SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
tst_test->dev_extra_opts);
}
if (tst_test->needs_rofs) {
prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
tdev.fs_type);
return;
}
if (tst_test->mount_device) {
mnt_data = limit_tmpfs_mount_size(tst_test->mnt_data,
buf, sizeof(buf), tdev.fs_type);
SAFE_MOUNT(get_device_name(tdev.fs_type), tst_test->mntpoint,
tdev.fs_type, tst_test->mnt_flags, mnt_data);
mntpoint_mounted = 1;
}
}
static void do_setup(int argc, char *argv[])
{
if (!tst_test)
tst_brk(TBROK, "No tests to run");
if (tst_test->tconf_msg)
tst_brk(TCONF, "%s", tst_test->tconf_msg);
if (tst_test->needs_kconfigs)
tst_kconfig_check(tst_test->needs_kconfigs);
assert_test_fn();
TCID = tid = get_tid(argv);
if (tst_test->sample)
tst_test = tst_timer_test_setup(tst_test);
parse_opts(argc, argv);
if (tst_test->needs_root && geteuid() != 0)
tst_brk(TCONF, "Test needs to be run as root");
if (tst_test->min_kver)
check_kver();
if (tst_test->skip_in_lockdown && tst_lockdown_enabled())
tst_brk(TCONF, "Kernel is locked down, skipping test");
if (tst_test->needs_cmds) {
const char *cmd;
char path[PATH_MAX];
int i;
for (i = 0; (cmd = tst_test->needs_cmds[i]); ++i)
if (tst_get_path(cmd, path, sizeof(path)))
tst_brk(TCONF, "Couldn't find '%s' in $PATH", cmd);
}
if (tst_test->needs_drivers) {
const char *name;
int i;
for (i = 0; (name = tst_test->needs_drivers[i]); ++i)
if (tst_check_driver(name))
tst_brk(TCONF, "%s driver not available", name);
}
if (tst_test->mount_device)