-
Notifications
You must be signed in to change notification settings - Fork 10
/
nfstest2.c
339 lines (280 loc) · 7.1 KB
/
nfstest2.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
/*
Put this file to dovecot v1.0.x sources' root directory. Dovecot must be
compiled before this. The run:
gcc nfstest.c -o nfstest -g -Wall -W -DHAVE_CONFIG_H -I. -Isrc/lib src/lib/liblib.a
On machine 1 use:
./nfstest <port> <path to a test file>
On machine 2 use:
./nfstest <machine 1 hostname> <machine 1 port> <path to the same test file>
Don't use the NFS server as either machine 1 or 2!
*/
#include "lib.h"
#include "ioloop.h"
#include "fd-set-nonblock.h"
#include "read-full.h"
#include "write-full.h"
#include "network.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
static bool reverse = FALSE;
static void send_cmd(int fd, char cmd)
{
if (write_full(fd, &cmd, 1) < 0)
i_fatal("write() failed: %m");
}
static char read_cmd(int fd)
{
int ret;
char cmd;
ret = read_full(fd, &cmd, 1);
if (ret <= 0) {
if (ret == 0)
i_fatal("Connection lost");
i_fatal("read() failed: %m");
}
return cmd;
}
static void wait_cmd(int fd, char wanted_cmd)
{
char cmd;
cmd = read_cmd(fd);
if (wanted_cmd != cmd)
i_fatal("Unexpected command: %c != %c", cmd, wanted_cmd);
}
static int nfs_safe_open(const char *path, int flags)
{
int fd, i;
for (i = 0; i < 10; i++) {
fd = open(path, flags);
if (fd != -1 || errno != ESTALE)
break;
}
return fd;
}
static int nfs_safe_create(const char *path, int flags, int mode)
{
int fd, i;
for (i = 0; i < 10; i++) {
fd = open(path, flags, mode);
if (fd != -1 || errno != ESTALE)
break;
}
return fd;
}
static void nfs_test_link_server(int socket_fd, const char *path)
{
const char *temp_path, *lock_path;
char cmd, buf[100];
int ret;
int fd, fd2, last_server = FALSE;
temp_path = t_strdup_printf("%s.server", path);
lock_path = t_strdup_printf("%s.lock", path);
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
if (unlink(lock_path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", lock_path);
cmd = '0';
for (;;) {
fd = open(temp_path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("open(%s) failed: %m", path);
if (write(fd, "server", 6) < 0)
perror("write()");
unlink(".");
fd2 = open(path, O_RDONLY);
if (fd2 == -1) {
if (cmd != '0')
i_fatal("%s not found", path);
} else {
ret = read(fd2, buf, sizeof(buf));
if (ret < 0)
i_fatal("read(%s) failed: %m", path);
buf[ret] = '\0';
if (last_server) {
if (memcmp(buf, "server", 6) != 0)
i_error("wrong file, got: %s", buf);
} else {
if (memcmp(buf, "client", 6) != 0)
i_error("wrong file, got: %s", buf);
}
close(fd2);
}
if (link(temp_path, lock_path) < 0) {
if (errno != EEXIST) {
i_fatal("link(%s, %s) failed: %m",
temp_path, lock_path);
}
if (cmd != '3') {
i_error("link(%s, %s) failed: %m",
temp_path, lock_path);
}
if (close(fd) < 0)
i_fatal("close() failed: %m");
if (cmd == '3') {
send_cmd(socket_fd, '5');
wait_cmd(socket_fd, '6');
fd = open(path, O_RDONLY);
if (fd == -1)
i_fatal("open(%s) failed: %m", path);
ret = read(fd, buf, sizeof(buf));
if (ret < 0)
i_fatal("read(%s) failed: %m", path);
buf[ret] = '\0';
if (memcmp(buf, "client", 6) != 0)
i_error("wrong file, got: %s", buf);
close(fd);
last_server = FALSE;
cmd = '4';
}
continue;
}
if (unlink(temp_path) < 0)
i_fatal("unlink(%s) failed: %m", temp_path);
send_cmd(socket_fd, '1');
wait_cmd(socket_fd, '2');
if (rename(lock_path, path) < 0)
i_fatal("rename(%s, %s) failed: %m", lock_path, path);
last_server = TRUE;
if (close(fd) < 0)
i_fatal("close() failed: %m");
cmd = read_cmd(socket_fd);
}
}
static void nfs_test_link_client(int socket_fd, const char *path)
{
const char *temp_path, *lock_path;
int fd, remote = 0, local = 0;
time_t start, now, prev = 0;
struct stat st;
char cmd;
i_info("Testing link()..");
temp_path = t_strdup_printf("%s.client", path);
lock_path = t_strdup_printf("%s.lock", path);
fd = open(temp_path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("open(%s) failed: %m", path);
if (fstat(fd, &st) < 0)
i_fatal("fstat() failed: %m");
if (write(fd, "client", 6) != 6)
i_fatal("write() failed: %m");
start = time(NULL);
do {
wait_cmd(socket_fd, '1');
if (link(temp_path, lock_path) != -1 && errno == EEXIST)
i_fatal("broken: link() succeeded");
remote++;
send_cmd(socket_fd, '2');
if (rand() % 2 == 0)
usleep(200000);
if (link(temp_path, lock_path) == 0)
cmd = '3';
else if (errno != EEXIST)
i_fatal("broken: link() succeeded");
else
cmd = '4';
send_cmd(socket_fd, cmd);
if (cmd == '3') {
local++;
cmd = read_cmd(socket_fd);
if (cmd != '5')
i_fatal("broken: server's link() succeeded");
if (rename(lock_path, path) < 0)
i_fatal("unlink(%s) failed: %m", lock_path);
send_cmd(socket_fd, '6');
}
now = time(NULL);
if (prev != now) {
if (prev != 0)
i_info("%u remote, %u local", remote, local);
prev = now;
}
} while (now - start < 10);
if (close(fd) < 0)
i_fatal("close() failed: %m");
}
static void nfs_test_client(int fd, const char *path)
{
send_cmd(fd, 'a');
wait_cmd(fd, 'b');
i_info("Connected: client");
nfs_test_link_client(fd, path);
}
static void nfs_test_server(int fd, const char *path)
{
send_cmd(fd, 'b');
wait_cmd(fd, 'a');
i_info("Connected: server");
nfs_test_link_server(fd, path);
}
static void nfs_listen(unsigned int port, const char *path)
{
struct ip_addr ip;
int listen_fd, fd;
net_get_ip_any4(&ip);
listen_fd = net_listen(&ip, &port, 2);
if (listen_fd < 0)
i_fatal("net_listen(%d) failed: %m", port);
fd = net_accept(listen_fd, NULL, NULL);
if (fd < 0)
i_fatal("net_accept() failed: %m");
if (!reverse)
nfs_test_client(fd, path);
else
nfs_test_server(fd, path);
}
static void connect_finish(void *context)
{
struct ioloop *ioloop = context;
io_loop_stop(ioloop);
}
static void nfs_connect(const char *host, unsigned int port, const char *path)
{
struct ip_addr *ips;
unsigned int ips_count;
int fd, ret;
if ((ret = net_gethostbyname(host, &ips, &ips_count)) != 0) {
i_fatal("net_gethostbyname(%s) failed: %s",
host, net_gethosterror(ret));
}
fd = net_connect_ip(ips, port, NULL);
if (fd < 0) {
i_fatal("net_connect_ip(%s, %d) failed: %m",
net_ip2addr(ips), port);
}
fd_set_nonblock(fd, FALSE);
{
/* ugly.. net_connect_ip() forces nonblocking connection. */
struct ioloop *ioloop;
struct io *io;
ioloop = io_loop_create();
io = io_add(fd, IO_WRITE, connect_finish, ioloop);
io_loop_run(ioloop);
io_remove(&io);
io_loop_destroy(&ioloop);
}
if (!reverse)
nfs_test_server(fd, path);
else
nfs_test_client(fd, path);
}
int main(int argc, char *argv[])
{
lib_init();
if (argc > 1 && strcmp(argv[1], "-rev") == 0) {
/* reverse client and server roles. just to simplify bypassing
firewalls when testing different client kernels. */
argc--;
argv++;
reverse = TRUE;
}
if (argc == 3)
nfs_listen(atoi(argv[1]), argv[2]);
else if (argc == 4)
nfs_connect(argv[1], atoi(argv[2]), argv[3]);
else
i_fatal("Usage: nfstest [<host>] <port> <path>");
return 0;
}