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