Home | History | Annotate | Line # | Download | only in nfsservice
rumpnfsd.c revision 1.7
      1 /*	$NetBSD: rumpnfsd.c,v 1.7 2011/02/28 21:21:14 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 
     30 #include <dlfcn.h>
     31 #include <err.h>
     32 #include <errno.h>
     33 #include <pthread.h>
     34 #include <semaphore.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <syslog.h>
     39 #include <unistd.h>
     40 
     41 void *mountd_main(void *);
     42 void *rpcbind_main(void *);
     43 int nfsd_main(int, char **);
     44 
     45 sem_t gensem;
     46 
     47 #include "../../../net/config/netconfig.c"
     48 #include "../../common/h_fsmacros.h"
     49 #include "svc_fdset.h"
     50 
     51 #include <rump/rump.h>
     52 #include <rump/rump_syscalls.h>
     53 
     54 int
     55 main(int argc, char *argv[])
     56 {
     57 	const char *ethername, *ethername_ro;
     58 	const char *serveraddr, *serveraddr_ro;
     59 	const char *netmask;
     60 	const char *exportpath;
     61 	const char *imagename;
     62 	char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
     63 	void *fsarg;
     64 	pthread_t t;
     65 	int rv;
     66 
     67 	/* for netcfg */
     68 	noatf = 1;
     69 
     70 	/* use defaults? */
     71 	if (argc == 1) {
     72 		ethername = "etherbus";
     73 		ethername_ro = "etherbus_ro";
     74 		serveraddr = "10.3.2.1";
     75 		serveraddr_ro = "10.4.2.1";
     76 		netmask = "255.255.255.0";
     77 		exportpath = "/myexport";
     78 		imagename = "ffs.img";
     79 	} else {
     80 		ethername = argv[1];
     81 		ethername_ro = argv[2];
     82 		serveraddr = argv[3];
     83 		serveraddr_ro = argv[4];
     84 		netmask = argv[5];
     85 		exportpath = argv[6];
     86 		imagename = argv[7];
     87 	}
     88 
     89 	rump_init();
     90 	init_fdsets();
     91 
     92 	rv = rump_pub_etfs_register("/etc/exports", "./exports", RUMP_ETFS_REG);
     93 	if (rv) {
     94 		errx(1, "register /etc/exports: %s", strerror(rv));
     95 	}
     96 
     97 	/* mini-mtree for mountd */
     98 	rump_sys_mkdir("/var", 0777);
     99 	rump_sys_mkdir("/var/run", 0777);
    100 	rump_sys_mkdir("/var/db", 0777);
    101 
    102 	if (ffs_fstest_newfs(NULL, &fsarg,
    103 	    imagename, FSTEST_IMGSIZE, NULL) != 0)
    104 		err(1, "newfs failed");
    105 	if (ffs_fstest_mount(NULL, fsarg, exportpath, 0) != 0)
    106 		err(1, "mount failed");
    107 
    108 #if 0
    109 	/*
    110 	 * Serve from host instead of dedicated mount?
    111 	 * THIS IS MORE EVIL THAN MURRAY THE DEMONIC TALKING SKULL!
    112 	 */
    113 
    114 	if (ukfs_modload("/usr/lib/librumpfs_syspuffs.so") < 1)
    115 		errx(1, "modload");
    116 
    117 	mount_syspuffs_parseargs(__arraycount(pnullarg), pnullarg,
    118 	    &args, &mntflags, canon_dev, canon_dir);
    119 	if ((ukfs = ukfs_mount(MOUNT_PUFFS, "/", UKFS_DEFAULTMP, MNT_RDONLY,
    120 	    &args, sizeof(args))) == NULL)
    121 		err(1, "mount");
    122 
    123 	if (ukfs_modload("/usr/lib/librumpfs_nfsserver.so") < 1)
    124 		errx(1, "modload");
    125 #endif
    126 
    127 	if (sem_init(&gensem, 1, 0) == -1)
    128 		err(1, "gensem init");
    129 
    130 	/* create interface */
    131 	netcfg_rump_makeshmif(ethername, ifname);
    132 	netcfg_rump_if(ifname, serveraddr, netmask);
    133 
    134 	netcfg_rump_makeshmif(ethername_ro, ifname_ro);
    135 	netcfg_rump_if(ifname_ro, serveraddr_ro, netmask);
    136 
    137 	/*
    138 	 * No syslogging, thanks.
    139 	 * XXX: "0" does not modify the mask, so pick something
    140 	 * which is unlikely to cause any logging
    141 	 */
    142 	setlogmask(0x10000000);
    143 
    144 	if (pthread_create(&t, NULL, rpcbind_main, NULL) == -1)
    145 		err(1, "rpcbind");
    146 	sem_wait(&gensem);
    147 
    148 	if (pthread_create(&t, NULL, mountd_main, NULL) == -1)
    149 		err(1, "mountd");
    150 	sem_wait(&gensem);
    151 
    152 	rv = 0;
    153 	/* signal the other process we're almost done */
    154 	if (write(3, &rv, 4) != 4)
    155 		errx(1, "magic write failed");
    156 
    157 	{
    158 	char *nfsargv[] = { __UNCONST("nfsd"), NULL };
    159 	nfsd_main(1, nfsargv);
    160 	}
    161 	/*NOTREACHED*/
    162 
    163 	return 0;
    164 }
    165