-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaccessor.c
188 lines (171 loc) · 3.34 KB
/
faccessor.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <unistd.h>
static void usage(const char *cmd)
{
fprintf(stderr, "Usage: %s <filepath>\n", cmd);
}
static int my_open(const char *filepath);
static void my_close(int d);
static long my_read(int fd);
static void my_write(int fd, long buf);
static long *my_mmap(int fd);
static void my_munmap(long *p);
static void my_msync(long *p);
static void my_socketpair(int sv[2]);
static void my_sendfile(int fd_from, int sd_to);
static long my_recv(int sd);
int main(int argc, char *argv[])
{
if (2 != argc) {
usage(argv[0]);
return 1;
}
const char *filepath = argv[1];
int fd = my_open(filepath);
char c;
long *p;
int sv[2];
while (1)
switch (c = getchar()) {
case '\n':
break;
case 'q':
printf("Bye.\n");
return 0;
case 'r':
p = my_mmap(fd);
printf("mem-read the first word: %lx\n", *p);
my_munmap(p);
break;
case 'w':
p = my_mmap(fd);
*p = 0xbeaf1;
my_munmap(p);
printf("mem-written to the first word\n");
break;
case 's':
p = my_mmap(fd);
*p = 0xbeaf2;
my_msync(p);
my_munmap(p);
printf("mem-written to the first word with msync\n");
break;
case 'W':
my_write(fd, 0xbeaf3);
printf("write(2)'ed to the first word\n");
break;
case 'R':
printf("read(2)'ed the first word: %lx\n", my_read(fd));
break;
case 'S':
my_socketpair(sv);
my_sendfile(fd, sv[0]);
my_close(sv[0]);
printf("sendfile(2)'ed the first word: %lx\n", my_recv(sv[1]));
my_close(sv[1]);
break;
default:
printf("Unsupported input '%d'\n", c);
break;
}
return 0;
}
static int my_open(const char *filepath)
{
int fd = open(filepath, O_RDWR);
if (-1 == fd) {
perror("open()");
exit(EXIT_FAILURE);
}
return fd;
}
static void my_close(int d)
{
if (-1 == close(d)) {
perror("close()");
exit(EXIT_FAILURE);
}
}
static void seekzero(int fd)
{
if (-1 == lseek(fd, 0, SEEK_SET)) {
perror("lseek()");
exit(EXIT_FAILURE);
}
}
static long my_read(int fd)
{
long buf;
if (-1 == read(fd, &buf, sizeof(buf))) {
perror("read()");
exit(EXIT_FAILURE);
}
seekzero(fd);
return buf;
}
static void my_write(int fd, long buf)
{
if (-1 == write(fd, &buf, sizeof(buf))) {
perror("write()");
exit(EXIT_FAILURE);
}
seekzero(fd);
}
static long *my_mmap(int fd)
{
void *p = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (MAP_FAILED == p) {
perror("mmap()");
exit(EXIT_FAILURE);
}
return (long *)p;
}
static void my_munmap(long *p)
{
if (-1 == munmap(p, 4096)) {
perror("munmap()");
exit(EXIT_FAILURE);
}
}
static void my_msync(long *p)
{
if (-1 == msync(p, 4096, MS_SYNC)) {
perror("msync()");
exit(EXIT_FAILURE);
}
}
static void my_socketpair(int sv[2])
{
if (-1 == socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
perror("socketpair()");
exit(EXIT_FAILURE);
}
}
static void my_sendfile(int fd_from, int sd_to)
{
off_t off = 0;
if (-1 == sendfile(sd_to, fd_from, &off, 100)) {
perror("sendfile()");
exit(EXIT_FAILURE);
}
assert(100 == off);
seekzero(fd_from);
}
static long my_recv(int sd)
{
long buf;
if (-1 == recv(sd, &buf, sizeof(buf), 0)) {
perror("recv()");
exit(EXIT_FAILURE);
}
return buf;
}