1*6f4965e0Srillig /* $NetBSD: nfsd.c,v 1.74 2021/11/27 22:30:25 rillig Exp $ */
20114e805Scgd
361f28255Scgd /*
4625a6892Smycroft * Copyright (c) 1989, 1993, 1994
5625a6892Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Rick Macklem at The University of Guelph.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
35c80b3534Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
379c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
389c194566Slukem The Regents of the University of California. All rights reserved.");
39d594ce93Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #ifndef lint
420114e805Scgd #if 0
43f916611fSfvdl static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
440114e805Scgd #else
45*6f4965e0Srillig __RCSID("$NetBSD: nfsd.c,v 1.74 2021/11/27 22:30:25 rillig Exp $");
460114e805Scgd #endif
47f916611fSfvdl #endif /* not lint */
4861f28255Scgd
49625a6892Smycroft #include <sys/param.h>
5061f28255Scgd #include <sys/ioctl.h>
5161f28255Scgd #include <sys/stat.h>
5261f28255Scgd #include <sys/wait.h>
53625a6892Smycroft #include <sys/uio.h>
54625a6892Smycroft #include <sys/ucred.h>
5561f28255Scgd #include <sys/mount.h>
5661f28255Scgd #include <sys/socket.h>
5761f28255Scgd #include <sys/socketvar.h>
58f46e9218Sitojun #include <poll.h>
59625a6892Smycroft
6061f28255Scgd #include <rpc/rpc.h>
6161f28255Scgd #include <rpc/pmap_clnt.h>
6261f28255Scgd #include <rpc/pmap_prot.h>
63625a6892Smycroft
6461f28255Scgd #include <nfs/rpcv2.h>
65f916611fSfvdl #include <nfs/nfsproto.h>
66625a6892Smycroft #include <nfs/nfs.h>
67625a6892Smycroft
68625a6892Smycroft #include <err.h>
69625a6892Smycroft #include <errno.h>
70625a6892Smycroft #include <fcntl.h>
71625a6892Smycroft #include <grp.h>
72da700ef1Sgson #include <paths.h>
73625a6892Smycroft #include <pwd.h>
74cebe06e8Syamt #include <pthread.h>
75625a6892Smycroft #include <signal.h>
76625a6892Smycroft #include <stdio.h>
77625a6892Smycroft #include <stdlib.h>
7875a6e035Sperry #include <string.h>
79e174025cSmycroft #include <syslog.h>
80625a6892Smycroft #include <unistd.h>
812a2d6dd2Sfvdl #include <netdb.h>
8261f28255Scgd
83813c1f38Schristos #ifdef NFSD_RUMP
84813c1f38Schristos #include <rump/rump.h>
85aa97815bSkamil #include <rump/rump_syscallshotgun.h>
86813c1f38Schristos #include <rump/rump_syscalls.h>
87813c1f38Schristos
88813c1f38Schristos #define nfssvc(a, b) rump_sys_nfssvc((a), (b))
89813c1f38Schristos #define close(a) rump_sys_close(a)
90813c1f38Schristos #define poll(a, b, c) rump_sys_poll((a), (b), (c))
91813c1f38Schristos #if 0
92813c1f38Schristos #define socket(a, b, c) rump_sys_socket((a), (b), (c))
93813c1f38Schristos #define setsockopt(a, b, c, d, e) rump_sys_setsockopt((a), (b), (c), (d), (e))
94813c1f38Schristos #define bind(a, b, c) rump_sys_bind((a), (b), (c))
95813c1f38Schristos #define listen(a, b) rump_sys_listen((a), (b))
96813c1f38Schristos #define accept(a, b, c) rump_sys_accept((a), (b), (c))
97813c1f38Schristos #endif
98813c1f38Schristos #define main nfsd_main
99813c1f38Schristos int nfsd_main(int, char *[]);
100813c1f38Schristos #endif
101813c1f38Schristos
10261f28255Scgd /* Global defs */
103813c1f38Schristos #if defined(DEBUG) || defined(NFSD_RUMP)
1048138b1d0Sjoerg static int debug = 1;
10561f28255Scgd #else
1068138b1d0Sjoerg static int debug = 0;
10761f28255Scgd #endif
108625a6892Smycroft
1095cbe0011Schristos #define logit(e, s, args...) \
1105cbe0011Schristos do { \
1115cbe0011Schristos if (debug) { \
1125cbe0011Schristos fprintf(stderr,(s), ## args); \
1135cbe0011Schristos fprintf(stderr, "\n"); \
1145cbe0011Schristos } else { \
1155cbe0011Schristos syslog(e, s, ## args); \
1165cbe0011Schristos } \
117*6f4965e0Srillig } while (0)
1185cbe0011Schristos
1198138b1d0Sjoerg static void nonfs(int);
1208138b1d0Sjoerg __dead static void usage(void);
12161f28255Scgd
122cebe06e8Syamt static void *
worker(void * dummy)1232d18baefSpooka worker(void *dummy)
124cebe06e8Syamt {
125cebe06e8Syamt struct nfsd_srvargs nsd;
126cebe06e8Syamt int nfssvc_flag;
127cebe06e8Syamt
1280fc9d7eeSyamt pthread_setname_np(pthread_self(), "slave", NULL);
129cebe06e8Syamt nfssvc_flag = NFSSVC_NFSD;
130cebe06e8Syamt memset(&nsd, 0, sizeof(nsd));
13174f55248Schristos while (nfssvc(nfssvc_flag, &nsd) == -1) {
132cebe06e8Syamt if (errno != ENEEDAUTH) {
1335cbe0011Schristos logit(LOG_ERR, "nfssvc: %s", strerror(errno));
13474f55248Schristos exit(EXIT_FAILURE);
135cebe06e8Syamt }
136cebe06e8Syamt nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
137cebe06e8Syamt }
138cebe06e8Syamt
139cebe06e8Syamt return NULL;
140cebe06e8Syamt }
141cebe06e8Syamt
142eff76661Schristos struct conf {
143eff76661Schristos struct addrinfo *ai;
144eff76661Schristos struct netconfig *nc;
145eff76661Schristos struct netbuf nb;
146eff76661Schristos struct pollfd pfd;
147eff76661Schristos };
148eff76661Schristos
149eff76661Schristos #define NFS_UDP4 0
150eff76661Schristos #define NFS_TCP4 1
151eff76661Schristos #define NFS_UDP6 2
152eff76661Schristos #define NFS_TCP6 3
153eff76661Schristos
154eff76661Schristos static int cfg_family[] = { PF_INET, PF_INET, PF_INET6, PF_INET6 };
155eff76661Schristos static const char *cfg_netconf[] = { "udp", "tcp", "udp6", "tcp6" };
156eff76661Schristos static int cfg_socktype[] = {
157eff76661Schristos SOCK_DGRAM, SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM };
158eff76661Schristos static int cfg_protocol[] = {
159eff76661Schristos IPPROTO_UDP, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_TCP };
160eff76661Schristos
161eff76661Schristos static int
tryconf(struct conf * cfg,int t,int reregister)162eff76661Schristos tryconf(struct conf *cfg, int t, int reregister)
163eff76661Schristos {
164eff76661Schristos struct addrinfo hints;
165eff76661Schristos int ecode;
166eff76661Schristos
167eff76661Schristos memset(&hints, 0, sizeof hints);
168eff76661Schristos hints.ai_flags = AI_PASSIVE;
169eff76661Schristos hints.ai_family = cfg_family[t];
170eff76661Schristos hints.ai_socktype = cfg_socktype[t];
171eff76661Schristos hints.ai_protocol = cfg_protocol[t];
172eff76661Schristos
173eff76661Schristos ecode = getaddrinfo(NULL, "nfs", &hints, &cfg->ai);
174eff76661Schristos if (ecode != 0) {
1755cbe0011Schristos logit(LOG_ERR, "getaddrinfo %s: %s", cfg_netconf[t],
176eff76661Schristos gai_strerror(ecode));
177eff76661Schristos return -1;
178eff76661Schristos }
179eff76661Schristos
180eff76661Schristos cfg->nc = getnetconfigent(cfg_netconf[t]);
181eff76661Schristos
182eff76661Schristos if (cfg->nc == NULL) {
1835cbe0011Schristos logit(LOG_ERR, "getnetconfigent %s failed: %s",
184813c1f38Schristos cfg_netconf[t], strerror(errno));
185c3c4374aSchristos goto out;
186eff76661Schristos }
187eff76661Schristos
188eff76661Schristos cfg->nb.buf = cfg->ai->ai_addr;
189eff76661Schristos cfg->nb.len = cfg->nb.maxlen = cfg->ai->ai_addrlen;
190eff76661Schristos if (reregister)
191eff76661Schristos if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb)) {
1925cbe0011Schristos logit(LOG_ERR, "rpcb_set %s failed", cfg_netconf[t]);
193c3c4374aSchristos goto out1;
194eff76661Schristos }
195eff76661Schristos return 0;
196c3c4374aSchristos out1:
197c3c4374aSchristos freenetconfigent(cfg->nc);
198c3c4374aSchristos cfg->nc = NULL;
199c3c4374aSchristos out:
200c3c4374aSchristos freeaddrinfo(cfg->ai);
201c3c4374aSchristos cfg->ai = NULL;
202c3c4374aSchristos return -1;
203eff76661Schristos }
204eff76661Schristos
205eff76661Schristos static int
setupsock(struct conf * cfg,struct pollfd * set,int p)206eff76661Schristos setupsock(struct conf *cfg, struct pollfd *set, int p)
207eff76661Schristos {
208eff76661Schristos int sock;
209eff76661Schristos struct nfsd_args nfsdargs;
210eff76661Schristos struct addrinfo *ai = cfg->ai;
211eff76661Schristos int on = 1;
212eff76661Schristos
213eff76661Schristos sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
214eff76661Schristos
215eff76661Schristos if (sock == -1) {
2165cbe0011Schristos logit(LOG_ERR, "can't create %s socket: %s", cfg_netconf[p],
217813c1f38Schristos strerror(errno));
218eff76661Schristos return -1;
219eff76661Schristos }
220eff76661Schristos if (cfg_family[p] == PF_INET6) {
221eff76661Schristos if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on,
222eff76661Schristos sizeof(on)) == -1) {
2235cbe0011Schristos logit(LOG_ERR, "can't set v6-only binding for %s "
224813c1f38Schristos "socket: %s", cfg_netconf[p], strerror(errno));
225eff76661Schristos goto out;
226eff76661Schristos }
227eff76661Schristos }
228eff76661Schristos
229eff76661Schristos if (cfg_protocol[p] == IPPROTO_TCP) {
230eff76661Schristos if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
231eff76661Schristos sizeof(on)) == -1) {
2325cbe0011Schristos logit(LOG_ERR, "setsockopt SO_REUSEADDR for %s: %s",
233813c1f38Schristos cfg_netconf[p], strerror(errno));
234eff76661Schristos goto out;
235eff76661Schristos }
236eff76661Schristos }
237eff76661Schristos
238eff76661Schristos if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
2395cbe0011Schristos logit(LOG_ERR, "can't bind %s addr: %s", cfg_netconf[p],
240813c1f38Schristos strerror(errno));
241eff76661Schristos goto out;
242eff76661Schristos }
243eff76661Schristos
244eff76661Schristos if (cfg_protocol[p] == IPPROTO_TCP) {
245eff76661Schristos if (listen(sock, 5) == -1) {
2465cbe0011Schristos logit(LOG_ERR, "listen failed");
247eff76661Schristos goto out;
248eff76661Schristos }
249eff76661Schristos }
250eff76661Schristos
251eff76661Schristos if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb) ||
252eff76661Schristos !rpcb_set(RPCPROG_NFS, 3, cfg->nc, &cfg->nb)) {
2535cbe0011Schristos logit(LOG_ERR, "can't register with %s portmap",
254eff76661Schristos cfg_netconf[p]);
255eff76661Schristos goto out;
256eff76661Schristos }
257eff76661Schristos
258eff76661Schristos
259eff76661Schristos if (cfg_protocol[p] == IPPROTO_TCP)
260eff76661Schristos set->fd = sock;
261eff76661Schristos else {
262eff76661Schristos nfsdargs.sock = sock;
263eff76661Schristos nfsdargs.name = NULL;
264eff76661Schristos nfsdargs.namelen = 0;
26574f55248Schristos if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) == -1) {
2665cbe0011Schristos logit(LOG_ERR, "can't add %s socket: %s",
267813c1f38Schristos cfg_netconf[p], strerror(errno));
268eff76661Schristos goto out;
269eff76661Schristos }
270eff76661Schristos (void)close(sock);
271eff76661Schristos }
272eff76661Schristos return 0;
273eff76661Schristos out:
274eff76661Schristos (void)close(sock);
275eff76661Schristos return -1;
276eff76661Schristos }
277eff76661Schristos
27861f28255Scgd /*
279da700ef1Sgson * The functions daemon2_fork() and daemon2_detach() below provide
280da700ef1Sgson * functionality similar to daemon(3) but split into two phases.
281da700ef1Sgson * daemon2_fork() is called early, before creating resources that
282da700ef1Sgson * cannot be inherited across a fork, such as threads or kqueues.
283da700ef1Sgson * When the daemon is ready to provide service, daemon2_detach()
284da700ef1Sgson * is called to complete the daemonization and signal the parent
285da700ef1Sgson * process to exit.
286da700ef1Sgson *
287da700ef1Sgson * These functions could potentially be moved to a library and
288da700ef1Sgson * shared by other daemons.
289da700ef1Sgson *
290da700ef1Sgson * The return value from daemon2_fork() is a file descriptor to
291da700ef1Sgson * be passed as the first argument to daemon2_detach().
292da700ef1Sgson */
293da700ef1Sgson
294da700ef1Sgson static int
daemon2_fork(void)295da700ef1Sgson daemon2_fork(void)
296da700ef1Sgson {
297da700ef1Sgson int i;
298da700ef1Sgson int fd;
299da700ef1Sgson int r;
300da700ef1Sgson pid_t pid;
301da700ef1Sgson int detach_msg_pipe[2];
302da700ef1Sgson
303da700ef1Sgson /*
3047d8085e8Sjoerg * Set up a pipe for signalling the parent, making sure the
305da700ef1Sgson * write end does not get allocated one of the file
306da700ef1Sgson * descriptors that may be closed in daemon2_detach(). The
307da700ef1Sgson * read end does not need such protection.
308da700ef1Sgson */
309da700ef1Sgson for (i = 0; i < 3; i++) {
310da700ef1Sgson r = pipe2(detach_msg_pipe, O_CLOEXEC|O_NOSIGPIPE);
31174f55248Schristos if (r == -1)
312da700ef1Sgson return -1;
313da700ef1Sgson if (detach_msg_pipe[1] <= STDERR_FILENO &&
314da700ef1Sgson (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
315da700ef1Sgson (void)dup2(fd, detach_msg_pipe[0]);
316da700ef1Sgson (void)dup2(fd, detach_msg_pipe[1]);
317da700ef1Sgson if (fd > STDERR_FILENO)
318da700ef1Sgson (void)close(fd);
319da700ef1Sgson continue;
320da700ef1Sgson }
321da700ef1Sgson break;
322da700ef1Sgson }
323da700ef1Sgson
324da700ef1Sgson pid = fork();
325da700ef1Sgson switch (pid) {
326da700ef1Sgson case -1:
327da700ef1Sgson return -1;
328da700ef1Sgson case 0:
329da700ef1Sgson /* child */
330da700ef1Sgson (void)close(detach_msg_pipe[0]);
331da700ef1Sgson return detach_msg_pipe[1];
332da700ef1Sgson default:
333da700ef1Sgson break;
334da700ef1Sgson }
335da700ef1Sgson
336da700ef1Sgson /* Parent */
337da700ef1Sgson (void)close(detach_msg_pipe[1]);
338da700ef1Sgson
339da700ef1Sgson for (;;) {
340da700ef1Sgson ssize_t nread;
341da700ef1Sgson char dummy;
342da700ef1Sgson nread = read(detach_msg_pipe[0], &dummy, 1);
34374f55248Schristos if (nread == -1) {
344da700ef1Sgson if (errno == EINTR)
345da700ef1Sgson continue;
34674f55248Schristos _exit(EXIT_FAILURE);
347da700ef1Sgson } else if (nread == 0) {
34874f55248Schristos _exit(EXIT_FAILURE);
349da700ef1Sgson } else { /* nread > 0 */
35074f55248Schristos _exit(EXIT_SUCCESS);
351da700ef1Sgson }
352da700ef1Sgson }
353da700ef1Sgson }
354da700ef1Sgson
355da700ef1Sgson static int
daemon2_detach(int parentfd,int nochdir,int noclose)356da700ef1Sgson daemon2_detach(int parentfd, int nochdir, int noclose)
357da700ef1Sgson {
358da700ef1Sgson int fd;
359da700ef1Sgson
360da700ef1Sgson if (setsid() == -1)
361da700ef1Sgson return -1;
362da700ef1Sgson
363da700ef1Sgson if (!nochdir)
364da700ef1Sgson (void)chdir("/");
365da700ef1Sgson
366da700ef1Sgson if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
367da700ef1Sgson (void)dup2(fd, STDIN_FILENO);
368da700ef1Sgson (void)dup2(fd, STDOUT_FILENO);
369da700ef1Sgson (void)dup2(fd, STDERR_FILENO);
370da700ef1Sgson if (fd > STDERR_FILENO)
371da700ef1Sgson (void)close(fd);
372da700ef1Sgson }
373da700ef1Sgson
374da700ef1Sgson while (1) {
375da700ef1Sgson ssize_t r = write(parentfd, "", 1);
37674f55248Schristos if (r == -1) {
377da700ef1Sgson if (errno == EINTR)
378da700ef1Sgson continue;
379da700ef1Sgson else if (errno == EPIPE)
380da700ef1Sgson break;
381da700ef1Sgson else
382da700ef1Sgson return -1;
383da700ef1Sgson } else if (r == 0) {
384da700ef1Sgson /* Should not happen */
385da700ef1Sgson return -1;
386da700ef1Sgson } else {
387da700ef1Sgson break;
388da700ef1Sgson }
389da700ef1Sgson }
390da700ef1Sgson
391da700ef1Sgson (void)close(parentfd);
392da700ef1Sgson
393da700ef1Sgson return 0;
394da700ef1Sgson }
395da700ef1Sgson
396da700ef1Sgson /*
39761f28255Scgd * Nfs server daemon mostly just a user context for nfssvc()
398625a6892Smycroft *
39961f28255Scgd * 1 - do file descriptor and signal cleanup
400cebe06e8Syamt * 2 - create the nfsd thread(s)
401625a6892Smycroft * 3 - create server socket(s)
402625a6892Smycroft * 4 - register socket with portmap
403625a6892Smycroft *
404e1a90057Slukem * For connectionless protocols, just pass the socket into the kernel via
405625a6892Smycroft * nfssvc().
406625a6892Smycroft * For connection based sockets, loop doing accepts. When you get a new
407e1a90057Slukem * socket from accept, pass the msgsock into the kernel via nfssvc().
40861f28255Scgd * The arguments are:
409625a6892Smycroft * -r - reregister with portmapper
410eff76661Schristos * -t - support only tcp nfs clients
411eff76661Schristos * -u - support only udp nfs clients
412eff76661Schristos * -n num how many threads to create.
413eff76661Schristos * -4 - use only ipv4
414eff76661Schristos * -6 - use only ipv6
41561f28255Scgd */
416625a6892Smycroft int
main(int argc,char * argv[])4178138b1d0Sjoerg main(int argc, char *argv[])
41861f28255Scgd {
419eff76661Schristos struct conf cfg[4];
420eff76661Schristos struct pollfd set[__arraycount(cfg)];
421eff76661Schristos int ch, connect_type_cnt;
422eff76661Schristos size_t i, nfsdcnt;
423eff76661Schristos int reregister;
424eff76661Schristos int tcpflag, udpflag;
425eff76661Schristos int ip6flag, ip4flag;
426eff76661Schristos int s, compat;
427da700ef1Sgson int parent_fd = -1;
428dd41e8f2Schristos pthread_t *workers;
42961f28255Scgd
430625a6892Smycroft #define DEFNFSDCNT 4
431625a6892Smycroft nfsdcnt = DEFNFSDCNT;
432eff76661Schristos compat = reregister = 0;
433eff76661Schristos tcpflag = udpflag = 1;
434eff76661Schristos ip6flag = ip4flag = 1;
4355cbe0011Schristos #define GETOPT "46dn:rtu"
4365cbe0011Schristos #define USAGE "[-46drtu] [-n num_servers]"
43727128659Sthorpej while ((ch = getopt(argc, argv, GETOPT)) != -1) {
438625a6892Smycroft switch (ch) {
4392a2d6dd2Sfvdl case '6':
4402a2d6dd2Sfvdl ip6flag = 1;
441eff76661Schristos ip4flag = 0;
4422a2d6dd2Sfvdl s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
44374f55248Schristos if (s == -1 && (errno == EPROTONOSUPPORT ||
4442a2d6dd2Sfvdl errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
4452a2d6dd2Sfvdl ip6flag = 0;
4462a2d6dd2Sfvdl else
4472a2d6dd2Sfvdl close(s);
4482a2d6dd2Sfvdl break;
449eff76661Schristos case '4':
450eff76661Schristos ip6flag = 0;
451eff76661Schristos ip4flag = 1;
452eff76661Schristos s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
45374f55248Schristos if (s == -1 && (errno == EPROTONOSUPPORT ||
454eff76661Schristos errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
455eff76661Schristos ip4flag = 0;
456eff76661Schristos else
457eff76661Schristos close(s);
458eff76661Schristos break;
4595cbe0011Schristos case 'd':
4605cbe0011Schristos debug++;
4615cbe0011Schristos break;
462625a6892Smycroft case 'n':
463625a6892Smycroft nfsdcnt = atoi(optarg);
4647c034e5bSyamt if (nfsdcnt < 1) {
465eff76661Schristos warnx("nfsd count %zu; reset to %d", nfsdcnt,
466eff76661Schristos DEFNFSDCNT);
467625a6892Smycroft nfsdcnt = DEFNFSDCNT;
468625a6892Smycroft }
469625a6892Smycroft break;
47061f28255Scgd case 'r':
471625a6892Smycroft reregister = 1;
47261f28255Scgd break;
47361f28255Scgd case 't':
474eff76661Schristos compat |= 2;
475625a6892Smycroft tcpflag = 1;
476eff76661Schristos udpflag = 0;
47761f28255Scgd break;
47861f28255Scgd case 'u':
479eff76661Schristos compat |= 1;
480eff76661Schristos tcpflag = 0;
481625a6892Smycroft udpflag = 1;
48261f28255Scgd break;
48361f28255Scgd default:
48461f28255Scgd case '?':
48561f28255Scgd usage();
486eff76661Schristos }
48727128659Sthorpej }
488625a6892Smycroft argv += optind;
489625a6892Smycroft argc -= optind;
49061f28255Scgd
491eff76661Schristos if (compat == 3) {
492eff76661Schristos warnx("Old -tu options detected; enabling both udp and tcp.");
493eff76661Schristos warnx("This is the default behavior now and you can remove");
494eff76661Schristos warnx("all options.");
495eff76661Schristos tcpflag = udpflag = 1;
496eff76661Schristos if (ip6flag == 1 && ip4flag == 0)
497eff76661Schristos ip4flag = 1;
498625a6892Smycroft }
499b1f625e5Ssoren
50061f28255Scgd if (debug == 0) {
501da700ef1Sgson parent_fd = daemon2_fork();
502e4057031Schristos if (parent_fd == -1)
503e4057031Schristos logit(LOG_ERR, "daemon2_fork failed");
5045cbe0011Schristos openlog("nfsd", LOG_PID, LOG_DAEMON);
50561f28255Scgd }
506625a6892Smycroft
507625a6892Smycroft
508bc9d1499Schristos memset(cfg, 0, sizeof(cfg));
509eff76661Schristos for (i = 0; i < __arraycount(cfg); i++) {
510eff76661Schristos if (ip4flag == 0 && cfg_family[i] == PF_INET)
511eff76661Schristos continue;
512eff76661Schristos if (ip6flag == 0 && cfg_family[i] == PF_INET6)
513eff76661Schristos continue;
514eff76661Schristos if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
515eff76661Schristos continue;
516eff76661Schristos if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
517eff76661Schristos continue;
518eff76661Schristos tryconf(&cfg[i], i, reregister);
519eff76661Schristos }
520eff76661Schristos
521dd41e8f2Schristos workers = calloc(nfsdcnt, sizeof(*workers));
522dd41e8f2Schristos if (workers == NULL) {
523dd41e8f2Schristos logit(LOG_ERR, "thread alloc %s", strerror(errno));
52474f55248Schristos exit(EXIT_FAILURE);
525dd41e8f2Schristos }
526dd41e8f2Schristos
527625a6892Smycroft for (i = 0; i < nfsdcnt; i++) {
528cebe06e8Syamt int error;
529625a6892Smycroft
530dd41e8f2Schristos error = pthread_create(&workers[i], NULL, worker, NULL);
531cebe06e8Syamt if (error) {
532cebe06e8Syamt errno = error;
5335cbe0011Schristos logit(LOG_ERR, "pthread_create: %s", strerror(errno));
53474f55248Schristos exit(EXIT_FAILURE);
53561f28255Scgd }
536625a6892Smycroft }
537625a6892Smycroft
538625a6892Smycroft connect_type_cnt = 0;
539eff76661Schristos for (i = 0; i < __arraycount(cfg); i++) {
540eff76661Schristos set[i].fd = -1;
541eff76661Schristos set[i].events = POLLIN;
542eff76661Schristos set[i].revents = 0;
543625a6892Smycroft
544eff76661Schristos if (cfg[i].nc == NULL)
545eff76661Schristos continue;
5462a2d6dd2Sfvdl
547eff76661Schristos setupsock(&cfg[i], &set[i], i);
548eff76661Schristos if (set[i].fd != -1)
549eff76661Schristos connect_type_cnt++;
550eff76661Schristos }
551625a6892Smycroft
5520fc9d7eeSyamt pthread_setname_np(pthread_self(), "master", NULL);
553625a6892Smycroft
554da700ef1Sgson if (debug == 0) {
555da700ef1Sgson daemon2_detach(parent_fd, 0, 0);
556da700ef1Sgson (void)signal(SIGHUP, SIG_IGN);
557da700ef1Sgson (void)signal(SIGINT, SIG_IGN);
558da700ef1Sgson (void)signal(SIGQUIT, SIG_IGN);
559da700ef1Sgson (void)signal(SIGSYS, nonfs);
560da700ef1Sgson }
561da700ef1Sgson
562dd41e8f2Schristos if (connect_type_cnt == 0) {
563dd41e8f2Schristos for (i = 0; i < nfsdcnt; i++)
564dd41e8f2Schristos pthread_join(workers[i], NULL);
56574f55248Schristos exit(EXIT_SUCCESS);
566dd41e8f2Schristos }
567dd41e8f2Schristos
568625a6892Smycroft /*
569625a6892Smycroft * Loop forever accepting connections and passing the sockets
570625a6892Smycroft * into the kernel for the mounts.
57161f28255Scgd */
57261f28255Scgd for (;;) {
573eff76661Schristos if (poll(set, __arraycount(set), INFTIM) == -1) {
5745cbe0011Schristos logit(LOG_ERR, "poll failed: %s", strerror(errno));
57574f55248Schristos exit(EXIT_FAILURE);
57661f28255Scgd }
57739f9b832Smycroft
578eff76661Schristos for (i = 0; i < __arraycount(set); i++) {
579eff76661Schristos struct nfsd_args nfsdargs;
580eff76661Schristos struct sockaddr_storage ss;
581eff76661Schristos socklen_t len;
582eff76661Schristos int msgsock;
583eff76661Schristos int on = 1;
5842a2d6dd2Sfvdl
585eff76661Schristos if ((set[i].revents & POLLIN) == 0)
586eff76661Schristos continue;
587eff76661Schristos len = sizeof(ss);
588eff76661Schristos if ((msgsock = accept(set[i].fd,
589eff76661Schristos (struct sockaddr *)&ss, &len)) == -1) {
590eff76661Schristos int serrno = errno;
5915cbe0011Schristos logit(LOG_ERR, "accept failed: %s",
592813c1f38Schristos strerror(errno));
5930283f362Schristos if (serrno == EINTR || serrno == ECONNABORTED)
5940283f362Schristos continue;
59574f55248Schristos exit(EXIT_FAILURE);
5962a2d6dd2Sfvdl }
597eff76661Schristos if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
598eff76661Schristos sizeof(on)) == -1)
5995cbe0011Schristos logit(LOG_ERR, "setsockopt SO_KEEPALIVE: %s",
600813c1f38Schristos strerror(errno));
601625a6892Smycroft nfsdargs.sock = msgsock;
602eff76661Schristos nfsdargs.name = (void *)&ss;
603625a6892Smycroft nfsdargs.namelen = len;
604625a6892Smycroft nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
605625a6892Smycroft (void)close(msgsock);
606625a6892Smycroft }
60761f28255Scgd }
60861f28255Scgd }
60961f28255Scgd
6108138b1d0Sjoerg static void
usage(void)6118138b1d0Sjoerg usage(void)
61261f28255Scgd {
613eff76661Schristos (void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
61474f55248Schristos exit(EXIT_FAILURE);
61561f28255Scgd }
61661f28255Scgd
6178138b1d0Sjoerg static void
nonfs(int signo)6188138b1d0Sjoerg nonfs(int signo)
61961f28255Scgd {
6205cbe0011Schristos logit(LOG_ERR, "missing system call: NFS not available.");
62161f28255Scgd }
622