xref: /freebsd/usr.sbin/nfsd/nfsd.c (revision 7447ca0eb235974642312b9555caec00b57d8fc1)
1 /*
2  * Copyright (c) 1989, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)nfsd.c	8.9 (Berkeley) 3/29/95";
42 #endif
43 static const char rcsid[] =
44   "$FreeBSD$";
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/syslog.h>
49 #include <sys/wait.h>
50 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/linker.h>
53 #include <sys/module.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <sys/sysctl.h>
57 #include <sys/ucred.h>
58 
59 #include <rpc/rpc.h>
60 #include <rpc/pmap_clnt.h>
61 #include <rpcsvc/nfs_prot.h>
62 
63 #include <netdb.h>
64 #include <arpa/inet.h>
65 #include <nfsserver/nfs.h>
66 #include <nfs/nfssvc.h>
67 
68 #include <err.h>
69 #include <errno.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <sysexits.h>
76 
77 #include <getopt.h>
78 
79 static int	debug = 0;
80 
81 #define	NFSD_STABLERESTART	"/var/db/nfs-stablerestart"
82 #define	NFSD_STABLEBACKUP	"/var/db/nfs-stablerestart.bak"
83 #define	MAXNFSDCNT	256
84 #define	DEFNFSDCNT	 4
85 static pid_t children[MAXNFSDCNT]; /* PIDs of children */
86 static int nfsdcnt;		/* number of children */
87 static int nfsdcnt_set;
88 static int minthreads;
89 static int maxthreads;
90 static int nfssvc_nfsd;		/* Set to correct NFSSVC_xxx flag */
91 static int stablefd = -1;	/* Fd for the stable restart file */
92 static int backupfd;		/* Fd for the backup stable restart file */
93 static const char *getopt_shortopts;
94 static const char *getopt_usage;
95 
96 static int minthreads_set;
97 static int maxthreads_set;
98 
99 static struct option longopts[] = {
100 	{ "debug", no_argument, &debug, 1 },
101 	{ "minthreads", required_argument, &minthreads_set, 1 },
102 	{ "maxthreads", required_argument, &maxthreads_set, 1 },
103 	{ NULL, 0, NULL, 0}
104 };
105 
106 static void	cleanup(int);
107 static void	child_cleanup(int);
108 static void	killchildren(void);
109 static void	nfsd_exit(int);
110 static void	nonfs(int);
111 static void	reapchild(int);
112 static int	setbindhost(struct addrinfo **ia, const char *bindhost,
113 		    struct addrinfo hints);
114 static void	start_server(int);
115 static void	unregistration(void);
116 static void	usage(void);
117 static void	open_stable(int *, int *);
118 static void	copy_stable(int, int);
119 static void	backup_stable(int);
120 static void	set_nfsdcnt(int);
121 
122 /*
123  * Nfs server daemon mostly just a user context for nfssvc()
124  *
125  * 1 - do file descriptor and signal cleanup
126  * 2 - fork the nfsd(s)
127  * 3 - create server socket(s)
128  * 4 - register socket with rpcbind
129  *
130  * For connectionless protocols, just pass the socket into the kernel via.
131  * nfssvc().
132  * For connection based sockets, loop doing accepts. When you get a new
133  * socket from accept, pass the msgsock into the kernel via. nfssvc().
134  * The arguments are:
135  *	-r - reregister with rpcbind
136  *	-d - unregister with rpcbind
137  *	-t - support tcp nfs clients
138  *	-u - support udp nfs clients
139  *	-e - forces it to run a server that supports nfsv4
140  * followed by "n" which is the number of nfsds' to fork off
141  */
142 int
143 main(int argc, char **argv)
144 {
145 	struct nfsd_addsock_args addsockargs;
146 	struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
147 	struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
148 	struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
149 	struct sockaddr_in inetpeer;
150 	struct sockaddr_in6 inet6peer;
151 	fd_set ready, sockbits;
152 	fd_set v4bits, v6bits;
153 	int ch, connect_type_cnt, i, maxsock, msgsock;
154 	socklen_t len;
155 	int on = 1, unregister, reregister, sock;
156 	int tcp6sock, ip6flag, tcpflag, tcpsock;
157 	int udpflag, ecode, error, s;
158 	int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
159 	int nfssvc_addsock;
160 	int longindex = 0;
161 	const char *lopt;
162 	char **bindhost = NULL;
163 	pid_t pid;
164 
165 	nfsdcnt = DEFNFSDCNT;
166 	unregister = reregister = tcpflag = maxsock = 0;
167 	bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
168 	getopt_shortopts = "ah:n:rdtue";
169 	getopt_usage =
170 	    "usage:\n"
171 	    "  nfsd [-ardtue] [-h bindip]\n"
172 	    "       [-n numservers] [--minthreads #] [--maxthreads #]\n";
173 	while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
174 		    &longindex)) != -1)
175 		switch (ch) {
176 		case 'a':
177 			bindanyflag = 1;
178 			break;
179 		case 'n':
180 			set_nfsdcnt(atoi(optarg));
181 			break;
182 		case 'h':
183 			bindhostc++;
184 			bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
185 			if (bindhost == NULL)
186 				errx(1, "Out of memory");
187 			bindhost[bindhostc-1] = strdup(optarg);
188 			if (bindhost[bindhostc-1] == NULL)
189 				errx(1, "Out of memory");
190 			break;
191 		case 'r':
192 			reregister = 1;
193 			break;
194 		case 'd':
195 			unregister = 1;
196 			break;
197 		case 't':
198 			tcpflag = 1;
199 			break;
200 		case 'u':
201 			udpflag = 1;
202 			break;
203 		case 'e':
204 			/* now a no-op, since this is the default */
205 			break;
206 		case 0:
207 			lopt = longopts[longindex].name;
208 			if (!strcmp(lopt, "minthreads")) {
209 				minthreads = atoi(optarg);
210 			} else if (!strcmp(lopt, "maxthreads")) {
211 				maxthreads = atoi(optarg);
212 			}
213 			break;
214 		default:
215 		case '?':
216 			usage();
217 		}
218 	if (!tcpflag && !udpflag)
219 		udpflag = 1;
220 	argv += optind;
221 	argc -= optind;
222 	if (minthreads_set && maxthreads_set && minthreads > maxthreads)
223 		errx(EX_USAGE,
224 		    "error: minthreads(%d) can't be greater than "
225 		    "maxthreads(%d)", minthreads, maxthreads);
226 
227 	/*
228 	 * XXX
229 	 * Backward compatibility, trailing number is the count of daemons.
230 	 */
231 	if (argc > 1)
232 		usage();
233 	if (argc == 1)
234 		set_nfsdcnt(atoi(argv[0]));
235 
236 	/*
237 	 * Unless the "-o" option was specified, try and run "nfsd".
238 	 * If "-o" was specified, try and run "nfsserver".
239 	 */
240 	if (modfind("nfsd") < 0) {
241 		/* Not present in kernel, try loading it */
242 		if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
243 			errx(1, "NFS server is not available");
244 	}
245 
246 	ip6flag = 1;
247 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
248 	if (s == -1) {
249 		if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
250 			err(1, "socket");
251 		ip6flag = 0;
252 	} else if (getnetconfigent("udp6") == NULL ||
253 		getnetconfigent("tcp6") == NULL) {
254 		ip6flag = 0;
255 	}
256 	if (s != -1)
257 		close(s);
258 
259 	if (bindhostc == 0 || bindanyflag) {
260 		bindhostc++;
261 		bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
262 		if (bindhost == NULL)
263 			errx(1, "Out of memory");
264 		bindhost[bindhostc-1] = strdup("*");
265 		if (bindhost[bindhostc-1] == NULL)
266 			errx(1, "Out of memory");
267 	}
268 
269 	if (unregister) {
270 		unregistration();
271 		exit (0);
272 	}
273 	if (reregister) {
274 		if (udpflag) {
275 			memset(&hints, 0, sizeof hints);
276 			hints.ai_flags = AI_PASSIVE;
277 			hints.ai_family = AF_INET;
278 			hints.ai_socktype = SOCK_DGRAM;
279 			hints.ai_protocol = IPPROTO_UDP;
280 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
281 			if (ecode != 0)
282 				err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
283 			nconf_udp = getnetconfigent("udp");
284 			if (nconf_udp == NULL)
285 				err(1, "getnetconfigent udp failed");
286 			nb_udp.buf = ai_udp->ai_addr;
287 			nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
288 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) ||
289 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp)))
290 				err(1, "rpcb_set udp failed");
291 			freeaddrinfo(ai_udp);
292 		}
293 		if (udpflag && ip6flag) {
294 			memset(&hints, 0, sizeof hints);
295 			hints.ai_flags = AI_PASSIVE;
296 			hints.ai_family = AF_INET6;
297 			hints.ai_socktype = SOCK_DGRAM;
298 			hints.ai_protocol = IPPROTO_UDP;
299 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
300 			if (ecode != 0)
301 				err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
302 			nconf_udp6 = getnetconfigent("udp6");
303 			if (nconf_udp6 == NULL)
304 				err(1, "getnetconfigent udp6 failed");
305 			nb_udp6.buf = ai_udp6->ai_addr;
306 			nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
307 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) ||
308 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6)))
309 				err(1, "rpcb_set udp6 failed");
310 			freeaddrinfo(ai_udp6);
311 		}
312 		if (tcpflag) {
313 			memset(&hints, 0, sizeof hints);
314 			hints.ai_flags = AI_PASSIVE;
315 			hints.ai_family = AF_INET;
316 			hints.ai_socktype = SOCK_STREAM;
317 			hints.ai_protocol = IPPROTO_TCP;
318 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
319 			if (ecode != 0)
320 				err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
321 			nconf_tcp = getnetconfigent("tcp");
322 			if (nconf_tcp == NULL)
323 				err(1, "getnetconfigent tcp failed");
324 			nb_tcp.buf = ai_tcp->ai_addr;
325 			nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
326 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, &nb_tcp)) ||
327 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, &nb_tcp)))
328 				err(1, "rpcb_set tcp failed");
329 			freeaddrinfo(ai_tcp);
330 		}
331 		if (tcpflag && ip6flag) {
332 			memset(&hints, 0, sizeof hints);
333 			hints.ai_flags = AI_PASSIVE;
334 			hints.ai_family = AF_INET6;
335 			hints.ai_socktype = SOCK_STREAM;
336 			hints.ai_protocol = IPPROTO_TCP;
337 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
338 			if (ecode != 0)
339 				err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
340 			nconf_tcp6 = getnetconfigent("tcp6");
341 			if (nconf_tcp6 == NULL)
342 				err(1, "getnetconfigent tcp6 failed");
343 			nb_tcp6.buf = ai_tcp6->ai_addr;
344 			nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
345 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) ||
346 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6)))
347 				err(1, "rpcb_set tcp6 failed");
348 			freeaddrinfo(ai_tcp6);
349 		}
350 		exit (0);
351 	}
352 	if (debug == 0) {
353 		daemon(0, 0);
354 		(void)signal(SIGHUP, SIG_IGN);
355 		(void)signal(SIGINT, SIG_IGN);
356 		/*
357 		 * nfsd sits in the kernel most of the time.  It needs
358 		 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
359 		 * as possible during a shutdown, otherwise loopback
360 		 * mounts will not be able to unmount.
361 		 */
362 		(void)signal(SIGTERM, SIG_IGN);
363 		(void)signal(SIGQUIT, SIG_IGN);
364 	}
365 	(void)signal(SIGSYS, nonfs);
366 	(void)signal(SIGCHLD, reapchild);
367 	(void)signal(SIGUSR2, backup_stable);
368 
369 	openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
370 
371 	/*
372 	 * For V4, we open the stablerestart file and call nfssvc()
373 	 * to get it loaded. This is done before the daemons do the
374 	 * regular nfssvc() call to service NFS requests.
375 	 * (This way the file remains open until the last nfsd is killed
376 	 *  off.)
377 	 * It and the backup copy will be created as empty files
378 	 * the first time this nfsd is started and should never be
379 	 * deleted/replaced if at all possible. It should live on a
380 	 * local, non-volatile storage device that does not do hardware
381 	 * level write-back caching. (See SCSI doc for more information
382 	 * on how to prevent write-back caching on SCSI disks.)
383 	 */
384 	open_stable(&stablefd, &backupfd);
385 	if (stablefd < 0) {
386 		syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
387 		exit(1);
388 	}
389 	/* This system call will fail for old kernels, but that's ok. */
390 	nfssvc(NFSSVC_BACKUPSTABLE, NULL);
391 	if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
392 		syslog(LOG_ERR, "Can't read stable storage file: %m\n");
393 		exit(1);
394 	}
395 	nfssvc_addsock = NFSSVC_NFSDADDSOCK;
396 	nfssvc_nfsd = NFSSVC_NFSDNFSD;
397 
398 	if (tcpflag) {
399 		/*
400 		 * For TCP mode, we fork once to start the first
401 		 * kernel nfsd thread. The kernel will add more
402 		 * threads as needed.
403 		 */
404 		pid = fork();
405 		if (pid == -1) {
406 			syslog(LOG_ERR, "fork: %m");
407 			nfsd_exit(1);
408 		}
409 		if (pid) {
410 			children[0] = pid;
411 		} else {
412 			(void)signal(SIGUSR1, child_cleanup);
413 			setproctitle("server");
414 			start_server(0);
415 		}
416 	}
417 
418 	(void)signal(SIGUSR1, cleanup);
419 	FD_ZERO(&v4bits);
420 	FD_ZERO(&v6bits);
421 	FD_ZERO(&sockbits);
422 
423 	rpcbregcnt = 0;
424 	/* Set up the socket for udp and rpcb register it. */
425 	if (udpflag) {
426 		rpcbreg = 0;
427 		for (i = 0; i < bindhostc; i++) {
428 			memset(&hints, 0, sizeof hints);
429 			hints.ai_flags = AI_PASSIVE;
430 			hints.ai_family = AF_INET;
431 			hints.ai_socktype = SOCK_DGRAM;
432 			hints.ai_protocol = IPPROTO_UDP;
433 			if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
434 				rpcbreg = 1;
435 				rpcbregcnt++;
436 				if ((sock = socket(ai_udp->ai_family,
437 				    ai_udp->ai_socktype,
438 				    ai_udp->ai_protocol)) < 0) {
439 					syslog(LOG_ERR,
440 					    "can't create udp socket");
441 					nfsd_exit(1);
442 				}
443 				if (bind(sock, ai_udp->ai_addr,
444 				    ai_udp->ai_addrlen) < 0) {
445 					syslog(LOG_ERR,
446 					    "can't bind udp addr %s: %m",
447 					    bindhost[i]);
448 					nfsd_exit(1);
449 				}
450 				freeaddrinfo(ai_udp);
451 				addsockargs.sock = sock;
452 				addsockargs.name = NULL;
453 				addsockargs.namelen = 0;
454 				if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
455 					syslog(LOG_ERR, "can't Add UDP socket");
456 					nfsd_exit(1);
457 				}
458 				(void)close(sock);
459 			}
460 		}
461 		if (rpcbreg == 1) {
462 			memset(&hints, 0, sizeof hints);
463 			hints.ai_flags = AI_PASSIVE;
464 			hints.ai_family = AF_INET;
465 			hints.ai_socktype = SOCK_DGRAM;
466 			hints.ai_protocol = IPPROTO_UDP;
467 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
468 			if (ecode != 0) {
469 				syslog(LOG_ERR, "getaddrinfo udp: %s",
470 				   gai_strerror(ecode));
471 				nfsd_exit(1);
472 			}
473 			nconf_udp = getnetconfigent("udp");
474 			if (nconf_udp == NULL)
475 				err(1, "getnetconfigent udp failed");
476 			nb_udp.buf = ai_udp->ai_addr;
477 			nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
478 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) ||
479 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp)))
480 				err(1, "rpcb_set udp failed");
481 			freeaddrinfo(ai_udp);
482 		}
483 	}
484 
485 	/* Set up the socket for udp6 and rpcb register it. */
486 	if (udpflag && ip6flag) {
487 		rpcbreg = 0;
488 		for (i = 0; i < bindhostc; i++) {
489 			memset(&hints, 0, sizeof hints);
490 			hints.ai_flags = AI_PASSIVE;
491 			hints.ai_family = AF_INET6;
492 			hints.ai_socktype = SOCK_DGRAM;
493 			hints.ai_protocol = IPPROTO_UDP;
494 			if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
495 				rpcbreg = 1;
496 				rpcbregcnt++;
497 				if ((sock = socket(ai_udp6->ai_family,
498 				    ai_udp6->ai_socktype,
499 				    ai_udp6->ai_protocol)) < 0) {
500 					syslog(LOG_ERR,
501 						"can't create udp6 socket");
502 					nfsd_exit(1);
503 				}
504 				if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
505 				    &on, sizeof on) < 0) {
506 					syslog(LOG_ERR,
507 					    "can't set v6-only binding for "
508 					    "udp6 socket: %m");
509 					nfsd_exit(1);
510 				}
511 				if (bind(sock, ai_udp6->ai_addr,
512 				    ai_udp6->ai_addrlen) < 0) {
513 					syslog(LOG_ERR,
514 					    "can't bind udp6 addr %s: %m",
515 					    bindhost[i]);
516 					nfsd_exit(1);
517 				}
518 				freeaddrinfo(ai_udp6);
519 				addsockargs.sock = sock;
520 				addsockargs.name = NULL;
521 				addsockargs.namelen = 0;
522 				if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
523 					syslog(LOG_ERR,
524 					    "can't add UDP6 socket");
525 					nfsd_exit(1);
526 				}
527 				(void)close(sock);
528 			}
529 		}
530 		if (rpcbreg == 1) {
531 			memset(&hints, 0, sizeof hints);
532 			hints.ai_flags = AI_PASSIVE;
533 			hints.ai_family = AF_INET6;
534 			hints.ai_socktype = SOCK_DGRAM;
535 			hints.ai_protocol = IPPROTO_UDP;
536 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
537 			if (ecode != 0) {
538 				syslog(LOG_ERR, "getaddrinfo udp6: %s",
539 				   gai_strerror(ecode));
540 				nfsd_exit(1);
541 			}
542 			nconf_udp6 = getnetconfigent("udp6");
543 			if (nconf_udp6 == NULL)
544 				err(1, "getnetconfigent udp6 failed");
545 			nb_udp6.buf = ai_udp6->ai_addr;
546 			nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
547 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) ||
548 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6)))
549 				err(1, "rpcb_set udp6 failed");
550 			freeaddrinfo(ai_udp6);
551 		}
552 	}
553 
554 	/* Set up the socket for tcp and rpcb register it. */
555 	if (tcpflag) {
556 		rpcbreg = 0;
557 		for (i = 0; i < bindhostc; i++) {
558 			memset(&hints, 0, sizeof hints);
559 			hints.ai_flags = AI_PASSIVE;
560 			hints.ai_family = AF_INET;
561 			hints.ai_socktype = SOCK_STREAM;
562 			hints.ai_protocol = IPPROTO_TCP;
563 			if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
564 				rpcbreg = 1;
565 				rpcbregcnt++;
566 				if ((tcpsock = socket(AF_INET, SOCK_STREAM,
567 				    0)) < 0) {
568 					syslog(LOG_ERR,
569 					    "can't create tcp socket");
570 					nfsd_exit(1);
571 				}
572 				if (setsockopt(tcpsock, SOL_SOCKET,
573 				    SO_REUSEADDR,
574 				    (char *)&on, sizeof(on)) < 0)
575 					syslog(LOG_ERR,
576 					     "setsockopt SO_REUSEADDR: %m");
577 				if (bind(tcpsock, ai_tcp->ai_addr,
578 				    ai_tcp->ai_addrlen) < 0) {
579 					syslog(LOG_ERR,
580 					    "can't bind tcp addr %s: %m",
581 					    bindhost[i]);
582 					nfsd_exit(1);
583 				}
584 				if (listen(tcpsock, -1) < 0) {
585 					syslog(LOG_ERR, "listen failed");
586 					nfsd_exit(1);
587 				}
588 				freeaddrinfo(ai_tcp);
589 				FD_SET(tcpsock, &sockbits);
590 				FD_SET(tcpsock, &v4bits);
591 				maxsock = tcpsock;
592 				connect_type_cnt++;
593 			}
594 		}
595 		if (rpcbreg == 1) {
596 			memset(&hints, 0, sizeof hints);
597 			hints.ai_flags = AI_PASSIVE;
598 			hints.ai_family = AF_INET;
599 			hints.ai_socktype = SOCK_STREAM;
600 			hints.ai_protocol = IPPROTO_TCP;
601 			ecode = getaddrinfo(NULL, "nfs", &hints,
602 			     &ai_tcp);
603 			if (ecode != 0) {
604 				syslog(LOG_ERR, "getaddrinfo tcp: %s",
605 				   gai_strerror(ecode));
606 				nfsd_exit(1);
607 			}
608 			nconf_tcp = getnetconfigent("tcp");
609 			if (nconf_tcp == NULL)
610 				err(1, "getnetconfigent tcp failed");
611 			nb_tcp.buf = ai_tcp->ai_addr;
612 			nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
613 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
614 			    &nb_tcp)) || (!rpcb_set(NFS_PROGRAM, 3,
615 			    nconf_tcp, &nb_tcp)))
616 				err(1, "rpcb_set tcp failed");
617 			freeaddrinfo(ai_tcp);
618 		}
619 	}
620 
621 	/* Set up the socket for tcp6 and rpcb register it. */
622 	if (tcpflag && ip6flag) {
623 		rpcbreg = 0;
624 		for (i = 0; i < bindhostc; i++) {
625 			memset(&hints, 0, sizeof hints);
626 			hints.ai_flags = AI_PASSIVE;
627 			hints.ai_family = AF_INET6;
628 			hints.ai_socktype = SOCK_STREAM;
629 			hints.ai_protocol = IPPROTO_TCP;
630 			if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
631 				rpcbreg = 1;
632 				rpcbregcnt++;
633 				if ((tcp6sock = socket(ai_tcp6->ai_family,
634 				    ai_tcp6->ai_socktype,
635 				    ai_tcp6->ai_protocol)) < 0) {
636 					syslog(LOG_ERR,
637 					    "can't create tcp6 socket");
638 					nfsd_exit(1);
639 				}
640 				if (setsockopt(tcp6sock, SOL_SOCKET,
641 				    SO_REUSEADDR,
642 				    (char *)&on, sizeof(on)) < 0)
643 					syslog(LOG_ERR,
644 					    "setsockopt SO_REUSEADDR: %m");
645 				if (setsockopt(tcp6sock, IPPROTO_IPV6,
646 				    IPV6_V6ONLY, &on, sizeof on) < 0) {
647 					syslog(LOG_ERR,
648 					"can't set v6-only binding for tcp6 "
649 					    "socket: %m");
650 					nfsd_exit(1);
651 				}
652 				if (bind(tcp6sock, ai_tcp6->ai_addr,
653 				    ai_tcp6->ai_addrlen) < 0) {
654 					syslog(LOG_ERR,
655 					    "can't bind tcp6 addr %s: %m",
656 					    bindhost[i]);
657 					nfsd_exit(1);
658 				}
659 				if (listen(tcp6sock, -1) < 0) {
660 					syslog(LOG_ERR, "listen failed");
661 					nfsd_exit(1);
662 				}
663 				freeaddrinfo(ai_tcp6);
664 				FD_SET(tcp6sock, &sockbits);
665 				FD_SET(tcp6sock, &v6bits);
666 				if (maxsock < tcp6sock)
667 					maxsock = tcp6sock;
668 				connect_type_cnt++;
669 			}
670 		}
671 		if (rpcbreg == 1) {
672 			memset(&hints, 0, sizeof hints);
673 			hints.ai_flags = AI_PASSIVE;
674 			hints.ai_family = AF_INET6;
675 			hints.ai_socktype = SOCK_STREAM;
676 			hints.ai_protocol = IPPROTO_TCP;
677 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
678 			if (ecode != 0) {
679 				syslog(LOG_ERR, "getaddrinfo tcp6: %s",
680 				   gai_strerror(ecode));
681 				nfsd_exit(1);
682 			}
683 			nconf_tcp6 = getnetconfigent("tcp6");
684 			if (nconf_tcp6 == NULL)
685 				err(1, "getnetconfigent tcp6 failed");
686 			nb_tcp6.buf = ai_tcp6->ai_addr;
687 			nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
688 			if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) ||
689 			    (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6)))
690 				err(1, "rpcb_set tcp6 failed");
691 			freeaddrinfo(ai_tcp6);
692 		}
693 	}
694 
695 	if (rpcbregcnt == 0) {
696 		syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
697 		nfsd_exit(1);
698 	}
699 
700 	if (tcpflag && connect_type_cnt == 0) {
701 		syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
702 		nfsd_exit(1);
703 	}
704 
705 	setproctitle("master");
706 	/*
707 	 * We always want a master to have a clean way to to shut nfsd down
708 	 * (with unregistration): if the master is killed, it unregisters and
709 	 * kills all children. If we run for UDP only (and so do not have to
710 	 * loop waiting waiting for accept), we instead make the parent
711 	 * a "server" too. start_server will not return.
712 	 */
713 	if (!tcpflag)
714 		start_server(1);
715 
716 	/*
717 	 * Loop forever accepting connections and passing the sockets
718 	 * into the kernel for the mounts.
719 	 */
720 	for (;;) {
721 		ready = sockbits;
722 		if (connect_type_cnt > 1) {
723 			if (select(maxsock + 1,
724 			    &ready, NULL, NULL, NULL) < 1) {
725 				error = errno;
726 				if (error == EINTR)
727 					continue;
728 				syslog(LOG_ERR, "select failed: %m");
729 				nfsd_exit(1);
730 			}
731 		}
732 		for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
733 			if (FD_ISSET(tcpsock, &ready)) {
734 				if (FD_ISSET(tcpsock, &v4bits)) {
735 					len = sizeof(inetpeer);
736 					if ((msgsock = accept(tcpsock,
737 					    (struct sockaddr *)&inetpeer, &len)) < 0) {
738 						error = errno;
739 						syslog(LOG_ERR, "accept failed: %m");
740 						if (error == ECONNABORTED ||
741 						    error == EINTR)
742 							continue;
743 						nfsd_exit(1);
744 					}
745 					memset(inetpeer.sin_zero, 0,
746 						sizeof(inetpeer.sin_zero));
747 					if (setsockopt(msgsock, SOL_SOCKET,
748 					    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
749 						syslog(LOG_ERR,
750 						    "setsockopt SO_KEEPALIVE: %m");
751 					addsockargs.sock = msgsock;
752 					addsockargs.name = (caddr_t)&inetpeer;
753 					addsockargs.namelen = len;
754 					nfssvc(nfssvc_addsock, &addsockargs);
755 					(void)close(msgsock);
756 				} else if (FD_ISSET(tcpsock, &v6bits)) {
757 					len = sizeof(inet6peer);
758 					if ((msgsock = accept(tcpsock,
759 					    (struct sockaddr *)&inet6peer,
760 					    &len)) < 0) {
761 						error = errno;
762 						syslog(LOG_ERR,
763 						     "accept failed: %m");
764 						if (error == ECONNABORTED ||
765 						    error == EINTR)
766 							continue;
767 						nfsd_exit(1);
768 					}
769 					if (setsockopt(msgsock, SOL_SOCKET,
770 					    SO_KEEPALIVE, (char *)&on,
771 					    sizeof(on)) < 0)
772 						syslog(LOG_ERR, "setsockopt "
773 						    "SO_KEEPALIVE: %m");
774 					addsockargs.sock = msgsock;
775 					addsockargs.name = (caddr_t)&inet6peer;
776 					addsockargs.namelen = len;
777 					nfssvc(nfssvc_addsock, &addsockargs);
778 					(void)close(msgsock);
779 				}
780 			}
781 		}
782 	}
783 }
784 
785 static int
786 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
787 {
788 	int ecode;
789 	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
790 	const char *hostptr;
791 
792 	if (bindhost == NULL || strcmp("*", bindhost) == 0)
793 		hostptr = NULL;
794 	else
795 		hostptr = bindhost;
796 
797 	if (hostptr != NULL) {
798 		switch (hints.ai_family) {
799 		case AF_INET:
800 			if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
801 				hints.ai_flags = AI_NUMERICHOST;
802 			} else {
803 				if (inet_pton(AF_INET6, hostptr,
804 				    host_addr) == 1)
805 					return (1);
806 			}
807 			break;
808 		case AF_INET6:
809 			if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
810 				hints.ai_flags = AI_NUMERICHOST;
811 			} else {
812 				if (inet_pton(AF_INET, hostptr,
813 				    host_addr) == 1)
814 					return (1);
815 			}
816 			break;
817 		default:
818 			break;
819 		}
820 	}
821 
822 	ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
823 	if (ecode != 0) {
824 		syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
825 		    gai_strerror(ecode));
826 		return (1);
827 	}
828 	return (0);
829 }
830 
831 static void
832 set_nfsdcnt(int proposed)
833 {
834 
835 	if (proposed < 1) {
836 		warnx("nfsd count too low %d; reset to %d", proposed,
837 		    DEFNFSDCNT);
838 		nfsdcnt = DEFNFSDCNT;
839 	} else if (proposed > MAXNFSDCNT) {
840 		warnx("nfsd count too high %d; truncated to %d", proposed,
841 		    MAXNFSDCNT);
842 		nfsdcnt = MAXNFSDCNT;
843 	} else
844 		nfsdcnt = proposed;
845 	nfsdcnt_set = 1;
846 }
847 
848 static void
849 usage(void)
850 {
851 	(void)fprintf(stderr, "%s", getopt_usage);
852 	exit(1);
853 }
854 
855 static void
856 nonfs(__unused int signo)
857 {
858 	syslog(LOG_ERR, "missing system call: NFS not available");
859 }
860 
861 static void
862 reapchild(__unused int signo)
863 {
864 	pid_t pid;
865 	int i;
866 
867 	while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
868 		for (i = 0; i < nfsdcnt; i++)
869 			if (pid == children[i])
870 				children[i] = -1;
871 	}
872 }
873 
874 static void
875 unregistration(void)
876 {
877 	if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
878 	    (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
879 		syslog(LOG_ERR, "rpcb_unset failed");
880 }
881 
882 static void
883 killchildren(void)
884 {
885 	int i;
886 
887 	for (i = 0; i < nfsdcnt; i++) {
888 		if (children[i] > 0)
889 			kill(children[i], SIGKILL);
890 	}
891 }
892 
893 /*
894  * Cleanup master after SIGUSR1.
895  */
896 static void
897 cleanup(__unused int signo)
898 {
899 	nfsd_exit(0);
900 }
901 
902 /*
903  * Cleanup child after SIGUSR1.
904  */
905 static void
906 child_cleanup(__unused int signo)
907 {
908 	exit(0);
909 }
910 
911 static void
912 nfsd_exit(int status)
913 {
914 	killchildren();
915 	unregistration();
916 	exit(status);
917 }
918 
919 static int
920 get_tuned_nfsdcount(void)
921 {
922 	int ncpu, error, tuned_nfsdcnt;
923 	size_t ncpu_size;
924 
925 	ncpu_size = sizeof(ncpu);
926 	error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
927 	if (error) {
928 		warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
929 		    DEFNFSDCNT);
930 		tuned_nfsdcnt = DEFNFSDCNT;
931 	} else {
932 		tuned_nfsdcnt = ncpu * 8;
933 	}
934 	return tuned_nfsdcnt;
935 }
936 
937 static void
938 start_server(int master)
939 {
940 	char principal[MAXHOSTNAMELEN + 5];
941 	struct nfsd_nfsd_args nfsdargs;
942 	int status, error;
943 	char hostname[MAXHOSTNAMELEN + 1], *cp;
944 	struct addrinfo *aip, hints;
945 
946 	status = 0;
947 	gethostname(hostname, sizeof (hostname));
948 	snprintf(principal, sizeof (principal), "nfs@%s", hostname);
949 	if ((cp = strchr(hostname, '.')) == NULL ||
950 	    *(cp + 1) == '\0') {
951 		/* If not fully qualified, try getaddrinfo() */
952 		memset((void *)&hints, 0, sizeof (hints));
953 		hints.ai_flags = AI_CANONNAME;
954 		error = getaddrinfo(hostname, NULL, &hints, &aip);
955 		if (error == 0) {
956 			if (aip->ai_canonname != NULL &&
957 			    (cp = strchr(aip->ai_canonname, '.')) !=
958 			    NULL && *(cp + 1) != '\0')
959 				snprintf(principal, sizeof (principal),
960 				    "nfs@%s", aip->ai_canonname);
961 			freeaddrinfo(aip);
962 		}
963 	}
964 	nfsdargs.principal = principal;
965 
966 	if (nfsdcnt_set)
967 		nfsdargs.minthreads = nfsdargs.maxthreads = nfsdcnt;
968 	else {
969 		nfsdargs.minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
970 		nfsdargs.maxthreads = maxthreads_set ? maxthreads : nfsdargs.minthreads;
971 		if (nfsdargs.maxthreads < nfsdargs.minthreads)
972 			nfsdargs.maxthreads = nfsdargs.minthreads;
973 	}
974 	error = nfssvc(nfssvc_nfsd, &nfsdargs);
975 	if (error < 0 && errno == EAUTH) {
976 		/*
977 		 * This indicates that it could not register the
978 		 * rpcsec_gss credentials, usually because the
979 		 * gssd daemon isn't running.
980 		 * (only the experimental server with nfsv4)
981 		 */
982 		syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
983 		principal[0] = '\0';
984 		error = nfssvc(nfssvc_nfsd, &nfsdargs);
985 	}
986 	if (error < 0) {
987 		syslog(LOG_ERR, "nfssvc: %m");
988 		status = 1;
989 	}
990 	if (master)
991 		nfsd_exit(status);
992 	else
993 		exit(status);
994 }
995 
996 /*
997  * Open the stable restart file and return the file descriptor for it.
998  */
999 static void
1000 open_stable(int *stable_fdp, int *backup_fdp)
1001 {
1002 	int stable_fd, backup_fd = -1, ret;
1003 	struct stat st, backup_st;
1004 
1005 	/* Open and stat the stable restart file. */
1006 	stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1007 	if (stable_fd < 0)
1008 		stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1009 	if (stable_fd >= 0) {
1010 		ret = fstat(stable_fd, &st);
1011 		if (ret < 0) {
1012 			close(stable_fd);
1013 			stable_fd = -1;
1014 		}
1015 	}
1016 
1017 	/* Open and stat the backup stable restart file. */
1018 	if (stable_fd >= 0) {
1019 		backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1020 		if (backup_fd < 0)
1021 			backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1022 			    0600);
1023 		if (backup_fd >= 0) {
1024 			ret = fstat(backup_fd, &backup_st);
1025 			if (ret < 0) {
1026 				close(backup_fd);
1027 				backup_fd = -1;
1028 			}
1029 		}
1030 		if (backup_fd < 0) {
1031 			close(stable_fd);
1032 			stable_fd = -1;
1033 		}
1034 	}
1035 
1036 	*stable_fdp = stable_fd;
1037 	*backup_fdp = backup_fd;
1038 	if (stable_fd < 0)
1039 		return;
1040 
1041 	/* Sync up the 2 files, as required. */
1042 	if (st.st_size > 0)
1043 		copy_stable(stable_fd, backup_fd);
1044 	else if (backup_st.st_size > 0)
1045 		copy_stable(backup_fd, stable_fd);
1046 }
1047 
1048 /*
1049  * Copy the stable restart file to the backup or vice versa.
1050  */
1051 static void
1052 copy_stable(int from_fd, int to_fd)
1053 {
1054 	int cnt, ret;
1055 	static char buf[1024];
1056 
1057 	ret = lseek(from_fd, (off_t)0, SEEK_SET);
1058 	if (ret >= 0)
1059 		ret = lseek(to_fd, (off_t)0, SEEK_SET);
1060 	if (ret >= 0)
1061 		ret = ftruncate(to_fd, (off_t)0);
1062 	if (ret >= 0)
1063 		do {
1064 			cnt = read(from_fd, buf, 1024);
1065 			if (cnt > 0)
1066 				ret = write(to_fd, buf, cnt);
1067 			else if (cnt < 0)
1068 				ret = cnt;
1069 		} while (cnt > 0 && ret >= 0);
1070 	if (ret >= 0)
1071 		ret = fsync(to_fd);
1072 	if (ret < 0)
1073 		syslog(LOG_ERR, "stable restart copy failure: %m");
1074 }
1075 
1076 /*
1077  * Back up the stable restart file when indicated by the kernel.
1078  */
1079 static void
1080 backup_stable(__unused int signo)
1081 {
1082 
1083 	if (stablefd >= 0)
1084 		copy_stable(stablefd, backupfd);
1085 }
1086 
1087