1*aa97815bSkamil /* $NetBSD: rumpnfsd.c,v 1.11 2020/06/17 00:16:21 kamil Exp $ */
225f5a6a3Spooka
325f5a6a3Spooka /*-
425f5a6a3Spooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
525f5a6a3Spooka *
625f5a6a3Spooka * Redistribution and use in source and binary forms, with or without
725f5a6a3Spooka * modification, are permitted provided that the following conditions
825f5a6a3Spooka * are met:
925f5a6a3Spooka * 1. Redistributions of source code must retain the above copyright
1025f5a6a3Spooka * notice, this list of conditions and the following disclaimer.
1125f5a6a3Spooka * 2. Redistributions in binary form must reproduce the above copyright
1225f5a6a3Spooka * notice, this list of conditions and the following disclaimer in the
1325f5a6a3Spooka * documentation and/or other materials provided with the distribution.
1425f5a6a3Spooka *
1525f5a6a3Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1625f5a6a3Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1725f5a6a3Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1825f5a6a3Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1925f5a6a3Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2025f5a6a3Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2125f5a6a3Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2225f5a6a3Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2325f5a6a3Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2425f5a6a3Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2525f5a6a3Spooka * SUCH DAMAGE.
2625f5a6a3Spooka */
2725f5a6a3Spooka
2825f5a6a3Spooka #include <sys/types.h>
2925f5a6a3Spooka
3025f5a6a3Spooka #include <dlfcn.h>
3125f5a6a3Spooka #include <err.h>
3225f5a6a3Spooka #include <errno.h>
3325f5a6a3Spooka #include <pthread.h>
3425f5a6a3Spooka #include <semaphore.h>
3525f5a6a3Spooka #include <stdio.h>
3625f5a6a3Spooka #include <stdlib.h>
3725f5a6a3Spooka #include <string.h>
3825f5a6a3Spooka #include <syslog.h>
3925f5a6a3Spooka #include <unistd.h>
40eda86e94Schristos #include <rpc/rpc.h>
4125f5a6a3Spooka
4225f5a6a3Spooka void *mountd_main(void *);
4325f5a6a3Spooka void *rpcbind_main(void *);
4425f5a6a3Spooka int nfsd_main(int, char **);
4525f5a6a3Spooka
4625f5a6a3Spooka sem_t gensem;
4725f5a6a3Spooka
48422dd9f1Sjoerg #define RUMPNFSD_NOATF
4925f5a6a3Spooka #include "../../../net/config/netconfig.c"
5025f5a6a3Spooka #include "../../common/h_fsmacros.h"
5125f5a6a3Spooka #include "svc_fdset.h"
5225f5a6a3Spooka
5325f5a6a3Spooka #include <rump/rump.h>
54*aa97815bSkamil #include <rump/rump_syscallshotgun.h>
5525f5a6a3Spooka #include <rump/rump_syscalls.h>
5625f5a6a3Spooka
5725f5a6a3Spooka int
main(int argc,char * argv[])5825f5a6a3Spooka main(int argc, char *argv[])
5925f5a6a3Spooka {
609dec8514Spooka const char *ethername, *ethername_ro;
619dec8514Spooka const char *serveraddr, *serveraddr_ro;
6225f5a6a3Spooka const char *netmask;
6325f5a6a3Spooka const char *exportpath;
6425f5a6a3Spooka const char *imagename;
659dec8514Spooka char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
6625f5a6a3Spooka void *fsarg;
6725f5a6a3Spooka pthread_t t;
6825f5a6a3Spooka int rv;
6925f5a6a3Spooka
7025f5a6a3Spooka /* use defaults? */
7125f5a6a3Spooka if (argc == 1) {
7225f5a6a3Spooka ethername = "etherbus";
739dec8514Spooka ethername_ro = "etherbus_ro";
7425f5a6a3Spooka serveraddr = "10.3.2.1";
759dec8514Spooka serveraddr_ro = "10.4.2.1";
7625f5a6a3Spooka netmask = "255.255.255.0";
7725f5a6a3Spooka exportpath = "/myexport";
7825f5a6a3Spooka imagename = "ffs.img";
7925f5a6a3Spooka } else {
8025f5a6a3Spooka ethername = argv[1];
819dec8514Spooka ethername_ro = argv[2];
829dec8514Spooka serveraddr = argv[3];
839dec8514Spooka serveraddr_ro = argv[4];
849dec8514Spooka netmask = argv[5];
859dec8514Spooka exportpath = argv[6];
869dec8514Spooka imagename = argv[7];
8725f5a6a3Spooka }
8825f5a6a3Spooka
8925f5a6a3Spooka rump_init();
90eda86e94Schristos svc_fdset_init(SVC_FDSET_MT);
9125f5a6a3Spooka
9225f5a6a3Spooka rv = rump_pub_etfs_register("/etc/exports", "./exports", RUMP_ETFS_REG);
9325f5a6a3Spooka if (rv) {
9425f5a6a3Spooka errx(1, "register /etc/exports: %s", strerror(rv));
9525f5a6a3Spooka }
9625f5a6a3Spooka
9725f5a6a3Spooka /* mini-mtree for mountd */
9803a5ccb5Schristos static const char *const dirs[] = { "/var", "/var/run", "/var/db" };
9903a5ccb5Schristos for (size_t i = 0; i < __arraycount(dirs); i++)
10003a5ccb5Schristos if (rump_sys_mkdir(dirs[i], 0777) == -1)
10103a5ccb5Schristos err(1, "can't mkdir `%s'", dirs[i]);
10225f5a6a3Spooka
1039749b771Spooka if (ffs_fstest_newfs(NULL, &fsarg,
104a3acbf5eSpooka imagename, FSTEST_IMGSIZE, NULL) != 0)
105a3acbf5eSpooka err(1, "newfs failed");
1069749b771Spooka if (ffs_fstest_mount(NULL, fsarg, exportpath, 0) != 0)
107a3acbf5eSpooka err(1, "mount failed");
10825f5a6a3Spooka
10925f5a6a3Spooka #if 0
11025f5a6a3Spooka /*
11125f5a6a3Spooka * Serve from host instead of dedicated mount?
11225f5a6a3Spooka * THIS IS MORE EVIL THAN MURRAY THE DEMONIC TALKING SKULL!
11325f5a6a3Spooka */
11425f5a6a3Spooka
11525f5a6a3Spooka if (ukfs_modload("/usr/lib/librumpfs_syspuffs.so") < 1)
11625f5a6a3Spooka errx(1, "modload");
11725f5a6a3Spooka
11825f5a6a3Spooka mount_syspuffs_parseargs(__arraycount(pnullarg), pnullarg,
11925f5a6a3Spooka &args, &mntflags, canon_dev, canon_dir);
12025f5a6a3Spooka if ((ukfs = ukfs_mount(MOUNT_PUFFS, "/", UKFS_DEFAULTMP, MNT_RDONLY,
12125f5a6a3Spooka &args, sizeof(args))) == NULL)
12225f5a6a3Spooka err(1, "mount");
12325f5a6a3Spooka
12425f5a6a3Spooka if (ukfs_modload("/usr/lib/librumpfs_nfsserver.so") < 1)
12525f5a6a3Spooka errx(1, "modload");
12625f5a6a3Spooka #endif
12725f5a6a3Spooka
12825f5a6a3Spooka if (sem_init(&gensem, 1, 0) == -1)
12925f5a6a3Spooka err(1, "gensem init");
13025f5a6a3Spooka
13125f5a6a3Spooka /* create interface */
13225f5a6a3Spooka netcfg_rump_makeshmif(ethername, ifname);
13325f5a6a3Spooka netcfg_rump_if(ifname, serveraddr, netmask);
13425f5a6a3Spooka
1359dec8514Spooka netcfg_rump_makeshmif(ethername_ro, ifname_ro);
1369dec8514Spooka netcfg_rump_if(ifname_ro, serveraddr_ro, netmask);
1379dec8514Spooka
13825f5a6a3Spooka /*
13925f5a6a3Spooka * No syslogging, thanks.
14025f5a6a3Spooka * XXX: "0" does not modify the mask, so pick something
14125f5a6a3Spooka * which is unlikely to cause any logging
14225f5a6a3Spooka */
14325f5a6a3Spooka setlogmask(0x10000000);
14425f5a6a3Spooka
14525f5a6a3Spooka if (pthread_create(&t, NULL, rpcbind_main, NULL) == -1)
14625f5a6a3Spooka err(1, "rpcbind");
14725f5a6a3Spooka sem_wait(&gensem);
14825f5a6a3Spooka
14925f5a6a3Spooka if (pthread_create(&t, NULL, mountd_main, NULL) == -1)
15025f5a6a3Spooka err(1, "mountd");
15125f5a6a3Spooka sem_wait(&gensem);
15225f5a6a3Spooka
15325f5a6a3Spooka rv = 0;
15425f5a6a3Spooka /* signal the other process we're almost done */
15525f5a6a3Spooka if (write(3, &rv, 4) != 4)
15625f5a6a3Spooka errx(1, "magic write failed");
15725f5a6a3Spooka
15825f5a6a3Spooka {
15925f5a6a3Spooka char *nfsargv[] = { __UNCONST("nfsd"), NULL };
16025f5a6a3Spooka nfsd_main(1, nfsargv);
16125f5a6a3Spooka }
16225f5a6a3Spooka /*NOTREACHED*/
16325f5a6a3Spooka
16425f5a6a3Spooka return 0;
16525f5a6a3Spooka }
166