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>
54 #include <sys/types.h>
56 #include <sys/sysctl.h>
57 #include <sys/ucred.h>
60 #include <rpc/pmap_clnt.h>
61 #include <rpcsvc/nfs_prot.h>
64 #include <arpa/inet.h>
65 #include <nfsserver/nfs.h>
66 #include <nfs/nfssvc.h>
81 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart"
82 #define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak"
83 #define MAXNFSDCNT 256
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;
96 static int minthreads_set;
97 static int maxthreads_set;
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 },
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);
123 * Nfs server daemon mostly just a user context for nfssvc()
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
130 * For connectionless protocols, just pass the socket into the kernel via.
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().
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
143 main(int argc, char **argv)
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;
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;
162 char **bindhost = NULL;
165 nfsdcnt = DEFNFSDCNT;
166 unregister = reregister = tcpflag = maxsock = 0;
167 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
168 getopt_shortopts = "ah:n:rdtue";
171 " nfsd [-ardtue] [-h bindip]\n"
172 " [-n numservers] [--minthreads #] [--maxthreads #]\n";
173 while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
180 set_nfsdcnt(atoi(optarg));
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");
204 /* now a no-op, since this is the default */
207 lopt = longopts[longindex].name;
208 if (!strcmp(lopt, "minthreads")) {
209 minthreads = atoi(optarg);
210 } else if (!strcmp(lopt, "maxthreads")) {
211 maxthreads = atoi(optarg);
218 if (!tcpflag && !udpflag)
222 if (minthreads_set && maxthreads_set && minthreads > maxthreads)
224 "error: minthreads(%d) can't be greater than "
225 "maxthreads(%d)", minthreads, maxthreads);
229 * Backward compatibility, trailing number is the count of daemons.
234 set_nfsdcnt(atoi(argv[0]));
237 * Unless the "-o" option was specified, try and run "nfsd".
238 * If "-o" was specified, try and run "nfsserver".
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");
247 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
249 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
252 } else if (getnetconfigent("udp6") == NULL ||
253 getnetconfigent("tcp6") == NULL) {
259 if (bindhostc == 0 || bindanyflag) {
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");
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);
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);
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);
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);
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);
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);
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);
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);
354 (void)signal(SIGHUP, SIG_IGN);
355 (void)signal(SIGINT, SIG_IGN);
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.
362 (void)signal(SIGTERM, SIG_IGN);
363 (void)signal(SIGQUIT, SIG_IGN);
365 (void)signal(SIGSYS, nonfs);
366 (void)signal(SIGCHLD, reapchild);
367 (void)signal(SIGUSR2, backup_stable);
369 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
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
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.)
384 open_stable(&stablefd, &backupfd);
386 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
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");
395 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
396 nfssvc_nfsd = NFSSVC_NFSDNFSD;
400 * For TCP mode, we fork once to start the first
401 * kernel nfsd thread. The kernel will add more
406 syslog(LOG_ERR, "fork: %m");
412 (void)signal(SIGUSR1, child_cleanup);
413 setproctitle("server");
418 (void)signal(SIGUSR1, cleanup);
424 /* Set up the socket for udp and rpcb register it. */
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) {
436 if ((sock = socket(ai_udp->ai_family,
438 ai_udp->ai_protocol)) < 0) {
440 "can't create udp socket");
443 if (bind(sock, ai_udp->ai_addr,
444 ai_udp->ai_addrlen) < 0) {
446 "can't bind udp addr %s: %m",
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");
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);
469 syslog(LOG_ERR, "getaddrinfo udp: %s",
470 gai_strerror(ecode));
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);
485 /* Set up the socket for udp6 and rpcb register it. */
486 if (udpflag && ip6flag) {
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) {
497 if ((sock = socket(ai_udp6->ai_family,
498 ai_udp6->ai_socktype,
499 ai_udp6->ai_protocol)) < 0) {
501 "can't create udp6 socket");
504 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
505 &on, sizeof on) < 0) {
507 "can't set v6-only binding for "
511 if (bind(sock, ai_udp6->ai_addr,
512 ai_udp6->ai_addrlen) < 0) {
514 "can't bind udp6 addr %s: %m",
518 freeaddrinfo(ai_udp6);
519 addsockargs.sock = sock;
520 addsockargs.name = NULL;
521 addsockargs.namelen = 0;
522 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
524 "can't add UDP6 socket");
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);
538 syslog(LOG_ERR, "getaddrinfo udp6: %s",
539 gai_strerror(ecode));
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);
554 /* Set up the socket for tcp and rpcb register it. */
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) {
566 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
569 "can't create tcp socket");
572 if (setsockopt(tcpsock, SOL_SOCKET,
574 (char *)&on, sizeof(on)) < 0)
576 "setsockopt SO_REUSEADDR: %m");
577 if (bind(tcpsock, ai_tcp->ai_addr,
578 ai_tcp->ai_addrlen) < 0) {
580 "can't bind tcp addr %s: %m",
584 if (listen(tcpsock, -1) < 0) {
585 syslog(LOG_ERR, "listen failed");
588 freeaddrinfo(ai_tcp);
589 FD_SET(tcpsock, &sockbits);
590 FD_SET(tcpsock, &v4bits);
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,
604 syslog(LOG_ERR, "getaddrinfo tcp: %s",
605 gai_strerror(ecode));
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);
621 /* Set up the socket for tcp6 and rpcb register it. */
622 if (tcpflag && ip6flag) {
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) {
633 if ((tcp6sock = socket(ai_tcp6->ai_family,
634 ai_tcp6->ai_socktype,
635 ai_tcp6->ai_protocol)) < 0) {
637 "can't create tcp6 socket");
640 if (setsockopt(tcp6sock, SOL_SOCKET,
642 (char *)&on, sizeof(on)) < 0)
644 "setsockopt SO_REUSEADDR: %m");
645 if (setsockopt(tcp6sock, IPPROTO_IPV6,
646 IPV6_V6ONLY, &on, sizeof on) < 0) {
648 "can't set v6-only binding for tcp6 "
652 if (bind(tcp6sock, ai_tcp6->ai_addr,
653 ai_tcp6->ai_addrlen) < 0) {
655 "can't bind tcp6 addr %s: %m",
659 if (listen(tcp6sock, -1) < 0) {
660 syslog(LOG_ERR, "listen failed");
663 freeaddrinfo(ai_tcp6);
664 FD_SET(tcp6sock, &sockbits);
665 FD_SET(tcp6sock, &v6bits);
666 if (maxsock < tcp6sock)
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);
679 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
680 gai_strerror(ecode));
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);
695 if (rpcbregcnt == 0) {
696 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
700 if (tcpflag && connect_type_cnt == 0) {
701 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
705 setproctitle("master");
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.
717 * Loop forever accepting connections and passing the sockets
718 * into the kernel for the mounts.
722 if (connect_type_cnt > 1) {
723 if (select(maxsock + 1,
724 &ready, NULL, NULL, NULL) < 1) {
728 syslog(LOG_ERR, "select failed: %m");
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) {
739 syslog(LOG_ERR, "accept failed: %m");
740 if (error == ECONNABORTED ||
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)
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,
763 "accept failed: %m");
764 if (error == ECONNABORTED ||
769 if (setsockopt(msgsock, SOL_SOCKET,
770 SO_KEEPALIVE, (char *)&on,
772 syslog(LOG_ERR, "setsockopt "
774 addsockargs.sock = msgsock;
775 addsockargs.name = (caddr_t)&inet6peer;
776 addsockargs.namelen = len;
777 nfssvc(nfssvc_addsock, &addsockargs);
778 (void)close(msgsock);
786 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
789 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
792 if (bindhost == NULL || strcmp("*", bindhost) == 0)
797 if (hostptr != NULL) {
798 switch (hints.ai_family) {
800 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
801 hints.ai_flags = AI_NUMERICHOST;
803 if (inet_pton(AF_INET6, hostptr,
809 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
810 hints.ai_flags = AI_NUMERICHOST;
812 if (inet_pton(AF_INET, hostptr,
822 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
824 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
825 gai_strerror(ecode));
832 set_nfsdcnt(int proposed)
836 warnx("nfsd count too low %d; reset to %d", proposed,
838 nfsdcnt = DEFNFSDCNT;
839 } else if (proposed > MAXNFSDCNT) {
840 warnx("nfsd count too high %d; truncated to %d", proposed,
842 nfsdcnt = MAXNFSDCNT;
851 (void)fprintf(stderr, "%s", getopt_usage);
856 nonfs(__unused int signo)
858 syslog(LOG_ERR, "missing system call: NFS not available");
862 reapchild(__unused int signo)
867 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
868 for (i = 0; i < nfsdcnt; i++)
869 if (pid == children[i])
877 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
878 (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
879 syslog(LOG_ERR, "rpcb_unset failed");
887 for (i = 0; i < nfsdcnt; i++) {
889 kill(children[i], SIGKILL);
894 * Cleanup master after SIGUSR1.
897 cleanup(__unused int signo)
903 * Cleanup child after SIGUSR1.
906 child_cleanup(__unused int signo)
912 nfsd_exit(int status)
920 get_tuned_nfsdcount(void)
922 int ncpu, error, tuned_nfsdcnt;
925 ncpu_size = sizeof(ncpu);
926 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
928 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
930 tuned_nfsdcnt = DEFNFSDCNT;
932 tuned_nfsdcnt = ncpu * 8;
934 return tuned_nfsdcnt;
938 start_server(int master)
940 char principal[MAXHOSTNAMELEN + 5];
941 struct nfsd_nfsd_args nfsdargs;
943 char hostname[MAXHOSTNAMELEN + 1], *cp;
944 struct addrinfo *aip, hints;
947 gethostname(hostname, sizeof (hostname));
948 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
949 if ((cp = strchr(hostname, '.')) == NULL ||
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);
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);
964 nfsdargs.principal = principal;
967 nfsdargs.minthreads = nfsdargs.maxthreads = nfsdcnt;
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;
974 error = nfssvc(nfssvc_nfsd, &nfsdargs);
975 if (error < 0 && errno == EAUTH) {
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)
982 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
984 error = nfssvc(nfssvc_nfsd, &nfsdargs);
987 syslog(LOG_ERR, "nfssvc: %m");
997 * Open the stable restart file and return the file descriptor for it.
1000 open_stable(int *stable_fdp, int *backup_fdp)
1002 int stable_fd, backup_fd = -1, ret;
1003 struct stat st, backup_st;
1005 /* Open and stat the stable restart file. */
1006 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1008 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1009 if (stable_fd >= 0) {
1010 ret = fstat(stable_fd, &st);
1017 /* Open and stat the backup stable restart file. */
1018 if (stable_fd >= 0) {
1019 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1021 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1023 if (backup_fd >= 0) {
1024 ret = fstat(backup_fd, &backup_st);
1030 if (backup_fd < 0) {
1036 *stable_fdp = stable_fd;
1037 *backup_fdp = backup_fd;
1041 /* Sync up the 2 files, as required. */
1043 copy_stable(stable_fd, backup_fd);
1044 else if (backup_st.st_size > 0)
1045 copy_stable(backup_fd, stable_fd);
1049 * Copy the stable restart file to the backup or vice versa.
1052 copy_stable(int from_fd, int to_fd)
1055 static char buf[1024];
1057 ret = lseek(from_fd, (off_t)0, SEEK_SET);
1059 ret = lseek(to_fd, (off_t)0, SEEK_SET);
1061 ret = ftruncate(to_fd, (off_t)0);
1064 cnt = read(from_fd, buf, 1024);
1066 ret = write(to_fd, buf, cnt);
1069 } while (cnt > 0 && ret >= 0);
1073 syslog(LOG_ERR, "stable restart copy failure: %m");
1077 * Back up the stable restart file when indicated by the kernel.
1080 backup_stable(__unused int signo)
1084 copy_stable(stablefd, backupfd);