rumpnfsd.c revision 1.10 1 1.10 joerg /* $NetBSD: rumpnfsd.c,v 1.10 2020/04/23 00:31:51 joerg 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.10 joerg #define RUMPNFSD_NOATF
49 1.1 pooka #include "../../../net/config/netconfig.c"
50 1.1 pooka #include "../../common/h_fsmacros.h"
51 1.1 pooka #include "svc_fdset.h"
52 1.1 pooka
53 1.1 pooka #include <rump/rump.h>
54 1.1 pooka #include <rump/rump_syscalls.h>
55 1.1 pooka
56 1.1 pooka int
57 1.1 pooka main(int argc, char *argv[])
58 1.1 pooka {
59 1.6 pooka const char *ethername, *ethername_ro;
60 1.6 pooka const char *serveraddr, *serveraddr_ro;
61 1.1 pooka const char *netmask;
62 1.1 pooka const char *exportpath;
63 1.1 pooka const char *imagename;
64 1.6 pooka char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
65 1.1 pooka void *fsarg;
66 1.1 pooka pthread_t t;
67 1.1 pooka int rv;
68 1.1 pooka
69 1.1 pooka /* use defaults? */
70 1.1 pooka if (argc == 1) {
71 1.1 pooka ethername = "etherbus";
72 1.6 pooka ethername_ro = "etherbus_ro";
73 1.1 pooka serveraddr = "10.3.2.1";
74 1.6 pooka serveraddr_ro = "10.4.2.1";
75 1.1 pooka netmask = "255.255.255.0";
76 1.1 pooka exportpath = "/myexport";
77 1.1 pooka imagename = "ffs.img";
78 1.1 pooka } else {
79 1.1 pooka ethername = argv[1];
80 1.6 pooka ethername_ro = argv[2];
81 1.6 pooka serveraddr = argv[3];
82 1.6 pooka serveraddr_ro = argv[4];
83 1.6 pooka netmask = argv[5];
84 1.6 pooka exportpath = argv[6];
85 1.6 pooka imagename = argv[7];
86 1.1 pooka }
87 1.1 pooka
88 1.1 pooka rump_init();
89 1.9 christos svc_fdset_init(SVC_FDSET_MT);
90 1.1 pooka
91 1.1 pooka rv = rump_pub_etfs_register("/etc/exports", "./exports", RUMP_ETFS_REG);
92 1.1 pooka if (rv) {
93 1.1 pooka errx(1, "register /etc/exports: %s", strerror(rv));
94 1.1 pooka }
95 1.1 pooka
96 1.1 pooka /* mini-mtree for mountd */
97 1.8 christos static const char *const dirs[] = { "/var", "/var/run", "/var/db" };
98 1.8 christos for (size_t i = 0; i < __arraycount(dirs); i++)
99 1.8 christos if (rump_sys_mkdir(dirs[i], 0777) == -1)
100 1.8 christos err(1, "can't mkdir `%s'", dirs[i]);
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