-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaulty_test.c
98 lines (91 loc) · 1.74 KB
/
faulty_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
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "kselftest.h"
struct test {
const char *name;
const char *dev;
int flags;
};
static void test(const struct test *restrict t)
{
char path[PATH_MAX];
int ret, fd;
ret = snprintf(path, sizeof(path), "/dev/%s", t->dev);
if (ret < 0)
goto perr;
fd = open(path, t->flags);
if (fd == -1)
goto perr;
if ((t->flags&O_ACCMODE) != O_WRONLY) {
ret = read(fd, NULL, 0);
if (ret == -1)
goto perr;
}
if ((t->flags&O_ACCMODE) != O_RDONLY) {
ret = write(fd, NULL, 0);
if (ret == -1)
goto perr;
}
exit(EXIT_SUCCESS);
perr:
perror(t->name);
exit(EXIT_FAILURE);
}
int main(void)
{
const struct test *t, tests[] = {
{
.name = "/dev/faulty0 read-only",
.dev = "faulty0",
.flags = O_RDONLY,
},
{
.name = "/dev/faulty1 read-write",
.dev = "faulty1",
.flags = O_RDWR,
},
{.name = NULL}, /* sentry */
};
for (t = tests; t->name; t++) {
int ret, status;
pid_t pid;
pid = fork();
if (pid == -1)
goto perr;
else if (pid == 0)
test(t);
ret = waitpid(pid, &status, 0);
if (ret == -1)
goto perr;
if (WIFSIGNALED(status)) {
fprintf(stderr, "%s: signaled with %s\n",
t->name, strsignal(WTERMSIG(status)));
goto err;
}
if (!WIFEXITED(status)) {
fprintf(stderr, "%s: not exit\n", t->name);
goto err;
}
if (WEXITSTATUS(status))
goto err;
ksft_inc_pass_cnt();
continue;
perr:
fprintf(stderr, "%s: %s\n", t->name,
strerror(errno));
err:
ksft_inc_fail_cnt();
}
if (ksft_get_fail_cnt())
ksft_exit_fail();
ksft_exit_pass();
}