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