1313498Sngie/*	$NetBSD: rumpnfsd.c,v 1.9 2015/11/08 02:45:16 christos Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie *
6272343Sngie * Redistribution and use in source and binary forms, with or without
7272343Sngie * modification, are permitted provided that the following conditions
8272343Sngie * are met:
9272343Sngie * 1. Redistributions of source code must retain the above copyright
10272343Sngie *    notice, this list of conditions and the following disclaimer.
11272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie *    notice, this list of conditions and the following disclaimer in the
13272343Sngie *    documentation and/or other materials provided with the distribution.
14272343Sngie *
15272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16272343Sngie * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17272343Sngie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18272343Sngie * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21272343Sngie * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25272343Sngie * SUCH DAMAGE.
26272343Sngie */
27272343Sngie
28272343Sngie#include <sys/types.h>
29272343Sngie
30272343Sngie#include <dlfcn.h>
31272343Sngie#include <err.h>
32272343Sngie#include <errno.h>
33272343Sngie#include <pthread.h>
34272343Sngie#include <semaphore.h>
35272343Sngie#include <stdio.h>
36272343Sngie#include <stdlib.h>
37272343Sngie#include <string.h>
38272343Sngie#include <syslog.h>
39272343Sngie#include <unistd.h>
40313498Sngie#include <rpc/rpc.h>
41272343Sngie
42272343Sngievoid *mountd_main(void *);
43272343Sngievoid *rpcbind_main(void *);
44272343Sngieint nfsd_main(int, char **);
45272343Sngie
46272343Sngiesem_t gensem;
47272343Sngie
48272343Sngie#include "../../../net/config/netconfig.c"
49272343Sngie#include "../../common/h_fsmacros.h"
50272343Sngie#include "svc_fdset.h"
51272343Sngie
52272343Sngie#include <rump/rump.h>
53272343Sngie#include <rump/rump_syscalls.h>
54272343Sngie
55272343Sngieint
56272343Sngiemain(int argc, char *argv[])
57272343Sngie{
58272343Sngie	const char *ethername, *ethername_ro;
59272343Sngie	const char *serveraddr, *serveraddr_ro;
60272343Sngie	const char *netmask;
61272343Sngie	const char *exportpath;
62272343Sngie	const char *imagename;
63272343Sngie	char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
64272343Sngie	void *fsarg;
65272343Sngie	pthread_t t;
66272343Sngie	int rv;
67272343Sngie
68272343Sngie	/* for netcfg */
69272343Sngie	noatf = 1;
70272343Sngie
71272343Sngie	/* use defaults? */
72272343Sngie	if (argc == 1) {
73272343Sngie		ethername = "etherbus";
74272343Sngie		ethername_ro = "etherbus_ro";
75272343Sngie		serveraddr = "10.3.2.1";
76272343Sngie		serveraddr_ro = "10.4.2.1";
77272343Sngie		netmask = "255.255.255.0";
78272343Sngie		exportpath = "/myexport";
79272343Sngie		imagename = "ffs.img";
80272343Sngie	} else {
81272343Sngie		ethername = argv[1];
82272343Sngie		ethername_ro = argv[2];
83272343Sngie		serveraddr = argv[3];
84272343Sngie		serveraddr_ro = argv[4];
85272343Sngie		netmask = argv[5];
86272343Sngie		exportpath = argv[6];
87272343Sngie		imagename = argv[7];
88272343Sngie	}
89272343Sngie
90272343Sngie	rump_init();
91313498Sngie	svc_fdset_init(SVC_FDSET_MT);
92272343Sngie
93272343Sngie	rv = rump_pub_etfs_register("/etc/exports", "./exports", RUMP_ETFS_REG);
94272343Sngie	if (rv) {
95272343Sngie		errx(1, "register /etc/exports: %s", strerror(rv));
96272343Sngie	}
97272343Sngie
98272343Sngie	/* mini-mtree for mountd */
99272343Sngie	static const char *const dirs[] = { "/var", "/var/run", "/var/db" };
100272343Sngie	for (size_t i = 0; i < __arraycount(dirs); i++)
101272343Sngie		if (rump_sys_mkdir(dirs[i], 0777) == -1)
102272343Sngie			err(1, "can't mkdir `%s'", dirs[i]);
103272343Sngie
104272343Sngie	if (ffs_fstest_newfs(NULL, &fsarg,
105272343Sngie	    imagename, FSTEST_IMGSIZE, NULL) != 0)
106272343Sngie		err(1, "newfs failed");
107272343Sngie	if (ffs_fstest_mount(NULL, fsarg, exportpath, 0) != 0)
108272343Sngie		err(1, "mount failed");
109272343Sngie
110272343Sngie#if 0
111272343Sngie	/*
112272343Sngie	 * Serve from host instead of dedicated mount?
113272343Sngie	 * THIS IS MORE EVIL THAN MURRAY THE DEMONIC TALKING SKULL!
114272343Sngie	 */
115272343Sngie
116272343Sngie	if (ukfs_modload("/usr/lib/librumpfs_syspuffs.so") < 1)
117272343Sngie		errx(1, "modload");
118272343Sngie
119272343Sngie	mount_syspuffs_parseargs(__arraycount(pnullarg), pnullarg,
120272343Sngie	    &args, &mntflags, canon_dev, canon_dir);
121272343Sngie	if ((ukfs = ukfs_mount(MOUNT_PUFFS, "/", UKFS_DEFAULTMP, MNT_RDONLY,
122272343Sngie	    &args, sizeof(args))) == NULL)
123272343Sngie		err(1, "mount");
124272343Sngie
125272343Sngie	if (ukfs_modload("/usr/lib/librumpfs_nfsserver.so") < 1)
126272343Sngie		errx(1, "modload");
127272343Sngie#endif
128272343Sngie
129272343Sngie	if (sem_init(&gensem, 1, 0) == -1)
130272343Sngie		err(1, "gensem init");
131272343Sngie
132272343Sngie	/* create interface */
133272343Sngie	netcfg_rump_makeshmif(ethername, ifname);
134272343Sngie	netcfg_rump_if(ifname, serveraddr, netmask);
135272343Sngie
136272343Sngie	netcfg_rump_makeshmif(ethername_ro, ifname_ro);
137272343Sngie	netcfg_rump_if(ifname_ro, serveraddr_ro, netmask);
138272343Sngie
139272343Sngie	/*
140272343Sngie	 * No syslogging, thanks.
141272343Sngie	 * XXX: "0" does not modify the mask, so pick something
142272343Sngie	 * which is unlikely to cause any logging
143272343Sngie	 */
144272343Sngie	setlogmask(0x10000000);
145272343Sngie
146272343Sngie	if (pthread_create(&t, NULL, rpcbind_main, NULL) == -1)
147272343Sngie		err(1, "rpcbind");
148272343Sngie	sem_wait(&gensem);
149272343Sngie
150272343Sngie	if (pthread_create(&t, NULL, mountd_main, NULL) == -1)
151272343Sngie		err(1, "mountd");
152272343Sngie	sem_wait(&gensem);
153272343Sngie
154272343Sngie	rv = 0;
155272343Sngie	/* signal the other process we're almost done */
156272343Sngie	if (write(3, &rv, 4) != 4)
157272343Sngie		errx(1, "magic write failed");
158272343Sngie
159272343Sngie	{
160272343Sngie	char *nfsargv[] = { __UNCONST("nfsd"), NULL };
161272343Sngie	nfsd_main(1, nfsargv);
162272343Sngie	}
163272343Sngie	/*NOTREACHED*/
164272343Sngie
165272343Sngie	return 0;
166272343Sngie}
167