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