1*0a6a1f1dSLionel Sambuc /* $NetBSD: rumpnfsd.c,v 1.8 2014/05/12 15:31:07 christos Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc *
611be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
711be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
811be35a1SLionel Sambuc * are met:
911be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1011be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1111be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1211be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1311be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1411be35a1SLionel Sambuc *
1511be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1611be35a1SLionel Sambuc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1711be35a1SLionel Sambuc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1811be35a1SLionel Sambuc * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1911be35a1SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2011be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2111be35a1SLionel Sambuc * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2211be35a1SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2311be35a1SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2411be35a1SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2511be35a1SLionel Sambuc * SUCH DAMAGE.
2611be35a1SLionel Sambuc */
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambuc #include <sys/types.h>
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #include <dlfcn.h>
3111be35a1SLionel Sambuc #include <err.h>
3211be35a1SLionel Sambuc #include <errno.h>
3311be35a1SLionel Sambuc #include <pthread.h>
3411be35a1SLionel Sambuc #include <semaphore.h>
3511be35a1SLionel Sambuc #include <stdio.h>
3611be35a1SLionel Sambuc #include <stdlib.h>
3711be35a1SLionel Sambuc #include <string.h>
3811be35a1SLionel Sambuc #include <syslog.h>
3911be35a1SLionel Sambuc #include <unistd.h>
4011be35a1SLionel Sambuc
4111be35a1SLionel Sambuc void *mountd_main(void *);
4211be35a1SLionel Sambuc void *rpcbind_main(void *);
4311be35a1SLionel Sambuc int nfsd_main(int, char **);
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambuc sem_t gensem;
4611be35a1SLionel Sambuc
4711be35a1SLionel Sambuc #include "../../../net/config/netconfig.c"
4811be35a1SLionel Sambuc #include "../../common/h_fsmacros.h"
4911be35a1SLionel Sambuc #include "svc_fdset.h"
5011be35a1SLionel Sambuc
5111be35a1SLionel Sambuc #include <rump/rump.h>
5211be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
5311be35a1SLionel Sambuc
5411be35a1SLionel Sambuc int
main(int argc,char * argv[])5511be35a1SLionel Sambuc main(int argc, char *argv[])
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc const char *ethername, *ethername_ro;
5811be35a1SLionel Sambuc const char *serveraddr, *serveraddr_ro;
5911be35a1SLionel Sambuc const char *netmask;
6011be35a1SLionel Sambuc const char *exportpath;
6111be35a1SLionel Sambuc const char *imagename;
6211be35a1SLionel Sambuc char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
6311be35a1SLionel Sambuc void *fsarg;
6411be35a1SLionel Sambuc pthread_t t;
6511be35a1SLionel Sambuc int rv;
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc /* for netcfg */
6811be35a1SLionel Sambuc noatf = 1;
6911be35a1SLionel Sambuc
7011be35a1SLionel Sambuc /* use defaults? */
7111be35a1SLionel Sambuc if (argc == 1) {
7211be35a1SLionel Sambuc ethername = "etherbus";
7311be35a1SLionel Sambuc ethername_ro = "etherbus_ro";
7411be35a1SLionel Sambuc serveraddr = "10.3.2.1";
7511be35a1SLionel Sambuc serveraddr_ro = "10.4.2.1";
7611be35a1SLionel Sambuc netmask = "255.255.255.0";
7711be35a1SLionel Sambuc exportpath = "/myexport";
7811be35a1SLionel Sambuc imagename = "ffs.img";
7911be35a1SLionel Sambuc } else {
8011be35a1SLionel Sambuc ethername = argv[1];
8111be35a1SLionel Sambuc ethername_ro = argv[2];
8211be35a1SLionel Sambuc serveraddr = argv[3];
8311be35a1SLionel Sambuc serveraddr_ro = argv[4];
8411be35a1SLionel Sambuc netmask = argv[5];
8511be35a1SLionel Sambuc exportpath = argv[6];
8611be35a1SLionel Sambuc imagename = argv[7];
8711be35a1SLionel Sambuc }
8811be35a1SLionel Sambuc
8911be35a1SLionel Sambuc rump_init();
9011be35a1SLionel Sambuc init_fdsets();
9111be35a1SLionel Sambuc
9211be35a1SLionel Sambuc rv = rump_pub_etfs_register("/etc/exports", "./exports", RUMP_ETFS_REG);
9311be35a1SLionel Sambuc if (rv) {
9411be35a1SLionel Sambuc errx(1, "register /etc/exports: %s", strerror(rv));
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc /* mini-mtree for mountd */
98*0a6a1f1dSLionel Sambuc static const char *const dirs[] = { "/var", "/var/run", "/var/db" };
99*0a6a1f1dSLionel Sambuc for (size_t i = 0; i < __arraycount(dirs); i++)
100*0a6a1f1dSLionel Sambuc if (rump_sys_mkdir(dirs[i], 0777) == -1)
101*0a6a1f1dSLionel Sambuc err(1, "can't mkdir `%s'", dirs[i]);
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc if (ffs_fstest_newfs(NULL, &fsarg,
10411be35a1SLionel Sambuc imagename, FSTEST_IMGSIZE, NULL) != 0)
10511be35a1SLionel Sambuc err(1, "newfs failed");
10611be35a1SLionel Sambuc if (ffs_fstest_mount(NULL, fsarg, exportpath, 0) != 0)
10711be35a1SLionel Sambuc err(1, "mount failed");
10811be35a1SLionel Sambuc
10911be35a1SLionel Sambuc #if 0
11011be35a1SLionel Sambuc /*
11111be35a1SLionel Sambuc * Serve from host instead of dedicated mount?
11211be35a1SLionel Sambuc * THIS IS MORE EVIL THAN MURRAY THE DEMONIC TALKING SKULL!
11311be35a1SLionel Sambuc */
11411be35a1SLionel Sambuc
11511be35a1SLionel Sambuc if (ukfs_modload("/usr/lib/librumpfs_syspuffs.so") < 1)
11611be35a1SLionel Sambuc errx(1, "modload");
11711be35a1SLionel Sambuc
11811be35a1SLionel Sambuc mount_syspuffs_parseargs(__arraycount(pnullarg), pnullarg,
11911be35a1SLionel Sambuc &args, &mntflags, canon_dev, canon_dir);
12011be35a1SLionel Sambuc if ((ukfs = ukfs_mount(MOUNT_PUFFS, "/", UKFS_DEFAULTMP, MNT_RDONLY,
12111be35a1SLionel Sambuc &args, sizeof(args))) == NULL)
12211be35a1SLionel Sambuc err(1, "mount");
12311be35a1SLionel Sambuc
12411be35a1SLionel Sambuc if (ukfs_modload("/usr/lib/librumpfs_nfsserver.so") < 1)
12511be35a1SLionel Sambuc errx(1, "modload");
12611be35a1SLionel Sambuc #endif
12711be35a1SLionel Sambuc
12811be35a1SLionel Sambuc if (sem_init(&gensem, 1, 0) == -1)
12911be35a1SLionel Sambuc err(1, "gensem init");
13011be35a1SLionel Sambuc
13111be35a1SLionel Sambuc /* create interface */
13211be35a1SLionel Sambuc netcfg_rump_makeshmif(ethername, ifname);
13311be35a1SLionel Sambuc netcfg_rump_if(ifname, serveraddr, netmask);
13411be35a1SLionel Sambuc
13511be35a1SLionel Sambuc netcfg_rump_makeshmif(ethername_ro, ifname_ro);
13611be35a1SLionel Sambuc netcfg_rump_if(ifname_ro, serveraddr_ro, netmask);
13711be35a1SLionel Sambuc
13811be35a1SLionel Sambuc /*
13911be35a1SLionel Sambuc * No syslogging, thanks.
14011be35a1SLionel Sambuc * XXX: "0" does not modify the mask, so pick something
14111be35a1SLionel Sambuc * which is unlikely to cause any logging
14211be35a1SLionel Sambuc */
14311be35a1SLionel Sambuc setlogmask(0x10000000);
14411be35a1SLionel Sambuc
14511be35a1SLionel Sambuc if (pthread_create(&t, NULL, rpcbind_main, NULL) == -1)
14611be35a1SLionel Sambuc err(1, "rpcbind");
14711be35a1SLionel Sambuc sem_wait(&gensem);
14811be35a1SLionel Sambuc
14911be35a1SLionel Sambuc if (pthread_create(&t, NULL, mountd_main, NULL) == -1)
15011be35a1SLionel Sambuc err(1, "mountd");
15111be35a1SLionel Sambuc sem_wait(&gensem);
15211be35a1SLionel Sambuc
15311be35a1SLionel Sambuc rv = 0;
15411be35a1SLionel Sambuc /* signal the other process we're almost done */
15511be35a1SLionel Sambuc if (write(3, &rv, 4) != 4)
15611be35a1SLionel Sambuc errx(1, "magic write failed");
15711be35a1SLionel Sambuc
15811be35a1SLionel Sambuc {
15911be35a1SLionel Sambuc char *nfsargv[] = { __UNCONST("nfsd"), NULL };
16011be35a1SLionel Sambuc nfsd_main(1, nfsargv);
16111be35a1SLionel Sambuc }
16211be35a1SLionel Sambuc /*NOTREACHED*/
16311be35a1SLionel Sambuc
16411be35a1SLionel Sambuc return 0;
16511be35a1SLionel Sambuc }
166