2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
43 static const char rcsid[] =
47 #include <sys/param.h>
48 #include <sys/syslog.h>
50 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/linker.h>
53 #include <sys/module.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpcsvc/nfs_prot.h>
60 #include <arpa/inet.h>
61 #include <nfsserver/nfs.h>
62 #include <nfs/nfssvc.h>
75 #define syslog(e, s...) fprintf(stderr,s)
81 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart"
82 #define MAXNFSDCNT 256
84 pid_t children[MAXNFSDCNT]; /* PIDs of children */
85 int nfsdcnt; /* number of children */
87 int run_v4server = 0; /* Force running of nfsv4 server */
88 int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */
91 void child_cleanup(int);
92 void killchildren(void);
96 int setbindhost(struct addrinfo **ia, const char *bindhost,
97 struct addrinfo hints);
98 void start_server(int);
99 void unregistration(void);
103 * Nfs server daemon mostly just a user context for nfssvc()
105 * 1 - do file descriptor and signal cleanup
106 * 2 - fork the nfsd(s)
107 * 3 - create server socket(s)
108 * 4 - register socket with rpcbind
110 * For connectionless protocols, just pass the socket into the kernel via.
112 * For connection based sockets, loop doing accepts. When you get a new
113 * socket from accept, pass the msgsock into the kernel via. nfssvc().
115 * -r - reregister with rpcbind
116 * -d - unregister with rpcbind
117 * -t - support tcp nfs clients
118 * -u - support udp nfs clients
119 * -e - forces it to run a server that supports nfsv4
120 * followed by "n" which is the number of nfsds' to fork off
123 main(int argc, char **argv)
125 struct nfsd_addsock_args addsockargs;
126 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
127 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
128 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
129 struct sockaddr_in inetpeer;
130 struct sockaddr_in6 inet6peer;
131 fd_set ready, sockbits;
132 fd_set v4bits, v6bits;
133 int ch, connect_type_cnt, i, maxsock, msgsock;
135 int on = 1, unregister, reregister, sock;
136 int tcp6sock, ip6flag, tcpflag, tcpsock;
137 int udpflag, ecode, s, srvcnt;
138 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
139 int stablefd, nfssvc_addsock;
140 char **bindhost = NULL;
143 nfsdcnt = DEFNFSDCNT;
144 unregister = reregister = tcpflag = maxsock = 0;
145 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
146 #define GETOPT "ah:n:rdtue"
147 #define USAGE "[-ardtue] [-n num_servers] [-h bindip]"
148 while ((ch = getopt(argc, argv, GETOPT)) != -1)
154 nfsdcnt = atoi(optarg);
155 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
156 warnx("nfsd count %d; reset to %d", nfsdcnt,
158 nfsdcnt = DEFNFSDCNT;
163 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
164 if (bindhost == NULL)
165 errx(1, "Out of memory");
166 bindhost[bindhostc-1] = strdup(optarg);
167 if (bindhost[bindhostc-1] == NULL)
168 errx(1, "Out of memory");
189 if (!tcpflag && !udpflag)
196 * Backward compatibility, trailing number is the count of daemons.
201 nfsdcnt = atoi(argv[0]);
202 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
203 warnx("nfsd count %d; reset to %d", nfsdcnt,
205 nfsdcnt = DEFNFSDCNT;
210 * If the "-e" option was specified OR only the nfsd module is
211 * found in the server, run "nfsd".
212 * Otherwise, try and run "nfsserver".
214 if (run_v4server > 0) {
215 if (modfind("nfsd") < 0) {
216 /* Not present in kernel, try loading it */
217 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
218 errx(1, "NFS server is not available");
220 } else if (modfind("nfsserver") < 0 && modfind("nfsd") >= 0) {
222 } else if (modfind("nfsserver") < 0) {
223 /* Not present in kernel, try loading it */
224 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
225 errx(1, "NFS server is not available");
229 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
231 if (errno != EPROTONOSUPPORT)
234 } else if (getnetconfigent("udp6") == NULL ||
235 getnetconfigent("tcp6") == NULL) {
241 if (bindhostc == 0 || bindanyflag) {
243 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
244 if (bindhost == NULL)
245 errx(1, "Out of memory");
246 bindhost[bindhostc-1] = strdup("*");
247 if (bindhost[bindhostc-1] == NULL)
248 errx(1, "Out of memory");
257 memset(&hints, 0, sizeof hints);
258 hints.ai_flags = AI_PASSIVE;
259 hints.ai_family = AF_INET;
260 hints.ai_socktype = SOCK_DGRAM;
261 hints.ai_protocol = IPPROTO_UDP;
262 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
264 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
265 nconf_udp = getnetconfigent("udp");
266 if (nconf_udp == NULL)
267 err(1, "getnetconfigent udp failed");
268 nb_udp.buf = ai_udp->ai_addr;
269 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
270 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) ||
271 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp)))
272 err(1, "rpcb_set udp failed");
273 freeaddrinfo(ai_udp);
275 if (udpflag && ip6flag) {
276 memset(&hints, 0, sizeof hints);
277 hints.ai_flags = AI_PASSIVE;
278 hints.ai_family = AF_INET6;
279 hints.ai_socktype = SOCK_DGRAM;
280 hints.ai_protocol = IPPROTO_UDP;
281 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
283 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
284 nconf_udp6 = getnetconfigent("udp6");
285 if (nconf_udp6 == NULL)
286 err(1, "getnetconfigent udp6 failed");
287 nb_udp6.buf = ai_udp6->ai_addr;
288 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
289 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) ||
290 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6)))
291 err(1, "rpcb_set udp6 failed");
292 freeaddrinfo(ai_udp6);
295 memset(&hints, 0, sizeof hints);
296 hints.ai_flags = AI_PASSIVE;
297 hints.ai_family = AF_INET;
298 hints.ai_socktype = SOCK_STREAM;
299 hints.ai_protocol = IPPROTO_TCP;
300 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
302 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
303 nconf_tcp = getnetconfigent("tcp");
304 if (nconf_tcp == NULL)
305 err(1, "getnetconfigent tcp failed");
306 nb_tcp.buf = ai_tcp->ai_addr;
307 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
308 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, &nb_tcp)) ||
309 (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, &nb_tcp)))
310 err(1, "rpcb_set tcp failed");
311 freeaddrinfo(ai_tcp);
313 if (tcpflag && ip6flag) {
314 memset(&hints, 0, sizeof hints);
315 hints.ai_flags = AI_PASSIVE;
316 hints.ai_family = AF_INET6;
317 hints.ai_socktype = SOCK_STREAM;
318 hints.ai_protocol = IPPROTO_TCP;
319 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
321 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
322 nconf_tcp6 = getnetconfigent("tcp6");
323 if (nconf_tcp6 == NULL)
324 err(1, "getnetconfigent tcp6 failed");
325 nb_tcp6.buf = ai_tcp6->ai_addr;
326 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
327 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) ||
328 (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6)))
329 err(1, "rpcb_set tcp6 failed");
330 freeaddrinfo(ai_tcp6);
336 (void)signal(SIGHUP, SIG_IGN);
337 (void)signal(SIGINT, SIG_IGN);
339 * nfsd sits in the kernel most of the time. It needs
340 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
341 * as possible during a shutdown, otherwise loopback
342 * mounts will not be able to unmount.
344 (void)signal(SIGTERM, SIG_IGN);
345 (void)signal(SIGQUIT, SIG_IGN);
347 (void)signal(SIGSYS, nonfs);
348 (void)signal(SIGCHLD, reapchild);
350 openlog("nfsd", LOG_PID, LOG_DAEMON);
353 * For V4, we open the stablerestart file and call nfssvc()
354 * to get it loaded. This is done before the daemons do the
355 * regular nfssvc() call to service NFS requests.
356 * (This way the file remains open until the last nfsd is killed
358 * Note that this file is not created by this daemon and can
359 * only be relocated by recompiling the daemon, in order to
360 * minimize accidentally starting up with the wrong file.
361 * If should be created as an empty file Read and Write for
362 * root before the first time you run NFS v4 and should never
363 * be re-initialized if at all possible. It should live on a
364 * local, non-volatile storage device that does not do hardware
365 * level write-back caching. (See SCSI doc for more information
366 * on how to prevent write-back caching on SCSI disks.)
368 if (run_v4server > 0) {
369 stablefd = open(NFSD_STABLERESTART, O_RDWR, 0);
371 syslog(LOG_ERR, "Can't open %s\n", NFSD_STABLERESTART);
374 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
375 syslog(LOG_ERR, "Can't read stable storage file\n");
378 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
379 nfssvc_nfsd = NFSSVC_NFSDNFSD;
382 nfssvc_addsock = NFSSVC_ADDSOCK;
383 nfssvc_nfsd = NFSSVC_NFSD;
385 * Figure out if the kernel supports the new-style
386 * NFSSVC_NFSD. Old kernels will return ENXIO because they
387 * don't recognise the flag value, new ones will return EINVAL
388 * because argp is NULL.
391 if (nfssvc(NFSSVC_NFSD, NULL) < 0 && errno == EINVAL)
396 /* If we use UDP only, we start the last server below. */
397 srvcnt = tcpflag ? nfsdcnt : nfsdcnt - 1;
398 for (i = 0; i < srvcnt; i++) {
399 switch ((pid = fork())) {
401 syslog(LOG_ERR, "fork: %m");
409 (void)signal(SIGUSR1, child_cleanup);
410 setproctitle("server");
414 } else if (tcpflag) {
416 * For TCP mode, we fork once to start the first
417 * kernel nfsd thread. The kernel will add more
422 syslog(LOG_ERR, "fork: %m");
428 (void)signal(SIGUSR1, child_cleanup);
429 setproctitle("server");
434 (void)signal(SIGUSR1, cleanup);
440 /* Set up the socket for udp and rpcb register it. */
443 for (i = 0; i < bindhostc; i++) {
444 memset(&hints, 0, sizeof hints);
445 hints.ai_flags = AI_PASSIVE;
446 hints.ai_family = AF_INET;
447 hints.ai_socktype = SOCK_DGRAM;
448 hints.ai_protocol = IPPROTO_UDP;
449 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
452 if ((sock = socket(ai_udp->ai_family,
454 ai_udp->ai_protocol)) < 0) {
456 "can't create udp socket");
459 if (bind(sock, ai_udp->ai_addr,
460 ai_udp->ai_addrlen) < 0) {
462 "can't bind udp addr %s: %m",
466 freeaddrinfo(ai_udp);
467 addsockargs.sock = sock;
468 addsockargs.name = NULL;
469 addsockargs.namelen = 0;
470 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
471 syslog(LOG_ERR, "can't Add UDP socket");
478 memset(&hints, 0, sizeof hints);
479 hints.ai_flags = AI_PASSIVE;
480 hints.ai_family = AF_INET;
481 hints.ai_socktype = SOCK_DGRAM;
482 hints.ai_protocol = IPPROTO_UDP;
483 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
485 syslog(LOG_ERR, "getaddrinfo udp: %s",
486 gai_strerror(ecode));
489 nconf_udp = getnetconfigent("udp");
490 if (nconf_udp == NULL)
491 err(1, "getnetconfigent udp failed");
492 nb_udp.buf = ai_udp->ai_addr;
493 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
494 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) ||
495 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp)))
496 err(1, "rpcb_set udp failed");
497 freeaddrinfo(ai_udp);
501 /* Set up the socket for udp6 and rpcb register it. */
502 if (udpflag && ip6flag) {
504 for (i = 0; i < bindhostc; i++) {
505 memset(&hints, 0, sizeof hints);
506 hints.ai_flags = AI_PASSIVE;
507 hints.ai_family = AF_INET6;
508 hints.ai_socktype = SOCK_DGRAM;
509 hints.ai_protocol = IPPROTO_UDP;
510 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
513 if ((sock = socket(ai_udp6->ai_family,
514 ai_udp6->ai_socktype,
515 ai_udp6->ai_protocol)) < 0) {
517 "can't create udp6 socket");
520 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
521 &on, sizeof on) < 0) {
523 "can't set v6-only binding for "
527 if (bind(sock, ai_udp6->ai_addr,
528 ai_udp6->ai_addrlen) < 0) {
530 "can't bind udp6 addr %s: %m",
534 freeaddrinfo(ai_udp6);
535 addsockargs.sock = sock;
536 addsockargs.name = NULL;
537 addsockargs.namelen = 0;
538 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
540 "can't add UDP6 socket");
547 memset(&hints, 0, sizeof hints);
548 hints.ai_flags = AI_PASSIVE;
549 hints.ai_family = AF_INET6;
550 hints.ai_socktype = SOCK_DGRAM;
551 hints.ai_protocol = IPPROTO_UDP;
552 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
554 syslog(LOG_ERR, "getaddrinfo udp6: %s",
555 gai_strerror(ecode));
558 nconf_udp6 = getnetconfigent("udp6");
559 if (nconf_udp6 == NULL)
560 err(1, "getnetconfigent udp6 failed");
561 nb_udp6.buf = ai_udp6->ai_addr;
562 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
563 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) ||
564 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6)))
565 err(1, "rpcb_set udp6 failed");
566 freeaddrinfo(ai_udp6);
570 /* Set up the socket for tcp and rpcb register it. */
573 for (i = 0; i < bindhostc; i++) {
574 memset(&hints, 0, sizeof hints);
575 hints.ai_flags = AI_PASSIVE;
576 hints.ai_family = AF_INET;
577 hints.ai_socktype = SOCK_STREAM;
578 hints.ai_protocol = IPPROTO_TCP;
579 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
582 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
585 "can't create tpc socket");
588 if (setsockopt(tcpsock, SOL_SOCKET,
590 (char *)&on, sizeof(on)) < 0)
592 "setsockopt SO_REUSEADDR: %m");
593 if (bind(tcpsock, ai_tcp->ai_addr,
594 ai_tcp->ai_addrlen) < 0) {
596 "can't bind tcp addr %s: %m",
600 if (listen(tcpsock, 5) < 0) {
601 syslog(LOG_ERR, "listen failed");
604 freeaddrinfo(ai_tcp);
605 FD_SET(tcpsock, &sockbits);
606 FD_SET(tcpsock, &v4bits);
612 memset(&hints, 0, sizeof hints);
613 hints.ai_flags = AI_PASSIVE;
614 hints.ai_family = AF_INET;
615 hints.ai_socktype = SOCK_STREAM;
616 hints.ai_protocol = IPPROTO_TCP;
617 ecode = getaddrinfo(NULL, "nfs", &hints,
620 syslog(LOG_ERR, "getaddrinfo tcp: %s",
621 gai_strerror(ecode));
624 nconf_tcp = getnetconfigent("tcp");
625 if (nconf_tcp == NULL)
626 err(1, "getnetconfigent tcp failed");
627 nb_tcp.buf = ai_tcp->ai_addr;
628 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
629 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
630 &nb_tcp)) || (!rpcb_set(NFS_PROGRAM, 3,
631 nconf_tcp, &nb_tcp)))
632 err(1, "rpcb_set tcp failed");
633 freeaddrinfo(ai_tcp);
637 /* Set up the socket for tcp6 and rpcb register it. */
638 if (tcpflag && ip6flag) {
640 for (i = 0; i < bindhostc; i++) {
641 memset(&hints, 0, sizeof hints);
642 hints.ai_flags = AI_PASSIVE;
643 hints.ai_family = AF_INET6;
644 hints.ai_socktype = SOCK_STREAM;
645 hints.ai_protocol = IPPROTO_TCP;
646 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
649 if ((tcp6sock = socket(ai_tcp6->ai_family,
650 ai_tcp6->ai_socktype,
651 ai_tcp6->ai_protocol)) < 0) {
653 "can't create tcp6 socket");
656 if (setsockopt(tcp6sock, SOL_SOCKET,
658 (char *)&on, sizeof(on)) < 0)
660 "setsockopt SO_REUSEADDR: %m");
661 if (setsockopt(tcp6sock, IPPROTO_IPV6,
662 IPV6_V6ONLY, &on, sizeof on) < 0) {
664 "can't set v6-only binding for tcp6 "
668 if (bind(tcp6sock, ai_tcp6->ai_addr,
669 ai_tcp6->ai_addrlen) < 0) {
671 "can't bind tcp6 addr %s: %m",
675 if (listen(tcp6sock, 5) < 0) {
676 syslog(LOG_ERR, "listen failed");
679 freeaddrinfo(ai_tcp6);
680 FD_SET(tcp6sock, &sockbits);
681 FD_SET(tcp6sock, &v6bits);
682 if (maxsock < tcp6sock)
688 memset(&hints, 0, sizeof hints);
689 hints.ai_flags = AI_PASSIVE;
690 hints.ai_family = AF_INET6;
691 hints.ai_socktype = SOCK_STREAM;
692 hints.ai_protocol = IPPROTO_TCP;
693 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
695 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
696 gai_strerror(ecode));
699 nconf_tcp6 = getnetconfigent("tcp6");
700 if (nconf_tcp6 == NULL)
701 err(1, "getnetconfigent tcp6 failed");
702 nb_tcp6.buf = ai_tcp6->ai_addr;
703 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
704 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) ||
705 (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6)))
706 err(1, "rpcb_set tcp6 failed");
707 freeaddrinfo(ai_tcp6);
711 if (rpcbregcnt == 0) {
712 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
716 if (tcpflag && connect_type_cnt == 0) {
717 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
721 setproctitle("master");
723 * We always want a master to have a clean way to to shut nfsd down
724 * (with unregistration): if the master is killed, it unregisters and
725 * kills all children. If we run for UDP only (and so do not have to
726 * loop waiting waiting for accept), we instead make the parent
727 * a "server" too. start_server will not return.
733 * Loop forever accepting connections and passing the sockets
734 * into the kernel for the mounts.
738 if (connect_type_cnt > 1) {
739 if (select(maxsock + 1,
740 &ready, NULL, NULL, NULL) < 1) {
741 syslog(LOG_ERR, "select failed: %m");
747 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
748 if (FD_ISSET(tcpsock, &ready)) {
749 if (FD_ISSET(tcpsock, &v4bits)) {
750 len = sizeof(inetpeer);
751 if ((msgsock = accept(tcpsock,
752 (struct sockaddr *)&inetpeer, &len)) < 0) {
753 syslog(LOG_ERR, "accept failed: %m");
754 if (errno == ECONNABORTED ||
759 memset(inetpeer.sin_zero, 0,
760 sizeof(inetpeer.sin_zero));
761 if (setsockopt(msgsock, SOL_SOCKET,
762 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
764 "setsockopt SO_KEEPALIVE: %m");
765 addsockargs.sock = msgsock;
766 addsockargs.name = (caddr_t)&inetpeer;
767 addsockargs.namelen = len;
768 nfssvc(nfssvc_addsock, &addsockargs);
769 (void)close(msgsock);
770 } else if (FD_ISSET(tcpsock, &v6bits)) {
771 len = sizeof(inet6peer);
772 if ((msgsock = accept(tcpsock,
773 (struct sockaddr *)&inet6peer,
776 "accept failed: %m");
777 if (errno == ECONNABORTED ||
782 if (setsockopt(msgsock, SOL_SOCKET,
783 SO_KEEPALIVE, (char *)&on,
785 syslog(LOG_ERR, "setsockopt "
787 addsockargs.sock = msgsock;
788 addsockargs.name = (caddr_t)&inet6peer;
789 addsockargs.namelen = len;
790 nfssvc(nfssvc_addsock, &addsockargs);
791 (void)close(msgsock);
799 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
802 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
805 if (bindhost == NULL || strcmp("*", bindhost) == 0)
810 if (hostptr != NULL) {
811 switch (hints.ai_family) {
813 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
814 hints.ai_flags = AI_NUMERICHOST;
816 if (inet_pton(AF_INET6, hostptr,
822 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
823 hints.ai_flags = AI_NUMERICHOST;
825 if (inet_pton(AF_INET, hostptr,
835 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
837 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
838 gai_strerror(ecode));
847 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
852 nonfs(__unused int signo)
854 syslog(LOG_ERR, "missing system call: NFS not available");
858 reapchild(__unused int signo)
863 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
864 for (i = 0; i < nfsdcnt; i++)
865 if (pid == children[i])
873 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
874 (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
875 syslog(LOG_ERR, "rpcb_unset failed");
883 for (i = 0; i < nfsdcnt; i++) {
885 kill(children[i], SIGKILL);
890 * Cleanup master after SIGUSR1.
893 cleanup(__unused int signo)
899 * Cleanup child after SIGUSR1.
902 child_cleanup(__unused int signo)
908 nfsd_exit(int status)
916 start_server(int master)
918 char principal[MAXHOSTNAMELEN + 5];
919 struct nfsd_nfsd_args nfsdargs;
921 char hostname[MAXHOSTNAMELEN + 1], *cp;
922 struct addrinfo *aip, hints;
926 gethostname(hostname, sizeof (hostname));
927 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
928 if ((cp = strchr(hostname, '.')) == NULL ||
930 /* If not fully qualified, try getaddrinfo() */
931 memset((void *)&hints, 0, sizeof (hints));
932 hints.ai_flags = AI_CANONNAME;
933 error = getaddrinfo(hostname, NULL, &hints, &aip);
935 if (aip->ai_canonname != NULL &&
936 (cp = strchr(aip->ai_canonname, '.')) !=
937 NULL && *(cp + 1) != '\0')
938 snprintf(principal, sizeof (principal),
939 "nfs@%s", aip->ai_canonname);
943 nfsdargs.principal = principal;
944 nfsdargs.minthreads = nfsdcnt;
945 nfsdargs.maxthreads = nfsdcnt;
946 error = nfssvc(nfssvc_nfsd, &nfsdargs);
947 if (error < 0 && errno == EAUTH) {
949 * This indicates that it could not register the
950 * rpcsec_gss credentials, usually because the
951 * gssd daemon isn't running.
952 * (only the experimental server with nfsv4)
954 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
956 error = nfssvc(nfssvc_nfsd, &nfsdargs);
959 syslog(LOG_ERR, "nfssvc: %m");
963 if (nfssvc(NFSSVC_OLDNFSD, NULL) < 0) {
964 syslog(LOG_ERR, "nfssvc: %m");