-
Notifications
You must be signed in to change notification settings - Fork 0
/
scullc_test.c
163 lines (156 loc) · 3.36 KB
/
scullc_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
/* 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/wait.h>
#include "kselftest.h"
struct test {
const char *const name;
const char *const dev;
size_t len;
size_t qset_size;
size_t quantum_size;
size_t qset_count;
};
static void test(const struct test *restrict t)
{
char buf[t->len > 80 ? t->len : 80];
char path[PATH_MAX];
int ret, fd;
long got;
FILE *fp;
ret = snprintf(path, sizeof(path), "/sys/devices/%s/qset_size",
t->dev);
if (ret < 0)
goto perr;
fp = fopen(path, "r");
if (!fp)
goto perr;
ret = fread(buf, sizeof(buf), 1, fp);
if (ret == 0 && ferror(fp))
goto perr;
if (fclose(fp) == -1)
goto perr;
got = strtol(buf, NULL, 10);
if (got != t->qset_size) {
fprintf(stderr, "%s: unexpected qset size:\n\t- want: %ld\n\t- got: %ld\n",
t->name, t->qset_size, got);
goto err;
}
ret = snprintf(path, sizeof(path), "/sys/devices/%s/quantum_size",
t->dev);
if (ret < 0)
goto perr;
fp = fopen(path, "r");
if (!fp)
goto perr;
ret = fread(buf, sizeof(buf), 1, fp);
if (ret == 0 && ferror(fp))
goto perr;
if (fclose(fp) == -1)
goto perr;
got = strtol(buf, NULL, 10);
if (got != t->quantum_size) {
fprintf(stderr, "%s: unexpected quantum size:\n\t- want: %ld\n\t- got: %ld\n",
t->name, t->quantum_size, got);
goto err;
}
ret = snprintf(path, sizeof(path), "/dev/%s", t->dev);
if (ret < 0)
goto perr;
fd = open(path, O_WRONLY);
if (fd == -1)
goto perr;
ret = write(fd, buf, t->len);
if (ret == -1)
goto perr;
if (ret != t->len) {
fprintf(stderr, "%s: unexpected write length:\n\t- want: %ld\n\t- got: %d\n",
t->name, t->len, ret);
goto err;
}
if (close(fd) == -1)
goto perr;
ret = snprintf(path, sizeof(path), "/sys/devices/%s/qset_count", t->dev);
if (ret < 0)
goto perr;
fp = fopen(path, "r");
if (!fp)
goto perr;
ret = fread(buf, sizeof(buf), 1, fp);
if (ret == 0 && ferror(fp))
goto perr;
if (fclose(fp) == -1)
goto perr;
got = strtol(buf, NULL, 10);
if (got != t->qset_count) {
fprintf(stderr, "%s: unexpected qset count:\n\t- want: %ld\n\t- got: %ld\n",
t->name, t->qset_count, got);
goto err;
}
exit(EXIT_SUCCESS);
perr:
perror(t->name);
err:
exit(EXIT_FAILURE);
}
int main(void)
{
const struct test *t, tests[] = {
{
.name = "write 1024 bytes to scullc0",
.dev = "scullc0",
.len = 1024,
.qset_size = 512,
.quantum_size = 4096,
.qset_count = (1024+1)/(512*4096)+1,
},
{
.name = "write 2048 bytes to scullc1",
.dev = "scullc1",
.len = 2048,
.qset_size = 512,
.quantum_size = 4096,
.qset_count = (1024+1)/(512*4096)+1,
},
{.name = NULL},
};
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 by %s\n",
t->name, strsignal(WTERMSIG(status)));
goto err;
}
if (!WIFEXITED(status)) {
fprintf(stderr, "%s: does not exit\n",
t->name);
goto err;
}
if (WEXITSTATUS(status))
goto err;
ksft_inc_pass_cnt();
continue;
perr:
perror(t->name);
err:
ksft_inc_fail_cnt();
}
if (ksft_get_fail_cnt())
ksft_exit_fail();
ksft_exit_pass();
}