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