t_basic.c revision 1.3 1 1.3 pooka /* $NetBSD: t_basic.c,v 1.3 2010/07/07 10:49:51 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka #include <sys/types.h>
4 1.1 pooka #include <sys/mount.h>
5 1.1 pooka #include <sys/socket.h>
6 1.1 pooka
7 1.1 pooka #include <assert.h>
8 1.1 pooka #include <atf-c.h>
9 1.1 pooka #include <err.h>
10 1.1 pooka #include <errno.h>
11 1.1 pooka #include <fcntl.h>
12 1.3 pooka #include <pthread.h>
13 1.1 pooka #include <puffs.h>
14 1.1 pooka #include <stdio.h>
15 1.1 pooka #include <unistd.h>
16 1.1 pooka #include <string.h>
17 1.1 pooka #include <stdlib.h>
18 1.1 pooka
19 1.1 pooka #include <rump/rump.h>
20 1.1 pooka #include <rump/rump_syscalls.h>
21 1.1 pooka
22 1.1 pooka #include "../../h_macros.h"
23 1.1 pooka
24 1.1 pooka struct puffs_args {
25 1.1 pooka uint8_t *us_pargs;
26 1.1 pooka size_t us_pargslen;
27 1.1 pooka
28 1.1 pooka int us_pflags;
29 1.1 pooka int us_servfd;
30 1.1 pooka pid_t us_childpid;
31 1.1 pooka };
32 1.1 pooka
33 1.1 pooka #define BUFSIZE (64*1024)
34 1.1 pooka
35 1.1 pooka struct thefds {
36 1.1 pooka int rumpfd;
37 1.1 pooka int servfd;
38 1.1 pooka };
39 1.1 pooka
40 1.1 pooka /*
41 1.1 pooka * Threads which shovel data between comfd and /dev/puffs.
42 1.1 pooka * (cannot use polling since fd's are in different namespaces)
43 1.1 pooka */
44 1.1 pooka static void *
45 1.1 pooka readshovel(void *arg)
46 1.1 pooka {
47 1.1 pooka struct thefds *fds = arg;
48 1.1 pooka char buf[BUFSIZE];
49 1.1 pooka ssize_t n;
50 1.1 pooka int error, comfd, puffsfd;
51 1.1 pooka
52 1.1 pooka comfd = fds->servfd;
53 1.1 pooka puffsfd = fds->rumpfd;
54 1.1 pooka
55 1.1 pooka /* use static thread id */
56 1.1 pooka rump_pub_lwp_alloc_and_switch(0, 0);
57 1.1 pooka
58 1.1 pooka for (;;) {
59 1.1 pooka ssize_t n, n2;
60 1.1 pooka
61 1.1 pooka n = rump_sys_read(puffsfd, buf, BUFSIZE);
62 1.1 pooka if (n <= 0) {
63 1.1 pooka break;
64 1.1 pooka }
65 1.1 pooka
66 1.1 pooka while (n) {
67 1.1 pooka n2 = write(comfd, buf, n);
68 1.1 pooka if (n2 == -1)
69 1.1 pooka err(1, "readshovel failed write: %d");
70 1.1 pooka if (n2 == 0)
71 1.1 pooka break;
72 1.1 pooka n -= n2;
73 1.1 pooka }
74 1.1 pooka }
75 1.1 pooka
76 1.1 pooka return NULL;
77 1.1 pooka }
78 1.1 pooka
79 1.1 pooka static void *
80 1.1 pooka writeshovel(void *arg)
81 1.1 pooka {
82 1.1 pooka struct thefds *fds = arg;
83 1.1 pooka struct putter_hdr *phdr;
84 1.1 pooka char buf[BUFSIZE];
85 1.1 pooka size_t toread;
86 1.1 pooka int error, comfd, puffsfd;
87 1.1 pooka
88 1.1 pooka /* use static thread id */
89 1.1 pooka rump_pub_lwp_alloc_and_switch(0, 0);
90 1.1 pooka
91 1.1 pooka comfd = fds->servfd;
92 1.1 pooka puffsfd = fds->rumpfd;
93 1.1 pooka
94 1.1 pooka phdr = (struct putter_hdr *)buf;
95 1.1 pooka
96 1.1 pooka for (;;) {
97 1.1 pooka off_t off;
98 1.1 pooka ssize_t n;
99 1.1 pooka
100 1.1 pooka /*
101 1.1 pooka * Need to write everything to the "kernel" in one chunk,
102 1.1 pooka * so make sure we have it here.
103 1.1 pooka */
104 1.1 pooka off = 0;
105 1.1 pooka toread = sizeof(struct putter_hdr);
106 1.1 pooka do {
107 1.1 pooka n = read(comfd, buf+off, toread);
108 1.1 pooka if (n <= 0) {
109 1.1 pooka break;
110 1.1 pooka }
111 1.1 pooka off += n;
112 1.1 pooka if (off >= sizeof(struct putter_hdr))
113 1.1 pooka toread = phdr->pth_framelen - off;
114 1.1 pooka else
115 1.1 pooka toread = off - sizeof(struct putter_hdr);
116 1.1 pooka } while (toread);
117 1.1 pooka
118 1.1 pooka n = rump_sys_write(puffsfd, buf, phdr->pth_framelen);
119 1.1 pooka if (n != phdr->pth_framelen)
120 1.1 pooka goto out;
121 1.1 pooka }
122 1.1 pooka
123 1.1 pooka out:
124 1.1 pooka return NULL;
125 1.1 pooka }
126 1.1 pooka
127 1.1 pooka static void
128 1.1 pooka rumpshovels(int rumpfd, int servfd)
129 1.1 pooka {
130 1.1 pooka struct thefds *fds;
131 1.1 pooka pthread_t pt;
132 1.1 pooka int rv;
133 1.1 pooka
134 1.1 pooka if ((rv = rump_init()) == -1)
135 1.1 pooka err(1, "rump_init");
136 1.1 pooka
137 1.1 pooka fds = malloc(sizeof(*fds));
138 1.1 pooka fds->rumpfd = rumpfd;
139 1.1 pooka fds->servfd = servfd;
140 1.1 pooka if (pthread_create(&pt, NULL, readshovel, fds) == -1)
141 1.1 pooka err(1, "read shovel");
142 1.1 pooka pthread_detach(pt);
143 1.1 pooka if (pthread_create(&pt, NULL, writeshovel, fds) == -1)
144 1.1 pooka err(1, "write shovel");
145 1.1 pooka pthread_detach(pt);
146 1.1 pooka }
147 1.1 pooka
148 1.1 pooka static int
149 1.1 pooka parseargs(int argc, char *argv[],
150 1.1 pooka struct puffs_args *args, int *mntflags,
151 1.1 pooka char *canon_dev, char *canon_dir)
152 1.1 pooka {
153 1.1 pooka pid_t childpid;
154 1.1 pooka pthread_t pt;
155 1.1 pooka int *pflags = &args->us_pflags;
156 1.1 pooka char comfd[16];
157 1.1 pooka int sv[2];
158 1.1 pooka size_t len;
159 1.1 pooka ssize_t n;
160 1.1 pooka int rv;
161 1.1 pooka
162 1.1 pooka /* Create sucketpair for communication with the real file server */
163 1.1 pooka if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
164 1.1 pooka err(1, "socketpair");
165 1.1 pooka
166 1.1 pooka switch ((childpid = fork())) {
167 1.1 pooka case 0:
168 1.1 pooka close(sv[1]);
169 1.1 pooka snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
170 1.1 pooka if (setenv("PUFFS_COMFD", comfd, 1) == -1)
171 1.3 pooka atf_tc_fail_errno("setenv");
172 1.1 pooka
173 1.1 pooka if (execvp(argv[0], argv) == -1)
174 1.3 pooka atf_tc_fail_errno("execvp");
175 1.1 pooka /*NOTREACHED*/
176 1.1 pooka case -1:
177 1.3 pooka atf_tc_fail_errno("fork");
178 1.1 pooka /*NOTREACHED*/
179 1.1 pooka default:
180 1.1 pooka close(sv[0]);
181 1.1 pooka break;
182 1.1 pooka }
183 1.1 pooka
184 1.1 pooka /* read args */
185 1.1 pooka if ((n = read(sv[1], &len, sizeof(len))) != sizeof(len))
186 1.1 pooka err(1, "mp 1 %zd", n);
187 1.1 pooka if (len > MAXPATHLEN)
188 1.1 pooka err(1, "mntpath > MAXPATHLEN");
189 1.1 pooka if ((size_t)read(sv[1], canon_dir, len) != len)
190 1.1 pooka err(1, "mp 2");
191 1.1 pooka if (read(sv[1], &len, sizeof(len)) != sizeof(len))
192 1.1 pooka err(1, "fn 1");
193 1.1 pooka if (len > MAXPATHLEN)
194 1.1 pooka err(1, "devpath > MAXPATHLEN");
195 1.1 pooka if ((size_t)read(sv[1], canon_dev, len) != len)
196 1.1 pooka err(1, "fn 2");
197 1.1 pooka if (read(sv[1], mntflags, sizeof(*mntflags)) != sizeof(*mntflags))
198 1.1 pooka err(1, "mntflags");
199 1.1 pooka if (read(sv[1], &args->us_pargslen, sizeof(args->us_pargslen)) != sizeof(args->us_pargslen))
200 1.1 pooka err(1, "puffs_args len");
201 1.1 pooka args->us_pargs = malloc(args->us_pargslen);
202 1.1 pooka if (args->us_pargs == NULL)
203 1.1 pooka err(1, "malloc");
204 1.1 pooka if (read(sv[1], args->us_pargs, args->us_pargslen) != args->us_pargslen)
205 1.1 pooka err(1, "puffs_args");
206 1.1 pooka if (read(sv[1], pflags, sizeof(*pflags)) != sizeof(*pflags))
207 1.1 pooka err(1, "pflags");
208 1.1 pooka
209 1.1 pooka args->us_childpid = childpid;
210 1.1 pooka args->us_servfd = sv[1];
211 1.1 pooka
212 1.1 pooka return 0;
213 1.1 pooka }
214 1.1 pooka
215 1.3 pooka #define dtfsmountv(a, b) \
216 1.3 pooka struct puffs_args pa; \
217 1.3 pooka dtfsmount1(a, __arraycount(b), b, atf_tc_get_config_var(tc, "srcdir"), &pa)
218 1.3 pooka static void
219 1.3 pooka dtfsmount1(const char *mp, int optcount, char *opts[], const char *srcdir,
220 1.3 pooka struct puffs_args *pa)
221 1.1 pooka {
222 1.1 pooka char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
223 1.3 pooka char dtfs_path[MAXPATHLEN], mountpath[MAXPATHLEN];
224 1.3 pooka char **dtfsargv;
225 1.1 pooka int mntflag;
226 1.3 pooka int rv, i;
227 1.1 pooka int fd;
228 1.1 pooka
229 1.3 pooka dtfsargv = malloc(sizeof(char *) * (optcount+3));
230 1.3 pooka
231 1.1 pooka /* build dtfs exec path for atf */
232 1.3 pooka sprintf(dtfs_path, "%s/h_dtfs/h_dtfs", srcdir);
233 1.3 pooka dtfsargv[0] = dtfs_path;
234 1.3 pooka
235 1.3 pooka for (i = 0; i < optcount; i++) {
236 1.3 pooka dtfsargv[i+1] = opts[i];
237 1.3 pooka }
238 1.1 pooka
239 1.3 pooka strlcpy(mountpath, mp, sizeof(mountpath));
240 1.3 pooka dtfsargv[optcount+1] = mountpath;
241 1.3 pooka dtfsargv[optcount+2] = NULL;
242 1.3 pooka
243 1.3 pooka rv = parseargs(optcount+3, dtfsargv,
244 1.3 pooka pa, &mntflag, canon_dev, canon_dir);
245 1.1 pooka if (rv)
246 1.1 pooka atf_tc_fail("comfd parseargs");
247 1.1 pooka
248 1.2 pooka rump_init();
249 1.1 pooka fd = rump_sys_open("/dev/puffs", O_RDWR);
250 1.1 pooka if (fd == -1)
251 1.1 pooka atf_tc_fail_errno("open puffs fd");
252 1.1 pooka #if 0
253 1.3 pooka pa->pa_fd = fd;
254 1.1 pooka #else
255 1.1 pooka assert(fd == 0); /* XXX: FIXME */
256 1.1 pooka #endif
257 1.1 pooka
258 1.3 pooka if (rump_sys_mkdir(mp, 0777) == -1)
259 1.1 pooka atf_tc_fail_errno("mkdir mountpoint");
260 1.1 pooka
261 1.3 pooka if (rump_sys_mount(MOUNT_PUFFS, mp, 0,
262 1.3 pooka pa->us_pargs, pa->us_pargslen) == -1)
263 1.1 pooka atf_tc_fail_errno("mount");
264 1.1 pooka
265 1.3 pooka rumpshovels(fd, pa->us_servfd);
266 1.3 pooka }
267 1.3 pooka
268 1.3 pooka ATF_TC(mount);
269 1.3 pooka ATF_TC_HEAD(mount, tc)
270 1.3 pooka {
271 1.3 pooka
272 1.3 pooka atf_tc_set_md_var(tc, "descr", "puffs+dtfs un/mount test");
273 1.3 pooka }
274 1.3 pooka
275 1.3 pooka ATF_TC_BODY(mount, tc)
276 1.3 pooka {
277 1.3 pooka char *myopts[] = {
278 1.3 pooka "dtfs",
279 1.3 pooka };
280 1.3 pooka
281 1.3 pooka dtfsmountv("/mp", myopts);
282 1.3 pooka if (rump_sys_unmount("/mp", 0) == -1)
283 1.3 pooka atf_tc_fail_errno("unmount");
284 1.3 pooka }
285 1.3 pooka
286 1.3 pooka ATF_TC(root_reg);
287 1.3 pooka ATF_TC_HEAD(root_reg, tc)
288 1.3 pooka {
289 1.3 pooka atf_tc_set_md_var(tc, "descr", "root is a regular file");
290 1.3 pooka }
291 1.3 pooka
292 1.3 pooka ATF_TC_BODY(root_reg, tc)
293 1.3 pooka {
294 1.3 pooka char *myopts[] = {
295 1.3 pooka "-r","reg",
296 1.3 pooka "dtfs",
297 1.3 pooka };
298 1.3 pooka int fd, rv;
299 1.3 pooka
300 1.3 pooka dtfsmountv("/mp", myopts);
301 1.3 pooka
302 1.3 pooka fd = rump_sys_open("/mp", O_RDWR);
303 1.3 pooka if (fd == -1)
304 1.3 pooka atf_tc_fail_errno("open root");
305 1.3 pooka if (rump_sys_write(fd, &fd, sizeof(fd)) != sizeof(fd))
306 1.3 pooka atf_tc_fail_errno("write to root");
307 1.3 pooka rv = rump_sys_mkdir("/mp/test", 0777);
308 1.3 pooka ATF_REQUIRE(errno == ENOTDIR);
309 1.3 pooka ATF_REQUIRE(rv == -1);
310 1.3 pooka rump_sys_close(fd);
311 1.1 pooka
312 1.1 pooka if (rump_sys_unmount("/mp", 0) == -1)
313 1.1 pooka atf_tc_fail_errno("unmount");
314 1.1 pooka }
315 1.1 pooka
316 1.3 pooka ATF_TC(root_lnk);
317 1.3 pooka ATF_TC_HEAD(root_lnk, tc)
318 1.3 pooka {
319 1.3 pooka
320 1.3 pooka atf_tc_set_md_var(tc, "descr", "root is a symbolic link");
321 1.3 pooka }
322 1.3 pooka
323 1.3 pooka #define LINKSTR "/path/to/nowhere"
324 1.3 pooka ATF_TC_BODY(root_lnk, tc)
325 1.3 pooka {
326 1.3 pooka char *myopts[] = {
327 1.3 pooka "-r", "lnk " LINKSTR,
328 1.3 pooka "-s",
329 1.3 pooka "dtfs",
330 1.3 pooka };
331 1.3 pooka char buf[PATH_MAX];
332 1.3 pooka ssize_t len;
333 1.3 pooka int rv;
334 1.3 pooka
335 1.3 pooka dtfsmountv("/mp", myopts);
336 1.3 pooka
337 1.3 pooka if ((len = rump_sys_readlink("/mp", buf, sizeof(buf)-1)) == -1)
338 1.3 pooka atf_tc_fail_errno("readlink");
339 1.3 pooka buf[len] = '\0';
340 1.3 pooka
341 1.3 pooka ATF_REQUIRE_STREQ(buf, LINKSTR);
342 1.3 pooka
343 1.3 pooka #if 0 /* XXX: unmount uses FOLLOW */
344 1.3 pooka if (rump_sys_unmount("/mp", 0) == -1)
345 1.3 pooka atf_tc_fail_errno("unmount");
346 1.3 pooka #endif
347 1.3 pooka
348 1.3 pooka /*
349 1.3 pooka * XXX2: due to atf issue #53, we must make sure the child dies
350 1.3 pooka * before we exit.
351 1.3 pooka */
352 1.3 pooka if (kill(pa.us_childpid, SIGTERM) == -1)
353 1.3 pooka err(1, "kill");
354 1.3 pooka }
355 1.3 pooka
356 1.3 pooka ATF_TC(root_fifo);
357 1.3 pooka ATF_TC_HEAD(root_fifo, tc)
358 1.3 pooka {
359 1.3 pooka
360 1.3 pooka atf_tc_set_md_var(tc, "descr", "root is a symbolic link");
361 1.3 pooka }
362 1.3 pooka
363 1.3 pooka #define MAGICSTR "nakit ja muusiperunat maustevoilla"
364 1.3 pooka static void *
365 1.3 pooka dofifow(void *arg)
366 1.3 pooka {
367 1.3 pooka int fd = (int)(uintptr_t)arg;
368 1.3 pooka char buf[512];
369 1.3 pooka
370 1.3 pooka printf("writing\n");
371 1.3 pooka strcpy(buf, MAGICSTR);
372 1.3 pooka if (rump_sys_write(fd, buf, strlen(buf)+1) != strlen(buf)+1)
373 1.3 pooka atf_tc_fail_errno("write to fifo");
374 1.3 pooka
375 1.3 pooka return NULL;
376 1.3 pooka }
377 1.3 pooka
378 1.3 pooka ATF_TC_BODY(root_fifo, tc)
379 1.3 pooka {
380 1.3 pooka char *myopts[] = {
381 1.3 pooka "-r", "fifo",
382 1.3 pooka "dtfs",
383 1.3 pooka };
384 1.3 pooka pthread_t pt;
385 1.3 pooka char buf[512];
386 1.3 pooka ssize_t len;
387 1.3 pooka int fd;
388 1.3 pooka
389 1.3 pooka dtfsmountv("/mp", myopts);
390 1.3 pooka fd = rump_sys_open("/mp", O_RDWR);
391 1.3 pooka if (fd == -1)
392 1.3 pooka atf_tc_fail_errno("open fifo");
393 1.3 pooka
394 1.3 pooka pthread_create(&pt, NULL, dofifow, (void *)(uintptr_t)fd);
395 1.3 pooka
396 1.3 pooka printf("reading\n");
397 1.3 pooka memset(buf, 0, sizeof(buf));
398 1.3 pooka if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
399 1.3 pooka atf_tc_fail_errno("read fifo");
400 1.3 pooka
401 1.3 pooka ATF_REQUIRE_STREQ(buf, MAGICSTR);
402 1.3 pooka
403 1.3 pooka rump_sys_close(fd);
404 1.3 pooka if (rump_sys_unmount("/mp", 0) == -1)
405 1.3 pooka atf_tc_fail_errno("unmount");
406 1.3 pooka }
407 1.3 pooka
408 1.3 pooka ATF_TC(root_chrdev);
409 1.3 pooka ATF_TC_HEAD(root_chrdev, tc)
410 1.3 pooka {
411 1.3 pooka
412 1.3 pooka atf_tc_set_md_var(tc, "descr", "root is /dev/null");
413 1.3 pooka }
414 1.3 pooka
415 1.3 pooka ATF_TC_BODY(root_chrdev, tc)
416 1.3 pooka {
417 1.3 pooka char *myopts[] = {
418 1.3 pooka "-r", "chr 2 0",
419 1.3 pooka "dtfs",
420 1.3 pooka };
421 1.3 pooka ssize_t rv;
422 1.3 pooka char buf[512];
423 1.3 pooka int fd;
424 1.3 pooka
425 1.3 pooka dtfsmountv("/mp", myopts);
426 1.3 pooka fd = rump_sys_open("/mp", O_RDWR);
427 1.3 pooka if (fd == -1)
428 1.3 pooka atf_tc_fail_errno("open null");
429 1.3 pooka
430 1.3 pooka rv = rump_sys_write(fd, buf, sizeof(buf));
431 1.3 pooka ATF_REQUIRE(rv == sizeof(buf));
432 1.3 pooka
433 1.3 pooka rv = rump_sys_read(fd, buf, sizeof(buf));
434 1.3 pooka ATF_REQUIRE(rv == 0);
435 1.3 pooka
436 1.3 pooka rump_sys_close(fd);
437 1.3 pooka if (rump_sys_unmount("/mp", 0) == -1)
438 1.3 pooka atf_tc_fail_errno("unmount");
439 1.3 pooka }
440 1.3 pooka
441 1.1 pooka ATF_TP_ADD_TCS(tp)
442 1.1 pooka {
443 1.3 pooka
444 1.1 pooka ATF_TP_ADD_TC(tp, mount);
445 1.1 pooka
446 1.3 pooka ATF_TP_ADD_TC(tp, root_fifo);
447 1.3 pooka ATF_TP_ADD_TC(tp, root_lnk);
448 1.3 pooka ATF_TP_ADD_TC(tp, root_reg);
449 1.3 pooka ATF_TP_ADD_TC(tp, root_chrdev);
450 1.3 pooka
451 1.1 pooka return atf_no_error();
452 1.1 pooka }
453