-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_test.c
107 lines (100 loc) · 1.99 KB
/
proc_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
/* 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 *const name;
const char *const path;
int flags;
const char *const want;
};
static void test(const struct test *restrict t)
{
char buf[LINE_MAX];
FILE *fp;
int fd;
fd = open(t->path, t->flags);
if (fd == -1)
goto err;
if (!t->want)
goto out;
fp = fdopen(fd, "r");
if (fp == NULL)
goto err;
fread(buf, LINE_MAX, 1, fp);
if (ferror(fp))
goto err;
if (strcmp(buf, t->want)) {
fprintf(stderr, "%s: unexpected value:\n\t- want: '%s'\n\t- got: '%s'\n",
t->name, t->want, buf);
exit(EXIT_FAILURE);
}
out:
exit(EXIT_SUCCESS);
err:
fprintf(stderr, "%s: %s\n",
t->name, strerror(errno));
exit(EXIT_FAILURE);
}
int main(void)
{
const struct test *t, tests[] = {
{
.name = "/proc/driver/proc directory",
.path = "/proc/driver/proc",
.flags = O_RDONLY|O_DIRECTORY,
.want = NULL,
},
{
.name = "/proc/driver/proc/version file",
.path = "/proc/driver/proc/version",
.flags = O_RDONLY,
.want = "1.0.0\n",
},
{.name = NULL}, /* sentry */
};
for (t = tests; t->name; t++) {
int ret, status;
pid_t pid;
pid = fork();
if (pid == -1) {
fprintf(stderr, "%s: %s\n",
t->name, strerror(errno));
goto err;
} else if (pid == 0)
test(t);
ret = waitpid(pid, &status, 0);
if (ret == -1) {
fprintf(stderr, "%s: %s\n",
t->name, strerror(errno));
goto err;
}
if (WIFSIGNALED(status)) {
fprintf(stderr, "%s: signaled (%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;
err:
ksft_inc_fail_cnt();
}
if (ksft_get_fail_cnt())
ksft_exit_fail();
ksft_exit_pass();
}