fstest_nfs.c revision 1.3 1 1.3 pooka /* $NetBSD: fstest_nfs.c,v 1.3 2010/07/30 16:15:05 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka #include <sys/types.h>
29 1.1 pooka #include <sys/mount.h>
30 1.1 pooka #include <sys/socket.h>
31 1.1 pooka #include <sys/statvfs.h>
32 1.1 pooka #include <sys/wait.h>
33 1.1 pooka
34 1.1 pooka #include <assert.h>
35 1.1 pooka #include <atf-c.h>
36 1.1 pooka #include <err.h>
37 1.1 pooka #include <errno.h>
38 1.1 pooka #include <fcntl.h>
39 1.1 pooka #include <libgen.h>
40 1.1 pooka #include <pthread.h>
41 1.1 pooka #include <puffs.h>
42 1.1 pooka #include <puffsdump.h>
43 1.1 pooka #include <stdio.h>
44 1.1 pooka #include <unistd.h>
45 1.1 pooka #include <string.h>
46 1.1 pooka #include <stdlib.h>
47 1.1 pooka
48 1.1 pooka #include <rump/rump.h>
49 1.1 pooka #include <rump/rump_syscalls.h>
50 1.1 pooka
51 1.1 pooka #include "h_fsmacros.h"
52 1.1 pooka #include "mount_nfs.h"
53 1.1 pooka #include "../../net/config/netconfig.c"
54 1.1 pooka
55 1.1 pooka #define SERVERADDR "10.3.2.1"
56 1.1 pooka #define CLIENTADDR "10.3.2.2"
57 1.1 pooka #define NETNETMASK "255.255.255.0"
58 1.1 pooka #define EXPORTPATH "/myexport"
59 1.1 pooka
60 1.1 pooka static void
61 1.1 pooka childfail(int status)
62 1.1 pooka {
63 1.1 pooka
64 1.1 pooka atf_tc_fail("child died");
65 1.1 pooka }
66 1.1 pooka
67 1.1 pooka struct nfstestargs *theargs;
68 1.1 pooka
69 1.1 pooka /* fork rump nfsd, configure interface */
70 1.1 pooka int
71 1.1 pooka nfs_fstest_newfs(const atf_tc_t *tc, void **argp,
72 1.3 pooka const char *image, off_t size, void *fspriv)
73 1.1 pooka {
74 1.1 pooka const char *srcdir;
75 1.1 pooka char *nfsdargv[7];
76 1.1 pooka char nfsdpath[MAXPATHLEN];
77 1.1 pooka char ethername[MAXPATHLEN];
78 1.1 pooka char imagepath[MAXPATHLEN];
79 1.1 pooka char ifname[IFNAMSIZ];
80 1.1 pooka char cwd[MAXPATHLEN];
81 1.1 pooka struct nfstestargs *args;
82 1.1 pooka pid_t childpid;
83 1.1 pooka int pipes[2];
84 1.1 pooka int devnull;
85 1.1 pooka
86 1.1 pooka /*
87 1.1 pooka * First, we start the nfs service.
88 1.1 pooka */
89 1.1 pooka srcdir = atf_tc_get_config_var(tc, "srcdir");
90 1.1 pooka sprintf(nfsdpath, "%s/../nfs/nfsservice/rumpnfsd", srcdir);
91 1.1 pooka sprintf(ethername, "/%s/%s.etherbus", getcwd(cwd, sizeof(cwd)), image);
92 1.1 pooka sprintf(imagepath, "/%s/%s", cwd, image);
93 1.1 pooka
94 1.1 pooka nfsdargv[0] = nfsdpath;
95 1.1 pooka nfsdargv[1] = ethername;
96 1.1 pooka nfsdargv[2] = __UNCONST(SERVERADDR);
97 1.1 pooka nfsdargv[3] = __UNCONST(NETNETMASK);
98 1.1 pooka nfsdargv[4] = __UNCONST(EXPORTPATH);
99 1.1 pooka nfsdargv[5] = imagepath;
100 1.1 pooka nfsdargv[6] = NULL;
101 1.1 pooka
102 1.1 pooka signal(SIGCHLD, childfail);
103 1.1 pooka if (pipe(pipes) == -1)
104 1.1 pooka return errno;
105 1.1 pooka
106 1.1 pooka switch ((childpid = fork())) {
107 1.1 pooka case 0:
108 1.1 pooka if (chdir(dirname(nfsdpath)) == -1)
109 1.1 pooka err(1, "chdir");
110 1.1 pooka close(pipes[0]);
111 1.1 pooka if (dup2(pipes[1], 3) == -1)
112 1.1 pooka err(1, "dup2");
113 1.1 pooka if (execvp(nfsdargv[0], nfsdargv) == -1)
114 1.1 pooka err(1, "execvp");
115 1.1 pooka case -1:
116 1.1 pooka return errno;
117 1.1 pooka default:
118 1.1 pooka close(pipes[1]);
119 1.1 pooka break;
120 1.1 pooka }
121 1.1 pooka
122 1.1 pooka /*
123 1.1 pooka * Ok, nfsd has been run. The following sleep helps with the
124 1.1 pooka * theoretical problem that nfsd can't start fast enough to
125 1.1 pooka * process our mount request and we end up doing a timeout
126 1.1 pooka * before the mount. This would take several seconds. So
127 1.1 pooka * try to make sure nfsd is up&running already at this stage.
128 1.1 pooka */
129 1.1 pooka if (read(pipes[0], &devnull, 4) == -1)
130 1.1 pooka return errno;
131 1.1 pooka
132 1.1 pooka /*
133 1.1 pooka * Configure our networking interface.
134 1.1 pooka */
135 1.1 pooka rump_init();
136 1.1 pooka netcfg_rump_makeshmif(ethername, ifname);
137 1.1 pooka netcfg_rump_if(ifname, CLIENTADDR, NETNETMASK);
138 1.1 pooka
139 1.1 pooka /*
140 1.1 pooka * That's it. The rest is done in mount, since we don't have
141 1.1 pooka * the mountpath available here.
142 1.1 pooka */
143 1.1 pooka args = malloc(sizeof(*args));
144 1.1 pooka args->ta_childpid = childpid;
145 1.1 pooka strcpy(args->ta_ethername, ethername);
146 1.1 pooka
147 1.1 pooka *argp = args;
148 1.1 pooka theargs = args;
149 1.1 pooka
150 1.1 pooka return 0;
151 1.1 pooka }
152 1.1 pooka
153 1.1 pooka /* mount the file system */
154 1.1 pooka int
155 1.1 pooka nfs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
156 1.1 pooka {
157 1.1 pooka char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
158 1.1 pooka const char *nfscliargs[] = {
159 1.1 pooka "nfsclient",
160 1.1 pooka SERVERADDR ":" EXPORTPATH,
161 1.1 pooka path,
162 1.1 pooka NULL,
163 1.1 pooka };
164 1.1 pooka struct nfs_args args;
165 1.1 pooka int mntflags;
166 1.1 pooka
167 1.1 pooka if (rump_sys_mkdir(path, 0777) == -1)
168 1.1 pooka return errno;
169 1.1 pooka
170 1.1 pooka /* XXX: atf does not reset values */
171 1.1 pooka optind = 1;
172 1.1 pooka opterr = 1;
173 1.1 pooka
174 1.1 pooka mount_nfs_parseargs(__arraycount(nfscliargs)-1, __UNCONST(nfscliargs),
175 1.1 pooka &args, &mntflags, canon_dev, canon_dir);
176 1.1 pooka
177 1.1 pooka /*
178 1.1 pooka * We use nfs parseargs here, since as a side effect it
179 1.1 pooka * takes care of the RPC hulabaloo.
180 1.1 pooka */
181 1.1 pooka if (rump_sys_mount(MOUNT_NFS, path, flags, &args, sizeof(args)) == -1) {
182 1.1 pooka return errno;
183 1.1 pooka }
184 1.1 pooka
185 1.1 pooka return 0;
186 1.1 pooka }
187 1.1 pooka
188 1.1 pooka int
189 1.1 pooka nfs_fstest_delfs(const atf_tc_t *tc, void *arg)
190 1.1 pooka {
191 1.1 pooka return 0;
192 1.1 pooka
193 1.1 pooka }
194 1.1 pooka
195 1.1 pooka int
196 1.1 pooka nfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
197 1.1 pooka {
198 1.1 pooka struct nfstestargs *args = theargs;
199 1.2 pooka int status;
200 1.1 pooka
201 1.1 pooka if (rump_sys_unmount(path, flags) == -1) {
202 1.1 pooka return errno;
203 1.1 pooka }
204 1.1 pooka
205 1.1 pooka /*
206 1.1 pooka * It's highly expected that the child will die next, so we
207 1.1 pooka * don't need that information anymore thank you very many.
208 1.1 pooka */
209 1.1 pooka signal(SIGCHLD, SIG_IGN);
210 1.1 pooka
211 1.1 pooka /*
212 1.1 pooka * Just KILL it. Sending it SIGTERM first causes it to try
213 1.1 pooka * to send some unmount RPCs, leading to sticky situations.
214 1.1 pooka */
215 1.1 pooka kill(args->ta_childpid, SIGKILL);
216 1.2 pooka wait(&status);
217 1.1 pooka
218 1.1 pooka /* remove ethernet bus */
219 1.2 pooka if (unlink(args->ta_ethername) == -1)
220 1.2 pooka atf_tc_fail_errno("unlink ethername");
221 1.1 pooka
222 1.1 pooka return 0;
223 1.1 pooka }
224