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>
59 #include <arpa/inet.h>
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
62 #include <nfsserver/nfs.h>
63 #include <nfs/nfssvc.h>
76 #define syslog(e, s...) fprintf(stderr,s)
82 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart"
83 #define MAXNFSDCNT 256
85 pid_t children[MAXNFSDCNT]; /* PIDs of children */
86 int nfsdcnt; /* number of children */
88 int run_v4server = 0; /* Force running of nfsv4 server */
89 int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */
92 void child_cleanup(int);
93 void killchildren(void);
97 int setbindhost(struct addrinfo **ia, const char *bindhost,
98 struct addrinfo hints);
99 void start_server(int);
100 void unregistration(void);
104 * Nfs server daemon mostly just a user context for nfssvc()
106 * 1 - do file descriptor and signal cleanup
107 * 2 - fork the nfsd(s)
108 * 3 - create server socket(s)
109 * 4 - register socket with rpcbind
111 * For connectionless protocols, just pass the socket into the kernel via.
113 * For connection based sockets, loop doing accepts. When you get a new
114 * socket from accept, pass the msgsock into the kernel via. nfssvc().
116 * -r - reregister with rpcbind
117 * -d - unregister with rpcbind
118 * -t - support tcp nfs clients
119 * -u - support udp nfs clients
120 * -4 - forces it to run a server that supports nfsv4
121 * followed by "n" which is the number of nfsds' to fork off
124 main(int argc, char **argv)
126 struct nfsd_addsock_args addsockargs;
127 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
128 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
129 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
130 struct sockaddr_in inetpeer;
131 struct sockaddr_in6 inet6peer;
132 fd_set ready, sockbits;
133 fd_set v4bits, v6bits;
134 int ch, connect_type_cnt, i, maxsock, msgsock;
136 int on = 1, unregister, reregister, sock;
137 int tcp6sock, ip6flag, tcpflag, tcpsock;
138 int udpflag, ecode, s, srvcnt;
139 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
140 int stablefd, nfssvc_addsock;
141 char **bindhost = NULL;
144 nfsdcnt = DEFNFSDCNT;
145 unregister = reregister = tcpflag = maxsock = 0;
146 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
147 #define GETOPT "ah:n:rdtu4"
148 #define USAGE "[-ardtu4] [-n num_servers] [-h bindip]"
149 while ((ch = getopt(argc, argv, GETOPT)) != -1)
155 nfsdcnt = atoi(optarg);
156 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
157 warnx("nfsd count %d; reset to %d", nfsdcnt,
159 nfsdcnt = DEFNFSDCNT;
164 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
165 if (bindhost == NULL)
166 errx(1, "Out of memory");
167 bindhost[bindhostc-1] = strdup(optarg);
168 if (bindhost[bindhostc-1] == NULL)
169 errx(1, "Out of memory");
190 if (!tcpflag && !udpflag)
197 * Backward compatibility, trailing number is the count of daemons.
202 nfsdcnt = atoi(argv[0]);
203 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
204 warnx("nfsd count %d; reset to %d", nfsdcnt,
206 nfsdcnt = DEFNFSDCNT;
211 * If the "-4" option was specified OR only the nfsd module is
212 * found in the server, run "nfsd".
213 * Otherwise, try and run "nfsserver".
215 if (run_v4server > 0) {
216 if (modfind("nfsd") < 0) {
217 /* Not present in kernel, try loading it */
218 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
219 errx(1, "NFS server is not available");
221 } else if (modfind("nfsserver") < 0 && modfind("nfsd") >= 0) {
223 } else if (modfind("nfsserver") < 0) {
224 /* Not present in kernel, try loading it */
225 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
226 errx(1, "NFS server is not available");
230 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
232 if (errno != EPROTONOSUPPORT)
235 } else if (getnetconfigent("udp6") == NULL ||
236 getnetconfigent("tcp6") == NULL) {
242 if (bindhostc == 0 || bindanyflag) {
244 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
245 if (bindhost == NULL)
246 errx(1, "Out of memory");
247 bindhost[bindhostc-1] = strdup("*");
248 if (bindhost[bindhostc-1] == NULL)
249 errx(1, "Out of memory");
258 memset(&hints, 0, sizeof hints);
259 hints.ai_flags = AI_PASSIVE;
260 hints.ai_family = AF_INET;
261 hints.ai_socktype = SOCK_DGRAM;
262 hints.ai_protocol = IPPROTO_UDP;
263 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
265 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
266 nconf_udp = getnetconfigent("udp");
267 if (nconf_udp == NULL)
268 err(1, "getnetconfigent udp failed");
269 nb_udp.buf = ai_udp->ai_addr;
270 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
271 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp)) ||
272 (!rpcb_set(RPCPROG_NFS, 3, nconf_udp, &nb_udp)))
273 err(1, "rpcb_set udp failed");
274 freeaddrinfo(ai_udp);
276 if (udpflag && ip6flag) {
277 memset(&hints, 0, sizeof hints);
278 hints.ai_flags = AI_PASSIVE;
279 hints.ai_family = AF_INET6;
280 hints.ai_socktype = SOCK_DGRAM;
281 hints.ai_protocol = IPPROTO_UDP;
282 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
284 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
285 nconf_udp6 = getnetconfigent("udp6");
286 if (nconf_udp6 == NULL)
287 err(1, "getnetconfigent udp6 failed");
288 nb_udp6.buf = ai_udp6->ai_addr;
289 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
290 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6)) ||
291 (!rpcb_set(RPCPROG_NFS, 3, nconf_udp6, &nb_udp6)))
292 err(1, "rpcb_set udp6 failed");
293 freeaddrinfo(ai_udp6);
296 memset(&hints, 0, sizeof hints);
297 hints.ai_flags = AI_PASSIVE;
298 hints.ai_family = AF_INET;
299 hints.ai_socktype = SOCK_STREAM;
300 hints.ai_protocol = IPPROTO_TCP;
301 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
303 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
304 nconf_tcp = getnetconfigent("tcp");
305 if (nconf_tcp == NULL)
306 err(1, "getnetconfigent tcp failed");
307 nb_tcp.buf = ai_tcp->ai_addr;
308 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
309 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp)) ||
310 (!rpcb_set(RPCPROG_NFS, 3, nconf_tcp, &nb_tcp)))
311 err(1, "rpcb_set tcp failed");
312 freeaddrinfo(ai_tcp);
314 if (tcpflag && ip6flag) {
315 memset(&hints, 0, sizeof hints);
316 hints.ai_flags = AI_PASSIVE;
317 hints.ai_family = AF_INET6;
318 hints.ai_socktype = SOCK_STREAM;
319 hints.ai_protocol = IPPROTO_TCP;
320 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
322 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
323 nconf_tcp6 = getnetconfigent("tcp6");
324 if (nconf_tcp6 == NULL)
325 err(1, "getnetconfigent tcp6 failed");
326 nb_tcp6.buf = ai_tcp6->ai_addr;
327 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
328 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6)) ||
329 (!rpcb_set(RPCPROG_NFS, 3, nconf_tcp6, &nb_tcp6)))
330 err(1, "rpcb_set tcp6 failed");
331 freeaddrinfo(ai_tcp6);
337 (void)signal(SIGHUP, SIG_IGN);
338 (void)signal(SIGINT, SIG_IGN);
340 * nfsd sits in the kernel most of the time. It needs
341 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
342 * as possible during a shutdown, otherwise loopback
343 * mounts will not be able to unmount.
345 (void)signal(SIGTERM, SIG_IGN);
346 (void)signal(SIGQUIT, SIG_IGN);
348 (void)signal(SIGSYS, nonfs);
349 (void)signal(SIGCHLD, reapchild);
351 openlog("nfsd", LOG_PID, LOG_DAEMON);
354 * For V4, we open the stablerestart file and call nfssvc()
355 * to get it loaded. This is done before the daemons do the
356 * regular nfssvc() call to service NFS requests.
357 * (This way the file remains open until the last nfsd is killed
359 * Note that this file is not created by this daemon and can
360 * only be relocated by recompiling the daemon, in order to
361 * minimize accidentally starting up with the wrong file.
362 * If should be created as an empty file Read and Write for
363 * root before the first time you run NFS v4 and should never
364 * be re-initialized if at all possible. It should live on a
365 * local, non-volatile storage device that does not do hardware
366 * level write-back caching. (See SCSI doc for more information
367 * on how to prevent write-back caching on SCSI disks.)
369 if (run_v4server > 0) {
370 stablefd = open(NFSD_STABLERESTART, O_RDWR, 0);
372 syslog(LOG_ERR, "Can't open %s\n", NFSD_STABLERESTART);
375 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
376 syslog(LOG_ERR, "Can't read stable storage file\n");
379 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
380 nfssvc_nfsd = NFSSVC_NFSDNFSD;
383 nfssvc_addsock = NFSSVC_ADDSOCK;
384 nfssvc_nfsd = NFSSVC_NFSD;
386 * Figure out if the kernel supports the new-style
387 * NFSSVC_NFSD. Old kernels will return ENXIO because they
388 * don't recognise the flag value, new ones will return EINVAL
389 * because argp is NULL.
392 if (nfssvc(NFSSVC_NFSD, NULL) < 0 && errno == EINVAL)
397 /* If we use UDP only, we start the last server below. */
398 srvcnt = tcpflag ? nfsdcnt : nfsdcnt - 1;
399 for (i = 0; i < srvcnt; i++) {
400 switch ((pid = fork())) {
402 syslog(LOG_ERR, "fork: %m");
410 (void)signal(SIGUSR1, child_cleanup);
411 setproctitle("server");
415 } else if (tcpflag) {
417 * For TCP mode, we fork once to start the first
418 * kernel nfsd thread. The kernel will add more
423 syslog(LOG_ERR, "fork: %m");
429 (void)signal(SIGUSR1, child_cleanup);
430 setproctitle("server");
435 (void)signal(SIGUSR1, cleanup);
441 /* Set up the socket for udp and rpcb register it. */
444 for (i = 0; i < bindhostc; i++) {
445 memset(&hints, 0, sizeof hints);
446 hints.ai_flags = AI_PASSIVE;
447 hints.ai_family = AF_INET;
448 hints.ai_socktype = SOCK_DGRAM;
449 hints.ai_protocol = IPPROTO_UDP;
450 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
453 if ((sock = socket(ai_udp->ai_family,
455 ai_udp->ai_protocol)) < 0) {
457 "can't create udp socket");
460 if (bind(sock, ai_udp->ai_addr,
461 ai_udp->ai_addrlen) < 0) {
463 "can't bind udp addr %s: %m",
467 freeaddrinfo(ai_udp);
468 addsockargs.sock = sock;
469 addsockargs.name = NULL;
470 addsockargs.namelen = 0;
471 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
472 syslog(LOG_ERR, "can't Add UDP socket");
479 memset(&hints, 0, sizeof hints);
480 hints.ai_flags = AI_PASSIVE;
481 hints.ai_family = AF_INET;
482 hints.ai_socktype = SOCK_DGRAM;
483 hints.ai_protocol = IPPROTO_UDP;
484 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
486 syslog(LOG_ERR, "getaddrinfo udp: %s",
487 gai_strerror(ecode));
490 nconf_udp = getnetconfigent("udp");
491 if (nconf_udp == NULL)
492 err(1, "getnetconfigent udp failed");
493 nb_udp.buf = ai_udp->ai_addr;
494 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
495 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp)) ||
496 (!rpcb_set(RPCPROG_NFS, 3, nconf_udp, &nb_udp)))
497 err(1, "rpcb_set udp failed");
498 freeaddrinfo(ai_udp);
502 /* Set up the socket for udp6 and rpcb register it. */
503 if (udpflag && ip6flag) {
505 for (i = 0; i < bindhostc; i++) {
506 memset(&hints, 0, sizeof hints);
507 hints.ai_flags = AI_PASSIVE;
508 hints.ai_family = AF_INET6;
509 hints.ai_socktype = SOCK_DGRAM;
510 hints.ai_protocol = IPPROTO_UDP;
511 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
514 if ((sock = socket(ai_udp6->ai_family,
515 ai_udp6->ai_socktype,
516 ai_udp6->ai_protocol)) < 0) {
518 "can't create udp6 socket");
521 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
522 &on, sizeof on) < 0) {
524 "can't set v6-only binding for "
528 if (bind(sock, ai_udp6->ai_addr,
529 ai_udp6->ai_addrlen) < 0) {
531 "can't bind udp6 addr %s: %m",
535 freeaddrinfo(ai_udp6);
536 addsockargs.sock = sock;
537 addsockargs.name = NULL;
538 addsockargs.namelen = 0;
539 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
541 "can't add UDP6 socket");
548 memset(&hints, 0, sizeof hints);
549 hints.ai_flags = AI_PASSIVE;
550 hints.ai_family = AF_INET6;
551 hints.ai_socktype = SOCK_DGRAM;
552 hints.ai_protocol = IPPROTO_UDP;
553 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
555 syslog(LOG_ERR, "getaddrinfo udp6: %s",
556 gai_strerror(ecode));
559 nconf_udp6 = getnetconfigent("udp6");
560 if (nconf_udp6 == NULL)
561 err(1, "getnetconfigent udp6 failed");
562 nb_udp6.buf = ai_udp6->ai_addr;
563 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
564 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6)) ||
565 (!rpcb_set(RPCPROG_NFS, 3, nconf_udp6, &nb_udp6)))
566 err(1, "rpcb_set udp6 failed");
567 freeaddrinfo(ai_udp6);
571 /* Set up the socket for tcp and rpcb register it. */
574 for (i = 0; i < bindhostc; i++) {
575 memset(&hints, 0, sizeof hints);
576 hints.ai_flags = AI_PASSIVE;
577 hints.ai_family = AF_INET;
578 hints.ai_socktype = SOCK_STREAM;
579 hints.ai_protocol = IPPROTO_TCP;
580 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
583 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
586 "can't create tpc socket");
589 if (setsockopt(tcpsock, SOL_SOCKET,
591 (char *)&on, sizeof(on)) < 0)
593 "setsockopt SO_REUSEADDR: %m");
594 if (bind(tcpsock, ai_tcp->ai_addr,
595 ai_tcp->ai_addrlen) < 0) {
597 "can't bind tcp addr %s: %m",
601 if (listen(tcpsock, 5) < 0) {
602 syslog(LOG_ERR, "listen failed");
605 freeaddrinfo(ai_tcp);
606 FD_SET(tcpsock, &sockbits);
607 FD_SET(tcpsock, &v4bits);
613 memset(&hints, 0, sizeof hints);
614 hints.ai_flags = AI_PASSIVE;
615 hints.ai_family = AF_INET;
616 hints.ai_socktype = SOCK_STREAM;
617 hints.ai_protocol = IPPROTO_TCP;
618 ecode = getaddrinfo(NULL, "nfs", &hints,
621 syslog(LOG_ERR, "getaddrinfo tcp: %s",
622 gai_strerror(ecode));
625 nconf_tcp = getnetconfigent("tcp");
626 if (nconf_tcp == NULL)
627 err(1, "getnetconfigent tcp failed");
628 nb_tcp.buf = ai_tcp->ai_addr;
629 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
630 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_tcp,
631 &nb_tcp)) || (!rpcb_set(RPCPROG_NFS, 3,
632 nconf_tcp, &nb_tcp)))
633 err(1, "rpcb_set tcp failed");
634 freeaddrinfo(ai_tcp);
638 /* Set up the socket for tcp6 and rpcb register it. */
639 if (tcpflag && ip6flag) {
641 for (i = 0; i < bindhostc; i++) {
642 memset(&hints, 0, sizeof hints);
643 hints.ai_flags = AI_PASSIVE;
644 hints.ai_family = AF_INET6;
645 hints.ai_socktype = SOCK_STREAM;
646 hints.ai_protocol = IPPROTO_TCP;
647 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
650 if ((tcp6sock = socket(ai_tcp6->ai_family,
651 ai_tcp6->ai_socktype,
652 ai_tcp6->ai_protocol)) < 0) {
654 "can't create tcp6 socket");
657 if (setsockopt(tcp6sock, SOL_SOCKET,
659 (char *)&on, sizeof(on)) < 0)
661 "setsockopt SO_REUSEADDR: %m");
662 if (setsockopt(tcp6sock, IPPROTO_IPV6,
663 IPV6_V6ONLY, &on, sizeof on) < 0) {
665 "can't set v6-only binding for tcp6 "
669 if (bind(tcp6sock, ai_tcp6->ai_addr,
670 ai_tcp6->ai_addrlen) < 0) {
672 "can't bind tcp6 addr %s: %m",
676 if (listen(tcp6sock, 5) < 0) {
677 syslog(LOG_ERR, "listen failed");
680 freeaddrinfo(ai_tcp6);
681 FD_SET(tcp6sock, &sockbits);
682 FD_SET(tcp6sock, &v6bits);
683 if (maxsock < tcp6sock)
689 memset(&hints, 0, sizeof hints);
690 hints.ai_flags = AI_PASSIVE;
691 hints.ai_family = AF_INET6;
692 hints.ai_socktype = SOCK_STREAM;
693 hints.ai_protocol = IPPROTO_TCP;
694 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
696 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
697 gai_strerror(ecode));
700 nconf_tcp6 = getnetconfigent("tcp6");
701 if (nconf_tcp6 == NULL)
702 err(1, "getnetconfigent tcp6 failed");
703 nb_tcp6.buf = ai_tcp6->ai_addr;
704 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
705 if ((!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6)) ||
706 (!rpcb_set(RPCPROG_NFS, 3, nconf_tcp6, &nb_tcp6)))
707 err(1, "rpcb_set tcp6 failed");
708 freeaddrinfo(ai_tcp6);
712 if (rpcbregcnt == 0) {
713 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
717 if (tcpflag && connect_type_cnt == 0) {
718 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
722 setproctitle("master");
724 * We always want a master to have a clean way to to shut nfsd down
725 * (with unregistration): if the master is killed, it unregisters and
726 * kills all children. If we run for UDP only (and so do not have to
727 * loop waiting waiting for accept), we instead make the parent
728 * a "server" too. start_server will not return.
734 * Loop forever accepting connections and passing the sockets
735 * into the kernel for the mounts.
739 if (connect_type_cnt > 1) {
740 if (select(maxsock + 1,
741 &ready, NULL, NULL, NULL) < 1) {
742 syslog(LOG_ERR, "select failed: %m");
748 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
749 if (FD_ISSET(tcpsock, &ready)) {
750 if (FD_ISSET(tcpsock, &v4bits)) {
751 len = sizeof(inetpeer);
752 if ((msgsock = accept(tcpsock,
753 (struct sockaddr *)&inetpeer, &len)) < 0) {
754 syslog(LOG_ERR, "accept failed: %m");
755 if (errno == ECONNABORTED ||
760 memset(inetpeer.sin_zero, 0,
761 sizeof(inetpeer.sin_zero));
762 if (setsockopt(msgsock, SOL_SOCKET,
763 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
765 "setsockopt SO_KEEPALIVE: %m");
766 addsockargs.sock = msgsock;
767 addsockargs.name = (caddr_t)&inetpeer;
768 addsockargs.namelen = len;
769 nfssvc(nfssvc_addsock, &addsockargs);
770 (void)close(msgsock);
771 } else if (FD_ISSET(tcpsock, &v6bits)) {
772 len = sizeof(inet6peer);
773 if ((msgsock = accept(tcpsock,
774 (struct sockaddr *)&inet6peer,
777 "accept failed: %m");
778 if (errno == ECONNABORTED ||
783 if (setsockopt(msgsock, SOL_SOCKET,
784 SO_KEEPALIVE, (char *)&on,
786 syslog(LOG_ERR, "setsockopt "
788 addsockargs.sock = msgsock;
789 addsockargs.name = (caddr_t)&inet6peer;
790 addsockargs.namelen = len;
791 nfssvc(nfssvc_addsock, &addsockargs);
792 (void)close(msgsock);
800 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
803 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
806 if (bindhost == NULL || strcmp("*", bindhost) == 0)
811 if (hostptr != NULL) {
812 switch (hints.ai_family) {
814 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
815 hints.ai_flags = AI_NUMERICHOST;
817 if (inet_pton(AF_INET6, hostptr,
823 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
824 hints.ai_flags = AI_NUMERICHOST;
826 if (inet_pton(AF_INET, hostptr,
836 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
838 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
839 gai_strerror(ecode));
848 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
853 nonfs(__unused int signo)
855 syslog(LOG_ERR, "missing system call: NFS not available");
859 reapchild(__unused int signo)
864 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
865 for (i = 0; i < nfsdcnt; i++)
866 if (pid == children[i])
874 if ((!rpcb_unset(RPCPROG_NFS, 2, NULL)) ||
875 (!rpcb_unset(RPCPROG_NFS, 3, NULL)))
876 syslog(LOG_ERR, "rpcb_unset failed");
884 for (i = 0; i < nfsdcnt; i++) {
886 kill(children[i], SIGKILL);
891 * Cleanup master after SIGUSR1.
894 cleanup(__unused int signo)
900 * Cleanup child after SIGUSR1.
903 child_cleanup(__unused int signo)
909 nfsd_exit(int status)
917 start_server(int master)
919 char principal[MAXHOSTNAMELEN + 5];
920 struct nfsd_nfsd_args nfsdargs;
922 char hostname[MAXHOSTNAMELEN + 1], *cp;
923 struct addrinfo *aip, hints;
927 gethostname(hostname, sizeof (hostname));
928 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
929 if ((cp = strchr(hostname, '.')) == NULL ||
931 /* If not fully qualified, try getaddrinfo() */
932 memset((void *)&hints, 0, sizeof (hints));
933 hints.ai_flags = AI_CANONNAME;
934 error = getaddrinfo(hostname, NULL, &hints, &aip);
936 if (aip->ai_canonname != NULL &&
937 (cp = strchr(aip->ai_canonname, '.')) !=
938 NULL && *(cp + 1) != '\0')
939 snprintf(principal, sizeof (principal),
940 "nfs@%s", aip->ai_canonname);
944 nfsdargs.principal = principal;
945 nfsdargs.minthreads = nfsdcnt;
946 nfsdargs.maxthreads = nfsdcnt;
947 error = nfssvc(nfssvc_nfsd, &nfsdargs);
948 if (error < 0 && errno == EAUTH) {
950 * This indicates that it could not register the
951 * rpcsec_gss credentials, usually because the
952 * gssd daemon isn't running.
953 * (only the experimental server with nfsv4)
955 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
957 error = nfssvc(nfssvc_nfsd, &nfsdargs);
960 syslog(LOG_ERR, "nfssvc: %m");
964 if (nfssvc(NFSSVC_OLDNFSD, NULL) < 0) {
965 syslog(LOG_ERR, "nfssvc: %m");