1*11be35a1SLionel Sambuc /* $NetBSD: t_basic.c,v 1.3 2010/06/09 08:37:16 pooka Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/mount.h>
5*11be35a1SLionel Sambuc
6*11be35a1SLionel Sambuc #include <atf-c.h>
7*11be35a1SLionel Sambuc #include <err.h>
8*11be35a1SLionel Sambuc #include <errno.h>
9*11be35a1SLionel Sambuc #include <fcntl.h>
10*11be35a1SLionel Sambuc #include <stdio.h>
11*11be35a1SLionel Sambuc #include <unistd.h>
12*11be35a1SLionel Sambuc #include <string.h>
13*11be35a1SLionel Sambuc #include <stdlib.h>
14*11be35a1SLionel Sambuc
15*11be35a1SLionel Sambuc #include <rump/rump.h>
16*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
17*11be35a1SLionel Sambuc
18*11be35a1SLionel Sambuc #include <miscfs/nullfs/null.h>
19*11be35a1SLionel Sambuc #include <fs/tmpfs/tmpfs_args.h>
20*11be35a1SLionel Sambuc
21*11be35a1SLionel Sambuc #include "../../h_macros.h"
22*11be35a1SLionel Sambuc
23*11be35a1SLionel Sambuc ATF_TC(basic);
ATF_TC_HEAD(basic,tc)24*11be35a1SLionel Sambuc ATF_TC_HEAD(basic, tc)
25*11be35a1SLionel Sambuc {
26*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "basic nullfs functionality");
27*11be35a1SLionel Sambuc }
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #define MSTR "magic bus"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc static void
xput_tfile(const char * path,const char * mstr)32*11be35a1SLionel Sambuc xput_tfile(const char *path, const char *mstr)
33*11be35a1SLionel Sambuc {
34*11be35a1SLionel Sambuc int fd;
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
37*11be35a1SLionel Sambuc if (fd == -1)
38*11be35a1SLionel Sambuc atf_tc_fail_errno("create %s", path);
39*11be35a1SLionel Sambuc if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR))
40*11be35a1SLionel Sambuc atf_tc_fail_errno("write to testfile");
41*11be35a1SLionel Sambuc rump_sys_close(fd);
42*11be35a1SLionel Sambuc }
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc static int
xread_tfile(const char * path,const char * mstr)45*11be35a1SLionel Sambuc xread_tfile(const char *path, const char *mstr)
46*11be35a1SLionel Sambuc {
47*11be35a1SLionel Sambuc char buf[128];
48*11be35a1SLionel Sambuc int fd;
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc fd = rump_sys_open(path, O_RDONLY);
51*11be35a1SLionel Sambuc if (fd == -1)
52*11be35a1SLionel Sambuc return errno;
53*11be35a1SLionel Sambuc if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
54*11be35a1SLionel Sambuc atf_tc_fail_errno("read tfile");
55*11be35a1SLionel Sambuc rump_sys_close(fd);
56*11be35a1SLionel Sambuc if (strcmp(buf, MSTR) == 0)
57*11be35a1SLionel Sambuc return 0;
58*11be35a1SLionel Sambuc return EPROGMISMATCH;
59*11be35a1SLionel Sambuc }
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc static void
mountnull(const char * what,const char * mp,int flags)62*11be35a1SLionel Sambuc mountnull(const char *what, const char *mp, int flags)
63*11be35a1SLionel Sambuc {
64*11be35a1SLionel Sambuc struct null_args nargs;
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc memset(&nargs, 0, sizeof(nargs));
67*11be35a1SLionel Sambuc nargs.nulla_target = __UNCONST(what);
68*11be35a1SLionel Sambuc if (rump_sys_mount(MOUNT_NULL, mp, flags, &nargs, sizeof(nargs)) == -1)
69*11be35a1SLionel Sambuc atf_tc_fail_errno("could not mount nullfs");
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc
ATF_TC_BODY(basic,tc)73*11be35a1SLionel Sambuc ATF_TC_BODY(basic, tc)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc struct tmpfs_args targs;
76*11be35a1SLionel Sambuc struct stat sb;
77*11be35a1SLionel Sambuc int error;
78*11be35a1SLionel Sambuc
79*11be35a1SLionel Sambuc rump_init();
80*11be35a1SLionel Sambuc if (rump_sys_mkdir("/td1", 0777) == -1)
81*11be35a1SLionel Sambuc atf_tc_fail_errno("mp1");
82*11be35a1SLionel Sambuc if (rump_sys_mkdir("/td2", 0777) == -1)
83*11be35a1SLionel Sambuc atf_tc_fail_errno("mp1");
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc /* use tmpfs because rumpfs doesn't support regular files */
86*11be35a1SLionel Sambuc memset(&targs, 0, sizeof(targs));
87*11be35a1SLionel Sambuc targs.ta_version = TMPFS_ARGS_VERSION;
88*11be35a1SLionel Sambuc targs.ta_root_mode = 0777;
89*11be35a1SLionel Sambuc if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1)
90*11be35a1SLionel Sambuc atf_tc_fail_errno("could not mount tmpfs td1");
91*11be35a1SLionel Sambuc
92*11be35a1SLionel Sambuc mountnull("/td1", "/td2", 0);
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc /* test unnull -> null */
95*11be35a1SLionel Sambuc xput_tfile("/td1/tensti", "jeppe");
96*11be35a1SLionel Sambuc error = xread_tfile("/td2/tensti", "jeppe");
97*11be35a1SLionel Sambuc if (error != 0)
98*11be35a1SLionel Sambuc atf_tc_fail("null compare failed: %d (%s)",
99*11be35a1SLionel Sambuc error, strerror(error));
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc /* test null -> unnull */
102*11be35a1SLionel Sambuc xput_tfile("/td2/kiekko", "keppi");
103*11be35a1SLionel Sambuc error = xread_tfile("/td1/kiekko", "keppi");
104*11be35a1SLionel Sambuc if (error != 0)
105*11be35a1SLionel Sambuc atf_tc_fail("unnull compare failed: %d (%s)",
106*11be35a1SLionel Sambuc error, strerror(error));
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc /* test unnull -> null overwrite */
109*11be35a1SLionel Sambuc xput_tfile("/td1/tensti", "se oolannin sota");
110*11be35a1SLionel Sambuc error = xread_tfile("/td2/tensti", "se oolannin sota");
111*11be35a1SLionel Sambuc if (error != 0)
112*11be35a1SLionel Sambuc atf_tc_fail("unnull compare failed: %d (%s)",
113*11be35a1SLionel Sambuc error, strerror(error));
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc /* test that /td2 is unaffected in "real life" */
116*11be35a1SLionel Sambuc if (rump_sys_unmount("/td2", 0) == -1)
117*11be35a1SLionel Sambuc atf_tc_fail_errno("cannot unmount nullfs");
118*11be35a1SLionel Sambuc if ((error = rump_sys_stat("/td2/tensti", &sb)) != -1
119*11be35a1SLionel Sambuc || errno != ENOENT) {
120*11be35a1SLionel Sambuc atf_tc_fail("stat tensti should return ENOENT, got %d", error);
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc if ((error = rump_sys_stat("/td2/kiekko", &sb)) != -1
123*11be35a1SLionel Sambuc || errno != ENOENT) {
124*11be35a1SLionel Sambuc atf_tc_fail("stat kiekko should return ENOENT, got %d", error);
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc /* done */
128*11be35a1SLionel Sambuc }
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambuc ATF_TC(twistymount);
ATF_TC_HEAD(twistymount,tc)131*11be35a1SLionel Sambuc ATF_TC_HEAD(twistymount, tc)
132*11be35a1SLionel Sambuc {
133*11be35a1SLionel Sambuc
134*11be35a1SLionel Sambuc /* this is expected to fail until the PR is fixed */
135*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "\"recursive\" mounts deadlock"
136*11be35a1SLionel Sambuc " (kern/43439)");
137*11be35a1SLionel Sambuc }
138*11be35a1SLionel Sambuc
139*11be35a1SLionel Sambuc /*
140*11be35a1SLionel Sambuc * Mapping to identifiers in kern/43439:
141*11be35a1SLionel Sambuc * /td = /home/current/pkgsrc
142*11be35a1SLionel Sambuc * /td/dist = /home/current/pkgsrc/distiles
143*11be35a1SLionel Sambuc * /mp = /usr/pkgsrc
144*11be35a1SLionel Sambuc * /mp/dist = /usr/pkgsrc/distfiles -- "created" by first null mount
145*11be35a1SLionel Sambuc */
146*11be35a1SLionel Sambuc
ATF_TC_BODY(twistymount,tc)147*11be35a1SLionel Sambuc ATF_TC_BODY(twistymount, tc)
148*11be35a1SLionel Sambuc {
149*11be35a1SLionel Sambuc int mkd = 0;
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc rump_init();
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc if (rump_sys_mkdir("/td", 0777) == -1)
154*11be35a1SLionel Sambuc atf_tc_fail_errno("mkdir %d", mkd++);
155*11be35a1SLionel Sambuc if (rump_sys_mkdir("/td/dist", 0777) == -1)
156*11be35a1SLionel Sambuc atf_tc_fail_errno("mkdir %d", mkd++);
157*11be35a1SLionel Sambuc if (rump_sys_mkdir("/mp", 0777) == -1)
158*11be35a1SLionel Sambuc atf_tc_fail_errno("mkdir %d", mkd++);
159*11be35a1SLionel Sambuc
160*11be35a1SLionel Sambuc /* MNT_RDONLY doesn't matter, but just for compat with the PR */
161*11be35a1SLionel Sambuc mountnull("/td", "/mp", MNT_RDONLY);
162*11be35a1SLionel Sambuc mountnull("/td/dist", "/mp/dist", 0);
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc /* if we didn't get a locking-against-meself panic, we passed */
165*11be35a1SLionel Sambuc }
166*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)167*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc
170*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, basic);
171*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, twistymount);
172*11be35a1SLionel Sambuc
173*11be35a1SLionel Sambuc return atf_no_error();
174*11be35a1SLionel Sambuc }
175